File: rexp.c

package info (click to toggle)
mawk 1.3.4.20260129-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,244 kB
  • sloc: ansic: 19,998; sh: 4,627; yacc: 1,182; awk: 903; makefile: 301
file content (790 lines) | stat: -rw-r--r-- 19,872 bytes parent folder | download
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
/********************************************
rexp.c
copyright 2008-2025,2026, Thomas E. Dickey
copyright 1991-1993,1996, 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.
********************************************/

/*
 * $MawkId: rexp.c,v 1.63 2026/01/27 13:47:19 tom Exp $
 */

/*  op precedence  parser for regular expressions  */

#include <rexp.h>
#include <regexp.h>

/*  DATA   */
int REerrno;
const char *const REerrlist[] =
{(char *) 0,
 /* ERR_1  */ "missing '('",
 /* ERR_2  */ "missing ')'",
 /* ERR_3  */ "bad class -- [], [^] or [",
 /* ERR_4  */ "missing operand",
 /* ERR_5  */ "resource exhaustion -- regular expression too large",
 /* ERR_6  */ "syntax error ^* or ^+",
 /* ERR_7  */ "bad interval expression",
 /* ERR_8  */ ""
};
/* ERR_5 is very unlikely to occur */

/* This table drives the operator precedence parser */
/* *INDENT-OFF* */
#ifdef NO_INTERVAL_EXPR
static  short  table[8][8] = {
/*        0      |      CAT     *      +      ?      (      )   */
/* 0 */  {0,     OP_L,  OP_L,   OP_L,  OP_L,  OP_L,  OP_L,  ERR_1},
/* | */  {OP_G,  OP_G,  OP_L,   OP_L,  OP_L,  OP_L,  OP_L,  OP_G},
/* CAT*/ {OP_G,  OP_G,  OP_G,   OP_L,  OP_L,  OP_L,  OP_L,  OP_G},
/* * */  {OP_G,  OP_G,  OP_G,   OP_G,  OP_G,  OP_G,  ERR_7, OP_G},
/* + */  {OP_G,  OP_G,  OP_G,   OP_G,  OP_G,  OP_G,  ERR_7, OP_G},
/* ? */  {OP_G,  OP_G,  OP_G,   OP_G,  OP_G,  OP_G,  ERR_7, OP_G},
/* ( */  {ERR_2, OP_L,  OP_L,   OP_L,  OP_L,  OP_L,  OP_L,  OP_EQ},
/* ) */  {OP_G , OP_G,  OP_G,   OP_G,  OP_G,  OP_G,  ERR_7, OP_G}};
#else
static  short  table[10][10]  =  {
/*       0       |      CAT    *      +      ?      (      )      {      }  */
/* 0 */  {0,     OP_L,  OP_L,  OP_L,  OP_L,  OP_L,  OP_L,  ERR_1, ERR_7, OP_L},
/* | */  {OP_G,  OP_G,  OP_L,  OP_L,  OP_L,  OP_L,  OP_L,  OP_G,  OP_G,  OP_G},
/* CAT*/ {OP_G,  OP_G,  OP_G,  OP_L,  OP_L,  OP_L,  OP_L,  OP_G,  OP_L,  OP_G},
/* * */  {OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  ERR_7, OP_G,  OP_G,  OP_G},
/* + */  {OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  ERR_7, OP_G,  OP_G,  OP_G},
/* ? */  {OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  ERR_7, OP_G,  OP_G,  OP_G},
/* ( */  {ERR_2, OP_L,  OP_L,  OP_L,  OP_L,  OP_L,  OP_L,  OP_EQ, OP_G,  OP_G},
/* ) */  {OP_G , OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  ERR_7, OP_G,  ERR_7, OP_G},
/* { */  {OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  ERR_7, OP_G,  OP_G,  OP_EQ},
/* } */  {OP_G , OP_G,  OP_G,  OP_G,  OP_G,  OP_G,  ERR_7, OP_G,  ERR_7, OP_G}   };
#endif
/* *INDENT-ON* */

#define	 STACKSZ   64

static const char *REs_type(STATE * p);

static jmp_buf err_buf;		/* used to trap on error */

