1 /* $OpenBSD: awk.h,v 1.32 2024/06/03 00:58:04 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) { 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 size_t awk_mb_cur_max; /* max size of a multi-byte character */ 70 71 extern char EMPTY[]; /* this avoid -Wwritable-strings issues */ 72 extern char **FS; 73 extern char **RS; 74 extern char **ORS; 75 extern char **OFS; 76 extern char **OFMT; 77 extern Awkfloat *NR; 78 extern Awkfloat *FNR; 79 extern Awkfloat *NF; 80 extern char **FILENAME; 81 extern char **SUBSEP; 82 extern Awkfloat *RSTART; 83 extern Awkfloat *RLENGTH; 84 85 extern bool CSV; /* true for csv input */ 86 87 extern char *record; /* points to $0 */ 88 extern int lineno; /* line number in awk program */ 89 extern int errorflag; /* 1 if error has occurred */ 90 extern bool donefld; /* true if record broken into fields */ 91 extern bool donerec; /* true if record is valid (no fld has changed */ 92 extern int dbg; 93 94 extern const char *patbeg; /* beginning of pattern matched */ 95 extern int patlen; /* length of pattern matched. set in b.c */ 96 97 /* Cell: all information about a variable or constant */ 98 99 typedef struct Cell { 100 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */ 101 uschar csub; /* CCON, CTEMP, CFLD, etc. */ 102 char *nval; /* name, for variables only */ 103 char *sval; /* string value */ 104 Awkfloat fval; /* value as number */ 105 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */ 106 char *fmt; /* CONVFMT/OFMT value used to convert from number */ 107 struct Cell *cnext; /* ptr to next if chained */ 108 } Cell; 109 110 typedef struct Array { /* symbol table array */ 111 int nelem; /* elements in table right now */ 112 int size; /* size of tab */ 113 Cell **tab; /* hash table pointers */ 114 } Array; 115 116 #define NSYMTAB 50 /* initial size of a symbol table */ 117 extern Array *symtab; 118 119 extern Cell *nrloc; /* NR */ 120 extern Cell *fnrloc; /* FNR */ 121 extern Cell *fsloc; /* FS */ 122 extern Cell *nfloc; /* NF */ 123 extern Cell *ofsloc; /* OFS */ 124 extern Cell *orsloc; /* ORS */ 125 extern Cell *rsloc; /* RS */ 126 extern Cell *rstartloc; /* RSTART */ 127 extern Cell *rlengthloc; /* RLENGTH */ 128 extern Cell *subseploc; /* SUBSEP */ 129 extern Cell *symtabloc; /* SYMTAB */ 130 131 /* Cell.tval values: */ 132 #define NUM 01 /* number value is valid */ 133 #define STR 02 /* string value is valid */ 134 #define DONTFREE 04 /* string space is not freeable */ 135 #define CON 010 /* this is a constant */ 136 #define ARR 020 /* this is an array */ 137 #define FCN 040 /* this is a function name */ 138 #define FLD 0100 /* this is a field $1, $2, ... */ 139 #define REC 0200 /* this is $0 */ 140 #define CONVC 0400 /* string was converted from number via CONVFMT */ 141 #define CONVO 01000 /* string was converted from number via OFMT */ 142 143 144 /* function types */ 145 #define FLENGTH 1 146 #define FSQRT 2 147 #define FEXP 3 148 #define FLOG 4 149 #define FINT 5 150 #define FSYSTEM 6 151 #define FRAND 7 152 #define FSRAND 8 153 #define FSIN 9 154 #define FCOS 10 155 #define FATAN 11 156 #define FTOUPPER 12 157 #define FTOLOWER 13 158 #define FFLUSH 14 159 #define FAND 15 160 #define FFOR 16 161 #define FXOR 17 162 #define FCOMPL 18 163 #define FLSHIFT 19 164 #define FRSHIFT 20 165 #define FSYSTIME 21 166 #define FSTRFTIME 22 167 #define FMKTIME 23 168 169 /* Node: parse tree is made of nodes, with Cell's at bottom */ 170 171 typedef struct Node { 172 int ntype; 173 struct Node *nnext; 174 int lineno; 175 int nobj; 176 struct Node *narg[1]; /* variable: actual size set by calling malloc */ 177 } Node; 178 179 #define NIL ((Node *) 0) 180 181 extern Node *winner; 182 extern Node *nullnode; 183 184 /* ctypes */ 185 #define OCELL 1 186 #define OBOOL 2 187 #define OJUMP 3 188 189 /* Cell subtypes: csub */ 190 #define CFREE 7 191 #define CCOPY 6 192 #define CCON 5 193 #define CTEMP 4 194 #define CNAME 3 195 #define CVAR 2 196 #define CFLD 1 197 #define CUNK 0 198 199 /* bool subtypes */ 200 #define BTRUE 11 201 #define BFALSE 12 202 203 /* jump subtypes */ 204 #define JEXIT 21 205 #define JNEXT 22 206 #define JBREAK 23 207 #define JCONT 24 208 #define JRET 25 209 #define JNEXTFILE 26 210 211 /* node types */ 212 #define NVALUE 1 213 #define NSTAT 2 214 #define NEXPR 3 215 216 217 extern int pairstack[], paircnt; 218 219 #define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) 220 #define isvalue(n) ((n)->ntype == NVALUE) 221 #define isexpr(n) ((n)->ntype == NEXPR) 222 #define isjump(n) ((n)->ctype == OJUMP) 223 #define isexit(n) ((n)->csub == JEXIT) 224 #define isbreak(n) ((n)->csub == JBREAK) 225 #define iscont(n) ((n)->csub == JCONT) 226 #define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE) 227 #define isret(n) ((n)->csub == JRET) 228 #define isrec(n) ((n)->tval & REC) 229 #define isfld(n) ((n)->tval & FLD) 230 #define isstr(n) ((n)->tval & STR) 231 #define isnum(n) ((n)->tval & NUM) 232 #define isarr(n) ((n)->tval & ARR) 233 #define isfcn(n) ((n)->tval & FCN) 234 #define istrue(n) ((n)->csub == BTRUE) 235 #define istemp(n) ((n)->csub == CTEMP) 236 #define isargument(n) ((n)->nobj == ARG) 237 /* #define freeable(p) (!((p)->tval & DONTFREE)) */ 238 #define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR ) 239 240 /* structures used by regular expression matching machinery, mostly b.c: */ 241 242 #define NCHARS (1256+3) /* 256 handles 8-bit chars; 128 does 7-bit */ 243 /* BUG: some overflows (caught) if we use 256 */ 244 /* watch out in match(), etc. */ 245 #define HAT (NCHARS+2) /* matches ^ in regular expr */ 246 #define NSTATES 32 247 248 typedef struct rrow { 249 long ltype; /* long avoids pointer warnings on 64-bit */ 250 union { 251 int i; 252 Node *np; 253 uschar *up; 254 int *rp; /* rune representation of char class */ 255 } lval; /* because Al stores a pointer in it! */ 256 int *lfollow; 257 } rrow; 258 259 typedef struct gtte { /* gototab entry */ 260 unsigned int ch; 261 unsigned int state; 262 } gtte; 263 264 typedef struct gtt { /* gototab */ 265 size_t allocated; 266 size_t inuse; 267 gtte *entries; 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 285 #include "proto.h" 286