17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
327c478bd9Sstevel@tonic-gate  * The Regents of the University of California
337c478bd9Sstevel@tonic-gate  * All Rights Reserved
347c478bd9Sstevel@tonic-gate  *
357c478bd9Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
367c478bd9Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
377c478bd9Sstevel@tonic-gate  * contributors.
387c478bd9Sstevel@tonic-gate  */
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate #include	<stdio.h>
437c478bd9Sstevel@tonic-gate #include	<sys/types.h>
447c478bd9Sstevel@tonic-gate #include	<fcntl.h>
457c478bd9Sstevel@tonic-gate #include	<stdlib.h>
467c478bd9Sstevel@tonic-gate #include	<string.h>
477c478bd9Sstevel@tonic-gate #include	<unistd.h>
487c478bd9Sstevel@tonic-gate #include	<errno.h>
497c478bd9Sstevel@tonic-gate #include	"curses_inc.h"
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #define	TERMPATH	"/usr/share/lib/terminfo/"
527c478bd9Sstevel@tonic-gate #define	TERMPATHLEN	512
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate extern	bool	_use_env;	/* in curses.c */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate chtype	bit_attributes[NUM_ATTRIBUTES] = {
577c478bd9Sstevel@tonic-gate 	    A_STANDOUT,
587c478bd9Sstevel@tonic-gate 	    A_UNDERLINE,
597c478bd9Sstevel@tonic-gate 	    A_ALTCHARSET,
607c478bd9Sstevel@tonic-gate 	    A_REVERSE,
617c478bd9Sstevel@tonic-gate 	    A_BLINK,
627c478bd9Sstevel@tonic-gate 	    A_DIM,
637c478bd9Sstevel@tonic-gate 	    A_BOLD,
647c478bd9Sstevel@tonic-gate 	    A_INVIS,
657c478bd9Sstevel@tonic-gate 	    A_PROTECT
667c478bd9Sstevel@tonic-gate 	};
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate char	*Def_term = "unknown",	/* default terminal type */
697c478bd9Sstevel@tonic-gate 	term_parm_err[32], ttytype[BUFSIZ], _frst_tblstr[1400];
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate TERMINAL		_first_term, *cur_term = &_first_term;
727c478bd9Sstevel@tonic-gate struct	_bool_struct	_frst_bools, *cur_bools = &_frst_bools;
737c478bd9Sstevel@tonic-gate struct	_num_struct	_frst_nums, *cur_nums = &_frst_nums;
747c478bd9Sstevel@tonic-gate struct	_str_struct	_frst_strs, *cur_strs = &_frst_strs;
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate /* _called_before is used/cleared by delterm.c and restart.c */
777c478bd9Sstevel@tonic-gate char	_called_before = 0;
787c478bd9Sstevel@tonic-gate short	term_errno = -1;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate #ifdef SYSV
817c478bd9Sstevel@tonic-gate int	prog_istermios = -1;
827c478bd9Sstevel@tonic-gate int	shell_istermios = -1;
837c478bd9Sstevel@tonic-gate #endif
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
867c478bd9Sstevel@tonic-gate extern	char	*boolfnames[], *boolnames[], *boolcodes[],
877c478bd9Sstevel@tonic-gate 		*numfnames[], *numnames[], *numcodes[],
887c478bd9Sstevel@tonic-gate 		*strfnames[], *strnames[], *strcodes[];
897c478bd9Sstevel@tonic-gate 
main(int argc,char ** argv)907c478bd9Sstevel@tonic-gate main(int argc, char **argv)	/* FOR DEBUG ONLY */
917c478bd9Sstevel@tonic-gate {
927c478bd9Sstevel@tonic-gate 	if (argc > 1)
937c478bd9Sstevel@tonic-gate 		setupterm(argv[1], 1, (int *)0);
947c478bd9Sstevel@tonic-gate 	else
957c478bd9Sstevel@tonic-gate 		setupterm((char *)0, 1, (int *)0);
967c478bd9Sstevel@tonic-gate 	return (0);
977c478bd9Sstevel@tonic-gate }
987c478bd9Sstevel@tonic-gate 
_Pr(int ch)997c478bd9Sstevel@tonic-gate _Pr(int ch)	/* FOR DEBUG ONLY */
1007c478bd9Sstevel@tonic-gate {
1017c478bd9Sstevel@tonic-gate 	if (ch >= 0200) {
1027c478bd9Sstevel@tonic-gate 		printf("M-");
1037c478bd9Sstevel@tonic-gate 		ch -= 0200;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	if ((ch < ' ') || (ch == 0177))
1067c478bd9Sstevel@tonic-gate 		printf("^%c", ch ^ 0100);
1077c478bd9Sstevel@tonic-gate 	else
1087c478bd9Sstevel@tonic-gate 		printf("%c", ch);
1097c478bd9Sstevel@tonic-gate }
1107c478bd9Sstevel@tonic-gate 
_Sprint(int n,char * string)1117c478bd9Sstevel@tonic-gate _Sprint(int n, char *string)	/* FOR DEBUG ONLY */
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	int	ch;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	if (n == -1) {
1167c478bd9Sstevel@tonic-gate 		printf(".\n");
1177c478bd9Sstevel@tonic-gate 		return;
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	printf(", string = '");
1207c478bd9Sstevel@tonic-gate 	while (ch = *string++)
1217c478bd9Sstevel@tonic-gate 		_Pr(ch&0377);
1227c478bd9Sstevel@tonic-gate 
1237c478bd9Sstevel@tonic-gate 	printf("'.\n");
1247c478bd9Sstevel@tonic-gate }
1257c478bd9Sstevel@tonic-gate 
_Mprint(int n,char * memory)1267c478bd9Sstevel@tonic-gate _Mprint(int n, char *memory)	/* FOR DEBUG ONLY */
1277c478bd9Sstevel@tonic-gate {
1287c478bd9Sstevel@tonic-gate 	unsigned	char	ch;
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	while (ch = *memory++, n-- > 0)
1317c478bd9Sstevel@tonic-gate 		_Pr(ch&0377);
1327c478bd9Sstevel@tonic-gate }
1337c478bd9Sstevel@tonic-gate 
1347c478bd9Sstevel@tonic-gate #define	_Vr2getshi()	_Vr2getsh(ip-2)
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate #if	vax || pdp11
1377c478bd9Sstevel@tonic-gate #define	_Vr2getsh(ip)	(* (short *)(ip))
1387c478bd9Sstevel@tonic-gate #endif	/* vax || pdp11 */
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate #ifndef	_Vr2getsh
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Here is a more portable version, which does not assume byte ordering
1437c478bd9Sstevel@tonic-gate  * in shorts, sign extension, etc.
1447c478bd9Sstevel@tonic-gate  */
_Vr2getsh(char * p)1457c478bd9Sstevel@tonic-gate _Vr2getsh(char *p)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	int	rv;
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 	if (*p == (char)0377)
1507c478bd9Sstevel@tonic-gate 		return (-1);
1517c478bd9Sstevel@tonic-gate 	rv = (unsigned char) *p++;
1527c478bd9Sstevel@tonic-gate 	rv += (unsigned char) *p * 256;
1537c478bd9Sstevel@tonic-gate 	return (rv);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate #endif	/* _Vr2getsh */
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
1587c478bd9Sstevel@tonic-gate 
1597c478bd9Sstevel@tonic-gate #define	_Getshi()	_Getsh(ip); ip += 2
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
1627c478bd9Sstevel@tonic-gate  * "function" to get a short from a pointer.  The short is in a standard
1637c478bd9Sstevel@tonic-gate  * format: two bytes, the first is the low order byte, the second is
1647c478bd9Sstevel@tonic-gate  * the high order byte (base 256).  The only negative numbers allowed are
1657c478bd9Sstevel@tonic-gate  * -1 and -2, which are represented as 255,255 and 255,254  This format
1667c478bd9Sstevel@tonic-gate  * happens to be the same as the hardware on the pdp-11, vax, and 386,
1677c478bd9Sstevel@tonic-gate  * making it fast and convenient and small to do this on a pdp-11.
1687c478bd9Sstevel@tonic-gate  */
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate #if	vax || pdp11 || i386
1717c478bd9Sstevel@tonic-gate #define	_Getsh(ip)	(* (short *)ip)
1727c478bd9Sstevel@tonic-gate #endif	/* vax || pdp11 */
1737c478bd9Sstevel@tonic-gate /*
1747c478bd9Sstevel@tonic-gate  * The following macro is partly due to Mike Laman, laman@sdcsvax
1757c478bd9Sstevel@tonic-gate  *	NCR @ Torrey Pines.		- Tony Hansen
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate #if	u3b || u3b15 || u3b2 || m68000
1787c478bd9Sstevel@tonic-gate #define	_Getsh(ip)	((short)(*((unsigned char *)ip) | (*(ip+1) << 8)))
1797c478bd9Sstevel@tonic-gate #endif	/* u3b || u3b15 || u3b2 || m68000 */
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate #ifndef	_Getsh
1827c478bd9Sstevel@tonic-gate /*
1837c478bd9Sstevel@tonic-gate  * Here is a more portable version, which does not assume byte ordering
1847c478bd9Sstevel@tonic-gate  * in shorts, sign extension, etc.  For the sake of the porters,
1857c478bd9Sstevel@tonic-gate  * two alternative implementations, for the machines that don't have
1867c478bd9Sstevel@tonic-gate  * casting to "unsigned char", are also given, but commented out.
1877c478bd9Sstevel@tonic-gate  * Not ANSI C implementation assumes that the * C preprocessor does
1887c478bd9Sstevel@tonic-gate  * sign-extension the same as on the machine being compiled for.
1897c478bd9Sstevel@tonic-gate  */
1907c478bd9Sstevel@tonic-gate static int
_Getsh(char * p)1917c478bd9Sstevel@tonic-gate _Getsh(char *p)
1927c478bd9Sstevel@tonic-gate {
1937c478bd9Sstevel@tonic-gate 	int	rv, rv2;
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	rv  = (unsigned char) p[0];
1967c478bd9Sstevel@tonic-gate 	rv2 = (unsigned char) p[1];
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	/* the following stuff is only for porting.  See the comment above */
1997c478bd9Sstevel@tonic-gate 
2007c478bd9Sstevel@tonic-gate #ifdef FOR_PORTING
2017c478bd9Sstevel@tonic-gate #if    CHAR_MIN < 0
2027c478bd9Sstevel@tonic-gate 	rv = (*p++) & 0377;
2037c478bd9Sstevel@tonic-gate 	rv2 = (*p) & 0377;
2047c478bd9Sstevel@tonic-gate #else   /* CHAR_MIN < 0 */
2057c478bd9Sstevel@tonic-gate 	rv = *p++;
2067c478bd9Sstevel@tonic-gate 	rv2 = *p;
2077c478bd9Sstevel@tonic-gate #endif  /* CHAR_MIN < 0 */
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate #endif  /* FOR_PORTING  */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	if ((rv2 == 0377) && ((rv == 0377) || (rv == 0376)))
2127c478bd9Sstevel@tonic-gate 		return (-1);
2137c478bd9Sstevel@tonic-gate 	return (rv + (rv2 * 256));
2147c478bd9Sstevel@tonic-gate }
2157c478bd9Sstevel@tonic-gate #endif	/* _Getsh */
2167c478bd9Sstevel@tonic-gate 
2177c478bd9Sstevel@tonic-gate /*
2187c478bd9Sstevel@tonic-gate  * setupterm: low level routine to dig up terminfo from database
2197c478bd9Sstevel@tonic-gate  * and read it in.  Parms are terminal type (0 means use getenv("TERM"),
2207c478bd9Sstevel@tonic-gate  * file descriptor all output will go to (for ioctls), and a pointer
2217c478bd9Sstevel@tonic-gate  * to an int into which the error return code goes (0 means to bomb
2227c478bd9Sstevel@tonic-gate  * out with an error message if there's an error).  Thus,
2237c478bd9Sstevel@tonic-gate  * setupterm((char *)0, 1, (int *)0) is a reasonable way for a simple
2247c478bd9Sstevel@tonic-gate  * program to set up.
2257c478bd9Sstevel@tonic-gate  */
2267c478bd9Sstevel@tonic-gate int
setupterm(char * term,int filenum,int * errret)2277c478bd9Sstevel@tonic-gate setupterm(char *term, int filenum, int *errret)
2287c478bd9Sstevel@tonic-gate 	/* filenum - This is a UNIX file descriptor, not a stdio ptr. */
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 	char	tiebuf[4096];
2317c478bd9Sstevel@tonic-gate 	char	fname[TERMPATHLEN];
2327c478bd9Sstevel@tonic-gate 	char	*ip;
2337c478bd9Sstevel@tonic-gate 	char	*cp;
2347c478bd9Sstevel@tonic-gate 	int	n, tfd;
2357c478bd9Sstevel@tonic-gate 	char	*lcp, *ccp, **on_sequences, **str_array;
2367c478bd9Sstevel@tonic-gate 	int	snames, nbools, nints, nstrs, sstrtab;
2377c478bd9Sstevel@tonic-gate 	char	*strtab;
2387c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
2397c478bd9Sstevel@tonic-gate 	int		Vr2val;
2407c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	(void) mbgetwidth();
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate 	if (term == NULL)
2457c478bd9Sstevel@tonic-gate 		term = getenv("TERM");
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (term == NULL || *term == '\0')
2487c478bd9Sstevel@tonic-gate 		term = Def_term;
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	tfd = -1;
2517c478bd9Sstevel@tonic-gate 	errno = 0; 	/* ehr3 */
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate 	if (errret != 0)
2547c478bd9Sstevel@tonic-gate 		*errret = -1;
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	if (((cp = getenv("TERMINFO")) != 0) && *cp) {
2577c478bd9Sstevel@tonic-gate 		/* $TERMINFO/?/$TERM */
2587c478bd9Sstevel@tonic-gate 		if (snprintf(fname, sizeof (fname),
2597c478bd9Sstevel@tonic-gate 			"%s/%c/%s", cp, *term, term) >= sizeof (fname)) {
2607c478bd9Sstevel@tonic-gate 			term_errno = TERMINFO_TOO_LONG;
2617c478bd9Sstevel@tonic-gate 			goto out_err;
2627c478bd9Sstevel@tonic-gate 		}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 		tfd = open(fname, 0);
2657c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
2667c478bd9Sstevel@tonic-gate 		printf("looking in file %s\n", fname);
2677c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
2687c478bd9Sstevel@tonic-gate 		if ((tfd < 0) && (errno == EACCES))
2697c478bd9Sstevel@tonic-gate 			goto cant_read;
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate 	if (tfd < 0) {
2737c478bd9Sstevel@tonic-gate 		/* /usr/share/lib/terminfo/?/$TERM */
2747c478bd9Sstevel@tonic-gate 		if (snprintf(fname, sizeof (fname),
2757c478bd9Sstevel@tonic-gate 			"%s/%c/%s", TERMPATH, *term, term) >= sizeof (fname)) {
2767c478bd9Sstevel@tonic-gate 			term_errno = TERMINFO_TOO_LONG;
2777c478bd9Sstevel@tonic-gate 			goto out_err;
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 
2807c478bd9Sstevel@tonic-gate 		tfd = open(fname, 0);
2817c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
2827c478bd9Sstevel@tonic-gate 		printf("looking in file %s\n", fname);
2837c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	}
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (tfd < 0) {
2887c478bd9Sstevel@tonic-gate 		if (errno == EACCES) {
2897c478bd9Sstevel@tonic-gate cant_read:
2907c478bd9Sstevel@tonic-gate 			term_errno = NOT_READABLE;
2917c478bd9Sstevel@tonic-gate 		} else {
2927c478bd9Sstevel@tonic-gate 			if (access(TERMPATH, 0) == -1)
2937c478bd9Sstevel@tonic-gate 				term_errno = UNACCESSIBLE;
2947c478bd9Sstevel@tonic-gate 			else {
2957c478bd9Sstevel@tonic-gate 				term_errno = NO_TERMINAL;
2967c478bd9Sstevel@tonic-gate 				if (errret != 0)
2977c478bd9Sstevel@tonic-gate 					*errret = 0;
2987c478bd9Sstevel@tonic-gate 			}
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 		/*
3017c478bd9Sstevel@tonic-gate 		 * if the length of the specified terminal name is longer
3027c478bd9Sstevel@tonic-gate 		 * than 31, it will be chopped after the 31st byte.
3037c478bd9Sstevel@tonic-gate 		 * This should be a rare case.
3047c478bd9Sstevel@tonic-gate 		 */
3057c478bd9Sstevel@tonic-gate 		(void) strncpy(term_parm_err, term, 31);
3067c478bd9Sstevel@tonic-gate 		term_parm_err[31] = '\0';
3077c478bd9Sstevel@tonic-gate 		goto out_err;
3087c478bd9Sstevel@tonic-gate 	}
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate 	/* LINTED */
3117c478bd9Sstevel@tonic-gate 	n = (int)read(tfd, tiebuf, sizeof (tiebuf));
3127c478bd9Sstevel@tonic-gate 	(void) close(tfd);
3137c478bd9Sstevel@tonic-gate 
3147c478bd9Sstevel@tonic-gate 	if (n <= 0) {
3157c478bd9Sstevel@tonic-gate corrupt:
3167c478bd9Sstevel@tonic-gate 		term_errno = CORRUPTED;
3177c478bd9Sstevel@tonic-gate 		goto out_err;
3187c478bd9Sstevel@tonic-gate 	} else
3197c478bd9Sstevel@tonic-gate 		if (n == sizeof (tiebuf)) {
3207c478bd9Sstevel@tonic-gate 			term_errno = ENTRY_TOO_LONG;
3217c478bd9Sstevel@tonic-gate 			goto out_err;
3227c478bd9Sstevel@tonic-gate 		}
3237c478bd9Sstevel@tonic-gate 	cp = ttytype;
3247c478bd9Sstevel@tonic-gate 	ip = tiebuf;
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 	/* Pick up header */
3277c478bd9Sstevel@tonic-gate 	snames = _Getshi();
3287c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
3297c478bd9Sstevel@tonic-gate 	Vr2val = _Vr2getshi();
3307c478bd9Sstevel@tonic-gate 	printf("Magic number = %d, %#o [%d, %#o].\n", snames,
3317c478bd9Sstevel@tonic-gate 	    snames, Vr2val, Vr2val);
3327c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
3337c478bd9Sstevel@tonic-gate 	if (snames != MAGNUM)
3347c478bd9Sstevel@tonic-gate 		goto corrupt;
3357c478bd9Sstevel@tonic-gate 	snames = _Getshi();
3367c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
3377c478bd9Sstevel@tonic-gate 	Vr2val = _Vr2getshi();
3387c478bd9Sstevel@tonic-gate 	printf("Size of names = %d, %#o [%d, %#o].\n", snames,
3397c478bd9Sstevel@tonic-gate 	    snames, Vr2val, Vr2val);
3407c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
3417c478bd9Sstevel@tonic-gate 
3427c478bd9Sstevel@tonic-gate 	nbools = _Getshi();
3437c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
3447c478bd9Sstevel@tonic-gate 	Vr2val = _Vr2getshi();
3457c478bd9Sstevel@tonic-gate 	printf("Number of bools = %d, %#o [%d, %#o].\n", nbools,
3467c478bd9Sstevel@tonic-gate 	    nbools, Vr2val, Vr2val);
3477c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	nints = _Getshi();
3507c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
3517c478bd9Sstevel@tonic-gate 	Vr2val = _Vr2getshi();
3527c478bd9Sstevel@tonic-gate 	printf("Number of ints = %d, %#o [%d, %#o].\n", nints, nints,
3537c478bd9Sstevel@tonic-gate 	    Vr2val, Vr2val);
3547c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	nstrs = _Getshi();
3577c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
3587c478bd9Sstevel@tonic-gate 	Vr2val = _Vr2getshi();
3597c478bd9Sstevel@tonic-gate 	printf("Number of strings = %d, %#o [%d, %#o].\n", nstrs, nstrs,
3607c478bd9Sstevel@tonic-gate 	    Vr2val, Vr2val);
3617c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
3627c478bd9Sstevel@tonic-gate 
3637c478bd9Sstevel@tonic-gate 	sstrtab = _Getshi();
3647c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
3657c478bd9Sstevel@tonic-gate 	Vr2val = _Vr2getshi();
3667c478bd9Sstevel@tonic-gate 	printf("Size of string table = %d, %#o [%d, %#o].\n", sstrtab,
3677c478bd9Sstevel@tonic-gate 	    sstrtab, Vr2val, Vr2val);
3687c478bd9Sstevel@tonic-gate 	printf("Names are: %.*s.\n", snames, ip);
3697c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 	/* allocate all of the space */
3727c478bd9Sstevel@tonic-gate 	strtab = NULL;
3737c478bd9Sstevel@tonic-gate 	if (_called_before) {
3747c478bd9Sstevel@tonic-gate 		/* 2nd or more times through */
3757c478bd9Sstevel@tonic-gate 		if ((cur_term = (TERMINAL *)
3767c478bd9Sstevel@tonic-gate 		    calloc(sizeof (TERMINAL), 1)) == NULL)
3777c478bd9Sstevel@tonic-gate 			goto badmalloc;
3787c478bd9Sstevel@tonic-gate 		if ((cur_bools = (struct _bool_struct *)
3797c478bd9Sstevel@tonic-gate 		    calloc(sizeof (struct _bool_struct), 1)) == NULL)
3807c478bd9Sstevel@tonic-gate 			goto freeterminal;
3817c478bd9Sstevel@tonic-gate 		if ((cur_nums = (struct _num_struct *)
3827c478bd9Sstevel@tonic-gate 		    calloc(sizeof (struct _num_struct), 1)) == NULL)
3837c478bd9Sstevel@tonic-gate 			goto freebools;
3847c478bd9Sstevel@tonic-gate 		if ((cur_strs = (struct _str_struct *)
3857c478bd9Sstevel@tonic-gate 		    calloc(sizeof (struct _str_struct), 1)) == NULL) {
3867c478bd9Sstevel@tonic-gate freenums:
3877c478bd9Sstevel@tonic-gate 			free((char *)cur_nums);
3887c478bd9Sstevel@tonic-gate freebools:
3897c478bd9Sstevel@tonic-gate 			free((char *)cur_bools);
3907c478bd9Sstevel@tonic-gate freeterminal:
3917c478bd9Sstevel@tonic-gate 			free((char *)cur_term);
3927c478bd9Sstevel@tonic-gate badmalloc:
3937c478bd9Sstevel@tonic-gate 			term_errno = TERM_BAD_MALLOC;
3947c478bd9Sstevel@tonic-gate #ifdef	DEBUG
3957c478bd9Sstevel@tonic-gate 			strcpy(term_parm_err, "setupterm");
3967c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
3977c478bd9Sstevel@tonic-gate out_err:
3987c478bd9Sstevel@tonic-gate 			if (errret == 0) {
3997c478bd9Sstevel@tonic-gate 				termerr();
4007c478bd9Sstevel@tonic-gate 				exit(-term_errno);
4017c478bd9Sstevel@tonic-gate 			} else
4027c478bd9Sstevel@tonic-gate 				return (ERR);
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 	} else {
4057c478bd9Sstevel@tonic-gate 		/* First time through */
4067c478bd9Sstevel@tonic-gate 		_called_before = TRUE;
4077c478bd9Sstevel@tonic-gate 		cur_term = &_first_term;
4087c478bd9Sstevel@tonic-gate 		cur_bools = &_frst_bools;
4097c478bd9Sstevel@tonic-gate 		cur_nums = &_frst_nums;
4107c478bd9Sstevel@tonic-gate 		cur_strs = &_frst_strs;
4117c478bd9Sstevel@tonic-gate 		if (sstrtab < sizeof (_frst_tblstr))
4127c478bd9Sstevel@tonic-gate 			strtab = _frst_tblstr;
4137c478bd9Sstevel@tonic-gate 	}
4147c478bd9Sstevel@tonic-gate 
4157c478bd9Sstevel@tonic-gate 	if (strtab == NULL) {
4167c478bd9Sstevel@tonic-gate 		if ((strtab = (char *)malloc((unsigned)sstrtab)) == NULL) {
4177c478bd9Sstevel@tonic-gate 			if (cur_strs != &_frst_strs)
4187c478bd9Sstevel@tonic-gate 				free((char *)cur_strs);
4197c478bd9Sstevel@tonic-gate 			goto freenums;
4207c478bd9Sstevel@tonic-gate 		}
4217c478bd9Sstevel@tonic-gate 	}
4227c478bd9Sstevel@tonic-gate 
4237c478bd9Sstevel@tonic-gate 	/* no more catchable errors */
4247c478bd9Sstevel@tonic-gate 	if (errret)
4257c478bd9Sstevel@tonic-gate 		*errret = 1;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	(void) strncpy(cur_term->_termname, term, 14);
4287c478bd9Sstevel@tonic-gate 	/* In case the name is exactly 15 characters */
4297c478bd9Sstevel@tonic-gate 	cur_term->_termname[14] = '\0';
4307c478bd9Sstevel@tonic-gate 	cur_term->_bools = cur_bools;
4317c478bd9Sstevel@tonic-gate 	cur_term->_nums = cur_nums;
4327c478bd9Sstevel@tonic-gate 	cur_term->_strs = cur_strs;
4337c478bd9Sstevel@tonic-gate 	cur_term->_strtab = strtab;
4347c478bd9Sstevel@tonic-gate 	cur_term->sgr_mode = cur_term->sgr_faked = A_NORMAL;
4357c478bd9Sstevel@tonic-gate 
436*06ca4e39SAndy Fiddaman 	if (filenum == 1) {
437*06ca4e39SAndy Fiddaman 		/*
438*06ca4e39SAndy Fiddaman 		 * isatty(3C) will set errno as a side-effect of returning 0.
439*06ca4e39SAndy Fiddaman 		 * Hide this from callers so that they do not see errno
440*06ca4e39SAndy Fiddaman 		 * changing for no apparent reason.
441*06ca4e39SAndy Fiddaman 		 */
442*06ca4e39SAndy Fiddaman 		int err = errno;
443*06ca4e39SAndy Fiddaman 		if (isatty(filenum) == 0)
444*06ca4e39SAndy Fiddaman 			filenum = 2;	/* Allow output redirect */
445*06ca4e39SAndy Fiddaman 		errno = err;
446*06ca4e39SAndy Fiddaman 	}
4477c478bd9Sstevel@tonic-gate 	/* LINTED */
4487c478bd9Sstevel@tonic-gate 	cur_term->Filedes = (short)filenum;
4497c478bd9Sstevel@tonic-gate 	_blast_keys(cur_term);
4507c478bd9Sstevel@tonic-gate 	cur_term->_iwait = cur_term->fl_typeahdok = cur_term->_chars_on_queue =
4517c478bd9Sstevel@tonic-gate 		cur_term->_fl_rawmode = cur_term->_ungotten = 0;
4527c478bd9Sstevel@tonic-gate 	cur_term->_cursorstate = 1;
4537c478bd9Sstevel@tonic-gate 	cur_term->_delay = cur_term->_inputfd = cur_term->_check_fd = -1;
4547c478bd9Sstevel@tonic-gate 	(void) memset((char *)cur_term->_regs, 0, 26 * sizeof (short));
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate #ifndef	DUMPTI
4577c478bd9Sstevel@tonic-gate 	(void) def_shell_mode();
4587c478bd9Sstevel@tonic-gate 	/* This is a useful default for PROGTTY, too */
4597c478bd9Sstevel@tonic-gate #ifdef SYSV
4607c478bd9Sstevel@tonic-gate 	if (shell_istermios < 0) {
4617c478bd9Sstevel@tonic-gate 		int i;
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 		SHELLTTY.c_lflag = SHELLTTYS.c_lflag;
4647c478bd9Sstevel@tonic-gate 		SHELLTTY.c_oflag = SHELLTTYS.c_oflag;
4657c478bd9Sstevel@tonic-gate 		SHELLTTY.c_iflag = SHELLTTYS.c_iflag;
4667c478bd9Sstevel@tonic-gate 		SHELLTTY.c_cflag = SHELLTTYS.c_cflag;
4677c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
4687c478bd9Sstevel@tonic-gate 			SHELLTTY.c_cc[i] = SHELLTTYS.c_cc[i];
4697c478bd9Sstevel@tonic-gate 		PROGTTY = SHELLTTY;
4707c478bd9Sstevel@tonic-gate 		prog_istermios = -1;
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 		PROGTTYS.c_lflag = PROGTTY.c_lflag;
4737c478bd9Sstevel@tonic-gate 		PROGTTYS.c_oflag = PROGTTY.c_oflag;
4747c478bd9Sstevel@tonic-gate 		PROGTTYS.c_iflag = PROGTTY.c_iflag;
4757c478bd9Sstevel@tonic-gate 		PROGTTYS.c_cflag = PROGTTY.c_cflag;
4767c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
4777c478bd9Sstevel@tonic-gate 			PROGTTYS.c_cc[i] = PROGTTY.c_cc[i];
4787c478bd9Sstevel@tonic-gate 	} else {
4797c478bd9Sstevel@tonic-gate 		PROGTTYS = SHELLTTYS;
4807c478bd9Sstevel@tonic-gate 		prog_istermios = 0;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate #else	/* SYSV */
4837c478bd9Sstevel@tonic-gate 	PROGTTY = SHELLTTY;
4847c478bd9Sstevel@tonic-gate #endif	/* SYSV */
4857c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/* Skip names of terminals */
4887c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)cp, (char *)ip, (snames * sizeof (*cp)));
4897c478bd9Sstevel@tonic-gate 	ip += snames;
4907c478bd9Sstevel@tonic-gate 
4917c478bd9Sstevel@tonic-gate 	/*
4927c478bd9Sstevel@tonic-gate 	 * Pull out the booleans.
4937c478bd9Sstevel@tonic-gate 	 * The for loop below takes care of a new curses with an old tic
4947c478bd9Sstevel@tonic-gate 	 * file and visa-versa.  nbools says how many bools the tic file has.
4957c478bd9Sstevel@tonic-gate 	 * So, we only loop for as long as there are bools to read.
4967c478bd9Sstevel@tonic-gate 	 * However, if this is an old curses that doesn't have all the
4977c478bd9Sstevel@tonic-gate 	 * bools that this new tic has dumped, then the extra if
4987c478bd9Sstevel@tonic-gate 	 * "if (cp < fp)" says that if we are going to read into our structure
4997c478bd9Sstevel@tonic-gate 	 * passed its size don't do it but we still need to keep bumping
5007c478bd9Sstevel@tonic-gate 	 * up the pointer of what we read in from the terminfo file.
5017c478bd9Sstevel@tonic-gate 	 */
5027c478bd9Sstevel@tonic-gate 	{
5037c478bd9Sstevel@tonic-gate 		char	*fp = &cur_bools->Sentinel;
5047c478bd9Sstevel@tonic-gate 		char	s;
5057c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
5067c478bd9Sstevel@tonic-gate 		int	tempindex = 0;
5077c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
5087c478bd9Sstevel@tonic-gate 		cp = &cur_bools->_auto_left_margin;
5097c478bd9Sstevel@tonic-gate 		while (nbools--) {
5107c478bd9Sstevel@tonic-gate 			s = *ip++;
5117c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
5127c478bd9Sstevel@tonic-gate 			printf("Bool %s [%s] (%s) = %d.\n",
5137c478bd9Sstevel@tonic-gate 			    boolfnames[tempindex], boolnames[tempindex],
5147c478bd9Sstevel@tonic-gate 			    boolcodes[tempindex], s);
5157c478bd9Sstevel@tonic-gate 			tempindex++;
5167c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
5177c478bd9Sstevel@tonic-gate 			if (cp < fp)
5187c478bd9Sstevel@tonic-gate 				*cp++ = s & 01;
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		if (cp < fp)
5217c478bd9Sstevel@tonic-gate 			(void) memset(cp, 0, ((fp - cp) * sizeof (bool)));
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	/* Force proper alignment */
5257c478bd9Sstevel@tonic-gate 	if (((unsigned long) ip) & 1)
5267c478bd9Sstevel@tonic-gate 		ip++;
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 	/*
5297c478bd9Sstevel@tonic-gate 	 * Pull out the numbers.
5307c478bd9Sstevel@tonic-gate 	 */
5317c478bd9Sstevel@tonic-gate 	{
5327c478bd9Sstevel@tonic-gate 		short	*sp = &cur_nums->_columns;
5337c478bd9Sstevel@tonic-gate 		short	*fp = &cur_nums->Sentinel;
5347c478bd9Sstevel@tonic-gate 		int	s;
5357c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
5367c478bd9Sstevel@tonic-gate 		int	tempindex = 0;
5377c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 		while (nints--) {
5407c478bd9Sstevel@tonic-gate 			s = _Getshi();
5417c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
5427c478bd9Sstevel@tonic-gate 			Vr2val = _Vr2getshi();
5437c478bd9Sstevel@tonic-gate 			printf("Num %s [%s] (%s) = %d [%d].\n",
5447c478bd9Sstevel@tonic-gate 			    numfnames[tempindex], numnames[tempindex],
5457c478bd9Sstevel@tonic-gate 			    numcodes[tempindex], s, Vr2val);
5467c478bd9Sstevel@tonic-gate 			tempindex++;
5477c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
5487c478bd9Sstevel@tonic-gate 			if (sp < fp)
5497c478bd9Sstevel@tonic-gate 				if (s < 0)
5507c478bd9Sstevel@tonic-gate 					*sp++ = -1;
5517c478bd9Sstevel@tonic-gate 				else
5527c478bd9Sstevel@tonic-gate 					/* LINTED */
5537c478bd9Sstevel@tonic-gate 					*sp++ = (short)s;
5547c478bd9Sstevel@tonic-gate 		}
5557c478bd9Sstevel@tonic-gate 		if (sp < fp)
5567c478bd9Sstevel@tonic-gate 			(void) memset((char *)sp, '\377',
5577c478bd9Sstevel@tonic-gate 			    ((fp - sp) * sizeof (short)));
5587c478bd9Sstevel@tonic-gate 	}
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (_use_env) {
5617c478bd9Sstevel@tonic-gate 		/*
5627c478bd9Sstevel@tonic-gate 		 * This ioctl defines the window size and overrides what
5637c478bd9Sstevel@tonic-gate 		 * it says in terminfo.
5647c478bd9Sstevel@tonic-gate 		 */
5657c478bd9Sstevel@tonic-gate 		{
5667c478bd9Sstevel@tonic-gate 			struct	winsize	w;
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 			if (ioctl(filenum, TIOCGWINSZ, &w) != -1) {
5697c478bd9Sstevel@tonic-gate 				if (w.ws_row != 0)
5707c478bd9Sstevel@tonic-gate 					cur_nums->_lines = w.ws_row;
5717c478bd9Sstevel@tonic-gate 				if (w.ws_col != 0)
5727c478bd9Sstevel@tonic-gate 					cur_nums->_columns = w.ws_col;
5737c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
5747c478bd9Sstevel@tonic-gate 				printf("ioctl TIOCGWINSZ override: "
5757c478bd9Sstevel@tonic-gate 				    "(lines, columns) = (%d, %d)\n",
5767c478bd9Sstevel@tonic-gate 				    w.ws_row, w.ws_col);
5777c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
5787c478bd9Sstevel@tonic-gate 			}
5797c478bd9Sstevel@tonic-gate 		}
5807c478bd9Sstevel@tonic-gate 
5817c478bd9Sstevel@tonic-gate 		/*
5827c478bd9Sstevel@tonic-gate 		 * Check $LINES and $COLUMNS.
5837c478bd9Sstevel@tonic-gate 		 */
5847c478bd9Sstevel@tonic-gate 		{
5857c478bd9Sstevel@tonic-gate 			int	ilines, icolumns;
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 			lcp = getenv("LINES");
5887c478bd9Sstevel@tonic-gate 			ccp = getenv("COLUMNS");
5897c478bd9Sstevel@tonic-gate 			if (lcp)
5907c478bd9Sstevel@tonic-gate 				if ((ilines = atoi(lcp)) > 0) {
5917c478bd9Sstevel@tonic-gate 					/* LINTED */
5927c478bd9Sstevel@tonic-gate 					cur_nums->_lines = (short)ilines;
5937c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
5947c478bd9Sstevel@tonic-gate 					printf("$LINES override: lines = %d\n",
5957c478bd9Sstevel@tonic-gate 					    ilines);
5967c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
5977c478bd9Sstevel@tonic-gate 				}
5987c478bd9Sstevel@tonic-gate 			if (ccp)
5997c478bd9Sstevel@tonic-gate 				if ((icolumns = atoi(ccp)) > 0) {
6007c478bd9Sstevel@tonic-gate 					/* LINTED */
6017c478bd9Sstevel@tonic-gate 					cur_nums->_columns = (short)icolumns;
6027c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
6037c478bd9Sstevel@tonic-gate 					printf("$COLUMNS override: columns = "
6047c478bd9Sstevel@tonic-gate 					    "%d\n", icolumns);
6057c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
6067c478bd9Sstevel@tonic-gate 				}
6077c478bd9Sstevel@tonic-gate 		}
6087c478bd9Sstevel@tonic-gate 	}
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	/* Pull out the strings. */
6117c478bd9Sstevel@tonic-gate 	{
6127c478bd9Sstevel@tonic-gate 		char	**pp = &cur_strs->strs._back_tab;
6137c478bd9Sstevel@tonic-gate 		char	**fp = &cur_strs->strs4.Sentinel;
6147c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
6157c478bd9Sstevel@tonic-gate 		int	tempindex = 0;
6167c478bd9Sstevel@tonic-gate 		char	*startstr = ip + sizeof (short) *
6177c478bd9Sstevel@tonic-gate 					    nstrs;
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate 		printf("string table = '");
6207c478bd9Sstevel@tonic-gate 		_Mprint(sstrtab, startstr);
6217c478bd9Sstevel@tonic-gate 		printf("'\n");
6227c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 		while (nstrs--) {
6257c478bd9Sstevel@tonic-gate 			n = _Getshi();
6267c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
6277c478bd9Sstevel@tonic-gate 			Vr2val = _Vr2getshi();
6287c478bd9Sstevel@tonic-gate 			printf("String %s [%s] (%s) offset = %d [%d]",
6297c478bd9Sstevel@tonic-gate 			    strfnames[tempindex], strnames[tempindex],
6307c478bd9Sstevel@tonic-gate 			    strcodes[tempindex], n, Vr2val);
6317c478bd9Sstevel@tonic-gate 			tempindex++;
6327c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
6337c478bd9Sstevel@tonic-gate 			if (pp < fp) {
6347c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
6357c478bd9Sstevel@tonic-gate 				_Sprint(n, startstr+n);
6367c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
6377c478bd9Sstevel@tonic-gate 				if (n < 0)
6387c478bd9Sstevel@tonic-gate 					*pp++ = NULL;
6397c478bd9Sstevel@tonic-gate 				else
6407c478bd9Sstevel@tonic-gate 					*pp++ = strtab + n;
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate #ifdef	DUMPTI
6437c478bd9Sstevel@tonic-gate 			else
6447c478bd9Sstevel@tonic-gate 				_Sprint(-1, (char *)0);
6457c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
6467c478bd9Sstevel@tonic-gate 		}
6477c478bd9Sstevel@tonic-gate 		if (pp < fp)
6487c478bd9Sstevel@tonic-gate 		(void) memset((char *)pp, 0, ((fp - pp) * sizeof (charptr)));
6497c478bd9Sstevel@tonic-gate 	}
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate 	(void) memcpy(strtab, ip, sstrtab);
6527c478bd9Sstevel@tonic-gate 
6537c478bd9Sstevel@tonic-gate #ifndef	DUMPTI
6547c478bd9Sstevel@tonic-gate 
6557c478bd9Sstevel@tonic-gate 	/*
6567c478bd9Sstevel@tonic-gate 	 * If tabs are being expanded in software, turn this off
6577c478bd9Sstevel@tonic-gate 	 * so output won't get messed up.  Also, don't use tab
6587c478bd9Sstevel@tonic-gate 	 * or backtab, even if the terminal has them, since the
6597c478bd9Sstevel@tonic-gate 	 * user might not have hardware tabs set right.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate #ifdef	SYSV
6627c478bd9Sstevel@tonic-gate 	if ((PROGTTYS.c_oflag & TABDLY) == TAB3) {
6637c478bd9Sstevel@tonic-gate 		PROGTTYS.c_oflag &= ~TABDLY;
6647c478bd9Sstevel@tonic-gate 		(void) reset_prog_mode();
6657c478bd9Sstevel@tonic-gate 		goto next;
6667c478bd9Sstevel@tonic-gate 	}
6677c478bd9Sstevel@tonic-gate #else	/* SYSV */
6687c478bd9Sstevel@tonic-gate 	if ((PROGTTY.sg_flags & XTABS) == XTABS) {
6697c478bd9Sstevel@tonic-gate 		PROGTTY.sg_flags &= ~XTABS;
6707c478bd9Sstevel@tonic-gate 		(void) reset_prog_mode();
6717c478bd9Sstevel@tonic-gate 		goto next;
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6747c478bd9Sstevel@tonic-gate 	if (dest_tabs_magic_smso) {
6757c478bd9Sstevel@tonic-gate next:
6767c478bd9Sstevel@tonic-gate 		cur_strs->strs2._tab = cur_strs->strs._back_tab = NULL;
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate #ifdef	LTILDE
6807c478bd9Sstevel@tonic-gate 	ioctl(cur_term -> Filedes, TIOCLGET, &n);
6817c478bd9Sstevel@tonic-gate #endif	/* LTILDE */
6827c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate #ifdef	_VR2_COMPAT_CODE
6857c478bd9Sstevel@tonic-gate 	(void) memcpy(&cur_term->_b1, &cur_bools->_auto_left_margin,
6867c478bd9Sstevel@tonic-gate 	    (char *)&cur_term->_c1 - (char *)&cur_term->_b1);
6877c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)&cur_term->_c1, (char *)&cur_nums->_columns,
6887c478bd9Sstevel@tonic-gate 	    (char *)&cur_term->_Vr2_Astrs._s1 - (char *)&cur_term->_c1);
6897c478bd9Sstevel@tonic-gate 	(void) memcpy((char *)&cur_term->_Vr2_Astrs._s1,
6907c478bd9Sstevel@tonic-gate 	    (char *)&cur_strs->strs._back_tab,
6917c478bd9Sstevel@tonic-gate 	    (char *)&cur_term->Filedes - (char *)&cur_term->_Vr2_Astrs._s1);
6927c478bd9Sstevel@tonic-gate #endif	/* _VR2_COMPAT_CODE */
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate 	on_sequences = cur_term->turn_on_seq;
6957c478bd9Sstevel@tonic-gate 	str_array = (char **)cur_strs;
6967c478bd9Sstevel@tonic-gate 	{
6977c478bd9Sstevel@tonic-gate 		static	char	offsets[] = {
6987c478bd9Sstevel@tonic-gate 			    35,	/* enter_standout_mode, */
6997c478bd9Sstevel@tonic-gate 			    36,	/* enter_underline_mode, */
7007c478bd9Sstevel@tonic-gate 			    25,	/* enter_alt_charset_mode, */
7017c478bd9Sstevel@tonic-gate 			    34,	/* enter_reverse_mode, */
7027c478bd9Sstevel@tonic-gate 			    26,	/* enter_blink_mode, */
7037c478bd9Sstevel@tonic-gate 			    30,	/* enter_dim_mode, */
7047c478bd9Sstevel@tonic-gate 			    27,	/* enter_bold_mode, */
7057c478bd9Sstevel@tonic-gate 			    32,	/* enter_secure_mode, */
7067c478bd9Sstevel@tonic-gate 			    33,	/* enter_protected_mode, */
7077c478bd9Sstevel@tonic-gate 			};
7087c478bd9Sstevel@tonic-gate 
7097c478bd9Sstevel@tonic-gate 		for (n = 0; n < NUM_ATTRIBUTES; n++) {
7107c478bd9Sstevel@tonic-gate 			if ((on_sequences[n] = str_array[offsets[n]]) != 0)
7117c478bd9Sstevel@tonic-gate 				cur_term->bit_vector |= bit_attributes[n];
7127c478bd9Sstevel@tonic-gate 		}
7137c478bd9Sstevel@tonic-gate 	}
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 	if (!(set_attributes)) {
7167c478bd9Sstevel@tonic-gate 		static	char	faked_attrs[] = { 1, 3, 4, 6 },
7177c478bd9Sstevel@tonic-gate 			offsets[] = {
7187c478bd9Sstevel@tonic-gate 			    43,	/* exit_standout_mode, */
7197c478bd9Sstevel@tonic-gate 			    44,	/* exit_underline_mode, */
7207c478bd9Sstevel@tonic-gate 			    38,	/* exit_alt_charset_mode, */
7217c478bd9Sstevel@tonic-gate 			};
7227c478bd9Sstevel@tonic-gate 		char		**off_sequences = cur_term->turn_off_seq;
7237c478bd9Sstevel@tonic-gate 		int		i;
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 		if ((max_attributes == -1) && (ceol_standout_glitch ||
7267c478bd9Sstevel@tonic-gate 		    (magic_cookie_glitch >= 0)))
7277c478bd9Sstevel@tonic-gate 			max_attributes = 1;
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 		/* Figure out what attributes need to be faked. */
7307c478bd9Sstevel@tonic-gate 		/* See vidupdate.c */
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 		for (n = 0; n < sizeof (faked_attrs); n++) {
7337c478bd9Sstevel@tonic-gate 			if (on_sequences[0] != NULL) {
7347c478bd9Sstevel@tonic-gate 				if ((!on_sequences[i = faked_attrs[n]]) ||
7357c478bd9Sstevel@tonic-gate 				    (strcmp(on_sequences[i],
7367c478bd9Sstevel@tonic-gate 				    on_sequences[0]) == 0)) {
7377c478bd9Sstevel@tonic-gate 					cur_term->sgr_faked |=
7387c478bd9Sstevel@tonic-gate 					    bit_attributes[i];
7397c478bd9Sstevel@tonic-gate 				}
7407c478bd9Sstevel@tonic-gate 			} else {
7417c478bd9Sstevel@tonic-gate 				if (!on_sequences[i = faked_attrs[n]]) {
7427c478bd9Sstevel@tonic-gate 					cur_term->sgr_faked |=
7437c478bd9Sstevel@tonic-gate 					    bit_attributes[i];
7447c478bd9Sstevel@tonic-gate 				}
7457c478bd9Sstevel@tonic-gate 			}
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		cur_term->check_turn_off = A_STANDOUT | A_UNDERLINE |
7497c478bd9Sstevel@tonic-gate 		    A_ALTCHARSET;
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 		for (n = 0; n < sizeof (offsets); n++) {
7527c478bd9Sstevel@tonic-gate 			if ((!(off_sequences[n] = str_array[offsets[n]])) ||
7537c478bd9Sstevel@tonic-gate 			    ((n > 0) && off_sequences[0] &&
7547c478bd9Sstevel@tonic-gate 			    (strcmp(off_sequences[n], off_sequences[0]) ==
7557c478bd9Sstevel@tonic-gate 			    0)) || ((n == 2) && (exit_attribute_mode) &&
7567c478bd9Sstevel@tonic-gate 			    (strcmp(exit_attribute_mode, off_sequences[n]) ==
7577c478bd9Sstevel@tonic-gate 			    0))) {
7587c478bd9Sstevel@tonic-gate 				cur_term->check_turn_off &= ~bit_attributes[n];
7597c478bd9Sstevel@tonic-gate 			}
7607c478bd9Sstevel@tonic-gate 		}
7617c478bd9Sstevel@tonic-gate 	}
7627c478bd9Sstevel@tonic-gate 	cur_term->cursor_seq[0] = cursor_invisible;
7637c478bd9Sstevel@tonic-gate 	cur_term->cursor_seq[1] = cursor_normal;
7647c478bd9Sstevel@tonic-gate 	cur_term->cursor_seq[2] = cursor_visible;
7657c478bd9Sstevel@tonic-gate 	cur_term->_pairs_tbl = (_Color_pair *) NULL;
7667c478bd9Sstevel@tonic-gate 	cur_term->_color_tbl = (_Color *) NULL;
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	return (OK);
7697c478bd9Sstevel@tonic-gate }
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate void
_blast_keys(TERMINAL * terminal)7727c478bd9Sstevel@tonic-gate _blast_keys(TERMINAL *terminal)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 	terminal->_keys = NULL;
7757c478bd9Sstevel@tonic-gate 	terminal->internal_keys = NULL;
7767c478bd9Sstevel@tonic-gate 	terminal->_ksz = terminal->_first_macro = 0;
7777c478bd9Sstevel@tonic-gate 	terminal->_lastkey_ordered = terminal->_lastmacro_ordered = -1;
7787c478bd9Sstevel@tonic-gate 	(void) memset((char *)terminal->funckeystarter, 0, 0400 *
7797c478bd9Sstevel@tonic-gate 	    sizeof (bool));
7807c478bd9Sstevel@tonic-gate }
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate #ifndef	DUMPTI
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate int
reset_prog_mode(void)7857c478bd9Sstevel@tonic-gate reset_prog_mode(void)
7867c478bd9Sstevel@tonic-gate {
7877c478bd9Sstevel@tonic-gate #ifdef	SYSV
7887c478bd9Sstevel@tonic-gate 	if (_BRS(PROGTTYS)) {
7897c478bd9Sstevel@tonic-gate 		if (prog_istermios < 0) {
7907c478bd9Sstevel@tonic-gate 			int i;
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 			PROGTTY.c_lflag = PROGTTYS.c_lflag;
7937c478bd9Sstevel@tonic-gate 			PROGTTY.c_oflag = PROGTTYS.c_oflag;
7947c478bd9Sstevel@tonic-gate 			PROGTTY.c_iflag = PROGTTYS.c_iflag;
7957c478bd9Sstevel@tonic-gate 			PROGTTY.c_cflag = PROGTTYS.c_cflag;
7967c478bd9Sstevel@tonic-gate 			for (i = 0; i < NCC; i++)
7977c478bd9Sstevel@tonic-gate 				PROGTTY.c_cc[i] = PROGTTYS.c_cc[i];
7987c478bd9Sstevel@tonic-gate 			(void) ioctl(cur_term -> Filedes, TCSETAW, &PROGTTY);
7997c478bd9Sstevel@tonic-gate 		} else
8007c478bd9Sstevel@tonic-gate 			(void) ioctl(cur_term -> Filedes, TCSETSW, &PROGTTYS);
8017c478bd9Sstevel@tonic-gate 	}
8027c478bd9Sstevel@tonic-gate #else	/* SYSV */
8037c478bd9Sstevel@tonic-gate 	if (_BR(PROGTTY))
8047c478bd9Sstevel@tonic-gate 		(void) ioctl(cur_term -> Filedes, TIOCSETN, &PROGTTY);
8057c478bd9Sstevel@tonic-gate #endif	/* SYSV */
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate #ifdef	LTILDE
8087c478bd9Sstevel@tonic-gate 	ioctl(cur_term -> Filedes, TIOCLGET, &cur_term -> oldlmode);
8097c478bd9Sstevel@tonic-gate 	cur_term -> newlmode = cur_term -> oldlmode & ~LTILDE;
8107c478bd9Sstevel@tonic-gate 	if (cur_term -> newlmode != cur_term -> oldlmode)
8117c478bd9Sstevel@tonic-gate 		ioctl(cur_term -> Filedes, TIOCLSET, &cur_term -> newlmode);
8127c478bd9Sstevel@tonic-gate #endif	/* LTILDE */
8137c478bd9Sstevel@tonic-gate #ifdef	DIOCSETT
8147c478bd9Sstevel@tonic-gate 	if (cur_term -> old.st_termt == 0)
8157c478bd9Sstevel@tonic-gate 		ioctl(cur_term->Filedes, DIOCGETT, &cur_term -> old);
8167c478bd9Sstevel@tonic-gate 	cur_term -> new = cur_term -> old;
8177c478bd9Sstevel@tonic-gate 	cur_term -> new.st_termt = 0;
8187c478bd9Sstevel@tonic-gate 	cur_term -> new.st_flgs |= TM_SET;
8197c478bd9Sstevel@tonic-gate 	ioctl(cur_term->Filedes, DIOCSETT, &cur_term -> new);
8207c478bd9Sstevel@tonic-gate #endif	/* DIOCSETT */
8217c478bd9Sstevel@tonic-gate 	return (OK);
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate int
def_shell_mode(void)8257c478bd9Sstevel@tonic-gate def_shell_mode(void)
8267c478bd9Sstevel@tonic-gate {
8277c478bd9Sstevel@tonic-gate #ifdef	SYSV
8287c478bd9Sstevel@tonic-gate 	if ((shell_istermios =
8297c478bd9Sstevel@tonic-gate 	    ioctl(cur_term -> Filedes, TCGETS, &SHELLTTYS)) < 0) {
8307c478bd9Sstevel@tonic-gate 		int i;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 		(void) ioctl(cur_term -> Filedes, TCGETA, &SHELLTTY);
8337c478bd9Sstevel@tonic-gate 		SHELLTTYS.c_lflag = SHELLTTY.c_lflag;
8347c478bd9Sstevel@tonic-gate 		SHELLTTYS.c_oflag = SHELLTTY.c_oflag;
8357c478bd9Sstevel@tonic-gate 		SHELLTTYS.c_iflag = SHELLTTY.c_iflag;
8367c478bd9Sstevel@tonic-gate 		SHELLTTYS.c_cflag = SHELLTTY.c_cflag;
8377c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
8387c478bd9Sstevel@tonic-gate 			SHELLTTYS.c_cc[i] = SHELLTTY.c_cc[i];
8397c478bd9Sstevel@tonic-gate 	}
8407c478bd9Sstevel@tonic-gate #else	/* SYSV */
8417c478bd9Sstevel@tonic-gate 	(void) ioctl(cur_term -> Filedes, TIOCGETP, &SHELLTTY);
8427c478bd9Sstevel@tonic-gate #endif	/* SYSV */
8437c478bd9Sstevel@tonic-gate 	return (OK);
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate #endif	/* DUMPTI */
847