4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
26 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
27 /* All Rights Reserved */
32 #include <sys/types.h>
40 typedef double Awkfloat
;
41 typedef unsigned char uchar
;
43 #define xfree(a) { if ((a) != NULL) { free(a); a = NULL; } }
47 /* uses have to be doubly parenthesized */
48 #define dprintf(x) if (dbg) (void) printf x
53 extern char errbuf
[200];
54 extern void error(int, char *);
55 #define ERROR (void) snprintf(errbuf, sizeof (errbuf),
57 #define FATAL ), error(1, errbuf)
59 #define WARNING ), error(0, errbuf)
61 #define SYNTAX ), yyerror(errbuf)
65 extern int compile_time
; /* 1 if compiling, 0 if running */
70 /* ensure that there is extra 1 byte in the buffer */
71 #define expand_buf(p, n, r) \
72 if (*(n) == 0 || (r) >= (*(n) - 1)) r_expand_buf(p, n, r)
82 extern uchar
**FILENAME
;
83 extern uchar
**SUBSEP
;
84 extern Awkfloat
*RSTART
;
85 extern Awkfloat
*RLENGTH
;
88 extern size_t record_size
;
90 extern int donefld
; /* 1 if record broken into fields */
91 extern int donerec
; /* 1 if record is valid (no fld has changed */
93 extern uchar
*patbeg
; /* beginning of pattern matched */
94 extern int patlen
; /* length. set in b.c */
96 /* Cell: all information about a variable or constant */
99 uchar ctype
; /* OCELL, OBOOL, OJUMP, etc. */
100 uchar csub
; /* CCON, CTEMP, CFLD, etc. */
101 uchar
*nval
; /* name, for variables only */
102 uchar
*sval
; /* string value */
103 Awkfloat fval
; /* value as number */
105 /* type info: STR|NUM|ARR|FCN|FLD|CON|DONTFREE */
106 struct Cell
*cnext
; /* ptr to next if chained */
109 typedef struct { /* symbol table array */
110 int nelem
; /* elements in table right now */
111 int size
; /* size of tab */
112 Cell
**tab
; /* hash table pointers */
115 #define NSYMTAB 50 /* initial size of a symbol table */
116 extern Array
*symtab
, *makesymtab(int);
117 extern Cell
*setsymtab(uchar
*, uchar
*, Awkfloat
, unsigned int, Array
*);
118 extern Cell
*lookup(uchar
*, Array
*);
120 extern Cell
*recloc
; /* location of input record */
121 extern Cell
*nrloc
; /* NR */
122 extern Cell
*fnrloc
; /* FNR */
123 extern Cell
*nfloc
; /* NF */
124 extern Cell
*rstartloc
; /* RSTART */
125 extern Cell
*rlengthloc
; /* RLENGTH */
127 /* Cell.tval values: */
128 #define NUM 01 /* number value is valid */
129 #define STR 02 /* string value is valid */
130 #define DONTFREE 04 /* string space is not freeable */
131 #define CON 010 /* this is a constant */
132 #define ARR 020 /* this is an array */
133 #define FCN 040 /* this is a function name */
134 #define FLD 0100 /* this is a field $1, $2, ... */
135 #define REC 0200 /* this is $0 */
137 #define freeable(p) (!((p)->tval & DONTFREE))
139 extern Awkfloat
setfval(Cell
*, Awkfloat
), getfval(Cell
*), r_getfval(Cell
*);
140 extern uchar
*setsval(Cell
*, uchar
*), *getsval(Cell
*), *r_getsval(Cell
*);
141 extern uchar
*tostring(uchar
*), *tokname(int), *qstring(uchar
*, int);
144 (((p)->tval & (ARR|FLD|REC|NUM)) == NUM ? (p)->fval : r_getfval(p))
146 (((p)->tval & (ARR|FLD|REC|STR)) == STR ? (p)->sval : r_getsval(p))
163 /* Node: parse tree is made of nodes, with Cell's at bottom */
165 typedef struct Node
{
170 struct Node
*narg
[1];
171 /* variable: actual size set by calling malloc */
174 #define NIL ((Node *)0)
177 extern Node
*nullstat
;
178 extern Node
*nullnode
;
185 /* Cell subtypes: csub */
211 extern Cell
*(*proctab
[])(Node
**, int);
212 extern Cell
*nullproc(Node
**, int);
213 extern int pairstack
[], paircnt
;
215 extern Node
*stat1(int, Node
*), *stat2(int, Node
*, Node
*);
216 extern Node
*stat3(int, Node
*, Node
*, Node
*);
217 extern Node
*stat4(int, Node
*, Node
*, Node
*, Node
*);
218 extern Node
*pa2stat(Node
*, Node
*, Node
*);
219 extern Node
*op1(int, Node
*), *op2(int, Node
*, Node
*);
220 extern Node
*op3(int, Node
*, Node
*, Node
*);
221 extern Node
*op4(int, Node
*, Node
*, Node
*, Node
*);
222 extern Node
*linkum(Node
*, Node
*), *valtonode(Cell
*, int);
223 extern Node
*rectonode(void), *exptostat(Node
*);
224 extern Node
*makearr(Node
*);
226 #define notlegal(n) \
227 (n <= FIRSTTOKEN || n >= LASTTOKEN || proctab[n-FIRSTTOKEN] == nullproc)
228 #define isvalue(n) ((n)->ntype == NVALUE)
229 #define isexpr(n) ((n)->ntype == NEXPR)
230 #define isjump(n) ((n)->ctype == OJUMP)
231 #define isexit(n) ((n)->csub == JEXIT)
232 #define isbreak(n) ((n)->csub == JBREAK)
233 #define iscont(n) ((n)->csub == JCONT)
234 #define isnext(n) ((n)->csub == JNEXT)
235 #define isret(n) ((n)->csub == JRET)
236 #define isstr(n) ((n)->tval & STR)
237 #define isnum(n) ((n)->tval & NUM)
238 #define isarr(n) ((n)->tval & ARR)
239 #define isfunc(n) ((n)->tval & FCN)
240 #define istrue(n) ((n)->csub == BTRUE)
241 #define istemp(n) ((n)->csub == CTEMP)
243 #define NCHARS (256+1)
246 typedef struct rrow
{
256 uchar gototab
[NSTATES
][NCHARS
];
267 extern fa
*makedfa(uchar
*, int);
268 extern int nematch(fa
*, uchar
*);
269 extern int match(fa
*, uchar
*);
270 extern int pmatch(fa
*, uchar
*);
273 extern int isclvar(uchar
*);
274 extern int is_number(uchar
*);
275 extern void setclvar(uchar
*);
276 extern int readrec(uchar
**, size_t *, FILE *);
277 extern void bracecheck(void);
278 extern void syminit(void);
279 extern void yyerror(char *);
280 extern void fldbld(void);
281 extern void recbld(void);
282 extern int getrec(uchar
**, size_t *);
283 extern Cell
*fieldadr(int);
284 extern void newfld(int);
285 extern Cell
*getfld(int);
286 extern int fldidx(Cell
*);
287 extern double errcheck(double, char *);
288 extern void fpecatch(int);
289 extern void init_buf(uchar
**, size_t *, size_t);
290 extern void adjust_buf(uchar
**, size_t);
291 extern void r_expand_buf(uchar
**, size_t *, size_t);
295 extern uchar
*record
;
296 extern size_t record_size
;
300 extern uchar
*cmdname
;
301 extern uchar
*lexprog
;
302 extern int compile_time
;
303 extern char radixpoint
;
306 extern void syminit(void);
307 extern void arginit(int, uchar
**);
308 extern void envinit(uchar
**);
309 extern void freesymtab(Cell
*);
310 extern void freeelem(Cell
*, uchar
*);
311 extern void funnyvar(Cell
*, char *);
312 extern int hash(uchar
*, int);
313 extern Awkfloat
*ARGC
;
316 extern void run(Node
*);
322 extern int input(void);
324 extern int yyparse(void);
329 extern Cell
*nullproc(Node
**, int);
330 extern Cell
*program(Node
**, int);
331 extern Cell
*boolop(Node
**, int);
332 extern Cell
*relop(Node
**, int);
333 extern Cell
*array(Node
**, int);
334 extern Cell
*indirect(Node
**, int);
335 extern Cell
*substr(Node
**, int);
336 extern Cell
*sub(Node
**, int);
337 extern Cell
*gsub(Node
**, int);
338 extern Cell
*sindex(Node
**, int);
339 extern Cell
*a_sprintf(Node
**, int);
340 extern Cell
*arith(Node
**, int);
341 extern Cell
*incrdecr(Node
**, int);
342 extern Cell
*cat(Node
**, int);
343 extern Cell
*pastat(Node
**, int);
344 extern Cell
*dopa2(Node
**, int);
345 extern Cell
*matchop(Node
**, int);
346 extern Cell
*intest(Node
**, int);
347 extern Cell
*aprintf(Node
**, int);
348 extern Cell
*print(Node
**, int);
349 extern Cell
*closefile(Node
**, int);
350 extern Cell
*delete(Node
**, int);
351 extern Cell
*split(Node
**, int);
352 extern Cell
*assign(Node
**, int);
353 extern Cell
*condexpr(Node
**, int);
354 extern Cell
*ifstat(Node
**, int);
355 extern Cell
*whilestat(Node
**, int);
356 extern Cell
*forstat(Node
**, int);
357 extern Cell
*dostat(Node
**, int);
358 extern Cell
*instat(Node
**, int);
359 extern Cell
*jump(Node
**, int);
360 extern Cell
*bltin(Node
**, int);
361 extern Cell
*call(Node
**, int);
362 extern Cell
*arg(Node
**, int);
363 extern Cell
*getnf(Node
**, int);
364 extern Cell
*getaline(Node
**, int);