Lines 64-80
Link Here
|
64 |
int lh_ref; |
64 |
int lh_ref; |
65 |
} *labels[LHSZ]; |
65 |
} *labels[LHSZ]; |
66 |
|
66 |
|
67 |
static char *compile_addr(char *, struct s_addr *); |
67 |
static const char *compile_addr(const char *, struct s_addr *); |
68 |
static char *compile_ccl(char **, char *); |
68 |
static char *compile_ccl(const char **, char *); |
69 |
static char *compile_delimited(char *, char *, int); |
69 |
static const char *compile_delimited(const char *, char *, int); |
70 |
static char *compile_flags(char *, struct s_subst *); |
70 |
static const char *compile_flags(const char *, struct s_subst *); |
71 |
static regex_t *compile_re(char *, int); |
71 |
static regex_t *compile_re(const char *, int); |
72 |
static char *compile_subst(char *, struct s_subst *); |
72 |
static const char *compile_subst(const char *, struct s_subst *); |
73 |
static char *compile_text(void); |
73 |
static char *compile_text(void); |
74 |
static char *compile_tr(char *, struct s_tr **); |
74 |
static const char *compile_tr(const char *, struct s_tr **); |
75 |
static struct s_command |
75 |
static struct s_command |
76 |
**compile_stream(struct s_command **); |
76 |
**compile_stream(struct s_command **); |
77 |
static char *duptoeol(char *, const char *); |
77 |
static char *duptoeol(const char *, const char *); |
78 |
static void enterlabel(struct s_command *); |
78 |
static void enterlabel(struct s_command *); |
79 |
static struct s_command |
79 |
static struct s_command |
80 |
*findlabel(char *); |
80 |
*findlabel(char *); |
Lines 153-160
Link Here
|
153 |
static struct s_command ** |
153 |
static struct s_command ** |
154 |
compile_stream(struct s_command **link) |
154 |
compile_stream(struct s_command **link) |
155 |
{ |
155 |
{ |
156 |
char *p; |
156 |
const char *p; |
157 |
static char lbuf[_POSIX2_LINE_MAX + 1]; /* To save stack */ |
|
|
158 |
struct s_command *cmd, *cmd2, *stack; |
157 |
struct s_command *cmd, *cmd2, *stack; |
159 |
struct s_format *fp; |
158 |
struct s_format *fp; |
160 |
char re[_POSIX2_LINE_MAX + 1]; |
159 |
char re[_POSIX2_LINE_MAX + 1]; |
Lines 162-168
Link Here
|
162 |
|
161 |
|
163 |
stack = 0; |
162 |
stack = 0; |
164 |
for (;;) { |
163 |
for (;;) { |
165 |
if ((p = cu_fgets(lbuf, sizeof(lbuf), NULL)) == NULL) { |
164 |
if ((p = cu_fgets(NULL)) == NULL) { |
166 |
if (stack != 0) |
165 |
if (stack != 0) |
167 |
errx(1, "%lu: %s: unexpected EOF (pending }'s)", |
166 |
errx(1, "%lu: %s: unexpected EOF (pending }'s)", |
168 |
linenum, fname); |
167 |
linenum, fname); |
Lines 372-379
Link Here
|
372 |
* in the case of a non-terminated string. The character array d is filled |
371 |
* in the case of a non-terminated string. The character array d is filled |
373 |
* with the processed string. |
372 |
* with the processed string. |
374 |
*/ |
373 |
*/ |
375 |
static char * |
374 |
static const char * |
376 |
compile_delimited(char *p, char *d, int is_tr) |
375 |
compile_delimited(const char *p, char *d, int is_tr) |
377 |
{ |
376 |
{ |
378 |
char c; |
377 |
char c; |
379 |
|
378 |
|
Lines 416-425
Link Here
|
416 |
|
415 |
|
417 |
/* compile_ccl: expand a POSIX character class */ |
416 |
/* compile_ccl: expand a POSIX character class */ |
418 |
static char * |
417 |
static char * |
419 |
compile_ccl(char **sp, char *t) |
418 |
compile_ccl(const char **sp, char *t) |
420 |
{ |
419 |
{ |
421 |
int c, d; |
420 |
int c, d; |
422 |
char *s = *sp; |
421 |
const char *s = *sp; |
423 |
|
422 |
|
424 |
*t++ = *s++; |
423 |
*t++ = *s++; |
425 |
if (*s == '^') |
424 |
if (*s == '^') |
Lines 442-448
Link Here
|
442 |
* Cflags are passed to regcomp. |
441 |
* Cflags are passed to regcomp. |
443 |
*/ |
442 |
*/ |
444 |
static regex_t * |
443 |
static regex_t * |
445 |
compile_re(char *re, int case_insensitive) |
444 |
compile_re(const char *re, int case_insensitive) |
446 |
{ |
445 |
{ |
447 |
regex_t *rep; |
446 |
regex_t *rep; |
448 |
int eval, flags; |
447 |
int eval, flags; |
Lines 466-475
Link Here
|
466 |
* point to a saved copy of it. Nsub is the number of parenthesized regular |
465 |
* point to a saved copy of it. Nsub is the number of parenthesized regular |
467 |
* expressions. |
466 |
* expressions. |
468 |
*/ |
467 |
*/ |
469 |
static char * |
468 |
static const char * |
470 |
compile_subst(char *p, struct s_subst *s) |
469 |
compile_subst(const char *p, struct s_subst *s) |
471 |
{ |
470 |
{ |
472 |
static char lbuf[_POSIX2_LINE_MAX + 1]; |
|
|
473 |
int asize, size; |
471 |
int asize, size; |
474 |
u_char ref; |
472 |
u_char ref; |
475 |
char c, *text, *op, *sp; |
473 |
char c, *text, *op, *sp; |
Lines 523-530
Link Here
|
523 |
*sp++ = '\\'; |
521 |
*sp++ = '\\'; |
524 |
} else if (*p == c) { |
522 |
} else if (*p == c) { |
525 |
if (*++p == '\0' && more) { |
523 |
if (*++p == '\0' && more) { |
526 |
if (cu_fgets(lbuf, sizeof(lbuf), &more)) |
524 |
const char *nextp; |
527 |
p = lbuf; |
525 |
|
|
|
526 |
nextp = cu_fgets(&more); |
527 |
if (nextp != NULL) |
528 |
p = nextp; |
528 |
} |
529 |
} |
529 |
*sp++ = '\0'; |
530 |
*sp++ = '\0'; |
530 |
size += sp - op; |
531 |
size += sp - op; |
Lines 544-550
Link Here
|
544 |
if ((text = realloc(text, asize)) == NULL) |
545 |
if ((text = realloc(text, asize)) == NULL) |
545 |
err(1, "realloc"); |
546 |
err(1, "realloc"); |
546 |
} |
547 |
} |
547 |
} while (cu_fgets(p = lbuf, sizeof(lbuf), &more)); |
548 |
} while ((p = cu_fgets(&more))); |
548 |
errx(1, "%lu: %s: unterminated substitute in regular expression", |
549 |
errx(1, "%lu: %s: unterminated substitute in regular expression", |
549 |
linenum, fname); |
550 |
linenum, fname); |
550 |
/* NOTREACHED */ |
551 |
/* NOTREACHED */ |
Lines 553-560
Link Here
|
553 |
/* |
554 |
/* |
554 |
* Compile the flags of the s command |
555 |
* Compile the flags of the s command |
555 |
*/ |
556 |
*/ |
556 |
static char * |
557 |
static const char * |
557 |
compile_flags(char *p, struct s_subst *s) |
558 |
compile_flags(const char *p, struct s_subst *s) |
558 |
{ |
559 |
{ |
559 |
int gn; /* True if we have seen g or n */ |
560 |
int gn; /* True if we have seen g or n */ |
560 |
unsigned long nval; |
561 |
unsigned long nval; |
Lines 594-605
Link Here
|
594 |
"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); |
595 |
"%lu: %s: more than one number or 'g' in substitute flags", linenum, fname); |
595 |
gn = 1; |
596 |
gn = 1; |
596 |
errno = 0; |
597 |
errno = 0; |
597 |
nval = strtol(p, &p, 10); |
598 |
nval = strtol(p, &q, 10); |
598 |
if (errno == ERANGE || nval > INT_MAX) |
599 |
if (errno == ERANGE || nval > INT_MAX) |
599 |
errx(1, |
600 |
errx(1, |
600 |
"%lu: %s: overflow in the 'N' substitute flag", linenum, fname); |
601 |
"%lu: %s: overflow in the 'N' substitute flag", linenum, fname); |
601 |
s->n = nval; |
602 |
s->n = nval; |
602 |
p--; |
603 |
p = q - 1; |
603 |
break; |
604 |
break; |
604 |
case 'w': |
605 |
case 'w': |
605 |
p++; |
606 |
p++; |
Lines 637-644
Link Here
|
637 |
/* |
638 |
/* |
638 |
* Compile a translation set of strings into a lookup table. |
639 |
* Compile a translation set of strings into a lookup table. |
639 |
*/ |
640 |
*/ |
640 |
static char * |
641 |
static const char * |
641 |
compile_tr(char *p, struct s_tr **py) |
642 |
compile_tr(const char *p, struct s_tr **py) |
642 |
{ |
643 |
{ |
643 |
struct s_tr *y; |
644 |
struct s_tr *y; |
644 |
int i; |
645 |
int i; |
Lines 733-748
Link Here
|
733 |
compile_text(void) |
734 |
compile_text(void) |
734 |
{ |
735 |
{ |
735 |
int asize, esc_nl, size; |
736 |
int asize, esc_nl, size; |
736 |
char *text, *p, *op, *s; |
737 |
char *text, *s; |
737 |
char lbuf[_POSIX2_LINE_MAX + 1]; |
738 |
const char *p, *op; |
738 |
|
739 |
|
739 |
asize = 2 * _POSIX2_LINE_MAX + 1; |
740 |
asize = 2 * _POSIX2_LINE_MAX + 1; |
740 |
if ((text = malloc(asize)) == NULL) |
741 |
if ((text = malloc(asize)) == NULL) |
741 |
err(1, "malloc"); |
742 |
err(1, "malloc"); |
742 |
size = 0; |
743 |
size = 0; |
743 |
while (cu_fgets(lbuf, sizeof(lbuf), NULL)) { |
744 |
while ((p = cu_fgets(NULL))) { |
744 |
op = s = text + size; |
745 |
op = s = text + size; |
745 |
p = lbuf; |
|
|
746 |
EATSPACE(); |
746 |
EATSPACE(); |
747 |
for (esc_nl = 0; *p != '\0'; p++) { |
747 |
for (esc_nl = 0; *p != '\0'; p++) { |
748 |
if (*p == '\\' && p[1] != '\0' && *++p == '\n') |
748 |
if (*p == '\\' && p[1] != '\0' && *++p == '\n') |
Lines 761-769
Link Here
|
761 |
} |
761 |
} |
762 |
} |
762 |
} |
763 |
text[size] = '\0'; |
763 |
text[size] = '\0'; |
764 |
if ((p = realloc(text, size + 1)) == NULL) |
764 |
if ((text = realloc(text, size + 1)) == NULL) |
765 |
err(1, "realloc"); |
765 |
err(1, "realloc"); |
766 |
return (p); |
766 |
return (text); |
767 |
} |
767 |
} |
768 |
|
768 |
|
769 |
/* |
769 |
/* |
Lines 770-777
Link Here
|
770 |
* Get an address and return a pointer to the first character after |
770 |
* Get an address and return a pointer to the first character after |
771 |
* it. Fill the structure pointed to according to the address. |
771 |
* it. Fill the structure pointed to according to the address. |
772 |
*/ |
772 |
*/ |
773 |
static char * |
773 |
static const char * |
774 |
compile_addr(char *p, struct s_addr *a) |
774 |
compile_addr(const char *p, struct s_addr *a) |
775 |
{ |
775 |
{ |
776 |
char *end, re[_POSIX2_LINE_MAX + 1]; |
776 |
char *end, re[_POSIX2_LINE_MAX + 1]; |
777 |
int icase; |
777 |
int icase; |
Lines 825-846
Link Here
|
825 |
* Return a copy of all the characters up to \n or \0. |
825 |
* Return a copy of all the characters up to \n or \0. |
826 |
*/ |
826 |
*/ |
827 |
static char * |
827 |
static char * |
828 |
duptoeol(char *s, const char *ctype) |
828 |
duptoeol(const char *s, const char *ctype) |
829 |
{ |
829 |
{ |
830 |
size_t len; |
830 |
size_t len; |
831 |
int ws; |
831 |
int ws; |
832 |
char *p, *start; |
832 |
char *p; |
|
|
833 |
const char *start; |
833 |
|
834 |
|
834 |
ws = 0; |
835 |
ws = 0; |
835 |
for (start = s; *s != '\0' && *s != '\n'; ++s) |
836 |
for (start = s; *s != '\0' && *s != '\n'; ++s) |
836 |
ws = isspace((unsigned char)*s); |
837 |
ws = isspace((unsigned char)*s); |
837 |
*s = '\0'; |
|
|
838 |
if (ws) |
838 |
if (ws) |
839 |
warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); |
839 |
warnx("%lu: %s: whitespace after %s", linenum, fname, ctype); |
840 |
len = s - start + 1; |
840 |
len = s - start; |
841 |
if ((p = malloc(len)) == NULL) |
841 |
if ((p = malloc(len + 1)) == NULL) |
842 |
err(1, "malloc"); |
842 |
err(1, "malloc"); |
843 |
return (memmove(p, start, len)); |
843 |
memmove(p, start, len); |
|
|
844 |
p[len] = '\0'; |
845 |
return p; |
844 |
} |
846 |
} |
845 |
|
847 |
|
846 |
/* |
848 |
/* |