xref: /titanic_50/usr/src/cmd/tput/tput.c (revision d23589482994f9ec7e17f81415324eea6e26d015)
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
5de81e71eSTim Marsland  * Common Development and Distribution License (the "License").
6de81e71eSTim Marsland  * 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  */
21de81e71eSTim Marsland 
227c478bd9Sstevel@tonic-gate /*
23de81e71eSTim Marsland  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*d2358948SGary Mills  * Copyright (c) 2012 Gary Mills
267c478bd9Sstevel@tonic-gate  */
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
297c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  *	tput - print terminal attribute
337c478bd9Sstevel@tonic-gate  *
347c478bd9Sstevel@tonic-gate  *  return-codes - command line arguments:
357c478bd9Sstevel@tonic-gate  *	0: ok if boolean capname -> TRUE
367c478bd9Sstevel@tonic-gate  *	1: for boolean capname -> FALSE
377c478bd9Sstevel@tonic-gate  *
387c478bd9Sstevel@tonic-gate  *  return-codes - standard input arguments:
397c478bd9Sstevel@tonic-gate  *	0: ok; tput for all lines was successful
407c478bd9Sstevel@tonic-gate  *
417c478bd9Sstevel@tonic-gate  *  return-codes - both cases:
427c478bd9Sstevel@tonic-gate  *	2	usage error
437c478bd9Sstevel@tonic-gate  *	3	bad terminal type given or no terminfo database
447c478bd9Sstevel@tonic-gate  *	4	unknown capname
457c478bd9Sstevel@tonic-gate  *	-1	capname is a numeric variable that is not specified in the
467c478bd9Sstevel@tonic-gate  *		terminfo database(E.g. tpu -T450 lines).
477c478bd9Sstevel@tonic-gate  *
487c478bd9Sstevel@tonic-gate  *  tput printfs a value if an INT capname was given; e.g. cols.
497c478bd9Sstevel@tonic-gate  *	putp's a string if a STRING capname was given; e.g. clear. and
507c478bd9Sstevel@tonic-gate  *  for BOOLEAN capnames, e.g. hard-copy, just returns the boolean value.
517c478bd9Sstevel@tonic-gate  */
527c478bd9Sstevel@tonic-gate 
537c478bd9Sstevel@tonic-gate #include <curses.h>
547c478bd9Sstevel@tonic-gate #include <term.h>
557c478bd9Sstevel@tonic-gate #include <fcntl.h>
567c478bd9Sstevel@tonic-gate #include <ctype.h>
577c478bd9Sstevel@tonic-gate #include <stdlib.h>
587c478bd9Sstevel@tonic-gate #include <string.h>
597c478bd9Sstevel@tonic-gate #include <sys/types.h>
607c478bd9Sstevel@tonic-gate #include <unistd.h>
617c478bd9Sstevel@tonic-gate #include <locale.h>
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate /* externs from libcurses */
647c478bd9Sstevel@tonic-gate extern int tigetnum();
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int outputcap(char *cap, int argc, char **argv);
677c478bd9Sstevel@tonic-gate static int allnumeric(char *string);
687c478bd9Sstevel@tonic-gate static int getpad(char *cap);
697c478bd9Sstevel@tonic-gate static void setdelay();
707c478bd9Sstevel@tonic-gate static void settabs();
717c478bd9Sstevel@tonic-gate static void cat(char *file);
727c478bd9Sstevel@tonic-gate static void initterm();
737c478bd9Sstevel@tonic-gate static void reset_term();
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate static char *progname;		/* argv[0] */
767c478bd9Sstevel@tonic-gate static int CurrentBaudRate;	/* current baud rate */
777c478bd9Sstevel@tonic-gate static int reset = 0;		/* called as reset_term */
787c478bd9Sstevel@tonic-gate static int fildes = 1;
797c478bd9Sstevel@tonic-gate 
807c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)817c478bd9Sstevel@tonic-gate main(int argc, char **argv)
827c478bd9Sstevel@tonic-gate {
837c478bd9Sstevel@tonic-gate 	int i, std_argc;
847c478bd9Sstevel@tonic-gate 	char *term = getenv("TERM");
857c478bd9Sstevel@tonic-gate 	char *cap, std_input = FALSE;
867c478bd9Sstevel@tonic-gate 	int setuperr;
877c478bd9Sstevel@tonic-gate 
887c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
897c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
907c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate 	progname = argv[0];
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "ST:")) != EOF) {
977c478bd9Sstevel@tonic-gate 		switch (i) {
987c478bd9Sstevel@tonic-gate 		case 'T':
997c478bd9Sstevel@tonic-gate 			fildes = -1;
1007c478bd9Sstevel@tonic-gate 			(void) putenv("LINES=");
1017c478bd9Sstevel@tonic-gate 			(void) putenv("COLUMNS=");
1027c478bd9Sstevel@tonic-gate 			term = optarg;
1037c478bd9Sstevel@tonic-gate 			break;
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate 		case 'S':
1067c478bd9Sstevel@tonic-gate 			std_input = TRUE;
1077c478bd9Sstevel@tonic-gate 			break;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 		case '?':			/* FALLTHROUGH		*/
1107c478bd9Sstevel@tonic-gate 		usage:				/* FALLTHROUGH		*/
1117c478bd9Sstevel@tonic-gate 		default:
1127c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
113de81e71eSTim Marsland 			    "usage:\t%s [-T [term]] capname "
114de81e71eSTim Marsland 			    "[parm argument...]\n"), progname);
1157c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("OR:\t%s -S <<\n"),
1167c478bd9Sstevel@tonic-gate 			    progname);
1177c478bd9Sstevel@tonic-gate 			exit(2);
1187c478bd9Sstevel@tonic-gate 		}
1197c478bd9Sstevel@tonic-gate 	}
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate 	if (!term || !*term) {
1227c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1237c478bd9Sstevel@tonic-gate 		    gettext("%s: No value for $TERM and no -T specified\n"),
1247c478bd9Sstevel@tonic-gate 		    progname);
1257c478bd9Sstevel@tonic-gate 		exit(2);
1267c478bd9Sstevel@tonic-gate 	}
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 	(void) setupterm(term, fildes, &setuperr);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 	switch (setuperr) {
1317c478bd9Sstevel@tonic-gate 	case -2:
1327c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1337c478bd9Sstevel@tonic-gate 		    gettext("%s: unreadable terminal descriptor \"%s\"\n"),
1347c478bd9Sstevel@tonic-gate 		    progname, term);
1357c478bd9Sstevel@tonic-gate 		exit(3);
1367c478bd9Sstevel@tonic-gate 		break;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	case -1:
1397c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1407c478bd9Sstevel@tonic-gate 		    gettext("%s: no terminfo database\n"), progname);
1417c478bd9Sstevel@tonic-gate 		exit(3);
1427c478bd9Sstevel@tonic-gate 		break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	case 0:
1457c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
1467c478bd9Sstevel@tonic-gate 		    gettext("%s: unknown terminal \"%s\"\n"),
1477c478bd9Sstevel@tonic-gate 		    progname, term);
1487c478bd9Sstevel@tonic-gate 		exit(3);
1497c478bd9Sstevel@tonic-gate 	}
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 	reset_shell_mode();
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 	/* command line arguments */
1547c478bd9Sstevel@tonic-gate 	if (!std_input) {
1557c478bd9Sstevel@tonic-gate 		if (argc == optind)
1567c478bd9Sstevel@tonic-gate 			goto usage;
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 		cap = argv[optind++];
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 		if (strcmp(cap, "init") == 0)
1617c478bd9Sstevel@tonic-gate 			initterm();
1627c478bd9Sstevel@tonic-gate 		else if (strcmp(cap, "reset") == 0)
1637c478bd9Sstevel@tonic-gate 			reset_term();
1647c478bd9Sstevel@tonic-gate 		else if (strcmp(cap, "longname") == 0)
1657c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", longname());
1667c478bd9Sstevel@tonic-gate 		else
1677c478bd9Sstevel@tonic-gate 			exit(outputcap(cap, argc, argv));
1687c478bd9Sstevel@tonic-gate 		return (0);
1697c478bd9Sstevel@tonic-gate 	} else {			/* standard input argumets	*/
170e1c8dda0SGarrett D'Amore 		char buff[128];
1717c478bd9Sstevel@tonic-gate 		char **v;
1727c478bd9Sstevel@tonic-gate 
173e1c8dda0SGarrett D'Amore 		/* allocate storage for the 'faked' argv[] array	*/
1747c478bd9Sstevel@tonic-gate 		v = (char **)malloc(10 * sizeof (char *));
175e1c8dda0SGarrett D'Amore 		for (i = 0; i < 10; i++)
176e1c8dda0SGarrett D'Amore 			v[i] = (char *)malloc(32 * sizeof (char));
1777c478bd9Sstevel@tonic-gate 
178*d2358948SGary Mills 		while (fgets(buff, sizeof (buff), stdin) != NULL) {
1797c478bd9Sstevel@tonic-gate 			/* read standard input line; skip over empty lines */
1807c478bd9Sstevel@tonic-gate 			if ((std_argc =
181*d2358948SGary Mills 			    sscanf(buff,
182*d2358948SGary Mills 			    "%31s %31s %31s %31s %31s %31s %31s %31s "
183*d2358948SGary Mills 			    "%31s %31s",
1847c478bd9Sstevel@tonic-gate 			    v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7],
1857c478bd9Sstevel@tonic-gate 			    v[8], v[9])) < 1) {
1867c478bd9Sstevel@tonic-gate 				continue;
1877c478bd9Sstevel@tonic-gate 			}
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 			cap = v[0];
1907c478bd9Sstevel@tonic-gate 			optind = 1;
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 			if (strcmp(cap, "init") == 0) {
1937c478bd9Sstevel@tonic-gate 				initterm();
1947c478bd9Sstevel@tonic-gate 			} else if (strcmp(cap, "reset") == 0) {
1957c478bd9Sstevel@tonic-gate 				reset_term();
1967c478bd9Sstevel@tonic-gate 			} else if (strcmp(cap, "longname") == 0) {
1977c478bd9Sstevel@tonic-gate 				(void) printf("%s\n", longname());
1987c478bd9Sstevel@tonic-gate 			} else {
1997c478bd9Sstevel@tonic-gate 				(void) outputcap(cap, std_argc, v);
2007c478bd9Sstevel@tonic-gate 			}
2017c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
2027c478bd9Sstevel@tonic-gate 		}
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 		return (0);
2057c478bd9Sstevel@tonic-gate 	}
2067c478bd9Sstevel@tonic-gate }
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate static long parm[9] = {
2097c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0, 0
2107c478bd9Sstevel@tonic-gate };
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static int
outputcap(char * cap,int argc,char ** argv)2137c478bd9Sstevel@tonic-gate outputcap(char *cap, int argc, char **argv)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	int parmset = 0;
2167c478bd9Sstevel@tonic-gate 	char *thisstr;
2177c478bd9Sstevel@tonic-gate 	int i;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 	if ((i = tigetflag(cap)) >= 0)
2207c478bd9Sstevel@tonic-gate 		return (1 - i);
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	if ((i = tigetnum(cap)) >= -1) {
2237c478bd9Sstevel@tonic-gate 		(void) printf("%d\n", i);
2247c478bd9Sstevel@tonic-gate 		return (0);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 
2277c478bd9Sstevel@tonic-gate 	if ((thisstr = tigetstr(cap)) != (char *)-1) {
2287c478bd9Sstevel@tonic-gate 		if (!thisstr) {
2297c478bd9Sstevel@tonic-gate 			return (1);
2307c478bd9Sstevel@tonic-gate 		}
2317c478bd9Sstevel@tonic-gate 		for (parmset = 0; optind < argc; optind++, parmset++)
2327c478bd9Sstevel@tonic-gate 			if (allnumeric(argv[optind]))
2337c478bd9Sstevel@tonic-gate 				parm[parmset] = atoi(argv[optind]);
2347c478bd9Sstevel@tonic-gate 			else
2357c478bd9Sstevel@tonic-gate 				parm[parmset] = (int)argv[optind];
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 		if (parmset)
2387c478bd9Sstevel@tonic-gate 			putp(tparm(thisstr,
239de81e71eSTim Marsland 			    parm[0], parm[1], parm[2], parm[3],
240de81e71eSTim Marsland 			    parm[4], parm[5], parm[6], parm[7], parm[8]));
2417c478bd9Sstevel@tonic-gate 		else
2427c478bd9Sstevel@tonic-gate 			putp(thisstr);
2437c478bd9Sstevel@tonic-gate 		return (0);
2447c478bd9Sstevel@tonic-gate 	}
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
2477c478bd9Sstevel@tonic-gate 	    gettext("%s: unknown terminfo capability '%s'\n"), progname, cap);
2487c478bd9Sstevel@tonic-gate 
2497c478bd9Sstevel@tonic-gate 	exit(4);
2507c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  *  The decision as to whether an argument is a number or not is to simply
2557c478bd9Sstevel@tonic-gate  *  look at whether there are any non-digits in the string.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static int
allnumeric(char * string)2587c478bd9Sstevel@tonic-gate allnumeric(char *string)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	if (*string) {
2617c478bd9Sstevel@tonic-gate 		while (*string) {
2627c478bd9Sstevel@tonic-gate 			if (!isdigit(*string++)) {
2637c478bd9Sstevel@tonic-gate 				return (0);
2647c478bd9Sstevel@tonic-gate 			}
2657c478bd9Sstevel@tonic-gate 		}
2667c478bd9Sstevel@tonic-gate 		return (1);
2677c478bd9Sstevel@tonic-gate 	} else {
2687c478bd9Sstevel@tonic-gate 		return (0);
2697c478bd9Sstevel@tonic-gate 	}
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate /*
2737c478bd9Sstevel@tonic-gate  *  SYSTEM DEPENDENT TERMINAL DELAY TABLES
2747c478bd9Sstevel@tonic-gate  *
2757c478bd9Sstevel@tonic-gate  *	These tables maintain the correspondence between the delays
2767c478bd9Sstevel@tonic-gate  *	defined in terminfo and the delay algorithms in the tty driver
2777c478bd9Sstevel@tonic-gate  *	on the particular systems. For each type of delay, the bits used
2787c478bd9Sstevel@tonic-gate  *	for that delay must be specified, in XXbits, and a table
2797c478bd9Sstevel@tonic-gate  *	must be defined giving correspondences between delays and
2807c478bd9Sstevel@tonic-gate  *	algorithms. Algorithms which are not fixed delays, such
2817c478bd9Sstevel@tonic-gate  *	as dependent on current column or line number, must be
2827c478bd9Sstevel@tonic-gate  *	kludged in some way at this time.
2837c478bd9Sstevel@tonic-gate  *
2847c478bd9Sstevel@tonic-gate  *	Some of this was taken from tset(1).
2857c478bd9Sstevel@tonic-gate  */
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate struct delay
2887c478bd9Sstevel@tonic-gate {
2897c478bd9Sstevel@tonic-gate     int d_delay;
2907c478bd9Sstevel@tonic-gate     int d_bits;
2917c478bd9Sstevel@tonic-gate };
2927c478bd9Sstevel@tonic-gate 
2937c478bd9Sstevel@tonic-gate /* The appropriate speeds for various termio settings. */
2947c478bd9Sstevel@tonic-gate static int speeds[] = {
2957c478bd9Sstevel@tonic-gate 		0,	/*  B0,		*/
2967c478bd9Sstevel@tonic-gate 		50,	/*  B50,	*/
2977c478bd9Sstevel@tonic-gate 		75,	/*  B75,	*/
2987c478bd9Sstevel@tonic-gate 		110,	/*  B110,	*/
2997c478bd9Sstevel@tonic-gate 		134,	/*  B134,	*/
3007c478bd9Sstevel@tonic-gate 		150,	/*  B150,	*/
3017c478bd9Sstevel@tonic-gate 		200,	/*  B200,	*/
3027c478bd9Sstevel@tonic-gate 		300,	/*  B300,	*/
3037c478bd9Sstevel@tonic-gate 		600,	/*  B600,	*/
3047c478bd9Sstevel@tonic-gate 		1200,	/*  B1200,	*/
3057c478bd9Sstevel@tonic-gate 		1800,	/*  B1800,	*/
3067c478bd9Sstevel@tonic-gate 		2400,	/*  B2400,	*/
3077c478bd9Sstevel@tonic-gate 		4800,	/*  B4800,	*/
3087c478bd9Sstevel@tonic-gate 		9600,	/*  B9600,	*/
3097c478bd9Sstevel@tonic-gate 		19200,	/*  EXTA,	*/
3107c478bd9Sstevel@tonic-gate 		38400,	/*  EXTB,	*/
3117c478bd9Sstevel@tonic-gate 		57600,	/*  B57600,	*/
3127c478bd9Sstevel@tonic-gate 		76800,	/*  B76800,	*/
3137c478bd9Sstevel@tonic-gate 		115200,	/*  B115200,	*/
3147c478bd9Sstevel@tonic-gate 		153600,	/*  B153600,	*/
3157c478bd9Sstevel@tonic-gate 		230400,	/*  B230400,	*/
3167c478bd9Sstevel@tonic-gate 		307200,	/*  B307200,	*/
3177c478bd9Sstevel@tonic-gate 		460800,	/*  B460800,	*/
318de81e71eSTim Marsland 		921600, /*  B921600,	*/
3197c478bd9Sstevel@tonic-gate 		0,
3207c478bd9Sstevel@tonic-gate };
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
3237c478bd9Sstevel@tonic-gate /*	Unix 3.0 on up */
3247c478bd9Sstevel@tonic-gate 
3257c478bd9Sstevel@tonic-gate /*    Carriage Return delays	*/
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate static int	CRbits = CRDLY;
3287c478bd9Sstevel@tonic-gate static struct delay	CRdelay[] =
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	0,	CR0,
3317c478bd9Sstevel@tonic-gate 	80,	CR1,
3327c478bd9Sstevel@tonic-gate 	100,	CR2,
3337c478bd9Sstevel@tonic-gate 	150,	CR3,
3347c478bd9Sstevel@tonic-gate 	-1
3357c478bd9Sstevel@tonic-gate };
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*	New Line delays	*/
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate static int	NLbits = NLDLY;
3407c478bd9Sstevel@tonic-gate static struct delay	NLdelay[] =
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 	0,	NL0,
3437c478bd9Sstevel@tonic-gate 	100,	NL1,
3447c478bd9Sstevel@tonic-gate 	-1
3457c478bd9Sstevel@tonic-gate };
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate /*	Back Space delays	*/
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate static int	BSbits = BSDLY;
3507c478bd9Sstevel@tonic-gate static struct delay	BSdelay[] =
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	0,	BS0,
3537c478bd9Sstevel@tonic-gate 	50,	BS1,
3547c478bd9Sstevel@tonic-gate 	-1
3557c478bd9Sstevel@tonic-gate };
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate /*	TaB delays	*/
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate static int	TBbits = TABDLY;
3607c478bd9Sstevel@tonic-gate static struct delay	TBdelay[] =
3617c478bd9Sstevel@tonic-gate {
3627c478bd9Sstevel@tonic-gate 	0,	TAB0,
3637c478bd9Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
3647c478bd9Sstevel@tonic-gate 	100,	TAB2,
3657c478bd9Sstevel@tonic-gate 				/* TAB3 is XTABS and not a delay */
3667c478bd9Sstevel@tonic-gate 	-1
3677c478bd9Sstevel@tonic-gate };
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate /*	Form Feed delays	*/
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate static int	FFbits = FFDLY;
3727c478bd9Sstevel@tonic-gate static struct delay	FFdelay[] =
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 	0,	FF0,
3757c478bd9Sstevel@tonic-gate 	2000,	FF1,
3767c478bd9Sstevel@tonic-gate 	-1
3777c478bd9Sstevel@tonic-gate };
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate #else	/* BSD */
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate /*	Carriage Return delays	*/
3827c478bd9Sstevel@tonic-gate 
3837c478bd9Sstevel@tonic-gate int	CRbits = CRDELAY;
3847c478bd9Sstevel@tonic-gate struct delay	CRdelay[] =
3857c478bd9Sstevel@tonic-gate {
3867c478bd9Sstevel@tonic-gate 	0,	CR0,
3877c478bd9Sstevel@tonic-gate 	9,	CR3,
3887c478bd9Sstevel@tonic-gate 	80,	CR1,
3897c478bd9Sstevel@tonic-gate 	160,	CR2,
3907c478bd9Sstevel@tonic-gate 	-1
3917c478bd9Sstevel@tonic-gate };
3927c478bd9Sstevel@tonic-gate 
3937c478bd9Sstevel@tonic-gate /*	New Line delays	*/
3947c478bd9Sstevel@tonic-gate 
3957c478bd9Sstevel@tonic-gate int	NLbits = NLDELAY;
3967c478bd9Sstevel@tonic-gate struct delay	NLdelay[] =
3977c478bd9Sstevel@tonic-gate {
3987c478bd9Sstevel@tonic-gate 	0,	NL0,
3997c478bd9Sstevel@tonic-gate 	66,	NL1,		/* special M37 delay */
4007c478bd9Sstevel@tonic-gate 	100,	NL2,
4017c478bd9Sstevel@tonic-gate 	-1
4027c478bd9Sstevel@tonic-gate };
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate /*	Tab delays	*/
4057c478bd9Sstevel@tonic-gate 
4067c478bd9Sstevel@tonic-gate int	TBbits = TBDELAY;
4077c478bd9Sstevel@tonic-gate struct delay	TBdelay[] =
4087c478bd9Sstevel@tonic-gate {
4097c478bd9Sstevel@tonic-gate 	0,	TAB0,
4107c478bd9Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
4117c478bd9Sstevel@tonic-gate 	-1
4127c478bd9Sstevel@tonic-gate };
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*	Form Feed delays	*/
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate int	FFbits = VTDELAY;
4177c478bd9Sstevel@tonic-gate struct delay	FFdelay[] =
4187c478bd9Sstevel@tonic-gate {
4197c478bd9Sstevel@tonic-gate 	0,	FF0,
4207c478bd9Sstevel@tonic-gate 	2000,	FF1,
4217c478bd9Sstevel@tonic-gate 	-1
4227c478bd9Sstevel@tonic-gate };
4237c478bd9Sstevel@tonic-gate #endif	/* BSD */
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate /*
4267c478bd9Sstevel@tonic-gate  *  Initterm, a.k.a. reset_term, does terminal specific initialization. In
4277c478bd9Sstevel@tonic-gate  *  particular, the init_strings from terminfo are output and tabs are
4287c478bd9Sstevel@tonic-gate  *  set, if they aren't hardwired in. Much of this stuff was done by
4297c478bd9Sstevel@tonic-gate  *  the tset(1) program.
4307c478bd9Sstevel@tonic-gate  */
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate /*
4337c478bd9Sstevel@tonic-gate  *  Figure out how many milliseconds of padding the capability cap
4347c478bd9Sstevel@tonic-gate  *  needs and return that number. Padding is stored in the string as "$<n>",
4357c478bd9Sstevel@tonic-gate  *  where n is the number of milliseconds of padding. More than one
4367c478bd9Sstevel@tonic-gate  *  padding string is allowed within the string, although this is unlikely.
4377c478bd9Sstevel@tonic-gate  */
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate static int
getpad(char * cap)4407c478bd9Sstevel@tonic-gate getpad(char *cap)
4417c478bd9Sstevel@tonic-gate {
4427c478bd9Sstevel@tonic-gate 	int padding = 0;
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	/* No padding needed at speeds below padding_baud_rate */
4457c478bd9Sstevel@tonic-gate 	if (padding_baud_rate > CurrentBaudRate || cap == NULL)
4467c478bd9Sstevel@tonic-gate 		return (0);
4477c478bd9Sstevel@tonic-gate 
4487c478bd9Sstevel@tonic-gate 	while (*cap) {
4497c478bd9Sstevel@tonic-gate 		if ((cap[0] == '$') && (cap[1] == '<')) {
4507c478bd9Sstevel@tonic-gate 			cap++;
4517c478bd9Sstevel@tonic-gate 			cap++;
4527c478bd9Sstevel@tonic-gate 			padding += atoi(cap);
4537c478bd9Sstevel@tonic-gate 			while (isdigit (*cap))
4547c478bd9Sstevel@tonic-gate 				cap++;
4557c478bd9Sstevel@tonic-gate 			while (*cap == '.' || *cap == '/' || *cap == '*' ||
4567c478bd9Sstevel@tonic-gate 			    isdigit(*cap))
4577c478bd9Sstevel@tonic-gate 				cap++;
4587c478bd9Sstevel@tonic-gate 			while (*cap == '>')
4597c478bd9Sstevel@tonic-gate 				cap++;
4607c478bd9Sstevel@tonic-gate 		} else {
4617c478bd9Sstevel@tonic-gate 			cap++;
4627c478bd9Sstevel@tonic-gate 		}
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	return (padding);
4667c478bd9Sstevel@tonic-gate }
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate /*
4697c478bd9Sstevel@tonic-gate  *  Set the appropriate delay bits in the termio structure for
4707c478bd9Sstevel@tonic-gate  *  the given delay.
4717c478bd9Sstevel@tonic-gate  */
4727c478bd9Sstevel@tonic-gate static void
setdelay(delay,delaytable,bits,flags)4737c478bd9Sstevel@tonic-gate setdelay(delay, delaytable, bits, flags)
4747c478bd9Sstevel@tonic-gate register int delay;
4757c478bd9Sstevel@tonic-gate struct delay delaytable[];
4767c478bd9Sstevel@tonic-gate int bits;
4777c478bd9Sstevel@tonic-gate #ifdef SYSV
4787c478bd9Sstevel@tonic-gate tcflag_t *flags;
4797c478bd9Sstevel@tonic-gate #else	/* SYSV */
4807c478bd9Sstevel@tonic-gate unsigned short *flags;
4817c478bd9Sstevel@tonic-gate #endif	/* SYSV */
4827c478bd9Sstevel@tonic-gate {
4837c478bd9Sstevel@tonic-gate 	register struct delay  *p;
4847c478bd9Sstevel@tonic-gate 	register struct delay  *lastdelay;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	/* Clear out the bits, replace with new ones */
4877c478bd9Sstevel@tonic-gate 	*flags &= ~bits;
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	/* Scan the delay table for first entry with adequate delay */
4907c478bd9Sstevel@tonic-gate 	for (lastdelay = p = delaytable;
4917c478bd9Sstevel@tonic-gate 	    (p -> d_delay >= 0) && (p -> d_delay < delay);
4927c478bd9Sstevel@tonic-gate 	    p++) {
4937c478bd9Sstevel@tonic-gate 		lastdelay = p;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 
4967c478bd9Sstevel@tonic-gate 	/* use last entry if none will do */
4977c478bd9Sstevel@tonic-gate 	*flags |= lastdelay -> d_bits;
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate /*
5017c478bd9Sstevel@tonic-gate  * Set the hardware tabs on the terminal, using clear_all_tabs,
5027c478bd9Sstevel@tonic-gate  * set_tab, and column_address capabilities. Cursor_address and cursor_right
5037c478bd9Sstevel@tonic-gate  * may also be used, if necessary.
5047c478bd9Sstevel@tonic-gate  * This is done before the init_file and init_3string, so they can patch in
5057c478bd9Sstevel@tonic-gate  * case we blow this.
5067c478bd9Sstevel@tonic-gate  */
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate static void
settabs()5097c478bd9Sstevel@tonic-gate settabs()
5107c478bd9Sstevel@tonic-gate {
5117c478bd9Sstevel@tonic-gate 	register int c;
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	/* Do not set tabs if they power up properly. */
5147c478bd9Sstevel@tonic-gate 	if (init_tabs == 8)
5157c478bd9Sstevel@tonic-gate 		return;
5167c478bd9Sstevel@tonic-gate 
5177c478bd9Sstevel@tonic-gate 	if (set_tab) {
5187c478bd9Sstevel@tonic-gate 		/* Force the cursor to be at the left margin. */
5197c478bd9Sstevel@tonic-gate 		if (carriage_return)
5207c478bd9Sstevel@tonic-gate 			putp(carriage_return);
5217c478bd9Sstevel@tonic-gate 		else
5227c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 		/* Clear any current tab settings. */
5257c478bd9Sstevel@tonic-gate 		if (clear_all_tabs)
5267c478bd9Sstevel@tonic-gate 			putp(clear_all_tabs);
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate 		/* Set the tabs. */
5297c478bd9Sstevel@tonic-gate 		for (c = 8; c < columns; c += 8) {
5307c478bd9Sstevel@tonic-gate 			/* Get to that column. */
5317c478bd9Sstevel@tonic-gate 			(void) fputs("        ", stdout);
5327c478bd9Sstevel@tonic-gate 
5337c478bd9Sstevel@tonic-gate 			/* Set the tab. */
5347c478bd9Sstevel@tonic-gate 			putp(set_tab);
5357c478bd9Sstevel@tonic-gate 		}
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate 		/* Get back to the left column. */
5387c478bd9Sstevel@tonic-gate 		if (carriage_return)
5397c478bd9Sstevel@tonic-gate 			putp(carriage_return);
5407c478bd9Sstevel@tonic-gate 		else
5417c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate 	}
5447c478bd9Sstevel@tonic-gate }
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate /*
5477c478bd9Sstevel@tonic-gate  *  Copy "file" onto standard output.
5487c478bd9Sstevel@tonic-gate  */
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate static void
cat(file)5517c478bd9Sstevel@tonic-gate cat(file)
5527c478bd9Sstevel@tonic-gate char *file;				/* File to copy. */
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 	register int fd;			/* File descriptor. */
5557c478bd9Sstevel@tonic-gate 	register ssize_t i;			/* Number characters read. */
5567c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];			/* Buffer to read into. */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (fd < 0) {
5617c478bd9Sstevel@tonic-gate 		perror("Cannot open initialization file");
5627c478bd9Sstevel@tonic-gate 	} else {
5637c478bd9Sstevel@tonic-gate 		while ((i = read(fd, buf, BUFSIZ)) > (ssize_t)0)
5647c478bd9Sstevel@tonic-gate 			(void) write(fileno(stdout), buf, (unsigned)i);
5657c478bd9Sstevel@tonic-gate 		(int)close(fd);
5667c478bd9Sstevel@tonic-gate 	}
5677c478bd9Sstevel@tonic-gate }
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate /*
5707c478bd9Sstevel@tonic-gate  *  Initialize the terminal.
5717c478bd9Sstevel@tonic-gate  *  Send the initialization strings to the terminal.
5727c478bd9Sstevel@tonic-gate  */
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate static void
initterm()5757c478bd9Sstevel@tonic-gate initterm()
5767c478bd9Sstevel@tonic-gate {
5777c478bd9Sstevel@tonic-gate 	register int filedes;		/* File descriptor for ioctl's. */
5787c478bd9Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
5797c478bd9Sstevel@tonic-gate 	struct termio termmode;		/* To hold terminal settings. */
5807c478bd9Sstevel@tonic-gate 	struct termios termmodes;	/* To hold terminal settings. */
5817c478bd9Sstevel@tonic-gate 	int i;
5827c478bd9Sstevel@tonic-gate 	int istermios = -1;
5837c478bd9Sstevel@tonic-gate #define	GTTY(fd, mode)	ioctl(fd, TCGETA, mode)
5847c478bd9Sstevel@tonic-gate #define	GTTYS(fd, mode) \
5857c478bd9Sstevel@tonic-gate 	(istermios = ioctl(fd, TCGETS, mode))
5867c478bd9Sstevel@tonic-gate #define	STTY(fd, mode)	ioctl(fd, TCSETAW, mode)
5877c478bd9Sstevel@tonic-gate #define	STTYS(fd, mode)	ioctl(fd, TCSETSW, mode)
5887c478bd9Sstevel@tonic-gate #define	SPEED(mode)	(mode.c_cflag & CBAUD)
5897c478bd9Sstevel@tonic-gate #define	SPEEDS(mode)	(cfgetospeed(&mode))
5907c478bd9Sstevel@tonic-gate #define	OFLAG(mode)	mode.c_oflag
5917c478bd9Sstevel@tonic-gate #else	/* BSD */
5927c478bd9Sstevel@tonic-gate 	struct sgttyb termmode;		/* To hold terminal settings. */
5937c478bd9Sstevel@tonic-gate #define	GTTY(fd, mode)	gtty(fd, mode)
5947c478bd9Sstevel@tonic-gate #define	STTY(fd, mode)	stty(fd, mode)
5957c478bd9Sstevel@tonic-gate #define	SPEED(mode)	(mode.sg_ospeed & 017)
5967c478bd9Sstevel@tonic-gate #define	OFLAG(mode)	mode.sg_flags
5977c478bd9Sstevel@tonic-gate #define	TAB3		XTABS
5987c478bd9Sstevel@tonic-gate #endif
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	/* Get the terminal settings. */
6017c478bd9Sstevel@tonic-gate 	/* First try standard output, then standard error, */
6027c478bd9Sstevel@tonic-gate 	/* then standard input, then /dev/tty. */
6037c478bd9Sstevel@tonic-gate #ifdef SYSV
6047c478bd9Sstevel@tonic-gate 	if ((filedes = 1, GTTYS(filedes, &termmodes) < 0) ||
6057c478bd9Sstevel@tonic-gate 	    (filedes = 2, GTTYS(filedes, &termmodes) < 0) ||
6067c478bd9Sstevel@tonic-gate 	    (filedes = 0, GTTYS(filedes, &termmodes) < 0) ||
6077c478bd9Sstevel@tonic-gate 	    (filedes = open("/dev/tty", O_RDWR),
6087c478bd9Sstevel@tonic-gate 	    GTTYS(filedes, &termmodes) < 0)) {
6097c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6107c478bd9Sstevel@tonic-gate 		if ((filedes = 1, GTTY(filedes, &termmode) == -1) ||
6117c478bd9Sstevel@tonic-gate 		    (filedes = 2, GTTY(filedes, &termmode) == -1) ||
6127c478bd9Sstevel@tonic-gate 		    (filedes = 0, GTTY(filedes, &termmode) == -1) ||
6137c478bd9Sstevel@tonic-gate 		    (filedes = open("/dev/tty", O_RDWR),
6147c478bd9Sstevel@tonic-gate 		    GTTY(filedes, &termmode) == -1)) {
6157c478bd9Sstevel@tonic-gate 			filedes = -1;
6167c478bd9Sstevel@tonic-gate 			CurrentBaudRate = speeds[B1200];
6177c478bd9Sstevel@tonic-gate 		} else
6187c478bd9Sstevel@tonic-gate 			CurrentBaudRate = speeds[SPEED(termmode)];
6197c478bd9Sstevel@tonic-gate #ifdef SYSV
6207c478bd9Sstevel@tonic-gate 		termmodes.c_lflag = termmode.c_lflag;
6217c478bd9Sstevel@tonic-gate 		termmodes.c_oflag = termmode.c_oflag;
6227c478bd9Sstevel@tonic-gate 		termmodes.c_iflag = termmode.c_iflag;
6237c478bd9Sstevel@tonic-gate 		termmodes.c_cflag = termmode.c_cflag;
6247c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
6257c478bd9Sstevel@tonic-gate 			termmodes.c_cc[i] = termmode.c_cc[i];
6267c478bd9Sstevel@tonic-gate 	} else
6277c478bd9Sstevel@tonic-gate 		CurrentBaudRate = speeds[SPEEDS(termmodes)];
6287c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (xon_xoff) {
6317c478bd9Sstevel@tonic-gate #ifdef SYSV
6327c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) &=
6337c478bd9Sstevel@tonic-gate 		    ~(NLbits | CRbits | BSbits | FFbits | TBbits);
6347c478bd9Sstevel@tonic-gate #else	/* SYSV */
6357c478bd9Sstevel@tonic-gate 		OFLAG(termmode) &=
6367c478bd9Sstevel@tonic-gate 		    ~(NLbits | CRbits | BSbits | FFbits | TBbits);
6377c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6387c478bd9Sstevel@tonic-gate 	} else {
6397c478bd9Sstevel@tonic-gate #ifdef SYSV
640de81e71eSTim Marsland 		setdelay(getpad(carriage_return),
641de81e71eSTim Marsland 		    CRdelay, CRbits, &OFLAG(termmodes));
642de81e71eSTim Marsland 		setdelay(getpad(scroll_forward),
643de81e71eSTim Marsland 		    NLdelay, NLbits, &OFLAG(termmodes));
644de81e71eSTim Marsland 		setdelay(getpad(cursor_left),
645de81e71eSTim Marsland 		    BSdelay, BSbits, &OFLAG(termmodes));
646de81e71eSTim Marsland 		setdelay(getpad(form_feed),
647de81e71eSTim Marsland 		    FFdelay, FFbits, &OFLAG(termmodes));
648de81e71eSTim Marsland 		setdelay(getpad(tab),
649de81e71eSTim Marsland 		    TBdelay, TBbits, &OFLAG(termmodes));
6507c478bd9Sstevel@tonic-gate #else	/* SYSV */
651de81e71eSTim Marsland 		setdelay(getpad(carriage_return),
652de81e71eSTim Marsland 		    CRdelay, CRbits, &OFLAG(termmode));
653de81e71eSTim Marsland 		setdelay(getpad(scroll_forward),
654de81e71eSTim Marsland 		    NLdelay, NLbits, &OFLAG(termmode));
655de81e71eSTim Marsland 		setdelay(getpad(cursor_left),
656de81e71eSTim Marsland 		    BSdelay, BSbits, &OFLAG(termmode));
657de81e71eSTim Marsland 		setdelay(getpad(form_feed),
658de81e71eSTim Marsland 		    FFdelay, FFbits, &OFLAG(termmode));
659de81e71eSTim Marsland 		setdelay(getpad(tab),
660de81e71eSTim Marsland 		    TBdelay, TBbits, &OFLAG(termmode));
6617c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6627c478bd9Sstevel@tonic-gate 	}
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	/* If tabs can be sent to the tty, turn off their expansion. */
6657c478bd9Sstevel@tonic-gate 	if (tab && set_tab || init_tabs == 8) {
6667c478bd9Sstevel@tonic-gate #ifdef SYSV
6677c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) &= ~(TAB3);
6687c478bd9Sstevel@tonic-gate #else	/* SYSV */
6697c478bd9Sstevel@tonic-gate 		OFLAG(termmode) &= ~(TAB3);
6707c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6717c478bd9Sstevel@tonic-gate 	} else {
6727c478bd9Sstevel@tonic-gate #ifdef SYSV
6737c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) |= TAB3;
6747c478bd9Sstevel@tonic-gate #else	/* SYSV */
6757c478bd9Sstevel@tonic-gate 		OFLAG(termmode) |= TAB3;
6767c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6777c478bd9Sstevel@tonic-gate 	}
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate 	/* Do the changes to the terminal settings */
6807c478bd9Sstevel@tonic-gate #ifdef SYSV
6817c478bd9Sstevel@tonic-gate 	if (istermios < 0) {
6827c478bd9Sstevel@tonic-gate 		int i;
6837c478bd9Sstevel@tonic-gate 
6847c478bd9Sstevel@tonic-gate 		termmode.c_lflag = termmodes.c_lflag;
6857c478bd9Sstevel@tonic-gate 		termmode.c_oflag = termmodes.c_oflag;
6867c478bd9Sstevel@tonic-gate 		termmode.c_iflag = termmodes.c_iflag;
6877c478bd9Sstevel@tonic-gate 		termmode.c_cflag = termmodes.c_cflag;
6887c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
6897c478bd9Sstevel@tonic-gate 			termmode.c_cc[i] = termmodes.c_cc[i];
6907c478bd9Sstevel@tonic-gate 		(void) STTY(filedes, &termmode);
6917c478bd9Sstevel@tonic-gate 	} else
6927c478bd9Sstevel@tonic-gate 		(void) STTYS(filedes, &termmodes);
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate #else	/* SYSV */
6957c478bd9Sstevel@tonic-gate 	(void) STTY(filedes, &termmode);
6967c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 	/* Send first initialization strings. */
6997c478bd9Sstevel@tonic-gate 	if (init_prog)
7007c478bd9Sstevel@tonic-gate 	(void) system(init_prog);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	if (reset && reset_1string) {
7037c478bd9Sstevel@tonic-gate 		putp(reset_1string);
7047c478bd9Sstevel@tonic-gate 	} else if (init_1string) {
7057c478bd9Sstevel@tonic-gate 		putp(init_1string);
7067c478bd9Sstevel@tonic-gate 	}
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate 	if (reset && reset_2string) {
7097c478bd9Sstevel@tonic-gate 		putp(reset_2string);
7107c478bd9Sstevel@tonic-gate 	} else if (init_2string) {
7117c478bd9Sstevel@tonic-gate 		putp(init_2string);
7127c478bd9Sstevel@tonic-gate 	}
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	/* Set up the tabs stops. */
7157c478bd9Sstevel@tonic-gate 	settabs();
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate 	/* Send out initializing file. */
7187c478bd9Sstevel@tonic-gate 	if (reset && reset_file) {
7197c478bd9Sstevel@tonic-gate 		cat(reset_file);
7207c478bd9Sstevel@tonic-gate 	} else if (init_file) {
7217c478bd9Sstevel@tonic-gate 		cat(init_file);
7227c478bd9Sstevel@tonic-gate 	}
7237c478bd9Sstevel@tonic-gate 
7247c478bd9Sstevel@tonic-gate 	/* Send final initialization strings. */
7257c478bd9Sstevel@tonic-gate 	if (reset && reset_3string) {
7267c478bd9Sstevel@tonic-gate 		putp(reset_3string);
7277c478bd9Sstevel@tonic-gate 	} else if (init_3string) {
7287c478bd9Sstevel@tonic-gate 		putp(init_3string);
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	if (carriage_return) {
7327c478bd9Sstevel@tonic-gate 		putp(carriage_return);
7337c478bd9Sstevel@tonic-gate 	} else {
7347c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
7357c478bd9Sstevel@tonic-gate 	}
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	/* Send color initialization strings */
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (orig_colors)
7407c478bd9Sstevel@tonic-gate 		putp(orig_colors);
7417c478bd9Sstevel@tonic-gate 
7427c478bd9Sstevel@tonic-gate 	if (orig_pair)
7437c478bd9Sstevel@tonic-gate 	putp(orig_pair);
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	/* Let the terminal settle down. */
7467c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
7477c478bd9Sstevel@tonic-gate 	(void) sleep(1);
7487c478bd9Sstevel@tonic-gate }
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate static void
reset_term()7517c478bd9Sstevel@tonic-gate reset_term()
7527c478bd9Sstevel@tonic-gate {
7537c478bd9Sstevel@tonic-gate 	reset++;
7547c478bd9Sstevel@tonic-gate 	initterm();
7557c478bd9Sstevel@tonic-gate }
756