Deleted Added
full compact
compile.c (17522) compile.c (28066)
1/*-
2 * Copyright (c) 1992 Diomidis Spinellis.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Diomidis Spinellis of Imperial College, University of London.
8 *

--- 22 unchanged lines hidden (view full) ---

31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
1/*-
2 * Copyright (c) 1992 Diomidis Spinellis.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Diomidis Spinellis of Imperial College, University of London.
8 *

--- 22 unchanged lines hidden (view full) ---

31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * SUCH DAMAGE.
36 */
37
38#ifndef lint
39#if 0
39static char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
40static char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93";
41#endif
42static const char rcsid[] =
43 "$Id$";
40#endif /* not lint */
41
42#include <sys/types.h>
43#include <sys/stat.h>
44
45#include <ctype.h>
44#endif /* not lint */
45
46#include <sys/types.h>
47#include <sys/stat.h>
48
49#include <ctype.h>
46#include <errno.h>
50#include
47#include <fcntl.h>
48#include <limits.h>
49#include <regex.h>
50#include <stdio.h>
51#include <stdlib.h>
52#include <string.h>
53
54#include "defs.h"

--- 98 unchanged lines hidden (view full) ---

153 struct s_command *cmd, *cmd2, *stack;
154 struct s_format *fp;
155 int naddr; /* Number of addresses */
156
157 stack = 0;
158 for (;;) {
159 if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
160 if (stack != 0)
51#include <fcntl.h>
52#include <limits.h>
53#include <regex.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
57
58#include "defs.h"

--- 98 unchanged lines hidden (view full) ---

157 struct s_command *cmd, *cmd2, *stack;
158 struct s_format *fp;
159 int naddr; /* Number of addresses */
160
161 stack = 0;
162 for (;;) {
163 if ((p = cu_fgets(lbuf, sizeof(lbuf))) == NULL) {
164 if (stack != 0)
161 err(COMPILE, "unexpected EOF (pending }'s)");
165 errx(1, "%lu: %s: unexpected EOF (pending }'s)",
166 linenum, fname);
162 return (link);
163 }
164
165semicolon: EATSPACE();
166 if (p && (*p == '#' || *p == '\0'))
167 continue;
168 *link = cmd = xmalloc(sizeof(struct s_command));
169 link = &cmd->next;

--- 17 unchanged lines hidden (view full) ---

187 EATSPACE();
188 } else
189 cmd->a2 = 0;
190 } else
191 cmd->a1 = cmd->a2 = 0;
192
193nonsel: /* Now parse the command */
194 if (!*p)
167 return (link);
168 }
169
170semicolon: EATSPACE();
171 if (p && (*p == '#' || *p == '\0'))
172 continue;
173 *link = cmd = xmalloc(sizeof(struct s_command));
174 link = &cmd->next;

--- 17 unchanged lines hidden (view full) ---

