Unnamed Fossil Project

Check-in [c3966df31c]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:import nawk from illumos(1)
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1:c3966df31c6cd1cd1481040d559c92535b8e553c
User & Date: labla 2012-05-17 18:24:47
Context
2012-05-17
19:09
add missing headers and missing declarations check-in: 1e5b57e115 user: labla tags: trunk
18:24
import nawk from illumos(1) check-in: c3966df31c user: labla tags: trunk
17:07
initial empty check-in check-in: 598f097076 user: onys tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added awk.h.



































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * awk -- common header file.
 *
 * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
 *
 * This version uses the POSIX.2 compatible <regex.h> routines.
 *
 * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
 *
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <math.h>
#include <limits.h>
#include <stdlib.h>
#include <regex.h>
#include <errno.h>
#include <sys/types.h>
#include <locale.h>
#include <wchar.h>
#include <widec.h>

#define	YYMAXDEPTH	300	/* Max # of productions (used by yacc) */
#define	YYSSIZE		300	/* Size of State/Value stacks (MKS YACC) */
#define	MAXDIGINT	19	/* Number of digits in an INT */
#define	FNULL		((FILE *)0)
#define	NNULL		((NODE *)0)
#define	SNULL		((STRING)0)
#define	LARGE		INT_MAX	/* Large integer */
#define	NPFILE		32	/* Number of -[fl] options allowed */
#define	NRECUR		3000	/* Maximum recursion depth */

#define	M_LDATA	1
#ifdef M_LDATA
#define	NLINE	20000	/* Longest input record */
#define	NFIELD	4000	/* Number of fields allowed */
#define	NBUCKET	1024	/* # of symtab buckets (power of 2) */
#else
#define	NLINE	2048	/* Longest input record */
#define	NFIELD	1024	/* Number of fields allowed */
#define	NBUCKET	256	/* # of symtab buckets (power of 2) */
#endif

#define	NSNODE		40	/* Number of cached nodes */
#define	NCONTEXT	50	/* Amount of context for error msgs */
#define	hashbuck(n)	((n)&(NBUCKET-1))
#if	BSD
/*
 * A speedup for BSD.  Use their routines which are
 * already optimised.  Note that BSD bcopy does not
 * return a value.
 */
int	bcmp();
#define	memcmp(b1, b2, n)	bcmp(b1, b2, n)
void	bcopy();
#define	memcpy(b1, b2, n)	bcopy(b2, b1, (int)n)
#endif	/* BSD */
#define	vlook(n)	vlookup(n, 0)

/*
 * Basic AWK internal types.
 */
typedef	double		REAL;
typedef	long long	INT;
typedef	wchar_t		*STRING;
typedef	struct NODE	*(*FUNCTION)(struct NODE *np);
typedef	void		*REGEXP;

/*
 * Node in the AWK interpreter expression tree.
 */
typedef	struct	NODE	{
	ushort_t	n_type;
	struct NODE	*n_next;		/* Symbol table/PARM link */
	ushort_t	n_flags;		/* Node flags, type */




	union	{
		struct	{
			ushort_t	N_hash;		/* Full hash value */
			struct NODE	*N_alink;	/* Array link */
			union	{
				struct	{
					STRING	N_string;
					size_t	N_strlen;
				}	n_str;
				INT	N_int;
				REAL	N_real;
				FUNCTION	N_function;
				struct	NODE	*N_ufunc;
			}	n_tun;
			wchar_t	N_name[1];
		}	n_term;
		struct	{
			struct	NODE	*N_left;
			struct	NODE	*N_right;
			ushort_t	N_lineno;
		}	n_op;
		struct {
			struct	NODE	*N_left;	/* Used for fliplist */
			struct	NODE	*N_right;
			REGEXP		N_regexp;	/* Regular expression */
		}	n_re;
	}	n_un;
}	NODE;

/*
 * Definitions to make the node access much easier.
 */
#define	n_hash		n_un.n_term.N_hash	/* full hash value is sym tbl */
#define	n_scope		n_un.n_term.N_hash	/* local variable scope level */
#define	n_alink		n_un.n_term.N_alink	/* link to array list */
#define	n_string	n_un.n_term.n_tun.n_str.N_string
#define	n_strlen	n_un.n_term.n_tun.n_str.N_strlen
#define	n_int		n_un.n_term.n_tun.N_int
#define	n_real		n_un.n_term.n_tun.N_real
#define	n_function	n_un.n_term.n_tun.N_function
#define	n_ufunc		n_un.n_term.n_tun.N_ufunc
#define	n_name		n_un.n_term.N_name
#define	n_left		n_un.n_op.N_left
#define	n_right		n_un.n_op.N_right
#define	n_lineno	n_un.n_op.N_lineno
#define	n_keywtype	n_un.n_op.N_lineno
#define	n_regexp	n_un.n_re.N_regexp
/*
 * Compress the types that are actually used in the final tree
 * to save space in the intermediate file. Allows 1 byte to
 * represent all types
 */







/*
 * n_flags bit assignments.
 */
#define	FALLOC		0x01	/* Allocated node */
#define	FSTATIC		0x00	/* Not allocated */
#define	FMATCH		0x02	/* pattern,pattern (first part matches) */
#define	FSPECIAL	0x04	/* Special pre-computed variable */
#define	FINARRAY	0x08	/* NODE installed in N_alink array list */
#define	FNOALLOC	0x10	/* mark node FALLOC, but don't malloc */
#define	FSENSE		0x20	/* Sense if string looks like INT/REAL */
#define	FSAVE		(FSPECIAL|FINARRAY)	/* assign leaves on */

#define	FINT		0x40	/* Node has integer type */
#define	FREAL		0x80	/* Node has real type */
#define	FSTRING		0x100	/* Node has string type */
#define	FNONTOK		0x200	/* Node has non-token type */
#define	FVINT		0x400	/* Node looks like an integer */
#define	FVREAL		0x800	/* Node looks like a real number */
#define	FLARRAY		0x1000	/* Local array node */

/*
 * n_flags macros
 * These work when given an argument of np->n_flags
 */
#define	isleaf(f)	(!((f)&FNONTOK))
#define	isstring(f)	((f)&FSTRING)
#define	isastring(f)	(((f)&(FSTRING|FALLOC)) == (FSTRING|FALLOC))
#define	isnumber(f)	((f)&(FINT|FVINT|FREAL|FVREAL))
#define	isreal(f)	((f)&(FREAL|FVREAL))
#define	isint(f)	((f)&(FINT|FVINT))

/*
 * Prototype file size is defined in awksize.h
 */





/*
 * Awkrun prototype default name
 */
#if defined(DOS)
#if defined(__386__)
#define	AWK_PROTOTYPE  M_ETCDIR(awkrunf.dos)
#define	AWK_LPROTOTYPE M_ETCDIR(awkrunf.dos)
#else
#define	AWK_PROTOTYPE  M_ETCDIR(awkrun.dos)
#define	AWK_LPROTOTYPE M_ETCDIR(awkrunl.dos)
#endif
#elif defined(OS2)
#define	AWK_PROTOTYPE M_ETCDIR(awkrun.os2)
#elif defined(NT)
#define	AWK_PROTOTYPE M_ETCDIR(awkrun.nt)
#else
#define	AWK_PROTOTYPE M_ETCDIR(awkrun.mod)
#endif

/*
 * This is a kludge that gets around a bug in compact & large
 * models under DOS.  It also makes the generated
 * code faster even if there wasn't a bug.  UNIX people: try
 * to ignore these noisy "near" declarations.
 */
#ifndef	DOS
#define	near
#endif

typedef	wchar_t	near	*LOCCHARP;	/* pointer to local strings */
/*
 * Form of builtin symbols
 * This should be a union because only one of r_ivalue
 * and r_svalue is needed, but (alas) unions cannot be
 * initialised.
 */
typedef	struct	RESERVED {
	LOCCHARP	r_name;
	int		r_type;		/* Type of node */
	INT		r_ivalue;	/* Integer value or wcslen(r_svalue) */
	STRING		r_svalue;	/* String value */
}	RESERVED;

/*
 * Table of builtin functions.
 */
typedef	struct	RESFUNC {
	LOCCHARP	rf_name;
	int		rf_type;	/* FUNC || GETLINE */
	FUNCTION	rf_func;	/* Function pointer */
}	RESFUNC;

/*
 * Structure holding list of open files.
 */
typedef	struct	OFILE	{
	ushort_t f_mode;		/* Open mode: WRITE, APPEND, PIPE */
	FILE	*f_fp;			/* File pointer if open */
	char	*f_name;		/* Remembered file name */
}	OFILE;

/* Global functions -- awk.y */
int	yyparse(void);

/* Global functions -- awk1.c */
#ifdef __WATCOMC__
#pragma aux yyerror aborts;
#pragma aux awkerr aborts;
#pragma aux awkperr aborts;
#endif
void	yyerror(char *msg, ...);
void	awkerr(char *fmt, ...);
void	awkperr(char *fmt, ...);
void	uexit(NODE *);
int	yylex(void);
NODE	*renode(wchar_t *restr);
wchar_t	*emalloc(unsigned);
wchar_t	*erealloc(wchar_t *, unsigned);

/* Global functions -- awk2.c */
void	awk(void);
void	dobegin(void);
void	doend(int status);
int	nextrecord(wchar_t *buf, FILE *fp);
wchar_t	*defrecord(wchar_t *bp, int lim, FILE *fp);
wchar_t	*charrecord(wchar_t *bp, int lim, FILE *fp);
wchar_t	*multirecord(wchar_t *bp, int lim, FILE *fp);
wchar_t	*whitefield(wchar_t **endp);
wchar_t	*blackfield(wchar_t **endp);
wchar_t	*refield(wchar_t **endp);
void	s_print(NODE *np);
void	s_prf(NODE *np);
size_t	xprintf(NODE *np, FILE *fp, wchar_t **cp);
void	awkclose(OFILE *op);

/* Global functions -- awk3.c */
void	strassign(NODE *np, STRING string, int flags, size_t length);
NODE	*nassign(NODE *np, NODE *value);
NODE	*assign(NODE *np, NODE *value);
void	delarray(NODE *np);
NODE	*node(int type, NODE *left, NODE *right);
NODE	*intnode(INT i);
NODE	*realnode(REAL r);
NODE	*stringnode(STRING str, int aflag, size_t wcslen);
NODE	*vlookup(wchar_t *name, int nocreate);
NODE	*emptynode(int type, size_t nlength);
void	freenode(NODE *np);
void	execute(NODE *np);
INT	exprint(NODE *np);
REAL	exprreal(NODE *np);
STRING	exprstring(NODE *np);
STRING	strsave(wchar_t *string);
NODE	*exprreduce(NODE *np);
NODE	*getlist(NODE **npp);
NODE	*symwalk(int *buckp, NODE **npp);
REGEXP	getregexp(NODE *np);
void	addsymtab(NODE *np);
void	delsymtab(NODE *np, int fflag);
NODE	* finstall(LOCCHARP name, FUNCTION f, int type);
void	kinstall(LOCCHARP name, int type);
void	fieldsplit(void);
void	promote(NODE *);







/* Global functions -- awk4.c */
NODE	*f_exp(NODE *np);
NODE	*f_int(NODE *np);
NODE	*f_log(NODE *np);
NODE	*f_sqrt(NODE *np);
NODE	*f_getline(NODE *np);
NODE	*f_index(NODE *np);
NODE	*f_length(NODE *np);
NODE	*f_split(NODE *np);
NODE	*f_sprintf(NODE *np);
NODE	*f_substr(NODE *np);
NODE	*f_rand(NODE *np);
NODE	*f_srand(NODE *np);
NODE	*f_sin(NODE *np);
NODE	*f_cos(NODE *np);
NODE	*f_atan2(NODE *np);
NODE	*f_sub(NODE *np);
NODE	*f_gsub(NODE *np);
NODE	*f_match(NODE *np);
NODE	*f_system(NODE *np);
NODE	*f_ord(NODE *np);
NODE	*f_tolower(NODE *np);
NODE	*f_toupper(NODE *np);
NODE	*f_close(NODE *np);
NODE	*f_asort(NODE *np);

/* In awk0.c */



extern	wchar_t	_null[];
extern	char	r[];
extern	char	w[];
extern	wchar_t	s_OFMT[];
extern	wchar_t	s_CONVFMT[];
extern	wchar_t	s_NR[];
extern	wchar_t	s_NF[];
extern	wchar_t	s_OFS[];
extern	wchar_t	s_ORS[];
extern	wchar_t	s_RS[];
extern	wchar_t	s_FS[];
extern	wchar_t	s_FNR[];
extern	wchar_t	s_SUBSEP[];
extern	wchar_t	s_ARGC[], s_ARGV[], s_ENVIRON[];
extern	wchar_t	s_FILENAME[], s_SYMTAB[];
extern	wchar_t	s_BEGIN[], s_END[], s_next[];
extern	wchar_t	_begin[], _end[];
extern	wchar_t	s_exp[], s_getline[], s_index[], s_int[], s_length[], s_log[];
extern	wchar_t	s_split[], s_sprintf[], s_sqrt[], s_substr[];
extern	wchar_t	s_rand[], s_srand[], s_sin[], s_cos[], s_atan2[];
extern	wchar_t	s_sub[], s_gsub[], s_match[], s_system[], s_ord[];
extern	wchar_t	s_toupper[], s_tolower[], s_asort[];
extern	wchar_t	s_close[];
extern	wchar_t	redelim;
extern	unsigned char	inprint;
extern	unsigned char	funparm;
extern	unsigned char	splitdone;
extern	uint_t	npattern;
extern	uint_t	nfield;
extern	uint_t	fcount;
extern	uint_t	phase;
extern	uint_t	running;
extern	uchar_t	catterm;
extern	uint_t	lexlast;
extern	uint_t	lineno;
extern	uchar_t	needsplit, needenviron, doing_begin, begin_getline;
extern	ushort_t	slevel;
extern	ushort_t	loopexit;
extern	wchar_t	radixpoint;
extern	REGEXP	resep;
extern	RESERVED	reserved[];
extern	RESFUNC		resfuncs[];
extern	long	NIOSTREAM;	/* Maximum open I/O streams */
extern	OFILE	*ofiles;
extern	wchar_t	*linebuf;
extern	size_t	lbuflen;
extern	char	interr[];
extern	char	nomem[];
extern	NODE	*symtab[NBUCKET];
extern	NODE	*yytree;
extern	NODE	*freelist;
extern	wchar_t	*(*awkrecord)(wchar_t *, int, FILE *);
extern	wchar_t	*(*awkfield)(wchar_t **);

extern	NODE	*constant;
extern	NODE	*const0;
extern	NODE	*const1;
extern	NODE	*constundef;
extern	NODE	*field0;
extern	NODE	*incNR;
extern	NODE	*incFNR;
extern	NODE	*clrFNR;
extern	NODE	*ARGVsubi;
extern	NODE	*varNR;
extern	NODE	*varFNR;
extern	NODE	*varNF;
extern	NODE	*varOFMT;
extern	NODE	*varCONVFMT;
extern	NODE	*varOFS;
extern	NODE	*varORS;
extern	NODE	*varFS;
extern	NODE	*varRS;
extern	NODE	*varARGC;
extern	NODE	*varSUBSEP;
extern	NODE	*varENVIRON;
extern	NODE	*varSYMTAB;
extern	NODE	*varFILENAME;
extern	NODE	*curnode;
extern	NODE    *inc_oper;
extern	NODE	*asn_oper;

extern char *mbunconvert(wchar_t *);
extern	wchar_t 	*mbstowcsdup(char *);
extern	char		*wcstombsdup(wchar_t *);
extern	void		awkerr(char *, ...);
/*
 * The following defines the expected max length in chars of a printed number.
 * This should be the longest expected size for any type of number
 * ie. float, long etc. This number is used to calculate the approximate
 * number of chars needed to hold the number.
 */
#ifdef M_NUMSIZE
#define	NUMSIZE M_NUMSIZE
#else
#define	NUMSIZE 30
#endif

#define	M_MB_L(s)	L##s
#ifdef  __STDC__
#define	ANSI(x) x
#else
#define	const
#define	signed
#define	volatile
#define	ANSI(x) ()
#endif

#define	isWblank(x) (((x) == ' ' || (x) == '\t') ? 1 : 0)


/*
 * Wide character version of regular expression functions.
 */
#define	REGWMATCH_T	int_regwmatch_t
#define	REGWCOMP	int_regwcomp
#define	REGWEXEC	int_regwexec
#define	REGWFREE	int_regwfree
#define	REGWERROR	int_regwerror
#define	REGWDOSUBA	int_regwdosuba

typedef struct {
	const wchar_t	*rm_sp, *rm_ep;
	regoff_t	rm_so, rm_eo;
} int_regwmatch_t;

extern int int_regwcomp(REGEXP *, const wchar_t *);
extern int int_regwexec(REGEXP, const wchar_t *, size_t,
			int_regwmatch_t *, int);
extern void int_regwfree(REGEXP);
extern size_t int_regwerror(int, REGEXP, char *, size_t);
extern int int_regwdosuba(REGEXP, const wchar_t *,
			const wchar_t *, wchar_t **, int, int *);

Added awk.y.



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
%{
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * awk -- YACC grammar
 *
 * Copyright (c) 1995 by Sun Microsystems, Inc.
 *
 * Copyright 1986, 1992 by Mortice Kern Systems Inc.  All rights reserved.
 *
 * This Software is unpublished, valuable, confidential property of
 * Mortice Kern Systems Inc.  Use is authorized only in accordance
 * with the terms and conditions of the source licence agreement
 * protecting this Software.  Any unauthorized use or disclosure of
 * this Software is strictly prohibited and will result in the
 * termination of the licence agreement.
 *
 * NOTE: this grammar correctly produces NO shift/reduce conflicts from YACC.
 *
 */

/*
 * Do not use any character constants as tokens, so the resulting C file
 * is codeset independent.
 */

#ident	"%Z%%M%	%I%	%E% SMI"
#include "awk.h"
static NODE * fliplist ANSI((NODE *np));
%}

%union	{
	NODE	*node;
};

/*
 * Do not use any character constants as tokens, so the resulting C file
 * is codeset independent.
 *
 * Declare terminal symbols before their operator
 * precedences to get them in a contiguous block
 * for giant switches in action() and exprreduce().
 */
/* Tokens from exprreduce() */
%token	<node>	PARM ARRAY UFUNC FIELD IN INDEX CONCAT
%token	<node>	NOT AND OR EXP QUEST
%token	<node>	EQ NE GE LE GT LT
%token	<node>	ADD SUB MUL DIV REM INC DEC PRE_INC PRE_DEC
%token	<node>	GETLINE CALLFUNC RE TILDE NRE

/* Tokens shared by exprreduce() and action() */
%token		ASG

/* Tokens from action() */
%token	<node>	PRINT PRINTF
%token	<node>	EXIT RETURN BREAK CONTINUE NEXT
%token	<node>	DELETE WHILE DO FOR FORIN IF

/*
 * Terminal symbols not used in action() and exprrreduce()
 * switch statements.
 */
%token	<node>	CONSTANT VAR FUNC
%token	<node>	DEFFUNC BEGIN END CLOSE ELSE PACT
%right		ELSE
%token		DOT CALLUFUNC

/*
 * Tokens not used in grammar
 */
%token		KEYWORD SVAR
%token		PIPESYM

/*
 * Tokens representing character constants
 * TILDE, '~', taken care of above
 */
%token BAR		/* '|' */ 
       CARAT		/* '^' */ 	
       LANGLE		/* '<' */
       RANGLE		/* '>' */
       PLUSC		/* '+' */
       HYPHEN		/* '-' */
       STAR		/* '*' */
       SLASH		/* '/' */
       PERCENT		/* '%' */
       EXCLAMATION	/* '!' */
       DOLLAR		/* '$' */
       LSQUARE		/* '[' */
       RSQUARE		/* ']' */
       LPAREN		/* '(' */
       RPAREN		/* ')' */
       SEMI		/* ';' */
       LBRACE		/* '{' */
       RBRACE		/* '}' */

/*
 * Priorities of operators
 * Lowest to highest
 */
%left	COMMA
%right	BAR PIPE WRITE APPEND
%right	ASG AADD ASUB AMUL ADIV AREM AEXP
%right	QUEST COLON
%left	OR
%left	AND
%left	IN
%left	CARAT	
%left	TILDE NRE
%left	EQ NE LANGLE RANGLE GE LE
%left	CONCAT
%left	PLUSC HYPHEN 
%left	STAR SLASH PERCENT 
%right	UPLUS UMINUS
%right	EXCLAMATION	
%right	EXP
%right	INC DEC URE
%left	DOLLAR LSQUARE RSQUARE
%left	LPAREN RPAREN 

%type	<node>	prog rule pattern expr rvalue lvalue fexpr varlist varlist2
%type	<node>	statement statlist fileout exprlist eexprlist simplepattern
%type	<node>	getline optvar var
%type	<node>	dummy

%start	dummy
%%

dummy:
		prog			= {
			yytree = fliplist(yytree);
		}
		;
prog:
	  rule				= {
		yytree = $1;
	}
	| rule SEMI prog		= {
		if ($1 != NNULL) {
			if (yytree != NNULL)
				yytree = node(COMMA, $1, yytree); else
				yytree = $1;
		}
	}
	;

rule:	  pattern LBRACE statlist RBRACE	= {
		$$ = node(PACT, $1, $3);
		doing_begin = 0;
	}
	| LBRACE statlist RBRACE		= {
		npattern++;
		$$ = node(PACT, NNULL, $2);
	}
	| pattern				= {
		$$ = node(PACT, $1, node(PRINT, NNULL, NNULL));
		doing_begin = 0;
	}
	| DEFFUNC VAR
		{ $2->n_type = UFUNC; funparm = 1; }
	    LPAREN varlist RPAREN 
		{ funparm = 0; }
	    LBRACE statlist { uexit($5); } RBRACE = {
		$2->n_ufunc = node(DEFFUNC, $5, fliplist($9));
		$$ = NNULL;
	}
	| DEFFUNC UFUNC				= {
		awkerr((char *) gettext("function \"%S\" redefined"), $2->n_name);
		/* NOTREACHED */
	}
	|					= {
		$$ = NNULL;
	}
	;

pattern:
	  simplepattern
	| expr COMMA expr			= {
		++npattern;
		$$ = node(COMMA, $1, $3);
	}
	;

simplepattern:
	  BEGIN					= {
		$$ = node(BEGIN, NNULL, NNULL);
		doing_begin++;
	}
	| END					= {
		++npattern;
		$$ = node(END, NNULL, NNULL);
	}
	| expr					 = {
		++npattern;
		$$ = $1;
	}
	;

eexprlist:
	  exprlist
	|					= {
		$$ = NNULL;
	}
	;

exprlist:
	  expr %prec COMMA
	| exprlist COMMA expr			= {
		$$ = node(COMMA, $1, $3);
	}
	;

varlist:
	  					= {
		$$ = NNULL;
	}
	| varlist2
	;

varlist2:
	  var
	| var COMMA varlist2			= {
		$$ = node(COMMA, $1, $3);
	}
	;

fexpr:
	  expr
	|					= {
		$$ = NNULL;
	}
	;

/*
 * Normal expression (includes regular expression)
 */
expr:
	  expr PLUSC expr			= {
		$$ = node(ADD, $1, $3);
	}
	| expr HYPHEN expr			= {
		$$ = node(SUB, $1, $3);
	}
	| expr STAR expr			= {
		$$ = node(MUL, $1, $3);
	}
	| expr SLASH expr			= {
		$$ = node(DIV, $1, $3);
	}
	| expr PERCENT expr			= {
		$$ = node(REM, $1, $3);
	}
	| expr EXP expr				= {
		$$ = node(EXP, $1, $3);
	}
	| expr AND expr				= {
		$$ = node(AND, $1, $3);
	}
	| expr OR expr				= {
		$$ = node(OR, $1, $3);
	}
	| expr QUEST expr COLON expr		= {
		$$ = node(QUEST, $1, node(COLON, $3, $5));
	}
	| lvalue ASG expr			= {
		$$ = node(ASG, $1, $3);
	}
	| lvalue AADD expr			= {
		$$ = node(AADD, $1, $3);
	}
	| lvalue ASUB expr			= {
		$$ = node(ASUB, $1, $3);
	}
	| lvalue AMUL expr			= {
		$$ = node(AMUL, $1, $3);
	}
	| lvalue ADIV expr			= {
		$$ = node(ADIV, $1, $3);
	}
	| lvalue AREM expr			= {
		$$ = node(AREM, $1, $3);
	}
	| lvalue AEXP expr			= {
		$$ = node(AEXP, $1, $3);
	}
	| lvalue INC				= {
		$$ = node(INC, $1, NNULL);
	}
	| lvalue DEC				= {
		$$ = node(DEC, $1, NNULL);
	}
	| expr EQ expr				= {
		$$ = node(EQ, $1, $3);
	}
	| expr NE expr				= {
		$$ = node(NE, $1, $3);
	}
	| expr RANGLE expr			= {
		$$ = node(GT, $1, $3);
	}
	| expr LANGLE expr			= {
		$$ = node(LT, $1, $3);
	}
	| expr GE expr				= {
		$$ = node(GE, $1, $3);
	}
	| expr LE expr				= {
		$$ = node(LE, $1, $3);
	}
	| expr TILDE expr			= {
		$$ = node(TILDE, $1, $3);
	}
	| expr NRE expr				= {
		$$ = node(NRE, $1, $3);
	}
	| expr IN var				= {
		$$ = node(IN, $3, $1);
	}
	| LPAREN exprlist RPAREN IN var		= {
		$$ = node(IN, $5, $2);
	}
	| getline
	| rvalue
	| expr CONCAT expr			= {
		$$ = node(CONCAT, $1, $3);
	}
	;

lvalue:
	  DOLLAR rvalue				= {
		$$ = node(FIELD, $2, NNULL);
	}
	/*
	 * Prevents conflict with FOR LPAREN var IN var RPAREN production
	 */
	| var %prec COMMA
	| var LSQUARE exprlist RSQUARE		= {
		$$ = node(INDEX, $1, $3);
	}
	;

var:
	  VAR
	| PARM
	;

rvalue:
	  lvalue %prec COMMA
	| CONSTANT
	| LPAREN expr RPAREN term		= {
		$$ = $2;
	}
	| EXCLAMATION expr			= {
		$$ = node(NOT, $2, NNULL);
	}
	| HYPHEN expr %prec UMINUS		= {
		$$ = node(SUB, const0, $2);
	}
	| PLUSC expr %prec UPLUS		= {
		$$ = $2;
	}
	| DEC lvalue				= {
		$$ = node(PRE_DEC, $2, NNULL);
	}
	| INC lvalue				= {
		$$ = node(PRE_INC, $2, NNULL);
	}
	| FUNC					= {
		$$ = node(CALLFUNC, $1, NNULL);
	}
	| FUNC LPAREN eexprlist RPAREN term	= {
		$$ = node(CALLFUNC, $1, $3);
	}
	| UFUNC LPAREN eexprlist RPAREN term	= {
		$$ = node(CALLUFUNC, $1, $3);
	}
	| VAR LPAREN eexprlist RPAREN term	= {
		$$ = node(CALLUFUNC, $1, $3);
	}
	| SLASH {redelim='/';} URE SLASH %prec URE	= {
		$$ = $<node>3;
	}
	;

statement:
	  FOR LPAREN fexpr SEMI fexpr SEMI fexpr RPAREN statement = {
		$$ = node(FOR, node(COMMA, $3, node(COMMA, $5, $7)), $9);
	}
	| FOR LPAREN var IN var RPAREN statement = {
		register NODE *np;

		/*
		 * attempt to optimize statements for the form 
		 *    for (i in x) delete x[i]
		 * to
		 *    delete x
		 */
		np = $7;
		if (np != NNULL
		 && np->n_type == DELETE
		 && (np = np->n_left)->n_type == INDEX
		 && np->n_left == $5
		 && np->n_right == $3)
			$$ = node(DELETE, $5, NNULL);
		else
			$$ = node(FORIN, node(IN, $3, $5), $7);
	}
	| WHILE LPAREN expr RPAREN statement	= {
		$$ = node(WHILE, $3, $5);
	}
	| DO statement WHILE LPAREN expr RPAREN	= {
		$$ = node(DO, $5, $2);
	}
	| IF LPAREN expr RPAREN statement ELSE statement = {
		$$ = node(IF, $3, node(ELSE, $5, $7));
	}
	| IF LPAREN expr RPAREN statement %prec ELSE	= {
		$$ = node(IF, $3, node(ELSE, $5, NNULL));
	}
	| CONTINUE SEMI				= {
		$$ = node(CONTINUE, NNULL, NNULL);
	}
	| BREAK SEMI				= {
		$$ = node(BREAK, NNULL, NNULL);
	}
	| NEXT SEMI				= {
		$$ = node(NEXT, NNULL, NNULL);
	}
	| DELETE lvalue SEMI			= {
		$$ = node(DELETE, $2, NNULL);
	}
	| RETURN fexpr SEMI			= {
		$$ = node(RETURN, $2, NNULL);
	}
	| EXIT fexpr SEMI			= {
		$$ = node(EXIT, $2, NNULL);
	}
	| PRINT eexprlist fileout SEMI		= {
		$$ = node(PRINT, $2, $3);
	}
	| PRINT LPAREN exprlist RPAREN fileout SEMI	= {
		$$ = node(PRINT, $3, $5);
	}
	| PRINTF exprlist fileout SEMI		= {
		$$ = node(PRINTF, $2, $3);
	}
	| PRINTF LPAREN exprlist RPAREN fileout SEMI	= {
		$$ = node(PRINTF, $3, $5);
	}
	| expr SEMI 				= {
		$$ = $1;
	}
	| SEMI					= {
		$$ = NNULL;
	}
	| LBRACE statlist RBRACE		= {
		$$ = $2;
	}
	;


statlist:
	  statement
	| statlist statement			= {
		if ($1 == NNULL)
			$$ = $2;
		else if ($2 == NNULL)
			$$ = $1;
		else
			$$ = node(COMMA, $1, $2);
	}
	;

