Wed, 31 Dec 2014 06:55:50 +0100
Added tag UPSTREAM_283F7C6 for changeset ca08bd8f51b2
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef prerror_h___ |
michael@0 | 7 | #define prerror_h___ |
michael@0 | 8 | |
michael@0 | 9 | #include "prtypes.h" |
michael@0 | 10 | |
michael@0 | 11 | PR_BEGIN_EXTERN_C |
michael@0 | 12 | |
michael@0 | 13 | typedef PRInt32 PRErrorCode; |
michael@0 | 14 | |
michael@0 | 15 | #define PR_NSPR_ERROR_BASE -6000 |
michael@0 | 16 | |
michael@0 | 17 | #include "prerr.h" |
michael@0 | 18 | |
michael@0 | 19 | /* |
michael@0 | 20 | ** Set error will preserve an error condition within a thread context. |
michael@0 | 21 | ** The values stored are the NSPR (platform independent) translation of |
michael@0 | 22 | ** the error. Also, if available, the platform specific oserror is stored. |
michael@0 | 23 | ** If there is no appropriate OS error number, a zero my be supplied. |
michael@0 | 24 | */ |
michael@0 | 25 | NSPR_API(void) PR_SetError(PRErrorCode errorCode, PRInt32 oserr); |
michael@0 | 26 | |
michael@0 | 27 | /* |
michael@0 | 28 | ** The text value specified may be NULL. If it is not NULL and the text length |
michael@0 | 29 | ** is zero, the string is assumed to be a null terminated C string. Otherwise |
michael@0 | 30 | ** the text is assumed to be the length specified and possibly include NULL |
michael@0 | 31 | ** characters (e.g., a multi-national string). |
michael@0 | 32 | ** |
michael@0 | 33 | ** The text will be copied into to thread structure and remain there |
michael@0 | 34 | ** until the next call to PR_SetError. |
michael@0 | 35 | */ |
michael@0 | 36 | NSPR_API(void) PR_SetErrorText( |
michael@0 | 37 | PRIntn textLength, const char *text); |
michael@0 | 38 | |
michael@0 | 39 | /* |
michael@0 | 40 | ** Return the current threads last set error code. |
michael@0 | 41 | */ |
michael@0 | 42 | NSPR_API(PRErrorCode) PR_GetError(void); |
michael@0 | 43 | |
michael@0 | 44 | /* |
michael@0 | 45 | ** Return the current threads last set os error code. This is used for |
michael@0 | 46 | ** machine specific code that desires the underlying os error. |
michael@0 | 47 | */ |
michael@0 | 48 | NSPR_API(PRInt32) PR_GetOSError(void); |
michael@0 | 49 | |
michael@0 | 50 | /* |
michael@0 | 51 | ** Get the length of the error text. If a zero is returned, then there |
michael@0 | 52 | ** is no text. Otherwise, the value returned is sufficient to contain |
michael@0 | 53 | ** the error text currently available. |
michael@0 | 54 | */ |
michael@0 | 55 | NSPR_API(PRInt32) PR_GetErrorTextLength(void); |
michael@0 | 56 | |
michael@0 | 57 | /* |
michael@0 | 58 | ** Copy the current threads current error text. Then actual number of bytes |
michael@0 | 59 | ** copied is returned as the result. If the result is zero, the 'text' area |
michael@0 | 60 | ** is unaffected. |
michael@0 | 61 | */ |
michael@0 | 62 | NSPR_API(PRInt32) PR_GetErrorText(char *text); |
michael@0 | 63 | |
michael@0 | 64 | |
michael@0 | 65 | /* |
michael@0 | 66 | Copyright (C) 1987, 1988 Student Information Processing Board of the |
michael@0 | 67 | Massachusetts Institute of Technology. |
michael@0 | 68 | |
michael@0 | 69 | Permission to use, copy, modify, and distribute this software and its |
michael@0 | 70 | documentation for any purpose and without fee is hereby granted, provided |
michael@0 | 71 | that the above copyright notice appear in all copies and that both that |
michael@0 | 72 | copyright notice and this permission notice appear in supporting |
michael@0 | 73 | documentation, and that the names of M.I.T. and the M.I.T. S.I.P.B. not be |
michael@0 | 74 | used in advertising or publicity pertaining to distribution of the software |
michael@0 | 75 | without specific, written prior permission. M.I.T. and the M.I.T. S.I.P.B. |
michael@0 | 76 | make no representations about the suitability of this software for any |
michael@0 | 77 | purpose. It is provided "as is" without express or implied warranty. |
michael@0 | 78 | */ |
michael@0 | 79 | |
michael@0 | 80 | |
michael@0 | 81 | /* |
michael@0 | 82 | * NOTE: |
michael@0 | 83 | * The interfaces for error-code-translation described in the rest of |
michael@0 | 84 | * this file are preliminary in the 3.1 release of nspr and are subject |
michael@0 | 85 | * to change in future releases. |
michael@0 | 86 | */ |
michael@0 | 87 | |
michael@0 | 88 | /* |
michael@0 | 89 | ** Description: Localizable error code to string function. |
michael@0 | 90 | ** |
michael@0 | 91 | ** |
michael@0 | 92 | ** NSPR provides a mechanism for converting an error code to a |
michael@0 | 93 | ** descriptive string, in a caller-specified language. |
michael@0 | 94 | ** |
michael@0 | 95 | ** Error codes themselves are 32 bit (signed) integers. Typically, |
michael@0 | 96 | ** the high order 24 bits are an identifier of which error table the |
michael@0 | 97 | ** error code is from, and the low order 8 bits are a sequential error |
michael@0 | 98 | ** number within the table. NSPR supports error tables whose first |
michael@0 | 99 | ** error code is not a multiple of 256, such error code assignments |
michael@0 | 100 | ** should be avoided when possible. |
michael@0 | 101 | ** |
michael@0 | 102 | ** Error table 0 is defined to match the UNIX system call error table |
michael@0 | 103 | ** (sys_errlist); this allows errno values to be used directly in the |
michael@0 | 104 | ** library. Other error table numbers are typically formed by |
michael@0 | 105 | ** compacting together the first four characters of the error table |
michael@0 | 106 | ** name. The mapping between characters in the name and numeric |
michael@0 | 107 | ** values in the error code are defined in a system-independent |
michael@0 | 108 | ** fashion, so that two systems that can pass integral values between |
michael@0 | 109 | ** them can reliably pass error codes without loss of meaning; this |
michael@0 | 110 | ** should work even if the character sets used are not the |
michael@0 | 111 | ** same. (However, if this is to be done, error table 0 should be |
michael@0 | 112 | ** avoided, since the local system call error tables may differ.) |
michael@0 | 113 | ** |
michael@0 | 114 | ** Libraries defining error codes need only provide a table mapping |
michael@0 | 115 | ** error code numbers to names and default English descriptions, |
michael@0 | 116 | ** calling a routine to install the table, making it ``known'' to NSPR |
michael@0 | 117 | ** library. Once installed, a table may not be removed. Any error |
michael@0 | 118 | ** code the library generates can be converted to the corresponding |
michael@0 | 119 | ** error message. There is also a default format for error codes |
michael@0 | 120 | ** accidentally returned before making the table known, which is of |
michael@0 | 121 | ** the form "unknown code foo 32", where "foo" would be the name of |
michael@0 | 122 | ** the table. |
michael@0 | 123 | ** |
michael@0 | 124 | ** Normally, the error code conversion routine only supports the |
michael@0 | 125 | ** languages "i-default" and "en", returning the error-table-provided |
michael@0 | 126 | ** English description for both languages. The application may |
michael@0 | 127 | ** provide a localization plugin, allowing support for additional |
michael@0 | 128 | ** languages. |
michael@0 | 129 | ** |
michael@0 | 130 | **/ |
michael@0 | 131 | |
michael@0 | 132 | /**********************************************************************/ |
michael@0 | 133 | /************************* TYPES AND CONSTANTS ************************/ |
michael@0 | 134 | /**********************************************************************/ |
michael@0 | 135 | |
michael@0 | 136 | /* |
michael@0 | 137 | * PRLanguageCode -- |
michael@0 | 138 | * |
michael@0 | 139 | * NSPR represents a language code as a non-negative integer. |
michael@0 | 140 | * Languages 0 is always "i-default" the language you get without |
michael@0 | 141 | * explicit negotiation. Language 1 is always "en", English |
michael@0 | 142 | * which has been explicitly negotiated. Additional language |
michael@0 | 143 | * codes are defined by an application-provided localization plugin. |
michael@0 | 144 | */ |
michael@0 | 145 | typedef PRUint32 PRLanguageCode; |
michael@0 | 146 | #define PR_LANGUAGE_I_DEFAULT 0 /* i-default, the default language */ |
michael@0 | 147 | #define PR_LANGUAGE_EN 1 /* English, explicitly negotiated */ |
michael@0 | 148 | |
michael@0 | 149 | /* |
michael@0 | 150 | * struct PRErrorMessage -- |
michael@0 | 151 | * |
michael@0 | 152 | * An error message in an error table. |
michael@0 | 153 | */ |
michael@0 | 154 | struct PRErrorMessage { |
michael@0 | 155 | const char * name; /* Macro name for error */ |
michael@0 | 156 | const char * en_text; /* Default English text */ |
michael@0 | 157 | }; |
michael@0 | 158 | |
michael@0 | 159 | /* |
michael@0 | 160 | * struct PRErrorTable -- |
michael@0 | 161 | * |
michael@0 | 162 | * An error table, provided by a library. |
michael@0 | 163 | */ |
michael@0 | 164 | struct PRErrorTable { |
michael@0 | 165 | const struct PRErrorMessage * msgs; /* Array of error information */ |
michael@0 | 166 | const char *name; /* Name of error table source */ |
michael@0 | 167 | PRErrorCode base; /* Error code for first error in table */ |
michael@0 | 168 | int n_msgs; /* Number of codes in table */ |
michael@0 | 169 | }; |
michael@0 | 170 | |
michael@0 | 171 | /* |
michael@0 | 172 | * struct PRErrorCallbackPrivate -- |
michael@0 | 173 | * |
michael@0 | 174 | * A private structure for the localization plugin |
michael@0 | 175 | */ |
michael@0 | 176 | struct PRErrorCallbackPrivate; |
michael@0 | 177 | |
michael@0 | 178 | /* |
michael@0 | 179 | * struct PRErrorCallbackTablePrivate -- |
michael@0 | 180 | * |
michael@0 | 181 | * A data structure under which the localization plugin may store information, |
michael@0 | 182 | * associated with an error table, that is private to itself. |
michael@0 | 183 | */ |
michael@0 | 184 | struct PRErrorCallbackTablePrivate; |
michael@0 | 185 | |
michael@0 | 186 | /* |
michael@0 | 187 | * PRErrorCallbackLookupFn -- |
michael@0 | 188 | * |
michael@0 | 189 | * A function of PRErrorCallbackLookupFn type is a localization |
michael@0 | 190 | * plugin callback which converts an error code into a description |
michael@0 | 191 | * in the requested language. The callback is provided the |
michael@0 | 192 | * appropriate error table, private data for the plugin and the table. |
michael@0 | 193 | * The callback returns the appropriate UTF-8 encoded description, or NULL |
michael@0 | 194 | * if no description can be found. |
michael@0 | 195 | */ |
michael@0 | 196 | typedef const char * |
michael@0 | 197 | PRErrorCallbackLookupFn(PRErrorCode code, PRLanguageCode language, |
michael@0 | 198 | const struct PRErrorTable *table, |
michael@0 | 199 | struct PRErrorCallbackPrivate *cb_private, |
michael@0 | 200 | struct PRErrorCallbackTablePrivate *table_private); |
michael@0 | 201 | |
michael@0 | 202 | /* |
michael@0 | 203 | * PRErrorCallbackNewTableFn -- |
michael@0 | 204 | * |
michael@0 | 205 | * A function PRErrorCallbackNewTableFn type is a localization plugin |
michael@0 | 206 | * callback which is called once with each error table registered |
michael@0 | 207 | * with NSPR. The callback is provided with the error table and |
michael@0 | 208 | * the plugin's private structure. The callback returns any table private |
michael@0 | 209 | * data it wishes to associate with the error table. Does not need to be thread |
michael@0 | 210 | * safe. |
michael@0 | 211 | */ |
michael@0 | 212 | typedef struct PRErrorCallbackTablePrivate * |
michael@0 | 213 | PRErrorCallbackNewTableFn(const struct PRErrorTable *table, |
michael@0 | 214 | struct PRErrorCallbackPrivate *cb_private); |
michael@0 | 215 | |
michael@0 | 216 | /**********************************************************************/ |
michael@0 | 217 | /****************************** FUNCTIONS *****************************/ |
michael@0 | 218 | /**********************************************************************/ |
michael@0 | 219 | |
michael@0 | 220 | /*********************************************************************** |
michael@0 | 221 | ** FUNCTION: PR_ErrorToString |
michael@0 | 222 | ** DESCRIPTION: |
michael@0 | 223 | ** Returns the UTF-8 message for an error code in |
michael@0 | 224 | ** the requested language. May return the message |
michael@0 | 225 | ** in the default language if a translation in the requested |
michael@0 | 226 | ** language is not available. The returned string is |
michael@0 | 227 | ** valid for the duration of the process. Never returns NULL. |
michael@0 | 228 | ** |
michael@0 | 229 | ***********************************************************************/ |
michael@0 | 230 | NSPR_API(const char *) PR_ErrorToString(PRErrorCode code, |
michael@0 | 231 | PRLanguageCode language); |
michael@0 | 232 | |
michael@0 | 233 | |
michael@0 | 234 | /*********************************************************************** |
michael@0 | 235 | ** FUNCTION: PR_ErrorToName |
michael@0 | 236 | ** DESCRIPTION: |
michael@0 | 237 | ** Returns the macro name for an error code, or NULL |
michael@0 | 238 | ** if the error code is not known. The returned string is |
michael@0 | 239 | ** valid for the duration of the process. |
michael@0 | 240 | ** |
michael@0 | 241 | ** Does not work for error table 0, the system error codes. |
michael@0 | 242 | ** |
michael@0 | 243 | ***********************************************************************/ |
michael@0 | 244 | NSPR_API(const char *) PR_ErrorToName(PRErrorCode code); |
michael@0 | 245 | |
michael@0 | 246 | |
michael@0 | 247 | /*********************************************************************** |
michael@0 | 248 | ** FUNCTION: PR_ErrorLanguages |
michael@0 | 249 | ** DESCRIPTION: |
michael@0 | 250 | ** Returns the RFC 1766 language tags for the language |
michael@0 | 251 | ** codes PR_ErrorToString() supports. The returned array is valid |
michael@0 | 252 | ** for the duration of the process. Never returns NULL. The first |
michael@0 | 253 | ** item in the returned array is the language tag for PRLanguageCode 0, |
michael@0 | 254 | ** the second is for PRLanguageCode 1, and so on. The array is terminated |
michael@0 | 255 | ** with a null pointer. |
michael@0 | 256 | ** |
michael@0 | 257 | ***********************************************************************/ |
michael@0 | 258 | NSPR_API(const char * const *) PR_ErrorLanguages(void); |
michael@0 | 259 | |
michael@0 | 260 | |
michael@0 | 261 | /*********************************************************************** |
michael@0 | 262 | ** FUNCTION: PR_ErrorInstallTable |
michael@0 | 263 | ** DESCRIPTION: |
michael@0 | 264 | ** Registers an error table with NSPR. Must be done exactly once per |
michael@0 | 265 | ** table. Memory pointed to by `table' must remain valid for the life |
michael@0 | 266 | ** of the process. |
michael@0 | 267 | ** |
michael@0 | 268 | ** NOT THREAD SAFE! |
michael@0 | 269 | ** |
michael@0 | 270 | ***********************************************************************/ |
michael@0 | 271 | NSPR_API(PRErrorCode) PR_ErrorInstallTable(const struct PRErrorTable *table); |
michael@0 | 272 | |
michael@0 | 273 | |
michael@0 | 274 | /*********************************************************************** |
michael@0 | 275 | ** FUNCTION: PR_ErrorInstallCallback |
michael@0 | 276 | ** DESCRIPTION: |
michael@0 | 277 | ** Registers an error localization plugin with NSPR. May be called |
michael@0 | 278 | ** at most one time. `languages' contains the language codes supported |
michael@0 | 279 | ** by this plugin. Languages 0 and 1 must be "i-default" and "en" |
michael@0 | 280 | ** respectively. `lookup' and `newtable' contain pointers to |
michael@0 | 281 | ** the plugin callback functions. `cb_private' contains any information |
michael@0 | 282 | ** private to the plugin functions. |
michael@0 | 283 | ** |
michael@0 | 284 | ** NOT THREAD SAFE! |
michael@0 | 285 | ** |
michael@0 | 286 | ***********************************************************************/ |
michael@0 | 287 | NSPR_API(void) PR_ErrorInstallCallback(const char * const * languages, |
michael@0 | 288 | PRErrorCallbackLookupFn *lookup, |
michael@0 | 289 | PRErrorCallbackNewTableFn *newtable, |
michael@0 | 290 | struct PRErrorCallbackPrivate *cb_private); |
michael@0 | 291 | |
michael@0 | 292 | PR_END_EXTERN_C |
michael@0 | 293 | |
michael@0 | 294 | #endif /* prerror_h___ */ |