1/**************************************************************** 2Copyright (C) Lucent Technologies 1997 3All Rights Reserved 4 5Permission to use, copy, modify, and distribute this software and 6its documentation for any purpose and without fee is hereby 7granted, provided that the above copyright notice appear in all 8copies and that both that the copyright notice and this 9permission notice and warranty disclaimer appear in supporting 10documentation, and that the name Lucent Technologies or any of 11its entities not be used in advertising or publicity pertaining 12to distribution of the software without specific, written prior 13permission. 14 15LUCENT DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, 16INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. 17IN NO EVENT SHALL LUCENT OR ANY OF ITS ENTITIES BE LIABLE FOR ANY 18SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER 20IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, 21ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF 22THIS SOFTWARE. 23****************************************************************/ 24 25#include <assert.h> 26#include <stdint.h> 27#include <stdbool.h> 28#if __STDC_VERSION__ <= 199901L 29#define noreturn 30#else 31#include <stdnoreturn.h> 32#endif 33 34typedef double Awkfloat; 35 36/* unsigned char is more trouble than it's worth */ 37 38typedef unsigned char uschar; 39 40#define xfree(a) { free((void *)(intptr_t)(a)); (a) = NULL; } 41/* 42 * We sometimes cheat writing read-only pointers to NUL-terminate them 43 * and then put back the original value 44 */ 45#define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a) 46 47#define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF 48*/ 49#define DEBUG 50#ifdef DEBUG 51# define DPRINTF(...) if (dbg) printf(__VA_ARGS__) 52#else 53# define DPRINTF(...) 54#endif 55 56extern enum compile_states { 57 RUNNING, 58 COMPILING, 59 ERROR_PRINTING 60} compile_time; 61 62extern bool safe; /* false => unsafe, true => safe */ 63 64#define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */ 65extern int recsize; /* size of current record, orig RECSIZE */ 66 67extern size_t awk_mb_cur_max; /* max size of a multi-byte character */ 68 69extern char EMPTY[]; /* this avoid -Wwritable-strings issues */ 70extern char **FS; 71extern char **RS; 72extern char **ORS; 73extern char **OFS; 74extern char **OFMT; 75extern Awkfloat *NR; 76extern Awkfloat *FNR; 77extern Awkfloat *NF; 78extern char **FILENAME; 79extern char **SUBSEP; 80extern Awkfloat *RSTART; 81extern Awkfloat *RLENGTH; 82 83extern bool CSV; /* true for csv input */ 84 85extern char *record; /* points to $0 */ 86extern int lineno; /* line number in awk program */ 87extern int errorflag; /* 1 if error has occurred */ 88extern bool donefld; /* true if record broken into fields */ 89extern bool donerec; /* true if record is valid (no fld has changed */ 90extern int dbg; 91 92extern const char *patbeg; /* beginning of pattern matched */ 93extern int patlen; /* length of pattern matched. set in b.c */ 94 95/* Cell: all information about a variable or constant */ 96 97typedef struct Cell { 98 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */ 99 uschar csub; /* CCON, CTEMP, CFLD, etc. */ 100 char *nval; /* name, for variables only */ 101 char *sval; /* string value */ 102 Awkfloat fval; /* value as number */ 103 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */ 104 char *fmt; /* CONVFMT/OFMT value used to convert from number */ 105 struct Cell *cnext; /* ptr to next if chained */ 106} Cell; 107 108typedef struct Array { /* symbol table array */ 109 int nelem; /* elements in table right now */ 110 int size; /* size of tab */ 111 Cell **tab; /* hash table pointers */ 112} Array; 113 114#define NSYMTAB 50 /* initial size of a symbol table */ 115extern Array *symtab; 116 117extern Cell *nrloc; /* NR */ 118extern Cell *fnrloc; /* FNR */ 119extern Cell *fsloc; /* FS */ 120extern Cell *nfloc; /* NF */ 121extern Cell *ofsloc; /* OFS */ 122extern Cell *orsloc; /* ORS */ 123extern Cell *rsloc; /* RS */ 124extern Cell *rstartloc; /* RSTART */ 125extern Cell *rlengthloc; /* RLENGTH */ 126extern Cell *subseploc; /* SUBSEP */ 127extern Cell *symtabloc; /* SYMTAB */ 128 129/* Cell.tval values: */ 130#define NUM 01 /* number value is valid */ 131#define STR 02 /* string value is valid */ 132#define DONTFREE 04 /* string space is not freeable */ 133#define CON 010 /* this is a constant */ 134#define ARR 020 /* this is an array */ 135#define FCN 040 /* this is a function name */ 136#define FLD 0100 /* this is a field $1, $2, ... */ 137#define REC 0200 /* this is $0 */ 138#define CONVC 0400 /* string was converted from number via CONVFMT */ 139#define CONVO 01000 /* string was converted from number via OFMT */ 140 141 142/* function types */ 143#define FLENGTH 1 144#define FSQRT 2 145#define FEXP 3 146#define FLOG 4 147#define FINT 5 148#define FSYSTEM 6 149#define FRAND 7 150#define FSRAND 8 151#define FSIN 9 152#define FCOS 10 153#define FATAN 11 154#define FTOUPPER 12 155#define FTOLOWER 13 156#define FFLUSH 14 157#define FAND 15 158#define FFOR 16 159#define FXOR 17 160#define FCOMPL 18 161#define FLSHIFT 19 162#define FRSHIFT 20 163#define FSYSTIME 21 164#define FSTRFTIME 22 165 166/* Node: parse tree is made of nodes, with Cell's at bottom */ 167 168typedef struct Node { 169 int ntype; 170 struct Node *nnext; 171 int lineno; 172 int nobj; 173 struct Node *narg[1]; /* variable: actual size set by calling malloc */ 174} Node; 175 176#define NIL ((Node *) 0) 177 178extern Node *winner; 179extern Node *nullstat; 180extern Node *nullnode; 181 182/* ctypes */ 183#define OCELL 1 184#define OBOOL 2 185#define OJUMP 3 186 187/* Cell subtypes: csub */ 188#define CFREE 7 189#define CCOPY 6 190#define CCON 5 191#define CTEMP 4 192#define CNAME 3 193#define CVAR 2 194#define CFLD 1 195#define CUNK 0 196 197/* bool subtypes */ 198#define BTRUE 11 199#define BFALSE 12 200 201/* jump subtypes */ 202#define JEXIT 21 203#define JNEXT 22 204#define JBREAK 23 205#define JCONT 24 206#define JRET 25 207#define JNEXTFILE 26 208 209/* node types */ 210#define NVALUE 1 211#define NSTAT 2 212#define NEXPR 3 213 214 215extern int pairstack[], paircnt; 216 217#define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) 218#define isvalue(n) ((n)->ntype == NVALUE) 219#define isexpr(n) ((n)->ntype == NEXPR) 220#define isjump(n) ((n)->ctype == OJUMP) 221#define isexit(n) ((n)->csub == JEXIT) 222#define isbreak(n) ((n)->csub == JBREAK) 223#define iscont(n) ((n)->csub == JCONT) 224#define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE) 225#define isret(n) ((n)->csub == JRET) 226#define isrec(n) ((n)->tval & REC) 227#define isfld(n) ((n)->tval & FLD) 228#define isstr(n) ((n)->tval & STR) 229#define isnum(n) ((n)->tval & NUM) 230#define isarr(n) ((n)->tval & ARR) 231#define isfcn(n) ((n)->tval & FCN) 232#define istrue(n) ((n)->csub == BTRUE) 233#define istemp(n) ((n)->csub == CTEMP) 234#define isargument(n) ((n)->nobj == ARG) 235/* #define freeable(p) (!((p)->tval & DONTFREE)) */ 236#define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR ) 237 238/* structures used by regular expression matching machinery, mostly b.c: */ 239 240#define NCHARS (1256+3) /* 256 handles 8-bit chars; 128 does 7-bit */ 241 /* BUG: some overflows (caught) if we use 256 */ 242 /* watch out in match(), etc. */ 243#define HAT (NCHARS+2) /* matches ^ in regular expr */ 244#define NSTATES 32 245 246typedef struct rrow { 247 long ltype; /* long avoids pointer warnings on 64-bit */ 248 union { 249 int i; 250 Node *np; 251 uschar *up; 252 int *rp; /* rune representation of char class */ 253 } lval; /* because Al stores a pointer in it! */ 254 int *lfollow; 255} rrow; 256 257typedef struct gtte { /* gototab entry */ 258 unsigned int ch; 259 unsigned int state; 260} gtte; 261 262typedef struct gtt { /* gototab */ 263 size_t allocated; 264 size_t inuse; 265 gtte *entries; 266} gtt; 267 268typedef struct fa { 269 gtt *gototab; 270 uschar *out; 271 uschar *restr; 272 int **posns; 273 int state_count; 274 bool anchor; 275 int use; 276 int initstat; 277 int curstat; 278 int accept; 279 struct rrow re[1]; /* variable: actual size set by calling malloc */ 280} fa; 281 282 283#include "proto.h" 284