freebsd-src/awk.h
Warner Losh 2e406c584f ota: Import One True Awk from 20231102 (254b979f32df)
This is a rollup of a lot of changes. In summary, lots of bug fixes,
Unicode support and CSV support to match the 2nd Edition of the Awk
Book.

In detail, from AWK's FIXES and FIXES.1e:

From FIXES:
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.

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.

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.

	Unicode literals can be written as \u followed by one
	to eight hexadecimal digits.  These may appear in strings and
	regular expressions.

From FIXES.1e:

Sep 06, 2023:
	Fix edge case where FS is changed on commandline. Thanks to
	Gordon Shephard and Miguel Pineiro Jr.

	Fix regular expression clobbering in the lexer, where lexer does
	not make a copy of regexp literals. also makedfa memory leaks have
	been plugged. Thanks to Miguel Pineiro Jr.

Dec 15, 2022:
	Force hex escapes in strings to be no more than two characters,
	as they already are in regular expressions. This brings internal
	consistency, as well as consistency with gawk. Thanks to
	Arnold Robbins.

Sep 12, 2022:
	adjbuf minlen error (cannot be 0) in cat, resulting in NULL pbuf.
	discovered by todd miller. also use-after-free issue with
	tempfree in cat, thanks to Miguel Pineiro Jr and valgrind.

Aug 30, 2022:
	Various leaks and use-after-free issues plugged/fixed.
	Thanks to Miguel Pineiro Jr. <mpj@pineiro.cc>.

May 23, 2022:
	Memory leak when assigning a string to some of the built-in
	variables. allocated string erroneously marked DONTFREE.
	Thanks to Miguel Pineiro Jr. <mpj@pineiro.cc>.

Mar 14, 2022:
	Historic bug: command-line "name=value" assignment had been
	truncating its entry in ARGV. (circa 1989) Thanks to
	Miguel Pineiro Jr. <mpj@pineiro.cc>.

Mar 3, 2022:
	Fixed file management memory leak that appears to have been
	there since the files array was first initialized with stdin,
	stdout, and stderr (circa 1992). Thanks to Miguel Pineiro Jr.
	<mpj@pineiro.cc>.

December 8, 2021:
	The error handling in closefile and closeall was mangled. Long
	standing warnings had been made fatal and some fatal errors went
	undetected. Thanks to Miguel Pineiro Jr. <mpj@pineiro.cc>.

Nov 03, 2021:
        getline accesses uninitialized data after getrec()
	returns 0 on EOF and leaves the contents of buf unchanged.
	Thanks to Volodymyr Gubarkov, and Todd C Miller.

Oct 12, 2021:
	The fix for #83 changed the code to insert 2 chars, but the
	call to adjbuf just above it only allows for 1 char. This can
	cause a heap buffer overflow.

Sponsored by:		Netflix
2023-11-02 10:20:09 -06:00

279 lines
7.5 KiB
C

