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