192 EATSPACE();
193 } else
194 cmd->a2 = 0;
195 } else
196 cmd->a1 = cmd->a2 = 0;
197
198nonsel: /* Now parse the command */
199 if (!*p)
195 err(COMPILE, "command expected");
200 errx(1, "%lu: %s: command expected", linenum, fname);
196 cmd->code = *p;
197 for (fp = cmd_fmts; fp->code; fp++)
198 if (fp->code == *p)
199 break;
200 if (!fp->code)
201 cmd->code = *p;
202 for (fp = cmd_fmts; fp->code; fp++)
203 if (fp->code == *p)
204 break;
205 if (!fp->code)
201 err(COMPILE, "invalid command code %c", *p);
206 errx(1, "%lu: %s: invalid command code %c", linenum, fname, *p);
202 if (naddr > fp->naddr)
207 if (naddr > fp->naddr)
203 err(COMPILE,
204"command %c expects up to %d address(es), found %d", *p, fp->naddr, naddr);
208 errx(1,
209 "%lu: %s: command %c expects up to %d address(es), found %d",
210 linenum, fname, *p, fp->naddr, naddr);
205 switch (fp->args) {
206 case NONSEL: /* ! */
207 p++;
208 EATSPACE();
209 cmd->nonsel = ! cmd->nonsel;
210 goto nonsel;
211 case GROUP: /* { */
212 p++;

--- 6 unchanged lines hidden (view full) ---

219 break;
220 case ENDGROUP:
221 /*
222 * Short-circuit command processing, since end of
223 * group is really just a noop.
224 */
225 cmd->nonsel = 1;
226 if (stack == 0)
211 switch (fp->args) {
212 case NONSEL: /* ! */
213 p++;
214 EATSPACE();
215 cmd->nonsel = ! cmd->nonsel;
216 goto nonsel;
217 case GROUP: /* { */
218 p++;

--- 6 unchanged lines hidden (view full) ---

225 break;
226 case ENDGROUP:
227 /*
228 * Short-circuit command processing, since end of
229 * group is really just a noop.
230 */
231 cmd->nonsel = 1;
232 if (stack == 0)
227 err(COMPILE, "unexpected }");
233 errx(1, "%lu: %s: unexpected }", linenum, fname);
228 cmd2 = stack;
229 stack = cmd2->next;
230 cmd2->next = cmd;
231 /*FALLTHROUGH*/
232 case EMPTY: /* d D g G h H l n N p P q x = \0 */
233 p++;
234 EATSPACE();
235 if (*p == ';') {
236 p++;
237 link = &cmd->next;
238 goto semicolon;
239 }
240 if (*p)
234 cmd2 = stack;
235 stack = cmd2->next;
236 cmd2->next = cmd;
237 /*FALLTHROUGH*/
238 case EMPTY: /* d D g G h H l n N p P q x = \0 */
239 p++;
240 EATSPACE();
241 if (*p == ';') {
242 p++;
243 link = &cmd->next;
244 goto semicolon;
245 }
246 if (*p)
241 err(COMPILE,
242"extra characters at the end of %c command", cmd->code);
247 errx(1, "%lu: %s: extra characters at the end of %c command",
248 linenum, fname, cmd->code);
243 break;
244 case TEXT: /* a c i */
245 p++;
246 EATSPACE();
247 if (*p != '\\')
249 break;
250 case TEXT: /* a c i */
251 p++;
252 EATSPACE();
253 if (*p != '\\')
248 err(COMPILE,
249"command %c expects \\ followed by text", cmd->code);
254 errx(1,
255"%lu: %s: command %c expects \\ followed by text", linenum, fname, cmd->code);
250 p++;
251 EATSPACE();
252 if (*p)
256 p++;
257 EATSPACE();
258 if (*p)
253 err(COMPILE,
254"extra characters after \\ at the end of %c command", cmd->code);
259 errx(1,
260 "%lu: %s: extra characters after \\ at the end of %c command",
261 linenum, fname, cmd->code);
255 cmd->t = compile_text();
256 break;
257 case COMMENT: /* \0 # */
258 break;
259 case WFILE: /* w */
260 p++;
261 EATSPACE();
262 if (*p == '\0')
262 cmd->t = compile_text();
263 break;
264 case COMMENT: /* \0 # */
265 break;
266 case WFILE: /* w */
267 p++;
268 EATSPACE();
269 if (*p == '\0')
263 err(COMPILE, "filename expected");
270 errx(1, "%lu: %s: filename expected", linenum, fname);
264 cmd->t = duptoeol(p, "w command");
265 if (aflag)
266 cmd->u.fd = -1;
267 else if ((cmd->u.fd = open(p,
268 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
269 DEFFILEMODE)) == -1)
271 cmd->t = duptoeol(p, "w command");
272 if (aflag)
273 cmd->u.fd = -1;
274 else if ((cmd->u.fd = open(p,
275 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
276 DEFFILEMODE)) == -1)
270 err(FATAL, "%s: %s\n", p, strerror(errno));
277 err(1, "%s", p);
271 break;
272 case RFILE: /* r */
273 p++;
274 EATSPACE();
275 if (*p == '\0')
278 break;
279 case RFILE: /* r */
280 p++;
281 EATSPACE();
282 if (*p == '\0')
276 err(COMPILE, "filename expected");
283 errx(1, "%lu: %s: filename expected", linenum, fname);
277 else
278 cmd->t = duptoeol(p, "read command");
279 break;
280 case BRANCH: /* b t */
281 p++;
282 EATSPACE();
283 if (*p == '\0')
284 cmd->t = NULL;
285 else
286 cmd->t = duptoeol(p, "branch");
287 break;
288 case LABEL: /* : */
289 p++;
290 EATSPACE();
291 cmd->t = duptoeol(p, "label");
292 if (strlen(p) == 0)
284 else
285 cmd->t = duptoeol(p, "read command");
286 break;
287 case BRANCH: /* b t */
288 p++;
289 EATSPACE();
290 if (*p == '\0')
291 cmd->t = NULL;
292 else
293 cmd->t = duptoeol(p, "branch");
294 break;
295 case LABEL: /* : */
296 p++;
297 EATSPACE();
298 cmd->t = duptoeol(p, "label");
299 if (strlen(p) == 0)
293 err(COMPILE, "empty label");
300 errx(1, "%lu: %s: empty label", linenum, fname);
294 enterlabel(cmd);
295 break;
296 case SUBST: /* s */
297 p++;
298 if (*p == '\0' || *p == '\\')
301 enterlabel(cmd);
302 break;
303 case SUBST: /* s */
304 p++;
305 if (*p == '\0' || *p == '\\')
299 err(COMPILE,
300"substitute pattern can not be delimited by newline or backslash");
306 errx(1,
307"%lu: %s: substitute pattern can not be delimited by newline or backslash",
308 linenum, fname);
301 cmd->u.s = xmalloc(sizeof(struct s_subst));
302 p = compile_re(p, &cmd->u.s->re);
303 if (p == NULL)
309 cmd->u.s = xmalloc(sizeof(struct s_subst));
310 p = compile_re(p, &cmd->u.s->re);
311 if (p == NULL)
304 err(COMPILE, "unterminated substitute pattern");
312 errx(1,
313 "%lu: %s: unterminated substitute pattern", linenum, fname);
305 --p;
306 p = compile_subst(p, cmd->u.s);
307 p = compile_flags(p, cmd->u.s);
308 EATSPACE();
309 if (*p == ';') {
310 p++;
311 link = &cmd->next;
312 goto semicolon;

--- 4 unchanged lines hidden (view full) ---

317 p = compile_tr(p, (char **)&cmd->u.y);
318 EATSPACE();
319 if (*p == ';') {
320 p++;
321 link = &cmd->next;
322 goto semicolon;
323 }
324 if (*p)
314 --p;
315 p = compile_subst(p, cmd->u.s);
316 p = compile_flags(p, cmd->u.s);
317 EATSPACE();
318 if (*p == ';') {
319 p++;
320 link = &cmd->next;
321 goto semicolon;

--- 4 unchanged lines hidden (view full) ---

326 p = compile_tr(p, (char **)&cmd->u.y);
327 EATSPACE();
328 if (*p == ';') {
329 p++;
330 link = &cmd->next;
331 goto semicolon;
332 }
333 if (*p)
325 err(COMPILE,
326"extra text at the end of a transform command");
334 errx(1,
335"%lu: %s: extra text at the end of a transform command", linenum, fname);
327 break;
328 }
329 }
330}
331
332/*
333 * Get a delimited string. P points to the delimeter of the string; d points
334 * to a buffer area. Newline and delimiter escapes are processed; other

--- 8 unchanged lines hidden (view full) ---

343 char *p, *d;
344{
345 char c;
346
347 c = *p++;
348 if (c == '\0')
349 return (NULL);
350 else if (c == '\\')
336 break;
337 }
338 }
339}
340
341/*
342 * Get a delimited string. P points to the delimeter of the string; d points
343 * to a buffer area. Newline and delimiter escapes are processed; other

--- 8 unchanged lines hidden (view full) ---

352 char *p, *d;
353{
354 char c;
355
356 c = *p++;
357 if (c == '\0')
358 return (NULL);
359 else if (c == '\\')
351 err(COMPILE, "\\ can not be used as a string delimiter");
360 errx(1, "%lu: %s: \\ can not be used as a string delimiter",
361 linenum, fname);
352 else if (c == '\n')
362 else if (c == '\n')
353 err(COMPILE, "newline can not be used as a string delimiter");
363 errx(1, "%lu: %s: newline can not be used as a string delimiter",
364 linenum, fname);
354 while (*p) {
355 if (*p == '[') {
356 if ((d = compile_ccl(&p, d)) == NULL)
365 while (*p) {
366 if (*p == '[') {
367 if ((d = compile_ccl(&p, d)) == NULL)
357 err(COMPILE, "unbalanced brackets ([])");
368 errx(1, "%lu: %s: unbalanced brackets ([])", linenum, fname);
358 continue;
359 } else if (*p == '\\' && p[1] == '[') {
360 *d++ = *p++;
361 } else if (*p == '\\' && p[1] == c)
362 p++;
363 else if (*p == '\\' && p[1] == 'n') {
364 *d++ = '\n';
365 p += 2;

--- 54 unchanged lines hidden (view full) ---

420
421 p = compile_delimited(p, re);
422 if (p && strlen(re) == 0) {
423 *repp = NULL;
424 return (p);
425 }
426 *repp = xmalloc(sizeof(regex_t));
427 if (p && (eval = regcomp(*repp, re, 0)) != 0)
369 continue;
370 } else if (*p == '\\' && p[1] == '[') {
371 *d++ = *p++;
372 } else if (*p == '\\' && p[1] == c)
373 p++;
374 else if (*p == '\\' && p[1] == 'n') {
375 *d++ = '\n';
376 p += 2;

--- 54 unchanged lines hidden (view full) ---

431
432 p = compile_delimited(p, re);
433 if (p && strlen(re) == 0) {
434 *repp = NULL;
435 return (p);
436 }
437 *repp = xmalloc(sizeof(regex_t));
438 if (p && (eval = regcomp(*repp, re, 0)) != 0)
428 err(COMPILE, "RE error: %s", strregerror(eval, *repp));
439 errx(1, "%lu: %s: RE error: %s",
440 linenum, fname, strregerror(eval, *repp));
429 if (maxnsub < (*repp)->re_nsub)
430 maxnsub = (*repp)->re_nsub;
431 return (p);
432}
433
434/*
435 * Compile the substitution string of a regular expression and set res to
436 * point to a saved copy of it. Nsub is the number of parenthesized regular

--- 22 unchanged lines hidden (view full) ---

459 for (; *p; p++) {
460 if (*p == '\\') {
461 p++;
462 if (strchr("123456789", *p) != NULL) {
463 *sp++ = '\\';
464 ref = *p - '0';
465 if (s->re != NULL &&
466 ref > s->re->re_nsub)
441 if (maxnsub < (*repp)->re_nsub)
442 maxnsub = (*repp)->re_nsub;
443 return (p);
444}
445
446/*
447 * Compile the substitution string of a regular expression and set res to
448 * point to a saved copy of it. Nsub is the number of parenthesized regular

--- 22 unchanged lines hidden (view full) ---

471 for (; *p; p++) {
472 if (*p == '\\') {
473 p++;
474 if (strchr("123456789", *p) != NULL) {
475 *sp++ = '\\';
476 ref = *p - '0';
477 if (s->re != NULL &&
478 ref > s->re->re_nsub)
467 err(COMPILE,
468"\\%c not defined in the RE", *p);
479 errx(1, "%lu: %s: \\%c not defined in the RE",
480 linenum, fname, *p);
469 if (s->maxbref < ref)
470 s->maxbref = ref;
471 } else if (*p == '&' || *p == '\\')
472 *sp++ = '\\';
473 } else if (*p == c) {
474 p++;
475 *sp++ = '\0';
476 size += sp - op;
477 s->new = xrealloc(text, size);
478 return (p);
479 } else if (*p == '\n') {
481 if (s->maxbref < ref)
482 s->maxbref = ref;
483 } else if (*p == '&' || *p == '\\')
484 *sp++ = '\\';
485 } else if (*p == c) {
486 p++;
487 *sp++ = '\0';
488 size += sp - op;
489 s->new = xrealloc(text, size);
490 return (p);
491 } else if (*p == '\n') {
480 err(COMPILE,
481"unescaped newline inside substitute pattern");
492 errx(1,
493"%lu: %s: unescaped newline inside substitute pattern", linenum, fname);
482 /* NOTREACHED */
483 }
484 *sp++ = *p;
485 }
486 size += sp - op;
487 if (asize - size < _POSIX2_LINE_MAX + 1) {
488 asize *= 2;
489 text = xmalloc(asize);
490 }
491 } while (cu_fgets(p = lbuf, sizeof(lbuf)));
494 /* NOTREACHED */
495 }
496 *sp++ = *p;
497 }
498 size += sp - op;
499 if (asize - size < _POSIX2_LINE_MAX + 1) {
500 asize *= 2;
501 text = xmalloc(asize);
502 }
503 } while (cu_fgets(p = lbuf, sizeof(lbuf)));
492 err(COMPILE, "unterminated substitute in regular expression");
504 errx(1, "%lu: %s: unterminated substitute in regular expression",
505 linenum, fname);
493 /* NOTREACHED */
494}
495
496/*
497 * Compile the flags of the s command
498 */
499static char *
500compile_flags(p, s)

