xref: /illumos-gate/usr/src/cmd/tput/tput.c (revision 7c478bd9)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate 
31*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
32*7c478bd9Sstevel@tonic-gate 
33*7c478bd9Sstevel@tonic-gate 
34*7c478bd9Sstevel@tonic-gate /*
35*7c478bd9Sstevel@tonic-gate  *	tput - print terminal attribute
36*7c478bd9Sstevel@tonic-gate  *
37*7c478bd9Sstevel@tonic-gate  *  return-codes - command line arguments:
38*7c478bd9Sstevel@tonic-gate  *	0: ok if boolean capname -> TRUE
39*7c478bd9Sstevel@tonic-gate  *	1: for boolean capname -> FALSE
40*7c478bd9Sstevel@tonic-gate  *
41*7c478bd9Sstevel@tonic-gate  *  return-codes - standard input arguments:
42*7c478bd9Sstevel@tonic-gate  *	0: ok; tput for all lines was successful
43*7c478bd9Sstevel@tonic-gate  *
44*7c478bd9Sstevel@tonic-gate  *  return-codes - both cases:
45*7c478bd9Sstevel@tonic-gate  *	2	usage error
46*7c478bd9Sstevel@tonic-gate  *	3	bad terminal type given or no terminfo database
47*7c478bd9Sstevel@tonic-gate  *	4	unknown capname
48*7c478bd9Sstevel@tonic-gate  *	-1	capname is a numeric variable that is not specified in the
49*7c478bd9Sstevel@tonic-gate  *		terminfo database(E.g. tpu -T450 lines).
50*7c478bd9Sstevel@tonic-gate  *
51*7c478bd9Sstevel@tonic-gate  *  tput printfs a value if an INT capname was given; e.g. cols.
52*7c478bd9Sstevel@tonic-gate  *	putp's a string if a STRING capname was given; e.g. clear. and
53*7c478bd9Sstevel@tonic-gate  *  for BOOLEAN capnames, e.g. hard-copy, just returns the boolean value.
54*7c478bd9Sstevel@tonic-gate  */
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate #include <curses.h>
57*7c478bd9Sstevel@tonic-gate #include <term.h>
58*7c478bd9Sstevel@tonic-gate #include <fcntl.h>
59*7c478bd9Sstevel@tonic-gate #include <ctype.h>
60*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
61*7c478bd9Sstevel@tonic-gate #include <string.h>
62*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
63*7c478bd9Sstevel@tonic-gate #include <unistd.h>
64*7c478bd9Sstevel@tonic-gate #include <locale.h>
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate /* externs from libcurses */
67*7c478bd9Sstevel@tonic-gate extern int tigetnum();
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate static int outputcap(char *cap, int argc, char **argv);
70*7c478bd9Sstevel@tonic-gate static int allnumeric(char *string);
71*7c478bd9Sstevel@tonic-gate static int getpad(char *cap);
72*7c478bd9Sstevel@tonic-gate static void setdelay();
73*7c478bd9Sstevel@tonic-gate static void settabs();
74*7c478bd9Sstevel@tonic-gate static void cat(char *file);
75*7c478bd9Sstevel@tonic-gate static void initterm();
76*7c478bd9Sstevel@tonic-gate static void reset_term();
77*7c478bd9Sstevel@tonic-gate 
78*7c478bd9Sstevel@tonic-gate static char *progname;		/* argv[0] */
79*7c478bd9Sstevel@tonic-gate static int CurrentBaudRate;	/* current baud rate */
80*7c478bd9Sstevel@tonic-gate static int reset = 0;		/* called as reset_term */
81*7c478bd9Sstevel@tonic-gate static int fildes = 1;
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate int
84*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
85*7c478bd9Sstevel@tonic-gate {
86*7c478bd9Sstevel@tonic-gate 	int i, std_argc;
87*7c478bd9Sstevel@tonic-gate 	char *term = getenv("TERM");
88*7c478bd9Sstevel@tonic-gate 	char *cap, std_input = FALSE;
89*7c478bd9Sstevel@tonic-gate 	int setuperr;
90*7c478bd9Sstevel@tonic-gate 
91*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
92*7c478bd9Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
93*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
94*7c478bd9Sstevel@tonic-gate #endif
95*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	progname = argv[0];
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "ST:")) != EOF) {
100*7c478bd9Sstevel@tonic-gate 		switch (i) {
101*7c478bd9Sstevel@tonic-gate 		case 'T':
102*7c478bd9Sstevel@tonic-gate 			fildes = -1;
103*7c478bd9Sstevel@tonic-gate 			(void) putenv("LINES=");
104*7c478bd9Sstevel@tonic-gate 			(void) putenv("COLUMNS=");
105*7c478bd9Sstevel@tonic-gate 			term = optarg;
106*7c478bd9Sstevel@tonic-gate 			break;
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 		case 'S':
109*7c478bd9Sstevel@tonic-gate 			std_input = TRUE;
110*7c478bd9Sstevel@tonic-gate 			break;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 		case '?':			/* FALLTHROUGH		*/
113*7c478bd9Sstevel@tonic-gate 		usage:				/* FALLTHROUGH		*/
114*7c478bd9Sstevel@tonic-gate 		default:
115*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
116*7c478bd9Sstevel@tonic-gate 			"usage:\t%s [-T [term]] capname [parm argument...]\n"),
117*7c478bd9Sstevel@tonic-gate 				progname);
118*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("OR:\t%s -S <<\n"),
119*7c478bd9Sstevel@tonic-gate 					progname);
120*7c478bd9Sstevel@tonic-gate 			exit(2);
121*7c478bd9Sstevel@tonic-gate 		}
122*7c478bd9Sstevel@tonic-gate 	}
123*7c478bd9Sstevel@tonic-gate 
124*7c478bd9Sstevel@tonic-gate 	if (!term || !*term) {
125*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
126*7c478bd9Sstevel@tonic-gate 			gettext("%s: No value for $TERM and no -T specified\n"),
127*7c478bd9Sstevel@tonic-gate 			progname);
128*7c478bd9Sstevel@tonic-gate 		exit(2);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate 
131*7c478bd9Sstevel@tonic-gate 	(void) setupterm(term, fildes, &setuperr);
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate 	switch (setuperr) {
134*7c478bd9Sstevel@tonic-gate 	case -2:
135*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
136*7c478bd9Sstevel@tonic-gate 		gettext("%s: unreadable terminal descriptor \"%s\"\n"),
137*7c478bd9Sstevel@tonic-gate 			progname, term);
138*7c478bd9Sstevel@tonic-gate 		exit(3);
139*7c478bd9Sstevel@tonic-gate 		break;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	case -1:
142*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr,
143*7c478bd9Sstevel@tonic-gate 			gettext("%s: no terminfo database\n"), progname);
144*7c478bd9Sstevel@tonic-gate 		exit(3);
145*7c478bd9Sstevel@tonic-gate 		break;
146*7c478bd9Sstevel@tonic-gate 
147*7c478bd9Sstevel@tonic-gate 	case 0:
148*7c478bd9Sstevel@tonic-gate 	    (void) fprintf(stderr,
149*7c478bd9Sstevel@tonic-gate 			gettext("%s: unknown terminal \"%s\"\n"),
150*7c478bd9Sstevel@tonic-gate 				progname, term);
151*7c478bd9Sstevel@tonic-gate 	    exit(3);
152*7c478bd9Sstevel@tonic-gate 	}
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	reset_shell_mode();
155*7c478bd9Sstevel@tonic-gate 
156*7c478bd9Sstevel@tonic-gate 	/* command line arguments */
157*7c478bd9Sstevel@tonic-gate 	if (!std_input) {
158*7c478bd9Sstevel@tonic-gate 		if (argc == optind)
159*7c478bd9Sstevel@tonic-gate 			goto usage;
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 		cap = argv[optind++];
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 		if (strcmp(cap, "init") == 0)
164*7c478bd9Sstevel@tonic-gate 			initterm();
165*7c478bd9Sstevel@tonic-gate 		else if (strcmp(cap, "reset") == 0)
166*7c478bd9Sstevel@tonic-gate 			reset_term();
167*7c478bd9Sstevel@tonic-gate 		else if (strcmp(cap, "longname") == 0)
168*7c478bd9Sstevel@tonic-gate 			(void) printf("%s\n", longname());
169*7c478bd9Sstevel@tonic-gate 		else
170*7c478bd9Sstevel@tonic-gate 			exit(outputcap(cap, argc, argv));
171*7c478bd9Sstevel@tonic-gate 		return (0);
172*7c478bd9Sstevel@tonic-gate 	} else {			/* standard input argumets	*/
173*7c478bd9Sstevel@tonic-gate 		char buff[128];
174*7c478bd9Sstevel@tonic-gate 		char **v;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 		/* allocate storage for the 'faked' argv[] array	*/
177*7c478bd9Sstevel@tonic-gate 		v = (char **)malloc(10 * sizeof (char *));
178*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 10; i++)
179*7c478bd9Sstevel@tonic-gate 			v[i] = (char *)malloc(32 * sizeof (char));
180*7c478bd9Sstevel@tonic-gate 
181*7c478bd9Sstevel@tonic-gate 		while (gets(buff) != NULL) {
182*7c478bd9Sstevel@tonic-gate 			/* read standard input line; skip over empty lines */
183*7c478bd9Sstevel@tonic-gate 			if ((std_argc =
184*7c478bd9Sstevel@tonic-gate 			    sscanf(buff, "%s %s %s %s %s %s %s %s %s %s",
185*7c478bd9Sstevel@tonic-gate 			    v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7],
186*7c478bd9Sstevel@tonic-gate 			    v[8], v[9])) < 1) {
187*7c478bd9Sstevel@tonic-gate 				continue;
188*7c478bd9Sstevel@tonic-gate 			}
189*7c478bd9Sstevel@tonic-gate 
190*7c478bd9Sstevel@tonic-gate 			cap = v[0];
191*7c478bd9Sstevel@tonic-gate 			optind = 1;
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate 			if (strcmp(cap, "init") == 0) {
194*7c478bd9Sstevel@tonic-gate 				initterm();
195*7c478bd9Sstevel@tonic-gate 			} else if (strcmp(cap, "reset") == 0) {
196*7c478bd9Sstevel@tonic-gate 				reset_term();
197*7c478bd9Sstevel@tonic-gate 			} else if (strcmp(cap, "longname") == 0) {
198*7c478bd9Sstevel@tonic-gate 				(void) printf("%s\n", longname());
199*7c478bd9Sstevel@tonic-gate 			} else {
200*7c478bd9Sstevel@tonic-gate 				(void) outputcap(cap, std_argc, v);
201*7c478bd9Sstevel@tonic-gate 			}
202*7c478bd9Sstevel@tonic-gate 			(void) fflush(stdout);
203*7c478bd9Sstevel@tonic-gate 		}
204*7c478bd9Sstevel@tonic-gate 
205*7c478bd9Sstevel@tonic-gate 		return (0);
206*7c478bd9Sstevel@tonic-gate 	}
207*7c478bd9Sstevel@tonic-gate }
208*7c478bd9Sstevel@tonic-gate 
209*7c478bd9Sstevel@tonic-gate static long parm[9] = {
210*7c478bd9Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0, 0
211*7c478bd9Sstevel@tonic-gate };
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate static int
214*7c478bd9Sstevel@tonic-gate outputcap(char *cap, int argc, char **argv)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	int parmset = 0;
217*7c478bd9Sstevel@tonic-gate 	char *thisstr;
218*7c478bd9Sstevel@tonic-gate 	int i;
219*7c478bd9Sstevel@tonic-gate 
220*7c478bd9Sstevel@tonic-gate 	if ((i = tigetflag(cap)) >= 0)
221*7c478bd9Sstevel@tonic-gate 		return (1 - i);
222*7c478bd9Sstevel@tonic-gate 
223*7c478bd9Sstevel@tonic-gate 	if ((i = tigetnum(cap)) >= -1) {
224*7c478bd9Sstevel@tonic-gate 		(void) printf("%d\n", i);
225*7c478bd9Sstevel@tonic-gate 		return (0);
226*7c478bd9Sstevel@tonic-gate 	}
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	if ((thisstr = tigetstr(cap)) != (char *)-1) {
229*7c478bd9Sstevel@tonic-gate 		if (!thisstr) {
230*7c478bd9Sstevel@tonic-gate 			return (1);
231*7c478bd9Sstevel@tonic-gate 		}
232*7c478bd9Sstevel@tonic-gate 		for (parmset = 0; optind < argc; optind++, parmset++)
233*7c478bd9Sstevel@tonic-gate 			if (allnumeric(argv[optind]))
234*7c478bd9Sstevel@tonic-gate 				parm[parmset] = atoi(argv[optind]);
235*7c478bd9Sstevel@tonic-gate 			else
236*7c478bd9Sstevel@tonic-gate 				parm[parmset] = (int)argv[optind];
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 		if (parmset)
239*7c478bd9Sstevel@tonic-gate 			putp(tparm(thisstr,
240*7c478bd9Sstevel@tonic-gate 			parm[0], parm[1], parm[2], parm[3], parm[4], parm[5],
241*7c478bd9Sstevel@tonic-gate 			parm[6], parm[7], parm[8]));
242*7c478bd9Sstevel@tonic-gate 		else
243*7c478bd9Sstevel@tonic-gate 			putp(thisstr);
244*7c478bd9Sstevel@tonic-gate 		return (0);
245*7c478bd9Sstevel@tonic-gate 	}
246*7c478bd9Sstevel@tonic-gate 
247*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
248*7c478bd9Sstevel@tonic-gate 	    gettext("%s: unknown terminfo capability '%s'\n"), progname, cap);
249*7c478bd9Sstevel@tonic-gate 
250*7c478bd9Sstevel@tonic-gate 	exit(4);
251*7c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
252*7c478bd9Sstevel@tonic-gate }
253*7c478bd9Sstevel@tonic-gate 
254*7c478bd9Sstevel@tonic-gate /*
255*7c478bd9Sstevel@tonic-gate  *  The decision as to whether an argument is a number or not is to simply
256*7c478bd9Sstevel@tonic-gate  *  look at whether there are any non-digits in the string.
257*7c478bd9Sstevel@tonic-gate  */
258*7c478bd9Sstevel@tonic-gate static int
259*7c478bd9Sstevel@tonic-gate allnumeric(char *string)
260*7c478bd9Sstevel@tonic-gate {
261*7c478bd9Sstevel@tonic-gate 	if (*string) {
262*7c478bd9Sstevel@tonic-gate 		while (*string) {
263*7c478bd9Sstevel@tonic-gate 			if (!isdigit(*string++)) {
264*7c478bd9Sstevel@tonic-gate 				return (0);
265*7c478bd9Sstevel@tonic-gate 			}
266*7c478bd9Sstevel@tonic-gate 		}
267*7c478bd9Sstevel@tonic-gate 		return (1);
268*7c478bd9Sstevel@tonic-gate 	} else {
269*7c478bd9Sstevel@tonic-gate 		return (0);
270*7c478bd9Sstevel@tonic-gate 	}
271*7c478bd9Sstevel@tonic-gate }
272*7c478bd9Sstevel@tonic-gate 
273*7c478bd9Sstevel@tonic-gate /*
274*7c478bd9Sstevel@tonic-gate  *  SYSTEM DEPENDENT TERMINAL DELAY TABLES
275*7c478bd9Sstevel@tonic-gate  *
276*7c478bd9Sstevel@tonic-gate  *	These tables maintain the correspondence between the delays
277*7c478bd9Sstevel@tonic-gate  *	defined in terminfo and the delay algorithms in the tty driver
278*7c478bd9Sstevel@tonic-gate  *	on the particular systems. For each type of delay, the bits used
279*7c478bd9Sstevel@tonic-gate  *	for that delay must be specified, in XXbits, and a table
280*7c478bd9Sstevel@tonic-gate  *	must be defined giving correspondences between delays and
281*7c478bd9Sstevel@tonic-gate  *	algorithms. Algorithms which are not fixed delays, such
282*7c478bd9Sstevel@tonic-gate  *	as dependent on current column or line number, must be
283*7c478bd9Sstevel@tonic-gate  *	kludged in some way at this time.
284*7c478bd9Sstevel@tonic-gate  *
285*7c478bd9Sstevel@tonic-gate  *	Some of this was taken from tset(1).
286*7c478bd9Sstevel@tonic-gate  */
287*7c478bd9Sstevel@tonic-gate 
288*7c478bd9Sstevel@tonic-gate struct delay
289*7c478bd9Sstevel@tonic-gate {
290*7c478bd9Sstevel@tonic-gate     int d_delay;
291*7c478bd9Sstevel@tonic-gate     int d_bits;
292*7c478bd9Sstevel@tonic-gate };
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate /* The appropriate speeds for various termio settings. */
295*7c478bd9Sstevel@tonic-gate static int speeds[] = {
296*7c478bd9Sstevel@tonic-gate 		0,	/*  B0,		*/
297*7c478bd9Sstevel@tonic-gate 		50,	/*  B50,	*/
298*7c478bd9Sstevel@tonic-gate 		75,	/*  B75,	*/
299*7c478bd9Sstevel@tonic-gate 		110,	/*  B110,	*/
300*7c478bd9Sstevel@tonic-gate 		134,	/*  B134,	*/
301*7c478bd9Sstevel@tonic-gate 		150,	/*  B150,	*/
302*7c478bd9Sstevel@tonic-gate 		200,	/*  B200,	*/
303*7c478bd9Sstevel@tonic-gate 		300,	/*  B300,	*/
304*7c478bd9Sstevel@tonic-gate 		600,	/*  B600,	*/
305*7c478bd9Sstevel@tonic-gate 		1200,	/*  B1200,	*/
306*7c478bd9Sstevel@tonic-gate 		1800,	/*  B1800,	*/
307*7c478bd9Sstevel@tonic-gate 		2400,	/*  B2400,	*/
308*7c478bd9Sstevel@tonic-gate 		4800,	/*  B4800,	*/
309*7c478bd9Sstevel@tonic-gate 		9600,	/*  B9600,	*/
310*7c478bd9Sstevel@tonic-gate 		19200,	/*  EXTA,	*/
311*7c478bd9Sstevel@tonic-gate 		38400,	/*  EXTB,	*/
312*7c478bd9Sstevel@tonic-gate 		57600,	/*  B57600,	*/
313*7c478bd9Sstevel@tonic-gate 		76800,	/*  B76800,	*/
314*7c478bd9Sstevel@tonic-gate 		115200,	/*  B115200,	*/
315*7c478bd9Sstevel@tonic-gate 		153600,	/*  B153600,	*/
316*7c478bd9Sstevel@tonic-gate 		230400,	/*  B230400,	*/
317*7c478bd9Sstevel@tonic-gate 		307200,	/*  B307200,	*/
318*7c478bd9Sstevel@tonic-gate 		460800,	/*  B460800,	*/
319*7c478bd9Sstevel@tonic-gate 		0,
320*7c478bd9Sstevel@tonic-gate };
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
323*7c478bd9Sstevel@tonic-gate /*	Unix 3.0 on up */
324*7c478bd9Sstevel@tonic-gate 
325*7c478bd9Sstevel@tonic-gate /*    Carriage Return delays	*/
326*7c478bd9Sstevel@tonic-gate 
327*7c478bd9Sstevel@tonic-gate static int	CRbits = CRDLY;
328*7c478bd9Sstevel@tonic-gate static struct delay	CRdelay[] =
329*7c478bd9Sstevel@tonic-gate {
330*7c478bd9Sstevel@tonic-gate 	0,	CR0,
331*7c478bd9Sstevel@tonic-gate 	80,	CR1,
332*7c478bd9Sstevel@tonic-gate 	100,	CR2,
333*7c478bd9Sstevel@tonic-gate 	150,	CR3,
334*7c478bd9Sstevel@tonic-gate 	-1
335*7c478bd9Sstevel@tonic-gate };
336*7c478bd9Sstevel@tonic-gate 
337*7c478bd9Sstevel@tonic-gate /*	New Line delays	*/
338*7c478bd9Sstevel@tonic-gate 
339*7c478bd9Sstevel@tonic-gate static int	NLbits = NLDLY;
340*7c478bd9Sstevel@tonic-gate static struct delay	NLdelay[] =
341*7c478bd9Sstevel@tonic-gate {
342*7c478bd9Sstevel@tonic-gate 	0,	NL0,
343*7c478bd9Sstevel@tonic-gate 	100,	NL1,
344*7c478bd9Sstevel@tonic-gate 	-1
345*7c478bd9Sstevel@tonic-gate };
346*7c478bd9Sstevel@tonic-gate 
347*7c478bd9Sstevel@tonic-gate /*	Back Space delays	*/
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate static int	BSbits = BSDLY;
350*7c478bd9Sstevel@tonic-gate static struct delay	BSdelay[] =
351*7c478bd9Sstevel@tonic-gate {
352*7c478bd9Sstevel@tonic-gate 	0,	BS0,
353*7c478bd9Sstevel@tonic-gate 	50,	BS1,
354*7c478bd9Sstevel@tonic-gate 	-1
355*7c478bd9Sstevel@tonic-gate };
356*7c478bd9Sstevel@tonic-gate 
357*7c478bd9Sstevel@tonic-gate /*	TaB delays	*/
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate static int	TBbits = TABDLY;
360*7c478bd9Sstevel@tonic-gate static struct delay	TBdelay[] =
361*7c478bd9Sstevel@tonic-gate {
362*7c478bd9Sstevel@tonic-gate 	0,	TAB0,
363*7c478bd9Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
364*7c478bd9Sstevel@tonic-gate 	100,	TAB2,
365*7c478bd9Sstevel@tonic-gate 				/* TAB3 is XTABS and not a delay */
366*7c478bd9Sstevel@tonic-gate 	-1
367*7c478bd9Sstevel@tonic-gate };
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate /*	Form Feed delays	*/
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate static int	FFbits = FFDLY;
372*7c478bd9Sstevel@tonic-gate static struct delay	FFdelay[] =
373*7c478bd9Sstevel@tonic-gate {
374*7c478bd9Sstevel@tonic-gate 	0,	FF0,
375*7c478bd9Sstevel@tonic-gate 	2000,	FF1,
376*7c478bd9Sstevel@tonic-gate 	-1
377*7c478bd9Sstevel@tonic-gate };
378*7c478bd9Sstevel@tonic-gate 
379*7c478bd9Sstevel@tonic-gate #else	/* BSD */
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate /*	Carriage Return delays	*/
382*7c478bd9Sstevel@tonic-gate 
383*7c478bd9Sstevel@tonic-gate int	CRbits = CRDELAY;
384*7c478bd9Sstevel@tonic-gate struct delay	CRdelay[] =
385*7c478bd9Sstevel@tonic-gate {
386*7c478bd9Sstevel@tonic-gate 	0,	CR0,
387*7c478bd9Sstevel@tonic-gate 	9,	CR3,
388*7c478bd9Sstevel@tonic-gate 	80,	CR1,
389*7c478bd9Sstevel@tonic-gate 	160,	CR2,
390*7c478bd9Sstevel@tonic-gate 	-1
391*7c478bd9Sstevel@tonic-gate };
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate /*	New Line delays	*/
394*7c478bd9Sstevel@tonic-gate 
395*7c478bd9Sstevel@tonic-gate int	NLbits = NLDELAY;
396*7c478bd9Sstevel@tonic-gate struct delay	NLdelay[] =
397*7c478bd9Sstevel@tonic-gate {
398*7c478bd9Sstevel@tonic-gate 	0,	NL0,
399*7c478bd9Sstevel@tonic-gate 	66,	NL1,		/* special M37 delay */
400*7c478bd9Sstevel@tonic-gate 	100,	NL2,
401*7c478bd9Sstevel@tonic-gate 	-1
402*7c478bd9Sstevel@tonic-gate };
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate /*	Tab delays	*/
405*7c478bd9Sstevel@tonic-gate 
406*7c478bd9Sstevel@tonic-gate int	TBbits = TBDELAY;
407*7c478bd9Sstevel@tonic-gate struct delay	TBdelay[] =
408*7c478bd9Sstevel@tonic-gate {
409*7c478bd9Sstevel@tonic-gate 	0,	TAB0,
410*7c478bd9Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
411*7c478bd9Sstevel@tonic-gate 	-1
412*7c478bd9Sstevel@tonic-gate };
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate /*	Form Feed delays	*/
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate int	FFbits = VTDELAY;
417*7c478bd9Sstevel@tonic-gate struct delay	FFdelay[] =
418*7c478bd9Sstevel@tonic-gate {
419*7c478bd9Sstevel@tonic-gate 	0,	FF0,
420*7c478bd9Sstevel@tonic-gate 	2000,	FF1,
421*7c478bd9Sstevel@tonic-gate 	-1
422*7c478bd9Sstevel@tonic-gate };
423*7c478bd9Sstevel@tonic-gate #endif	/* BSD */
424*7c478bd9Sstevel@tonic-gate 
425*7c478bd9Sstevel@tonic-gate /*
426*7c478bd9Sstevel@tonic-gate  *  Initterm, a.k.a. reset_term, does terminal specific initialization. In
427*7c478bd9Sstevel@tonic-gate  *  particular, the init_strings from terminfo are output and tabs are
428*7c478bd9Sstevel@tonic-gate  *  set, if they aren't hardwired in. Much of this stuff was done by
429*7c478bd9Sstevel@tonic-gate  *  the tset(1) program.
430*7c478bd9Sstevel@tonic-gate  */
431*7c478bd9Sstevel@tonic-gate 
432*7c478bd9Sstevel@tonic-gate /*
433*7c478bd9Sstevel@tonic-gate  *  Figure out how many milliseconds of padding the capability cap
434*7c478bd9Sstevel@tonic-gate  *  needs and return that number. Padding is stored in the string as "$<n>",
435*7c478bd9Sstevel@tonic-gate  *  where n is the number of milliseconds of padding. More than one
436*7c478bd9Sstevel@tonic-gate  *  padding string is allowed within the string, although this is unlikely.
437*7c478bd9Sstevel@tonic-gate  */
438*7c478bd9Sstevel@tonic-gate 
439*7c478bd9Sstevel@tonic-gate static int
440*7c478bd9Sstevel@tonic-gate getpad(char *cap)
441*7c478bd9Sstevel@tonic-gate {
442*7c478bd9Sstevel@tonic-gate 	int padding = 0;
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	/* No padding needed at speeds below padding_baud_rate */
445*7c478bd9Sstevel@tonic-gate 	if (padding_baud_rate > CurrentBaudRate || cap == NULL)
446*7c478bd9Sstevel@tonic-gate 		return (0);
447*7c478bd9Sstevel@tonic-gate 
448*7c478bd9Sstevel@tonic-gate 	while (*cap) {
449*7c478bd9Sstevel@tonic-gate 		if ((cap[0] == '$') && (cap[1] == '<')) {
450*7c478bd9Sstevel@tonic-gate 			cap++;
451*7c478bd9Sstevel@tonic-gate 			cap++;
452*7c478bd9Sstevel@tonic-gate 			padding += atoi(cap);
453*7c478bd9Sstevel@tonic-gate 			while (isdigit (*cap))
454*7c478bd9Sstevel@tonic-gate 				cap++;
455*7c478bd9Sstevel@tonic-gate 			while (*cap == '.' || *cap == '/' || *cap == '*' ||
456*7c478bd9Sstevel@tonic-gate 			isdigit(*cap))
457*7c478bd9Sstevel@tonic-gate 				cap++;
458*7c478bd9Sstevel@tonic-gate 			while (*cap == '>')
459*7c478bd9Sstevel@tonic-gate 				cap++;
460*7c478bd9Sstevel@tonic-gate 		} else {
461*7c478bd9Sstevel@tonic-gate 			cap++;
462*7c478bd9Sstevel@tonic-gate 		}
463*7c478bd9Sstevel@tonic-gate 	}
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	return (padding);
466*7c478bd9Sstevel@tonic-gate }
467*7c478bd9Sstevel@tonic-gate 
468*7c478bd9Sstevel@tonic-gate /*
469*7c478bd9Sstevel@tonic-gate  *  Set the appropriate delay bits in the termio structure for
470*7c478bd9Sstevel@tonic-gate  *  the given delay.
471*7c478bd9Sstevel@tonic-gate  */
472*7c478bd9Sstevel@tonic-gate static void
473*7c478bd9Sstevel@tonic-gate setdelay(delay, delaytable, bits, flags)
474*7c478bd9Sstevel@tonic-gate register int delay;
475*7c478bd9Sstevel@tonic-gate struct delay delaytable[];
476*7c478bd9Sstevel@tonic-gate int bits;
477*7c478bd9Sstevel@tonic-gate #ifdef SYSV
478*7c478bd9Sstevel@tonic-gate tcflag_t *flags;
479*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
480*7c478bd9Sstevel@tonic-gate unsigned short *flags;
481*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
482*7c478bd9Sstevel@tonic-gate {
483*7c478bd9Sstevel@tonic-gate 	register struct delay  *p;
484*7c478bd9Sstevel@tonic-gate 	register struct delay  *lastdelay;
485*7c478bd9Sstevel@tonic-gate 
486*7c478bd9Sstevel@tonic-gate 	/* Clear out the bits, replace with new ones */
487*7c478bd9Sstevel@tonic-gate 	*flags &= ~bits;
488*7c478bd9Sstevel@tonic-gate 
489*7c478bd9Sstevel@tonic-gate 	/* Scan the delay table for first entry with adequate delay */
490*7c478bd9Sstevel@tonic-gate 	for (lastdelay = p = delaytable;
491*7c478bd9Sstevel@tonic-gate 	    (p -> d_delay >= 0) && (p -> d_delay < delay);
492*7c478bd9Sstevel@tonic-gate 	    p++) {
493*7c478bd9Sstevel@tonic-gate 		lastdelay = p;
494*7c478bd9Sstevel@tonic-gate 	}
495*7c478bd9Sstevel@tonic-gate 
496*7c478bd9Sstevel@tonic-gate 	/* use last entry if none will do */
497*7c478bd9Sstevel@tonic-gate 	*flags |= lastdelay -> d_bits;
498*7c478bd9Sstevel@tonic-gate }
499*7c478bd9Sstevel@tonic-gate 
500*7c478bd9Sstevel@tonic-gate /*
501*7c478bd9Sstevel@tonic-gate  * Set the hardware tabs on the terminal, using clear_all_tabs,
502*7c478bd9Sstevel@tonic-gate  * set_tab, and column_address capabilities. Cursor_address and cursor_right
503*7c478bd9Sstevel@tonic-gate  * may also be used, if necessary.
504*7c478bd9Sstevel@tonic-gate  * This is done before the init_file and init_3string, so they can patch in
505*7c478bd9Sstevel@tonic-gate  * case we blow this.
506*7c478bd9Sstevel@tonic-gate  */
507*7c478bd9Sstevel@tonic-gate 
508*7c478bd9Sstevel@tonic-gate static void
509*7c478bd9Sstevel@tonic-gate settabs()
510*7c478bd9Sstevel@tonic-gate {
511*7c478bd9Sstevel@tonic-gate 	register int c;
512*7c478bd9Sstevel@tonic-gate 
513*7c478bd9Sstevel@tonic-gate 	/* Do not set tabs if they power up properly. */
514*7c478bd9Sstevel@tonic-gate 	if (init_tabs == 8)
515*7c478bd9Sstevel@tonic-gate 		return;
516*7c478bd9Sstevel@tonic-gate 
517*7c478bd9Sstevel@tonic-gate 	if (set_tab) {
518*7c478bd9Sstevel@tonic-gate 		/* Force the cursor to be at the left margin. */
519*7c478bd9Sstevel@tonic-gate 		if (carriage_return)
520*7c478bd9Sstevel@tonic-gate 			putp(carriage_return);
521*7c478bd9Sstevel@tonic-gate 		else
522*7c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
523*7c478bd9Sstevel@tonic-gate 
524*7c478bd9Sstevel@tonic-gate 		/* Clear any current tab settings. */
525*7c478bd9Sstevel@tonic-gate 		if (clear_all_tabs)
526*7c478bd9Sstevel@tonic-gate 			putp(clear_all_tabs);
527*7c478bd9Sstevel@tonic-gate 
528*7c478bd9Sstevel@tonic-gate 		/* Set the tabs. */
529*7c478bd9Sstevel@tonic-gate 		for (c = 8; c < columns; c += 8) {
530*7c478bd9Sstevel@tonic-gate 			/* Get to that column. */
531*7c478bd9Sstevel@tonic-gate 			(void) fputs("        ", stdout);
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 			/* Set the tab. */
534*7c478bd9Sstevel@tonic-gate 			putp(set_tab);
535*7c478bd9Sstevel@tonic-gate 		}
536*7c478bd9Sstevel@tonic-gate 
537*7c478bd9Sstevel@tonic-gate 		/* Get back to the left column. */
538*7c478bd9Sstevel@tonic-gate 		if (carriage_return)
539*7c478bd9Sstevel@tonic-gate 			putp(carriage_return);
540*7c478bd9Sstevel@tonic-gate 		else
541*7c478bd9Sstevel@tonic-gate 			(void) putchar('\r');
542*7c478bd9Sstevel@tonic-gate 
543*7c478bd9Sstevel@tonic-gate 	}
544*7c478bd9Sstevel@tonic-gate }
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate /*
547*7c478bd9Sstevel@tonic-gate  *  Copy "file" onto standard output.
548*7c478bd9Sstevel@tonic-gate  */
549*7c478bd9Sstevel@tonic-gate 
550*7c478bd9Sstevel@tonic-gate static void
551*7c478bd9Sstevel@tonic-gate cat(file)
552*7c478bd9Sstevel@tonic-gate char *file;				/* File to copy. */
553*7c478bd9Sstevel@tonic-gate {
554*7c478bd9Sstevel@tonic-gate 	register int fd;			/* File descriptor. */
555*7c478bd9Sstevel@tonic-gate 	register ssize_t i;			/* Number characters read. */
556*7c478bd9Sstevel@tonic-gate 	char buf[BUFSIZ];			/* Buffer to read into. */
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
559*7c478bd9Sstevel@tonic-gate 
560*7c478bd9Sstevel@tonic-gate 	if (fd < 0) {
561*7c478bd9Sstevel@tonic-gate 		perror("Cannot open initialization file");
562*7c478bd9Sstevel@tonic-gate 	} else {
563*7c478bd9Sstevel@tonic-gate 		while ((i = read(fd, buf, BUFSIZ)) > (ssize_t)0)
564*7c478bd9Sstevel@tonic-gate 			(void) write(fileno(stdout), buf, (unsigned)i);
565*7c478bd9Sstevel@tonic-gate 		(int)close(fd);
566*7c478bd9Sstevel@tonic-gate 	}
567*7c478bd9Sstevel@tonic-gate }
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate /*
570*7c478bd9Sstevel@tonic-gate  *  Initialize the terminal.
571*7c478bd9Sstevel@tonic-gate  *  Send the initialization strings to the terminal.
572*7c478bd9Sstevel@tonic-gate  */
573*7c478bd9Sstevel@tonic-gate 
574*7c478bd9Sstevel@tonic-gate static void
575*7c478bd9Sstevel@tonic-gate initterm()
576*7c478bd9Sstevel@tonic-gate {
577*7c478bd9Sstevel@tonic-gate 	register int filedes;		/* File descriptor for ioctl's. */
578*7c478bd9Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
579*7c478bd9Sstevel@tonic-gate 	struct termio termmode;		/* To hold terminal settings. */
580*7c478bd9Sstevel@tonic-gate 	struct termios termmodes;	/* To hold terminal settings. */
581*7c478bd9Sstevel@tonic-gate 	int i;
582*7c478bd9Sstevel@tonic-gate 	int istermios = -1;
583*7c478bd9Sstevel@tonic-gate #define	GTTY(fd, mode)	ioctl(fd, TCGETA, mode)
584*7c478bd9Sstevel@tonic-gate #define	GTTYS(fd, mode) \
585*7c478bd9Sstevel@tonic-gate 	(istermios = ioctl(fd, TCGETS, mode))
586*7c478bd9Sstevel@tonic-gate #define	STTY(fd, mode)	ioctl(fd, TCSETAW, mode)
587*7c478bd9Sstevel@tonic-gate #define	STTYS(fd, mode)	ioctl(fd, TCSETSW, mode)
588*7c478bd9Sstevel@tonic-gate #define	SPEED(mode)	(mode.c_cflag & CBAUD)
589*7c478bd9Sstevel@tonic-gate #define	SPEEDS(mode)	(cfgetospeed(&mode))
590*7c478bd9Sstevel@tonic-gate #define	OFLAG(mode)	mode.c_oflag
591*7c478bd9Sstevel@tonic-gate #else	/* BSD */
592*7c478bd9Sstevel@tonic-gate 	struct sgttyb termmode;		/* To hold terminal settings. */
593*7c478bd9Sstevel@tonic-gate #define	GTTY(fd, mode)	gtty(fd, mode)
594*7c478bd9Sstevel@tonic-gate #define	STTY(fd, mode)	stty(fd, mode)
595*7c478bd9Sstevel@tonic-gate #define	SPEED(mode)	(mode.sg_ospeed & 017)
596*7c478bd9Sstevel@tonic-gate #define	OFLAG(mode)	mode.sg_flags
597*7c478bd9Sstevel@tonic-gate #define	TAB3		XTABS
598*7c478bd9Sstevel@tonic-gate #endif
599*7c478bd9Sstevel@tonic-gate 
600*7c478bd9Sstevel@tonic-gate 	/* Get the terminal settings. */
601*7c478bd9Sstevel@tonic-gate 	/* First try standard output, then standard error, */
602*7c478bd9Sstevel@tonic-gate 	/* then standard input, then /dev/tty. */
603*7c478bd9Sstevel@tonic-gate #ifdef SYSV
604*7c478bd9Sstevel@tonic-gate 	if ((filedes = 1, GTTYS(filedes, &termmodes) < 0) ||
605*7c478bd9Sstevel@tonic-gate 	    (filedes = 2, GTTYS(filedes, &termmodes) < 0) ||
606*7c478bd9Sstevel@tonic-gate 	    (filedes = 0, GTTYS(filedes, &termmodes) < 0) ||
607*7c478bd9Sstevel@tonic-gate 	    (filedes = open("/dev/tty", O_RDWR),
608*7c478bd9Sstevel@tonic-gate 	    GTTYS(filedes, &termmodes) < 0)) {
609*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
610*7c478bd9Sstevel@tonic-gate 		if ((filedes = 1, GTTY(filedes, &termmode) == -1) ||
611*7c478bd9Sstevel@tonic-gate 		    (filedes = 2, GTTY(filedes, &termmode) == -1) ||
612*7c478bd9Sstevel@tonic-gate 		    (filedes = 0, GTTY(filedes, &termmode) == -1) ||
613*7c478bd9Sstevel@tonic-gate 		    (filedes = open("/dev/tty", O_RDWR),
614*7c478bd9Sstevel@tonic-gate 		    GTTY(filedes, &termmode) == -1)) {
615*7c478bd9Sstevel@tonic-gate 			filedes = -1;
616*7c478bd9Sstevel@tonic-gate 			CurrentBaudRate = speeds[B1200];
617*7c478bd9Sstevel@tonic-gate 		} else
618*7c478bd9Sstevel@tonic-gate 			CurrentBaudRate = speeds[SPEED(termmode)];
619*7c478bd9Sstevel@tonic-gate #ifdef SYSV
620*7c478bd9Sstevel@tonic-gate 		termmodes.c_lflag = termmode.c_lflag;
621*7c478bd9Sstevel@tonic-gate 		termmodes.c_oflag = termmode.c_oflag;
622*7c478bd9Sstevel@tonic-gate 		termmodes.c_iflag = termmode.c_iflag;
623*7c478bd9Sstevel@tonic-gate 		termmodes.c_cflag = termmode.c_cflag;
624*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
625*7c478bd9Sstevel@tonic-gate 			termmodes.c_cc[i] = termmode.c_cc[i];
626*7c478bd9Sstevel@tonic-gate 	} else
627*7c478bd9Sstevel@tonic-gate 		CurrentBaudRate = speeds[SPEEDS(termmodes)];
628*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
629*7c478bd9Sstevel@tonic-gate 
630*7c478bd9Sstevel@tonic-gate 	if (xon_xoff) {
631*7c478bd9Sstevel@tonic-gate #ifdef SYSV
632*7c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) &=
633*7c478bd9Sstevel@tonic-gate 			~(NLbits | CRbits | BSbits | FFbits | TBbits);
634*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
635*7c478bd9Sstevel@tonic-gate 		OFLAG(termmode) &=
636*7c478bd9Sstevel@tonic-gate 			~(NLbits | CRbits | BSbits | FFbits | TBbits);
637*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
638*7c478bd9Sstevel@tonic-gate 	} else {
639*7c478bd9Sstevel@tonic-gate #ifdef SYSV
640*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(carriage_return), CRdelay, CRbits, &OFLAG(termmodes));
641*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(scroll_forward), NLdelay, NLbits, &OFLAG(termmodes));
642*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(cursor_left), BSdelay, BSbits, &OFLAG(termmodes));
643*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(form_feed), FFdelay, FFbits, &OFLAG(termmodes));
644*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(tab), TBdelay, TBbits, &OFLAG(termmodes));
645*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
646*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(carriage_return), CRdelay, CRbits, &OFLAG(termmode));
647*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(scroll_forward), NLdelay, NLbits, &OFLAG(termmode));
648*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(cursor_left), BSdelay, BSbits, &OFLAG(termmode));
649*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(form_feed), FFdelay, FFbits, &OFLAG(termmode));
650*7c478bd9Sstevel@tonic-gate 	setdelay(getpad(tab), TBdelay, TBbits, &OFLAG(termmode));
651*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
652*7c478bd9Sstevel@tonic-gate 	}
653*7c478bd9Sstevel@tonic-gate 
654*7c478bd9Sstevel@tonic-gate 	/* If tabs can be sent to the tty, turn off their expansion. */
655*7c478bd9Sstevel@tonic-gate 	if (tab && set_tab || init_tabs == 8) {
656*7c478bd9Sstevel@tonic-gate #ifdef SYSV
657*7c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) &= ~(TAB3);
658*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
659*7c478bd9Sstevel@tonic-gate 		OFLAG(termmode) &= ~(TAB3);
660*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
661*7c478bd9Sstevel@tonic-gate 	} else {
662*7c478bd9Sstevel@tonic-gate #ifdef SYSV
663*7c478bd9Sstevel@tonic-gate 		OFLAG(termmodes) |= TAB3;
664*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
665*7c478bd9Sstevel@tonic-gate 		OFLAG(termmode) |= TAB3;
666*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
667*7c478bd9Sstevel@tonic-gate 	}
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 	/* Do the changes to the terminal settings */
670*7c478bd9Sstevel@tonic-gate #ifdef SYSV
671*7c478bd9Sstevel@tonic-gate 	if (istermios < 0) {
672*7c478bd9Sstevel@tonic-gate 		int i;
673*7c478bd9Sstevel@tonic-gate 
674*7c478bd9Sstevel@tonic-gate 		termmode.c_lflag = termmodes.c_lflag;
675*7c478bd9Sstevel@tonic-gate 		termmode.c_oflag = termmodes.c_oflag;
676*7c478bd9Sstevel@tonic-gate 		termmode.c_iflag = termmodes.c_iflag;
677*7c478bd9Sstevel@tonic-gate 		termmode.c_cflag = termmodes.c_cflag;
678*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
679*7c478bd9Sstevel@tonic-gate 			termmode.c_cc[i] = termmodes.c_cc[i];
680*7c478bd9Sstevel@tonic-gate 		(void) STTY(filedes, &termmode);
681*7c478bd9Sstevel@tonic-gate 	} else
682*7c478bd9Sstevel@tonic-gate 		(void) STTYS(filedes, &termmodes);
683*7c478bd9Sstevel@tonic-gate 
684*7c478bd9Sstevel@tonic-gate #else	/* SYSV */
685*7c478bd9Sstevel@tonic-gate 	(void) STTY(filedes, &termmode);
686*7c478bd9Sstevel@tonic-gate #endif	/* SYSV */
687*7c478bd9Sstevel@tonic-gate 
688*7c478bd9Sstevel@tonic-gate 	/* Send first initialization strings. */
689*7c478bd9Sstevel@tonic-gate 	if (init_prog)
690*7c478bd9Sstevel@tonic-gate 	(void) system(init_prog);
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate 	if (reset && reset_1string) {
693*7c478bd9Sstevel@tonic-gate 		putp(reset_1string);
694*7c478bd9Sstevel@tonic-gate 	} else if (init_1string) {
695*7c478bd9Sstevel@tonic-gate 		putp(init_1string);
696*7c478bd9Sstevel@tonic-gate 	}
697*7c478bd9Sstevel@tonic-gate 
698*7c478bd9Sstevel@tonic-gate 	if (reset && reset_2string) {
699*7c478bd9Sstevel@tonic-gate 		putp(reset_2string);
700*7c478bd9Sstevel@tonic-gate 	} else if (init_2string) {
701*7c478bd9Sstevel@tonic-gate 		putp(init_2string);
702*7c478bd9Sstevel@tonic-gate 	}
703*7c478bd9Sstevel@tonic-gate 
704*7c478bd9Sstevel@tonic-gate 	/* Set up the tabs stops. */
705*7c478bd9Sstevel@tonic-gate 	settabs();
706*7c478bd9Sstevel@tonic-gate 
707*7c478bd9Sstevel@tonic-gate 	/* Send out initializing file. */
708*7c478bd9Sstevel@tonic-gate 	if (reset && reset_file) {
709*7c478bd9Sstevel@tonic-gate 		cat(reset_file);
710*7c478bd9Sstevel@tonic-gate 	} else if (init_file) {
711*7c478bd9Sstevel@tonic-gate 		cat(init_file);
712*7c478bd9Sstevel@tonic-gate 	}
713*7c478bd9Sstevel@tonic-gate 
714*7c478bd9Sstevel@tonic-gate 	/* Send final initialization strings. */
715*7c478bd9Sstevel@tonic-gate 	if (reset && reset_3string) {
716*7c478bd9Sstevel@tonic-gate 		putp(reset_3string);
717*7c478bd9Sstevel@tonic-gate 	} else if (init_3string) {
718*7c478bd9Sstevel@tonic-gate 		putp(init_3string);
719*7c478bd9Sstevel@tonic-gate 	}
720*7c478bd9Sstevel@tonic-gate 
721*7c478bd9Sstevel@tonic-gate 	if (carriage_return) {
722*7c478bd9Sstevel@tonic-gate 		putp(carriage_return);
723*7c478bd9Sstevel@tonic-gate 	} else {
724*7c478bd9Sstevel@tonic-gate 		(void) putchar('\r');
725*7c478bd9Sstevel@tonic-gate 	}
726*7c478bd9Sstevel@tonic-gate 
727*7c478bd9Sstevel@tonic-gate 	/* Send color initialization strings */
728*7c478bd9Sstevel@tonic-gate 
729*7c478bd9Sstevel@tonic-gate 	if (orig_colors)
730*7c478bd9Sstevel@tonic-gate 		putp(orig_colors);
731*7c478bd9Sstevel@tonic-gate 
732*7c478bd9Sstevel@tonic-gate 	if (orig_pair)
733*7c478bd9Sstevel@tonic-gate 	putp(orig_pair);
734*7c478bd9Sstevel@tonic-gate 
735*7c478bd9Sstevel@tonic-gate 	/* Let the terminal settle down. */
736*7c478bd9Sstevel@tonic-gate 	(void) fflush(stdout);
737*7c478bd9Sstevel@tonic-gate 	(void) sleep(1);
738*7c478bd9Sstevel@tonic-gate }
739*7c478bd9Sstevel@tonic-gate 
740*7c478bd9Sstevel@tonic-gate static void
741*7c478bd9Sstevel@tonic-gate reset_term()
742*7c478bd9Sstevel@tonic-gate {
743*7c478bd9Sstevel@tonic-gate 	reset++;
744*7c478bd9Sstevel@tonic-gate 	initterm();
745*7c478bd9Sstevel@tonic-gate }
746