Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
| 3 | * Copyright (C)2003-2006 Benny Prijono <benny@prijono.org> |
| 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 | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 21 | #include <pj/assert.h> |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 22 | |
| 23 | /* Prototype for platform specific error message, which will be defined |
| 24 | * in separate file. |
| 25 | */ |
| 26 | extern int platform_strerror( pj_os_err_type code, |
| 27 | char *buf, pj_size_t bufsize ); |
| 28 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 29 | #define PJLIB_MAX_ERR_MSG_HANDLER 8 |
| 30 | |
| 31 | /* Error message handler. */ |
| 32 | static unsigned err_msg_hnd_cnt; |
| 33 | static struct err_msg_hnd |
| 34 | { |
| 35 | pj_status_t begin; |
| 36 | pj_status_t end; |
| 37 | pj_str_t (*strerror)(pj_status_t, char*, pj_size_t); |
| 38 | |
| 39 | } err_msg_hnd[PJLIB_MAX_ERR_MSG_HANDLER]; |
| 40 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 41 | /* PJLIB's own error codes/messages */ |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 42 | #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0 |
Benny Prijono | 7eaa0fd | 2006-06-22 18:30:13 +0000 | [diff] [blame] | 43 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 44 | static const struct |
| 45 | { |
| 46 | int code; |
| 47 | const char *msg; |
| 48 | } err_str[] = |
| 49 | { |
Benny Prijono | 7eaa0fd | 2006-06-22 18:30:13 +0000 | [diff] [blame] | 50 | PJ_BUILD_ERR(PJ_EUNKNOWN, "Unknown Error" ), |
| 51 | PJ_BUILD_ERR(PJ_EPENDING, "Pending operation" ), |
| 52 | PJ_BUILD_ERR(PJ_ETOOMANYCONN, "Too many connecting sockets" ), |
| 53 | PJ_BUILD_ERR(PJ_EINVAL, "Invalid value or argument" ), |
| 54 | PJ_BUILD_ERR(PJ_ENAMETOOLONG, "Name too long" ), |
| 55 | PJ_BUILD_ERR(PJ_ENOTFOUND, "Not found" ), |
| 56 | PJ_BUILD_ERR(PJ_ENOMEM, "Not enough memory" ), |
| 57 | PJ_BUILD_ERR(PJ_EBUG, "BUG DETECTED!" ), |
| 58 | PJ_BUILD_ERR(PJ_ETIMEDOUT, "Operation timed out" ), |
| 59 | PJ_BUILD_ERR(PJ_ETOOMANY, "Too many objects of the specified type"), |
| 60 | PJ_BUILD_ERR(PJ_EBUSY, "Object is busy"), |
| 61 | PJ_BUILD_ERR(PJ_ENOTSUP, "Option/operation is not supported"), |
| 62 | PJ_BUILD_ERR(PJ_EINVALIDOP, "Invalid operation"), |
| 63 | PJ_BUILD_ERR(PJ_ECANCELLED, "Operation cancelled"), |
| 64 | PJ_BUILD_ERR(PJ_EEXISTS, "Object already exists" ), |
| 65 | PJ_BUILD_ERR(PJ_EEOF, "End of file" ), |
| 66 | PJ_BUILD_ERR(PJ_ETOOBIG, "Size is too big"), |
Benny Prijono | cca3281 | 2006-09-14 21:16:11 +0000 | [diff] [blame] | 67 | PJ_BUILD_ERR(PJ_ERESOLVE, "gethostbyname() has returned error"), |
Benny Prijono | d424f5b | 2006-09-30 11:39:17 +0000 | [diff] [blame] | 68 | PJ_BUILD_ERR(PJ_ETOOSMALL, "Size is too short"), |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 69 | }; |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 70 | #endif /* PJ_HAS_ERROR_STRING */ |
| 71 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 72 | |
| 73 | /* |
| 74 | * pjlib_error() |
| 75 | * |
| 76 | * Retrieve message string for PJLIB's own error code. |
| 77 | */ |
| 78 | static int pjlib_error(pj_status_t code, char *buf, pj_size_t size) |
| 79 | { |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 80 | #if defined(PJ_HAS_ERROR_STRING) && PJ_HAS_ERROR_STRING!=0 |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 81 | unsigned i; |
| 82 | |
| 83 | for (i=0; i<sizeof(err_str)/sizeof(err_str[0]); ++i) { |
| 84 | if (err_str[i].code == code) { |
| 85 | pj_size_t len = strlen(err_str[i].msg); |
| 86 | if (len >= size) len = size-1; |
| 87 | pj_memcpy(buf, err_str[i].msg, len); |
| 88 | buf[len] = '\0'; |
| 89 | return len; |
| 90 | } |
| 91 | } |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 92 | #endif |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 93 | |
Benny Prijono | 3ba816e | 2006-03-18 12:26:55 +0000 | [diff] [blame] | 94 | return pj_ansi_snprintf( buf, size, "Unknown pjlib error %d", code); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 97 | #define IN_RANGE(val,start,end) ((val)>=(start) && (val)<(end)) |
| 98 | |
| 99 | /* Register strerror handle. */ |
| 100 | PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start, |
| 101 | pj_status_t space, |
| 102 | pj_str_t (*f)(pj_status_t,char*, |
| 103 | pj_size_t)) |
| 104 | { |
| 105 | unsigned i; |
| 106 | |
| 107 | /* Check arguments. */ |
| 108 | PJ_ASSERT_RETURN(start && space && f, PJ_EINVAL); |
| 109 | |
| 110 | /* Check if there aren't too many handlers registered. */ |
| 111 | PJ_ASSERT_RETURN(err_msg_hnd_cnt < PJ_ARRAY_SIZE(err_msg_hnd), |
| 112 | PJ_ETOOMANY); |
| 113 | |
| 114 | /* Start error must be greater than PJ_ERRNO_START_USER */ |
| 115 | PJ_ASSERT_RETURN(start >= PJ_ERRNO_START_USER, PJ_EEXISTS); |
| 116 | |
| 117 | /* Check that no existing handler has covered the specified range. */ |
| 118 | for (i=0; i<err_msg_hnd_cnt; ++i) { |
| 119 | if (IN_RANGE(start, err_msg_hnd[i].begin, err_msg_hnd[i].end) || |
| 120 | IN_RANGE(start+space-1, err_msg_hnd[i].begin, err_msg_hnd[i].end)) |
| 121 | { |
| 122 | return PJ_EEXISTS; |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | /* Register the handler. */ |
| 127 | err_msg_hnd[err_msg_hnd_cnt].begin = start; |
| 128 | err_msg_hnd[err_msg_hnd_cnt].end = start + space; |
| 129 | err_msg_hnd[err_msg_hnd_cnt].strerror = f; |
| 130 | |
| 131 | ++err_msg_hnd_cnt; |
| 132 | |
| 133 | return PJ_SUCCESS; |
| 134 | } |
| 135 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 136 | /* |
| 137 | * pj_strerror() |
| 138 | */ |
| 139 | PJ_DEF(pj_str_t) pj_strerror( pj_status_t statcode, |
| 140 | char *buf, pj_size_t bufsize ) |
| 141 | { |
| 142 | int len = -1; |
| 143 | pj_str_t errstr; |
| 144 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 145 | pj_assert(buf && bufsize); |
| 146 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 147 | if (statcode < PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) { |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 148 | len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 149 | |
| 150 | } else if (statcode < PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE) { |
| 151 | len = pjlib_error(statcode, buf, bufsize); |
| 152 | |
| 153 | } else if (statcode < PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE) { |
| 154 | len = platform_strerror(PJ_STATUS_TO_OS(statcode), buf, bufsize); |
| 155 | |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 156 | } else { |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 157 | unsigned i; |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 158 | |
Benny Prijono | dcc2952 | 2006-02-02 19:15:03 +0000 | [diff] [blame] | 159 | /* Find user handler to get the error message. */ |
| 160 | for (i=0; i<err_msg_hnd_cnt; ++i) { |
| 161 | if (IN_RANGE(statcode, err_msg_hnd[i].begin, err_msg_hnd[i].end)) { |
| 162 | return (*err_msg_hnd[i].strerror)(statcode, buf, bufsize); |
| 163 | } |
| 164 | } |
Benny Prijono | d424f5b | 2006-09-30 11:39:17 +0000 | [diff] [blame] | 165 | |
| 166 | /* Handler not found! */ |
| 167 | len = pj_ansi_snprintf( buf, bufsize, "Unknown error %d", statcode); |
Benny Prijono | 9033e31 | 2005-11-21 02:08:39 +0000 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | if (len < 1) { |
| 171 | *buf = '\0'; |
| 172 | len = 0; |
| 173 | } |
| 174 | |
| 175 | errstr.ptr = buf; |
| 176 | errstr.slen = len; |
| 177 | |
| 178 | return errstr; |
| 179 | } |
| 180 | |