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