compile.c (148692) | compile.c (171206) |
---|---|
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 * --- 18 unchanged lines hidden (view full) --- 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> | 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 * --- 18 unchanged lines hidden (view full) --- 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34#include <sys/cdefs.h> |
35__FBSDID("$FreeBSD: head/usr.bin/sed/compile.c 148692 2005-08-04 10:05:12Z dds $"); | 35__FBSDID("$FreeBSD: head/usr.bin/sed/compile.c 171206 2007-07-04 16:42:41Z ssouhlal $"); |
36 37#ifndef lint 38static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; 39#endif 40 41#include <sys/types.h> 42#include <sys/stat.h> 43 --- 19 unchanged lines hidden (view full) --- 63 struct s_command *lh_cmd; 64 int lh_ref; 65} *labels[LHSZ]; 66 67static char *compile_addr(char *, struct s_addr *); 68static char *compile_ccl(char **, char *); 69static char *compile_delimited(char *, char *); 70static char *compile_flags(char *, struct s_subst *); | 36 37#ifndef lint 38static const char sccsid[] = "@(#)compile.c 8.1 (Berkeley) 6/6/93"; 39#endif 40 41#include <sys/types.h> 42#include <sys/stat.h> 43 --- 19 unchanged lines hidden (view full) --- 63 struct s_command *lh_cmd; 64 int lh_ref; 65} *labels[LHSZ]; 66 67static char *compile_addr(char *, struct s_addr *); 68static char *compile_ccl(char **, char *); 69static char *compile_delimited(char *, char *); 70static char *compile_flags(char *, struct s_subst *); |
71static char *compile_re(char *, regex_t **); | 71static regex_t *compile_re(char *, int); |
72static char *compile_subst(char *, struct s_subst *); 73static char *compile_text(void); 74static char *compile_tr(char *, struct s_tr **); 75static struct s_command 76 **compile_stream(struct s_command **); 77static char *duptoeol(char *, const char *); 78static void enterlabel(struct s_command *); 79static struct s_command --- 72 unchanged lines hidden (view full) --- 152 153static struct s_command ** 154compile_stream(struct s_command **link) 155{ 156 char *p; 157 static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 158 struct s_command *cmd, *cmd2, *stack; 159 struct s_format *fp; | 72static char *compile_subst(char *, struct s_subst *); 73static char *compile_text(void); 74static char *compile_tr(char *, struct s_tr **); 75static struct s_command 76 **compile_stream(struct s_command **); 77static char *duptoeol(char *, const char *); 78static void enterlabel(struct s_command *); 79static struct s_command --- 72 unchanged lines hidden (view full) --- 152 153static struct s_command ** 154compile_stream(struct s_command **link) 155{ 156 char *p; 157 static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ 158 struct s_command *cmd, *cmd2, *stack; 159 struct s_format *fp; |
160 char re[_POSIX2_LINE_MAX + 1]; |
|
160 int naddr; /* Number of addresses */ 161 162 stack = 0; 163 for (;;) { 164 if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { 165 if (stack != 0) 166 errx(1, "%lu: %s: unexpected EOF (pending }'s)", 167 linenum, fname); --- 146 unchanged lines hidden (view full) --- 314 case SUBST: /* s */ 315 p++; 316 if (*p == '\0' || *p == '\\') 317 errx(1, 318"%lu: %s: substitute pattern can not be delimited by newline or backslash", 319 linenum, fname); 320 if ((cmd->u.s = malloc(sizeof(struct s_subst))) == NULL) 321 err(1, "malloc"); | 161 int naddr; /* Number of addresses */ 162 163 stack = 0; 164 for (;;) { 165 if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { 166 if (stack != 0) 167 errx(1, "%lu: %s: unexpected EOF (pending }'s)", 168 linenum, fname); --- 146 unchanged lines hidden (view full) --- 315 case SUBST: /* s */ 316 p++; 317 if (*p == '\0' || *p == '\\') 318 errx(1, 319"%lu: %s: substitute pattern can not be delimited by newline or backslash", 320 linenum, fname); 321 if ((cmd->u.s = malloc(sizeof(struct s_subst))) == NULL) 322 err(1, "malloc"); |
322 p = compile_re(p, &cmd->u.s->re); | 323 p = compile_delimited(p, re); |
323 if (p == NULL) 324 errx(1, 325 "%lu: %s: unterminated substitute pattern", linenum, fname); | 324 if (p == NULL) 325 errx(1, 326 "%lu: %s: unterminated substitute pattern", linenum, fname); |
327 if (*re == '\0') 328 cmd->u.s->re = NULL; 329 else 330 cmd->u.s->re = compile_re(re, cmd->u.s->icase); |
|
326 --p; 327 p = compile_subst(p, cmd->u.s); 328 p = compile_flags(p, cmd->u.s); 329 EATSPACE(); 330 if (*p == ';') { 331 p++; 332 link = &cmd->next; 333 goto semicolon; --- 83 unchanged lines hidden (view full) --- 417 if ((c = *s) == '\0') 418 return NULL; 419 } else if (*s == '\\' && s[1] == 'n') 420 *t = '\n', s++; 421 return (*s == ']') ? *sp = ++s, ++t : NULL; 422} 423 424/* | 331 --p; 332 p = compile_subst(p, cmd->u.s); 333 p = compile_flags(p, cmd->u.s); 334 EATSPACE(); 335 if (*p == ';') { 336 p++; 337 link = &cmd->next; 338 goto semicolon; --- 83 unchanged lines hidden (view full) --- 422 if ((c = *s) == '\0') 423 return NULL; 424 } else if (*s == '\\' && s[1] == 'n') 425 *t = '\n', s++; 426 return (*s == ']') ? *sp = ++s, ++t : NULL; 427} 428 429/* |
425 * Get a regular expression. P points to the delimiter of the regular 426 * expression; repp points to the address of a regexp pointer. Newline 427 * and delimiter escapes are processed; other escapes are ignored. 428 * Returns a pointer to the first character after the final delimiter 429 * or NULL in the case of a non terminated regular expression. The regexp 430 * pointer is set to the compiled regular expression. | 430 * Compiles the regular expression in RE and returns a pointer to the compiled 431 * regular expression. |
431 * Cflags are passed to regcomp. 432 */ | 432 * Cflags are passed to regcomp. 433 */ |
433static char * 434compile_re(char *p, regex_t **repp) | 434static regex_t * 435compile_re(char *re, int case_insensitive) |
435{ | 436{ |
436 int eval; 437 char re[_POSIX2_LINE_MAX + 1]; | 437 regex_t *rep; 438 int eval, flags; |
438 | 439 |
439 p = compile_delimited(p, re); 440 if (p && strlen(re) == 0) { 441 *repp = NULL; 442 return (p); 443 } 444 if ((*repp = malloc(sizeof(regex_t))) == NULL) | 440 441 flags = rflags; 442 if (case_insensitive) 443 flags |= REG_ICASE; 444 if ((rep = malloc(sizeof(regex_t))) == NULL) |
445 err(1, "malloc"); | 445 err(1, "malloc"); |
446 if (p && (eval = regcomp(*repp, re, rflags)) != 0) | 446 if (eval = regcomp(rep, re, flags) != 0) |
447 errx(1, "%lu: %s: RE error: %s", | 447 errx(1, "%lu: %s: RE error: %s", |
448 linenum, fname, strregerror(eval, *repp)); 449 if (maxnsub < (*repp)->re_nsub) 450 maxnsub = (*repp)->re_nsub; 451 return (p); | 448 linenum, fname, strregerror(eval, rep)); 449 if (maxnsub < rep->re_nsub) 450 maxnsub = rep->re_nsub; 451 return (rep); |
452} 453 454/* 455 * Compile the substitution string of a regular expression and set res to 456 * point to a saved copy of it. Nsub is the number of parenthesized regular 457 * expressions. 458 */ 459static char * --- 89 unchanged lines hidden (view full) --- 549 int gn; /* True if we have seen g or n */ 550 unsigned long nval; 551 char wfile[_POSIX2_LINE_MAX + 1], *q; 552 553 s->n = 1; /* Default */ 554 s->p = 0; 555 s->wfile = NULL; 556 s->wfd = -1; | 452} 453 454/* 455 * Compile the substitution string of a regular expression and set res to 456 * point to a saved copy of it. Nsub is the number of parenthesized regular 457 * expressions. 458 */ 459static char * --- 89 unchanged lines hidden (view full) --- 549 int gn; /* True if we have seen g or n */ 550 unsigned long nval; 551 char wfile[_POSIX2_LINE_MAX + 1], *q; 552 553 s->n = 1; /* Default */ 554 s->p = 0; 555 s->wfile = NULL; 556 s->wfd = -1; |
557 s->icase = 0; |
|
557 for (gn = 0;;) { 558 EATSPACE(); /* EXTENSION */ 559 switch (*p) { 560 case 'g': 561 if (gn) 562 errx(1, 563"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 564 gn = 1; 565 s->n = 0; 566 break; 567 case '\0': 568 case '\n': 569 case ';': 570 return (p); 571 case 'p': 572 s->p = 1; 573 break; | 558 for (gn = 0;;) { 559 EATSPACE(); /* EXTENSION */ 560 switch (*p) { 561 case 'g': 562 if (gn) 563 errx(1, 564"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 565 gn = 1; 566 s->n = 0; 567 break; 568 case '\0': 569 case '\n': 570 case ';': 571 return (p); 572 case 'p': 573 s->p = 1; 574 break; |
575 case 'I': 576 s->icase = 1; 577 break; |
|
574 case '1': case '2': case '3': 575 case '4': case '5': case '6': 576 case '7': case '8': case '9': 577 if (gn) 578 errx(1, 579"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 580 gn = 1; 581 errno = 0; --- 171 unchanged lines hidden (view full) --- 753 754/* 755 * Get an address and return a pointer to the first character after 756 * it. Fill the structure pointed to according to the address. 757 */ 758static char * 759compile_addr(char *p, struct s_addr *a) 760{ | 578 case '1': case '2': case '3': 579 case '4': case '5': case '6': 580 case '7': case '8': case '9': 581 if (gn) 582 errx(1, 583"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); 584 gn = 1; 585 errno = 0; --- 171 unchanged lines hidden (view full) --- 757 758/* 759 * Get an address and return a pointer to the first character after 760 * it. Fill the structure pointed to according to the address. 761 */ 762static char * 763compile_addr(char *p, struct s_addr *a) 764{ |
761 char *end; | 765 char *end, re[_POSIX2_LINE_MAX + 1]; 766 int icase; |
762 | 767 |
768 icase = 0; 769 |
|
763 switch (*p) { 764 case '\\': /* Context address */ 765 ++p; 766 /* FALLTHROUGH */ 767 case '/': /* Context address */ | 770 switch (*p) { 771 case '\\': /* Context address */ 772 ++p; 773 /* FALLTHROUGH */ 774 case '/': /* Context address */ |
768 p = compile_re(p, &a->u.r); | 775 p = compile_delimited(p, re); |
769 if (p == NULL) 770 errx(1, "%lu: %s: unterminated regular expression", linenum, fname); | 776 if (p == NULL) 777 errx(1, "%lu: %s: unterminated regular expression", linenum, fname); |
778 /* Check for case insensitive regexp flag */ 779 if (*p == 'I') { 780 icase = 1; 781 p++; 782 } 783 if (*re == '\0') 784 a->u.r = NULL; 785 else 786 a->u.r = compile_re(re, icase); |
|
771 a->type = AT_RE; 772 return (p); 773 774 case '$': /* Last line */ 775 a->type = AT_LAST; 776 return (p + 1); 777 /* Line number */ 778 case '0': case '1': case '2': case '3': case '4': --- 135 unchanged lines hidden --- | 787 a->type = AT_RE; 788 return (p); 789 790 case '$': /* Last line */ 791 a->type = AT_LAST; 792 return (p + 1); 793 /* Line number */ 794 case '0': case '1': case '2': case '3': case '4': --- 135 unchanged lines hidden --- |