--- 7 unchanged lines hidden (view full) ---

508 s->p = 0;
509 s->wfile = NULL;
510 s->wfd = -1;
511 for (gn = 0;;) {
512 EATSPACE(); /* EXTENSION */
513 switch (*p) {
514 case 'g':
515 if (gn)
506 /* NOTREACHED */
507}
508
509/*
510 * Compile the flags of the s command
511 */
512static char *
513compile_flags(p, s)

--- 7 unchanged lines hidden (view full) ---

521 s->p = 0;
522 s->wfile = NULL;
523 s->wfd = -1;
524 for (gn = 0;;) {
525 EATSPACE(); /* EXTENSION */
526 switch (*p) {
527 case 'g':
528 if (gn)
516 err(COMPILE,
517"more than one number or 'g' in substitute flags");
529 errx(1,
530"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
518 gn = 1;
519 s->n = 0;
520 break;
521 case '\0':
522 case '\n':
523 case ';':
524 return (p);
525 case 'p':
526 s->p = 1;
527 break;
528 case '1': case '2': case '3':
529 case '4': case '5': case '6':
530 case '7': case '8': case '9':
531 if (gn)
531 gn = 1;
532 s->n = 0;
533 break;
534 case '\0':
535 case '\n':
536 case ';':
537 return (p);
538 case 'p':
539 s->p = 1;
540 break;
541 case '1': case '2': case '3':
542 case '4': case '5': case '6':
543 case '7': case '8': case '9':
544 if (gn)
532 err(COMPILE,
533"more than one number or 'g' in substitute flags");
545 errx(1,
546"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname);
534 gn = 1;
535 /* XXX Check for overflow */
536 s->n = (int)strtol(p, &p, 10);
537 break;
538 case 'w':
539 p++;
540#ifdef HISTORIC_PRACTICE
541 if (*p != ' ') {
547 gn = 1;
548 /* XXX Check for overflow */
549 s->n = (int)strtol(p, &p, 10);
550 break;
551 case 'w':
552 p++;
553#ifdef HISTORIC_PRACTICE
554 if (*p != ' ') {
542 err(WARNING, "space missing before w wfile");
555 warnx("%lu: %s: space missing before w wfile", linenum, fname);
543 return (p);
544 }
545#endif
546 EATSPACE();
547 q = wfile;
548 while (*p) {
549 if (*p == '\n')
550 break;
551 *q++ = *p++;
552 }
553 *q = '\0';
554 if (q == wfile)
556 return (p);
557 }
558#endif
559 EATSPACE();
560 q = wfile;
561 while (*p) {
562 if (*p == '\n')
563 break;
564 *q++ = *p++;
565 }
566 *q = '\0';
567 if (q == wfile)
555 err(COMPILE, "no wfile specified");
568 errx(1, "%lu: %s: no wfile specified", linenum, fname);
556 s->wfile = strdup(wfile);
557 if (!aflag && (s->wfd = open(wfile,
558 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
559 DEFFILEMODE)) == -1)
569 s->wfile = strdup(wfile);
570 if (!aflag && (s->wfd = open(wfile,
571 O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
572 DEFFILEMODE)) == -1)
560 err(FATAL, "%s: %s\n", wfile, strerror(errno));
573 err(1, "%s", wfile);
561 return (p);
562 default:
574 return (p);
575 default:
563 err(COMPILE,
564 "bad flag in substitute command: '%c'", *p);
576 errx(1, "%lu: %s: bad flag in substitute command: '%c'",
577 linenum, fname, *p);
565 break;
566 }
567 p++;
568 }
569}
570
571/*
572 * Compile a translation set of strings into a lookup table.

--- 4 unchanged lines hidden (view full) ---

577 char **transtab;
578{
579 int i;
580 char *lt, *op, *np;
581 char old[_POSIX2_LINE_MAX + 1];
582 char new[_POSIX2_LINE_MAX + 1];
583
584 if (*p == '\0' || *p == '\\')
578 break;
579 }
580 p++;
581 }
582}
583
584/*
585 * Compile a translation set of strings into a lookup table.

--- 4 unchanged lines hidden (view full) ---

590 char **transtab;
591{
592 int i;
593 char *lt, *op, *np;
594 char old[_POSIX2_LINE_MAX + 1];
595 char new[_POSIX2_LINE_MAX + 1];
596
597 if (*p == '\0' || *p == '\\')
585 err(COMPILE,
586"transform pattern can not be delimited by newline or backslash");
598 errx(1,
599 "%lu: %s: transform pattern can not be delimited by newline or backslash",
600 linenum, fname);
587 p = compile_delimited(p, old);
601 p = compile_delimited(p, old);
588 if (p == NULL) {
589 err(COMPILE, "unterminated transform source string");
590 return (NULL);
591 }
602 if (p == NULL)
603 errx(1, "%lu: %s: unterminated transform source string",
604 linenum, fname);
592 p = compile_delimited(--p, new);
605 p = compile_delimited(--p, new);
593 if (p == NULL) {
594 err(COMPILE, "unterminated transform target string");
595 return (NULL);
596 }
606 if (p == NULL)
607 errx(1, "%lu: %s: unterminated transform target string",
608 linenum, fname);
597 EATSPACE();
609 EATSPACE();
598 if (strlen(new) != strlen(old)) {
599 err(COMPILE, "transform strings are not the same length");
600 return (NULL);
601 }
610 if (strlen(new) != strlen(old))
611 errx(1, "%lu: %s: transform strings are not the same length",
612 linenum, fname);
602 /* We assume characters are 8 bits */
603 lt = xmalloc(UCHAR_MAX);
604 for (i = 0; i <= UCHAR_MAX; i++)
605 lt[i] = (char)i;
606 for (op = old, np = new; *op; op++, np++)
607 lt[(u_char)*op] = *np;
608 *transtab = lt;
609 return (p);

--- 47 unchanged lines hidden (view full) ---

657
658 switch (*p) {
659 case '\\': /* Context address */
660 ++p;
661 /* FALLTHROUGH */
662 case '/': /* Context address */
663 p = compile_re(p, &a->u.r);
664 if (p == NULL)
613 /* We assume characters are 8 bits */
614 lt = xmalloc(UCHAR_MAX);
615 for (i = 0; i <= UCHAR_MAX; i++)
616 lt[i] = (char)i;
617 for (op = old, np = new; *op; op++, np++)
618 lt[(u_char)*op] = *np;
619 *transtab = lt;
620 return (p);

--- 47 unchanged lines hidden (view full) ---

668
669 switch (*p) {
670 case '\\': /* Context address */
671 ++p;
672 /* FALLTHROUGH */
673 case '/': /* Context address */
674 p = compile_re(p, &a->u.r);
675 if (p == NULL)
665 err(COMPILE, "unterminated regular expression");
676 errx(1, "%lu: %s: unterminated regular expression", linenum, fname);
666 a->type = AT_RE;
667 return (p);
668
669 case '$': /* Last line */
670 a->type = AT_LAST;
671 return (p + 1);
672 /* Line number */
673 case '0': case '1': case '2': case '3': case '4':
674 case '5': case '6': case '7': case '8': case '9':
675 a->type = AT_LINE;
676 a->u.l = strtol(p, &end, 10);
677 return (end);
678 default:
677 a->type = AT_RE;
678 return (p);
679
680 case '$': /* Last line */
681 a->type = AT_LAST;
682 return (p + 1);
683 /* Line number */
684 case '0': case '1': case '2': case '3': case '4':
685 case '5': case '6': case '7': case '8': case '9':
686 a->type = AT_LINE;
687 a->u.l = strtol(p, &end, 10);
688 return (end);
689 default:
679 err(COMPILE, "expected context address");
690 errx(1, "%lu: %s: expected context address", linenum, fname);
680 return (NULL);
681 }
682}
683
684/*
685 * duptoeol --
686 * Return a copy of all the characters up to \n or \0.
687 */

--- 6 unchanged lines hidden (view full) ---

694 int ws;
695 char *start;
696
697 ws = 0;
698 for (start = s; *s != '\0' && *s != '\n'; ++s)
699 ws = isspace((unsigned char)*s);
700 *s = '\0';
701 if (ws)
691 return (NULL);
692 }
693}
694
695/*
696 * duptoeol --
697 * Return a copy of all the characters up to \n or \0.
698 */

