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