compile.c revision 1.4 1 1.1 alm /*-
2 1.1 alm * Copyright (c) 1992 Diomidis Spinellis.
3 1.1 alm * Copyright (c) 1992 The Regents of the University of California.
4 1.1 alm * All rights reserved.
5 1.1 alm *
6 1.1 alm * This code is derived from software contributed to Berkeley by
7 1.1 alm * Diomidis Spinellis of Imperial College, University of London.
8 1.1 alm *
9 1.1 alm * Redistribution and use in source and binary forms, with or without
10 1.1 alm * modification, are permitted provided that the following conditions
11 1.1 alm * are met:
12 1.1 alm * 1. Redistributions of source code must retain the above copyright
13 1.1 alm * notice, this list of conditions and the following disclaimer.
14 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 alm * notice, this list of conditions and the following disclaimer in the
16 1.1 alm * documentation and/or other materials provided with the distribution.
17 1.1 alm * 3. All advertising materials mentioning features or use of this software
18 1.1 alm * must display the following acknowledgement:
19 1.1 alm * This product includes software developed by the University of
20 1.1 alm * California, Berkeley and its contributors.
21 1.1 alm * 4. Neither the name of the University nor the names of its contributors
22 1.1 alm * may be used to endorse or promote products derived from this software
23 1.1 alm * without specific prior written permission.
24 1.1 alm *
25 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 alm * SUCH DAMAGE.
36 1.1 alm */
37 1.1 alm
38 1.1 alm #ifndef lint
39 1.4 mycroft /*static char sccsid[] = "from: @(#)compile.c 5.6 (Berkeley) 11/2/92";*/
40 1.4 mycroft static char rcsid[] = "$Id: compile.c,v 1.4 1993/08/01 18:08:55 mycroft Exp $";
41 1.1 alm #endif /* not lint */
42 1.1 alm
43 1.1 alm #include <sys/types.h>
44 1.1 alm #include <sys/stat.h>
45 1.1 alm
46 1.1 alm #include <ctype.h>
47 1.1 alm #include <errno.h>
48 1.1 alm #include <fcntl.h>
49 1.1 alm #include <limits.h>
50 1.1 alm #include <regex.h>
51 1.1 alm #include <stdio.h>
52 1.1 alm #include <stdlib.h>
53 1.1 alm #include <string.h>
54 1.1 alm
55 1.1 alm #include "defs.h"
56 1.1 alm #include "extern.h"
57 1.1 alm
58 1.1 alm static char *compile_addr __P((char *, struct s_addr *));
59 1.2 alm static char *compile_ccl __P((char **, char *));
60 1.1 alm static char *compile_delimited __P((char *, char *));
61 1.1 alm static char *compile_flags __P((char *, struct s_subst *));
62 1.1 alm static char *compile_re __P((char *, regex_t **));
63 1.1 alm static char *compile_subst __P((char *, struct s_subst *));
64 1.1 alm static char *compile_text __P((void));
65 1.1 alm static char *compile_tr __P((char *, char **));
66 1.1 alm static struct s_command
67 1.1 alm **compile_stream __P((char *, struct s_command **, char *));
68 1.1 alm static char *duptoeol __P((char *));
69 1.1 alm static struct s_command
70 1.1 alm *findlabel __P((struct s_command *, struct s_command *));
71 1.1 alm static void fixuplabel __P((struct s_command *, struct s_command *,
72 1.1 alm struct s_command *));
73 1.1 alm
74 1.1 alm /*
75 1.1 alm * Command specification. This is used to drive the command parser.
76 1.1 alm */
77 1.1 alm struct s_format {
78 1.1 alm char code; /* Command code */
79 1.1 alm int naddr; /* Number of address args */
80 1.1 alm enum e_args args; /* Argument type */
81 1.1 alm };
82 1.1 alm
83 1.1 alm static struct s_format cmd_fmts[] = {
84 1.1 alm {'{', 2, GROUP},
85 1.1 alm {'a', 1, TEXT},
86 1.1 alm {'b', 2, BRANCH},
87 1.1 alm {'c', 2, TEXT},
88 1.1 alm {'d', 2, EMPTY},
89 1.1 alm {'D', 2, EMPTY},
90 1.1 alm {'g', 2, EMPTY},
91 1.1 alm {'G', 2, EMPTY},
92 1.1 alm {'h', 2, EMPTY},
93 1.1 alm {'H', 2, EMPTY},
94 1.1 alm {'i', 1, TEXT},
95 1.1 alm {'l', 2, EMPTY},
96 1.1 alm {'n', 2, EMPTY},
97 1.1 alm {'N', 2, EMPTY},
98 1.1 alm {'p', 2, EMPTY},
99 1.1 alm {'P', 2, EMPTY},
100 1.1 alm {'q', 1, EMPTY},
101 1.1 alm {'r', 1, RFILE},
102 1.1 alm {'s', 2, SUBST},
103 1.1 alm {'t', 2, BRANCH},
104 1.1 alm {'w', 2, WFILE},
105 1.1 alm {'x', 2, EMPTY},
106 1.1 alm {'y', 2, TR},
107 1.1 alm {'!', 2, NONSEL},
108 1.1 alm {':', 0, LABEL},
109 1.1 alm {'#', 0, COMMENT},
110 1.1 alm {'=', 1, EMPTY},
111 1.1 alm {'\0', 0, COMMENT},
112 1.1 alm };
113 1.1 alm
114 1.1 alm /* The compiled program. */
115 1.1 alm struct s_command *prog;
116 1.1 alm
117 1.1 alm /*
118 1.1 alm * Compile the program into prog.
119 1.1 alm * Initialise appends.
120 1.1 alm */
121 1.1 alm void
122 1.1 alm compile()
123 1.1 alm {
124 1.1 alm *compile_stream(NULL, &prog, NULL) = NULL;
125 1.1 alm fixuplabel(prog, prog, NULL);
126 1.1 alm appends = xmalloc(sizeof(struct s_appends) * appendnum);
127 1.1 alm match = xmalloc((maxnsub + 1) * sizeof(regmatch_t));
128 1.1 alm }
129 1.1 alm
130 1.1 alm #define EATSPACE() do { \
131 1.1 alm if (p) \
132 1.1 alm while (*p && isascii(*p) && isspace(*p)) \
133 1.1 alm p++; \
134 1.1 alm } while (0)
135 1.1 alm
136 1.1 alm static struct s_command **
137 1.1 alm compile_stream(terminator, link, p)
138 1.1 alm char *terminator;
139 1.1 alm struct s_command **link;
140 1.1 alm register char *p;
141 1.1 alm {
142 1.1 alm static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */
143 1.1 alm struct s_command *cmd, *cmd2;
144 1.1 alm struct s_format *fp;
145 1.1 alm int naddr; /* Number of addresses */
146 1.1 alm
147 1.1 alm if (p != NULL)
148 1.1 alm goto semicolon;
149 1.1 alm for (;;) {
150 1.1 alm if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
151 1.1 alm if (terminator != NULL)
152 1.1 alm err(COMPILE, "unexpected EOF (pending }'s)");
153 1.1 alm return (link);
154 1.1 alm }
155 1.1 alm
156 1.1 alm semicolon: EATSPACE();
157 1.1 alm if (p && (*p == '#' || *p == '\0'))
158 1.1 alm continue;
159 1.1 alm if (*p == '}') {
160 1.1 alm if (terminator == NULL)
161 1.1 alm err(COMPILE, "unexpected }");
162 1.1 alm return (link);
163 1.1 alm }
164 1.1 alm *link = cmd = xmalloc(sizeof(struct s_command));
165 1.1 alm link = &cmd->next;
166 1.1 alm cmd->nonsel = cmd->inrange = 0;
167 1.1 alm /* First parse the addresses */
168 1.1 alm naddr = 0;
169 1.1 alm cmd->a1 = cmd->a2 = NULL;
170 1.1 alm
171 1.1 alm /* Valid characters to start an address */
172 1.1 alm #define addrchar(c) (strchr("0123456789/\\$", (c)))
173 1.1 alm if (addrchar(*p)) {
174 1.1 alm naddr++;
175 1.1 alm cmd->a1 = xmalloc(sizeof(struct s_addr));
176 1.1 alm p = compile_addr(p, cmd->a1);
177 1.1 alm EATSPACE(); /* EXTENSION */
178 1.1 alm if (*p == ',') {
179 1.1 alm naddr++;
180 1.1 alm p++;
181 1.1 alm EATSPACE(); /* EXTENSION */
182 1.1 alm cmd->a2 = xmalloc(sizeof(struct s_addr));
183 1.1 alm p = compile_addr(p, cmd->a2);
184 1.1 alm }
185 1.1 alm }
186 1.1 alm
187 1.1 alm nonsel: /* Now parse the command */
188 1.1 alm EATSPACE();
189 1.1 alm if (!*p)
190 1.1 alm err(COMPILE, "command expected");
191 1.1 alm cmd->code = *p;
192 1.1 alm for (fp = cmd_fmts; fp->code; fp++)
193 1.1 alm if (fp->code == *p)
194 1.1 alm break;
195 1.1 alm if (!fp->code)
196 1.1 alm err(COMPILE, "invalid command code %c", *p);
197 1.1 alm if (naddr > fp->naddr)
198 1.1 alm err(COMPILE,
199 1.1 alm "command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
200 1.1 alm switch (fp->args) {
201 1.1 alm case NONSEL: /* ! */
202 1.1 alm cmd->nonsel = ! cmd->nonsel;
203 1.1 alm p++;
204 1.1 alm goto nonsel;
205 1.1 alm case GROUP: /* { */
206 1.1 alm p++;
207 1.1 alm EATSPACE();
208 1.1 alm if (!*p)
209 1.1 alm p = NULL;
210 1.1 alm cmd2 = xmalloc(sizeof(struct s_command));
211 1.1 alm cmd2->code = '}';
212 1.1 alm *compile_stream("}", &cmd->u.c, p) = cmd2;
213 1.1 alm cmd->next = cmd2;
214 1.1 alm link = &cmd2->next;
215 1.1 alm break;
216 1.1 alm case EMPTY: /* d D g G h H l n N p P q x = \0 */
217 1.1 alm p++;
218 1.1 alm EATSPACE();
219 1.1 alm if (*p == ';') {
220 1.1 alm p++;
221 1.1 alm link = &cmd->next;
222 1.1 alm goto semicolon;
223 1.1 alm }
224 1.1 alm if (*p)
225 1.1 alm err(COMPILE,
226 1.1 alm "extra characters at the end of %c command", cmd->code);
227 1.1 alm break;
228 1.1 alm case TEXT: /* a c i */
229 1.1 alm p++;
230 1.1 alm EATSPACE();
231 1.1 alm if (*p != '\\')
232 1.1 alm err(COMPILE,
233 1.1 alm "command %c expects \\ followed by text", cmd->code);
234 1.1 alm p++;
235 1.1 alm EATSPACE();
236 1.1 alm if (*p)
237 1.1 alm err(COMPILE,
238 1.1 alm "extra characters after \\ at the end of %c command", cmd->code);
239 1.1 alm cmd->t = compile_text();
240 1.1 alm break;
241 1.1 alm case COMMENT: /* \0 # */
242 1.1 alm break;
243 1.1 alm case WFILE: /* w */
244 1.1 alm p++;
245 1.1 alm EATSPACE();
246 1.1 alm if (*p == '\0')
247 1.1 alm err(COMPILE, "filename expected");
248 1.1 alm cmd->t = duptoeol(p);
249 1.1 alm if (aflag)
250 1.1 alm cmd->u.fd = -1;
251 1.1 alm else if ((cmd->u.fd = open(p,
252 1.1 alm O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
253 1.1 alm DEFFILEMODE)) == -1)
254 1.1 alm err(FATAL, "%s: %s\n", p, strerror(errno));
255 1.1 alm break;
256 1.1 alm case RFILE: /* r */
257 1.1 alm p++;
258 1.1 alm EATSPACE();
259 1.1 alm if (*p == '\0')
260 1.1 alm err(COMPILE, "filename expected");
261 1.1 alm else
262 1.1 alm cmd->t = duptoeol(p);
263 1.1 alm break;
264 1.1 alm case BRANCH: /* b t */
265 1.1 alm p++;
266 1.1 alm EATSPACE();
267 1.1 alm if (*p == '\0')
268 1.1 alm cmd->t = NULL;
269 1.1 alm else
270 1.1 alm cmd->t = duptoeol(p);
271 1.1 alm break;
272 1.1 alm case LABEL: /* : */
273 1.1 alm p++;
274 1.1 alm EATSPACE();
275 1.1 alm cmd->t = duptoeol(p);
276 1.1 alm if (strlen(p) == 0)
277 1.1 alm err(COMPILE, "empty label");
278 1.1 alm break;
279 1.1 alm case SUBST: /* s */
280 1.1 alm p++;
281 1.1 alm if (*p == '\0' || *p == '\\')
282 1.1 alm err(COMPILE,
283 1.1 alm "substitute pattern can not be delimited by newline or backslash");
284 1.1 alm cmd->u.s = xmalloc(sizeof(struct s_subst));
285 1.1 alm p = compile_re(p, &cmd->u.s->re);
286 1.1 alm if (p == NULL)
287 1.1 alm err(COMPILE, "unterminated substitute pattern");
288 1.1 alm --p;
289 1.1 alm p = compile_subst(p, cmd->u.s);
290 1.1 alm p = compile_flags(p, cmd->u.s);
291 1.1 alm EATSPACE();
292 1.1 alm if (*p == ';') {
293 1.1 alm p++;
294 1.1 alm link = &cmd->next;
295 1.1 alm goto semicolon;
296 1.1 alm }
297 1.1 alm break;
298 1.1 alm case TR: /* y */
299 1.1 alm p++;
300 1.1 alm p = compile_tr(p, (char **)&cmd->u.y);
301 1.1 alm EATSPACE();
302 1.1 alm if (*p == ';') {
303 1.1 alm p++;
304 1.1 alm link = &cmd->next;
305 1.1 alm goto semicolon;
306 1.1 alm }
307 1.1 alm if (*p)
308 1.1 alm err(COMPILE,
309 1.1 alm "extra text at the end of a transform command");
310 1.1 alm break;
311 1.1 alm }
312 1.1 alm }
313 1.1 alm }
314 1.1 alm
315 1.1 alm /*
316 1.1 alm * Get a delimited string. P points to the delimeter of the string; d points
317 1.1 alm * to a buffer area. Newline and delimiter escapes are processed; other
318 1.1 alm * escapes are ignored.
319 1.1 alm *
320 1.1 alm * Returns a pointer to the first character after the final delimiter or NULL
321 1.1 alm * in the case of a non-terminated string. The character array d is filled
322 1.1 alm * with the processed string.
323 1.1 alm */
324 1.1 alm static char *
325 1.1 alm compile_delimited(p, d)
326 1.1 alm char *p, *d;
327 1.1 alm {
328 1.1 alm char c;
329 1.1 alm
330 1.1 alm c = *p++;
331 1.1 alm if (c == '\0')
332 1.1 alm return (NULL);
333 1.1 alm else if (c == '\\')
334 1.1 alm err(COMPILE, "\\ can not be used as a string delimiter");
335 1.1 alm else if (c == '\n')
336 1.1 alm err(COMPILE, "newline can not be used as a string delimiter");
337 1.1 alm while (*p) {
338 1.2 alm if (*p == '[') {
339 1.2 alm if ((d = compile_ccl(&p, d)) == NULL)
340 1.2 alm err(COMPILE, "unbalanced brackets ([])");
341 1.2 alm continue;
342 1.2 alm } else if (*p == '\\' && p[1] == c)
343 1.1 alm p++;
344 1.1 alm else if (*p == '\\' && p[1] == 'n') {
345 1.1 alm *d++ = '\n';
346 1.1 alm p += 2;
347 1.1 alm continue;
348 1.1 alm } else if (*p == '\\' && p[1] == '\\')
349 1.1 alm *d++ = *p++;
350 1.1 alm else if (*p == c) {
351 1.1 alm *d = '\0';
352 1.1 alm return (p + 1);
353 1.1 alm }
354 1.1 alm *d++ = *p++;
355 1.1 alm }
356 1.1 alm return (NULL);
357 1.2 alm }
358 1.2 alm
359 1.2 alm
360 1.2 alm /* compile_ccl: expand a POSIX character class */
361 1.2 alm static char *
362 1.2 alm compile_ccl(sp, t)
363 1.2 alm char **sp;
364 1.2 alm char *t;
365 1.2 alm {
366 1.2 alm int c, d;
367 1.2 alm char *s = *sp;
368 1.2 alm
369 1.2 alm *t++ = *s++;
370 1.2 alm if (*s == '^')
371 1.2 alm *t++ = *s++;
372 1.2 alm if (*s == ']')
373 1.2 alm *t++ = *s++;
374 1.2 alm for (; *s && (*t = *s) != ']'; s++, t++)
375 1.2 alm if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) {
376 1.2 alm *++t = *++s, t++, s++;
377 1.2 alm for (c = *s; (*t = *s) != ']' || c != d; s++, t++)
378 1.2 alm if ((c = *s) == '\0')
379 1.2 alm return NULL;
380 1.2 alm } else if (*s == '\\' && s[1] == 'n')
381 1.2 alm *t = '\n', s++;
382 1.2 alm return (*s == ']') ? *sp = ++s, ++t : NULL;
383 1.1 alm }
384 1.1 alm
385 1.1 alm /*
386 1.1 alm * Get a regular expression. P points to the delimiter of the regular
387 1.1 alm * expression; repp points to the address of a regexp pointer. Newline
388 1.1 alm * and delimiter escapes are processed; other escapes are ignored.
389 1.1 alm * Returns a pointer to the first character after the final delimiter
390 1.1 alm * or NULL in the case of a non terminated regular expression. The regexp
391 1.1 alm * pointer is set to the compiled regular expression.
392 1.1 alm * Cflags are passed to regcomp.
393 1.1 alm */
394 1.1 alm static char *
395 1.1 alm compile_re(p, repp)
396 1.1 alm char *p;
397 1.1 alm regex_t **repp;
398 1.1 alm {
399 1.1 alm int eval;
400 1.1 alm char re[_POSIX2_LINE_MAX + 1];
401 1.1 alm
402 1.1 alm p = compile_delimited(p, re);
403 1.1 alm if (p && strlen(re) == 0) {
404 1.1 alm *repp = NULL;
405 1.1 alm return (p);
406 1.1 alm }
407 1.1 alm *repp = xmalloc(sizeof(regex_t));
408 1.1 alm #ifdef GNU_REGEX
409 1.1 alm /* initialize pattern buffer */
410 1.1 alm (*repp)->buffer = NULL;
411 1.1 alm (*repp)->allocated = 0L;
412 1.3 alm (*repp)->fastmap = 0; /* fastmap not used by regex > 0.12 */
413 1.1 alm (*repp)->translate = 0;
414 1.1 alm #endif
415 1.1 alm if (p && (eval = regcomp(*repp, re, 0)) != 0)
416 1.1 alm err(COMPILE, "RE error: %s", strregerror(eval, *repp));
417 1.1 alm if (maxnsub < (*repp)->re_nsub)
418 1.1 alm maxnsub = (*repp)->re_nsub;
419 1.1 alm return (p);
420 1.1 alm }
421 1.1 alm
422 1.1 alm /*
423 1.1 alm * Compile the substitution string of a regular expression and set res to
424 1.1 alm * point to a saved copy of it. Nsub is the number of parenthesized regular
425 1.1 alm * expressions.
426 1.1 alm */
427 1.1 alm static char *
428 1.1 alm compile_subst(p, s)
429 1.1 alm char *p;
430 1.1 alm struct s_subst *s;
431 1.1 alm {
432 1.1 alm static char lbuf[_POSIX2_LINE_MAX + 1];
433 1.1 alm int asize, ref, size;
434 1.1 alm char c, *text, *op, *sp;
435 1.1 alm
436 1.1 alm c = *p++; /* Terminator character */
437 1.1 alm if (c == '\0')
438 1.1 alm return (NULL);
439 1.1 alm
440 1.1 alm s->maxbref = 0;
441 1.1 alm s->linenum = linenum;
442 1.1 alm asize = 2 * _POSIX2_LINE_MAX + 1;
443 1.1 alm text = xmalloc(asize);
444 1.1 alm size = 0;
445 1.1 alm do {
446 1.1 alm op = sp = text + size;
447 1.1 alm for (; *p; p++) {
448 1.1 alm if (*p == '\\') {
449 1.1 alm p++;
450 1.1 alm if (strchr("123456789", *p) != NULL) {
451 1.1 alm *sp++ = '\\';
452 1.1 alm ref = *p - '0';
453 1.1 alm if (s->re != NULL &&
454 1.1 alm ref > s->re->re_nsub)
455 1.1 alm err(COMPILE,
456 1.1 alm "\\%c not defined in the RE", *p);
457 1.1 alm if (s->maxbref < ref)
458 1.1 alm s->maxbref = ref;
459 1.1 alm } else if (*p == '&' || *p == '\\')
460 1.1 alm *sp++ = '\\';
461 1.1 alm } else if (*p == c) {
462 1.1 alm p++;
463 1.1 alm *sp++ = '\0';
464 1.1 alm size += sp - op;
465 1.1 alm s->new = xrealloc(text, size);
466 1.1 alm return (p);
467 1.1 alm } else if (*p == '\n') {
468 1.1 alm err(COMPILE,
469 1.1 alm "unescaped newline inside substitute pattern");
470 1.1 alm /* NOTREACHED */
471 1.1 alm }
472 1.1 alm *sp++ = *p;
473 1.1 alm }
474 1.1 alm size += sp - op;
475 1.1 alm if (asize - size < _POSIX2_LINE_MAX + 1) {
476 1.1 alm asize *= 2;
477 1.1 alm text = xmalloc(asize);
478 1.1 alm }
479 1.1 alm } while (cu_fgets(p = lbuf, sizeof(lbuf)));
480 1.1 alm err(COMPILE, "unterminated substitute in regular expression");
481 1.1 alm /* NOTREACHED */
482 1.1 alm }
483 1.1 alm
484 1.1 alm /*
485 1.1 alm * Compile the flags of the s command
486 1.1 alm */
487 1.1 alm static char *
488 1.1 alm compile_flags(p, s)
489 1.1 alm char *p;
490 1.1 alm struct s_subst *s;
491 1.1 alm {
492 1.1 alm int gn; /* True if we have seen g or n */
493 1.1 alm char wfile[_POSIX2_LINE_MAX + 1], *q;
494 1.1 alm
495 1.1 alm s->n = 1; /* Default */
496 1.1 alm s->p = 0;
497 1.1 alm s->wfile = NULL;
498 1.1 alm s->wfd = -1;
499 1.1 alm for (gn = 0;;) {
500 1.1 alm EATSPACE(); /* EXTENSION */
501 1.1 alm switch (*p) {
502 1.1 alm case 'g':
503 1.1 alm if (gn)
504 1.1 alm err(COMPILE,
505 1.1 alm "more than one number or 'g' in substitute flags");
506 1.1 alm gn = 1;
507 1.1 alm s->n = 0;
508 1.1 alm break;
509 1.1 alm case '\0':
510 1.1 alm case '\n':
511 1.1 alm case ';':
512 1.1 alm return (p);
513 1.1 alm case 'p':
514 1.1 alm s->p = 1;
515 1.1 alm break;
516 1.1 alm case '1': case '2': case '3':
517 1.1 alm case '4': case '5': case '6':
518 1.1 alm case '7': case '8': case '9':
519 1.1 alm if (gn)
520 1.1 alm err(COMPILE,
521 1.1 alm "more than one number or 'g' in substitute flags");
522 1.1 alm gn = 1;
523 1.1 alm /* XXX Check for overflow */
524 1.1 alm s->n = (int)strtol(p, &p, 10);
525 1.1 alm break;
526 1.1 alm case 'w':
527 1.1 alm p++;
528 1.1 alm #ifdef HISTORIC_PRACTICE
529 1.1 alm if (*p != ' ') {
530 1.1 alm err(WARNING, "space missing before w wfile");
531 1.1 alm return (p);
532 1.1 alm }
533 1.1 alm #endif
534 1.1 alm EATSPACE();
535 1.1 alm q = wfile;
536 1.1 alm while (*p) {
537 1.1 alm if (*p == '\n')
538 1.1 alm break;
539 1.1 alm *q++ = *p++;
540 1.1 alm }
541 1.1 alm *q = '\0';
542 1.1 alm if (q == wfile)
543 1.1 alm err(COMPILE, "no wfile specified");
544 1.1 alm s->wfile = strdup(wfile);
545 1.1 alm if (!aflag && (s->wfd = open(wfile,
546 1.1 alm O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
547 1.1 alm DEFFILEMODE)) == -1)
548 1.1 alm err(FATAL, "%s: %s\n", wfile, strerror(errno));
549 1.1 alm return (p);
550 1.1 alm default:
551 1.1 alm err(COMPILE,
552 1.1 alm "bad flag in substitute command: '%c'", *p);
553 1.1 alm break;
554 1.1 alm }
555 1.1 alm p++;
556 1.1 alm }
557 1.1 alm }
558 1.1 alm
559 1.1 alm /*
560 1.1 alm * Compile a translation set of strings into a lookup table.
561 1.1 alm */
562 1.1 alm static char *
563 1.1 alm compile_tr(p, transtab)
564 1.1 alm char *p;
565 1.1 alm char **transtab;
566 1.1 alm {
567 1.1 alm int i;
568 1.1 alm char *lt, *op, *np;
569 1.1 alm char old[_POSIX2_LINE_MAX + 1];
570 1.1 alm char new[_POSIX2_LINE_MAX + 1];
571 1.1 alm
572 1.1 alm if (*p == '\0' || *p == '\\')
573 1.1 alm err(COMPILE,
574 1.1 alm "transform pattern can not be delimited by newline or backslash");
575 1.1 alm p = compile_delimited(p, old);
576 1.1 alm if (p == NULL) {
577 1.1 alm err(COMPILE, "unterminated transform source string");
578 1.1 alm return (NULL);
579 1.1 alm }
580 1.1 alm p = compile_delimited(--p, new);
581 1.1 alm if (p == NULL) {
582 1.1 alm err(COMPILE, "unterminated transform target string");
583 1.1 alm return (NULL);
584 1.1 alm }
585 1.1 alm EATSPACE();
586 1.1 alm if (strlen(new) != strlen(old)) {
587 1.1 alm err(COMPILE, "transform strings are not the same length");
588 1.1 alm return (NULL);
589 1.1 alm }
590 1.1 alm /* We assume characters are 8 bits */
591 1.1 alm lt = xmalloc(UCHAR_MAX);
592 1.1 alm for (i = 0; i <= UCHAR_MAX; i++)
593 1.1 alm lt[i] = (char)i;
594 1.1 alm for (op = old, np = new; *op; op++, np++)
595 1.1 alm lt[(u_char)*op] = *np;
596 1.1 alm *transtab = lt;
597 1.1 alm return (p);
598 1.1 alm }
599 1.1 alm
600 1.1 alm /*
601 1.1 alm * Compile the text following an a or i command.
602 1.1 alm */
603 1.1 alm static char *
604 1.1 alm compile_text()
605 1.1 alm {
606 1.1 alm int asize, size;
607 1.1 alm char *text, *p, *op, *s;
608 1.1 alm char lbuf[_POSIX2_LINE_MAX + 1];
609 1.1 alm
610 1.1 alm asize = 2 * _POSIX2_LINE_MAX + 1;
611 1.1 alm text = xmalloc(asize);
612 1.1 alm size = 0;
613 1.1 alm while (cu_fgets(lbuf, sizeof(lbuf))) {
614 1.1 alm op = s = text + size;
615 1.1 alm p = lbuf;
616 1.1 alm EATSPACE();
617 1.1 alm for (; *p; p++) {
618 1.1 alm if (*p == '\\')
619 1.1 alm p++;
620 1.1 alm *s++ = *p;
621 1.1 alm }
622 1.1 alm size += s - op;
623 1.1 alm if (p[-2] != '\\') {
624 1.1 alm *s = '\0';
625 1.1 alm break;
626 1.1 alm }
627 1.1 alm if (asize - size < _POSIX2_LINE_MAX + 1) {
628 1.1 alm asize *= 2;
629 1.1 alm text = xmalloc(asize);
630 1.1 alm }
631 1.1 alm }
632 1.1 alm return (xrealloc(text, size + 1));
633 1.1 alm }
634 1.1 alm
635 1.1 alm /*
636 1.1 alm * Get an address and return a pointer to the first character after
637 1.1 alm * it. Fill the structure pointed to according to the address.
638 1.1 alm */
639 1.1 alm static char *
640 1.1 alm compile_addr(p, a)
641 1.1 alm char *p;
642 1.1 alm struct s_addr *a;
643 1.1 alm {
644 1.1 alm char *end;
645 1.1 alm
646 1.1 alm switch (*p) {
647 1.1 alm case '\\': /* Context address */
648 1.1 alm ++p;
649 1.1 alm /* FALLTHROUGH */
650 1.1 alm case '/': /* Context address */
651 1.1 alm p = compile_re(p, &a->u.r);
652 1.1 alm if (p == NULL)
653 1.1 alm err(COMPILE, "unterminated regular expression");
654 1.1 alm a->type = AT_RE;
655 1.1 alm return (p);
656 1.1 alm
657 1.1 alm case '$': /* Last line */
658 1.1 alm a->type = AT_LAST;
659 1.1 alm return (p + 1);
660 1.1 alm /* Line number */
661 1.1 alm case '0': case '1': case '2': case '3': case '4':
662 1.1 alm case '5': case '6': case '7': case '8': case '9':
663 1.1 alm a->type = AT_LINE;
664 1.1 alm a->u.l = strtol(p, &end, 10);
665 1.1 alm return (end);
666 1.1 alm default:
667 1.1 alm err(COMPILE, "expected context address");
668 1.1 alm return (NULL);
669 1.1 alm }
670 1.1 alm }
671 1.1 alm
672 1.1 alm /*
673 1.1 alm * Return a copy of all the characters up to \n or \0
674 1.1 alm */
675 1.1 alm static char *
676 1.1 alm duptoeol(s)
677 1.1 alm register char *s;
678 1.1 alm {
679 1.1 alm size_t len;
680 1.1 alm char *start;
681 1.1 alm
682 1.1 alm for (start = s; *s != '\0' && *s != '\n'; ++s);
683 1.1 alm *s = '\0';
684 1.1 alm len = s - start + 1;
685 1.1 alm return (memmove(xmalloc(len), start, len));
686 1.1 alm }
687 1.1 alm
688 1.1 alm /*
689 1.1 alm * Find the label contained in the command l in the command linked list cp.
690 1.1 alm * L is excluded from the search. Return NULL if not found.
691 1.1 alm */
692 1.1 alm static struct s_command *
693 1.1 alm findlabel(l, cp)
694 1.1 alm struct s_command *l, *cp;
695 1.1 alm {
696 1.1 alm struct s_command *r;
697 1.1 alm
698 1.1 alm for (; cp; cp = cp->next)
699 1.1 alm if (cp->code == ':' && cp != l && strcmp(l->t, cp->t) == 0)
700 1.1 alm return (cp);
701 1.1 alm else if (cp->code == '{' && (r = findlabel(l, cp->u.c)))
702 1.1 alm return (r);
703 1.1 alm return (NULL);
704 1.1 alm }
705 1.1 alm
706 1.1 alm /*
707 1.1 alm * Convert goto label names to addresses.
708 1.1 alm * Detect duplicate labels.
709 1.1 alm * Set appendnum to the number of a and r commands in the script.
710 1.1 alm * Free the memory used by labels in b and t commands (but not by :)
711 1.1 alm * Root is a pointer to the script linked list; cp points to the
712 1.1 alm * search start.
713 1.1 alm * TODO: Remove } nodes
714 1.1 alm */
715 1.1 alm static void
716 1.1 alm fixuplabel(root, cp, end)
717 1.1 alm struct s_command *root, *cp, *end;
718 1.1 alm {
719 1.1 alm struct s_command *cp2;
720 1.1 alm
721 1.1 alm for (; cp != end; cp = cp->next)
722 1.1 alm switch (cp->code) {
723 1.1 alm case ':':
724 1.1 alm if (findlabel(cp, root))
725 1.1 alm err(COMPILE2, "duplicate label %s", cp->t);
726 1.1 alm break;
727 1.1 alm case 'a':
728 1.1 alm case 'r':
729 1.1 alm appendnum++;
730 1.1 alm break;
731 1.1 alm case 'b':
732 1.1 alm case 't':
733 1.1 alm if (cp->t == NULL) {
734 1.1 alm cp->u.c = NULL;
735 1.1 alm break;
736 1.1 alm }
737 1.1 alm if ((cp2 = findlabel(cp, root)) == NULL)
738 1.1 alm err(COMPILE2, "undefined label '%s'", cp->t);
739 1.1 alm free(cp->t);
740 1.1 alm cp->u.c = cp2;
741 1.1 alm break;
742 1.1 alm case '{':
743 1.1 alm fixuplabel(root, cp->u.c, cp->next);
744 1.1 alm break;
745 1.1 alm }
746 1.1 alm }
747