Package list libmawk / 1df34837-a1d9-4cbd-a796-a1f5ebe673fc/main scconfig / src / tmpasm / tmpasm_scconfig.c
1df34837-a1d9-4cbd-a796-a1f5ebe673fc/main

Tree @1df34837-a1d9-4cbd-a796-a1f5ebe673fc/main (Download .tar.gz)

tmpasm_scconfig.c @1df34837-a1d9-4cbd-a796-a1f5ebe673fc/mainraw · 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
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "tmpasm.h"
#include "db.h"
#include "regex.h"
#include "openfiles.h"
#include "libs.h"
#include "tmpasm_scconfig.h"
#include "log.h"
#include "regex.h"

#ifndef TMPASM_PATH
#define TMPASM_PATH "/tmpasm"
#endif

#ifndef IFS_PATH
#define IFS_PATH TMPASM_PATH "/IFS"
#endif

#ifndef IFS_DEFAULT
#define IFS_DEFAULT " \t\r\n"
#endif

#ifndef OFS_PATH
#define OFS_PATH TMPASM_PATH "/OFS"
#endif

#ifndef OFS_DEFAULT
#define OFS_DEFAULT "\n"
#endif

typedef struct scc_s {
	openfiles_t ofl;
	FILE *fout, *default_fout;
	const char *cwd;
} scc_t;

static const char *scc_runtime_error_fmts[] = {
	/* -0  */  "success scc %s",
	/* -1  */  "\"put\" requires exactly two arguments (got %s)",
	/* -2  */  "not enough arguments for sub; should be \"sub node pattern str\"%s",
	/* -3  */  "regex syntax error: %s",
	/* -4  */  "not enough arguments for uniq; should be \"uniq destnode\" or \"uniq destnode src\"%s",
	/* -5  */  "redir: too many arguments%s",
	/* -6  */  "redir: can't open %s",
	/* -7  */  "exiting due to a previous runtime error occurred in included file %s",
	/* -8  */  "can't include '%s': can't open file",
	/* -9  */  "\"put\" requires two or three arguments (got %s)",
	/* -10 */  "\"order\" requires 4 or 5 arguments: \"order destnode word before|after word\" or \"order destnode src word before|after word\"",
	/* -11 */  "\"uniq\" got too many grouping regular expressions",
	NULL
};

static int print_runtime_error(tmpasm_t *ctx, const char *ifn)
{
	if (ctx->runtime_error != 0) {
		const char *fmt = tmpasm_runtime_error_fmt(ctx);
		fprintf(stderr, "Runtime error at %s %d:%d: ", ifn, ctx->runtime_error_line, ctx->runtime_error_col);
		fprintf(stderr, fmt, (ctx->runtime_error_data == NULL ? "" : ctx->runtime_error_data));
		fprintf(stderr, "\n");
		return -1;
	}
	return 0;
}

/* allocate and build a full path using ud->cwd and fn */
static char *scc_path(scc_t *ud, const char *fn)
{
	if (ud->cwd == NULL)
		return strclone(fn);
	return str_concat("", ud->cwd, "/", fn, NULL);
}

/******** db binding ********/

static const char *scc_get(tmpasm_t *ctx, const char *addr)
{
	(void) ctx;  /* not used */

	if (*addr == '&') { /* return whether exists */
		if (get(addr+1) != NULL)
			return strue;
		return sfalse;
	}

	if (*addr == '?') { /* safe get: return "" instead of NULL to avoid runtime error */
		const char *res = get(addr+1);
		if (res == NULL)
			return "";
		return res;
	}
	return get(addr);
}

static void scc_set(tmpasm_t *ctx, const char *addr, const char *data)
{
	(void) ctx;  /* not used */
	put(addr, data);
}

static int scc_is_true(tmpasm_t *ctx, const char *data)
{
	(void) ctx;  /* not used */
	return ((strcmp(data, "1") == 0) || istrue(data));
}