--- 6 unchanged lines hidden (view full) ---

705 int ws;
706 char *start;
707
708 ws = 0;
709 for (start = s; *s != '\0' && *s != '\n'; ++s)
710 ws = isspace((unsigned char)*s);
711 *s = '\0';
712 if (ws)
702 err(WARNING, "whitespace after %s", ctype);
713 warnx("%lu: %s: whitespace after %s", linenum, fname, ctype);
703 len = s - start + 1;
704 return (memmove(xmalloc(len), start, len));
705}
706
707/*
708 * Convert goto label names to addresses, and count a and r commands, in
709 * the given subset of the script. Free the memory used by labels in b
710 * and t commands (but not by :).

--- 14 unchanged lines hidden (view full) ---

725 case 'b':
726 case 't':
727 /* Resolve branch target. */
728 if (cp->t == NULL) {
729 cp->u.c = NULL;
730 break;
731 }
732 if ((cp->u.c = findlabel(cp->t)) == NULL)
714 len = s - start + 1;
715 return (memmove(xmalloc(len), start, len));
716}
717
718/*
719 * Convert goto label names to addresses, and count a and r commands, in
720 * the given subset of the script. Free the memory used by labels in b
721 * and t commands (but not by :).

--- 14 unchanged lines hidden (view full) ---

736 case 'b':
737 case 't':
738 /* Resolve branch target. */
739 if (cp->t == NULL) {
740 cp->u.c = NULL;
741 break;
742 }
743 if ((cp->u.c = findlabel(cp->t)) == NULL)
733 err(COMPILE2, "undefined label '%s'", cp->t);
744 errx(1, "%lu: %s: undefined label '%s'", linenum, fname, cp->t);
734 free(cp->t);
735 break;
736 case '{':
737 /* Do interior commands. */
738 fixuplabel(cp->u.c, cp->next);
739 break;
740 }
741}

