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) { 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 56 extern enum compile_states { 57 RUNNING, 58 COMPILING, 59 ERROR_PRINTING 60 } compile_time; 61 62 extern bool safe; /* false => unsafe, true => safe */ 63 64 #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */ 65 extern int recsize; /* size of current record, orig RECSIZE */ 66 67 extern size_t awk_mb_cur_max; /* max size of a multi-byte character */ 68 69 extern char EMPTY[]; /* this avoid -Wwritable-strings issues */ 70 extern char **FS; 71 extern char **RS; 72 extern char **ORS; 73 extern char **OFS; 74 extern char **OFMT; 75 extern Awkfloat *NR; 76 extern Awkfloat *FNR; 77 extern Awkfloat *NF; 78 extern char **FILENAME; 79 extern char **SUBSEP; 80 extern Awkfloat *RSTART; 81 extern Awkfloat *RLENGTH; 82 83 extern bool CSV; /* true for csv input */ 84 85 extern char *record; /* points to $0 */ 86 extern int lineno; /* line number in awk program */ 87 extern int errorflag; /* 1 if error has occurred */ 88 extern bool donefld; /* true if record broken into fields */ 89 extern bool donerec; /* true if record is valid (no fld has changed */ 90 extern int dbg; 91 92 extern const char *patbeg; /* beginning of pattern matched */ 93 extern int patlen; /* length of pattern matched. set in b.c */ 94 95 /* Cell: all information about a variable or constant */ 96 97 typedef 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 108 typedef 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 */ 115 extern Array *symtab; 116 117 extern Cell *nrloc; /* NR */ 118 extern Cell *fnrloc; /* FNR */ 119 extern Cell *fsloc; /* FS */ 120 extern Cell *nfloc; /* NF */ 121 extern Cell *ofsloc; /* OFS */ 122 extern Cell *orsloc; /* ORS */ 123 extern Cell *rsloc; /* RS */ 124 extern Cell *rstartloc; /* RSTART */ 125 extern Cell *rlengthloc; /* RLENGTH */ 126 extern Cell *subseploc; /* SUBSEP */ 127 extern 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 #define FMKTIME 23 166 167 /* Node: parse tree is made of nodes, with Cell's at bottom */ 168 169 typedef struct Node { 170 int ntype; 171 struct Node *nnext; 172 int lineno; 173 int nobj; 174 struct Node *narg[1]; /* variable: actual size set by calling malloc */ 175 } Node; 176 177 #define NIL ((Node *) 0) 178 179 extern Node *winner; 180 extern 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 215 extern 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 246 typedef 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 257 typedef struct gtte { /* gototab entry */ 258 unsigned int ch; 259 unsigned int state; 260 } gtte; 261 262 typedef struct gtt { /* gototab */ 263 size_t allocated; 264 size_t inuse; 265 gtte *entries; 266 } gtt; 267 268 typedef 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