static int scc_match(tmpasm_t *ctx, const char *str, const char *pat)
{
	(void) ctx;  /* not used */
	re_comp(pat);
	return re_exec(str);
}

static const char *scc_ifs(tmpasm_t *ctx)
{
	const char *ifs = get(IFS_PATH);
	(void) ctx;  /* not used */
	if (ifs == NULL)
		return IFS_DEFAULT;
	return ifs;
}

static const char *scc_ofs(tmpasm_t *ctx)
{
	const char *ofs = get(OFS_PATH);
	(void) ctx;  /* not used */
	if (ofs == NULL)
		return OFS_DEFAULT;
	return ofs;
}

static const char *scc_next(tmpasm_t *ctx, void **state)
{
	char **s = (char **)state;
	char *start;
	const char *IFS;

	IFS = scc_ifs(ctx);

	/* strip leading whitespace */
	while(chr_inset(**s, IFS)) (*s)++;

	/* at the end of the string, no more tokens */
	if (**s == '\0')
		return NULL;

	start = *s;

	/* skip non-whitespace */
	while(!(chr_inset(**s, IFS)) && (**s != '\0')) (*s)++;

	if (**s != '\0') {
		**s = '\0';
		(*s)++;
	}
	return start;
}


static const char *scc_first(tmpasm_t *ctx, void **state, char *list)
{
	*state = list;

	return scc_next(ctx, state);
}


/******** instructions ********/
static void instr_put(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *addr, *val;
	(void) iname;  /* not used */
	if (argc != 2) {
		char str[16];
		sprintf(str, "%d", argc);
		tmpasm_runtime_error(ctx, -1, str);
		return;
	}
	addr = tmpasm_arg2str(ctx, argv[0], 1);
	val  = tmpasm_arg2str(ctx, argv[1], 0);
	if (*addr != '\0')
		put(addr, val);
	free(addr);
	free(val);
}

static void instr_resolve(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *dst, *srca;
	const char *src;
	(void) iname;  /* not used */
	if (argc != 2) {
		char str[16];
		sprintf(str, "%d", argc);
		tmpasm_runtime_error(ctx, -1, str);
		return;
	}

	dst = tmpasm_arg2str(ctx, argv[0], 1);
	srca = tmpasm_arg2str(ctx, argv[1], 0);
	src = scc_get(ctx, srca);
	if (*dst != '\0')
		put(dst, src);
	free(dst);
	free(srca);
}


static void instr_append(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *addr, *val;
	char *sep;
	(void) iname;  /* not used */

	if ((argc < 2) || (argc > 3)) {
		char str[16];
		sprintf(str, "%d", argc);
		tmpasm_runtime_error(ctx, -9, str);
		return;
	}
	addr = tmpasm_arg2str(ctx, argv[0], 1);
	val  = tmpasm_arg2str(ctx, argv[1], 0);
	if (argc >= 3)
		sep = tmpasm_arg2str(ctx, argv[2], 0);
	else
		sep = strclone(scc_ofs(ctx));
	if (*addr != '\0') {
		append(addr, sep);
		append(addr, val);
	}
	free(addr);
	free(val);
	free(sep);
}

static void instr_report(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	int n;
	(void) iname;  /* not used */
	for(n = 0; n < argc; n++) {
		char *val;
		val  = tmpasm_arg2str(ctx, argv[n], 0);
		report("%s", val);
		free(val);
	}
}

static void instr_abort(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	scc_t *ud = (scc_t *)ctx->user_data;
	(void) iname;  /* not used */
	(void) argc;  /* not used */
	(void) argv;  /* not used */
	report("Abort requested by template.\n");
	if (ud->fout) fflush(ud->fout);
	fflush(stdout);
	fflush(stderr);
	abort();
}

static void instr_halt(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	(void) iname;  /* not used */
	(void) argc;  /* not used */
	(void) argv;  /* not used */
	ctx->halt = 1;
}