#if OPT_TRACE > 0
static const char *
token_name(int token)
{
    const char *result;
#define CASE(name) case name: result = #name; break
    switch (token) {
	CASE(T_NONE);
	CASE(T_OR);
	CASE(T_CAT);
	CASE(T_STAR);
	CASE(T_PLUS);
	CASE(T_Q);
	CASE(T_LP);
	CASE(T_RP);
	CASE(T_START);
	CASE(T_END);
	CASE(T_ANY);
	CASE(T_CLASS);
	CASE(T_SLASH);
	CASE(T_CHAR);
	CASE(T_STR);
#ifndef NO_INTERVAL_EXPR
	CASE(T_LB);
	CASE(T_RB);
#endif
	CASE(T_U);
    default:
	result = "?";
	break;
    }
#undef CASE
    return result;
}
#endif

void
RE_error_trap(int x)
{
    TRACE(("RE_error_trap(%d)\n", x));
    REerrno = x;
    longjmp(err_buf, 1);
}

typedef struct {
    int token;
    int prec;
} OPS;

#ifndef NO_INTERVAL_EXPR
#define MAX_LOOP_LEVEL 10	/* this would be very complex... */
static int used_loop_level;	/* used to flag post-processing step */

/* duplicate a machine, oldmp into newmp */
static void
duplicate_m(MACHINE * newmp, MACHINE * oldmp)
{
    register STATE *p;
    TRACE(("duplicate_m %p -> %p\n", (void *) oldmp, (void *) newmp));
    TRACE(("...start %p\n", (void *) oldmp->start));
    TRACE(("...stop  %p\n", (void *) oldmp->stop));
    p = (STATE *) RE_malloc(2 * STATESZ);
    RE_copy_states(p, oldmp->start, 2);
    newmp->start = (STATE *) p;
    newmp->stop = (STATE *) (p + 1);
}

extern FILE *trace_fp;
/*
 * Find the end of the last-created loop, i.e., with M_2JC, and replace that
 * with an M_LOOP with the given limits.  Also:
 *
 * (a) resize the machine and insert a M_ENTER before the M_SAVE_POS which
 *     is at the beginning of the M_2JC loop.
 * (b) replace any nested loops within this updated loop, so that the whole
 *     stack will use M_LOOP consistently.
 *
 * Because this is applied to the last-created loop, it is not necessary to
 * adjust jump-offsets to following loops which could span this loop.  But it
 * is necessary to adjust jumps which precede (i.e., jump over) this loop.
 *
 * If this is called with nonzero `unrolled', then there is an additional
 * adjustment to make:
 * (a) the cells between the M_SAVE_POS M_2JC are the unrolled part which
 * (b) has to be moved before the loop's M_ENTER and M_SAVE_POS, and
 * (c) an M_2JA to branch around the whole thing in case of mismatch,
 *     since the unrolled+loop cells must be treated as a whole.
 */
