Convert variables to bool and enum.

This commit is contained in:
Arnold D. Robbins 2019-11-10 21:19:18 +02:00
parent c879fbf013
commit 108224b484
10 changed files with 106 additions and 90 deletions

6
FIXES
View File

@ -25,6 +25,12 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987.
November 10, 2019:
Convert a number of Boolean integer variables into
actual bools. Convert compile_time variable into an
enum and simplify some of the related code. Thanks
to Arnold Robbins.
November 8, 2019:
Fix from Ori Bernstein to get UTF-8 characters instead of
bytes when FS = "". This is currently the only bit of

16
awk.h
View File

@ -24,6 +24,7 @@ THIS SOFTWARE.
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
typedef double Awkfloat;
@ -48,8 +49,13 @@ typedef unsigned char uschar;
# define dprintf(x)
#endif
extern int compile_time; /* 1 if compiling, 0 if running */
extern int safe; /* 0 => unsafe, 1 => safe */
extern enum compile_states {
RUNNING,
COMPILING,
ERROR_PRINTING
} compile_time;
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 */
@ -70,8 +76,8 @@ extern Awkfloat *RLENGTH;
extern char *record; /* points to $0 */
extern int lineno; /* line number in awk program */
extern int errorflag; /* 1 if error has occurred */
extern int donefld; /* 1 if record broken into fields */
extern int donerec; /* 1 if record is valid (no fld has changed */
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;
@ -237,7 +243,7 @@ typedef struct fa {
uschar *restr;
int **posns;
int state_count;
int anchor;
bool anchor;
int use;
int initstat;
int curstat;

View File

@ -32,8 +32,8 @@ int yywrap(void) { return(1); }
Node *beginloc = 0;
Node *endloc = 0;
int infunc = 0; /* = 1 if in arglist or body of func */
int inloop = 0; /* = 1 if in while, for, do */
bool infunc = false; /* = true if in arglist or body of func */
int inloop = 0; /* >= 1 if in while, for, do; can't be bool, since loops can next */
char *curfname = 0; /* current function name */
Node *arglist = 0; /* list of args for current function */
%}
@ -182,8 +182,8 @@ pa_stat:
{ beginloc = linkum(beginloc, $3); $$ = 0; }
| XEND lbrace stmtlist '}'
{ endloc = linkum(endloc, $3); $$ = 0; }
| FUNC funcname '(' varlist rparen {infunc++;} lbrace stmtlist '}'
{ infunc--; curfname=0; defn((Cell *)$2, $4, $8); $$ = 0; }
| FUNC funcname '(' varlist rparen {infunc = true;} lbrace stmtlist '}'
{ infunc = false; curfname=0; defn((Cell *)$2, $4, $8); $$ = 0; }
;
pa_stats:

38
b.c
View File

