awk.h (9039B)
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 <stddef.h> 27 #include <stdint.h> 28 29 #ifndef bool 30 #define bool int 31 #define true 1 32 #define false 0 33 #endif 34 35 #if __STDC_VERSION__ <= 199901L 36 #define noreturn 37 #else 38 #include <stdnoreturn.h> 39 #endif 40 41 typedef double Awkfloat; 42 43 /* unsigned char is more trouble than its worth */ 44 45 typedef unsigned char uschar; 46 47 #define xfree(a) \ 48 { \ 49 free((void *)(intptr_t)(a)); \ 50 (a) = NULL; \ 51 } 52 /* 53 * we sometimes cheat writing read-only pointers to nul-terminate them 54 * and then put back the original value 55 */ 56 #define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a) 57 58 #define NN(p) \ 59 ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF \ 60 */ 61 #define DEBUG 62 #ifdef DEBUG 63 #define DPRINTF(...) \ 64 if (dbg) \ 65 printf(__VA_ARGS__) 66 #else 67 #define DPRINTF(...) 68 #endif 69 70 extern enum compile_states { RUNNING, COMPILING, ERROR_PRINTING } compile_time; 71 72 extern bool safe; /* false => unsafe, true => safe */ 73 74 #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc */ 75 extern int recsize; /* size of current record, orig RECSIZE */ 76 77 extern size_t awk_mb_cur_max; /* max size of a multi-byte character */ 78 79 extern char EMPTY[]; /* this avoid -wwritable-strings issues */ 80 extern char **FS; 81 extern char **RS; 82 extern char **ORS; 83 extern char **OFS; 84 extern char **OFMT; 85 extern Awkfloat *NR; 86 extern Awkfloat *FNR; 87 extern Awkfloat *NF; 88 extern char **FILENAME; 89 extern char **SUBSEP; 90 extern Awkfloat *RSTART; 91 extern Awkfloat *RLENGTH; 92 93 extern bool CSV; /* true for csv input */ 94 95 extern char *record; /* points to $0 */ 96 extern int lineno; /* line number in awk program */ 97 extern int errorflag; /* 1 if error has occurred */ 98 extern bool donefld; /* true if record broken into fields */ 99 extern bool donerec; /* true if record is valid (no fld has changed */ 100 extern int dbg; 101 102 extern const char *patbeg; /* beginning of pattern matched */ 103 extern int patlen; /* length of pattern matched. set in b.c */ 104 105 /* cell: all information about a variable or constant */ 106 107 typedef struct Cell { 108 uschar ctype; /* OCELL, OBOOL, OJUMP, etc */ 109 uschar csub; /* CCON, CTEMP, CFLD, etc */ 110 char *nval; /* name, for variables only */ 111 char *sval; /* string value */ 112 Awkfloat fval; /* value as number */ 113 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */ 114 char *fmt; /* CONVFMT/OFMT value used to convert from number */ 115 struct Cell *cnext; /* ptr to next if chained */ 116 } Cell; 117 118 typedef struct Array { /* symbol table array */ 119 int nelem; /* elements in table right now */ 120 int size; /* size of tab */ 121 Cell **tab; /* hash table pointers */ 122 } Array; 123 124 #define NSYMTAB 50 /* initial size of a symbol table */ 125 extern Array *symtab; 126 127 extern Cell *nrloc; /* NR */ 128 extern Cell *fnrloc; /* FNR */ 129 extern Cell *fsloc; /* FS */ 130 extern Cell *nfloc; /* NF */ 131 extern Cell *ofsloc; /* OFS */ 132 extern Cell *orsloc; /* ORS */ 133 extern Cell *rsloc; /* RS */ 134 extern Cell *rstartloc; /* RSTART */ 135 extern Cell *rlengthloc; /* RLENGTH */ 136 extern Cell *subseploc; /* SUBSEP */ 137 extern Cell *symtabloc; /* SYMTAB */ 138 139 /* cell.tval values: */ 140 #define NUM 01 /* number value is valid */ 141 #define STR 02 /* string value is valid */ 142 #define DONTFREE 04 /* string space is not freeable */ 143 #define CON 010 /* this is a constant */ 144 #define ARR 020 /* this is an array */ 145 #define FCN 040 /* this is a function name */ 146 #define FLD 0100 /* this is a field $1, $2, ... */ 147 #define REC 0200 /* this is $0 */ 148 #define CONVC 0400 /* string was converted from number via CONVFMT */ 149 #define CONVO 01000 /* string was converted from number via OFMT */ 150 151 /* function types */ 152 #define FLENGTH 1 153 #define FSQRT 2 154 #define FEXP 3 155 #define FLOG 4 156 #define FINT 5 157 #define FSYSTEM 6 158 #define FRAND 7 159 #define FSRAND 8 160 #define FSIN 9 161 #define FCOS 10 162 #define FATAN 11 163 #define FTOUPPER 12 164 #define FTOLOWER 13 165 #define FFLUSH 14 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 extern int pairstack[], paircnt; 215 216 #define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n - FIRSTTOKEN] == nullproc) 217 #define isvalue(n) ((n)->ntype == NVALUE) 218 #define isexpr(n) ((n)->ntype == NEXPR) 219 #define isjump(n) ((n)->ctype == OJUMP) 220 #define isexit(n) ((n)->csub == JEXIT) 221 #define isbreak(n) ((n)->csub == JBREAK) 222 #define iscont(n) ((n)->csub == JCONT) 223 #define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE) 224 #define isret(n) ((n)->csub == JRET) 225 #define isrec(n) ((n)->tval & REC) 226 #define isfld(n) ((n)->tval & FLD) 227 #define isstr(n) ((n)->tval & STR) 228 #define isnum(n) ((n)->tval & NUM) 229 #define isarr(n) ((n)->tval & ARR) 230 #define isfcn(n) ((n)->tval & FCN) 231 #define istrue(n) ((n)->csub == BTRUE) 232 #define istemp(n) ((n)->csub == CTEMP) 233 #define isargument(n) ((n)->nobj == ARG) 234 /* #define freeable(p) (!((p)->tval & DONTFREE)) */ 235 #define freeable(p) (((p)->tval & (STR | DONTFREE)) == STR) 236 237 /* structures used by regular expression matching machinery, mostly b.c: */ 238 239 #define NCHARS (1256 + 3) /* 256 handles 8-bit chars; 128 does 7-bit */ 240 /* BUG: some overflows (caught) if we use 256 */ 241 /* watch out in match(), etc */ 242 #define HAT (NCHARS + 2) /* matches ^ in regular expr */ 243 #define NSTATES 32 244 245 typedef struct rrow { 246 long ltype; /* long avoids pointer warnings on 64-bit */ 247 union { 248 int i; 249 Node *np; 250 uschar *up; 251 int *rp; /* rune representation of char class */ 252 } lval; /* because al stores a pointer in it! */ 253 int *lfollow; 254 } rrow; 255 256 typedef struct gtte { /* gototab entry */ 257 unsigned int ch; 258 unsigned int state; 259 } gtte; 260 261 #define GOTO_DIRECT 128 /* size of ASCII direct-index fast path */ 262 263 typedef struct gtt { /* gototab */ 264 size_t allocated; 265 size_t inuse; 266 gtte *entries; 267 int direct[GOTO_DIRECT]; /* fast path for ASCII; 0 == miss */ 268 } gtt; 269 270 typedef struct fa { 271 gtt *gototab; 272 uschar *out; 273 uschar *restr; 274 int **posns; 275 int state_count; 276 bool anchor; 277 int use; 278 int initstat; 279 int curstat; 280 int accept; 281 struct rrow re[1]; /* variable: actual size set by calling malloc */ 282 } fa; 283 284 #include "proto.h"