Codebase list libmawk / debian/1.0.0-1 src / libmawk / field_exec.c
debian/1.0.0-1

Tree @debian/1.0.0-1 (Download .tar.gz)

field_exec.c @debian/1.0.0-1raw · history · blame

  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
/********************************************
field.c

libmawk changes (C) 2009-2010, Tibor 'Igor2' Palinkas;
based on mawk code coming with the below copyright:

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.
********************************************/
#include <stdio.h>
#include "mawk.h"
#include "field.h"
#include "init.h"
#include "memory.h"
#include "scan.h"
#include "bi_vars.h"
#include "repl.h"
#include "regexp.h"
#include "cell.h"

static void build_field0(mawk_state_t *);
static void set_rs_shadow(mawk_state_t *);
static void load_field_ov(mawk_state_t *);

static void set_rs_shadow(mawk_state_t * MAWK)
{
	mawk_cell_t c;
	mawk_string_t *sval;
	char *s;
	unsigned len;

	if (MAWK->posix_space_flag && MAWK->mawk_state == EXECUTION)
		MAWK->scan_code['\n'] = SC_UNEXPECTED;

	if (MAWK->rs_shadow.type == SEP_STR) {
		free_STRING((mawk_string_t *) MAWK->rs_shadow.ptr);
	}

	mawk_cellcpy(MAWK, &c, MAWK_RS);
	mawk_cast_for_split(MAWK, &c);
	switch (c.type) {
	case C_RE:
		if ((s = mawk_is_string_split(c.ptr, &len))) {
			if (len == 1) {
				MAWK->rs_shadow.type = SEP_CHAR;
				MAWK->rs_shadow.c = s[0];
			}
			else {
				MAWK->rs_shadow.type = SEP_STR;
				MAWK->rs_shadow.ptr = (PTR) mawk_new_STRING(MAWK, s);
			}
		}
		else {
			MAWK->rs_shadow.type = SEP_RE;
			MAWK->rs_shadow.ptr = c.ptr;
		}
		break;

	case C_SPACE:
		MAWK->rs_shadow.type = SEP_CHAR;
		MAWK->rs_shadow.c = ' ';
		break;

	case C_SNULL:								/* RS becomes one or more blank lines */
		if (MAWK->mawk_state == EXECUTION)
			MAWK->scan_code['\n'] = SC_SPACE;
		MAWK->rs_shadow.type = SEP_MLR;
		sval = mawk_new_STRING(MAWK, "\n\n+");
		MAWK->rs_shadow.ptr = mawk_re_compile(MAWK, sval);
		free_STRING(sval);
		break;

	default:
		mawk_bozo(MAWK, "bad cell in set_rs_shadow");
	}
}


void mawk_set_field0(mawk_state_t *MAWK, char *s, unsigned len)
{
	mawk_cell_destroy(MAWK, &MAWK->field[0]);
	MAWK->nf = -1;

	if (len) {
		MAWK->field[0].type = C_MBSTRN;
		MAWK->field[0].ptr = (PTR) mawk_new_STRING0(MAWK, len);
		memcpy(string(&MAWK->field[0])->str, s, len);
	}
	else {
		MAWK->field[0].type = C_STRING;
		MAWK->field[0].ptr = (PTR) & MAWK->null_str;
		MAWK->null_str.ref_cnt++;
	}
}



/* split field[0] into $1, $2 ... and set NF  */

void mawk_split_field0(mawk_state_t * MAWK)
{
	register mawk_cell_t *cp;
	register int cnt;
	mawk_cell_t c;												/* copy field[0] here if not string */


	if (MAWK->field[0].type < C_STRING) {
		mawk_cellcpy(MAWK, &c, MAWK->field + 0);
		mawk_cast1_to_str(MAWK, &c);
		cp = &c;
	}
	else
		cp = &MAWK->field[0];

	if (string(cp)->len == 0)
		MAWK->nf = 0;
	else {
		switch (MAWK->fs_shadow.type) {
		case C_SNULL:							/* FS == "" */
			MAWK->nf = mawk_null_split(MAWK, string(cp)->str);
			break;

		case C_SPACE:
			MAWK->nf = mawk_space_split(MAWK, string(cp)->str, string(cp)->len);
			break;

		default:
			MAWK->nf = mawk_re_split(MAWK, string(cp)->str, MAWK->fs_shadow.ptr);
			break;
		}

	}

	mawk_cell_destroy(MAWK, MAWK_NF);
	MAWK_NF->type = C_NUM;
	MAWK_NF->d.dval = (mawk_num_t) MAWK->nf;

	if (MAWK->nf > MAX_SPLIT) {
		cnt = MAX_SPLIT;
		load_field_ov(MAWK);
	}
	else
		cnt = MAWK->nf;

	while (cnt > 0) {
		mawk_cell_destroy(MAWK, MAWK->field + cnt);
		MAWK->field[cnt].ptr = (PTR) split_buff[cnt - 1];
		MAWK->field[cnt--].type = C_MBSTRN;
	}

	if (cp == &c) {
		free_STRING(string(cp));
	}
}

