-
Notifications
You must be signed in to change notification settings - Fork 0
/
error.c
195 lines (165 loc) · 4.17 KB
/
error.c
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
183
184
185
186
187
188
189
190
191
192
193
194
195
/********************************************
error.c
copyright 1991, 1992,2014-2016 Michael D. Brennan
This is a source file for mawk, an implementation of
the AWK programming language.
Mawk is distributed without warranty under the terms of
the GNU General Public License, version 3, 2007.
If you import elements of this code into another product,
you agree to not name that product mawk.
********************************************/
#include <stdarg.h>
#include "mawk.h"
#include "scan.h"
#include "bi_vars.h"
static void rt_where( void );
static const char * type_to_str( int );
/* for run time error messages only */
unsigned rt_nr, rt_fnr;
/* generic error message with a hook into the system error
messages if errnum > 0 */
void
errmsg( int errnum, const char * format, ... )
{
va_list args;
fprintf( stderr, "%s: ", progname );
va_start( args, format );
vfprintf( stderr, format, args );
va_end( args );
if ( errnum > 0 )
fprintf( stderr, " (%s)", strerror( errnum ) );
fprintf( stderr, "\n" );
fflush( stderr );
}
void
compile_error( const char * format, ... )
{
va_list args;
const char * s0;
const char * s1;
/* with multiple program files put program name in
error message */
if ( pfile_name ) {
s0 = pfile_name;
s1 = ": ";
}
else {
s0 = s1 = "";
}
fprintf( stderr, "%s: %s%sline %u: ", progname, s0, s1, token_lineno );
va_start( args, format );
vfprintf( stderr, format, args );
va_end( args );
fprintf( stderr, "\n" );
fflush( stderr );
if ( ++compile_error_count == MAX_COMPILE_ERRORS )
mawk_exit( 2 );
}
void
call_error( unsigned lineno, const char * format, ... )
{
va_list args;
const char * s0 = pfile_name;
const char * s1 = ": ";
if ( !pfile_name ) {
s0 = s1 = "";
}
fprintf( stderr, "%s: %s%sline %u: ", progname, s0, s1, lineno );
va_start( args, format );
vfprintf( stderr, format, args );
va_end( args );
fprintf( stderr, "\n" );
fflush( stderr );
if ( ++compile_error_count == MAX_COMPILE_ERRORS )
mawk_exit( 2 );
}
void
rt_error( const char * format, ... )
{
va_list args;
fprintf( stderr, "%s: run time error: ", progname );
va_start( args, format );
vfprintf( stderr, format, args );
va_end( args );
fputc( '\n', stderr );
rt_where();
mawk_exit( 2 );
}
void
compile_or_rt_error( const char * format, ... )
{
/* up to caller not to exceed this buffer */
char buffer[1024];
va_list args;
va_start( args, format );
vsprintf( buffer, format, args );
if ( mawk_state == EXECUTION ) {
rt_error( buffer );
}
else {
compile_error( buffer );
}
}
void
bozo( const char * s )
{
errmsg( 0, "bozo: %s", s );
mawk_exit( 3 );
}
void
overflow( const char * s, unsigned size )
{
errmsg( 0, "program limit exceeded: %s size=%u", s, size );
mawk_exit( 2 );
}
/* print as much as we know about where a rt error occured */
static void
rt_where( void )
{
if ( FILENAME->type != C_STRING )
cast1_to_s( FILENAME );
fprintf( stderr, "\tFILENAME=\"%s\" FNR=%u NR=%u\n",
string( FILENAME )->str, rt_fnr, rt_nr );
}
/* run time */
void
rt_overflow( const char * s, unsigned size )
{
errmsg( 0, "program limit exceeded: %s size=%u", s, size );
rt_where();
mawk_exit( 2 );
}
static const char *
type_to_str( int type )
{
const char * retval;
switch ( type ) {
case ST_VAR:
retval = "variable";
break;
case ST_ARRAY:
retval = "array";
break;
case ST_FUNCT:
retval = "function";
break;
case ST_LOCAL_VAR:
retval = "local variable";
break;
case ST_LOCAL_ARRAY:
retval = "local array";
break;
default:
bozo( "type_to_str" );
/* not reached */
retval = 0;
}
return retval;
}
/* emit an error message about a type clash */
void
type_error( SYMTAB * p )
{
compile_error( "illegal reference to %s %s",
type_to_str( p->type ), p->name );
}