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