fileout:
	  WRITE expr				= {
		$$ = node(WRITE, $2, NNULL);
	}
	| APPEND expr				= {
		$$ = node(APPEND, $2, NNULL);
	}
	| PIPE expr				= {
		$$ = node(PIPE, $2, NNULL);
	}
	|					= {
		$$ = NNULL;
	}
	;

getline:
	  GETLINE optvar %prec WRITE		= {
		$$ = node(GETLINE, $2, NNULL);
	}
	| expr BAR GETLINE optvar		= {
		$$ = node(GETLINE, $4, node(PIPESYM, $1, NNULL));
	}
	| GETLINE optvar LANGLE expr		= {
		$$ = node(GETLINE, $2, node(LT, $4, NNULL));
	}
	;

optvar:
	  lvalue
	|					= {
		$$ = NNULL;
	}
	;

term:
	  {catterm = 1;}
	;
%%
/*
 * Flip a left-recursively generated list
 * so that it can easily be traversed from left
 * to right without recursion.
 */
static NODE *
fliplist(np)
register NODE *np;
{
	int type;

	if (np!=NNULL && !isleaf(np->n_flags)
#if 0
		 && (type = np->n_type)!=FUNC && type!=UFUNC
#endif
	) {
		np->n_right = fliplist(np->n_right);
		if ((type=np->n_type)==COMMA) {
			register NODE *lp;

			while ((lp = np->n_left)!=NNULL && lp->n_type==COMMA) {
				register NODE* *spp;

				lp->n_right = fliplist(lp->n_right);
				for (spp = &lp->n_right;
				    *spp != NNULL && (*spp)->n_type==COMMA;
				     spp = &(*spp)->n_right)
					;
				np->n_left = *spp;
				*spp = np;
				np = lp;
			}
		}
		if (np->n_left != NULL &&
		    (type = np->n_left->n_type)!= FUNC && type!=UFUNC)
			np->n_left = fliplist(np->n_left);
	}
	return (np);
}

Added awk0.c.





































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Awk -- data definitions
 *
 * Copyright (c) 1995 by Sun Microsystems, Inc.
 *
 * Copyright 1986, 1992 by Mortice Kern Systems Inc.  All rights reserved.
 *
 * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "awk.h"
#include "y.tab.h"

/*
 * This file contains data definitions for awk.
 */

RESERVED	reserved[] = {
	s_BEGIN, KEYWORD, BEGIN, NULL,
	s_END, KEYWORD, END, NULL,
	M_MB_L("break"), KEYWORD, BREAK, NULL,
	M_MB_L("continue"), KEYWORD, CONTINUE, NULL,
	M_MB_L("for"), KEYWORD, FOR, NULL,
	M_MB_L("if"), KEYWORD, IF, NULL,
	M_MB_L("else"), KEYWORD, ELSE, NULL,
	M_MB_L("in"), KEYWORD, IN, NULL,
	s_next, KEYWORD, NEXT, NULL,
	M_MB_L("while"), KEYWORD, WHILE, NULL,
	M_MB_L("do"), KEYWORD, DO, NULL,
	M_MB_L("print"), KEYWORD, PRINT, NULL,
	M_MB_L("printf"), KEYWORD, PRINTF, NULL,
	M_MB_L("return"), KEYWORD, RETURN, NULL,
	M_MB_L("func"), KEYWORD, DEFFUNC, NULL,
	M_MB_L("function"), KEYWORD, DEFFUNC, NULL,
	M_MB_L("delete"), KEYWORD, DELETE, NULL,
	M_MB_L("exit"), KEYWORD, EXIT, NULL,
	s_FILENAME, VAR, 0, _null,
	s_NF, SVAR, 0, NULL,
	s_NR, VAR, 0, NULL,
	s_FS, SVAR, 1, M_MB_L(" "),
	s_OFS, VAR, 1, M_MB_L(" "),
	s_ORS, VAR, 1, M_MB_L("\n"),
	s_OFMT, VAR, 4, M_MB_L("%.6g"),
	s_CONVFMT, VAR, 4, M_MB_L("%.6g"),
	s_RS, SVAR, 1, M_MB_L("\n"),
	s_FNR, VAR, 0, NULL,
	s_SUBSEP, VAR, 1,
#ifdef	M_AWK_SUBSEP
	M_AWK_SUBSEP,
#else
	M_MB_L("\34"),
#endif
	s_ARGC, SVAR, 0, NULL,
	(LOCCHARP)NULL
};

RESFUNC	resfuncs[] = {
	s_exp, FUNC, f_exp,
	s_getline, GETLINE, f_getline,
	s_index, FUNC, f_index,
	s_int, FUNC, f_int,
	s_length, FUNC, f_length,
	s_log, FUNC, f_log,
	s_split, FUNC, f_split,
	s_sprintf, FUNC, f_sprintf,
	s_sqrt, FUNC, f_sqrt,
	s_substr, FUNC, f_substr,
	s_rand, FUNC, f_rand,
	s_srand, FUNC, f_srand,
	s_sin, FUNC, f_sin,
	s_cos, FUNC, f_cos,
	s_atan2, FUNC, f_atan2,
	s_sub, FUNC, f_sub,
	s_gsub, FUNC, f_gsub,
	s_match, FUNC, f_match,
	s_system, FUNC, f_system,
	s_ord, FUNC, f_ord,
	s_toupper, FUNC, f_toupper,
	s_tolower, FUNC, f_tolower,
	s_asort, FUNC, f_asort,
	s_close, FUNC, f_close,
	(LOCCHARP)NULL
};


OFILE	*ofiles;			/* Remembered open files (print) */
long	NIOSTREAM = 512;		/* max num of open file descriptors */




wchar_t	_null[] = M_MB_L("");		/* Empty string */
char	r[] = "r";			/* Read file mode */
char	w[] = "w";			/* Write file mode */
wchar_t	s_OFMT[] = M_MB_L("OFMT");	/* Name of "OFMT" variable */
wchar_t	s_CONVFMT[] = M_MB_L("CONVFMT"); /* Name of "CONVFMT" variable */
wchar_t	s_NR[] = M_MB_L("NR");		/* Name of "NR" variable */
wchar_t	s_NF[] = M_MB_L("NF");		/* Name of "NF" variable */
wchar_t	s_OFS[] = M_MB_L("OFS");	/* Name of "OFS" variable */
wchar_t	s_ORS[] = M_MB_L("ORS");	/* Name of "ORS" variable */
wchar_t	s_RS[] = M_MB_L("RS");		/* Name of "RS" variable */
wchar_t	s_FS[] = M_MB_L("FS");		/* Name of "FS" variable */
wchar_t	s_FNR[] = M_MB_L("FNR");	/* Name of "FNR" variable */
wchar_t	s_SUBSEP[] = M_MB_L("SUBSEP");	/* Name of "SUBSEP" variable */
wchar_t	s_ARGC[] = M_MB_L("ARGC");	/* Name of "ARGC" variable */
wchar_t	s_ARGV[] = M_MB_L("ARGV");	/* Name of "ARGV" array variable */
wchar_t	s_ENVIRON[] = M_MB_L("ENVIRON"); /* Name of "ENVIRON" array variable */
wchar_t	s_FILENAME[] = M_MB_L("FILENAME"); /* Name of "FILENAME" variable */
wchar_t	s_SYMTAB[] = M_MB_L("SYMTAB");	/* Name of "SYMTAB" variable */
wchar_t	s_BEGIN[] = M_MB_L("BEGIN");	/* Name of "BEGIN" action */
wchar_t	s_END[] = M_MB_L("END");	/* Name of "END" action */
wchar_t	s_next[] = M_MB_L("next");	/* Name of "next" keyword */
wchar_t	s_exp[] = M_MB_L("exp");	/* Name of "exp" function */
wchar_t	s_getline[] = M_MB_L("getline"); /* Name of "getline" function */
wchar_t	s_index[] = M_MB_L("index");	/* Name of "index" function */
wchar_t	s_int[] = M_MB_L("int");	/* Name of "int" function */
wchar_t	s_length[] = M_MB_L("length");	/* Name of "length" function */
wchar_t	s_log[] = M_MB_L("log");	/* Name of "log" function */
wchar_t	s_split[] = M_MB_L("split");	/* Name of "split" function */
wchar_t	s_sprintf[] = M_MB_L("sprintf"); /* Name of "sprintf" function */
wchar_t	s_sqrt[] = M_MB_L("sqrt");	/* Name of "sqrt" function */
wchar_t	s_substr[] = M_MB_L("substr");	/* Name of "substr" function */
wchar_t	s_rand[] = M_MB_L("rand");	/* Name of "rand" function */
wchar_t	s_srand[] = M_MB_L("srand");	/* Name of "srand" function */
wchar_t	s_sin[] = M_MB_L("sin");	/* Name of "sin" function */
wchar_t	s_cos[] = M_MB_L("cos");	/* Name of "cos" function */
wchar_t	s_atan2[] = M_MB_L("atan2");	/* Name of "atan" function */
wchar_t	s_sub[] = M_MB_L("sub");	/* Name of "sub" function */
wchar_t	s_gsub[] = M_MB_L("gsub");	/* Name of "gsub" function */
wchar_t	s_match[] = M_MB_L("match");	/* Name of "match" function */
wchar_t	s_system[] = M_MB_L("system");	/* Name of "system" function */
wchar_t	s_ord[] = M_MB_L("ord");	/* Name of "ord" function */
wchar_t	s_toupper[] = M_MB_L("toupper"); /* Name of "toupper" function */
wchar_t	s_tolower[] = M_MB_L("tolower"); /* Name of "tolower" function */
wchar_t	s_asort[] = M_MB_L("asort");	/* Name of "asort" function */
wchar_t	s_close[] = M_MB_L("close");	/* Name of "close" function */

wchar_t redelim;			/* Delimiter for regexp (yylex) */
uchar_t	inprint;			/* Special meaning for '>' & '|' */
uchar_t	funparm;			/* Defining function parameters */
uchar_t	splitdone;			/* Line split into fields (fieldbuf) */
uint	npattern;			/* Number of non-BEGIN patterns */
uint	nfield;				/* Number of fields (if splitdone) */
uint	fcount;				/* Field counter (used by blackfield)*/
uint	phase;				/* BEGIN, END, or 0 */
uint	running = 0;			/* Set if not in compile phase */
uchar_t	catterm;			/* Can inject concat or ';' */
uint	lexlast = '\n';			/* Last lexical token */
uint	lineno = 0;			/* Current programme line number */
uchar_t	doing_begin;			/* set if compiling BEGIN block */
uchar_t	begin_getline;			/* flags a getline was done in BEGIN */
uchar_t	needsplit;			/* Set if $0 must be split when read */
uchar_t	needenviron;			/* Set if ENVIRON variable referenced */
ushort	slevel;				/* Scope level (0 == root) */
ushort	loopexit;			/* Short circuit loop with keyword */
wchar_t	radixpoint;			/* soft radix point for I18N */
REGEXP	resep;				/* Field separator as regexp */
wchar_t	*linebuf = NULL;		/* $0 buffer - malloc'd in awk1.c */
size_t	lbuflen;			/* Length of linebuf */

/*
 * XXX - Make sure to check where this error message is printed
 */
char	interr[] = "internal execution tree error at E string";
char	nomem[] =  "insufficient memory for string storage";
NODE	*symtab[NBUCKET];		/* Heads of symbol table buckets */
NODE	*yytree;			/* Code tree */
NODE	*freelist;			/* Free every pattern {action} line */
wchar_t	*(*awkrecord) ANSI((wchar_t *, int, FILE*)) = defrecord;
					/* Function to read a record */
wchar_t	*(*awkfield) ANSI((wchar_t **)) = whitefield;
					/* Function to extract a field */

/*
 * Nodes used to speed up the execution of the
 * interpreter.
 */
NODE	*constant;			/* Node to hold a constant INT */
NODE	*const0;			/* Constant INT 0 node */
NODE	*const1;			/* Constant INT 1 node */
NODE	*constundef;			/* Undefined variable */
NODE	*field0;			/* $0 */
NODE	*incNR;				/* Code to increment NR variable */
NODE	*incFNR;			/* Code to increment FNR variable */
NODE	*clrFNR;			/* Zero FNR variable (each file) */
NODE	*ARGVsubi;			/* Compute ARGV[i] */
NODE	*varNR;				/* Remove search for NR variable */
NODE	*varFNR;			/* Don't search for FNR variable */
NODE	*varNF;				/* Pointer to NF variable */
NODE	*varOFMT;			/* For s_prf */
NODE	*varCONVFMT;			/* For internal conv of float to str */
NODE	*varOFS;			/* For s_print */
NODE	*varORS;			/* For s_print */
NODE	*varFS;				/* Field separtor */
NODE	*varRS;				/* Record separator */
NODE	*varARGC;			/* Quick access to ARGC */
NODE	*varSUBSEP;			/* Quick access to SUBSEP */
NODE	*varENVIRON;			/* Pointer to ENVIRON variable */
NODE	*varSYMTAB;			/* Symbol table special variable */
NODE	*varFILENAME;			/* Node for FILENAME variable */
NODE	*curnode;			/* Pointer to current line */
NODE	*inc_oper;			/* used by INC/DEC in awk3.c */
NODE	*asn_oper;			/* used by AADD, etc in awk3.c */

Added awk1.c.











































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * awk -- mainline, yylex, etc.
 *
 * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
 */

#include "awk.h"
#include "y.tab.h"
#include <stdarg.h>
#include <unistd.h>
#include <locale.h>
#include <search.h>

static char	*progfiles[NPFILE];	/* Programmes files for yylex */
static char	**progfilep = &progfiles[0]; /* Pointer to last file */
static wchar_t	*progptr;		/* In-memory programme */
static int	proglen;		/* Length of progptr */
static wchar_t	context[NCONTEXT];	/* Circular buffer of context */
static wchar_t	*conptr = &context[0];	/* context ptr */
static FILE	*progfp;		/* Stdio stream for programme */
static char	*filename;
#ifdef	DEBUG
static int	dflag;
#endif

#define	AWK_EXEC_MAGIC	"<MKS AWKC>"
#define	LEN_EXEC_MAGIC	10

static char	unbal[] = "unbalanced E char";

static void	awkarginit(int c, char **av);
static int	lexid(wint_t c);
static int	lexnumber(wint_t c);
static int	lexstring(wint_t endc);
static int	lexregexp(wint_t endc);

static void	awkvarinit(void);
static wint_t	lexgetc(void);
static void	lexungetc(wint_t c);
static size_t	lexescape(wint_t endc, int regx, int cmd_line_operand);
static void	awkierr(int perr, char *fmt, va_list ap);
static int	usage(void);
void		strescape(wchar_t *str);
static const char	*toprint(wint_t);
char *_cmdname;
static wchar_t *mbconvert(char *str);

extern int	isclvar(wchar_t *arg);

/*
 * mainline for awk
 */
int
main(int argc, char *argv[])
{
	wchar_t *ap;
	char *cmd;

	cmd = argv[0];
	_cmdname = cmd;

	linebuf = emalloc(NLINE * sizeof (wchar_t));

	/*
	 * At this point only messaging should be internationalized.
	 * numbers are still scanned as in the Posix locale.
	 */
	(void) setlocale(LC_ALL, "");
	(void) setlocale(LC_NUMERIC, "C");
#if !defined(TEXT_DOMAIN)
#define	TEXT_DOMAIN	"SYS_TEST"
#endif
	(void) textdomain(TEXT_DOMAIN);

	awkvarinit();
	/* running = 1; */
	while (argc > 1 && *argv[1] == '-') {
		void *save_ptr = NULL;
		ap = mbstowcsdup(&argv[1][1]);
		if (ap == NULL)
			break;
		if (*ap == '\0') {
			free(ap);
			break;
		}
		save_ptr = (void *) ap;
		++argv;
		--argc;
		if (*ap == '-' && ap[1] == '\0')
			break;
		for (; *ap != '\0'; ++ap) {
			switch (*ap) {
#ifdef DEBUG
			case 'd':
				dflag = 1;
				continue;

#endif
			case 'f':
				if (argc < 2) {
					(void) fprintf(stderr,
				gettext("Missing script file\n"));
					return (1);
				}
				*progfilep++ = argv[1];
				--argc;
				++argv;
				continue;

			case 'F':
				if (ap[1] == '\0') {
					if (argc < 2) {
						(void) fprintf(stderr,
				gettext("Missing field separator\n"));
						return (1);
					}
					ap = mbstowcsdup(argv[1]);
					--argc;
					++argv;
				} else
					++ap;
				strescape(ap);
				strassign(varFS, linebuf, FALLOC,
				    wcslen(linebuf));
				break;

			case 'v': {
				wchar_t *vp;
				wchar_t *arg;

				if (argc < 2) {
					(void) fprintf(stderr,
		gettext("Missing variable assignment\n"));
					return (1);
				}
				arg = mbconvert(argv[1]);
				/*
				 * Ensure the variable expression
				 * is valid (correct form).
				 */
				if (((vp = wcschr(arg, '=')) != NULL) &&
				    isclvar(arg)) {
					*vp = '\0';
					strescape(vp+1);
					strassign(vlook(arg), linebuf,
					    FALLOC|FSENSE,
					    wcslen(linebuf));
					*vp = '=';
				} else {
					(void) fprintf(stderr, gettext(
					    "Invalid form for variable "
					    "assignment: %S\n"), arg);
					return (1);
				}
				--argc;
				++argv;
				continue;
			}

			default:
				(void) fprintf(stderr,
				gettext("Unknown option \"-%S\"\n"), ap);
				return (usage());
			}
			break;
		}
		if (save_ptr)
			free(save_ptr);
	}
	if (progfilep == &progfiles[0]) {
		if (argc < 2)
			return (usage());
		filename = "[command line]";	/* BUG: NEEDS TRANSLATION */
		progptr = mbstowcsdup(argv[1]);
		proglen = wcslen(progptr);
		--argc;
		++argv;
	}

	argv[0] = cmd;

	awkarginit(argc, argv);

	/* running = 0; */
	(void) yyparse();

	lineno = 0;
	/*
	 * Ok, done parsing, so now activate the rest of the nls stuff, set
	 * the radix character.
	 */
	(void) setlocale(LC_ALL, "");
	radixpoint = *localeconv()->decimal_point;
	awk();
	/* NOTREACHED */
	return (0);
}

/*
 * Do initial setup of buffers, etc.
 * This must be called before most processing
 * and especially before lexical analysis.
 * Variables initialised here will be overruled by command
 * line parameter initialisation.
 */
static void
awkvarinit()
{
	NODE *np;

	(void) setvbuf(stderr, NULL, _IONBF, 0);

	if ((NIOSTREAM = sysconf(_SC_OPEN_MAX) - 4) <= 0) {
		(void) fprintf(stderr,
	gettext("not enough available file descriptors"));
		exit(1);
	}
	ofiles = (OFILE *)emalloc(sizeof (OFILE)*NIOSTREAM);
#ifdef A_ZERO_POINTERS
	(void) memset((wchar_t *)ofiles, 0, sizeof (OFILE) * NIOSTREAM);
#else
	{
		/* initialize file descriptor table */
		OFILE *fp;
		for (fp = ofiles; fp < &ofiles[NIOSTREAM]; fp += 1) {
			fp->f_fp = FNULL;
					fp->f_mode = 0;
					fp->f_name = (char *)0;
		}
	}
#endif
	constant = intnode((INT)0);

	const0 = intnode((INT)0);
	const1 = intnode((INT)1);
	constundef = emptynode(CONSTANT, 0);
	constundef->n_flags = FSTRING|FVINT;
	constundef->n_string = _null;
	constundef->n_strlen = 0;
	inc_oper = emptynode(ADD, 0);
	inc_oper->n_right = const1;
	asn_oper = emptynode(ADD, 0);
	field0 = node(FIELD, const0, NNULL);

	{
		RESFUNC near*rp;

		for (rp = &resfuncs[0]; rp->rf_name != (LOCCHARP)NULL; ++rp) {
			np = finstall(rp->rf_name, rp->rf_func, rp->rf_type);
		}
	}
	{
		RESERVED near*rp;

		for (rp = &reserved[0]; rp->r_name != (LOCCHARP)NULL; ++rp) {
			switch (rp->r_type) {
			case SVAR:
			case VAR:
				running = 1;
				np = vlook(rp->r_name);
				if (rp->r_type == SVAR)
					np->n_flags |= FSPECIAL;
				if (rp->r_svalue != NULL)
					strassign(np, rp->r_svalue, FSTATIC,
					    (size_t)rp->r_ivalue);
				else {
					constant->n_int = rp->r_ivalue;
					(void) assign(np, constant);
				}
				running = 0;
				break;

			case KEYWORD:
				kinstall(rp->r_name, (int)rp->r_ivalue);
				break;
			}
		}
	}

	varNR = vlook(s_NR);
	varFNR = vlook(s_FNR);
	varNF = vlook(s_NF);
	varOFMT = vlook(s_OFMT);
	varCONVFMT = vlook(s_CONVFMT);
	varOFS = vlook(s_OFS);
	varORS = vlook(s_ORS);
	varRS = vlook(s_RS);
	varFS = vlook(s_FS);
	varARGC = vlook(s_ARGC);
	varSUBSEP = vlook(s_SUBSEP);
	varENVIRON = vlook(s_ENVIRON);
	varFILENAME = vlook(s_FILENAME);
	varSYMTAB = vlook(s_SYMTAB);
	incNR = node(ASG, varNR, node(ADD, varNR, const1));
	incFNR = node(ASG, varFNR, node(ADD, varFNR, const1));
	clrFNR = node(ASG, varFNR, const0);
}

/*
 * Initialise awk ARGC, ARGV variables.
 */
static void
awkarginit(int ac, char **av)
{
	int i;
	wchar_t *cp;

	ARGVsubi = node(INDEX, vlook(s_ARGV), constant);
	running = 1;
	constant->n_int = ac;
	(void) assign(varARGC, constant);
	for (i = 0; i < ac; ++i) {
		cp = mbstowcsdup(av[i]);
		constant->n_int = i;
		strassign(exprreduce(ARGVsubi), cp,
		    FSTATIC|FSENSE, wcslen(cp));
	}
	running = 0;
}

/*
 * Clean up when done parsing a function.
 * All formal parameters, because of a deal (funparm) in
 * yylex, get put into the symbol table in front of any
 * global variable of the same name.  When the entire
 * function is parsed, remove these formal dummy nodes
 * from the symbol table but retain the nodes because
 * the generated tree points at them.
 */
void
uexit(NODE *np)
{
	NODE *formal;

	while ((formal = getlist(&np)) != NNULL)
		delsymtab(formal, 0);
}

/*
 * The lexical analyzer.
 */
int
yylex()
#ifdef	DEBUG
{
	int l;

	l = yyhex();
	if (dflag)
		(void) printf("%d\n", l);
	return (l);
}
yyhex()
#endif
{
	wint_t c, c1;
	int i;
	static int savetoken = 0;
	static int wasfield;
	static int isfuncdef;
	static int nbrace, nparen, nbracket;
	static struct ctosymstruct {
		wint_t c, sym;
	} ctosym[] = {
		{ '|', BAR },		{ '^', CARAT },
		{ '~', TILDE },		{ '<', LANGLE },
		{ '>', RANGLE },	{ '+', PLUSC },
		{ '-', HYPHEN },	{ '*', STAR },
		{ '/', SLASH },		{ '%', PERCENT },
		{ '!', EXCLAMATION },	{ '$', DOLLAR },
		{ '[', LSQUARE },	{ ']', RSQUARE },
		{ '(', LPAREN },	{ ')', RPAREN },
		{ ';', SEMI },		{ '{', LBRACE },
		{ '}', RBRACE },	{   0, 0 }
	};

	if (savetoken) {
		c = savetoken;
		savetoken = 0;
	} else if (redelim != '\0') {
		c = redelim;
		redelim = 0;
		catterm = 0;
		savetoken = c;
		return (lexlast = lexregexp(c));
	} else while ((c = lexgetc()) != WEOF) {
		if (iswalpha(c) || c == '_') {
			c = lexid(c);
		} else if (iswdigit(c) || c == '.') {
			c = lexnumber(c);
		} else if (isWblank(c)) {
			continue;
		} else switch (c) {
#if DOS || OS2
		case 032:		/* ^Z */
			continue;
#endif

		case '"':
			c = lexstring(c);
			break;

		case '#':
			while ((c = lexgetc()) != '\n' && c != WEOF)
				;
			lexungetc(c);
			continue;

		case '+':
			if ((c1 = lexgetc()) == '+')
				c = INC;
			else if (c1 == '=')
				c = AADD;
			else
				lexungetc(c1);
			break;

		case '-':
			if ((c1 = lexgetc()) == '-')
				c = DEC;
			else if (c1 == '=')
				c = ASUB;
			else
				lexungetc(c1);
			break;

		case '*':
			if ((c1 = lexgetc()) == '=')
				c = AMUL;
			else if (c1 == '*') {
				if ((c1 = lexgetc()) == '=')
					c = AEXP;
				else {
					c = EXP;
					lexungetc(c1);
				}
			} else
				lexungetc(c1);
			break;

		case '^':
			if ((c1 = lexgetc()) == '=') {
				c = AEXP;
			} else {
				c = EXP;
				lexungetc(c1);
			}
			break;

		case '/':
			if ((c1 = lexgetc()) == '=' &&
			    lexlast != RE && lexlast != NRE &&
			    lexlast != ';' && lexlast != '\n' &&
			    lexlast != ',' && lexlast != '(')
				c = ADIV;
			else
				lexungetc(c1);
			break;

		case '%':
			if ((c1 = lexgetc()) == '=')
				c = AREM;
			else
				lexungetc(c1);
			break;

		case '&':
			if ((c1 = lexgetc()) == '&')
				c = AND;
			else
				lexungetc(c1);
			break;

		case '|':
			if ((c1 = lexgetc()) == '|')
				c = OR;
			else {
				lexungetc(c1);
				if (inprint)
					c = PIPE;
			}
			break;

		case '>':
			if ((c1 = lexgetc()) == '=')
				c = GE;
			else if (c1 == '>')
				c = APPEND;
			else {
				lexungetc(c1);
				if (nparen == 0 && inprint)
					c = WRITE;
			}
			break;

		case '<':
			if ((c1 = lexgetc()) == '=')
				c = LE;
			else
				lexungetc(c1);
			break;

		case '!':
			if ((c1 = lexgetc()) == '=')
				c = NE;
			else if (c1 == '~')
				c = NRE;
			else
				lexungetc(c1);
			break;

		case '=':
			if ((c1 = lexgetc()) == '=')
				c = EQ;
			else {
				lexungetc(c1);
				c = ASG;
			}
			break;

		case '\n':
			switch (lexlast) {
			case ')':
				if (catterm || inprint) {
					c = ';';
					break;
				}
			/*FALLTHRU*/
			case AND:
			case OR:
			case COMMA:
			case '{':
			case ELSE:
			case ';':
			case DO:
				continue;

			case '}':
				if (nbrace != 0)
					continue;

			default:
				c = ';';
				break;
			}
			break;

		case ELSE:
			if (lexlast != ';') {
				savetoken = ELSE;
				c = ';';
			}
			break;

		case '(':
			++nparen;
			break;

		case ')':
			if (--nparen < 0)
				awkerr(unbal, "()");
			break;

		case '{':
			nbrace++;
			break;

		case '}':
			if (--nbrace < 0) {
				char brk[3];

				brk[0] = '{';
				brk[1] = '}';
				brk[2] = '\0';
				awkerr(unbal, brk);
			}
			if (lexlast != ';') {
				savetoken = c;
				c = ';';
			}
			break;

		case '[':
			++nbracket;
			break;

		case ']':
			if (--nbracket < 0) {
				char brk[3];

				brk[0] = '[';
				brk[1] = ']';
				brk[2] = '\0';
				awkerr(unbal, brk);
			}
			break;

		case '\\':
			if ((c1 = lexgetc()) == '\n')
				continue;
			lexungetc(c1);
			break;

		case ',':
			c = COMMA;
			break;

		case '?':
			c = QUEST;
			break;

		case ':':
			c = COLON;
			break;

		default:
			if (!iswprint(c))
				awkerr(
				    gettext("invalid character \"%s\""),
				    toprint(c));
			break;
		}
		break;
	}

	switch (c) {
	case ']':
		++catterm;
		break;

	case VAR:
		if (catterm) {
			savetoken = c;
			c = CONCAT;
			catterm = 0;
		} else if (!isfuncdef) {
			if ((c1 = lexgetc()) != '(')
				++catterm;
			lexungetc(c1);
		}
		isfuncdef = 0;
		break;

	case PARM:
	case CONSTANT:
		if (catterm) {
			savetoken = c;
			c = CONCAT;
			catterm = 0;
		} else {
			if (lexlast == '$')
				wasfield = 2;
			++catterm;
		}
		break;

	case INC:
	case DEC:
		if (!catterm || lexlast != CONSTANT || wasfield)
			break;

	/*FALLTHRU*/
	case UFUNC:
	case FUNC:
	case GETLINE:
	case '!':
	case '$':
	case '(':
		if (catterm) {
			savetoken = c;
			c = CONCAT;
			catterm = 0;
		}
		break;

	/* { */ case '}':
		if (nbrace == 0)
			savetoken = ';';
	/*FALLTHRU*/
	case ';':
		inprint = 0;
	/*FALLTHRU*/
	default:
		if (c == DEFFUNC)
			isfuncdef = 1;
		catterm = 0;
	}
	lexlast = c;
	if (wasfield)
		wasfield--;
	/*
	 * Map character constants to symbolic names.
	 */
	for (i = 0; ctosym[i].c != 0; i++)
		if (c == ctosym[i].c) {
			c = ctosym[i].sym;
			break;
		}
	return ((int)c);
}

/*
 * Read a number for the lexical analyzer.
 * Input is the first character of the number.
 * Return value is the lexical type.
 */
