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