/* construct field[0] from the other fields */

static void build_field0(mawk_state_t * MAWK)
{
#ifdef DEBUG
	if (MAWK->nf < 0)
		mawk_bozo(MAWK, "nf <0 in build_field0");
#endif

	mawk_cell_destroy(MAWK, MAWK->field + 0);

	if (MAWK->nf == 0) {
		MAWK->field[0].type = C_STRING;
		MAWK->field[0].ptr = (PTR) & MAWK->null_str;
		MAWK->null_str.ref_cnt++;
	}
	else if (MAWK->nf == 1) {
		mawk_cellcpy(MAWK, MAWK->field, MAWK->field + 1);
	}
	else {
		mawk_cell_t c;
		mawk_string_t *ofs, *tail;
		unsigned len;
		register mawk_cell_t *cp;
		register char *p, *q;
		int cnt;
		mawk_cell_t **fbp, *cp_limit;


		mawk_cellcpy(MAWK, &c, OFS);
		mawk_cast1_to_str(MAWK, &c);
		ofs = (mawk_string_t *) c.ptr;
		mawk_cellcpy(MAWK, &c, field_ptr(MAWK->nf));
		mawk_cast1_to_str(MAWK, &c);
		tail = (mawk_string_t *) c.ptr;
		cnt = MAWK->nf - 1;

		len = cnt * ofs->len + tail->len;

		fbp = MAWK->fbank;
		cp_limit = MAWK->field + FBANK_SZ;
		cp = MAWK->field + 1;

		while (cnt-- > 0) {
			if (cp->type < C_STRING) {	/* use the string field temporarily */
				if (cp->type == C_NOINIT) {
					cp->ptr = (PTR) & MAWK->null_str;
					MAWK->null_str.ref_cnt++;
				}
				else {									/* its a number */

					Int ival;
					char xbuff[260];

					ival = mawk_d_to_I(cp->d.dval);
					if (ival == cp->d.dval)
						sprintf(xbuff, INT_FMT, ival);
					else
						sprintf(xbuff, string(MAWK_CONVFMT)->str, cp->d.dval);

					cp->ptr = (PTR) mawk_new_STRING(MAWK, xbuff);
				}
			}

			len += string(cp)->len;

			if (++cp == cp_limit) {
				cp = *++fbp;
				cp_limit = cp + FBANK_SZ;
			}

		}

		MAWK->field[0].type = C_STRING;
		MAWK->field[0].ptr = (PTR) mawk_new_STRING0(MAWK, len);

		p = string(MAWK->field)->str;

		/* walk it again , putting things together */
		cnt = MAWK->nf - 1;
		fbp = MAWK->fbank;
		cp = MAWK->field + 1;
		cp_limit = MAWK->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 */
		free_STRING(tail);
		free_STRING(ofs);
	}
}

/* We are assigning to a mawk_cell_t and we aren't sure if its
   a field */

void mawk_bifunct_target_assign(mawk_state_t *MAWK, register mawk_cell_t *target_, mawk_cell_t *source)
{
	mawk_cell_t *target = target_->ptr; /* target is assumed to be the varref */

	if (target_->type == C_ARR_REF_BT) {
		/* reference to an array member: ->ptr is the array, ->d.idx_str is the zmalloc'd string cell */
		mawk_array_set(MAWK, (mawk_array_t)target_->ptr, target_->d.idx_cell, source);
		mawk_cell_destroy(MAWK, target_->d.idx_cell);
		mawk_zfree(MAWK, target_->d.idx_cell, sizeof(mawk_cell_t));
		target_->type = C_NOINIT; /* don't need to destroy this: it's a special arr ref */
		return ;
	}

	/* original code dealing with a normal varref */
	if (target >= MAWK->field && target <= LAST_PFIELD)
		mawk_field_assign(MAWK, target, source);
	else {
		mawk_cell_t **p = MAWK->fbank + 1;

		while (*p) {
			if (target >= *p && target < *p + FBANK_SZ) {
				mawk_field_assign(MAWK, target, source);
				return;
			}
			p++;
		}
		/* its not a field */
		mawk_cell_destroy(MAWK, target);
		mawk_cellcpy(MAWK, target, source);
	}
}

/*
  $0 split into more than MAX_SPLIT fields,
  $(MAX_FIELD+1) ... are on the split_ov_list.
  Copy into fields which start at fbank[1]
*/

