1 /* $OpenBSD: awk.h,v 1.11 2004/12/30 01:52:48 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 28 typedef double Awkfloat; 29 30 /* unsigned char is more trouble than it's worth */ 31 32 typedef unsigned char uschar; 33 34 #define xfree(a) { if ((a) != NULL) { free((char *) a); a = NULL; } } 35 36 #define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for dprintf 37 */ 38 #define DEBUG 39 #ifdef DEBUG 40 /* uses have to be doubly parenthesized */ 41 # define dprintf(x) if (dbg) printf x 42 #else 43 # define dprintf(x) 44 #endif 45 46 extern int compile_time; /* 1 if compiling, 0 if running */ 47 extern int safe; /* 0 => unsafe, 1 => safe */ 48 49 #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */ 50 extern int recsize; /* size of current record, orig RECSIZE */ 51 52 extern char **FS; 53 extern char **RS; 54 extern char **ORS; 55 extern char **OFS; 56 extern char **OFMT; 57 extern Awkfloat *NR; 58 extern Awkfloat *FNR; 59 extern Awkfloat *NF; 60 extern char **FILENAME; 61 extern char **SUBSEP; 62 extern Awkfloat *RSTART; 63 extern Awkfloat *RLENGTH; 64 65 extern char *record; /* points to $0 */ 66 extern int lineno; /* line number in awk program */ 67 extern int errorflag; /* 1 if error has occurred */ 68 extern int donefld; /* 1 if record broken into fields */ 69 extern int donerec; /* 1 if record is valid (no fld has changed */ 70 extern char inputFS[]; /* FS at time of input, for field splitting */ 71 72 extern int dbg; 73 74 extern char *patbeg; /* beginning of pattern matched */ 75 extern int patlen; /* length of pattern matched. set in b.c */ 76 77 /* Cell: all information about a variable or constant */ 78 79 typedef struct Cell { 80 uschar ctype; /* OCELL, OBOOL, OJUMP, etc. */ 81 uschar csub; /* CCON, CTEMP, CFLD, etc. */ 82 char *nval; /* name, for variables only */ 83 char *sval; /* string value */ 84 Awkfloat fval; /* value as number */ 85 int tval; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */ 86 struct Cell *cnext; /* ptr to next if chained */ 87 } Cell; 88 89 typedef struct Array { /* symbol table array */ 90 int nelem; /* elements in table right now */ 91 int size; /* size of tab */ 92 Cell **tab; /* hash table pointers */ 93 } Array; 94 95 #define NSYMTAB 50 /* initial size of a symbol table */ 96 extern Array *symtab; 97 98 extern Cell *nrloc; /* NR */ 99 extern Cell *fnrloc; /* FNR */ 100 extern Cell *nfloc; /* NF */ 101 extern Cell *rstartloc; /* RSTART */ 102 extern Cell *rlengthloc; /* RLENGTH */ 103 104 /* Cell.tval values: */ 105 #define NUM 01 /* number value is valid */ 106 #define STR 02 /* string value is valid */ 107 #define DONTFREE 04 /* string space is not freeable */ 108 #define CON 010 /* this is a constant */ 109 #define ARR 020 /* this is an array */ 110 #define FCN 040 /* this is a function name */ 111 #define FLD 0100 /* this is a field $1, $2, ... */ 112 #define REC 0200 /* this is $0 */ 113 114 115 /* function types */ 116 #define FLENGTH 1 117 #define FSQRT 2 118 #define FEXP 3 119 #define FLOG 4 120 #define FINT 5 121 #define FSYSTEM 6 122 #define FRAND 7 123 #define FSRAND 8 124 #define FSIN 9 125 #define FCOS 10 126 #define FATAN 11 127 #define FTOUPPER 12 128 #define FTOLOWER 13 129 #define FFLUSH 14 130 131 /* Node: parse tree is made of nodes, with Cell's at bottom */ 132 133 typedef struct Node { 134 int ntype; 135 struct Node *nnext; 136 int lineno; 137 int nobj; 138 struct Node *narg[1]; /* variable: actual size set by calling malloc */ 139 } Node; 140 141 #define NIL ((Node *) 0) 142 143 extern Node *winner; 144 extern Node *nullstat; 145 extern Node *nullnode; 146 147 /* ctypes */ 148 #define OCELL 1 149 #define OBOOL 2 150 #define OJUMP 3 151 152 /* Cell subtypes: csub */ 153 #define CFREE 7 154 #define CCOPY 6 155 #define CCON 5 156 #define CTEMP 4 157 #define CNAME 3 158 #define CVAR 2 159 #define CFLD 1 160 #define CUNK 0 161 162 /* bool subtypes */ 163 #define BTRUE 11 164 #define BFALSE 12 165 166 /* jump subtypes */ 167 #define JEXIT 21 168 #define JNEXT 22 169 #define JBREAK 23 170 #define JCONT 24 171 #define JRET 25 172 #define JNEXTFILE 26 173 174 /* node types */ 175 #define NVALUE 1 176 #define NSTAT 2 177 #define NEXPR 3 178 179 180 extern int pairstack[], paircnt; 181 182 #define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc) 183 #define isvalue(n) ((n)->ntype == NVALUE) 184 #define isexpr(n) ((n)->ntype == NEXPR) 185 #define isjump(n) ((n)->ctype == OJUMP) 186 #define isexit(n) ((n)->csub == JEXIT) 187 #define isbreak(n) ((n)->csub == JBREAK) 188 #define iscont(n) ((n)->csub == JCONT) 189 #define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE) 190 #define isret(n) ((n)->csub == JRET) 191 #define isrec(n) ((n)->tval & REC) 192 #define isfld(n) ((n)->tval & FLD) 193 #define isstr(n) ((n)->tval & STR) 194 #define isnum(n) ((n)->tval & NUM) 195 #define isarr(n) ((n)->tval & ARR) 196 #define isfcn(n) ((n)->tval & FCN) 197 #define istrue(n) ((n)->csub == BTRUE) 198 #define istemp(n) ((n)->csub == CTEMP) 199 #define isargument(n) ((n)->nobj == ARG) 200 /* #define freeable(p) (!((p)->tval & DONTFREE)) */ 201 #define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR ) 202 203 /* structures used by regular expression matching machinery, mostly b.c: */ 204 205 #define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */ 206 /* watch out in match(), etc. */ 207 #define NSTATES 32 208 209 typedef struct rrow { 210 long ltype; /* long avoids pointer warnings on 64-bit */ 211 union { 212 int i; 213 Node *np; 214 uschar *up; 215 } lval; /* because Al stores a pointer in it! */ 216 int *lfollow; 217 } rrow; 218 219 typedef struct fa { 220 uschar gototab[NSTATES][NCHARS]; 221 uschar out[NSTATES]; 222 uschar *restr; 223 int *posns[NSTATES]; 224 int anchor; 225 int use; 226 int initstat; 227 int curstat; 228 int accept; 229 int reset; 230 struct rrow re[1]; /* variable: actual size set by calling malloc */ 231 } fa; 232 233 234 #include "proto.h" 235