static void instr_sub(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *node, *pat, *err, *csub, *buff, *end;
	const char *start;
	const char *val;
	int score, slen, global;

	if (argc < 3) {
		tmpasm_runtime_error(ctx, -2, NULL);
		return;
	}

	node = tmpasm_arg2str(ctx, argv[0], 1);
	pat  = tmpasm_arg2str(ctx, argv[1], 0);
	csub = tmpasm_arg2str(ctx, argv[2], 0);
	global = (*iname == 'g');

	val = get(node);
	if (val == NULL)
		val="";
	err = re_comp(pat);
	if (err != NULL) {
		tmpasm_runtime_error(ctx, -3, err);
		return;
	}

	slen = strlen(csub);
	if (global)
		buff = malloc(strlen(val)*(slen+3)+32); /* big enough for worst case, when every letter and $ and ^ are replaced with sub */
	else
		buff = malloc(strlen(val)+slen+32);  /* only one replacement will be done */
	strcpy(buff, val);

	start = buff;
	do {
		score = re_exec(start);
		if (score == 0)
			break;
		end = buff + strlen(buff);
		if (eopat[0] - bopat[0] != slen) {
			int mlen = end - eopat[0]+1;
			if (mlen > 0)
				memmove((char *)(bopat[0] + slen), eopat[0], mlen);
		}
		memcpy((char *)bopat[0], csub, slen);
		start = bopat[0] + slen;
	} while(global);

	buff = realloc(buff, strlen(buff)+1);
	put(node, buff);
	free(buff);
	free(node);
	free(pat);
	free(csub);
}

#define UNIQ_ERE_MAX 16
static char *uniq_eres[UNIQ_ERE_MAX];

static void instr_uniq(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *node, *strlist, *buff;
	int eres = 0;

	if (argc < 1) {
		tmpasm_runtime_error(ctx, -4, NULL);
		return;
	}
	node = tmpasm_arg2str(ctx, argv[0], 1);
	if (argc > 1) {
		int offs = 2;

		strlist = tmpasm_arg2str(ctx, argv[1], 0);
		if ((argc-offs) >= UNIQ_ERE_MAX) {
			tmpasm_runtime_error(ctx, -11, NULL);
			return;
		}
		while(argc > offs)
			uniq_eres[eres++] = tmpasm_arg2str(ctx, argv[offs++], 0);
		if (eres > 0)
			uniq_eres[eres++] = ".*";
	}
	else
		strlist = strclone(get(node));
	buff = uniq_inc_str(strlist, scc_ifs(ctx), scc_ofs(ctx), (*iname == 's'), eres, uniq_eres);
	put(node, buff);
	free(buff);
	free(strlist);
	free(node);
}

static void instr_order(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *node, *strlist, *buff, *w1, *dirs, *w2;
	int offs, dir;

	if ((argc != 4) && (argc != 5)) {
		tmpasm_runtime_error(ctx, -10, NULL);
		return;
	}
	node = tmpasm_arg2str(ctx, argv[0], 1);
	if (argc > 4) {
		strlist = tmpasm_arg2str(ctx, argv[1], 0);
		offs = 2;
	}
	else {
		strlist = strclone(get(node));
		offs = 1;
	}

	w1   = tmpasm_arg2str(ctx, argv[offs], 0);
	dirs = tmpasm_arg2str(ctx, argv[offs+1], 0);
	w2   = tmpasm_arg2str(ctx, argv[offs+2], 0);

	if (strcmp(dirs, "before") == 0)
		dir = -1;
	else if (strcmp(dirs, "after") == 0)
		dir = +1;
	else {
		tmpasm_runtime_error(ctx, -10, NULL);
		return;
	}

	buff = order_inc_str(strlist, scc_ifs(ctx), w1, dir, w2);
	put(node, buff);
	free(buff);
	free(strlist);
	free(node);
}

