-
Notifications
You must be signed in to change notification settings - Fork 0
/
scan.c
1337 lines (1150 loc) · 33.1 KB
/
scan.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
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
/********************************************
scan.c
copyright 1991,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 "mawk.h"
#include "scan.h"
#include "types_string.h"
#include "field.h"
#include "init.h"
#include "types_int.h"
#include "fin.h"
#include "repl.h"
#include "code.h"
#include <fcntl.h>
#include "files.h"
/* static functions */
static void scan_fillbuff( void );
static void scan_open( void );
static int slow_next( void );
static void eat_comment( void );
static void eat_semi_colon( void );
static double collect_decimal( YYSTYPE * yylval, int, int * );
static int collect_string( YYSTYPE * yylval );
static int collect_RE( YYSTYPE * yylval );
#define COLLECT_DECIMAL( i, ip ) \
collect_decimal( yylval, i, ip )
#define COLLECT_STRING() \
collect_string( yylval )
#define COLLECT_RE() \
collect_RE( yylval )
/*-----------------------------
program file management
*----------------------------*/
const char * pfile_name;
STRING * program_string;
PFILE * pfile_list;
static unsigned char * buffer;
static unsigned char * buffp;
static int program_fd; /* unsigned so it works with 8 bit chars */
static int eof_flag;
/* tmp buffer */
char * string_buff;
char * string_buff_end;
/* on entry p points into string_buff, grow string_buff and
return a new p */
char *
enlarge_string_buff( char * p )
{
const unsigned s32 = 32 * 1024;
size_t len = string_buff_end - string_buff;
size_t nlen = ( len < s32 ) ? 2 * len : len + s32;
size_t pdiff = p - string_buff;
string_buff = (char *)erealloc( string_buff, nlen );
string_buff_end = string_buff + nlen;
return string_buff + pdiff;
}
void
scan_init( const 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 );
buffer[BUFFSZ] = 0;
scan_fillbuff();
}
#ifdef OS2 /* OS/2 "extproc" is similar to #! */
if ( strnicmp( buffp, "extproc ", 8 ) == 0 )
eat_comment();
#endif
EAT_NL_NULL; /* 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( void )
{
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] */
if (posix_space_flag && rs_shadow.type != SEP_MLR)
SCAN_CODE_SET_NL(SC_UNEXPECTED);
else
SCAN_CODE_SET_NL(SC_SPACE);
// TODO: ???????? NOT NEEDED b/c after parse() below has no impact??????????
SCAN_CODE_CLEAN;
// 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( void )
{
unsigned r;
/* size of buffer is BUFFSZ+1 and buffer[BUFFSZ] == 0 */
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( void )
{
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( void )
{
register int c;
while ( ( c = next() ) != '\n' && scan_code_get(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( void )
/* eat one semi-colon on the current line */
{
register int c;
while ( scan_code_get(c = next()) == SC_SPACE )
;
if ( c != ';' )
un_next();
}
void
eat_nl( YYSTYPE * yylval ) /* eat all space including newlines */
{
while ( 1 )
switch ( scan_code_get(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_get(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( YYSTYPE * yylval )
// https://www.gnu.org/software/bison/manual/bison.html#Pure-Calling
// yylex(void)
{
register int c;
token_lineno = lineno;
reswitch:
switch ( scan_code_get(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_get(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 = (void *)&double_zero;
}
else if ( d == 1.0 ) {
yylval->ptr = (void *)&double_one;
}
else {
yylval->ptr = (void *)ZMALLOC( double );
*(double *)yylval->ptr = d;
}
ct_ret( DOUBLE );
}
case SC_DOLLAR: /* '$' */
{
double d;
int flag;
while ( scan_code_get(c = next()) == SC_SPACE )
;
if ( scan_code_get(c) != SC_DIGIT && scan_code_get(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 {
int ival = d_to_int( d );
double dval = (double)ival;
if ( dval != d ) {
compile_error(
"$%g is an invalid field index", d );
/* set it to something valid so error does not propagate,
it will not be executed or da'ed */
ival = 0;
}
yylval->cp = field_ptr( ival );
}
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_get(* 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_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( YYSTYPE * yylval, 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_get(* p++ = next()) != SC_DIGIT ) {
*flag = UNEXPECTED;
yylval->ival = '.';
return 0.0;
}
}
else {
while ( scan_code_get( * p++ = next()) == SC_DIGIT )
;
if ( p[-1] != '.' ) {
un_next();
p--;
}
}
/* get rest of digits after decimal point */
while ( scan_code_get( * 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_get( * 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_get( * 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_get(x) == SC_DIGIT || ( 'A' <= ( x ) && ( x ) <= 'f' && hex_value( x ) ) )
/* process one , two or three octal digits
moving a pointer forward by reference */
static int
octal( 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( char ** start_p )
{
register unsigned char * p = (unsigned char *)*start_p;
register unsigned x;
unsigned t;
if ( scan_code_get(*p) == SC_DIGIT )
x = *p++ - '0';
else
x = hex_value( *p++ );
if ( scan_code_get(*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
return the new length */
size_t
rm_escape( char * s )
{
char * p, *q;
char * t;
int i;
q = p = s;
while ( *p ) {
if ( *p == '\\' ) {
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 q - s;
}
static int
collect_string( YYSTYPE * yylval )
{
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_get( * 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 : {
size_t len = (char *)p - string_buff;
if ( e_flag ) {
len = rm_escape( string_buff );
}
yylval->ptr = new_STRING2( string_buff, len );
}
return STRING_;
}
/* bad character class in an RE */
static void
box_error(
unsigned char * p )
{
*p = 0;
compile_error(
"invalid character class in regular expr /%.10s ..",
string_buff, token_lineno );
mawk_exit( 2 );
}
/* seen [: collect to :] */
static unsigned char *
collect_named_box(
unsigned char * p )
{
unsigned c;
while ( 1 ) {
if ( p > (unsigned char *)string_buff_end - 2 ) {
p = (unsigned char *)enlarge_string_buff( (char *)p );
}
switch ( c = next() ) {
case 0:
case '\n':
box_error( p );
case '\\':
c = next();
if ( c != '\n' )
box_error( p );
break;
case ':':
c = next();
if ( c == ']' ) {
p[0] = ':';
p[1] = ']';
return p + 2;
}
else
box_error( p );
default:
*p++ = c;
break;
}