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