@ -141,7 +141,7 @@ out:
overflo(__func__);
}
fa *makedfa(const char *s, int anchor) /* returns dfa for reg expr s */
fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */
{
int i, use, nuse;
fa *pfa;
@ -151,7 +151,7 @@ fa *makedfa(const char *s, int anchor) /* returns dfa for reg expr s */
resizesetvec(__func__);
}
if (compile_time) /* a constant for sure */
if (compile_time != RUNNING) /* a constant for sure */
return mkdfa(s, anchor);
for (i = 0; i < nfatab; i++) /* is it there already? */
if (fatab[i]->anchor == anchor
@ -179,7 +179,7 @@ fa *makedfa(const char *s, int anchor) /* returns dfa for reg expr s */
return pfa;
}
fa *mkdfa(const char *s, int anchor) /* does the real work of making a dfa */
fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */
/* anchor = 1 for anchored matches, else 0 */
{
Node *p, *p1;
@ -214,7 +214,7 @@ fa *mkdfa(const char *s, int anchor) /* does the real work of making a dfa */
return f;
}
int makeinit(fa *f, int anchor)
int makeinit(fa *f, bool anchor)
{
int i, k;
@ -645,11 +645,11 @@ int nematch(fa *f, const char *p0) /* non-empty match, for sub */
* a match is found, patbeg and patlen are set appropriately.
*
* RETURN VALUES
* 0 No match found.
* 1 Match found.
* false No match found.
* true Match found.
*/
int fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum)
bool fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum)
{
char *buf = *pbuf;
int bufsize = *pbufsize;
@ -715,10 +715,10 @@ int fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum)
FATAL("unable to ungetc '%c'", buf[k]);
while (k > i + patlen);
buf[k] = 0;
return 1;
return true;
}
else
return 0;
return false;
}
Node *reparse(const char *p) /* parses regular expression pointed to by p */
@ -908,7 +908,7 @@ replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom,
int i, j;
uschar *buf = 0;
int ret = 1;
int init_q = (firstnum==0); /* first added char will be ? */
int init_q = (firstnum == 0); /* first added char will be ? */
int n_q_reps = secondnum-firstnum; /* m>n, so reduce until {1,m-n} left */
int prefix_length = reptok - basestr; /* prefix includes first rep */
int suffix_length = strlen((const char *) reptok) - reptoklen; /* string after rep specifier */
@ -942,8 +942,9 @@ replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom,
if (special_case == REPEAT_PLUS_APPENDED) {
buf[j++] = '+';
} else if (special_case == REPEAT_WITH_Q) {
if (init_q) buf[j++] = '?';
for (i=0; i < n_q_reps; i++) { /* copy x? reps */
if (init_q)
buf[j++] = '?';
for (i = 0; i < n_q_reps; i++) { /* copy x? reps */
memcpy(&buf[j], atom, atomlen);
j += atomlen;
buf[j++] = '?';
@ -1017,7 +1018,8 @@ int relex(void) /* lexical analyzer for reparse */
uschar *bp;
struct charclass *cc;
int i;
int num, m, commafound, digitfound;
int num, m;
bool commafound, digitfound;
const uschar *startreptok;
static int parens = 0;
@ -1151,8 +1153,8 @@ rescan:
if (isdigit(*(prestr))) {
num = 0; /* Process as a repetition */
n = -1; m = -1;
commafound = 0;
digitfound = 0;
commafound = false;
digitfound = false;
startreptok = prestr-1;
/* Remember start of previous atom here ? */
} else { /* just a { char, not a repetition */
@ -1199,15 +1201,15 @@ rescan:
lastre);
} else if (isdigit(c)) {
num = 10 * num + c - '0';
digitfound = 1;
digitfound = true;
} else if (c == ',') {
if (commafound)
FATAL("illegal repetition expression: class %.20s",
lastre);
/* looking for {n,} or {n,m} */
commafound = 1;
commafound = true;
n = num;
digitfound = 0; /* reset */
digitfound = false; /* reset */
num = 0;
} else {
FATAL("illegal repetition expression: class %.20s",

12
lex.c
View File

@ -164,8 +164,8 @@ int gettok(char **pbuf, int *psz) /* get next input token */
int word(char *);
int string(void);
int regexpr(void);
int sc = 0; /* 1 => return a } right now */
int reg = 0; /* 1 => return a REGEXPR now */
bool sc = false; /* true => return a } right now */
bool reg = false; /* true => return a REGEXPR now */
int yylex(void)
{
@ -176,11 +176,11 @@ int yylex(void)
if (buf == NULL && (buf = malloc(bufsize)) == NULL)
FATAL( "out of space in yylex" );
if (sc) {
sc = 0;
sc = false;
RET('}');
}
if (reg) {
reg = 0;
reg = false;
return regexpr();
}
for (;;) {
@ -327,7 +327,7 @@ int yylex(void)
case '}':
if (--bracecnt < 0)
SYNTAX( "extra }" );
sc = 1;
sc = true;
RET(';');
case ']':
if (--brackcnt < 0)
@ -500,7 +500,7 @@ int word(char *w)
void startreg(void) /* next call to yylex will return a regular expression */
{
reg = 1;
reg = true;
}
int regexpr(void)

51
lib.c
View File

@ -45,8 +45,8 @@ char inputFS[100] = " ";
#define MAXFLD 2
int nfields = MAXFLD; /* last allocated slot for $i */
int donefld; /* 1 = implies rec broken into fields */
int donerec; /* 1 = record is valid (no flds have changed) */
bool donefld; /* true = implies rec broken into fields */
bool donerec; /* true = record is valid (no flds have changed) */
int lastfld = 0; /* last used field */
int argno = 1; /* current input argument number */
@ -121,9 +121,9 @@ void savefs(void)
strcpy(inputFS, *FS);
}
static int firsttime = 1;
static bool firsttime = true;
int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */
int getrec(char **pbuf, int *pbufsize, bool isrecord) /* get next input record */
{ /* note: cares whether buf == record */
int c;
char *buf = *pbuf;
@ -131,14 +131,14 @@ int getrec(char **pbuf, int *pbufsize, int isrecord) /* get next input record */
int bufsize = *pbufsize, savebufsize = bufsize;
if (firsttime) {
firsttime = 0;
firsttime = false;
initgetrec();
}
dprintf( ("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n",
*RS, *FS, *ARGC, *FILENAME) );
if (isrecord) {
donefld = 0;
donerec = 1;
donefld = false;
donerec = true;
savefs();
}
saveb0 = buf[0];
@ -210,7 +210,7 @@ int readrec(char **pbuf, int *pbufsize, FILE *inf) /* read one record into buf *
char *rs = getsval(rsloc);
if (*rs && rs[1]) {
int found;
bool found;
fa *pfa = makedfa(rs, 1);
found = fnematch(pfa, inf, &buf, &bufsize, recsize);
@ -377,7 +377,7 @@ void fldbld(void) /* create fields from current record */
FATAL("record `%.30s...' has too many fields; can't happen", r);
cleanfld(i+1, lastfld); /* clean out junk from previous record */
lastfld = i;
donefld = 1;
donefld = true;
for (j = 1; j <= lastfld; j++) {
p = fldtab[j];
if(is_number(p->sval)) {
@ -386,7 +386,7 @@ void fldbld(void) /* create fields from current record */
}
}
setfval(nfloc, (Awkfloat) lastfld);
donerec = 1; /* restore */
donerec = true; /* restore */
if (dbg) {
for (j = 0; j <= lastfld; j++) {
p = fldtab[j];
@ -513,7 +513,7 @@ void recbld(void) /* create $0 from $1..$NF if necessary */
char *r, *p;
char *sep = getsval(ofsloc);
if (donerec == 1)
if (donerec)
return;
r = record;
for (i = 1; i <= *NF; i++) {
@ -541,7 +541,7 @@ void recbld(void) /* create $0 from $1..$NF if necessary */
dprintf( ("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]) );
dprintf( ("recbld = |%s|\n", record) );
donerec = 1;
donerec = true;
}
int errorflag = 0;
@ -566,7 +566,7 @@ void SYNTAX(const char *fmt, ...)
fprintf(stderr, " at source line %d", lineno);
if (curfname != NULL)
fprintf(stderr, " in function %s", curfname);
if (compile_time == 1 && cursource() != NULL)
if (compile_time == COMPILING && cursource() != NULL)
fprintf(stderr, " source file %s", cursource());
fprintf(stderr, "\n");
errorflag = 2;
@ -640,17 +640,20 @@ void error()
extern Node *curnode;
fprintf(stderr, "\n");
if (compile_time != 2 && NR && *NR > 0) {
fprintf(stderr, " input record number %d", (int) (*FNR));
if (strcmp(*FILENAME, "-") != 0)
fprintf(stderr, ", file %s", *FILENAME);
fprintf(stderr, "\n");
if (compile_time != ERROR_PRINTING) {
if (NR && *NR > 0) {
fprintf(stderr, " input record number %d", (int) (*FNR));
if (strcmp(*FILENAME, "-") != 0)
fprintf(stderr, ", file %s", *FILENAME);
fprintf(stderr, "\n");
}
if (curnode)
fprintf(stderr, " source line number %d", curnode->lineno);
else if (lineno)
fprintf(stderr, " source line number %d", lineno);
}
if (compile_time != 2 && curnode)
fprintf(stderr, " source line number %d", curnode->lineno);
else if (compile_time != 2 && lineno)
fprintf(stderr, " source line number %d", lineno);
if (compile_time == 1 && cursource() != NULL)
if (compile_time == COMPILING && cursource() != NULL)
fprintf(stderr, " source file %s", cursource());
fprintf(stderr, "\n");
eprint();
@ -663,7 +666,7 @@ void eprint(void) /* try to print context around error */
static int been_here = 0;
extern char ebuf[], *ep;
if (compile_time == 2 || compile_time == 0 || been_here++ > 0 || ebuf == ep)
if (compile_time != COMPILING || been_here++ > 0 || ebuf == ep)
return;
if (ebuf == ep)
return;

13
main.c
View File

@ -22,7 +22,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
const char *version = "version 20191108";
const char *version = "version 20191110";
#define DEBUG
#include <stdio.h>
@ -43,8 +43,7 @@ char *cmdname; /* gets argv[0] for error messages */
extern FILE *yyin; /* lex input file */
char *lexprog; /* points to program argument if it exists */
extern int errorflag; /* non-zero if any syntax errors; set by yyerror */
int compile_time = 2; /* for error printing: */
/* 2 = cmdline, 1 = compile, 0 = running */
enum compile_states compile_time = ERROR_PRINTING;
#define MAX_PFILE 20 /* max number of -f's */
@ -52,7 +51,7 @@ char *pfile[MAX_PFILE]; /* program filenames from -f's */
int npfile = 0; /* number of filenames */
int curpfile = 0; /* current filename */
int safe = 0; /* 1 => "safe" mode */
bool safe = false; /* true => "safe" mode */
/* Can this work with recursive calls? I don't think so.
void segvcatch(int n)
@ -96,7 +95,7 @@ int main(int argc, char *argv[])
switch (argv[1][1]) {
case 's':
if (strcmp(argv[1], "-safe") == 0)
safe = 1;
safe = true;
break;
case 'f': /* next argument is program filename */
if (argv[1][2] != 0) { /* arg is -fsomething */
@ -171,7 +170,7 @@ int main(int argc, char *argv[])
}
recinit(recsize);
syminit();
compile_time = 1;
compile_time = COMPILING;
argv[0] = cmdname; /* put prog name at front of arglist */
dprintf( ("argc=%d, argv[0]=%s\n", argc, argv[0]) );
arginit(argc, argv);
@ -183,7 +182,7 @@ int main(int argc, char *argv[])
*FS = qstring(fs, '\0');
dprintf( ("errorflag=%d\n", errorflag) );
if (errorflag == 0) {
compile_time = 0;
compile_time = RUNNING;
run(winner);
} else
bracecheck();

10
proto.h
View File

@ -38,9 +38,9 @@ extern int yylook(void);
extern int yyback(int *, int);
extern int yyinput(void);
extern fa *makedfa(const char *, int);
extern fa *mkdfa(const char *, int);
extern int makeinit(fa *, int);
extern fa *makedfa(const char *, bool);
extern fa *mkdfa(const char *, bool);
extern int makeinit(fa *, bool);
extern void penter(Node *);
extern void freetr(Node *);
extern int hexstr(const uschar **);
@ -54,7 +54,7 @@ extern int member(int, const char *);
extern int match(fa *, const char *);
extern int pmatch(fa *, const char *);
extern int nematch(fa *, const char *);
extern int fnematch(fa *, FILE *, char **, int *, int);
extern bool fnematch(fa *, FILE *, char **, int *, int);
extern Node *reparse(const char *);
extern Node *regexp(void);
extern Node *primary(void);
@ -119,7 +119,7 @@ extern void initgetrec(void);
extern void makefields(int, int);
extern void growfldtab(int n);
extern void savefs(void);
extern int getrec(char **, int *, int);
extern int getrec(char **, int *, bool);
extern void nextfile(void);
extern int readrec(char **buf, int *bufsize, FILE *inf);
extern char *getargv(int);

14
run.c
View File

@ -189,7 +189,7 @@ Cell *program(Node **a, int n) /* execute an awk program */
tempfree(x);
}
if (a[1] || a[2])
while (getrec(&record, &recsize, 1) > 0) {
while (getrec(&record, &recsize, true) > 0) {
x = execute(a[1]);
if (isexit(x))
break;
@ -438,9 +438,9 @@ Cell *awkgetline(Node **a, int n) /* get next line from specific input */
}
} else { /* bare getline; use current input */
if (a[0] == NULL) /* getline */
n = getrec(&record, &recsize, 1);
n = getrec(&record, &recsize, true);
else { /* getline var */
n = getrec(&buf, &bufsize, 0);
n = getrec(&buf, &bufsize, false);
x = execute(a[0]);
setsval(x, buf);
if (is_number(x->sval)) {
@ -457,7 +457,7 @@ Cell *awkgetline(Node **a, int n) /* get next line from specific input */
Cell *getnf(Node **a, int n) /* get NF */
{
if (donefld == 0)
if (!donefld)
fldbld();
return (Cell *) a[0];
}
@ -821,15 +821,15 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
#define FMTSZ(a) (fmtsz - ((a) - fmt))
#define BUFSZ(a) (bufsize - ((a) - buf))
static int first = 1;
static int have_a_format = 0;
static bool first = true;
static bool have_a_format = false;
if (first) {
char buf[100];
snprintf(buf, sizeof(buf), "%a", 42.0);
have_a_format = (strcmp(buf, "0x1.5p+5") == 0);
first = 0;
first = false;
}
os = s;

28
tran.c
View File

@ -306,21 +306,21 @@ Awkfloat setfval(Cell *vp, Awkfloat f) /* set float val of a Cell */
if ((vp->tval & (NUM | STR)) == 0)
funnyvar(vp, "assign to");
if (isfld(vp)) {
donerec = 0; /* mark $0 invalid */
donerec = false; /* mark $0 invalid */
fldno = atoi(vp->nval);
if (fldno > *NF)
newfld(fldno);
dprintf( ("setting field %d to %g\n", fldno, f) );
} else if (&vp->fval == NF) {
donerec = 0; /* mark $0 invalid */
donerec = false; /* mark $0 invalid */
setlastfld(f);
dprintf( ("setting NF to %g\n", f) );
} else if (isrec(vp)) {
donefld = 0; /* mark $1... invalid */
donerec = 1;
donefld = false; /* mark $1... invalid */
donerec = true;
savefs();
} else if (vp == ofsloc) {
if (donerec == 0)
if (!donerec)
recbld();
}
if (freeable(vp))
@ -355,17 +355,17 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
if ((vp->tval & (NUM | STR)) == 0)
funnyvar(vp, "assign to");
if (isfld(vp)) {
donerec = 0; /* mark $0 invalid */
donerec = false; /* mark $0 invalid */
fldno = atoi(vp->nval);
if (fldno > *NF)
newfld(fldno);
dprintf( ("setting field %d to %s (%p)\n", fldno, s, s) );
} else if (isrec(vp)) {
donefld = 0; /* mark $1... invalid */
donerec = 1;
donefld = false; /* mark $1... invalid */
donerec = true;
savefs();
} else if (vp == ofsloc) {
if (donerec == 0)
if (!donerec)
recbld();
}
t = s ? tostring(s) : tostring(""); /* in case it's self-assign */
@ -379,7 +379,7 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
(void*)vp, NN(vp->nval), t, t, vp->tval, donerec, donefld) );
vp->sval = t;
if (&vp->fval == NF) {
donerec = 0; /* mark $0 invalid */
donerec = false; /* mark $0 invalid */
f = getfval(vp);
setlastfld(f);
dprintf( ("setting NF to %g\n", f) );
@ -392,9 +392,9 @@ Awkfloat getfval(Cell *vp) /* get float val of a Cell */
{
if ((vp->tval & (NUM | STR)) == 0)
funnyvar(vp, "read value of");
if (isfld(vp) && donefld == 0)
if (isfld(vp) && !donefld)
fldbld();
else if (isrec(vp) && donerec == 0)
else if (isrec(vp) && !donerec)
recbld();
if (!isnum(vp)) { /* not a number */
vp->fval = atof(vp->sval); /* best guess */
@ -413,9 +413,9 @@ static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cel
if ((vp->tval & (NUM | STR)) == 0)
funnyvar(vp, "read value of");
if (isfld(vp) && donefld == 0)
if (isfld(vp) && ! donefld)
fldbld();
else if (isrec(vp) && donerec == 0)
else if (isrec(vp) && ! donerec)
recbld();
/*