static int
lexnumber(wint_t c)
{
	wchar_t *cp;
	int dotfound = 0;
	int efound = 0;
	INT number;

	cp = linebuf;
	do {
		if (iswdigit(c))
			;
		else if (c == '.') {
			if (dotfound++)
				break;
		} else if (c == 'e' || c == 'E') {
			if ((c = lexgetc()) != '-' && c != '+') {
				lexungetc(c);
				c = 'e';
			} else
				*cp++ = 'e';
			if (efound++)
				break;
		} else
			break;
		*cp++ = c;
	} while ((c = lexgetc()) != WEOF);
	*cp = '\0';
	if (dotfound && cp == linebuf+1)
		return (DOT);
	lexungetc(c);
	errno = 0;
	if (!dotfound && !efound &&
	    ((number = wcstol(linebuf, (wchar_t **)0, 10)), errno != ERANGE))
		yylval.node = intnode(number);
	else
		yylval.node = realnode((REAL)wcstod(linebuf, (wchar_t **)0));
	return (CONSTANT);
}

/*
 * Read an identifier.
 * Input is first character of identifier.
 * Return VAR.
 */
static int
lexid(wint_t c)
{
	wchar_t *cp;
	size_t i;
	NODE *np;

	cp = linebuf;
	do {
		*cp++ = c;
		c = lexgetc();
	} while (iswalpha(c) || iswdigit(c) || c == '_');
	*cp = '\0';
	lexungetc(c);
	yylval.node = np = vlook(linebuf);

	switch (np->n_type) {
	case KEYWORD:
		switch (np->n_keywtype) {
		case PRINT:
		case PRINTF:
			++inprint;
		default:
			return ((int)np->n_keywtype);
		}
		/* NOTREACHED */

	case ARRAY:
	case VAR:
		/*
		 * If reading the argument list, create a dummy node
		 * for the duration of that function. These variables
		 * can be removed from the symbol table at function end
		 * but they must still exist because the execution tree
		 * knows about them.
		 */
		if (funparm) {
do_funparm:
			np = emptynode(PARM, i = (cp-linebuf));
			np->n_flags = FSTRING;
			np->n_string = _null;
			np->n_strlen = 0;
			(void) memcpy(np->n_name, linebuf,
			    (i+1) * sizeof (wchar_t));
			addsymtab(np);
			yylval.node = np;
		} else if (np == varNF || (np == varFS &&
		    (!doing_begin || begin_getline))) {
			/*
			 * If the user program references NF or sets
			 * FS either outside of a begin block or
			 * in a begin block after a getline then the
			 * input line will be split immediately upon read
			 * rather than when a field is first referenced.
			 */
			needsplit = 1;
		} else if (np == varENVIRON)
			needenviron = 1;
	/*FALLTHRU*/
	case PARM:
		return (VAR);

	case UFUNC:
		/*
		 * It is ok to redefine functions as parameters
		 */
		if (funparm) goto do_funparm;
	/*FALLTHRU*/
	case FUNC:
	case GETLINE:
		/*
		 * When a getline is encountered, clear the 'doing_begin' flag.
		 * This will force the 'needsplit' flag to be set, even inside
		 * a begin block, if FS is altered. (See VAR case above)
		 */
		if (doing_begin)
			begin_getline = 1;
		return (np->n_type);
	}
	/* NOTREACHED */
	return (0);
}

/*
 * Read a string for the lexical analyzer.
 * `endc' terminates the string.
 */
static int
lexstring(wint_t endc)
{
	size_t length = lexescape(endc, 0, 0);

	yylval.node = stringnode(linebuf, FALLOC, length);
	return (CONSTANT);
}

/*
 * Read a regular expression.
 */
static int
lexregexp(wint_t endc)
{
	(void) lexescape(endc, 1, 0);
	yylval.node = renode(linebuf);
	return (URE);
}

/*
 * Process a string, converting the escape characters as required by
 * 1003.2. The processed string ends up in the global linebuf[]. This
 * routine also changes the value of 'progfd' - the program file
 * descriptor, so it should be used with some care. It is presently used to
 * process -v (awk1.c) and var=str type arguments (awk2.c, nextrecord()).
 */
void
strescape(wchar_t *str)
{
	progptr = str;
	proglen = wcslen(str) + 1;	/* Include \0 */
	(void) lexescape('\0', 0, 1);
	progptr = NULL;
}

/*
 * Read a string or regular expression, terminated by ``endc'',
 * for lexical analyzer, processing escape sequences.
 * Return string length.
 */
static size_t
lexescape(wint_t endc, int regx, int cmd_line_operand)
{
	static char nlre[256];
	static char nlstr[256];
	static char eofre[256];
	static char eofstr[256];
	int first_time = 1;
	wint_t c;
	wchar_t *cp;
	int n, max;

	if (first_time == 1) {
		(void) strcpy(nlre, gettext("Newline in regular expression\n"));
		(void) strcpy(nlstr, gettext("Newline in string\n"));
		(void) strcpy(eofre, gettext("EOF in regular expression\n"));
		(void) strcpy(eofstr, gettext("EOF in string\n"));
		first_time = 0;
	}

	cp = linebuf;
	while ((c = lexgetc()) != endc) {
		if (c == '\n')
			awkerr(regx ? nlre : nlstr);
		if (c == '\\') {
			switch (c = lexgetc(), c) {
			case '\\':
				if (regx)
					*cp++ = '\\';
				break;

			case '/':
				c = '/';
				break;

			case 'n':
				c = '\n';
				break;

			case 'b':
				c = '\b';
				break;

			case 't':
				c = '\t';
				break;

			case 'r':
				c = '\r';
				break;

			case 'f':
				c = '\f';
				break;

			case 'v':
				c = '\v';
				break;

			case 'a':
				c = (char)0x07;
				break;

			case 'x':
				n = 0;
				while (iswxdigit(c = lexgetc())) {
					if (iswdigit(c))
						c -= '0';
					else if (iswupper(c))
						c -= 'A'-10;
					else
						c -= 'a'-10;
					n = (n<<4) + c;
				}
				lexungetc(c);
				c = n;
				break;

			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
#if 0
/*
 * Posix.2 draft 10 disallows the use of back-referencing - it explicitly
 * requires processing of the octal escapes both in strings and
 * regular expressions. The following code is disabled instead of
 * removed as back-referencing may be reintroduced in a future draft
 * of the standard.
 */
				/*
				 * For regular expressions, we disallow
				 * \ooo to mean octal character, in favour
				 * of back referencing.
				 */
				if (regx) {
					*cp++ = '\\';
					break;
				}
#endif
				max = 3;
				n = 0;
				do {
					n = (n<<3) + c-'0';
					if ((c = lexgetc()) > '7' || c < '0')
						break;
				} while (--max);
				lexungetc(c);
				/*
				 * an octal escape sequence must have at least
				 * 2 digits after the backslash, otherwise
				 * it gets passed straight thru for possible
				 * use in backreferencing.
				 */
				if (max == 3) {
					*cp++ = '\\';
					n += '0';
				}
				c = n;
				break;

			case '\n':
				continue;

			default:
				if (c != endc || cmd_line_operand) {
					*cp++ = '\\';
					if (c == endc)
						lexungetc(c);
				}
			}
		}
		if (c == WEOF)
			awkerr(regx ? eofre : eofstr);
		*cp++ = c;
	}
	*cp = '\0';
	return (cp - linebuf);
}

/*
 * Build a regular expression NODE.
 * Argument is the string holding the expression.
 */
NODE *
renode(wchar_t *s)
{
	NODE *np;
	int n;

	np = emptynode(RE, 0);
	np->n_left = np->n_right = NNULL;
	if ((n = REGWCOMP(&np->n_regexp, s)) != REG_OK) {
		int m;
		char *p;

		m = REGWERROR(n, np->n_regexp, NULL, 0);
		p = (char *)emalloc(m);
		REGWERROR(n, np->n_regexp, p, m);
		awkerr("/%S/: %s", s, p);
	}
	return (np);
}
/*
 * Get a character for the lexical analyser routine.
 */
static wint_t
lexgetc()
{
	wint_t c;
	static char **files = &progfiles[0];

	if (progfp != FNULL && (c = fgetwc(progfp)) != WEOF)
		;
	else {
		if (progptr != NULL) {
			if (proglen-- <= 0)
				c = WEOF;
			else
				c = *progptr++;
		} else {
			if (progfp != FNULL)
				if (progfp != stdin)
					(void) fclose(progfp);
				else
					clearerr(progfp);
				progfp = FNULL;
			if (files < progfilep) {
				filename = *files++;
				lineno = 1;
				if (filename[0] == '-' && filename[1] == '\0')
					progfp = stdin;
				else if ((progfp = fopen(filename, r))
				    == FNULL) {
					(void) fprintf(stderr,
				gettext("script file \"%s\""), filename);
					exit(1);
				}
				c = fgetwc(progfp);
			}
		}
	}
	if (c == '\n')
		++lineno;
	if (conptr >= &context[NCONTEXT])
		conptr = &context[0];
	if (c != WEOF)
		*conptr++ = c;
	return (c);
}

/*
 * Return a character for lexical analyser.
 * Only one returned character is (not enforced) legitimite.
 */
static void
lexungetc(wint_t c)
{
	if (c == '\n')
		--lineno;
	if (c != WEOF) {
		if (conptr == &context[0])
			conptr = &context[NCONTEXT];
		*--conptr = '\0';
	}
	if (progfp != FNULL) {
		(void) ungetwc(c, progfp);
		return;
	}
	if (c == WEOF)
		return;
	*--progptr = c;
	proglen++;
}

/*
 * Syntax errors during parsing.
 */
void
yyerror(char *s, ...)
{
	if (lexlast == FUNC || lexlast == GETLINE || lexlast == KEYWORD)
		if (lexlast == KEYWORD)
			awkerr(gettext("inadmissible use of reserved keyword"));
		else
			awkerr(gettext("attempt to redefine builtin function"));
	awkerr(s);
}

/*
 * Error routine for all awk errors.
 */
/* ARGSUSED */
void
awkerr(char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	awkierr(0, fmt, args);
	va_end(args);
}

/*
 * Error routine like "awkerr" except that it prints out
 * a message that includes an errno-specific indication.
 */
/* ARGSUSED */
void
awkperr(char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	awkierr(1, fmt, args);
	va_end(args);
}

/*
 * Common internal routine for awkerr, awkperr
 */
static void
awkierr(int perr, char *fmt, va_list ap)
{
	static char sep1[] = "\n>>>\t";
	static char sep2[] = "\t<<<";
	int saveerr = errno;

	(void) fprintf(stderr, "%s: ", _cmdname);
	if (running) {
		(void) fprintf(stderr, gettext("line %u ("),
		    curnode == NNULL ? 0 : curnode->n_lineno);
		if (phase == 0)
			(void) fprintf(stderr, "NR=%lld): ",
			    (INT)exprint(varNR));
		else
			(void) fprintf(stderr, "%s): ",
			    phase == BEGIN ? s_BEGIN : s_END);
	} else if (lineno != 0) {
		(void) fprintf(stderr, gettext("file \"%s\": "), filename);
		(void) fprintf(stderr, gettext("line %u: "), lineno);
	}
	(void) vfprintf(stderr, gettext(fmt), ap);
	if (perr == 1)
		(void) fprintf(stderr, ": %s", strerror(saveerr));
	if (perr != 2 && !running) {
		wchar_t *cp;
		int n;
		int c;

		(void) fprintf(stderr, gettext("  Context is:%s"), sep1);
		cp = conptr;
		n = NCONTEXT;
		do {
			if (cp >= &context[NCONTEXT])
				cp = &context[0];
			if ((c = *cp++) != '\0')
				(void) fputs(c == '\n' ? sep1 : toprint(c),
				    stderr);
		} while (--n != 0);
		(void) fputs(sep2, stderr);
	}
	(void) fprintf(stderr, "\n");
	exit(1);
}

wchar_t *
emalloc(unsigned n)
{
	wchar_t *cp;

	if ((cp = malloc(n)) == NULL)
		awkerr(nomem);
	return (cp);
}

wchar_t *
erealloc(wchar_t *p, unsigned n)
{
	wchar_t *cp;

	if ((cp = realloc(p, n)) == NULL)
		awkerr(nomem);
	return (cp);
}


/*
 * usage message for awk
 */
static int
usage()
{
	(void) fprintf(stderr, gettext(
"Usage:	awk [-F ERE] [-v var=val] 'program' [var=val ...] [file ...]\n"
"	awk [-F ERE] -f progfile ... [-v var=val] [var=val ...] [file ...]\n"));
	return (2);
}


static wchar_t *
mbconvert(char *str)
{
	static wchar_t *op = 0;

	if (op != 0)
		free(op);
	return (op = mbstowcsdup(str));
}

char *
mbunconvert(wchar_t *str)
{
	static char *op = 0;

	if (op != 0)
		free(op);
	return (op = wcstombsdup(str));
}

/*
 * Solaris port - following functions are typical MKS functions written
 * to work for Solaris.
 */

wchar_t *
mbstowcsdup(s)
char *s;
{
	int n;
	wchar_t *w;

	n = strlen(s) + 1;
	if ((w = (wchar_t *)malloc(n * sizeof (wchar_t))) == NULL)
		return (NULL);

	if (mbstowcs(w, s, n) == (size_t)-1)
		return (NULL);
	return (w);

}

char *
wcstombsdup(wchar_t *w)
{
	int n;
	char *mb;

	/* Fetch memory for worst case string length */
	n = wslen(w) + 1;
	n *= MB_CUR_MAX;
	if ((mb = (char *)malloc(n)) == NULL) {
		return (NULL);
	}

	/* Convert the string */
	if ((n = wcstombs(mb, w, n)) == -1) {
		int saverr = errno;

		free(mb);
		errno = saverr;
		return (0);
	}

	/* Shrink the string down */
	if ((mb = (char *)realloc(mb, strlen(mb)+1)) == NULL)  {
		return (NULL);
	}
	return (mb);
}

/*
 * The upe_ctrls[] table contains the printable 'control-sequences' for the
 * character values 0..31 and 127.  The first entry is for value 127, thus the
 * entries for the remaining character values are from 1..32.
 */
static const char *const upe_ctrls[] =
{
	"^?",
	"^@",  "^A",  "^B",  "^C",  "^D",  "^E",  "^F",  "^G",
	"^H",  "^I",  "^J",  "^K",  "^L",  "^M",  "^N",  "^O",
	"^P",  "^Q",  "^R",  "^S",  "^T",  "^U",  "^V",  "^W",
	"^X",  "^Y",  "^Z",  "^[",  "^\\", "^]",  "^^",  "^_"
};


/*
 * Return a printable string corresponding to the given character value.  If
 * the character is printable, simply return it as the string.  If it is in
 * the range specified by table 5-101 in the UPE, return the corresponding
 * string.  Otherwise, return an octal escape sequence.
 */
static const char *
toprint(c)
wchar_t c;
{
	int n, len;
	unsigned char *ptr;
	static char mbch[MB_LEN_MAX+1];
	static char buf[5 * MB_LEN_MAX + 1];

	if ((n = wctomb(mbch, c)) == -1) {
		/* Should never happen */
		(void) sprintf(buf, "\\%x", c);
		return (buf);
	}
	mbch[n] = '\0';
	if (iswprint(c)) {
		return (mbch);
	} else if (c == 127) {
		return (upe_ctrls[0]);
	} else if (c < 32) {
		/* Print as in Table 5-101 in the UPE */
		return (upe_ctrls[c+1]);
	} else {
		/* Print as an octal escape sequence */
		for (len = 0, ptr = (unsigned char *) mbch; 0 < n; --n, ++ptr)
			len += sprintf(buf+len, "\\%03o", *ptr);
	}
	return (buf);
}

static int
wcoff(const wchar_t *astring, const int off)
{
	const wchar_t *s = astring;
	int c = 0;
	char mb[MB_LEN_MAX];

	while (c < off) {
		int n;
		if ((n = wctomb(mb, *s)) == 0)
			break;
		if (n == -1)
			n = 1;
		c += n;
		s++;
	}

	return (s - astring);
}

#define	NREGHASH	64
#define	NREGHOLD	1024	/* max number unused entries */

static int	nregunref;

struct reghashq {
	struct qelem hq;
	struct regcache *regcachep;
};

struct regcache {
	struct qelem	lq;
	wchar_t	*pattern;
	regex_t	re;
	int	refcnt;
	struct reghashq	hash;
};

static struct qelem reghash[NREGHASH], reglink;

/*
 * Generate a hash value of the given wchar string.
 * The hashing method is similar to what Java does for strings.
 */
static uint_t
regtxthash(const wchar_t *str)
{
	int k = 0;

	while (*str != L'\0')
		k = (31 * k) + *str++;

	k += ~(k << 9);
	k ^=  (k >> 14);
	k +=  (k << 4);
	k ^=  (k >> 10);

	return (k % NREGHASH);
}

int
int_regwcomp(REGEXP *r, const wchar_t *pattern)
{
	regex_t re;
	char *mbpattern;
	int ret;
	uint_t key;
	struct qelem *qp;
	struct regcache *rcp;

	key = regtxthash(pattern);
	for (qp = reghash[key].q_forw; qp != NULL; qp = qp->q_forw) {
		rcp = ((struct reghashq *)qp)->regcachep;
		if (*rcp->pattern == *pattern &&
		    wcscmp(rcp->pattern, pattern) == 0)
			break;
	}
	if (qp != NULL) {
		/* update link. put this one at the beginning */
		if (rcp != (struct regcache *)reglink.q_forw) {
			remque(&rcp->lq);
			insque(&rcp->lq, &reglink);
		}
		if (rcp->refcnt == 0)
			nregunref--;	/* no longer unref'ed */
		rcp->refcnt++;
		*(struct regcache **)r = rcp;
		return (REG_OK);
	}

	if ((mbpattern = wcstombsdup((wchar_t *)pattern)) == NULL)
		return (REG_ESPACE);

	ret = regcomp(&re, mbpattern, REG_EXTENDED);

	free(mbpattern);

	if (ret != REG_OK)
		return (ret);

	if ((rcp = malloc(sizeof (struct regcache))) == NULL)
		return (REG_ESPACE);
	rcp->re = re;
	if ((rcp->pattern = wsdup(pattern)) == NULL) {
		regfree(&re);
		free(rcp);
		return (REG_ESPACE);
	}
	rcp->refcnt = 1;
	insque(&rcp->lq, &reglink);
	insque(&rcp->hash.hq, &reghash[key]);
	rcp->hash.regcachep = rcp;

	*(struct regcache **)r = rcp;
	return (ret);
}

void
int_regwfree(REGEXP r)
{
	int	cnt;
	struct qelem *qp, *nqp;
	struct regcache *rcp;

	rcp = (struct regcache *)r;

	if (--rcp->refcnt != 0)
		return;

	/* this cache has no reference */
	if (++nregunref < NREGHOLD)
		return;

	/*
	 * We've got too much unref'ed regex. Free half of least
	 * used regex.
	 */
	cnt = 0;
	for (qp = reglink.q_forw; qp != NULL; qp = nqp) {
		nqp = qp->q_forw;
		rcp = (struct regcache *)qp;
		if (rcp->refcnt != 0)
			continue;

		/* free half of them */
		if (++cnt < (NREGHOLD / 2))
			continue;

		/* detach and free */
		remque(&rcp->lq);
		remque(&rcp->hash.hq);

		/* free up */
		free(rcp->pattern);
		regfree(&rcp->re);
		free(rcp);

		nregunref--;
	}
}

size_t
int_regwerror(int errcode, REGEXP r, char *errbuf, size_t bufsiz)
{
	struct regcache *rcp;

	rcp = (struct regcache *)r;
	return (regerror(errcode, &rcp->re, errbuf, bufsiz));
}

int
int_regwexec(REGEXP r,		/* compiled RE */
	const wchar_t *astring,	/* subject string */
	size_t nsub,		/* number of subexpressions */
	int_regwmatch_t *sub,	/* subexpression pointers */
	int flags)
{
	char *mbs;
	regmatch_t *mbsub = NULL;
	int i;
	struct regcache *rcp;

	if ((mbs = wcstombsdup((wchar_t *)astring)) == NULL)
		return (REG_ESPACE);

	if (nsub > 0 && sub) {
		if ((mbsub = malloc(nsub * sizeof (regmatch_t))) == NULL)
			return (REG_ESPACE);
	}

	rcp = (struct regcache *)r;

	i = regexec(&rcp->re, mbs, nsub, mbsub, flags);

	/* Now, adjust the pointers/counts in sub */
	if (i == REG_OK && nsub > 0 && mbsub) {
		int j, k;

		for (j = 0; j < nsub; j++) {
			regmatch_t *ms = &mbsub[j];
			int_regwmatch_t *ws = &sub[j];

			if ((k = ms->rm_so) >= 0) {
				ws->rm_so = wcoff(astring, k);
				ws->rm_sp = astring + ws->rm_so;
			}
			if ((k = ms->rm_eo) >= 0) {
				ws->rm_eo = wcoff(astring, k);
				ws->rm_ep = astring + ws->rm_eo;
			}
		}
	}

	free(mbs);
	if (mbsub)
		free(mbsub);
	return (i);
}

int
int_regwdosuba(REGEXP rp,		/* compiled RE: Pattern */
	const wchar_t *rpl,		/* replacement string: /rpl/ */
	const wchar_t *src,		/* source string */
	wchar_t **dstp,			/* destination string */
	int len,			/* destination length */
	int *globp)	/* IN: occurence, 0 for all; OUT: substitutions */
{
	wchar_t *dst, *odst;
	const wchar_t *ip, *xp;
	wchar_t *op;
	int i;
	wchar_t c;
	int glob, iglob = *globp, oglob = 0;
#define	NSUB	10
	int_regwmatch_t rm[NSUB], *rmp;
	int flags;
	wchar_t *end;
	int regerr;

/* handle overflow of dst. we need "i" more bytes */
#ifdef OVERFLOW
#undef OVERFLOW
#define	OVERFLOW(i) { \
		int pos = op - dst; \
		dst = (wchar_t *)realloc(odst = dst, \
			(len += len + i) * sizeof (wchar_t)); \
		if (dst == NULL) \
			goto nospace; \
		op = dst + pos; \
		end = dst + len; \
	}
#endif

	*dstp = dst = (wchar_t *)malloc(len * sizeof (wchar_t));
	if (dst == NULL)
		return (REG_ESPACE);

	if (rp == NULL || rpl == NULL || src == NULL || dst ==  NULL)
		return (REG_EFATAL);

	glob = 0;	/* match count */
	ip = src;	/* source position */
	op = dst;	/* destination position */
	end = dst + len;

	flags = 0;
	while ((regerr = int_regwexec(rp, ip, NSUB, rm, flags)) == REG_OK) {
		/* Copy text preceding match */
		if (op + (i = rm[0].rm_sp - ip) >= end)
			OVERFLOW(i)
		while (i--)
			*op++ = *ip++;

		if (iglob == 0 || ++glob == iglob) {
			oglob++;
			xp = rpl;		/* do substitute */
		} else
			xp = L"&";		/* preserve text */

		/* Perform replacement of matched substing */
		while ((c = *xp++) != '\0') {
			rmp = NULL;
			if (c == '&')
				rmp = &rm[0];
			else if (c == '\\') {
				if ('0' <= *xp && *xp <= '9')
					rmp = &rm[*xp++ - '0'];
				else if (*xp != '\0')
					c = *xp++;
			}

			if (rmp ==  NULL) {	/* Ordinary character. */
				*op++ = c;
				if (op >= end)
					OVERFLOW(1)
			} else if (rmp->rm_sp != NULL && rmp->rm_ep != NULL) {
				ip = rmp->rm_sp;
				if (op + (i = rmp->rm_ep - rmp->rm_sp) >= end)
					OVERFLOW(i)
				while (i--)
					*op++ = *ip++;
			}
		}

		ip = rm[0].rm_ep;
		if (*ip == '\0')	/* If at end break */
			break;
		else if (rm[0].rm_sp == rm[0].rm_ep) {
			/* If empty match copy next char */
			*op++ = *ip++;
			if (op >= end)
				OVERFLOW(1)
		}
		flags = REG_NOTBOL;
	}

	if (regerr != REG_OK && regerr != REG_NOMATCH)
		return (regerr);

	/* Copy rest of text */
	if (op + (i =  wcslen(ip)) >= end)
		OVERFLOW(i)
	while (i--)
		*op++ = *ip++;
	*op++ = '\0';

	if ((*dstp = dst = (wchar_t *)realloc(odst = dst,
	    sizeof (wchar_t) * (size_t)(op - dst))) == NULL) {
nospace:
		free(odst);
		return (REG_ESPACE);
	}

	*globp = oglob;

	return ((oglob == 0) ? REG_NOMATCH : REG_OK);
}

Added awk2.c.































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

/*
 * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

/*
 * awk -- process input files, field extraction, output
 *
 * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
 */

#include "awk.h"
#include "y.tab.h"

static FILE	*awkinfp;		/* Input file pointer */
static int	reclen;			/* Length of last record */
static int	exstat;			/* Exit status */

static FILE	*openfile(NODE *np, int flag, int fatal);
static FILE	*newfile(void);
static NODE	*nextarg(NODE **npp);
static void	adjust_buf(wchar_t **, int *, wchar_t **, char *, size_t);
static void	awk_putwc(wchar_t, FILE *);

/*
 * mainline for awk execution
 */
void
awk()
{
	running = 1;
	dobegin();
	while (nextrecord(linebuf, awkinfp) > 0)
		execute(yytree);
	doend(exstat);
}

/*
 * "cp" is the buffer to fill.  There is a special case if this buffer is
 * "linebuf" ($0)
 * Return 1 if OK, zero on EOF, -1 on error.
 */
int
nextrecord(wchar_t *cp, FILE *fp)
{
	wchar_t *ep = cp;

nextfile:
	if (fp == FNULL && (fp = newfile()) == FNULL)
		return (0);
	if ((*awkrecord)(ep, NLINE, fp) == NULL) {
		if (fp == awkinfp) {
			if (fp != stdin)
				(void) fclose(awkinfp);
			awkinfp = fp = FNULL;
			goto nextfile;
		}
		if (ferror(fp))
			return (-1);
		return (0);
	}
	if (fp == awkinfp) {
		if (varNR->n_flags & FINT)
			++varNR->n_int;
		else
			(void) exprreduce(incNR);
		if (varFNR->n_flags & FINT)
			++varFNR->n_int;
		else
			(void) exprreduce(incFNR);
	}
	if (cp == linebuf) {
		lbuflen = reclen;
		splitdone = 0;
		if (needsplit)
			fieldsplit();
	}
	/* if record length is too long then bail out */
	if (reclen > NLINE - 2) {
		awkerr(gettext("Record too long (LIMIT: %d bytes)"),
		    NLINE - 1);
		/* Not Reached */
	}
	return (1);
}

/*
 * isclvar()
 *
 * Returns 1 if the input string, arg, is a variable assignment,
 * otherwise returns 0.
 *
 * An argument to awk can be either a pathname of a file, or a variable
 * assignment.  An operand that begins with an undersore or alphabetic
 * character from the portable character set, followed by a sequence of
 * underscores, digits, and alphabetics from the portable character set,
 * followed by the '=' character, shall specify a variable assignment
 * rather than a pathname.
 */
int
isclvar(wchar_t *arg)
{
	wchar_t	*tmpptr = arg;

	if (tmpptr != NULL) {

		/* Begins with an underscore or alphabetic character */
		if (iswalpha(*tmpptr) || *tmpptr == '_') {

			/*
			 * followed by a sequence of underscores, digits,
			 * and alphabetics
			 */
			for (tmpptr++; *tmpptr; tmpptr++) {
				if (!(iswalnum(*tmpptr) || (*tmpptr == '_'))) {
					break;
				}
			}
			return (*tmpptr == '=');
		}
	}

	return (0);
}

/*
 * Return the next file from the command line.
 * Return FNULL when no more files.
 * Sets awkinfp variable to the new current input file.
 */
static FILE *
newfile()
{
	static int argindex = 1;
	static int filedone;
	wchar_t *ap;
	int argc;
	wchar_t *arg;
	extern void strescape(wchar_t *);

	argc = (int)exprint(varARGC);
	for (;;) {
		if (argindex >= argc) {
			if (filedone)
				return (FNULL);
			++filedone;
			awkinfp = stdin;
			arg = M_MB_L("-");
			break;
		}
		constant->n_int = argindex++;
		arg = (wchar_t *)exprstring(ARGVsubi);
		/*
		 * If the argument contains a '=', determine if the
		 * argument needs to be treated as a variable assignment
		 * or as the pathname of a file.
		 */
		if (((ap = wcschr(arg, '=')) != NULL) && isclvar(arg)) {
			*ap = '\0';
			strescape(ap+1);
			strassign(vlook(arg), linebuf, FALLOC|FSENSE,
			    wcslen(linebuf));
			*ap = '=';
			continue;
		}
		if (arg[0] == '\0')
			continue;
		++filedone;
		if (arg[0] == '-' && arg[1] == '\0') {
			awkinfp = stdin;
			break;
		}
		if ((awkinfp = fopen(mbunconvert(arg), r)) == FNULL) {
			(void) fprintf(stderr, gettext("input file \"%s\""),
			    mbunconvert(arg));
			exstat = 1;
			continue;
		}
		break;
	}
	strassign(varFILENAME, arg, FALLOC, wcslen(arg));
	if (varFNR->n_flags & FINT)
		varFNR->n_int = 0;
	else
		(void) exprreduce(clrFNR);
	return (awkinfp);
}

/*
 * Default record reading code
 * Uses fgets for potential speedups found in some (e.g. MKS)
 * stdio packages.
 */
wchar_t *
defrecord(wchar_t *bp, int lim, FILE *fp)
{
	wchar_t *endp;

	if (fgetws(bp, lim, fp) == NULL) {
		*bp = '\0';
		return (NULL);
	}
/*
 * XXXX
 *	switch (fgetws(bp, lim, fp)) {
 *	case M_FGETS_EOF:
 *		*bp = '\0';
 *		return (NULL);
 *	case M_FGETS_BINARY:
 *		awkerr(gettext("file is binary"));
 *	case M_FGETS_LONG:
 *		awkerr(gettext("line too long: limit %d"),
 *			lim);
 *	case M_FGETS_ERROR:
 *		awkperr(gettext("error reading file"));
 *	}
 */

	if (*(endp = (bp + (reclen = wcslen(bp))-1)) == '\n') {
		*endp = '\0';
		reclen--;
	}
	return (bp);
}

/*
 * Read a record separated by one character in the RS.
 * Compatible calling sequence with fgets, but don't include
 * record separator character in string.
 */