static void instr_print(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	int n;
	scc_t *ud = (scc_t *)ctx->user_data;
	(void) iname;  /* not used */

	for(n = 0; n < argc; n++) {
		char *val;
		val  = tmpasm_arg2str(ctx, argv[n], 0);
		fprintf(ud->fout, "%s", val);
		free(val);
	}
}

static void instr_print_ternary(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *s_cond, *s;
	scc_t *ud = (scc_t *)ctx->user_data;
	(void) iname;  /* not used */

	if ((argc < 2) || (argc > 3)) {
		char str[16];
		sprintf(str, "%d", argc);
		tmpasm_runtime_error(ctx, -1, str);
		return;
	}

	s_cond  = tmpasm_arg2str(ctx, argv[0], 0);

	if (ctx->cb->is_true(ctx, s_cond))
		s  = tmpasm_arg2str(ctx, argv[1], 0);
	else
		s  = tmpasm_arg2str(ctx, argv[2], 0);

	fprintf(ud->fout, "%s", s);

	free(s_cond);
	free(s);
}

static void scc_tmpasm_parse_(tmpasm_t *ctx, const char *cwd, FILE *fin, FILE *default_fout, FILE *fout)
{
	scc_t *ud = malloc(sizeof(scc_t));
	memset(&ud->ofl, 0, sizeof(ud->ofl));
	ctx->user_data = ud;
	ud->default_fout = default_fout;
	ud->fout = fout;
	ud->cwd = cwd;

	for(;;) {
		int c;
		c = fgetc(fin);
		if (c == EOF)
			break;
		tmpasm_gotchar(ctx, c);
	}

}

void scc_tmpasm_parse(tmpasm_t *ctx, const char *cwd, FILE *fin, FILE *fout)
{
	scc_tmpasm_parse_(ctx, cwd, fin, fout, fout);
}

#ifndef NO_FILE_IO
static void instr_include(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	scc_t *ud = (scc_t *)ctx->user_data;
	int n;
	(void) iname;  /* not used */

	for(n = 0; n < argc; n++) {
		char *fn, *path;
		FILE *fin;
		tmpasm_t *child;

		fn = tmpasm_arg2str(ctx, argv[n], 0);
		path = scc_path(ud, fn);
		fin = fopen(path, "r");
		if (fin == NULL) {
			tmpasm_runtime_error(ctx, -8, path);
			free(fn);
			free(path);
			return;
		}
		child = tmpasm_init(ctx->cb);
		scc_tmpasm_parse_(child, ud->cwd, fin, ud->default_fout, ud->fout);
		tmpasm_execute(child);
		if (print_runtime_error(child, path) != 0)
			tmpasm_runtime_error(ctx, -7, path);
		tmpasm_uninit(child);
		fclose(fin);
		free(fn);
		free(path);
	}
}


static void instr_redir(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	char *path, *fn, *mode;
	scc_t *ud = (scc_t *)ctx->user_data;
	(void) iname;  /* not used */
	fflush(ud->fout);
	switch(argc) {
		case 0: ud->fout = ud->default_fout; return;  /* set redirection to default */
		case 1: mode = strclone("w"); break;
		case 2: mode = tmpasm_arg2str(ctx, argv[1], 0); break;
		default:
			tmpasm_runtime_error(ctx, -5, NULL);
			return;
	}

	fn = tmpasm_arg2str(ctx, argv[0], 0);
	path = scc_path(ud, fn);
	ud->fout = openfile_open(&ud->ofl, path, mode);
	if (ud->fout == NULL) {
		char *err = malloc(strlen(fn) + strlen(path) + strlen(mode) + 16);
		sprintf(err, "%s (%s) for %s", path, fn, mode);
		tmpasm_runtime_error(ctx, -6, err);
		free(err);
		free(path);
		return;
	}
	free(fn);
	free(mode);
	free(path);
}
#endif

