Make it compile with g++.

This commit is contained in:
Arnold D. Robbins 2020-10-13 20:52:43 +03:00
parent 9804285af0
commit 3b42cfaf73
10 changed files with 65 additions and 55 deletions

4
FIXES
View File

@ -25,6 +25,10 @@ THIS SOFTWARE.
This file lists all bug fixes, changes, etc., made since the AWK book
was sent to the printers in August, 1987.
October 13, 2020:
Add casts before all the calls to malloc/calloc/realloc in order
to get it to compile with g++. Thanks to Arnold Robbins.
August 16, 2020:
Additional fixes for DJGPP. Thanks to Eli Zaretskii for
the testing.

View File

@ -99,6 +99,9 @@ welcome.
This compiles without change on Macintosh OS X using `gcc` and
the standard developer tools.
You can also use `make CC=g++` to build with the GNU C++ compiler,
should you choose to do so.
The version of `malloc` that comes with some systems is sometimes
astonishly slow. If `awk` seems slow, you might try fixing that.
More generally, turning on optimization can significantly improve
@ -112,8 +115,9 @@ as we can. Unfortunately, however, keeping this program going
is not at the top of our priority list.
_If_ you (yes, you!) are interested in taking over active maintenance of
`awk`, please open an issue to indicate that fact, give a little bit of
`awk`, please open an issue to indicate that fact, and give us a little bit of
your background and some idea of your plans and dreams. Thanks!
#### Last Updated
Thu Jul 2 21:39:31 IDT 2020
Tue Oct 13 20:00:09 IDT 2020

34
b.c
View File