static void
RE_set_limit(MACHINE * mp, Int minlimit, Int maxlimit, Int unrolled)
{
    STATE *p = mp->start;
    STATE *last_1st = NULL;
    STATE *last_end = NULL;
    STATE *temp = NULL;
    int nests = 0;

    TRACE(("RE_set_limit " INT_FMT ".." INT_FMT "\n", minlimit, maxlimit));
    TRACE(("... unrolled %ld (%ld)\n", unrolled, 1 + mp->stop - mp->start));

    if (p->s_type == M_2JA)
	++p;

    if (p->s_type == M_SAVE_POS) {
	int depth = 0;
	temp = p;
	do {
	    switch (temp->s_type) {
	    case M_SAVE_POS:
		if (depth++ == 0)
		    last_1st = temp;
		break;
	    case M_2JC:
		if (depth > 1)
		    ++nests;
		/* FALLTHRU */
	    case M_LOOP:
		if (--depth == 0) {
		    last_end = temp;
		}
		break;
	    case M_ACCEPT:
		depth = -1;
		break;
	    }
	    ++temp;
	} while (depth > 0);
    }
    /*
     * If we found the end of a top-level loop (i.e., the M_SAVE_POS),
     * we can modify it.
     */
    if (last_end != NULL) {
	/* *INDENT-EQLS* */
	size_t bypass = (unrolled != 0) ? 1 : 0;
	size_t len    = (size_t) (mp->stop - mp->start + 2);
	size_t base   = (size_t) (last_1st - mp->start);
	size_t newlen = (size_t) (1 + len + (size_t) nests + bypass);
	int offset    = (int) (last_end - mp->start);

	TRACE(("len    %ld\n", len));
	TRACE(("base   %ld\n", base));
	TRACE(("newlen %ld\n", newlen));
	TRACE(("offset %d\n", offset));

	last_end->s_type = M_LOOP;
	last_end->it_min = minlimit;
	last_end->it_max = maxlimit;
	last_end->s_enter = -(offset + 1);
	last_end->s_enter += (int) base;

	/*
	 * Reallocate the states, to insert an item at the beginning.
	 *
	 * The new size accounts for any nested loops which we found, but the
	 * stop-pointer is set for the current length of the top loop.
	 */
	mp->start = (STATE *) RE_realloc(mp->start, newlen * STATESZ);
	mp->stop = mp->start + len - 1 + (unrolled ? 1 : 0);
	temp = mp->start + base;
	len -= base;
	while (--len != 0) {
	    temp[len] = temp[len - 1];
	}
	temp->s_type = M_ENTER;
	temp->s_data.jump = (int) ((size_t) (offset + 1) - base);
	used_loop_level = 1;

	/*
	 * If unrolled, there is an extra cell to use in the loop.  Shift the
	 * the extra cell up, and reinsert the entry and saved cells.
	 */
	if (unrolled) {
	    int adjusts = (int) unrolled;
	    STATE entry = temp[0];
	    STATE saved = temp[1];
	    STATE jumps;
	    int n;

	    entry.s_data.jump -= adjusts;

	    for (n = 0; n < adjusts; ++n) {
		temp[0] = temp[2];
		++temp;
	    }

	    /* conditional relative jump past the M_LOOP */
	    jumps.s_type = M_2JA;
	    jumps.s_data.jump = (int) ((size_t) 5);
	    *temp++ = jumps;

	    temp[2].s_data.jump += adjusts;	/* M_LOOP */
	    temp[2].s_enter += adjusts;

	    for (n = 4; n > 1; --n) {
		temp[n] = temp[n - 1];
	    }
	    temp[1] = saved;
	    temp[0] = entry;
	}

	/* if there were jumps over the adjusted loop, adjust them */
	if (base) {
	    int n;
	    temp = mp->start;
	    for (n = 0; n < (int) base; ++n) {
		switch (temp[n].s_type) {
		case M_1J:
		case M_2JA:
		case M_2JB:
		    if ((size_t) (n + temp[n].s_data.jump) > base)
			temp[n].s_data.jump++;
		    break;
		}
	    }
	}

	/*
	 * Transform nested loops ending with M_2JC (+), to M_LOOP {1,}
	 * Exclude for now any which are preceded by M_2JA (*), which is
	 * not yet handled for M_LOOP (2025-01-31).
	 */
	while (nests > 0) {
	    int probe;
	    int inner;
	    int outer;
	    int ender;
	    int check;
	    int oldlen = (int) (mp->stop - mp->start + 1);
	    p = mp->start;
	    /*
	     * Look for a loop to expand.
	     */
	    for (probe = oldlen; probe != 0; --probe) {
		if (p[probe - 1].s_type == M_2JC) {
		    --probe;
		    inner = probe + p[probe].s_data.jump;
		    if (inner != 0
			&& p[inner].s_type == M_SAVE_POS
			&& p[inner - 1].s_type == M_2JA) {
			nests--;
			continue;
		    }
		    /*
		     * Adjust jumps across the loop which will be expanded.
		     */
		    for (outer = 0; outer < oldlen && outer < probe; ++outer) {
			switch (p[outer].s_type) {
			case M_2JA:
			    break;
			case M_ENTER:
			    ender = outer + p[outer].s_data.jump;
			    check = ender + p[ender].s_enter;
			    if (ender > probe
				&& check == outer) {
				p[outer].s_data.jump++;
				p[ender].s_enter--;
			    }
			    break;
			}
		    }
		    for (outer = probe + 1; outer < oldlen; ++outer) {
			switch (p[outer].s_type) {
			case M_LOOP:
			    ender = outer + p[outer].s_data.jump;
			    if (ender < probe)
				p[outer].s_data.jump--;
			    break;
			}
		    }
		    /*
		     * Now, expand the loop we found.
		     */
		    p[probe].s_type = M_LOOP;
		    p[probe].it_min = 1;
		    p[probe].it_max = MAX__INT;
		    p[probe].s_enter = (int) (inner - probe - 1);
		    for (outer = oldlen + 1; outer != inner; --outer) {
			p[outer] = p[outer - 1];
		    }
		    p[inner].s_type = M_ENTER;
		    p[inner].s_data.jump = probe - inner + 1;
		    mp->stop++;
		    /*
		     * Find any remaining jumps spanning the adjusted loop,
		     * and adjust those as well.
		     */
		    while (inner != 0) {
			switch (p[inner].s_type) {
			case M_1J:
			case M_2JA:
			case M_2JB:
			case M_2JC:
			    if (inner + p[inner].s_data.jump >= probe) {
				p[inner].s_data.jump++;
			    }
			    break;
			}
			--inner;
		    }
		    /*
		     * Done for now, look for remaining loops.
		     */
		    break;
		}
	    }
	    --nests;
	}
	for (p = mp->start, nests = 0; p != mp->stop; ++p) {
	    switch (p->s_type) {
	    case M_ENTER:
		p->it_cnt = ++nests;
		break;
	    case M_LOOP:
		--nests;
		break;
	    }
	}
    }
#if OPT_TRACE
    REmprint(mp->start, trace_fp);
#endif
}

