aboutsummaryrefslogtreecommitdiffstats
path: root/awk7.c
blob: 12de0318103dfcd8d71c2bf2dc581c62c97e2841 (plain)
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
/*
 * gawk - routines for dealing with record input and fields
 */

/*
 * GAWK is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY.  No author or distributor accepts responsibility to anyone for
 * the consequences of using it or for whether it serves any particular
 * purpose or works at all, unless he says so in writing. Refer to the GAWK
 * General Public License for full details. 
 *
 * Everyone is granted permission to copy, modify and redistribute GAWK, but
 * only under the conditions described in the GAWK General Public License.  A
 * copy of this license is supposed to have been given to you along with GAWK
 * so you can know your rights and responsibilities.  It should be in a file
 * named COPYING.  Among other things, the copyright notice and this notice
 * must be preserved on all copies. 
 *
 * In other words, go ahead and share GAWK, but don't try to stop anyone else
 * from sharing it farther.  Help stamp out software hoarding! 
 */

#include "awk.h"

extern int get_rs();
extern NODE *concat_exp();

static int re_split();
static int get_a_record();

static int getline_redirect = 0;/* "getline <file" being executed */
static char *line_buf = NULL;	/* holds current input line */
static int line_alloc = 0;	/* current allocation for line_buf */
static char *parse_extent;	/* marks where to restart parse of record */
static int parse_high_water=0;	/* field number that we have parsed so far */
static char *save_fs = " ";	/* save current value of FS when line is read,
				 * to be used in deferred parsing
				 */

int field_num;			/* save number of field in get_lhs */
NODE **fields_arr;		/* array of pointers to the field nodes */
NODE node0;			/* node for $0 which never gets free'd */
int node0_valid = 1;		/* $(>0) has not been changed yet */
char f_empty[] = "";

void
init_fields()
{
	emalloc(fields_arr, NODE **, sizeof(NODE *), "init_fields");
	node0.type = Node_val;
	node0.stref = 0;
	node0.flags = (STR|PERM);	/* never free buf */
	fields_arr[0] = &node0;
}

/*
 * Danger!  Must only be called for fields we know have just been blanked, or
 * fields we know don't exist yet.  
 */

static void
set_field(num, str, len, dummy)
int num;
char *str;
int len;
NODE *dummy;	/* not used -- just to make interface same as set_element */
{
	NODE *n;
	int t;
	static int nf_high_water = 0;

	if (num > nf_high_water) {
		erealloc(fields_arr, NODE **, (num + 1) * sizeof(NODE *), "set_field");
		nf_high_water = num;
	}
	/* fill in fields that don't exist */
	for (t = parse_high_water + 1; t < num; t++)
		fields_arr[t] = Nnull_string;
	n = make_string(str, len);
	fields_arr[num] = n;
	parse_high_water = num;
}

/* Someone assigned a value to $(something).  Fix up $0 to be right */
static void
rebuild_record()
{
	register int tlen;
	register NODE *tmp;
	NODE *ofs;
	char *ops;
	register char *cops;
	register NODE **ptr, **maxp;

	maxp = 0;
	tlen = 0;
	ofs = force_string(OFS_node->var_value);
	ptr = &fields_arr[parse_high_water];
	while (ptr > &fields_arr[0]) {
		tmp = force_string(*ptr);
		tlen += tmp->stlen;
		if (tmp->stlen && !maxp)
			maxp = ptr;
		ptr--;
	}
	if (maxp) {
		tlen += ((maxp - fields_arr) - 1) * ofs->stlen;
		emalloc(ops, char *, tlen + 1, "fix_fields");
		cops = ops;
		for (ptr = &fields_arr[1]; ptr <= maxp; ptr++) {
			tmp = force_string(*ptr);
			bcopy(tmp->stptr, cops, tmp->stlen);
			cops += tmp->stlen;
			if (ptr != maxp) {
				bcopy(ofs->stptr, cops, ofs->stlen);
				cops += ofs->stlen;
			}
		}
	} else
		ops = "";
	tmp = make_string(ops, tlen);
	deref = fields_arr[0];
	do_deref();
	fields_arr[0] = tmp;
}


/*
 * This reads in a record from the input file
 */