wchar_t *
charrecord(wchar_t *abp, int alim, FILE *fp)
{
	wchar_t *bp;
	wint_t c;
	int limit = alim;
	wint_t endc;

	bp = abp;
	endc = *(wchar_t *)varRS->n_string;
	while (--limit > 0 && (c = getwc(fp)) != endc && c != WEOF)
		*bp++ = c;
	*bp = '\0';
	reclen = bp-abp;
	return (c == WEOF && bp == abp ? NULL : abp);
}

/*
 * Special routine for multiple line records.
 */
wchar_t *
multirecord(wchar_t *abp, int limit, FILE *fp)
{
	wchar_t *bp;
	int c;

	while ((c = getwc(fp)) == '\n')
		;
	bp = abp;
	if (c != WEOF) do {
		if (--limit == 0)
			break;
		if (c == '\n' && bp[-1] == '\n')
			break;

		*bp++ = c;
	} while ((c = getwc(fp)) != WEOF);
	*bp = '\0';
	if (bp > abp)
		*--bp = '\0';
	reclen = bp-abp;
	return (c == WEOF && bp == abp ? NULL : abp);
}

/*
 * Look for fields separated by spaces, tabs or newlines.
 * Extract the next field, given pointer to start address.
 * Return pointer to beginning of field or NULL.
 * Reset end of field reference, which is the beginning of the
 * next field.
 */
wchar_t *
whitefield(wchar_t **endp)
{
	wchar_t *sp;
	wchar_t *ep;

	sp = *endp;
	while (*sp == ' ' || *sp == '\t' || *sp == '\n')
		++sp;
	if (*sp == '\0')
		return (NULL);
	for (ep = sp; *ep != ' ' && *ep != '\0' && *ep != '\t' &&
	    *ep != '\n'; ++ep)
		;
	*endp = ep;
	return (sp);
}

/*
 * Look for fields separated by non-whitespace characters.
 * Same calling sequence as whitefield().
 */
wchar_t *
blackfield(wchar_t **endp)
{
	wchar_t *cp;
	int endc;

	endc = *(wchar_t *)varFS->n_string;
	cp = *endp;
	if (*cp == '\0')
		return (NULL);
	if (*cp == endc && fcount != 0)
		cp++;
	if ((*endp = wcschr(cp, endc)) == NULL)
		*endp = wcschr(cp, '\0');
	return (cp);
}

/*
 * This field separation routine uses the same logic as
 * blackfield but uses a regular expression to separate
 * the fields.
 */
wchar_t *
refield(wchar_t **endpp)
{
	wchar_t *cp, *start;
	int flags;
	static	REGWMATCH_T match[10];
	int result;

	cp = *endpp;
	if (*cp == '\0') {
		match[0].rm_ep = NULL;
		return (NULL);
	}
	if (match[0].rm_ep != NULL) {
		flags = REG_NOTBOL;
		cp = (wchar_t *)match[0].rm_ep;
	} else
		flags = 0;
	start = cp;
again:
	switch ((result = REGWEXEC(resep, cp, 10, match, flags))) {
	case REG_OK:
		/*
		 * Check to see if a null string was matched. If this is the
		 * case, then move the current pointer beyond this position.
		 */
		if (match[0].rm_sp == match[0].rm_ep) {
			cp = (wchar_t *)match[0].rm_sp;
			if (*cp++ != '\0') {
				goto again;
			}
		}
		*endpp = (wchar_t *)match[0].rm_sp;
		break;
	case REG_NOMATCH:
		match[0].rm_ep = NULL;
		*endpp = wcschr(cp, '\0');
		break;
	default:
		(void) REGWERROR(result, resep, (char *)linebuf,
		    sizeof (linebuf));
		awkerr(gettext("error splitting record: %s"),
		    (char *)linebuf);
	}
	return (start);
}

/*
 * do begin processing
 */
void
dobegin()
{
	/*
	 * Free all keyword nodes to save space.
	 */
	{
		NODE *np;
		int nbuck;
		NODE *knp;

		np = NNULL;
		nbuck = 0;
		while ((knp = symwalk(&nbuck, &np)) != NNULL)
			if (knp->n_type == KEYWORD)
				delsymtab(knp, 1);
	}
	/*
	 * Copy ENVIRON array only if needed.
	 * Note the convoluted work to assign to an array
	 * and that the temporary nodes will be freed by
	 * freetemps() because we are "running".
	 */
	if (needenviron) {
		char **app;
		wchar_t *name, *value;
		NODE *namep = stringnode(_null, FSTATIC, 0);
		NODE *valuep = stringnode(_null, FSTATIC, 0);
		NODE *ENVsubname = node(INDEX, varENVIRON, namep);
		extern char **environ;

		/* (void) m_setenv(); XXX what's this do? */
		for (app = environ; *app != NULL; /* empty */) {
			name = mbstowcsdup(*app++);

			if ((value = wcschr(name, '=')) != NULL) {
				*value++ = '\0';
				valuep->n_strlen = wcslen(value);
				valuep->n_string = value;
			} else {
				valuep->n_strlen = 0;
				valuep->n_string = _null;
			}
			namep->n_strlen = wcslen(namep->n_string = name);
			(void) assign(ENVsubname, valuep);
			if (value != NULL)
				value[-1] = '=';
		}
	}
	phase = BEGIN;
	execute(yytree);
	phase = 0;
	if (npattern == 0)
		doend(0);
	/*
	 * Delete all pattern/action rules that are BEGIN at this
	 * point to save space.
	 * NOTE: this is not yet implemented.
	 */
}

/*
 * Do end processing.
 * Exit with a status
 */
void
doend(int s)
{
	OFILE *op;

	if (phase != END) {
		phase = END;
		awkinfp = stdin;
		execute(yytree);
	}
	for (op = &ofiles[0]; op < &ofiles[NIOSTREAM]; op++)
		if (op->f_fp != FNULL)
			awkclose(op);
	if (awkinfp == stdin)
		(void) fflush(awkinfp);
	exit(s);
}

/*
 * Print statement.
 */
void
s_print(NODE *np)
{
	FILE *fp;
	NODE *listp;
	char *ofs;
	int notfirst = 0;

	fp = openfile(np->n_right, 1, 1);
	if (np->n_left == NNULL)
		(void) fputs(mbunconvert(linebuf), fp);
	else {
		ofs = wcstombsdup((isstring(varOFS->n_flags)) ?
		    (wchar_t *)varOFS->n_string :
		    (wchar_t *)exprstring(varOFS));
		listp = np->n_left;
		while ((np = getlist(&listp)) != NNULL) {
			if (notfirst++)
				(void) fputs(ofs, fp);
			np = exprreduce(np);
			if (np->n_flags & FINT)
				(void) fprintf(fp, "%lld", (INT)np->n_int);
			else if (isstring(np->n_flags))
				(void) fprintf(fp, "%S", np->n_string);
			else
				(void) fprintf(fp,
				    mbunconvert((wchar_t *)exprstring(varOFMT)),
				    (double)np->n_real);
		}
		free(ofs);
	}
	(void) fputs(mbunconvert(isstring(varORS->n_flags) ?
	    (wchar_t *)varORS->n_string : (wchar_t *)exprstring(varORS)),
	    fp);
	if (ferror(fp))
		awkperr("error on print");
}

/*
 * printf statement.
 */
void
s_prf(NODE *np)
{
	FILE *fp;

	fp = openfile(np->n_right, 1, 1);
	(void) xprintf(np->n_left, fp, (wchar_t **)NULL);
	if (ferror(fp))
		awkperr("error on printf");
}

/*
 * Get next input line.
 * Read into variable on left of node (or $0 if NULL).
 * Read from pipe or file on right of node (or from regular
 * input if NULL).
 * This is an oddball inasmuch as it is a function
 * but parses more like the keywords print, etc.
 */
NODE *
f_getline(NODE *np)
{
	wchar_t *cp;
	INT ret;
	FILE *fp;
	size_t len;

	if (np->n_right == NULL && phase == END) {
		/* Pretend we've reached end of (the non-existant) file. */
		return (intnode(0));
	}

	if ((fp = openfile(np->n_right, 0, 0)) != FNULL) {
		if (np->n_left == NNULL) {
			ret = nextrecord(linebuf, fp);
		} else {
			cp = emalloc(NLINE * sizeof (wchar_t));
			ret = nextrecord(cp, fp);
			np = np->n_left;
			len = wcslen(cp);
			cp = erealloc(cp, (len+1)*sizeof (wchar_t));
			if (isleaf(np->n_flags)) {
				if (np->n_type == PARM)
					np = np->n_next;
				strassign(np, cp, FNOALLOC, len);
			} else
				(void) assign(np, stringnode(cp,
				    FNOALLOC, len));
		}
	} else
		ret = -1;
	return (intnode(ret));
}

/*
 * Open a file.  Flag is non-zero for output.
 */
static FILE *
openfile(NODE *np, int flag, int fatal)
{
	OFILE *op;
	char *cp;
	FILE *fp;
	int type;
	OFILE *fop;

	if (np == NNULL) {
		if (flag)
			return (stdout);
		if (awkinfp == FNULL)
			awkinfp = newfile();
		return (awkinfp);
	}
	if ((type = np->n_type) == APPEND)
		type = WRITE;
	cp = mbunconvert(exprstring(np->n_left));
	fop = (OFILE *)NULL;
	for (op = &ofiles[0]; op < &ofiles[NIOSTREAM]; op++) {
		if (op->f_fp == FNULL) {
			if (fop == (OFILE *)NULL)
				fop = op;
			continue;
		}
		if (op->f_mode == type && strcmp(op->f_name, cp) == 0)
			return (op->f_fp);
	}
	if (fop == (OFILE *)NULL)
		awkerr(gettext("too many open streams to %s onto \"%s\""),
		    flag ? "print/printf" : "getline", cp);
	(void) fflush(stdout);
	op = fop;
	if (cp[0] == '-' && cp[1] == '\0') {
		fp = flag ? stdout : stdin;
	} else {
		switch (np->n_type) {
		case WRITE:
			if ((fp = fopen(cp, w)) != FNULL) {
				if (isatty(fileno(fp)))
					(void) setvbuf(fp, 0, _IONBF, 0);
			}
			break;

		case APPEND:
			fp = fopen(cp, "a");
			break;

		case PIPE:
			fp = popen(cp, w);
			(void) setvbuf(fp, (char *)0, _IOLBF, 0);
			break;

		case PIPESYM:
			fp = popen(cp, r);
			break;

		case LT:
			fp = fopen(cp, r);
			break;

		default:
			awkerr(interr, "openfile");
		}
	}
	if (fp != FNULL) {
		op->f_name = strdup(cp);
		op->f_fp = fp;
		op->f_mode = type;
	} else if (fatal) {
		awkperr(flag ? gettext("output file \"%s\"") :
		    gettext("input file \"%s\""), cp);
	}
	return (fp);
}

/*
 * Close a stream.
 */
void
awkclose(OFILE *op)
{
	if (op->f_mode == PIPE || op->f_mode == PIPESYM)
		(void) pclose(op->f_fp);
	else if (fclose(op->f_fp) == EOF)
		awkperr("error on stream \"%s\"", op->f_name);
	op->f_fp = FNULL;
	free(op->f_name);
	op->f_name = NULL;
}

/*
 * Internal routine common to printf, sprintf.
 * The node is that describing the arguments.
 * Returns the number of characters written to file
 * pointer `fp' or the length of the string return
 * in cp. If cp is NULL then the file pointer is used. If
 * cp points to a string pointer, a pointer to an allocated
 * buffer will be returned in it.
 */
size_t
xprintf(NODE *np, FILE *fp, wchar_t **cp)
{
	wchar_t *fmt;
	int c;
	wchar_t *bptr = (wchar_t *)NULL;
	char fmtbuf[40];
	size_t length = 0;
	char *ofmtp;
	NODE *fnp;
	wchar_t *fmtsave;
	int slen;
	int cplen;

	fnp = getlist(&np);
	if (isleaf(fnp->n_flags) && fnp->n_type == PARM)
		fnp = fnp->n_next;
	if (isstring(fnp->n_flags)) {
		fmt = fnp->n_string;
		fmtsave = NULL;
	} else
		fmtsave = fmt = (wchar_t *)strsave(exprstring(fnp));

	/*
	 * if a char * pointer has been passed in then allocate an initial
	 * buffer for the string. Make it LINE_MAX plus the length of
	 * the format string but do reallocs only based LINE_MAX.
	 */
	if (cp != (wchar_t **)NULL) {
		cplen = LINE_MAX;
		bptr = *cp = emalloc(sizeof (wchar_t) * (cplen + wcslen(fmt)));
	}

	while ((c = *fmt++) != '\0') {
		if (c != '%') {
			if (bptr == (wchar_t *)NULL)
				awk_putwc(c, fp);
			else
				*bptr++ = c;
			++length;
			continue;
		}
		ofmtp = fmtbuf;
		*ofmtp++ = (char)c;
	nextc:
		switch (c = *fmt++) {
		case '%':
			if (bptr == (wchar_t *)NULL)
				awk_putwc(c, fp);
			else
				*bptr++ = c;
			++length;
			continue;

		case 'c':
			*ofmtp++ = 'w';
			*ofmtp++ = 'c';
			*ofmtp = '\0';
			fnp = exprreduce(nextarg(&np));
			if (isnumber(fnp->n_flags))
				c = exprint(fnp);
			else
				c = *(wchar_t *)exprstring(fnp);
			if (bptr == (wchar_t *)NULL)
				length += fprintf(fp, fmtbuf, c);
			else {
				/*
				 * Make sure that the buffer is long
				 * enough to hold the formatted string.
				 */
				adjust_buf(cp, &cplen, &bptr, fmtbuf, 0);
				/*
				 * Since the call to adjust_buf() has already
				 * guaranteed that the buffer will be long
				 * enough, just pass in INT_MAX as
				 * the length.
				 */
				(void) wsprintf(bptr, (const char *) fmtbuf, c);
				bptr += (slen = wcslen(bptr));
				length += slen;
			}
			continue;
/* XXXX Is this bogus? Figure out what s & S mean - look at original code */
		case 's':
		case 'S':
			*ofmtp++ = 'w';
			*ofmtp++ = 's';
			*ofmtp = '\0';
			if (bptr == (wchar_t *)NULL)
				length += fprintf(fp, fmtbuf,
				    (wchar_t *)exprstring(nextarg(&np)));
			else {
				wchar_t *ts = exprstring(nextarg(&np));

				adjust_buf(cp, &cplen, &bptr, fmtbuf,
				    wcslen(ts));
				(void) wsprintf(bptr, (const char *) fmtbuf,
				    ts);
				bptr += (slen = wcslen(bptr));
				length += slen;
			}
			continue;

		case 'o':
		case 'O':
		case 'X':
		case 'x':
		case 'd':
		case 'i':
		case 'D':
		case 'U':
		case 'u':
			*ofmtp++ = 'l';
			*ofmtp++ = 'l'; /* now dealing with long longs */
			*ofmtp++ = c;
			*ofmtp = '\0';
			if (bptr == (wchar_t *)NULL)
				length += fprintf(fp, fmtbuf,
				    exprint(nextarg(&np)));
			else {
				adjust_buf(cp, &cplen, &bptr, fmtbuf, 0);
				(void) wsprintf(bptr, (const char *) fmtbuf,
				    exprint(nextarg(&np)));
				bptr += (slen = wcslen(bptr));
				length += slen;
			}
			continue;

		case 'e':
		case 'E':
		case 'f':
		case 'F':
		case 'g':
		case 'G':
			*ofmtp++ = c;
			*ofmtp = '\0';
			if (bptr == (wchar_t *)NULL)
				length += fprintf(fp, fmtbuf,
				    exprreal(nextarg(&np)));
			else {
				adjust_buf(cp, &cplen, &bptr, fmtbuf, 0);
				(void) wsprintf(bptr, (const char *) fmtbuf,
				    exprreal(nextarg(&np)));
				bptr += (slen = wcslen(bptr));
				length += slen;
			}
			continue;

		case 'l':
		case 'L':
			break;

		case '*':
#ifdef M_BSD_SPRINTF
			sprintf(ofmtp, "%lld", (INT)exprint(nextarg(&np)));
			ofmtp += strlen(ofmtp);
#else
			ofmtp += sprintf(ofmtp, "%lld",
			    (INT)exprint(nextarg(&np)));
#endif
			break;

		default:
			if (c == '\0') {
				*ofmtp = (wchar_t)NULL;
				(void) fprintf(fp, "%s", fmtbuf);
				continue;
			} else {
				*ofmtp++ = (wchar_t)c;
				break;
			}
		}
		goto nextc;
	}
	if (fmtsave != NULL)
		free(fmtsave);
	/*
	 * If printing to a character buffer then make sure it is
	 * null-terminated and only uses as much space as required.
	 */
	if (bptr != (wchar_t *)NULL) {
		*bptr = '\0';
		*cp = erealloc(*cp, (length+1) * sizeof (wchar_t));
	}
	return (length);
}

/*
 * Return the next argument from the list.
 */
static NODE *
nextarg(NODE **npp)
{
	NODE *np;

	if ((np = getlist(npp)) == NNULL)
		awkerr(gettext("insufficient arguments to printf or sprintf"));
	if (isleaf(np->n_flags) && np->n_type == PARM)
		return (np->n_next);
	return (np);
}


/*
 * Check and adjust the length of the buffer that has been passed in
 * to make sure that it has space to accomodate the sequence string
 * described in fmtstr. This routine is used by xprintf() to allow
 * for arbitrarily long sprintf() strings.
 *
 * bp		= start of current buffer
 * len		= length of current buffer
 * offset	= offset in current buffer
 * fmtstr	= format string to check
 * slen		= size of string for %s formats
 */
static void
adjust_buf(wchar_t **bp, int *len, wchar_t **offset, char *fmtstr, size_t slen)
{
	int ioff;
	int width = 0;
	int prec = 0;

	do {
		fmtstr++;
	} while (strchr("-+ 0", *fmtstr) != (char *)0 || *fmtstr == ('#'));
	if (*fmtstr != '*') {
		if (isdigit(*fmtstr)) {
			width = *fmtstr-'0';
			while (isdigit(*++fmtstr))
				width = width * 10 + *fmtstr - '0';
		}
	} else
		fmtstr++;
	if (*fmtstr == '.') {
		if (*++fmtstr != '*') {
			prec = *fmtstr-'0';
			while (isdigit(*++fmtstr))
				prec = prec * 10 + *fmtstr - '0';
		} else
			fmtstr++;
	}
	if (strchr("Llh", *fmtstr) != (char *)0)
		fmtstr++;
	if (*fmtstr == 'S') {
		if (width && slen < width)
			slen = width;
		if (prec && slen > prec)
			slen = prec;
		width = slen+1;
	} else
		if (width == 0)
			width = NUMSIZE;

	if (*offset+ width > *bp+ *len) {
		ioff = *offset-*bp;
		*len += width+1;
		*bp = erealloc(*bp, *len * sizeof (wchar_t));
		*offset = *bp+ioff;
	}
}

static void
awk_putwc(wchar_t c, FILE *fp)
{
	char mb[MB_LEN_MAX];
	size_t mbl;

	if ((mbl = wctomb(mb, c)) > 0) {
		mb[mbl] = '\0';
		(void) fputs(mb, fp);
	} else
		awkerr(gettext("invalid wide character %x"), c);
}

Added awk3.c.





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * awk -- executor
 *
 * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 *
 * Copyright 1985, 1994 by Mortice Kern Systems Inc.  All rights reserved.
 *
 * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "awk.h"
#include "y.tab.h"

static int	dohash(wchar_t *name);
static NODE	*arithmetic(NODE *np);
static NODE	*comparison(NODE *np);
static int	type_of(NODE *np);
static NODE	*lfield(INT fieldno, NODE *value);
static NODE	*rfield(INT fieldno);
static NODE	*userfunc(NODE *np);
static wchar_t	*lltoa(long long l);
static NODE	*exprconcat(NODE *np, int len);
static int	s_if(NODE *np);
static int	s_while(NODE *np);
static int	s_for(NODE *np);
static int	s_forin(NODE *np);
static void	setrefield(NODE *value);
static void	freetemps(void);
static int	action(NODE *np);
static wchar_t	*makeindex(NODE *np, wchar_t *array, int tag);
static int	exprtest(NODE *np);

#define	regmatch(rp, s) REGWEXEC(rp, s, 0, (REGWMATCH_T*)NULL, 0)

/*
 * This code allows for integers to be stored in longs (type INT) and
 * only promoted to double precision floating point numbers (type REAL)
 * when overflow occurs during +, -, or * operations.  This is very
 * non-portable if you desire such a speed optimisation.  You may wish
 * to put something here for your system.  This "something" would likely
 * include either an assembler "jump on overflow" instruction or a
 * method to get traps on overflows from the hardware.
 *
 * This portable method works for ones and twos complement integer
 * representations (which is, realistically) almost all machines.
 */
#if	__TURBOC__
#define	addoverflow()	asm	jo	overflow
#define	suboverflow()	asm	jo	overflow
#else
/*
 * These are portable to two's complement integer machines
 */
#define	addoverflow()	if ((i1^i2) >= 0 && (iresult^i1) < 0) goto overflow
#define	suboverflow()	if ((i1^i2) < 0 && (iresult^i2) >= 0) goto overflow
#endif
#define	muloverflow()	if (((short)i1 != i1 || (short)i2 != i2) &&	\
			    ((i2 != 0 && iresult/i2 != i1) ||		\
			    (i1 == LONG_MIN && i2 == -1)))	  goto overflow

static char	notarray[] = "scalar \"%s\" cannot be used as array";
static char	badarray[] = "array \"%s\" cannot be used as a scalar";
static char	varnotfunc[] = "variable \"%s\" cannot be used as a function";
static char	tmfld[] = "Too many fields (LIMIT: %d)";
static char	toolong[] = "Record too long (LIMIT: %d bytes)";
static char	divzero[] =  "division (/ or %%) by zero";
static char	toodeep[] = "too deeply nested for in loop (LIMIT: %d)";

static wchar_t	numbuf[NUMSIZE];	/* Used to convert INTs to strings */
static wchar_t	*fields[NFIELD];	/* Cache of pointers into fieldbuf */
static wchar_t	*fieldbuf;		/* '\0' separated copy of linebuf */
static NODE	nodes[NSNODE];		/* Cache of quick access nodes */
static NODE	*fnodep = &nodes[0];
#define	NINDEXBUF	50
static wchar_t	indexbuf[NINDEXBUF];	/* Used for simple array indices */
static int	concflag;		/* In CONCAT operation (no frees) */
static NODE	*retval;		/* Last return value of a function */

/*
 * The following stack is used to store the next pointers for all nested
 * for-in loops. This needs to be global so that delete can check to see
 * if it is deleting the next node to be used by a loop.
 */
#define	NFORINLOOP	10
static NODE*	forindex[NFORINLOOP];
static NODE**	next_forin = forindex;

/*
 * Assign a string directly to a NODE without creating an intermediate
 * NODE.  This can handle either FALLOC, FSTATIC, FNOALLOC or FSENSE for
 * "flags" argument.  Also the NODE "np" must be reduced to an lvalue
 * (PARM nodes are not acceptable).
 */
void
strassign(NODE *np, STRING string, int flags, size_t length)
{
	if (np->n_type == FUNC)
		awkerr(gettext("attempt to redefine builtin function"));
	else if (np->n_type == GETLINE || np->n_type == KEYWORD)
		awkerr(gettext("inadmissible use of reserved keyword"));
	if (np->n_flags & FSPECIAL) {
		(void) nassign(np, stringnode(string, flags, length));
		return;
	}
	if (isastring(np->n_flags))
		free((wchar_t *)np->n_string);
	np->n_strlen = length++;
	if (flags & FALLOC) {
		length *= sizeof (wchar_t);
		np->n_string = (STRING) emalloc(length);
		(void) memcpy((void *)np->n_string, string, length);
	} else {
		np->n_string = string;
		if (flags & FNOALLOC) {
			flags &= ~FNOALLOC;
			flags |= FALLOC;
		}
	}
	np->n_flags &= FSAVE;
	if (flags & FSENSE) {
		flags &= ~FSENSE;
		flags |= type_of(np);
	} else
		flags |= FSTRING;
	np->n_flags |= flags;
}

/*
 * Assign to a variable node.
 * LHS must be a VAR type and RHS must be reduced by now.
 * To speed certain operations up, check for
 * certain things here and do special assignments.
 */
NODE *
nassign(NODE *np, NODE *value)
{
	register wchar_t *cp;
	register int len;

	/* short circuit assignment of a node to itself */
	if (np == value)
		return (np);
	if (np->n_flags & FSPECIAL) {
		if (np == varRS || np == varFS) {
			if (isastring(np->n_flags))
				free((void *)np->n_string);
			len = sizeof (wchar_t) * ((np->n_strlen =
				wcslen(cp = exprstring(value)))+1);
			np->n_string = emalloc(len);
			(void) memcpy((wchar_t *)np->n_string, cp, len);
			np->n_flags = FALLOC|FSTRING|FSPECIAL;
			if (np == varRS) {
				if (np->n_string[0] == '\n')
					awkrecord = defrecord;
				else if (np->n_string[0] == '\0')
					awkrecord = multirecord;
				else
					awkrecord = charrecord;
			} else if (np == varFS) {
				if (resep != (REGEXP)NULL) {
					REGWFREE(resep);
					resep = (REGEXP)NULL;
				}
				if (wcslen((wchar_t *)np->n_string) > 1)
					setrefield(np);
				else if (np->n_string[0] == ' ')
					awkfield = whitefield;
				else
					awkfield = blackfield;
			}
			return (np);
		}
	}
	if (isastring(np->n_flags))
		free((wchar_t *)np->n_string);
	if (isstring(value->n_flags)) {
		np->n_strlen = value->n_strlen;
		if (value->n_flags&FALLOC || value->n_string != _null) {
			len = (np->n_strlen+1) * sizeof (wchar_t);
			np->n_string = emalloc(len);
			(void) memcpy(np->n_string, value->n_string, len);
			np->n_flags &= FSAVE;
			np->n_flags |= value->n_flags & ~FSAVE;
			np->n_flags |= FALLOC;
			return (np);
		} else
			np->n_string = value->n_string;
	} else if (value->n_flags & FINT)
		np->n_int = value->n_int;
	else
		np->n_real = value->n_real;
	np->n_flags &= FSAVE;
	np->n_flags |= value->n_flags & ~FSAVE;
	return (np);
}

/*
 * Set regular expression FS value.
 */
static void
setrefield(NODE *np)
{
	static REGEXP re;
	int n;

	if ((n = REGWCOMP(&re, np->n_string)) != REG_OK) {
		REGWERROR(n, &re, (char *)linebuf, sizeof (linebuf));
		awkerr(gettext("syntax error \"%s\" in /%s/\n"),
			(char *)linebuf, np->n_string);
	}
	resep = re;
	awkfield = refield;
}

/*
 * Assign to an l-value node.
 */
NODE *
assign(NODE *left, NODE *right)
{
	if (isleaf(right->n_flags)) {
		if (right->n_type == PARM)
			right = right->n_next;
	} else
		right = exprreduce(right);
top:
	switch (left->n_type) {
	case INDEX:
		left = exprreduce(left);
	/*FALLTHRU*/
	case VAR:
		return (nassign(left, right));

	case PARM:
		/*
		 * If it's a parameter then link to the actual value node and
		 * do the checks again.
		 */
		left = left->n_next;
		goto top;

	case FIELD:
		return (lfield(exprint(left->n_left), right));

	case CALLUFUNC:
	case UFUNC:
		awkerr(gettext("cannot assign to function \"%s\""),
		    left->n_name);

	default:
		awkerr(gettext("lvalue required in assignment"));
	}
	/* NOTREACHED */
	return (0);
}

/*
 * Compiled tree non-terminal node.
 */
NODE *
node(int type, NODE *left, NODE *right)
{
	register NODE *np;

	np = emptynode(type, 0);
	np->n_left = left;
	np->n_right = right;
	np->n_lineno = lineno;
	return (np);
}

/*
 * Create an integer node.
 */
NODE *
intnode(INT i)
{
	register NODE *np;

	np = emptynode(CONSTANT, 0);
	np->n_flags = FINT|FVINT;
	np->n_int = i;
	return (np);
}

/*
 * Create a real number node.
 */
NODE *
realnode(REAL real)
{
	register NODE *np;

	np = emptynode(CONSTANT, 0);
	np->n_flags = FREAL|FVREAL;
	np->n_real = real;
	return (np);
}

/*
 * Make a node for a string.
 */
NODE *
stringnode(STRING s, int how, size_t length)
{
	register NODE *np;

	np = emptynode(CONSTANT, 0);
	np->n_strlen = length;
	if (how & FALLOC) {
		np->n_string = emalloc(length = (length+1) * sizeof (wchar_t));
		(void) memcpy(np->n_string, s, length);
	} else {
		np->n_string = s;
		if (how & FNOALLOC) {
			how &= ~FNOALLOC;
			how |= FALLOC;
		}
	}
	if (how & FSENSE) {
		np->n_flags = type_of(np);
		how &= ~FSENSE;
	} else
		np->n_flags = FSTRING;
	np->n_flags |= how;
	return (np);
}

/*
 * Save a copy of a string.
 */
STRING
strsave(wchar_t *old)
{
	STRING new;
	register size_t len;

	new = (STRING)emalloc(len = (wcslen(old)+1) * sizeof (wchar_t));
	(void) memcpy(new, old, len);
	return (new);
}

/*
 * Allocate an empty node of given type.
 * String space for the node is given by `length'.
 */
NODE *
emptynode(int type, size_t length)
{
	register NODE *np;

	if (length == 0 && running && fnodep < &nodes[NSNODE]) {
		np = fnodep++;
	} else {
		np = (NODE *)emalloc(sizeof (NODE) +
		    (length * sizeof (wchar_t)));
		if (running && type != VAR && type != ARRAY) {
			np->n_next = freelist;
			freelist = np;
		}
	}
	np->n_flags = FNONTOK;
	np->n_type = type;
	np->n_alink = NNULL;

	return (np);
}

/*
 * Free a node.
 */
