diff --git a/FIXES b/FIXES index 52f49e3eed0f..a043b356fafa 100644 --- a/FIXES +++ b/FIXES @@ -1,92 +1,108 @@ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ This file lists all bug fixes, changes, etc., made since the second edition of the AWK book was published in September 2023. +Jan 22, 2024: + Restore the ability to compile with g++. Thanks to + Arnold Robbins. + +Dec 24, 2023: + Matchop dereference after free problem fix when the first + argument is a function call. Thanks to Oguz Ismail Uysal. + Fix inconsistent handling of --csv and FS set in the + command line. Thanks to Wilbert van der Poel. + Casting changes to int for is* functions. + +Nov 27, 2023: + Fix exit status of system on MacOS. Update to REGRESS. + Thanks to Arnold Robbins. + Fix inconsistent handling of -F and --csv, and loss of csv + mode when FS is set. + Nov 24, 2023: Fix issue #199: gototab improvements to dynamically resize the table, qsort and bsearch to improve the lookup speed as the - table gets larger for multibyte input. thanks to Arnold Robbins. + table gets larger for multibyte input. Thanks to Arnold Robbins. Nov 23, 2023: Fix Issue #169, related to escape sequences in strings. Thanks to Github user rajeevvp. Fix Issue #147, reported by Github user drawkula, and fixed by Miguel Pineiro Jr. Nov 20, 2023: - rewrite of fnematch to fix a number of issues, including + Rewrite of fnematch to fix a number of issues, including extraneous output, out-of-bounds access, number of bytes to push back after a failed match etc. - thanks to Miguel Pineiro Jr. + Thanks to Miguel Pineiro Jr. Nov 15, 2023: - Man page edit, regression test fixes. thanks to Arnold Robbins - consolidation of sub and gsub into dosub, removing duplicate - code. thanks to Miguel Pineiro Jr. + Man page edit, regression test fixes. Thanks to Arnold Robbins + Consolidation of sub and gsub into dosub, removing duplicate + code. Thanks to Miguel Pineiro Jr. gcc replaced with cc everywhere. Oct 30, 2023: - multiple fixes and a minor code cleanup. - disabled utf-8 for non-multibyte locales, such as C or POSIX. - fixed a bad char * cast that causes incorrect results on big-endian - systems. also fixed an out-of-bounds read for empty CCL. - fixed a buffer overflow in substr with utf-8 strings. - many thanks to Todd C Miller. + Multiple fixes and a minor code cleanup. + Disabled utf-8 for non-multibyte locales, such as C or POSIX. + Fixed a bad char * cast that causes incorrect results on big-endian + systems. Also fixed an out-of-bounds read for empty CCL. + Fixed a buffer overflow in substr with utf-8 strings. + Many thanks to Todd C Miller. Sep 24, 2023: fnematch and getrune have been overhauled to solve issues around - unicode FS and RS. also fixed gsub null match issue with unicode. - big thanks to Arnold Robbins. + unicode FS and RS. Also fixed gsub null match issue with unicode. + Big thanks to Arnold Robbins. Sep 12, 2023: Fixed a length error in u8_byte2char that set RSTART to incorrect (cannot happen) value for EOL match(str, /$/). ----------------------------------------------------------------- [This entry is a summary, not a precise list of changes.] Added --csv option to enable processing of comma-separated values inputs. When --csv is enabled, fields are separated by commas, fields may be quoted with " double quotes, fields may contain embedded newlines. If no explicit separator argument is provided, split() uses the setting of --csv to determine how fields are split. Strings may now contain UTF-8 code points (not necessarily characters). Functions that operate on characters, like length, substr, index, match, etc., use UTF-8, so the length of a string of 3 emojis is 3, not 12 as it would be if bytes were counted. - Regular expressions are processes as UTF-8. + Regular expressions are processed as UTF-8. Unicode literals can be written as \u followed by one to eight hexadecimal digits. These may appear in strings and regular expressions. - diff --git a/README.md b/README.md index 84fb06e48833..a41fb3c3b128 100644 --- a/README.md +++ b/README.md @@ -1,146 +1,149 @@ # The One True Awk This is the version of `awk` described in _The AWK Programming Language_, Second Edition, by Al Aho, Brian Kernighan, and Peter Weinberger (Addison-Wesley, 2024, ISBN-13 978-0138269722, ISBN-10 0138269726). ## What's New? ## This version of Awk handles UTF-8 and comma-separated values (CSV) input. ### Strings ### Functions that process strings now count Unicode code points, not bytes; this affects `length`, `substr`, `index`, `match`, `split`, `sub`, `gsub`, and others. Note that code points are not necessarily characters. UTF-8 sequences may appear in literal strings and regular expressions. Aribtrary characters may be included with `\u` followed by 1 to 8 hexadecimal digits. ### Regular expressions ### Regular expressions may include UTF-8 code points, including `\u`. ### CSV ### The option `--csv` turns on CSV processing of input: fields are separated by commas, fields may be quoted with double-quote (`"`) characters, quoted fields may contain embedded newlines. +Double-quotes in fields have to be doubled and enclosed in quoted fields. In CSV mode, `FS` is ignored. If no explicit separator argument is provided, field-splitting in `split` is determined by CSV mode. ## Copyright Copyright (C) Lucent Technologies 1997
All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ## Distribution and Reporting Problems Changes, mostly bug fixes and occasional enhancements, are listed in `FIXES`. If you distribute this code further, please please please distribute `FIXES` with it. If you find errors, please report them to the current maintainer, ozan.yigit@gmail.com. Please _also_ open an issue in the GitHub issue tracker, to make it easy to track issues. Thanks. ## Submitting Pull Requests Pull requests are welcome. Some guidelines: * Please do not use functions or facilities that are not standard (e.g., `strlcpy()`, `fpurge()`). * Please run the test suite and make sure that your changes pass before posting the pull request. To do so: 1. Save the previous version of `awk` somewhere in your path. Call it `nawk` (for example). 1. Run `oldawk=nawk make check > check.out 2>&1`. 1. Search for `BAD` or `error` in the result. In general, look over it manually to make sure there are no errors. * Please create the pull request with a request to merge into the `staging` branch instead of into the `master` branch. This allows us to do testing, and to make any additional edits or changes after the merge but before merging to `master`. ## Building The program itself is created by make which should produce a sequence of messages roughly like this: bison -d awkgram.y awkgram.y: warning: 44 shift/reduce conflicts [-Wconflicts-sr] awkgram.y: warning: 85 reduce/reduce conflicts [-Wconflicts-rr] awkgram.y: note: rerun with option '-Wcounterexamples' to generate conflict counterexamples gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o awkgram.tab.o awkgram.tab.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o b.o b.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o main.o main.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o parse.o parse.c gcc -g -Wall -pedantic -Wcast-qual -O2 maketab.c -o maketab ./maketab awkgram.tab.h >proctab.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o proctab.o proctab.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o tran.o tran.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o lib.o lib.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o run.o run.c gcc -g -Wall -pedantic -Wcast-qual -O2 -c -o lex.o lex.c gcc -g -Wall -pedantic -Wcast-qual -O2 awkgram.tab.o b.o main.o parse.o proctab.o tran.o lib.o run.o lex.o -lm This produces an executable `a.out`; you will eventually want to move this to some place like `/usr/bin/awk`. If your system does not have `yacc` or `bison` (the GNU equivalent), you need to install one of them first. +The default in the `makefile` is `bison`; you will have +to edit the `makefile` to use `yacc`. NOTE: This version uses ISO/IEC C99, as you should also. We have compiled this without any changes using `gcc -Wall` and/or local C compilers on a variety of systems, but new systems or compilers may raise some new complaint; reports of difficulties are 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. ## A Note About Releases We don't usually do releases. ## A Note About Maintenance NOTICE! Maintenance of this program is on a ''best effort'' basis. We try to get to issues and pull requests as quickly as we can. Unfortunately, however, keeping this program going is not at the top of our priority list. #### Last Updated -Mon 16 Oct 2023 11:23:08 IDT +Mon 05 Feb 2024 08:46:55 IST diff --git a/b.c b/b.c index 881c052811b1..4c438fab4cd4 100644 --- a/b.c +++ b/b.c @@ -1,1564 +1,1562 @@ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ /* lasciate ogne speranza, voi ch'intrate. */ #define DEBUG #include #include #include #include #include #include "awk.h" #include "awkgram.tab.h" #define MAXLIN 22 #define type(v) (v)->nobj /* badly overloaded here */ #define info(v) (v)->ntype /* badly overloaded here */ #define left(v) (v)->narg[0] #define right(v) (v)->narg[1] #define parent(v) (v)->nnext #define LEAF case CCL: case NCCL: case CHAR: case DOT: case FINAL: case ALL: #define ELEAF case EMPTYRE: /* empty string in regexp */ #define UNARY case STAR: case PLUS: case QUEST: /* encoding in tree Nodes: leaf (CCL, NCCL, CHAR, DOT, FINAL, ALL, EMPTYRE): left is index, right contains value or pointer to value unary (STAR, PLUS, QUEST): left is child, right is null binary (CAT, OR): left and right are children parent contains pointer to parent */ int *setvec; int *tmpset; int maxsetvec = 0; int rtok; /* next token in current re */ int rlxval; 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 const uschar *firstbasestr; static int setcnt; static int poscnt; const char *patbeg; int patlen; #define NFA 128 /* cache this many dynamic fa's */ fa *fatab[NFA]; int nfatab = 0; /* entries in fatab */ extern int u8_nextlen(const char *s); /* utf-8 mechanism: For most of Awk, utf-8 strings just "work", since they look like null-terminated sequences of 8-bit bytes. Functions like length(), index(), and substr() have to operate in units of utf-8 characters. The u8_* functions in run.c handle this. Regular expressions are more complicated, since the basic mechanism of the goto table used 8-bit byte indices into the gototab entries to compute the next state. Unicode is a lot bigger, so the gototab entries are now structs with a character and a next state. These are sorted by code point and binary searched. Throughout the RE mechanism in b.c, utf-8 characters are converted to their utf-32 value. This mostly shows up in cclenter, which expands character class ranges like a-z and now alpha-omega. The size of a gototab array is still about 256. This should be dynamic, but for now things work ok for a single code page of Unicode, which is the most likely case. The code changes are localized in run.c and b.c. I have added a handful of functions to somewhat better hide the implementation, but a lot more could be done. */ static int entry_cmp(const void *l, const void *r); static int get_gototab(fa*, int, int); static int set_gototab(fa*, int, int, int); static void clear_gototab(fa*, int); -extern int u8_rune(int *, const uschar *); +extern int u8_rune(int *, const char *); static int * intalloc(size_t n, const char *f) { int *p = (int *) calloc(n, sizeof(int)); if (p == NULL) overflo(f); return p; } static void resizesetvec(const char *f) { if (maxsetvec == 0) maxsetvec = MAXLIN; else maxsetvec *= 4; setvec = (int *) realloc(setvec, maxsetvec * sizeof(*setvec)); tmpset = (int *) realloc(tmpset, maxsetvec * sizeof(*tmpset)); if (setvec == NULL || tmpset == NULL) overflo(f); } static void resize_state(fa *f, int state) { gtt *p; uschar *p2; int **p3; int i, new_count; if (++state < f->state_count) return; new_count = state + 10; /* needs to be tuned */ p = (gtt *) realloc(f->gototab, new_count * sizeof(gtt)); if (p == NULL) goto out; f->gototab = p; p2 = (uschar *) realloc(f->out, new_count * sizeof(f->out[0])); if (p2 == NULL) goto out; f->out = p2; p3 = (int **) realloc(f->posns, new_count * sizeof(f->posns[0])); if (p3 == NULL) goto out; f->posns = p3; for (i = f->state_count; i < new_count; ++i) { f->gototab[i].entries = (gtte *) calloc(NCHARS, sizeof(gtte)); if (f->gototab[i].entries == NULL) goto out; f->gototab[i].allocated = NCHARS; f->gototab[i].inuse = 0; f->out[i] = 0; f->posns[i] = NULL; } f->state_count = new_count; return; out: overflo(__func__); } fa *makedfa(const char *s, bool anchor) /* returns dfa for reg expr s */ { int i, use, nuse; fa *pfa; static int now = 1; if (setvec == NULL) { /* first time through any RE */ resizesetvec(__func__); } 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 && strcmp((const char *) fatab[i]->restr, s) == 0) { fatab[i]->use = now++; return fatab[i]; } pfa = mkdfa(s, anchor); if (nfatab < NFA) { /* room for another */ fatab[nfatab] = pfa; fatab[nfatab]->use = now++; nfatab++; return pfa; } use = fatab[0]->use; /* replace least-recently used */ nuse = 0; for (i = 1; i < nfatab; i++) if (fatab[i]->use < use) { use = fatab[i]->use; nuse = i; } freefa(fatab[nuse]); fatab[nuse] = pfa; pfa->use = now++; return pfa; } fa *mkdfa(const char *s, bool anchor) /* does the real work of making a dfa */ /* anchor = true for anchored matches, else false */ { Node *p, *p1; fa *f; firstbasestr = (const uschar *) s; basestr = firstbasestr; p = reparse(s); p1 = op2(CAT, op2(STAR, op2(ALL, NIL, NIL), NIL), p); /* put ALL STAR in front of reg. exp. */ p1 = op2(CAT, p1, op2(FINAL, NIL, NIL)); /* put FINAL after reg. exp. */ poscnt = 0; penter(p1); /* enter parent pointers and leaf indices */ 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 */ freetr(p1); resize_state(f, 1); f->posns[0] = intalloc(*(f->re[0].lfollow), __func__); f->posns[1] = intalloc(1, __func__); *f->posns[1] = 0; f->initstat = makeinit(f, anchor); f->anchor = anchor; f->restr = (uschar *) tostring(s); if (firstbasestr != basestr) { if (basestr) xfree(basestr); } return f; } int makeinit(fa *f, bool anchor) { int i, k; f->curstat = 2; f->out[2] = 0; k = *(f->re[0].lfollow); xfree(f->posns[2]); f->posns[2] = intalloc(k + 1, __func__); for (i = 0; i <= k; i++) { (f->posns[2])[i] = (f->re[0].lfollow)[i]; } if ((f->posns[2])[1] == f->accept) f->out[2] = 1; clear_gototab(f, 2); f->curstat = cgoto(f, 2, HAT); if (anchor) { *f->posns[2] = k-1; /* leave out position 0 */ for (i = 0; i < k; i++) { (f->posns[0])[i] = (f->posns[2])[i]; } f->out[0] = f->out[2]; if (f->curstat != 2) --(*f->posns[f->curstat]); } return f->curstat; } void penter(Node *p) /* set up parent pointers and leaf indices */ { switch (type(p)) { ELEAF LEAF info(p) = poscnt; poscnt++; break; UNARY penter(left(p)); parent(left(p)) = p; break; case CAT: case OR: penter(left(p)); penter(right(p)); parent(left(p)) = p; parent(right(p)) = p; break; case ZERO: break; default: /* can't happen */ FATAL("can't happen: unknown type %d in penter", type(p)); break; } } void freetr(Node *p) /* free parse tree */ { switch (type(p)) { ELEAF LEAF xfree(p); break; UNARY case ZERO: freetr(left(p)); xfree(p); break; case CAT: case OR: freetr(left(p)); freetr(right(p)); xfree(p); break; default: /* can't happen */ FATAL("can't happen: unknown type %d in freetr", type(p)); break; } } /* in the parsing of regular expressions, metacharacters like . have */ /* to be seen literally; \056 is not a metacharacter. */ int hexstr(const uschar **pp, int max) /* find and eval hex string at pp, return new p */ { /* only pick up one 8-bit byte (2 chars) */ const uschar *p; int n = 0; int i; for (i = 0, p = *pp; i < max && isxdigit(*p); i++, p++) { - if (isdigit(*p)) + if (isdigit((int) *p)) n = 16 * n + *p - '0'; else if (*p >= 'a' && *p <= 'f') n = 16 * n + *p - 'a' + 10; else if (*p >= 'A' && *p <= 'F') n = 16 * n + *p - 'A' + 10; } *pp = p; return n; } #define isoctdigit(c) ((c) >= '0' && (c) <= '7') /* multiple use of arg */ int quoted(const uschar **pp) /* pick up next thing after a \\ */ /* and increment *pp */ { const uschar *p = *pp; int c; /* BUG: should advance by utf-8 char even if makes no sense */ if ((c = *p++) == 't') { c = '\t'; } else if (c == 'n') { c = '\n'; } else if (c == 'f') { c = '\f'; } else if (c == 'r') { c = '\r'; } else if (c == 'b') { c = '\b'; } else if (c == 'v') { c = '\v'; } else if (c == 'a') { c = '\a'; } else if (c == '\\') { c = '\\'; } else if (c == 'x') { /* 2 hex digits follow */ c = hexstr(&p, 2); /* this adds a null if number is invalid */ } else if (c == 'u') { /* unicode char number up to 8 hex digits */ c = hexstr(&p, 8); } else if (isoctdigit(c)) { /* \d \dd \ddd */ int n = c - '0'; if (isoctdigit(*p)) { n = 8 * n + *p++ - '0'; if (isoctdigit(*p)) n = 8 * n + *p++ - '0'; } c = n; } /* else */ /* c = c; */ *pp = p; return c; } int *cclenter(const char *argp) /* add a character class */ { int i, c, c2; int n; const uschar *p = (const uschar *) argp; int *bp, *retp; static int *buf = NULL; static int bufsz = 100; if (buf == NULL && (buf = (int *) calloc(bufsz, sizeof(int))) == NULL) FATAL("out of space for character class [%.10s...] 1", p); bp = buf; for (i = 0; *p != 0; ) { - n = u8_rune(&c, p); + n = u8_rune(&c, (const char *) p); p += n; if (c == '\\') { c = quoted(&p); } else if (c == '-' && i > 0 && bp[-1] != 0) { if (*p != 0) { c = bp[-1]; /* c2 = *p++; */ - n = u8_rune(&c2, p); + n = u8_rune(&c2, (const char *) p); p += n; if (c2 == '\\') c2 = quoted(&p); /* BUG: sets p, has to be u8 size */ if (c > c2) { /* empty; ignore */ bp--; i--; continue; } while (c < c2) { if (i >= bufsz) { bufsz *= 2; buf = (int *) realloc(buf, bufsz * sizeof(int)); if (buf == NULL) FATAL("out of space for character class [%.10s...] 2", p); bp = buf + i; } *bp++ = ++c; i++; } continue; } } if (i >= bufsz) { bufsz *= 2; buf = (int *) realloc(buf, bufsz * sizeof(int)); if (buf == NULL) FATAL("out of space for character class [%.10s...] 2", p); bp = buf + i; } *bp++ = c; i++; } *bp = 0; /* DPRINTF("cclenter: in = |%s|, out = |%s|\n", op, buf); BUG: can't print array of int */ /* xfree(op); BUG: what are we freeing here? */ retp = (int *) calloc(bp-buf+1, sizeof(int)); for (i = 0; i < bp-buf+1; i++) retp[i] = buf[i]; return retp; } void overflo(const char *s) { FATAL("regular expression too big: out of space in %.30s...", s); } void cfoll(fa *f, Node *v) /* enter follow set of each leaf of vertex v into lfollow[leaf] */ { int i; int *p; switch (type(v)) { ELEAF LEAF f->re[info(v)].ltype = type(v); f->re[info(v)].lval.np = right(v); while (f->accept >= maxsetvec) { /* guessing here! */ resizesetvec(__func__); } for (i = 0; i <= f->accept; i++) setvec[i] = 0; setcnt = 0; follow(v); /* computes setvec and setcnt */ p = intalloc(setcnt + 1, __func__); f->re[info(v)].lfollow = p; *p = setcnt; for (i = f->accept; i >= 0; i--) if (setvec[i] == 1) *++p = i; break; UNARY cfoll(f,left(v)); break; case CAT: case OR: cfoll(f,left(v)); cfoll(f,right(v)); break; case ZERO: break; default: /* can't happen */ FATAL("can't happen: unknown type %d in cfoll", type(v)); } } int first(Node *p) /* collects initially active leaves of p into setvec */ /* returns 0 if p matches empty string */ { int b, lp; switch (type(p)) { ELEAF LEAF lp = info(p); /* look for high-water mark of subscripts */ while (setcnt >= maxsetvec || lp >= maxsetvec) { /* guessing here! */ resizesetvec(__func__); } if (type(p) == EMPTYRE) { setvec[lp] = 0; return(0); } if (setvec[lp] != 1) { setvec[lp] = 1; setcnt++; } if (type(p) == CCL && (*(int *) right(p)) == 0) return(0); /* empty CCL */ return(1); case PLUS: if (first(left(p)) == 0) return(0); return(1); case STAR: case QUEST: first(left(p)); return(0); case CAT: if (first(left(p)) == 0 && first(right(p)) == 0) return(0); return(1); case OR: b = first(right(p)); if (first(left(p)) == 0 || b == 0) return(0); return(1); case ZERO: return 0; } FATAL("can't happen: unknown type %d in first", type(p)); /* can't happen */ return(-1); } void follow(Node *v) /* collects leaves that can follow v into setvec */ { Node *p; if (type(v) == FINAL) return; p = parent(v); switch (type(p)) { case STAR: case PLUS: first(v); follow(p); return; case OR: case QUEST: follow(p); return; case CAT: if (v == left(p)) { /* v is left child of p */ if (first(right(p)) == 0) { follow(p); return; } } else /* v is right child */ follow(p); return; } } int member(int c, int *sarg) /* is c in s? */ { int *s = (int *) sarg; while (*s) if (c == *s++) return(1); return(0); } static void resize_gototab(fa *f, int state) { size_t new_size = f->gototab[state].allocated * 2; gtte *p = (gtte *) realloc(f->gototab[state].entries, new_size * sizeof(gtte)); if (p == NULL) overflo(__func__); // need to initialized the new memory to zero size_t orig_size = f->gototab[state].allocated; // 2nd half of new mem is this size memset(p + orig_size, 0, orig_size * sizeof(gtte)); // clean it out - f->gototab[state].allocated = new_size; // update gotottab info + f->gototab[state].allocated = new_size; // update gototab info f->gototab[state].entries = p; } -static int get_gototab(fa *f, int state, int ch) /* hide gototab inplementation */ +static int get_gototab(fa *f, int state, int ch) /* hide gototab implementation */ { gtte key; gtte *item; key.ch = ch; key.state = 0; /* irrelevant */ - item = bsearch(& key, f->gototab[state].entries, + item = (gtte *) bsearch(& key, f->gototab[state].entries, f->gototab[state].inuse, sizeof(gtte), entry_cmp); if (item == NULL) return 0; else return item->state; } static int entry_cmp(const void *l, const void *r) { const gtte *left, *right; left = (const gtte *) l; right = (const gtte *) r; return left->ch - right->ch; } -static int set_gototab(fa *f, int state, int ch, int val) /* hide gototab inplementation */ +static int set_gototab(fa *f, int state, int ch, int val) /* hide gototab implementation */ { if (f->gototab[state].inuse == 0) { f->gototab[state].entries[0].ch = ch; f->gototab[state].entries[0].state = val; f->gototab[state].inuse++; return val; } else if (ch > f->gototab[state].entries[f->gototab[state].inuse-1].ch) { // not seen yet, insert and return gtt *tab = & f->gototab[state]; if (tab->inuse + 1 >= tab->allocated) resize_gototab(f, state); f->gototab[state].entries[f->gototab[state].inuse-1].ch = ch; f->gototab[state].entries[f->gototab[state].inuse-1].state = val; f->gototab[state].inuse++; return val; } else { // maybe we have it, maybe we don't gtte key; gtte *item; key.ch = ch; key.state = 0; /* irrelevant */ - item = bsearch(& key, f->gototab[state].entries, + item = (gtte *) bsearch(& key, f->gototab[state].entries, f->gototab[state].inuse, sizeof(gtte), entry_cmp); if (item != NULL) { // we have it, update state and return item->state = val; return item->state; } // otherwise, fall through to insert and reallocate. } gtt *tab = & f->gototab[state]; if (tab->inuse + 1 >= tab->allocated) resize_gototab(f, state); ++tab->inuse; f->gototab[state].entries[tab->inuse].ch = ch; f->gototab[state].entries[tab->inuse].state = val; qsort(f->gototab[state].entries, f->gototab[state].inuse, sizeof(gtte), entry_cmp); return val; /* not used anywhere at the moment */ } static void clear_gototab(fa *f, int state) { memset(f->gototab[state].entries, 0, f->gototab[state].allocated * sizeof(gtte)); f->gototab[state].inuse = 0; } int match(fa *f, const char *p0) /* shortest match ? */ { int s, ns; int n; int rune; const uschar *p = (const uschar *) p0; /* return pmatch(f, p0); does it matter whether longest or shortest? */ s = f->initstat; assert (s < f->state_count); if (f->out[s]) return(1); do { /* assert(*p < NCHARS); */ - n = u8_rune(&rune, p); + n = u8_rune(&rune, (const char *) p); if ((ns = get_gototab(f, s, rune)) != 0) s = ns; else s = cgoto(f, s, rune); if (f->out[s]) return(1); if (*p == 0) break; p += n; } while (1); /* was *p++ != 0 */ return(0); } int pmatch(fa *f, const char *p0) /* longest match, for sub */ { int s, ns; int n; int rune; const uschar *p = (const uschar *) p0; const uschar *q; s = f->initstat; assert(s < f->state_count); patbeg = (const char *)p; patlen = -1; do { q = p; do { if (f->out[s]) /* final state */ patlen = q-p; /* assert(*q < NCHARS); */ - n = u8_rune(&rune, q); + n = u8_rune(&rune, (const char *) q); if ((ns = get_gototab(f, s, rune)) != 0) s = ns; else s = cgoto(f, s, rune); assert(s < f->state_count); if (s == 1) { /* no transition */ if (patlen >= 0) { patbeg = (const char *) p; return(1); } else goto nextin; /* no match */ } if (*q == 0) break; q += n; } while (1); q++; /* was *q++ */ if (f->out[s]) patlen = q-p-1; /* don't count $ */ if (patlen >= 0) { patbeg = (const char *) p; return(1); } nextin: s = 2; if (*p == 0) break; - n = u8_rune(&rune, p); + n = u8_rune(&rune, (const char *) p); p += n; } while (1); /* was *p++ */ return (0); } int nematch(fa *f, const char *p0) /* non-empty match, for sub */ { int s, ns; int n; int rune; const uschar *p = (const uschar *) p0; const uschar *q; s = f->initstat; assert(s < f->state_count); patbeg = (const char *)p; patlen = -1; while (*p) { q = p; do { if (f->out[s]) /* final state */ patlen = q-p; /* assert(*q < NCHARS); */ - n = u8_rune(&rune, q); + n = u8_rune(&rune, (const char *) q); if ((ns = get_gototab(f, s, rune)) != 0) s = ns; else s = cgoto(f, s, rune); if (s == 1) { /* no transition */ if (patlen > 0) { patbeg = (const char *) p; return(1); } else goto nnextin; /* no nonempty match */ } if (*q == 0) break; q += n; } while (1); q++; if (f->out[s]) patlen = q-p-1; /* don't count $ */ if (patlen > 0 ) { patbeg = (const char *) p; return(1); } nnextin: s = 2; p++; } return (0); } #define MAX_UTF_BYTES 4 // UTF-8 is up to 4 bytes long /* * NAME * fnematch * * DESCRIPTION * A stream-fed version of nematch which transfers characters to a * null-terminated buffer. All characters up to and including the last * character of the matching text or EOF are placed in the buffer. If * a match is found, patbeg and patlen are set appropriately. * * RETURN VALUES * false No match found. * true Match found. */ bool fnematch(fa *pfa, FILE *f, char **pbuf, int *pbufsize, int quantum) { char *i, *j, *k, *buf = *pbuf; int bufsize = *pbufsize; int c, n, ns, s; s = pfa->initstat; patlen = 0; /* * buf <= i <= j <= k <= buf+bufsize * * i: origin of active substring * j: current character * k: destination of the next getc */ i = j = k = buf; do { /* * Call u8_rune with at least MAX_UTF_BYTES ahead in * the buffer until EOF interferes. */ if (k - j < MAX_UTF_BYTES) { if (k + MAX_UTF_BYTES > buf + bufsize) { adjbuf((char **) &buf, &bufsize, bufsize + MAX_UTF_BYTES, quantum, 0, "fnematch"); } for (n = MAX_UTF_BYTES ; n > 0; n--) { *k++ = (c = getc(f)) != EOF ? c : 0; if (c == EOF) { if (ferror(f)) FATAL("fnematch: getc error"); break; } } } - j += u8_rune(&c, (uschar *)j); + j += u8_rune(&c, j); if ((ns = get_gototab(pfa, s, c)) != 0) s = ns; else s = cgoto(pfa, s, c); if (pfa->out[s]) { /* final state */ patbeg = i; patlen = j - i; if (c == 0) /* don't count $ */ patlen--; } if (c && s != 1) continue; /* origin i still viable, next j */ if (patlen) break; /* best match found */ /* no match at origin i, next i and start over */ - i += u8_rune(&c, (uschar *)i); + i += u8_rune(&c, i); if (c == 0) break; /* no match */ j = i; s = 2; } while (1); /* adjbuf() may have relocated a resized buffer. Inform the world. */ *pbuf = buf; *pbufsize = bufsize; if (patlen) { /* * Under no circumstances is the last character fed to * the automaton part of the match. It is EOF's nullbyte, * or it sent the automaton into a state with no further * transitions available (s==1), or both. Room for a * terminating nullbyte is guaranteed. * * ungetc any chars after the end of matching text * (except for EOF's nullbyte, if present) and null * terminate the buffer. */ do if (*--k && ungetc(*k, f) == EOF) FATAL("unable to ungetc '%c'", *k); while (k > patbeg + patlen); *k = '\0'; return true; } else return false; } Node *reparse(const char *p) /* parses regular expression pointed to by p */ { /* uses relex() to scan regular expression */ Node *np; DPRINTF("reparse <%s>\n", p); lastre = prestr = (const uschar *) p; /* prestr points to string to be parsed */ rtok = relex(); /* GNU compatibility: an empty regexp matches anything */ if (rtok == '\0') { /* FATAL("empty regular expression"); previous */ return(op2(EMPTYRE, NIL, NIL)); } np = regexp(); if (rtok != '\0') FATAL("syntax error in regular expression %s at %s", lastre, prestr); return(np); } Node *regexp(void) /* top-level parse of reg expr */ { return (alt(concat(primary()))); } Node *primary(void) { Node *np; int savelastatom; switch (rtok) { case CHAR: lastatom = starttok; np = op2(CHAR, NIL, itonp(rlxval)); rtok = relex(); return (unary(np)); case ALL: rtok = relex(); return (unary(op2(ALL, NIL, NIL))); case EMPTYRE: rtok = relex(); return (unary(op2(EMPTYRE, NIL, NIL))); case DOT: lastatom = starttok; rtok = relex(); return (unary(op2(DOT, NIL, NIL))); case CCL: np = op2(CCL, NIL, (Node*) cclenter((const char *) rlxstr)); lastatom = starttok; rtok = relex(); return (unary(np)); case NCCL: np = op2(NCCL, NIL, (Node *) cclenter((const char *) rlxstr)); lastatom = starttok; rtok = relex(); return (unary(np)); case '^': rtok = relex(); return (unary(op2(CHAR, NIL, itonp(HAT)))); case '$': rtok = relex(); return (unary(op2(CHAR, NIL, NIL))); case '(': lastatom = starttok; savelastatom = starttok - basestr; /* Retain over recursion */ rtok = relex(); if (rtok == ')') { /* special pleading for () */ rtok = relex(); return unary(op2(CCL, NIL, (Node *) cclenter(""))); } np = regexp(); if (rtok == ')') { lastatom = basestr + savelastatom; /* Restore */ rtok = relex(); return (unary(np)); } else FATAL("syntax error in regular expression %s at %s", lastre, prestr); default: FATAL("illegal primary in regular expression %s at %s", lastre, prestr); } return 0; /*NOTREACHED*/ } Node *concat(Node *np) { switch (rtok) { case CHAR: case DOT: case ALL: case CCL: case NCCL: case '$': case '(': return (concat(op2(CAT, np, primary()))); case EMPTYRE: rtok = relex(); return (concat(op2(CAT, op2(CCL, NIL, (Node *) cclenter("")), primary()))); } return (np); } Node *alt(Node *np) { if (rtok == OR) { rtok = relex(); return (alt(op2(OR, np, concat(primary())))); } return (np); } Node *unary(Node *np) { switch (rtok) { case STAR: rtok = relex(); return (unary(op2(STAR, np, NIL))); case PLUS: rtok = relex(); return (unary(op2(PLUS, np, NIL))); case QUEST: rtok = relex(); return (unary(op2(QUEST, np, NIL))); case ZERO: rtok = relex(); return (unary(op2(ZERO, np, NIL))); default: return (np); } } /* * Character class definitions conformant to the POSIX locale as * defined in IEEE P1003.1 draft 7 of June 2001, assuming the source * and operating character sets are both ASCII (ISO646) or supersets * thereof. * * Note that to avoid overflowing the temporary buffer used in * relex(), the expanded character class (prior to range expansion) * must be less than twice the size of their full name. */ /* Because isblank doesn't show up in any of the header files on any * system i use, it's defined here. if some other locale has a richer * definition of "blank", define HAS_ISBLANK and provide your own * version. * the parentheses here are an attempt to find a path through the maze * of macro definition and/or function and/or version provided. thanks * to nelson beebe for the suggestion; let's see if it works everywhere. */ /* #define HAS_ISBLANK */ #ifndef HAS_ISBLANK int (xisblank)(int c) { return c==' ' || c=='\t'; } #endif static const struct charclass { const char *cc_name; int cc_namelen; int (*cc_func)(int); } charclasses[] = { { "alnum", 5, isalnum }, { "alpha", 5, isalpha }, #ifndef HAS_ISBLANK { "blank", 5, xisblank }, #else { "blank", 5, isblank }, #endif { "cntrl", 5, iscntrl }, { "digit", 5, isdigit }, { "graph", 5, isgraph }, { "lower", 5, islower }, { "print", 5, isprint }, { "punct", 5, ispunct }, { "space", 5, isspace }, { "upper", 5, isupper }, { "xdigit", 6, isxdigit }, { NULL, 0, NULL }, }; #define REPEAT_SIMPLE 0 #define REPEAT_PLUS_APPENDED 1 #define REPEAT_WITH_Q 2 #define REPEAT_ZERO 3 static int replace_repeat(const uschar *reptok, int reptoklen, const uschar *atom, int atomlen, int firstnum, int secondnum, int special_case) { int i, j; uschar *buf = 0; int ret = 1; 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 */ int size = prefix_length + suffix_length; if (firstnum > 1) { /* add room for reps 2 through firstnum */ size += atomlen*(firstnum-1); } /* Adjust size of buffer for special cases */ if (special_case == REPEAT_PLUS_APPENDED) { size++; /* for the final + */ } else if (special_case == REPEAT_WITH_Q) { size += init_q + (atomlen+1)* (n_q_reps-init_q); } else if (special_case == REPEAT_ZERO) { size += 2; /* just a null ERE: () */ } 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; if (special_case == REPEAT_ZERO) { j -= atomlen; buf[j++] = '('; buf[j++] = ')'; } for (i = 1; i < firstnum; i++) { /* copy x reps */ memcpy(&buf[j], atom, atomlen); j += atomlen; } if (special_case == REPEAT_PLUS_APPENDED) { buf[j++] = '+'; } else if (special_case == REPEAT_WITH_Q) { if (init_q) buf[j++] = '?'; for (i = init_q; i < n_q_reps; i++) { /* copy x? reps */ memcpy(&buf[j], atom, atomlen); j += atomlen; buf[j++] = '?'; } } memcpy(&buf[j], reptok+reptoklen, suffix_length); j += suffix_length; buf[j] = '\0'; /* free old basestr */ if (firstbasestr != basestr) { if (basestr) xfree(basestr); } basestr = buf; prestr = buf + prefix_length; if (special_case == REPEAT_ZERO) { prestr -= atomlen; ret++; } return ret; } static int repeat(const uschar *reptok, int reptoklen, const uschar *atom, int atomlen, int firstnum, int secondnum) { /* In general, the repetition specifier or "bound" is replaced here by an equivalent ERE string, repeating the immediately previous atom and appending ? and + as needed. Note that the first copy of the atom is left in place, except in the special_case of a zero-repeat (i.e., {0}). */ if (secondnum < 0) { /* means {n,} -> repeat n-1 times followed by PLUS */ if (firstnum < 2) { /* 0 or 1: should be handled before you get here */ FATAL("internal error"); } else { return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_PLUS_APPENDED); } } else if (firstnum == secondnum) { /* {n} or {n,n} -> simply repeat n-1 times */ if (firstnum == 0) { /* {0} or {0,0} */ /* This case is unusual because the resulting replacement string might actually be SMALLER than the original ERE */ return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_ZERO); } else { /* (firstnum >= 1) */ return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_SIMPLE); } } else if (firstnum < secondnum) { /* {n,m} -> repeat n-1 times then alternate */ /* x{n,m} => xx...x{1, m-n+1} => xx...x?x?x?..x? */ return replace_repeat(reptok, reptoklen, atom, atomlen, firstnum, secondnum, REPEAT_WITH_Q); } else { /* Error - shouldn't be here (n>m) */ FATAL("internal error"); } return 0; } -extern int u8_rune(int *, const uschar *); /* run.c; should be in header file */ - int relex(void) /* lexical analyzer for reparse */ { int c, n; int cflag; static uschar *buf = NULL; static int bufsz = 100; uschar *bp; const struct charclass *cc; int i; int num, m; bool commafound, digitfound; const uschar *startreptok; static int parens = 0; rescan: starttok = prestr; - if ((n = u8_rune(&rlxval, prestr)) > 1) { + if ((n = u8_rune(&rlxval, (const char *) prestr)) > 1) { prestr += n; starttok = prestr; return CHAR; } switch (c = *prestr++) { case '|': return OR; case '*': return STAR; case '+': return PLUS; case '?': return QUEST; case '.': return DOT; case '\0': prestr--; return '\0'; case '^': case '$': return c; case '(': parens++; return c; case ')': if (parens) { parens--; return c; } /* unmatched close parenthesis; per POSIX, treat as literal */ rlxval = c; return CHAR; case '\\': rlxval = quoted(&prestr); return CHAR; default: rlxval = c; return CHAR; case '[': if (buf == NULL && (buf = (uschar *) malloc(bufsz)) == NULL) FATAL("out of space in reg expr %.10s..", lastre); bp = buf; if (*prestr == '^') { cflag = 1; prestr++; } else cflag = 0; n = 5 * strlen((const char *) prestr)+1; /* BUG: was 2. what value? */ if (!adjbuf((char **) &buf, &bufsz, n, n, (char **) &bp, "relex1")) FATAL("out of space for reg expr %.10s...", lastre); for (; ; ) { - if ((n = u8_rune(&rlxval, prestr)) > 1) { + if ((n = u8_rune(&rlxval, (const char *) prestr)) > 1) { for (i = 0; i < n; i++) *bp++ = *prestr++; continue; } if ((c = *prestr++) == '\\') { *bp++ = '\\'; if ((c = *prestr++) == '\0') FATAL("nonterminated character class %.20s...", lastre); *bp++ = c; /* } else if (c == '\n') { */ /* FATAL("newline in character class %.20s...", lastre); */ } else if (c == '[' && *prestr == ':') { /* POSIX char class names, Dag-Erling Smorgrav, des@ofug.org */ for (cc = charclasses; cc->cc_name; cc++) if (strncmp((const char *) prestr + 1, (const char *) cc->cc_name, cc->cc_namelen) == 0) break; if (cc->cc_name != NULL && prestr[1 + cc->cc_namelen] == ':' && prestr[2 + cc->cc_namelen] == ']') { prestr += cc->cc_namelen + 3; /* * BUG: We begin at 1, instead of 0, since we * would otherwise prematurely terminate the * string for classes like [[:cntrl:]]. This * means that we can't match the NUL character, * not without first adapting the entire * program to track each string's length. */ for (i = 1; i <= UCHAR_MAX; i++) { if (!adjbuf((char **) &buf, &bufsz, bp-buf+2, 100, (char **) &bp, "relex2")) FATAL("out of space for reg expr %.10s...", lastre); if (cc->cc_func(i)) { /* escape backslash */ if (i == '\\') { *bp++ = '\\'; n++; } *bp++ = i; n++; } } } else *bp++ = c; } else if (c == '[' && *prestr == '.') { char collate_char; prestr++; collate_char = *prestr++; if (*prestr == '.' && prestr[1] == ']') { prestr += 2; /* Found it: map via locale TBD: for now, simply return this char. This is sufficient to pass conformance test awk.ex 156 */ if (*prestr == ']') { prestr++; rlxval = collate_char; return CHAR; } } } else if (c == '[' && *prestr == '=') { char equiv_char; prestr++; equiv_char = *prestr++; if (*prestr == '=' && prestr[1] == ']') { prestr += 2; /* Found it: map via locale TBD: for now simply return this char. This is sufficient to pass conformance test awk.ex 156 */ if (*prestr == ']') { prestr++; rlxval = equiv_char; return CHAR; } } } else if (c == '\0') { FATAL("nonterminated character class %.20s", lastre); } else if (bp == buf) { /* 1st char is special */ *bp++ = c; } else if (c == ']') { *bp++ = 0; rlxstr = (uschar *) tostring((char *) buf); if (cflag == 0) return CCL; else return NCCL; } else *bp++ = c; } break; case '{': - if (isdigit(*(prestr))) { + if (isdigit((int) *(prestr))) { num = 0; /* Process as a repetition */ n = -1; m = -1; commafound = false; digitfound = false; startreptok = prestr-1; /* Remember start of previous atom here ? */ } else { /* just a { char, not a repetition */ rlxval = c; return CHAR; } for (; ; ) { if ((c = *prestr++) == '}') { if (commafound) { if (digitfound) { /* {n,m} */ m = num; if (m < n) FATAL("illegal repetition expression: class %.20s", lastre); if (n == 0 && m == 1) { return QUEST; } } else { /* {n,} */ if (n == 0) return STAR; else if (n == 1) return PLUS; } } else { if (digitfound) { /* {n} same as {n,n} */ n = num; m = num; } else { /* {} */ FATAL("illegal repetition expression: class %.20s", lastre); } } if (repeat(starttok, prestr-starttok, lastatom, startreptok - lastatom, n, m) > 0) { if (n == 0 && m == 0) { return ZERO; } /* must rescan input for next token */ goto rescan; } /* Failed to replace: eat up {...} characters and treat like just PLUS */ return PLUS; } else if (c == '\0') { FATAL("nonterminated character class %.20s", lastre); } else if (isdigit(c)) { num = 10 * num + c - '0'; digitfound = true; } else if (c == ',') { if (commafound) FATAL("illegal repetition expression: class %.20s", lastre); /* looking for {n,} or {n,m} */ commafound = true; n = num; digitfound = false; /* reset */ num = 0; } else { FATAL("illegal repetition expression: class %.20s", lastre); } } break; } } int cgoto(fa *f, int s, int c) { int *p, *q; int i, j, k; /* assert(c == HAT || c < NCHARS); BUG: seg fault if disable test */ while (f->accept >= maxsetvec) { /* guessing here! */ resizesetvec(__func__); } for (i = 0; i <= f->accept; i++) setvec[i] = 0; setcnt = 0; resize_state(f, s); /* compute positions of gototab[s,c] into setvec */ p = f->posns[s]; for (i = 1; i <= *p; i++) { if ((k = f->re[p[i]].ltype) != FINAL) { if ((k == CHAR && c == ptoi(f->re[p[i]].lval.np)) || (k == DOT && c != 0 && c != HAT) || (k == ALL && c != 0) || (k == EMPTYRE && c != 0) || (k == CCL && member(c, (int *) f->re[p[i]].lval.rp)) || (k == NCCL && !member(c, (int *) f->re[p[i]].lval.rp) && c != 0 && c != HAT)) { q = f->re[p[i]].lfollow; for (j = 1; j <= *q; j++) { if (q[j] >= maxsetvec) { resizesetvec(__func__); } if (setvec[q[j]] == 0) { setcnt++; setvec[q[j]] = 1; } } } } } /* determine if setvec is a previous state */ tmpset[0] = setcnt; j = 1; for (i = f->accept; i >= 0; i--) if (setvec[i]) { tmpset[j++] = i; } resize_state(f, f->curstat > s ? f->curstat : s); /* tmpset == previous state? */ for (i = 1; i <= f->curstat; i++) { p = f->posns[i]; if ((k = tmpset[0]) != p[0]) goto different; for (j = 1; j <= k; j++) if (tmpset[j] != p[j]) goto different; /* setvec is state i */ if (c != HAT) set_gototab(f, s, c, i); return i; different:; } /* add tmpset to current set of states */ ++(f->curstat); resize_state(f, f->curstat); clear_gototab(f, f->curstat); xfree(f->posns[f->curstat]); p = intalloc(setcnt + 1, __func__); f->posns[f->curstat] = p; if (c != HAT) set_gototab(f, s, c, f->curstat); for (i = 0; i <= setcnt; i++) p[i] = tmpset[i]; if (setvec[f->accept]) f->out[f->curstat] = 1; else f->out[f->curstat] = 0; return f->curstat; } void freefa(fa *f) /* free a finite automaton */ { int i; if (f == NULL) return; for (i = 0; i < f->state_count; i++) xfree(f->gototab[i].entries); xfree(f->gototab); for (i = 0; i <= f->curstat; i++) xfree(f->posns[i]); for (i = 0; i <= f->accept; i++) { xfree(f->re[i].lfollow); if (f->re[i].ltype == CCL || f->re[i].ltype == NCCL) xfree(f->re[i].lval.np); } xfree(f->restr); xfree(f->out); xfree(f->posns); xfree(f->gototab); xfree(f); } diff --git a/bugs-fixed/REGRESS b/bugs-fixed/REGRESS index 98d578ac22ef..acdbeebb6271 100755 --- a/bugs-fixed/REGRESS +++ b/bugs-fixed/REGRESS @@ -1,28 +1,32 @@ #! /bin/sh if [ ! -f ../a.out ] then echo Making executable (cd .. ; make) || exit 0 fi for i in *.awk do echo === $i OUT=${i%.awk}.OUT OK=${i%.awk}.ok + OK2=${i%.awk}.ok2 IN=${i%.awk}.in input= if [ -f $IN ] then input=$IN fi ../a.out -f $i $input > $OUT 2>&1 if cmp -s $OK $OUT + then + rm -f $OUT + elif [ -f $OK2 ] && cmp -s $OK2 $OUT then rm -f $OUT else - echo ++++ $i failed! + echo '++++ $i failed!' fi done diff --git a/lib.c b/lib.c index b5b83f864274..0dac1f929cf2 100644 --- a/lib.c +++ b/lib.c @@ -1,931 +1,931 @@ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ #define DEBUG #include #include #include #include #include #include #include #include #include #include "awk.h" extern int u8_nextlen(const char *s); char EMPTY[] = { '\0' }; FILE *infile = NULL; bool innew; /* true = infile has not been read by readrec */ char *file = EMPTY; char *record; int recsize = RECSIZE; char *fields; int fieldssize = RECSIZE; Cell **fldtab; /* pointers to Cells */ static size_t len_inputFS = 0; static char *inputFS = NULL; /* FS at time of input, for field splitting */ #define MAXFLD 2 int nfields = MAXFLD; /* last allocated slot for $i */ 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 */ extern Awkfloat *ARGC; static Cell dollar0 = { OCELL, CFLD, NULL, EMPTY, 0.0, REC|STR|DONTFREE, NULL, NULL }; static Cell dollar1 = { OCELL, CFLD, NULL, EMPTY, 0.0, FLD|STR|DONTFREE, NULL, NULL }; void recinit(unsigned int n) { 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; fldtab[0]->sval = record; fldtab[0]->nval = tostring("0"); makefields(1, nfields); } void makefields(int n1, int n2) /* create $n1..$n2 inclusive */ { char temp[50]; int i; for (i = n1; i <= n2; i++) { fldtab[i] = (Cell *) malloc(sizeof(**fldtab)); if (fldtab[i] == NULL) FATAL("out of space in makefields %d", i); *fldtab[i] = dollar1; snprintf(temp, sizeof(temp), "%d", i); fldtab[i]->nval = tostring(temp); } } void initgetrec(void) { int i; char *p; for (i = 1; i < *ARGC; i++) { p = getargv(i); /* find 1st real filename */ if (p == NULL || *p == '\0') { /* deleted or zapped */ argno++; continue; } if (!isclvar(p)) { setsval(lookup("FILENAME", symtab), p); return; } setclvar(p); /* a commandline assignment before filename */ argno++; } infile = stdin; /* no filenames, so use stdin */ innew = true; } /* * POSIX specifies that fields are supposed to be evaluated as if they were * split using the value of FS at the time that the record's value ($0) was * read. * * Since field-splitting is done lazily, we save the current value of FS * whenever a new record is read in (implicitly or via getline), or when * a new value is assigned to $0. */ void savefs(void) { size_t len; if ((len = strlen(getsval(fsloc))) < len_inputFS) { strcpy(inputFS, *FS); /* for subsequent field splitting */ return; } len_inputFS = len + 1; inputFS = (char *) realloc(inputFS, len_inputFS); if (inputFS == NULL) FATAL("field separator %.10s... is too long", *FS); memcpy(inputFS, *FS, len_inputFS); } static bool firsttime = true; int getrec(char **pbuf, int *pbufsize, bool isrecord) /* get next input record */ { /* note: cares whether buf == record */ int c; char *buf = *pbuf; uschar saveb0; int bufsize = *pbufsize, savebufsize = bufsize; if (firsttime) { firsttime = false; initgetrec(); } DPRINTF("RS=<%s>, FS=<%s>, ARGC=%g, FILENAME=%s\n", *RS, *FS, *ARGC, *FILENAME); saveb0 = buf[0]; buf[0] = 0; while (argno < *ARGC || infile == stdin) { DPRINTF("argno=%d, file=|%s|\n", argno, file); if (infile == NULL) { /* have to open a new file */ file = getargv(argno); if (file == NULL || *file == '\0') { /* deleted or zapped */ argno++; continue; } if (isclvar(file)) { /* a var=value arg */ setclvar(file); argno++; continue; } *FILENAME = file; DPRINTF("opening file %s\n", file); if (*file == '-' && *(file+1) == '\0') infile = stdin; else if ((infile = fopen(file, "r")) == NULL) FATAL("can't open file %s", file); innew = true; setfval(fnrloc, 0.0); } c = readrec(&buf, &bufsize, infile, innew); if (innew) innew = false; if (c != 0 || buf[0] != '\0') { /* normal record */ if (isrecord) { double result; if (freeable(fldtab[0])) xfree(fldtab[0]->sval); fldtab[0]->sval = buf; /* buf == record */ fldtab[0]->tval = REC | STR | DONTFREE; if (is_number(fldtab[0]->sval, & result)) { fldtab[0]->fval = result; fldtab[0]->tval |= NUM; } donefld = false; donerec = true; savefs(); } setfval(nrloc, nrloc->fval+1); setfval(fnrloc, fnrloc->fval+1); *pbuf = buf; *pbufsize = bufsize; return 1; } /* EOF arrived on this file; set up next */ if (infile != stdin) fclose(infile); infile = NULL; argno++; } buf[0] = saveb0; *pbuf = buf; *pbufsize = savebufsize; return 0; /* true end of file */ } void nextfile(void) { if (infile != NULL && infile != stdin) fclose(infile); infile = NULL; argno++; } extern int readcsvrec(char **pbuf, int *pbufsize, FILE *inf, bool newflag); int readrec(char **pbuf, int *pbufsize, FILE *inf, bool newflag) /* read one record into buf */ { int sep, c, isrec; // POTENTIAL BUG? isrec is a macro in awk.h char *rr = *pbuf, *buf = *pbuf; int bufsize = *pbufsize; char *rs = getsval(rsloc); if (CSV) { c = readcsvrec(pbuf, pbufsize, inf, newflag); isrec = (c == EOF && rr == buf) ? false : true; } else if (*rs && rs[1]) { bool found; memset(buf, 0, bufsize); fa *pfa = makedfa(rs, 1); if (newflag) found = fnematch(pfa, inf, &buf, &bufsize, recsize); else { int tempstat = pfa->initstat; pfa->initstat = 2; found = fnematch(pfa, inf, &buf, &bufsize, recsize); pfa->initstat = tempstat; } if (found) setptr(patbeg, '\0'); isrec = (found == 0 && *buf == '\0') ? false : true; } else { if ((sep = *rs) == 0) { sep = '\n'; while ((c=getc(inf)) == '\n' && c != EOF) /* skip leading \n's */ ; if (c != EOF) ungetc(c, inf); } for (rr = buf; ; ) { for (; (c=getc(inf)) != sep && c != EOF; ) { if (rr-buf+1 > bufsize) if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 1")) FATAL("input record `%.30s...' too long", buf); *rr++ = c; } if (*rs == sep || c == EOF) break; if ((c = getc(inf)) == '\n' || c == EOF) /* 2 in a row */ break; if (!adjbuf(&buf, &bufsize, 2+rr-buf, recsize, &rr, "readrec 2")) FATAL("input record `%.30s...' too long", buf); *rr++ = '\n'; *rr++ = c; } if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readrec 3")) FATAL("input record `%.30s...' too long", buf); *rr = 0; isrec = (c == EOF && rr == buf) ? false : true; } *pbuf = buf; *pbufsize = bufsize; DPRINTF("readrec saw <%s>, returns %d\n", buf, isrec); return isrec; } /******************* * loose ends here: * \r\n should become \n * what about bare \r? Excel uses that for embedded newlines * can't have "" in unquoted fields, according to RFC 4180 */ int readcsvrec(char **pbuf, int *pbufsize, FILE *inf, bool newflag) /* csv can have \n's */ { /* so read a complete record that might be multiple lines */ int sep, c; char *rr = *pbuf, *buf = *pbuf; int bufsize = *pbufsize; bool in_quote = false; sep = '\n'; /* the only separator; have to skip over \n embedded in "..." */ rr = buf; while ((c = getc(inf)) != EOF) { if (c == sep) { if (! in_quote) break; if (rr > buf && rr[-1] == '\r') // remove \r if was \r\n rr--; } if (rr-buf+1 > bufsize) if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readcsvrec 1")) FATAL("input record `%.30s...' too long", buf); *rr++ = c; if (c == '"') in_quote = ! in_quote; } if (c == '\n' && rr > buf && rr[-1] == '\r') // remove \r if was \r\n rr--; if (!adjbuf(&buf, &bufsize, 1+rr-buf, recsize, &rr, "readcsvrec 4")) FATAL("input record `%.30s...' too long", buf); *rr = 0; *pbuf = buf; *pbufsize = bufsize; DPRINTF("readcsvrec saw <%s>, returns %d\n", buf, c); return c; } char *getargv(int n) /* get ARGV[n] */ { Cell *x; char *s, temp[50]; extern Array *ARGVtab; snprintf(temp, sizeof(temp), "%d", n); if (lookup(temp, ARGVtab) == NULL) return NULL; x = setsymtab(temp, "", 0.0, STR, ARGVtab); s = getsval(x); DPRINTF("getargv(%d) returns |%s|\n", n, s); return s; } void setclvar(char *s) /* set var=value from s */ { char *e, *p; Cell *q; double result; /* commit f3d9187d4e0f02294fb1b0e31152070506314e67 broke T.argv test */ /* I don't understand why it was changed. */ for (p=s; *p != '='; p++) ; e = p; *p++ = 0; p = qstring(p, '\0'); q = setsymtab(s, p, 0.0, STR, symtab); setsval(q, p); if (is_number(q->sval, & result)) { q->fval = result; q->tval |= NUM; } DPRINTF("command line set %s to |%s|\n", s, p); free(p); *e = '='; } void fldbld(void) /* create fields from current record */ { /* this relies on having fields[] the same length as $0 */ /* the fields are all stored in this one array with \0's */ /* possibly with a final trailing \0 not associated with any field */ char *r, *fr, sep; Cell *p; int i, j, n; if (donefld) return; if (!isstr(fldtab[0])) getsval(fldtab[0]); r = fldtab[0]->sval; n = strlen(r); if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+2)) == NULL) /* possibly 2 final \0s */ FATAL("out of space for fields in fldbld %d", n); fieldssize = n; } fr = fields; i = 0; /* number of fields accumulated here */ if (inputFS == NULL) /* make sure we have a copy of FS */ savefs(); - if (strlen(inputFS) > 1) { /* it's a regular expression */ + if (!CSV && strlen(inputFS) > 1) { /* it's a regular expression */ i = refldbld(r, inputFS); } else if (!CSV && (sep = *inputFS) == ' ') { /* default whitespace */ for (i = 0; ; ) { while (*r == ' ' || *r == '\t' || *r == '\n') r++; if (*r == 0) break; i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; do *fr++ = *r++; while (*r != ' ' && *r != '\t' && *r != '\n' && *r != '\0'); *fr++ = 0; } *fr = 0; } else if (CSV) { /* CSV processing. no error handling */ if (*r != 0) { for (;;) { i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; if (*r == '"' ) { /* start of "..." */ for (r++ ; *r != '\0'; ) { if (*r == '"' && r[1] != '\0' && r[1] == '"') { r += 2; /* doubled quote */ *fr++ = '"'; } else if (*r == '"' && (r[1] == '\0' || r[1] == ',')) { r++; /* skip over closing quote */ break; } else { *fr++ = *r++; } } *fr++ = 0; } else { /* unquoted field */ while (*r != ',' && *r != '\0') *fr++ = *r++; *fr++ = 0; } if (*r++ == 0) break; } } *fr = 0; } else if ((sep = *inputFS) == 0) { /* new: FS="" => 1 char/field */ for (i = 0; *r != '\0'; ) { char buf[10]; i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); n = u8_nextlen(r); for (j = 0; j < n; j++) buf[j] = *r++; buf[j] = '\0'; fldtab[i]->sval = tostring(buf); fldtab[i]->tval = FLD | STR; } *fr = 0; } else if (*r != 0) { /* if 0, it's a null field */ /* subtle case: if length(FS) == 1 && length(RS > 0) * \n is NOT a field separator (cf awk book 61,84). * this variable is tested in the inner while loop. */ int rtest = '\n'; /* normal case */ if (strlen(*RS) > 0) rtest = '\0'; for (;;) { i++; if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->sval = fr; fldtab[i]->tval = FLD | STR | DONTFREE; while (*r != sep && *r != rtest && *r != '\0') /* \n is always a separator */ *fr++ = *r++; *fr++ = 0; if (*r++ == 0) break; } *fr = 0; } if (i > nfields) FATAL("record `%.30s...' has too many fields; can't happen", r); cleanfld(i+1, lastfld); /* clean out junk from previous record */ lastfld = i; donefld = true; for (j = 1; j <= lastfld; j++) { double result; p = fldtab[j]; if(is_number(p->sval, & result)) { p->fval = result; p->tval |= NUM; } } setfval(nfloc, (Awkfloat) lastfld); donerec = true; /* restore */ if (dbg) { for (j = 0; j <= lastfld; j++) { p = fldtab[j]; printf("field %d (%s): |%s|\n", j, p->nval, p->sval); } } } void cleanfld(int n1, int n2) /* clean out fields n1 .. n2 inclusive */ { /* nvals remain intact */ Cell *p; int i; for (i = n1; i <= n2; i++) { p = fldtab[i]; if (freeable(p)) xfree(p->sval); p->sval = EMPTY, p->tval = FLD | STR | DONTFREE; } } void newfld(int n) /* add field n after end of existing lastfld */ { if (n > nfields) growfldtab(n); cleanfld(lastfld+1, n); lastfld = n; setfval(nfloc, (Awkfloat) n); } void setlastfld(int n) /* set lastfld cleaning fldtab cells if necessary */ { if (n < 0) FATAL("cannot set NF to a negative value"); if (n > nfields) growfldtab(n); if (lastfld < n) cleanfld(lastfld+1, n); else cleanfld(n+1, lastfld); lastfld = n; } Cell *fieldadr(int n) /* get nth field */ { if (n < 0) FATAL("trying to access out of range field %d", n); if (n > nfields) /* fields after NF are empty */ growfldtab(n); /* but does not increase NF */ return(fldtab[n]); } void growfldtab(int n) /* make new fields up to at least $n */ { int nf = 2 * nfields; size_t s; if (n > nf) 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 = (Cell **) realloc(fldtab, s); else /* overflow sizeof int */ xfree(fldtab); /* make it null */ if (fldtab == NULL) FATAL("out of space creating %d fields", nf); makefields(nfields+1, nf); nfields = nf; } int refldbld(const char *rec, const char *fs) /* build fields from reg expr in FS */ { /* this relies on having fields[] the same length as $0 */ /* the fields are all stored in this one array with \0's */ char *fr; int i, tempstat, n; fa *pfa; n = strlen(rec); if (n > fieldssize) { xfree(fields); if ((fields = (char *) malloc(n+1)) == NULL) FATAL("out of space for fields in refldbld %d", n); fieldssize = n; } fr = fields; *fr = '\0'; if (*rec == '\0') return 0; pfa = makedfa(fs, 1); DPRINTF("into refldbld, rec = <%s>, pat = <%s>\n", rec, fs); tempstat = pfa->initstat; for (i = 1; ; i++) { if (i > nfields) growfldtab(i); if (freeable(fldtab[i])) xfree(fldtab[i]->sval); fldtab[i]->tval = FLD | STR | DONTFREE; fldtab[i]->sval = fr; DPRINTF("refldbld: i=%d\n", i); if (nematch(pfa, rec)) { pfa->initstat = 2; /* horrible coupling to b.c */ DPRINTF("match %s (%d chars)\n", patbeg, patlen); strncpy(fr, rec, patbeg-rec); fr += patbeg - rec + 1; *(fr-1) = '\0'; rec = patbeg + patlen; } else { DPRINTF("no match %s\n", rec); strcpy(fr, rec); pfa->initstat = tempstat; break; } } return i; } void recbld(void) /* create $0 from $1..$NF if necessary */ { int i; char *r, *p; char *sep = getsval(ofsloc); if (donerec) return; r = record; for (i = 1; i <= *NF; i++) { p = getsval(fldtab[i]); if (!adjbuf(&record, &recsize, 1+strlen(p)+r-record, recsize, &r, "recbld 1")) FATAL("created $0 `%.30s...' too long", record); while ((*r = *p++) != 0) r++; if (i < *NF) { if (!adjbuf(&record, &recsize, 2+strlen(sep)+r-record, recsize, &r, "recbld 2")) FATAL("created $0 `%.30s...' too long", record); for (p = sep; (*r = *p++) != 0; ) r++; } } if (!adjbuf(&record, &recsize, 2+r-record, recsize, &r, "recbld 3")) FATAL("built giant record `%.30s...'", record); *r = '\0'; DPRINTF("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]); if (freeable(fldtab[0])) xfree(fldtab[0]->sval); fldtab[0]->tval = REC | STR | DONTFREE; fldtab[0]->sval = record; DPRINTF("in recbld inputFS=%s, fldtab[0]=%p\n", inputFS, (void*)fldtab[0]); DPRINTF("recbld = |%s|\n", record); donerec = true; } int errorflag = 0; void yyerror(const char *s) { SYNTAX("%s", s); } void SYNTAX(const char *fmt, ...) { extern char *cmdname, *curfname; static int been_here = 0; va_list varg; if (been_here++ > 2) return; fprintf(stderr, "%s: ", cmdname); va_start(varg, fmt); vfprintf(stderr, fmt, varg); va_end(varg); fprintf(stderr, " at source line %d", lineno); if (curfname != NULL) fprintf(stderr, " in function %s", curfname); if (compile_time == COMPILING && cursource() != NULL) fprintf(stderr, " source file %s", cursource()); fprintf(stderr, "\n"); errorflag = 2; eprint(); } extern int bracecnt, brackcnt, parencnt; void bracecheck(void) { int c; static int beenhere = 0; if (beenhere++) return; while ((c = input()) != EOF && c != '\0') bclass(c); bcheck2(bracecnt, '{', '}'); bcheck2(brackcnt, '[', ']'); bcheck2(parencnt, '(', ')'); } void bcheck2(int n, int c1, int c2) { if (n == 1) fprintf(stderr, "\tmissing %c\n", c2); else if (n > 1) fprintf(stderr, "\t%d missing %c's\n", n, c2); else if (n == -1) fprintf(stderr, "\textra %c\n", c2); else if (n < -1) fprintf(stderr, "\t%d extra %c's\n", -n, c2); } void FATAL(const char *fmt, ...) { extern char *cmdname; va_list varg; fflush(stdout); fprintf(stderr, "%s: ", cmdname); va_start(varg, fmt); vfprintf(stderr, fmt, varg); va_end(varg); error(); if (dbg > 1) /* core dump if serious debugging on */ abort(); exit(2); } void WARNING(const char *fmt, ...) { extern char *cmdname; va_list varg; fflush(stdout); fprintf(stderr, "%s: ", cmdname); va_start(varg, fmt); vfprintf(stderr, fmt, varg); va_end(varg); error(); } void error() { extern Node *curnode; 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 == COMPILING && cursource() != NULL) fprintf(stderr, " source file %s", cursource()); fprintf(stderr, "\n"); eprint(); } } void eprint(void) /* try to print context around error */ { char *p, *q; int c; static int been_here = 0; extern char ebuf[], *ep; if (compile_time != COMPILING || been_here++ > 0 || ebuf == ep) return; if (ebuf == ep) return; p = ep - 1; if (p > ebuf && *p == '\n') p--; for ( ; p > ebuf && *p != '\n' && *p != '\0'; p--) ; while (*p == '\n') p++; fprintf(stderr, " context is\n\t"); for (q=ep-1; q>=p && *q!=' ' && *q!='\t' && *q!='\n'; q--) ; for ( ; p < q; p++) if (*p) putc(*p, stderr); fprintf(stderr, " >>> "); for ( ; p < ep; p++) if (*p) putc(*p, stderr); fprintf(stderr, " <<< "); if (*ep) while ((c = input()) != '\n' && c != '\0' && c != EOF) { putc(c, stderr); bclass(c); } putc('\n', stderr); ep = ebuf; } void bclass(int c) { switch (c) { case '{': bracecnt++; break; case '}': bracecnt--; break; case '[': brackcnt++; break; case ']': brackcnt--; break; case '(': parencnt++; break; case ')': parencnt--; break; } } double errcheck(double x, const char *s) { if (errno == EDOM) { errno = 0; WARNING("%s argument out of domain", s); x = 1; } else if (errno == ERANGE) { errno = 0; WARNING("%s result out of range", s); x = 1; } return x; } int isclvar(const char *s) /* is s of form var=something ? */ { const char *os = s; - if (!isalpha((uschar) *s) && *s != '_') + if (!isalpha((int) *s) && *s != '_') return 0; for ( ; *s; s++) - if (!(isalnum((uschar) *s) || *s == '_')) + if (!(isalnum((int) *s) || *s == '_')) break; return *s == '=' && s > os; } /* strtod is supposed to be a proper test of what's a valid number */ /* appears to be broken in gcc on linux: thinks 0x123 is a valid FP number */ /* wrong: violates 4.10.1.4 of ansi C standard */ /* well, not quite. As of C99, hex floating point is allowed. so this is * a bit of a mess. We work around the mess by checking for a hexadecimal * value and disallowing it. Similarly, we now follow gawk and allow only * +nan, -nan, +inf, and -inf for NaN and infinity values. */ /* * This routine now has a more complicated interface, the main point * being to avoid the double conversion of a string to double, and * also to convey out, if requested, the information that the numeric * value was a leading string or is all of the string. The latter bit * is used in getfval(). */ bool is_valid_number(const char *s, bool trailing_stuff_ok, bool *no_trailing, double *result) { double r; char *ep; bool retval = false; bool is_nan = false; bool is_inf = false; if (no_trailing) *no_trailing = false; - while (isspace(*s)) + while (isspace((int) *s)) s++; /* no hex floating point, sorry */ if (s[0] == '0' && tolower(s[1]) == 'x') return false; /* allow +nan, -nan, +inf, -inf, any other letter, no */ if (s[0] == '+' || s[0] == '-') { is_nan = (strncasecmp(s+1, "nan", 3) == 0); is_inf = (strncasecmp(s+1, "inf", 3) == 0); if ((is_nan || is_inf) - && (isspace(s[4]) || s[4] == '\0')) + && (isspace((int) s[4]) || s[4] == '\0')) goto convert; else if (! isdigit(s[1]) && s[1] != '.') return false; } else if (! isdigit(s[0]) && s[0] != '.') return false; convert: errno = 0; r = strtod(s, &ep); if (ep == s || errno == ERANGE) return false; if (isnan(r) && s[0] == '-' && signbit(r) == 0) r = -r; if (result != NULL) *result = r; /* * check for trailing stuff */ - while (isspace(*ep)) + while (isspace((int) *ep)) ep++; if (no_trailing != NULL) *no_trailing = (*ep == '\0'); /* return true if found the end, or trailing stuff is allowed */ retval = *ep == '\0' || trailing_stuff_ok; return retval; } diff --git a/main.c b/main.c index c478e321f886..73af89ec1058 100644 --- a/main.c +++ b/main.c @@ -1,268 +1,272 @@ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ -const char *version = "version 20231124"; +const char *version = "version 20240122"; #define DEBUG #include #include #include #include #include #include #include "awk.h" extern char **environ; extern int nfields; int dbg = 0; Awkfloat srand_seed = 1; 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 */ enum compile_states compile_time = ERROR_PRINTING; static char **pfile; /* program filenames from -f's */ static size_t maxpfile; /* max program filename */ static size_t npfile; /* number of filenames */ static size_t curpfile; /* current filename */ bool CSV = false; /* true for csv input */ bool safe = false; /* true => "safe" mode */ size_t awk_mb_cur_max = 1; static noreturn void fpecatch(int n #ifdef SA_SIGINFO , siginfo_t *si, void *uc #endif ) { #ifdef SA_SIGINFO static const char *emsg[] = { [0] = "Unknown error", [FPE_INTDIV] = "Integer divide by zero", [FPE_INTOVF] = "Integer overflow", [FPE_FLTDIV] = "Floating point divide by zero", [FPE_FLTOVF] = "Floating point overflow", [FPE_FLTUND] = "Floating point underflow", [FPE_FLTRES] = "Floating point inexact result", [FPE_FLTINV] = "Invalid Floating point operation", [FPE_FLTSUB] = "Subscript out of range", }; #endif FATAL("floating point exception" #ifdef SA_SIGINFO ": %s", (size_t)si->si_code < sizeof(emsg) / sizeof(emsg[0]) && emsg[si->si_code] ? emsg[si->si_code] : emsg[0] #endif ); } /* Can this work with recursive calls? I don't think so. void segvcatch(int n) { FATAL("segfault. Do you have an unbounded recursive call?", n); } */ static const char * setfs(char *p) { /* wart: t=>\t */ if (p[0] == 't' && p[1] == '\0') return "\t"; return p; } static char * getarg(int *argc, char ***argv, const char *msg) { if ((*argv)[1][2] != '\0') { /* arg is -fsomething */ return &(*argv)[1][2]; } else { /* arg is -f something */ (*argc)--; (*argv)++; if (*argc <= 1) FATAL("%s", msg); return (*argv)[1]; } } int main(int argc, char *argv[]) { const char *fs = NULL; char *fn, *vn; setlocale(LC_CTYPE, ""); setlocale(LC_NUMERIC, "C"); /* for parsing cmdline & prog */ awk_mb_cur_max = MB_CUR_MAX; cmdname = argv[0]; if (argc == 1) { fprintf(stderr, "usage: %s [-F fs | --csv] [-v var=value] [-f progfile | 'prog'] [file ...]\n", cmdname); exit(1); } #ifdef SA_SIGINFO { struct sigaction sa; sa.sa_sigaction = fpecatch; sa.sa_flags = SA_SIGINFO; sigemptyset(&sa.sa_mask); (void)sigaction(SIGFPE, &sa, NULL); } #else (void)signal(SIGFPE, fpecatch); #endif /*signal(SIGSEGV, segvcatch); experiment */ /* Set and keep track of the random seed */ srand_seed = 1; srandom((unsigned long) srand_seed); yyin = NULL; symtab = makesymtab(NSYMTAB/NSYMTAB); while (argc > 1 && argv[1][0] == '-' && argv[1][1] != '\0') { if (strcmp(argv[1], "-version") == 0 || strcmp(argv[1], "--version") == 0) { printf("awk %s\n", version); return 0; } if (strcmp(argv[1], "--") == 0) { /* explicit end of args */ argc--; argv++; break; } if (strcmp(argv[1], "--csv") == 0) { /* turn on csv input processing */ CSV = true; argc--; argv++; continue; } switch (argv[1][1]) { case 's': if (strcmp(argv[1], "-safe") == 0) safe = true; break; case 'f': /* next argument is program filename */ fn = getarg(&argc, &argv, "no program filename"); if (npfile >= maxpfile) { maxpfile += 20; pfile = (char **) realloc(pfile, maxpfile * sizeof(*pfile)); if (pfile == NULL) FATAL("error allocating space for -f options"); } pfile[npfile++] = fn; break; case 'F': /* set field separator */ fs = setfs(getarg(&argc, &argv, "no field separator")); break; case 'v': /* -v a=1 to be done NOW. one -v for each */ vn = getarg(&argc, &argv, "no variable name"); if (isclvar(vn)) setclvar(vn); else FATAL("invalid -v option argument: %s", vn); break; case 'd': dbg = atoi(&argv[1][2]); if (dbg == 0) dbg = 1; printf("awk %s\n", version); break; default: WARNING("unknown option %s ignored", argv[1]); break; } argc--; argv++; } + + if (CSV && (fs != NULL || lookup("FS", symtab) != NULL)) + WARNING("danger: don't set FS when --csv is in effect"); + /* argv[1] is now the first argument */ if (npfile == 0) { /* no -f; first argument is program */ if (argc <= 1) { if (dbg) exit(0); FATAL("no program given"); } DPRINTF("program = |%s|\n", argv[1]); lexprog = argv[1]; argc--; argv++; } recinit(recsize); syminit(); 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); if (!safe) envinit(environ); yyparse(); #if 0 // Doing this would comply with POSIX, but is not compatible with // other awks and with what most users expect. So comment it out. setlocale(LC_NUMERIC, ""); /* back to whatever it is locally */ #endif if (fs) *FS = qstring(fs, '\0'); DPRINTF("errorflag=%d\n", errorflag); if (errorflag == 0) { compile_time = RUNNING; run(winner); } else bracecheck(); return(errorflag); } int pgetc(void) /* get 1 character from awk program */ { int c; for (;;) { if (yyin == NULL) { if (curpfile >= npfile) return EOF; if (strcmp(pfile[curpfile], "-") == 0) yyin = stdin; else if ((yyin = fopen(pfile[curpfile], "r")) == NULL) FATAL("can't open file %s", pfile[curpfile]); lineno = 1; } if ((c = getc(yyin)) != EOF) return c; if (yyin != stdin) fclose(yyin); yyin = NULL; curpfile++; } } char *cursource(void) /* current source file name */ { if (npfile > 0) return pfile[curpfile < npfile ? curpfile : curpfile - 1]; else return NULL; } diff --git a/run.c b/run.c index 5efade2108fb..ede3ba7d9827 100644 --- a/run.c +++ b/run.c @@ -1,2849 +1,2854 @@ /**************************************************************** Copyright (C) Lucent Technologies 1997 All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that the copyright notice and this permission notice and warranty disclaimer appear in supporting documentation, and that the name Lucent Technologies or any of its entities not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ****************************************************************/ #define DEBUG #include #include #include #include #include #include #include #include #include #include #include #include #include #include "awk.h" #include "awkgram.tab.h" static void stdinit(void); static void flush_all(void); static char *wide_char_to_byte_str(int rune, size_t *outlen); #if 1 #define tempfree(x) do { if (istemp(x)) tfree(x); } while (/*CONSTCOND*/0) #else void tempfree(Cell *p) { if (p->ctype == OCELL && (p->csub < CUNK || p->csub > CFREE)) { WARNING("bad csub %d in Cell %d %s", p->csub, p->ctype, p->sval); } if (istemp(p)) tfree(p); } #endif /* do we really need these? */ /* #ifdef _NFILE */ /* #ifndef FOPEN_MAX */ /* #define FOPEN_MAX _NFILE */ /* #endif */ /* #endif */ /* */ /* #ifndef FOPEN_MAX */ /* #define FOPEN_MAX 40 */ /* max number of open files */ /* #endif */ /* */ /* #ifndef RAND_MAX */ /* #define RAND_MAX 32767 */ /* all that ansi guarantees */ /* #endif */ jmp_buf env; extern int pairstack[]; extern Awkfloat srand_seed; Node *winner = NULL; /* root of parse tree */ Cell *tmps; /* free temporary cells for execution */ static Cell truecell ={ OBOOL, BTRUE, 0, 0, 1.0, NUM, NULL, NULL }; Cell *True = &truecell; static Cell falsecell ={ OBOOL, BFALSE, 0, 0, 0.0, NUM, NULL, NULL }; Cell *False = &falsecell; static Cell breakcell ={ OJUMP, JBREAK, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jbreak = &breakcell; static Cell contcell ={ OJUMP, JCONT, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jcont = &contcell; static Cell nextcell ={ OJUMP, JNEXT, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jnext = &nextcell; static Cell nextfilecell ={ OJUMP, JNEXTFILE, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jnextfile = &nextfilecell; static Cell exitcell ={ OJUMP, JEXIT, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jexit = &exitcell; static Cell retcell ={ OJUMP, JRET, 0, 0, 0.0, NUM, NULL, NULL }; Cell *jret = &retcell; static Cell tempcell ={ OCELL, CTEMP, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL, NULL }; Node *curnode = NULL; /* the node being executed, for debugging */ /* buffer memory management */ int adjbuf(char **pbuf, int *psiz, int minlen, int quantum, char **pbptr, const char *whatrtn) /* pbuf: address of pointer to buffer being managed * psiz: address of buffer size variable * minlen: minimum length of buffer needed * quantum: buffer size quantum * pbptr: address of movable pointer into buffer, or 0 if none * whatrtn: name of the calling routine if failure should cause fatal error * * return 0 for realloc failure, !=0 for success */ { if (minlen > *psiz) { char *tbuf; int rminlen = quantum ? minlen % quantum : 0; int boff = pbptr ? *pbptr - *pbuf : 0; /* 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); if (tbuf == NULL) { if (whatrtn) FATAL("out of memory in %s", whatrtn); return 0; } *pbuf = tbuf; *psiz = minlen; if (pbptr) *pbptr = tbuf + boff; } return 1; } void run(Node *a) /* execution of parse tree starts here */ { stdinit(); execute(a); closeall(); } Cell *execute(Node *u) /* execute a node of the parse tree */ { Cell *(*proc)(Node **, int); Cell *x; Node *a; if (u == NULL) return(True); for (a = u; ; a = a->nnext) { curnode = a; if (isvalue(a)) { x = (Cell *) (a->narg[0]); if (isfld(x) && !donefld) fldbld(); else if (isrec(x) && !donerec) recbld(); return(x); } if (notlegal(a->nobj)) /* probably a Cell* but too risky to print */ FATAL("illegal statement"); proc = proctab[a->nobj-FIRSTTOKEN]; x = (*proc)(a->narg, a->nobj); if (isfld(x) && !donefld) fldbld(); else if (isrec(x) && !donerec) recbld(); if (isexpr(a)) return(x); if (isjump(x)) return(x); if (a->nnext == NULL) return(x); tempfree(x); } } Cell *program(Node **a, int n) /* execute an awk program */ { /* a[0] = BEGIN, a[1] = body, a[2] = END */ Cell *x; if (setjmp(env) != 0) goto ex; if (a[0]) { /* BEGIN */ x = execute(a[0]); if (isexit(x)) return(True); if (isjump(x)) FATAL("illegal break, continue, next or nextfile from BEGIN"); tempfree(x); } if (a[1] || a[2]) while (getrec(&record, &recsize, true) > 0) { x = execute(a[1]); if (isexit(x)) break; tempfree(x); } ex: if (setjmp(env) != 0) /* handles exit within END */ goto ex1; if (a[2]) { /* END */ x = execute(a[2]); if (isbreak(x) || isnext(x) || iscont(x)) FATAL("illegal break, continue, next or nextfile from END"); tempfree(x); } ex1: return(True); } struct Frame { /* stack frame for awk function calls */ int nargs; /* number of arguments in this call */ Cell *fcncell; /* pointer to Cell for function */ Cell **args; /* pointer to array of arguments after execute */ Cell *retval; /* return value */ }; #define NARGS 50 /* max args in a call */ struct Frame *frame = NULL; /* base of stack frames; dynamically allocated */ int nframe = 0; /* number of frames allocated */ struct Frame *frp = NULL; /* frame pointer. bottom level unused */ Cell *call(Node **a, int n) /* function call. very kludgy and fragile */ { static const Cell newcopycell = { OCELL, CCOPY, 0, EMPTY, 0.0, NUM|STR|DONTFREE, NULL, NULL }; int i, ncall, ndef; int freed = 0; /* handles potential double freeing when fcn & param share a tempcell */ Node *x; Cell *args[NARGS], *oargs[NARGS]; /* BUG: fixed size arrays */ Cell *y, *z, *fcn; char *s; fcn = execute(a[0]); /* the function itself */ s = fcn->nval; if (!isfcn(fcn)) FATAL("calling undefined function %s", s); if (frame == NULL) { frp = frame = (struct Frame *) calloc(nframe += 100, sizeof(*frame)); if (frame == NULL) FATAL("out of space for stack frames calling %s", s); } for (ncall = 0, x = a[1]; x != NULL; x = x->nnext) /* args in call */ ncall++; ndef = (int) fcn->fval; /* args in defn */ DPRINTF("calling %s, %d args (%d in defn), frp=%d\n", s, ncall, ndef, (int) (frp-frame)); if (ncall > ndef) WARNING("function %s called with %d args, uses only %d", s, ncall, ndef); if (ncall + ndef > NARGS) FATAL("function %s has %d arguments, limit %d", s, ncall+ndef, NARGS); for (i = 0, x = a[1]; x != NULL; i++, x = x->nnext) { /* get call args */ DPRINTF("evaluate args[%d], frp=%d:\n", i, (int) (frp-frame)); y = execute(x); oargs[i] = y; DPRINTF("args[%d]: %s %f <%s>, t=%o\n", i, NN(y->nval), y->fval, isarr(y) ? "(array)" : NN(y->sval), y->tval); if (isfcn(y)) FATAL("can't use function %s as argument in %s", y->nval, s); if (isarr(y)) args[i] = y; /* arrays by ref */ else args[i] = copycell(y); tempfree(y); } for ( ; i < ndef; i++) { /* add null args for ones not provided */ args[i] = gettemp(); *args[i] = newcopycell; } frp++; /* now ok to up frame */ if (frp >= frame + nframe) { int dfp = frp - frame; /* old index */ 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; } frp->fcncell = fcn; frp->args = args; frp->nargs = ndef; /* number defined with (excess are locals) */ frp->retval = gettemp(); DPRINTF("start exec of %s, frp=%d\n", s, (int) (frp-frame)); y = execute((Node *)(fcn->sval)); /* execute body */ DPRINTF("finished exec of %s, frp=%d\n", s, (int) (frp-frame)); for (i = 0; i < ndef; i++) { Cell *t = frp->args[i]; if (isarr(t)) { if (t->csub == CCOPY) { if (i >= ncall) { freesymtab(t); t->csub = CTEMP; tempfree(t); } else { oargs[i]->tval = t->tval; oargs[i]->tval &= ~(STR|NUM|DONTFREE); oargs[i]->sval = t->sval; tempfree(t); } } } else if (t != y) { /* kludge to prevent freeing twice */ t->csub = CTEMP; tempfree(t); } else if (t == y && t->csub == CCOPY) { t->csub = CTEMP; tempfree(t); freed = 1; } } tempfree(fcn); if (isexit(y) || isnext(y)) return y; if (freed == 0) { tempfree(y); /* don't free twice! */ } z = frp->retval; /* return value */ DPRINTF("%s returns %g |%s| %o\n", s, getfval(z), getsval(z), z->tval); frp--; return(z); } Cell *copycell(Cell *x) /* make a copy of a cell in a temp */ { Cell *y; /* copy is not constant or field */ y = gettemp(); y->tval = x->tval & ~(CON|FLD|REC); y->csub = CCOPY; /* prevents freeing until call is over */ y->nval = x->nval; /* BUG? */ if (isstr(x) /* || x->ctype == OCELL */) { y->sval = tostring(x->sval); y->tval &= ~DONTFREE; } else y->tval |= DONTFREE; y->fval = x->fval; return y; } Cell *arg(Node **a, int n) /* nth argument of a function */ { n = ptoi(a[0]); /* argument number, counting from 0 */ DPRINTF("arg(%d), frp->nargs=%d\n", n, frp->nargs); if (n+1 > frp->nargs) FATAL("argument #%d of function %s was not supplied", n+1, frp->fcncell->nval); return frp->args[n]; } Cell *jump(Node **a, int n) /* break, continue, next, nextfile, return */ { Cell *y; switch (n) { case EXIT: if (a[0] != NULL) { y = execute(a[0]); errorflag = (int) getfval(y); tempfree(y); } longjmp(env, 1); case RETURN: if (a[0] != NULL) { y = execute(a[0]); if ((y->tval & (STR|NUM)) == (STR|NUM)) { setsval(frp->retval, getsval(y)); frp->retval->fval = getfval(y); frp->retval->tval |= NUM; } else if (y->tval & STR) setsval(frp->retval, getsval(y)); else if (y->tval & NUM) setfval(frp->retval, getfval(y)); else /* can't happen */ FATAL("bad type variable %d", y->tval); tempfree(y); } return(jret); case NEXT: return(jnext); case NEXTFILE: nextfile(); return(jnextfile); case BREAK: return(jbreak); case CONTINUE: return(jcont); default: /* can't happen */ FATAL("illegal jump type %d", n); } return 0; /* not reached */ } Cell *awkgetline(Node **a, int n) /* get next line from specific input */ { /* a[0] is variable, a[1] is operator, a[2] is filename */ Cell *r, *x; extern Cell **fldtab; FILE *fp; char *buf; int bufsize = recsize; int mode; bool newflag; double result; if ((buf = (char *) malloc(bufsize)) == NULL) FATAL("out of memory in getline"); fflush(stdout); /* in case someone is waiting for a prompt */ r = gettemp(); if (a[1] != NULL) { /* getline < file */ x = execute(a[2]); /* filename */ mode = ptoi(a[1]); if (mode == '|') /* input pipe */ mode = LE; /* arbitrary flag */ fp = openfile(mode, getsval(x), &newflag); tempfree(x); if (fp == NULL) n = -1; else n = readrec(&buf, &bufsize, fp, newflag); if (n <= 0) { ; } else if (a[0] != NULL) { /* getline var sval, & result)) { x->fval = result; x->tval |= NUM; } tempfree(x); } else { /* getline sval, & result)) { fldtab[0]->fval = result; fldtab[0]->tval |= NUM; } } } else { /* bare getline; use current input */ if (a[0] == NULL) /* getline */ n = getrec(&record, &recsize, true); else { /* getline var */ n = getrec(&buf, &bufsize, false); if (n > 0) { x = execute(a[0]); setsval(x, buf); if (is_number(x->sval, & result)) { x->fval = result; x->tval |= NUM; } tempfree(x); } } } setfval(r, (Awkfloat) n); free(buf); return r; } Cell *getnf(Node **a, int n) /* get NF */ { if (!donefld) fldbld(); return (Cell *) a[0]; } static char * makearraystring(Node *p, const char *func) { char *buf; int bufsz = recsize; size_t blen; if ((buf = (char *) malloc(bufsz)) == NULL) { FATAL("%s: out of memory", func); } blen = 0; buf[blen] = '\0'; for (; p; p = p->nnext) { Cell *x = execute(p); /* expr */ char *s = getsval(x); size_t seplen = strlen(getsval(subseploc)); 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)) xfree(x->sval); x->tval &= ~(STR|NUM|DONTFREE); x->tval |= ARR; x->sval = (char *) makesymtab(NSYMTAB); } z = setsymtab(buf, "", 0.0, STR|NUM, (Array *) x->sval); z->ctype = OCELL; z->csub = CVAR; tempfree(x); free(buf); return(z); } Cell *awkdelete(Node **a, int n) /* a[0] is symtab, a[1] is list of subscripts */ { Cell *x; x = execute(a[0]); /* Cell* for symbol table */ if (x == symtabloc) { FATAL("cannot delete SYMTAB or its elements"); } if (!isarr(x)) return True; if (a[1] == NULL) { /* delete the elements, not the table */ freesymtab(x); x->tval &= ~STR; x->tval |= ARR; x->sval = (char *) makesymtab(NSYMTAB); } else { char *buf = makearraystring(a[1], __func__); freeelem(x, buf); free(buf); } tempfree(x); return True; } Cell *intest(Node **a, int n) /* a[0] is index (list), a[1] is symtab */ { Cell *ap, *k; char *buf; ap = execute(a[1]); /* array name */ if (!isarr(ap)) { DPRINTF("making %s into an array\n", ap->nval); if (freeable(ap)) xfree(ap->sval); ap->tval &= ~(STR|NUM|DONTFREE); ap->tval |= ARR; ap->sval = (char *) makesymtab(NSYMTAB); } buf = makearraystring(a[0], __func__); k = lookup(buf, (Array *) ap->sval); tempfree(ap); free(buf); if (k == NULL) return(False); else return(True); } /* ======== utf-8 code ========== */ /* * Awk strings can contain ascii, random 8-bit items (eg Latin-1), * or utf-8. u8_isutf tests whether a string starts with a valid * utf-8 sequence, and returns 0 if not (e.g., high bit set). * u8_nextlen returns length of next valid sequence, which is * 1 for ascii, 2..4 for utf-8, or 1 for high bit non-utf. * u8_strlen returns length of string in valid utf-8 sequences * and/or high-bit bytes. Conversion functions go between byte * number and character number. * * In theory, this behaves the same as before for non-utf8 bytes. * * Limited checking! This is a potential security hole. */ /* is s the beginning of a valid utf-8 string? */ /* return length 1..4 if yes, 0 if no */ int u8_isutf(const char *s) { int n, ret; unsigned char c; c = s[0]; if (c < 128 || awk_mb_cur_max == 1) return 1; /* what if it's 0? */ n = strlen(s); if (n >= 2 && ((c>>5) & 0x7) == 0x6 && (s[1] & 0xC0) == 0x80) { ret = 2; /* 110xxxxx 10xxxxxx */ } else if (n >= 3 && ((c>>4) & 0xF) == 0xE && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80) { ret = 3; /* 1110xxxx 10xxxxxx 10xxxxxx */ } else if (n >= 4 && ((c>>3) & 0x1F) == 0x1E && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80 && (s[3] & 0xC0) == 0x80) { ret = 4; /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ } else { ret = 0; } return ret; } /* Convert (prefix of) utf8 string to utf-32 rune. */ /* Sets *rune to the value, returns the length. */ /* No error checking: watch out. */ int u8_rune(int *rune, const char *s) { int n, ret; unsigned char c; c = s[0]; if (c < 128 || awk_mb_cur_max == 1) { *rune = c; return 1; } n = strlen(s); if (n >= 2 && ((c>>5) & 0x7) == 0x6 && (s[1] & 0xC0) == 0x80) { *rune = ((c & 0x1F) << 6) | (s[1] & 0x3F); /* 110xxxxx 10xxxxxx */ ret = 2; } else if (n >= 3 && ((c>>4) & 0xF) == 0xE && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80) { *rune = ((c & 0xF) << 12) | ((s[1] & 0x3F) << 6) | (s[2] & 0x3F); /* 1110xxxx 10xxxxxx 10xxxxxx */ ret = 3; } else if (n >= 4 && ((c>>3) & 0x1F) == 0x1E && (s[1] & 0xC0) == 0x80 && (s[2] & 0xC0) == 0x80 && (s[3] & 0xC0) == 0x80) { *rune = ((c & 0x7) << 18) | ((s[1] & 0x3F) << 12) | ((s[2] & 0x3F) << 6) | (s[3] & 0x3F); /* 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx */ ret = 4; } else { *rune = c; ret = 1; } return ret; /* returns one byte if sequence doesn't look like utf */ } /* return length of next sequence: 1 for ascii or random, 2..4 for valid utf8 */ int u8_nextlen(const char *s) { int len; len = u8_isutf(s); if (len == 0) len = 1; return len; } /* return number of utf characters or single non-utf bytes */ int u8_strlen(const char *s) { int i, len, n, totlen; unsigned char c; n = strlen(s); totlen = 0; for (i = 0; i < n; i += len) { c = s[i]; if (c < 128 || awk_mb_cur_max == 1) { len = 1; } else { len = u8_nextlen(&s[i]); } totlen++; if (i > n) FATAL("bad utf count [%s] n=%d i=%d\n", s, n, i); } return totlen; } /* convert utf-8 char number in a string to its byte offset */ int u8_char2byte(const char *s, int charnum) { int n; int bytenum = 0; while (charnum > 0) { n = u8_nextlen(s); s += n; bytenum += n; charnum--; } return bytenum; } /* convert byte offset in s to utf-8 char number that starts there */ int u8_byte2char(const char *s, int bytenum) { int i, len, b; int charnum = 0; /* BUG: what origin? */ /* should be 0 to match start==0 which means no match */ b = strlen(s); if (bytenum > b) { return -1; /* ??? */ } for (i = 0; i <= bytenum; i += len) { len = u8_nextlen(s+i); charnum++; } return charnum; } /* runetochar() adapted from rune.c in the Plan 9 distributione */ enum { Runeerror = 128, /* from somewhere else */ Runemax = 0x10FFFF, Bit1 = 7, Bitx = 6, Bit2 = 5, Bit3 = 4, Bit4 = 3, Bit5 = 2, T1 = ((1<<(Bit1+1))-1) ^ 0xFF, /* 0000 0000 */ Tx = ((1<<(Bitx+1))-1) ^ 0xFF, /* 1000 0000 */ T2 = ((1<<(Bit2+1))-1) ^ 0xFF, /* 1100 0000 */ T3 = ((1<<(Bit3+1))-1) ^ 0xFF, /* 1110 0000 */ T4 = ((1<<(Bit4+1))-1) ^ 0xFF, /* 1111 0000 */ T5 = ((1<<(Bit5+1))-1) ^ 0xFF, /* 1111 1000 */ Rune1 = (1<<(Bit1+0*Bitx))-1, /* 0000 0000 0000 0000 0111 1111 */ Rune2 = (1<<(Bit2+1*Bitx))-1, /* 0000 0000 0000 0111 1111 1111 */ Rune3 = (1<<(Bit3+2*Bitx))-1, /* 0000 0000 1111 1111 1111 1111 */ Rune4 = (1<<(Bit4+3*Bitx))-1, /* 0011 1111 1111 1111 1111 1111 */ Maskx = (1< 00-7F */ if (c <= Rune1) { str[0] = c; return 1; } /* two character sequence 00080-007FF => T2 Tx */ if (c <= Rune2) { str[0] = T2 | (c >> 1*Bitx); str[1] = Tx | (c & Maskx); return 2; } /* three character sequence 00800-0FFFF => T3 Tx Tx */ if (c > Runemax) c = Runeerror; if (c <= Rune3) { str[0] = T3 | (c >> 2*Bitx); str[1] = Tx | ((c >> 1*Bitx) & Maskx); str[2] = Tx | (c & Maskx); return 3; } /* four character sequence 010000-1FFFFF => T4 Tx Tx Tx */ str[0] = T4 | (c >> 3*Bitx); str[1] = Tx | ((c >> 2*Bitx) & Maskx); str[2] = Tx | ((c >> 1*Bitx) & Maskx); str[3] = Tx | (c & Maskx); return 4; } /* ========== end of utf8 code =========== */ Cell *matchop(Node **a, int n) /* ~ and match() */ { - Cell *x, *y; + Cell *x, *y, *z; char *s, *t; int i; int cstart, cpatlen, len; fa *pfa; int (*mf)(fa *, const char *) = match, mode = 0; if (n == MATCHFCN) { mf = pmatch; mode = 1; } x = execute(a[1]); /* a[1] = target text */ s = getsval(x); if (a[0] == NULL) /* a[1] == 0: already-compiled reg expr */ i = (*mf)((fa *) a[2], s); else { y = execute(a[2]); /* a[2] = regular expr */ t = getsval(y); pfa = makedfa(t, mode); i = (*mf)(pfa, s); tempfree(y); } - tempfree(x); + z = x; if (n == MATCHFCN) { int start = patbeg - s + 1; /* origin 1 */ if (patlen < 0) { start = 0; /* not found */ } else { cstart = u8_byte2char(s, start-1); cpatlen = 0; for (i = 0; i < patlen; i += len) { len = u8_nextlen(patbeg+i); cpatlen++; } start = cstart; patlen = cpatlen; } setfval(rstartloc, (Awkfloat) start); setfval(rlengthloc, (Awkfloat) patlen); x = gettemp(); x->tval = NUM; x->fval = start; - return x; } else if ((n == MATCH && i == 1) || (n == NOTMATCH && i == 0)) - return(True); + x = True; else - return(False); + x = False; + + tempfree(z); + return x; } Cell *boolop(Node **a, int n) /* a[0] || a[1], a[0] && a[1], !a[0] */ { Cell *x, *y; int i; x = execute(a[0]); i = istrue(x); tempfree(x); switch (n) { case BOR: if (i) return(True); y = execute(a[1]); i = istrue(y); tempfree(y); if (i) return(True); else return(False); case AND: if ( !i ) return(False); y = execute(a[1]); i = istrue(y); tempfree(y); if (i) return(True); else return(False); case NOT: if (i) return(False); else return(True); default: /* can't happen */ FATAL("unknown boolean operator %d", n); } return 0; /*NOTREACHED*/ } Cell *relop(Node **a, int n) /* a[0 < a[1], etc. */ { int i; Cell *x, *y; Awkfloat j; bool x_is_nan, y_is_nan; x = execute(a[0]); y = execute(a[1]); x_is_nan = isnan(x->fval); y_is_nan = isnan(y->fval); if (x->tval&NUM && y->tval&NUM) { if ((x_is_nan || y_is_nan) && n != NE) return(False); j = x->fval - y->fval; i = j<0? -1: (j>0? 1: 0); } else { i = strcmp(getsval(x), getsval(y)); } tempfree(x); tempfree(y); switch (n) { case LT: if (i<0) return(True); else return(False); case LE: if (i<=0) return(True); else return(False); case NE: if (x_is_nan && y_is_nan) return(True); else if (i!=0) return(True); else return(False); case EQ: if (i == 0) return(True); else return(False); case GE: if (i>=0) return(True); else return(False); case GT: if (i>0) return(True); else return(False); default: /* can't happen */ FATAL("unknown relational operator %d", n); } return 0; /*NOTREACHED*/ } void tfree(Cell *a) /* free a tempcell */ { if (freeable(a)) { DPRINTF("freeing %s %s %o\n", NN(a->nval), NN(a->sval), a->tval); xfree(a->sval); } if (a == tmps) FATAL("tempcell list is curdled"); a->cnext = tmps; tmps = a; } Cell *gettemp(void) /* get a tempcell */ { int i; Cell *x; if (!tmps) { tmps = (Cell *) calloc(100, sizeof(*tmps)); if (!tmps) FATAL("out of space for temporaries"); for (i = 1; i < 100; i++) tmps[i-1].cnext = &tmps[i]; tmps[i-1].cnext = NULL; } x = tmps; tmps = x->cnext; *x = tempcell; return(x); } Cell *indirect(Node **a, int n) /* $( a[0] ) */ { Awkfloat val; Cell *x; int m; char *s; x = execute(a[0]); val = getfval(x); /* freebsd: defend against super large field numbers */ if ((Awkfloat)INT_MAX < val) FATAL("trying to access out of range field %s", x->nval); m = (int) val; if (m == 0 && !is_number(s = getsval(x), NULL)) /* suspicion! */ FATAL("illegal field $(%s), name \"%s\"", s, x->nval); /* BUG: can x->nval ever be null??? */ tempfree(x); x = fieldadr(m); x->ctype = OCELL; /* BUG? why are these needed? */ x->csub = CFLD; return(x); } Cell *substr(Node **a, int nnn) /* substr(a[0], a[1], a[2]) */ { int k, m, n; int mb, nb; char *s; int temp; Cell *x, *y, *z = NULL; x = execute(a[0]); y = execute(a[1]); if (a[2] != NULL) z = execute(a[2]); s = getsval(x); k = u8_strlen(s) + 1; if (k <= 1) { tempfree(x); tempfree(y); if (a[2] != NULL) { tempfree(z); } x = gettemp(); setsval(x, ""); return(x); } m = (int) getfval(y); if (m <= 0) m = 1; else if (m > k) m = k; tempfree(y); if (a[2] != NULL) { n = (int) getfval(z); tempfree(z); } else n = k - 1; if (n < 0) n = 0; else if (n > k - m) n = k - m; /* m is start, n is length from there */ DPRINTF("substr: m=%d, n=%d, s=%s\n", m, n, s); y = gettemp(); mb = u8_char2byte(s, m-1); /* byte offset of start char in s */ nb = u8_char2byte(s, m-1+n); /* byte offset of end+1 char in s */ temp = s[nb]; /* with thanks to John Linderman */ s[nb] = '\0'; setsval(y, s + mb); s[nb] = temp; tempfree(x); return(y); } Cell *sindex(Node **a, int nnn) /* index(a[0], a[1]) */ { Cell *x, *y, *z; char *s1, *s2, *p1, *p2, *q; Awkfloat v = 0.0; x = execute(a[0]); s1 = getsval(x); y = execute(a[1]); s2 = getsval(y); z = gettemp(); for (p1 = s1; *p1 != '\0'; p1++) { for (q = p1, p2 = s2; *p2 != '\0' && *q == *p2; q++, p2++) continue; if (*p2 == '\0') { /* v = (Awkfloat) (p1 - s1 + 1); origin 1 */ /* should be a function: used in match() as well */ int i, len; v = 0; for (i = 0; i < p1-s1+1; i += len) { len = u8_nextlen(s1+i); v++; } break; } } tempfree(x); tempfree(y); setfval(z, v); return(z); } int has_utf8(char *s) /* return 1 if s contains any utf-8 (2 bytes or more) character */ { int n; for (n = 0; *s != 0; s += n) { n = u8_nextlen(s); if (n > 1) return 1; } return 0; } #define MAXNUMSIZE 50 int format(char **pbuf, int *pbufsize, const char *s, Node *a) /* printf-like conversions */ { char *fmt; char *p, *t; const char *os; Cell *x; int flag = 0, n; int fmtwd; /* format width */ int fmtsz = recsize; char *buf = *pbuf; int bufsize = *pbufsize; #define FMTSZ(a) (fmtsz - ((a) - fmt)) #define BUFSZ(a) (bufsize - ((a) - buf)) static bool first = true; static bool have_a_format = false; if (first) { char xbuf[100]; snprintf(xbuf, sizeof(xbuf), "%a", 42.0); have_a_format = (strcmp(xbuf, "0x1.5p+5") == 0); first = false; } os = s; p = buf; if ((fmt = (char *) malloc(fmtsz)) == NULL) FATAL("out of memory in format()"); while (*s) { adjbuf(&buf, &bufsize, MAXNUMSIZE+1+p-buf, recsize, &p, "format1"); if (*s != '%') { *p++ = *s++; continue; } if (*(s+1) == '%') { *p++ = '%'; s += 2; continue; } fmtwd = atoi(s+1); if (fmtwd < 0) fmtwd = -fmtwd; adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format2"); for (t = fmt; (*t++ = *s) != '\0'; s++) { if (!adjbuf(&fmt, &fmtsz, MAXNUMSIZE+1+t-fmt, recsize, &t, "format3")) FATAL("format item %.30s... ran format() out of memory", os); /* Ignore size specifiers */ if (strchr("hjLlqtz", *s) != NULL) { /* the ansi panoply */ t--; continue; } if (isalpha((uschar)*s)) break; if (*s == '$') { FATAL("'$' not permitted in awk formats"); } if (*s == '*') { if (a == NULL) { FATAL("not enough args in printf(%s)", os); } x = execute(a); a = a->nnext; 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"); t = fmt + strlen(fmt); tempfree(x); } } *t = '\0'; if (fmtwd < 0) fmtwd = -fmtwd; adjbuf(&buf, &bufsize, fmtwd+1+p-buf, recsize, &p, "format4"); switch (*s) { case 'a': case 'A': if (have_a_format) flag = *s; else flag = 'f'; break; case 'f': case 'e': case 'g': case 'E': case 'G': flag = 'f'; break; case 'd': case 'i': case 'o': case 'x': case 'X': case 'u': flag = (*s == 'd' || *s == 'i') ? 'd' : 'u'; *(t-1) = 'j'; *t = *s; *++t = '\0'; break; case 's': flag = 's'; break; case 'c': flag = 'c'; break; default: WARNING("weird printf conversion %s", fmt); flag = '?'; break; } if (a == NULL) FATAL("not enough args in printf(%s)", os); x = execute(a); a = a->nnext; n = MAXNUMSIZE; if (fmtwd > n) n = fmtwd; adjbuf(&buf, &bufsize, 1+n+p-buf, recsize, &p, "format5"); switch (flag) { case '?': snprintf(p, BUFSZ(p), "%s", fmt); /* unknown, so dump it too */ 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, BUFSZ(p), "%s", t); break; case 'a': case 'A': case 'f': snprintf(p, BUFSZ(p), fmt, getfval(x)); break; case 'd': snprintf(p, BUFSZ(p), fmt, (intmax_t) getfval(x)); break; case 'u': snprintf(p, BUFSZ(p), fmt, (uintmax_t) getfval(x)); break; case 's': { t = getsval(x); n = strlen(t); /* if simple format or no utf-8 in the string, sprintf works */ if (!has_utf8(t) || strcmp(fmt,"%s") == 0) { if (fmtwd > n) 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, BUFSZ(p), fmt, t); break; } /* get here if string has utf-8 chars and fmt is not plain %s */ /* "%-w.ps", where -, w and .p are all optional */ /* '0' before the w is a flag character */ /* fmt points at % */ int ljust = 0, wid = 0, prec = n, pad = 0; char *f = fmt+1; if (f[0] == '-') { ljust = 1; f++; } // flags '0' and '+' are recognized but skipped if (f[0] == '0') { f++; if (f[0] == '+') f++; } if (f[0] == '+') { f++; if (f[0] == '0') f++; } if (isdigit(f[0])) { /* there is a wid */ wid = strtol(f, &f, 10); } if (f[0] == '.') { /* there is a .prec */ prec = strtol(++f, &f, 10); } if (prec > u8_strlen(t)) prec = u8_strlen(t); pad = wid>prec ? wid - prec : 0; // has to be >= 0 int i, k, n; if (ljust) { // print prec chars from t, then pad blanks n = u8_char2byte(t, prec); for (k = 0; k < n; k++) { //putchar(t[k]); *p++ = t[k]; } for (i = 0; i < pad; i++) { //printf(" "); *p++ = ' '; } } else { // print pad blanks, then prec chars from t for (i = 0; i < pad; i++) { //printf(" "); *p++ = ' '; } n = u8_char2byte(t, prec); for (k = 0; k < n; k++) { //putchar(t[k]); *p++ = t[k]; } } *p = 0; break; } case 'c': { /* * If a numeric value is given, awk should just turn * it into a character and print it: * BEGIN { printf("%c\n", 65) } * prints "A". * * But what if the numeric value is > 128 and * represents a valid Unicode code point?!? We do * our best to convert it back into UTF-8. If we * can't, we output the encoding of the Unicode * "invalid character", 0xFFFD. */ if (isnum(x)) { int charval = (int) getfval(x); if (charval != 0) { if (charval < 128 || awk_mb_cur_max == 1) snprintf(p, BUFSZ(p), fmt, charval); else { // possible unicode character size_t count; char *bs = wide_char_to_byte_str(charval, &count); if (bs == NULL) { // invalid character // use unicode invalid character, 0xFFFD - bs = "\357\277\275"; + static char invalid_char[] = "\357\277\275"; + bs = invalid_char; count = 3; } t = bs; n = count; goto format_percent_c; } } else { *p++ = '\0'; /* explicit null byte */ *p = '\0'; /* next output will start here */ } break; } t = getsval(x); n = u8_nextlen(t); format_percent_c: if (n < 2) { /* not utf8 */ snprintf(p, BUFSZ(p), fmt, getsval(x)[0]); break; } // utf8 character, almost same song and dance as for %s int ljust = 0, wid = 0, prec = n, pad = 0; char *f = fmt+1; if (f[0] == '-') { ljust = 1; f++; } // flags '0' and '+' are recognized but skipped if (f[0] == '0') { f++; if (f[0] == '+') f++; } if (f[0] == '+') { f++; if (f[0] == '0') f++; } if (isdigit(f[0])) { /* there is a wid */ wid = strtol(f, &f, 10); } if (f[0] == '.') { /* there is a .prec */ prec = strtol(++f, &f, 10); } if (prec > 1) // %c --> only one character prec = 1; pad = wid>prec ? wid - prec : 0; // has to be >= 0 int i; if (ljust) { // print one char from t, then pad blanks for (i = 0; i < n; i++) *p++ = t[i]; for (i = 0; i < pad; i++) { //printf(" "); *p++ = ' '; } } else { // print pad blanks, then prec chars from t for (i = 0; i < pad; i++) { //printf(" "); *p++ = ' '; } for (i = 0; i < n; i++) *p++ = t[i]; } *p = 0; break; } default: FATAL("can't happen: bad conversion %c in format()", flag); } tempfree(x); p += strlen(p); s++; } *p = '\0'; free(fmt); for ( ; a; a = a->nnext) { /* evaluate any remaining args */ x = execute(a); tempfree(x); } *pbuf = buf; *pbufsize = bufsize; return p - buf; } Cell *awksprintf(Node **a, int n) /* sprintf(a[0]) */ { Cell *x; Node *y; char *buf; int bufsz=3*recsize; if ((buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in awksprintf"); y = a[0]->nnext; x = execute(a[0]); if (format(&buf, &bufsz, getsval(x), y) == -1) FATAL("sprintf string %.30s... too long. can't happen.", buf); tempfree(x); x = gettemp(); x->sval = buf; x->tval = STR; return(x); } Cell *awkprintf(Node **a, int n) /* printf */ { /* a[0] is list of args, starting with format string */ /* a[1] is redirection operator, a[2] is redirection file */ FILE *fp; Cell *x; Node *y; char *buf; int len; int bufsz=3*recsize; if ((buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in awkprintf"); y = a[0]->nnext; x = execute(a[0]); if ((len = format(&buf, &bufsz, getsval(x), y)) == -1) FATAL("printf string %.30s... too long. can't happen.", buf); tempfree(x); if (a[1] == NULL) { /* fputs(buf, stdout); */ fwrite(buf, len, 1, stdout); if (ferror(stdout)) FATAL("write error on stdout"); } else { fp = redirect(ptoi(a[1]), a[2]); /* fputs(buf, fp); */ fwrite(buf, len, 1, fp); fflush(fp); if (ferror(fp)) FATAL("write error on %s", filename(fp)); } free(buf); return(True); } Cell *arith(Node **a, int n) /* a[0] + a[1], etc. also -a[0] */ { Awkfloat i, j = 0; double v; Cell *x, *y, *z; x = execute(a[0]); i = getfval(x); tempfree(x); if (n != UMINUS && n != UPLUS) { y = execute(a[1]); j = getfval(y); tempfree(y); } z = gettemp(); switch (n) { case ADD: i += j; break; case MINUS: i -= j; break; case MULT: i *= j; break; case DIVIDE: if (j == 0) FATAL("division by zero"); i /= j; break; case MOD: if (j == 0) FATAL("division by zero in mod"); modf(i/j, &v); i = i - j * v; break; case UMINUS: i = -i; break; case UPLUS: /* handled by getfval(), above */ break; case POWER: if (j >= 0 && modf(j, &v) == 0.0) /* pos integer exponent */ i = ipow(i, (int) j); else { errno = 0; i = errcheck(pow(i, j), "pow"); } break; default: /* can't happen */ FATAL("illegal arithmetic operator %d", n); } setfval(z, i); return(z); } double ipow(double x, int n) /* x**n. ought to be done by pow, but isn't always */ { double v; if (n <= 0) return 1; v = ipow(x, n/2); if (n % 2 == 0) return v * v; else return x * v * v; } Cell *incrdecr(Node **a, int n) /* a[0]++, etc. */ { Cell *x, *z; int k; Awkfloat xf; x = execute(a[0]); xf = getfval(x); k = (n == PREINCR || n == POSTINCR) ? 1 : -1; if (n == PREINCR || n == PREDECR) { setfval(x, xf + k); return(x); } z = gettemp(); setfval(z, xf); setfval(x, xf + k); tempfree(x); return(z); } Cell *assign(Node **a, int n) /* a[0] = a[1], a[0] += a[1], etc. */ { /* this is subtle; don't muck with it. */ Cell *x, *y; Awkfloat xf, yf; double v; y = execute(a[1]); x = execute(a[0]); if (n == ASSIGN) { /* ordinary assignment */ if (x == y && !(x->tval & (FLD|REC)) && x != nfloc) ; /* self-assignment: leave alone unless it's a field or NF */ else if ((y->tval & (STR|NUM)) == (STR|NUM)) { yf = getfval(y); setsval(x, getsval(y)); x->fval = yf; x->tval |= NUM; } else if (isstr(y)) setsval(x, getsval(y)); else if (isnum(y)) setfval(x, getfval(y)); else funnyvar(y, "read value of"); tempfree(y); return(x); } xf = getfval(x); yf = getfval(y); switch (n) { case ADDEQ: xf += yf; break; case SUBEQ: xf -= yf; break; case MULTEQ: xf *= yf; break; case DIVEQ: if (yf == 0) FATAL("division by zero in /="); xf /= yf; break; case MODEQ: if (yf == 0) FATAL("division by zero in %%="); modf(xf/yf, &v); xf = xf - yf * v; break; case POWEQ: if (yf >= 0 && modf(yf, &v) == 0.0) /* pos integer exponent */ xf = ipow(xf, (int) yf); else { errno = 0; xf = errcheck(pow(xf, yf), "pow"); } break; default: FATAL("illegal assignment operator %d", n); break; } tempfree(y); setfval(x, xf); return(x); } Cell *cat(Node **a, int q) /* a[0] cat a[1] */ { Cell *x, *y, *z; int n1, n2; char *s = NULL; int ssz = 0; x = execute(a[0]); n1 = strlen(getsval(x)); adjbuf(&s, &ssz, n1 + 1, recsize, 0, "cat1"); memcpy(s, x->sval, n1); tempfree(x); y = execute(a[1]); n2 = strlen(getsval(y)); adjbuf(&s, &ssz, n1 + n2 + 1, recsize, 0, "cat2"); memcpy(s + n1, y->sval, n2); s[n1 + n2] = '\0'; tempfree(y); z = gettemp(); z->sval = s; z->tval = STR; return(z); } Cell *pastat(Node **a, int n) /* a[0] { a[1] } */ { Cell *x; if (a[0] == NULL) x = execute(a[1]); else { x = execute(a[0]); if (istrue(x)) { tempfree(x); x = execute(a[1]); } } return x; } Cell *dopa2(Node **a, int n) /* a[0], a[1] { a[2] } */ { Cell *x; int pair; pair = ptoi(a[3]); if (pairstack[pair] == 0) { x = execute(a[0]); if (istrue(x)) pairstack[pair] = 1; tempfree(x); } if (pairstack[pair] == 1) { x = execute(a[1]); if (istrue(x)) pairstack[pair] = 0; tempfree(x); x = execute(a[2]); return(x); } return(False); } Cell *split(Node **a, int nnn) /* split(a[0], a[1], a[2]); a[3] is type */ { Cell *x = NULL, *y, *ap; const char *s, *origs, *t; const char *fs = NULL; char *origfs = NULL; int sep; char temp, num[50]; int n, tempstat, arg3type; int j; double result; y = execute(a[0]); /* source string */ origs = s = strdup(getsval(y)); tempfree(y); arg3type = ptoi(a[3]); if (a[2] == NULL) { /* BUG: CSV should override implicit fs but not explicit */ fs = getsval(fsloc); } else if (arg3type == STRING) { /* split(str,arr,"string") */ x = execute(a[2]); fs = origfs = strdup(getsval(x)); tempfree(x); } else if (arg3type == REGEXPR) { fs = "(regexpr)"; /* split(str,arr,/regexpr/) */ } else { FATAL("illegal type of split"); } sep = *fs; ap = execute(a[1]); /* array name */ /* BUG 7/26/22: this appears not to reset array: see C1/asplit */ freesymtab(ap); DPRINTF("split: s=|%s|, a=%s, sep=|%s|\n", s, NN(ap->nval), fs); ap->tval &= ~STR; ap->tval |= ARR; ap->sval = (char *) makesymtab(NSYMTAB); n = 0; if (arg3type == REGEXPR && strlen((char*)((fa*)a[2])->restr) == 0) { /* split(s, a, //); have to arrange that it looks like empty sep */ arg3type = 0; fs = ""; sep = 0; } if (*s != '\0' && (strlen(fs) > 1 || arg3type == REGEXPR)) { /* reg expr */ fa *pfa; if (arg3type == REGEXPR) { /* it's ready already */ pfa = (fa *) a[2]; } else { pfa = makedfa(fs, 1); } if (nematch(pfa,s)) { tempstat = pfa->initstat; pfa->initstat = 2; do { n++; snprintf(num, sizeof(num), "%d", n); temp = *patbeg; setptr(patbeg, '\0'); if (is_number(s, & result)) setsymtab(num, s, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, s, 0.0, STR, (Array *) ap->sval); setptr(patbeg, temp); s = patbeg + patlen; if (*(patbeg+patlen-1) == '\0' || *s == '\0') { n++; snprintf(num, sizeof(num), "%d", n); setsymtab(num, "", 0.0, STR, (Array *) ap->sval); pfa->initstat = tempstat; goto spdone; } } while (nematch(pfa,s)); pfa->initstat = tempstat; /* bwk: has to be here to reset */ /* cf gsub and refldbld */ } n++; snprintf(num, sizeof(num), "%d", n); if (is_number(s, & result)) setsymtab(num, s, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, s, 0.0, STR, (Array *) ap->sval); spdone: pfa = NULL; } else if (a[2] == NULL && CSV) { /* CSV only if no explicit separator */ char *newt = (char *) malloc(strlen(s)); /* for building new string; reuse for each field */ for (;;) { char *fr = newt; n++; if (*s == '"' ) { /* start of "..." */ for (s++ ; *s != '\0'; ) { if (*s == '"' && s[1] != '\0' && s[1] == '"') { s += 2; /* doubled quote */ *fr++ = '"'; } else if (*s == '"' && (s[1] == '\0' || s[1] == ',')) { s++; /* skip over closing quote */ break; } else { *fr++ = *s++; } } *fr++ = 0; } else { /* unquoted field */ while (*s != ',' && *s != '\0') *fr++ = *s++; *fr++ = 0; } snprintf(num, sizeof(num), "%d", n); if (is_number(newt, &result)) setsymtab(num, newt, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, newt, 0.0, STR, (Array *) ap->sval); if (*s++ == '\0') break; } free(newt); } else if (!CSV && sep == ' ') { /* usual case: split on white space */ for (n = 0; ; ) { #define ISWS(c) ((c) == ' ' || (c) == '\t' || (c) == '\n') while (ISWS(*s)) s++; if (*s == '\0') break; n++; t = s; do s++; while (*s != '\0' && !ISWS(*s)); temp = *s; setptr(s, '\0'); snprintf(num, sizeof(num), "%d", n); if (is_number(t, & result)) setsymtab(num, t, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, t, 0.0, STR, (Array *) ap->sval); setptr(s, temp); if (*s != '\0') s++; } } else if (sep == 0) { /* new: split(s, a, "") => 1 char/elem */ for (n = 0; *s != '\0'; s += u8_nextlen(s)) { char buf[10]; n++; snprintf(num, sizeof(num), "%d", n); for (j = 0; j < u8_nextlen(s); j++) { buf[j] = s[j]; } buf[j] = '\0'; if (isdigit((uschar)buf[0])) setsymtab(num, buf, atof(buf), STR|NUM, (Array *) ap->sval); else setsymtab(num, buf, 0.0, STR, (Array *) ap->sval); } } else if (*s != '\0') { /* some random single character */ for (;;) { n++; t = s; while (*s != sep && *s != '\n' && *s != '\0') s++; temp = *s; setptr(s, '\0'); snprintf(num, sizeof(num), "%d", n); if (is_number(t, & result)) setsymtab(num, t, result, STR|NUM, (Array *) ap->sval); else setsymtab(num, t, 0.0, STR, (Array *) ap->sval); setptr(s, temp); if (*s++ == '\0') break; } } tempfree(ap); xfree(origs); xfree(origfs); x = gettemp(); x->tval = NUM; x->fval = n; return(x); } Cell *condexpr(Node **a, int n) /* a[0] ? a[1] : a[2] */ { Cell *x; x = execute(a[0]); if (istrue(x)) { tempfree(x); x = execute(a[1]); } else { tempfree(x); x = execute(a[2]); } return(x); } Cell *ifstat(Node **a, int n) /* if (a[0]) a[1]; else a[2] */ { Cell *x; x = execute(a[0]); if (istrue(x)) { tempfree(x); x = execute(a[1]); } else if (a[2] != NULL) { tempfree(x); x = execute(a[2]); } return(x); } Cell *whilestat(Node **a, int n) /* while (a[0]) a[1] */ { Cell *x; for (;;) { x = execute(a[0]); if (!istrue(x)) return(x); tempfree(x); x = execute(a[1]); if (isbreak(x)) { x = True; return(x); } if (isnext(x) || isexit(x) || isret(x)) return(x); tempfree(x); } } Cell *dostat(Node **a, int n) /* do a[0]; while(a[1]) */ { Cell *x; for (;;) { x = execute(a[0]); if (isbreak(x)) return True; if (isnext(x) || isexit(x) || isret(x)) return(x); tempfree(x); x = execute(a[1]); if (!istrue(x)) return(x); tempfree(x); } } Cell *forstat(Node **a, int n) /* for (a[0]; a[1]; a[2]) a[3] */ { Cell *x; x = execute(a[0]); tempfree(x); for (;;) { if (a[1]!=NULL) { x = execute(a[1]); if (!istrue(x)) return(x); else tempfree(x); } x = execute(a[3]); if (isbreak(x)) /* turn off break */ return True; if (isnext(x) || isexit(x) || isret(x)) return(x); tempfree(x); x = execute(a[2]); tempfree(x); } } Cell *instat(Node **a, int n) /* for (a[0] in a[1]) a[2] */ { Cell *x, *vp, *arrayp, *cp, *ncp; Array *tp; int i; vp = execute(a[0]); arrayp = execute(a[1]); if (!isarr(arrayp)) { return True; } tp = (Array *) arrayp->sval; tempfree(arrayp); for (i = 0; i < tp->size; i++) { /* this routine knows too much */ for (cp = tp->tab[i]; cp != NULL; cp = ncp) { setsval(vp, cp->nval); ncp = cp->cnext; x = execute(a[2]); if (isbreak(x)) { tempfree(vp); return True; } if (isnext(x) || isexit(x) || isret(x)) { tempfree(vp); return(x); } tempfree(x); } } return True; } static char *nawk_convert(const char *s, int (*fun_c)(int), wint_t (*fun_wc)(wint_t)) { char *buf = NULL; char *pbuf = NULL; const char *ps = NULL; size_t n = 0; wchar_t wc; const size_t sz = awk_mb_cur_max; int unused; if (sz == 1) { buf = tostring(s); for (pbuf = buf; *pbuf; pbuf++) *pbuf = fun_c((uschar)*pbuf); return buf; } else { /* upper/lower character may be shorter/longer */ buf = tostringN(s, strlen(s) * sz + 1); (void) mbtowc(NULL, NULL, 0); /* reset internal state */ /* * Reset internal state here too. * Assign result to avoid a compiler warning. (Casting to void * doesn't work.) * Increment said variable to avoid a different warning. */ unused = wctomb(NULL, L'\0'); unused++; ps = s; pbuf = buf; while (n = mbtowc(&wc, ps, sz), n > 0 && n != (size_t)-1 && n != (size_t)-2) { ps += n; n = wctomb(pbuf, fun_wc(wc)); if (n == (size_t)-1) FATAL("illegal wide character %s", s); pbuf += n; } *pbuf = '\0'; if (n) FATAL("illegal byte sequence %s", s); return buf; } } #ifdef __DJGPP__ static wint_t towupper(wint_t wc) { if (wc >= 0 && wc < 256) return toupper(wc & 0xFF); return wc; } static wint_t towlower(wint_t wc) { if (wc >= 0 && wc < 256) return tolower(wc & 0xFF); return wc; } #endif static char *nawk_toupper(const char *s) { return nawk_convert(s, toupper, towupper); } static char *nawk_tolower(const char *s) { return nawk_convert(s, tolower, towlower); } Cell *bltin(Node **a, int n) /* builtin functions. a[0] is type, a[1] is arg list */ { Cell *x, *y; Awkfloat u; int t, sz; Awkfloat tmp; char *buf, *fmt; Node *nextarg; FILE *fp; int status = 0; time_t tv; struct tm *tm; + int estatus = 0; t = ptoi(a[0]); x = execute(a[1]); nextarg = a[1]->nnext; switch (t) { case FLENGTH: if (isarr(x)) u = ((Array *) x->sval)->nelem; /* GROT. should be function*/ else u = u8_strlen(getsval(x)); break; case FLOG: errno = 0; u = errcheck(log(getfval(x)), "log"); break; case FINT: modf(getfval(x), &u); break; case FEXP: errno = 0; u = errcheck(exp(getfval(x)), "exp"); break; case FSQRT: errno = 0; u = errcheck(sqrt(getfval(x)), "sqrt"); break; case FSIN: u = sin(getfval(x)); break; case FCOS: u = cos(getfval(x)); break; case FATAN: if (nextarg == NULL) { WARNING("atan2 requires two arguments; returning 1.0"); u = 1.0; } else { y = execute(a[1]->nnext); u = atan2(getfval(x), getfval(y)); tempfree(y); nextarg = nextarg->nnext; } break; case FCOMPL: u = ~((int)getfval(x)); break; case FAND: if (nextarg == 0) { WARNING("and requires two arguments; returning 0"); u = 0; break; } y = execute(a[1]->nnext); u = ((int)getfval(x)) & ((int)getfval(y)); tempfree(y); nextarg = nextarg->nnext; break; case FFOR: if (nextarg == 0) { WARNING("or requires two arguments; returning 0"); u = 0; break; } y = execute(a[1]->nnext); u = ((int)getfval(x)) | ((int)getfval(y)); tempfree(y); nextarg = nextarg->nnext; break; case FXOR: if (nextarg == 0) { WARNING("xor requires two arguments; returning 0"); u = 0; break; } y = execute(a[1]->nnext); u = ((int)getfval(x)) ^ ((int)getfval(y)); tempfree(y); nextarg = nextarg->nnext; break; case FLSHIFT: if (nextarg == 0) { WARNING("lshift requires two arguments; returning 0"); u = 0; break; } y = execute(a[1]->nnext); u = ((int)getfval(x)) << ((int)getfval(y)); tempfree(y); nextarg = nextarg->nnext; break; case FRSHIFT: if (nextarg == 0) { WARNING("rshift requires two arguments; returning 0"); u = 0; break; } y = execute(a[1]->nnext); u = ((int)getfval(x)) >> ((int)getfval(y)); tempfree(y); nextarg = nextarg->nnext; break; case FSYSTEM: fflush(stdout); /* in case something is buffered already */ - status = system(getsval(x)); - u = status; + estatus = status = system(getsval(x)); if (status != -1) { if (WIFEXITED(status)) { - u = WEXITSTATUS(status); + estatus = WEXITSTATUS(status); } else if (WIFSIGNALED(status)) { - u = WTERMSIG(status) + 256; + estatus = WTERMSIG(status) + 256; #ifdef WCOREDUMP if (WCOREDUMP(status)) - u += 256; + estatus += 256; #endif } else /* something else?!? */ - u = 0; + estatus = 0; } + /* else estatus was set to -1 */ + u = estatus; break; case FRAND: /* random() returns numbers in [0..2^31-1] * in order to get a number in [0, 1), divide it by 2^31 */ u = (Awkfloat) random() / (0x7fffffffL + 0x1UL); break; case FSRAND: if (isrec(x)) /* no argument provided */ u = time((time_t *)0); else u = getfval(x); tmp = u; srandom((unsigned long) u); u = srand_seed; srand_seed = tmp; break; case FTOUPPER: case FTOLOWER: if (t == FTOUPPER) buf = nawk_toupper(getsval(x)); else buf = nawk_tolower(getsval(x)); tempfree(x); x = gettemp(); setsval(x, buf); free(buf); return x; case FFLUSH: if (isrec(x) || strlen(getsval(x)) == 0) { flush_all(); /* fflush() or fflush("") -> all */ u = 0; } else if ((fp = openfile(FFLUSH, getsval(x), NULL)) == NULL) u = EOF; else u = fflush(fp); break; case FSYSTIME: u = time((time_t *) 0); break; case FSTRFTIME: /* strftime([format [,timestamp]]) */ if (nextarg) { y = execute(nextarg); nextarg = nextarg->nnext; tv = (time_t) getfval(y); tempfree(y); } else tv = time((time_t *) 0); tm = localtime(&tv); if (tm == NULL) FATAL("bad time %ld", (long)tv); if (isrec(x)) { /* format argument not provided, use default */ fmt = tostring("%a %b %d %H:%M:%S %Z %Y"); } else fmt = tostring(getsval(x)); sz = 32; buf = NULL; do { if ((buf = realloc(buf, (sz *= 2))) == NULL) FATAL("out of memory in strftime"); } while (strftime(buf, sz, fmt, tm) == 0 && fmt[0] != '\0'); y = gettemp(); setsval(y, buf); free(fmt); free(buf); return y; default: /* can't happen */ FATAL("illegal function type %d", t); break; } tempfree(x); x = gettemp(); setfval(x, u); if (nextarg != NULL) { WARNING("warning: function has too many arguments"); for ( ; nextarg; nextarg = nextarg->nnext) { y = execute(nextarg); tempfree(y); } } return(x); } Cell *printstat(Node **a, int n) /* print a[0] */ { Node *x; Cell *y; FILE *fp; if (a[1] == NULL) /* a[1] is redirection operator, a[2] is file */ fp = stdout; else fp = redirect(ptoi(a[1]), a[2]); for (x = a[0]; x != NULL; x = x->nnext) { y = execute(x); fputs(getpssval(y), fp); tempfree(y); if (x->nnext == NULL) fputs(getsval(orsloc), fp); else fputs(getsval(ofsloc), fp); } if (a[1] != NULL) fflush(fp); if (ferror(fp)) FATAL("write error on %s", filename(fp)); return(True); } Cell *nullproc(Node **a, int n) { return 0; } FILE *redirect(int a, Node *b) /* set up all i/o redirections */ { FILE *fp; Cell *x; char *fname; x = execute(b); fname = getsval(x); fp = openfile(a, fname, NULL); if (fp == NULL) FATAL("can't open file %s", fname); tempfree(x); return fp; } struct files { FILE *fp; const char *fname; int mode; /* '|', 'a', 'w' => LE/LT, GT */ } *files; size_t nfiles; static void stdinit(void) /* in case stdin, etc., are not constants */ { nfiles = FOPEN_MAX; files = (struct files *) calloc(nfiles, sizeof(*files)); if (files == NULL) FATAL("can't allocate file memory for %zu files", nfiles); files[0].fp = stdin; files[0].fname = tostring("/dev/stdin"); files[0].mode = LT; files[1].fp = stdout; files[1].fname = tostring("/dev/stdout"); files[1].mode = GT; files[2].fp = stderr; files[2].fname = tostring("/dev/stderr"); files[2].mode = GT; } FILE *openfile(int a, const char *us, bool *pnewflag) { const char *s = us; size_t i; int m; FILE *fp = NULL; if (*s == '\0') FATAL("null file name in print or getline"); for (i = 0; i < nfiles; i++) if (files[i].fname && strcmp(s, files[i].fname) == 0 && (a == files[i].mode || (a==APPEND && files[i].mode==GT) || a == FFLUSH)) { if (pnewflag) *pnewflag = false; return files[i].fp; } if (a == FFLUSH) /* didn't find it, so don't create it! */ return NULL; for (i = 0; i < nfiles; i++) if (files[i].fp == NULL) break; if (i >= nfiles) { struct files *nf; size_t nnf = nfiles + FOPEN_MAX; 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)); nfiles = nnf; files = nf; } fflush(stdout); /* force a semblance of order */ m = a; if (a == GT) { fp = fopen(s, "w"); } else if (a == APPEND) { fp = fopen(s, "a"); m = GT; /* so can mix > and >> */ } else if (a == '|') { /* output pipe */ fp = popen(s, "w"); } else if (a == LE) { /* input pipe */ fp = popen(s, "r"); } else if (a == LT) { /* getline sval, files[i].fname) != 0) continue; if (files[i].mode == GT || files[i].mode == '|') fflush(files[i].fp); if (ferror(files[i].fp)) { if ((files[i].mode == GT && files[i].fp != stderr) || files[i].mode == '|') FATAL("write error on %s", files[i].fname); else WARNING("i/o error occurred on %s", files[i].fname); } if (files[i].fp == stdin || files[i].fp == stdout || files[i].fp == stderr) stat = freopen("/dev/null", "r+", files[i].fp) == NULL; else if (files[i].mode == '|' || files[i].mode == LE) stat = pclose(files[i].fp) == -1; else stat = fclose(files[i].fp) == EOF; if (stat) WARNING("i/o error occurred closing %s", files[i].fname); xfree(files[i].fname); files[i].fname = NULL; /* watch out for ref thru this */ files[i].fp = NULL; break; } tempfree(x); x = gettemp(); setfval(x, (Awkfloat) (stat ? -1 : 0)); return(x); } void closeall(void) { size_t i; bool stat = false; for (i = 0; i < nfiles; i++) { if (! files[i].fp) continue; if (files[i].mode == GT || files[i].mode == '|') fflush(files[i].fp); if (ferror(files[i].fp)) { if ((files[i].mode == GT && files[i].fp != stderr) || files[i].mode == '|') FATAL("write error on %s", files[i].fname); else WARNING("i/o error occurred on %s", files[i].fname); } if (files[i].fp == stdin || files[i].fp == stdout || files[i].fp == stderr) continue; if (files[i].mode == '|' || files[i].mode == LE) stat = pclose(files[i].fp) == -1; else stat = fclose(files[i].fp) == EOF; if (stat) WARNING("i/o error occurred while closing %s", files[i].fname); } } static void flush_all(void) { size_t i; for (i = 0; i < nfiles; i++) if (files[i].fp) fflush(files[i].fp); } void backsub(char **pb_ptr, const char **sptr_ptr); Cell *dosub(Node **a, int subop) /* sub and gsub */ { fa *pfa; int tempstat; char *repl; Cell *x; char *buf = NULL; char *pb = NULL; int bufsz = recsize; const char *r, *s; const char *start; const char *noempty = NULL; /* empty match disallowed here */ size_t m = 0; /* match count */ size_t whichm; /* which match to select, 0 = global */ int mtype; /* match type */ if (a[0] == NULL) { /* 0 => a[1] is already-compiled regexpr */ pfa = (fa *) a[1]; } else { x = execute(a[1]); pfa = makedfa(getsval(x), 1); tempfree(x); } x = execute(a[2]); /* replacement string */ repl = tostring(getsval(x)); tempfree(x); switch (subop) { case SUB: whichm = 1; x = execute(a[3]); /* source string */ break; case GSUB: whichm = 0; x = execute(a[3]); /* source string */ break; default: FATAL("dosub: unrecognized subop: %d", subop); } start = getsval(x); while (pmatch(pfa, start)) { if (buf == NULL) { - if ((pb = buf = malloc(bufsz)) == NULL) + if ((pb = buf = (char *) malloc(bufsz)) == NULL) FATAL("out of memory in dosub"); tempstat = pfa->initstat; pfa->initstat = 2; } /* match types */ #define MT_IGNORE 0 /* unselected or invalid */ #define MT_INSERT 1 /* selected, empty */ #define MT_REPLACE 2 /* selected, not empty */ /* an empty match just after replacement is invalid */ if (patbeg == noempty && patlen == 0) { mtype = MT_IGNORE; /* invalid, not counted */ } else if (whichm == ++m || whichm == 0) { mtype = patlen ? MT_REPLACE : MT_INSERT; } else { mtype = MT_IGNORE; /* unselected, but counted */ } /* leading text: */ if (patbeg > start) { adjbuf(&buf, &bufsz, (pb - buf) + (patbeg - start), recsize, &pb, "dosub"); s = start; while (s < patbeg) *pb++ = *s++; } if (mtype == MT_IGNORE) goto matching_text; /* skip replacement text */ r = repl; while (*r != 0) { adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "dosub"); if (*r == '\\') { backsub(&pb, &r); } else if (*r == '&') { r++; adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "dosub"); for (s = patbeg; s < patbeg+patlen; ) *pb++ = *s++; } else { *pb++ = *r++; } } matching_text: if (mtype == MT_REPLACE || *patbeg == '\0') goto next_search; /* skip matching text */ if (patlen == 0) patlen = u8_nextlen(patbeg); adjbuf(&buf, &bufsz, (pb-buf) + patlen, recsize, &pb, "dosub"); s = patbeg; while (s < patbeg + patlen) *pb++ = *s++; next_search: start = patbeg + patlen; if (m == whichm || *patbeg == '\0') break; if (mtype == MT_REPLACE) noempty = start; #undef MT_IGNORE #undef MT_INSERT #undef MT_REPLACE } xfree(repl); if (buf != NULL) { pfa->initstat = tempstat; /* trailing text */ adjbuf(&buf, &bufsz, 1+strlen(start)+pb-buf, 0, &pb, "dosub"); while ((*pb++ = *start++) != '\0') ; setsval(x, buf); free(buf); } tempfree(x); x = gettemp(); x->tval = NUM; x->fval = m; return x; } Cell *gensub(Node **a, int nnn) /* global selective substitute */ /* XXX incomplete - doesn't support backreferences \0 ... \9 */ { Cell *x, *y, *res, *h; char *rptr; const char *sptr; char *buf, *pb; const char *t, *q; fa *pfa; int mflag, tempstat, num, whichm; int bufsz = recsize; if ((buf = malloc(bufsz)) == NULL) FATAL("out of memory in gensub"); mflag = 0; /* if mflag == 0, can replace empty string */ num = 0; x = execute(a[4]); /* source string */ t = getsval(x); res = copycell(x); /* target string - initially copy of source */ res->csub = CTEMP; /* result values are temporary */ if (a[0] == 0) /* 0 => a[1] is already-compiled regexpr */ pfa = (fa *) a[1]; /* regular expression */ else { y = execute(a[1]); pfa = makedfa(getsval(y), 1); tempfree(y); } y = execute(a[2]); /* replacement string */ h = execute(a[3]); /* which matches should be replaced */ sptr = getsval(h); if (sptr[0] == 'g' || sptr[0] == 'G') whichm = -1; else { /* * The specified number is index of replacement, starting * from 1. GNU awk treats index lower than 0 same as * 1, we do same for compatibility. */ whichm = (int) getfval(h) - 1; if (whichm < 0) whichm = 0; } tempfree(h); if (pmatch(pfa, t)) { char *sl; tempstat = pfa->initstat; pfa->initstat = 2; pb = buf; rptr = getsval(y); /* * XXX if there are any backreferences in subst string, * complain now. */ for (sl = rptr; (sl = strchr(sl, '\\')) && sl[1]; sl++) { if (strchr("0123456789", sl[1])) { FATAL("gensub doesn't support backreferences (subst \"%s\")", rptr); } } do { if (whichm >= 0 && whichm != num) { num++; adjbuf(&buf, &bufsz, (pb - buf) + (patbeg - t) + patlen, recsize, &pb, "gensub"); /* copy the part of string up to and including * match to output buffer */ while (t < patbeg + patlen) *pb++ = *t++; continue; } if (patlen == 0 && *patbeg != 0) { /* matched empty string */ if (mflag == 0) { /* can replace empty */ num++; sptr = rptr; while (*sptr != 0) { adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gensub"); if (*sptr == '\\') { backsub(&pb, &sptr); } else if (*sptr == '&') { sptr++; adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gensub"); for (q = patbeg; q < patbeg+patlen; ) *pb++ = *q++; } else *pb++ = *sptr++; } } if (*t == 0) /* at end */ goto done; adjbuf(&buf, &bufsz, 2+pb-buf, recsize, &pb, "gensub"); *pb++ = *t++; if (pb > buf + bufsz) /* BUG: not sure of this test */ FATAL("gensub result0 %.30s too big; can't happen", buf); mflag = 0; } else { /* matched nonempty string */ num++; sptr = t; adjbuf(&buf, &bufsz, 1+(patbeg-sptr)+pb-buf, recsize, &pb, "gensub"); while (sptr < patbeg) *pb++ = *sptr++; sptr = rptr; while (*sptr != 0) { adjbuf(&buf, &bufsz, 5+pb-buf, recsize, &pb, "gensub"); if (*sptr == '\\') { backsub(&pb, &sptr); } else if (*sptr == '&') { sptr++; adjbuf(&buf, &bufsz, 1+patlen+pb-buf, recsize, &pb, "gensub"); for (q = patbeg; q < patbeg+patlen; ) *pb++ = *q++; } else *pb++ = *sptr++; } t = patbeg + patlen; if (patlen == 0 || *t == 0 || *(t-1) == 0) goto done; if (pb > buf + bufsz) FATAL("gensub result1 %.30s too big; can't happen", buf); mflag = 1; } } while (pmatch(pfa,t)); sptr = t; adjbuf(&buf, &bufsz, 1+strlen(sptr)+pb-buf, 0, &pb, "gensub"); while ((*pb++ = *sptr++) != 0) ; done: if (pb > buf + bufsz) FATAL("gensub result2 %.30s too big; can't happen", buf); *pb = '\0'; setsval(res, buf); pfa->initstat = tempstat; } tempfree(x); tempfree(y); free(buf); return(res); } void backsub(char **pb_ptr, const char **sptr_ptr) /* handle \\& variations */ { /* sptr[0] == '\\' */ char *pb = *pb_ptr; const char *sptr = *sptr_ptr; static bool first = true; static bool do_posix = false; if (first) { first = false; do_posix = (getenv("POSIXLY_CORRECT") != NULL); } if (sptr[1] == '\\') { if (sptr[2] == '\\' && sptr[3] == '&') { /* \\\& -> \& */ *pb++ = '\\'; *pb++ = '&'; sptr += 4; } else if (sptr[2] == '&') { /* \\& -> \ + matched */ *pb++ = '\\'; sptr += 2; } else if (do_posix) { /* \\x -> \x */ sptr++; *pb++ = *sptr++; } else { /* \\x -> \\x */ *pb++ = *sptr++; *pb++ = *sptr++; } } else if (sptr[1] == '&') { /* literal & */ sptr++; *pb++ = *sptr++; } else /* literal \ */ *pb++ = *sptr++; *pb_ptr = pb; *sptr_ptr = sptr; } static char *wide_char_to_byte_str(int rune, size_t *outlen) { static char buf[5]; int len; if (rune < 0 || rune > 0x10FFFF) return NULL; memset(buf, 0, sizeof(buf)); len = 0; if (rune <= 0x0000007F) { buf[len++] = rune; } else if (rune <= 0x000007FF) { // 110xxxxx 10xxxxxx buf[len++] = 0xC0 | (rune >> 6); buf[len++] = 0x80 | (rune & 0x3F); } else if (rune <= 0x0000FFFF) { // 1110xxxx 10xxxxxx 10xxxxxx buf[len++] = 0xE0 | (rune >> 12); buf[len++] = 0x80 | ((rune >> 6) & 0x3F); buf[len++] = 0x80 | (rune & 0x3F); } else { // 0x00010000 - 0x10FFFF // 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx buf[len++] = 0xF0 | (rune >> 18); buf[len++] = 0x80 | ((rune >> 12) & 0x3F); buf[len++] = 0x80 | ((rune >> 6) & 0x3F); buf[len++] = 0x80 | (rune & 0x3F); } *outlen = len; buf[len++] = '\0'; return buf; } diff --git a/testdir/T.csv b/testdir/T.csv index 79c15104cfb3..e0f3d708edaf 100755 --- a/testdir/T.csv +++ b/testdir/T.csv @@ -1,80 +1,80 @@ #!/bin/sh echo T.csv: tests of csv field splitting, no embedded newlines awk=${awk-../a.out} $awk ' BEGIN { FS = "\t" awk = "../a.out --csv" } NF == 0 || $1 ~ /^#/ { next } $1 ~ /try/ { # new test nt++ sub(/try /, "") prog = $0 printf("%3d %s\n", nt, prog) - prog = sprintf("%s -F\"\\t\" '"'"'%s'"'"'", awk, prog) + prog = sprintf("%s '"'"'%s'"'"'", awk, prog) # print "prog is", prog nt2 = 0 while (getline > 0) { if (NF == 0) # blank line terminates a sequence break input = $1 for (i = 2; i < NF; i++) # input data input = input "\t" $i test = sprintf("./echo '"'"'%s'"'"' | %s >foo1; ", input, prog) if ($NF == "\"\"") output = ">foo2;" else output = sprintf("./echo '"'"'%s'"'"' >foo2; ", $NF) gsub(/\\t/, "\t", output) gsub(/\\n/, "\n", output) run = sprintf("cmp foo1 foo2 || echo test %d.%d failed", nt, ++nt2) # print "input is", input # print "test is", test # print "output is", output # print "run is", run system(test output run) } tt += nt2 } END { print tt, "tests" } ' <<\!!!! # General format: # try program as rest of line # $1 $2 $3 output1 (\t for tab, \n for newline, # $1 $2 $3 output2 ("" for null) # ... terminated by blank line try { for (i=1; i<=NF; i++) printf("[%s]", $i); printf("\n") } a [a] a [ a] ,a [][a] , a [ ][ a] a,b [a][b] a,b,c [a][b][c] "" [] "abc" [abc] "a""b" [a"b] "a","b" [a][b] a""b [a""b] "a,b" [a,b] """" ["] """""" [""] """x""" ["x"] """,""" [","] ,,"" [][][] a""b [a""b] a''b [a''b] ,, [][][] a, [a][] "", [][] , [][] !!!! diff --git a/testdir/T.overflow b/testdir/T.overflow index d3d97d4cc57d..ac9c0bdde9d8 100755 --- a/testdir/T.overflow +++ b/testdir/T.overflow @@ -1,86 +1,88 @@ echo T.overflow: test some overflow conditions awk=${awk-../a.out} $awk 'BEGIN { for (i = 0; i < 1000; i++) printf("abcdefghijklmnopqsrtuvwxyz") printf("\n") exit }' >foo1 $awk '{print}' foo1 >foo2 cmp -s foo1 foo2 || echo 'BAD: T.overflow record 1' echo 'abcdefghijklmnopqsrtuvwxyz' >foo1 echo hello | $awk ' { for (i = 1; i < 500; i++) s = s "abcdefghijklmnopqsrtuvwxyz " $0 = s print $1 }' >foo2 cmp -s foo1 foo2 || echo 'BAD: T.overflow abcdef' # default input record 3072, fields 200: $awk ' BEGIN { for (j = 0; j < 2; j++) { for (i = 0; i < 500; i++) printf(" 123456789") printf("\n"); } } ' >foo1 $awk '{$1 = " 123456789"; print}' foo1 >foo2 cmp -s foo1 foo2 || echo 'BAD: T.overflow -mr -mf set $1' $awk ' BEGIN { for (j = 0; j < 2; j++) { for (i = 0; i < 500; i++) printf(" 123456789") printf("\n"); } } ' >foo $awk '{print NF}' foo >foo1 echo '500 500' >foo2 cmp -s foo1 foo2 || echo 'BAD: T.overflow -mr -mf NF' rm -f core # this should not drop core $awk 'BEGIN { for (i = 1; i < 1000; i++) s = s "a-z" if ("x" ~ "[" s "]") print "ugh" }' >foo 2>foo test -r core && echo 1>&2 "BAD: T.overflow too long char class dropped core" echo 4000004 >foo1 $awk ' BEGIN { x1 = sprintf("%1000000s\n", "hello") x2 = sprintf("%-1000000s\n", "world") x3 = sprintf("%1000000.1000000s\n", "goodbye") x4 = sprintf("%-1000000.1000000s\n", "goodbye") print length(x1 x2 x3 x4) }' >foo2 cmp -s foo1 foo2 || echo 'BAD: T.overflow huge sprintfs' echo 0 >foo1 $awk ' BEGIN { for (i = 0; i < 100000; i++) x[i] = i for (i in x) delete x[i] n = 0 for (i in x) n++ print n }' >foo2 cmp -s foo1 foo2 || echo 'BAD: T.overflow big array' echo x >foo1 $awk '{print $40000000000000}' foo2 2>foo grep "out of range field" foo >/dev/null || echo 1>&2 "BAD: T.overflow \$400000" rm -rf /tmp/awktestfoo* $awk 'BEGIN { for (i=1; i <= 1000; i++) print i >("/tmp/awktestfoo" i) }' ls /tmp/awktestfoo* | grep '1000' >/dev/null || echo 1>&2 "BAD: T.overflow openfiles" +rm -rf /tmp/awktestfoo* +exit 0 diff --git a/testdir/T.split b/testdir/T.split index f7b24ba52d75..d938404b796b 100755 --- a/testdir/T.split +++ b/testdir/T.split @@ -1,224 +1,225 @@ #!/bin/sh awk=${awk-../a.out} WORKDIR=$(mktemp -d /tmp/nawktest.XXXXXX) TEMP0=$WORKDIR/test.temp.0 TEMP1=$WORKDIR/test.temp.1 TEMP2=$WORKDIR/test.temp.2 RESULT=0 fail() { echo "$1" >&2 RESULT=1 } echo T.split: misc tests of field splitting and split command $awk 'BEGIN { # Assign string to $0, then change FS. FS = ":" $0="a:bc:def" FS = "-" print FS, $1, NF # Assign number to $0, then change FS. FS = "2" $0=1212121 FS="3" print FS, $1, NF }' > $TEMP1 echo '- a 3 3 1 4' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.1' $awk 'BEGIN { # FS changes after getline. FS = ":" "echo a:bc:def" | getline FS = "-" print FS, $1, NF }' > $TEMP1 echo '- a 3' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.2' echo ' a a:b c:d:e e:f:g:h' > $TEMP0 $awk 'BEGIN { FS = ":" while (getline <"'$TEMP0'" > 0) print NF }' > $TEMP1 echo '0 1 2 3 4' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.3' # getline var shouldn't impact fields. echo 'f b a' > $TEMP0 $awk '{ FS = ":" getline a < "/etc/passwd" print $1 }' $TEMP0 > $TEMP1 echo 'f' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.4' echo 'a b c d foo e f g h i bar' > $TEMP0 $awk '{ FS=":" getline v print $2, NF FS=" " }' $TEMP0 > $TEMP1 echo 'b 4 f 5' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split 0.5' echo 'a.b.c=d.e.f g.h.i=j.k.l m.n.o=p.q.r' > $TEMP0 echo 'b h n' > $TEMP1 $awk 'BEGIN { FS="=" } { FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 1)' echo 'a.b.c=d.e.f g.h.i=j.k.l m.n.o=p.q.r' > $TEMP0 echo 'd.e.f b j.k.l h p.q.r n' > $TEMP1 $awk 'BEGIN { FS="=" } { print $2; FS="."; $0=$1; print $2; FS="="; }' $TEMP0 > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split (record assignment 2)' echo 'abc de f ' > $TEMP0 who | sed 10q >> $TEMP0 sed 10q /etc/passwd >> $TEMP0 $awk ' { n = split($0, x, "") m = length($0) if (m != n) print "error 1", NR s = "" for (i = 1; i <= m; i++) s = s x[i] if (s != $0) print "error 2", NR print s }' $TEMP0 > $TEMP1 diff $TEMP0 $TEMP1 || fail 'BAD: T.split 1' # assumes same test.temp.0! bad design $awk ' { n = split($0, x, //) m = length($0) if (m != n) print "error 1", NR s = "" for (i = 1; i <= m; i++) s = s x[i] if (s != $0) print "error 2", NR print s }' $TEMP0 > $TEMP1 diff $TEMP0 $TEMP1 || fail 'BAD: T.split //' $awk ' BEGIN { FS = "" } { n = split($0, x) # will be split with FS m = length($0) if (m != n) print "error 1", NR s = "" for (i = 1; i <= m; i++) s = s x[i] if (s != $0) print "error 2", NR print s }' $TEMP0 > $TEMP2 diff $TEMP0 $TEMP2 || fail 'BAD: T.split 2' # assumes same test.temp.0! $awk ' BEGIN { FS = "" } { n = NF m = length($0) if (m != n) print "error 1", NR s = "" for (i = 1; i <= m; i++) s = s $i if (s != $0) print "error 2", NR print s }' $TEMP0 > $TEMP2 diff $TEMP0 $TEMP2 || fail 'BAD: T.split 3' $awk ' { n = split( $0, temp, /^@@@ +/ ) print n }' > $TEMP1 < $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split 4' rm -f $WORKDIR/test.temp* echo ' a bc def' > $TEMP0 $awk ' { print split($0, x, "") }' $TEMP0 > $TEMP1 echo '0 1 2 3' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split null 3rd arg' rm -f $WORKDIR/test.temp* $awk 'BEGIN { a[1]="a b" print split(a[1],a),a[1],a[2] }' > $TEMP1 echo '2 a b' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a[1],a)' $awk 'BEGIN { a = "cat\n\n\ndog" split(a, b, "[\r\n]+") print b[1], b[2] }' > $TEMP1 echo 'cat dog' > $TEMP2 diff $TEMP1 $TEMP2 || fail 'BAD: T.split(a, b, "[\r\n]+")' +rm -rf $WORKDIR exit $RESULT