int
inrec()
{
	int cnt;
	int retval = 0;

	cnt = get_a_record(&line_buf, &line_alloc);
	if (cnt == EOF) {
		cnt = 0;
		retval = 1;
	} else {
		if (!getline_redirect) {
			assign_number(&(NR_node->var_value),
			    NR_node->var_value->numbr + 1.0);
			assign_number(&(FNR_node->var_value),
			    FNR_node->var_value->numbr + 1.0);
		}
	}
	set_record(line_buf, cnt);

	return retval;
}

/*
 * setup $0, but defer parsing rest of line until reference is made to $(>0)
 * or to NF.  At that point, parse only as much as necessary.
 */
void
set_record(buf, cnt)
char *buf;
int cnt;
{
	register int i;

	assign_number(&(NF_node->var_value), (AWKNUM) -1);
	for (i = 1; i <= parse_high_water; i++) {
		deref = fields_arr[i];
		do_deref();
	}
	parse_high_water = 0;
	node0_valid = 1;
	if (buf == line_buf) {
		deref = fields_arr[0];
		do_deref();
		save_fs = get_fs();
		node0.type = Node_val;
		node0.stptr = buf;
		node0.stlen = cnt;
		node0.stref = 1;
		node0.flags = (STR|PERM);	/* never free buf */
		fields_arr[0] = &node0;
	}
}

NODE **
get_field(num, assign)
int num;
int assign;	/* this field is on the LHS of an assign */
{
	int n;

	/*
	 * if requesting whole line but some other field has been altered,
	 * then the whole line must be rebuilt
	 */
	if (num == 0 && node0_valid == 0) {
		/* first, parse remainder of input record */
		n = parse_fields(HUGE-1, &parse_extent,
		    node0.stlen - (parse_extent - node0.stptr),
		    save_fs, set_field, (NODE *)NULL);
		assign_number(&(NF_node->var_value), (AWKNUM) n);
		rebuild_record();
		return &fields_arr[0];
	}
	if (num > 0 && assign)
		node0_valid = 0;
	if (num <= parse_high_water)	/* we have already parsed this field */
		return &fields_arr[num];
	if (parse_high_water == 0 && num > 0)	/* starting at the beginning */
		parse_extent = fields_arr[0]->stptr;
	/*
	 * parse up to num fields, calling set_field() for each, and saving
	 * in parse_extent the point where the parse left off
	 */
	n = parse_fields(num, &parse_extent,
		fields_arr[0]->stlen - (parse_extent-fields_arr[0]->stptr),
		save_fs, set_field, (NODE *)NULL);
	if (num == HUGE-1)
		num = n;
	if (n < num) {	/* requested field number beyond end of record;
			 * set_field will just extend the number of fields,
			 * with empty fields
			 */
		set_field(num, f_empty, 0, (NODE *) NULL);
		/*
		 * if this field is onthe LHS of an assignment, then we want to
		 * set NF to this value, below
		 */
		if (assign)
			n = num;
	}
	/*
	 * if we reached the end of the record, set NF to the number of fields
	 * so far.  Note that num might actually refer to a field that
	 * is beyond the end of the record, but we won't set NF to that value at
	 * this point, since this is only a reference to the field and NF
	 * only gets set if the field is assigned to -- in this case n has
	 * been set to num above
	 */
	if (*parse_extent == '\0')
		assign_number(&(NF_node->var_value), (AWKNUM) n);

	return &fields_arr[num];
}

/*
 * this is called both from get_field() and from do_split()
 */
