1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 
6 
7 
8 /*
9 
10 Copyright 1987, 1988 by the Student Information Processing Board
11     of the Massachusetts Institute of Technology
12 
13 Permission to use, copy, modify, and distribute this software
14 and its documentation for any purpose and without fee is
15 hereby granted, provided that the above copyright notice
16 appear in all copies and that both that copyright notice and
17 this permission notice appear in supporting documentation,
18 and that the names of M.I.T. and the M.I.T. S.I.P.B. not be
19 used in advertising or publicity pertaining to distribution
20 of the software without specific, written prior permission.
21 M.I.T. and the M.I.T. S.I.P.B. make no representations about
22 the suitability of this software for any purpose.  It is
23 provided "as is" without express or implied warranty.
24 
25 */
26 
27 #include <string.h>
28 #include <assert.h>
29 #include <errno.h>
30 #include "prmem.h"
31 #include "prerror.h"
32 
33 #define ERRCODE_RANGE   8   /* # of bits to shift table number */
34 #define BITS_PER_CHAR   6   /* # bits to shift per character in name */
35 
36 #ifdef NEED_SYS_ERRLIST
37 extern char const * const sys_errlist[];
38 extern const int sys_nerr;
39 #endif
40 
41 /* List of error tables */
42 struct PRErrorTableList {
43     struct PRErrorTableList *next;
44     const struct PRErrorTable *table;
45     struct PRErrorCallbackTablePrivate *table_private;
46 };
47 static struct PRErrorTableList * Table_List = (struct PRErrorTableList *) NULL;
48 
49 /* Supported languages */
50 static const char * default_languages[] = { "i-default", "en", 0 };
51 static const char * const * callback_languages = default_languages;
52 
53 /* Callback info */
54 static struct PRErrorCallbackPrivate *callback_private = 0;
55 static PRErrorCallbackLookupFn *callback_lookup = 0;
56 static PRErrorCallbackNewTableFn *callback_newtable = 0;
57 
58 
59 static const char char_set[] =
60     "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_";
61 
62 static const char *
error_table_name(PRErrorCode num)63 error_table_name (PRErrorCode num)
64 {
65     static char buf[6]; /* only used if internal code problems exist */
66 
67     long ch;
68     int i;
69     char *p;
70 
71     /* num = aa aaa abb bbb bcc ccc cdd ddd d?? ??? ??? */
72     p = buf;
73     num >>= ERRCODE_RANGE;
74     /* num = ?? ??? ??? aaa aaa bbb bbb ccc ccc ddd ddd */
75     num &= 077777777;
76     /* num = 00 000 000 aaa aaa bbb bbb ccc ccc ddd ddd */
77     for (i = 4; i >= 0; i--) {
78         ch = (num >> BITS_PER_CHAR * i) & ((1 << BITS_PER_CHAR) - 1);
79         if (ch != 0) {
80             *p++ = char_set[ch-1];
81         }
82     }
83     *p = '\0';
84     return(buf);
85 }
86 
87 PR_IMPLEMENT(const char *)
PR_ErrorToString(PRErrorCode code,PRLanguageCode language)88 PR_ErrorToString(PRErrorCode code, PRLanguageCode language)
89 {
90     /* static buffer only used if code is using inconsistent error message
91      * numbers, so just ignore the possible thread contention
92      */
93     static char buffer[25];
94 
95     const char *msg;
96     int offset;
97     PRErrorCode table_num;
98     struct PRErrorTableList *et;
99     int started = 0;
100     char *cp;
101 
102     for (et = Table_List; et; et = et->next) {
103         if (et->table->base <= code &&
104             et->table->base + et->table->n_msgs > code) {
105             /* This is the right table */
106             if (callback_lookup) {
107                 msg = callback_lookup(code, language, et->table,
108                                       callback_private, et->table_private);
109                 if (msg) {
110                     return msg;
111                 }
112             }
113 
114             return(et->table->msgs[code - et->table->base].en_text);
115         }
116     }
117 
118     if (code >= 0 && code < 256) {
119         return strerror(code);
120     }
121 
122     offset = (int) (code & ((1<<ERRCODE_RANGE)-1));
123     table_num = code - offset;
124     strcpy (buffer, "Unknown code ");
125     if (table_num) {
126         strcat(buffer, error_table_name (table_num));
127         strcat(buffer, " ");
128     }
129     for (cp = buffer; *cp; cp++)
130         ;
131     if (offset >= 100) {
132         *cp++ = (char)('0' + offset / 100);
133         offset %= 100;
134         started++;
135     }
136     if (started || offset >= 10) {
137         *cp++ = (char)('0' + offset / 10);
138         offset %= 10;
139     }
140     *cp++ = (char)('0' + offset);
141     *cp = '\0';
142     return(buffer);
143 }
144 
145 PR_IMPLEMENT(const char *)
PR_ErrorToName(PRErrorCode code)146 PR_ErrorToName(PRErrorCode code)
147 {
148     struct PRErrorTableList *et;
149 
150     for (et = Table_List; et; et = et->next) {
151         if (et->table->base <= code &&
152             et->table->base + et->table->n_msgs > code) {
153             /* This is the right table */
154             return(et->table->msgs[code - et->table->base].name);
155         }
156     }
157 
158     return 0;
159 }
160 
161 PR_IMPLEMENT(const char * const *)
PR_ErrorLanguages(void)162 PR_ErrorLanguages(void)
163 {
164     return callback_languages;
165 }
166 
167 PR_IMPLEMENT(PRErrorCode)
PR_ErrorInstallTable(const struct PRErrorTable * table)168 PR_ErrorInstallTable(const struct PRErrorTable *table)
169 {
170     struct PRErrorTableList * new_et;
171 
172     new_et = (struct PRErrorTableList *)
173              PR_Malloc(sizeof(struct PRErrorTableList));
174     if (!new_et) {
175         return errno;    /* oops */
176     }
177     new_et->table = table;
178     if (callback_newtable) {
179         new_et->table_private = callback_newtable(table, callback_private);
180     } else {
181         new_et->table_private = 0;
182     }
183     new_et->next = Table_List;
184     Table_List = new_et;
185     return 0;
186 }
187 
188 PR_IMPLEMENT(void)
PR_ErrorInstallCallback(const char * const * languages,PRErrorCallbackLookupFn * lookup,PRErrorCallbackNewTableFn * newtable,struct PRErrorCallbackPrivate * cb_private)189 PR_ErrorInstallCallback(const char * const * languages,
190                         PRErrorCallbackLookupFn *lookup,
191                         PRErrorCallbackNewTableFn *newtable,
192                         struct PRErrorCallbackPrivate *cb_private)
193 {
194     struct PRErrorTableList *et;
195 
196     assert(strcmp(languages[0], "i-default") == 0);
197     assert(strcmp(languages[1], "en") == 0);
198 
199     callback_languages = languages;
200     callback_lookup = lookup;
201     callback_newtable = newtable;
202     callback_private = cb_private;
203 
204     if (callback_newtable) {
205         for (et = Table_List; et; et = et->next) {
206             et->table_private = callback_newtable(et->table, callback_private);
207         }
208     }
209 }
210