-
Notifications
You must be signed in to change notification settings - Fork 0
/
field.c
693 lines (581 loc) · 16.9 KB
/
field.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
/********************************************
field.c
copyright 1991-1995,2014-16 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.
********************************************/
/* field.c */
#include "mawk.h"
#include "types_int.h"
#include "split.h"
#include "field.h"
#include "init.h"
#include "types_string.h"
#include "scan.h"
#include "bi_vars.h"
#include "repl.h"
#include "regexp.h"
STRING * check_convfmt( const STRING * );
/* initial fields and pseudo fields,
most programs only need these */
CELL field[FBANK_SZ + NUM_PFIELDS];
/* hold banks of more fields if needed */
CELL ** fbankv;
/* fbankv grows in chunks */
#define FBANKV_CHUNK_SIZE 1024
static size_t fbankv_num_chunks;
/* make fbankv big enough to hold field CELL $i
is called with i==0 during initialization
This does not create field CELL $i, it just
makes fbankv big enough to hold the fbank that will hold $i
*/
static void
allocate_fbankv( int i )
{
if ( i == 0 ) {
size_t sz = FBANKV_CHUNK_SIZE * sizeof( CELL * );
fbankv_num_chunks = 1;
fbankv = (CELL **)emalloc( sz );
memset( fbankv, 0, sz );
fbankv[0] = field;
}
else {
size_t u = (size_t)i + 1;
size_t chunks = ( u / ( FBANK_SZ * FBANKV_CHUNK_SIZE ) ) + 1;
if ( chunks > fbankv_num_chunks ) {
size_t old_size = fbankv_num_chunks * FBANKV_CHUNK_SIZE;
size_t new_size = chunks * FBANKV_CHUNK_SIZE;
fbankv = (CELL **)erealloc( fbankv, new_size * sizeof( CELL * ) );
memset( &fbankv[old_size], 0, ( new_size - old_size ) * sizeof( CELL * ) );
fbankv_num_chunks = chunks;
}
}
}
/* max_field created i.e. $max_field exists
as new fields are created max_field grows
*/
static int max_field = FBANK_SZ - 1;
/* The fields $0, $1, ... $max_field are always valid, the
value of nf (below) does not affect validity of the
allocated fields. When a new $0 is read, nf is set to -1
to indicate $0 has not been split, field[1], field[2] ...
field[3] (actually fbankv[i/1024][i%1024]) are unchanged.
So any time a field is assigned or changed, it has to
be cell_destroyed first and this is the only way a field gets
cell_destroyed.
*/
static void build_field0( void );
/* a description of how to split based on RS.
If RS is changed, so is rs_shadow */
SEPARATOR rs_shadow = {
SEP_CHAR, '\n', NULL
};
/* a splitting CELL version of FS */
CELL fs_shadow = {
C_SPACE, 0, 0.0
};
int nf;
/* nf holds the true value of NF. If nf < 0 , then
NF has not been computed, i.e., $0 has not been split
*/
static void
set_rs_shadow( void )
{
CELL c;
STRING * sval;
const char * s;
size_t len;
if ( posix_space_flag && mawk_state == EXECUTION )
SCAN_CODE_SET_NL( SC_UNEXPECTED );
if ( rs_shadow.type == SEP_STR ) {
free_STRING( (STRING *)rs_shadow.ptr );
}
cast_for_split( cellcpy( &c, RS ) );
switch ( c.type ) {
case C_RE:
if ( ( s = is_string_split( c.ptr, &len ) ) ) {
if ( len == 1 ) {
rs_shadow.type = SEP_CHAR;
rs_shadow.c = s[0];
}
else {
rs_shadow.type = SEP_STR;
rs_shadow.ptr = (void *)new_STRING2( s, len );
}
}
else {
rs_shadow.type = SEP_RE;
rs_shadow.ptr = c.ptr;
}
break;
case C_SPACE:
rs_shadow.type = SEP_CHAR;
rs_shadow.c = ' ';
break;
case C_SNULL: /* RS becomes one or more blank lines */
if ( mawk_state == EXECUTION )
SCAN_CODE_SET_NL( SC_SPACE );
rs_shadow.type = SEP_MLR;
sval = new_STRING( "\n\n+" );
rs_shadow.ptr = re_compile( sval );
free_STRING( sval );
break;
case C_STRING:
/*
* Check for special case where we retained the cell as a string,
* bypassing regular-expression compiling.
*/
if ( string( &c )->len == 1 ) {
rs_shadow.type = SEP_CHAR;
rs_shadow.c = string( &c )->str[0];
break;
}
/* FALLTHRU */
default:
bozo( "bad cell in set_rs_shadow" );
}
}
static void
load_pfield( const char * name, CELL * cp )
{
SYMTAB * stp;
stp = insert( name );
stp->type = ST_FIELD;
stp->stval.cp = cp;
}
/* initialize $0 and the pseudo fields */
void
field_init( void )
{
allocate_fbankv( 0 );
field[0].type = C_STRING;
field[0].ptr = (void *)&null_str;
null_str.ref_cnt++;
load_pfield( "NF", NF );
NF->type = C_DOUBLE;
NF->dval = 0.0;
load_pfield( "RS", RS );
RS->type = C_STRING;
RS->ptr = (void *)new_STRING( "\n" );
/* rs_shadow already set */
load_pfield( "FS", FS );
FS->type = C_STRING;
FS->ptr = (void *)new_STRING( " " );
/* fs_shadow is already set */
load_pfield( "OFMT", OFMT );
OFMT->type = C_STRING;
OFMT->ptr = (void *)new_STRING( "%.6g" );
load_pfield( "CONVFMT", CONVFMT );
CONVFMT->type = C_STRING;
CONVFMT->ptr = OFMT->ptr;
string( OFMT )->ref_cnt++;
}
void
set_field0( const char * s, size_t len )
{
cell_destroy( &field[0] );
nf = -1;
if ( len ) {
field[0].type = C_MBSTRN;
field[0].ptr = (void *)new_STRING2( s, len );
}
else {
field[0].type = C_STRING;
field[0].ptr = STRING_dup( the_empty_str );
}
}
/* split field[0] into $1, $2 ... and set NF
*
* Note the current values are valid CELLS and
* have to be destroyed when the new values are loaded.
*/
void
split_field0( void )
{
CELL * cp0;
size_t cnt = 0;
CELL hold0; /* copy field[0] here if not string */
if ( field[0].type < C_STRING ) {
cast1_to_s( cellcpy( &hold0, field + 0 ) );
cp0 = &hold0;
}
else {
cp0 = &field[0];
}
if ( string( cp0 )->len > 0 ) {
switch ( fs_shadow.type ) {
case C_SNULL: /* FS == "" */
cnt = null_split( string( cp0 )->str, string( cp0 )->len );
break;
case C_SPACE:
cnt = space_split( string( cp0 )->str, string( cp0 )->len );
break;
default:
cnt = re_split( string( cp0 )->str, string( cp0 )->len, fs_shadow.ptr );
break;
}
}
/* the above xxx_split() function put the fields in an anonyous
* buffer that will be pulled into the fields with a transer call */
/* we are done with cp0 */
if ( cp0 == &hold0 ) {
free_STRING( string( cp0 ) );
}
nf = (int)cnt;
cell_destroy( NF );
NF->type = C_DOUBLE;
NF->dval = (double)nf;
if ( nf > max_field ) {
slow_field_ptr( nf );
}
/* fields 1 .. nf are created and valid */
/* retrieves the result of xxx_split() */
if ( cnt > 0 ) {
transfer_to_fields( cnt );
}
}
/*
assign CELL *cp to field or pseudo field
and take care of all side effects
*/
void
field_assign( CELL * fp, CELL * cp )
{
CELL c;
int i, j;
/* the most common case first */
if ( fp == field ) {
cell_destroy( field );
cellcpy( fp, cp );
nf = -1;
return;
}
/* its not important to do any of this fast */
if ( nf < 0 ) {
split_field0();
}
switch ( i = (int)( fp - field ) ) {
case NF_field:
cell_destroy( NF );
cellcpy( NF, cellcpy( &c, cp ) );
if ( c.type != C_DOUBLE )
cast1_to_d( &c );
if ( ( j = d_to_int( c.dval ) ) < 0 )
rt_error( "negative value assigned to NF" );
if ( j > nf )
for ( i = nf + 1; i <= j; i++ ) {
cp = field_ptr( i );
cell_destroy( cp );
cp->type = C_STRING;
cp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
nf = j;
build_field0();
break;
case RS_field:
cell_destroy( RS );
cellcpy( RS, cp );
set_rs_shadow();
break;
case FS_field:
cell_destroy( FS );
cast_for_split( cellcpy( &fs_shadow, cellcpy( FS, cp ) ) );
break;
case OFMT_field:
case CONVFMT_field:
/* If the user does something stupid with OFMT or CONVFMT,
we could crash.
We make such assignments a run-time error.
This is new behavior.
*/
free_STRING( string( fp ) );
cellcpy( fp, cp );
if ( fp->type == C_MBSTRN ) {
fp->type = C_STRING;
}
if ( fp->type != C_STRING ) {
rt_error( "numeric assignment to %s",
fp == CONVFMT ? "CONVFMT" : "OFMT" );
}
{
STRING * check = check_convfmt( string( fp ) );
if ( !check ) {
rt_error( "invalid format \"%s\" assigned to %s",
string( fp )->str, fp == CONVFMT ? "CONVFMT" : "OFMT" );
}
free_STRING( string( fp ) );
fp->ptr = check;
}
break;
default: /* $1 or $2 or ... */
cell_destroy( fp );
cellcpy( fp, cp );
if ( i < 0 || i >= FBANK_SZ ) {
/* field assigned to was not in field[0..FBANK_SZ-1]
* or a pseudo field, so compute actual field index
*/
i = field_addr_to_index( fp );
}
if ( i > nf ) {
for ( j = nf + 1; j < i; j++ ) {
cp = field_ptr( j );
cell_destroy( cp );
cp->type = C_STRING;
cp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
nf = i;
cell_destroy( NF );
NF->type = C_DOUBLE;
NF->dval = (double)i;
}
build_field0();
}
}
/* construct field[0] from the other fields */
static void
build_field0( void )
{
#ifdef DEBUG
if ( nf < 0 )
bozo( "nf <0 in build_field0" );
#endif
cell_destroy( field + 0 );
if ( nf == 0 ) {
field[0].type = C_STRING;
field[0].ptr = (void *)&null_str;
null_str.ref_cnt++;
}
else if ( nf == 1 ) {
cellcpy( field, field + 1 );
}
else {
CELL c;
STRING * ofs, *tail;
size_t len;
register CELL * cp;
register char * p, *q;
int cnt;
CELL ** fbp, *cp_limit;
cast1_to_s( cellcpy( &c, OFS ) );
ofs = (STRING *)c.ptr;
cast1_to_s( cellcpy( &c, field_ptr( nf ) ) );
tail = (STRING *)c.ptr;
cnt = nf - 1;
len = ( (size_t)cnt ) * ofs->len + tail->len;
fbp = fbankv;
cp_limit = field + FBANK_SZ;
cp = field + 1;
while ( cnt-- > 0 ) {
if ( cp->type < C_STRING ) { /* use the string field temporarily */
if ( cp->type == C_NOINIT ) {
cp->ptr = (void *)&null_str;
null_str.ref_cnt++;
}
else { /* its a double */
CELL tmp;
tmp.type = C_DOUBLE;
tmp.dval = cp->dval;
cast1_to_s( &tmp );
cp->ptr = tmp.ptr;
}
}
len += string( cp )->len;
if ( ++cp == cp_limit ) {
cp = *++fbp;
cp_limit = cp + FBANK_SZ;
}
}
field[0].type = C_STRING;
field[0].ptr = (void *)new_STRING0( len );
p = string( field )->str;
/* walk it again , putting things together */
cnt = nf - 1;
fbp = fbankv;
cp = field + 1;
cp_limit = field + FBANK_SZ;
while ( cnt-- > 0 ) {
memcpy( p, string( cp )->str, string( cp )->len );
p += string( cp )->len;
/* if not really string, free temp use of ptr */
if ( cp->type < C_STRING ) {
free_STRING( string( cp ) );
}
if ( ++cp == cp_limit ) {
cp = *++fbp;
cp_limit = cp + FBANK_SZ;
}
/* add the separator */
q = ofs->str;
while ( *q )
*p++ = *q++;
}
/* tack tail on the end */
memcpy( p, tail->str, tail->len );
/* cleanup */
if ( tail == ofs ) {
free_STRING( tail );
}
else {
free_STRING( tail );
free_STRING( ofs );
}
}
}
/* We are assigning to a CELL and we aren't sure if its
a field
*/
void
slow_cell_assign( CELL * target, CELL * source )
{
if ( field <= target && target <= LAST_PFIELD ) {
field_assign( target, source );
}
else {
size_t i;
for ( i = 1; i < fbankv_num_chunks * FBANKV_CHUNK_SIZE; i++ ) {
CELL * bank_start = fbankv[i];
CELL * bank_end = bank_start + FBANK_SZ;
if ( bank_start == 0 )
break;
if ( bank_start <= target && target < bank_end ) {
/* it is a field */
field_assign( target, source );
return;
}
}
/* its not a field */
cell_destroy( target );
cellcpy( target, source );
}
}
int
field_addr_to_index( CELL * cp )
{
CELL ** p = fbankv;
while ( cp < *p || cp >= *p + FBANK_SZ ) {
p++;
}
return (int)( ( ( p - fbankv ) << FB_SHIFT ) + ( cp - *p ) );
}
/*------- more than 1 fbank needed ------------*/
/*
compute the address of a field $i
if CELL $i doesn't exist, because it is bigger than max_field,
then it gets created and max_field grows.
*/
CELL *
slow_field_ptr( int i )
{
if ( i > max_field ) { /* need to allocate more field memory */
int j;
allocate_fbankv( i );
j = ( max_field >> FB_SHIFT ) + 1;
#ifdef DEBUG
if ( !( j > 0 && fbankv[j - 1] != 0 && fbankv[j] == 0 ) ) {
bozo( "confused in slow_field_ptr" );
}
#endif
do {
fbankv[j] = (CELL *)emalloc( sizeof( CELL ) * FBANK_SZ );
memset( fbankv[j], 0, sizeof( CELL ) * FBANK_SZ );
j++;
max_field += FBANK_SZ;
} while ( i > max_field );
}
return &fbankv[i >> FB_SHIFT][i & ( FBANK_SZ - 1 )];
}
#if USE_BINMODE
/* read current value of BINMODE */
int
binmode( void )
{
CELL c;
cast1_to_d( cellcpy( &c, BINMODE ) );
return d_to_int( c.dval );
}
/* set BINMODE and RS and ORS
from environment or -W binmode= */
void
set_binmode( int x )
{
CELL c;
int change = ( ( x & 4 ) == 0 );
/* set RS */
c.type = C_STRING;
c.ptr = (void *)new_STRING( ( change && ( x & 1 ) ) ? "\r\n" : "\n" );
field_assign( RS, &c );
free_STRING( string( &c ) );
/* set ORS */
cell_destroy( ORS );
ORS->type = C_STRING;
ORS->ptr = (void *)new_STRING( ( change && ( x & 2 ) ) ? "\r\n" : "\n" );
cell_destroy( BINMODE );
BINMODE->type = C_DOUBLE;
BINMODE->dval = (double)x;
}
#endif /* USE_BINMODE */
#ifdef MEM_CHECK
static void
fbank_free( CELL * const fb )
{
CELL * end = fb + FBANK_SZ;
CELL * cp;
for ( cp = fb; cp < end; cp++ ) {
cell_destroy( cp );
}
free( fb );
}
static void
fbankv_free( void )
{
unsigned i = 1;
const size_t cnt = FBANKV_CHUNK_SIZE * fbankv_num_chunks;
while ( i < cnt && fbankv[i] != 0 ) {
fbank_free( fbankv[i] );
i++;
}
for ( ; i < cnt; i++ ) {
if ( fbankv[i] != 0 ) {
bozo( "unexpected pointer in fbankv[]" );
}
}
free( fbankv );
}
void
field_leaks( void )
{
int n;
/* everything in field[] */
for ( n = 0; n < FBANK_SZ + NUM_PFIELDS; n++ ) {
cell_destroy( &field[n] );
}
/* fbankv[0] == field
this call does all the rest of the fields
*/
fbankv_free();
switch ( fs_shadow.type ) {
case C_RE:
break;
case C_STRING:
case C_STRNUM:
case C_MBSTRN:
cell_destroy( &fs_shadow );
break;
default:
break;
}
switch ( rs_shadow.type ) {
case SEP_STR:
free_STRING( ( (STRING *)( &rs_shadow.ptr ) ) );
break;
case SEP_RE:
break;
}
}
#endif