int
parse_fields(up_to, buf, len, fs, set, n)
int up_to;	/* parse only up to this field number */
char **buf;	/* on input: string to parse; on output: point to start next */
int len;
char *fs;
int (*set) ();	/* routine to set the value of the parsed field */
NODE *n;
{
	char *s = *buf;
	register char *field;
	register char *scan;
	register char *end = s + len;
	int NF = parse_high_water;

	if (up_to == HUGE)
		NF = 0;
	if (*fs && *(fs + 1) != '\0') {	/* fs is a regexp */
		struct re_registers reregs;

		scan = s;
		while (re_split(scan, end - scan, fs, &reregs) != -1 &&
		    NF < up_to) {
			(*set)(++NF, scan, reregs.start[0], n);
			scan += reregs.end[0];
		}
		if (NF != up_to && scan <= end) {
			(*set)(++NF, scan, end - scan, n);
			scan = end;
		}
		*buf = scan;
		return (NF);
	}
	for (scan = s; scan < end && NF < up_to; scan++) {
		/*
		 * special case:  fs is single space, strip leading
		 * whitespace 
		 */
		if (*fs == ' ') {
			while ((*scan == ' ' || *scan == '\t') && scan < end)
				scan++;
			if (scan >= end)
				break;
		}
		field = scan;
		if (*fs == ' ')
			while (*scan != ' ' && *scan != '\t' && scan < end)
				scan++;
		else {
			while (*scan != *fs && scan < end)
				scan++;
			if (scan == end-1 && *scan == *fs) {
				(*set)(++NF, field, scan - field, n);
				field = scan;
			}
		}
		(*set)(++NF, field, scan - field, n);
		if (scan == end)
			break;
	}
	*buf = scan;
	return NF;
}

static int
re_split(buf, len, fs, reregs)
char *buf, *fs;
int len;
struct re_registers *reregs;
{
	typedef struct re_pattern_buffer RPAT;
	static RPAT *rp;
	static char *last_fs = NULL;

	if ((last_fs != NULL && !STREQ(fs, last_fs))
	    || (rp && ! strict && ((IGNORECASE_node->var_value->numbr != 0)
			 ^ (rp->translate != NULL))))
	{
		/* fs has changed or IGNORECASE has changed */
		free(rp->buffer);
		free(rp->fastmap);
		free((char *) rp);
		free(last_fs);
		last_fs = NULL;
	}
	if (last_fs == NULL) {	/* first time */
		emalloc(rp, RPAT *, sizeof(RPAT), "re_split");
		bzero((char *) rp, sizeof(RPAT));
		emalloc(rp->buffer, char *, 8, "re_split");
		rp->allocated = 8;
		emalloc(rp->fastmap, char *, 256, "re_split");
		emalloc(last_fs, char *, strlen(fs) + 1, "re_split");
		(void) strcpy(last_fs, fs);
		if (! strict && IGNORECASE_node->var_value->numbr != 0.0)
			rp->translate = casetable;
		else
			rp->translate = NULL;
		if (re_compile_pattern(fs, strlen(fs), rp) != NULL)
			fatal("illegal regular expression for FS: `%s'", fs);
	}
	return re_search(rp, buf, len, 0, len, reregs);
}

static int				/* count of chars read or EOF */
get_a_record(bp, sizep)
char **bp;			/* *bp points to beginning of line on return */
int *sizep;			/* *sizep is current allocation of *bp */
{
	register char *buf;	/* buffer; realloced if necessary */
	int bsz;		/* current buffer size */
	register char *cur;	/* current position in buffer */
	register char *buf_end;	/* end of buffer */
	register int rs;	/* rs is the current record separator */
	register int c;
	extern FILE *input_file;

	bsz = *sizep;
	buf = *bp;
	if (!buf) {
		emalloc(buf, char *, 128, "get_a_record");
		bsz = 128;
	}
	rs = get_rs();
	buf_end = buf + bsz;
	cur = buf;
	while ((c = getc(input_file)) != EOF) {
		if (rs == 0 && c == '\n' && cur != buf && cur[-1] == '\n') {
			cur--;
			break;
		}
		else if (c == rs)
			break;
		*cur++ = c;
		if (cur == buf_end) {
			erealloc(buf, char *, bsz * 2, "get_a_record");
			cur = buf + bsz;
			bsz *= 2;
			buf_end = buf + bsz;
		}
	}
	if (rs == 0 && c == EOF && cur != buf && cur[-1] == '\n')
		cur--;
	*cur = '\0';
	*bp = buf;
	*sizep = bsz;
	if (c == EOF && cur == buf)
		return EOF;
	return cur - buf;
}

