shithub: trueawk

Download patch

ref: 108224b4845d7ac622cdc3dcbe47b463e4253a4b
parent: c879fbf013b5314c27fa236c987ea56a521420e6
author: Arnold D. Robbins <arnold@skeeve.com>
date: Sun Nov 10 16:19:18 EST 2019

Convert variables to bool and enum.

--- a/FIXES
+++ b/FIXES
@@ -25,6 +25,12 @@
 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
--- a/awk.h
+++ b/awk.h
@@ -24,6 +24,7 @@
 
 #include <assert.h>
 #include <stdint.h>
+#include <stdbool.h>
 
 typedef double	Awkfloat;
 
@@ -48,9 +49,14 @@
 #	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 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 @@
 	uschar	*restr;
 	int	**posns;
 	int	state_count;
-	int	anchor;
+	bool	anchor;
 	int	use;
 	int	initstat;
 	int	curstat;
--- a/awkgram.y
+++ b/awkgram.y
@@ -32,8 +32,8 @@
 
 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 @@
 		{ 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:
--- a/b.c
+++ b/b.c
@@ -141,7 +141,7 @@
 	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 @@
 		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 @@
 	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 @@
 	return f;
 }
 
-int makeinit(fa *f, int anchor)
+int makeinit(fa *f, bool anchor)
 {
 	int i, k;
 
@@ -645,11 +645,11 @@
  *     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 @@
 				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 @@
 	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 @@
 	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 @@
 	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 @@
 		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 @@
 					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",
--- a/lex.c
+++ b/lex.c
@@ -164,8 +164,8 @@
 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 @@
 	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 @@
 		case '}':
 			if (--bracecnt < 0)
 				SYNTAX( "extra }" );
-			sc = 1;
+			sc = true;
 			RET(';');
 		case ']':
 			if (--brackcnt < 0)
@@ -500,7 +500,7 @@
 
 void startreg(void)	/* next call to yylex will return a regular expression */
 {
-	reg = 1;
+	reg = true;
 }
 
 int regexpr(void)
--- a/lib.c
+++ b/lib.c
@@ -45,8 +45,8 @@
 #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 @@
 	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 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 @@
 	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 @@
 		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 @@
 		}
 	}
 	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 @@
 	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 @@
 
 	   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 @@
 	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 @@
 	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 @@
 	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;
--- a/main.c
+++ b/main.c
@@ -22,7 +22,7 @@
 THIS SOFTWARE.
 ****************************************************************/
 
-const char	*version = "version 20191108";
+const char	*version = "version 20191110";
 
 #define DEBUG
 #include <stdio.h>
@@ -43,8 +43,7 @@
 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 @@
 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 @@
 		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 @@
 	}
 	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 @@
 		*FS = qstring(fs, '\0');
 	   dprintf( ("errorflag=%d\n", errorflag) );
 	if (errorflag == 0) {
-		compile_time = 0;
+		compile_time = RUNNING;
 		run(winner);
 	} else
 		bracecheck();
--- a/proto.h
+++ b/proto.h
@@ -38,9 +38,9 @@
 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	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	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);
--- a/run.c
+++ b/run.c
@@ -189,7 +189,7 @@
 		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 @@
 		}
 	} 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 *getnf(Node **a, int n)	/* get NF */
 {
-	if (donefld == 0)
+	if (!donefld)
 		fldbld();
 	return (Cell *) a[0];
 }
@@ -821,8 +821,8 @@
 #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];
@@ -829,7 +829,7 @@
 
 		snprintf(buf, sizeof(buf), "%a", 42.0);
 		have_a_format = (strcmp(buf, "0x1.5p+5") == 0);
-		first = 0;
+		first = false;
 	}
 
 	os = s;
--- a/tran.c
+++ b/tran.c
@@ -306,21 +306,21 @@
 	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 @@
 	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 @@
 		(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 @@
 {
 	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 @@
 
 	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();
 
 	/*
--