--- 9 unchanged lines hidden (view full) ---

751 register u_char *p;
752 register u_int h, c;
753
754 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
755 h = (h << 5) + h + c;
756 lhp = &labels[h & LHMASK];
757 for (lh = *lhp; lh != NULL; lh = lh->lh_next)
758 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
745 free(cp->t);
746 break;
747 case '{':
748 /* Do interior commands. */
749 fixuplabel(cp->u.c, cp->next);
750 break;
751 }
752}

--- 9 unchanged lines hidden (view full) ---

762 register u_char *p;
763 register u_int h, c;
764
765 for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++)
766 h = (h << 5) + h + c;
767 lhp = &labels[h & LHMASK];
768 for (lh = *lhp; lh != NULL; lh = lh->lh_next)
769 if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0)
759 err(COMPILE2, "duplicate label '%s'", cp->t);
770 errx(1, "%lu: %s: duplicate label '%s'", linenum, fname, cp->t);
760 lh = xmalloc(sizeof *lh);
761 lh->lh_next = *lhp;
762 lh->lh_hash = h;
763 lh->lh_cmd = cp;
764 lh->lh_ref = 0;
765 *lhp = lh;
766}
767

--- 29 unchanged lines hidden (view full) ---

797{
798 register struct labhash *lh, *next;
799 register int i;
800
801 for (i = 0; i < LHSZ; i++) {
802 for (lh = labels[i]; lh != NULL; lh = next) {
803 next = lh->lh_next;
804 if (!lh->lh_ref)
771 lh = xmalloc(sizeof *lh);
772 lh->lh_next = *lhp;
773 lh->lh_hash = h;
774 lh->lh_cmd = cp;
775 lh->lh_ref = 0;
776 *lhp = lh;
777}
778

--- 29 unchanged lines hidden (view full) ---

808{
809 register struct labhash *lh, *next;
810 register int i;
811
812 for (i = 0; i < LHSZ; i++) {
813 for (lh = labels[i]; lh != NULL; lh = next) {
814 next = lh->lh_next;
815 if (!lh->lh_ref)
805 err(WARNING, "unused label '%s'",
806 lh->lh_cmd->t);
816 warnx("%lu: %s: unused label '%s'",
817 linenum, fname, lh->lh_cmd->t);
807 free(lh);
808 }
809 }
810}
818 free(lh);
819 }
820 }
821}