NODE *
do_getline(tree)
NODE *tree;
{
	FILE *save_fp;
	int cnt;
	NODE **lhs;
	extern FILE *input_file;
	int redir_error = 0;

	if (tree->rnode == NULL && (input_file == NULL || feof(input_file))) {
		input_file = nextfile();
		if (input_file == NULL)
			return tmp_number((AWKNUM) 0.0);
	}
	save_fp = input_file;
	if (tree->rnode != NULL) {	/* with redirection */
		input_file = redirect(tree->rnode, & redir_error);
		if (input_file == NULL && redir_error)
			return tmp_number((AWKNUM) -1.0);
		getline_redirect++;
	}
	if (tree->lnode == NULL) {	/* read in $0 */
		if (inrec() != 0) {
			input_file = save_fp;
			getline_redirect = 0;
			return tmp_number((AWKNUM) 0.0);
		}
	} else {			/* read in a named variable */
		char *s = NULL;
		int n = 0;

		lhs = get_lhs(tree->lnode, 1);
		cnt = get_a_record(&s, &n);
		if (!getline_redirect) {
			assign_number(&(NR_node->var_value),
			    NR_node->var_value->numbr + 1.0);
			assign_number(&(FNR_node->var_value),
			    FNR_node->var_value->numbr + 1.0);
		}
		if (cnt == EOF) {
			input_file = save_fp;
			getline_redirect = 0;
			free(s);
			return tmp_number((AWKNUM) 0.0);
		}
		*lhs = make_string(s, strlen(s));
		free(s);
		do_deref();
		/* we may have to regenerate $0 here! */
		if (field_num == 0)
			set_record(fields_arr[0]->stptr, fields_arr[0]->stlen);
		field_num = -1;
	}
	getline_redirect = 0;
	input_file = save_fp;
	return tmp_number((AWKNUM) 1.0);
}

/*
 * We can't dereference a variable until after we've given it its new value.
 * This variable points to the value we have to free up 
 */
NODE *deref;

/*
 * This returns a POINTER to a node pointer. get_lhs(ptr) is the current
 * value of the var, or where to store the var's new value 
 */

NODE **
get_lhs(ptr, assign)
NODE *ptr;
int assign;		/* this is being called for the LHS of an assign. */
{
	register NODE **aptr;
	NODE *n;

#ifdef DEBUG
	if (ptr == NULL)
		cant_happen();
#endif
	deref = NULL;
	field_num = -1;
	switch (ptr->type) {
	case Node_var:
	case Node_var_array:
		if (ptr == NF_node && (int) NF_node->var_value->numbr == -1)
			(void) get_field(HUGE-1, assign); /* parse record */
		deref = ptr->var_value;
#ifdef DEBUG
		if (deref->type != Node_val)
			cant_happen();
		if (deref->flags == 0)
			cant_happen();
#endif
		return &(ptr->var_value);

	case Node_param_list:
		n = stack_ptr[ptr->param_cnt];
#ifdef DEBUG
		deref = n->var_value;
		if (deref->type != Node_val)
			cant_happen();
		if (deref->flags == 0)
			cant_happen();
		deref = 0;
#endif
		return &(n->var_value);

	case Node_field_spec:
		field_num = (int) force_number(tree_eval(ptr->lnode));
		free_result();
		if (field_num < 0)
			fatal("attempt to access field %d", field_num);
		aptr = get_field(field_num, assign);
		deref = *aptr;
		return aptr;

	case Node_subscript:
		n = ptr->lnode;
		if (n->type == Node_param_list)
			n = stack_ptr[n->param_cnt];
		aptr = assoc_lookup(n, concat_exp(ptr->rnode));
		deref = *aptr;
#ifdef DEBUG
		if (deref->type != Node_val)
			cant_happen();
		if (deref->flags == 0)
			cant_happen();
#endif
		return aptr;
	case Node_func:
		fatal ("`%s' is a function, assignment is not allowed",
			ptr->lnode->param);
	}
	return 0;
}

void
do_deref()
{
	if (deref == NULL)
		return;
	if (deref == Nnull_string) {
		deref = 0;
		return;
	}
#ifdef DEBUG
	if (deref->flags == 0)
		cant_happen();
#endif
	if ((deref->flags & MALLOC) || (deref->flags & TEMP)) {
#ifdef DEBUG
		if (deref->flags & PERM)
			cant_happen();
#endif
		if (deref->flags & STR) {
			if (deref->stref > 0 && deref->stref != 255)
				deref->stref--;
			if (deref->stref > 0) {
				deref->flags &= ~TEMP;
				deref = 0;
				return;
			}
			free(deref->stptr);
		}
		freenode(deref);
	}
	deref = 0;
}