void
freenode(NODE *np)
{
	if (isastring(np->n_flags))
		free((wchar_t *)np->n_string);
	else if (np->n_type == RE) {
		REGWFREE(np->n_regexp);
	}
	free((wchar_t *)np);
}

/*
 * Install a keyword of given `type'.
 */
void
kinstall(LOCCHARP name, int type)
{
	register NODE *np;
	register size_t l;

	l = wcslen(name);
	np = emptynode(KEYWORD, l);
	np->n_keywtype = type;
	(void) memcpy(np->n_name, name, (l+1) * sizeof (wchar_t));
	addsymtab(np);
}

/*
 * Install built-in function.
 */
NODE *
finstall(LOCCHARP name, FUNCTION func, int type)
{
	register NODE *np;
	register size_t l;

	l = wcslen(name);
	np = emptynode(type, l);
	np->n_function = func;
	(void) memcpy(np->n_name, name, (l+1) * sizeof (wchar_t));
	addsymtab(np);
	return (np);
}

/*
 * Lookup an identifier.
 * nocreate contains the following flag values:
 *	1 if no creation of a new NODE,
 *	0 if ok to create new NODE
 */
NODE *
vlookup(wchar_t *name, int nocreate)
{
	register ushort_t hash;
	register NODE *np;

	np = symtab[hashbuck(hash = dohash((wchar_t *)name))];
	while (np != NNULL) {
		if (np->n_hash == hash && wcscmp(name, np->n_name) == 0)
			return (np);
		np = np->n_next;
	}
	if (nocreate) {
		np = NNULL;
	} else {
		np = emptynode(VAR, hash = wcslen(name));
		np->n_flags = FSTRING|FVINT;
		np->n_strlen = 0;
		np->n_string = _null;
		(void) memcpy(np->n_name, name,
			(hash+1) * sizeof (wchar_t));
		addsymtab(np);
	}
	return (np);
}

/*
 * Add a symbol to the table.
 */
void
addsymtab(NODE *np)
{
	register NODE **spp;

	np->n_hash = dohash((wchar_t *)np->n_name);
	spp = &symtab[hashbuck(np->n_hash)];
	np->n_next = *spp;
	*spp = np;
}

/*
 * Delete the given node from the symbol table.
 * If fflag is non-zero, also free the node space.
 * This routine must also check the stack of forin loop pointers. If
 * we are deleting the next item to be used, then the pointer must be
 * advanced.
 */
void
delsymtab(NODE *np, int fflag)
{
	register NODE *rnp;
	register NODE *prevp;
	register NODE **sptr;
	register ushort_t h;





	h = hashbuck(np->n_hash);
	prevp = NNULL;
	for (rnp = symtab[h]; rnp != NNULL; rnp = rnp->n_next) {
		if (rnp == np) {
			/*
			 * check all of the for-in loop pointers
			 * to see if any need to be advanced because
			 * this element is being deleted.
			 */
			if (next_forin != forindex) {
				sptr = next_forin;
				do {
					if (*--sptr == rnp) {
						*sptr = rnp->n_next;
						break;
					}
				} while (sptr != forindex);
			}
			if (prevp == NNULL)
				symtab[h] = rnp->n_next; else
				prevp->n_next = rnp->n_next;
			if (fflag)
				freenode(rnp);
			break;
		}
		prevp = rnp;
	}
}

/*
 * Hashing function.
 */
static int
dohash(wchar_t *name)
{
	register int hash = 0;

	while (*name != '\0')
		hash += *name++;
	return (hash);
}

/*
 * Top level executor for an awk programme.
 * This will be passed: pattern, action or a list of these.
 * The former function to evaluate a pattern has been
 * subsumed into this function for speed.
 * Patterns are:
 *	BEGIN,
 *	END,
 *	other expressions (including regular expressions)
 */
void
execute(NODE *wp)
{
	register NODE *np;
	register int type;
	register NODE *tnp;

	curnode = wp;
	if (phase != 0) {
		linebuf[0] = '\0';
		lbuflen = 0;
	}
	while (wp != NNULL) {
		if (wp->n_type == COMMA) {
			np = wp->n_left;
			wp = wp->n_right;
		} else {
			np = wp;
			wp = NNULL;
		}
		if (np->n_type != PACT)
			awkerr(interr, "PACT");
		/*
		 * Save the parent node and evaluate the pattern.
		 * If it evaluates to false (0) just continue
		 * to the next pattern/action (PACT) pair.
		 */
		tnp = np;
		np = np->n_left;
		if (np == NNULL) {
			if (phase != 0)
				continue;
		} else if (phase != 0) {
			if (np->n_type != phase)
				continue;
		} else if ((type = np->n_type) == BEGIN || type == END) {
			continue;
		} else if (type == COMMA) {
			/*
			 * The grammar only allows expressions
			 * to be separated by the ',' operator
			 * for range patterns.
			 */
			if (np->n_flags & FMATCH) {
				if (exprint(np->n_right) != 0)
					np->n_flags &= ~FMATCH;
			} else if (exprint(np->n_left) != 0) {
				if (exprint(np->n_right) == 0)
					np->n_flags |= FMATCH;
			} else
				continue;
		} else if (exprint(np) == 0)
			continue;
		np = tnp;
		if (action(np->n_right)) {
			loopexit = 0;
			break;
		}
	}
	if (freelist != NNULL)
		freetemps();
}

/*
 * Free all temporary nodes.
 */
static void
freetemps()
{
	register NODE *np, *nnp;

	if (concflag)
		return;
	for (np = &nodes[0]; np < fnodep; np++) {
		if (isastring(np->n_flags)) {
			free((wchar_t *)np->n_string);
		} else if (np->n_type == RE) {
			REGWFREE(np->n_regexp);
		}
	}
	fnodep = &nodes[0];
	for (np = freelist; np != NNULL; np = nnp) {
		nnp = np->n_next;
		freenode(np);
	}
	freelist = NNULL;
}

/*
 * Do the given action.
 * Actions are statements or expressions.
 */
static int
action(NODE *wp)
{
	register NODE *np;
	register int act = 0;
	register NODE *l;

	while (wp != NNULL) {
		if (wp->n_type == COMMA) {
			np = wp->n_left;
			wp = wp->n_right;
		} else {
			np = wp;
			wp = NNULL;
		}
		if (freelist != NNULL)
			freetemps();
		curnode = np;
		/*
		 * Don't change order of these cases without
		 * changing order in awk.y declarations.
		 * The order is optimised.
		 */
		switch (np->n_type) {
		case ASG:
			(void) assign(np->n_left, np->n_right);
			continue;

		case PRINT:
			s_print(np);
			continue;

		case PRINTF:
			s_prf(np);
			continue;

		case EXIT:
			if (np->n_left != NNULL)
				act = (int)exprint(np->n_left); else
				act = 0;
			doend(act);
			/* NOTREACHED */

		case RETURN:
			if (slevel == 0)
				awkerr(gettext("return outside of a function"));
			np = np->n_left != NNULL
			    ? exprreduce(np->n_left)
			    : const0;
			retval = emptynode(CONSTANT, 0);
			retval->n_flags = FINT;
			(void) nassign(retval, np);
			return (RETURN);

		case NEXT:
			loopexit = NEXT;
		/*FALLTHRU*/
		case BREAK:
		case CONTINUE:
			return (np->n_type);

		case DELETE:
			if ((l = np->n_left)->n_type == PARM) {
				l = l->n_next;
				if (!(l->n_flags & FLARRAY))
					l = l->n_alink;
			}
			switch (l->n_type) {
			case ARRAY:
				delarray(l);
				break;

			case INDEX:
				if ((np = l->n_left)->n_type == PARM) {
					np = np->n_next;
					if (!(np->n_flags & FLARRAY))
						np = np->n_alink;
				}
				/*
				 * get pointer to the node for this array
				 * element using the hash key.
				 */
				l = exprreduce(l);
				/*
				 * now search linearly from the beginning of
				 * the list to find the element before the
				 * one being deleted. This must be done
				 * because arrays are singley-linked.
				 */
				while (np != NNULL) {
					if (np->n_alink == l) {
						np->n_alink = l->n_alink;
						break;
					}
					np = np->n_alink;
				}
				delsymtab(l, 1);
				break;

			case VAR:
				if (isstring(l->n_flags) &&
				    l->n_string == _null)
					break;
			default:
				awkerr(gettext(
				    "may delete only array element or array"));
				break;
			}
			continue;

		case WHILE:
		case DO:
			if ((act = s_while(np)) != 0)
				break;
			continue;

		case FOR:
			if ((act = s_for(np)) != 0)
				break;
			continue;

		case FORIN:
			if ((act = s_forin(np)) != 0)
				break;
			continue;

		case IF:
			if ((act = s_if(np)) != 0)
				break;
			continue;

		default:
			(void) exprreduce(np);
			if (loopexit != 0) {
				act = loopexit;
				break;
			}
			continue;
		}
		return (act);
	}
	return (0);
}

/*
 * Delete an entire array
 */
void
delarray(NODE *np)
{
	register NODE *nnp;

	nnp = np->n_alink;
	np->n_alink = NNULL;
	while (nnp != NNULL) {
		np = nnp->n_alink;
		delsymtab(nnp, 1);
		nnp = np;
	}
}

/*
 * Return the INT value of an expression.
 */
INT
exprint(NODE *np)
{
	if (isleaf(np->n_flags)) {
		if (np->n_type == PARM)
			np = np->n_next;
		goto leaf;
	}
	np = exprreduce(np);
	switch (np->n_type) {
	case CONSTANT:
	case VAR:
	leaf:
		if (np->n_flags & FINT)
			return (np->n_int);
		if (np->n_flags & FREAL)
			return ((INT)np->n_real);
		return ((INT)wcstoll(np->n_string, NULL, 10));

	default:
		awkerr(interr, "exprint");
	}
	/* NOTREACHED */
	return (0);
}

/*
 * Return a real number from an expression tree.
 */
REAL
exprreal(NODE *np)
{
	if (loopexit)
		return ((REAL)loopexit);
	if (isleaf(np->n_flags)) {
		if (np->n_type == PARM)
			np = np->n_next;
		goto leaf;
	}
	np = exprreduce(np);
	switch (np->n_type) {
	case CONSTANT:
	case VAR:
	leaf:
		if (np->n_flags & FREAL)
			return (np->n_real);
		if (np->n_flags & FINT)
			return ((REAL)np->n_int);
		return ((REAL)wcstod((wchar_t *)np->n_string, (wchar_t **)0));

	default:
		awkerr(interr, "exprreal");
	}
	/* NOTREACHED */
	return ((REAL)0);
}

/*
 * Return a string from an expression tree.
 */
STRING
exprstring(NODE *np)
{
	if (isleaf(np->n_flags)) {
		if (np->n_type == PARM)
			np = np->n_next;
		goto leaf;
	}
	np = exprreduce(np);
	switch (np->n_type) {
	case CONSTANT:
	case VAR:
	leaf:
		if (isstring(np->n_flags))
			return (np->n_string);
		if (np->n_flags & FINT)
			return (STRING)lltoa((long long)np->n_int);
		{
			char *tmp;
			(void) wsprintf(numbuf,
		(const char *) (tmp = wcstombsdup(exprstring(varCONVFMT))),
				(double)np->n_real);
			if (tmp != NULL)
				free(tmp);
		}
		return ((STRING)numbuf);

	default:
		awkerr(interr, "exprstring");
	}
	/* NOTREACHED */
	return (0);
}

/*
 * Convert number to string.
 */
static wchar_t *
lltoa(long long l)
{
	register wchar_t *p = &numbuf[NUMSIZE];
	register int s;
	register int neg;
	static wchar_t zero[] = M_MB_L("0");

	if (l == 0)
		return (zero);
	*--p = '\0';
	if (l < 0)
		neg = 1, l = -l; else
		neg = 0;
	if ((s = (short)l) == l) {
		while (s != 0) {
			*--p = s%10 + '0';
			s /= 10;
		}
	} else {
		while (l != 0) {
			*--p = l%10 + '0';
			l /= 10;
		}
	}
	if (neg)
		*--p = '-';
	return (wcscpy(numbuf, p));
}

/*
 * Return pointer to node with concatenation of operands of CONCAT node.
 * In the interest of speed, a left recursive tree of CONCAT nodes
 * is handled with a single malloc.  The accumulated lengths of the
 * right operands are passed down recursive invocations of this
 * routine, which allocates a large enough string when the left
 * operand is not a CONCAT node.
 */
static NODE *
exprconcat(NODE *np, int len)
{
	/* we KNOW (np->n_type==CONCAT) */
	register NODE *lnp = np->n_left;
	register NODE *rnp = np->n_right;
	register STRING	rsp;
	int rlen;
	size_t llen;
	wchar_t *cp;
	wchar_t rnumbuf[NUMSIZE];

	if (isleaf(rnp->n_flags) && rnp->n_type == PARM)
		rnp = rnp->n_next;
	if (isstring(rnp->n_flags)) {
		rsp = rnp->n_string;
		rlen = rnp->n_strlen;
	} else
		rlen = wcslen((wchar_t *)(rsp = exprstring(rnp)));
	if (rsp == numbuf) {	/* static, so save a copy */
		(void) memcpy(rnumbuf, (wchar_t *)rsp,
			(rlen+1) * sizeof (wchar_t));
		rsp = rnumbuf;
	}
	len += rlen;
	if (lnp->n_type == CONCAT) {
		lnp = exprconcat(lnp, len);
		cp = lnp->n_string;
		llen = lnp->n_strlen;
	} else {
		register STRING	lsp;

		if (isleaf(lnp->n_flags) && lnp->n_type == PARM)
			lnp = lnp->n_next;
		if (isstring(lnp->n_flags)) {
			lsp = lnp->n_string;
			llen = lnp->n_strlen;
		} else
			llen = wcslen((wchar_t *)(lsp = exprstring(lnp)));
		cp = emalloc((llen+len+1) * sizeof (wchar_t));
		(void) memcpy(cp, (wchar_t *)lsp, llen * sizeof (wchar_t));
		lnp = stringnode(cp, FNOALLOC, llen);
	}
	(void) memcpy(cp+llen, (wchar_t *)rsp, (rlen+1) * sizeof (wchar_t));
	lnp->n_strlen += rlen;
	return (lnp);
}

/*
 * Reduce an expression to a terminal node.
 */
NODE *
exprreduce(NODE *np)
{
	register wchar_t *cp;
	NODE *tnp;
	register int temp;
	register int t;
	register int  tag;
	register wchar_t *fname;
	register wchar_t *aname;

	/*
	 * a var or constant is a leaf-node (no further reduction required)
	 * so return immediately.
	 */
	if ((t = np->n_type) == VAR || t == CONSTANT)
		return (np);
	/*
	 * If it's a parameter then it is probably a leaf node but it
	 * might be an array so we check.. If it is an array, then signal
	 * an error as an array by itself cannot be used in this context.
	 */
	if (t == PARM)
		if ((np = np->n_next)->n_type == ARRAY)
			awkerr(badarray, np->n_name);
		else
			return (np);
	/*
	 * All the rest are non-leaf nodes.
	 */
	curnode = np;
	switch (t) {
	case CALLUFUNC:
		return (userfunc(np));

	case FIELD:
		return (rfield(exprint(np->n_left)));

	case IN:
	case INDEX:
		tag = 0;
		temp = np->n_type;
		tnp = np->n_left;
		np = np->n_right;
		/* initially formal var name and array key name are the same */
		fname = aname = tnp->n_name;
		if (tnp->n_type == PARM) {
			tnp = tnp->n_next;
			tag = tnp->n_scope;
			if (!(tnp->n_flags & FLARRAY)) {
				tnp = tnp->n_alink;
			}
			aname = tnp->n_name;
		}
		if (tnp->n_type != ARRAY) {
			if (!isstring(tnp->n_flags) || tnp->n_string != _null)
				awkerr(notarray, fname);
			else {
				/* promotion to array */
				promote(tnp);
				if (tnp->n_alink != NNULL) {
					tag = tnp->n_scope;
					if (!(tnp->n_flags & FLARRAY))
						tnp = tnp->n_alink;
					aname = tnp->n_name;
				} else {
					tag = 0;
					if (tnp->n_flags & FLARRAY)
						tag = tnp->n_scope;
				}
			}
		}
		if (tnp == varSYMTAB) {
			if (np == NNULL || np->n_type == COMMA)
				awkerr(gettext(
				    "SYMTAB must have exactly one index"));
			np = vlook(exprstring(np));
			return (np);
		}
		cp = makeindex(np, aname, tag);
		if (temp == INDEX) {
			np = vlook(cp);
			if (!(np->n_flags & FINARRAY)) {
				np->n_alink = tnp->n_alink;
				tnp->n_alink = np;
				np->n_flags |= FINARRAY;
			}
		} else
			np = vlookup(cp, 1) == NNULL ? const0 : const1;
		if (cp != indexbuf)
			free(cp);
		return (np);

	case CONCAT:
		++concflag;
		np = exprconcat(np, 0);
		--concflag;
		return (np);

	case NOT:
		return (intnode(exprtest(np->n_left) == 0 ? (INT)1 : (INT)0));

	case AND:
		return ((exprtest(np->n_left) != 0 &&
		    exprtest(np->n_right) != 0) ? const1 : const0);

	case OR:
		return ((exprtest(np->n_left) != 0 ||
		    exprtest(np->n_right) != 0) ? const1 : const0);

	case EXP:
		{
			double f1, f2;

			/*
			 * evaluate expressions in proper order before
			 * calling pow().
			 * Can't guarantee that compiler will do this
			 * correctly for us if we put them inline.
			 */
			f1 = (double)exprreal(np->n_left);
			f2 = (double)exprreal(np->n_right);
			return (realnode((REAL)pow(f1, f2)));
		}

	case QUEST:
		if (np->n_right->n_type != COLON)
			awkerr(interr, "?:");
		if (exprtest(np->n_left))
			np = np->n_right->n_left; else
			np = np->n_right->n_right;
		return (exprreduce(np));

	case EQ:
	case NE:
	case GE:
	case LE:
	case GT:
	case LT:
		return (comparison(np));

	case ADD:
	case SUB:
	case MUL:
	case DIV:
	case REM:
		return (arithmetic(np));

	case DEC:
		inc_oper->n_type = SUB;
		goto do_inc_op;
	case INC:
		inc_oper->n_type = ADD;
do_inc_op:
		if ((np = np->n_left)->n_type == INDEX)
			np = exprreduce(np);
		if (np->n_flags & FREAL)
			tnp = realnode(np->n_real);
		else
			tnp = intnode(exprint(np));
		inc_oper->n_left = np;
		(void) assign(np, inc_oper);
		return (tnp);

	case PRE_DEC:
		inc_oper->n_type = SUB;
		goto do_pinc_op;
	case PRE_INC:
		inc_oper->n_type = ADD;
do_pinc_op:
		if ((np = np->n_left)->n_type == INDEX)
			np = exprreduce(np);
		inc_oper->n_left = np;
		return (assign(np, inc_oper));

	case AADD:
		asn_oper->n_type = ADD;
		goto do_asn_op;
	case ASUB:
		asn_oper->n_type = SUB;
		goto do_asn_op;
	case AMUL:
		asn_oper->n_type = MUL;
		goto do_asn_op;
	case ADIV:
		asn_oper->n_type = DIV;
		goto do_asn_op;
	case AREM:
		asn_oper->n_type = REM;
		goto do_asn_op;
	case AEXP:
		asn_oper->n_type = EXP;
do_asn_op:
		asn_oper->n_right = np->n_right;
		if ((np = np->n_left)->n_type == INDEX)
			np = exprreduce(np);
		asn_oper->n_left = np;
		return (assign(np, asn_oper));


	case GETLINE:
		return (f_getline(np));

	case CALLFUNC:
		return ((*np->n_left->n_function)(np->n_right));

	case RE:
		if (regmatch(np->n_regexp, linebuf) == REG_OK)
			return (const1);
		return (const0);

	case TILDE:
		cp = exprstring(np->n_left);
		if (regmatch(getregexp(np->n_right), cp) == REG_OK)
			return (const1);
		return (const0);

	case NRE:
		cp = exprstring(np->n_left);
		if (regmatch(getregexp(np->n_right), cp) != REG_OK)
			return (const1);
		return (const0);

	case ASG:
		return (assign(np->n_left, np->n_right));

	case ARRAY:
		awkerr(badarray, np->n_name);

	/*FALLTHRU*/
	case UFUNC:
		awkerr(varnotfunc, np->n_name);

	/*FALLTHRU*/
	default:
		awkerr(gettext("panic: exprreduce(%d)"), t);
		/* NOTREACHED */
	}
	return (0);
}

/*
 * Do arithmetic operators.
 */
static NODE *
arithmetic(NODE *np)
{
	register NODE *left, *right;
	int type;
	register INT i1, i2;
	register INT iresult;
	register REAL r1, r2;

	left = exprreduce(np->n_left);
	if (isreal(left->n_flags) ||
	    (isstring(left->n_flags) && (type_of(left)&FVREAL))) {
		type = FREAL;
		r1 = exprreal(left);
		r2 = exprreal(np->n_right);
	} else {
		i1 = exprint(left);
		right = exprreduce(np->n_right);
		if (isreal(right->n_flags) ||
		    (isstring(right->n_flags) && (type_of(right)&FVREAL))) {

			type = FREAL;
			r1 = i1;
			r2 = exprreal(right);
		} else {
			type = FINT;
			i2 = exprint(right);
		}
	}
reswitch:
	switch (np->n_type) {
	case ADD:
		if (type == FINT) {
			iresult = i1 + i2;
			addoverflow();
		} else
			r1 += r2;
		break;

	/*
	 * Strategically placed between ADD and SUB
	 * so "jo" branches will reach on 80*86
	 */
	overflow:
		r1 = i1;
		r2 = i2;
		type = FREAL;
		goto reswitch;

	case SUB:
		if (type == FINT) {
			iresult = i1 - i2;
			suboverflow();
		} else
			r1 -= r2;
		break;

	case MUL:
		if (type == FINT) {
			iresult = i1 * i2;
			muloverflow();
		} else
			r1 *= r2;
		break;

	case DIV:
		if (type == FINT) {
			r1 = i1;
			r2 = i2;
			type = FREAL;
		}
		if (r2 == 0.0)
			awkerr(divzero);
		r1 /= r2;
		break;

	case REM:
		if (type == FINT) {
			if (i2 == 0)
				awkerr(divzero);
			iresult = i1 % i2;
		} else {
			double fmod(double x, double y);

			errno = 0;
			r1 = fmod(r1, r2);
			if (errno == EDOM)
				awkerr(divzero);
		}
		break;
	}
	return (type == FINT ? intnode(iresult) : realnode(r1));
}

/*
 * Do comparison operators.
 */
static NODE *
comparison(NODE *np)
{
	register NODE *left, *right;
	register int cmp;
	int tl, tr;
	register REAL r1, r2;
	register INT i1, i2;

	left = np->n_left;
	if (isleaf(left->n_flags)) {
		if (left->n_type == PARM)
			left = left->n_next;
	} else
		left = exprreduce(left);
	tl = left->n_flags;
	right = np->n_right;
	if (isleaf(right->n_flags)) {
		if (right->n_type == PARM)
			right = right->n_next;
	} else {
		++concflag;
		right = exprreduce(right);
		--concflag;
	}
	tr = right->n_flags;
	/*
	 * Posix mandates semantics for the comparison operators that
	 * are incompatible with traditional AWK behaviour. If the following
	 * define is true then awk will use the traditional behaviour.
	 * if it's false, then AWK will use the POSIX-mandated behaviour.
	 */
#define	TRADITIONAL 0
#if TRADITIONAL
	if (!isnumber(tl) || !isnumber(tr)) {
		cmp = wcscoll((wchar_t *)exprstring(left),
		    (wchar_t *)exprstring(right));
	} else if (isreal(tl) || isreal(tr)) {
		r1 = exprreal(left);
		r2 = exprreal(right);
		if (r1 < r2)
			cmp = -1;
		else if (r1 > r2)
			cmp = 1;
		else
			cmp = 0;
	} else {
		i1 = exprint(left);
		i2 = exprint(right);
		if (i1 < i2)
			cmp = -1;
		else if (i1 > i2)
			cmp = 1;
		else
			cmp = 0;
	}
#else
	if (!isnumber(tl) && !isnumber(tr)) {
do_strcmp:
		cmp = wcscoll((wchar_t *)exprstring(left),
		    (wchar_t *)exprstring(right));
	} else {
		if (isstring(tl))
			tl = type_of(left);
		if (isstring(tr))
			tr = type_of(right);
		if (!isnumber(tl) || !isnumber(tr))
			goto do_strcmp;
		if (isreal(tl) || isreal(tr)) {
			r1 = exprreal(left);
			r2 = exprreal(right);
			if (r1 < r2)
				cmp = -1;
			else if (r1 > r2)
				cmp = 1;
			else
				cmp = 0;
		} else {
			i1 = exprint(left);
			i2 = exprint(right);
			if (i1 < i2)
				cmp = -1;
			else if (i1 > i2)
				cmp = 1;
			else
				cmp = 0;
		}
	}
#endif
	switch (np->n_type) {
	case EQ:
		return (cmp == 0 ? const1 : const0);

	case  NE:
		return (cmp != 0 ? const1 : const0);

	case GE:
		return (cmp >= 0 ? const1 : const0);

	case LE:
		return (cmp <= 0 ? const1 : const0);

	case GT:
		return (cmp > 0 ? const1 : const0);

	case LT:
		return (cmp < 0 ? const1 : const0);

	default:
		awkerr(interr, "comparison");
	}
	/* NOTREACHED */
	return (0);
}

/*
 * Return the type of a constant that is a string.
 * The node must be a FSTRING type and the return value
 * will possibly have FVINT or FVREAL or'ed in.
 */
static int
type_of(NODE *np)
{
	wchar_t *cp;
	int somedigits = 0;
	int seene = 0;
	int seenradix = 0;
	int seensign = 0;
	int digitsaftere = 0;

	cp = (wchar_t *)np->n_string;
	if (*cp == '\0')
		return (FSTRING|FVINT);
	while (iswspace(*cp))
		cp++;
	if (*cp == '-' || *cp == '+')
		cp++;
	while (*cp != '\0') {
		switch (*cp) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			if (seene)
				digitsaftere = 1;
			somedigits++;
			break;

		case 'E':
		case 'e':
			if (seene || !somedigits)
				return (FSTRING);
			seene = 1;
			break;

		case '+':
		case '-':
			if (seensign || !seene || digitsaftere)
				return (FSTRING);
			seensign = 1;
			break;

		default:
			if (*cp == radixpoint) {
				if (seenradix || seene || (!somedigits &&
				    !iswdigit(*++cp)))
					return (FSTRING);
			} else
				return (FSTRING);
			seenradix = 1;
		}
		cp++;
	}
	if (somedigits == 0)
		return (FSTRING);
	if (somedigits >= MAXDIGINT || seenradix || seene) {
		if (seensign && !digitsaftere)
			return (FSTRING);
		else
			return (FSTRING|FVREAL);
	} else
		return (FSTRING|FVINT);
}

/*
 * Return a field rvalue.
 */
static NODE *
rfield(INT fieldno)
{
	register wchar_t *cp;

	if (fieldno == 0)
		return (stringnode(linebuf, FSTATIC|FSENSE, lbuflen));
	if (!splitdone)
		fieldsplit();
	if (fieldno > nfield || fieldno < 0)
		return (stringnode(_null, FSTATIC, 0));
	cp = fields[fieldno-1];
	return (stringnode(cp, FSTATIC|FSENSE, wcslen(cp)));
}

/*
 * Split linebuf into fields.  Done only once
 * per input record (maximum).
 */
void
fieldsplit()
{
	register wchar_t *ip, *op;
	register int n;
	wchar_t *ep;

	if (fieldbuf == NULL)
		fieldbuf = emalloc(NLINE * sizeof (wchar_t));
	fcount = 0;
	ep = linebuf;
	op = fieldbuf;
	while ((ip = (*awkfield)(&ep)) != NULL) {
		fields[fcount++] = op;
		if (fcount > NFIELD)
			awkerr(tmfld, NFIELD);
		n = ep-ip;
		(void) memcpy(op, ip, n * sizeof (wchar_t));
		op += n;
		*op++ = '\0';
	}
	if (varNF->n_flags & FINT)
		varNF->n_int = fcount;
	else {
		constant->n_int = fcount;
		(void) nassign(varNF, constant);
	}
	nfield = fcount;
	splitdone++;
}

/*
 * Assign to a field as an lvalue.
 * Return the unevaluated node as one doesn't always need it
 * evaluated in an assignment.
 */
static NODE *
lfield(INT fieldno, NODE *np)
{
	register wchar_t *cp;
	register wchar_t *op;
	register wchar_t *sep;
	register int i;
	register wchar_t *newval;
	register int seplen;
	register int newlen;

	newlen = wcslen(newval = (wchar_t *)exprstring(np));
	if (fieldno == 0) {
		splitdone = 0;
		(void) memcpy(linebuf, newval, (newlen+1) * sizeof (wchar_t));
		lbuflen = newlen;
		fieldsplit();
	} else {
		seplen = wcslen(sep = (wchar_t *)exprstring(varOFS));
		if (!splitdone)
			fieldsplit();
		if (--fieldno < nfield &&
		    (newlen <= wcslen(fields[fieldno]))) {
			(void) memcpy(fields[fieldno], newval,
				(newlen+1) * sizeof (wchar_t));
		} else {
			register wchar_t *buf;

			buf = fieldbuf;
			fieldbuf = emalloc(NLINE * sizeof (wchar_t));
			if (fieldno >= nfield) {
				if (fieldno >= NFIELD)
					awkerr(tmfld, NFIELD);
				while (nfield < fieldno)
					fields[nfield++] = _null;
				++nfield;
			}
			fields[fieldno] = newval;
			op = fieldbuf;
			for (i = 0; i < nfield; i++) {
				newlen = wcslen(cp = fields[i])+1;
				fields[i] = op;
				if (op+newlen >= fieldbuf+NLINE)
					awkerr(toolong, NLINE);
				(void) memcpy(op, cp,
				    newlen * sizeof (wchar_t));
				op += newlen;
			}
			free(buf);
		}
		/*
		 * Reconstruct $0
		 */
		op = linebuf;
		i = 0;
		while (i < nfield) {
			newlen = wcslen(cp = fields[i++]);
			(void) memcpy(op, cp, newlen * sizeof (wchar_t));
			op += newlen;
			if (i < nfield) {
				(void) memcpy(op, sep,
					seplen * sizeof (wchar_t));
				op += seplen;
			}
			if (op >= &linebuf[NLINE])
				awkerr(toolong, NLINE);
		}
		*op = '\0';
		lbuflen = op-linebuf;
		if (varNF->n_flags & FINT)
			varNF->n_int = nfield;
		else {
			constant->n_int = nfield;
			(void) nassign(varNF, constant);
		}
	}
	return (np);
}

