Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 3 | * Copyright (C)2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #include <pj/errno.h> |
| 20 | #include <pj/string.h> |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 21 | #include <pj/compat/string.h> |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 22 | #include <pj/assert.h> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 23 | |
| 24 | /* Prototype for platform specific error message, which will be defined |
| 25 | * in separate file. |
| 26 | */ |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 27 | PJ_BEGIN_DECL |
| 28 | |
| 29 | PJ_DECL(int) platform_strerror(pj_os_err_type code, |
| 30 | char *buf, pj_size_t bufsize ); |
| 31 | PJ_END_DECL |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 32 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 33 | #define PJLIB_MAX_ERR_MSG_HANDLER 8 |
| 34 | |
| 35 | /* Error message handler. */ |
| 36 | static unsigned err_msg_hnd_cnt; |
| 37 | static struct err_msg_hnd |
| 38 | { |
| 39 | pj_status_t begin; |
| 40 | pj_status_t end; |
| 41 | pj_str_t (*strerror)(pj_status_t, char*, pj_size_t); |
| 42 | |
| 43 | } err_msg_hnd[PJLIB_MAX_ERR_MSG_HANDLER]; |
| 44 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 45 | /* PJLIB's own error codes/messages */ |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 46 | #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0 |
Benny Prijono | 7eaa0fd | 2006-06-22 18:30:13 +0000 | [diff] [blame] | 47 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 48 | static const struct |
| 49 | { |
| 50 | int code; |
| 51 | const char *msg; |
| 52 | } err_str[] = |
| 53 | { |
Benny Prijono | 7eaa0fd | 2006-06-22 18:30:13 +0000 | [diff] [blame] | 54 | PJ_BUILD_ERR(PJ_EUNKNOWN, "Unknown Error" ), |
| 55 | PJ_BUILD_ERR(PJ_EPENDING, "Pending operation" ), |
| 56 | PJ_BUILD_ERR(PJ_ETOOMANYCONN, "Too many connecting sockets" ), |
| 57 | PJ_BUILD_ERR(PJ_EINVAL, "Invalid value or argument" ), |
| 58 | PJ_BUILD_ERR(PJ_ENAMETOOLONG, "Name too long" ), |
| 59 | PJ_BUILD_ERR(PJ_ENOTFOUND, "Not found" ), |
| 60 | PJ_BUILD_ERR(PJ_ENOMEM, "Not enough memory" ), |
| 61 | PJ_BUILD_ERR(PJ_EBUG, "BUG DETECTED!" ), |
| 62 | PJ_BUILD_ERR(PJ_ETIMEDOUT, "Operation timed out" ), |
| 63 | PJ_BUILD_ERR(PJ_ETOOMANY, "Too many objects of the specified type"), |
| 64 | PJ_BUILD_ERR(PJ_EBUSY, "Object is busy"), |
| 65 | PJ_BUILD_ERR(PJ_ENOTSUP, "Option/operation is not supported"), |
| 66 | PJ_BUILD_ERR(PJ_EINVALIDOP, "Invalid operation"), |
| 67 | PJ_BUILD_ERR(PJ_ECANCELLED, "Operation cancelled"), |
| 68 | PJ_BUILD_ERR(PJ_EEXISTS, "Object already exists" ), |
| 69 | PJ_BUILD_ERR(PJ_EEOF, "End of file" ), |
| 70 | PJ_BUILD_ERR(PJ_ETOOBIG, "Size is too big"), |
Benny Prijono | cca3281 | 2006-09-14 21:16:11 +0000 | [diff] [blame] | 71 | PJ_BUILD_ERR(PJ_ERESOLVE, "gethostbyname() has returned error"), |
Benny Prijono | d424f5b | 2006-09-30 11:39:17 +0000 | [diff] [blame] | 72 | PJ_BUILD_ERR(PJ_ETOOSMALL, "Size is too short"), |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 73 | PJ_BUILD_ERR(PJ_EIGNORED, "Ignored"), |
Benny Prijono | 62b86eb | 2007-12-01 08:52:57 +0000 | [diff] [blame] | 74 | PJ_BUILD_ERR(PJ_EIPV6NOTSUP, "IPv6 is not supported"), |
| 75 | PJ_BUILD_ERR(PJ_EAFNOTSUP, "Unsupported address family") |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 76 | }; |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 77 | #endif /* PJ_HAS_ERROR_STRING */ |
| 78 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 79 | |
| 80 | /* |
| 81 | * pjlib_error() |
| 82 | * |
| 83 | * Retrieve message string for PJLIB's own error code. |
| 84 | */ |
| 85 | static int pjlib_error(pj_status_t code, char *buf, pj_size_t size) |
| 86 | { |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 87 | #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0 |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 88 | unsigned i; |
| 89 | |
| 90 | for (i=0; i<sizeof(err_str)/sizeof(err_str[0]); ++i) { |
| 91 | if (err_str[i].code == code) { |
| 92 | pj_size_t len = strlen(err_str[i].msg); |
| 93 | if (len >= size) len = size-1; |
| 94 | pj_memcpy(buf, err_str[i].msg, len); |
| 95 | buf[len] = '\0'; |
| 96 | return len; |
| 97 | } |
| 98 | } |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 99 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 100 | |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 101 | return pj_ansi_snprintf( buf, size, "Unknown pjlib error %d", code); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 104 | #define IN_RANGE(val,start,end) ((val)>=(start) && (val)<(end)) |
| 105 | |
| 106 | /* Register strerror handle. */ |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 107 | PJ_DEF(pj_status_t) pj_register_strerror( pj_status_t start, |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 108 | pj_status_t space, |
Benny Prijono | a7b376b | 2008-01-25 16:06:33 +0000 | [diff] [blame] | 109 | pj_error_callback f) |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 110 | { |
| 111 | unsigned i; |
| 112 | |
| 113 | /* Check arguments. */ |
| 114 | PJ_ASSERT_RETURN(start && space && f, PJ_EINVAL); |
| 115 | |
| 116 | /* Check if there aren't too many handlers registered. */ |
| 117 | PJ_ASSERT_RETURN(err_msg_hnd_cnt < PJ_ARRAY_SIZE(err_msg_hnd), |
| 118 | PJ_ETOOMANY); |
| 119 | |
| 120 | /* Start error must be greater than PJ_ERRNO_START_USER */ |
| 121 | PJ_ASSERT_RETURN(start >= PJ_ERRNO_START_USER, PJ_EEXISTS); |
| 122 | |
| 123 | /* Check that no existing handler has covered the specified range. */ |
| 124 | for (i=0; i<err_msg_hnd_cnt; ++i) { |
| 125 | if (IN_RANGE(start, err_msg_hnd[i].begin, err_msg_hnd[i].end) || |
| 126 | IN_RANGE(start+space-1, err_msg_hnd[i].begin, err_msg_hnd[i].end)) |
| 127 | { |
| 128 | return PJ_EEXISTS; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* Register the handler. */ |
| 133 | err_msg_hnd[err_msg_hnd_cnt].begin = start; |
| 134 | err_msg_hnd[err_msg_hnd_cnt].end = start + space; |
| 135 | err_msg_hnd[err_msg_hnd_cnt].strerror = f; |
| 136 | |
| 137 | ++err_msg_hnd_cnt; |
| 138 | |
| 139 | return PJ_SUCCESS; |
| 140 | } |
| 141 | |
Benny Prijono | f762ee7 | 2006-12-01 11:14:37 +0000 | [diff] [blame] | 142 | /* Internal PJLIB function called by pj_shutdown() to clear error handlers */ |
| 143 | void pj_errno_clear_handlers(void) |
| 144 | { |
| 145 | err_msg_hnd_cnt = 0; |
| 146 | pj_bzero(err_msg_hnd, sizeof(err_msg_hnd)); |
| 147 | } |
| 148 | |
| 149 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 150 | /* |
| 151 | * pj_strerror() |
| 152 | */ |
| 153 | PJ_DEF(pj_str_t) pj_strerror( pj_status_t statcode, |
| 154 | char *buf, pj_size_t bufsize ) |
| 155 | { |
| 156 | int len = -1; |
| 157 | pj_str_t errstr; |
| 158 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 159 | pj_assert(buf && bufsize); |
| 160 | |
Benny Prijono | d0a3585 | 2007-02-23 01:07:54 +0000 | [diff] [blame] | 161 | if (statcode == PJ_SUCCESS) { |
| 162 | len = pj_ansi_snprintf( buf, bufsize, "Success"); |
| 163 | |
| 164 | } else if (statcode < PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) { |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 165 | len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 166 | |
| 167 | } else if (statcode < PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE) { |
| 168 | len = pjlib_error(statcode, buf, bufsize); |
| 169 | |
| 170 | } else if (statcode < PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE) { |
| 171 | len = platform_strerror(PJ_STATUS_TO_OS(statcode), buf, bufsize); |
| 172 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 173 | } else { |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 174 | unsigned i; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 175 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 176 | /* Find user handler to get the error message. */ |
| 177 | for (i=0; i<err_msg_hnd_cnt; ++i) { |
| 178 | if (IN_RANGE(statcode, err_msg_hnd[i].begin, err_msg_hnd[i].end)) { |
| 179 | return (*err_msg_hnd[i].strerror)(statcode, buf, bufsize); |
| 180 | } |
| 181 | } |
Benny Prijono | d424f5b | 2006-09-30 11:39:17 +0000 | [diff] [blame] | 182 | |
| 183 | /* Handler not found! */ |
| 184 | len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | if (len < 1) { |
| 188 | *buf = '\0'; |
| 189 | len = 0; |
| 190 | } |
| 191 | |
| 192 | errstr.ptr = buf; |
| 193 | errstr.slen = len; |
| 194 | |
| 195 | return errstr; |
| 196 | } |
| 197 | |