Added silc_errno_set_location and silc_errno_location
[runtime.git] / lib / silcutil / silcerrno.h
1 /*
2
3   silcerrno.h
4
5   Author: Pekka Riikonen <priikone@silcnet.org>
6
7   Copyright (C) 2007 - 2008 Pekka Riikonen
8
9   This program is free software; you can redistribute it and/or modify
10   it under the terms of the GNU General Public License as published by
11   the Free Software Foundation; version 2 of the License.
12
13   This program is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18 */
19
20 /****h* silcutil/Errno Interface
21  *
22  * DESCRIPTION
23  *
24  * Error codes and routines for accessing the error codes in case of
25  * error condition.  SILC Runtime toolkit contains a global silc_errno
26  * that contains the error code that occurred.  Each thread has their own
27  * silc_errno.
28  *
29  * Each silc_errno error code can be mapped to a string that can be used
30  * to display the error for user.  Some routines may also provide detailed
31  * reason why the error occurred.  The reason string can be retrieved for
32  * the last error by using silc_errno_reason.
33  *
34  * EXAMPLE
35  *
36  * // Use silc_errno
37  * buf = silc_file_readfile(filename, &buf_len, NULL);
38  * if (buf == NULL) {
39  *   fprintf(stderr, "Error reading file %s: %s (%d)", filename,
40  *           silc_errno_string(silc_errno), silc_errno);
41  *   exit(1);
42  * }
43  *
44  * // Get the detailed reason for the error too
45  * if (silc_some_routine() == FALSE) {
46  *   fprintf(stderr, "%s (%d) (%s)", silc_errno_string(silc_errno),
47  *          silc_errno, silc_errno_reason());
48  *   exit(1);
49  * }
50  *
51  ***/
52
53 #ifndef SILCERRNO_H
54 #define SILCERRNO_H
55
56 /****d* silcutil/SilcResult
57  *
58  * NAME
59  *
60  *    typedef enum { ... } SilcResult;
61  *
62  * DESCRIPTION
63  *
64  *    Error codes.
65  *
66  * SOURCE
67  */
68 typedef enum {
69   SILC_OK                              = 0,   /* Ok, no error */
70
71   /* General errors */
72   SILC_ERR                             = 1,   /* General error */
73   SILC_ERR_OUT_OF_MEMORY               = 2,   /* Out of memory */
74   SILC_ERR_ZERO_ALLOCATION             = 3,   /* Allocation by zero */
75   SILC_ERR_TOO_LARGE_ALLOCATION        = 4,   /* Too large allocation */
76   SILC_ERR_OVERFLOW                    = 5,   /* Would overflow */
77   SILC_ERR_UNDERFLOW                   = 6,   /* Would underflow */
78   SILC_ERR_NOT_SUPPORTED               = 7,   /* Feature not supported */
79   SILC_ERR_NOT_PERMITTED               = 8,   /* Operation not permitted */
80   SILC_ERR_TRY_AGAIN                   = 9,   /* Try again */
81   SILC_ERR_PERMISSION_DENIED           = 10,  /* Permission denied */
82   SILC_ERR_INVALID_ARGUMENT            = 11,  /* Invalid argument */
83   SILC_ERR_BAD_TIME                    = 12,  /* Bad time value */
84   SILC_ERR_TIMEOUT                     = 13,  /* Timeout occurred */
85   SILC_ERR_ASSERT                      = 14,  /* Assertion failed */
86   SILC_ERR_NOT_FOUND                   = 15,  /* Item/entry not found */
87   SILC_ERR_UNKNOWN_CHAR                = 16,  /* Unknown character */
88   SILC_ERR_PROHIBITED_CHAR             = 17,  /* Prohibited character */
89   SILC_ERR_BAD_CHAR_ENCODING           = 18,  /* Bad character encoding */
90   SILC_ERR_UNSUPPORTED_CHAR_ENCODING   = 19,  /* Unsupported char encoding */
91   SILC_ERR_BAD_VERSION                 = 20,  /* Bad/unsupported version */
92   SILC_ERR_BAD_ADDRESS                 = 21,  /* Bad memory address */
93   SILC_ERR_BAD_ENCODING                = 22,  /* Bad data encoding */
94   SILC_ERR_INTERRUPTED                 = 23,  /* Interrupted */
95   SILC_ERR_NOT_VALID                   = 24,  /* Not valid */
96   SILC_ERR_LIMIT                       = 25,  /* Limit reached */
97   SILC_ERR_SYNTAX                      = 26,  /* Syntax error */
98
99   /* File, directory and device errors */
100   SILC_ERR_NO_SUCH_FILE                = 40,  /* No such file */
101   SILC_ERR_ALREADY_EXISTS              = 41,  /* File already exists */
102   SILC_ERR_NOT_DIRECTORY               = 42,  /* Not a directory */
103   SILC_ERR_IS_DIRECTORY                = 43,  /* Is a directory */
104   SILC_ERR_NOT_EMPTY                   = 44,  /* Directory not empty */
105   SILC_ERR_BUSY                        = 45,  /* Device or resource busy */
106   SILC_ERR_NO_SUCH_DEVICE              = 46,  /* No such device */
107   SILC_ERR_NO_SPACE_LEFT               = 47,  /* No space left on device */
108   SILC_ERR_BROKEN_PIPE                 = 48,  /* Broken pipe */
109   SILC_ERR_READ_ONLY                   = 49,  /* Read only */
110   SILC_ERR_IO                          = 50,  /* I/O error */
111   SILC_ERR_BAD_FD                      = 51,  /* Bad file descriptor */
112   SILC_ERR_EOF                         = 52,  /* End of file */
113
114   /* Network errors */
115   SILC_ERR_BAD_IP                      = 70,  /* Bad IP address */
116   SILC_ERR_UNKNOWN_IP                  = 71,  /* Unknown IP address */
117   SILC_ERR_UNKNOWN_HOST                = 72,  /* Unknown host name */
118   SILC_ERR_UNREACHABLE                 = 73,  /* Destination unreachable */
119   SILC_ERR_REFUSED                     = 74,  /* Connection refused */
120   SILC_ERR_ABORTED                     = 75,  /* Connection aborted */
121   SILC_ERR_RESET                       = 76,  /* Connection reset by peer */
122   SILC_ERR_WOULD_BLOCK                 = 77,  /* Would block */
123   SILC_ERR_HOST_DOWN                   = 78,  /* Host is down */
124   SILC_ERR_BAD_SOCKET                  = 79,  /* Bad socket */
125   SILC_ERR_BAD_STREAM                  = 80,  /* Bad stream */
126   SILC_ERR_ADDR_IN_USE                 = 81,  /* Address already in use */
127   SILC_ERR_NET_DOWN                    = 82,  /* Network is down */
128   SILC_ERR_EOS                         = 83,  /* End of stream */
129
130   /* Regular expression errors */
131   SILC_ERR_REGEX_PAREN                 = 100, /* Unmatched parenthesis */
132   SILC_ERR_REGEX_HEX                   = 101, /* Bad hex number */
133   SILC_ERR_REGEX_REG                   = 102, /* Bad register number */
134   SILC_ERR_REGEX_SPECIAL               = 103, /* Unmatched special character */
135   SILC_ERR_REGEX_TOO_COMPLEX           = 104, /* Too complex expression */
136   SILC_ERR_REGEX_OPCODE                = 105, /* Bad regex opcode */
137   SILC_ERR_REGEX_REPEAT                = 106, /* Bad repeat value */
138
139   SILC_ERR_MAX,
140 } SilcResult;
141 /***/
142
143 /****d* silcutil/silc_errno
144  *
145  * NAME
146  *
147  *    SilcResult silc_errno;
148  *
149  * DESCRIPTION
150  *
151  *    Returns the error code of the last error.  To map the error code to a
152  *    string call silc_errno_string.
153  *
154  ***/
155 #define silc_errno silc_get_errno()
156
157 /****f* silcutil/silc_errno_string
158  *
159  * NAME
160  *
161  *    const char *silc_errno_string(SilcResult error);
162  *
163  * DESCRIPTION
164  *
165  *    Returns the string of the error `errno'.  This routine never returns
166  *    NULL.
167  *
168  ***/
169 const char *silc_errno_string(SilcResult error);
170
171 /****d* silcutil/silc_errno_reason
172  *
173  * NAME
174  *
175  *    const char *silc_errno_reason(void);
176  *
177  * DESCRIPTION
178  *
179  *    Returns additional reason string for the last occurred error or ""
180  *    if the additional information is not available.  This routine never
181  *    returns NULL.
182  *
183  ***/
184 const char *silc_errno_reason(void);
185
186 /****d* silcutil/silc_errno_location
187  *
188  * NAME
189  *
190  *    SilcBool silc_errno_location(const char **filename,
191  *                                 SilcUInt32 *current_line,
192  *                                 SilcUInt32 *current_column);
193  *
194  * DESCRIPTION
195  *
196  *    Returns the error location information.  The filename where the error
197  *    occurred is returned to `filename'.  The line and columns where the
198  *    error occurred is returned to `current_line' and `current_column',
199  *    respectively, or 0 is returned if the information is not present.
200  *
201  *    Returns FALSE if the location information is not available.
202  *
203  ***/
204 SilcBool silc_errno_location(const char **filename,
205                              SilcUInt32 *current_line,
206                              SilcUInt32 *current_column);
207
208 /* Low-level routines for the error handling. */
209
210 /****d* silcutil/silc_set_errno
211  *
212  * NAME
213  *
214  *    void silc_set_errno(SilcResult error);
215  *
216  * DESCRIPTION
217  *
218  *    A low level routine to set the error for current thread.
219  *
220  ***/
221 void silc_set_errno(SilcResult error);
222
223 /****d* silcutil/silc_set_errno_nofail
224  *
225  * NAME
226  *
227  *    void silc_set_errno_nofail(SilcResult error);
228  *
229  * DESCRIPTION
230  *
231  *    A low level routine to set the error for current thread.  This call
232  *    itself cannot cause setting of errno.
233  *
234  ***/
235 void silc_set_errno_nofail(SilcResult error);
236
237 /****d* silcutil/silc_set_errno_reason
238  *
239  * NAME
240  *
241  *    void silc_set_errno_reason(SilcResult error, const char *format, ...);
242  *
243  * DESCRIPTION
244  *
245  *    A low level routine to set the error reason for current thread.
246  *
247  ***/
248 void silc_set_errno_reason(SilcResult error, const char *format, ...);
249
250 /****d* silcutil/silc_set_errno_reason_nofail
251  *
252  * NAME
253  *
254  *    void silc_set_errno_reason_nofail(SilcResult error,
255  *                                      const char *format, ...);
256  *
257  * DESCRIPTION
258  *
259  *    A low level routine to set the error reason for current thread.  This
260  *    call itself cannot cause setting of errno.
261  *
262  ***/
263 void silc_set_errno_reason_nofail(SilcResult error, const char *format, ...);
264
265 /****d* silcutil/silc_set_errno_posix
266  *
267  * NAME
268  *
269  *    void silc_set_errno_posix(int error);
270  *
271  * DESCRIPTION
272  *
273  *    A low level routine to set the error for current thread from POSIX
274  *    errno.
275  *
276  ***/
277 void silc_set_errno_posix(int error);
278
279 /****d* silcutil/silc_set_errno_location
280  *
281  * NAME
282  *
283  *    void silc_set_errno_location(const char *filename,
284  *                                 SilcUInt32 current_line_number,
285  *                                 SilcUInt32 current_column_number);
286  *
287  * DESCRIPTION
288  *
289  *    A low level routine to set the error location in a file indicated by
290  *    the `filename'.  The `current_line_number' is the line where the
291  *    error occurred.  The `current_column_number' number if the column in
292  *    the line where the error occurred or 0 if so such information is
293  *    present.  The errno must be set before calling this function.
294  *
295  ***/
296 void silc_set_errno_location(const char *filename,
297                              SilcUInt32 current_line_number,
298                              SilcUInt32 current_column_number);
299
300 /* Return last error */
301 SilcResult silc_get_errno(void);
302
303 #endif /* SILCERRNO_H */