/*
 * Do a user function.
 * Each formal parameter must:
 *	have the actual parameter assigned to it (call by value),
 *	have a pointer to an array put into it (call by reference),
 *	and be made undefined (extra formal parameters)
 */
static NODE *
userfunc(NODE *np)
{
	register NODE *temp;
	NODE *fnp;

	if ((fnp = np->n_left) == NNULL)
		awkerr(gettext("impossible function call"));
	if (fnp->n_type != UFUNC)
		awkerr(varnotfunc, fnp->n_name);

#ifndef M_STKCHK
	if (slevel >= NRECUR)
		awkerr(gettext("function \"%S\" nesting level > %u"),
		    fnp->n_name, NRECUR);
#else
	if (!M_STKCHK)
		awkerr(gettext("function \"%s\" nesting level too deep"),
		    fnp->n_name);
#endif

	fnp = fnp->n_ufunc;
	{
		register NODE *formal;
		register NODE *actual;
		NODE *formlist, *actlist, *templist, *temptail;

		templist = temptail = NNULL;
		actlist = np->n_right;
		formlist = fnp->n_left;
		/*
		 * pass through formal list, setting up a list
		 * (on templist) containing temps for the values
		 * of the actuals.
		 * If the actual list runs out before the formal
		 * list, assign 'constundef' as the value
		 */
		while ((formal = getlist(&formlist)) != NNULL) {
			register NODE *array;
			register int t;
			register size_t len;
			register int scope_tag;

			actual = getlist(&actlist);
			if (actual == NNULL) {
				actual = constundef;
				scope_tag = slevel+1;
			} else
				scope_tag = 0;
			array = actual;
			switch (actual->n_type) {
			case ARRAY:
				t = ARRAY;
				scope_tag = 0;
				break;

			case PARM:
				array = actual = actual->n_next;
				t = actual->n_type;
				scope_tag = actual->n_scope;
				if (!(actual->n_flags & FLARRAY))
					array = actual->n_alink;
				break;

			default:
				t = VAR;
				break;
			}
			temp = emptynode(t, len = wcslen(formal->n_name));
			(void) memcpy(temp->n_name, formal->n_name,
			    (len+1) * sizeof (wchar_t));
			temp->n_flags = FSTRING|FVINT;
			temp->n_string = _null;
			temp->n_strlen = 0;
			if (t == VAR)
				(void) assign(temp, actual);
			if (t != ARRAY)
				temp->n_flags |= FLARRAY;
			temp->n_scope = scope_tag;
			/*
			 * link to actual parameter in case of promotion to
			 * array
			 */
			if (actual != constundef)
				temp->n_alink = actual;
			/*
			 * Build the templist
			 */
			if (templist != NNULL) {
				temptail->n_next = temp;
				temptail = temp;
			} else
				templist = temptail = temp;
			temp->n_next = NNULL;
			if (actual->n_type == CONSTANT)
				temp->n_alink = temp;
			else
				temp->n_alink = array;
		}
		/*
		 * Bind results of the evaluation of actuals to formals.
		 */
		formlist = fnp->n_left;
		while (templist != NNULL) {
			temp = templist;
			templist = temp->n_next;
			formal = getlist(&formlist);
			temp->n_next = formal->n_next;
			formal->n_next = temp;








		}
	}
	{
		register NODE *savenode = curnode;

		++slevel;
		if (action(fnp->n_right) == RETURN)
			np = retval; else
			np = const0;
		curnode = savenode;
	}
	{
		register NODE *formal;
		NODE *formlist;

		formlist = fnp->n_left;
		while ((formal = getlist(&formlist)) != NNULL) {
			temp = formal->n_next;
			formal->n_next = temp->n_next;
			/* if node is a local array, free the elements */
			if (temp->n_type == ARRAY && (temp->n_scope == slevel))
				delarray(temp);
			freenode(temp);
		}
	}
	--slevel;
	return (np);
}

/*
 * Get the regular expression from an expression tree.
 */
REGEXP
getregexp(NODE *np)
{
	if (np->n_type == RE)
		return (np->n_regexp);
	np = renode((wchar_t *)exprstring(np));
	return (np->n_regexp);
}

/*
 * Get the next element from a list.
 */
NODE *
getlist(NODE **npp)
{
	register NODE *np;

	if ((np = *npp) == NNULL)
		return (np);
	if (np->n_type == COMMA) {
		*npp = np->n_right;
		return (np->n_left);
	} else {
		*npp = NNULL;
		return (np);
	}
}

/*
 * if statement.
 */
static int
s_if(NODE *np)
{
	register NODE *xp;
	register int test;

	test = exprtest(np->n_left);
	xp = np->n_right;
	if (xp->n_type != ELSE)
		awkerr(interr, "if/else");
	if (test)
		xp = xp->n_left;
	else
		xp = xp->n_right;
	return (action(xp));
}

/*
 * while and do{}while statements.
 */
static int
s_while(NODE *np)
{
	register int act = 0;

	if (np->n_type == DO)
		goto dowhile;
	for (;;) {
		if (exprtest(np->n_left) == 0)
			break;
	dowhile:
		if ((act = action(np->n_right)) != 0) {
			switch (act) {
			case BREAK:
				act = 0;
				break;

			case CONTINUE:
				act = 0;
				continue;
			}
			break;
		}
	}
	return (act);
}

/*
 * for statement.
 */
static int
s_for(NODE *np)
{
	register NODE *testnp, *incnp, *initnp;
	register int act = 0;
	NODE *listp;

	listp = np->n_left;
	initnp = getlist(&listp);
	testnp = getlist(&listp);
	incnp = getlist(&listp);
	if (initnp != NNULL)
		(void) exprreduce(initnp);
	for (;;) {
		if (exprtest(testnp) == 0)
			break;
		if ((act = action(np->n_right)) != 0) {
			switch (act) {
			case BREAK:
				act = 0;
				break;

			case CONTINUE:
				act = 0;
				goto clabel;
			}
			break;
		}
	clabel:
		if (incnp != NNULL)
			(void) exprreduce(incnp);
	}
	return (act);
}

/*
 * for variable in array statement.
 */
static int
s_forin(NODE *np)
{
	register NODE *left;
	register int act = 0;
	register NODE *var;
	register NODE **nnp;
	register NODE *statement;
	register int issymtab = 0;
	wchar_t *index;
	register int alen;
	int nbuck;

	left = np->n_left;
	statement = np->n_right;
	if (left->n_type != IN)
		awkerr(interr, "for (var in array)");
	if ((var = left->n_left)->n_type == PARM)
		var = var->n_next;
	np = left->n_right;
	if (np->n_type == PARM) {
		np = np->n_next;
		if (!(np->n_flags & FLARRAY))
			np = np->n_alink;
	}
	if (np == varSYMTAB) {
		issymtab++;
		np = NNULL;
		nbuck = 0;
	} else {
		/*
		 * At this point if the node is not actually an array
		 * check to see if it has already been established as
		 * a scalar. If it is a scalar then flag an error. If
		 * not then promote the object to an array type.
		 */
		if (np->n_type != ARRAY) {
			if (!isstring(np->n_flags) || np->n_string != _null)
				awkerr(notarray, np->n_name);
			else {
				/* promotion to array */
				promote(np);
				if (np->n_alink != NNULL)
					if (!(np->n_flags & FLARRAY))
						np = np->n_alink;
			}
		}
		/*
		 * Set up a pointer to the first node in the array list.
		 * Save this pointer on the delete stack. This information
		 * is used by the delete function to advance any pointers
		 * that might be pointing at a node which has been deleted.
		 * See the delsymtab() function for more information. Note
		 * that if the a_link field is nil, then just return 0 since
		 * this array has no elements yet.
		 */
		if ((*(nnp = next_forin) = np->n_alink) == 0)
			return (0);
		if (++next_forin > &forindex[NFORINLOOP])
			awkerr(toodeep, NFORINLOOP);
		/*
		 * array elements have names of the form
		 *	<name>]<index> (global arrays)
		 * or
		 *	<name>[<scope>]<index> (local arrays)
		 * We need to know the offset of the index portion of the
		 * name string in order to place it in the index variable so
		 * we look for the ']'. This is calculated here and then
		 * used below.
		 */
		for (alen = 0; (*nnp)->n_name[alen++] != ']'; )
			if ((*nnp)->n_name[alen] == '\0')
				awkerr(interr, "for: invalid array");
	}
	for (;;) {
		if (issymtab) {
			if ((left = symwalk(&nbuck, &np)) == NNULL)
				break;
			index = left->n_name;
		} else {
			if ((np = *nnp) == NNULL)
				break;
			index = np->n_name+alen;
			*nnp = np->n_alink;
		}
		strassign(var, index, FSTATIC, wcslen(index));
		if ((act = action(statement)) != 0) {
			switch (act) {
			case BREAK:
				act = 0;
				break;

			case CONTINUE:
				act = 0;
				continue;
			}
			break;
		}
	}
	next_forin--;
	return (act);
}

/*
 * Walk the symbol table using the same algorithm as arraynode.
 */
NODE *
symwalk(int *buckp, NODE **npp)
{
	register NODE *np;

	np = *npp;
	for (;;) {
		while (np == NNULL) {
			if (*buckp >= NBUCKET)
				return (*npp = NNULL);
			np = symtab[(*buckp)++];
		}
		if (np->n_type == VAR &&
		    (!isstring(np->n_flags) || np->n_string != _null)) {
			*npp = np->n_next;
			return (np);
		}
		np = np->n_next;
	}
	/* NOTREACHED */
}

/*
 * Test the result of an expression.
 */
static int
exprtest(NODE *np)
{
	register int t;

	if (np == NNULL)
		return (1);
	if (freelist != NNULL)
		freetemps();
	np = exprreduce(np);
	if (isint(t = np->n_flags)) {
		if (isstring(t))
			return (exprint(np) != 0);
		return (np->n_int != 0);
	}
	if (isreal(t)) {
		REAL rval;

		rval = isstring(t) ? exprreal(np) : np->n_real;
		return (rval != 0.0);
	}
	return (*(wchar_t *)exprstring(np) != '\0');
}

/*
 * Return malloc'ed space that holds the given name "[" scope "]" index ...
 * concatenated string.
 * The node (np) is the list of indices and 'array' is the array name.
 */
static wchar_t *
makeindex(NODE *np, wchar_t *array, int tag)
{
	static wchar_t tags[sizeof (int)];
	static wchar_t tag_chars[] = M_MB_L("0123456789ABCDEF");
	register wchar_t *cp;
	register NODE *index;
	register uint_t n;
	register int len;
	register wchar_t *indstr;
	register wchar_t *sep;
	register int seplen;
	register int taglen;


	/*
	 * calculate and create the tag string
	 */
	for (taglen = 0; tag; tag >>= 4)
		tags[taglen++] = tag_chars[tag & 0xf];
	/*
	 * Special (normal) case: only one index.
	 */
	if (np->n_type != COMMA) {
		wchar_t *ocp;
		size_t i;

		if (isleaf(np->n_flags) && np->n_type == PARM)
			np = np->n_next;
		if (isstring(np->n_flags)) {
			indstr = np->n_string;
			len = np->n_strlen;
		} else {
			indstr = exprstring(np);
			len = wcslen(indstr);
		}
		i = (n = wcslen(array)) + len + 3 + taglen;
		if (i < NINDEXBUF)
			ocp = indexbuf;
		else
			ocp = emalloc(i * sizeof (wchar_t));
		(void) memcpy(ocp, array, n * sizeof (wchar_t));
		cp = ocp+n;
		if (taglen) {
			*cp++ = '[';
			while (taglen)
				*cp++ = tags[--taglen];
		}
		*cp++ = ']';
		(void) memcpy(cp, indstr, (len+1) * sizeof (wchar_t));

		return (ocp);
	}
	n = 0;
	seplen = wcslen(sep = (wchar_t *)exprstring(varSUBSEP));
	while ((index = getlist(&np)) != NNULL) {
		indstr = exprstring(index);
		len = wcslen(indstr);
		if (n == 0) {
			cp = emalloc(sizeof (wchar_t) * ((n = wcslen(array)) +
				len + 3 + taglen));
			(void) memcpy(cp, array, n * sizeof (wchar_t));
			if (taglen) {
				cp[n++] = '[';
				while (taglen)
					cp[n++] = tags[--taglen];
			}
			cp[n++] = ']';
		} else {
			cp = erealloc(cp, (n+len+seplen+1) * sizeof (wchar_t));
			(void) memcpy(cp+n, sep, seplen * sizeof (wchar_t));
			n += seplen;
		}
		(void) memcpy(cp+n, indstr, (len+1) * sizeof (wchar_t));
		n += len;
	}
	return (cp);
}


/*
 * Promote a node to an array. In the simplest case, just set the
 * node type field to ARRAY. The more complicated case involves walking
 * a list of variables that haven't been determined yet as scalar or array.
 * This routine plays with the pointers to avoid recursion.
 */
void
promote(NODE *n)
{
	register NODE *prev = NNULL;
	register NODE *next;

	/*
	 * walk down the variable chain, reversing the pointers and
	 * setting each node to type array.
	 */
	while ((n->n_flags & FLARRAY) && (n->n_alink != n)) {
		n->n_type = ARRAY;
		next = n->n_alink;
		n->n_alink = prev;
		prev = n;
		n = next;
	}

	/*
	 * If the final entity on the chain is a local variable, then
	 * reset it's alink field to NNULL - normally it points back
	 * to itself - this is used in other parts of the code to
	 * reduce the number of conditionals when handling locals.
	 */
	n->n_type = ARRAY;
	if (n->n_flags & FLARRAY)
		n->n_alink = NNULL;

	/*
	 * Now walk back up the list setting the alink to point to
	 * the last entry in the chain and clear the 'local array'
	 * flag.
	 */
	while (prev != NNULL) {
		prev->n_flags &= ~FLARRAY;
		next = prev->n_alink;
		prev->n_alink = n;
		prev = next;
	}
}

Added awk4.c.



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * awk -- functions
 *
 * Copyright (c) 1995, 1996 by Sun Microsystems, Inc.
 * All rights reserved.
 *
 * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
 *
 * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include "awk.h"
#include "y.tab.h"
#include <time.h>
#include <sys/wait.h>

static uint	nargs(NODE *np);
static NODE	*dosub(NODE *np, int glob);
static NODE	*docasetr(NODE *np, int upper);
static int	asortcmp(const void *npp1, const void *npp2);

static char	nargerr[] = "wrong number of arguments to function \"%s\"";
static NODE	*asortfunc;		/* Function call for asort() */
static NODE	*asnp1, *asnp2;		/* index1, index2 nodes */
static int	asarraylen;		/* strlen(array)+1 for asort */

/*
 * Return the value of exp(x).
 * Usage:	y = exp(x)
 *		y = exp()
 */
NODE *
f_exp(NODE *np)
{
	register uint na;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, s_exp);
	return (realnode(exp(exprreal(na==0 ? field0 : getlist(&np)))));
}

/*
 * Return the integer part of the argument.
 * Usage:	i = int(r)
 *		i = int()
 */
NODE *
f_int(NODE *np)
{
	register uint na;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, s_int);
	return (intnode(exprint(na==0 ? field0 : getlist(&np))));
}

/*
 * Logarithm function.
 * Usage:	y = log(x)
 *		y = log()
 */
NODE *
f_log(NODE *np)
{
	register uint na;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, s_log);
	return (realnode(log(exprreal(na==0 ? field0 : getlist(&np)))));
}

/*
 * Square root function.
 * Usage:	y = sqrt(x)
 *		y = sqrt()
 */
NODE *
f_sqrt(NODE *np)
{
	register uint na;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, s_sqrt);
	return (realnode(sqrt(exprreal(na==0 ? field0 : getlist(&np)))));
}

/*
 * Trigonometric sine function.
 * Usage:	y = sin(x)
 */
NODE *
f_sin(NODE *np)
{
	if (nargs(np) != 1)
		awkerr(nargerr, s_sin);
	return (realnode(sin(exprreal(getlist(&np)))));
}

/*
 * Trigonometric cosine function.
 * Usage:	y = cos(x)
 */
NODE *
f_cos(NODE *np)
{
	if (nargs(np) != 1)
		awkerr(nargerr, s_cos);
	return (realnode(cos(exprreal(getlist(&np)))));
}

/*
 * Arctangent of y/x.
 * Usage:	z = atan2(y, x)
 */
NODE *
f_atan2(NODE *np)
{
	double y, x;

	if (nargs(np) != 2)
		awkerr(nargerr, s_atan2);
	y = (double)exprreal(getlist(&np));
	x = (double)exprreal(getlist(&np));
	return (realnode(atan2(y, x)));
}

/*
 * Set the seed for the random number generator function -- rand.
 * Usage:	srand(x)
 *		srand()
 */
NODE *
f_srand(NODE *np)
{
	register uint na;
	register uint seed;
	static uint oldseed = 0;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, s_srand);
	if (na == 0)
		seed = (uint)time((time_t *)0); else
		seed = (uint)exprint(getlist(&np));
	srand(seed);
	na = oldseed;
	oldseed = seed;
	return (intnode((INT)na));
}

/*
 * Generate a random number.
 * Usage:	x = rand()
 */
NODE *
f_rand(NODE *np)
{
	double result;
	int expon;
	ushort rint;

	if (nargs(np) != 0)
		awkerr(nargerr, s_rand);
	rint = rand() & SHRT_MAX;
	result = frexp((double)rint, &expon);
	return (realnode((REAL)ldexp(result, expon-15)));
}

/*
 * Substitute function.
 * Usage:	n = sub(regex, replace, target)
 *		n = sub(regex, replace)
 */
NODE *
f_sub(NODE *np)
{
	return (dosub(np, 1));
}

/*
 * Global substitution function.
 * Usage:	n = gsub(regex, replace, target)
 *		n = gsub(regex, replace)
 */
NODE *
f_gsub(NODE *np)
{
	return (dosub(np, 0));
}

/*
 * Do actual substitutions.
 * `glob' is the number to substitute, 0 for all.
 */
static NODE *
dosub(NODE *np, int glob)
{
	wchar_t *text;
	register wchar_t *sub;
	register uint n;
	register uint na;
	register REGEXP rp;
	NODE *left;
	static wchar_t *buf;

	if ((na = nargs(np)) != 2 && na != 3)
		awkerr(nargerr, glob==0 ? s_gsub : s_sub);
	rp = getregexp(getlist(&np));
	sub = exprstring(getlist(&np));
	if (na == 3) {
		left = getlist(&np);
		text = exprstring(left);
	} else {
		left = field0;
		text = linebuf;
	}
	switch (REGWDOSUBA(rp, sub, text, &buf, 256, &glob)) {
	case REG_OK:
	case REG_NOMATCH:
		n = glob;
		break;
	case REG_ESPACE:
		if (buf != NULL)
			free(buf);
		awkerr(nomem);
	default:
		awkerr(gettext("regular expression error"));
	}
	(void)assign(left, stringnode(buf, FNOALLOC, wcslen(buf)));
	return (intnode((INT)n));
}

/*
 * Match function.  Return position (origin 1) or 0 for regular
 * expression match in string.  Set new variables RSTART and RLENGTH
 * as well.
 * Usage:	pos = match(string, re)
 */
NODE *
f_match(NODE *np)
{
	register wchar_t *text;
	register REGEXP rp;
	register int pos, length;
	REGWMATCH_T match[10];

	if (nargs(np) != 2)
		awkerr(nargerr, s_match);
	text = exprstring(getlist(&np));
	rp = getregexp(getlist(&np));
	if (REGWEXEC(rp, text, 10, match, 0) == REG_OK) {
		pos = match[0].rm_sp-text+1;
		length = match[0].rm_ep - match[0].rm_sp;
	} else {
		pos = 0;
		length = -1;
	}
	constant->n_int = length;
	(void)assign(vlook(M_MB_L("RLENGTH")), constant);
	return (assign(vlook(M_MB_L("RSTART")), intnode((INT)pos)));
}

/*
 * Call shell or command interpreter.
 * Usage:	status = system(command)
 */
NODE *
f_system(NODE *np)
{
	int retcode;

	if (nargs(np) != 1)
		awkerr(nargerr, s_system);
	(void) fflush(stdout);
	retcode = system(mbunconvert(exprstring(getlist(&np))));
	return (intnode((INT)WEXITSTATUS(retcode)));
}

/*
 * Search for string within string.
 * Usage:	pos = index(string1, string2)
 */
NODE *
f_index(NODE *np)
{
	register wchar_t *s1, *s2;
	register int l1, l2;
	register int result;

	if (nargs(np) != 2)
		awkerr(nargerr, s_index);
	s1 = (wchar_t *)exprstring(getlist(&np));
	s2 = (wchar_t *)exprstring(getlist(&np));
	l1 = wcslen(s1);
	l2 = wcslen(s2);
	result = 1;
	while (l2 <= l1) {
		if (memcmp(s1, s2, l2 * sizeof(wchar_t)) == 0)
			break;
		result++;
		s1++;
		l1--;
	}
	if (l2 > l1)
		result = 0;
	return (intnode((INT)result));
}

/*
 * Return length of argument or $0
 * Usage:	n = length(string)
 *		n = length()
 *		n = length
 */
NODE *
f_length(NODE *np)
{
	register uint na;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, s_length);
	if (na == 0)
		na = lbuflen; else
		na = wcslen((wchar_t *)exprstring(getlist(&np)));
	return (intnode((INT)na));
}

/*
 * Split string into fields.
 * Usage: nfields = split(string, array [, separator]);
 */
NODE *
f_split(NODE *np)
{
	register wchar_t *cp;
	wchar_t *ep, *saved = 0;
	register NODE *tnp, *snp, *otnp;
	register NODE *sep;
	REGEXP old_resep = 0;
	size_t seplen;
	uint n;
	wint_t c;
	wchar_t savesep[20];
	wchar_t  *(*old_awkfield)(wchar_t **) = 0;

	if ((n = nargs(np))<2 || n>3)
		awkerr(nargerr, s_split);
	ep = exprstring(snp = getlist(&np));
	tnp = getlist(&np);
	if (snp->n_type == INDEX && snp->n_left == tnp)
		ep = saved = wsdup(ep);
	if (n == 3) {
		sep = getlist(&np);
	} else
		sep = NNULL;
	switch (tnp->n_type) {
	case ARRAY:
		delarray(tnp);
		break;

	case PARM:
		break;

	case VAR:
		if (isstring(tnp->n_flags) && tnp->n_string==_null)
			break;
	default:
		awkerr(gettext(
			"second parameter to \"split\" must be an array"));
		/*NOTREACHED*/
	}
	/*
	 * If an argument has been passed in to be used as the
	 * field separator check to see if it is a constant regular
	 * expression. If so, use it directly otherwise reduce the
	 * expression, convert the result into a string and assign it
	 * to "FS" (after saving the old value for FS.)
	 */
	if (sep != NNULL) {
		if (sep->n_type == PARM)
			sep = sep->n_next;
		if (sep->n_type == RE) {
			old_resep = resep;
			resep = sep->n_regexp;
			old_awkfield = awkfield;
			awkfield = refield;
		} else {
			sep = exprreduce(sep);
			seplen = wcslen(cp = (wchar_t *)exprstring(varFS));
			(void) memcpy(savesep, cp, 
				(seplen+1) * sizeof(wchar_t));
			(void) assign(varFS, sep);
		}
	}
	/*
	 * Iterate over the record, extracting each field and assigning it to
	 * the corresponding element in the array.
	 */
	otnp = tnp;	/* save tnp for possible promotion */
	tnp = node(INDEX, tnp, constant);
	fcount = 0;
	for (;;) {
		if ((cp = (*awkfield)(&ep)) == NULL) {
			if (fcount == 0) {
				if (otnp->n_type == PARM)
					otnp = otnp->n_next;
				promote(otnp);
			}
			break;
		}
		c = *ep;
		*ep = '\0';
		constant->n_int = ++fcount;
		(void)assign(tnp, stringnode(cp,FALLOC|FSENSE,(size_t)(ep-cp)));
		*ep = c;
	}
	/*
	 * Restore the old record separator/and or regular expression.
	 */
	if (sep != NNULL) {
		if (old_awkfield != 0) {
			resep = old_resep;
			awkfield = old_awkfield;
		} else {
			(void)assign(varFS,
				stringnode(savesep, FSTATIC, seplen));
		}
	}
	if (saved)
		free(saved);
	return (intnode((INT)fcount));
}

/*
 * Sprintf function.
 * Usage:	string = sprintf(format, arg, ...)
 */
NODE *
f_sprintf(NODE *np)
{
        wchar_t *cp;
        size_t length;

        if (nargs(np) == 0)
                awkerr(nargerr, s_sprintf);
        length = xprintf(np, (FILE *)NULL, &cp);
        np = stringnode(cp, FNOALLOC, length);
        return (np);
}

/*
 * Substring.
 * newstring = substr(string, start, [length])
 */
NODE *
f_substr(NODE *np)
{
	register STRING str;
	register size_t n;
	register int start;
	register size_t len;

	if ((n = nargs(np))<2 || n>3)
		awkerr(nargerr, s_substr);
	str = exprstring(getlist(&np));
	if ((start = (int)exprint(getlist(&np))-1) < 0)
		start = 0;
	if (n == 3) {
		int x;
		x = (int)exprint(getlist(&np));
		if (x < 0)
			len = 0;
		else
			len = (size_t)x;
	} else
		len = LARGE;
	n = wcslen((wchar_t *)str);
	if (start > n)
		start = n;
	n -= start;
	if (len > n)
		len = n;
	str += start;
	n = str[len];
	str[len] = '\0';
	np = stringnode(str, FALLOC, len);
	str[len] = n;
	return (np);
}

/*
 * Close an output or input file stream.
 */
NODE *
f_close(NODE *np)
{
	register OFILE *op;
	register char *name;

	if (nargs(np) != 1)
		awkerr(nargerr, s_close);
	name = mbunconvert(exprstring(getlist(&np)));
	for (op = &ofiles[0]; op < &ofiles[NIOSTREAM]; op++)
		if (op->f_fp!=FNULL && strcmp(name, op->f_name)==0) {
			awkclose(op);
			break;
		}
	if (op >= &ofiles[NIOSTREAM])
		return (const1);
	return (const0);
}

/*
 * Return the integer value of the first character of a string.
 * Usage:	char = ord(string)
 */
NODE *
f_ord(NODE *np)
{
	if (nargs(np) != 1)
		awkerr(nargerr, s_ord);
	return (intnode((INT)*exprstring(getlist(&np))));
}

/*
 * Return the argument string in lower case:
 * Usage:
 *	lower = tolower(upper)
 */
NODE *
f_tolower(NODE *np)
{
	return (docasetr(np, 0));
}

/*
 * Return the argument string in upper case:
 * Usage:
 *	upper = toupper(lower)
 */
NODE *
f_toupper(NODE *np)
{
	return (docasetr(np, 1));
}

/*
 * Sort the array into traversal order by the next "for (i in array)" loop.
 * Usage:
 *	asort(array, "cmpfunc")
 * 	cmpfunc(array, index1, index2)
 *		returns:
 *		<0		if 	array[index1] <  array[index2]
 *		 0		if	array[index1] == array[index2]
 *		>0		if	array[index1] >  array[index2]
 */
NODE *
f_asort(NODE *np)
{
	NODE *array;
	STRING funcname;
	register size_t nel;
	register NODE *tnp;
	register NODE *funcnp;
	register NODE **alist, **npp;

	if (nargs(np) != 2)
		awkerr(nargerr, s_asort);
	array = getlist(&np);
	if (array->n_type == PARM)
		array = array->n_next;
	if (array->n_type != ARRAY)
		awkerr(gettext("%s function requires an array"),
			s_asort);
	funcname = exprstring(getlist(&np));
	if ((funcnp = vlookup(funcname, 1)) == NNULL
	 || funcnp->n_type != UFUNC)
		awkerr(gettext("%s: %s is not a function\n"),
		    s_asort, funcname);
	/*
	 * Count size of array, allowing one extra for NULL at end
	 */
	nel = 1;
	for (tnp = array->n_alink; tnp != NNULL; tnp = tnp->n_alink)
		++nel;
	/*
	 * Create UFUNC node that points at the funcnp on left and the
	 * list of three variables on right (array, index1, index2)
	 *				UFUNC
	 *				/    \
	 *			   funcnp    COMMA
	 *				      /   \
	 *				array	  COMMA
	 *					  /    \
	 *					index1 index2
	 */
	if (asortfunc == NNULL) {
		running = 0;
		asortfunc = node(CALLUFUNC, NNULL,
				    node(COMMA, NNULL,
				    node(COMMA,
					asnp1=stringnode(_null, FSTATIC, 0),
					asnp2=stringnode(_null, FSTATIC, 0))));
		running = 1;
	}
	asortfunc->n_left = funcnp;
	asortfunc->n_right->n_left = array;
	asarraylen = wcslen(array->n_name)+1;
	alist = (NODE **) emalloc(nel*sizeof(NODE *));
	/*
	 * Copy array into alist.
	 */
	npp = alist;
	for (tnp = array->n_alink; tnp != NNULL; tnp = tnp->n_alink)
		*npp++ = tnp;
	*npp = NNULL;
	/*
	 * Re-order array to this list
	 */
	qsort((wchar_t *)alist, nel-1, sizeof (NODE *), asortcmp);
	tnp = array;
	npp = alist;
	do {
		tnp = tnp->n_alink = *npp;
	} while (*npp++ != NNULL);
	free((wchar_t *)alist);
	return (constundef);
}

/*
 * Return the number of arguments of a function.
 */