/*  replace m with m*  limited to the max iterations
        (variation of m*   closure)   */
static void
RE_close_limit(MACHINE * mp, Int min_limit, Int max_limit)
{
    RE_close(mp);
    RE_set_limit(mp, min_limit, max_limit, 0);
}

/*  replace m with m+  limited to the max iterations
     which is one or more, limited
        (variation of m+   positive closure)   */
static void
RE_poscl_limit(MACHINE * mp, Int min_limit, Int max_limit, Int unrolled)
{
    RE_poscl(mp);
    RE_set_limit(mp, min_limit, max_limit, unrolled);
}

/* If we used M_ENTER/M_LOOP, set the level-number for M_ENTER */
static STATE *
markup_loop_levels(MACHINE * mp)
{
    STATE *p = mp->start;
    if (used_loop_level && p != NULL) {
	STATE *q = p;
	STATE *r;
	int level = 0;
	int done = 0;
	while (!done && q != mp->stop) {
	    switch (q->s_type) {
	    case M_ACCEPT:
		done = 1;
		break;
	    case M_ENTER:
		q->it_cnt = ++level;
		if (level > MAX_LOOP_LEVEL)
		    compile_error("brace expression exceeds %d levels\n",
				  MAX_LOOP_LEVEL);
		break;
	    case M_LOOP:
		r = (q + q->s_enter);
		if (level-- != (int) r->it_cnt)
		    compile_error("mismatched levels for brace-expression");
		break;
	    }
	    ++q;
	}
    }
    return p;
}
#else
#define markup_loop_levels(mp) (mp)->start
#endif /* ! NO_INTERVAL_EXPR */

/* duplicate_m() relies upon copying machines whose size is 1, i.e., atoms */
#define BigMachine(mp) (((mp)->stop - (mp)->start) > 1)

