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.
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)
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;
50 str
= "User Error"; break;
52 str
= "Illegal usage of function";
54 case SL_FLOATING_EXCEPTION
:
55 str
= "Floating Point Exception";
57 case SL_UNKNOWN_ERROR
:
58 default: str
= "Unknown Error Code";
61 SLang_Error_Message
= NULL
;
65 void SLang_doerror (char *error
)
69 char *malloced_err_buf
;
72 malloced_err_buf
= NULL
;
74 if (((SLang_Error
== SL_USER_ERROR
)
75 || (SLang_Error
== SL_USAGE_ERROR
))
76 && (error
!= NULL
) && (*error
!= 0))
80 char *sle
= "S-Lang Error: ";
84 str
= get_error_string ();
87 if ((error
== NULL
) || (*error
== 0))
89 else if (SLang_Error
== SL_UNKNOWN_ERROR
)
90 /* Do not display an unknown error message if error is non-NULL */
95 len
= strlen (sle
) + strlen (str
) + strlen(error
) + 1;
98 if (len
>= sizeof (err_buf
))
100 if (NULL
== (malloced_err_buf
= SLmalloc (len
)))
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
)
113 fputs("\r\n", stderr
);
117 (*SLang_Error_Hook
)(err
);
119 SLfree (malloced_err_buf
);
122 void SLang_verror (int err_code
, char *fmt
, ...)
127 if (err_code
== 0) err_code
= SL_INTRINSIC_ERROR
;
128 if (SLang_Error
== 0) SLang_Error
= err_code
;
133 (void) _SLvsnprintf (err
, sizeof (err
), fmt
, ap
);
141 void SLang_exit_error (char *fmt
, ...)
146 if (SLang_Exit_Error_Hook
!= NULL
)
148 (*SLang_Exit_Error_Hook
) (fmt
, ap
);
154 vfprintf (stderr
, fmt
, ap
);
155 fputs ("\r\n", stderr
);
163 void SLang_vmessage (char *fmt
, ...)
172 if (SLang_VMessage_Hook
!= NULL
)
173 (*SLang_VMessage_Hook
) (fmt
, ap
);
176 vfprintf (stdout
, fmt
, ap
);
177 fputs ("\r\n", stdout
);