xref: /titanic_41/usr/src/cmd/awk_xpg4/awk0.c (revision 159d09a20817016f09b3ea28d1bdada4a336bb91)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Awk -- data definitions
24  *
25  * Copyright (c) 1995 by Sun Microsystems, Inc.
26  *
27  * Copyright 1986, 1992 by Mortice Kern Systems Inc.  All rights reserved.
28  *
29  * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
30  */
31 
32 #pragma ident	"%Z%%M%	%I%	%E% SMI"
33 
34 #include "awk.h"
35 #include "y.tab.h"
36 
37 /*
38  * This file contains data definitions for awk.
39  */
40 
41 RESERVED	reserved[] = {
42 	s_BEGIN, KEYWORD, BEGIN, NULL,
43 	s_END, KEYWORD, END, NULL,
44 	M_MB_L("break"), KEYWORD, BREAK, NULL,
45 	M_MB_L("continue"), KEYWORD, CONTINUE, NULL,
46 	M_MB_L("for"), KEYWORD, FOR, NULL,
47 	M_MB_L("if"), KEYWORD, IF, NULL,
48 	M_MB_L("else"), KEYWORD, ELSE, NULL,
49 	M_MB_L("in"), KEYWORD, IN, NULL,
50 	s_next, KEYWORD, NEXT, NULL,
51 	M_MB_L("while"), KEYWORD, WHILE, NULL,
52 	M_MB_L("do"), KEYWORD, DO, NULL,
53 	M_MB_L("print"), KEYWORD, PRINT, NULL,
54 	M_MB_L("printf"), KEYWORD, PRINTF, NULL,
55 	M_MB_L("return"), KEYWORD, RETURN, NULL,
56 	M_MB_L("func"), KEYWORD, DEFFUNC, NULL,
57 	M_MB_L("function"), KEYWORD, DEFFUNC, NULL,
58 	M_MB_L("delete"), KEYWORD, DELETE, NULL,
59 	M_MB_L("exit"), KEYWORD, EXIT, NULL,
60 	s_FILENAME, VAR, 0, _null,
61 	s_NF, SVAR, 0, NULL,
62 	s_NR, VAR, 0, NULL,
63 	s_FS, SVAR, 1, M_MB_L(" "),
64 	s_OFS, VAR, 1, M_MB_L(" "),
65 	s_ORS, VAR, 1, M_MB_L("\n"),
66 	s_OFMT, VAR, 4, M_MB_L("%.6g"),
67 	s_CONVFMT, VAR, 4, M_MB_L("%.6g"),
68 	s_RS, SVAR, 1, M_MB_L("\n"),
69 	s_FNR, VAR, 0, NULL,
70 	s_SUBSEP, VAR, 1,
71 #ifdef	M_AWK_SUBSEP
72 	M_AWK_SUBSEP,
73 #else
74 	M_MB_L("\34"),
75 #endif
76 	s_ARGC, SVAR, 0, NULL,
77 	(LOCCHARP)NULL
78 };
79 
80 RESFUNC	resfuncs[] = {
81 	s_exp, FUNC, f_exp,
82 	s_getline, GETLINE, f_getline,
83 	s_index, FUNC, f_index,
84 	s_int, FUNC, f_int,
85 	s_length, FUNC, f_length,
86 	s_log, FUNC, f_log,
87 	s_split, FUNC, f_split,
88 	s_sprintf, FUNC, f_sprintf,
89 	s_sqrt, FUNC, f_sqrt,
90 	s_substr, FUNC, f_substr,
91 	s_rand, FUNC, f_rand,
92 	s_srand, FUNC, f_srand,
93 	s_sin, FUNC, f_sin,
94 	s_cos, FUNC, f_cos,
95 	s_atan2, FUNC, f_atan2,
96 	s_sub, FUNC, f_sub,
97 	s_gsub, FUNC, f_gsub,
98 	s_match, FUNC, f_match,
99 	s_system, FUNC, f_system,
100 	s_ord, FUNC, f_ord,
101 	s_toupper, FUNC, f_toupper,
102 	s_tolower, FUNC, f_tolower,
103 	s_asort, FUNC, f_asort,
104 	s_close, FUNC, f_close,
105 	(LOCCHARP)NULL
106 };
107 
108 
109 OFILE	*ofiles;			/* Remembered open files (print) */
110 long	NIOSTREAM = 512;		/* max num of open file descriptors */
111 
112 
113 
114 
115 wchar_t	_null[] = M_MB_L("");		/* Empty string */
116 char	r[] = "r";			/* Read file mode */
117 char	w[] = "w";			/* Write file mode */
118 wchar_t	s_OFMT[] = M_MB_L("OFMT");	/* Name of "OFMT" variable */
119 wchar_t	s_CONVFMT[] = M_MB_L("CONVFMT"); /* Name of "CONVFMT" variable */
120 wchar_t	s_NR[] = M_MB_L("NR");		/* Name of "NR" variable */
121 wchar_t	s_NF[] = M_MB_L("NF");		/* Name of "NF" variable */
122 wchar_t	s_OFS[] = M_MB_L("OFS");	/* Name of "OFS" variable */
123 wchar_t	s_ORS[] = M_MB_L("ORS");	/* Name of "ORS" variable */
124 wchar_t	s_RS[] = M_MB_L("RS");		/* Name of "RS" variable */
125 wchar_t	s_FS[] = M_MB_L("FS");		/* Name of "FS" variable */
126 wchar_t	s_FNR[] = M_MB_L("FNR");	/* Name of "FNR" variable */
127 wchar_t	s_SUBSEP[] = M_MB_L("SUBSEP");	/* Name of "SUBSEP" variable */
128 wchar_t	s_ARGC[] = M_MB_L("ARGC");	/* Name of "ARGC" variable */
129 wchar_t	s_ARGV[] = M_MB_L("ARGV");	/* Name of "ARGV" array variable */
130 wchar_t	s_ENVIRON[] = M_MB_L("ENVIRON"); /* Name of "ENVIRON" array variable */
131 wchar_t	s_FILENAME[] = M_MB_L("FILENAME"); /* Name of "FILENAME" variable */
132 wchar_t	s_SYMTAB[] = M_MB_L("SYMTAB");	/* Name of "SYMTAB" variable */
133 wchar_t	s_BEGIN[] = M_MB_L("BEGIN");	/* Name of "BEGIN" action */
134 wchar_t	s_END[] = M_MB_L("END");	/* Name of "END" action */
135 wchar_t	s_next[] = M_MB_L("next");	/* Name of "next" keyword */
136 wchar_t	s_exp[] = M_MB_L("exp");	/* Name of "exp" function */
137 wchar_t	s_getline[] = M_MB_L("getline"); /* Name of "getline" function */
138 wchar_t	s_index[] = M_MB_L("index");	/* Name of "index" function */
139 wchar_t	s_int[] = M_MB_L("int");	/* Name of "int" function */
140 wchar_t	s_length[] = M_MB_L("length");	/* Name of "length" function */
141 wchar_t	s_log[] = M_MB_L("log");	/* Name of "log" function */
142 wchar_t	s_split[] = M_MB_L("split");	/* Name of "split" function */
143 wchar_t	s_sprintf[] = M_MB_L("sprintf"); /* Name of "sprintf" function */
144 wchar_t	s_sqrt[] = M_MB_L("sqrt");	/* Name of "sqrt" function */
145 wchar_t	s_substr[] = M_MB_L("substr");	/* Name of "substr" function */
146 wchar_t	s_rand[] = M_MB_L("rand");	/* Name of "rand" function */
147 wchar_t	s_srand[] = M_MB_L("srand");	/* Name of "srand" function */
148 wchar_t	s_sin[] = M_MB_L("sin");	/* Name of "sin" function */
149 wchar_t	s_cos[] = M_MB_L("cos");	/* Name of "cos" function */
150 wchar_t	s_atan2[] = M_MB_L("atan2");	/* Name of "atan" function */
151 wchar_t	s_sub[] = M_MB_L("sub");	/* Name of "sub" function */
152 wchar_t	s_gsub[] = M_MB_L("gsub");	/* Name of "gsub" function */
153 wchar_t	s_match[] = M_MB_L("match");	/* Name of "match" function */
154 wchar_t	s_system[] = M_MB_L("system");	/* Name of "system" function */
155 wchar_t	s_ord[] = M_MB_L("ord");	/* Name of "ord" function */
156 wchar_t	s_toupper[] = M_MB_L("toupper"); /* Name of "toupper" function */
157 wchar_t	s_tolower[] = M_MB_L("tolower"); /* Name of "tolower" function */
158 wchar_t	s_asort[] = M_MB_L("asort");	/* Name of "asort" function */
159 wchar_t	s_close[] = M_MB_L("close");	/* Name of "close" function */
160 
161 wchar_t redelim;			/* Delimiter for regexp (yylex) */
162 uchar_t	inprint;			/* Special meaning for '>' & '|' */
163 uchar_t	funparm;			/* Defining function parameters */
164 uchar_t	splitdone;			/* Line split into fields (fieldbuf) */
165 uint	npattern;			/* Number of non-BEGIN patterns */
166 uint	nfield;				/* Number of fields (if splitdone) */
167 uint	fcount;				/* Field counter (used by blackfield)*/
168 uint	phase;				/* BEGIN, END, or 0 */
169 uint	running = 0;			/* Set if not in compile phase */
170 uchar_t	catterm;			/* Can inject concat or ';' */
171 uint	lexlast = '\n';			/* Last lexical token */
172 uint	lineno = 0;			/* Current programme line number */
173 uchar_t	doing_begin;			/* set if compiling BEGIN block */
174 uchar_t	begin_getline;			/* flags a getline was done in BEGIN */
175 uchar_t	needsplit;			/* Set if $0 must be split when read */
176 uchar_t	needenviron;			/* Set if ENVIRON variable referenced */
177 ushort	slevel;				/* Scope level (0 == root) */
178 ushort	loopexit;			/* Short circuit loop with keyword */
179 wchar_t	radixpoint;			/* soft radix point for I18N */
180 REGEXP	resep;				/* Field separator as regexp */
181 wchar_t	*linebuf = NULL;		/* $0 buffer - malloc'd in awk1.c */
182 size_t	lbuflen;			/* Length of linebuf */
183 
184 /*
185  * XXX - Make sure to check where this error message is printed
186  */
187 char	interr[] = "internal execution tree error at E string";
188 char	nomem[] =  "insufficient memory for string storage";
189 NODE	*symtab[NBUCKET];		/* Heads of symbol table buckets */
190 NODE	*yytree;			/* Code tree */
191 NODE	*freelist;			/* Free every pattern {action} line */
192 wchar_t	*(*awkrecord) ANSI((wchar_t *, int, FILE*)) = defrecord;
193 					/* Function to read a record */
194 wchar_t	*(*awkfield) ANSI((wchar_t **)) = whitefield;
195 					/* Function to extract a field */
196 
197 /*
198  * Nodes used to speed up the execution of the
199  * interpreter.
200  */
201 NODE	*constant;			/* Node to hold a constant INT */
202 NODE	*const0;			/* Constant INT 0 node */
203 NODE	*const1;			/* Constant INT 1 node */
204 NODE	*constundef;			/* Undefined variable */
205 NODE	*field0;			/* $0 */
206 NODE	*incNR;				/* Code to increment NR variable */
207 NODE	*incFNR;			/* Code to increment FNR variable */
208 NODE	*clrFNR;			/* Zero FNR variable (each file) */
209 NODE	*ARGVsubi;			/* Compute ARGV[i] */
210 NODE	*varNR;				/* Remove search for NR variable */
211 NODE	*varFNR;			/* Don't search for FNR variable */
212 NODE	*varNF;				/* Pointer to NF variable */
213 NODE	*varOFMT;			/* For s_prf */
214 NODE	*varCONVFMT;			/* For internal conv of float to str */
215 NODE	*varOFS;			/* For s_print */
216 NODE	*varORS;			/* For s_print */
217 NODE	*varFS;				/* Field separtor */
218 NODE	*varRS;				/* Record separator */
219 NODE	*varARGC;			/* Quick access to ARGC */
220 NODE	*varSUBSEP;			/* Quick access to SUBSEP */
221 NODE	*varENVIRON;			/* Pointer to ENVIRON variable */
222 NODE	*varSYMTAB;			/* Symbol table special variable */
223 NODE	*varFILENAME;			/* Node for FILENAME variable */
224 NODE	*curnode;			/* Pointer to current line */
225 NODE	*inc_oper;			/* used by INC/DEC in awk3.c */
226 NODE	*asn_oper;			/* used by AADD, etc in awk3.c */
227