File: slerr.c

package info (click to toggle)
mc 1:4.6.0-4.6.1-pre3-3sarge1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 15,224 kB
  • ctags: 11,992
  • sloc: ansic: 93,654; sh: 5,102; perl: 1,961; makefile: 1,107; yacc: 316; cs: 43; sed: 16
file content (182 lines) | stat: -rw-r--r-- 4,425 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/* error handling common to all routines. */
/* Copyright (c) 1992, 1999, 2001, 2002, 2003 John E. Davis
 * This file is part of the S-Lang library.
 *
 * You may distribute under the terms of either the GNU General Public
 * License or the Perl Artistic License.
 */

#include "slinclud.h"

#include "slang.h"
#include "_slang.h"

void (*SLang_VMessage_Hook) (char *, va_list);
void (*SLang_Error_Hook)(char *);
void (*SLang_Exit_Error_Hook)(char *, va_list);
volatile int SLang_Error = 0;
char *SLang_Error_Message;
volatile int SLKeyBoard_Quit = 0;

static char *get_error_string (void)
{
   char *str;

   if (!SLang_Error) SLang_Error = SL_UNKNOWN_ERROR;
   if (SLang_Error_Message != NULL) str = SLang_Error_Message;
   else switch(SLang_Error)
     {
      case SL_NOT_IMPLEMENTED: str = "Not Implemented"; break;
      case SL_APPLICATION_ERROR: str = "Application Error"; break;
      case SL_VARIABLE_UNINITIALIZED: str = "Variable Uninitialized"; break;
      case SL_MALLOC_ERROR : str = "Malloc Error"; break;
      case SL_INTERNAL_ERROR: str = "Internal Error"; break;
      case SL_STACK_OVERFLOW: str = "Stack Overflow"; break;
      case SL_STACK_UNDERFLOW: str = "Stack Underflow"; break;
      case SL_INTRINSIC_ERROR: str = "Intrinsic Error"; break;
      case SL_USER_BREAK: str = "User Break"; break;
      case SL_UNDEFINED_NAME: str = "Undefined Name"; break;
      case SL_SYNTAX_ERROR: str = "Syntax Error"; break;
      case SL_DUPLICATE_DEFINITION: str = "Duplicate Definition"; break;
      case SL_TYPE_MISMATCH: str = "Type Mismatch"; break;
      case SL_READONLY_ERROR: str = "Variable is read-only"; break;
      case SL_DIVIDE_ERROR: str = "Divide by zero"; break;
      case SL_OBJ_NOPEN: str = "Object not opened"; break;
      case SL_OBJ_UNKNOWN: str = "Object unknown"; break;
      case SL_INVALID_PARM: str = "Invalid Parameter"; break;
      case SL_TYPE_UNDEFINED_OP_ERROR:
	str = "Operation not defined for datatype"; break;
      case SL_USER_ERROR:
	str = "User Error"; break;
      case SL_USAGE_ERROR:
	str = "Illegal usage of function";
	break;
      case SL_FLOATING_EXCEPTION:
	str = "Floating Point Exception";
	break;
      case SL_UNKNOWN_ERROR:
      default: str = "Unknown Error Code";
     }

   SLang_Error_Message = NULL;
   return str;
}

void SLang_doerror (char *error)
{
   char *str = NULL;
   char *err;
   char *malloced_err_buf;
   char err_buf [1024];

   malloced_err_buf = NULL;

   if (((SLang_Error == SL_USER_ERROR)
	|| (SLang_Error == SL_USAGE_ERROR))
       && (error != NULL) && (*error != 0))
     err = error;
   else
     {
	char *sle = "S-Lang Error: ";
	unsigned int len = 0;
	char *fmt = "%s%s%s";

	str = get_error_string ();

	if ((error == NULL) || (*error == 0))
	  error = "";
	else if (SLang_Error == SL_UNKNOWN_ERROR)
	  /* Do not display an unknown error message if error is non-NULL */
	  str = "";
	else {
	  fmt = "%s%s: %s";
	  len = 2;	/* ": " */
	}

	len += strlen (sle) + strlen (str) + strlen(error) + 1 /* trailing 0 */;

	err = err_buf;
	if (len > sizeof (err_buf))
	  {
	     if (NULL == (malloced_err_buf = SLmalloc (len)))
	       err = NULL;
	     else
	       err = malloced_err_buf;
	  }

	if (err != NULL) sprintf (err, fmt, sle, str, error);
	else err = "Out of memory";
     }

   if (SLang_Error_Hook == NULL)
     {
	fputs (err, stderr);
	fputs("\r\n", stderr);
	fflush (stderr);
     }
   else
     (*SLang_Error_Hook)(err);

   SLfree (malloced_err_buf);
}

void SLang_verror (int err_code, char *fmt, ...)
{
   va_list ap;
   char err [1024];

   if (err_code == 0) err_code = SL_INTRINSIC_ERROR;
   if (SLang_Error == 0) SLang_Error = err_code;

   if (fmt != NULL)
     {
	va_start(ap, fmt);
	(void) _SLvsnprintf (err, sizeof (err), fmt, ap);
	fmt = err;
	va_end(ap);
     }

   SLang_doerror (fmt);
}

void SLang_exit_error (char *fmt, ...)
{
   va_list ap;

   va_start (ap, fmt);
   if (SLang_Exit_Error_Hook != NULL)
     {
	(*SLang_Exit_Error_Hook) (fmt, ap);
	exit (1);
     }

   if (fmt != NULL)
     {
	vfprintf (stderr, fmt, ap);
	fputs ("\r\n", stderr);
	fflush (stderr);
     }
   va_end (ap);

   exit (1);
}

void SLang_vmessage (char *fmt, ...)
{
   va_list ap;

   if (fmt == NULL)
     return;

   va_start (ap, fmt);

   if (SLang_VMessage_Hook != NULL)
     (*SLang_VMessage_Hook) (fmt, ap);
   else
     {
	vfprintf (stdout, fmt, ap);
	fputs ("\r\n", stdout);
     }

   va_end (ap);
}