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