@ -83,7 +83,7 @@ int nfatab = 0; /* entries in fatab */
static int *
intalloc(size_t n, const char *f)
{
void *p = calloc(n, sizeof(int));
int *p = (int *) calloc(n, sizeof(int));
if (p == NULL)
overflo(f);
return p;
@ -96,8 +96,8 @@ resizesetvec(const char *f)
maxsetvec = MAXLIN;
else
maxsetvec *= 4;
setvec = realloc(setvec, maxsetvec * sizeof(*setvec));
tmpset = realloc(tmpset, maxsetvec * sizeof(*tmpset));
setvec = (int *) realloc(setvec, maxsetvec * sizeof(*setvec));
tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(*tmpset));
if (setvec == NULL || tmpset == NULL)
overflo(f);
}
@ -105,7 +105,9 @@ resizesetvec(const char *f)
static void
resize_state(fa *f, int state)
{
void *p;
unsigned int **p;
uschar *p2;
int **p3;
int i, new_count;
if (++state < f->state_count)
@ -113,23 +115,23 @@ resize_state(fa *f, int state)
new_count = state + 10; /* needs to be tuned */
p = realloc(f->gototab, new_count * sizeof(f->gototab[0]));
p = (unsigned int **) realloc(f->gototab, new_count * sizeof(f->gototab[0]));
if (p == NULL)
goto out;
f->gototab = p;
p = realloc(f->out, new_count * sizeof(f->out[0]));
if (p == NULL)
p2 = (uschar *) realloc(f->out, new_count * sizeof(f->out[0]));
if (p2 == NULL)
goto out;
f->out = p;
f->out = p2;
p = realloc(f->posns, new_count * sizeof(f->posns[0]));
if (p == NULL)
p3 = (int **) realloc(f->posns, new_count * sizeof(f->posns[0]));
if (p3 == NULL)
goto out;
f->posns = p;
f->posns = p3;
for (i = f->state_count; i < new_count; ++i) {
f->gototab[i] = calloc(NCHARS, sizeof(**f->gototab));
f->gototab[i] = (unsigned int *) calloc(NCHARS, sizeof(**f->gototab));
if (f->gototab[i] == NULL)
goto out;
f->out[i] = 0;
@ -195,7 +197,7 @@ fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */
poscnt = 0;
penter(p1); /* enter parent pointers and leaf indices */
if ((f = calloc(1, sizeof(fa) + poscnt * sizeof(rrow))) == NULL)
if ((f = (fa *) 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 */
@ -365,7 +367,7 @@ char *cclenter(const char *argp) /* add a character class */
static int bufsz = 100;
op = p;
if (buf == NULL && (buf = malloc(bufsz)) == NULL)
if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL)
FATAL("out of space for character class [%.10s...] 1", p);
bp = buf;
for (i = 0; (c = *p++) != 0; ) {
@ -937,7 +939,7 @@ replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom,
} else if (special_case == REPEAT_ZERO) {
size += 2; /* just a null ERE: () */
}
if ((buf = malloc(size + 1)) == NULL)
if ((buf = (uschar *) malloc(size + 1)) == NULL)
FATAL("out of space in reg expr %.10s..", lastre);
memcpy(buf, basestr, prefix_length); /* copy prefix */
j = prefix_length;
@ -1065,7 +1067,7 @@ rescan:
rlxval = c;
return CHAR;
case '[':
if (buf == NULL && (buf = malloc(bufsz)) == NULL)
if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL)
FATAL("out of space in reg expr %.10s..", lastre);
bp = buf;
if (*prestr == '^') {

6
lex.c
View File

@ -173,7 +173,7 @@ int yylex(void)
static char *buf = NULL;
static int bufsize = 5; /* BUG: setting this small causes core dump! */
if (buf == NULL && (buf = malloc(bufsize)) == NULL)
if (buf == NULL && (buf = (char *) malloc(bufsize)) == NULL)
FATAL( "out of space in yylex" );
if (sc) {
sc = false;
@ -370,7 +370,7 @@ int string(void)
static char *buf = NULL;
static int bufsz = 500;
if (buf == NULL && (buf = malloc(bufsz)) == NULL)
if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of space for strings");
for (bp = buf; (c = input()) != '"'; ) {
if (!adjbuf(&buf, &bufsz, bp-buf+2, 500, &bp, "string"))
@ -519,7 +519,7 @@ int regexpr(void)
static int bufsz = 500;
char *bp;
if (buf == NULL && (buf = malloc(bufsz)) == NULL)
if (buf == NULL && (buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of space for rex expr");
bp = buf;
for ( ; (c = input()) != '/' && c != 0; ) {

18
lib.c
View File

@ -60,10 +60,10 @@ static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE, NULL, N
void recinit(unsigned int n)
{
if ( (record = malloc(n)) == NULL
|| (fields = malloc(n+1)) == NULL
|| (fldtab = calloc(nfields+2, sizeof(*fldtab))) == NULL
|| (fldtab[0] = malloc(sizeof(**fldtab))) == NULL)
if ( (record = (char *) malloc(n)) == NULL
|| (fields = (char *) malloc(n+1)) == NULL
|| (fldtab = (Cell **) calloc(nfields+2, sizeof(*fldtab))) == NULL
|| (fldtab[0] = (Cell *) malloc(sizeof(**fldtab))) == NULL)
FATAL("out of space for $0 and fields");
*record = '\0';
*fldtab[0] = dollar0;
@ -78,7 +78,7 @@ void makefields(int n1, int n2) /* create $n1..$n2 inclusive */
int i;
for (i = n1; i <= n2; i++) {
fldtab[i] = malloc(sizeof(**fldtab));
fldtab[i] = (Cell *) malloc(sizeof(**fldtab));
if (fldtab[i] == NULL)
FATAL("out of space in makefields %d", i);
*fldtab[i] = dollar1;
@ -127,7 +127,7 @@ void savefs(void)
}
len_inputFS = len + 1;
inputFS = realloc(inputFS, len_inputFS);
inputFS = (char *) realloc(inputFS, len_inputFS);
if (inputFS == NULL)
FATAL("field separator %.10s... is too long", *FS);
memcpy(inputFS, *FS, len_inputFS);
@ -325,7 +325,7 @@ void fldbld(void) /* create fields from current record */
n = strlen(r);
if (n > fieldssize) {
xfree(fields);
if ((fields = malloc(n+2)) == NULL) /* possibly 2 final \0s */
if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */
FATAL("out of space for fields in fldbld %d", n);
fieldssize = n;
}
@ -474,7 +474,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 == (size_t)nf) /* didn't overflow */
fldtab = realloc(fldtab, s);
fldtab = (Cell **) realloc(fldtab, s);
else /* overflow sizeof int */
xfree(fldtab); /* make it null */
if (fldtab == NULL)
@ -494,7 +494,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 = malloc(n+1)) == NULL)
if ((fields = (char *) malloc(n+1)) == NULL)
FATAL("out of space for fields in refldbld %d", n);
fieldssize = n;
}

4
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 20200816";
const char *version = "version 20201013";
#define DEBUG
#include <stdio.h>
@ -161,7 +161,7 @@ int main(int argc, char *argv[])
fn = getarg(&argc, &argv, "no program filename");
if (npfile >= maxpfile) {
maxpfile += 20;
pfile = realloc(pfile, maxpfile * sizeof(*pfile));
pfile = (char **) realloc(pfile, maxpfile * sizeof(*pfile));
if (pfile == NULL)
FATAL("error allocating space for -f options");
}

View File

@ -104,7 +104,7 @@ cleaner: testclean
# This is a bit of a band-aid until we can invest some more time
# in the test suite.
testclean:
cd testdir; rm -fr arnold-fixes beebe echo foo* \
cd testdir; rm -fr arnold-fixes beebe devnull echo foo* \
glop glop1 glop2 lilly.diff tempbig tempsmall time
# For the habits of GNU maintainers:

View File

@ -33,7 +33,7 @@ Node *nodealloc(int n)
{
Node *x;
x = malloc(sizeof(*x) + (n-1) * sizeof(x));
x = (Node *) malloc(sizeof(*x) + (n-1) * sizeof(x));
if (x == NULL)
FATAL("out of space in nodealloc");
x->nnext = NULL;

26
run.c
View File

@ -118,7 +118,7 @@ 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 = realloc(*pbuf, minlen);
tbuf = (char *) realloc(*pbuf, minlen);
DPRINTF("adjbuf %s: %d %d (pbuf=%p, tbuf=%p)\n", whatrtn, *psiz, minlen, (void*)*pbuf, (void*)tbuf);
if (tbuf == NULL) {
if (whatrtn)
@ -240,7 +240,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) {
frp = frame = calloc(nframe += 100, sizeof(*frame));
frp = frame = (struct 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 */
frp++; /* now ok to up frame */
if (frp >= frame + nframe) {
int dfp = frp - frame; /* old index */
frame = realloc(frame, (nframe += 100) * sizeof(*frame));
frame = (struct Frame *) realloc(frame, (nframe += 100) * sizeof(*frame));
if (frame == NULL)
FATAL("out of space for stack frames in %s", s);
frp = frame + dfp;
@ -408,7 +408,7 @@ Cell *awkgetline(Node **a, int n) /* get next line from specific input */
int mode;
bool newflag;
if ((buf = malloc(bufsize)) == NULL)
if ((buf = (char *) malloc(bufsize)) == NULL)
FATAL("out of memory in getline");
fflush(stdout); /* in case someone is waiting for a prompt */
@ -474,7 +474,7 @@ makearraystring(Node *p, const char *func)
int bufsz = recsize;
size_t blen;
if ((buf = malloc(bufsz)) == NULL) {
if ((buf = (char *) malloc(bufsz)) == NULL) {
FATAL("%s: out of memory", func);
}
@ -701,7 +701,7 @@ Cell *gettemp(void) /* get a tempcell */
Cell *x;
if (!tmps) {
tmps = calloc(100, sizeof(*tmps));
tmps = (Cell *) calloc(100, sizeof(*tmps));
if (!tmps)
FATAL("out of space for temporaries");
for (i = 1; i < 100; i++)
@ -839,7 +839,7 @@ int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like co
os = s;
p = buf;
if ((fmt = malloc(fmtsz)) == NULL)
if ((fmt = (char *) malloc(fmtsz)) == NULL)
FATAL("out of memory in format()");
while (*s) {
adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1");
@ -982,7 +982,7 @@ Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */
char *buf;
int bufsz=3*recsize;
if ((buf = malloc(bufsz)) == NULL)
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in awksprintf");
y = a[0]->nnext;
x = execute(a[0]);
@ -1005,7 +1005,7 @@ Cell *awkprintf(Node **a, int n) /* printf */
int len;
int bufsz=3*recsize;
if ((buf = malloc(bufsz)) == NULL)
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in awkprintf");
y = a[0]->nnext;
x = execute(a[0]);
@ -1772,7 +1772,7 @@ size_t nfiles;
static void stdinit(void) /* in case stdin, etc., are not constants */
{
nfiles = FOPEN_MAX;
files = calloc(nfiles, sizeof(*files));
files = (struct files *) calloc(nfiles, sizeof(*files));
if (files == NULL)
FATAL("can't allocate file memory for %zu files", nfiles);
files[0].fp = stdin;
@ -1812,7 +1812,7 @@ FILE *openfile(int a, const char *us, bool *pnewflag)
if (i >= nfiles) {
struct files *nf;
size_t nnf = nfiles + FOPEN_MAX;
nf = realloc(files, nnf * sizeof(*nf));
nf = (struct files *) realloc(files, nnf * sizeof(*nf));
if (nf == NULL)
FATAL("cannot grow files for %s and %zu files", s, nnf);
memset(&nf[nfiles], 0, FOPEN_MAX * sizeof(*nf));
@ -1933,7 +1933,7 @@ Cell *sub(Node **a, int nnn) /* substitute command */
fa *pfa;
int bufsz = recsize;
if ((buf = malloc(bufsz)) == NULL)
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in sub");
x = execute(a[3]); /* target string */
t = getsval(x);
@ -1995,7 +1995,7 @@ Cell *gsub(Node **a, int nnn) /* global substitute */
int mflag, tempstat, num;
int bufsz = recsize;
if ((buf = malloc(bufsz)) == NULL)
if ((buf = (char *) malloc(bufsz)) == NULL)
FATAL("out of memory in gsub");
mflag = 0; /* if mflag == 0, can replace empty string */
num = 0;

16
tran.c
View File

@ -166,8 +166,8 @@ Array *makesymtab(int n) /* make a new symbol table */
Array *ap;
Cell **tp;
ap = malloc(sizeof(*ap));
tp = calloc(n, sizeof(*tp));
ap = (Array *) malloc(sizeof(*ap));
tp = (Cell **) calloc(n, sizeof(*tp));
if (ap == NULL || tp == NULL)
FATAL("out of space in makesymtab");
ap->nelem = 0;
@ -237,7 +237,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 = malloc(sizeof(*p));
p = (Cell *) malloc(sizeof(*p));
if (p == NULL)
FATAL("out of space for symbol table at %s", n);
p->nval = tostring(n);
@ -272,7 +272,7 @@ void rehash(Array *tp) /* rehash items in small table into big one */
Cell *cp, *op, **np;
nsz = GROWTAB * tp->size;
np = calloc(nsz, sizeof(*np));
np = (Cell **) 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++) {
@ -519,7 +519,7 @@ char *tostringN(const char *s, size_t n) /* make a copy of string s */
{
char *p;
p = malloc(n);
p = (char *) malloc(n);
if (p == NULL)
FATAL("out of space in tostring on %s", s);
strcpy(p, s);
@ -533,13 +533,13 @@ Cell *catstr(Cell *a, Cell *b) /* concatenate a and b */
char *sa = getsval(a);
char *sb = getsval(b);
size_t l = strlen(sa) + strlen(sb) + 1;
p = malloc(l);
p = (char *) malloc(l);
if (p == NULL)
FATAL("out of space concatenating %s and %s", sa, sb);
snprintf(p, l, "%s%s", sa, sb);
l++; // add room for ' '
char *newbuf = malloc(l);
char *newbuf = (char *) malloc(l);
if (newbuf == NULL)
FATAL("out of space concatenating %s and %s", sa, sb);
// See string() in lex.c; a string "xx" is stored in the symbol
@ -558,7 +558,7 @@ char *qstring(const char *is, int delim) /* collect string up to next delim */
const uschar *s = (const uschar *) is;
uschar *buf, *bp;
if ((buf = malloc(strlen(is)+3)) == NULL)
if ((buf = (uschar *) malloc(strlen(is)+3)) == NULL)
FATAL( "out of space in qstring(%s)", s);
for (bp = buf; (c = *s) != delim; s++) {
if (c == '\n')