static uint
nargs(NODE *np)
{
	register int n;

	if (np == NNULL)
		return (0);
	n = 1;
	while (np!=NNULL && np->n_type==COMMA) {
		np = np->n_right;
		n++;
	}
	return (n);
}

/*
 * Do case translation.
 */
static NODE *
docasetr(NODE *np, int upper)
{
	register int c;
	register wchar_t *cp;
	register wchar_t *str;
	register uint na;

	if ((na = nargs(np)) > 1)
		awkerr(nargerr, upper ? s_toupper : s_tolower);
	str = strsave(na==0 ? linebuf : exprstring(getlist(&np)));
	cp = str;
	if (upper) {
		while ((c = *cp++) != '\0')
			cp[-1] = towupper(c);
	} else {
		while ((c = *cp++) != '\0')
			cp[-1] = towlower(c);
	}
	return (stringnode((STRING)str, FNOALLOC, (size_t)(cp-str-1)));
}

/*
 * The comparison routine used by qsort inside f_asort()
 */
static int
asortcmp(const void *npp1, const void *npp2)
{
	asnp1->n_strlen =
	    wcslen(asnp1->n_string = (*(NODE **)npp1)->n_name+asarraylen);
	asnp2->n_strlen =
	    wcslen(asnp2->n_string = (*(NODE **)npp2)->n_name+asarraylen);
	return ((int)exprint(asortfunc));
}

#if M_MATHERR
#if !defined(__BORLANDC__)&&defined(__TURBOC__)&&__COMPACT__&&__EMULATE__
/* So it won't optimize registers our FP is using */
#define	flushesbx()	(_BX = 0, _ES = _BX)
#else
#define	flushesbx()	(0)
#endif

/*
 * Math error for awk.
 */
int
matherr(struct exception *ep)
{
	register uint type;
	static char msgs[7][256];
	static int first_time = 1;

	if (first_time) {
		msgs[0] = gettext("Unknown FP error"),
		msgs[1] = gettext("Domain"),
		msgs[2] = gettext("Singularity"),
		msgs[3] = gettext("Overflow"),
		msgs[4] = gettext("Underflow"),
		msgs[5] = gettext("Total loss of precision"),
		msgs[6] = gettext("Partial loss of precision")
		first_time = 0;
	}

	if ((type = ep->type) > (uint)PLOSS)
		type = 0;
	(void)fprintf(stderr, "awk: %s", strmsg(msgs[type]));
	(void)fprintf(stderr, gettext(
		" error in function %s(%g) at NR=%lld\n"),
		((void) flushesbx(), ep->name), ep->arg1, (INT)exprint(varNR));
	return (1);
}
#endif	/*M_MATHERR*/

Added nawk.1.



































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
'\" te
.\" Copyright 1989 AT&T
.\" Copyright 1992, X/Open Company Limited  All Rights Reserved
.\" Portions Copyright (c) 2005, 2006 Sun Microsystems, Inc.  All Rights Reserved
.\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at
.\" http://www.opengroup.org/bookstore/.
.\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
.\"  This notice shall appear on any product containing this material.
.\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
.\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
.\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
.TH NAWK 1 "May 24, 2006"
.SH NAME
nawk \- pattern scanning and processing language
.SH SYNOPSIS
.LP
.nf
\fB/usr/bin/nawk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR] \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
     [\fIargument\fR]...
.fi

.LP
.nf
\fB/usr/xpg4/bin/awk\fR [\fB-F\fR \fIERE\fR] [\fB-v\fR \fIassignment\fR]... \fI\&'program'\fR | \fB-f\fR \fIprogfile\fR...
     [\fIargument\fR]...
.fi

