xref: /titanic_52/usr/src/cmd/awk_xpg4/awk2.c (revision cb4658fbb85e4290093c4fea0eb396a7d98de1fb)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23*cb4658fbSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*cb4658fbSceastha  * Use is subject to license terms.
25*cb4658fbSceastha  */
26*cb4658fbSceastha 
27*cb4658fbSceastha /*
287c478bd9Sstevel@tonic-gate  * Copyright 1986, 1994 by Mortice Kern Systems Inc.  All rights reserved.
297c478bd9Sstevel@tonic-gate  */
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
327c478bd9Sstevel@tonic-gate 
33*cb4658fbSceastha /*
34*cb4658fbSceastha  * awk -- process input files, field extraction, output
35*cb4658fbSceastha  *
36*cb4658fbSceastha  * Based on MKS awk(1) ported to be /usr/xpg4/bin/awk with POSIX/XCU4 changes
37*cb4658fbSceastha  */
38*cb4658fbSceastha 
397c478bd9Sstevel@tonic-gate #include "awk.h"
407c478bd9Sstevel@tonic-gate #include "y.tab.h"
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate static FILE	*awkinfp;		/* Input file pointer */
437c478bd9Sstevel@tonic-gate static int	reclen;			/* Length of last record */
447c478bd9Sstevel@tonic-gate static int	exstat;			/* Exit status */
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate static FILE	*openfile(NODE *np, int flag, int fatal);
477c478bd9Sstevel@tonic-gate static FILE	*newfile(void);
487c478bd9Sstevel@tonic-gate static NODE	*nextarg(NODE **npp);
497c478bd9Sstevel@tonic-gate static void	adjust_buf(wchar_t **, int *, wchar_t **, char *, size_t);
507c478bd9Sstevel@tonic-gate static void	awk_putwc(wchar_t, FILE *);
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*
537c478bd9Sstevel@tonic-gate  * mainline for awk execution
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate void
567c478bd9Sstevel@tonic-gate awk()
577c478bd9Sstevel@tonic-gate {
587c478bd9Sstevel@tonic-gate 	running = 1;
597c478bd9Sstevel@tonic-gate 	dobegin();
607c478bd9Sstevel@tonic-gate 	while (nextrecord(linebuf, awkinfp) > 0)
617c478bd9Sstevel@tonic-gate 		execute(yytree);
627c478bd9Sstevel@tonic-gate 	doend(exstat);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate  * "cp" is the buffer to fill.  There is a special case if this buffer is
677c478bd9Sstevel@tonic-gate  * "linebuf" ($0)
687c478bd9Sstevel@tonic-gate  * Return 1 if OK, zero on EOF, -1 on error.
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate int
717c478bd9Sstevel@tonic-gate nextrecord(wchar_t *cp, FILE *fp)
727c478bd9Sstevel@tonic-gate {
737c478bd9Sstevel@tonic-gate 	wchar_t *ep = cp;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate nextfile:
767c478bd9Sstevel@tonic-gate 	if (fp == FNULL && (fp = newfile()) == FNULL)
777c478bd9Sstevel@tonic-gate 		return (0);
787c478bd9Sstevel@tonic-gate 	if ((*awkrecord)(ep, NLINE, fp) == NULL) {
797c478bd9Sstevel@tonic-gate 		if (fp == awkinfp) {
807c478bd9Sstevel@tonic-gate 			if (fp != stdin)
817c478bd9Sstevel@tonic-gate 				(void) fclose(awkinfp);
827c478bd9Sstevel@tonic-gate 			awkinfp = fp = FNULL;
837c478bd9Sstevel@tonic-gate 			goto nextfile;
847c478bd9Sstevel@tonic-gate 		}
857c478bd9Sstevel@tonic-gate 		if (ferror(fp))
867c478bd9Sstevel@tonic-gate 			return (-1);
877c478bd9Sstevel@tonic-gate 		return (0);
887c478bd9Sstevel@tonic-gate 	}
897c478bd9Sstevel@tonic-gate 	if (fp == awkinfp) {
907c478bd9Sstevel@tonic-gate 		if (varNR->n_flags & FINT)
91*cb4658fbSceastha 			++varNR->n_int;
92*cb4658fbSceastha 		else
937c478bd9Sstevel@tonic-gate 			(void) exprreduce(incNR);
947c478bd9Sstevel@tonic-gate 		if (varFNR->n_flags & FINT)
95*cb4658fbSceastha 			++varFNR->n_int;
96*cb4658fbSceastha 		else
977c478bd9Sstevel@tonic-gate 			(void) exprreduce(incFNR);
987c478bd9Sstevel@tonic-gate 	}
997c478bd9Sstevel@tonic-gate 	if (cp == linebuf) {
1007c478bd9Sstevel@tonic-gate 		lbuflen = reclen;
1017c478bd9Sstevel@tonic-gate 		splitdone = 0;
1027c478bd9Sstevel@tonic-gate 		if (needsplit)
1037c478bd9Sstevel@tonic-gate 			fieldsplit();
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	/* if record length is too long then bail out */
1067c478bd9Sstevel@tonic-gate 	if (reclen > NLINE - 2) {
1077c478bd9Sstevel@tonic-gate 		awkerr(gettext("Record too long (LIMIT: %d bytes)"),
1087c478bd9Sstevel@tonic-gate 		    NLINE - 1);
1097c478bd9Sstevel@tonic-gate 		/* Not Reached */
1107c478bd9Sstevel@tonic-gate 	}
1117c478bd9Sstevel@tonic-gate 	return (1);
1127c478bd9Sstevel@tonic-gate }
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate /*
115*cb4658fbSceastha  * isclvar()
116*cb4658fbSceastha  *
117*cb4658fbSceastha  * Returns 1 if the input string, arg, is a variable assignment,
118*cb4658fbSceastha  * otherwise returns 0.
119*cb4658fbSceastha  *
120*cb4658fbSceastha  * An argument to awk can be either a pathname of a file, or a variable
121*cb4658fbSceastha  * assignment.  An operand that begins with an undersore or alphabetic
122*cb4658fbSceastha  * character from the portable character set, followed by a sequence of
123*cb4658fbSceastha  * underscores, digits, and alphabetics from the portable character set,
124*cb4658fbSceastha  * followed by the '=' character, shall specify a variable assignment
125*cb4658fbSceastha  * rather than a pathname.
126*cb4658fbSceastha  */
127*cb4658fbSceastha int
128*cb4658fbSceastha isclvar(wchar_t *arg)
129*cb4658fbSceastha {
130*cb4658fbSceastha 	wchar_t	*tmpptr = arg;
131*cb4658fbSceastha 
132*cb4658fbSceastha 	if (tmpptr != NULL) {
133*cb4658fbSceastha 
134*cb4658fbSceastha 		/* Begins with an underscore or alphabetic character */
135*cb4658fbSceastha 		if (iswalpha(*tmpptr) || *tmpptr == '_') {
136*cb4658fbSceastha 
137*cb4658fbSceastha 			/*
138*cb4658fbSceastha 			 * followed by a sequence of underscores, digits,
139*cb4658fbSceastha 			 * and alphabetics
140*cb4658fbSceastha 			 */
141*cb4658fbSceastha 			for (tmpptr++; *tmpptr; tmpptr++) {
142*cb4658fbSceastha 				if (!(isalnum(*tmpptr) || (*tmpptr == '_'))) {
143*cb4658fbSceastha 					break;
144*cb4658fbSceastha 				}
145*cb4658fbSceastha 			}
146*cb4658fbSceastha 			return (*tmpptr == '=');
147*cb4658fbSceastha 		}
148*cb4658fbSceastha 	}
149*cb4658fbSceastha 
150*cb4658fbSceastha 	return (0);
151*cb4658fbSceastha }
152*cb4658fbSceastha 
153*cb4658fbSceastha /*
1547c478bd9Sstevel@tonic-gate  * Return the next file from the command line.
1557c478bd9Sstevel@tonic-gate  * Return FNULL when no more files.
1567c478bd9Sstevel@tonic-gate  * Sets awkinfp variable to the new current input file.
1577c478bd9Sstevel@tonic-gate  */
1587c478bd9Sstevel@tonic-gate static FILE *
1597c478bd9Sstevel@tonic-gate newfile()
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 	static int argindex = 1;
1627c478bd9Sstevel@tonic-gate 	static int filedone;
163*cb4658fbSceastha 	wchar_t *ap;
164*cb4658fbSceastha 	int argc;
1657c478bd9Sstevel@tonic-gate 	wchar_t *arg;
1667c478bd9Sstevel@tonic-gate 	extern void strescape(wchar_t *);
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	argc = (int)exprint(varARGC);
1697c478bd9Sstevel@tonic-gate 	for (;;) {
1707c478bd9Sstevel@tonic-gate 		if (argindex >= argc) {
1717c478bd9Sstevel@tonic-gate 			if (filedone)
1727c478bd9Sstevel@tonic-gate 				return (FNULL);
1737c478bd9Sstevel@tonic-gate 			++filedone;
1747c478bd9Sstevel@tonic-gate 			awkinfp = stdin;
1757c478bd9Sstevel@tonic-gate 			arg = M_MB_L("-");
1767c478bd9Sstevel@tonic-gate 			break;
1777c478bd9Sstevel@tonic-gate 		}
1787c478bd9Sstevel@tonic-gate 		constant->n_int = argindex++;
1797c478bd9Sstevel@tonic-gate 		arg = (wchar_t *)exprstring(ARGVsubi);
180*cb4658fbSceastha 		/*
181*cb4658fbSceastha 		 * If the argument contains a '=', determine if the
182*cb4658fbSceastha 		 * argument needs to be treated as a variable assignment
183*cb4658fbSceastha 		 * or as the pathname of a file.
184*cb4658fbSceastha 		 */
185*cb4658fbSceastha 		if (((ap = wcschr(arg, '=')) != NULL) && isclvar(arg)) {
1867c478bd9Sstevel@tonic-gate 			*ap = '\0';
1877c478bd9Sstevel@tonic-gate 			strescape(ap+1);
1887c478bd9Sstevel@tonic-gate 			strassign(vlook(arg), linebuf, FALLOC|FSENSE,
1897c478bd9Sstevel@tonic-gate 			    wcslen(linebuf));
1907c478bd9Sstevel@tonic-gate 			*ap = '=';
1917c478bd9Sstevel@tonic-gate 			continue;
1927c478bd9Sstevel@tonic-gate 		}
1937c478bd9Sstevel@tonic-gate 		if (arg[0] == '\0')
1947c478bd9Sstevel@tonic-gate 			continue;
1957c478bd9Sstevel@tonic-gate 		++filedone;
1967c478bd9Sstevel@tonic-gate 		if (arg[0] == '-' && arg[1] == '\0') {
1977c478bd9Sstevel@tonic-gate 			awkinfp = stdin;
1987c478bd9Sstevel@tonic-gate 			break;
1997c478bd9Sstevel@tonic-gate 		}
2007c478bd9Sstevel@tonic-gate 		if ((awkinfp = fopen(mbunconvert(arg), r)) == FNULL) {
2017c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("input file \"%s\""),
2027c478bd9Sstevel@tonic-gate 			    mbunconvert(arg));
2037c478bd9Sstevel@tonic-gate 			exstat = 1;
2047c478bd9Sstevel@tonic-gate 			continue;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 		break;
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	strassign(varFILENAME, arg, FALLOC, wcslen(arg));
2097c478bd9Sstevel@tonic-gate 	if (varFNR->n_flags & FINT)
210*cb4658fbSceastha 		varFNR->n_int = 0;
211*cb4658fbSceastha 	else
2127c478bd9Sstevel@tonic-gate 		(void) exprreduce(clrFNR);
2137c478bd9Sstevel@tonic-gate 	return (awkinfp);
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate 
2167c478bd9Sstevel@tonic-gate /*
2177c478bd9Sstevel@tonic-gate  * Default record reading code
2187c478bd9Sstevel@tonic-gate  * Uses fgets for potential speedups found in some (e.g. MKS)
2197c478bd9Sstevel@tonic-gate  * stdio packages.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate wchar_t *
2227c478bd9Sstevel@tonic-gate defrecord(wchar_t *bp, int lim, FILE *fp)
2237c478bd9Sstevel@tonic-gate {
224*cb4658fbSceastha 	wchar_t *endp;
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if (fgetws(bp, lim, fp) == NULL) {
2277c478bd9Sstevel@tonic-gate 		*bp = '\0';
2287c478bd9Sstevel@tonic-gate 		return (NULL);
2297c478bd9Sstevel@tonic-gate 	}
230*cb4658fbSceastha /*
231*cb4658fbSceastha  * XXXX
232*cb4658fbSceastha  *	switch (fgetws(bp, lim, fp)) {
233*cb4658fbSceastha  *	case M_FGETS_EOF:
234*cb4658fbSceastha  *		*bp = '\0';
235*cb4658fbSceastha  *		return (NULL);
236*cb4658fbSceastha  *	case M_FGETS_BINARY:
237*cb4658fbSceastha  *		awkerr(gettext("file is binary"));
238*cb4658fbSceastha  *	case M_FGETS_LONG:
239*cb4658fbSceastha  *		awkerr(gettext("line too long: limit %d"),
240*cb4658fbSceastha  *			lim);
241*cb4658fbSceastha  *	case M_FGETS_ERROR:
242*cb4658fbSceastha  *		awkperr(gettext("error reading file"));
243*cb4658fbSceastha  *	}
2447c478bd9Sstevel@tonic-gate  */
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	if (*(endp = (bp + (reclen = wcslen(bp))-1)) == '\n') {
2477c478bd9Sstevel@tonic-gate 		*endp = '\0';
2487c478bd9Sstevel@tonic-gate 		reclen--;
2497c478bd9Sstevel@tonic-gate 	}
2507c478bd9Sstevel@tonic-gate 	return (bp);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * Read a record separated by one character in the RS.
2557c478bd9Sstevel@tonic-gate  * Compatible calling sequence with fgets, but don't include
2567c478bd9Sstevel@tonic-gate  * record separator character in string.
2577c478bd9Sstevel@tonic-gate  */
2587c478bd9Sstevel@tonic-gate wchar_t *
2597c478bd9Sstevel@tonic-gate charrecord(wchar_t *abp, int alim, FILE *fp)
2607c478bd9Sstevel@tonic-gate {
261*cb4658fbSceastha 	wchar_t *bp;
262*cb4658fbSceastha 	wint_t c;
263*cb4658fbSceastha 	int limit = alim;
264*cb4658fbSceastha 	wint_t endc;
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate 	bp = abp;
2677c478bd9Sstevel@tonic-gate 	endc = *(wchar_t *)varRS->n_string;
2687c478bd9Sstevel@tonic-gate 	while (--limit > 0 && (c = getwc(fp)) != endc && c != WEOF)
2697c478bd9Sstevel@tonic-gate 		*bp++ = c;
2707c478bd9Sstevel@tonic-gate 	*bp = '\0';
2717c478bd9Sstevel@tonic-gate 	reclen = bp-abp;
2727c478bd9Sstevel@tonic-gate 	return (c == WEOF && bp == abp ? NULL : abp);
2737c478bd9Sstevel@tonic-gate }
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate /*
2767c478bd9Sstevel@tonic-gate  * Special routine for multiple line records.
2777c478bd9Sstevel@tonic-gate  */
2787c478bd9Sstevel@tonic-gate wchar_t *
2797c478bd9Sstevel@tonic-gate multirecord(wchar_t *abp, int limit, FILE *fp)
2807c478bd9Sstevel@tonic-gate {
281*cb4658fbSceastha 	wchar_t *bp;
282*cb4658fbSceastha 	int c;
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	while ((c = getwc(fp)) == '\n')
2857c478bd9Sstevel@tonic-gate 		;
2867c478bd9Sstevel@tonic-gate 	bp = abp;
2877c478bd9Sstevel@tonic-gate 	if (c != WEOF) do {
2887c478bd9Sstevel@tonic-gate 		if (--limit == 0)
2897c478bd9Sstevel@tonic-gate 			break;
2907c478bd9Sstevel@tonic-gate 		if (c == '\n' && bp[-1] == '\n')
2917c478bd9Sstevel@tonic-gate 			break;
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate 		*bp++ = c;
2947c478bd9Sstevel@tonic-gate 	} while ((c = getwc(fp)) != WEOF);
2957c478bd9Sstevel@tonic-gate 	*bp = '\0';
2967c478bd9Sstevel@tonic-gate 	if (bp > abp)
2977c478bd9Sstevel@tonic-gate 		*--bp = '\0';
2987c478bd9Sstevel@tonic-gate 	reclen = bp-abp;
2997c478bd9Sstevel@tonic-gate 	return (c == WEOF && bp == abp ? NULL : abp);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate /*
3037c478bd9Sstevel@tonic-gate  * Look for fields separated by spaces, tabs or newlines.
3047c478bd9Sstevel@tonic-gate  * Extract the next field, given pointer to start address.
3057c478bd9Sstevel@tonic-gate  * Return pointer to beginning of field or NULL.
3067c478bd9Sstevel@tonic-gate  * Reset end of field reference, which is the beginning of the
3077c478bd9Sstevel@tonic-gate  * next field.
3087c478bd9Sstevel@tonic-gate  */
3097c478bd9Sstevel@tonic-gate wchar_t *
3107c478bd9Sstevel@tonic-gate whitefield(wchar_t **endp)
3117c478bd9Sstevel@tonic-gate {
312*cb4658fbSceastha 	wchar_t *sp;
313*cb4658fbSceastha 	wchar_t *ep;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	sp = *endp;
3167c478bd9Sstevel@tonic-gate 	while (*sp == ' ' || *sp == '\t' || *sp == '\n')
3177c478bd9Sstevel@tonic-gate 		++sp;
3187c478bd9Sstevel@tonic-gate 	if (*sp == '\0')
3197c478bd9Sstevel@tonic-gate 		return (NULL);
320*cb4658fbSceastha 	for (ep = sp; *ep != ' ' && *ep != '\0' && *ep != '\t' &&
321*cb4658fbSceastha 	    *ep != '\n'; ++ep)
3227c478bd9Sstevel@tonic-gate 		;
3237c478bd9Sstevel@tonic-gate 	*endp = ep;
3247c478bd9Sstevel@tonic-gate 	return (sp);
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * Look for fields separated by non-whitespace characters.
3297c478bd9Sstevel@tonic-gate  * Same calling sequence as whitefield().
3307c478bd9Sstevel@tonic-gate  */
3317c478bd9Sstevel@tonic-gate wchar_t *
3327c478bd9Sstevel@tonic-gate blackfield(wchar_t **endp)
3337c478bd9Sstevel@tonic-gate {
334*cb4658fbSceastha 	wchar_t *cp;
335*cb4658fbSceastha 	int endc;
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	endc = *(wchar_t *)varFS->n_string;
3387c478bd9Sstevel@tonic-gate 	cp = *endp;
3397c478bd9Sstevel@tonic-gate 	if (*cp == '\0')
3407c478bd9Sstevel@tonic-gate 		return (NULL);
3417c478bd9Sstevel@tonic-gate 	if (*cp == endc && fcount != 0)
3427c478bd9Sstevel@tonic-gate 		cp++;
3437c478bd9Sstevel@tonic-gate 	if ((*endp = wcschr(cp, endc)) == NULL)
3447c478bd9Sstevel@tonic-gate 		*endp = wcschr(cp, '\0');
3457c478bd9Sstevel@tonic-gate 	return (cp);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@tonic-gate /*
3497c478bd9Sstevel@tonic-gate  * This field separation routine uses the same logic as
3507c478bd9Sstevel@tonic-gate  * blackfield but uses a regular expression to separate
3517c478bd9Sstevel@tonic-gate  * the fields.
3527c478bd9Sstevel@tonic-gate  */
3537c478bd9Sstevel@tonic-gate wchar_t *
3547c478bd9Sstevel@tonic-gate refield(wchar_t **endpp)
3557c478bd9Sstevel@tonic-gate {
356*cb4658fbSceastha 	wchar_t *cp, *start;
357*cb4658fbSceastha 	int flags;
3587c478bd9Sstevel@tonic-gate 	static	REGWMATCH_T match[10];
3597c478bd9Sstevel@tonic-gate 	int result;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	cp = *endpp;
3627c478bd9Sstevel@tonic-gate 	if (*cp == '\0') {
3637c478bd9Sstevel@tonic-gate 		match[0].rm_ep = NULL;
3647c478bd9Sstevel@tonic-gate 		return (NULL);
3657c478bd9Sstevel@tonic-gate 	}
3667c478bd9Sstevel@tonic-gate 	if (match[0].rm_ep != NULL) {
3677c478bd9Sstevel@tonic-gate 		flags = REG_NOTBOL;
3687c478bd9Sstevel@tonic-gate 		cp = (wchar_t *)match[0].rm_ep;
3697c478bd9Sstevel@tonic-gate 	} else
3707c478bd9Sstevel@tonic-gate 		flags = 0;
3717c478bd9Sstevel@tonic-gate 	start = cp;
3727c478bd9Sstevel@tonic-gate again:
3737c478bd9Sstevel@tonic-gate 	switch ((result = REGWEXEC(resep, cp, 10, match, flags))) {
3747c478bd9Sstevel@tonic-gate 	case REG_OK:
3757c478bd9Sstevel@tonic-gate 		/*
3767c478bd9Sstevel@tonic-gate 		 * Check to see if a null string was matched. If this is the
3777c478bd9Sstevel@tonic-gate 		 * case, then move the current pointer beyond this position.
3787c478bd9Sstevel@tonic-gate 		 */
3797c478bd9Sstevel@tonic-gate 		if (match[0].rm_sp == match[0].rm_ep) {
3807c478bd9Sstevel@tonic-gate 			cp = (wchar_t *)match[0].rm_sp;
3817c478bd9Sstevel@tonic-gate 			if (*cp++ != '\0') {
3827c478bd9Sstevel@tonic-gate 				goto again;
3837c478bd9Sstevel@tonic-gate 			}
3847c478bd9Sstevel@tonic-gate 		}
3857c478bd9Sstevel@tonic-gate 		*endpp = (wchar_t *)match[0].rm_sp;
3867c478bd9Sstevel@tonic-gate 		break;
3877c478bd9Sstevel@tonic-gate 	case REG_NOMATCH:
3887c478bd9Sstevel@tonic-gate 		match[0].rm_ep = NULL;
3897c478bd9Sstevel@tonic-gate 		*endpp = wcschr(cp, '\0');
3907c478bd9Sstevel@tonic-gate 		break;
3917c478bd9Sstevel@tonic-gate 	default:
392*cb4658fbSceastha 		(void) regerror(result, resep, (char *)linebuf,
393*cb4658fbSceastha 		    sizeof (linebuf));
3947c478bd9Sstevel@tonic-gate 		awkerr(gettext("error splitting record: %s"),
3957c478bd9Sstevel@tonic-gate 		    (char *)linebuf);
3967c478bd9Sstevel@tonic-gate 	}
3977c478bd9Sstevel@tonic-gate 	return (start);
3987c478bd9Sstevel@tonic-gate }
3997c478bd9Sstevel@tonic-gate 
4007c478bd9Sstevel@tonic-gate /*
4017c478bd9Sstevel@tonic-gate  * do begin processing
4027c478bd9Sstevel@tonic-gate  */
4037c478bd9Sstevel@tonic-gate void
4047c478bd9Sstevel@tonic-gate dobegin()
4057c478bd9Sstevel@tonic-gate {
406*cb4658fbSceastha 	/*
4077c478bd9Sstevel@tonic-gate 	 * Free all keyword nodes to save space.
4087c478bd9Sstevel@tonic-gate 	 */
4097c478bd9Sstevel@tonic-gate 	{
4107c478bd9Sstevel@tonic-gate 		NODE *np;
4117c478bd9Sstevel@tonic-gate 		int nbuck;
412*cb4658fbSceastha 		NODE *knp;
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 		np = NNULL;
4157c478bd9Sstevel@tonic-gate 		nbuck = 0;
4167c478bd9Sstevel@tonic-gate 		while ((knp = symwalk(&nbuck, &np)) != NNULL)
4177c478bd9Sstevel@tonic-gate 			if (knp->n_type == KEYWORD)
4187c478bd9Sstevel@tonic-gate 				delsymtab(knp, 1);
4197c478bd9Sstevel@tonic-gate 	}
420*cb4658fbSceastha 	/*
4217c478bd9Sstevel@tonic-gate 	 * Copy ENVIRON array only if needed.
4227c478bd9Sstevel@tonic-gate 	 * Note the convoluted work to assign to an array
4237c478bd9Sstevel@tonic-gate 	 * and that the temporary nodes will be freed by
4247c478bd9Sstevel@tonic-gate 	 * freetemps() because we are "running".
4257c478bd9Sstevel@tonic-gate 	 */
4267c478bd9Sstevel@tonic-gate 	if (needenviron) {
427*cb4658fbSceastha 		char **app;
428*cb4658fbSceastha 		wchar_t *name, *value;
429*cb4658fbSceastha 		NODE *namep = stringnode(_null, FSTATIC, 0);
430*cb4658fbSceastha 		NODE *valuep = stringnode(_null, FSTATIC, 0);
431*cb4658fbSceastha 		NODE *ENVsubname = node(INDEX, varENVIRON, namep);
4327c478bd9Sstevel@tonic-gate 		extern char **environ;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 		/* (void) m_setenv(); XXX what's this do? */
435*cb4658fbSceastha 		for (app = environ; *app != NULL; /* empty */) {
4367c478bd9Sstevel@tonic-gate 			name = mbstowcsdup(*app++);
4377c478bd9Sstevel@tonic-gate 
4387c478bd9Sstevel@tonic-gate 			if ((value = wcschr(name, '=')) != NULL) {
4397c478bd9Sstevel@tonic-gate 				*value++ = '\0';
4407c478bd9Sstevel@tonic-gate 				valuep->n_strlen = wcslen(value);
4417c478bd9Sstevel@tonic-gate 				valuep->n_string = value;
4427c478bd9Sstevel@tonic-gate 			} else {
4437c478bd9Sstevel@tonic-gate 				valuep->n_strlen = 0;
4447c478bd9Sstevel@tonic-gate 				valuep->n_string = _null;
4457c478bd9Sstevel@tonic-gate 			}
4467c478bd9Sstevel@tonic-gate 			namep->n_strlen = wcslen(namep->n_string = name);
4477c478bd9Sstevel@tonic-gate 			(void) assign(ENVsubname, valuep);
4487c478bd9Sstevel@tonic-gate 			if (value != NULL)
4497c478bd9Sstevel@tonic-gate 				value[-1] = '=';
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 	}
4527c478bd9Sstevel@tonic-gate 	phase = BEGIN;
4537c478bd9Sstevel@tonic-gate 	execute(yytree);
4547c478bd9Sstevel@tonic-gate 	phase = 0;
4557c478bd9Sstevel@tonic-gate 	if (npattern == 0)
4567c478bd9Sstevel@tonic-gate 		doend(0);
4577c478bd9Sstevel@tonic-gate 	/*
4587c478bd9Sstevel@tonic-gate 	 * Delete all pattern/action rules that are BEGIN at this
4597c478bd9Sstevel@tonic-gate 	 * point to save space.
4607c478bd9Sstevel@tonic-gate 	 * NOTE: this is not yet implemented.
4617c478bd9Sstevel@tonic-gate 	 */
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate /*
4657c478bd9Sstevel@tonic-gate  * Do end processing.
4667c478bd9Sstevel@tonic-gate  * Exit with a status
4677c478bd9Sstevel@tonic-gate  */
4687c478bd9Sstevel@tonic-gate void
4697c478bd9Sstevel@tonic-gate doend(int s)
4707c478bd9Sstevel@tonic-gate {
471*cb4658fbSceastha 	OFILE *op;
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	if (phase != END) {
4747c478bd9Sstevel@tonic-gate 		phase = END;
4757c478bd9Sstevel@tonic-gate 		awkinfp = stdin;
4767c478bd9Sstevel@tonic-gate 		execute(yytree);
4777c478bd9Sstevel@tonic-gate 	}
4787c478bd9Sstevel@tonic-gate 	for (op = &ofiles[0]; op < &ofiles[NIOSTREAM]; op++)
4797c478bd9Sstevel@tonic-gate 		if (op->f_fp != FNULL)
4807c478bd9Sstevel@tonic-gate 			awkclose(op);
4817c478bd9Sstevel@tonic-gate 	if (awkinfp == stdin)
4827c478bd9Sstevel@tonic-gate 		(void) fflush(awkinfp);
4837c478bd9Sstevel@tonic-gate 	exit(s);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate /*
4877c478bd9Sstevel@tonic-gate  * Print statement.
4887c478bd9Sstevel@tonic-gate  */
4897c478bd9Sstevel@tonic-gate void
4907c478bd9Sstevel@tonic-gate s_print(NODE *np)
4917c478bd9Sstevel@tonic-gate {
492*cb4658fbSceastha 	FILE *fp;
4937c478bd9Sstevel@tonic-gate 	NODE *listp;
494*cb4658fbSceastha 	char *ofs;
495*cb4658fbSceastha 	int notfirst = 0;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	fp = openfile(np->n_right, 1, 1);
4987c478bd9Sstevel@tonic-gate 	if (np->n_left == NNULL)
4997c478bd9Sstevel@tonic-gate 		(void) fputs(mbunconvert(linebuf), fp);
5007c478bd9Sstevel@tonic-gate 	else {
5017c478bd9Sstevel@tonic-gate 		ofs = wcstombsdup((isstring(varOFS->n_flags)) ?
5027c478bd9Sstevel@tonic-gate 		    (wchar_t *)varOFS->n_string :
5037c478bd9Sstevel@tonic-gate 		    (wchar_t *)exprstring(varOFS));
5047c478bd9Sstevel@tonic-gate 		listp = np->n_left;
5057c478bd9Sstevel@tonic-gate 		while ((np = getlist(&listp)) != NNULL) {
5067c478bd9Sstevel@tonic-gate 			if (notfirst++)
5077c478bd9Sstevel@tonic-gate 				(void) fputs(ofs, fp);
5087c478bd9Sstevel@tonic-gate 			np = exprreduce(np);
5097c478bd9Sstevel@tonic-gate 			if (np->n_flags & FINT)
5107c478bd9Sstevel@tonic-gate 				(void) fprintf(fp, "%lld", (INT)np->n_int);
5117c478bd9Sstevel@tonic-gate 			else if (isstring(np->n_flags))
512*cb4658fbSceastha 				(void) fprintf(fp, "%S", np->n_string);
5137c478bd9Sstevel@tonic-gate 			else
5147c478bd9Sstevel@tonic-gate 				(void) fprintf(fp,
5157c478bd9Sstevel@tonic-gate 				    mbunconvert((wchar_t *)exprstring(varOFMT)),
5167c478bd9Sstevel@tonic-gate 				    (double)np->n_real);
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 		free(ofs);
5197c478bd9Sstevel@tonic-gate 	}
5207c478bd9Sstevel@tonic-gate 	(void) fputs(mbunconvert(isstring(varORS->n_flags) ?
5217c478bd9Sstevel@tonic-gate 	    (wchar_t *)varORS->n_string : (wchar_t *)exprstring(varORS)),
5227c478bd9Sstevel@tonic-gate 	    fp);
5237c478bd9Sstevel@tonic-gate 	if (ferror(fp))
5247c478bd9Sstevel@tonic-gate 		awkperr("error on print");
5257c478bd9Sstevel@tonic-gate }
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /*
5287c478bd9Sstevel@tonic-gate  * printf statement.
5297c478bd9Sstevel@tonic-gate  */
5307c478bd9Sstevel@tonic-gate void
5317c478bd9Sstevel@tonic-gate s_prf(NODE *np)
5327c478bd9Sstevel@tonic-gate {
533*cb4658fbSceastha 	FILE *fp;
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	fp = openfile(np->n_right, 1, 1);
5367c478bd9Sstevel@tonic-gate 	(void) xprintf(np->n_left, fp, (wchar_t **)NULL);
5377c478bd9Sstevel@tonic-gate 	if (ferror(fp))
5387c478bd9Sstevel@tonic-gate 		awkperr("error on printf");
5397c478bd9Sstevel@tonic-gate }
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate /*
5427c478bd9Sstevel@tonic-gate  * Get next input line.
5437c478bd9Sstevel@tonic-gate  * Read into variable on left of node (or $0 if NULL).
5447c478bd9Sstevel@tonic-gate  * Read from pipe or file on right of node (or from regular
5457c478bd9Sstevel@tonic-gate  * input if NULL).
5467c478bd9Sstevel@tonic-gate  * This is an oddball inasmuch as it is a function
5477c478bd9Sstevel@tonic-gate  * but parses more like the keywords print, etc.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate NODE *
5507c478bd9Sstevel@tonic-gate f_getline(NODE *np)
5517c478bd9Sstevel@tonic-gate {
552*cb4658fbSceastha 	wchar_t *cp;
553*cb4658fbSceastha 	INT ret;
554*cb4658fbSceastha 	FILE *fp;
555*cb4658fbSceastha 	size_t len;
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate 	if (np->n_right == NULL && phase == END) {
5587c478bd9Sstevel@tonic-gate 		/* Pretend we've reached end of (the non-existant) file. */
559*cb4658fbSceastha 		return (intnode(0));
5607c478bd9Sstevel@tonic-gate 	}
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if ((fp = openfile(np->n_right, 0, 0)) != FNULL) {
5637c478bd9Sstevel@tonic-gate 		if (np->n_left == NNULL) {
5647c478bd9Sstevel@tonic-gate 			ret = nextrecord(linebuf, fp);
5657c478bd9Sstevel@tonic-gate 		} else {
5667c478bd9Sstevel@tonic-gate 			cp = emalloc(NLINE * sizeof (wchar_t));
5677c478bd9Sstevel@tonic-gate 			ret = nextrecord(cp, fp);
5687c478bd9Sstevel@tonic-gate 			np = np->n_left;
5697c478bd9Sstevel@tonic-gate 			len = wcslen(cp);
5707c478bd9Sstevel@tonic-gate 			cp = erealloc(cp, (len+1)*sizeof (wchar_t));
5717c478bd9Sstevel@tonic-gate 			if (isleaf(np->n_flags)) {
5727c478bd9Sstevel@tonic-gate 				if (np->n_type == PARM)
5737c478bd9Sstevel@tonic-gate 					np = np->n_next;
5747c478bd9Sstevel@tonic-gate 				strassign(np, cp, FNOALLOC, len);
5757c478bd9Sstevel@tonic-gate 			} else
576*cb4658fbSceastha 				(void) assign(np, stringnode(cp,
577*cb4658fbSceastha 				    FNOALLOC, len));
5787c478bd9Sstevel@tonic-gate 		}
5797c478bd9Sstevel@tonic-gate 	} else
5807c478bd9Sstevel@tonic-gate 		ret = -1;
5817c478bd9Sstevel@tonic-gate 	return (intnode(ret));
5827c478bd9Sstevel@tonic-gate }
5837c478bd9Sstevel@tonic-gate 
5847c478bd9Sstevel@tonic-gate /*
5857c478bd9Sstevel@tonic-gate  * Open a file.  Flag is non-zero for output.
5867c478bd9Sstevel@tonic-gate  */
5877c478bd9Sstevel@tonic-gate static FILE *
5887c478bd9Sstevel@tonic-gate openfile(NODE *np, int flag, int fatal)
5897c478bd9Sstevel@tonic-gate {
590*cb4658fbSceastha 	OFILE *op;
591*cb4658fbSceastha 	char *cp;
592*cb4658fbSceastha 	FILE *fp;
593*cb4658fbSceastha 	int type;
594*cb4658fbSceastha 	OFILE *fop;
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	if (np == NNULL) {
5977c478bd9Sstevel@tonic-gate 		if (flag)
5987c478bd9Sstevel@tonic-gate 			return (stdout);
5997c478bd9Sstevel@tonic-gate 		if (awkinfp == FNULL)
6007c478bd9Sstevel@tonic-gate 			awkinfp = newfile();
6017c478bd9Sstevel@tonic-gate 		return (awkinfp);
6027c478bd9Sstevel@tonic-gate 	}
6037c478bd9Sstevel@tonic-gate 	if ((type = np->n_type) == APPEND)
6047c478bd9Sstevel@tonic-gate 		type = WRITE;
6057c478bd9Sstevel@tonic-gate 	cp = mbunconvert(exprstring(np->n_left));
6067c478bd9Sstevel@tonic-gate 	fop = (OFILE *)NULL;
6077c478bd9Sstevel@tonic-gate 	for (op = &ofiles[0]; op < &ofiles[NIOSTREAM]; op++) {
6087c478bd9Sstevel@tonic-gate 		if (op->f_fp == FNULL) {
6097c478bd9Sstevel@tonic-gate 			if (fop == (OFILE *)NULL)
6107c478bd9Sstevel@tonic-gate 				fop = op;
6117c478bd9Sstevel@tonic-gate 			continue;
6127c478bd9Sstevel@tonic-gate 		}
613*cb4658fbSceastha 		if (op->f_mode == type && strcmp(op->f_name, cp) == 0)
6147c478bd9Sstevel@tonic-gate 			return (op->f_fp);
6157c478bd9Sstevel@tonic-gate 	}
6167c478bd9Sstevel@tonic-gate 	if (fop == (OFILE *)NULL)
6177c478bd9Sstevel@tonic-gate 		awkerr(gettext("too many open streams to %s onto \"%s\""),
6187c478bd9Sstevel@tonic-gate 		    flag ? "print/printf" : "getline", cp);
6197c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
6207c478bd9Sstevel@tonic-gate 	op = fop;
6217c478bd9Sstevel@tonic-gate 	if (cp[0] == '-' && cp[1] == '\0') {
6227c478bd9Sstevel@tonic-gate 		fp = flag ? stdout : stdin;
6237c478bd9Sstevel@tonic-gate 	} else {
6247c478bd9Sstevel@tonic-gate 		switch (np->n_type) {
6257c478bd9Sstevel@tonic-gate 		case WRITE:
6267c478bd9Sstevel@tonic-gate 			if ((fp = fopen(cp, w)) != FNULL) {
6277c478bd9Sstevel@tonic-gate 				if (isatty(fileno(fp)))
6287c478bd9Sstevel@tonic-gate 					(void) setvbuf(fp, 0, _IONBF, 0);
6297c478bd9Sstevel@tonic-gate 			}
6307c478bd9Sstevel@tonic-gate 			break;
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate 		case APPEND:
6337c478bd9Sstevel@tonic-gate 			fp = fopen(cp, "a");
6347c478bd9Sstevel@tonic-gate 			break;
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 		case PIPE:
6377c478bd9Sstevel@tonic-gate 			fp = popen(cp, w);
6387c478bd9Sstevel@tonic-gate 			(void) setvbuf(fp, (char *)0, _IOLBF, 0);
6397c478bd9Sstevel@tonic-gate 			break;
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate 		case PIPESYM:
6427c478bd9Sstevel@tonic-gate 			fp = popen(cp, r);
6437c478bd9Sstevel@tonic-gate 			break;
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 		case LT:
6467c478bd9Sstevel@tonic-gate 			fp = fopen(cp, r);
6477c478bd9Sstevel@tonic-gate 			break;
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 		default:
6507c478bd9Sstevel@tonic-gate 			awkerr(interr, "openfile");
6517c478bd9Sstevel@tonic-gate 		}
6527c478bd9Sstevel@tonic-gate 	}
6537c478bd9Sstevel@tonic-gate 	if (fp != FNULL) {
6547c478bd9Sstevel@tonic-gate 		op->f_name = strdup(cp);
6557c478bd9Sstevel@tonic-gate 		op->f_fp = fp;
6567c478bd9Sstevel@tonic-gate 		op->f_mode = type;
6577c478bd9Sstevel@tonic-gate 	} else if (fatal) {
6587c478bd9Sstevel@tonic-gate 		awkperr(flag ? gettext("output file \"%s\"") :
6597c478bd9Sstevel@tonic-gate 		    gettext("input file \"%s\""), cp);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate 	return (fp);
6627c478bd9Sstevel@tonic-gate }
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate /*
6657c478bd9Sstevel@tonic-gate  * Close a stream.
6667c478bd9Sstevel@tonic-gate  */
6677c478bd9Sstevel@tonic-gate void
6687c478bd9Sstevel@tonic-gate awkclose(OFILE *op)
6697c478bd9Sstevel@tonic-gate {
6707c478bd9Sstevel@tonic-gate 	if (op->f_mode == PIPE || op->f_mode == PIPESYM)
6717c478bd9Sstevel@tonic-gate 		(void) pclose(op->f_fp);
6727c478bd9Sstevel@tonic-gate 	else if (fclose(op->f_fp) == EOF)
6737c478bd9Sstevel@tonic-gate 		awkperr("error on stream \"%s\"", op->f_name);
6747c478bd9Sstevel@tonic-gate 	op->f_fp = FNULL;
6757c478bd9Sstevel@tonic-gate 	free(op->f_name);
6767c478bd9Sstevel@tonic-gate 	op->f_name = NULL;
6777c478bd9Sstevel@tonic-gate }
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate /*
6807c478bd9Sstevel@tonic-gate  * Internal routine common to printf, sprintf.
6817c478bd9Sstevel@tonic-gate  * The node is that describing the arguments.
6827c478bd9Sstevel@tonic-gate  * Returns the number of characters written to file
6837c478bd9Sstevel@tonic-gate  * pointer `fp' or the length of the string return
6847c478bd9Sstevel@tonic-gate  * in cp. If cp is NULL then the file pointer is used. If
6857c478bd9Sstevel@tonic-gate  * cp points to a string pointer, a pointer to an allocated
6867c478bd9Sstevel@tonic-gate  * buffer will be returned in it.
6877c478bd9Sstevel@tonic-gate  */
6887c478bd9Sstevel@tonic-gate size_t
6897c478bd9Sstevel@tonic-gate xprintf(NODE *np, FILE *fp, wchar_t **cp)
6907c478bd9Sstevel@tonic-gate {
691*cb4658fbSceastha 	wchar_t *fmt;
692*cb4658fbSceastha 	int c;
6937c478bd9Sstevel@tonic-gate 	wchar_t *bptr = (wchar_t *)NULL;
6947c478bd9Sstevel@tonic-gate 	char fmtbuf[40];
695*cb4658fbSceastha 	size_t length = 0;
696*cb4658fbSceastha 	char *ofmtp;
697*cb4658fbSceastha 	NODE *fnp;
698*cb4658fbSceastha 	wchar_t *fmtsave;
6997c478bd9Sstevel@tonic-gate 	int slen;
7007c478bd9Sstevel@tonic-gate 	int cplen;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	fnp = getlist(&np);
7037c478bd9Sstevel@tonic-gate 	if (isleaf(fnp->n_flags) && fnp->n_type == PARM)
7047c478bd9Sstevel@tonic-gate 		fnp = fnp->n_next;
7057c478bd9Sstevel@tonic-gate 	if (isstring(fnp->n_flags)) {
7067c478bd9Sstevel@tonic-gate 		fmt = fnp->n_string;
7077c478bd9Sstevel@tonic-gate 		fmtsave = NULL;
7087c478bd9Sstevel@tonic-gate 	} else
7097c478bd9Sstevel@tonic-gate 		fmtsave = fmt = (wchar_t *)strsave(exprstring(fnp));
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 	/*
7127c478bd9Sstevel@tonic-gate 	 * if a char * pointer has been passed in then allocate an initial
7137c478bd9Sstevel@tonic-gate 	 * buffer for the string. Make it LINE_MAX plus the length of
7147c478bd9Sstevel@tonic-gate 	 * the format string but do reallocs only based LINE_MAX.
7157c478bd9Sstevel@tonic-gate 	 */
7167c478bd9Sstevel@tonic-gate 	if (cp != (wchar_t **)NULL) {
7177c478bd9Sstevel@tonic-gate 		cplen = LINE_MAX;
7187c478bd9Sstevel@tonic-gate 		bptr = *cp = emalloc(sizeof (wchar_t) * (cplen + wcslen(fmt)));
7197c478bd9Sstevel@tonic-gate 	}
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	while ((c = *fmt++) != '\0') {
7227c478bd9Sstevel@tonic-gate 		if (c != '%') {
7237c478bd9Sstevel@tonic-gate 			if (bptr == (wchar_t *)NULL)
7247c478bd9Sstevel@tonic-gate 				awk_putwc(c, fp);
7257c478bd9Sstevel@tonic-gate 			else
7267c478bd9Sstevel@tonic-gate 				*bptr++ = c;
7277c478bd9Sstevel@tonic-gate 			++length;
7287c478bd9Sstevel@tonic-gate 			continue;
7297c478bd9Sstevel@tonic-gate 		}
7307c478bd9Sstevel@tonic-gate 		ofmtp = fmtbuf;
7317c478bd9Sstevel@tonic-gate 		*ofmtp++ = (char)c;
7327c478bd9Sstevel@tonic-gate 	nextc:
7337c478bd9Sstevel@tonic-gate 		switch (c = *fmt++) {
7347c478bd9Sstevel@tonic-gate 		case '%':
7357c478bd9Sstevel@tonic-gate 			if (bptr == (wchar_t *)NULL)
7367c478bd9Sstevel@tonic-gate 				awk_putwc(c, fp);
7377c478bd9Sstevel@tonic-gate 			else
7387c478bd9Sstevel@tonic-gate 				*bptr++ = c;
7397c478bd9Sstevel@tonic-gate 			++length;
7407c478bd9Sstevel@tonic-gate 			continue;
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 		case 'c':
7437c478bd9Sstevel@tonic-gate 			*ofmtp++ = 'w';
7447c478bd9Sstevel@tonic-gate 			*ofmtp++ = 'c';
7457c478bd9Sstevel@tonic-gate 			*ofmtp = '\0';
7467c478bd9Sstevel@tonic-gate 			fnp = exprreduce(nextarg(&np));
7477c478bd9Sstevel@tonic-gate 			if (isnumber(fnp->n_flags))
7487c478bd9Sstevel@tonic-gate 				c = exprint(fnp);
7497c478bd9Sstevel@tonic-gate 			else
7507c478bd9Sstevel@tonic-gate 				c = *(wchar_t *)exprstring(fnp);
7517c478bd9Sstevel@tonic-gate 			if (bptr == (wchar_t *)NULL)
7527c478bd9Sstevel@tonic-gate 				length += fprintf(fp, fmtbuf, c);
7537c478bd9Sstevel@tonic-gate 			else {
7547c478bd9Sstevel@tonic-gate 				/*
7557c478bd9Sstevel@tonic-gate 				 * Make sure that the buffer is long
7567c478bd9Sstevel@tonic-gate 				 * enough to hold the formatted string.
7577c478bd9Sstevel@tonic-gate 				 */
7587c478bd9Sstevel@tonic-gate 				adjust_buf(cp, &cplen, &bptr, fmtbuf, 0);
7597c478bd9Sstevel@tonic-gate 				/*
7607c478bd9Sstevel@tonic-gate 				 * Since the call to adjust_buf() has already
7617c478bd9Sstevel@tonic-gate 				 * guaranteed that the buffer will be long
7627c478bd9Sstevel@tonic-gate 				 * enough, just pass in INT_MAX as
7637c478bd9Sstevel@tonic-gate 				 * the length.
7647c478bd9Sstevel@tonic-gate 				 */
7657c478bd9Sstevel@tonic-gate 				(void) wsprintf(bptr, (const char *) fmtbuf, c);
7667c478bd9Sstevel@tonic-gate 				bptr += (slen = wcslen(bptr));
7677c478bd9Sstevel@tonic-gate 				length += slen;
7687c478bd9Sstevel@tonic-gate 			}
7697c478bd9Sstevel@tonic-gate 			continue;
7707c478bd9Sstevel@tonic-gate /* XXXX Is this bogus? Figure out what s & S mean - look at original code */
7717c478bd9Sstevel@tonic-gate 		case 's':
7727c478bd9Sstevel@tonic-gate 		case 'S':
7737c478bd9Sstevel@tonic-gate 			*ofmtp++ = 'w';
7747c478bd9Sstevel@tonic-gate 			*ofmtp++ = 's';
7757c478bd9Sstevel@tonic-gate 			*ofmtp = '\0';
7767c478bd9Sstevel@tonic-gate 			if (bptr == (wchar_t *)NULL)
7777c478bd9Sstevel@tonic-gate 				length += fprintf(fp, fmtbuf,
7787c478bd9Sstevel@tonic-gate 				    (wchar_t *)exprstring(nextarg(&np)));
7797c478bd9Sstevel@tonic-gate 			else {
7807c478bd9Sstevel@tonic-gate 				wchar_t *ts = exprstring(nextarg(&np));
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 				adjust_buf(cp, &cplen, &bptr, fmtbuf,
7837c478bd9Sstevel@tonic-gate 				    wcslen(ts));
7847c478bd9Sstevel@tonic-gate 				(void) wsprintf(bptr, (const char *) fmtbuf,
7857c478bd9Sstevel@tonic-gate 				    ts);
7867c478bd9Sstevel@tonic-gate 				bptr += (slen = wcslen(bptr));
7877c478bd9Sstevel@tonic-gate 				length += slen;
7887c478bd9Sstevel@tonic-gate 			}
7897c478bd9Sstevel@tonic-gate 			continue;
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 		case 'o':
7927c478bd9Sstevel@tonic-gate 		case 'O':
7937c478bd9Sstevel@tonic-gate 		case 'X':
7947c478bd9Sstevel@tonic-gate 		case 'x':
7957c478bd9Sstevel@tonic-gate 		case 'd':
7967c478bd9Sstevel@tonic-gate 		case 'i':
7977c478bd9Sstevel@tonic-gate 		case 'D':
7987c478bd9Sstevel@tonic-gate 		case 'U':
7997c478bd9Sstevel@tonic-gate 		case 'u':
8007c478bd9Sstevel@tonic-gate 			*ofmtp++ = 'l';
8017c478bd9Sstevel@tonic-gate 			*ofmtp++ = 'l'; /* now dealing with long longs */
8027c478bd9Sstevel@tonic-gate 			*ofmtp++ = c;
8037c478bd9Sstevel@tonic-gate 			*ofmtp = '\0';
8047c478bd9Sstevel@tonic-gate 			if (bptr == (wchar_t *)NULL)
8057c478bd9Sstevel@tonic-gate 				length += fprintf(fp, fmtbuf,
8067c478bd9Sstevel@tonic-gate 				    exprint(nextarg(&np)));
8077c478bd9Sstevel@tonic-gate 			else {
8087c478bd9Sstevel@tonic-gate 				adjust_buf(cp, &cplen, &bptr, fmtbuf, 0);
8097c478bd9Sstevel@tonic-gate 				(void) wsprintf(bptr, (const char *) fmtbuf,
8107c478bd9Sstevel@tonic-gate 				    exprint(nextarg(&np)));
8117c478bd9Sstevel@tonic-gate 				bptr += (slen = wcslen(bptr));
8127c478bd9Sstevel@tonic-gate 				length += slen;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 			continue;
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 		case 'e':
8177c478bd9Sstevel@tonic-gate 		case 'E':
8187c478bd9Sstevel@tonic-gate 		case 'f':
8197c478bd9Sstevel@tonic-gate 		case 'F':
8207c478bd9Sstevel@tonic-gate 		case 'g':
8217c478bd9Sstevel@tonic-gate 		case 'G':
8227c478bd9Sstevel@tonic-gate 			*ofmtp++ = c;
8237c478bd9Sstevel@tonic-gate 			*ofmtp = '\0';
8247c478bd9Sstevel@tonic-gate 			if (bptr == (wchar_t *)NULL)
8257c478bd9Sstevel@tonic-gate 				length += fprintf(fp, fmtbuf,
8267c478bd9Sstevel@tonic-gate 				    exprreal(nextarg(&np)));
8277c478bd9Sstevel@tonic-gate 			else {
8287c478bd9Sstevel@tonic-gate 				adjust_buf(cp, &cplen, &bptr, fmtbuf, 0);
8297c478bd9Sstevel@tonic-gate 				(void) wsprintf(bptr, (const char *) fmtbuf,
8307c478bd9Sstevel@tonic-gate 				    exprreal(nextarg(&np)));
8317c478bd9Sstevel@tonic-gate 				bptr += (slen = wcslen(bptr));
8327c478bd9Sstevel@tonic-gate 				length += slen;
8337c478bd9Sstevel@tonic-gate 			}
8347c478bd9Sstevel@tonic-gate 			continue;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 		case 'l':
8377c478bd9Sstevel@tonic-gate 		case 'L':
8387c478bd9Sstevel@tonic-gate 			break;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 		case '*':
8417c478bd9Sstevel@tonic-gate #ifdef M_BSD_SPRINTF
8427c478bd9Sstevel@tonic-gate 			sprintf(ofmtp, "%lld", (INT)exprint(nextarg(&np)));
8437c478bd9Sstevel@tonic-gate 			ofmtp += strlen(ofmtp);
8447c478bd9Sstevel@tonic-gate #else
845*cb4658fbSceastha 			ofmtp += sprintf(ofmtp, "%lld",
846*cb4658fbSceastha 			    (INT)exprint(nextarg(&np)));
8477c478bd9Sstevel@tonic-gate #endif
8487c478bd9Sstevel@tonic-gate 			break;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 		default:
8517c478bd9Sstevel@tonic-gate 			if (c == '\0') {
8527c478bd9Sstevel@tonic-gate 				*ofmtp = (wchar_t)NULL;
853*cb4658fbSceastha 				(void) fprintf(fp, "%s", fmtbuf);
8547c478bd9Sstevel@tonic-gate 				continue;
855*cb4658fbSceastha 			} else {
8567c478bd9Sstevel@tonic-gate 				*ofmtp++ = (wchar_t)c;
8577c478bd9Sstevel@tonic-gate 				break;
8587c478bd9Sstevel@tonic-gate 			}
8597c478bd9Sstevel@tonic-gate 		}
8607c478bd9Sstevel@tonic-gate 		goto nextc;
8617c478bd9Sstevel@tonic-gate 	}
8627c478bd9Sstevel@tonic-gate 	if (fmtsave != NULL)
8637c478bd9Sstevel@tonic-gate 		free(fmtsave);
8647c478bd9Sstevel@tonic-gate 	/*
8657c478bd9Sstevel@tonic-gate 	 * If printing to a character buffer then make sure it is
8667c478bd9Sstevel@tonic-gate 	 * null-terminated and only uses as much space as required.
8677c478bd9Sstevel@tonic-gate 	 */
8687c478bd9Sstevel@tonic-gate 	if (bptr != (wchar_t *)NULL) {
8697c478bd9Sstevel@tonic-gate 		*bptr = '\0';
8707c478bd9Sstevel@tonic-gate 		*cp = erealloc(*cp, (length+1) * sizeof (wchar_t));
8717c478bd9Sstevel@tonic-gate 	}
8727c478bd9Sstevel@tonic-gate 	return (length);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate /*
8767c478bd9Sstevel@tonic-gate  * Return the next argument from the list.
8777c478bd9Sstevel@tonic-gate  */
8787c478bd9Sstevel@tonic-gate static NODE *
8797c478bd9Sstevel@tonic-gate nextarg(NODE **npp)
8807c478bd9Sstevel@tonic-gate {
881*cb4658fbSceastha 	NODE *np;
8827c478bd9Sstevel@tonic-gate 
8837c478bd9Sstevel@tonic-gate 	if ((np = getlist(npp)) == NNULL)
8847c478bd9Sstevel@tonic-gate 		awkerr(gettext("insufficient arguments to printf or sprintf"));
8857c478bd9Sstevel@tonic-gate 	if (isleaf(np->n_flags) && np->n_type == PARM)
8867c478bd9Sstevel@tonic-gate 		return (np->n_next);
8877c478bd9Sstevel@tonic-gate 	return (np);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate /*
8927c478bd9Sstevel@tonic-gate  * Check and adjust the length of the buffer that has been passed in
8937c478bd9Sstevel@tonic-gate  * to make sure that it has space to accomodate the sequence string
8947c478bd9Sstevel@tonic-gate  * described in fmtstr. This routine is used by xprintf() to allow
8957c478bd9Sstevel@tonic-gate  * for arbitrarily long sprintf() strings.
8967c478bd9Sstevel@tonic-gate  *
8977c478bd9Sstevel@tonic-gate  * bp		= start of current buffer
8987c478bd9Sstevel@tonic-gate  * len		= length of current buffer
8997c478bd9Sstevel@tonic-gate  * offset	= offset in current buffer
9007c478bd9Sstevel@tonic-gate  * fmtstr	= format string to check
9017c478bd9Sstevel@tonic-gate  * slen		= size of string for %s formats
9027c478bd9Sstevel@tonic-gate  */
9037c478bd9Sstevel@tonic-gate static void
9047c478bd9Sstevel@tonic-gate adjust_buf(wchar_t **bp, int *len, wchar_t **offset, char *fmtstr, size_t slen)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	int ioff;
9077c478bd9Sstevel@tonic-gate 	int width = 0;
9087c478bd9Sstevel@tonic-gate 	int prec = 0;
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	do {
9117c478bd9Sstevel@tonic-gate 		fmtstr++;
912*cb4658fbSceastha 	} while (strchr("-+ 0", *fmtstr) != (char *)0 || *fmtstr == ('#'));
9137c478bd9Sstevel@tonic-gate 	if (*fmtstr != '*') {
9147c478bd9Sstevel@tonic-gate 		if (isdigit(*fmtstr)) {
9157c478bd9Sstevel@tonic-gate 			width = *fmtstr-'0';
9167c478bd9Sstevel@tonic-gate 			while (isdigit(*++fmtstr))
9177c478bd9Sstevel@tonic-gate 				width = width * 10 + *fmtstr - '0';
9187c478bd9Sstevel@tonic-gate 		}
9197c478bd9Sstevel@tonic-gate 	} else
9207c478bd9Sstevel@tonic-gate 		fmtstr++;
9217c478bd9Sstevel@tonic-gate 	if (*fmtstr == '.') {
9227c478bd9Sstevel@tonic-gate 		if (*++fmtstr != '*') {
9237c478bd9Sstevel@tonic-gate 			prec = *fmtstr-'0';
9247c478bd9Sstevel@tonic-gate 			while (isdigit(*++fmtstr))
9257c478bd9Sstevel@tonic-gate 				prec = prec * 10 + *fmtstr - '0';
9267c478bd9Sstevel@tonic-gate 		} else
9277c478bd9Sstevel@tonic-gate 			fmtstr++;
9287c478bd9Sstevel@tonic-gate 	}
9297c478bd9Sstevel@tonic-gate 	if (strchr("Llh", *fmtstr) != (char *)0)
9307c478bd9Sstevel@tonic-gate 		fmtstr++;
931*cb4658fbSceastha 	if (*fmtstr == 'S') {
9327c478bd9Sstevel@tonic-gate 		if (width && slen < width)
9337c478bd9Sstevel@tonic-gate 			slen = width;
9347c478bd9Sstevel@tonic-gate 		if (prec && slen > prec)
9357c478bd9Sstevel@tonic-gate 			slen = prec;
9367c478bd9Sstevel@tonic-gate 		width = slen+1;
9377c478bd9Sstevel@tonic-gate 	} else
9387c478bd9Sstevel@tonic-gate 		if (width == 0)
9397c478bd9Sstevel@tonic-gate 			width = NUMSIZE;
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate 	if (*offset+ width > *bp+ *len) {
9427c478bd9Sstevel@tonic-gate 		ioff = *offset-*bp;
9437c478bd9Sstevel@tonic-gate 		*len += width+1;
9447c478bd9Sstevel@tonic-gate 		*bp = erealloc(*bp, *len * sizeof (wchar_t));
9457c478bd9Sstevel@tonic-gate 		*offset = *bp+ioff;
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate }
9487c478bd9Sstevel@tonic-gate 
9497c478bd9Sstevel@tonic-gate static void
9507c478bd9Sstevel@tonic-gate awk_putwc(wchar_t c, FILE *fp)
9517c478bd9Sstevel@tonic-gate {
9527c478bd9Sstevel@tonic-gate 	char mb[MB_LEN_MAX];
9537c478bd9Sstevel@tonic-gate 	size_t mbl;
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 	if ((mbl = wctomb(mb, c)) > 0) {
9567c478bd9Sstevel@tonic-gate 		mb[mbl] = '\0';
9577c478bd9Sstevel@tonic-gate 		(void) fputs(mb, fp);
9587c478bd9Sstevel@tonic-gate 	} else
9597c478bd9Sstevel@tonic-gate 		awkerr(gettext("invalid wide character %x"), c);
9607c478bd9Sstevel@tonic-gate }
961