xref: /illumos-gate/usr/src/cmd/tput/tput.c (revision d9c3e05c)
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.
25d2358948SGary 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"),
116de81e71eSTim Marsland 			    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,
123de81e71eSTim Marsland 		    gettext("%s: No value for $TERM and no -T specified\n"),
124de81e71eSTim Marsland 		    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,
133de81e71eSTim Marsland 		    gettext("%s: unreadable terminal descriptor \"%s\"\n"),
134de81e71eSTim Marsland 		    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,
140de81e71eSTim Marsland 		    gettext("%s: no terminfo database\n"), progname);
1417c478bd9Sstevel@tonic-gate 		exit(3);
1427c478bd9Sstevel@tonic-gate 		break;
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	case 0:
145de81e71eSTim Marsland 		(void) fprintf(stderr,
146de81e71eSTim Marsland 		    gettext("%s: unknown terminal \"%s\"\n"),
147de81e71eSTim Marsland 		    progname, term);
148de81e71eSTim Marsland 		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 
178d2358948SGary 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 =
181d2358948SGary Mills 			    sscanf(buff,
182d2358948SGary Mills 			    "%31s %31s %31s %31s %31s %31s %31s %31s "
183d2358948SGary 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[] = {
295*d9c3e05cSJoshua M. Clulow 	0,		/* B0		*/
296*d9c3e05cSJoshua M. Clulow 	50,		/* B50		*/
297*d9c3e05cSJoshua M. Clulow 	75,		/* B75		*/
298*d9c3e05cSJoshua M. Clulow 	110,		/* B110		*/
299*d9c3e05cSJoshua M. Clulow 	134,		/* B134		*/
300*d9c3e05cSJoshua M. Clulow 	150,		/* B150		*/
301*d9c3e05cSJoshua M. Clulow 	200,		/* B200		*/
302*d9c3e05cSJoshua M. Clulow 	300,		/* B300		*/
303*d9c3e05cSJoshua M. Clulow 	600,		/* B600		*/
304*d9c3e05cSJoshua M. Clulow 	1200,		/* B1200	*/
305*d9c3e05cSJoshua M. Clulow 	1800,		/* B1800	*/
306*d9c3e05cSJoshua M. Clulow 	2400,		/* B2400	*/
307*d9c3e05cSJoshua M. Clulow 	4800,		/* B4800	*/
308*d9c3e05cSJoshua M. Clulow 	9600,		/* B9600	*/
309*d9c3e05cSJoshua M. Clulow 	19200,		/* B19200	*/
310*d9c3e05cSJoshua M. Clulow 	38400,		/* B38400	*/
311*d9c3e05cSJoshua M. Clulow 	57600,		/* B57600	*/
312*d9c3e05cSJoshua M. Clulow 	76800,		/* B76800	*/
313*d9c3e05cSJoshua M. Clulow 	115200,		/* B115200	*/
314*d9c3e05cSJoshua M. Clulow 	153600,		/* B153600	*/
315*d9c3e05cSJoshua M. Clulow 	230400,		/* B230400	*/
316*d9c3e05cSJoshua M. Clulow 	307200,		/* B307200	*/
317*d9c3e05cSJoshua M. Clulow 	460800,		/* B460800	*/
318*d9c3e05cSJoshua M. Clulow 	921600,		/* B921600	*/
319*d9c3e05cSJoshua M. Clulow 	1000000,	/* B1000000	*/
320*d9c3e05cSJoshua M. Clulow 	1152000,	/* B1152000	*/
321*d9c3e05cSJoshua M. Clulow 	1500000,	/* B1500000	*/
322*d9c3e05cSJoshua M. Clulow 	2000000,	/* B2000000	*/
323*d9c3e05cSJoshua M. Clulow 	2500000,	/* B2500000	*/
324*d9c3e05cSJoshua M. Clulow 	3000000,	/* B3000000	*/
325*d9c3e05cSJoshua M. Clulow 	3500000,	/* B3500000	*/
326*d9c3e05cSJoshua M. Clulow 	4000000,	/* B4000000	*/
327*d9c3e05cSJoshua M. Clulow 	0
3287c478bd9Sstevel@tonic-gate };
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
3317c478bd9Sstevel@tonic-gate /*	Unix 3.0 on up */
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate /*    Carriage Return delays	*/
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate static int	CRbits = CRDLY;
3367c478bd9Sstevel@tonic-gate static struct delay	CRdelay[] =
3377c478bd9Sstevel@tonic-gate {
3387c478bd9Sstevel@tonic-gate 	0,	CR0,
3397c478bd9Sstevel@tonic-gate 	80,	CR1,
3407c478bd9Sstevel@tonic-gate 	100,	CR2,
3417c478bd9Sstevel@tonic-gate 	150,	CR3,
3427c478bd9Sstevel@tonic-gate 	-1
3437c478bd9Sstevel@tonic-gate };
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /*	New Line delays	*/
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate static int	NLbits = NLDLY;
3487c478bd9Sstevel@tonic-gate static struct delay	NLdelay[] =
3497c478bd9Sstevel@tonic-gate {
3507c478bd9Sstevel@tonic-gate 	0,	NL0,
3517c478bd9Sstevel@tonic-gate 	100,	NL1,
3527c478bd9Sstevel@tonic-gate 	-1
3537c478bd9Sstevel@tonic-gate };
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate /*	Back Space delays	*/
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate static int	BSbits = BSDLY;
3587c478bd9Sstevel@tonic-gate static struct delay	BSdelay[] =
3597c478bd9Sstevel@tonic-gate {
3607c478bd9Sstevel@tonic-gate 	0,	BS0,
3617c478bd9Sstevel@tonic-gate 	50,	BS1,
3627c478bd9Sstevel@tonic-gate 	-1
3637c478bd9Sstevel@tonic-gate };
3647c478bd9Sstevel@tonic-gate 
3657c478bd9Sstevel@tonic-gate /*	TaB delays	*/
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate static int	TBbits = TABDLY;
3687c478bd9Sstevel@tonic-gate static struct delay	TBdelay[] =
3697c478bd9Sstevel@tonic-gate {
3707c478bd9Sstevel@tonic-gate 	0,	TAB0,
3717c478bd9Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
3727c478bd9Sstevel@tonic-gate 	100,	TAB2,
3737c478bd9Sstevel@tonic-gate 				/* TAB3 is XTABS and not a delay */
3747c478bd9Sstevel@tonic-gate 	-1
3757c478bd9Sstevel@tonic-gate };
3767c478bd9Sstevel@tonic-gate 
3777c478bd9Sstevel@tonic-gate /*	Form Feed delays	*/
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate static int	FFbits = FFDLY;
3807c478bd9Sstevel@tonic-gate static struct delay	FFdelay[] =
3817c478bd9Sstevel@tonic-gate {
3827c478bd9Sstevel@tonic-gate 	0,	FF0,
3837c478bd9Sstevel@tonic-gate 	2000,	FF1,
3847c478bd9Sstevel@tonic-gate 	-1
3857c478bd9Sstevel@tonic-gate };
3867c478bd9Sstevel@tonic-gate 
3877c478bd9Sstevel@tonic-gate #else	/* BSD */
3887c478bd9Sstevel@tonic-gate 
3897c478bd9Sstevel@tonic-gate /*	Carriage Return delays	*/
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate int	CRbits = CRDELAY;
3927c478bd9Sstevel@tonic-gate struct delay	CRdelay[] =
3937c478bd9Sstevel@tonic-gate {
3947c478bd9Sstevel@tonic-gate 	0,	CR0,
3957c478bd9Sstevel@tonic-gate 	9,	CR3,
3967c478bd9Sstevel@tonic-gate 	80,	CR1,
3977c478bd9Sstevel@tonic-gate 	160,	CR2,
3987c478bd9Sstevel@tonic-gate 	-1
3997c478bd9Sstevel@tonic-gate };
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate /*	New Line delays	*/
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate int	NLbits = NLDELAY;
4047c478bd9Sstevel@tonic-gate struct delay	NLdelay[] =
4057c478bd9Sstevel@tonic-gate {
4067c478bd9Sstevel@tonic-gate 	0,	NL0,
4077c478bd9Sstevel@tonic-gate 	66,	NL1,		/* special M37 delay */
4087c478bd9Sstevel@tonic-gate 	100,	NL2,
4097c478bd9Sstevel@tonic-gate 	-1
4107c478bd9Sstevel@tonic-gate };
4117c478bd9Sstevel@tonic-gate 
4127c478bd9Sstevel@tonic-gate /*	Tab delays	*/
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate int	TBbits = TBDELAY;
4157c478bd9Sstevel@tonic-gate struct delay	TBdelay[] =
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 	0,	TAB0,
4187c478bd9Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
4197c478bd9Sstevel@tonic-gate 	-1
4207c478bd9Sstevel@tonic-gate };
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate /*	Form Feed delays	*/
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate int	FFbits = VTDELAY;
4257c478bd9Sstevel@tonic-gate struct delay	FFdelay[] =
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	0,	FF0,
4287c478bd9Sstevel@tonic-gate 	2000,	FF1,
4297c478bd9Sstevel@tonic-gate 	-1
4307c478bd9Sstevel@tonic-gate };
4317c478bd9Sstevel@tonic-gate #endif	/* BSD */
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  *  Initterm, a.k.a. reset_term, does terminal specific initialization. In
4357c478bd9Sstevel@tonic-gate  *  particular, the init_strings from terminfo are output and tabs are
4367c478bd9Sstevel@tonic-gate  *  set, if they aren't hardwired in. Much of this stuff was done by
4377c478bd9Sstevel@tonic-gate  *  the tset(1) program.
4387c478bd9Sstevel@tonic-gate  */
4397c478bd9Sstevel@tonic-gate 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  *  Figure out how many milliseconds of padding the capability cap
4427c478bd9Sstevel@tonic-gate  *  needs and return that number. Padding is stored in the string as "$<n>",
4437c478bd9Sstevel@tonic-gate  *  where n is the number of milliseconds of padding. More than one
4447c478bd9Sstevel@tonic-gate  *  padding string is allowed within the string, although this is unlikely.
4457c478bd9Sstevel@tonic-gate  */
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate static int
getpad(char * cap)4487c478bd9Sstevel@tonic-gate getpad(char *cap)
4497c478bd9Sstevel@tonic-gate {
4507c478bd9Sstevel@tonic-gate 	int padding = 0;
4517c478bd9Sstevel@tonic-gate 
4527c478bd9Sstevel@tonic-gate 	/* No padding needed at speeds below padding_baud_rate */
4537c478bd9Sstevel@tonic-gate 	if (padding_baud_rate > CurrentBaudRate || cap == NULL)
4547c478bd9Sstevel@tonic-gate 		return (0);
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	while (*cap) {
4577c478bd9Sstevel@tonic-gate 		if ((cap[0] == '$') && (cap[1] == '<')) {
4587c478bd9Sstevel@tonic-gate 			cap++;
4597c478bd9Sstevel@tonic-gate 			cap++;
4607c478bd9Sstevel@tonic-gate 			padding += atoi(cap);
4617c478bd9Sstevel@tonic-gate 			while (isdigit (*cap))
4627c478bd9Sstevel@tonic-gate 				cap++;
4637c478bd9Sstevel@tonic-gate 			while (*cap == '.' || *cap == '/' || *cap == '*' ||
464de81e71eSTim Marsland 			    isdigit(*cap))
4657c478bd9Sstevel@tonic-gate 				cap++;
4667c478bd9Sstevel@tonic-gate 			while (*cap == '>')
4677c478bd9Sstevel@tonic-gate 				cap++;
4687c478bd9Sstevel@tonic-gate 		} else {
4697c478bd9Sstevel@tonic-gate 			cap++;
4707c478bd9Sstevel@tonic-gate 		}
4717c478bd9Sstevel@tonic-gate 	}
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 	return (padding);
4747c478bd9Sstevel@tonic-gate }
4757c478bd9Sstevel@tonic-gate 
4767c478bd9Sstevel@tonic-gate /*
4777c478bd9Sstevel@tonic-gate  *  Set the appropriate delay bits in the termio structure for
4787c478bd9Sstevel@tonic-gate  *  the given delay.
4797c478bd9Sstevel@tonic-gate  */
4807c478bd9Sstevel@tonic-gate static void
setdelay(delay,delaytable,bits,flags)4817c478bd9Sstevel@tonic-gate setdelay(delay, delaytable, bits, flags)
4827c478bd9Sstevel@tonic-gate register int delay;
4837c478bd9Sstevel@tonic-gate struct delay delaytable[];
4847c478bd9Sstevel@tonic-gate int bits;
4857c478bd9Sstevel@tonic-gate #ifdef SYSV
4867c478bd9Sstevel@tonic-gate tcflag_t *flags;
4877c478bd9Sstevel@tonic-gate #else	/* SYSV */
4887c478bd9Sstevel@tonic-gate unsigned short *flags;
4897c478bd9Sstevel@tonic-gate #endif	/* SYSV */
4907c478bd9Sstevel@tonic-gate {
4917c478bd9Sstevel@tonic-gate 	register struct delay  *p;
4927c478bd9Sstevel@tonic-gate 	register struct delay  *lastdelay;
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate 	/* Clear out the bits, replace with new ones */
4957c478bd9Sstevel@tonic-gate 	*flags &= ~bits;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 	/* Scan the delay table for first entry with adequate delay */
4987c478bd9Sstevel@tonic-gate 	for (lastdelay = p = delaytable;
4997c478bd9Sstevel@tonic-gate 	    (p -> d_delay >= 0) && (p -> d_delay < delay);
5007c478bd9Sstevel@tonic-gate 	    p++) {
5017c478bd9Sstevel@tonic-gate 		lastdelay = p;
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 	/* use last entry if none will do */
5057c478bd9Sstevel@tonic-gate 	*flags |= lastdelay -> d_bits;
5067c478bd9Sstevel@tonic-gate }
5077c478bd9Sstevel@tonic-gate 
5087c478bd9Sstevel@tonic-gate /*
5097c478bd9Sstevel@tonic-gate  * Set the hardware tabs on the terminal, using clear_all_tabs,
5107c478bd9Sstevel@tonic-gate  * set_tab, and column_address capabilities. Cursor_address and cursor_right
5117c478bd9Sstevel@tonic-gate  * may also be used, if necessary.
5127c478bd9Sstevel@tonic-gate  * This is done before the init_file and init_3string, so they can patch in
5137c478bd9Sstevel@tonic-gate  * case we blow this.
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate static void
settabs()5177c478bd9Sstevel@tonic-gate settabs()
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	register int c;
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate 	/* Do not set tabs if they power up properly. */
5227c478bd9Sstevel@tonic-gate 	if (init_tabs == 8)
5237c478bd9Sstevel@tonic-gate 		return;
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 	if (set_tab) {
5267c478bd9Sstevel@tonic-gate 		/* Force the cursor to be at the left margin. */
5277c478bd9Sstevel@tonic-gate 		if (carriage_return)
5287c478bd9Sstevel@tonic-gate 			putp(carriage_return);
5297c478bd9Sstevel@tonic-gate 		else
5307c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 		/* Clear any current tab settings. */
5337c478bd9Sstevel@tonic-gate 		if (clear_all_tabs)
5347c478bd9Sstevel@tonic-gate 			putp(clear_all_tabs);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 		/* Set the tabs. */
5377c478bd9Sstevel@tonic-gate 		for (c = 8; c < columns; c += 8) {
5387c478bd9Sstevel@tonic-gate 			/* Get to that column. */
5397c478bd9Sstevel@tonic-gate 			(void) fputs("        ", stdout);
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 			/* Set the tab. */
5427c478bd9Sstevel@tonic-gate 			putp(set_tab);
5437c478bd9Sstevel@tonic-gate 		}
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 		/* Get back to the left column. */
5467c478bd9Sstevel@tonic-gate 		if (carriage_return)
5477c478bd9Sstevel@tonic-gate 			putp(carriage_return);
5487c478bd9Sstevel@tonic-gate 		else
5497c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate }
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate /*
5557c478bd9Sstevel@tonic-gate  *  Copy "file" onto standard output.
5567c478bd9Sstevel@tonic-gate  */
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate static void
cat(file)5597c478bd9Sstevel@tonic-gate cat(file)
5607c478bd9Sstevel@tonic-gate char *file;				/* File to copy. */
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	register int fd;			/* File descriptor. */
5637c478bd9Sstevel@tonic-gate 	register ssize_t i;			/* Number characters read. */
5647c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];			/* Buffer to read into. */
5657c478bd9Sstevel@tonic-gate 
5667c478bd9Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
5677c478bd9Sstevel@tonic-gate 
5687c478bd9Sstevel@tonic-gate 	if (fd < 0) {
5697c478bd9Sstevel@tonic-gate 		perror("Cannot open initialization file");
5707c478bd9Sstevel@tonic-gate 	} else {
5717c478bd9Sstevel@tonic-gate 		while ((i = read(fd, buf, BUFSIZ)) > (ssize_t)0)
5727c478bd9Sstevel@tonic-gate 			(void) write(fileno(stdout), buf, (unsigned)i);
5737c478bd9Sstevel@tonic-gate 		(int)close(fd);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate }
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate /*
5787c478bd9Sstevel@tonic-gate  *  Initialize the terminal.
5797c478bd9Sstevel@tonic-gate  *  Send the initialization strings to the terminal.
5807c478bd9Sstevel@tonic-gate  */
5817c478bd9Sstevel@tonic-gate 
5827c478bd9Sstevel@tonic-gate static void
initterm()5837c478bd9Sstevel@tonic-gate initterm()
5847c478bd9Sstevel@tonic-gate {
5857c478bd9Sstevel@tonic-gate 	register int filedes;		/* File descriptor for ioctl's. */
5867c478bd9Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
5877c478bd9Sstevel@tonic-gate 	struct termio termmode;		/* To hold terminal settings. */
5887c478bd9Sstevel@tonic-gate 	struct termios termmodes;	/* To hold terminal settings. */
5897c478bd9Sstevel@tonic-gate 	int i;
5907c478bd9Sstevel@tonic-gate 	int istermios = -1;
5917c478bd9Sstevel@tonic-gate #define	GTTY(fd, mode)	ioctl(fd, TCGETA, mode)
5927c478bd9Sstevel@tonic-gate #define	GTTYS(fd, mode) \
5937c478bd9Sstevel@tonic-gate 	(istermios = ioctl(fd, TCGETS, mode))
5947c478bd9Sstevel@tonic-gate #define	STTY(fd, mode)	ioctl(fd, TCSETAW, mode)
5957c478bd9Sstevel@tonic-gate #define	STTYS(fd, mode)	ioctl(fd, TCSETSW, mode)
5967c478bd9Sstevel@tonic-gate #define	SPEED(mode)	(mode.c_cflag & CBAUD)
5977c478bd9Sstevel@tonic-gate #define	SPEEDS(mode)	(cfgetospeed(&mode))
5987c478bd9Sstevel@tonic-gate #define	OFLAG(mode)	mode.c_oflag
5997c478bd9Sstevel@tonic-gate #else	/* BSD */
6007c478bd9Sstevel@tonic-gate 	struct sgttyb termmode;		/* To hold terminal settings. */
6017c478bd9Sstevel@tonic-gate #define	GTTY(fd, mode)	gtty(fd, mode)
6027c478bd9Sstevel@tonic-gate #define	STTY(fd, mode)	stty(fd, mode)
6037c478bd9Sstevel@tonic-gate #define	SPEED(mode)	(mode.sg_ospeed & 017)
6047c478bd9Sstevel@tonic-gate #define	OFLAG(mode)	mode.sg_flags
6057c478bd9Sstevel@tonic-gate #define	TAB3		XTABS
6067c478bd9Sstevel@tonic-gate #endif
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	/* Get the terminal settings. */
6097c478bd9Sstevel@tonic-gate 	/* First try standard output, then standard error, */
6107c478bd9Sstevel@tonic-gate 	/* then standard input, then /dev/tty. */
6117c478bd9Sstevel@tonic-gate #ifdef SYSV
6127c478bd9Sstevel@tonic-gate 	if ((filedes = 1, GTTYS(filedes, &termmodes) < 0) ||
6137c478bd9Sstevel@tonic-gate 	    (filedes = 2, GTTYS(filedes, &termmodes) < 0) ||
6147c478bd9Sstevel@tonic-gate 	    (filedes = 0, GTTYS(filedes, &termmodes) < 0) ||
6157c478bd9Sstevel@tonic-gate 	    (filedes = open("/dev/tty", O_RDWR),
6167c478bd9Sstevel@tonic-gate 	    GTTYS(filedes, &termmodes) < 0)) {
6177c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6187c478bd9Sstevel@tonic-gate 		if ((filedes = 1, GTTY(filedes, &termmode) == -1) ||
6197c478bd9Sstevel@tonic-gate 		    (filedes = 2, GTTY(filedes, &termmode) == -1) ||
6207c478bd9Sstevel@tonic-gate 		    (filedes = 0, GTTY(filedes, &termmode) == -1) ||
6217c478bd9Sstevel@tonic-gate 		    (filedes = open("/dev/tty", O_RDWR),
6227c478bd9Sstevel@tonic-gate 		    GTTY(filedes, &termmode) == -1)) {
6237c478bd9Sstevel@tonic-gate 			filedes = -1;
6247c478bd9Sstevel@tonic-gate 			CurrentBaudRate = speeds[B1200];
6257c478bd9Sstevel@tonic-gate 		} else
6267c478bd9Sstevel@tonic-gate 			CurrentBaudRate = speeds[SPEED(termmode)];
6277c478bd9Sstevel@tonic-gate #ifdef SYSV
6287c478bd9Sstevel@tonic-gate 		termmodes.c_lflag = termmode.c_lflag;
6297c478bd9Sstevel@tonic-gate 		termmodes.c_oflag = termmode.c_oflag;
6307c478bd9Sstevel@tonic-gate 		termmodes.c_iflag = termmode.c_iflag;
6317c478bd9Sstevel@tonic-gate 		termmodes.c_cflag = termmode.c_cflag;
6327c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
6337c478bd9Sstevel@tonic-gate 			termmodes.c_cc[i] = termmode.c_cc[i];
6347c478bd9Sstevel@tonic-gate 	} else
6357c478bd9Sstevel@tonic-gate 		CurrentBaudRate = speeds[SPEEDS(termmodes)];
6367c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6377c478bd9Sstevel@tonic-gate 
6387c478bd9Sstevel@tonic-gate 	if (xon_xoff) {
6397c478bd9Sstevel@tonic-gate #ifdef SYSV
6407c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) &=
641de81e71eSTim Marsland 		    ~(NLbits | CRbits | BSbits | FFbits | TBbits);
6427c478bd9Sstevel@tonic-gate #else	/* SYSV */
6437c478bd9Sstevel@tonic-gate 		OFLAG(termmode) &=
644de81e71eSTim Marsland 		    ~(NLbits | CRbits | BSbits | FFbits | TBbits);
6457c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6467c478bd9Sstevel@tonic-gate 	} else {
6477c478bd9Sstevel@tonic-gate #ifdef SYSV
648de81e71eSTim Marsland 		setdelay(getpad(carriage_return),
649de81e71eSTim Marsland 		    CRdelay, CRbits, &OFLAG(termmodes));
650de81e71eSTim Marsland 		setdelay(getpad(scroll_forward),
651de81e71eSTim Marsland 		    NLdelay, NLbits, &OFLAG(termmodes));
652de81e71eSTim Marsland 		setdelay(getpad(cursor_left),
653de81e71eSTim Marsland 		    BSdelay, BSbits, &OFLAG(termmodes));
654de81e71eSTim Marsland 		setdelay(getpad(form_feed),
655de81e71eSTim Marsland 		    FFdelay, FFbits, &OFLAG(termmodes));
656de81e71eSTim Marsland 		setdelay(getpad(tab),
657de81e71eSTim Marsland 		    TBdelay, TBbits, &OFLAG(termmodes));
6587c478bd9Sstevel@tonic-gate #else	/* SYSV */
659de81e71eSTim Marsland 		setdelay(getpad(carriage_return),
660de81e71eSTim Marsland 		    CRdelay, CRbits, &OFLAG(termmode));
661de81e71eSTim Marsland 		setdelay(getpad(scroll_forward),
662de81e71eSTim Marsland 		    NLdelay, NLbits, &OFLAG(termmode));
663de81e71eSTim Marsland 		setdelay(getpad(cursor_left),
664de81e71eSTim Marsland 		    BSdelay, BSbits, &OFLAG(termmode));
665de81e71eSTim Marsland 		setdelay(getpad(form_feed),
666de81e71eSTim Marsland 		    FFdelay, FFbits, &OFLAG(termmode));
667de81e71eSTim Marsland 		setdelay(getpad(tab),
668de81e71eSTim Marsland 		    TBdelay, TBbits, &OFLAG(termmode));
6697c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6707c478bd9Sstevel@tonic-gate 	}
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	/* If tabs can be sent to the tty, turn off their expansion. */
6737c478bd9Sstevel@tonic-gate 	if (tab && set_tab || init_tabs == 8) {
6747c478bd9Sstevel@tonic-gate #ifdef SYSV
6757c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) &= ~(TAB3);
6767c478bd9Sstevel@tonic-gate #else	/* SYSV */
6777c478bd9Sstevel@tonic-gate 		OFLAG(termmode) &= ~(TAB3);
6787c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6797c478bd9Sstevel@tonic-gate 	} else {
6807c478bd9Sstevel@tonic-gate #ifdef SYSV
6817c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) |= TAB3;
6827c478bd9Sstevel@tonic-gate #else	/* SYSV */
6837c478bd9Sstevel@tonic-gate 		OFLAG(termmode) |= TAB3;
6847c478bd9Sstevel@tonic-gate #endif	/* SYSV */
6857c478bd9Sstevel@tonic-gate 	}
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	/* Do the changes to the terminal settings */
6887c478bd9Sstevel@tonic-gate #ifdef SYSV
6897c478bd9Sstevel@tonic-gate 	if (istermios < 0) {
6907c478bd9Sstevel@tonic-gate 		int i;
6917c478bd9Sstevel@tonic-gate 
6927c478bd9Sstevel@tonic-gate 		termmode.c_lflag = termmodes.c_lflag;
6937c478bd9Sstevel@tonic-gate 		termmode.c_oflag = termmodes.c_oflag;
6947c478bd9Sstevel@tonic-gate 		termmode.c_iflag = termmodes.c_iflag;
6957c478bd9Sstevel@tonic-gate 		termmode.c_cflag = termmodes.c_cflag;
6967c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
6977c478bd9Sstevel@tonic-gate 			termmode.c_cc[i] = termmodes.c_cc[i];
6987c478bd9Sstevel@tonic-gate 		(void) STTY(filedes, &termmode);
6997c478bd9Sstevel@tonic-gate 	} else
7007c478bd9Sstevel@tonic-gate 		(void) STTYS(filedes, &termmodes);
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate #else	/* SYSV */
7037c478bd9Sstevel@tonic-gate 	(void) STTY(filedes, &termmode);
7047c478bd9Sstevel@tonic-gate #endif	/* SYSV */
7057c478bd9Sstevel@tonic-gate 
7067c478bd9Sstevel@tonic-gate 	/* Send first initialization strings. */
7077c478bd9Sstevel@tonic-gate 	if (init_prog)
7087c478bd9Sstevel@tonic-gate 	(void) system(init_prog);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 	if (reset && reset_1string) {
7117c478bd9Sstevel@tonic-gate 		putp(reset_1string);
7127c478bd9Sstevel@tonic-gate 	} else if (init_1string) {
7137c478bd9Sstevel@tonic-gate 		putp(init_1string);
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (reset && reset_2string) {
7177c478bd9Sstevel@tonic-gate 		putp(reset_2string);
7187c478bd9Sstevel@tonic-gate 	} else if (init_2string) {
7197c478bd9Sstevel@tonic-gate 		putp(init_2string);
7207c478bd9Sstevel@tonic-gate 	}
7217c478bd9Sstevel@tonic-gate 
7227c478bd9Sstevel@tonic-gate 	/* Set up the tabs stops. */
7237c478bd9Sstevel@tonic-gate 	settabs();
7247c478bd9Sstevel@tonic-gate 
7257c478bd9Sstevel@tonic-gate 	/* Send out initializing file. */
7267c478bd9Sstevel@tonic-gate 	if (reset && reset_file) {
7277c478bd9Sstevel@tonic-gate 		cat(reset_file);
7287c478bd9Sstevel@tonic-gate 	} else if (init_file) {
7297c478bd9Sstevel@tonic-gate 		cat(init_file);
7307c478bd9Sstevel@tonic-gate 	}
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/* Send final initialization strings. */
7337c478bd9Sstevel@tonic-gate 	if (reset && reset_3string) {
7347c478bd9Sstevel@tonic-gate 		putp(reset_3string);
7357c478bd9Sstevel@tonic-gate 	} else if (init_3string) {
7367c478bd9Sstevel@tonic-gate 		putp(init_3string);
7377c478bd9Sstevel@tonic-gate 	}
7387c478bd9Sstevel@tonic-gate 
7397c478bd9Sstevel@tonic-gate 	if (carriage_return) {
7407c478bd9Sstevel@tonic-gate 		putp(carriage_return);
7417c478bd9Sstevel@tonic-gate 	} else {
7427c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate 
7457c478bd9Sstevel@tonic-gate 	/* Send color initialization strings */
7467c478bd9Sstevel@tonic-gate 
7477c478bd9Sstevel@tonic-gate 	if (orig_colors)
7487c478bd9Sstevel@tonic-gate 		putp(orig_colors);
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (orig_pair)
7517c478bd9Sstevel@tonic-gate 	putp(orig_pair);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 	/* Let the terminal settle down. */
7547c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
7557c478bd9Sstevel@tonic-gate 	(void) sleep(1);
7567c478bd9Sstevel@tonic-gate }
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate static void
reset_term()7597c478bd9Sstevel@tonic-gate reset_term()
7607c478bd9Sstevel@tonic-gate {
7617c478bd9Sstevel@tonic-gate 	reset++;
7627c478bd9Sstevel@tonic-gate 	initterm();
7637c478bd9Sstevel@tonic-gate }
764