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 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
|
/********************************************
scan.c
copyright 1991, 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: scan.c,v $
* Revision 1.8 1996/07/28 21:47:05 mike
* gnuish patch
*
* Revision 1.7 1995/06/18 19:42:24 mike
* Remove some redundant declarations and add some prototypes
*
* Revision 1.6 1995/06/10 16:57:52 mike
* silently exit(0) if no program
* always add a '\n' on eof in scan_fillbuff()
*
* Revision 1.5 1995/06/06 00:18:33 mike
* change mawk_exit(1) to mawk_exit(2)
*
* Revision 1.4 1994/09/23 00:20:04 mike
* minor bug fix: handle \ in eat_nl()
*
* Revision 1.3 1993/07/17 00:45:21 mike
* indent
*
* Revision 1.2 1993/07/04 12:52:09 mike
* start on autoconfig changes
*
* Revision 1.1.1.1 1993/07/03 18:58:20 mike
* move source to cvs
*
* Revision 5.6 1993/02/13 21:57:33 mike
* merge patch3
*
* Revision 5.5 1993/01/01 21:30:48 mike
* split new_STRING() into new_STRING and new_STRING0
*
* Revision 5.4.1.1 1993/01/15 03:33:50 mike
* patch3: safer double to int conversion
*
* Revision 5.4 1992/11/29 18:57:50 mike
* field expressions convert to long so 16 bit and 32 bit
* systems behave the same
*
* Revision 5.3 1992/07/08 15:43:41 brennan
* patch2: length returns. I am a wimp
*
* Revision 5.2 1992/02/21 14:16:53 brennan
* fix: getline <=
*
* Revision 5.1 91/12/05 07:56:27 brennan
* 1.1 pre-release
*
*/
#include "mawk.h"
#include "scan.h"
#include "memory.h"
#include "field.h"
#include "init.h"
#include "fin.h"
#include "repl.h"
#include "code.h"
#ifndef NO_FCNTL_H
#include <fcntl.h>
#endif
#include "files.h"
/* static functions */
static void PROTO(scan_fillbuff, (void)) ;
static void PROTO(scan_open, (void)) ;
static int PROTO(slow_next, (void)) ;
static void PROTO(eat_comment, (void)) ;
static void PROTO(eat_semi_colon, (void)) ;
static double PROTO(collect_decimal, (int, int *)) ;
static int PROTO(collect_string, (void)) ;
static int PROTO(collect_RE, (void)) ;
/*-----------------------------
program file management
*----------------------------*/
char *pfile_name ;
STRING *program_string ;
PFILE *pfile_list ;
static unsigned char *buffer ;
static unsigned char *buffp ;
/* unsigned so it works with 8 bit chars */
static int program_fd ;
static int eof_flag ;
void
scan_init(cmdline_program)
char *cmdline_program ;
{
if (cmdline_program)
{
program_fd = -1 ; /* command line program */
program_string = new_STRING0(strlen(cmdline_program) + 1) ;
strcpy(program_string->str, cmdline_program) ;
/* simulate file termination */
program_string->str[program_string->len - 1] = '\n' ;
buffp = (unsigned char *) program_string->str ;
eof_flag = 1 ;
}
else /* program from file[s] */
{
scan_open() ;
buffp = buffer = (unsigned char *) zmalloc(BUFFSZ + 1) ;
scan_fillbuff() ;
}
#ifdef OS2 /* OS/2 "extproc" is similar to #! */
if (strnicmp(buffp, "extproc ", 8) == 0)
eat_comment();
#endif
eat_nl() ; /* scan to first token */
if (next() == 0)
{
/* no program */
mawk_exit(0) ;
}
un_next() ;
}
static void
scan_open() /* open pfile_name */
{
if (pfile_name[0] == '-' && pfile_name[1] == 0)
{
program_fd = 0 ;
}
else if ((program_fd = open(pfile_name, O_RDONLY, 0)) == -1)
{
errmsg(errno, "cannot open %s", pfile_name) ;
mawk_exit(2) ;
}
}
void
scan_cleanup()
{
if (program_fd >= 0) zfree(buffer, BUFFSZ + 1) ;
else free_STRING(program_string) ;
if (program_fd > 0) close(program_fd) ;
/* redefine SPACE as [ \t\n] */
scan_code['\n'] = posix_space_flag && rs_shadow.type != SEP_MLR
? SC_UNEXPECTED : SC_SPACE ;
scan_code['\f'] = SC_UNEXPECTED ; /*value doesn't matter */
scan_code['\013'] = SC_UNEXPECTED ; /* \v not space */
scan_code['\r'] = SC_UNEXPECTED ;
}
/*--------------------------------
global variables shared by yyparse() and yylex()
and used for error messages too
*-------------------------------*/
int current_token = -1 ;
unsigned token_lineno ;
unsigned compile_error_count ;
int NR_flag ; /* are we tracking NR */
int paren_cnt ;
int brace_cnt ;
int print_flag ; /* changes meaning of '>' */
int getline_flag ; /* changes meaning of '<' */
/*----------------------------------------
file reading functions
next() and un_next(c) are macros in scan.h
*---------------------*/
static unsigned lineno = 1 ;
static void
scan_fillbuff()
{
unsigned r ;
r = fillbuff(program_fd, (char *) buffer, BUFFSZ) ;
if (r < BUFFSZ)
{
eof_flag = 1 ;
/* make sure eof is terminated */
buffer[r] = '\n' ;
buffer[r + 1] = 0 ;
}
}
/* read one character -- slowly */
static int
slow_next()
{
while (*buffp == 0)
{
if (!eof_flag)
{
buffp = buffer ;
scan_fillbuff() ;
}
else if (pfile_list /* open another program file */ )
{
PFILE *q ;
if (program_fd > 0) close(program_fd) ;
eof_flag = 0 ;
pfile_name = pfile_list->fname ;
q = pfile_list ;
pfile_list = pfile_list->link ;
ZFREE(q) ;
scan_open() ;
token_lineno = lineno = 1 ;
}
else break /* real eof */ ;
}
return *buffp++ ; /* note can un_next() , eof which is zero */
}
static void
eat_comment()
{
register int c ;
while ((c = next()) != '\n' && scan_code[c]) ;
un_next() ;
}
/* this is how we handle extra semi-colons that are
now allowed to separate pattern-action blocks
A proof that they are useless clutter to the language:
we throw them away
*/
static void
eat_semi_colon()
/* eat one semi-colon on the current line */
{
register int c ;
while (scan_code[c = next()] == SC_SPACE) ;
if (c != ';') un_next() ;
}
void
eat_nl() /* eat all space including newlines */
{
while (1)
switch (scan_code[next()])
{
case SC_COMMENT:
eat_comment() ;
break ;
case SC_NL:
lineno++ ;
/* fall thru */
case SC_SPACE:
break ;
case SC_ESCAPE:
/* bug fix - surprised anyone did this,
a csh user with backslash dyslexia.(Not a joke)
*/
{
unsigned c ;
while (scan_code[c = next()] == SC_SPACE) ;
if (c == '\n')
token_lineno = ++lineno ;
else if (c == 0)
{
un_next() ;
return ;
}
else /* error */
{
un_next() ;
/* can't un_next() twice so deal with it */
yylval.ival = '\\' ;
unexpected_char() ;
if( ++compile_error_count == MAX_COMPILE_ERRORS )
mawk_exit(2) ;
return ;
}
}
break ;
default:
un_next() ;
return ;
}
}
int
yylex()
{
register int c ;
token_lineno = lineno ;
reswitch:
switch (scan_code[c = next()])
{
case 0:
ct_ret(EOF) ;
case SC_SPACE:
goto reswitch ;
case SC_COMMENT:
eat_comment() ;
goto reswitch ;
case SC_NL:
lineno++ ;
eat_nl() ;
ct_ret(NL) ;
case SC_ESCAPE:
while (scan_code[c = next()] == SC_SPACE) ;
if (c == '\n')
{
token_lineno = ++lineno ;
goto reswitch ;
}
if (c == 0) ct_ret(EOF) ;
un_next() ;
yylval.ival = '\\' ;
ct_ret(UNEXPECTED) ;
case SC_SEMI_COLON:
eat_nl() ;
ct_ret(SEMI_COLON) ;
case SC_LBRACE:
eat_nl() ;
brace_cnt++ ;
ct_ret(LBRACE) ;
case SC_PLUS:
switch (next())
{
case '+':
yylval.ival = '+' ;
string_buff[0] =
string_buff[1] = '+' ;
string_buff[2] = 0 ;
ct_ret(INC_or_DEC) ;
case '=':
ct_ret(ADD_ASG) ;
default:
un_next() ;
ct_ret(PLUS) ;
}
case SC_MINUS:
switch (next())
{
case '-':
yylval.ival = '-' ;
string_buff[0] =
string_buff[1] = '-' ;
string_buff[2] = 0 ;
ct_ret(INC_or_DEC) ;
case '=':
ct_ret(SUB_ASG) ;
default:
un_next() ;
ct_ret(MINUS) ;
}
case SC_COMMA:
eat_nl() ;
ct_ret(COMMA) ;
case SC_MUL:
test1_ret('=', MUL_ASG, MUL) ;
case SC_DIV:
{
static int can_precede_div[] =
{DOUBLE, STRING_, RPAREN, ID, D_ID, RE, RBOX, FIELD,
GETLINE, INC_or_DEC, -1} ;
int *p = can_precede_div ;
do
{
if (*p == current_token)
{
if (*p != INC_or_DEC) test1_ret('=', DIV_ASG, DIV) ;
if (next() == '=')
{
un_next() ;
ct_ret(collect_RE()) ;
}
}
}
while (*++p != -1) ;
ct_ret(collect_RE()) ;
}
case SC_MOD:
test1_ret('=', MOD_ASG, MOD) ;
case SC_POW:
test1_ret('=', POW_ASG, POW) ;
case SC_LPAREN:
paren_cnt++ ;
ct_ret(LPAREN) ;
case SC_RPAREN:
if (--paren_cnt < 0)
{
compile_error("extra ')'") ;
paren_cnt = 0 ;
goto reswitch ;
}
ct_ret(RPAREN) ;
case SC_LBOX:
ct_ret(LBOX) ;
case SC_RBOX:
ct_ret(RBOX) ;
case SC_MATCH:
string_buff[0] = '~' ;
string_buff[0] = 0 ;
yylval.ival = 1 ;
ct_ret(MATCH) ;
case SC_EQUAL:
test1_ret('=', EQ, ASSIGN) ;
case SC_NOT: /* ! */
if ((c = next()) == '~')
{
string_buff[0] = '!' ;
string_buff[1] = '~' ;
string_buff[2] = 0 ;
yylval.ival = 0 ;
ct_ret(MATCH) ;
}
else if (c == '=') ct_ret(NEQ) ;
un_next() ;
ct_ret(NOT) ;
case SC_LT: /* '<' */
if (next() == '=') ct_ret(LTE) ;
else un_next() ;
if (getline_flag)
{
getline_flag = 0 ;
ct_ret(IO_IN) ;
}
else ct_ret(LT) ;
case SC_GT: /* '>' */
if (print_flag && paren_cnt == 0)
{
print_flag = 0 ;
/* there are 3 types of IO_OUT
-- build the error string in string_buff */
string_buff[0] = '>' ;
if (next() == '>')
{
yylval.ival = F_APPEND ;
string_buff[1] = '>' ;
string_buff[2] = 0 ;
}
else
{
un_next() ;
yylval.ival = F_TRUNC ;
string_buff[1] = 0 ;
}
return current_token = IO_OUT ;
}
test1_ret('=', GTE, GT) ;
case SC_OR:
if (next() == '|')
{
eat_nl() ;
ct_ret(OR) ;
}
else
{
un_next() ;
if (print_flag && paren_cnt == 0)
{
print_flag = 0 ;
yylval.ival = PIPE_OUT ;
string_buff[0] = '|' ;
string_buff[1] = 0 ;
ct_ret(IO_OUT) ;
}
else ct_ret(PIPE) ;
}
case SC_AND:
if (next() == '&')
{
eat_nl() ;
ct_ret(AND) ;
}
else
{
un_next() ;
yylval.ival = '&' ;
ct_ret(UNEXPECTED) ;
}
case SC_QMARK:
ct_ret(QMARK) ;
case SC_COLON:
ct_ret(COLON) ;
case SC_RBRACE:
if (--brace_cnt < 0)
{
compile_error("extra '}'") ;
eat_semi_colon() ;
brace_cnt = 0 ;
goto reswitch ;
}
if ((c = current_token) == NL || c == SEMI_COLON
|| c == SC_FAKE_SEMI_COLON || c == RBRACE)
{
/* if the brace_cnt is zero , we've completed
a pattern action block. If the user insists
on adding a semi-colon on the same line
we will eat it. Note what we do below:
physical law -- conservation of semi-colons */
if (brace_cnt == 0) eat_semi_colon() ;
eat_nl() ;
ct_ret(RBRACE) ;
}
/* supply missing semi-colon to statement that
precedes a '}' */
brace_cnt++ ;
un_next() ;
current_token = SC_FAKE_SEMI_COLON ;
return SEMI_COLON ;
case SC_DIGIT:
case SC_DOT:
{
double d;
int flag ;
static double double_zero = 0.0 ;
static double double_one = 1.0 ;
if ((d = collect_decimal(c, &flag)) == 0.0)
{
if (flag) ct_ret(flag) ;
else yylval.ptr = (PTR) & double_zero ;
}
else if (d == 1.0)
{
yylval.ptr = (PTR) & double_one ;
}
else
{
yylval.ptr = (PTR) ZMALLOC(double) ;
*(double *) yylval.ptr = d ;
}
ct_ret(DOUBLE) ;
}
case SC_DOLLAR: /* '$' */
{
double d;
int flag ;
while (scan_code[c = next()] == SC_SPACE) ;
if (scan_code[c] != SC_DIGIT &&
scan_code[c] != SC_DOT)
{
un_next() ;
ct_ret(DOLLAR) ;
}
/* compute field address at compile time */
if ((d = collect_decimal(c, &flag)) == 0.0)
{
if (flag) ct_ret(flag) ; /* an error */
else yylval.cp = &field[0] ;
}
else
{
if (d > MAX_FIELD)
{
compile_error(
"$%g exceeds maximum field(%d)", d, MAX_FIELD) ;
d = MAX_FIELD ;
}
yylval.cp = field_ptr((int) d) ;
}
ct_ret(FIELD) ;
}
case SC_DQUOTE:
return current_token = collect_string() ;
case SC_IDCHAR: /* collect an identifier */
{
unsigned char *p =
(unsigned char *) string_buff + 1 ;
SYMTAB *stp ;
string_buff[0] = c ;
while (
(c = scan_code[*p++ = next()]) == SC_IDCHAR ||
c == SC_DIGIT) ;
un_next() ;
*--p = 0 ;
switch ((stp = find(string_buff))->type)
{
case ST_NONE:
/* check for function call before defined */
if (next() == '(')
{
stp->type = ST_FUNCT ;
stp->stval.fbp = (FBLOCK *)
zmalloc(sizeof(FBLOCK)) ;
stp->stval.fbp->name = stp->name ;
stp->stval.fbp->code = (INST *) 0 ;
yylval.fbp = stp->stval.fbp ;
current_token = FUNCT_ID ;
}
else
{
yylval.stp = stp ;
current_token =
current_token == DOLLAR ? D_ID : ID ;
}
un_next() ;
break ;
case ST_NR:
NR_flag = 1 ;
stp->type = ST_VAR ;
/* fall thru */
case ST_VAR:
case ST_ARRAY:
case ST_LOCAL_NONE:
case ST_LOCAL_VAR:
case ST_LOCAL_ARRAY:
yylval.stp = stp ;
current_token =
current_token == DOLLAR ? D_ID : ID ;
break ;
case ST_ENV:
stp->type = ST_ARRAY ;
stp->stval.array = new_ARRAY() ;
load_environ(stp->stval.array) ;
yylval.stp = stp ;
current_token =
current_token == DOLLAR ? D_ID : ID ;
break ;
case ST_FUNCT:
yylval.fbp = stp->stval.fbp ;
current_token = FUNCT_ID ;
break ;
case ST_KEYWORD:
current_token = stp->stval.kw ;
break ;
case ST_BUILTIN:
yylval.bip = stp->stval.bip ;
current_token = BUILTIN ;
break ;
case ST_LENGTH:
yylval.bip = stp->stval.bip ;
/* check for length alone, this is an ugly
hack */
while (scan_code[c = next()] == SC_SPACE) ;
un_next() ;
current_token = c == '(' ? BUILTIN : LENGTH ;
break ;
case ST_FIELD:
yylval.cp = stp->stval.cp ;
current_token = FIELD ;
break ;
default:
bozo("find returned bad st type") ;
}
return current_token ;
}
case SC_UNEXPECTED:
yylval.ival = c & 0xff ;
ct_ret(UNEXPECTED) ;
}
return 0 ; /* never get here make lint happy */
}
/* collect a decimal constant in temp_buff.
Return the value and error conditions by reference */
static double
collect_decimal(c, flag)
int c ;
int *flag ;
{
register unsigned char *p = (unsigned char *) string_buff + 1 ;
unsigned char *endp ;
double d;
*flag = 0 ;
string_buff[0] = c ;
if (c == '.')
{
if (scan_code[*p++ = next()] != SC_DIGIT)
{
*flag = UNEXPECTED ;
yylval.ival = '.' ;
return 0.0 ;
}
}
else
{
while (scan_code[*p++ = next()] == SC_DIGIT) ;
if (p[-1] != '.')
{
un_next() ;
p-- ;
}
}
/* get rest of digits after decimal point */
while (scan_code[*p++ = next()] == SC_DIGIT) ;
/* check for exponent */
if (p[-1] != 'e' && p[-1] != 'E')
{
un_next() ;
*--p = 0 ;
}
else /* get the exponent */
{
if (scan_code[*p = next()] != SC_DIGIT &&
*p != '-' && *p != '+')
{
*++p = 0 ;
*flag = BAD_DECIMAL ;
return 0.0 ;
}
else /* get the rest of the exponent */
{
p++ ;
while (scan_code[*p++ = next()] == SC_DIGIT) ;
un_next() ;
*--p = 0 ;
}
}
errno = 0 ; /* check for overflow/underflow */
d = strtod(string_buff, (char **) &endp) ;
#ifndef STRTOD_UNDERFLOW_ON_ZERO_BUG
if (errno) compile_error("%s : decimal %sflow", string_buff,
d == 0.0 ? "under" : "over") ;
#else /* ! sun4 bug */
if (errno && d != 0.0)
compile_error("%s : decimal overflow", string_buff) ;
#endif
if (endp < p)
{
*flag = BAD_DECIMAL ;
return 0.0 ;
}
return d ;
}
/*---------- process escape characters ---------------*/
static char hex_val['f' - 'A' + 1] =
{
10, 11, 12, 13, 14, 15, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
10, 11, 12, 13, 14, 15} ;
#define isoctal(x) ((x)>='0'&&(x)<='7')
#define hex_value(x) hex_val[(x)-'A']
#define ishex(x) (scan_code[x] == SC_DIGIT ||\
'A' <= (x) && (x) <= 'f' && hex_value(x))
static int PROTO(octal, (char **)) ;
static int PROTO(hex, (char **)) ;
/* process one , two or three octal digits
moving a pointer forward by reference */
static int
octal(start_p)
char **start_p ;
{
register char *p = *start_p ;
register unsigned x ;
x = *p++ - '0' ;
if (isoctal(*p))
{
x = (x << 3) + *p++ - '0' ;
if (isoctal(*p)) x = (x << 3) + *p++ - '0' ;
}
*start_p = p ;
return x & 0xff ;
}
/* process one or two hex digits
moving a pointer forward by reference */
static int
hex(start_p)
char **start_p ;
{
register unsigned char *p = (unsigned char *) *start_p ;
register unsigned x ;
unsigned t ;
if (scan_code[*p] == SC_DIGIT) x = *p++ - '0' ;
else x = hex_value(*p++) ;
if (scan_code[*p] == SC_DIGIT) x = (x << 4) + *p++ - '0' ;
else if ('A' <= *p && *p <= 'f' && (t = hex_value(*p)))
{
x = (x << 4) + t ;
p++ ;
}
*start_p = (char *) p ;
return x ;
}
#define ET_END 9
static struct
{
char in, out ;
}
escape_test[ET_END + 1] =
{
'n', '\n',
't', '\t',
'f', '\f',
'b', '\b',
'r', '\r',
'a', '\07',
'v', '\013',
'\\', '\\',
'\"', '\"',
0, 0
} ;
/* process the escape characters in a string, in place . */
char *
rm_escape(s)
char *s ;
{
register char *p, *q ;
char *t ;
int i ;
q = p = s ;
while (*p)
{
if (*p == '\\')
{
escape_test[ET_END].in = *++p ; /* sentinal */
i = 0 ;
while (escape_test[i].in != *p) i++ ;
if (i != ET_END) /* in table */
{
p++ ;
*q++ = escape_test[i].out ;
}
else if (isoctal(*p))
{
t = p ;
*q++ = octal(&t) ;
p = t ;
}
else if (*p == 'x' && ishex(*(unsigned char *) (p + 1)))
{
t = p + 1 ;
*q++ = hex(&t) ;
p = t ;
}
else if (*p == 0) /* can only happen with command line assign */
*q++ = '\\' ;
else /* not an escape sequence */
{
*q++ = '\\' ;
*q++ = *p++ ;
}
}
else *q++ = *p++ ;
}
*q = 0 ;
return s ;
}
static int
collect_string()
{
register unsigned char *p = (unsigned char *) string_buff ;
int c ;
int e_flag = 0 ; /* on if have an escape char */
while (1)
switch (scan_code[*p++ = next()])
{
case SC_DQUOTE: /* done */
*--p = 0 ;
goto out ;
case SC_NL:
p[-1] = 0 ;
/* fall thru */
case 0: /* unterminated string */
compile_error(
"runaway string constant \"%.10s ...",
string_buff, token_lineno) ;
mawk_exit(2) ;
case SC_ESCAPE:
if ((c = next()) == '\n')
{
p-- ;
lineno++ ;
}
else if (c == 0) un_next() ;
else
{
*p++ = c ;
e_flag = 1 ;
}
break ;
default:
break ;
}
out:
yylval.ptr = (PTR) new_STRING(
e_flag ? rm_escape(string_buff)
: string_buff) ;
return STRING_ ;
}
static int
collect_RE()
{
register unsigned char *p = (unsigned char *) string_buff ;
int c ;
STRING *sval ;
while (1)
switch (scan_code[*p++ = next()])
{
case SC_DIV: /* done */
*--p = 0 ;
goto out ;
case SC_NL:
p[-1] = 0 ;
/* fall thru */
case 0: /* unterminated re */
compile_error(
"runaway regular expression /%.10s ...",
string_buff, token_lineno) ;
mawk_exit(2) ;
case SC_ESCAPE:
switch (c = next())
{
case '/':
p[-1] = '/' ;
break ;
case '\n':
p-- ;
break ;
case 0:
un_next() ;
break ;
default:
*p++ = c ;
break ;
}
break ;
}
out:
/* now we've got the RE, so compile it */
sval = new_STRING(string_buff) ;
yylval.ptr = re_compile(sval) ;
free_STRING(sval) ;
return RE ;
}
|