* main.c (print_color_usage): Update list of keywords for file
[midnight-commander.git] / slang / slerr.c
blob43486f4294a42ef1586dc7e98a88eee973a4e4ab
1 /* error handling common to all routines. */
2 /* Copyright (c) 1992, 1999, 2001, 2002 John E. Davis
3 * This file is part of the S-Lang library.
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Perl Artistic License.
7 */
9 #include "slinclud.h"
11 #include "slang.h"
12 #include "_slang.h"
14 void (*SLang_VMessage_Hook) (char *, va_list);
15 void (*SLang_Error_Hook)(char *);
16 void (*SLang_Exit_Error_Hook)(char *, va_list);
17 volatile int SLang_Error = 0;
18 char *SLang_Error_Message;
19 volatile int SLKeyBoard_Quit = 0;
21 static char *get_error_string (void)
23 char *str;
25 if (!SLang_Error) SLang_Error = SL_UNKNOWN_ERROR;
26 if (SLang_Error_Message != NULL) str = SLang_Error_Message;
27 else switch(SLang_Error)
29 case SL_NOT_IMPLEMENTED: str = "Not Implemented"; break;
30 case SL_APPLICATION_ERROR: str = "Application Error"; break;
31 case SL_VARIABLE_UNINITIALIZED: str = "Variable Uninitialized"; break;
32 case SL_MALLOC_ERROR : str = "Malloc Error"; break;
33 case SL_INTERNAL_ERROR: str = "Internal Error"; break;
34 case SL_STACK_OVERFLOW: str = "Stack Overflow"; break;
35 case SL_STACK_UNDERFLOW: str = "Stack Underflow"; break;
36 case SL_INTRINSIC_ERROR: str = "Intrinsic Error"; break;
37 case SL_USER_BREAK: str = "User Break"; break;
38 case SL_UNDEFINED_NAME: str = "Undefined Name"; break;
39 case SL_SYNTAX_ERROR: str = "Syntax Error"; break;
40 case SL_DUPLICATE_DEFINITION: str = "Duplicate Definition"; break;
41 case SL_TYPE_MISMATCH: str = "Type Mismatch"; break;
42 case SL_READONLY_ERROR: str = "Variable is read-only"; break;
43 case SL_DIVIDE_ERROR: str = "Divide by zero"; break;
44 case SL_OBJ_NOPEN: str = "Object not opened"; break;
45 case SL_OBJ_UNKNOWN: str = "Object unknown"; break;
46 case SL_INVALID_PARM: str = "Invalid Parameter"; break;
47 case SL_TYPE_UNDEFINED_OP_ERROR:
48 str = "Operation not defined for datatype"; break;
49 case SL_USER_ERROR:
50 str = "User Error"; break;
51 case SL_USAGE_ERROR:
52 str = "Illegal usage of function";
53 break;
54 case SL_FLOATING_EXCEPTION:
55 str = "Floating Point Exception";
56 break;
57 case SL_UNKNOWN_ERROR:
58 default: str = "Unknown Error Code";
61 SLang_Error_Message = NULL;
62 return str;
65 void SLang_doerror (char *error)
67 char *str = NULL;
68 char *err;
69 char *malloced_err_buf;
70 char err_buf [1024];
72 malloced_err_buf = NULL;
74 if (((SLang_Error == SL_USER_ERROR)
75 || (SLang_Error == SL_USAGE_ERROR))
76 && (error != NULL) && (*error != 0))
77 err = error;
78 else
80 char *sle = "S-Lang Error: ";
81 unsigned int len;
82 char *fmt;
84 str = get_error_string ();
86 fmt = "%s%s%s";
87 if ((error == NULL) || (*error == 0))
88 error = "";
89 else if (SLang_Error == SL_UNKNOWN_ERROR)
90 /* Do not display an unknown error message if error is non-NULL */
91 str = "";
92 else
93 fmt = "%s%s: %s";
95 len = strlen (sle) + strlen (str) + strlen(error) + 1;
97 err = err_buf;
98 if (len >= sizeof (err_buf))
100 if (NULL == (malloced_err_buf = SLmalloc (len)))
101 err = NULL;
102 else
103 err = malloced_err_buf;
106 if (err != NULL) sprintf (err, fmt, sle, str, error);
107 else err = "Out of memory";
110 if (SLang_Error_Hook == NULL)
112 fputs (err, stderr);
113 fputs("\r\n", stderr);
114 fflush (stderr);
116 else
117 (*SLang_Error_Hook)(err);
119 SLfree (malloced_err_buf);
122 void SLang_verror (int err_code, char *fmt, ...)
124 va_list ap;
125 char err [1024];
127 if (err_code == 0) err_code = SL_INTRINSIC_ERROR;
128 if (SLang_Error == 0) SLang_Error = err_code;
130 if (fmt != NULL)
132 va_start(ap, fmt);
133 (void) _SLvsnprintf (err, sizeof (err), fmt, ap);
134 fmt = err;
135 va_end(ap);
138 SLang_doerror (fmt);
141 void SLang_exit_error (char *fmt, ...)
143 va_list ap;
145 va_start (ap, fmt);
146 if (SLang_Exit_Error_Hook != NULL)
148 (*SLang_Exit_Error_Hook) (fmt, ap);
149 exit (1);
152 if (fmt != NULL)
154 vfprintf (stderr, fmt, ap);
155 fputs ("\r\n", stderr);
156 fflush (stderr);
158 va_end (ap);
160 exit (1);
163 void SLang_vmessage (char *fmt, ...)
165 va_list ap;
167 if (fmt == NULL)
168 return;
170 va_start (ap, fmt);
172 if (SLang_VMessage_Hook != NULL)
173 (*SLang_VMessage_Hook) (fmt, ap);
174 else
176 vfprintf (stdout, fmt, ap);
177 fputs ("\r\n", stdout);
180 va_end (ap);