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