* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
* This code is derived from software contributed to Berkeley by
* Diomidis Spinellis of Imperial College, University of London.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
static char sccsid
[] = "@(#)compile.c 8.2 (Berkeley) 4/28/95";
#define LHMASK (LHSZ - 1)
struct s_command
*lh_cmd
;
static char *compile_addr
__P((char *, struct s_addr
*));
static char *compile_delimited
__P((char *, char *));
static char *compile_flags
__P((char *, struct s_subst
*));
static char *compile_re
__P((char *, regex_t
**));
static char *compile_subst
__P((char *, struct s_subst
*));
static char *compile_text
__P((void));
static char *compile_tr
__P((char *, char **));
**compile_stream
__P((char *, struct s_command
**, char *));
static char *duptoeol
__P((char *, char *));
static void enterlabel
__P((struct s_command
*));
*findlabel
__P((char *));
static void fixuplabel
__P((struct s_command
*, struct s_command
*));
static void uselabel
__P((void));
* Command specification. This is used to drive the command parser.
char code
; /* Command code */
int naddr
; /* Number of address args */
enum e_args args
; /* Argument type */
static struct s_format cmd_fmts
[] = {
/* The compiled program. */
* Compile the program into prog.
*compile_stream(NULL
, &prog
, NULL
) = NULL
;
appends
= xmalloc(sizeof(struct s_appends
) * appendnum
);
match
= xmalloc((maxnsub
+ 1) * sizeof(regmatch_t
));
#define EATSPACE() do { \
while (*p && isascii(*p) && isspace(*p)) \
static struct s_command
**
compile_stream(terminator
, link
, p
)
static char lbuf
[_POSIX2_LINE_MAX
+ 1]; /* To save stack */
struct s_command
*cmd
, *cmd2
;
int naddr
; /* Number of addresses */
if ((p
= cu_fgets(lbuf
, sizeof(lbuf
))) == NULL
) {
err(COMPILE
, "unexpected EOF (pending }'s)");
if (p
&& (*p
== '#' || *p
== '\0'))
err(COMPILE
, "unexpected }");
*link
= cmd
= xmalloc(sizeof(struct s_command
));
cmd
->nonsel
= cmd
->inrange
= 0;
/* First parse the addresses */
/* Valid characters to start an address */
#define addrchar(c) (strchr("0123456789/\\$", (c)))
cmd
->a1
= xmalloc(sizeof(struct s_addr
));
p
= compile_addr(p
, cmd
->a1
);
EATSPACE(); /* EXTENSION */
EATSPACE(); /* EXTENSION */
cmd
->a2
= xmalloc(sizeof(struct s_addr
));
p
= compile_addr(p
, cmd
->a2
);
nonsel
: /* Now parse the command */
err(COMPILE
, "command expected");
for (fp
= cmd_fmts
; fp
->code
; fp
++)
err(COMPILE
, "invalid command code %c", *p
);
"command %c expects up to %d address(es), found %d", *p
, fp
->naddr
, naddr
);
cmd
->nonsel
= ! cmd
->nonsel
;
cmd2
= xmalloc(sizeof(struct s_command
));
*compile_stream("}", &cmd
->u
.c
, p
) = cmd2
;
* Short-circuit command processing, since end of
* group is really just a noop.
case EMPTY
: /* d D g G h H l n N p P q x = \0 */
"extra characters at the end of %c command", cmd
->code
);
"command %c expects \\ followed by text", cmd
->code
);
"extra characters after \\ at the end of %c command", cmd
->code
);
err(COMPILE
, "filename expected");
cmd
->t
= duptoeol(p
, "w command");
else if ((cmd
->u
.fd
= open(p
,
O_WRONLY
|O_APPEND
|O_CREAT
|O_TRUNC
,
err(FATAL
, "%s: %s\n", p
, strerror(errno
));
err(COMPILE
, "filename expected");
cmd
->t
= duptoeol(p
, "read command");
cmd
->t
= duptoeol(p
, "branch");
cmd
->t
= duptoeol(p
, "label");
err(COMPILE
, "empty label");
if (*p
== '\0' || *p
== '\\')
"substitute pattern can not be delimited by newline or backslash");
cmd
->u
.s
= xmalloc(sizeof(struct s_subst
));
p
= compile_re(p
, &cmd
->u
.s
->re
);
err(COMPILE
, "unterminated substitute pattern");
p
= compile_subst(p
, cmd
->u
.s
);
p
= compile_flags(p
, cmd
->u
.s
);
p
= compile_tr(p
, (char **)&cmd
->u
.y
);
"extra text at the end of a transform command");
* Get a delimited string. P points to the delimeter of the string; d points
* to a buffer area. Newline and delimiter escapes are processed; other
* Returns a pointer to the first character after the final delimiter or NULL
* in the case of a non-terminated string. The character array d is filled
* with the processed string.
err(COMPILE
, "\\ can not be used as a string delimiter");
err(COMPILE
, "newline can not be used as a string delimiter");
if (*p
== '\\' && p
[1] == c
)
else if (*p
== '\\' && p
[1] == 'n') {
} else if (*p
== '\\' && p
[1] == '\\')
* Get a regular expression. P points to the delimiter of the regular
* expression; repp points to the address of a regexp pointer. Newline
* and delimiter escapes are processed; other escapes are ignored.
* Returns a pointer to the first character after the final delimiter
* or NULL in the case of a non terminated regular expression. The regexp
* pointer is set to the compiled regular expression.
* Cflags are passed to regcomp.
char re
[_POSIX2_LINE_MAX
+ 1];
p
= compile_delimited(p
, re
);
if (p
&& strlen(re
) == 0) {
*repp
= xmalloc(sizeof(regex_t
));
if (p
&& (eval
= regcomp(*repp
, re
, 0)) != 0)
err(COMPILE
, "RE error: %s", strregerror(eval
, *repp
));
if (maxnsub
< (*repp
)->re_nsub
)
maxnsub
= (*repp
)->re_nsub
;
* Compile the substitution string of a regular expression and set res to
* point to a saved copy of it. Nsub is the number of parenthesized regular
static char lbuf
[_POSIX2_LINE_MAX
+ 1];
c
= *p
++; /* Terminator character */
asize
= 2 * _POSIX2_LINE_MAX
+ 1;
if (strchr("123456789", *p
) != NULL
) {
"\\%c not defined in the RE", *p
);
} else if (*p
== '&' || *p
== '\\')
s
->new = xrealloc(text
, size
);
"unescaped newline inside substitute pattern");
if (asize
- size
< _POSIX2_LINE_MAX
+ 1) {
} while (cu_fgets(p
= lbuf
, sizeof(lbuf
)));
err(COMPILE
, "unterminated substitute in regular expression");
* Compile the flags of the s command
int gn
; /* True if we have seen g or n */
char wfile
[_POSIX2_LINE_MAX
+ 1], *q
;
EATSPACE(); /* EXTENSION */
"more than one number or 'g' in substitute flags");
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8': case '9':
"more than one number or 'g' in substitute flags");
/* XXX Check for overflow */
s
->n
= (int)strtol(p
, &p
, 10);
err(WARNING
, "space missing before w wfile");
err(COMPILE
, "no wfile specified");
s
->wfile
= strdup(wfile
);
if (!aflag
&& (s
->wfd
= open(wfile
,
O_WRONLY
|O_APPEND
|O_CREAT
|O_TRUNC
,
err(FATAL
, "%s: %s\n", wfile
, strerror(errno
));
"bad flag in substitute command: '%c'", *p
);
* Compile a translation set of strings into a lookup table.
char old
[_POSIX2_LINE_MAX
+ 1];
char new[_POSIX2_LINE_MAX
+ 1];
if (*p
== '\0' || *p
== '\\')
"transform pattern can not be delimited by newline or backslash");
p
= compile_delimited(p
, old
);
err(COMPILE
, "unterminated transform source string");
p
= compile_delimited(--p
, new);
err(COMPILE
, "unterminated transform target string");
if (strlen(new) != strlen(old
)) {
err(COMPILE
, "transform strings are not the same length");
/* We assume characters are 8 bits */
for (i
= 0; i
<= UCHAR_MAX
; i
++)
for (op
= old
, np
= new; *op
; op
++, np
++)
* Compile the text following an a or i command.
char lbuf
[_POSIX2_LINE_MAX
+ 1];
asize
= 2 * _POSIX2_LINE_MAX
+ 1;
while (cu_fgets(lbuf
, sizeof(lbuf
))) {
if (asize
- size
< _POSIX2_LINE_MAX
+ 1) {
return (xrealloc(text
, size
+ 1));
* Get an address and return a pointer to the first character after
* it. Fill the structure pointed to according to the address.
case '\\': /* Context address */
case '/': /* Context address */
p
= compile_re(p
, &a
->u
.r
);
err(COMPILE
, "unterminated regular expression");
case '$': /* Last line */
case '0': case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
a
->u
.l
= strtol(p
, &end
, 10);
err(COMPILE
, "expected context address");
* Return a copy of all the characters up to \n or \0.
for (start
= s
; *s
!= '\0' && *s
!= '\n'; ++s
)
err(WARNING
, "whitespace after %s", ctype
);
return (memmove(xmalloc(len
), start
, len
));
* Convert goto label names to addresses, and count a and r commands, in
* the given subset of the script. Free the memory used by labels in b
* and t commands (but not by :).
struct s_command
*cp
, *end
;
for (; cp
!= end
; cp
= cp
->next
)
/* Resolve branch target. */
if ((cp
->u
.c
= findlabel(cp
->t
)) == NULL
)
err(COMPILE2
, "undefined label '%s'", cp
->t
);
/* Do interior commands. */
fixuplabel(cp
->u
.c
, cp
->next
);
* Associate the given command label for later lookup.
register struct labhash
**lhp
, *lh
;
for (h
= 0, p
= (u_char
*)cp
->t
; (c
= *p
) != 0; p
++)
lhp
= &labels
[h
& LHMASK
];
for (lh
= *lhp
; lh
!= NULL
; lh
= lh
->lh_next
)
if (lh
->lh_hash
== h
&& strcmp(cp
->t
, lh
->lh_cmd
->t
) == 0)
err(COMPILE2
, "duplicate label '%s'", cp
->t
);
lh
= xmalloc(sizeof *lh
);
* Find the label contained in the command l in the command linked
* list cp. L is excluded from the search. Return NULL if not found.
static struct s_command
*
register struct labhash
*lh
;
for (h
= 0, p
= (u_char
*)name
; (c
= *p
) != 0; p
++)
for (lh
= labels
[h
& LHMASK
]; lh
!= NULL
; lh
= lh
->lh_next
) {
if (lh
->lh_hash
== h
&& strcmp(name
, lh
->lh_cmd
->t
) == 0) {
* Warn about any unused labels. As a side effect, release the label hash
register struct labhash
*lh
, *next
;
for (i
= 0; i
< LHSZ
; i
++) {
for (lh
= labels
[i
]; lh
!= NULL
; lh
= next
) {
err(WARNING
, "unused label '%s'",