1 /****************************************************************
2 Copyright (C) Lucent Technologies 1997
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
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
23 ****************************************************************/
28 #if __STDC_VERSION__ <= 199901L
31 #include <stdnoreturn.h>
34 typedef double Awkfloat
;
36 /* unsigned char is more trouble than it's worth */
38 typedef unsigned char uschar
;
40 #define xfree(a) { free((void *)(intptr_t)(a)); (a) = NULL; }
42 * We sometimes cheat writing read-only pointers to NUL-terminate them
43 * and then put back the original value
45 #define setptr(ptr, a) (*(char *)(intptr_t)(ptr)) = (a)
47 #define NN(p) ((p) ? (p) : "(null)") /* guaranteed non-null for DPRINTF
51 # define DPRINTF(...) if (dbg) printf(__VA_ARGS__)
56 extern enum compile_states
{
62 extern bool safe
; /* false => unsafe, true => safe */
64 #define RECSIZE (8 * 1024) /* sets limit on records, fields, etc., etc. */
65 extern int recsize
; /* size of current record, orig RECSIZE */
67 extern char EMPTY
[]; /* this avoid -Wwritable-strings issues */
76 extern char **FILENAME
;
78 extern Awkfloat
*RSTART
;
79 extern Awkfloat
*RLENGTH
;
81 extern char *record
; /* points to $0 */
82 extern int lineno
; /* line number in awk program */
83 extern int errorflag
; /* 1 if error has occurred */
84 extern bool donefld
; /* true if record broken into fields */
85 extern bool donerec
; /* true if record is valid (no fld has changed */
88 extern const char *patbeg
; /* beginning of pattern matched */
89 extern int patlen
; /* length of pattern matched. set in b.c */
91 /* Cell: all information about a variable or constant */
94 uschar ctype
; /* OCELL, OBOOL, OJUMP, etc. */
95 uschar csub
; /* CCON, CTEMP, CFLD, etc. */
96 char *nval
; /* name, for variables only */
97 char *sval
; /* string value */
98 Awkfloat fval
; /* value as number */
99 int tval
; /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE|CONVC|CONVO */
100 char *fmt
; /* CONVFMT/OFMT value used to convert from number */
101 struct Cell
*cnext
; /* ptr to next if chained */
104 typedef struct Array
{ /* symbol table array */
105 int nelem
; /* elements in table right now */
106 int size
; /* size of tab */
107 Cell
**tab
; /* hash table pointers */
110 #define NSYMTAB 50 /* initial size of a symbol table */
111 extern Array
*symtab
;
113 extern Cell
*nrloc
; /* NR */
114 extern Cell
*fnrloc
; /* FNR */
115 extern Cell
*fsloc
; /* FS */
116 extern Cell
*nfloc
; /* NF */
117 extern Cell
*ofsloc
; /* OFS */
118 extern Cell
*orsloc
; /* ORS */
119 extern Cell
*rsloc
; /* RS */
120 extern Cell
*rstartloc
; /* RSTART */
121 extern Cell
*rlengthloc
; /* RLENGTH */
122 extern Cell
*subseploc
; /* SUBSEP */
123 extern Cell
*symtabloc
; /* SYMTAB */
125 /* Cell.tval values: */
126 #define NUM 01 /* number value is valid */
127 #define STR 02 /* string value is valid */
128 #define DONTFREE 04 /* string space is not freeable */
129 #define CON 010 /* this is a constant */
130 #define ARR 020 /* this is an array */
131 #define FCN 040 /* this is a function name */
132 #define FLD 0100 /* this is a field $1, $2, ... */
133 #define REC 0200 /* this is $0 */
134 #define CONVC 0400 /* string was converted from number via CONVFMT */
135 #define CONVO 01000 /* string was converted from number via OFMT */
154 /* Node: parse tree is made of nodes, with Cell's at bottom */
156 typedef struct Node
{
161 struct Node
*narg
[1]; /* variable: actual size set by calling malloc */
164 #define NIL ((Node *) 0)
167 extern Node
*nullstat
;
168 extern Node
*nullnode
;
175 /* Cell subtypes: csub */
203 extern int pairstack
[], paircnt
;
205 #define notlegal(n) (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
206 #define isvalue(n) ((n)->ntype == NVALUE)
207 #define isexpr(n) ((n)->ntype == NEXPR)
208 #define isjump(n) ((n)->ctype == OJUMP)
209 #define isexit(n) ((n)->csub == JEXIT)
210 #define isbreak(n) ((n)->csub == JBREAK)
211 #define iscont(n) ((n)->csub == JCONT)
212 #define isnext(n) ((n)->csub == JNEXT || (n)->csub == JNEXTFILE)
213 #define isret(n) ((n)->csub == JRET)
214 #define isrec(n) ((n)->tval & REC)
215 #define isfld(n) ((n)->tval & FLD)
216 #define isstr(n) ((n)->tval & STR)
217 #define isnum(n) ((n)->tval & NUM)
218 #define isarr(n) ((n)->tval & ARR)
219 #define isfcn(n) ((n)->tval & FCN)
220 #define istrue(n) ((n)->csub == BTRUE)
221 #define istemp(n) ((n)->csub == CTEMP)
222 #define isargument(n) ((n)->nobj == ARG)
223 /* #define freeable(p) (!((p)->tval & DONTFREE)) */
224 #define freeable(p) ( ((p)->tval & (STR|DONTFREE)) == STR )
226 /* structures used by regular expression matching machinery, mostly b.c: */
228 #define NCHARS (256+3) /* 256 handles 8-bit chars; 128 does 7-bit */
229 /* watch out in match(), etc. */
230 #define HAT (NCHARS+2) /* matches ^ in regular expr */
233 typedef struct rrow
{
234 long ltype
; /* long avoids pointer warnings on 64-bit */
239 } lval
; /* because Al stores a pointer in it! */
244 unsigned int **gototab
;
254 struct rrow re
[1]; /* variable: actual size set by calling malloc */