.SH DESCRIPTION
.sp
.LP
The \fB/usr/bin/nawk\fR and \fB/usr/xpg4/bin/awk\fR utilities execute
\fIprogram\fRs written in the \fBnawk\fR programming language, which is
specialized for textual data manipulation. A \fBnawk\fR \fIprogram\fR is a
sequence of patterns and corresponding actions. The string specifying
\fIprogram\fR must be enclosed in single quotes (') to protect it from
interpretation by the shell. The sequence of pattern - action statements can be
specified in the command line as \fIprogram\fR or in one, or more, file(s)
specified by the \fB-f\fR\fIprogfile\fR option. When input is read that matches
a pattern, the action associated with the pattern is performed.
.sp
.LP
Input is interpreted as a sequence of records. By default, a record is a line,
but this can be changed by using the \fBRS\fR built-in variable. Each record of
input is matched to each pattern in the \fIprogram\fR. For each pattern
matched, the associated action is executed.
.sp
.LP
The \fBnawk\fR utility interprets each input record as a sequence of fields
where, by default, a field is a string of non-blank characters. This default
white-space field delimiter (blanks and/or tabs) can be changed by using the
\fBFS\fR built-in variable or the \fB-F\fR\fIERE\fR option. The \fBnawk\fR
utility denotes the first field in a record \fB$1\fR, the second \fB$2\fR, and
so forth. The symbol \fB$0\fR refers to the entire record; setting any other
field causes the reevaluation of \fB$0\fR. Assigning to \fB$0\fR resets the
values of all fields and the \fBNF\fR built-in variable.
.SH OPTIONS
.sp
.LP
The following options are supported:
.sp
.ne 2
.na
\fB\fB-F\fR \fIERE\fR\fR
.ad
.RS 17n
Define the input field separator to be the extended regular expression
\fIERE\fR, before any input is read (can be a character).
.RE

.sp
.ne 2
.na
\fB\fB-f\fR \fIprogfile\fR\fR
.ad
.RS 17n
Specifies the pathname of the file \fIprogfile\fR containing a \fBnawk\fR
program. If multiple instances of this option are specified, the concatenation
of the files specified as \fIprogfile\fR in the order specified is the
\fBnawk\fR program. The \fBnawk\fR program can alternatively be specified in
the command line as a single argument.
.RE

.sp
.ne 2
.na
\fB\fB-v\fR \fIassignment\fR\fR
.ad
.RS 17n
The \fIassignment\fR argument must be in the same form as an \fIassignment\fR
operand. The assignment is of the form \fIvar=value\fR, where \fIvar\fR is the
name of one of the variables described below. The specified assignment occurs
before executing the \fBnawk\fR program, including the actions associated with
\fBBEGIN\fR patterns (if any). Multiple occurrences of this option can be
specified.
.RE

.SH OPERANDS
.sp
.LP
The following operands are supported:
.sp
.ne 2
.na
\fB\fIprogram\fR\fR
.ad
.RS 12n
If no \fB-f\fR option is specified, the first operand to \fBnawk\fR is the text
of the \fBnawk\fR program. The application supplies the \fIprogram\fR operand
as a single argument to \fBnawk.\fR If the text does not end in a newline
character, \fBnawk\fR interprets the text as if it did.
.RE

.sp
.ne 2
.na
\fB\fIargument\fR\fR
.ad
.RS 12n
Either of the following two types of \fIargument\fR can be intermixed:
.sp
.ne 2
.na
\fB\fIfile\fR\fR
.ad
.RS 14n
A pathname of a file that contains the input to be read, which is matched
against the set of patterns in the program. If no \fIfile\fR operands are
specified, or if a \fIfile\fR operand is \fB\(mi\fR, the standard input is
used.
.RE

.sp
.ne 2
.na
\fB\fIassignment\fR\fR
.ad
.RS 14n
An operand that begins with an underscore or alphabetic character from the
portable character set, followed by a sequence of underscores, digits and
alphabetics from the portable character set, followed by the \fB=\fR character
specifies a variable assignment rather than a pathname. The characters before
the \fB=\fR represent the name of a \fBnawk\fR variable. If that name is a
\fBnawk\fR reserved word, the behavior is undefined. The characters following
the equal sign is interpreted as if they appeared in the \fBnawk\fR program
preceded and followed by a double-quote (\fB"\fR) character, as a \fBSTRING\fR
token , except that if the last character is an unescaped backslash, it is
interpreted as a literal backslash rather than as the first character of the
sequence \fB\e\fR\&.. The variable is assigned the value of that \fBSTRING\fR
token. If the value is considered a \fInumeric\fRstring\fI,\fR the variable is
assigned its numeric value. Each such variable assignment is performed just
before the processing of the following \fIfile\fR, if any. Thus, an assignment
before the first \fBfile\fR argument is executed after the \fBBEGIN\fR actions
(if any), while an assignment after the last \fIfile\fR argument is executed
before the \fBEND\fR actions (if any).  If there are no \fIfile\fR arguments,
assignments are executed before processing the standard input.
.RE

.RE

.SH INPUT FILES
.sp
.LP
Input files to the \fBnawk\fR program from any of the following sources:
.RS +4
.TP
.ie t \(bu
.el o
any \fIfile\fR operands or their equivalents, achieved by modifying the
\fBnawk\fR variables \fBARGV\fR and \fBARGC\fR
.RE
.RS +4
.TP
.ie t \(bu
.el o
standard input in the absence of any \fIfile\fR operands
.RE
.RS +4
.TP
.ie t \(bu
.el o
arguments to the \fBgetline\fR function
.RE
.sp
.LP
must be text files. Whether the variable \fBRS\fR is set to a value other than
a newline character or not, for these files, implementations support records
terminated with the specified separator up to \fB{LINE_MAX}\fR bytes and can
support longer records.
.sp
.LP
If \fB-\fR\fBf\fR \fIprogfile\fR is specified, the files named by each of the
\fIprogfile\fR option-arguments must be text files containing an \fBnawk\fR
program.
.sp
.LP
The standard input are used only if no \fIfile\fR operands are specified, or if
a \fIfile\fR operand is \fB\(mi\fR\&.
.SH EXTENDED DESCRIPTION
.sp
.LP
A \fBnawk\fR program is composed of pairs of the form:
.sp
.in +2
.nf
pattern { \fIaction\fR }
.fi
.in -2

.sp
.LP
Either the pattern or the action (including the enclosing brace characters) can
be omitted. Pattern-action statements are separated by a semicolon or by a
newline.
.sp
.LP
A missing pattern matches any record of input, and a missing action is
equivalent to an action that writes the matched record of input to standard
output.
.sp
.LP
Execution of the \fBnawk\fR program starts by first executing the actions
associated with all \fBBEGIN\fR patterns in the order they occur in the
program. Then each \fIfile\fR operand (or standard input if no files were
specified) is processed by reading data from the file until a record separator
is seen (a newline character by default), splitting the current record into
fields using the current value of \fBFS\fR, evaluating each pattern in the
program in the order of occurrence, and executing the action associated with
each pattern that matches the current record. The action for a matching pattern
is executed before evaluating subsequent patterns. Last, the actions associated
with all \fBEND\fR patterns is executed in the order they occur in the program.
.SS "Expressions in nawk"
.sp
.LP
Expressions describe computations used in \fIpatterns\fR and \fIactions\fR. In
the following table, valid expression operations are given in groups from
highest precedence first to lowest precedence last, with equal-precedence
operators grouped between horizontal lines. In expression evaluation, where the
grammar is formally ambiguous, higher precedence operators are evaluated before
lower precedence operators.  In this table \fIexpr,\fR \fIexpr1,\fR
\fIexpr2,\fR and \fIexpr3\fR represent any expression, while \fIlvalue\fR
represents any entity that can be assigned to (that is, on the left side of an
assignment operator).
.sp

.sp
.TS
c c c c
l l l l .
\fBSyntax\fR	\fBName\fR	\fBType of Result\fR	\fBAssociativity\fR
_
( \fIexpr\fR )	Grouping	type of \fIexpr\fR	n/a
_
$\fIexpr\fR	Field reference	string	n/a
_
++ \fIlvalue\fR	Pre-increment	numeric	n/a
 \(mi\(mi\fIlvalue\fR	Pre-decrement	numeric	n/a
\fIlvalue\fR ++	Post-increment	numeric	n/a
\fIlvalue\fR \(mi\(mi	Post-decrement 	numeric	n/a
_
\fIexpr\fR ^ \fIexpr\fR	Exponentiation	numeric	right
_
! \fIexpr\fR	Logical not	numeric	n/a
+ \fIexpr\fR	Unary plus	numeric	n/a
\(mi \fIexpr\fR	Unary minus	numeric	n/a
_
\fIexpr\fR * \fIexpr\fR	Multiplication	numeric	left
\fIexpr\fR / \fIexpr\fR	Division	numeric	left
\fIexpr\fR % \fIexpr\fR	Modulus	numeric	left
_
\fIexpr\fR + \fIexpr\fR	Addition	numeric	left
\fIexpr\fR \(mi \fIexpr\fR	Subtraction	numeric 	left
_
\fIexpr\fR \fIexpr\fR	String concatenation	string	left
_
\fIexpr\fR < \fIexpr\fR	Less than	numeric	none
\fIexpr\fR <= \fIexpr\fR	Less than or equal to	numeric	none
\fIexpr\fR != \fIexpr\fR	Not equal to	numeric	none
\fIexpr\fR == \fIexpr\fR	Equal to	numeric	none
\fIexpr\fR > \fIexpr\fR	Greater than	numeric	none
\fIexpr\fR >= \fIexpr\fR	Greater than or equal to	numeric	none
_
\fIexpr\fR ~ \fIexpr\fR	ERE match	numeric	none
\fIexpr\fR !~ \fIexpr\fR	ERE non-match	 numeric	none
_
\fIexpr\fR in array	Array membership	numeric	left
( \fIindex\fR ) in	Multi-dimension array	numeric	left
    \fIarray\fR	    membership		
_
\fBexpr\fR && \fIexpr\fR	Logical AND	numeric	left
_
\fBexpr\fR |\|| \fIexpr\fR	Logical OR	numeric	left
_
\fIexpr1\fR ? \fIexpr2\fR	Conditional expression	type of selected	right
    : \fIexpr3\fR		   \fIexpr2\fR or \fIexpr3\fR	
_
\fIlvalue\fR ^= \fIexpr\fR	Exponentiation	numeric	right
	assignment		
\fIlvalue\fR %= \fIexpr\fR	Modulus assignment	numeric	right
\fIlvalue\fR *= \fIexpr\fR	Multiplication	numeric	right
	assignment		
\fIlvalue\fR /= \fIexpr\fR	Division assignment	numeric	right
\fIlvalue\fR +=  \fIexpr\fR	Addition assignment	numeric	right
\fIlvalue\fR \(mi= \fIexpr\fR	Subtraction assignment	numeric	right
\fIlvalue\fR = \fIexpr\fR	Assignment	type of \fIexpr\fR	right
.TE

.sp
.LP
Each expression has either a string value, a numeric value or both. Except as
stated for specific contexts, the value of an expression is implicitly
converted to the type needed for the context in which it is used.  A string
value is converted to a numeric value by the equivalent of the following calls:
.sp
.in +2
.nf
setlocale(LC_NUMERIC, "");
\fInumeric_value\fR = atof(\fIstring_value\fR);
.fi
.in -2

.sp
.LP
A numeric value that is exactly equal to the value of an integer is converted
to a string by the equivalent of a call to the \fBsprintf\fR function with the
string \fB%d\fR as the \fBfmt\fR argument and the numeric value being converted
as the first and only \fIexpr\fR argument.  Any other numeric value is
converted to a string by the equivalent of a call to the \fBsprintf\fR function
with the value of the variable \fBCONVFMT\fR as the \fBfmt\fR argument and the
numeric value being converted as the first and only \fIexpr\fR argument.
.sp
.LP
A string value is considered to be a \fInumeric string\fR in the following
case:
.RS +4
.TP
1.
Any leading and trailing blank characters is ignored.
.RE
.RS +4
.TP
2.
If the first unignored character is a \fB+\fR or \fB\(mi\fR, it is ignored.
.RE
.RS +4
.TP
3.
If the remaining unignored characters would be lexically recognized as a
\fBNUMBER\fR token, the string is considered a \fInumeric string\fR.
.RE
.sp
.LP
If a \fB\(mi\fR character is ignored in the above steps, the numeric value of
the \fInumeric string\fR is the negation of the numeric value of the recognized
\fBNUMBER\fR token. Otherwise the numeric value of the \fInumeric string\fR is
the numeric value of the recognized \fBNUMBER\fR token. Whether or not a string
is a \fInumeric string\fR is relevant only in contexts where that term is used
in this section.
.sp
.LP
When an expression is used in a Boolean context, if it has a numeric value, a
value of zero is treated as false and any other value is treated as true.
Otherwise, a string value of the null string is treated as false and any other
value is treated as true. A Boolean context is one of the following:
.RS +4
.TP
.ie t \(bu
.el o
the first subexpression of a conditional expression.
.RE
.RS +4
.TP
.ie t \(bu
.el o
an expression operated on by logical NOT, logical \fBAND,\fR or logical OR.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the second expression of a \fBfor\fR statement.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the expression of an \fBif\fR statement.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the expression of the \fBwhile\fR clause in either a \fBwhile\fR or \fBdo\fR
\fB\&.\|.\|.\fR \fBwhile\fR statement.
.RE
.RS +4
.TP
.ie t \(bu
.el o
an expression used as a pattern (as in Overall Program Structure).
.RE
.sp
.LP
The \fBnawk\fR language supplies arrays that are used for storing numbers or
strings. Arrays need not be declared. They are initially empty, and their sizes
changes dynamically. The subscripts, or element identifiers, are strings,
providing a type of associative array capability. An array name followed by a
subscript within square brackets can be used as an \fIlvalue\fR and as an
expression, as described in the grammar.  Unsubscripted array names are used in
only the following contexts:
.RS +4
.TP
.ie t \(bu
.el o
a parameter in a function definition or function call.
.RE
.RS +4
.TP
.ie t \(bu
.el o
the \fBNAME\fR token following any use of the keyword \fBin\fR.
.RE
.sp
.LP
A valid array \fIindex\fR consists of one or more comma-separated expressions,
similar to the way in which multi-dimensional arrays are indexed in some
programming languages. Because \fBnawk\fR arrays are really one-dimensional,
such a comma-separated list is converted to a single string by concatenating
the string values of the separate expressions, each separated from the other by
the value of the \fBSUBSEP\fR variable.
.sp
.LP
Thus, the following two index operations are equivalent:
.sp
.in +2
.nf
var[expr1, expr2, ... exprn]
var[expr1 SUBSEP expr2 SUBSEP ... SUBSEP exprn]
.fi
.in -2

.sp
.LP
A multi-dimensioned \fIindex\fR used with the \fBin\fR operator must be put in
parentheses. The \fBin\fR operator, which tests for the existence of a
particular array element, does not create the element if it does not exist.
Any other reference to a non-existent array element automatically creates it.
.SS "Variables and Special Variables"
.sp
.LP
Variables can be used in an \fBnawk\fR program by referencing them. With the
exception of function parameters, they are not explicitly declared.
Uninitialized scalar variables and array elements have both a numeric value of
zero and a string value of the empty string.
.sp
.LP
Field variables are designated by a \fB$\fR followed by a number or numerical
expression. The effect of the field number \fIexpression\fR evaluating to
anything other than a non-negative integer is unspecified. Uninitialized
variables or string values need not be converted to numeric values in this
context. New field variables are created by assigning a value to them.
References to non-existent fields (that is, fields after \fB$NF\fR) produce the
null string. However, assigning to a non-existent field (for example,
\fB$(NF+2) = 5\fR) increases the value of \fBNF\fR, create any intervening
fields with the null string as their values and cause the value of \fB$0\fR to
be recomputed, with the fields being separated by the value of \fBOFS\fR. Each
field variable has a string value when created. If the string, with any
occurrence of the decimal-point character from the current locale changed to a
period character, is considered a \fInumeric string\fR (see \fBExpressions in
nawk\fR above), the field variable also has the numeric value of the \fInumeric
string\fR.
.SS "/usr/bin/nawk, /usr/xpg4/bin/awk"
.sp
.LP
\fBnawk\fR sets the following special variables that are supported by both
\fB/usr/bin/nawk\fR and \fB/usr/xpg4/bin/awk\fR:
.sp
.ne 2
.na
\fB\fBARGC\fR\fR
.ad
.RS 12n
The number of elements in the \fBARGV\fR array.
.RE

.sp
.ne 2
.na
\fB\fBARGV\fR\fR
.ad
.RS 12n
An array of command line arguments, excluding options and the \fIprogram\fR
argument, numbered from zero to \fBARGC\fR\(mi1.
.sp
The arguments in \fBARGV\fR can be modified or added to; \fBARGC\fR can be
altered.  As each input file ends, \fBnawk\fR treats the next non-null element
of \fBARGV\fR, up to the current value of \fBARGC\fR\(mi1, inclusive, as the
name of the next input file.  Setting an element of \fBARGV\fR to null means
that it is not treated as an input file. The name \fB\(mi\fR indicates the
standard input. If an argument matches the format of an \fIassignment\fR
operand, this argument is treated as an assignment rather than a \fIfile\fR
argument.
.RE

.sp
.ne 2
.na
\fB\fBENVIRON\fR\fR
.ad
.RS 12n
The variable \fBENVIRON\fR is an array representing the value of the
environment. The indices of the array are strings consisting of the names of
the environment variables, and the value of each array element is a string
consisting of the value of that variable. If the value of an environment
variable is considered a \fInumeric string\fR, the array element also has its
numeric value.
.sp
In all cases where \fBnawk\fR behavior is affected by environment variables
(including the environment of any commands that \fBnawk\fR executes via the
\fBsystem\fR function or via pipeline redirections with the \fBprint\fR
statement, the \fBprintf\fR statement, or the \fBgetline\fR function), the
environment used is the environment at the time \fBnawk\fR began executing.
.RE

.sp
.ne 2
.na
\fB\fBFILENAME\fR\fR
.ad
.RS 12n
A pathname of the current input file. Inside a \fBBEGIN\fR action the value is
undefined. Inside an \fBEND\fR action the value is the name of the last input
file processed.
.RE

.sp
.ne 2
.na
\fB\fBFNR\fR\fR
.ad
.RS 12n
The ordinal number of the current record in the current file. Inside a
\fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
the number of the last record processed in the last file processed.
.RE

.sp
.ne 2
.na
\fB\fBFS\fR\fR
.ad
.RS 12n
Input field separator regular expression; a space character by default.
.RE

.sp
.ne 2
.na
\fB\fBNF\fR\fR
.ad
.RS 12n
The number of fields in the current record. Inside a \fBBEGIN\fR action, the
use of \fBNF\fR is undefined unless a \fBgetline\fR function without a
\fIvar\fR argument is executed previously. Inside an \fBEND\fR action, \fBNF\fR
retains the value it had for the last record read, unless a subsequent,
redirected, \fBgetline\fR function without a \fIvar\fR argument is performed
prior to entering the \fBEND\fR action.
.RE

.sp
.ne 2
.na
\fB\fBNR\fR\fR
.ad
.RS 12n
The ordinal number of the current record from the start of input. Inside a
\fBBEGIN\fR action the value is zero. Inside an \fBEND\fR action the value is
the number of the last record processed.
.RE

.sp
.ne 2
.na
\fB\fBOFMT\fR\fR
.ad
.RS 12n
The \fBprintf\fR format for converting numbers to strings in output statements
\fB"%.6g"\fR by default. The result of the conversion is unspecified if the
value of \fBOFMT\fR is not a floating-point format specification.
.RE

.sp
.ne 2
.na
\fB\fBOFS\fR\fR
.ad
.RS 12n
The \fBprint\fR statement output field separator; a space character by default.
.RE

.sp
.ne 2
.na
\fB\fBORS\fR\fR
.ad
.RS 12n
The \fBprint\fR output record separator; a newline character by default.
.RE

.sp
.ne 2
.na
\fB\fBLENGTH\fR\fR
.ad
.RS 12n
The length of the string matched by the \fBmatch\fR function.
.RE

.sp
.ne 2
.na
\fB\fBRS\fR\fR
.ad
.RS 12n
The first character of the string value of \fBRS\fR is the input record
separator; a newline character by default. If \fBRS\fR contains more than one
character, the results are unspecified. If \fBRS\fR is null, then records are
separated by sequences of one or more blank lines. Leading or trailing blank
lines do not produce empty records at the beginning or end of input, and the
field separator is always newline, no matter what the value of \fBFS\fR.
.RE

.sp
.ne 2
.na
\fB\fBRSTART\fR\fR
.ad
.RS 12n
The starting position of the string matched by the \fBmatch\fR function,
numbering from 1. This is always equivalent to the return value of the
\fBmatch\fR function.
.RE

.sp
.ne 2
.na
\fB\fBSUBSEP\fR\fR
.ad
.RS 12n
The subscript separator string for multi-dimensional arrays. The default value
is \fB\e034\fR\&.
.RE

.SS "/usr/xpg4/bin/awk"
.sp
.LP
The following variable is supported for \fB/usr/xpg4/bin/awk\fR only:
.sp
.ne 2
.na
\fB\fBCONVFMT\fR\fR
.ad
.RS 11n
The \fBprintf\fR format for converting numbers to strings (except for output
statements, where \fBOFMT\fR is used). The default is \fB%.6g\fR.
.RE

.SS "Regular Expressions"
.sp
.LP
The \fBnawk\fR utility makes use of the extended regular expression notation
(see \fBregex\fR(5)) except that it allows the use of C-language conventions to
escape special characters within the EREs, namely \fB\e\e\fR, \fB\ea\fR,
\fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR, and those
specified in the following table.  These escape sequences are recognized both
inside and outside bracket expressions.  Note that records need not be
separated by newline characters and string constants can contain newline
characters, so even the \fB\en\fR sequence is valid in \fBnawk\fR EREs.  Using
a slash character within the regular expression requires escaping as shown in
the table below:
.sp

.sp
.TS
l l l
l l l .
\fBEscape Sequence\fR	\fBDescription\fR	\fBMeaning\fR
_
\fB\e"\fR	Backslash quotation-mark	Quotation-mark character
_
\fB\e/\fR	Backslash slash	Slash character
_
\fB\e\fR\fIddd\fR	T{
A backslash character followed by the longest sequence of one, two, or three octal-digit characters (01234567).  If all of the digits are 0, (that is, representation of the NULL character), the behavior is undefined.
T}	T{
The character encoded by the one-, two- or three-digit octal integer. Multi-byte characters require multiple, concatenated escape sequences, including the leading \e for each byte.
T}
_
\fB\e\fR\fIc\fR	T{
A backslash character followed by any character not described in this table or special characters (\fB\e\e\fR, \fB\ea\fR, \fB\eb\fR, \fB\ef\fR, \fB\en\fR, \fB\er\fR, \fB\et\fR, \fB\ev\fR).
T}	Undefined
.TE

.sp
.LP
A regular expression can be matched against a specific field or string by using
one of the two regular expression matching operators, \fB~\fR and \fB!\|~\fR.
These operators interpret their right-hand operand as a regular expression and
their left-hand operand as a string. If the regular expression matches the
string, the \fB~\fR expression evaluates to the value \fB1\fR, and the
\fB!\|~\fR expression evaluates to the value \fB0\fR. If the regular expression
does not match the string, the \fB~\fR expression evaluates to the value
\fB0\fR, and the \fB!\|~\fR expression evaluates to the value \fB1\fR. If the
right-hand operand is any expression other than the lexical token \fBERE\fR,
the string value of the expression is interpreted as an extended regular
expression, including the escape conventions described above. Notice that these
same escape conventions also are applied in the determining the value of a
string literal (the lexical token \fBSTRING\fR), and is applied a second time
when a string literal is used in this context.
.sp
.LP
When an \fBERE\fR token appears as an expression in any context other than as
the right-hand of the \fB~\fR or \fB!\|~\fR operator or as one of the built-in
function arguments described below, the value of the resulting expression is
the equivalent of:
.sp
.in +2
.nf
$0 ~ /\fIere\fR/
.fi
.in -2

.sp
.LP
The \fIere\fR argument to the \fBgsub,\fR \fBmatch,\fR \fBsub\fR functions, and
the \fIfs\fR argument to the \fBsplit\fR function (see \fBString Functions\fR)
is interpreted as extended regular expressions. These can be either \fBERE\fR
tokens or arbitrary expressions, and are interpreted in the same manner as the
right-hand side of the \fB~\fR or \fB!\|~\fR operator.
.sp
.LP
An extended regular expression can be used to separate fields by using the
\fB-F\fR \fIERE\fR option or by assigning a string containing the expression to
the built-in variable \fBFS\fR. The default value of the \fBFS\fR variable is a
single space character. The following describes \fBFS\fR behavior:
.RS +4
.TP
1.
If \fBFS\fR is a single character:
.RS +4
.TP
.ie t \(bu
.el o
If \fBFS\fR is the space character, skip leading and trailing blank characters;
fields are delimited by sets of one or more blank characters.
.RE
.RS +4
.TP
.ie t \(bu
.el o
Otherwise, if \fBFS\fR is any other character \fIc\fR, fields are delimited by
each single occurrence of \fIc\fR.
.RE
.RE
.RS +4
.TP
2.
Otherwise, the string value of \fBFS\fR is considered to be an extended
regular expression. Each occurrence of a sequence matching the extended regular
expression delimits fields.
.RE
.sp
.LP
Except in the \fBgsub\fR, \fBmatch\fR, \fBsplit\fR, and \fBsub\fR built-in
functions, regular expression matching is based on input records. That is,
record separator characters (the first character of the value of the variable
\fBRS\fR, a newline character by default) cannot be embedded in the expression,
and no expression matches the record separator character. If the record
separator is not a newline character, newline characters embedded in the
expression can be matched. In those four built-in functions, regular expression
matching are based on text strings. So, any character (including the newline
character and the record separator) can be embedded in the pattern and an
appropriate pattern matches any character. However, in all \fBnawk\fR regular
expression matching, the use of one or more NULL characters in the pattern,
input record or text string produces undefined results.
.SS "Patterns"
.sp
.LP
A \fIpattern\fR is any valid \fIexpression,\fR a range specified by two
expressions separated by comma, or one of the two special patterns \fBBEGIN\fR
or \fBEND\fR.
.SS "Special Patterns"
.sp
.LP
The \fBnawk\fR utility recognizes two special patterns, \fBBEGIN\fR and
\fBEND\fR. Each \fBBEGIN\fR pattern is matched once and its associated action
executed before the first record of input is read (except possibly by use of
the \fBgetline\fR function in a prior \fBBEGIN\fR action) and before command
line assignment is done. Each \fBEND\fR pattern is matched once and its
associated action executed after the last record of input has been read. These
two patterns have associated actions.
.sp
.LP
\fBBEGIN\fR and \fBEND\fR do not combine with other patterns.  Multiple
\fBBEGIN\fR and \fBEND\fR patterns are allowed. The actions associated with the
\fBBEGIN\fR patterns are executed in the order specified in the program, as are
the \fBEND\fR actions. An \fBEND\fR pattern can precede a \fBBEGIN\fR pattern
in a program.
.sp
.LP
If an \fBnawk\fR program consists of only actions with the pattern \fBBEGIN\fR,
and the \fBBEGIN\fR action contains no \fBgetline\fR function, \fBnawk\fR exits
without reading its input when the last statement in the last \fBBEGIN\fR
action is executed. If an \fBnawk\fR program consists of only actions with the
pattern \fBEND\fR or only actions with the patterns \fBBEGIN\fR and \fBEND\fR,
the input is read before the statements in the \fBEND\fR actions are executed.
.SS "Expression Patterns"
.sp
.LP
An expression pattern is evaluated as if it were an expression in a Boolean
context. If the result is true, the pattern is considered to match, and the
associated action (if any) is executed. If the result is false, the action is
not executed.
.SS "Pattern Ranges"
.sp
.LP
A pattern range consists of two expressions separated by a comma. In this case,
the action is performed for all records between a match of the first expression
and the following match of the second expression, inclusive. At this point, the
pattern range can be repeated starting at input records subsequent to the end
of the matched range.
.SS "Actions"
.sp
.LP
An action is a sequence of statements. A statement can be one of the following:
.sp
.in +2
.nf
if ( \fIexpression\fR ) \fIstatement\fR [ else \fIstatement\fR ]
while ( \fIexpression\fR ) \fIstatement\fR
do \fIstatement\fR while ( \fIexpression\fR )
for ( \fIexpression\fR ; \fIexpression\fR ; \fIexpression\fR ) \fIstatement\fR
for ( \fIvar\fR in \fIarray\fR ) \fIstatement\fR
delete \fIarray\fR[\fIsubscript\fR] #delete an array element
break
continue
{ [ \fIstatement\fR ] .\|.\|. }
\fIexpression\fR        # commonly variable = expression
print [ \fIexpression-list\fR ] [ >\fIexpression\fR ]
printf format [ ,\fIexpression-list\fR ] [ >\fIexpression\fR ]
next              # skip remaining patterns on this input line
exit [expr] # skip the rest of the input; exit status is expr
return [expr]
.fi
.in -2

.sp
.LP
Any single statement can be replaced by a statement list enclosed in braces.
The statements are terminated by newline characters or semicolons, and are
executed sequentially in the order that they appear.
.sp
.LP
The \fBnext\fR statement causes all further processing of the current input
record to be abandoned. The behavior is undefined if a \fBnext\fR statement
appears or is invoked in a \fBBEGIN\fR or \fBEND\fR action.
.sp
.LP
The \fBexit\fR statement invokes all \fBEND\fR actions in the order in which
they occur in the program source and then terminate the program without reading
further input. An \fBexit\fR statement inside an \fBEND\fR action terminates
the program without further execution of \fBEND\fR actions.  If an expression
is specified in an \fBexit\fR statement, its numeric value is the exit status
of \fBnawk\fR, unless subsequent errors are encountered or a subsequent
\fBexit\fR statement with an expression is executed.
.SS "Output Statements"
.sp
.LP
Both \fBprint\fR and \fBprintf\fR statements write to standard output by
default.  The output is written to the location specified by
\fIoutput_redirection\fR if one is supplied, as follows:
.sp
.in +2
.nf
\fB>\fR \fIexpression\fR\fB>>\fR \fIexpression\fR\fB|\fR \fIexpression\fR
.fi
.in -2

.sp
.LP
In all cases, the \fIexpression\fR is evaluated to produce a string that is
used as a full pathname to write into (for \fB>\fR or \fB>>\fR) or as a command
to be executed (for \fB|\fR). Using the first two forms, if the file of that
name is not currently open, it is opened, creating it if necessary and using
the first form, truncating the file. The output then is appended to the file.
As long as the file remains open, subsequent calls in which \fIexpression\fR
evaluates to the same string value simply appends output to the file. The file
remains open until the \fBclose\fR function, which is called with an expression
that evaluates to the same string value.
.sp
.LP
The third form writes output onto a stream piped to the input of a command. The
stream is created if no stream is currently open with the value of
\fIexpression\fR as its command name.  The stream created is equivalent to one
created by a call to the \fBpopen\fR(3C) function with the value of
\fIexpression\fR as the \fIcommand\fR argument and a value of \fBw\fR as the
\fImode\fR argument.  As long as the stream remains open, subsequent calls in
which \fIexpression\fR evaluates to the same string value writes output to the
existing stream. The stream remains open until the \fBclose\fR function is
called with an expression that evaluates to the same string value.  At that
time, the stream is closed as if by a call to the \fBpclose\fR function.
.sp
.LP
These output statements take a comma-separated list of \fIexpression\fR \fIs\fR
referred in the grammar by the non-terminal symbols \fBexpr_list,\fR
\fBprint_expr_list\fR or \fBprint_expr_list_opt.\fR This list is referred to
here as the \fIexpression list\fR, and each member is referred to as an
\fIexpression argument\fR.
.sp
.LP
The \fBprint\fR statement writes the value of each expression argument onto the
indicated output stream separated by the current output field separator (see
variable \fBOFS\fR above), and terminated by the output record separator (see
variable \fBORS\fR above). All expression arguments is taken as strings, being
converted if necessary; with the exception that the \fBprintf\fR format in
\fBOFMT\fR is used instead of the value in \fBCONVFMT\fR. An empty expression
list stands for the whole input record \fB(\fR$0\fB)\fR.
.sp
.LP
The \fBprintf\fR statement produces output based on a notation similar to the
File Format Notation used to describe file formats in this document Output is
produced as specified with the first expression argument as the string
\fBformat\fR and subsequent expression arguments as the strings \fBarg1\fR to
\fBargn,\fR inclusive, with the following exceptions:
.RS +4
.TP
1.
The \fIformat\fR is an actual character string rather than a graphical
representation. Therefore, it cannot contain empty character positions. The
space character in the \fIformat\fR string, in any context other than a
\fIflag\fR of a conversion specification, is treated as an ordinary character
that is copied to the output.
.RE
.RS +4
.TP
2.
If the character set contains a Delta character and that character appears
in the \fIformat\fR string, it is treated as an ordinary character that is
copied to the output.
.RE
.RS +4
.TP
3.
The \fIescape sequences\fR beginning with a backslash character is treated
as sequences of ordinary characters that are copied to the output. Note that
these same sequences is interpreted lexically by \fBnawk\fR when they appear in
literal strings, but they is not treated specially by the \fBprintf\fR
statement.
.RE
.RS +4
.TP
4.
A \fIfield width\fR or \fIprecision\fR can be specified as the \fB*\fR
character instead of a digit string. In this case the next argument from the
expression list is fetched and its numeric value taken as the field width or
precision.
.RE
.RS +4
.TP
5.
The implementation does not precede or follow output from the \fBd\fR or
\fBu\fR conversion specifications with blank characters not specified by the
\fIformat\fR string.
.RE
.RS +4
.TP
6.
The implementation does not precede output from the \fBo\fR conversion
specification with leading zeros not specified by the \fIformat\fR string.
.RE
.RS +4
.TP
7.
For the \fBc\fR conversion specification: if the argument has a numeric
value, the character whose encoding is that value is output.  If the value is
zero or is not the encoding of any character in the character set, the behavior
is undefined.  If the argument does not have a numeric value, the first
character of the string value is output; if the string does not contain any
characters the behavior is undefined.
.RE
.RS +4
.TP
8.
For each conversion specification that consumes an argument, the next
expression argument is evaluated. With the exception of the \fBc\fR conversion,
the value is converted to the appropriate type for the conversion
specification.
.RE
.RS +4
.TP
9.
If there are insufficient expression arguments to satisfy all the conversion
specifications in the \fIformat\fR string, the behavior is undefined.
.RE
.RS +4
.TP
10.
If any character sequence in the \fIformat\fR string begins with a %
character, but does not form a valid conversion specification, the behavior is
unspecified.
.RE
.sp
.LP
Both \fBprint\fR and \fBprintf\fR can output at least \fB{LINE_MAX}\fR bytes.
.SS "Functions"
.sp
.LP
The \fBnawk\fR language has a variety of built-in functions: arithmetic,
string, input/output and general.
.SS "Arithmetic Functions"
.sp
.LP
The arithmetic functions, except for \fBint\fR, are based on the \fBISO\fR
\fBC\fR standard. The behavior is undefined in cases where the \fBISO\fR
\fBC\fR standard specifies that an error be returned or that the behavior is
undefined. Although the grammar permits built-in functions to appear with no
arguments or parentheses, unless the argument or parentheses are indicated as
optional in the following list (by displaying them within the \fB[ ]\fR
brackets), such use is undefined.
.sp
.ne 2
.na
\fB\fBatan2(\fR\fIy\fR,\fIx\fR\fB)\fR\fR
.ad
.RS 17n
Return arctangent of \fIy\fR/\fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBcos\fR(\fIx\fR)\fR
.ad
.RS 17n
Return cosine of \fIx,\fR where \fIx\fR is in radians.
.RE

.sp
.ne 2
.na
\fB\fBsin\fR(\fIx\fR)\fR
.ad
.RS 17n
Return sine of \fIx,\fR where \fIx\fR is in radians.
.RE

.sp
.ne 2
.na
\fB\fBexp\fR(\fIx\fR)\fR
.ad
.RS 17n
Return the exponential function of \fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBlog\fR(\fIx\fR)\fR
.ad
.RS 17n
Return the natural logarithm of \fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBsqrt\fR(\fIx\fR)\fR
.ad
.RS 17n
Return the square root of \fIx\fR.
.RE

.sp
.ne 2
.na
\fB\fBint\fR(\fIx\fR)\fR
.ad
.RS 17n
Truncate its argument to an integer. It is truncated toward 0 when \fIx\fR > 0.
.RE

.sp
.ne 2
.na
\fB\fBrand()\fR\fR
.ad
.RS 17n
Return a random number \fIn\fR, such that 0 \(<= \fIn\fR < 1.
.RE

.sp
.ne 2
.na
\fB\fBsrand\fR([\fBexpr\fR])\fR
.ad
.RS 17n
Set the seed value for \fBrand\fR to \fIexpr\fR or use the time of day if
\fIexpr\fR is omitted. The previous seed value is returned.
.RE

.SS "String Functions"
.sp
.LP
The string functions in the following list shall be supported. Although the
grammar permits built-in functions to appear with no arguments or parentheses,
unless the argument or parentheses are indicated as optional in the following
list (by displaying them within the \fB[ ]\fR brackets), such use is undefined.
.sp
.ne 2
.na
\fB\fBgsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
.ad
.sp .6
.RS 4n
Behave like \fBsub\fR (see below), except that it replaces all occurrences of
the regular expression (like the \fBed\fR utility global substitute) in
\fB$0\fR or in the \fIin\fR argument, when specified.
.RE

.sp
.ne 2
.na
\fB\fBindex\fR(\fIs\fR,\fIt\fR)\fR
.ad
.sp .6
.RS 4n
Return the position, in characters, numbering from 1, in string \fIs\fR where
string \fIt\fR first occurs, or zero if it does not occur at all.
.RE

.sp
.ne 2
.na
\fB\fBlength\fR[([\fIs\fR])]\fR
.ad
.sp .6
.RS 4n
Return the length, in characters, of its argument taken as a string, or of the
whole record, \fB$0\fR, if there is no argument.
.RE

.sp
.ne 2
.na
\fB\fBmatch\fR(\fIs\fR,\fIere\fR)\fR
.ad
.sp .6
.RS 4n
Return the position, in characters, numbering from 1, in string \fIs\fR where
the extended regular expression \fIere\fR occurs, or zero if it does not occur
at all. \fBRSTART\fR is set to the starting position (which is the same as the
returned value), zero if no match is found; \fBRLENGTH\fR is set to the length
of the matched string, \(mi1 if no match is found.
.RE

.sp
.ne 2
.na
\fB\fBsplit\fR(\fIs\fR,\fIa\fR[,\|\fIfs\fR])\fR
.ad
.sp .6
.RS 4n
Split the string \fIs\fR into array elements \fIa\fR[1], \fIa\fR[2],
\fB\&...,\fR \fIa\fR[\fIn\fR], and return \fIn\fR. The separation is done with
the extended regular expression \fIfs\fR or with the field separator \fBFS\fR
if \fIfs\fR is not given. Each array element has a string value when created.
If the string assigned to any array element, with any occurrence of the
decimal-point character from the current locale changed to a period character,
would be considered a \fInumeric string\fR; the array element also has the
numeric value of the \fInumeric string\fR. The effect of a null string as the
value of \fIfs\fR is unspecified.
.RE

.sp
.ne 2
.na
\fB\fBsprintf\fR(\fBfmt\fR,\fIexpr\fR,\fIexpr\fR,\fB\&...\fR)\fR
.ad
.sp .6
.RS 4n
Format the expressions according to the \fBprintf\fR format given by \fIfmt\fR
and return the resulting string.
.RE

.sp
.ne 2
.na
\fB\fBsub\fR(\fIere\fR,\fIrepl\fR[,\|\fIin\fR])\fR
.ad
.sp .6
.RS 4n
Substitute the string \fIrepl\fR in place of the first instance of the extended
regular expression \fBERE\fR in string in and return the number of
substitutions. An ampersand ( \fB&\fR ) appearing in the string \fIrepl\fR is
replaced by the string from in that matches the regular expression. An
ampersand preceded with a backslash ( \fB\e\fR ) is interpreted as the literal
ampersand character. An occurrence of two consecutive backslashes is
interpreted as just a single literal backslash character.  Any other occurrence
of a backslash (for example, preceding any other character) is treated as a
literal backslash character. If \fIrepl\fR is a string literal, the handling of
the ampersand character occurs after any lexical processing, including any
lexical backslash escape sequence processing. If \fBin\fR is specified and it
is not an \fBlvalue\fR the behavior is undefined. If in is omitted, \fBnawk\fR
uses the current record (\fB$0\fR) in its place.
.RE

.sp
.ne 2
.na
\fB\fBsubstr\fR(\fIs\fR,\fIm\fR[,\|\fIn\fR])\fR
.ad
.sp .6
.RS 4n
Return the at most \fIn\fR-character substring of \fIs\fR that begins at
position \fIm,\fR numbering from 1. If \fIn\fR is missing, the length of the
substring is limited by the length of the string \fIs\fR.
.RE

.sp
.ne 2
.na
\fB\fBtolower\fR(\fIs\fR)\fR
.ad
.sp .6
.RS 4n
Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
an upper-case letter specified to have a \fBtolower\fR mapping by the
\fBLC_CTYPE\fR category of the current locale is replaced in the returned
string by the lower-case letter specified by the mapping. Other characters in
\fIs\fR are unchanged in the returned string.
.RE

.sp
.ne 2
.na
\fB\fBtoupper\fR(\fIs\fR)\fR
.ad
.sp .6
.RS 4n
Return a string based on the string \fIs\fR. Each character in \fIs\fR that is
a lower-case letter specified to have a \fBtoupper\fR mapping by the
\fBLC_CTYPE\fR category of the current locale is replaced in the returned
string by the upper-case letter specified by the mapping. Other characters in
\fIs\fR are unchanged in the returned string.
.RE

.sp
.LP
All of the preceding functions that take \fIERE\fR as a parameter expect a
pattern or a string valued expression that is a regular expression as defined
below.
.SS "Input/Output and General Functions"
.sp
.LP
The input/output and general functions are:
.sp
.ne 2
.na
\fB\fBclose(\fR\fIexpression\fR)\fR
.ad
.RS 27n
Close the file or pipe opened by a \fBprint\fR or \fBprintf\fR statement or a
call to \fBgetline\fR with the same string-valued \fIexpression\fR. If the
close was successful, the function returns \fB0\fR; otherwise, it returns
non-zero.
.RE

.sp
.ne 2
.na
\fB\fIexpression\fR|\fBgetline\fR[\fIvar\fR]\fR
.ad
.RS 27n
Read a record of input from a stream piped from the output of a command. The
stream is created if no stream is currently open with the value of
\fIexpression\fR as its command name. The stream created is equivalent to one
created by a call to the \fBpopen\fR function with the value of
\fIexpression\fR as the \fIcommand\fR argument and a value of \fBr\fR as the
\fImode\fR argument. As long as the stream remains open, subsequent calls in
which \fIexpression\fR evaluates to the same string value reads subsequent
records from the file. The stream remains open until the \fBclose\fR function
is called with an expression that evaluates to the same string value. At that
time, the stream is closed as if by a call to the \fBpclose\fR function. If
\fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise, \fIvar\fR is
set.
.sp
The \fBgetline\fR operator can form ambiguous constructs when there are
operators that are not in parentheses (including concatenate) to the left of
the \fB|\fR (to the beginning of the expression containing \fBgetline\fR). In
the context of the \fB$\fR operator, \fB|\fR behaves as if it had a lower
precedence than \fB$\fR. The result of evaluating other operators is
unspecified, and all such uses of portable applications must be put in
parentheses properly.
.RE

.sp
.ne 2
.na
\fB\fBgetline\fR\fR
.ad
.RS 30n
Set \fB$0\fR to the next input record from the current input file. This form of
\fBgetline\fR sets the \fBNF\fR, \fBNR\fR, and \fBFNR\fR variables.
.RE

.sp
.ne 2
.na
\fB\fBgetline\fR \fIvar\fR\fR
.ad
.RS 30n
Set variable \fIvar\fR to the next input record from the current input file.
This form of \fBgetline\fR sets the \fBFNR\fR and \fBNR\fR variables.
.RE

.sp
.ne 2
.na
\fB\fBgetline\fR [\fIvar\fR] \fB<\fR \fIexpression\fR\fR
.ad
.RS 30n
Read the next record of input from a named file. The \fIexpression\fR is
evaluated to produce a string that is used as a full pathname. If the file of
that name is not currently open, it is opened. As long as the stream remains
open, subsequent calls in which \fIexpression\fR evaluates to the same string
value reads subsequent records from the file. The file remains open until the
\fBclose\fR function is called with an expression that evaluates to the same
string value. If \fIvar\fR is missing, \fB$0\fR and \fBNF\fR is set. Otherwise,
\fIvar\fR is set.
.sp
The \fBgetline\fR operator can form ambiguous constructs when there are binary
operators that are not in parentheses (including concatenate) to the right of
the \fB<\fR (up to the end of the expression containing the \fBgetline\fR). The
result of evaluating such a construct is unspecified, and all such uses of
portable applications must be put in parentheses properly.
.RE

.sp
.ne 2
.na
\fB\fBsystem\fR(\fIexpression\fR)\fR
.ad
.RS 30n
Execute the command given by \fIexpression\fR in a manner equivalent to the
\fBsystem\fR(3C) function and return the exit status of the command.
.RE

.sp
.LP
All forms of \fBgetline\fR return \fB1\fR for successful input, \fB0\fR for end
of file, and \fB\(mi1\fR for an error.
.sp
.LP
Where strings are used as the name of a file or pipeline, the strings must be
textually identical. The terminology ``same string value'' implies that
``equivalent strings'', even those that differ only by space characters,
represent different files.
.SS "User-defined Functions"
.sp
.LP
The \fBnawk\fR language also provides user-defined functions. Such functions
can be defined as:
.sp
.in +2
.nf
\fBfunction\fR \fIname\fR(\fIargs\fR,\|.\|.\|.) { \fIstatements\fR }
.fi
.in -2

.sp
.LP
A function can be referred to anywhere in an \fBnawk\fR program; in particular,
its use can precede its definition. The scope of a function is global.
.sp
.LP
Function arguments can be either scalars or arrays; the behavior is undefined
if an array name is passed as an argument that the function uses as a scalar,
or if a scalar expression is passed as an argument that the function uses as an
array. Function arguments are passed by value if scalar and by reference if
array name. Argument names are local to the function; all other variable names
are global. The same name is not used as both an argument name and as the name
of a function or a special \fBnawk\fR variable. The same name must not be used
both as a variable name with global scope and as the name of a function. The
same name must not be used within the same scope both as a scalar variable and
as an array.
.sp
.LP
The number of parameters in the function definition need not match the number
of parameters in the function call. Excess formal parameters can be used as
local variables. If fewer arguments are supplied in a function call than are in
the function definition, the extra parameters that are used in the function
body as scalars are initialized with a string value of the null string and a
numeric value of zero, and the extra parameters that are used in the function
body as arrays are initialized as empty arrays. If more arguments are supplied
in a function call than are in the function definition, the behavior is
undefined.
.sp
.LP
When invoking a function, no white space can be placed between the function
name and the opening parenthesis. Function calls can be nested and recursive
calls can be made upon functions. Upon return from any nested or recursive
function call, the values of all of the calling function's parameters are
unchanged, except for array parameters passed by reference. The \fBreturn\fR
statement can be used to return a value. If a \fBreturn\fR statement appears
outside of a function definition, the behavior is undefined.
.sp
.LP
In the function definition, newline characters are optional before the opening
brace and after the closing brace. Function definitions can appear anywhere in
the program where a \fIpattern-action\fR pair is allowed.
.SH USAGE
.sp
.LP
The \fBindex\fR, \fBlength\fR, \fBmatch\fR, and \fBsubstr\fR functions should
not be confused with similar functions in the \fBISO C\fR standard; the
\fBnawk\fR versions deal with characters, while the \fBISO C\fR standard deals
with bytes.
.sp
.LP
Because the concatenation operation is represented by adjacent expressions
rather than an explicit operator, it is often necessary to use parentheses to
enforce the proper evaluation precedence.
.sp
.LP
See \fBlargefile\fR(5) for the description of the behavior of \fBnawk\fR when
encountering files greater than or equal to 2 Gbyte (2^31 bytes).
.SH EXAMPLES
.sp
.LP
The \fBnawk\fR program specified in the command line is most easily specified
within single-quotes (for example, \fB\&'\fR\fIprogram\fR\fB\&'\fR) for
applications using \fBsh\fR, because \fBnawk\fR programs commonly contain
characters that are special to the shell, including double-quotes. In the cases
where a \fBnawk\fR program contains single-quote characters, it is usually
easiest to specify most of the program as strings within single-quotes
concatenated by the shell with quoted single-quote characters. For example:
.sp
.in +2
.nf
nawk '/'\e''/ { print "quote:", $0 }'
.fi
.in -2

.sp
.LP
prints all lines from the standard input containing a single-quote character,
prefixed with \fBquote:\fR.
.sp
.LP
The following are examples of simple \fBnawk\fR programs:
.LP
\fBExample 1 \fRWrite to the standard output all input lines for which field 3
is greater than 5:
.sp
.in +2
.nf
\fB$3 > 5\fR
.fi
.in -2
.sp

.LP
\fBExample 2 \fRWrite every tenth line:
.sp
.in +2
.nf
\fB(NR % 10) == 0\fR
.fi
.in -2
.sp

.LP
\fBExample 3 \fRWrite any line with a substring matching the regular
expression:
.sp
.in +2
.nf
\fB/(G|D)(2[0-9][[:alpha:]]*)/\fR
.fi
.in -2
.sp

.LP
\fBExample 4 \fRPrint any line with a substring containing a G or D, followed
by a sequence of digits and characters:
.sp
.LP
This example uses character classes \fBdigit\fR and \fBalpha\fR to match
language-independent digit and alphabetic characters, respectively.

.sp
.in +2
.nf
\fB/(G|D)([[:digit:][:alpha:]]*)/\fR
.fi
.in -2
.sp

.LP
\fBExample 5 \fRWrite any line in which the second field matches the regular
expression and the fourth field does not:
.sp
.in +2
.nf
\fB$2 ~ /xyz/ && $4 !~ /xyz/\fR
.fi
.in -2
.sp

.LP
\fBExample 6 \fRWrite any line in which the second field contains a backslash:
.sp
.in +2
.nf
\fB$2 ~ /\e\e/\fR
.fi
.in -2
.sp

.LP
\fBExample 7 \fRWrite any line in which the second field contains a backslash
(alternate method):
.sp
.LP
Notice that backslash escapes are interpreted twice, once in lexical processing
of the string and once in processing the regular expression.

.sp
.in +2
.nf
\fB$2 ~ "\e\e\e\e"\fR
.fi
.in -2
.sp

.LP
\fBExample 8 \fRWrite the second to the last and the last field in each line,
separating the fields by a colon:
.sp
.in +2
.nf
\fB{OFS=":";print $(NF-1), $NF}\fR
.fi
.in -2
.sp

.LP
\fBExample 9 \fRWrite the line number and number of fields in each line:
.sp
.LP
The three strings representing the line number, the colon and the number of
fields are concatenated and that string is written to standard output.

.sp
.in +2
.nf
\fB{print NR ":" NF}\fR
.fi
.in -2
.sp

.LP
\fBExample 10 \fRWrite lines longer than 72 characters:
.sp
.in +2
.nf
\fB{length($0) > 72}\fR
.fi
.in -2
.sp

.LP
\fBExample 11 \fRWrite first two fields in opposite order separated by the OFS:
.sp
.in +2
.nf
\fB{ print $2, $1 }\fR
.fi
.in -2
.sp

.LP
\fBExample 12 \fRSame, with input fields separated by comma or space and tab
characters, or both:
.sp
.in +2
.nf
\fBBEGIN { FS = ",[\et]*|[\et]+" }
      { print $2, $1 }\fR
.fi
.in -2
.sp

.LP
\fBExample 13 \fRAdd up first column, print sum and average:
.sp
.in +2
.nf
\fB{s += $1 }
END {print "sum is ", s, " average is", s/NR}\fR
.fi
.in -2
.sp

.LP
\fBExample 14 \fRWrite fields in reverse order, one per line (many lines out
for each line in):
.sp
.in +2
.nf
\fB{ for (i = NF; i > 0; --i) print $i }\fR
.fi
.in -2
.sp

.LP
\fBExample 15 \fRWrite all lines between occurrences of the strings "start" and
"stop":
.sp
.in +2
.nf
\fB/start/, /stop/\fR
.fi
.in -2
.sp

.LP
\fBExample 16 \fRWrite all lines whose first field is different from the
previous one:
.sp
.in +2
.nf
\fB$1 != prev { print; prev = $1 }\fR
.fi
.in -2
.sp

.LP
\fBExample 17 \fRSimulate the echo command:
.sp
.in +2
.nf
\fBBEGIN  {
       for (i = 1; i < ARGC; ++i)
             printf "%s%s", ARGV[i], i==ARGC-1?"\en":""
       }\fR
.fi
.in -2
.sp

.LP
\fBExample 18 \fRWrite the path prefixes contained in the PATH environment
variable, one per line:
.sp
.in +2
.nf
\fBBEGIN  {
       n = split (ENVIRON["PATH"], path, ":")
       for (i = 1; i <= n; ++i)
              print path[i]
       }\fR
.fi
.in -2
.sp

.LP
\fBExample 19 \fRPrint the file "input", filling in page numbers starting at 5:
.sp
.LP
If there is a file named \fBinput\fR containing page headers of the form

.sp
.in +2
.nf
Page#
.fi
.in -2

.sp
.LP
and a file named \fBprogram\fR that contains

.sp
.in +2
.nf
/Page/{ $2 = n++; }
{ print }
.fi
.in -2

.sp
.LP
then the command line

.sp
.in +2
.nf
\fBnawk -f program n=5 input\fR
.fi
.in -2
.sp

.sp
.LP
prints the file \fBinput\fR, filling in page numbers starting at 5.

.SH ENVIRONMENT VARIABLES
.sp
.LP
See \fBenviron\fR(5) for descriptions of the following environment variables
that affect execution: \fBLC_COLLATE\fR, \fBLC_CTYPE\fR, \fBLC_MESSAGES\fR, and
\fBNLSPATH\fR.
.sp
.ne 2
.na
\fB\fBLC_NUMERIC\fR\fR
.ad
.RS 14n
Determine the radix character used when interpreting numeric input, performing
conversions between numeric and string values and formatting numeric output.
Regardless of locale, the period character (the decimal-point character of the
POSIX locale) is the decimal-point character recognized in processing \fBawk\fR
programs (including assignments in command-line arguments).
.RE

.SH EXIT STATUS
.sp
.LP
The following exit values are returned:
.sp
.ne 2
.na
\fB\fB0\fR\fR
.ad
.RS 6n
All input files were processed successfully.
.RE

.sp
.ne 2
.na
\fB\fB>0\fR\fR
.ad
.RS 6n
An error occurred.
.RE

.sp
.LP
The exit status can be altered within the program by using an \fBexit\fR
expression.

.SH SEE ALSO
.sp
.LP
\fBawk\fR(1), \fBed\fR(1), \fBegrep\fR(1), \fBgrep\fR(1), \fBlex\fR(1),
\fBsed\fR(1), \fBpopen\fR(3C), \fBprintf\fR(3C), \fBsystem\fR(3C),
\fBattributes\fR(5), \fBenviron\fR(5), \fBlargefile\fR(5), \fBregex\fR(5),
\fBXPG4\fR(5)
.sp
.LP
Aho, A. V., B. W. Kernighan, and P. J. Weinberger, \fIThe AWK Programming
Language\fR, Addison-Wesley, 1988.
.SH DIAGNOSTICS
.sp
.LP
If any \fIfile\fR operand is specified and the named file cannot be accessed,
\fBnawk\fR writes a diagnostic message to standard error and terminate without
any further action.
.sp
.LP
If the program specified by either the \fIprogram\fR operand or a
\fIprogfile\fR operand is not a valid \fBnawk\fR program (as specified in
\fBEXTENDED DESCRIPTION\fR), the behavior is undefined.
.SH NOTES
.sp
.LP
Input white space is not preserved on output if fields are involved.
.sp
.LP
There are no explicit conversions between numbers and strings. To force an
expression to be treated as a number add 0 to it; to force it to be treated as
a string concatenate the null string (\fB""\fR) to it.