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