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