Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add the new integrated files version (use xbt data structures instead my own data...
[simgrid.git] / tools / tesh2 / src / xerrno.c
1 #include <xerrno.h>\r
2 \r
3 typedef struct s_entry\r
4 {\r
5         const char* name;\r
6         int code;\r
7         const char* string;\r
8 }entry_t;\r
9 \r
10 \r
11 static const\r
12 entry_t err[] =\r
13 {\r
14         \r
15         #ifdef E2BIG\r
16         {"E2BIG", E2BIG, "Argument list too long."},\r
17         #endif\r
18         \r
19         #ifdef EACCES\r
20         {"EACCES", EACCES, "Permission denied."},\r
21         #endif\r
22         \r
23         #ifdef EADDRINUSE\r
24         {"EADDRINUSE", EADDRINUSE, "Address in use."},\r
25         #endif\r
26         \r
27         #ifdef EADDRNOTAVAIL\r
28         {"EADDRNOTAVAIL", EADDRNOTAVAIL, "Address not available."},\r
29         #endif\r
30         \r
31         #ifdef EAFNOSUPPORT\r
32         {"EAFNOSUPPORT", EAFNOSUPPORT, "Address family not supported."},\r
33         #endif\r
34         \r
35         #ifdef EAGAIN\r
36         {"EAGAIN", EAGAIN, "Resource unavailable, try again."},\r
37         #endif\r
38         \r
39         #ifdef EALREADY\r
40         {"EALREADY", EALREADY, "Connection already in progress."},\r
41         #endif\r
42         \r
43         #ifdef EBADF\r
44         {"EBADF", EBADF, "Bad file descriptor."},\r
45         #endif\r
46         \r
47         #ifdef EBADMSG\r
48         {"EBADMSG", EBADMSG, "Bad message."},\r
49         #endif\r
50         \r
51         #ifdef EBUSY\r
52         {"EBUSY", EBUSY, "Device or resource busy."},\r
53         #endif\r
54         \r
55         #ifdef ECANCELED\r
56         {"ECANCELED", ECANCELED, "Operation canceled."},\r
57         #endif\r
58         \r
59         #ifdef ECHILD\r
60         {"ECHILD", ECHILD, "No child processes."},\r
61         #endif\r
62         \r
63         #ifdef ECONNABORTED\r
64         {"ECONNABORTED", ECONNABORTED, "Connection aborted."},\r
65         #endif\r
66         \r
67         #ifdef ECONNREFUSED\r
68         {"ECONNREFUSED", ECONNREFUSED, "Connection refused."},\r
69         #endif\r
70         \r
71         #ifdef ECONNRESET\r
72         {"ECONNRESET", ECONNRESET, "Connection reset."},\r
73         #endif\r
74         \r
75         #ifdef EDEADLK\r
76         {"EDEADLK", EDEADLK, "Resource deadlock would occur."},\r
77         #endif\r
78         \r
79         #ifdef EDESTADDRREQ\r
80         {"EDESTADDRREQ", EDESTADDRREQ, "Destination address required."},\r
81         #endif\r
82         \r
83         #ifdef EDOM\r
84         {"EDOM", EDOM, "Mathematics argument out of domain of function."},\r
85         #endif \r
86         \r
87         #ifdef EEXIST\r
88         {"EEXIST", EEXIST, "File exists."},\r
89         #endif\r
90         \r
91         #ifdef EFAULT\r
92         {"EFAULT", EFAULT, "Bad address."},\r
93         #endif\r
94         \r
95         #ifdef EFBIG\r
96         {"EFBIG", EFBIG, "File too large."},\r
97         #endif\r
98         \r
99         #ifdef EHOSTUNREACH\r
100         {"EHOSTUNREACH", EHOSTUNREACH, "Host is unreachable."},\r
101         #endif\r
102         \r
103         #ifdef EIDRM\r
104         {"EIDRM", EIDRM, "Identifier removed."},\r
105         #endif\r
106         \r
107         #ifdef EILSEQ\r
108         {"EILSEQ", EILSEQ, "Illegal byte sequence."},\r
109         #endif\r
110         \r
111         #ifdef EINPROGRESS\r
112         {"EINPROGRESS", EINPROGRESS, "Operation in progress."},\r
113         #endif\r
114         \r
115         #ifdef EINTR\r
116         {"EINTR", EINTR, "Interrupted function."},\r
117         #endif\r
118         \r
119         #ifdef EINVAL\r
120         {"EINVAL", EINVAL, "Invalid argument."},\r
121         #endif\r
122         \r
123         #ifdef EIO\r
124         {"EIO", EIO, "I/O error."},\r
125         #endif\r
126         \r
127         #ifdef EISCONN\r
128         {"EISCONN", EISCONN, "Socket is connected."},\r
129         #endif\r
130         \r
131         #ifdef EISDIR\r
132         {"EISDIR", EISDIR, "Is a directory."},\r
133         #endif\r
134         \r
135         #ifdef ELOOP\r
136         {"ELOOP", ELOOP, "Too many levels of symbolic links."},\r
137         #endif\r
138         \r
139         #ifdef EMFILE\r
140         {"EMFILE", EMFILE, "Too many open files."},\r
141         #endif\r
142         \r
143         #ifdef EMLINK\r
144         {"EMLINK", EMLINK, "Too many links."},\r
145         #endif\r
146         \r
147         #ifdef EMSGSIZE\r
148         {"EMSGSIZE", EMSGSIZE, "Message too large."},\r
149         #endif\r
150         \r
151         #ifdef ENAMETOOLONG\r
152         {"ENAMETOOLONG", ENAMETOOLONG, "Filename too long."},\r
153         #endif\r
154         \r
155         #ifdef ENETDOWN\r
156         {"ENETDOWN", ENETDOWN, "Network is down."},\r
157         #endif\r
158         \r
159         #ifdef ENETRESET\r
160         {"ENETRESET", ENETRESET, "Connection aborted by network."},\r
161         #endif\r
162         \r
163         #ifdef ENETUNREACH\r
164         {"ENETUNREACH", ENETUNREACH, "Network unreachable."},\r
165         #endif\r
166         \r
167         #ifdef ENFILE\r
168         {"ENFILE", ENFILE, "Too many files open in system."},\r
169         #endif\r
170         \r
171         #ifdef ENOBUFS\r
172         {"ENOBUFS", ENOBUFS, "No buffer space available."},\r
173         #endif\r
174         \r
175         #ifdef ENODATA\r
176         {"ENODATA", ENODATA, "No message is available on the STREAM head read queue."},\r
177         #endif\r
178         \r
179         #ifdef ENODEV\r
180         {"ENODEV", ENODEV, "No such device."},\r
181         #endif\r
182         \r
183         #ifdef ENOENT\r
184         {"ENOENT", ENOENT, "No such file or directory."},\r
185         #endif\r
186         \r
187         #ifdef ENOEXEC\r
188         {"ENOEXEC", ENOEXEC, "Executable file format error."},\r
189         #endif\r
190         \r
191         #ifdef ENOLCK\r
192         {"ENOLCK", ENOLCK, "No locks available."},\r
193         #endif\r
194         \r
195         #ifdef ENOMEM\r
196         {"ENOMEM", ENOMEM, "Not enough space."},\r
197         #endif\r
198         \r
199         #ifdef ENOMSG\r
200         {"ENOMSG", ENOMSG, "No message of the desired type."},\r
201         #endif\r
202         \r
203         #ifdef ENOPROTOOPT\r
204         {"ENOPROTOOPT", ENOPROTOOPT, "Protocol not available."},\r
205         #endif\r
206         \r
207         #ifdef ENOSPC\r
208         {"ENOSPC", ENOSPC, "No space left on device."},\r
209         #endif\r
210         \r
211         #ifdef ENOSR\r
212         {"ENOSR", ENOSR, "No stream resources."},\r
213         #endif\r
214         \r
215         #ifdef ENOSTR\r
216         {"ENOSTR", ENOSTR, "Not a stream."},\r
217         #endif\r
218         \r
219         #ifdef ENOSYS\r
220         {"ENOSYS", ENOSYS, "Function not supported."},\r
221         #endif\r
222         \r
223         #ifdef ENOTCONN\r
224         {"ENOTCONN", ENOTCONN, "The socket is not connected."},\r
225         #endif\r
226         \r
227         #ifdef ENOTDIR\r
228         {"ENOTDIR", ENOTDIR, "Not a directory."},\r
229         #endif\r
230         \r
231         #ifdef ENOTEMPTY\r
232         {"ENOTEMPTY", ENOTEMPTY, "Directory not empty."},\r
233         #endif\r
234         \r
235         #ifdef ENOTSOCK\r
236         {"ENOTSOCK", ENOTSOCK, "Not a socket."},\r
237         #endif\r
238         \r
239         #ifdef ENOTSUP\r
240         {"ENOTSUP", ENOTSUP, "Not supported."},\r
241         #endif\r
242         \r
243         #ifdef ENOTTY\r
244         {"ENOTTY", ENOTTY, "Inappropriate I/O control operation."},\r
245         #endif\r
246         \r
247         #ifdef ENXIO\r
248         {"ENXIO", ENXIO, "No such device or address."},\r
249         #endif\r
250         \r
251         #ifdef EOPNOTSUPP\r
252         {"EOPNOTSUPP", EOPNOTSUPP, "Operation not supported on socket."},\r
253         #endif\r
254         \r
255         #ifdef EOVERFLOW\r
256         {"EOVERFLOW", EOVERFLOW, "Value too large to be stored in data type."},\r
257         #endif\r
258         \r
259         #ifdef EPERM\r
260         {"EPERM", EPERM, "Operation not permitted."},\r
261         #endif\r
262         \r
263         #ifdef EPIPE\r
264         {"EPIPE", EPIPE, "Broken pipe."},\r
265         #endif\r
266         \r
267         #ifdef EPROTO\r
268         {"EPROTO", EPROTO, "Protocol error."},\r
269         #endif\r
270         \r
271         #ifdef EPROTONOSUPPORT\r
272         {"EPROTONOSUPPORT", EPROTONOSUPPORT, "Protocol not supported."},\r
273         #endif\r
274         \r
275         #ifdef EPROTOTYPE\r
276         {"EPROTOTYPE", EPROTOTYPE, "Protocol wrong type for socket."},\r
277         #endif\r
278         \r
279         #ifdef ERANGE\r
280         {"ERANGE", ERANGE, "Result too large."},\r
281         #endif\r
282         \r
283         #ifdef EROFS\r
284         {"EROFS", EROFS, "Read-only file system."},\r
285         #endif\r
286         \r
287         #ifdef ESPIPE\r
288         {"ESPIPE", ESPIPE, "Invalid seek."},\r
289         #endif\r
290         \r
291         #ifdef ESRCH\r
292         {"ESRCH", ESRCH, "No such process."},\r
293         #endif\r
294         \r
295         #ifdef ETIME\r
296         {"ETIME", ETIME, "Stream ioctl() timeout."},\r
297         #endif\r
298         \r
299         #ifdef ETIMEDOUT\r
300         {"ETIMEDOUT", ETIMEDOUT, "Connection timed out."},\r
301         #endif\r
302         \r
303         #ifdef ETXTBSY\r
304         {"ETXTBSY", ETXTBSY, "Text file busy."},\r
305         #endif\r
306         \r
307         #ifdef EWOULDBLOCK\r
308         {"EWOULDBLOCK", EWOULDBLOCK, "Operation would block ."},\r
309         #endif\r
310         \r
311         #ifdef EXDEV\r
312         {"EXDEV", EXDEV, "Cross-device link ."},\r
313         #endif\r
314         \r
315         {"ECMDTIMEDOUT", ECMDTIMEDOUT, "Command timed out"},\r
316         \r
317         {"EEXEC", EEXEC, "can't execute a command"},\r
318         {"EWAIT", EWAIT, "wait function failed"},\r
319         {"ECMDNOTFOUND", ECMDNOTFOUND, "command is not found"},\r
320         {"EEXITCODENOTMATCH", EEXITCODENOTMATCH, "Exit code does not match"},\r
321         {"EOUTPUTNOTMATCH", EOUTPUTNOTMATCH, "Output does not match"},\r
322         {"ESIGNOTMATCH", ESIGNOTMATCH, "Signal does not match"},\r
323         {"EUNXPSIG", EUNXPSIG, "Unexpected signal caught"},\r
324         {"ESIGNOTRECEIPT", ESIGNOTRECEIPT, "Expected signal not receipt"},\r
325         {"EFILENOTFOUND", EFILENOTFOUND, "specified tesh file not found"},\r
326         {"EGETCWD", EGETCWD, "system error : the getcwd() function failed"},\r
327         {"EDIRNOTFOUND", EDIRNOTFOUND, "specified directory not found"},\r
328         {"EPROCCMDLINE", EPROCCMDLINE, "process_command_line() failed : internal error"},\r
329         {"ENOARG", ENOARG, "none optional argument not specified"},\r
330         {"ENOTPOSITIVENUM", ENOTPOSITIVENUM, "argument option not strictly positive"},\r
331         {"ESYNTAX", ESYNTAX, "Syntax error"},\r
332         {"EINVALIDTIMEOUT", EINVALIDTIMEOUT, "timeout value specified by metacommand invalid"},\r
333         {"EINVALIDEXITCODE", EINVALIDEXITCODE, "expected exit code value specified by the metacommand invalid"},\r
334         {"ESIGNOTSUPP", ESIGNOTSUPP, "signal specified by the metacommand not supported (Windows specific)"},\r
335         {"ELEADTIME", ELEADTIME, "lead time"},\r
336         {"EREADMENOTFOUND", EREADMENOTFOUND, "unable to locate the README.txt file"},\r
337         {"EINCLUDENOTFOUND", EINCLUDENOTFOUND, "include file specified by a metacommand is not found"},\r
338         {"ESUFFIXTOOLONG", ESUFFIXTOOLONG, "suffix is too long"},\r
339         {"EFILENOTINSPECDIR", EFILENOTINSPECDIR,"file not found in the specified directories"},\r
340         {"EFILENOTINCURDIR", EFILENOTINCURDIR,"file not found in the current directory"},\r
341         {"EINVCMDLINE", EINVCMDLINE, "invalid command line"},\r
342         {"unkwown", -1, "unknown"}\r
343 };\r
344 \r
345 #include <stdio.h>\r
346 \r
347 #ifdef WIN32\r
348 static char *\r
349 w32error_to_string(DWORD errcode) \r
350 {\r
351         static char buffer[128];\r
352 \r
353         /*\r
354          *  Special code for winsock error handling.\r
355          */\r
356         if (errcode > WSABASEERR) \r
357         {\r
358                 HMODULE hModule = GetModuleHandle("wsock32");\r
359                 \r
360                 if(hModule) \r
361                 {\r
362                         FormatMessage(FORMAT_MESSAGE_FROM_HMODULE,hModule, errcode, LANG_NEUTRAL, buffer, 128, NULL);\r
363                         FreeLibrary(hModule);\r
364                 }\r
365         } \r
366         else \r
367         {\r
368                 /*\r
369                  *  Default system message handling\r
370                  */\r
371         FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, LANG_NEUTRAL, buffer, 128, NULL);\r
372         }\r
373 \r
374     return buffer;\r
375 }\r
376 #endif\r
377 \r
378 const char*\r
379 error_to_string(int errcode)\r
380 {\r
381         int i;\r
382         \r
383         for(i = 0; err[i].code != -1; i++)\r
384                 if(err[i].code == errcode)\r
385                         return err[i].string;\r
386 \r
387         #ifdef WIN32\r
388         return w32error_to_string((DWORD)errcode);\r
389         #else\r
390         return "unknow error";  \r
391         #endif\r
392         \r
393 }\r
394 \r
395 const char*\r
396 error_get_at(int pos, int* code)\r
397 {\r
398         if(pos < 0 || (pos > (sizeof(err)/sizeof(entry_t)) - 2))\r
399         {\r
400                 errno = ERANGE;\r
401                 return NULL;\r
402         }\r
403         \r
404         *code = err[pos].code;\r
405         return err[pos].name;\r
406 }\r
407 \r
408 void\r
409 error_register(const char* reason, int errcode, const char* command, const char* unit)\r
410 {\r
411         xerror_t error;\r
412         \r
413         xbt_os_mutex_acquire(err_mutex);\r
414         \r
415         if(!exit_code)\r
416                 exit_code = errcode;\r
417         \r
418         error = xbt_new0(s_xerror_t, 1);\r
419         \r
420         error->reason = reason;\r
421         error->command = command;\r
422         error->unit = unit;\r
423         error->errcode = errcode;\r
424         \r
425         xbt_dynar_push(errors, &error);\r
426         \r
427         xbt_os_mutex_release(err_mutex);\r
428 }\r