/****************************************************************
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.
****************************************************************/
#include <assert.h>
#include <stdint.h>
#include <stdbool.h>
#if __STDC_VERSION__ <= 199901L
#define noreturn
#else
#include <stdnoreturn.h>
#endif
typedef double Awkfloat;
/* unsigned char is more trouble than it's worth */
typedef unsigned char uschar;
#define xfree(a) { free((void *)(intptr_t)(a)); (a) = NULL; }
/*
* We sometimes cheat writing read-only pointers to NUL-terminate them
* and then put back the original value
*/
#define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
#define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF
*/
#define DEBUG
#ifdef DEBUG
# define DPRINTF(...) if (dbg) printf(__VA_ARGS__)
#else
# define DPRINTF(...)
#endif
extern enum compile_states {
RUNNING,
COMPILING,
ERROR_PRINTING
} compile_time;
extern bool safe; /* false => unsafe, true => safe */
#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
extern int recsize; /* size of current record, orig RECSIZE */
extern size_t awk_mb_cur_max; /* max size of a multi-byte character */
extern char EMPTY[]; /* this avoid -Wwritable-strings issues */
extern char **FS;
extern char **RS;
extern char **ORS;
extern char **OFS;
extern char **OFMT;
extern Awkfloat *NR;
extern Awkfloat *FNR;
extern Awkfloat *NF;
extern char **FILENAME;
extern char **SUBSEP;
extern Awkfloat *RSTART;
extern Awkfloat *RLENGTH;
extern bool CSV; /* true for csv input */
extern char *record; /* points to $0 */
extern int lineno; /* line number in awk program */
extern int errorflag; /* 1 if error has occurred */
extern bool donefld; /* true if record broken into fields */
extern bool donerec; /* true if record is valid (no fld has changed */
extern int dbg;
extern const char *patbeg; /* beginning of pattern matched */
extern int patlen; /* length of pattern matched. set in b.c */
/* Cell: all information about a variable or constant */
typedef struct Cell {
uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */
uschar csub; /* CCON, CTEMP, CFLD, etc. */
char *nval; /* name, for variables only */
char *sval; /* string value */
Awkfloat fval; /* value as number */
int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
char *fmt; /* CONVFMT/OFMT value used to convert from number */
struct Cell *cnext; /* ptr to next if chained */
} Cell;
typedef struct Array { /* symbol table array */
int nelem; /* elements in table right now */
int size; /* size of tab */
Cell **tab; /* hash table pointers */
} Array;
#define NSYMTAB 50 /* initial size of a symbol table */
extern Array *symtab;
extern Cell *nrloc; /* NR */
extern Cell *fnrloc; /* FNR */
extern Cell *fsloc; /* FS */
extern Cell *nfloc; /* NF */
extern Cell *ofsloc; /* OFS */
extern Cell *orsloc; /* ORS */
extern Cell *rsloc; /* RS */
extern Cell *rstartloc; /* RSTART */
extern Cell *rlengthloc; /* RLENGTH */
extern Cell *subseploc; /* SUBSEP */
extern Cell *symtabloc; /* SYMTAB */
/* Cell.tval values: */
#define NUM 01 /* number value is valid */
#define STR 02 /* string value is valid */
#define DONTFREE 04 /* string space is not freeable */
#define CON 010 /* this is a constant */
#define ARR 020 /* this is an array */
#define FCN 040 /* this is a function name */
#define FLD 0100 /* this is a field $1, $2, ... */
#define REC 0200 /* this is $0 */
#define CONVC 0400 /* string was converted from number via CONVFMT */
#define CONVO 01000 /* string was converted from number via OFMT */
/* function types */
#define FLENGTH 1
#define FSQRT 2
#define FEXP 3
#define FLOG 4
#define FINT 5
#define FSYSTEM 6
#define FRAND 7
#define FSRAND 8
#define FSIN 9
#define FCOS 10
#define FATAN 11
#define FTOUPPER 12
#define FTOLOWER 13
#define FFLUSH 14
#define FAND 15
#define FFOR 16
#define FXOR 17
#define FCOMPL 18
#define FLSHIFT 19
#define FRSHIFT 20
#define FSYSTIME 21
#define FSTRFTIME 22
/* Node: parse tree is made of nodes, with Cell's at bottom */
typedef struct Node {
int ntype;
struct Node *nnext;
int lineno;
int nobj;
struct Node *narg[1]; /* variable: actual size set by calling malloc */
} Node;
#define NIL ((Node *) 0)
extern Node *winner;
extern Node *nullstat;
extern Node *nullnode;
/* ctypes */
#define OCELL 1
#define OBOOL 2
#define OJUMP 3
/* Cell subtypes: csub */
#define CFREE 7
#define CCOPY 6
#define CCON 5
#define CTEMP 4
#define CNAME 3
#define CVAR 2
#define CFLD 1
#define CUNK 0
/* bool subtypes */
#define BTRUE 11
#define BFALSE 12
/* jump subtypes */
#define JEXIT 21
#define JNEXT 22
#define JBREAK 23
#define JCONT 24
#define JRET 25
#define JNEXTFILE 26
/* node types */
#define NVALUE 1
#define NSTAT 2
#define NEXPR 3
extern int pairstack[], paircnt;
#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
#define isvalue(n) ((n)->ntype == NVALUE)
#define isexpr(n) ((n)->ntype == NEXPR)
#define isjump(n) ((n)->ctype == OJUMP)
#define isexit(n) ((n)->csub == JEXIT)
#define isbreak(n) ((n)->csub == JBREAK)
#define iscont(n) ((n)->csub == JCONT)
#define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
#define isret(n) ((n)->csub == JRET)
#define isrec(n) ((n)->tval & REC)
#define isfld(n) ((n)->tval & FLD)
#define isstr(n) ((n)->tval & STR)
#define isnum(n) ((n)->tval & NUM)
#define isarr(n) ((n)->tval & ARR)
#define isfcn(n) ((n)->tval & FCN)
#define istrue(n) ((n)->csub == BTRUE)
#define istemp(n) ((n)->csub == CTEMP)
#define isargument(n) ((n)->nobj == ARG)
/* #define freeable(p) (!((p)->tval & DONTFREE)) */
#define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR )
/* structures used by regular expression matching machinery, mostly b.c: */
#define NCHARS (1256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
/* BUG: some overflows (caught) if we use 256 */
/* watch out in match(), etc. */
#define HAT (NCHARS+2) /* matches ^ in regular expr */
#define NSTATES 32
typedef struct rrow {
long ltype; /* long avoids pointer warnings on 64-bit */
union {
int i;
Node *np;
uschar *up;
int *rp; /* rune representation of char class */
} lval; /* because Al stores a pointer in it! */
int *lfollow;
} rrow;
typedef struct gtt { /* gototab entry */
unsigned int ch;
unsigned int state;
} gtt;
typedef struct fa {
gtt **gototab;
int gototab_len;
uschar *out;
uschar *restr;
int **posns;
int state_count;
bool anchor;
int use;
int initstat;
int curstat;
int accept;
struct rrow re[1]; /* variable: actual size set by calling malloc */
} fa;
#include "proto.h"