STATE *
REcompile(char *re, size_t len)
{
#define m_stack(n) &m_array[(n) + 1]
    MACHINE m_array[1 + STACKSZ];
    OPS op_stack[STACKSZ];
    register MACHINE *m_ptr;
    register OPS *op_ptr;
    register int t;

    TRACE(("REcompile %.*s\n", (int) len, re));
    /* do this first because it also checks if we have a
       run time stack */
    RE_lex_init(re, len);

    if (len == 0) {
	STATE *p = (STATE *) RE_malloc(sizeof(STATE));
	p->s_type = M_ACCEPT;
	return p;
    }

    if (setjmp(err_buf))
	return NULL;
    /* we used to try to recover memory left on machine stack ;
       but now m_ptr is in a register so it won't be right unless
       we force it out of a register which isn't worth the trouble */

    /* initialize the stacks  */
    m_ptr = m_array;
    op_ptr = op_stack;
    op_ptr->token = 0;

    t = RE_lex(m_stack(0));
    memset(m_ptr, 0, sizeof(*m_ptr));

#ifndef NO_INTERVAL_EXPR
    used_loop_level = 0;
#endif

    /* provide for making the trace a little easier to read by indenting */
#if OPT_TRACE > 1
#define M_FMT(format) "@%d: %*s " format, __LINE__, 4 * ((int) (m_ptr - m_array)), " "
#else
#define M_FMT(format) format
#endif

    while (1) {
	TRACE((M_FMT("RE_lex token %s\n"), token_name(t)));
	switch (t) {
	case T_STR:
	case T_ANY:
	case T_U:
	case T_START:
	case T_END:
	case T_CLASS:
	    m_ptr++;
	    break;

#ifndef NO_INTERVAL_EXPR
	case T_RB:
	    if (!repetitions_flag) {
		goto default_case;
	    }
	    /* interval expression {n,m}
	     * eg,
	     *   convert m{3} to mmm
	     *   convert m{3,} to mmm* (with a limit of MAX_INT)
	     *   convert m{3,10} to mmm* with a limit of 10
	     */
	    TRACE((M_FMT("interval {%ld,%ld}\n"), (long) intrvalmin, (long) intrvalmax));
	    if ((m_ptr - m_array) < STACKSZ)
		memset(m_ptr + 1, 0, sizeof(*m_ptr));
	    if (intrvalmin == 0) {	/* zero or more */
		switch (intrvalmax) {
		case 0:
		    /* user stupidity: m{0} or m{0,0}
		     * don't add this re token
		     */
		    if (m_ptr == m_array) {
			t = RE_lex(++m_ptr);
			if (t != T_NONE) {
			    continue;
			} else {
			    m_array[1] = RE_any();	/* FIXME: RE_none? */
			    m_ptr = m_stack(0);
			}
		    } else if (op_ptr != op_stack) {
			/* no previous re */
			RE_free(m_ptr->start);
			m_ptr--;
			switch (op_ptr->token) {
			case T_RP:
			    while (op_ptr != op_stack) {
				--op_ptr;
				if (op_ptr->token == T_LP) {
				    if (op_ptr == op_stack) {
					op_ptr->token = T_NONE;
				    }
				    break;
				}
			    }
			    op_ptr = op_stack + 1;
			    break;
			case T_LP:
			    break;
			default:
			    op_ptr--;
			    break;
			}
		    } else if (*re_exp == '\0') {
			/* this was the only re expr
			   so leave one M_ACCEPT as the machine */
			m_ptr->start->s_type = M_ACCEPT;
		    } else {
			RE_free(m_ptr->start);
			m_ptr--;
		    }
		    TRACE((M_FMT("RE_lex token %s\n"),
			   "of zero interval is ignored!"));
		    break;
		case 1:
		    RE_01(m_ptr);	/* m{0,1} which is m? */
		    TRACE((M_FMT("RE_lex token %s\n"), token_name(T_Q)));
		    break;
		default:
		    RE_close_limit(m_ptr, intrvalmin, intrvalmax);
		    TRACE((M_FMT("RE_lex token %s\n"), token_name(T_Q)));
		}
	    } else if (BigMachine(m_ptr)) {
		RE_poscl_limit(m_ptr, intrvalmin, intrvalmax, 0);
	    } else if (intrvalmin == 1) {	/* one or more */
		RE_poscl_limit(m_ptr, intrvalmin, intrvalmax, 0);
	    } else if (m_ptr->start != NULL) {	/* n or more */
		/* loop-unrolling works best if min==max, so that the loops in
		 * test/match functions can process the whole loop in each
		 * iteration */
		if ((intrvalmin <= intrvalmax) && (intrvalmin > 1)) {
		    register Int i;
		    Int splice = (intrvalmin == intrvalmax) ? 2 : 1;
		    /* copy 2 copies of m_ptr, use 2nd copy to replace
		       the first copy that gets swallowed by concat */
		    MACHINE *result_mp = m_ptr;
		    MACHINE *concat_mp = (m_ptr + 1);
		    MACHINE *new_mp = (m_ptr + 2);
		    TRACE((M_FMT("calling duplicate_m result_mp %ld -> concat_mp %ld\n"),
			   result_mp - m_array,
			   concat_mp - m_array));
		    duplicate_m(concat_mp, result_mp);
		    TRACE((M_FMT("calling duplicate_m result_mp %ld -> new_mp %ld\n"),
			   result_mp - m_array,
			   new_mp - m_array));
		    duplicate_m(new_mp, result_mp);
		    for (i = splice; i <= intrvalmin; i++) {
			RE_cat(result_mp, concat_mp);
			duplicate_m(concat_mp, new_mp);
		    }
		    /* don't need 2nd copy in new_mp */
		    RE_free(new_mp->start);

		    /*
		     * After unrolling the loop, replace any remainder with a
		     * loop.
		     */
		    if (intrvalmin < intrvalmax) {
			Int unrolled = intrvalmin;
			if (intrvalmax < MAX__INT)
			    intrvalmax -= intrvalmin;
			intrvalmin = 0;
			RE_poscl_limit(m_ptr, intrvalmin, intrvalmax, unrolled);
		    }
		} else {
		    /*
		     * Handle this as a loop, no unrolling.
		     */
		    RE_poscl_limit(m_ptr, intrvalmin, intrvalmax, 0);
		}
	    }
	    break;
#endif /* ! NO_INTERVAL_EXPR */

	case T_NONE:		/*  end of reg expr   */
	    if (op_ptr->token == 0) {
		/*  done   */
		if (m_ptr == m_stack(0)) {
		    return markup_loop_levels(m_ptr);
		} else {
		    /* machines still on the stack  */
		    RE_panic("values still on machine stack for %s", re);
		}
	    }
	    /* FALLTHRU */

	    /*  otherwise, default is operator case  */

	default:
#ifndef NO_INTERVAL_EXPR
	  default_case:
#endif

	    if ((op_ptr->prec = table[op_ptr->token][t]) == OP_G) {
		do {		/* op_pop   */

		    if (op_ptr->token <= T_CAT) {	/*binary op */
			if (m_ptr == m_stack(0)
			    && op_ptr->token == T_CAT) {
			    TRACE(("...ignoring empty T_CAT\n"));
			    op_ptr--;
			    continue;
			}
			m_ptr--;
		    }
		    /* if not enough values on machine stack
		       then we have a missing operand */
		    if (m_ptr == m_array)
			RE_error_trap(-ERR_4);

		    switch (op_ptr->token) {
		    case T_CAT:
			RE_cat(m_ptr, m_ptr + 1);
			break;

		    case T_OR:
			RE_or(m_ptr, m_ptr + 1);
			break;

		    case T_STAR:
			RE_close(m_ptr);
			break;

		    case T_PLUS:
			RE_poscl(m_ptr);
			break;

		    case T_Q:
			RE_01(m_ptr);
			break;

		    default:
			/*nothing on ( or ) */
			break;
		    }

		    op_ptr--;
		}
		while (op_ptr->prec != OP_L);

		continue;	/* back thru switch at top */
	    }

	    if (op_ptr->prec < 0) {
		if (op_ptr->prec == ERR_7)
		    RE_panic("parser returns ERR_7");
		else
		    RE_error_trap(-op_ptr->prec);
	    }

	    if (++op_ptr == op_stack + STACKSZ) {
		/* stack overflow */
		RE_error_trap(-ERR_5);
	    }

	    op_ptr->token = t;
	}			/* end of switch */

	if (m_ptr >= m_stack(STACKSZ - 1)) {
	    /*overflow */
	    RE_error_trap(-ERR_5);
	}

	t = RE_lex(m_ptr + 1);
    }
}

