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