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