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
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
|
/* $OpenBSD: compile.c,v 1.42 2017/08/01 18:05:53 martijn Exp $ */
/*-
* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Diomidis Spinellis of Imperial College, University of London.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "defs.h"
#include "extern.h"
#define LHSZ 128
#define LHMASK (LHSZ - 1)
static struct labhash {
struct labhash *lh_next;
u_int lh_hash;
struct s_command *lh_cmd;
int lh_ref;
} *labels[LHSZ];
static char *compile_addr(char *, struct s_addr *);
static char *compile_ccl(char **, char *);
static char *compile_delimited(char *, char *, int);
static char *compile_flags(char *, struct s_subst *);
static char *compile_re(char *, regex_t **);
static char *compile_subst(char *, struct s_subst *);
static char *compile_text(void);
static char *compile_tr(char *, char **);
static struct s_command
**compile_stream(struct s_command **);
static char *duptoeol(char *, char *, char **);
static void enterlabel(struct s_command *);
static struct s_command
*findlabel(char *);
static void fixuplabel(struct s_command *, struct s_command *);
static void uselabel(void);
/*
* Command specification. This is used to drive the command parser.
*/
struct s_format {
char code; /* Command code */
int naddr; /* Number of address args */
enum e_args args; /* Argument type */
};
static struct s_format cmd_fmts[] = {
{'{', 2, GROUP},
{'}', 0, ENDGROUP},
{'a', 1, TEXT},
{'b', 2, BRANCH},
{'c', 2, TEXT},
{'d', 2, EMPTY},
{'D', 2, EMPTY},
{'g', 2, EMPTY},
{'G', 2, EMPTY},
{'h', 2, EMPTY},
{'H', 2, EMPTY},
{'i', 1, TEXT},
{'l', 2, EMPTY},
{'n', 2, EMPTY},
{'N', 2, EMPTY},
{'p', 2, EMPTY},
{'P', 2, EMPTY},
{'q', 1, EMPTY},
{'r', 1, RFILE},
{'s', 2, SUBST},
{'t', 2, BRANCH},
{'w', 2, WFILE},
{'x', 2, EMPTY},
{'y', 2, TR},
{'!', 2, NONSEL},
{':', 0, LABEL},
{'#', 0, COMMENT},
{'=', 1, EMPTY},
{'\0', 0, COMMENT},
};
/* The compiled program. */
struct s_command *prog;
/*
* Compile the program into prog.
* Initialise appends.
*/
void
compile(void)
{
*compile_stream(&prog) = NULL;
fixuplabel(prog, NULL);
uselabel();
appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends));
match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t));
}
#define EATSPACE() do { \
if (p) \
while (isascii((unsigned char)*p) && \
isspace((unsigned char)*p)) \
p++; \
} while (0)
static struct s_command **
compile_stream(struct s_command **link)
{
char *p;
static char *lbuf; /* To avoid excessive malloc calls */
static size_t bufsize;
struct s_command *cmd, *cmd2, *stack;
struct s_format *fp;
int naddr; /* Number of addresses */
stack = 0;
for (;;) {
if ((p = cu_fgets(&lbuf, &bufsize)) == NULL) {
if (stack != 0)
error(COMPILE, "unexpected EOF (pending }'s)");
return (link);
}
semicolon: EATSPACE();
if (*p == '#' || *p == '\0')
continue;
if (*p == ';') {
p++;
goto semicolon;
}
*link = cmd = xmalloc(sizeof(struct s_command));
link = &cmd->next;
cmd->nonsel = cmd->inrange = 0;
/* First parse the addresses */
naddr = 0;
/* Valid characters to start an address */
#define addrchar(c) (strchr("0123456789/\\$", (c)))
if (addrchar(*p)) {
naddr++;
cmd->a1 = xmalloc(sizeof(struct s_addr));
p = compile_addr(p, cmd->a1);
EATSPACE(); /* EXTENSION */
if (*p == ',') {
p++;
EATSPACE(); /* EXTENSION */
naddr++;
cmd->a2 = xmalloc(sizeof(struct s_addr));
p = compile_addr(p, cmd->a2);
EATSPACE();
} else {
cmd->a2 = 0;
}
} else {
cmd->a1 = cmd->a2 = 0;
}
nonsel: /* Now parse the command */
if (!*p)
error(COMPILE, "command expected");
cmd->code = *p;
for (fp = cmd_fmts; fp->code; fp++)
if (fp->code == *p)
break;
if (!fp->code)
error(COMPILE, "invalid command code %c", *p);
if (naddr > fp->naddr)
error(COMPILE,
"command %c expects up to %d address(es), found %d",
*p, fp->naddr, naddr);
switch (fp->args) {
case NONSEL: /* ! */
p++;
EATSPACE();
cmd->nonsel = 1;
goto nonsel;
case GROUP: /* { */
p++;
EATSPACE();
cmd->next = stack;
stack = cmd;
link = &cmd->u.c;
if (*p)
goto semicolon;
break;
case ENDGROUP:
/*
* Short-circuit command processing, since end of
* group is really just a noop.
*/
cmd->nonsel = 1;
if (stack == 0)
error(COMPILE, "unexpected }");
cmd2 = stack;
stack = cmd2->next;
cmd2->next = cmd;
/*FALLTHROUGH*/
case EMPTY: /* d D g G h H l n N p P q x = \0 */
p++;
EATSPACE();
if (*p == ';') {
p++;
link = &cmd->next;
goto semicolon;
}
if (*p)
error(COMPILE,
"extra characters at the end of %c command", cmd->code);
break;
case TEXT: /* a c i */
p++;
EATSPACE();
if (*p != '\\')
error(COMPILE, "command %c expects \\ followed by"
" text", cmd->code);
p++;
EATSPACE();
if (*p)
error(COMPILE, "extra characters after \\ at the"
" end of %c command", cmd->code);
cmd->t = compile_text();
break;
case COMMENT: /* \0 # */
break;
case WFILE: /* w */
p++;
EATSPACE();
if (*p == '\0')
error(COMPILE, "filename expected");
cmd->t = duptoeol(p, "w command", NULL);
if (aflag) {
cmd->u.fd = -1;
pledge_wpath = 1;
}
else if ((cmd->u.fd = open(p,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
DEFFILEMODE)) == -1)
error(FATAL, "%s: %s", p, strerror(errno));
break;
case RFILE: /* r */
pledge_rpath = 1;
p++;
EATSPACE();
cmd->t = duptoeol(p, "read command", NULL);
break;
case BRANCH: /* b t */
p++;
EATSPACE();
if (*p == '\0')
cmd->t = NULL;
else
cmd->t = duptoeol(p, "branch", &p);
if (*p == ';') {
p++;
goto semicolon;
}
break;
case LABEL: /* : */
p++;
EATSPACE();
cmd->t = duptoeol(p, "label", &p);
if (strlen(cmd->t) == 0)
error(COMPILE, "empty label");
enterlabel(cmd);
if (*p == ';') {
p++;
goto semicolon;
}
break;
case SUBST: /* s */
p++;
if (*p == '\0' || *p == '\\')
error(COMPILE, "substitute pattern can not be"
" delimited by newline or backslash");
cmd->u.s = xmalloc(sizeof(struct s_subst));
p = compile_re(p, &cmd->u.s->re);
if (p == NULL)
error(COMPILE, "unterminated substitute pattern");
--p;
p = compile_subst(p, cmd->u.s);
p = compile_flags(p, cmd->u.s);
EATSPACE();
if (*p == ';') {
p++;
link = &cmd->next;
goto semicolon;
}
break;
case TR: /* y */
p++;
p = compile_tr(p, (char **)&cmd->u.y);
EATSPACE();
if (*p == ';') {
p++;
link = &cmd->next;
goto semicolon;
}
if (*p)
error(COMPILE, "extra text at the end of a"
" transform command");
break;
}
}
}
/*
* Get a delimited string. P points to the delimeter of the string; d points
* to a buffer area. Newline and delimiter escapes are processed; other
* escapes are ignored.
*
* Returns a pointer to the first character after the final delimiter or NULL
* in the case of a non-terminated string. The character array d is filled
* with the processed string.
*/
static char *
compile_delimited(char *p, char *d, int is_tr)
{
char c;
c = *p++;
if (c == '\0')
return (NULL);
else if (c == '\\')
error(COMPILE, "\\ can not be used as a string delimiter");
else if (c == '\n')
error(COMPILE, "newline can not be used as a string delimiter");
while (*p) {
if (*p == '[' && *p != c) {
if ((d = compile_ccl(&p, d)) == NULL)
error(COMPILE, "unbalanced brackets ([])");
continue;
} else if (*p == '\\' && p[1] == '[') {
*d++ = *p++;
} else if (*p == '\\' && p[1] == c) {
p++;
} else if (*p == '\\' && p[1] == 'n') {
*d++ = '\n';
p += 2;
continue;
} else if (*p == '\\' && p[1] == '\\') {
if (is_tr)
p++;
else
*d++ = *p++;
} else if (*p == c) {
*d = '\0';
return (p + 1);
}
*d++ = *p++;
}
return (NULL);
}
/* compile_ccl: expand a POSIX character class */
static char *
compile_ccl(char **sp, char *t)
{
int c, d;
char *s = *sp;
*t++ = *s++;
if (*s == '^')
*t++ = *s++;
if (*s == ']')
*t++ = *s++;
for (; *s && (*t = *s) != ']'; s++, t++)
if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
*++t = *++s, t++, s++;
for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
if ((c = *s) == '\0')
return NULL;
} else if (*s == '\\' && s[1] == 'n') {
*t = '\n';
s++;
}
if (*s == ']') {
*sp = ++s;
return (++t);
} else {
return (NULL);
}
}
/*
* Get a regular expression. P points to the delimiter of the regular
* expression; repp points to the address of a regexp pointer. Newline
* and delimiter escapes are processed; other escapes are ignored.
* Returns a pointer to the first character after the final delimiter
* or NULL in the case of a non terminated regular expression. The regexp
* pointer is set to the compiled regular expression.
* Cflags are passed to regcomp.
*/
static char *
compile_re(char *p, regex_t **repp)
{
int eval;
char *re;
re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */
p = compile_delimited(p, re, 0);
if (p && strlen(re) == 0) {
*repp = NULL;
free(re);
return (p);
}
*repp = xmalloc(sizeof(regex_t));
if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0)
error(COMPILE, "RE error: %s", strregerror(eval, *repp));
if (maxnsub < (*repp)->re_nsub)
maxnsub = (*repp)->re_nsub;
free(re);
return (p);
}
/*
* Compile the substitution string of a regular expression and set res to
* point to a saved copy of it. Nsub is the number of parenthesized regular
* expressions.
*/
static char *
compile_subst(char *p, struct s_subst *s)
{
static char *lbuf;
static size_t bufsize;
int asize, ref, size;
char c, *text, *op, *sp;
int sawesc = 0;
c = *p++; /* Terminator character */
if (c == '\0')
return (NULL);
s->maxbref = 0;
s->linenum = linenum;
text = NULL;
asize = size = 0;
do {
size_t len = ROUNDLEN(strlen(p) + 1);
if (asize - size < len) {
do {
asize += len;
} while (asize - size < len);
text = xrealloc(text, asize);
}
op = sp = text + size;
for (; *p; p++) {
if (*p == '\\' || sawesc) {
/*
* If this is a continuation from the last
* buffer, we won't have a character to
* skip over.
*/
if (sawesc)
sawesc = 0;
else
p++;
if (*p == '\0') {
/*
* This escaped character is continued
* in the next part of the line. Note
* this fact, then cause the loop to
* exit w/ normal EOL case and reenter
* above with the new buffer.
*/
sawesc = 1;
p--;
continue;
} else if (strchr("123456789", *p) != NULL) {
*sp++ = '\\';
ref = *p - '0';
if (s->re != NULL &&
ref > s->re->re_nsub)
error(COMPILE,
"\\%c not defined in the RE", *p);
if (s->maxbref < ref)
s->maxbref = ref;
} else if (*p == '&' || *p == '\\')
*sp++ = '\\';
} else if (*p == c) {
p++;
*sp++ = '\0';
size += sp - op;
s->new = xrealloc(text, size);
return (p);
} else if (*p == '\n') {
error(COMPILE,
"unescaped newline inside substitute pattern");
}
*sp++ = *p;
}
size += sp - op;
} while ((p = cu_fgets(&lbuf, &bufsize)));
error(COMPILE, "unterminated substitute in regular expression");
}
/*
* Compile the flags of the s command
*/
static char *
compile_flags(char *p, struct s_subst *s)
{
int gn; /* True if we have seen g or n */
long l;
char wfile[PATH_MAX], *q, *eq;
s->n = 1; /* Default */
s->p = 0;
s->wfile = NULL;
s->wfd = -1;
for (gn = 0;;) {
EATSPACE(); /* EXTENSION */
switch (*p) {
case 'g':
if (gn)
error(COMPILE, "more than one number or 'g' in"
" substitute flags");
gn = 1;
s->n = 0;
break;
case '\0':
case '\n':
case ';':
return (p);
case 'p':
s->p = 1;
break;
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
if (gn)
error(COMPILE, "more than one number or 'g' in"
" substitute flags");
gn = 1;
l = strtol(p, &p, 10);
if (l <= 0 || l >= INT_MAX)
error(COMPILE,
"number in substitute flags out of range");
s->n = (int)l;
continue;
case 'w':
p++;
#ifdef HISTORIC_PRACTICE
if (*p != ' ') {
warning("space missing before w wfile");
return (p);
}
#endif
EATSPACE();
q = wfile;
eq = wfile + sizeof(wfile) - 1;
while (*p) {
if (*p == '\n')
break;
if (q >= eq)
error(COMPILE, "wfile too long");
*q++ = *p++;
}
*q = '\0';
if (q == wfile)
error(COMPILE, "no wfile specified");
s->wfile = strdup(wfile);
if (aflag)
pledge_wpath = 1;
else if ((s->wfd = open(wfile,
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
DEFFILEMODE)) == -1)
error(FATAL, "%s: %s", wfile, strerror(errno));
return (p);
default:
error(COMPILE,
"bad flag in substitute command: '%c'", *p);
break;
}
p++;
}
}
/*
* Compile a translation set of strings into a lookup table.
*/
static char *
compile_tr(char *p, char **transtab)
{
int i;
char *lt, *op, *np;
char *old = NULL, *new = NULL;
if (*p == '\0' || *p == '\\')
error(COMPILE,
"transform pattern can not be delimited by newline or backslash");
old = xmalloc(strlen(p) + 1);
p = compile_delimited(p, old, 1);
if (p == NULL) {
error(COMPILE, "unterminated transform source string");
goto bad;
}
new = xmalloc(strlen(p) + 1);
p = compile_delimited(--p, new, 1);
if (p == NULL) {
error(COMPILE, "unterminated transform target string");
goto bad;
}
EATSPACE();
if (strlen(new) != strlen(old)) {
error(COMPILE, "transform strings are not the same length");
goto bad;
}
/* We assume characters are 8 bits */
lt = xmalloc(UCHAR_MAX + 1);
for (i = 0; i <= UCHAR_MAX; i++)
lt[i] = (char)i;
for (op = old, np = new; *op; op++, np++)
lt[(u_char)*op] = *np;
*transtab = lt;
free(old);
free(new);
return (p);
bad:
free(old);
free(new);
return (NULL);
}
/*
* Compile the text following an a, c, or i command.
*/
static char *
compile_text(void)
{
int asize, esc_nl, size;
char *lbuf, *text, *p, *op, *s;
size_t bufsize;
lbuf = text = NULL;
asize = size = 0;
while ((p = cu_fgets(&lbuf, &bufsize))) {
size_t len = ROUNDLEN(strlen(p) + 1);
if (asize - size < len) {
do {
asize += len;
} while (asize - size < len);
text = xrealloc(text, asize);
}
op = s = text + size;
for (esc_nl = 0; *p != '\0'; p++) {
if (*p == '\\' && p[1] != '\0' && *++p == '\n')
esc_nl = 1;
*s++ = *p;
}
size += s - op;
if (!esc_nl) {
*s = '\0';
break;
}
}
free(lbuf);
text = xrealloc(text, size + 1);
text[size] = '\0';
return (text);
}
/*
* Get an address and return a pointer to the first character after
* it. Fill the structure pointed to according to the address.
*/
static char *
compile_addr(char *p, struct s_addr *a)
{
char *end;
switch (*p) {
case '\\': /* Context address */
++p;
/* FALLTHROUGH */
case '/': /* Context address */
p = compile_re(p, &a->u.r);
if (p == NULL)
error(COMPILE, "unterminated regular expression");
a->type = AT_RE;
return (p);
case '$': /* Last line */
a->type = AT_LAST;
return (p + 1);
/* Line number */
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
a->type = AT_LINE;
a->u.l = strtoul(p, &end, 10);
return (end);
default:
error(COMPILE, "expected context address");
return (NULL);
}
}
/*
* duptoeol --
* Return a copy of all the characters up to \n or \0.
*/
static char *
duptoeol(char *s, char *ctype, char **semi)
{
size_t len;
int ws;
char *start;
ws = 0;
if (semi) {
for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s)
ws = isspace((unsigned char)*s);
} else {
for (start = s; *s != '\0' && *s != '\n'; ++s)
ws = isspace((unsigned char)*s);
*s = '\0';
}
if (ws)
warning("whitespace after %s", ctype);
len = s - start + 1;
if (semi)
*semi = s;
s = xmalloc(len);
strlcpy(s, start, len);
return (s);
}
/*
* Convert goto label names to addresses, and count a and r commands, in
* the given subset of the script. Free the memory used by labels in b
* and t commands (but not by :).
*
* TODO: Remove } nodes
*/
static void
fixuplabel(struct s_command *cp, struct s_command *end)
{
for (; cp != end; cp = cp->next)
switch (cp->code) {
case 'a':
case 'r':
appendnum++;
break;
case 'b':
case 't':
/* Resolve branch target. */
if (cp->t == NULL) {
cp->u.c = NULL;
break;
}
if ((cp->u.c = findlabel(cp->t)) == NULL)
error(COMPILE, "undefined label '%s'", cp->t);
free(cp->t);
break;
case '{':
/* Do interior commands. */
fixuplabel(cp->u.c, cp->next);
break;
}
}
/*
* Associate the given command label for later lookup.
*/
static void
enterlabel(struct s_command *cp)
{
struct labhash **lhp, *lh;
u_char *p;
u_int h, c;
for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
h = (h << 5) + h + c;
lhp = &labels[h & LHMASK];
for (lh = *lhp; lh != NULL; lh = lh->lh_next)
if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
error(COMPILE, "duplicate label '%s'", cp->t);
lh = xmalloc(sizeof *lh);
lh->lh_next = *lhp;
lh->lh_hash = h;
lh->lh_cmd = cp;
lh->lh_ref = 0;
*lhp = lh;
}
/*
* Find the label contained in the command l in the command linked
* list cp. L is excluded from the search. Return NULL if not found.
*/
static struct s_command *
findlabel(char *name)
{
struct labhash *lh;
u_char *p;
u_int h, c;
for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
h = (h << 5) + h + c;
for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
lh->lh_ref = 1;
return (lh->lh_cmd);
}
}
return (NULL);
}
/*
* Warn about any unused labels. As a side effect, release the label hash
* table space.
*/
static void
uselabel(void)
{
struct labhash *lh, *next;
int i;
for (i = 0; i < LHSZ; i++) {
for (lh = labels[i]; lh != NULL; lh = next) {
next = lh->lh_next;
if (!lh->lh_ref)
warning("unused label '%s'",
lh->lh_cmd->t);
free(lh);
}
}
}
|