Small fixes (#68)
* sprinkle const, static * account for lineno in unput * Add an EMPTY string that is used when a non-const empty string is needed. * make inputFS static and dynamically allocated * Simplify and in the process avoid -Wwritable-strings * make fs const to avoid -Wwritable-strings
This commit is contained in:
parent
5a18f63b8d
commit
6a8770929d
3
awk.h
3
awk.h
@ -60,6 +60,7 @@ extern bool safe; /* false => unsafe, true => safe */
|
|||||||
#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
|
#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
|
||||||
extern int recsize; /* size of current record, orig RECSIZE */
|
extern int recsize; /* size of current record, orig RECSIZE */
|
||||||
|
|
||||||
|
extern char EMPTY[];
|
||||||
extern char **FS;
|
extern char **FS;
|
||||||
extern char **RS;
|
extern char **RS;
|
||||||
extern char **ORS;
|
extern char **ORS;
|
||||||
@ -78,8 +79,6 @@ extern int lineno; /* line number in awk program */
|
|||||||
extern int errorflag; /* 1 if error has occurred */
|
extern int errorflag; /* 1 if error has occurred */
|
||||||
extern bool donefld; /* true if record broken into fields */
|
extern bool donefld; /* true if record broken into fields */
|
||||||
extern bool donerec; /* true if record is valid (no fld has changed */
|
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 int dbg;
|
||||||
|
|
||||||
extern const char *patbeg; /* beginning of pattern matched */
|
extern const char *patbeg; /* beginning of pattern matched */
|
||||||
|
4
b.c
4
b.c
@ -873,7 +873,7 @@ int (xisblank)(int c)
|
|||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
struct charclass {
|
static const struct charclass {
|
||||||
const char *cc_name;
|
const char *cc_name;
|
||||||
int cc_namelen;
|
int cc_namelen;
|
||||||
int (*cc_func)(int);
|
int (*cc_func)(int);
|
||||||
@ -1017,7 +1017,7 @@ int relex(void) /* lexical analyzer for reparse */
|
|||||||
static uschar *buf = NULL;
|
static uschar *buf = NULL;
|
||||||
static int bufsz = 100;
|
static int bufsz = 100;
|
||||||
uschar *bp;
|
uschar *bp;
|
||||||
struct charclass *cc;
|
const struct charclass *cc;
|
||||||
int i;
|
int i;
|
||||||
int num, m;
|
int num, m;
|
||||||
bool commafound, digitfound;
|
bool commafound, digitfound;
|
||||||
|
12
lex.c
12
lex.c
@ -43,7 +43,7 @@ typedef struct Keyword {
|
|||||||
int type;
|
int type;
|
||||||
} Keyword;
|
} Keyword;
|
||||||
|
|
||||||
Keyword keywords[] ={ /* keep sorted: binary searched */
|
const Keyword keywords[] = { /* keep sorted: binary searched */
|
||||||
{ "BEGIN", XBEGIN, XBEGIN },
|
{ "BEGIN", XBEGIN, XBEGIN },
|
||||||
{ "END", XEND, XEND },
|
{ "END", XEND, XEND },
|
||||||
{ "NF", VARNF, VARNF },
|
{ "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); }
|
#define RET(x) { if(dbg)printf("lex %s\n", tokname(x)); return(x); }
|
||||||
|
|
||||||
int peek(void)
|
static int peek(void)
|
||||||
{
|
{
|
||||||
int c = input();
|
int c = input();
|
||||||
unput(c);
|
unput(c);
|
||||||
return 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;
|
int c, retc;
|
||||||
char *buf = *pbuf;
|
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;
|
int cond, low, mid, high;
|
||||||
|
|
||||||
@ -460,7 +460,7 @@ int binsearch(char *w, Keyword *kp, int n)
|
|||||||
|
|
||||||
int word(char *w)
|
int word(char *w)
|
||||||
{
|
{
|
||||||
Keyword *kp;
|
const Keyword *kp;
|
||||||
int c, n;
|
int c, n;
|
||||||
|
|
||||||
n = binsearch(w, keywords, sizeof(keywords)/sizeof(keywords[0]));
|
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 */
|
void unput(int c) /* put lexical character back on input */
|
||||||
{
|
{
|
||||||
|
if (c == '\n')
|
||||||
|
lineno--;
|
||||||
if (yysptr >= yysbuf + sizeof(yysbuf))
|
if (yysptr >= yysbuf + sizeof(yysbuf))
|
||||||
FATAL("pushed back too much: %.20s...", yysbuf);
|
FATAL("pushed back too much: %.20s...", yysbuf);
|
||||||
*yysptr++ = c;
|
*yysptr++ = c;
|
||||||
|
24
lib.c
24
lib.c
@ -32,15 +32,17 @@ THIS SOFTWARE.
|
|||||||
#include "awk.h"
|
#include "awk.h"
|
||||||
#include "ytab.h"
|
#include "ytab.h"
|
||||||
|
|
||||||
|
char EMPTY[] = { '\0' };
|
||||||
FILE *infile = NULL;
|
FILE *infile = NULL;
|
||||||
char *file = "";
|
char *file = EMPTY;
|
||||||
char *record;
|
char *record;
|
||||||
int recsize = RECSIZE;
|
int recsize = RECSIZE;
|
||||||
char *fields;
|
char *fields;
|
||||||
int fieldssize = RECSIZE;
|
int fieldssize = RECSIZE;
|
||||||
|
|
||||||
Cell **fldtab; /* pointers to Cells */
|
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
|
#define MAXFLD 2
|
||||||
int nfields = MAXFLD; /* last allocated slot for $i */
|
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 */
|
int argno = 1; /* current input argument number */
|
||||||
extern Awkfloat *ARGC;
|
extern Awkfloat *ARGC;
|
||||||
|
|
||||||
static Cell dollar0 = { OCELL, CFLD, NULL, "", 0.0, REC|STR|DONTFREE };
|
static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, 0.0, REC|STR|DONTFREE };
|
||||||
static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE };
|
static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE };
|
||||||
|
|
||||||
void recinit(unsigned int n)
|
void recinit(unsigned int n)
|
||||||
{
|
{
|
||||||
@ -116,9 +118,17 @@ void initgetrec(void)
|
|||||||
*/
|
*/
|
||||||
void savefs(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);
|
FATAL("field separator %.10s... is too long", *FS);
|
||||||
strcpy(inputFS, *FS);
|
memcpy(inputFS, *FS, len_inputFS);
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool firsttime = true;
|
static bool firsttime = true;
|
||||||
@ -404,7 +414,7 @@ void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */
|
|||||||
p = fldtab[i];
|
p = fldtab[i];
|
||||||
if (freeable(p))
|
if (freeable(p))
|
||||||
xfree(p->sval);
|
xfree(p->sval);
|
||||||
p->sval = "";
|
p->sval = EMPTY,
|
||||||
p->tval = FLD | STR | DONTFREE;
|
p->tval = FLD | STR | DONTFREE;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,8 +122,6 @@ int main(int argc, char *argv[])
|
|||||||
printf("#include <stdio.h>\n");
|
printf("#include <stdio.h>\n");
|
||||||
printf("#include \"awk.h\"\n");
|
printf("#include \"awk.h\"\n");
|
||||||
printf("#include \"ytab.h\"\n\n");
|
printf("#include \"ytab.h\"\n\n");
|
||||||
for (i = SIZE; --i >= 0; )
|
|
||||||
names[i] = "";
|
|
||||||
|
|
||||||
if (argc != 2) {
|
if (argc != 2) {
|
||||||
fprintf(stderr, "usage: maketab YTAB_H\n");
|
fprintf(stderr, "usage: maketab YTAB_H\n");
|
||||||
@ -160,10 +158,8 @@ int main(int argc, char *argv[])
|
|||||||
table[p->token-FIRSTTOKEN] = p->name;
|
table[p->token-FIRSTTOKEN] = p->name;
|
||||||
printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
|
printf("\nCell *(*proctab[%d])(Node **, int) = {\n", SIZE);
|
||||||
for (i=0; i<SIZE; i++)
|
for (i=0; i<SIZE; i++)
|
||||||
if (table[i]==NULL)
|
printf("\t%s,\t/* %s */\n",
|
||||||
printf("\tnullproc,\t/* %s */\n", names[i]);
|
table[i] ? table[i] : "nullproc", names[i] ? names[i] : "");
|
||||||
else
|
|
||||||
printf("\t%s,\t/* %s */\n", table[i], names[i]);
|
|
||||||
printf("};\n\n");
|
printf("};\n\n");
|
||||||
|
|
||||||
printf("const char *tokname(int n)\n"); /* print a tokname() function */
|
printf("const char *tokname(int n)\n"); /* print a tokname() function */
|
||||||
|
9
run.c
9
run.c
@ -90,7 +90,7 @@ static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, NULL };
|
|||||||
Cell *jexit = &exitcell;
|
Cell *jexit = &exitcell;
|
||||||
static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL };
|
static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL };
|
||||||
Cell *jret = &retcell;
|
Cell *jret = &retcell;
|
||||||
static Cell tempcell ={ OCELL, CTEMP, 0, "", 0.0, NUM|STR|DONTFREE, NULL };
|
static Cell tempcell ={ OCELL, CTEMP, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL };
|
||||||
|
|
||||||
Node *curnode = NULL; /* the node being executed, for debugging */
|
Node *curnode = NULL; /* the node being executed, for debugging */
|
||||||
|
|
||||||
@ -224,7 +224,7 @@ struct Frame *fp = NULL; /* frame pointer. bottom level unused */
|
|||||||
|
|
||||||
Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
|
Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
|
||||||
{
|
{
|
||||||
static Cell newcopycell = { OCELL, CCOPY, 0, "", 0.0, NUM|STR|DONTFREE, NULL };
|
static Cell newcopycell = { OCELL, CCOPY, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL };
|
||||||
int i, ncall, ndef;
|
int i, ncall, ndef;
|
||||||
int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */
|
int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */
|
||||||
Node *x;
|
Node *x;
|
||||||
@ -1246,7 +1246,8 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
|
|||||||
{
|
{
|
||||||
Cell *x = NULL, *y, *ap;
|
Cell *x = NULL, *y, *ap;
|
||||||
const char *s, *origs, *t;
|
const char *s, *origs, *t;
|
||||||
char *fs = NULL, *origfs = NULL;
|
const char *fs = NULL;
|
||||||
|
char *origfs = NULL;
|
||||||
int sep;
|
int sep;
|
||||||
char temp, num[50];
|
char temp, num[50];
|
||||||
int n, tempstat, arg3type;
|
int n, tempstat, arg3type;
|
||||||
@ -1258,7 +1259,7 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
|
|||||||
fs = getsval(fsloc);
|
fs = getsval(fsloc);
|
||||||
else if (arg3type == STRING) { /* split(str,arr,"string") */
|
else if (arg3type == STRING) { /* split(str,arr,"string") */
|
||||||
x = execute(a[2]);
|
x = execute(a[2]);
|
||||||
origfs = fs = strdup(getsval(x));
|
fs = origfs = strdup(getsval(x));
|
||||||
tempfree(x);
|
tempfree(x);
|
||||||
} else if (arg3type == REGEXPR)
|
} else if (arg3type == REGEXPR)
|
||||||
fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
|
fs = "(regexpr)"; /* split(str,arr,/regexpr/) */
|
||||||
|
Loading…
Reference in New Issue
Block a user