xref: /illumos-gate/usr/src/cmd/sgs/m4/common/m4.c (revision 4c56998a)
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
51dd08564Sab  * Common Development and Distribution License (the "License").
61dd08564Sab  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate 
227c478bd9Sstevel@tonic-gate /*
23*4c56998aSRich Burridge  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
269fb11590Smike_s /*	Copyright (c) 1988 AT&T	*/
279fb11590Smike_s /*	  All Rights Reserved  	*/
289fb11590Smike_s 
297c478bd9Sstevel@tonic-gate #include	<signal.h>
307c478bd9Sstevel@tonic-gate #include	<unistd.h>
317c478bd9Sstevel@tonic-gate #include	<fcntl.h>
327c478bd9Sstevel@tonic-gate #include	"m4.h"
337c478bd9Sstevel@tonic-gate 
341dd08564Sab #if defined(__lint)
351dd08564Sab extern int yydebug;
361dd08564Sab #endif
371dd08564Sab 
387c478bd9Sstevel@tonic-gate #define	match(c, s)	(c == *s && (!s[1] || inpmatch(s+1)))
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static char	tmp_name[] = "/tmp/m4aXXXXX";
417c478bd9Sstevel@tonic-gate static wchar_t	prev_char;
427c478bd9Sstevel@tonic-gate static int mb_cur_max;
437c478bd9Sstevel@tonic-gate 
447c478bd9Sstevel@tonic-gate static void getflags(int *, char ***, int *);
457c478bd9Sstevel@tonic-gate static void initalloc(void);
467c478bd9Sstevel@tonic-gate static void expand(wchar_t **, int);
477c478bd9Sstevel@tonic-gate static void lnsync(FILE *);
487c478bd9Sstevel@tonic-gate static void fpath(FILE *);
497c478bd9Sstevel@tonic-gate static void puttok(wchar_t *);
507c478bd9Sstevel@tonic-gate static void error3(void);
517c478bd9Sstevel@tonic-gate static wchar_t itochr(int);
521dd08564Sab /*LINTED: E_STATIC_UNUSED*/
537c478bd9Sstevel@tonic-gate static wchar_t *chkbltin(wchar_t *);
547c478bd9Sstevel@tonic-gate static wchar_t *inpmatch(wchar_t *);
557c478bd9Sstevel@tonic-gate static void chkspace(char **, int *, char ***);
567c478bd9Sstevel@tonic-gate static void catchsig(int);
577c478bd9Sstevel@tonic-gate static FILE *m4open(char ***, char *, int *);
587c478bd9Sstevel@tonic-gate static void showwrap(void);
597c478bd9Sstevel@tonic-gate static void sputchr(wchar_t, FILE *);
607c478bd9Sstevel@tonic-gate static void putchr(wchar_t);
617c478bd9Sstevel@tonic-gate static void *xcalloc(size_t, size_t);
627c478bd9Sstevel@tonic-gate static wint_t myfgetwc(FILE *, int);
637c478bd9Sstevel@tonic-gate static wint_t myfputwc(wchar_t, FILE *);
647c478bd9Sstevel@tonic-gate static int myfeof(int);
657c478bd9Sstevel@tonic-gate 
669fb11590Smike_s int
679fb11590Smike_s main(int argc, char **argv)
687c478bd9Sstevel@tonic-gate {
697c478bd9Sstevel@tonic-gate 	wchar_t t;
707c478bd9Sstevel@tonic-gate 	int i, opt_end = 0;
717c478bd9Sstevel@tonic-gate 	int sigs[] = {SIGHUP, SIGINT, SIGPIPE, 0};
727c478bd9Sstevel@tonic-gate 
731dd08564Sab #if defined(__lint)
741dd08564Sab 	yydebug = 0;
751dd08564Sab #endif
761dd08564Sab 
777c478bd9Sstevel@tonic-gate 	for (i = 0; sigs[i]; ++i) {
787c478bd9Sstevel@tonic-gate 		if (signal(sigs[i], SIG_IGN) != SIG_IGN)
797c478bd9Sstevel@tonic-gate 			(void) signal(sigs[i], catchsig);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 	tempfile = mktemp(tmp_name);
827c478bd9Sstevel@tonic-gate 	(void) close(creat(tempfile, 0));
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)	/* Should be defined by cc -D */
877c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
887c478bd9Sstevel@tonic-gate #endif
897c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	if ((mb_cur_max = MB_CUR_MAX) > 1)
927c478bd9Sstevel@tonic-gate 		wide = 1;
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	procnam = argv[0];
957c478bd9Sstevel@tonic-gate 	getflags(&argc, &argv, &opt_end);
967c478bd9Sstevel@tonic-gate 	initalloc();
977c478bd9Sstevel@tonic-gate 
987c478bd9Sstevel@tonic-gate 	setfname("-");
997c478bd9Sstevel@tonic-gate 	if (argc > 1) {
1007c478bd9Sstevel@tonic-gate 		--argc;
1017c478bd9Sstevel@tonic-gate 		++argv;
1027c478bd9Sstevel@tonic-gate 		if (strcmp(argv[0], "-")) {
1037c478bd9Sstevel@tonic-gate 			ifile[ifx] = m4open(&argv, "r", &argc);
1047c478bd9Sstevel@tonic-gate 			setfname(argv[0]);
1057c478bd9Sstevel@tonic-gate 		}
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	for (;;) {
1097c478bd9Sstevel@tonic-gate 		token[0] = t = getchr();
1107c478bd9Sstevel@tonic-gate 		token[1] = EOS;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		if (t == WEOF) {
1137c478bd9Sstevel@tonic-gate 			if (ifx > 0) {
1147c478bd9Sstevel@tonic-gate 				(void) fclose(ifile[ifx]);
1157c478bd9Sstevel@tonic-gate 				ipflr = ipstk[--ifx];
1167c478bd9Sstevel@tonic-gate 				continue;
1177c478bd9Sstevel@tonic-gate 			}
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 			getflags(&argc, &argv, &opt_end);
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 			if (argc <= 1)
1227c478bd9Sstevel@tonic-gate 				/*
1237c478bd9Sstevel@tonic-gate 				 * If dowrap() has been called, the m4wrap
1247c478bd9Sstevel@tonic-gate 				 * macro has been processed, and a linked
1257c478bd9Sstevel@tonic-gate 				 * list of m4wrap strings has been created.
1267c478bd9Sstevel@tonic-gate 				 * The list starts at wrapstart.
1277c478bd9Sstevel@tonic-gate 				 */
1287c478bd9Sstevel@tonic-gate 				if (wrapstart) {
1297c478bd9Sstevel@tonic-gate 					/*
1307c478bd9Sstevel@tonic-gate 					 * Now that EOF has been processed,
1317c478bd9Sstevel@tonic-gate 					 * display the m4wrap strings.
1327c478bd9Sstevel@tonic-gate 					 */
1337c478bd9Sstevel@tonic-gate 					showwrap();
1347c478bd9Sstevel@tonic-gate 					continue;
1357c478bd9Sstevel@tonic-gate 				} else
1367c478bd9Sstevel@tonic-gate 					break;
1377c478bd9Sstevel@tonic-gate 			--argc;
1387c478bd9Sstevel@tonic-gate 			++argv;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 			if (ifile[ifx] != stdin)
1417c478bd9Sstevel@tonic-gate 				(void) fclose(ifile[ifx]);
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 			if (strcmp(argv[0], "-"))
1447c478bd9Sstevel@tonic-gate 				ifile[ifx] = m4open(&argv, "r", &argc);
1457c478bd9Sstevel@tonic-gate 			else
1467c478bd9Sstevel@tonic-gate 				ifile[ifx] = stdin;
1477c478bd9Sstevel@tonic-gate 
1487c478bd9Sstevel@tonic-gate 			setfname(argv[0]);
1497c478bd9Sstevel@tonic-gate 			continue;
1507c478bd9Sstevel@tonic-gate 		}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 		if (is_alpha(t) || t == '_') {
1537c478bd9Sstevel@tonic-gate 			wchar_t	*tp = token+1;
1547c478bd9Sstevel@tonic-gate 			int tlim = toksize;
1557c478bd9Sstevel@tonic-gate 			struct nlist	*macadd;  /* temp variable */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 			while ((*tp = getchr()) != WEOF &&
1581dd08564Sab 			    (is_alnum(*tp) || *tp == '_')) {
1597c478bd9Sstevel@tonic-gate 				tp++;
1607c478bd9Sstevel@tonic-gate 				if (--tlim <= 0)
1617c478bd9Sstevel@tonic-gate 					error2(gettext(
1621dd08564Sab 					    "more than %d chars in word"),
1631dd08564Sab 					    toksize);
1647c478bd9Sstevel@tonic-gate 			}
1657c478bd9Sstevel@tonic-gate 			putbak(*tp);
1667c478bd9Sstevel@tonic-gate 			*tp = EOS;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 			macadd = lookup(token);
1697c478bd9Sstevel@tonic-gate 			*Ap = (wchar_t *)macadd;
1707c478bd9Sstevel@tonic-gate 			if (macadd->def) {
1717c478bd9Sstevel@tonic-gate 				if ((wchar_t *)(++Ap) >= astklm) {
1727c478bd9Sstevel@tonic-gate 					--Ap;
1737c478bd9Sstevel@tonic-gate 					error2(gettext(
1741dd08564Sab 					    "more than %d items on "
1751dd08564Sab 					    "argument stack"),
1761dd08564Sab 					    stksize);
1777c478bd9Sstevel@tonic-gate 				}
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 				if (Cp++ == NULL)
1807c478bd9Sstevel@tonic-gate 					Cp = callst;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 				Cp->argp = Ap;
1837c478bd9Sstevel@tonic-gate 				*Ap++ = op;
1847c478bd9Sstevel@tonic-gate 				puttok(token);
1857c478bd9Sstevel@tonic-gate 				stkchr(EOS);
1867c478bd9Sstevel@tonic-gate 				t = getchr();
1877c478bd9Sstevel@tonic-gate 				putbak(t);
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 				if (t != '(')
1907c478bd9Sstevel@tonic-gate 					pbstr(L"()");
1917c478bd9Sstevel@tonic-gate 				else	/* try to fix arg count */
1927c478bd9Sstevel@tonic-gate 					*Ap++ = op;
1937c478bd9Sstevel@tonic-gate 
1947c478bd9Sstevel@tonic-gate 				Cp->plev = 0;
1957c478bd9Sstevel@tonic-gate 			} else {
1967c478bd9Sstevel@tonic-gate 				puttok(token);
1977c478bd9Sstevel@tonic-gate 			}
1987c478bd9Sstevel@tonic-gate 		} else if (match(t, lquote)) {
1999fb11590Smike_s 			int	qlev = 1;
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate 			for (;;) {
2027c478bd9Sstevel@tonic-gate 				token[0] = t = getchr();
2037c478bd9Sstevel@tonic-gate 				token[1] = EOS;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 				if (match(t, rquote)) {
2067c478bd9Sstevel@tonic-gate 					if (--qlev > 0)
2077c478bd9Sstevel@tonic-gate 						puttok(token);
2087c478bd9Sstevel@tonic-gate 					else
2097c478bd9Sstevel@tonic-gate 						break;
2107c478bd9Sstevel@tonic-gate 				} else if (match(t, lquote)) {
2117c478bd9Sstevel@tonic-gate 					++qlev;
2127c478bd9Sstevel@tonic-gate 					puttok(token);
2137c478bd9Sstevel@tonic-gate 				} else {
2147c478bd9Sstevel@tonic-gate 					if (t == WEOF)
2157c478bd9Sstevel@tonic-gate 						error(gettext(
2167c478bd9Sstevel@tonic-gate 						"EOF in quote"));
2177c478bd9Sstevel@tonic-gate 					putchr(t);
2187c478bd9Sstevel@tonic-gate 				}
2197c478bd9Sstevel@tonic-gate 			}
2207c478bd9Sstevel@tonic-gate 		} else if (match(t, lcom) &&
2211dd08564Sab 		    ((lcom[0] != L'#' || lcom[1] != L'\0') ||
2221dd08564Sab 		    prev_char != '$')) {
2237c478bd9Sstevel@tonic-gate 
2247c478bd9Sstevel@tonic-gate 			/*
2257c478bd9Sstevel@tonic-gate 			 * Don't expand commented macro (between lcom and
2267c478bd9Sstevel@tonic-gate 			 * rcom).
2277c478bd9Sstevel@tonic-gate 			 * What we know so far is that we have found the
2287c478bd9Sstevel@tonic-gate 			 * left comment char (lcom).
2297c478bd9Sstevel@tonic-gate 			 * Make sure we haven't found '#' (lcom) immediately
2307c478bd9Sstevel@tonic-gate 			 * preceded by '$' because we want to expand "$#".
2317c478bd9Sstevel@tonic-gate 			 */
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 			puttok(token);
2347c478bd9Sstevel@tonic-gate 			for (;;) {
2357c478bd9Sstevel@tonic-gate 				token[0] = t = getchr();
2367c478bd9Sstevel@tonic-gate 				token[1] = EOS;
2377c478bd9Sstevel@tonic-gate 				if (match(t, rcom)) {
2387c478bd9Sstevel@tonic-gate 					puttok(token);
2397c478bd9Sstevel@tonic-gate 					break;
2407c478bd9Sstevel@tonic-gate 				} else {
2417c478bd9Sstevel@tonic-gate 					if (t == WEOF)
2427c478bd9Sstevel@tonic-gate 						error(gettext(
2437c478bd9Sstevel@tonic-gate 						"EOF in comment"));
2447c478bd9Sstevel@tonic-gate 					putchr(t);
2457c478bd9Sstevel@tonic-gate 				}
2467c478bd9Sstevel@tonic-gate 			}
2477c478bd9Sstevel@tonic-gate 		} else if (Cp == NULL) {
2487c478bd9Sstevel@tonic-gate 			putchr(t);
2497c478bd9Sstevel@tonic-gate 		} else if (t == '(') {
2507c478bd9Sstevel@tonic-gate 			if (Cp->plev)
2517c478bd9Sstevel@tonic-gate 				stkchr(t);
2527c478bd9Sstevel@tonic-gate 			else {
2537c478bd9Sstevel@tonic-gate 				/* skip white before arg */
2547c478bd9Sstevel@tonic-gate 				while ((t = getchr()) != WEOF && is_space(t))
2557c478bd9Sstevel@tonic-gate 					;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 				putbak(t);
2587c478bd9Sstevel@tonic-gate 			}
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			++Cp->plev;
2617c478bd9Sstevel@tonic-gate 		} else if (t == ')') {
2627c478bd9Sstevel@tonic-gate 			--Cp->plev;
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 			if (Cp->plev == 0) {
2657c478bd9Sstevel@tonic-gate 				stkchr(EOS);
2667c478bd9Sstevel@tonic-gate 				expand(Cp->argp, Ap-Cp->argp-1);
2677c478bd9Sstevel@tonic-gate 				op = *Cp->argp;
2687c478bd9Sstevel@tonic-gate 				Ap = Cp->argp-1;
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 				if (--Cp < callst)
2717c478bd9Sstevel@tonic-gate 					Cp = NULL;
2727c478bd9Sstevel@tonic-gate 			} else
2737c478bd9Sstevel@tonic-gate 				stkchr(t);
2747c478bd9Sstevel@tonic-gate 		} else if (t == ',' && Cp->plev <= 1) {
2757c478bd9Sstevel@tonic-gate 			stkchr(EOS);
2767c478bd9Sstevel@tonic-gate 			*Ap = op;
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 			if ((wchar_t *)(++Ap) >= astklm) {
2797c478bd9Sstevel@tonic-gate 				--Ap;
2807c478bd9Sstevel@tonic-gate 				error2(gettext(
2811dd08564Sab 				    "more than %d items on argument stack"),
2821dd08564Sab 				    stksize);
2837c478bd9Sstevel@tonic-gate 			}
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 			while ((t = getchr()) != WEOF && is_space(t))
2867c478bd9Sstevel@tonic-gate 				;
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 			putbak(t);
2897c478bd9Sstevel@tonic-gate 		} else {
2907c478bd9Sstevel@tonic-gate 			stkchr(t);
2917c478bd9Sstevel@tonic-gate 		}
2927c478bd9Sstevel@tonic-gate 	}
2937c478bd9Sstevel@tonic-gate 
2947c478bd9Sstevel@tonic-gate 	if (Cp != NULL)
2957c478bd9Sstevel@tonic-gate 		error(gettext(
2967c478bd9Sstevel@tonic-gate 		"EOF in argument list"));
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate 	delexit(exitstat, 1);
2997c478bd9Sstevel@tonic-gate 	return (0);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate static wchar_t *
3037c478bd9Sstevel@tonic-gate inpmatch(wchar_t *s)
3047c478bd9Sstevel@tonic-gate {
3057c478bd9Sstevel@tonic-gate 	wchar_t	*tp = token+1;
3067c478bd9Sstevel@tonic-gate 
3077c478bd9Sstevel@tonic-gate 	while (*s) {
3087c478bd9Sstevel@tonic-gate 		*tp = getchr();
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 		if (*tp++ != *s++) {
3117c478bd9Sstevel@tonic-gate 			*tp = EOS;
3127c478bd9Sstevel@tonic-gate 			pbstr(token+1);
3137c478bd9Sstevel@tonic-gate 			return (0);
3147c478bd9Sstevel@tonic-gate 		}
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate 
3177c478bd9Sstevel@tonic-gate 	*tp = EOS;
3187c478bd9Sstevel@tonic-gate 	return (token);
3197c478bd9Sstevel@tonic-gate }
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate static void
3227c478bd9Sstevel@tonic-gate getflags(int *xargc, char ***xargv, int *option_end)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	char	*arg;
3257c478bd9Sstevel@tonic-gate 	char *t;
3267c478bd9Sstevel@tonic-gate 	wchar_t *s[3];
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	while (*xargc > 1) {
3297c478bd9Sstevel@tonic-gate 		arg = (*xargv)[1]; /* point arg to current argument */
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		/*
3327c478bd9Sstevel@tonic-gate 		 * This argument is not an option if it equals "-" or if
3337c478bd9Sstevel@tonic-gate 		 * "--" has already been parsed.
3347c478bd9Sstevel@tonic-gate 		 */
3357c478bd9Sstevel@tonic-gate 		if (arg[0] != '-' || arg[1] == EOS || *option_end)
3367c478bd9Sstevel@tonic-gate 			break;
3377c478bd9Sstevel@tonic-gate 		if (arg[0] == '-' && arg[1] == '-' && arg[2] == '\0') {
3387c478bd9Sstevel@tonic-gate 			*option_end = 1;
3397c478bd9Sstevel@tonic-gate 		} else {
3407c478bd9Sstevel@tonic-gate 			switch (arg[1]) {
3417c478bd9Sstevel@tonic-gate 			case 'B':
3427c478bd9Sstevel@tonic-gate 				chkspace(&arg, xargc, xargv);
3437c478bd9Sstevel@tonic-gate 				bufsize = atoi(&arg[2]);
344*4c56998aSRich Burridge 				if (bufsize <= 0) {
345*4c56998aSRich Burridge 					bufsize = DEF_BUFSIZE;
346*4c56998aSRich Burridge 				}
3477c478bd9Sstevel@tonic-gate 				break;
3487c478bd9Sstevel@tonic-gate 			case 'D':
3497c478bd9Sstevel@tonic-gate 				initalloc();
3507c478bd9Sstevel@tonic-gate 				chkspace(&arg, xargc, xargv);
3517c478bd9Sstevel@tonic-gate 				for (t = &arg[2]; *t; t++) {
3527c478bd9Sstevel@tonic-gate 					if (*t == '=') {
3537c478bd9Sstevel@tonic-gate 						*t++ = EOS;
3547c478bd9Sstevel@tonic-gate 						break;
3557c478bd9Sstevel@tonic-gate 					}
3567c478bd9Sstevel@tonic-gate 				}
3577c478bd9Sstevel@tonic-gate 				s[1] = str2wstr(&arg[2], 1);
3587c478bd9Sstevel@tonic-gate 				s[2] = str2wstr(t, 1);
3597c478bd9Sstevel@tonic-gate 				dodef(&s[0], 2);
3607c478bd9Sstevel@tonic-gate 				free(s[1]);
3617c478bd9Sstevel@tonic-gate 				free(s[2]);
3627c478bd9Sstevel@tonic-gate 				break;
3637c478bd9Sstevel@tonic-gate 			case 'H':
3647c478bd9Sstevel@tonic-gate 				chkspace(&arg, xargc, xargv);
3657c478bd9Sstevel@tonic-gate 				hshsize = atoi(&arg[2]);
366*4c56998aSRich Burridge 				if (hshsize <= 0) {
367*4c56998aSRich Burridge 					hshsize = DEF_HSHSIZE;
368*4c56998aSRich Burridge 				}
3697c478bd9Sstevel@tonic-gate 				break;
3707c478bd9Sstevel@tonic-gate 			case 'S':
3717c478bd9Sstevel@tonic-gate 				chkspace(&arg, xargc, xargv);
3727c478bd9Sstevel@tonic-gate 				stksize = atoi(&arg[2]);
373*4c56998aSRich Burridge 				if (stksize <= 0) {
374*4c56998aSRich Burridge 					stksize = DEF_STKSIZE;
375*4c56998aSRich Burridge 				}
3767c478bd9Sstevel@tonic-gate 				break;
3777c478bd9Sstevel@tonic-gate 			case 'T':
3787c478bd9Sstevel@tonic-gate 				chkspace(&arg, xargc, xargv);
3797c478bd9Sstevel@tonic-gate 				toksize = atoi(&arg[2]);
380*4c56998aSRich Burridge 				if (toksize <= 0) {
381*4c56998aSRich Burridge 					toksize = DEF_TOKSIZE;
382*4c56998aSRich Burridge 				}
3837c478bd9Sstevel@tonic-gate 				break;
3847c478bd9Sstevel@tonic-gate 			case 'U':
3857c478bd9Sstevel@tonic-gate 				initalloc();
3867c478bd9Sstevel@tonic-gate 				chkspace(&arg, xargc, xargv);
3877c478bd9Sstevel@tonic-gate 				s[1] = str2wstr(&arg[2], 1);
3887c478bd9Sstevel@tonic-gate 				doundef(&s[0], 1);
3897c478bd9Sstevel@tonic-gate 				free(s[1]);
3907c478bd9Sstevel@tonic-gate 				break;
3917c478bd9Sstevel@tonic-gate 			case 'e':
3927c478bd9Sstevel@tonic-gate 				setbuf(stdout, NULL);
3937c478bd9Sstevel@tonic-gate 				(void) signal(SIGINT, SIG_IGN);
3947c478bd9Sstevel@tonic-gate 				break;
3957c478bd9Sstevel@tonic-gate 			case 's':
3967c478bd9Sstevel@tonic-gate 				/* turn on line sync */
3977c478bd9Sstevel@tonic-gate 				sflag = 1;
3987c478bd9Sstevel@tonic-gate 				break;
3997c478bd9Sstevel@tonic-gate 			default:
4007c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
4017c478bd9Sstevel@tonic-gate 				    gettext("%s: bad option: %s\n"),
4027c478bd9Sstevel@tonic-gate 				    procnam, arg);
4037c478bd9Sstevel@tonic-gate 				delexit(NOT_OK, 0);
4047c478bd9Sstevel@tonic-gate 			}
4057c478bd9Sstevel@tonic-gate 		} /* end else not "--" */
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate 		(*xargv)++;
4087c478bd9Sstevel@tonic-gate 		--(*xargc);
4097c478bd9Sstevel@tonic-gate 	} /* end while options to process */
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*
4137c478bd9Sstevel@tonic-gate  * Function: chkspace
4147c478bd9Sstevel@tonic-gate  *
4157c478bd9Sstevel@tonic-gate  * If there is a space between the option and its argument,
4167c478bd9Sstevel@tonic-gate  * adjust argptr so that &arg[2] will point to beginning of the option argument.
4177c478bd9Sstevel@tonic-gate  * This will ensure that processing in getflags() will work, because &arg[2]
4187c478bd9Sstevel@tonic-gate  * will point to the beginning of the option argument whether or not we have
4197c478bd9Sstevel@tonic-gate  * a space between the option and its argument.  If there is a space between
4207c478bd9Sstevel@tonic-gate  * the option and its argument, also adjust xargv and xargc because we are
4217c478bd9Sstevel@tonic-gate  * processing the next argument.
4227c478bd9Sstevel@tonic-gate  */
4237c478bd9Sstevel@tonic-gate static void
4247c478bd9Sstevel@tonic-gate chkspace(char **argptr, int *xargc, char ***xargv)
4257c478bd9Sstevel@tonic-gate {
4267c478bd9Sstevel@tonic-gate 	if ((*argptr)[2] == EOS) {
4277c478bd9Sstevel@tonic-gate 		/* there is a space between the option and its argument */
4287c478bd9Sstevel@tonic-gate 		(*xargv)++; /* look at the next argument */
4297c478bd9Sstevel@tonic-gate 		--(*xargc);
4307c478bd9Sstevel@tonic-gate 		/*
4317c478bd9Sstevel@tonic-gate 		 * Adjust argptr if the option is followed by an
4327c478bd9Sstevel@tonic-gate 		 * option argument.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 		if (*xargc > 1) {
4357c478bd9Sstevel@tonic-gate 			*argptr = (*xargv)[1];
4367c478bd9Sstevel@tonic-gate 			/* point &arg[2] to beginning of option argument */
4377c478bd9Sstevel@tonic-gate 			*argptr -= 2;
4387c478bd9Sstevel@tonic-gate 		}
4397c478bd9Sstevel@tonic-gate 	}
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate static void
4439fb11590Smike_s initalloc(void)
4447c478bd9Sstevel@tonic-gate {
4459fb11590Smike_s 	static int done = 0;
4469fb11590Smike_s 	int	t;
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	if (done++)
4497c478bd9Sstevel@tonic-gate 		return;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	hshtab = xcalloc(hshsize, sizeof (struct nlist *));
4527c478bd9Sstevel@tonic-gate 	callst = xcalloc(stksize/3+1, sizeof (struct call));
4537c478bd9Sstevel@tonic-gate 	Ap = argstk = xcalloc(stksize+3, sizeof (wchar_t *));
4547c478bd9Sstevel@tonic-gate 	ipstk[0] = ipflr = ip = ibuf = xcalloc(bufsize+1, sizeof (wchar_t));
4557c478bd9Sstevel@tonic-gate 	op = obuf = xcalloc(bufsize+1, sizeof (wchar_t));
4567c478bd9Sstevel@tonic-gate 	token = xcalloc(toksize+1, sizeof (wchar_t));
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	astklm = (wchar_t *)(&argstk[stksize]);
4597c478bd9Sstevel@tonic-gate 	ibuflm = &ibuf[bufsize];
4607c478bd9Sstevel@tonic-gate 	obuflm = &obuf[bufsize];
4617c478bd9Sstevel@tonic-gate 	toklm = &token[toksize];
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	for (t = 0; barray[t].bname; ++t) {
4647c478bd9Sstevel@tonic-gate 		wchar_t	p[2] = {0, EOS};
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 		p[0] = builtin(t);
4677c478bd9Sstevel@tonic-gate 		install(barray[t].bname, p, NOPUSH);
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	install(L"unix", nullstr, NOPUSH);
4707c478bd9Sstevel@tonic-gate }
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate void
4737c478bd9Sstevel@tonic-gate install(wchar_t *nam, wchar_t *val, int mode)
4747c478bd9Sstevel@tonic-gate {
4759fb11590Smike_s 	struct nlist *np;
4767c478bd9Sstevel@tonic-gate 	wchar_t	*cp;
4777c478bd9Sstevel@tonic-gate 	int		l;
4787c478bd9Sstevel@tonic-gate 
4797c478bd9Sstevel@tonic-gate 	if (mode == PUSH)
4807c478bd9Sstevel@tonic-gate 		(void) lookup(nam);	/* lookup sets hshval */
4817c478bd9Sstevel@tonic-gate 	else
4827c478bd9Sstevel@tonic-gate 		while (undef(nam))	/* undef calls lookup */
4837c478bd9Sstevel@tonic-gate 			;
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	np = xcalloc(1, sizeof (*np));
4867c478bd9Sstevel@tonic-gate 	np->name = wstrdup(nam);
4877c478bd9Sstevel@tonic-gate 	np->next = hshtab[hshval];
4887c478bd9Sstevel@tonic-gate 	hshtab[hshval] = np;
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate 	cp = xcalloc((l = wcslen(val))+1, sizeof (*val));
4917c478bd9Sstevel@tonic-gate 	np->def = cp;
4927c478bd9Sstevel@tonic-gate 	cp = &cp[l];
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	while (*val)
4957c478bd9Sstevel@tonic-gate 		*--cp = *val++;
4967c478bd9Sstevel@tonic-gate }
4977c478bd9Sstevel@tonic-gate 
4987c478bd9Sstevel@tonic-gate struct nlist *
4997c478bd9Sstevel@tonic-gate lookup(wchar_t *str)
5007c478bd9Sstevel@tonic-gate {
5017c478bd9Sstevel@tonic-gate 	wchar_t	*s1;
5029fb11590Smike_s 	struct nlist	*np;
5037c478bd9Sstevel@tonic-gate 	static struct nlist	nodef;
5047c478bd9Sstevel@tonic-gate 
5057c478bd9Sstevel@tonic-gate 	s1 = str;
5067c478bd9Sstevel@tonic-gate 
5077c478bd9Sstevel@tonic-gate 	for (hshval = 0; *s1; )
5087c478bd9Sstevel@tonic-gate 		hshval += *s1++;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	hshval %= hshsize;
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate 	for (np = hshtab[hshval]; np != NULL; np = np->next) {
5137c478bd9Sstevel@tonic-gate 		if (*str == *np->name && wcscmp(str, np->name) == 0)
5147c478bd9Sstevel@tonic-gate 			return (np);
5157c478bd9Sstevel@tonic-gate 	}
5167c478bd9Sstevel@tonic-gate 	return (&nodef);
5177c478bd9Sstevel@tonic-gate }
5187c478bd9Sstevel@tonic-gate 
5197c478bd9Sstevel@tonic-gate static void
5207c478bd9Sstevel@tonic-gate expand(wchar_t **a1, int c)
5217c478bd9Sstevel@tonic-gate {
5227c478bd9Sstevel@tonic-gate 	wchar_t	*dp;
5239fb11590Smike_s 	struct nlist	*sp;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	sp = (struct nlist *)a1[-1];
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 	if (sp->tflag || trace) {
5281dd08564Sab #if !defined(__lint)	/* lint doesn't grok "%ws" */
5297c478bd9Sstevel@tonic-gate 		int	i;
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
5327c478bd9Sstevel@tonic-gate 		    "Trace(%d): %ws", Cp-callst, a1[0]);
5331dd08564Sab #endif
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 		if (c > 0) {
5361dd08564Sab #if !defined(__lint)	/* lint doesn't grok "%ws" */
5377c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, "(%ws", chkbltin(a1[1]));
5387c478bd9Sstevel@tonic-gate 			for (i = 2; i <= c; ++i)
5397c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, ",%ws", chkbltin(a1[i]));
5401dd08564Sab #endif
5417c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, ")");
5427c478bd9Sstevel@tonic-gate 		}
5437c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, "\n");
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	dp = sp->def;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	for (; *dp; ++dp) {
5497c478bd9Sstevel@tonic-gate 		if (is_builtin(*dp)) {
5507c478bd9Sstevel@tonic-gate 			(*barray[builtin_idx(*dp)].bfunc)(a1, c);
5517c478bd9Sstevel@tonic-gate 		} else if (dp[1] == '$') {
5527c478bd9Sstevel@tonic-gate 			if (is_digit(*dp)) {
5539fb11590Smike_s 				int	n;
5547c478bd9Sstevel@tonic-gate 				if ((n = *dp-'0') <= c)
5557c478bd9Sstevel@tonic-gate 					pbstr(a1[n]);
5567c478bd9Sstevel@tonic-gate 				++dp;
5577c478bd9Sstevel@tonic-gate 			} else if (*dp == '#') {
5587c478bd9Sstevel@tonic-gate 				pbnum((long)c);
5597c478bd9Sstevel@tonic-gate 				++dp;
5607c478bd9Sstevel@tonic-gate 			} else if (*dp == '*' || *dp == '@') {
5619fb11590Smike_s 				int i = c;
5627c478bd9Sstevel@tonic-gate 				wchar_t **a = a1;
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 				if (i > 0)
5657c478bd9Sstevel@tonic-gate 					for (;;) {
5667c478bd9Sstevel@tonic-gate 						if (*dp == '@')
5677c478bd9Sstevel@tonic-gate 							pbstr(rquote);
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 						pbstr(a[i--]);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 						if (*dp == '@')
5727c478bd9Sstevel@tonic-gate 							pbstr(lquote);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 						if (i <= 0)
5757c478bd9Sstevel@tonic-gate 						break;
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 						pbstr(L",");
5787c478bd9Sstevel@tonic-gate 					}
5797c478bd9Sstevel@tonic-gate 				++dp;
5807c478bd9Sstevel@tonic-gate 			} else
5817c478bd9Sstevel@tonic-gate 				putbak(*dp);
5827c478bd9Sstevel@tonic-gate 		} else
5837c478bd9Sstevel@tonic-gate 			putbak(*dp);
5847c478bd9Sstevel@tonic-gate 	}
5857c478bd9Sstevel@tonic-gate }
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate void
5887c478bd9Sstevel@tonic-gate setfname(char *s)
5897c478bd9Sstevel@tonic-gate {
5907c478bd9Sstevel@tonic-gate 	if (fname[ifx])
5917c478bd9Sstevel@tonic-gate 		free(fname[ifx]);
5927c478bd9Sstevel@tonic-gate 	if ((fname[ifx] = strdup(s)) == NULL)
5937c478bd9Sstevel@tonic-gate 		error(gettext("out of storage"));
5947c478bd9Sstevel@tonic-gate 	fline[ifx] = 1;
5957c478bd9Sstevel@tonic-gate 	nflag = 1;
5967c478bd9Sstevel@tonic-gate 	lnsync(stdout);
5977c478bd9Sstevel@tonic-gate }
5987c478bd9Sstevel@tonic-gate 
5997c478bd9Sstevel@tonic-gate static void
6007c478bd9Sstevel@tonic-gate lnsync(FILE *iop)
6017c478bd9Sstevel@tonic-gate {
6027c478bd9Sstevel@tonic-gate 	static int cline = 0;
6037c478bd9Sstevel@tonic-gate 	static int cfile = 0;
6047c478bd9Sstevel@tonic-gate 
6057c478bd9Sstevel@tonic-gate 	if (!sflag || iop != stdout)
6067c478bd9Sstevel@tonic-gate 		return;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	if (nflag || ifx != cfile) {
6097c478bd9Sstevel@tonic-gate 		nflag = 0;
6107c478bd9Sstevel@tonic-gate 		cfile = ifx;
6117c478bd9Sstevel@tonic-gate 		(void) fprintf(iop, "#line %d \"", cline = fline[ifx]);
6127c478bd9Sstevel@tonic-gate 		fpath(iop);
6137c478bd9Sstevel@tonic-gate 		(void) fprintf(iop, "\"\n");
6147c478bd9Sstevel@tonic-gate 	} else if (++cline != fline[ifx])
6157c478bd9Sstevel@tonic-gate 		(void) fprintf(iop, "#line %d\n", cline = fline[ifx]);
6167c478bd9Sstevel@tonic-gate }
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate static void
6197c478bd9Sstevel@tonic-gate fpath(FILE *iop)
6207c478bd9Sstevel@tonic-gate {
6219fb11590Smike_s 	int	i;
6227c478bd9Sstevel@tonic-gate 
6237c478bd9Sstevel@tonic-gate 	if (fname[0] == NULL)
6247c478bd9Sstevel@tonic-gate 		return;
6257c478bd9Sstevel@tonic-gate 
6267c478bd9Sstevel@tonic-gate 	(void) fprintf(iop, "%s", fname[0]);
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	for (i = 1; i <= ifx; ++i)
6297c478bd9Sstevel@tonic-gate 		(void) fprintf(iop, ":%s", fname[i]);
6307c478bd9Sstevel@tonic-gate }
6317c478bd9Sstevel@tonic-gate 
6327c478bd9Sstevel@tonic-gate /* ARGSUSED */
6337c478bd9Sstevel@tonic-gate static void
6347c478bd9Sstevel@tonic-gate catchsig(int i)
6357c478bd9Sstevel@tonic-gate {
6367c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);
6377c478bd9Sstevel@tonic-gate 	(void) signal(SIGINT, SIG_IGN);
6387c478bd9Sstevel@tonic-gate 	delexit(NOT_OK, 0);
6397c478bd9Sstevel@tonic-gate }
6407c478bd9Sstevel@tonic-gate 
6417c478bd9Sstevel@tonic-gate void
6427c478bd9Sstevel@tonic-gate delexit(int code, int flushio)
6437c478bd9Sstevel@tonic-gate {
6449fb11590Smike_s 	int i;
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate 	cf = stdout;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate /*
6497c478bd9Sstevel@tonic-gate  *	if (ofx != 0) {
6507c478bd9Sstevel@tonic-gate  *		ofx = 0;
6517c478bd9Sstevel@tonic-gate  *		code = NOT_OK;
6527c478bd9Sstevel@tonic-gate  *	}
6537c478bd9Sstevel@tonic-gate  */
6547c478bd9Sstevel@tonic-gate 	ofx = 0;	/* ensure that everything comes out */
6557c478bd9Sstevel@tonic-gate 	for (i = 1; i < 10; i++)
6567c478bd9Sstevel@tonic-gate 		undiv(i, code);
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	tempfile[7] = 'a';
6597c478bd9Sstevel@tonic-gate 	(void) unlink(tempfile);
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate 	/* flush standard I/O buffers, ie: call exit() not _exit() */
6627c478bd9Sstevel@tonic-gate 	if (flushio)
6637c478bd9Sstevel@tonic-gate 		exit(code);
6647c478bd9Sstevel@tonic-gate 
6657c478bd9Sstevel@tonic-gate 	_exit(code);
6667c478bd9Sstevel@tonic-gate }
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate static void
6697c478bd9Sstevel@tonic-gate puttok(wchar_t *tp)
6707c478bd9Sstevel@tonic-gate {
6717c478bd9Sstevel@tonic-gate 	if (Cp) {
6727c478bd9Sstevel@tonic-gate 		while (*tp)
6737c478bd9Sstevel@tonic-gate 			stkchr(*tp++);
6747c478bd9Sstevel@tonic-gate 	} else if (cf) {
6757c478bd9Sstevel@tonic-gate 		while (*tp) {
6767c478bd9Sstevel@tonic-gate 			sputchr(*tp++, cf);
6777c478bd9Sstevel@tonic-gate 		}
6787c478bd9Sstevel@tonic-gate 	}
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate void
6827c478bd9Sstevel@tonic-gate pbstr(wchar_t *str)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	wchar_t *p;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 	for (p = str + wcslen(str); --p >= str; )
6877c478bd9Sstevel@tonic-gate 		putbak(*p);
6887c478bd9Sstevel@tonic-gate }
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate void
6917c478bd9Sstevel@tonic-gate undiv(int i, int code)
6927c478bd9Sstevel@tonic-gate {
6939fb11590Smike_s 	FILE *fp;
6947c478bd9Sstevel@tonic-gate 	wint_t c;
6957c478bd9Sstevel@tonic-gate 
6967c478bd9Sstevel@tonic-gate 	if (i < 1 || i > 9 || i == ofx || !ofile[i])
6977c478bd9Sstevel@tonic-gate 		return;
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 	(void) fclose(ofile[i]);
7007c478bd9Sstevel@tonic-gate 	tempfile[7] = 'a'+i;
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (code == OK && cf) {
7037c478bd9Sstevel@tonic-gate 		fp = xfopen(tempfile, "r");
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate 		if (wide) {
7067c478bd9Sstevel@tonic-gate 			while ((c = myfgetwc(fp, -1)) != WEOF)
7077c478bd9Sstevel@tonic-gate 				sputchr((wchar_t)c, cf);
7087c478bd9Sstevel@tonic-gate 		} else {
7097c478bd9Sstevel@tonic-gate 			while ((c = (wint_t)getc(fp)) != WEOF)
7107c478bd9Sstevel@tonic-gate 				sputchr((wchar_t)c, cf);
7117c478bd9Sstevel@tonic-gate 		}
7127c478bd9Sstevel@tonic-gate 
7137c478bd9Sstevel@tonic-gate 		(void) fclose(fp);
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	(void) unlink(tempfile);
7177c478bd9Sstevel@tonic-gate 	ofile[i] = NULL;
7187c478bd9Sstevel@tonic-gate }
7197c478bd9Sstevel@tonic-gate 
7207c478bd9Sstevel@tonic-gate void
7217c478bd9Sstevel@tonic-gate pbnum(long num)
7227c478bd9Sstevel@tonic-gate {
7237c478bd9Sstevel@tonic-gate 	pbnbr(num, 10, 1);
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate void
7277c478bd9Sstevel@tonic-gate pbnbr(long nbr, int base, int len)
7287c478bd9Sstevel@tonic-gate {
7299fb11590Smike_s 	int	neg = 0;
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (base <= 0)
7327c478bd9Sstevel@tonic-gate 		return;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (nbr < 0)
7357c478bd9Sstevel@tonic-gate 		neg = 1;
7367c478bd9Sstevel@tonic-gate 	else
7377c478bd9Sstevel@tonic-gate 		nbr = -nbr;
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	while (nbr < 0) {
7409fb11590Smike_s 		int	i;
7417c478bd9Sstevel@tonic-gate 		if (base > 1) {
7427c478bd9Sstevel@tonic-gate 			i = nbr%base;
7437c478bd9Sstevel@tonic-gate 			nbr /= base;
7447c478bd9Sstevel@tonic-gate #if (-3 % 2) != -1
7457c478bd9Sstevel@tonic-gate 			while (i > 0) {
7467c478bd9Sstevel@tonic-gate 				i -= base;
7477c478bd9Sstevel@tonic-gate 				++nbr;
7487c478bd9Sstevel@tonic-gate 			}
7497c478bd9Sstevel@tonic-gate #endif
7507c478bd9Sstevel@tonic-gate 			i = -i;
7517c478bd9Sstevel@tonic-gate 		} else {
7527c478bd9Sstevel@tonic-gate 			i = 1;
7537c478bd9Sstevel@tonic-gate 			++nbr;
7547c478bd9Sstevel@tonic-gate 		}
7557c478bd9Sstevel@tonic-gate 		putbak(itochr(i));
7567c478bd9Sstevel@tonic-gate 		--len;
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	while (--len >= 0)
7607c478bd9Sstevel@tonic-gate 		putbak('0');
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	if (neg)
7637c478bd9Sstevel@tonic-gate 		putbak('-');
7647c478bd9Sstevel@tonic-gate }
7657c478bd9Sstevel@tonic-gate 
7667c478bd9Sstevel@tonic-gate static wchar_t
7677c478bd9Sstevel@tonic-gate itochr(int i)
7687c478bd9Sstevel@tonic-gate {
7697c478bd9Sstevel@tonic-gate 	if (i > 9)
7707c478bd9Sstevel@tonic-gate 		return ((wchar_t)(i-10+'A'));
7717c478bd9Sstevel@tonic-gate 	else
7727c478bd9Sstevel@tonic-gate 		return ((wchar_t)(i+'0'));
7737c478bd9Sstevel@tonic-gate }
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate long
7767c478bd9Sstevel@tonic-gate ctol(wchar_t *str)
7777c478bd9Sstevel@tonic-gate {
7789fb11590Smike_s 	int sign;
7797c478bd9Sstevel@tonic-gate 	long num;
7807c478bd9Sstevel@tonic-gate 
7817c478bd9Sstevel@tonic-gate 	while (is_space(*str))
7827c478bd9Sstevel@tonic-gate 		++str;
7837c478bd9Sstevel@tonic-gate 	num = 0;
7847c478bd9Sstevel@tonic-gate 	if (*str == '-') {
7857c478bd9Sstevel@tonic-gate 		sign = -1;
7867c478bd9Sstevel@tonic-gate 		++str;
7877c478bd9Sstevel@tonic-gate 	} else
7887c478bd9Sstevel@tonic-gate 		sign = 1;
7897c478bd9Sstevel@tonic-gate 	while (is_digit(*str))
7907c478bd9Sstevel@tonic-gate 		num = num*10 + *str++ - '0';
7917c478bd9Sstevel@tonic-gate 	return (sign * num);
7927c478bd9Sstevel@tonic-gate }
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate int
7957c478bd9Sstevel@tonic-gate min(int a, int b)
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 	if (a > b)
7987c478bd9Sstevel@tonic-gate 		return (b);
7997c478bd9Sstevel@tonic-gate 	return (a);
8007c478bd9Sstevel@tonic-gate }
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate FILE *
8037c478bd9Sstevel@tonic-gate xfopen(char *name, char *mode)
8047c478bd9Sstevel@tonic-gate {
8057c478bd9Sstevel@tonic-gate 	FILE	*fp;
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	if ((fp = fopen(name, mode)) == NULL)
8087c478bd9Sstevel@tonic-gate 		error(gettext("can't open file"));
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate 	return (fp);
8117c478bd9Sstevel@tonic-gate }
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate /*
8147c478bd9Sstevel@tonic-gate  * m4open
8157c478bd9Sstevel@tonic-gate  *
8167c478bd9Sstevel@tonic-gate  * Continue processing files when unable to open the given file argument.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate FILE *
8197c478bd9Sstevel@tonic-gate m4open(char ***argvec, char *mode, int *argcnt)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	FILE	*fp;
8227c478bd9Sstevel@tonic-gate 	char *arg;
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate 	while (*argcnt > 0) {
8257c478bd9Sstevel@tonic-gate 		arg = (*argvec)[0]; /* point arg to current file name */
8267c478bd9Sstevel@tonic-gate 		if (arg[0] == '-' && arg[1] == EOS)
8277c478bd9Sstevel@tonic-gate 			return (stdin);
8287c478bd9Sstevel@tonic-gate 		else {
8297c478bd9Sstevel@tonic-gate 			if ((fp = fopen(arg, mode)) == NULL) {
8307c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext(
8317c478bd9Sstevel@tonic-gate 				"m4: cannot open %s: "), arg);
8327c478bd9Sstevel@tonic-gate 				perror("");
8337c478bd9Sstevel@tonic-gate 				if (*argcnt == 1) {
8347c478bd9Sstevel@tonic-gate 					/* last arg therefore exit */
8357c478bd9Sstevel@tonic-gate 					error3();
8367c478bd9Sstevel@tonic-gate 				} else {
8377c478bd9Sstevel@tonic-gate 					exitstat = 1;
8387c478bd9Sstevel@tonic-gate 					(*argvec)++; /* try next arg */
8397c478bd9Sstevel@tonic-gate 					(*argcnt)--;
8407c478bd9Sstevel@tonic-gate 				}
8417c478bd9Sstevel@tonic-gate 			} else
8427c478bd9Sstevel@tonic-gate 				break;
8437c478bd9Sstevel@tonic-gate 		}
8447c478bd9Sstevel@tonic-gate 	}
8457c478bd9Sstevel@tonic-gate 	return (fp);
8467c478bd9Sstevel@tonic-gate }
8477c478bd9Sstevel@tonic-gate 
8487c478bd9Sstevel@tonic-gate void *
8497c478bd9Sstevel@tonic-gate xmalloc(size_t size)
8507c478bd9Sstevel@tonic-gate {
8517c478bd9Sstevel@tonic-gate 	void *ptr;
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	if ((ptr = malloc(size)) == NULL)
8547c478bd9Sstevel@tonic-gate 		error(gettext("out of storage"));
8557c478bd9Sstevel@tonic-gate 	return (ptr);
8567c478bd9Sstevel@tonic-gate }
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate static void *
8597c478bd9Sstevel@tonic-gate xcalloc(size_t nbr, size_t size)
8607c478bd9Sstevel@tonic-gate {
8619fb11590Smike_s 	void	*ptr;
8627c478bd9Sstevel@tonic-gate 
8637c478bd9Sstevel@tonic-gate 	ptr = xmalloc(nbr * size);
8647c478bd9Sstevel@tonic-gate 	(void) memset(ptr, '\0', nbr * size);
8657c478bd9Sstevel@tonic-gate 	return (ptr);
8667c478bd9Sstevel@tonic-gate }
8677c478bd9Sstevel@tonic-gate 
8689fb11590Smike_s /* PRINTFLIKE1 */
8697c478bd9Sstevel@tonic-gate void
8707c478bd9Sstevel@tonic-gate error2(char *str, int num)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	char buf[500];
8737c478bd9Sstevel@tonic-gate 
8747c478bd9Sstevel@tonic-gate 	(void) snprintf(buf, sizeof (buf), str, num);
8757c478bd9Sstevel@tonic-gate 	error(buf);
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate void
8797c478bd9Sstevel@tonic-gate error(char *str)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "\n%s:", procnam);
8827c478bd9Sstevel@tonic-gate 	fpath(stderr);
8837c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, ":%d %s\n", fline[ifx], str);
8847c478bd9Sstevel@tonic-gate 	error3();
8857c478bd9Sstevel@tonic-gate }
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate static void
8887c478bd9Sstevel@tonic-gate error3()
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 	if (Cp) {
8919fb11590Smike_s 		struct call	*mptr;
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate 		/* fix limit */
8947c478bd9Sstevel@tonic-gate 		*op = EOS;
8957c478bd9Sstevel@tonic-gate 		(Cp+1)->argp = Ap+1;
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate 		for (mptr = callst; mptr <= Cp; ++mptr) {
8987c478bd9Sstevel@tonic-gate 			wchar_t	**aptr, **lim;
8997c478bd9Sstevel@tonic-gate 
9007c478bd9Sstevel@tonic-gate 			aptr = mptr->argp;
9017c478bd9Sstevel@tonic-gate 			lim = (mptr+1)->argp-1;
9027c478bd9Sstevel@tonic-gate 			if (mptr == callst)
9037c478bd9Sstevel@tonic-gate 				(void) fputws(*aptr, stderr);
9047c478bd9Sstevel@tonic-gate 			++aptr;
9057c478bd9Sstevel@tonic-gate 			(void) fputs("(", stderr);
9067c478bd9Sstevel@tonic-gate 			if (aptr < lim)
9077c478bd9Sstevel@tonic-gate 				for (;;) {
9087c478bd9Sstevel@tonic-gate 					(void) fputws(*aptr++, stderr);
9097c478bd9Sstevel@tonic-gate 					if (aptr >= lim)
9107c478bd9Sstevel@tonic-gate 						break;
9117c478bd9Sstevel@tonic-gate 					(void) fputs(",", stderr);
9127c478bd9Sstevel@tonic-gate 				}
9137c478bd9Sstevel@tonic-gate 		}
9147c478bd9Sstevel@tonic-gate 		while (--mptr >= callst)
9157c478bd9Sstevel@tonic-gate 			(void) fputs(")", stderr);
9167c478bd9Sstevel@tonic-gate 
9177c478bd9Sstevel@tonic-gate 		(void) fputs("\n", stderr);
9187c478bd9Sstevel@tonic-gate 	}
9197c478bd9Sstevel@tonic-gate 	delexit(NOT_OK, 1);
9207c478bd9Sstevel@tonic-gate }
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate static wchar_t *
9237c478bd9Sstevel@tonic-gate chkbltin(wchar_t *s)
9247c478bd9Sstevel@tonic-gate {
9257c478bd9Sstevel@tonic-gate 	static wchar_t buf[24];
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 	if (is_builtin(*s)) {
9287c478bd9Sstevel@tonic-gate 		(void) swprintf(buf, sizeof (buf)/sizeof (wchar_t), L"<%ls>",
9297c478bd9Sstevel@tonic-gate 		    barray[builtin_idx(*s)].bname);
9307c478bd9Sstevel@tonic-gate 		return (buf);
9317c478bd9Sstevel@tonic-gate 	}
9327c478bd9Sstevel@tonic-gate 	return (s);
9337c478bd9Sstevel@tonic-gate }
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate wchar_t
9367c478bd9Sstevel@tonic-gate getchr()
9377c478bd9Sstevel@tonic-gate {
9387c478bd9Sstevel@tonic-gate 	static wchar_t C;
9397c478bd9Sstevel@tonic-gate 
9407c478bd9Sstevel@tonic-gate 	prev_char = C;
9417c478bd9Sstevel@tonic-gate 	if (ip > ipflr)
9427c478bd9Sstevel@tonic-gate 		return (*--ip);
9437c478bd9Sstevel@tonic-gate 	if (wide) {
9447c478bd9Sstevel@tonic-gate 		C = (wchar_t)(myfeof(ifx) ? WEOF : myfgetwc(NULL, ifx));
9457c478bd9Sstevel@tonic-gate 	} else {
9467c478bd9Sstevel@tonic-gate 		C = (wchar_t)(feof(ifile[ifx]) ?
9477c478bd9Sstevel@tonic-gate 		    WEOF : (wint_t)getc(ifile[ifx]));
9487c478bd9Sstevel@tonic-gate 	}
9497c478bd9Sstevel@tonic-gate 	if (C == '\n')
9507c478bd9Sstevel@tonic-gate 		fline[ifx]++;
9517c478bd9Sstevel@tonic-gate 	return (C);
9527c478bd9Sstevel@tonic-gate }
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate /*
9557c478bd9Sstevel@tonic-gate  * showwrap
9567c478bd9Sstevel@tonic-gate  *
9577c478bd9Sstevel@tonic-gate  * Loop through the list of m4wrap strings.  Call pbstr() so that the
9587c478bd9Sstevel@tonic-gate  * string will be displayed, then delete the list entry and free the memory
9597c478bd9Sstevel@tonic-gate  * allocated for it.
9607c478bd9Sstevel@tonic-gate  */
9617c478bd9Sstevel@tonic-gate static void
9627c478bd9Sstevel@tonic-gate showwrap()
9637c478bd9Sstevel@tonic-gate {
9647c478bd9Sstevel@tonic-gate 	struct Wrap *prev;
9657c478bd9Sstevel@tonic-gate 
9667c478bd9Sstevel@tonic-gate 	while (wrapstart) {
9677c478bd9Sstevel@tonic-gate 		pbstr(wrapstart->wrapstr);
9687c478bd9Sstevel@tonic-gate 		free(wrapstart->wrapstr);
9697c478bd9Sstevel@tonic-gate 		prev = wrapstart;
9707c478bd9Sstevel@tonic-gate 		wrapstart = wrapstart->nxt;
9717c478bd9Sstevel@tonic-gate 		free(prev);
9727c478bd9Sstevel@tonic-gate 	}
9737c478bd9Sstevel@tonic-gate }
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate static void
9767c478bd9Sstevel@tonic-gate sputchr(wchar_t c, FILE *f)
9777c478bd9Sstevel@tonic-gate {
9787c478bd9Sstevel@tonic-gate 	wint_t ret;
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate 	if (is_builtin(c))
9817c478bd9Sstevel@tonic-gate 		return;
9827c478bd9Sstevel@tonic-gate 	if (wide)
9837c478bd9Sstevel@tonic-gate 		ret = myfputwc(c, f);
9847c478bd9Sstevel@tonic-gate 	else
9857c478bd9Sstevel@tonic-gate 		ret = (wint_t)putc((int)c, f);
9867c478bd9Sstevel@tonic-gate 	if (ret == WEOF)
9877c478bd9Sstevel@tonic-gate 		error(gettext("output error"));
9887c478bd9Sstevel@tonic-gate 	if (ret == '\n')
9897c478bd9Sstevel@tonic-gate 		lnsync(f);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
9927c478bd9Sstevel@tonic-gate static void
9937c478bd9Sstevel@tonic-gate putchr(wchar_t c)
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	wint_t ret;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (Cp)
9987c478bd9Sstevel@tonic-gate 		stkchr(c);
9997c478bd9Sstevel@tonic-gate 	else if (cf) {
10007c478bd9Sstevel@tonic-gate 		if (sflag)
10017c478bd9Sstevel@tonic-gate 			sputchr(c, cf);
10027c478bd9Sstevel@tonic-gate 		else {
10037c478bd9Sstevel@tonic-gate 			if (is_builtin(c))
10047c478bd9Sstevel@tonic-gate 				return;
10057c478bd9Sstevel@tonic-gate 			if (wide)
10067c478bd9Sstevel@tonic-gate 				ret = myfputwc(c, cf);
10077c478bd9Sstevel@tonic-gate 			else
10087c478bd9Sstevel@tonic-gate 				ret = (wint_t)putc((int)c, cf);
10097c478bd9Sstevel@tonic-gate 			if (ret == WEOF) {
10107c478bd9Sstevel@tonic-gate 				error(gettext("output error"));
10117c478bd9Sstevel@tonic-gate 			}
10127c478bd9Sstevel@tonic-gate 		}
10137c478bd9Sstevel@tonic-gate 	}
10147c478bd9Sstevel@tonic-gate }
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate wchar_t *
10177c478bd9Sstevel@tonic-gate wstrdup(wchar_t *p)
10187c478bd9Sstevel@tonic-gate {
10197c478bd9Sstevel@tonic-gate 	size_t len = wcslen(p);
10207c478bd9Sstevel@tonic-gate 	wchar_t *ret;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	ret = xmalloc((len + 1) * sizeof (wchar_t));
10237c478bd9Sstevel@tonic-gate 	(void) wcscpy(ret, p);
10247c478bd9Sstevel@tonic-gate 	return (ret);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate int
10287c478bd9Sstevel@tonic-gate wstoi(wchar_t *p)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	return ((int)wcstol(p, NULL, 10));
10317c478bd9Sstevel@tonic-gate }
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate char *
10347c478bd9Sstevel@tonic-gate wstr2str(wchar_t *from, int alloc)
10357c478bd9Sstevel@tonic-gate {
10367c478bd9Sstevel@tonic-gate 	static char *retbuf;
10377c478bd9Sstevel@tonic-gate 	static size_t bsiz;
10387c478bd9Sstevel@tonic-gate 	char *p, *ret;
10397c478bd9Sstevel@tonic-gate 
10407c478bd9Sstevel@tonic-gate 	if (alloc) {
10417c478bd9Sstevel@tonic-gate 		ret = p = xmalloc(wcslen(from) * mb_cur_max + 1);
10427c478bd9Sstevel@tonic-gate 	} else {
10437c478bd9Sstevel@tonic-gate 		while (bsiz < (wcslen(from) * mb_cur_max + 1)) {
10447c478bd9Sstevel@tonic-gate 			if ((p = realloc(retbuf, bsiz + 256)) == NULL)
10457c478bd9Sstevel@tonic-gate 				error(gettext("out of storage"));
10467c478bd9Sstevel@tonic-gate 			bsiz += 256;
10477c478bd9Sstevel@tonic-gate 			retbuf = p;
10487c478bd9Sstevel@tonic-gate 		}
10497c478bd9Sstevel@tonic-gate 		ret = p = retbuf;
10507c478bd9Sstevel@tonic-gate 	}
10517c478bd9Sstevel@tonic-gate 
10527c478bd9Sstevel@tonic-gate 	if (wide) {
10537c478bd9Sstevel@tonic-gate 		while (*from) {
10547c478bd9Sstevel@tonic-gate 			int len;
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate 			if (*from & INVALID_CHAR) {
10577c478bd9Sstevel@tonic-gate 				*p = (char)(*from & ~INVALID_CHAR);
10587c478bd9Sstevel@tonic-gate 				len = 1;
10597c478bd9Sstevel@tonic-gate 			} else {
10607c478bd9Sstevel@tonic-gate 				if ((len = wctomb(p, *from)) == -1) {
10617c478bd9Sstevel@tonic-gate 					*p = (char)*from;
10627c478bd9Sstevel@tonic-gate 					len = 1;
10637c478bd9Sstevel@tonic-gate 				}
10647c478bd9Sstevel@tonic-gate 			}
10657c478bd9Sstevel@tonic-gate 			p += len;
10667c478bd9Sstevel@tonic-gate 			from++;
10677c478bd9Sstevel@tonic-gate 		}
10687c478bd9Sstevel@tonic-gate 	} else {
10697c478bd9Sstevel@tonic-gate 		while (*from)
10707c478bd9Sstevel@tonic-gate 			*p++ = (char)*from++;
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate 	*p = '\0';
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 	return (ret);
10757c478bd9Sstevel@tonic-gate }
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate wchar_t *
10787c478bd9Sstevel@tonic-gate str2wstr(char *from, int alloc)
10797c478bd9Sstevel@tonic-gate {
10807c478bd9Sstevel@tonic-gate 	static wchar_t *retbuf;
10817c478bd9Sstevel@tonic-gate 	static size_t bsiz;
10827c478bd9Sstevel@tonic-gate 	wchar_t *p, *ret;
10837c478bd9Sstevel@tonic-gate 
10847c478bd9Sstevel@tonic-gate 	if (alloc) {
10857c478bd9Sstevel@tonic-gate 		ret = p = xmalloc((strlen(from) + 1) * sizeof (wchar_t));
10867c478bd9Sstevel@tonic-gate 	} else {
10877c478bd9Sstevel@tonic-gate 		while (bsiz < (strlen(from) + 1)) {
10887c478bd9Sstevel@tonic-gate 			if ((p = realloc(retbuf,
10897c478bd9Sstevel@tonic-gate 			    (bsiz + 256) * sizeof (wchar_t))) == NULL) {
10907c478bd9Sstevel@tonic-gate 				error(gettext("out of storage"));
10917c478bd9Sstevel@tonic-gate 			}
10927c478bd9Sstevel@tonic-gate 			bsiz += 256;
10937c478bd9Sstevel@tonic-gate 			retbuf = p;
10947c478bd9Sstevel@tonic-gate 		}
10957c478bd9Sstevel@tonic-gate 		ret = p = retbuf;
10967c478bd9Sstevel@tonic-gate 	}
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	if (wide) {
10997c478bd9Sstevel@tonic-gate 		while (*from) {
11007c478bd9Sstevel@tonic-gate 			int len;
11017c478bd9Sstevel@tonic-gate 			wchar_t wc;
11027c478bd9Sstevel@tonic-gate 
11037c478bd9Sstevel@tonic-gate 			if ((len = mbtowc(&wc, from, mb_cur_max)) <= 0) {
11047c478bd9Sstevel@tonic-gate 				wc = *from | INVALID_CHAR;
11057c478bd9Sstevel@tonic-gate 				len = 1;
11067c478bd9Sstevel@tonic-gate 			}
11077c478bd9Sstevel@tonic-gate 			*p++ = wc;
11087c478bd9Sstevel@tonic-gate 			from += len;
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 	} else {
11117c478bd9Sstevel@tonic-gate 		while (*from)
11127c478bd9Sstevel@tonic-gate 			*p++ = (unsigned char) *from++;
11137c478bd9Sstevel@tonic-gate 	}
11147c478bd9Sstevel@tonic-gate 	*p = 0;
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate 	return (ret);
11177c478bd9Sstevel@tonic-gate }
11187c478bd9Sstevel@tonic-gate 
11197c478bd9Sstevel@tonic-gate static wint_t
11207c478bd9Sstevel@tonic-gate myfgetwc(FILE *fp, int idx)
11217c478bd9Sstevel@tonic-gate {
11227c478bd9Sstevel@tonic-gate 	int i, c, len, nb;
11237c478bd9Sstevel@tonic-gate 	wchar_t wc;
11247c478bd9Sstevel@tonic-gate 	unsigned char *buf;
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate 	if (fp == NULL)
11277c478bd9Sstevel@tonic-gate 		fp = ifile[idx];
11287c478bd9Sstevel@tonic-gate 	else
11297c478bd9Sstevel@tonic-gate 		idx = 10; /* extra slot */
11307c478bd9Sstevel@tonic-gate 	buf = ibuffer[idx].buffer;
11317c478bd9Sstevel@tonic-gate 	nb = ibuffer[idx].nbytes;
11327c478bd9Sstevel@tonic-gate 	len = 0;
11337c478bd9Sstevel@tonic-gate 	for (i = 1; i <= mb_cur_max; i++) {
11347c478bd9Sstevel@tonic-gate 		if (nb < i) {
11357c478bd9Sstevel@tonic-gate 			c = getc(fp);
11367c478bd9Sstevel@tonic-gate 			if (c == EOF) {
11377c478bd9Sstevel@tonic-gate 				if (nb == 0)
11387c478bd9Sstevel@tonic-gate 					return (WEOF);
11397c478bd9Sstevel@tonic-gate 				else
11407c478bd9Sstevel@tonic-gate 					break;
11417c478bd9Sstevel@tonic-gate 			}
11427c478bd9Sstevel@tonic-gate 			buf[nb++] = (unsigned char)c;
11437c478bd9Sstevel@tonic-gate 		}
11447c478bd9Sstevel@tonic-gate 		if ((len = mbtowc(&wc, (char *)buf, i)) >= 0)
11457c478bd9Sstevel@tonic-gate 			break;
11467c478bd9Sstevel@tonic-gate 	}
11477c478bd9Sstevel@tonic-gate 	if (len <= 0) {
11487c478bd9Sstevel@tonic-gate 		wc = buf[0] | INVALID_CHAR;
11497c478bd9Sstevel@tonic-gate 		len = 1;
11507c478bd9Sstevel@tonic-gate 	}
11517c478bd9Sstevel@tonic-gate 	nb -= len;
11527c478bd9Sstevel@tonic-gate 	if (nb > 0) {
11537c478bd9Sstevel@tonic-gate 		for (i = 0; i < nb; i++)
11547c478bd9Sstevel@tonic-gate 			buf[i] = buf[i + len];
11557c478bd9Sstevel@tonic-gate 	}
11567c478bd9Sstevel@tonic-gate 	ibuffer[idx].nbytes = nb;
11577c478bd9Sstevel@tonic-gate 	return (wc);
11587c478bd9Sstevel@tonic-gate }
11597c478bd9Sstevel@tonic-gate 
11607c478bd9Sstevel@tonic-gate static wint_t
11617c478bd9Sstevel@tonic-gate myfputwc(wchar_t wc, FILE *fp)
11627c478bd9Sstevel@tonic-gate {
11637c478bd9Sstevel@tonic-gate 	if (wc & INVALID_CHAR) {
11647c478bd9Sstevel@tonic-gate 		wc &= ~INVALID_CHAR;
11657c478bd9Sstevel@tonic-gate 		return (fputc((int)wc, fp));
11667c478bd9Sstevel@tonic-gate 	}
11677c478bd9Sstevel@tonic-gate 	return (fputwc(wc, fp));
11687c478bd9Sstevel@tonic-gate }
11697c478bd9Sstevel@tonic-gate 
11707c478bd9Sstevel@tonic-gate static int
11717c478bd9Sstevel@tonic-gate myfeof(int idx)
11727c478bd9Sstevel@tonic-gate {
11737c478bd9Sstevel@tonic-gate 	return (ibuffer[idx].nbytes == 0 && feof(ifile[idx]));
11747c478bd9Sstevel@tonic-gate }
1175