diff --git a/awk.h b/awk.h index bede1b0..7b8f564 100644 --- a/awk.h +++ b/awk.h @@ -60,6 +60,7 @@ extern bool safe; /* false => unsafe, true => safe */ #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */ extern int recsize; /* size of current record, orig RECSIZE */ +extern char EMPTY[]; extern char **FS; extern char **RS; extern char **ORS; @@ -78,8 +79,6 @@ extern int lineno; /* line number in awk program */ extern int errorflag; /* 1 if error has occurred */ extern bool donefld; /* true if record broken into fields */ extern bool donerec; /* true if record is valid (no fld has changed */ -extern char inputFS[]; /* FS at time of input, for field splitting */ - extern int dbg; extern const char *patbeg; /* beginning of pattern matched */ diff --git a/b.c b/b.c index 5671796..34dfb20 100644 --- a/b.c +++ b/b.c @@ -873,7 +873,7 @@ int (xisblank)(int c) #endif -struct charclass { +static const struct charclass { const char *cc_name; int cc_namelen; int (*cc_func)(int); @@ -1017,7 +1017,7 @@ int relex(void) /* lexical analyzer for reparse */ static uschar *buf = NULL; static int bufsz = 100; uschar *bp; - struct charclass *cc; + const struct charclass *cc; int i; int num, m; bool commafound, digitfound; diff --git a/lex.c b/lex.c index d729516..503e41a 100644 --- a/lex.c +++ b/lex.c @@ -43,7 +43,7 @@ typedef struct Keyword { int type; } Keyword; -Keyword keywords[] ={ /* keep sorted: binary searched */ +const Keyword keywords[] = { /* keep sorted: binary searched */ { "BEGIN", XBEGIN, XBEGIN }, { "END", XEND, XEND }, { "NF", VARNF, VARNF }, @@ -91,14 +91,14 @@ Keyword keywords[] ={ /* keep sorted: binary searched */ #define RET(x) { if(dbg)printf("lex %s\n", tokname(x)); return(x); } -int peek(void) +static int peek(void) { int c = input(); unput(c); return c; } -int gettok(char **pbuf, int *psz) /* get next input token */ +static int gettok(char **pbuf, int *psz) /* get next input token */ { int c, retc; char *buf = *pbuf; @@ -440,7 +440,7 @@ int string(void) } -int binsearch(char *w, Keyword *kp, int n) +static int binsearch(char *w, const Keyword *kp, int n) { int cond, low, mid, high; @@ -460,7 +460,7 @@ int binsearch(char *w, Keyword *kp, int n) int word(char *w) { - Keyword *kp; + const Keyword *kp; int c, n; n = binsearch(w, keywords, sizeof(keywords)/sizeof(keywords[0])); @@ -572,6 +572,8 @@ int input(void) /* get next lexical input character */ void unput(int c) /* put lexical character back on input */ { + if (c == '\n') + lineno--; if (yysptr >= yysbuf + sizeof(yysbuf)) FATAL("pushed back too much: %.20s...", yysbuf); *yysptr++ = c; diff --git a/lib.c b/lib.c index dbc09c3..d96b830 100644 --- a/lib.c +++ b/lib.c @@ -32,15 +32,17 @@ THIS SOFTWARE. #include "awk.h" #include "ytab.h" +char EMPTY[] = { '\0' }; FILE *infile = NULL; -char *file = ""; +char *file = EMPTY; char *record; int recsize = RECSIZE; char *fields; int fieldssize = RECSIZE; Cell **fldtab; /* pointers to Cells */ -char inputFS[100] = " "; +static size_t len_inputFS = 0; +static char *inputFS = NULL; /* FS at time of input, for field splitting */ #define MAXFLD 2 int nfields = MAXFLD; /* last allocated slot for $i */ @@ -52,8 +54,8 @@ int lastfld = 0; /* last used field */ int argno = 1; /* current input argument number */ extern Awkfloat *ARGC; -static Cell dollar0 = { OCELL, CFLD, NULL, "", 0.0, REC|STR|DONTFREE }; -static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE }; +static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, 0.0, REC|STR|DONTFREE }; +static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE }; void recinit(unsigned int n) { @@ -116,9 +118,17 @@ void initgetrec(void) */ void savefs(void) { - if (strlen(getsval(fsloc)) >= sizeof (inputFS)) + size_t len; + if ((len = strlen(getsval(fsloc))) < len_inputFS) { + strcpy(inputFS, *FS); /* for subsequent field splitting */ + return; + } + + len_inputFS = len + 1; + inputFS = realloc(inputFS, len_inputFS); + if (inputFS == NULL) FATAL("field separator %.10s... is too long", *FS); - strcpy(inputFS, *FS); + memcpy(inputFS, *FS, len_inputFS); } static bool firsttime = true; @@ -404,7 +414,7 @@ void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */ p = fldtab[i]; if (freeable(p)) xfree(p->sval); - p->sval = ""; + p->sval = EMPTY, p->tval = FLD | STR | DONTFREE; } } diff --git a/maketab.c b/maketab.c index c3ce5c6..9ac833e 100644 --- a/maketab.c +++ b/maketab.c @@ -122,8 +122,6 @@ int main(int argc, char *argv[]) printf("#include \n"); printf("#include \"awk.h\"\n"); printf("#include \"ytab.h\"\n\n"); - for (i = SIZE; --i >= 0; ) - names[i] = ""; if (argc != 2) { fprintf(stderr, "usage: maketab YTAB_H\n"); @@ -160,10 +158,8 @@ int main(int argc, char *argv[]) table[p->token-FIRSTTOKEN] = p->name; printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE); for (i=0; i