#ifdef NO_LEAKS
void
REdestroy(STATE * ptr)
{
    int done = 0;
    int n = 0;
    STATE *q = ptr;

    TRACE(("REdestroy %p\n", (void *) ptr));
    while (!done) {
	TRACE(("...destroy[%d] %p type %s\n", n, (void *) q, REs_type(q)));
	switch (q->s_type) {
	case M_ACCEPT:
	    done = 1;
	    break;
	case M_STR:
	    RE_free(q->s_data.str);
	    break;
	default:
	    if (q->s_type < 0 || q->s_type > END_ON)
		done = -1;
	    break;
	}
	++q;
	++n;
    }
    RE_free(ptr);
}
#endif /* NO_LEAKS */

/* getting here means a logic flaw or unforeseen case */
void
RE_panic(const char *format, ...)
{
    const char *where = "REcompile() - panic:  ";
    va_list args;

    fflush(stdout);

#if OPT_TRACE > 0
    va_start(args, format);
    Trace("?? %s", where);
    TraceVA(format, args);
    Trace("\n");
    va_end(args);
#endif

    fputs(where, stderr);

    va_start(args, format);
    vfprintf(stderr, format, args);
    va_end(args);

    fprintf(stderr, "\n");

    mawk_exit(100);
}

/* getting regexp error message */
const char *
REerror(void)
{
    return REerrlist[REerrno];
}