#ifdef TMPASM_TESTER
static void instr_unknown(tmpasm_t *ctx, char *iname, int argc, tmpasm_arg_t *argv[])
{
	printf("ERROR: unknown instruction '%s'\n", iname);
}
#endif


/******** interface ********/

tmpasm_instr *scc_resolve(tmpasm_t *ctx, const char *name)
{
	(void) ctx;  /* not used */
/* TODO: make this a hash */
	if (strcmp(name, "put") == 0)
		return instr_put;
	if (strcmp(name, "resolve") == 0)
		return instr_resolve;
	if (strcmp(name, "append") == 0)
		return instr_append;
	if (strcmp(name, "print") == 0)
		return instr_print;
	if (strcmp(name, "print_ternary") == 0)
		return instr_print_ternary;
#ifndef TMPASM_NO_FILE_IO
	if (strcmp(name, "redir") == 0)
		return instr_redir;
	if (strcmp(name, "include") == 0)
		return instr_include;
#endif
	if (strcmp(name, "report") == 0)
		return instr_report;
	if (strcmp(name, "abort") == 0)
		return instr_abort;
	if (strcmp(name, "halt") == 0)
		return instr_halt;
	if (strcmp(name, "uniq") == 0)
		return instr_uniq;
	if (strcmp(name, "order") == 0)
		return instr_order;
	if (strcmp(name, "sortuniq") == 0)
		return instr_uniq;
	if ((strcmp(name, "sub") == 0) || (strcmp(name, "gsub") == 0))
		return instr_sub;

#ifndef TMPASM_TESTER
	return NULL;
#else
	return instr_unknown;
#endif
}


static const char *scc_err_fmt(tmpasm_t *ctx)
{
	int code;
	code = -ctx->runtime_error;

	if ((code < 0) || ((size_t)code > (sizeof(scc_runtime_error_fmts)/sizeof(char *))))
		return NULL;
	return scc_runtime_error_fmts[code];
}


static void scc_preexec(tmpasm_t *ctx)
{
	(void) ctx;  /* not used */
	db_mkdir(TMPASM_PATH);
}

static void scc_postexec(tmpasm_t *ctx)
{
	scc_t *ud = (scc_t *)ctx->user_data;
	openfile_free(&ud->ofl);
	free(ud);
}

tmpasm_cb_t scc_cb = {
	scc_get, scc_set, scc_is_true, scc_match, scc_first, scc_next,
	scc_resolve, scc_preexec, scc_postexec, scc_err_fmt
};

int tmpasm(const char *wdir, const char *input, const char *output)
{
	tmpasm_t *ctx;
	FILE *fin, *fout;
	int ret;
	scc_t ud_tmp;
	char *path;

	ud_tmp.cwd = wdir;

	path = scc_path(&ud_tmp, input);
	fin = fopen(path, "r");
	if (fin == NULL) {
		fprintf(stderr, "ERROR: tmpasm: can not open script '%s' (%s in %s)\n", path, input, wdir);
		free(path);
		return -1;
	}
	free(path);

	path = scc_path(&ud_tmp, output);
	fout = fopen(path, "w");
	if (fout == NULL) {
		fprintf(stderr, "ERROR: tmpasm: can not open output '%s' (%s in %s)\n", path, output, wdir);
		free(path);
		return -1;
	}
	free(path);

	ctx = tmpasm_init(&scc_cb);
	scc_tmpasm_parse_(ctx, wdir, fin, fout, fout);
	if (!ctx->dead)
		tmpasm_execute(ctx);
	fclose(fin);
	fclose(fout);

	ret = print_runtime_error(ctx, input);

	tmpasm_uninit(ctx);
	return ret;
}

FILE *tmpasm_fout(tmpasm_t *ctx)
{
	scc_t *ud = (scc_t *)ctx->user_data;
	return ud->fout;
}