static void load_field_ov(mawk_state_t * MAWK)
{
	register SPLIT_OV *p;					/* walks split_ov_list */
	register mawk_cell_t *cp;						/* target of copy */
	int j;												/* current fbank[] */
	mawk_cell_t *cp_limit;								/* change fbank[] */
	SPLIT_OV *q;									/* trails p */

	/* make sure the fields are allocated */
	mawk_slow_field_ptr(MAWK, MAWK->nf);

	p = MAWK->split_ov_list;
	MAWK->split_ov_list = (SPLIT_OV *) 0;
	j = 1;
	cp = MAWK->fbank[j];
	cp_limit = cp + FBANK_SZ;
	while (p) {
		mawk_cell_destroy(MAWK, cp);
		cp->type = C_MBSTRN;
		cp->ptr = (PTR) p->sval;

		if (++cp == cp_limit) {
			cp = MAWK->fbank[++j];
			cp_limit = cp + FBANK_SZ;
		}

		q = p;
		p = p->link;
		MAWK_ZFREE(MAWK, q);
	}
}

/*
  assign mawk_cell_t *cp to field or pseudo field
  and take care of all side effects
*/
void mawk_field_assign(mawk_state_t *MAWK, register mawk_cell_t *fp, mawk_cell_t *cp)
{
	mawk_cell_t c;
	int i, j;

	/* the most common case first */
	if (fp == MAWK->field) {
		mawk_cell_destroy(MAWK, MAWK->field);
		mawk_cellcpy(MAWK, fp, cp);
		MAWK->nf = -1;
		return;
	}

	/* its not important to do any of this fast */

	if (MAWK->nf < 0)
		mawk_split_field0(MAWK);

	switch (i = (fp - MAWK->field)) {

	case MAWK_NF_field:

		mawk_cell_destroy(MAWK, MAWK_NF);
		mawk_cellcpy(MAWK, &c, cp);
		mawk_cellcpy(MAWK, MAWK_NF, &c);
		if (c.type != C_NUM)
			mawk_cast1_to_num(MAWK, &c);

		if ((j = d_to_i(c.d.dval)) < 0)
			mawk_rt_error(MAWK, "negative value assigned to NF");

		if (j > MAWK->nf)
			for (i = MAWK->nf + 1; i <= j; i++) {
				cp = field_ptr(i);
				mawk_cell_destroy(MAWK, cp);
				cp->type = C_STRING;
				cp->ptr = (PTR) & MAWK->null_str;
				MAWK->null_str.ref_cnt++;
			}

		MAWK->nf = j;
		build_field0(MAWK);
		break;

	case MAWK_RS_field:
		mawk_cell_destroy(MAWK, MAWK_RS);
		mawk_cellcpy(MAWK, MAWK_RS, cp);
		set_rs_shadow(MAWK);
		break;

	case MAWK_FS_field:
		mawk_cell_destroy(MAWK, MAWK_FS);
		mawk_cellcpy(MAWK, MAWK_FS, cp);
		mawk_cellcpy(MAWK, &MAWK->fs_shadow, MAWK_FS);
		mawk_cast_for_split(MAWK, &MAWK->fs_shadow);
		break;

	case MAWK_OFMT_field:
	case MAWK_CONVFMT_field:
		/* If the user does something stupid with OFMT or CONVFMT,
		   we could crash.
		   We'll make an attempt to protect ourselves here.  This is
		   why OFMT and CONVFMT are pseudo fields.

		   The ptrs of OFMT and CONVFMT always have a valid mawk_string_t,
		   even if assigned a NUM or NOINIT
		 */

		free_STRING(string(fp));
		mawk_cellcpy(MAWK, fp, cp);
		if (fp->type < C_STRING)		/* !! */
			fp->ptr = (PTR) mawk_new_STRING(MAWK, "%.6g");
		else if (fp == MAWK_CONVFMT) {
			/* It's a string, but if it's really goofy and CONVFMT,
			   it could still mawk_damage us. Test it .
			 */
			char xbuff[512];

			xbuff[256] = 0;
			sprintf(xbuff, string(fp)->str, 3.1459);
			if (xbuff[256])
				mawk_rt_error(MAWK, "CONVFMT assigned unusable value");
		}
		break;

	default:											/* $1 or $2 or ... */
		mawk_cell_destroy(MAWK, fp);
		mawk_cellcpy(MAWK, fp, cp);

		if (i < 0 || i > MAX_SPLIT)
			i = mawk_field_addr_to_index(MAWK, fp);

		if (i > MAWK->nf) {
			for (j = MAWK->nf + 1; j < i; j++) {
				cp = field_ptr(j);
				mawk_cell_destroy(MAWK, cp);
				cp->type = C_STRING;
				cp->ptr = (PTR) & MAWK->null_str;
				MAWK->null_str.ref_cnt++;
			}
			MAWK->nf = i;
			mawk_cell_destroy(MAWK, MAWK_NF);
			MAWK_NF->type = C_NUM;
			MAWK_NF->d.dval = (mawk_num_t) i;
		}

		build_field0(MAWK);

	}
}