summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2020-06-10 21:03:37 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2020-06-10 21:03:37 +0000
commit64f0343d590e58d28ed139270fa1948c7e41aa8a (patch)
tree3f7908f1ebc1e49bb4362f2693b976940247bfd5
parent39376d8bdeae54592ccfd497c13993baee0afda5 (diff)
Update awk to Oct 24, 2019 version.
-rw-r--r--usr.bin/awk/FIXES12
-rw-r--r--usr.bin/awk/awk.h12
-rw-r--r--usr.bin/awk/b.c70
-rw-r--r--usr.bin/awk/lex.c8
-rw-r--r--usr.bin/awk/lib.c24
-rw-r--r--usr.bin/awk/main.c4
-rw-r--r--usr.bin/awk/maketab.c27
-rw-r--r--usr.bin/awk/parse.c4
-rw-r--r--usr.bin/awk/proto.h8
-rw-r--r--usr.bin/awk/run.c206
-rw-r--r--usr.bin/awk/tran.c26
11 files changed, 204 insertions, 197 deletions
diff --git a/usr.bin/awk/FIXES b/usr.bin/awk/FIXES
index 6682ea68b4c..4bf883ea167 100644
--- a/usr.bin/awk/FIXES
+++ b/usr.bin/awk/FIXES
@@ -1,4 +1,4 @@
-/* $OpenBSD: FIXES,v 1.27 2020/06/10 21:03:12 millert Exp $ */
+/* $OpenBSD: FIXES,v 1.28 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -26,6 +26,16 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987.
+October 25, 2019:
+ More fixes and cleanups from NetBSD, courtesy of Christos
+ Zoulas. Merges PRs 54 and 55.
+
+October 24, 2019:
+ Import second round of code cleanups from NetBSD. Much thanks
+ to Christos Zoulas (Github user zoulasc). Merges PR 53.
+ Add an optimization for string concatenation, also from
+ Christos.
+
October 17, 2019:
Import code cleanups from NetBSD. Much thanks to Christos
Zoulas (Github user zoulasc). Merges PR 51.
diff --git a/usr.bin/awk/awk.h b/usr.bin/awk/awk.h
index 719f414882f..ca64bd02306 100644
--- a/usr.bin/awk/awk.h
+++ b/usr.bin/awk/awk.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: awk.h,v 1.19 2020/06/10 21:03:12 millert Exp $ */
+/* $OpenBSD: awk.h,v 1.20 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -24,6 +24,7 @@ THIS SOFTWARE.
****************************************************************/
#include <assert.h>
+#include <stdint.h>
typedef double Awkfloat;
@@ -31,7 +32,12 @@ typedef double Awkfloat;
typedef unsigned char uschar;
-#define xfree(a) { if ((a) != NULL) { free((void *) (a)); (a) = NULL; } }
+#define xfree(a) { if ((a) != NULL) { free((void *)(intptr_t)(a)); (a) = NULL; } }
+/*
+ * We sometimes cheat writing read-only pointers to NUL-terminate them
+ * and then put back the original value
+ */
+#define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
#define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF
*/
@@ -71,7 +77,7 @@ extern char inputFS[]; /* FS at time of input, for field splitting */
extern int dbg;
-extern char *patbeg; /* beginning of pattern matched */
+extern const char *patbeg; /* beginning of pattern matched */
extern int patlen; /* length of pattern matched. set in b.c */
/* Cell: all information about a variable or constant */
diff --git a/usr.bin/awk/b.c b/usr.bin/awk/b.c
index 1cc54e55a92..60317607645 100644
--- a/usr.bin/awk/b.c
+++ b/usr.bin/awk/b.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: b.c,v 1.27 2020/06/10 21:03:12 millert Exp $ */
+/* $OpenBSD: b.c,v 1.28 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -62,19 +62,19 @@ int maxsetvec = 0;
int rtok; /* next token in current re */
int rlxval;
-static uschar *rlxstr;
-static uschar *prestr; /* current position in current re */
-static uschar *lastre; /* origin of last re */
-static uschar *lastatom; /* origin of last Atom */
-static uschar *starttok;
-static uschar *basestr; /* starts with original, replaced during
+static const uschar *rlxstr;
+static const uschar *prestr; /* current position in current re */
+static const uschar *lastre; /* origin of last re */
+static const uschar *lastatom; /* origin of last Atom */
+static const uschar *starttok;
+static const uschar *basestr; /* starts with original, replaced during
repetition processing */
-static uschar *firstbasestr;
+static const uschar *firstbasestr;
static int setcnt;
static int poscnt;
-char *patbeg;
+const char *patbeg;
int patlen;
#define NFA 128 /* cache this many dynamic fa's */
@@ -193,7 +193,7 @@ fa *mkdfa(const char *s, int anchor) /* does the real work of making a dfa */
Node *p, *p1;
fa *f;
- firstbasestr = (uschar *) s;
+ firstbasestr = (const uschar *) s;
basestr = firstbasestr;
p = reparse(s);
p1 = op2(CAT, op2(STAR, op2(ALL, NIL, NIL), NIL), p);
@@ -203,7 +203,7 @@ fa *mkdfa(const char *s, int anchor) /* does the real work of making a dfa */
poscnt = 0;
penter(p1); /* enter parent pointers and leaf indices */
- if ((f = (fa *) calloc(1, sizeof(fa) + poscnt*sizeof(rrow))) == NULL)
+ if ((f = calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL)
overflo(__func__);
f->accept = poscnt-1; /* penter has computed number of positions in re */
cfoll(f, p1); /* set up follow sets */
@@ -303,13 +303,13 @@ void freetr(Node *p) /* free parse tree */
/* in the parsing of regular expressions, metacharacters like . have */
/* to be seen literally; \056 is not a metacharacter. */
-int hexstr(uschar **pp) /* find and eval hex string at pp, return new p */
+int hexstr(const uschar **pp) /* find and eval hex string at pp, return new p */
{ /* only pick up one 8-bit byte (2 chars) */
- uschar *p;
+ const uschar *p;
int n = 0;
int i;
- for (i = 0, p = (uschar *) *pp; i < 2 && isxdigit(*p); i++, p++) {
+ for (i = 0, p = *pp; i < 2 && isxdigit(*p); i++, p++) {
if (isdigit(*p))
n = 16 * n + *p - '0';
else if (*p >= 'a' && *p <= 'f')
@@ -317,16 +317,16 @@ int hexstr(uschar **pp) /* find and eval hex string at pp, return new p */
else if (*p >= 'A' && *p <= 'F')
n = 16 * n + *p - 'A' + 10;
}
- *pp = (uschar *) p;
+ *pp = p;
return n;
}
#define isoctdigit(c) ((c) >= '0' && (c) <= '7') /* multiple use of arg */
-int quoted(uschar **pp) /* pick up next thing after a \\ */
+int quoted(const uschar **pp) /* pick up next thing after a \\ */
/* and increment *pp */
{
- uschar *p = *pp;
+ const uschar *p = *pp;
int c;
if ((c = *p++) == 't')
@@ -364,8 +364,8 @@ int quoted(uschar **pp) /* pick up next thing after a \\ */
char *cclenter(const char *argp) /* add a character class */
{
int i, c, c2;
- uschar *p = (uschar *) argp;
- uschar *op, *bp;
+ const uschar *op, *p = (const uschar *) argp;
+ uschar *bp;
static uschar *buf = NULL;
static int bufsz = 100;
@@ -524,7 +524,7 @@ void follow(Node *v) /* collects leaves that can follow v into setvec */
int member(int c, const char *sarg) /* is c in s? */
{
- uschar *s = (uschar *) sarg;
+ const uschar *s = (const uschar *) sarg;
while (*s)
if (c == *s++)
@@ -535,7 +535,7 @@ int member(int c, const char *sarg) /* is c in s? */
int match(fa *f, const char *p0) /* shortest match ? */
{
int s, ns;
- uschar *p = (uschar *) p0;
+ const uschar *p = (const uschar *) p0;
s = f->initstat;
assert (s < f->state_count);
@@ -557,13 +557,13 @@ int match(fa *f, const char *p0) /* shortest match ? */
int pmatch(fa *f, const char *p0) /* longest match, for sub */
{
int s, ns;
- uschar *p = (uschar *) p0;
- uschar *q;
+ const uschar *p = (const uschar *) p0;
+ const uschar *q;
s = f->initstat;
assert(s < f->state_count);
- patbeg = (char *)p;
+ patbeg = (const char *)p;
patlen = -1;
do {
q = p;
@@ -580,7 +580,7 @@ int pmatch(fa *f, const char *p0) /* longest match, for sub */
if (s == 1) { /* no transition */
if (patlen >= 0) {
- patbeg = (char *) p;
+ patbeg = (const char *) p;
return(1);
}
else
@@ -590,7 +590,7 @@ int pmatch(fa *f, const char *p0) /* longest match, for sub */
if (f->out[s])
patlen = q-p-1; /* don't count $ */
if (patlen >= 0) {
- patbeg = (char *) p;
+ patbeg = (const char *) p;
return(1);
}
nextin:
@@ -602,13 +602,13 @@ int pmatch(fa *f, const char *p0) /* longest match, for sub */
int nematch(fa *f, const char *p0) /* non-empty match, for sub */
{
int s, ns;
- uschar *p = (uschar *) p0;
- uschar *q;
+ const uschar *p = (const uschar *) p0;
+ const uschar *q;
s = f->initstat;
assert(s < f->state_count);
- patbeg = (char *)p;
+ patbeg = (const char *)p;
patlen = -1;
while (*p) {
q = p;
@@ -622,7 +622,7 @@ int nematch(fa *f, const char *p0) /* non-empty match, for sub */
s = cgoto(f, s, *q);
if (s == 1) { /* no transition */
if (patlen > 0) {
- patbeg = (char *) p;
+ patbeg = (const char *) p;
return(1);
} else
goto nnextin; /* no nonempty match */
@@ -631,7 +631,7 @@ int nematch(fa *f, const char *p0) /* non-empty match, for sub */
if (f->out[s])
patlen = q-p-1; /* don't count $ */
if (patlen > 0 ) {
- patbeg = (char *) p;
+ patbeg = (const char *) p;
return(1);
}
nnextin:
@@ -734,7 +734,7 @@ Node *reparse(const char *p) /* parses regular expression pointed to by p */
Node *np;
DPRINTF( ("reparse <%s>\n", p) );
- lastre = prestr = (uschar *) p; /* prestr points to string to be parsed */
+ lastre = prestr = (const uschar *) p; /* prestr points to string to be parsed */
rtok = relex();
/* GNU compatibility: an empty regexp matches anything */
if (rtok == '\0') {
@@ -774,12 +774,12 @@ Node *primary(void)
rtok = relex();
return (unary(op2(DOT, NIL, NIL)));
case CCL:
- np = op2(CCL, NIL, (Node*) cclenter((char *) rlxstr));
+ np = op2(CCL, NIL, (Node*) cclenter((const char *) rlxstr));
lastatom = starttok;
rtok = relex();
return (unary(np));
case NCCL:
- np = op2(NCCL, NIL, (Node *) cclenter((char *) rlxstr));
+ np = op2(NCCL, NIL, (Node *) cclenter((const char *) rlxstr));
lastatom = starttok;
rtok = relex();
return (unary(np));
@@ -918,7 +918,7 @@ replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom,
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((char *) reptok) - reptoklen; /* string after rep specifier */
+ int suffix_length = strlen((const char *) reptok) - reptoklen; /* string after rep specifier */
int size = prefix_length + suffix_length;
if (firstnum > 1) { /* add room for reps 2 through firstnum */
diff --git a/usr.bin/awk/lex.c b/usr.bin/awk/lex.c
index 0dd2aaeadae..746e1e4978f 100644
--- a/usr.bin/awk/lex.c
+++ b/usr.bin/awk/lex.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: lex.c,v 1.14 2020/06/10 21:02:33 millert Exp $ */
+/* $OpenBSD: lex.c,v 1.15 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -184,7 +184,7 @@ int yylex(void)
static char *buf = NULL;
static int bufsize = 5; /* BUG: setting this small causes core dump! */
- if (buf == NULL && (buf = (char *) malloc(bufsize)) == NULL)
+ if (buf == NULL && (buf = malloc(bufsize)) == NULL)
FATAL( "out of space in yylex" );
if (sc) {
sc = 0;
@@ -374,7 +374,7 @@ int string(void)
static char *buf = NULL;
static int bufsz = 500;
- if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
+ if (buf == NULL && (buf = malloc(bufsz)) == NULL)
FATAL("out of space for strings");
for (bp = buf; (c = input()) != '"'; ) {
if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, "string"))
@@ -521,7 +521,7 @@ int regexpr(void)
static int bufsz = 500;
char *bp;
- if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
+ if (buf == NULL && (buf = malloc(bufsz)) == NULL)
FATAL("out of space for rex expr");
bp = buf;
for ( ; ((c = input()) != '/' || openclass == 1) && c != 0; ) {
diff --git a/usr.bin/awk/lib.c b/usr.bin/awk/lib.c
index fe828cb38cd..175e39bc663 100644
--- a/usr.bin/awk/lib.c
+++ b/usr.bin/awk/lib.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: lib.c,v 1.30 2020/06/10 21:02:53 millert Exp $ */
+/* $OpenBSD: lib.c,v 1.31 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -59,10 +59,10 @@ static Cell dollar1 = { OCELL, CFLD, NULL, "", 0.0, FLD|STR|DONTFREE };
void recinit(unsigned int n)
{
- if ( (record = (char *) malloc(n)) == NULL
- || (fields = (char *) malloc(n+1)) == NULL
- || (fldtab = (Cell **) calloc(nfields+2, sizeof(Cell *))) == NULL
- || (fldtab[0] = (Cell *) malloc(sizeof(Cell))) == NULL )
+ if ( (record = malloc(n)) == NULL
+ || (fields = malloc(n+1)) == NULL
+ || (fldtab = calloc(nfields+2, sizeof(*fldtab))) == NULL
+ || (fldtab[0] = malloc(sizeof(**fldtab))) == NULL)
FATAL("out of space for $0 and fields");
*record = '\0';
*fldtab[0] = dollar0;
@@ -77,11 +77,11 @@ void makefields(int n1, int n2) /* create $n1..$n2 inclusive */
int i;
for (i = n1; i <= n2; i++) {
- fldtab[i] = (Cell *) malloc(sizeof (struct Cell));
+ fldtab[i] = malloc(sizeof(**fldtab));
if (fldtab[i] == NULL)
FATAL("out of space in makefields %d", i);
*fldtab[i] = dollar1;
- snprintf(temp, sizeof temp, "%d", i);
+ snprintf(temp, sizeof(temp), "%d", i);
fldtab[i]->nval = tostring(temp);
}
}
@@ -217,7 +217,7 @@ int readrec(char **pbuf, int *pbufsize, FILE *inf) /* read one record into buf *
fa *pfa = makedfa(rs, 1);
found = fnematch(pfa, inf, &buf, &bufsize, recsize);
if (found)
- *patbeg = 0;
+ setptr(patbeg, '\0');
} else {
if ((sep = *rs) == 0) {
sep = '\n';
@@ -261,7 +261,7 @@ char *getargv(int n) /* get ARGV[n] */
char *s, temp[50];
extern Array *ARGVtab;
- snprintf(temp, sizeof temp, "%d", n);
+ snprintf(temp, sizeof(temp), "%d", n);
if (lookup(temp, ARGVtab) == NULL)
return NULL;
x = setsymtab(temp, "", 0.0, STR, ARGVtab);
@@ -306,7 +306,7 @@ void fldbld(void) /* create fields from current record */
n = strlen(r);
if (n > fieldssize) {
xfree(fields);
- if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
+ if ((fields = malloc(n+2)) == NULL) /* possibly 2 final \0s */
FATAL("out of space for fields in fldbld %d", n);
fieldssize = n;
}
@@ -449,7 +449,7 @@ void growfldtab(int n) /* make new fields up to at least $n */
nf = n;
s = (nf+1) * (sizeof (struct Cell *)); /* freebsd: how much do we need? */
if (s / sizeof(struct Cell *) - 1 == nf) /* didn't overflow */
- fldtab = (Cell **) realloc(fldtab, s);
+ fldtab = realloc(fldtab, s);
else /* overflow sizeof int */
xfree(fldtab); /* make it null */
if (fldtab == NULL)
@@ -469,7 +469,7 @@ int refldbld(const char *rec, const char *fs) /* build fields from reg expr in F
n = strlen(rec);
if (n > fieldssize) {
xfree(fields);
- if ((fields = (char *) malloc(n+1)) == NULL)
+ if ((fields = malloc(n+1)) == NULL)
FATAL("out of space for fields in refldbld %d", n);
fieldssize = n;
}
diff --git a/usr.bin/awk/main.c b/usr.bin/awk/main.c
index 3d4e2d7b245..eca436d48de 100644
--- a/usr.bin/awk/main.c
+++ b/usr.bin/awk/main.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: main.c,v 1.32 2020/06/10 21:03:12 millert Exp $ */
+/* $OpenBSD: main.c,v 1.33 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -23,7 +23,7 @@ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
THIS SOFTWARE.
****************************************************************/
-const char *version = "version 20191017";
+const char *version = "version 20191025";
#define DEBUG
#include <stdio.h>
diff --git a/usr.bin/awk/maketab.c b/usr.bin/awk/maketab.c
index 06708dc99a7..a488d7ed187 100644
--- a/usr.bin/awk/maketab.c
+++ b/usr.bin/awk/maketab.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: maketab.c,v 1.15 2020/06/10 21:02:33 millert Exp $ */
+/* $OpenBSD: maketab.c,v 1.16 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -134,10 +134,11 @@ int main(int argc, char *argv[])
fprintf(stderr, "maketab can't open %s!\n", argv[1]);
exit(1);
}
- printf("static char *printname[%d] = {\n", SIZE);
+ printf("static const char * const printname[%d] = {\n", SIZE);
i = 0;
while (fgets(buf, sizeof buf, fp) != NULL) {
- n = sscanf(buf, "%1c %s %s %d", &c, def, name, &tok);
+ // 199 is sizeof(def) - 1
+ n = sscanf(buf, "%1c %199s %199s %d", &c, def, name, &tok);
if (n != 4 || c != '#' || strcmp(def, "define") != 0)
continue; /* not a valid #define */
if (strcmp(name, "YYSTYPE_IS_DECLARED") == 0)
@@ -146,12 +147,12 @@ int main(int argc, char *argv[])
/* fprintf(stderr, "maketab: funny token %d %s ignored\n", tok, buf); */
continue;
}
- names[tok-FIRSTTOKEN] = (char *) strdup(name);
+ names[tok-FIRSTTOKEN] = strdup(name);
if (names[tok-FIRSTTOKEN] == NULL) {
- fprintf(stderr, "maketab: out of memory\n");
+ fprintf(stderr, "maketab out of space copying %s", name);
exit(1);
}
- printf("\t(char *) \"%s\",\t/* %d */\n", name, tok);
+ printf("\t\"%s\",\t/* %d */\n", name, tok);
i++;
}
printf("};\n\n");
@@ -166,14 +167,14 @@ int main(int argc, char *argv[])
printf("\t%s,\t/* %s */\n", table[i], names[i]);
printf("};\n\n");
- printf("char *tokname(int n)\n"); /* print a tokname() function */
+ printf("const char *tokname(int n)\n"); /* print a tokname() function */
printf("{\n");
- printf(" static char buf[100];\n\n");
- printf(" if (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
- printf(" snprintf(buf, sizeof buf, \"token %%d\", n);\n");
- printf(" return buf;\n");
- printf(" }\n");
- printf(" return printname[n-FIRSTTOKEN];\n");
+ printf("\tstatic char buf[100];\n\n");
+ printf("\tif (n < FIRSTTOKEN || n > LASTTOKEN) {\n");
+ printf("\t\tsnprintf(buf, sizeof(buf), \"token %%d\", n);\n");
+ printf("\t\treturn buf;\n");
+ printf("\t}\n");
+ printf("\treturn printname[n-FIRSTTOKEN];\n");
printf("}\n");
return 0;
}
diff --git a/usr.bin/awk/parse.c b/usr.bin/awk/parse.c
index 9060384027c..0394da3a7e8 100644
--- a/usr.bin/awk/parse.c
+++ b/usr.bin/awk/parse.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: parse.c,v 1.8 2020/06/10 21:02:33 millert Exp $ */
+/* $OpenBSD: parse.c,v 1.9 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -34,7 +34,7 @@ Node *nodealloc(int n)
{
Node *x;
- x = (Node *) malloc(sizeof(Node) + (n-1)*sizeof(Node *));
+ x = malloc(sizeof(*x) + (n-1) * sizeof(x));
if (x == NULL)
FATAL("out of space in nodealloc");
x->nnext = NULL;
diff --git a/usr.bin/awk/proto.h b/usr.bin/awk/proto.h
index 0abcf78437b..ef48003c2af 100644
--- a/usr.bin/awk/proto.h
+++ b/usr.bin/awk/proto.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: proto.h,v 1.14 2020/06/10 21:02:53 millert Exp $ */
+/* $OpenBSD: proto.h,v 1.15 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -44,8 +44,8 @@ extern fa *mkdfa(const char *, int);
extern int makeinit(fa *, int);
extern void penter(Node *);
extern void freetr(Node *);
-extern int hexstr(uschar **);
-extern int quoted(uschar **);
+extern int hexstr(const uschar **);
+extern int quoted(const uschar **);
extern char *cclenter(const char *);
extern void overflo(const char *) __attribute__((__noreturn__));
extern void cfoll(fa *, Node *);
@@ -90,7 +90,7 @@ extern Node *pa2stat(Node *, Node *, Node *);
extern Node *linkum(Node *, Node *);
extern void defn(Cell *, Node *, Node *);
extern int isarg(const char *);
-extern char *tokname(int);
+extern const char *tokname(int);
extern Cell *(*proctab[])(Node **, int);
extern int ptoi(void *);
extern Node *itonp(int);
diff --git a/usr.bin/awk/run.c b/usr.bin/awk/run.c
index f0110beecf8..b74723ea89e 100644
--- a/usr.bin/awk/run.c
+++ b/usr.bin/awk/run.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: run.c,v 1.52 2020/06/10 21:03:12 millert Exp $ */
+/* $OpenBSD: run.c,v 1.53 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -118,8 +118,8 @@ int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr,
/* round up to next multiple of quantum */
if (rminlen)
minlen += quantum - rminlen;
- tbuf = (char *) realloc(*pbuf, minlen);
- DPRINTF( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void *) *pbuf, (void *) tbuf) );
+ tbuf = realloc(*pbuf, minlen);
+ DPRINTF( ("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, *pbuf, tbuf) );
if (tbuf == NULL) {
if (whatrtn)
FATAL("out of memory in %s", whatrtn);
@@ -239,7 +239,7 @@ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
if (!isfcn(fcn))
FATAL("calling undefined function %s", s);
if (frame == NULL) {
- fp = frame = (struct Frame *) calloc(nframe += 100, sizeof(struct Frame));
+ fp = frame = calloc(nframe += 100, sizeof(*frame));
if (frame == NULL)
FATAL("out of space for stack frames calling %s", s);
}
@@ -274,7 +274,7 @@ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */
if (fp >= frame + nframe) {
int dfp = fp - frame; /* old index */
frame = reallocarray(frame, (nframe += 100),
- sizeof(struct Frame));
+ sizeof(*frame));
if (frame == NULL)
FATAL("out of space for stack frames in %s", s);
fp = frame + dfp;
@@ -407,7 +407,7 @@ Cell *awkgetline(Node **a, int n) /* get next line from specific input */
int bufsize = recsize;
int mode;
- if ((buf = (char *) malloc(bufsize)) == NULL)
+ if ((buf = malloc(bufsize)) == NULL)
FATAL("out of memory in getline");
fflush(stdout); /* in case someone is waiting for a prompt */
@@ -466,31 +466,50 @@ Cell *getnf(Node **a, int n) /* get NF */
return (Cell *) a[0];
}
-Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
+static char *
+makearraystring(Node *p, const char *func)
{
- Cell *x, *y, *z;
- char *s;
- Node *np;
char *buf;
int bufsz = recsize;
- int nsub;
+ size_t blen, seplen;
- if ((buf = (char *) malloc(bufsz)) == NULL)
- FATAL("out of memory in array");
+ if ((buf = malloc(bufsz)) == NULL) {
+ FATAL("%s: out of memory", func);
+ }
- x = execute(a[0]); /* Cell* for symbol table */
- buf[0] = 0;
- for (np = a[1]; np; np = np->nnext) {
- y = execute(np); /* subscript */
- s = getsval(y);
- nsub = strlen(getsval(subseploc));
- if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "array"))
- FATAL("out of memory for %s[%s...]", x->nval, buf);
- strlcat(buf, s, bufsz);
- if (np->nnext)
- strlcat(buf, *SUBSEP, bufsz);
- tempfree(y);
+ blen = 0;
+ buf[blen] = '\0';
+ seplen = strlen(getsval(subseploc));
+
+ for (; p; p = p->nnext) {
+ Cell *x = execute(p); /* expr */
+ char *s = getsval(x);
+ size_t nsub = p->nnext ? seplen : 0;
+ size_t slen = strlen(s);
+ size_t tlen = blen + slen + nsub;
+
+ if (!adjbuf(&buf, &bufsz, tlen + 1, recsize, 0, func)) {
+ FATAL("%s: out of memory %s[%s...]",
+ func, x->nval, buf);
+ }
+ memcpy(buf + blen, s, slen);
+ if (nsub) {
+ memcpy(buf + blen + slen, *SUBSEP, nsub);
+ }
+ buf[tlen] = '\0';
+ blen = tlen;
+ tempfree(x);
}
+ return buf;
+}
+
+Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
+{
+ Cell *x, *z;
+ char *buf;
+
+ x = execute(a[0]); /* Cell* for symbol table */
+ buf = makearraystring(a[1], __func__);
if (!isarr(x)) {
DPRINTF( ("making %s into an array\n", NN(x->nval)) );
if (freeable(x))
@@ -509,10 +528,7 @@ Cell *array(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */
{
- Cell *x, *y;
- Node *np;
- char *s;
- int nsub;
+ Cell *x;
x = execute(a[0]); /* Cell* for symbol table */
if (x == symtabloc) {
@@ -526,22 +542,7 @@ Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts *
x->tval |= ARR;
x->sval = (char *) makesymtab(NSYMTAB);
} else {
- int bufsz = recsize;
- char *buf;
- if ((buf = (char *) malloc(bufsz)) == NULL)
- FATAL("out of memory in adelete");
- buf[0] = 0;
- for (np = a[1]; np; np = np->nnext) {
- y = execute(np); /* subscript */
- s = getsval(y);
- nsub = strlen(getsval(subseploc));
- if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "awkdelete"))
- FATAL("out of memory deleting %s[%s...]", x->nval, buf);
- strlcat(buf, s, bufsz);
- if (np->nnext)
- strlcat(buf, *SUBSEP, bufsz);
- tempfree(y);
- }
+ char *buf = makearraystring(a[1], __func__);
freeelem(x, buf);
free(buf);
}
@@ -551,12 +552,8 @@ Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts *
Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */
{
- Cell *x, *ap, *k;
- Node *p;
+ Cell *ap, *k;
char *buf;
- char *s;
- int bufsz = recsize;
- int nsub;
ap = execute(a[1]); /* array name */
if (!isarr(ap)) {
@@ -567,21 +564,7 @@ Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */
ap->tval |= ARR;
ap->sval = (char *) makesymtab(NSYMTAB);
}
- if ((buf = (char *) malloc(bufsz)) == NULL) {
- FATAL("out of memory in intest");
- }
- buf[0] = 0;
- for (p = a[0]; p; p = p->nnext) {
- x = execute(p); /* expr */
- s = getsval(x);
- nsub = strlen(getsval(subseploc));
- if (!adjbuf(&buf, &bufsz, strlen(buf)+strlen(s)+nsub+1, recsize, 0, "intest"))
- FATAL("out of memory deleting %s[%s...]", x->nval, buf);
- strlcat(buf, s, bufsz);
- tempfree(x);
- if (p->nnext)
- strlcat(buf, *SUBSEP, bufsz);
- }
+ buf = makearraystring(a[0], __func__);
k = lookup(buf, (Array *) ap->sval);
tempfree(ap);
free(buf);
@@ -717,7 +700,7 @@ Cell *gettemp(void) /* get a tempcell */
Cell *x;
if (!tmps) {
- tmps = (Cell *) calloc(100, sizeof(Cell));
+ tmps = calloc(100, sizeof(*tmps));
if (!tmps)
FATAL("out of space for temporaries");
for (i = 1; i < 100; i++)
@@ -839,6 +822,8 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
int fmtsz = recsize;
char *buf = *pbuf;
int bufsize = *pbufsize;
+#define FMTSZ(a) (fmtsz - ((a) - fmt))
+#define BUFSZ(a) (bufsize - ((a) - buf))
static int first = 1;
static int have_a_format = 0;
@@ -853,7 +838,7 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
os = s;
p = buf;
- if ((fmt = (char *) malloc(fmtsz)) == NULL)
+ if ((fmt = malloc(fmtsz)) == NULL)
FATAL("out of memory in format()");
while (*s) {
adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
@@ -885,7 +870,8 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
}
x = execute(a);
a = a->nnext;
- snprintf(t-1, fmt + fmtsz - (t-1), "%d", fmtwd=(int) getfval(x));
+ snprintf(t - 1, FMTSZ(t - 1),
+ "%d", fmtwd=(int) getfval(x));
if (fmtwd < 0)
fmtwd = -fmtwd;
adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format");
@@ -910,12 +896,15 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
case 'd': case 'i':
flag = 'd';
if(*(s-1) == 'l') break;
- *(t-1) = 'l';
+ *(t-1) = 'j';
*t = 'd';
*++t = '\0';
break;
case 'o': case 'x': case 'X': case 'u':
flag = *(s-1) == 'l' ? 'd' : 'u';
+ *(t-1) = 'l';
+ *t = *s;
+ *++t = '\0';
break;
case 's':
flag = 's';
@@ -938,20 +927,20 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format5");
switch (flag) {
case '?': /* unknown, so dump it too */
- snprintf(p, buf + bufsize - p, "%s", fmt);
+ snprintf(p, BUFSZ(p), "%s", fmt);
t = getsval(x);
n = strlen(t);
if (fmtwd > n)
n = fmtwd;
adjbuf(&buf, &bufsize, 1+strlen(p)+n+p-buf, recsize, &p, "format6");
p += strlen(p);
- snprintf(p, buf + bufsize - p, "%s", t);
+ snprintf(p, BUFSZ(p), "%s", t);
break;
case 'a':
case 'A':
- case 'f': snprintf(p, buf + bufsize - p, fmt, getfval(x)); break;
- case 'd': snprintf(p, buf + bufsize - p, fmt, (long) getfval(x)); break;
- case 'u': snprintf(p, buf + bufsize - p, fmt, (int) getfval(x)); break;
+ case 'f': snprintf(p, BUFSZ(p), fmt, getfval(x)); break;
+ case 'd': snprintf(p, BUFSZ(p), fmt, (long) getfval(x)); break;
+ case 'u': snprintf(p, BUFSZ(p), fmt, (int) getfval(x)); break;
case 's':
t = getsval(x);
n = strlen(t);
@@ -959,18 +948,18 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
n = fmtwd;
if (!adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format7"))
FATAL("huge string/format (%d chars) in printf %.30s... ran format() out of memory", n, t);
- snprintf(p, buf + bufsize - p, fmt, t);
+ snprintf(p, BUFSZ(p), fmt, t);
break;
case 'c':
if (isnum(x)) {
if ((int)getfval(x))
- snprintf(p, buf + bufsize - p, fmt, (int) getfval(x));
+ snprintf(p, BUFSZ(p), fmt, (int) getfval(x));
else {
*p++ = '\0'; /* explicit null byte */
*p = '\0'; /* next output will start here */
}
} else
- snprintf(p, buf + bufsize - p, fmt, getsval(x)[0]);
+ snprintf(p, BUFSZ(p), fmt, getsval(x)[0]);
break;
default:
FATAL("can't happen: bad conversion %c in format()", flag);
@@ -995,7 +984,7 @@ Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */
char *buf;
int bufsz=3*recsize;
- if ((buf = (char *) malloc(bufsz)) == NULL)
+ if ((buf = malloc(bufsz)) == NULL)
FATAL("out of memory in awksprintf");
y = a[0]->nnext;
x = execute(a[0]);
@@ -1018,7 +1007,7 @@ Cell *awkprintf(Node **a, int n) /* printf */
int len;
int bufsz=3*recsize;
- if ((buf = (char *) malloc(bufsz)) == NULL)
+ if ((buf = malloc(bufsz)) == NULL)
FATAL("out of memory in awkprintf");
y = a[0]->nnext;
x = execute(a[0]);
@@ -1205,13 +1194,14 @@ Cell *cat(Node **a, int q) /* a[0] cat a[1] */
x = execute(a[0]);
n1 = strlen(getsval(x));
- adjbuf(&s, &ssz, n1 + 1, recsize, 0, "cat1");
- strlcpy(s, x->sval, ssz);
y = execute(a[1]);
n2 = strlen(getsval(y));
- adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2");
- strlcpy(s + n1, y->sval, ssz - n1);
+
+ adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat");
+ memcpy(s, x->sval, n1);
+ memcpy(s + n1, y->sval, n2);
+ s[n1 + n2] = '\0';
tempfree(x);
tempfree(y);
@@ -1265,10 +1255,10 @@ Cell *dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */
Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
{
Cell *x = NULL, *y, *ap;
- char *s, *origs;
+ const char *s, *origs, *t;
char *fs = NULL, *origfs = NULL;
int sep;
- char *t, temp, num[50];
+ char temp, num[50];
int n, tempstat, arg3type;
y = execute(a[0]); /* source string */
@@ -1315,18 +1305,18 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
pfa->initstat = 2;
do {
n++;
- snprintf(num, sizeof num, "%d", n);
+ snprintf(num, sizeof(num), "%d", n);
temp = *patbeg;
- *patbeg = '\0';
+ setptr(patbeg, '\0');
if (is_number(s))
setsymtab(num, s, atof(s), STR|NUM, (Array *) ap->sval);
else
setsymtab(num, s, 0.0, STR, (Array *) ap->sval);
- *patbeg = temp;
+ setptr(patbeg, temp);
s = patbeg + patlen;
if (*(patbeg+patlen-1) == 0 || *s == 0) {
n++;
- snprintf(num, sizeof num, "%d", n);
+ snprintf(num, sizeof(num), "%d", n);
setsymtab(num, "", 0.0, STR, (Array *) ap->sval);
pfa->initstat = tempstat;
goto spdone;
@@ -1336,7 +1326,7 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
/* cf gsub and refldbld */
}
n++;
- snprintf(num, sizeof num, "%d", n);
+ snprintf(num, sizeof(num), "%d", n);
if (is_number(s))
setsymtab(num, s, atof(s), STR|NUM, (Array *) ap->sval);
else
@@ -1355,13 +1345,13 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
s++;
while (*s!=' ' && *s!='\t' && *s!='\n' && *s!='\0');
temp = *s;
- *s = '\0';
- snprintf(num, sizeof num, "%d", n);
+ setptr(s, '\0');
+ snprintf(num, sizeof(num), "%d", n);
if (is_number(t))
setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
else
setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
- *s = temp;
+ setptr(s, temp);
if (*s != 0)
s++;
}
@@ -1369,7 +1359,7 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
for (n = 0; *s != 0; s++) {
char buf[2];
n++;
- snprintf(num, sizeof num, "%d", n);
+ snprintf(num, sizeof(num), "%d", n);
buf[0] = *s;
buf[1] = 0;
if (isdigit((uschar)buf[0]))
@@ -1384,21 +1374,21 @@ Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */
while (*s != sep && *s != '\n' && *s != '\0')
s++;
temp = *s;
- *s = '\0';
- snprintf(num, sizeof num, "%d", n);
+ setptr(s, '\0');
+ snprintf(num, sizeof(num), "%d", n);
if (is_number(t))
setsymtab(num, t, atof(t), STR|NUM, (Array *) ap->sval);
else
setsymtab(num, t, 0.0, STR, (Array *) ap->sval);
- *s = temp;
+ setptr(s, temp);
if (*s++ == 0)
break;
}
}
tempfree(ap);
tempfree(y);
- free(origs);
- free(origfs);
+ xfree(origs);
+ xfree(origfs);
x = gettemp();
x->tval = NUM;
x->fval = n;
@@ -1903,17 +1893,17 @@ void flush_all(void)
fflush(files[i].fp);
}
-void backsub(char **pb_ptr, char **sptr_ptr);
+void backsub(char **pb_ptr, const char **sptr_ptr);
Cell *sub(Node **a, int nnn) /* substitute command */
{
- char *sptr, *pb, *q;
+ const char *sptr, *q;
Cell *x, *y, *result;
- char *t, *buf;
+ char *t, *buf, *pb;
fa *pfa;
int bufsz = recsize;
- if ((buf = (char *) malloc(bufsz)) == NULL)
+ if ((buf = malloc(bufsz)) == NULL)
FATAL("out of memory in sub");
x = execute(a[3]); /* target string */
t = getsval(x);
@@ -1968,13 +1958,14 @@ Cell *sub(Node **a, int nnn) /* substitute command */
Cell *gsub(Node **a, int nnn) /* global substitute */
{
Cell *x, *y;
- char *rptr, *sptr, *t, *pb, *q;
+ char *rptr, *pb;
+ const char *q, *t, *sptr;
char *buf;
fa *pfa;
int mflag, tempstat, num;
int bufsz = recsize;
- if ((buf = (char *) malloc(bufsz)) == NULL)
+ if ((buf = malloc(bufsz)) == NULL)
FATAL("out of memory in gsub");
mflag = 0; /* if mflag == 0, can replace empty string */
num = 0;
@@ -2066,9 +2057,10 @@ Cell *gsub(Node **a, int nnn) /* global substitute */
return(x);
}
-void backsub(char **pb_ptr, char **sptr_ptr) /* handle \\& variations */
+void backsub(char **pb_ptr, const char **sptr_ptr) /* handle \\& variations */
{ /* sptr[0] == '\\' */
- char *pb = *pb_ptr, *sptr = *sptr_ptr;
+ char *pb = *pb_ptr;
+ const char *sptr = *sptr_ptr;
if (sptr[1] == '\\') {
if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */
diff --git a/usr.bin/awk/tran.c b/usr.bin/awk/tran.c
index 89cb7e172b8..f4c989e2e8e 100644
--- a/usr.bin/awk/tran.c
+++ b/usr.bin/awk/tran.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: tran.c,v 1.22 2020/06/10 21:02:33 millert Exp $ */
+/* $OpenBSD: tran.c,v 1.23 2020/06/10 21:03:36 millert Exp $ */
/****************************************************************
Copyright (C) Lucent Technologies 1997
All Rights Reserved
@@ -165,8 +165,8 @@ Array *makesymtab(int n) /* make a new symbol table */
Array *ap;
Cell **tp;
- ap = (Array *) malloc(sizeof(Array));
- tp = (Cell **) calloc(n, sizeof(Cell *));
+ ap = malloc(sizeof(*ap));
+ tp = calloc(n, sizeof(*tp));
if (ap == NULL || tp == NULL)
FATAL("out of space in makesymtab");
ap->nelem = 0;
@@ -236,7 +236,7 @@ Cell *setsymtab(const char *n, const char *s, Awkfloat f, unsigned t, Array *tp)
(void*)p, NN(p->nval), NN(p->sval), p->fval, p->tval) );
return(p);
}
- p = (Cell *) malloc(sizeof(Cell));
+ p = malloc(sizeof(*p));
if (p == NULL)
FATAL("out of space for symbol table at %s", n);
p->nval = tostring(n);
@@ -271,7 +271,7 @@ void rehash(Array *tp) /* rehash items in small table into big one */
Cell *cp, *op, **np;
nsz = GROWTAB * tp->size;
- np = (Cell **) calloc(nsz, sizeof(Cell *));
+ np = calloc(nsz, sizeof(*np));
if (np == NULL) /* can't do it, but can keep running. */
return; /* someone else will run out later. */
for (i = 0; i < tp->size; i++) {
@@ -360,7 +360,7 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
fldno = atoi(vp->nval);
if (fldno > *NF)
newfld(fldno);
- DPRINTF( ("setting field %d to %s (%p)\n", fldno, s, (void *) s) );
+ DPRINTF( ("setting field %d to %s (%p)\n", fldno, s, s) );
} else if (isrec(vp)) {
donefld = 0; /* mark $1... invalid */
donerec = 1;
@@ -377,7 +377,7 @@ char *setsval(Cell *vp, const char *s) /* set string val of a Cell */
vp->fmt = NULL;
setfree(vp);
DPRINTF( ("setsval %p: %s = \"%s (%p) \", t=%o r,f=%d,%d\n",
- (void*)vp, NN(vp->nval), t, (void *) t, vp->tval, donerec, donefld) );
+ (void*)vp, NN(vp->nval), t, t, vp->tval, donerec, donefld) );
vp->sval = t;
if (&vp->fval == NF) {
donerec = 0; /* mark $0 invalid */
@@ -492,7 +492,7 @@ static char *get_str_val(Cell *vp, char **fmt) /* get string val of a Cel
}
done:
DPRINTF( ("getsval %p: %s = \"%s (%p)\", t=%o\n",
- (void*)vp, NN(vp->nval), vp->sval, (void *) vp->sval, vp->tval) );
+ (void*)vp, NN(vp->nval), vp->sval, vp->sval, vp->tval) );
return(vp->sval);
}
@@ -509,12 +509,10 @@ char *getpssval(Cell *vp) /* get string val of a Cell for print */
char *tostring(const char *s) /* make a copy of string s */
{
- char *p;
-
- p = strdup(s);
+ char *p = strdup(s);
if (p == NULL)
FATAL("out of space in tostring on %s", s);
- return p;
+ return(p);
}
Cell *catstr(Cell *a, Cell *b) /* concatenate a and b */
@@ -537,10 +535,10 @@ char *qstring(const char *is, int delim) /* collect string up to next delim */
{
const char *os = is;
int c, n;
- uschar *s = (uschar *) is;
+ const uschar *s = (const uschar *) is;
uschar *buf, *bp;
- if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL)
+ if ((buf = malloc(strlen(is)+3)) == NULL)
FATAL( "out of space in qstring(%s)", s);
for (bp = buf; (c = *s) != delim; s++) {
if (c == '\n')