Sync usage with man page.
[netbsd-mini2440.git] / external / bsd / bind / dist / lib / isc / result.c
blobc737afc53ce469f4eb2fe1f1e8d94af076989ab1
1 /* $NetBSD$ */
3 /*
4 * Copyright (C) 2004, 2005, 2007, 2008 Internet Systems Consortium, Inc. ("ISC")
5 * Copyright (C) 1998-2001, 2003 Internet Software Consortium.
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
12 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
13 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
14 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
15 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
16 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17 * PERFORMANCE OF THIS SOFTWARE.
20 /* Id: result.c,v 1.71 2008/09/25 04:02:39 tbox Exp */
22 /*! \file */
24 #include <config.h>
26 #include <stddef.h>
27 #include <stdlib.h>
29 #include <isc/lib.h>
30 #include <isc/msgs.h>
31 #include <isc/mutex.h>
32 #include <isc/once.h>
33 #include <isc/resultclass.h>
34 #include <isc/util.h>
36 typedef struct resulttable {
37 unsigned int base;
38 unsigned int last;
39 const char ** text;
40 isc_msgcat_t * msgcat;
41 int set;
42 ISC_LINK(struct resulttable) link;
43 } resulttable;
45 static const char *text[ISC_R_NRESULTS] = {
46 "success", /*%< 0 */
47 "out of memory", /*%< 1 */
48 "timed out", /*%< 2 */
49 "no available threads", /*%< 3 */
50 "address not available", /*%< 4 */
51 "address in use", /*%< 5 */
52 "permission denied", /*%< 6 */
53 "no pending connections", /*%< 7 */
54 "network unreachable", /*%< 8 */
55 "host unreachable", /*%< 9 */
56 "network down", /*%< 10 */
57 "host down", /*%< 11 */
58 "connection refused", /*%< 12 */
59 "not enough free resources", /*%< 13 */
60 "end of file", /*%< 14 */
61 "socket already bound", /*%< 15 */
62 "reload", /*%< 16 */
63 "lock busy", /*%< 17 */
64 "already exists", /*%< 18 */
65 "ran out of space", /*%< 19 */
66 "operation canceled", /*%< 20 */
67 "socket is not bound", /*%< 21 */
68 "shutting down", /*%< 22 */
69 "not found", /*%< 23 */
70 "unexpected end of input", /*%< 24 */
71 "failure", /*%< 25 */
72 "I/O error", /*%< 26 */
73 "not implemented", /*%< 27 */
74 "unbalanced parentheses", /*%< 28 */
75 "no more", /*%< 29 */
76 "invalid file", /*%< 30 */
77 "bad base64 encoding", /*%< 31 */
78 "unexpected token", /*%< 32 */
79 "quota reached", /*%< 33 */
80 "unexpected error", /*%< 34 */
81 "already running", /*%< 35 */
82 "ignore", /*%< 36 */
83 "address mask not contiguous", /*%< 37 */
84 "file not found", /*%< 38 */
85 "file already exists", /*%< 39 */
86 "socket is not connected", /*%< 40 */
87 "out of range", /*%< 41 */
88 "out of entropy", /*%< 42 */
89 "invalid use of multicast address", /*%< 43 */
90 "not a file", /*%< 44 */
91 "not a directory", /*%< 45 */
92 "queue is full", /*%< 46 */
93 "address family mismatch", /*%< 47 */
94 "address family not supported", /*%< 48 */
95 "bad hex encoding", /*%< 49 */
96 "too many open files", /*%< 50 */
97 "not blocking", /*%< 51 */
98 "unbalanced quotes", /*%< 52 */
99 "operation in progress", /*%< 53 */
100 "connection reset", /*%< 54 */
101 "soft quota reached", /*%< 55 */
102 "not a valid number", /*%< 56 */
103 "disabled", /*%< 57 */
104 "max size", /*%< 58 */
105 "invalid address format", /*%< 59 */
106 "bad base32 encoding", /*%< 60 */
109 #define ISC_RESULT_RESULTSET 2
110 #define ISC_RESULT_UNAVAILABLESET 3
112 static isc_once_t once = ISC_ONCE_INIT;
113 static ISC_LIST(resulttable) tables;
114 static isc_mutex_t lock;
116 static isc_result_t
117 register_table(unsigned int base, unsigned int nresults, const char **text,
118 isc_msgcat_t *msgcat, int set)
120 resulttable *table;
122 REQUIRE(base % ISC_RESULTCLASS_SIZE == 0);
123 REQUIRE(nresults <= ISC_RESULTCLASS_SIZE);
124 REQUIRE(text != NULL);
127 * We use malloc() here because we we want to be able to use
128 * isc_result_totext() even if there is no memory context.
130 table = malloc(sizeof(*table));
131 if (table == NULL)
132 return (ISC_R_NOMEMORY);
133 table->base = base;
134 table->last = base + nresults - 1;
135 table->text = text;
136 table->msgcat = msgcat;
137 table->set = set;
138 ISC_LINK_INIT(table, link);
140 LOCK(&lock);
142 ISC_LIST_APPEND(tables, table, link);
144 UNLOCK(&lock);
146 return (ISC_R_SUCCESS);
149 static void
150 initialize_action(void) {
151 isc_result_t result;
153 RUNTIME_CHECK(isc_mutex_init(&lock) == ISC_R_SUCCESS);
154 ISC_LIST_INIT(tables);
156 result = register_table(ISC_RESULTCLASS_ISC, ISC_R_NRESULTS, text,
157 isc_msgcat, ISC_RESULT_RESULTSET);
158 if (result != ISC_R_SUCCESS)
159 UNEXPECTED_ERROR(__FILE__, __LINE__,
160 "register_table() %s: %u",
161 isc_msgcat_get(isc_msgcat, ISC_MSGSET_GENERAL,
162 ISC_MSG_FAILED, "failed"),
163 result);
166 static void
167 initialize(void) {
168 isc_lib_initmsgcat();
169 RUNTIME_CHECK(isc_once_do(&once, initialize_action) == ISC_R_SUCCESS);
172 const char *
173 isc_result_totext(isc_result_t result) {
174 resulttable *table;
175 const char *text, *default_text;
176 int index;
178 initialize();
180 LOCK(&lock);
182 text = NULL;
183 for (table = ISC_LIST_HEAD(tables);
184 table != NULL;
185 table = ISC_LIST_NEXT(table, link)) {
186 if (result >= table->base && result <= table->last) {
187 index = (int)(result - table->base);
188 default_text = table->text[index];
190 * Note: we use 'index + 1' as the message number
191 * instead of index because isc_msgcat_get() requires
192 * the message number to be > 0.
194 text = isc_msgcat_get(table->msgcat, table->set,
195 index + 1, default_text);
196 break;
199 if (text == NULL)
200 text = isc_msgcat_get(isc_msgcat, ISC_RESULT_UNAVAILABLESET,
201 1, "(result code text not available)");
203 UNLOCK(&lock);
205 return (text);
208 isc_result_t
209 isc_result_register(unsigned int base, unsigned int nresults,
210 const char **text, isc_msgcat_t *msgcat, int set)
212 initialize();
214 return (register_table(base, nresults, text, msgcat, set));