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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
|
/********************************************
error.c
copyright 1991, 1992 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 2, 1991.
********************************************/
/* $Log: error.c,v $
* Revision 1.6 1995/06/06 00:18:22 mike
* change mawk_exit(1) to mawk_exit(2)
*
* Revision 1.5 1994/12/13 00:26:33 mike
* rt_nr and rt_fnr for run-time error messages
*
* Revision 1.4 1994/09/23 00:20:00 mike
* minor bug fix: handle \ in eat_nl()
*
* Revision 1.3 1993/07/17 13:22:49 mike
* indent and general code cleanup
*
* Revision 1.2 1993/07/04 12:51:44 mike
* start on autoconfig changes
*
* Revision 1.1.1.1 1993/07/03 18:58:11 mike
* move source to cvs
*
* Revision 5.3 1993/01/22 14:55:46 mike
* trivial change for unexpected_char()
*
* Revision 5.2 1992/10/02 23:26:04 mike
* using vargs.h
*
* Revision 5.1 1991/12/05 07:55:48 brennan
* 1.1 pre-release
*
*/
#include "mawk.h"
#include "scan.h"
#include "bi_vars.h"
#include "vargs.h"
#ifndef EOF
#define EOF (-1)
#endif
static void PROTO( rt_where, (void) ) ;
static void PROTO( missing, (int, char *, int) ) ;
static char *PROTO( type_to_str, (int) ) ;
#ifdef NO_VFPRINTF
#define vfprintf simple_vfprintf
#endif
/* for run time error messages only */
unsigned rt_nr , rt_fnr ;
static struct token_str {
short token ;
char *str ; } token_str[] = {
EOF , "end of file" ,
NL , "end of line",
SEMI_COLON , ";" ,
LBRACE , "{" ,
RBRACE , "}" ,
SC_FAKE_SEMI_COLON, "}",
LPAREN , "(" ,
RPAREN , ")" ,
LBOX , "[",
RBOX , "]",
QMARK , "?",
COLON , ":",
OR, "||",
AND, "&&",
ASSIGN , "=" ,
ADD_ASG, "+=",
SUB_ASG, "-=",
MUL_ASG, "*=",
DIV_ASG, "/=",
MOD_ASG, "%=",
POW_ASG, "^=",
EQ , "==" ,
NEQ , "!=",
LT, "<" ,
LTE, "<=" ,
GT, ">",
GTE, ">=" ,
MATCH, string_buff,
PLUS , "+" ,
MINUS, "-" ,
MUL , "*" ,
DIV, "/" ,
MOD, "%" ,
POW, "^" ,
NOT, "!" ,
COMMA, "," ,
INC_or_DEC , string_buff ,
DOUBLE , string_buff ,
STRING_ , string_buff ,
ID , string_buff ,
FUNCT_ID , string_buff ,
BUILTIN , string_buff ,
IO_OUT , string_buff ,
IO_IN, "<" ,
PIPE, "|" ,
DOLLAR, "$" ,
FIELD, "$" ,
0, (char *) 0 } ;
/* if paren_cnt >0 and we see one of these, we are missing a ')' */
static int missing_rparen[] =
{ EOF, NL, SEMI_COLON, SC_FAKE_SEMI_COLON, RBRACE, 0 } ;
/* ditto for '}' */
static int missing_rbrace[] =
{ EOF, BEGIN, END , 0 } ;
static void missing( c, n , ln)
int c ;
char *n ;
int ln ;
{ char *s0, *s1 ;
if ( pfile_name )
{ s0 = pfile_name ; s1 = ": " ; }
else s0 = s1 = "" ;
errmsg(0, "%s%sline %u: missing %c near %s" ,s0, s1, ln, c, n) ;
}
void yyerror(s)
char *s ; /* we won't use s as input
(yacc and bison force this).
We will use s for storage to keep lint or the compiler
off our back */
{ struct token_str *p ;
int *ip ;
s = (char *) 0 ;
for ( p = token_str ; p->token ; p++ )
if ( current_token == p->token )
{ s = p->str ; break ; }
if ( ! s ) /* search the keywords */
s = find_kw_str(current_token) ;
if ( s )
{
if ( paren_cnt )
for( ip = missing_rparen ; *ip ; ip++)
if ( *ip == current_token )
{ missing(')', s, token_lineno) ;
paren_cnt = 0 ;
goto done ;
}
if ( brace_cnt )
for( ip = missing_rbrace ; *ip ; ip++)
if ( *ip == current_token )
{ missing('}', s, token_lineno) ;
brace_cnt = 0 ;
goto done ;
}
compile_error("syntax error at or near %s", s) ;
}
else /* special cases */
switch ( current_token )
{
case UNEXPECTED :
unexpected_char() ;
goto done ;
case BAD_DECIMAL :
compile_error(
"syntax error in decimal constant %s",
string_buff ) ;
break ;
case RE :
compile_error(
"syntax error at or near /%s/",
string_buff ) ;
break ;
default :
compile_error("syntax error") ;
break ;
}
return ;
done :
if ( ++compile_error_count == MAX_COMPILE_ERRORS ) mawk_exit(2) ;
}
/* generic error message with a hook into the system error
messages if errnum > 0 */
void errmsg VA_ALIST2(int , errnum, char *, format)
va_list args ;
fprintf(stderr, "%s: " , progname) ;
VA_START2(args, int, errnum, char *, format) ;
vfprintf(stderr, format, args) ;
va_end(args) ;
if ( errnum > 0 ) fprintf(stderr, " (%s)" , strerror(errnum) ) ;
fprintf( stderr, "\n") ;
}
void compile_error VA_ALIST(char *, format)
va_list args ;
char *s0, *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, char *, format) ;
vfprintf(stderr, format, args) ;
va_end(args) ;
fprintf(stderr, "\n") ;
if ( ++compile_error_count == MAX_COMPILE_ERRORS ) mawk_exit(2) ;
}
void rt_error VA_ALIST( char *, format)
va_list args ;
fprintf(stderr, "%s: run time error: " , progname ) ;
VA_START(args, char *, format) ;
vfprintf(stderr, format, args) ;
va_end(args) ;
putc('\n',stderr) ;
rt_where() ;
mawk_exit(2) ;
}
void bozo(s)
char *s ;
{
errmsg(0, "bozo: %s" , s) ;
mawk_exit(3) ;
}
void overflow(s, size)
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()
{
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(s, size)
char *s ; unsigned size ;
{
errmsg(0 , "program limit exceeded: %s size=%u", s, size) ;
rt_where() ;
mawk_exit(2) ;
}
void
unexpected_char()
{ int c = yylval.ival ;
fprintf(stderr, "%s: %u: ", progname, token_lineno) ;
if ( c > ' ' && c < 127 )
fprintf(stderr, "unexpected character '%c'\n" , c) ;
else
fprintf(stderr, "unexpected character 0x%02x\n" , c) ;
}
static char *type_to_str( type )
int type ;
{ 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") ;
}
return retval ;
}
/* emit an error message about a type clash */
void type_error(p)
SYMTAB *p ;
{ compile_error("illegal reference to %s %s",
type_to_str(p->type) , p->name) ;
}
#ifdef NO_VFPRINTF
/* a minimal vfprintf */
int simple_vfprintf( fp, format, argp)
FILE *fp ;
char *format ;
va_list argp ;
{
char *q , *p, *t ;
int l_flag ;
char xbuff[64] ;
q = format ;
xbuff[0] = '%' ;
while ( *q != 0 )
{
if ( *q != '%' )
{
putc(*q, fp) ; q++ ; continue ;
}
/* mark the start with p */
p = ++q ; t = xbuff + 1 ;
if ( *q == '-' ) *t++ = *q++ ;
while ( scan_code[*(unsigned char*)q] == SC_DIGIT ) *t++ = *q++ ;
if ( *q == '.' )
{ *t++ = *q++ ;
while ( scan_code[*(unsigned char*)q] == SC_DIGIT ) *t++ = *q++ ;
}
if ( *q == 'l' ) { l_flag = 1 ; *t++ = *q++ ; }
else l_flag = 0 ;
*t = *q++ ; t[1] = 0 ;
switch( *t )
{
case 'c' :
case 'd' :
case 'o' :
case 'x' :
case 'u' :
if ( l_flag ) fprintf(fp, xbuff, va_arg(argp,long) ) ;
else fprintf(fp, xbuff, va_arg(argp, int)) ;
break ;
case 's' :
fprintf(fp, xbuff, va_arg(argp, char*)) ;
break ;
case 'g' :
case 'f' :
fprintf(fp, xbuff, va_arg(argp, double)) ;
break ;
default:
putc('%', fp) ;
q = p ;
break ;
}
}
return 0 ; /* shut up */
}
#endif /* USE_SIMPLE_VFPRINTF */
|