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