nrelease - fix/improve livecd
[dragonfly.git] / usr.bin / sed / compile.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992 Diomidis Spinellis.
5  * Copyright (c) 1992, 1993
6  *      The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Diomidis Spinellis of Imperial College, University of London.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  * @(#)compile.c        8.1 (Berkeley) 6/6/93
36  * $FreeBSD: head/usr.bin/sed/compile.c 361884 2020-06-07 04:32:38Z kevans $
37  */
38
39 #include <sys/cdefs.h>
40
41 #include <sys/types.h>
42 #include <sys/stat.h>
43
44 #include <ctype.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <fcntl.h>
48 #include <limits.h>
49 #include <regex.h>
50 #include <stdbool.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <wchar.h>
55
56 #include "defs.h"
57 #include "extern.h"
58
59 #define LHSZ    128
60 #define LHMASK  (LHSZ - 1)
61 static struct labhash {
62         struct  labhash *lh_next;
63         u_int   lh_hash;
64         struct  s_command *lh_cmd;
65         int     lh_ref;
66 } *labels[LHSZ];
67
68 static char      *compile_addr(char *, struct s_addr *);
69 static char      *compile_ccl(char **, char *);
70 static char      *compile_delimited(char *, char *, int);
71 static char      *compile_flags(char *, struct s_subst *);
72 static regex_t   *compile_re(char *, int);
73 static char      *compile_subst(char *, struct s_subst *);
74 static char      *compile_text(void);
75 static char      *compile_tr(char *, struct s_tr **);
76 static struct s_command
77                 **compile_stream(struct s_command **);
78 static char      *duptoeol(char *, const char *);
79 static void       enterlabel(struct s_command *);
80 static struct s_command
81                  *findlabel(char *);
82 static void       fixuplabel(struct s_command *, struct s_command *);
83 static void       uselabel(void);
84
85 /*
86  * Command specification.  This is used to drive the command parser.
87  */
88 struct s_format {
89         char code;                              /* Command code */
90         int naddr;                              /* Number of address args */
91         enum e_args args;                       /* Argument type */
92 };
93
94 static struct s_format cmd_fmts[] = {
95         {'{', 2, GROUP},
96         {'}', 0, ENDGROUP},
97         {'a', 1, TEXT},
98         {'b', 2, BRANCH},
99         {'c', 2, TEXT},
100         {'d', 2, EMPTY},
101         {'D', 2, EMPTY},
102         {'g', 2, EMPTY},
103         {'G', 2, EMPTY},
104         {'h', 2, EMPTY},
105         {'H', 2, EMPTY},
106         {'i', 1, TEXT},
107         {'l', 2, EMPTY},
108         {'n', 2, EMPTY},
109         {'N', 2, EMPTY},
110         {'p', 2, EMPTY},
111         {'P', 2, EMPTY},
112         {'q', 1, EMPTY},
113         {'r', 1, RFILE},
114         {'s', 2, SUBST},
115         {'t', 2, BRANCH},
116         {'w', 2, WFILE},
117         {'x', 2, EMPTY},
118         {'y', 2, TR},
119         {'!', 2, NONSEL},
120         {':', 0, LABEL},
121         {'#', 0, COMMENT},
122         {'=', 1, EMPTY},
123         {'\0', 0, COMMENT},
124 };
125
126 /* The compiled program. */
127 struct s_command *prog;
128
129 /*
130  * Compile the program into prog.
131  * Initialise appends.
132  */
133 void
134 compile(void)
135 {
136         *compile_stream(&prog) = NULL;
137         fixuplabel(prog, NULL);
138         uselabel();
139         if (appendnum == 0)
140                 appends = NULL;
141         else if ((appends = malloc(sizeof(struct s_appends) * appendnum)) ==
142             NULL)
143                 err(1, "malloc");
144         if ((match = malloc((maxnsub + 1) * sizeof(regmatch_t))) == NULL)
145                 err(1, "malloc");
146 }
147
148 #define EATSPACE() do {                                                 \
149         if (p)                                                          \
150                 while (*p && isspace((unsigned char)*p))                \
151                         p++;                                            \
152         } while (0)
153
154 static struct s_command **
155 compile_stream(struct s_command **link)
156 {
157         char *p;
158         static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
159         struct s_command *cmd, *cmd2, *stack;
160         struct s_format *fp;
161         char re[_POSIX2_LINE_MAX + 1];
162         int naddr;                              /* Number of addresses */
163
164         stack = NULL;
165         for (;;) {
166                 if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) {
167                         if (stack != NULL)
168                                 errx(1, "%lu: %s: unexpected EOF (pending }'s)",
169                                                         linenum, fname);
170                         return (link);
171                 }
172
173 semicolon:      EATSPACE();
174                 if (p) {
175                         if (*p == '#' || *p == '\0')
176                                 continue;
177                         else if (*p == ';') {
178                                 p++;
179                                 goto semicolon;
180                         }
181                 }
182                 if ((*link = cmd = malloc(sizeof(struct s_command))) == NULL)
183                         err(1, "malloc");
184                 link = &cmd->next;
185                 cmd->startline = cmd->nonsel = 0;
186                 /* First parse the addresses */
187                 naddr = 0;
188
189 /* Valid characters to start an address */
190 #define addrchar(c)     (strchr("0123456789/\\$", (c)))
191                 if (addrchar(*p)) {
192                         naddr++;
193                         if ((cmd->a1 = malloc(sizeof(struct s_addr))) == NULL)
194                                 err(1, "malloc");
195                         p = compile_addr(p, cmd->a1);
196                         EATSPACE();                             /* EXTENSION */
197                         if (*p == ',') {
198                                 p++;
199                                 EATSPACE();                     /* EXTENSION */
200                                 naddr++;
201                                 if ((cmd->a2 = malloc(sizeof(struct s_addr)))
202                                     == NULL)
203                                         err(1, "malloc");
204                                 p = compile_addr(p, cmd->a2);
205                                 EATSPACE();
206                         } else
207                                 cmd->a2 = NULL;
208                 } else
209                         cmd->a1 = cmd->a2 = NULL;
210
211 nonsel:         /* Now parse the command */
212                 if (!*p)
213                         errx(1, "%lu: %s: command expected", linenum, fname);
214                 cmd->code = *p;
215                 for (fp = cmd_fmts; fp->code; fp++)
216                         if (fp->code == *p)
217                                 break;
218                 if (!fp->code)
219                         errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p);
220                 if (naddr > fp->naddr)
221                         errx(1,
222                                 "%lu: %s: command %c expects up to %d address(es), found %d",
223                                 linenum, fname, *p, fp->naddr, naddr);
224                 switch (fp->args) {
225                 case NONSEL:                    /* ! */
226                         p++;
227                         EATSPACE();
228                         cmd->nonsel = 1;
229                         goto nonsel;
230                 case GROUP:                     /* { */
231                         p++;
232                         EATSPACE();
233                         cmd->next = stack;
234                         stack = cmd;
235                         link = &cmd->u.c;
236                         if (*p)
237                                 goto semicolon;
238                         break;
239                 case ENDGROUP:
240                         /*
241                          * Short-circuit command processing, since end of
242                          * group is really just a noop.
243                          */
244                         cmd->nonsel = 1;
245                         if (stack == NULL)
246                                 errx(1, "%lu: %s: unexpected }", linenum, fname);
247                         cmd2 = stack;
248                         stack = cmd2->next;
249                         cmd2->next = cmd;
250                         /*FALLTHROUGH*/
251                 case EMPTY:             /* d D g G h H l n N p P q x = \0 */
252                         p++;
253                         EATSPACE();
254                         if (*p == ';') {
255                                 p++;
256                                 link = &cmd->next;
257                                 goto semicolon;
258                         }
259                         if (*p)
260                                 errx(1, "%lu: %s: extra characters at the end of %c command",
261                                                 linenum, fname, cmd->code);
262                         break;
263                 case TEXT:                      /* a c i */
264                         p++;
265                         EATSPACE();
266                         if (*p != '\\')
267                                 errx(1,
268 "%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code);
269                         p++;
270                         EATSPACE();
271                         if (*p)
272                                 errx(1,
273                                 "%lu: %s: extra characters after \\ at the end of %c command",
274                                 linenum, fname, cmd->code);
275                         cmd->t = compile_text();
276                         break;
277                 case COMMENT:                   /* \0 # */
278                         break;
279                 case WFILE:                     /* w */
280                         p++;
281                         EATSPACE();
282                         if (*p == '\0')
283                                 errx(1, "%lu: %s: filename expected", linenum, fname);
284                         cmd->t = duptoeol(p, "w command");
285                         if (aflag)
286                                 cmd->u.fd = -1;
287                         else if ((cmd->u.fd = open(p,
288                             O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
289                             DEFFILEMODE)) == -1)
290                                 err(1, "%s", p);
291                         break;
292                 case RFILE:                     /* r */
293                         p++;
294                         EATSPACE();
295                         if (*p == '\0')
296                                 errx(1, "%lu: %s: filename expected", linenum, fname);
297                         else
298                                 cmd->t = duptoeol(p, "read command");
299                         break;
300                 case BRANCH:                    /* b t */
301                         p++;
302                         EATSPACE();
303                         if (*p == '\0')
304                                 cmd->t = NULL;
305                         else
306                                 cmd->t = duptoeol(p, "branch");
307                         break;
308                 case LABEL:                     /* : */
309                         p++;
310                         EATSPACE();
311                         cmd->t = duptoeol(p, "label");
312                         if (strlen(p) == 0)
313                                 errx(1, "%lu: %s: empty label", linenum, fname);
314                         enterlabel(cmd);
315                         break;
316                 case SUBST:                     /* s */
317                         p++;
318                         if (*p == '\0' || *p == '\\')
319                                 errx(1,
320 "%lu: %s: substitute pattern can not be delimited by newline or backslash",
321                                         linenum, fname);
322                         if ((cmd->u.s = calloc(1, sizeof(struct s_subst))) == NULL)
323                                 err(1, "malloc");
324                         p = compile_delimited(p, re, 0);
325                         if (p == NULL)
326                                 errx(1,
327                                 "%lu: %s: unterminated substitute pattern", linenum, fname);
328
329                         /* Compile RE with no case sensitivity temporarily */
330                         if (*re == '\0')
331                                 cmd->u.s->re = NULL;
332                         else
333                                 cmd->u.s->re = compile_re(re, 0);
334                         --p;
335                         p = compile_subst(p, cmd->u.s);
336                         p = compile_flags(p, cmd->u.s);
337
338                         /* Recompile RE with case sensitivity from "I" flag if any */
339                         if (*re == '\0')
340                                 cmd->u.s->re = NULL;
341                         else
342                                 cmd->u.s->re = compile_re(re, cmd->u.s->icase);
343                         EATSPACE();
344                         if (*p == ';') {
345                                 p++;
346                                 link = &cmd->next;
347                                 goto semicolon;
348                         }
349                         break;
350                 case TR:                        /* y */
351                         p++;
352                         p = compile_tr(p, &cmd->u.y);
353                         EATSPACE();
354                         if (*p == ';') {
355                                 p++;
356                                 link = &cmd->next;
357                                 goto semicolon;
358                         }
359                         if (*p)
360                                 errx(1,
361 "%lu: %s: extra text at the end of a transform command", linenum, fname);
362                         break;
363                 }
364         }
365 }
366
367 static int
368 hex2char(const char *in, char *out, int len)
369 {
370         long ord;
371         char *endptr, hexbuf[3];
372
373         hexbuf[0] = in[0];
374         hexbuf[1] = len > 1 ? in[1] : '\0';
375         hexbuf[2] = '\0';
376
377         errno = 0;
378         ord = strtol(hexbuf, &endptr, 16);
379         if (*endptr != '\0' || errno != 0)
380                 return (ERANGE);
381         *out = (char)ord;
382         return (0);
383 }
384
385 static bool
386 hexdigit(char c)
387 {
388         int lc;
389
390         lc = tolower(c);
391         return isdigit(lc) || (lc >= 'a' && lc <= 'f');
392 }
393
394 static bool
395 dohex(const char *in, char *out, int *len)
396 {
397         int tmplen;
398
399         if (!hexdigit(in[0]))
400                 return (false);
401         tmplen = 1;
402         if (hexdigit(in[1]))
403                 ++tmplen;
404         if (hex2char(in, out, tmplen) == 0) {
405                 *len = tmplen;
406                 return (true);
407         }
408
409         return (false);
410 }
411
412 /*
413  * Get a delimited string.  P points to the delimiter of the string; d points
414  * to a buffer area.  Newline and delimiter escapes are processed; other
415  * escapes are ignored.
416  *
417  * Returns a pointer to the first character after the final delimiter or NULL
418  * in the case of a non-terminated string.  The character array d is filled
419  * with the processed string.
420  */
421 static char *
422 compile_delimited(char *p, char *d, int is_tr)
423 {
424         int hexlen;
425         char c;
426
427         c = *p++;
428         if (c == '\0')
429                 return (NULL);
430         else if (c == '\\')
431                 errx(1, "%lu: %s: \\ can not be used as a string delimiter",
432                                 linenum, fname);
433         else if (c == '\n')
434                 errx(1, "%lu: %s: newline can not be used as a string delimiter",
435                                 linenum, fname);
436         while (*p) {
437                 if (*p == '[' && *p != c) {
438                         if ((d = compile_ccl(&p, d)) == NULL)
439                                 errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname);
440                         continue;
441                 } else if (*p == '\\' && p[1] == '[') {
442                         *d++ = *p++;
443                 } else if (*p == '\\' && p[1] == c) {
444                         p++;
445                 } else if (*p == '\\' &&
446                     (p[1] == 'n' || p[1] == 'r' || p[1] == 't')) {
447                         switch (p[1]) {
448                         case 'n':
449                                 *d++ = '\n';
450                                 break;
451                         case 'r':
452                                 *d++ = '\r';
453                                 break;
454                         case 't':
455                                 *d++ = '\t';
456                                 break;
457                         }
458                         p += 2;
459                         continue;
460                 } else if (*p == '\\' && p[1] == 'x') {
461                         if (dohex(&p[2], d, &hexlen)) {
462                                 ++d;
463                                 p += hexlen + 2;
464                                 continue;
465                         }
466                 } else if (*p == '\\' && p[1] == '\\') {
467                         if (is_tr)
468                                 p++;
469                         else
470                                 *d++ = *p++;
471                 } else if (*p == c) {
472                         *d = '\0';
473                         return (p + 1);
474                 }
475                 *d++ = *p++;
476         }
477         return (NULL);
478 }
479
480
481 /* compile_ccl: expand a POSIX character class */
482 static char *
483 compile_ccl(char **sp, char *t)
484 {
485         int c, d, hexlen;
486         char *s = *sp;
487
488         *t++ = *s++;
489         if (*s == '^')
490                 *t++ = *s++;
491         if (*s == ']')
492                 *t++ = *s++;
493         for (; *s && (*t = *s) != ']'; s++, t++) {
494                 if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
495                         *++t = *++s, t++, s++;
496                         for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
497                                 if ((c = *s) == '\0')
498                                         return NULL;
499                 } else if (*s == '\\') {
500                         switch (s[1]) {
501                         case 'n':
502                                 *t = '\n';
503                                 s++;
504                                 break;
505                         case 'r':
506                                 *t = '\r';
507                                 s++;
508                                 break;
509                         case 't':
510                                 *t = '\t';
511                                 s++;
512                                 break;
513                         case 'x':
514                                 if (dohex(&s[2], t, &hexlen))
515                                         s += hexlen + 1;
516                                 break;
517                         }
518                 }
519         }
520         return (*s == ']') ? *sp = ++s, ++t : NULL;
521 }
522
523 /*
524  * Compiles the regular expression in RE and returns a pointer to the compiled
525  * regular expression.
526  * Cflags are passed to regcomp.
527  */
528 static regex_t *
529 compile_re(char *re, int case_insensitive)
530 {
531         regex_t *rep;
532         int eval, flags;
533
534
535         flags = rflags;
536         if (case_insensitive)
537                 flags |= REG_ICASE;
538         if ((rep = malloc(sizeof(regex_t))) == NULL)
539                 err(1, "malloc");
540         if ((eval = regcomp(rep, re, flags)) != 0)
541                 errx(1, "%lu: %s: RE error: %s",
542                                 linenum, fname, strregerror(eval, rep));
543         if (maxnsub < rep->re_nsub)
544                 maxnsub = rep->re_nsub;
545         return (rep);
546 }
547
548 /*
549  * Compile the substitution string of a regular expression and set res to
550  * point to a saved copy of it.  Nsub is the number of parenthesized regular
551  * expressions.
552  */
553 static char *
554 compile_subst(char *p, struct s_subst *s)
555 {
556         static char lbuf[_POSIX2_LINE_MAX + 1];
557         int asize, hexlen, size;
558         u_char ref;
559         char c, *text, *op, *sp;
560         int more = 1, sawesc = 0;
561
562         c = *p++;                       /* Terminator character */
563         if (c == '\0')
564                 return (NULL);
565
566         s->maxbref = 0;
567         s->linenum = linenum;
568         asize = 2 * _POSIX2_LINE_MAX + 1;
569         if ((text = malloc(asize)) == NULL)
570                 err(1, "malloc");
571         size = 0;
572         do {
573                 op = sp = text + size;
574                 for (; *p; p++) {
575                         if (*p == '\\' || sawesc) {
576                                 /*
577                                  * If this is a continuation from the last
578                                  * buffer, we won't have a character to
579                                  * skip over.
580                                  */
581                                 if (sawesc)
582                                         sawesc = 0;
583                                 else
584                                         p++;
585
586                                 if (*p == '\0') {
587                                         /*
588                                          * This escaped character is continued
589                                          * in the next part of the line.  Note
590                                          * this fact, then cause the loop to
591                                          * exit w/ normal EOL case and reenter
592                                          * above with the new buffer.
593                                          */
594                                         sawesc = 1;
595                                         p--;
596                                         continue;
597                                 } else if (strchr("123456789", *p) != NULL) {
598                                         *sp++ = '\\';
599                                         ref = *p - '0';
600                                         if (s->re != NULL &&
601                                             ref > s->re->re_nsub)
602                                                 errx(1, "%lu: %s: \\%c not defined in the RE",
603                                                                 linenum, fname, *p);
604                                         if (s->maxbref < ref)
605                                                 s->maxbref = ref;
606                                 } else {
607                                         switch (*p) {
608                                         case '&':
609                                         case '\\':
610                                                 *sp++ = '\\';
611                                                 break;
612                                         case 'n':
613                                                 *p = '\n';
614                                                 break;
615                                         case 'r':
616                                                 *p = '\r';
617                                                 break;
618                                         case 't':
619                                                 *p = '\t';
620                                                 break;
621                                         case 'x':
622 #define ADVANCE_N(s, n)                                 \
623         do {                                            \
624                 char *adv = (s);                        \
625                 while (*(adv + (n) - 1) != '\0') {      \
626                         *adv = *(adv + (n));            \
627                         ++adv;                          \
628                 }                                       \
629                 *adv = '\0';                            \
630         } while (0);
631                                                 if (dohex(&p[1], p, &hexlen)) {
632                                                         ADVANCE_N(p + 1,
633                                                             hexlen);
634                                                 }
635                                                 break;
636                                         }
637                                 }
638                         } else if (*p == c) {
639                                 if (*++p == '\0' && more) {
640                                         if (cu_fgets(lbuf, sizeof(lbuf), &more))
641                                                 p = lbuf;
642                                 }
643                                 *sp++ = '\0';
644                                 size += sp - op;
645                                 if ((s->new = realloc(text, size)) == NULL)
646                                         err(1, "realloc");
647                                 return (p);
648                         } else if (*p == '\n') {
649                                 errx(1,
650 "%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
651                                 /* NOTREACHED */
652                         }
653                         *sp++ = *p;
654                 }
655                 size += sp - op;
656                 if (asize - size < _POSIX2_LINE_MAX + 1) {
657                         asize *= 2;
658                         if ((text = realloc(text, asize)) == NULL)
659                                 err(1, "realloc");
660                 }
661         } while (cu_fgets(p = lbuf, sizeof(lbuf), &more) != NULL);
662         errx(1, "%lu: %s: unterminated substitute in regular expression",
663                         linenum, fname);
664         /* NOTREACHED */
665 }
666
667 /*
668  * Compile the flags of the s command
669  */
670 static char *
671 compile_flags(char *p, struct s_subst *s)
672 {
673         int gn;                 /* True if we have seen g or n */
674         unsigned long nval;
675         char wfile[_POSIX2_LINE_MAX + 1], *q, *eq;
676
677         s->n = 1;                               /* Default */
678         s->p = 0;
679         s->wfile = NULL;
680         s->wfd = -1;
681         s->icase = 0;
682         for (gn = 0;;) {
683                 EATSPACE();                     /* EXTENSION */
684                 switch (*p) {
685                 case 'g':
686                         if (gn)
687                                 errx(1,
688 "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
689                         gn = 1;
690                         s->n = 0;
691                         break;
692                 case '\0':
693                 case '\n':
694                 case ';':
695                         return (p);
696                 case 'p':
697                         s->p = 1;
698                         break;
699                 case 'i':
700                 case 'I':
701                         s->icase = 1;
702                         break;
703                 case '1': case '2': case '3':
704                 case '4': case '5': case '6':
705                 case '7': case '8': case '9':
706                         if (gn)
707                                 errx(1,
708 "%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
709                         gn = 1;
710                         errno = 0;
711                         nval = strtol(p, &p, 10);
712                         if (errno == ERANGE || nval > INT_MAX)
713                                 errx(1,
714 "%lu: %s: overflow in the 'N' substitute flag", linenum, fname);
715                         s->n = nval;
716                         p--;
717                         break;
718                 case 'w':
719                         p++;
720 #ifdef HISTORIC_PRACTICE
721                         if (*p != ' ') {
722                                 warnx("%lu: %s: space missing before w wfile", linenum, fname);
723                                 return (p);
724                         }
725 #endif
726                         EATSPACE();
727                         q = wfile;
728                         eq = wfile + sizeof(wfile) - 1;
729                         while (*p) {
730                                 if (*p == '\n')
731                                         break;
732                                 if (q >= eq)
733                                         err(1, "wfile too long");
734                                 *q++ = *p++;
735                         }
736                         *q = '\0';
737                         if (q == wfile)
738                                 errx(1, "%lu: %s: no wfile specified", linenum, fname);
739                         s->wfile = strdup(wfile);
740                         if (!aflag && (s->wfd = open(wfile,
741                             O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
742                             DEFFILEMODE)) == -1)
743                                 err(1, "%s", wfile);
744                         return (p);
745                 default:
746                         errx(1, "%lu: %s: bad flag in substitute command: '%c'",
747                                         linenum, fname, *p);
748                         break;
749                 }
750                 p++;
751         }
752 }
753
754 /*
755  * Compile a translation set of strings into a lookup table.
756  */
757 static char *
758 compile_tr(char *p, struct s_tr **py)
759 {
760         struct s_tr *y;
761         int i;
762         const char *op, *np;
763         char old[_POSIX2_LINE_MAX + 1];
764         char new[_POSIX2_LINE_MAX + 1];
765         size_t oclen, oldlen, nclen, newlen;
766         mbstate_t mbs1, mbs2;
767
768         if ((*py = y = malloc(sizeof(*y))) == NULL)
769                 err(1, NULL);
770         y->multis = NULL;
771         y->nmultis = 0;
772
773         if (*p == '\0' || *p == '\\')
774                 errx(1,
775         "%lu: %s: transform pattern can not be delimited by newline or backslash",
776                         linenum, fname);
777         p = compile_delimited(p, old, 1);
778         if (p == NULL)
779                 errx(1, "%lu: %s: unterminated transform source string",
780                                 linenum, fname);
781         p = compile_delimited(p - 1, new, 1);
782         if (p == NULL)
783                 errx(1, "%lu: %s: unterminated transform target string",
784                                 linenum, fname);
785         EATSPACE();
786         op = old;
787         oldlen = mbsrtowcs(NULL, &op, 0, NULL);
788         if (oldlen == (size_t)-1)
789                 err(1, NULL);
790         np = new;
791         newlen = mbsrtowcs(NULL, &np, 0, NULL);
792         if (newlen == (size_t)-1)
793                 err(1, NULL);
794         if (newlen != oldlen)
795                 errx(1, "%lu: %s: transform strings are not the same length",
796                                 linenum, fname);
797         if (MB_CUR_MAX == 1) {
798                 /*
799                  * The single-byte encoding case is easy: generate a
800                  * lookup table.
801                  */
802                 for (i = 0; i <= UCHAR_MAX; i++)
803                         y->bytetab[i] = (char)i;
804                 for (; *op; op++, np++)
805                         y->bytetab[(u_char)*op] = *np;
806         } else {
807                 /*
808                  * Multi-byte encoding case: generate a lookup table as
809                  * above, but only for single-byte characters. The first
810                  * bytes of multi-byte characters have their lookup table
811                  * entries set to 0, which causes do_tr() to search through
812                  * an auxiliary vector of multi-byte mappings.
813                  */
814                 memset(&mbs1, 0, sizeof(mbs1));
815                 memset(&mbs2, 0, sizeof(mbs2));
816                 for (i = 0; i <= UCHAR_MAX; i++)
817                         y->bytetab[i] = (btowc(i) != WEOF) ? i : 0;
818                 while (*op != '\0') {
819                         oclen = mbrlen(op, MB_LEN_MAX, &mbs1);
820                         if (oclen == (size_t)-1 || oclen == (size_t)-2)
821                                 errc(1, EILSEQ, NULL);
822                         nclen = mbrlen(np, MB_LEN_MAX, &mbs2);
823                         if (nclen == (size_t)-1 || nclen == (size_t)-2)
824                                 errc(1, EILSEQ, NULL);
825                         if (oclen == 1 && nclen == 1)
826                                 y->bytetab[(u_char)*op] = *np;
827                         else {
828                                 y->bytetab[(u_char)*op] = 0;
829                                 y->multis = realloc(y->multis,
830                                     (y->nmultis + 1) * sizeof(*y->multis));
831                                 if (y->multis == NULL)
832                                         err(1, NULL);
833                                 i = y->nmultis++;
834                                 y->multis[i].fromlen = oclen;
835                                 memcpy(y->multis[i].from, op, oclen);
836                                 y->multis[i].tolen = nclen;
837                                 memcpy(y->multis[i].to, np, nclen);
838                         }
839                         op += oclen;
840                         np += nclen;
841                 }
842         }
843         return (p);
844 }
845
846 /*
847  * Compile the text following an a, c, or i command.
848  */
849 static char *
850 compile_text(void)
851 {
852         int asize, esc_nl, size;
853         char *text, *p, *op, *s;
854         char lbuf[_POSIX2_LINE_MAX + 1];
855
856         asize = 2 * _POSIX2_LINE_MAX + 1;
857         if ((text = malloc(asize)) == NULL)
858                 err(1, "malloc");
859         size = 0;
860         while (cu_fgets(lbuf, sizeof(lbuf), NULL) != NULL) {
861                 op = s = text + size;
862                 p = lbuf;
863 #ifdef LEGACY_BSDSED_COMPAT
864                 EATSPACE();
865 #endif
866                 for (esc_nl = 0; *p != '\0'; p++) {
867                         if (*p == '\\' && p[1] != '\0' && *++p == '\n')
868                                 esc_nl = 1;
869                         *s++ = *p;
870                 }
871                 size += s - op;
872                 if (!esc_nl) {
873                         *s = '\0';
874                         break;
875                 }
876                 if (asize - size < _POSIX2_LINE_MAX + 1) {
877                         asize *= 2;
878                         if ((text = realloc(text, asize)) == NULL)
879                                 err(1, "realloc");
880                 }
881         }
882         text[size] = '\0';
883         if ((p = realloc(text, size + 1)) == NULL)
884                 err(1, "realloc");
885         return (p);
886 }
887
888 /*
889  * Get an address and return a pointer to the first character after
890  * it.  Fill the structure pointed to according to the address.
891  */
892 static char *
893 compile_addr(char *p, struct s_addr *a)
894 {
895         char *end, re[_POSIX2_LINE_MAX + 1];
896         int icase;
897
898         icase = 0;
899
900         a->type = 0;
901         switch (*p) {
902         case '\\':                              /* Context address */
903                 ++p;
904                 /* FALLTHROUGH */
905         case '/':                               /* Context address */
906                 p = compile_delimited(p, re, 0);
907                 if (p == NULL)
908                         errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
909                 /* Check for case insensitive regexp flag */
910                 if (*p == 'I') {
911                         icase = 1;
912                         p++;
913                 }
914                 if (*re == '\0')
915                         a->u.r = NULL;
916                 else
917                         a->u.r = compile_re(re, icase);
918                 a->type = AT_RE;
919                 return (p);
920
921         case '$':                               /* Last line */
922                 a->type = AT_LAST;
923                 return (p + 1);
924
925         case '+':                               /* Relative line number */
926                 a->type = AT_RELLINE;
927                 p++;
928                 /* FALLTHROUGH */
929                                                 /* Line number */
930         case '0': case '1': case '2': case '3': case '4':
931         case '5': case '6': case '7': case '8': case '9':
932                 if (a->type == 0)
933                         a->type = AT_LINE;
934                 a->u.l = strtol(p, &end, 10);
935                 return (end);
936         default:
937                 errx(1, "%lu: %s: expected context address", linenum, fname);
938                 return (NULL);
939         }
940 }
941
942 /*
943  * duptoeol --
944  *      Return a copy of all the characters up to \n or \0.
945  */
946 static char *
947 duptoeol(char *s, const char *ctype)
948 {
949         size_t len;
950         int ws;
951         char *p, *start;
952
953         ws = 0;
954         for (start = s; *s != '\0' && *s != '\n'; ++s)
955                 ws = isspace((unsigned char)*s);
956         *s = '\0';
957         if (ws)
958                 warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
959         len = s - start + 1;
960         if ((p = malloc(len)) == NULL)
961                 err(1, "malloc");
962         return (memmove(p, start, len));
963 }
964
965 /*
966  * Convert goto label names to addresses, and count a and r commands, in
967  * the given subset of the script.  Free the memory used by labels in b
968  * and t commands (but not by :).
969  *
970  * TODO: Remove } nodes
971  */
972 static void
973 fixuplabel(struct s_command *cp, struct s_command *end)
974 {
975
976         for (; cp != end; cp = cp->next)
977                 switch (cp->code) {
978                 case 'a':
979                 case 'r':
980                         appendnum++;
981                         break;
982                 case 'b':
983                 case 't':
984                         /* Resolve branch target. */
985                         if (cp->t == NULL) {
986                                 cp->u.c = NULL;
987                                 break;
988                         }
989                         if ((cp->u.c = findlabel(cp->t)) == NULL)
990                                 errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
991                         free(cp->t);
992                         break;
993                 case '{':
994                         /* Do interior commands. */
995                         fixuplabel(cp->u.c, cp->next);
996                         break;
997                 }
998 }
999
1000 /*
1001  * Associate the given command label for later lookup.
1002  */
1003 static void
1004 enterlabel(struct s_command *cp)
1005 {
1006         struct labhash **lhp, *lh;
1007         u_char *p;
1008         u_int h, c;
1009
1010         for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
1011                 h = (h << 5) + h + c;
1012         lhp = &labels[h & LHMASK];
1013         for (lh = *lhp; lh != NULL; lh = lh->lh_next)
1014                 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
1015                         errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
1016         if ((lh = malloc(sizeof *lh)) == NULL)
1017                 err(1, "malloc");
1018         lh->lh_next = *lhp;
1019         lh->lh_hash = h;
1020         lh->lh_cmd = cp;
1021         lh->lh_ref = 0;
1022         *lhp = lh;
1023 }
1024
1025 /*
1026  * Find the label contained in the command l in the command linked
1027  * list cp.  L is excluded from the search.  Return NULL if not found.
1028  */
1029 static struct s_command *
1030 findlabel(char *name)
1031 {
1032         struct labhash *lh;
1033         u_char *p;
1034         u_int h, c;
1035
1036         for (h = 0, p = (u_char *)name; (c = *p) != 0; p++)
1037                 h = (h << 5) + h + c;
1038         for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) {
1039                 if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) {
1040                         lh->lh_ref = 1;
1041                         return (lh->lh_cmd);
1042                 }
1043         }
1044         return (NULL);
1045 }
1046
1047 /*
1048  * Warn about any unused labels.  As a side effect, release the label hash
1049  * table space.
1050  */
1051 static void
1052 uselabel(void)
1053 {
1054         struct labhash *lh, *next;
1055         int i;
1056
1057         for (i = 0; i < LHSZ; i++) {
1058                 for (lh = labels[i]; lh != NULL; lh = next) {
1059                         next = lh->lh_next;
1060                         if (!lh->lh_ref)
1061                                 warnx("%lu: %s: unused label '%s'",
1062                                     linenum, fname, lh->lh_cmd->t);
1063                         free(lh);
1064                 }
1065         }
1066 }