xref: /onnv-gate/usr/src/cmd/tput/tput.c (revision 9354:9559ac454e7e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*9354STim.Marsland@Sun.COM  * Common Development and Distribution License (the "License").
6*9354STim.Marsland@Sun.COM  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*9354STim.Marsland@Sun.COM 
220Sstevel@tonic-gate /*
23*9354STim.Marsland@Sun.COM  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /*
310Sstevel@tonic-gate  *	tput - print terminal attribute
320Sstevel@tonic-gate  *
330Sstevel@tonic-gate  *  return-codes - command line arguments:
340Sstevel@tonic-gate  *	0: ok if boolean capname -> TRUE
350Sstevel@tonic-gate  *	1: for boolean capname -> FALSE
360Sstevel@tonic-gate  *
370Sstevel@tonic-gate  *  return-codes - standard input arguments:
380Sstevel@tonic-gate  *	0: ok; tput for all lines was successful
390Sstevel@tonic-gate  *
400Sstevel@tonic-gate  *  return-codes - both cases:
410Sstevel@tonic-gate  *	2	usage error
420Sstevel@tonic-gate  *	3	bad terminal type given or no terminfo database
430Sstevel@tonic-gate  *	4	unknown capname
440Sstevel@tonic-gate  *	-1	capname is a numeric variable that is not specified in the
450Sstevel@tonic-gate  *		terminfo database(E.g. tpu -T450 lines).
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  *  tput printfs a value if an INT capname was given; e.g. cols.
480Sstevel@tonic-gate  *	putp's a string if a STRING capname was given; e.g. clear. and
490Sstevel@tonic-gate  *  for BOOLEAN capnames, e.g. hard-copy, just returns the boolean value.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #include <curses.h>
530Sstevel@tonic-gate #include <term.h>
540Sstevel@tonic-gate #include <fcntl.h>
550Sstevel@tonic-gate #include <ctype.h>
560Sstevel@tonic-gate #include <stdlib.h>
570Sstevel@tonic-gate #include <string.h>
580Sstevel@tonic-gate #include <sys/types.h>
590Sstevel@tonic-gate #include <unistd.h>
600Sstevel@tonic-gate #include <locale.h>
610Sstevel@tonic-gate 
620Sstevel@tonic-gate /* externs from libcurses */
630Sstevel@tonic-gate extern int tigetnum();
640Sstevel@tonic-gate 
650Sstevel@tonic-gate static int outputcap(char *cap, int argc, char **argv);
660Sstevel@tonic-gate static int allnumeric(char *string);
670Sstevel@tonic-gate static int getpad(char *cap);
680Sstevel@tonic-gate static void setdelay();
690Sstevel@tonic-gate static void settabs();
700Sstevel@tonic-gate static void cat(char *file);
710Sstevel@tonic-gate static void initterm();
720Sstevel@tonic-gate static void reset_term();
730Sstevel@tonic-gate 
740Sstevel@tonic-gate static char *progname;		/* argv[0] */
750Sstevel@tonic-gate static int CurrentBaudRate;	/* current baud rate */
760Sstevel@tonic-gate static int reset = 0;		/* called as reset_term */
770Sstevel@tonic-gate static int fildes = 1;
780Sstevel@tonic-gate 
790Sstevel@tonic-gate int
main(int argc,char ** argv)800Sstevel@tonic-gate main(int argc, char **argv)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	int i, std_argc;
830Sstevel@tonic-gate 	char *term = getenv("TERM");
840Sstevel@tonic-gate 	char *cap, std_input = FALSE;
850Sstevel@tonic-gate 	int setuperr;
860Sstevel@tonic-gate 
870Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
880Sstevel@tonic-gate #if !defined(TEXT_DOMAIN)
890Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
900Sstevel@tonic-gate #endif
910Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
920Sstevel@tonic-gate 
930Sstevel@tonic-gate 	progname = argv[0];
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	while ((i = getopt(argc, argv, "ST:")) != EOF) {
960Sstevel@tonic-gate 		switch (i) {
970Sstevel@tonic-gate 		case 'T':
980Sstevel@tonic-gate 			fildes = -1;
990Sstevel@tonic-gate 			(void) putenv("LINES=");
1000Sstevel@tonic-gate 			(void) putenv("COLUMNS=");
1010Sstevel@tonic-gate 			term = optarg;
1020Sstevel@tonic-gate 			break;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 		case 'S':
1050Sstevel@tonic-gate 			std_input = TRUE;
1060Sstevel@tonic-gate 			break;
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 		case '?':			/* FALLTHROUGH		*/
1090Sstevel@tonic-gate 		usage:				/* FALLTHROUGH		*/
1100Sstevel@tonic-gate 		default:
1110Sstevel@tonic-gate 			(void) fprintf(stderr, gettext(
112*9354STim.Marsland@Sun.COM 			    "usage:\t%s [-T [term]] capname "
113*9354STim.Marsland@Sun.COM 			    "[parm argument...]\n"), progname);
1140Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("OR:\t%s -S <<\n"),
115*9354STim.Marsland@Sun.COM 			    progname);
1160Sstevel@tonic-gate 			exit(2);
1170Sstevel@tonic-gate 		}
1180Sstevel@tonic-gate 	}
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	if (!term || !*term) {
1210Sstevel@tonic-gate 		(void) fprintf(stderr,
122*9354STim.Marsland@Sun.COM 		    gettext("%s: No value for $TERM and no -T specified\n"),
123*9354STim.Marsland@Sun.COM 		    progname);
1240Sstevel@tonic-gate 		exit(2);
1250Sstevel@tonic-gate 	}
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	(void) setupterm(term, fildes, &setuperr);
1280Sstevel@tonic-gate 
1290Sstevel@tonic-gate 	switch (setuperr) {
1300Sstevel@tonic-gate 	case -2:
1310Sstevel@tonic-gate 		(void) fprintf(stderr,
132*9354STim.Marsland@Sun.COM 		    gettext("%s: unreadable terminal descriptor \"%s\"\n"),
133*9354STim.Marsland@Sun.COM 		    progname, term);
1340Sstevel@tonic-gate 		exit(3);
1350Sstevel@tonic-gate 		break;
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate 	case -1:
1380Sstevel@tonic-gate 		(void) fprintf(stderr,
139*9354STim.Marsland@Sun.COM 		    gettext("%s: no terminfo database\n"), progname);
1400Sstevel@tonic-gate 		exit(3);
1410Sstevel@tonic-gate 		break;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	case 0:
144*9354STim.Marsland@Sun.COM 		(void) fprintf(stderr,
145*9354STim.Marsland@Sun.COM 		    gettext("%s: unknown terminal \"%s\"\n"),
146*9354STim.Marsland@Sun.COM 		    progname, term);
147*9354STim.Marsland@Sun.COM 		exit(3);
1480Sstevel@tonic-gate 	}
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate 	reset_shell_mode();
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	/* command line arguments */
1530Sstevel@tonic-gate 	if (!std_input) {
1540Sstevel@tonic-gate 		if (argc == optind)
1550Sstevel@tonic-gate 			goto usage;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 		cap = argv[optind++];
1580Sstevel@tonic-gate 
1590Sstevel@tonic-gate 		if (strcmp(cap, "init") == 0)
1600Sstevel@tonic-gate 			initterm();
1610Sstevel@tonic-gate 		else if (strcmp(cap, "reset") == 0)
1620Sstevel@tonic-gate 			reset_term();
1630Sstevel@tonic-gate 		else if (strcmp(cap, "longname") == 0)
1640Sstevel@tonic-gate 			(void) printf("%s\n", longname());
1650Sstevel@tonic-gate 		else
1660Sstevel@tonic-gate 			exit(outputcap(cap, argc, argv));
1670Sstevel@tonic-gate 		return (0);
1680Sstevel@tonic-gate 	} else {			/* standard input argumets	*/
1690Sstevel@tonic-gate 		char buff[128];
1700Sstevel@tonic-gate 		char **v;
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		/* allocate storage for the 'faked' argv[] array	*/
1730Sstevel@tonic-gate 		v = (char **)malloc(10 * sizeof (char *));
1740Sstevel@tonic-gate 		for (i = 0; i < 10; i++)
1750Sstevel@tonic-gate 			v[i] = (char *)malloc(32 * sizeof (char));
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 		while (gets(buff) != NULL) {
1780Sstevel@tonic-gate 			/* read standard input line; skip over empty lines */
1790Sstevel@tonic-gate 			if ((std_argc =
1800Sstevel@tonic-gate 			    sscanf(buff, "%s %s %s %s %s %s %s %s %s %s",
1810Sstevel@tonic-gate 			    v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7],
1820Sstevel@tonic-gate 			    v[8], v[9])) < 1) {
1830Sstevel@tonic-gate 				continue;
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 			cap = v[0];
1870Sstevel@tonic-gate 			optind = 1;
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 			if (strcmp(cap, "init") == 0) {
1900Sstevel@tonic-gate 				initterm();
1910Sstevel@tonic-gate 			} else if (strcmp(cap, "reset") == 0) {
1920Sstevel@tonic-gate 				reset_term();
1930Sstevel@tonic-gate 			} else if (strcmp(cap, "longname") == 0) {
1940Sstevel@tonic-gate 				(void) printf("%s\n", longname());
1950Sstevel@tonic-gate 			} else {
1960Sstevel@tonic-gate 				(void) outputcap(cap, std_argc, v);
1970Sstevel@tonic-gate 			}
1980Sstevel@tonic-gate 			(void) fflush(stdout);
1990Sstevel@tonic-gate 		}
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 		return (0);
2020Sstevel@tonic-gate 	}
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate static long parm[9] = {
2060Sstevel@tonic-gate     0, 0, 0, 0, 0, 0, 0, 0, 0
2070Sstevel@tonic-gate };
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate static int
outputcap(char * cap,int argc,char ** argv)2100Sstevel@tonic-gate outputcap(char *cap, int argc, char **argv)
2110Sstevel@tonic-gate {
2120Sstevel@tonic-gate 	int parmset = 0;
2130Sstevel@tonic-gate 	char *thisstr;
2140Sstevel@tonic-gate 	int i;
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	if ((i = tigetflag(cap)) >= 0)
2170Sstevel@tonic-gate 		return (1 - i);
2180Sstevel@tonic-gate 
2190Sstevel@tonic-gate 	if ((i = tigetnum(cap)) >= -1) {
2200Sstevel@tonic-gate 		(void) printf("%d\n", i);
2210Sstevel@tonic-gate 		return (0);
2220Sstevel@tonic-gate 	}
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	if ((thisstr = tigetstr(cap)) != (char *)-1) {
2250Sstevel@tonic-gate 		if (!thisstr) {
2260Sstevel@tonic-gate 			return (1);
2270Sstevel@tonic-gate 		}
2280Sstevel@tonic-gate 		for (parmset = 0; optind < argc; optind++, parmset++)
2290Sstevel@tonic-gate 			if (allnumeric(argv[optind]))
2300Sstevel@tonic-gate 				parm[parmset] = atoi(argv[optind]);
2310Sstevel@tonic-gate 			else
2320Sstevel@tonic-gate 				parm[parmset] = (int)argv[optind];
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 		if (parmset)
2350Sstevel@tonic-gate 			putp(tparm(thisstr,
236*9354STim.Marsland@Sun.COM 			    parm[0], parm[1], parm[2], parm[3],
237*9354STim.Marsland@Sun.COM 			    parm[4], parm[5], parm[6], parm[7], parm[8]));
2380Sstevel@tonic-gate 		else
2390Sstevel@tonic-gate 			putp(thisstr);
2400Sstevel@tonic-gate 		return (0);
2410Sstevel@tonic-gate 	}
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	(void) fprintf(stderr,
2440Sstevel@tonic-gate 	    gettext("%s: unknown terminfo capability '%s'\n"), progname, cap);
2450Sstevel@tonic-gate 
2460Sstevel@tonic-gate 	exit(4);
2470Sstevel@tonic-gate 	/* NOTREACHED */
2480Sstevel@tonic-gate }
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate /*
2510Sstevel@tonic-gate  *  The decision as to whether an argument is a number or not is to simply
2520Sstevel@tonic-gate  *  look at whether there are any non-digits in the string.
2530Sstevel@tonic-gate  */
2540Sstevel@tonic-gate static int
allnumeric(char * string)2550Sstevel@tonic-gate allnumeric(char *string)
2560Sstevel@tonic-gate {
2570Sstevel@tonic-gate 	if (*string) {
2580Sstevel@tonic-gate 		while (*string) {
2590Sstevel@tonic-gate 			if (!isdigit(*string++)) {
2600Sstevel@tonic-gate 				return (0);
2610Sstevel@tonic-gate 			}
2620Sstevel@tonic-gate 		}
2630Sstevel@tonic-gate 		return (1);
2640Sstevel@tonic-gate 	} else {
2650Sstevel@tonic-gate 		return (0);
2660Sstevel@tonic-gate 	}
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  *  SYSTEM DEPENDENT TERMINAL DELAY TABLES
2710Sstevel@tonic-gate  *
2720Sstevel@tonic-gate  *	These tables maintain the correspondence between the delays
2730Sstevel@tonic-gate  *	defined in terminfo and the delay algorithms in the tty driver
2740Sstevel@tonic-gate  *	on the particular systems. For each type of delay, the bits used
2750Sstevel@tonic-gate  *	for that delay must be specified, in XXbits, and a table
2760Sstevel@tonic-gate  *	must be defined giving correspondences between delays and
2770Sstevel@tonic-gate  *	algorithms. Algorithms which are not fixed delays, such
2780Sstevel@tonic-gate  *	as dependent on current column or line number, must be
2790Sstevel@tonic-gate  *	kludged in some way at this time.
2800Sstevel@tonic-gate  *
2810Sstevel@tonic-gate  *	Some of this was taken from tset(1).
2820Sstevel@tonic-gate  */
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate struct delay
2850Sstevel@tonic-gate {
2860Sstevel@tonic-gate     int d_delay;
2870Sstevel@tonic-gate     int d_bits;
2880Sstevel@tonic-gate };
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate /* The appropriate speeds for various termio settings. */
2910Sstevel@tonic-gate static int speeds[] = {
2920Sstevel@tonic-gate 		0,	/*  B0,		*/
2930Sstevel@tonic-gate 		50,	/*  B50,	*/
2940Sstevel@tonic-gate 		75,	/*  B75,	*/
2950Sstevel@tonic-gate 		110,	/*  B110,	*/
2960Sstevel@tonic-gate 		134,	/*  B134,	*/
2970Sstevel@tonic-gate 		150,	/*  B150,	*/
2980Sstevel@tonic-gate 		200,	/*  B200,	*/
2990Sstevel@tonic-gate 		300,	/*  B300,	*/
3000Sstevel@tonic-gate 		600,	/*  B600,	*/
3010Sstevel@tonic-gate 		1200,	/*  B1200,	*/
3020Sstevel@tonic-gate 		1800,	/*  B1800,	*/
3030Sstevel@tonic-gate 		2400,	/*  B2400,	*/
3040Sstevel@tonic-gate 		4800,	/*  B4800,	*/
3050Sstevel@tonic-gate 		9600,	/*  B9600,	*/
3060Sstevel@tonic-gate 		19200,	/*  EXTA,	*/
3070Sstevel@tonic-gate 		38400,	/*  EXTB,	*/
3080Sstevel@tonic-gate 		57600,	/*  B57600,	*/
3090Sstevel@tonic-gate 		76800,	/*  B76800,	*/
3100Sstevel@tonic-gate 		115200,	/*  B115200,	*/
3110Sstevel@tonic-gate 		153600,	/*  B153600,	*/
3120Sstevel@tonic-gate 		230400,	/*  B230400,	*/
3130Sstevel@tonic-gate 		307200,	/*  B307200,	*/
3140Sstevel@tonic-gate 		460800,	/*  B460800,	*/
315*9354STim.Marsland@Sun.COM 		921600, /*  B921600,	*/
3160Sstevel@tonic-gate 		0,
3170Sstevel@tonic-gate };
3180Sstevel@tonic-gate 
3190Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
3200Sstevel@tonic-gate /*	Unix 3.0 on up */
3210Sstevel@tonic-gate 
3220Sstevel@tonic-gate /*    Carriage Return delays	*/
3230Sstevel@tonic-gate 
3240Sstevel@tonic-gate static int	CRbits = CRDLY;
3250Sstevel@tonic-gate static struct delay	CRdelay[] =
3260Sstevel@tonic-gate {
3270Sstevel@tonic-gate 	0,	CR0,
3280Sstevel@tonic-gate 	80,	CR1,
3290Sstevel@tonic-gate 	100,	CR2,
3300Sstevel@tonic-gate 	150,	CR3,
3310Sstevel@tonic-gate 	-1
3320Sstevel@tonic-gate };
3330Sstevel@tonic-gate 
3340Sstevel@tonic-gate /*	New Line delays	*/
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate static int	NLbits = NLDLY;
3370Sstevel@tonic-gate static struct delay	NLdelay[] =
3380Sstevel@tonic-gate {
3390Sstevel@tonic-gate 	0,	NL0,
3400Sstevel@tonic-gate 	100,	NL1,
3410Sstevel@tonic-gate 	-1
3420Sstevel@tonic-gate };
3430Sstevel@tonic-gate 
3440Sstevel@tonic-gate /*	Back Space delays	*/
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate static int	BSbits = BSDLY;
3470Sstevel@tonic-gate static struct delay	BSdelay[] =
3480Sstevel@tonic-gate {
3490Sstevel@tonic-gate 	0,	BS0,
3500Sstevel@tonic-gate 	50,	BS1,
3510Sstevel@tonic-gate 	-1
3520Sstevel@tonic-gate };
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate /*	TaB delays	*/
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate static int	TBbits = TABDLY;
3570Sstevel@tonic-gate static struct delay	TBdelay[] =
3580Sstevel@tonic-gate {
3590Sstevel@tonic-gate 	0,	TAB0,
3600Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
3610Sstevel@tonic-gate 	100,	TAB2,
3620Sstevel@tonic-gate 				/* TAB3 is XTABS and not a delay */
3630Sstevel@tonic-gate 	-1
3640Sstevel@tonic-gate };
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate /*	Form Feed delays	*/
3670Sstevel@tonic-gate 
3680Sstevel@tonic-gate static int	FFbits = FFDLY;
3690Sstevel@tonic-gate static struct delay	FFdelay[] =
3700Sstevel@tonic-gate {
3710Sstevel@tonic-gate 	0,	FF0,
3720Sstevel@tonic-gate 	2000,	FF1,
3730Sstevel@tonic-gate 	-1
3740Sstevel@tonic-gate };
3750Sstevel@tonic-gate 
3760Sstevel@tonic-gate #else	/* BSD */
3770Sstevel@tonic-gate 
3780Sstevel@tonic-gate /*	Carriage Return delays	*/
3790Sstevel@tonic-gate 
3800Sstevel@tonic-gate int	CRbits = CRDELAY;
3810Sstevel@tonic-gate struct delay	CRdelay[] =
3820Sstevel@tonic-gate {
3830Sstevel@tonic-gate 	0,	CR0,
3840Sstevel@tonic-gate 	9,	CR3,
3850Sstevel@tonic-gate 	80,	CR1,
3860Sstevel@tonic-gate 	160,	CR2,
3870Sstevel@tonic-gate 	-1
3880Sstevel@tonic-gate };
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*	New Line delays	*/
3910Sstevel@tonic-gate 
3920Sstevel@tonic-gate int	NLbits = NLDELAY;
3930Sstevel@tonic-gate struct delay	NLdelay[] =
3940Sstevel@tonic-gate {
3950Sstevel@tonic-gate 	0,	NL0,
3960Sstevel@tonic-gate 	66,	NL1,		/* special M37 delay */
3970Sstevel@tonic-gate 	100,	NL2,
3980Sstevel@tonic-gate 	-1
3990Sstevel@tonic-gate };
4000Sstevel@tonic-gate 
4010Sstevel@tonic-gate /*	Tab delays	*/
4020Sstevel@tonic-gate 
4030Sstevel@tonic-gate int	TBbits = TBDELAY;
4040Sstevel@tonic-gate struct delay	TBdelay[] =
4050Sstevel@tonic-gate {
4060Sstevel@tonic-gate 	0,	TAB0,
4070Sstevel@tonic-gate 	11,	TAB1,		/* special M37 delay */
4080Sstevel@tonic-gate 	-1
4090Sstevel@tonic-gate };
4100Sstevel@tonic-gate 
4110Sstevel@tonic-gate /*	Form Feed delays	*/
4120Sstevel@tonic-gate 
4130Sstevel@tonic-gate int	FFbits = VTDELAY;
4140Sstevel@tonic-gate struct delay	FFdelay[] =
4150Sstevel@tonic-gate {
4160Sstevel@tonic-gate 	0,	FF0,
4170Sstevel@tonic-gate 	2000,	FF1,
4180Sstevel@tonic-gate 	-1
4190Sstevel@tonic-gate };
4200Sstevel@tonic-gate #endif	/* BSD */
4210Sstevel@tonic-gate 
4220Sstevel@tonic-gate /*
4230Sstevel@tonic-gate  *  Initterm, a.k.a. reset_term, does terminal specific initialization. In
4240Sstevel@tonic-gate  *  particular, the init_strings from terminfo are output and tabs are
4250Sstevel@tonic-gate  *  set, if they aren't hardwired in. Much of this stuff was done by
4260Sstevel@tonic-gate  *  the tset(1) program.
4270Sstevel@tonic-gate  */
4280Sstevel@tonic-gate 
4290Sstevel@tonic-gate /*
4300Sstevel@tonic-gate  *  Figure out how many milliseconds of padding the capability cap
4310Sstevel@tonic-gate  *  needs and return that number. Padding is stored in the string as "$<n>",
4320Sstevel@tonic-gate  *  where n is the number of milliseconds of padding. More than one
4330Sstevel@tonic-gate  *  padding string is allowed within the string, although this is unlikely.
4340Sstevel@tonic-gate  */
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate static int
getpad(char * cap)4370Sstevel@tonic-gate getpad(char *cap)
4380Sstevel@tonic-gate {
4390Sstevel@tonic-gate 	int padding = 0;
4400Sstevel@tonic-gate 
4410Sstevel@tonic-gate 	/* No padding needed at speeds below padding_baud_rate */
4420Sstevel@tonic-gate 	if (padding_baud_rate > CurrentBaudRate || cap == NULL)
4430Sstevel@tonic-gate 		return (0);
4440Sstevel@tonic-gate 
4450Sstevel@tonic-gate 	while (*cap) {
4460Sstevel@tonic-gate 		if ((cap[0] == '$') && (cap[1] == '<')) {
4470Sstevel@tonic-gate 			cap++;
4480Sstevel@tonic-gate 			cap++;
4490Sstevel@tonic-gate 			padding += atoi(cap);
4500Sstevel@tonic-gate 			while (isdigit (*cap))
4510Sstevel@tonic-gate 				cap++;
4520Sstevel@tonic-gate 			while (*cap == '.' || *cap == '/' || *cap == '*' ||
453*9354STim.Marsland@Sun.COM 			    isdigit(*cap))
4540Sstevel@tonic-gate 				cap++;
4550Sstevel@tonic-gate 			while (*cap == '>')
4560Sstevel@tonic-gate 				cap++;
4570Sstevel@tonic-gate 		} else {
4580Sstevel@tonic-gate 			cap++;
4590Sstevel@tonic-gate 		}
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 
4620Sstevel@tonic-gate 	return (padding);
4630Sstevel@tonic-gate }
4640Sstevel@tonic-gate 
4650Sstevel@tonic-gate /*
4660Sstevel@tonic-gate  *  Set the appropriate delay bits in the termio structure for
4670Sstevel@tonic-gate  *  the given delay.
4680Sstevel@tonic-gate  */
4690Sstevel@tonic-gate static void
setdelay(delay,delaytable,bits,flags)4700Sstevel@tonic-gate setdelay(delay, delaytable, bits, flags)
4710Sstevel@tonic-gate register int delay;
4720Sstevel@tonic-gate struct delay delaytable[];
4730Sstevel@tonic-gate int bits;
4740Sstevel@tonic-gate #ifdef SYSV
4750Sstevel@tonic-gate tcflag_t *flags;
4760Sstevel@tonic-gate #else	/* SYSV */
4770Sstevel@tonic-gate unsigned short *flags;
4780Sstevel@tonic-gate #endif	/* SYSV */
4790Sstevel@tonic-gate {
4800Sstevel@tonic-gate 	register struct delay  *p;
4810Sstevel@tonic-gate 	register struct delay  *lastdelay;
4820Sstevel@tonic-gate 
4830Sstevel@tonic-gate 	/* Clear out the bits, replace with new ones */
4840Sstevel@tonic-gate 	*flags &= ~bits;
4850Sstevel@tonic-gate 
4860Sstevel@tonic-gate 	/* Scan the delay table for first entry with adequate delay */
4870Sstevel@tonic-gate 	for (lastdelay = p = delaytable;
4880Sstevel@tonic-gate 	    (p -> d_delay >= 0) && (p -> d_delay < delay);
4890Sstevel@tonic-gate 	    p++) {
4900Sstevel@tonic-gate 		lastdelay = p;
4910Sstevel@tonic-gate 	}
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	/* use last entry if none will do */
4940Sstevel@tonic-gate 	*flags |= lastdelay -> d_bits;
4950Sstevel@tonic-gate }
4960Sstevel@tonic-gate 
4970Sstevel@tonic-gate /*
4980Sstevel@tonic-gate  * Set the hardware tabs on the terminal, using clear_all_tabs,
4990Sstevel@tonic-gate  * set_tab, and column_address capabilities. Cursor_address and cursor_right
5000Sstevel@tonic-gate  * may also be used, if necessary.
5010Sstevel@tonic-gate  * This is done before the init_file and init_3string, so they can patch in
5020Sstevel@tonic-gate  * case we blow this.
5030Sstevel@tonic-gate  */
5040Sstevel@tonic-gate 
5050Sstevel@tonic-gate static void
settabs()5060Sstevel@tonic-gate settabs()
5070Sstevel@tonic-gate {
5080Sstevel@tonic-gate 	register int c;
5090Sstevel@tonic-gate 
5100Sstevel@tonic-gate 	/* Do not set tabs if they power up properly. */
5110Sstevel@tonic-gate 	if (init_tabs == 8)
5120Sstevel@tonic-gate 		return;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 	if (set_tab) {
5150Sstevel@tonic-gate 		/* Force the cursor to be at the left margin. */
5160Sstevel@tonic-gate 		if (carriage_return)
5170Sstevel@tonic-gate 			putp(carriage_return);
5180Sstevel@tonic-gate 		else
5190Sstevel@tonic-gate 			(void) putchar('\r');
5200Sstevel@tonic-gate 
5210Sstevel@tonic-gate 		/* Clear any current tab settings. */
5220Sstevel@tonic-gate 		if (clear_all_tabs)
5230Sstevel@tonic-gate 			putp(clear_all_tabs);
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 		/* Set the tabs. */
5260Sstevel@tonic-gate 		for (c = 8; c < columns; c += 8) {
5270Sstevel@tonic-gate 			/* Get to that column. */
5280Sstevel@tonic-gate 			(void) fputs("        ", stdout);
5290Sstevel@tonic-gate 
5300Sstevel@tonic-gate 			/* Set the tab. */
5310Sstevel@tonic-gate 			putp(set_tab);
5320Sstevel@tonic-gate 		}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 		/* Get back to the left column. */
5350Sstevel@tonic-gate 		if (carriage_return)
5360Sstevel@tonic-gate 			putp(carriage_return);
5370Sstevel@tonic-gate 		else
5380Sstevel@tonic-gate 			(void) putchar('\r');
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	}
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate 
5430Sstevel@tonic-gate /*
5440Sstevel@tonic-gate  *  Copy "file" onto standard output.
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate static void
cat(file)5480Sstevel@tonic-gate cat(file)
5490Sstevel@tonic-gate char *file;				/* File to copy. */
5500Sstevel@tonic-gate {
5510Sstevel@tonic-gate 	register int fd;			/* File descriptor. */
5520Sstevel@tonic-gate 	register ssize_t i;			/* Number characters read. */
5530Sstevel@tonic-gate 	char buf[BUFSIZ];			/* Buffer to read into. */
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate 	fd = open(file, O_RDONLY);
5560Sstevel@tonic-gate 
5570Sstevel@tonic-gate 	if (fd < 0) {
5580Sstevel@tonic-gate 		perror("Cannot open initialization file");
5590Sstevel@tonic-gate 	} else {
5600Sstevel@tonic-gate 		while ((i = read(fd, buf, BUFSIZ)) > (ssize_t)0)
5610Sstevel@tonic-gate 			(void) write(fileno(stdout), buf, (unsigned)i);
5620Sstevel@tonic-gate 		(int)close(fd);
5630Sstevel@tonic-gate 	}
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate 
5660Sstevel@tonic-gate /*
5670Sstevel@tonic-gate  *  Initialize the terminal.
5680Sstevel@tonic-gate  *  Send the initialization strings to the terminal.
5690Sstevel@tonic-gate  */
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate static void
initterm()5720Sstevel@tonic-gate initterm()
5730Sstevel@tonic-gate {
5740Sstevel@tonic-gate 	register int filedes;		/* File descriptor for ioctl's. */
5750Sstevel@tonic-gate #if defined(SYSV) || defined(USG)
5760Sstevel@tonic-gate 	struct termio termmode;		/* To hold terminal settings. */
5770Sstevel@tonic-gate 	struct termios termmodes;	/* To hold terminal settings. */
5780Sstevel@tonic-gate 	int i;
5790Sstevel@tonic-gate 	int istermios = -1;
5800Sstevel@tonic-gate #define	GTTY(fd, mode)	ioctl(fd, TCGETA, mode)
5810Sstevel@tonic-gate #define	GTTYS(fd, mode) \
5820Sstevel@tonic-gate 	(istermios = ioctl(fd, TCGETS, mode))
5830Sstevel@tonic-gate #define	STTY(fd, mode)	ioctl(fd, TCSETAW, mode)
5840Sstevel@tonic-gate #define	STTYS(fd, mode)	ioctl(fd, TCSETSW, mode)
5850Sstevel@tonic-gate #define	SPEED(mode)	(mode.c_cflag & CBAUD)
5860Sstevel@tonic-gate #define	SPEEDS(mode)	(cfgetospeed(&mode))
5870Sstevel@tonic-gate #define	OFLAG(mode)	mode.c_oflag
5880Sstevel@tonic-gate #else	/* BSD */
5890Sstevel@tonic-gate 	struct sgttyb termmode;		/* To hold terminal settings. */
5900Sstevel@tonic-gate #define	GTTY(fd, mode)	gtty(fd, mode)
5910Sstevel@tonic-gate #define	STTY(fd, mode)	stty(fd, mode)
5920Sstevel@tonic-gate #define	SPEED(mode)	(mode.sg_ospeed & 017)
5930Sstevel@tonic-gate #define	OFLAG(mode)	mode.sg_flags
5940Sstevel@tonic-gate #define	TAB3		XTABS
5950Sstevel@tonic-gate #endif
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	/* Get the terminal settings. */
5980Sstevel@tonic-gate 	/* First try standard output, then standard error, */
5990Sstevel@tonic-gate 	/* then standard input, then /dev/tty. */
6000Sstevel@tonic-gate #ifdef SYSV
6010Sstevel@tonic-gate 	if ((filedes = 1, GTTYS(filedes, &termmodes) < 0) ||
6020Sstevel@tonic-gate 	    (filedes = 2, GTTYS(filedes, &termmodes) < 0) ||
6030Sstevel@tonic-gate 	    (filedes = 0, GTTYS(filedes, &termmodes) < 0) ||
6040Sstevel@tonic-gate 	    (filedes = open("/dev/tty", O_RDWR),
6050Sstevel@tonic-gate 	    GTTYS(filedes, &termmodes) < 0)) {
6060Sstevel@tonic-gate #endif	/* SYSV */
6070Sstevel@tonic-gate 		if ((filedes = 1, GTTY(filedes, &termmode) == -1) ||
6080Sstevel@tonic-gate 		    (filedes = 2, GTTY(filedes, &termmode) == -1) ||
6090Sstevel@tonic-gate 		    (filedes = 0, GTTY(filedes, &termmode) == -1) ||
6100Sstevel@tonic-gate 		    (filedes = open("/dev/tty", O_RDWR),
6110Sstevel@tonic-gate 		    GTTY(filedes, &termmode) == -1)) {
6120Sstevel@tonic-gate 			filedes = -1;
6130Sstevel@tonic-gate 			CurrentBaudRate = speeds[B1200];
6140Sstevel@tonic-gate 		} else
6150Sstevel@tonic-gate 			CurrentBaudRate = speeds[SPEED(termmode)];
6160Sstevel@tonic-gate #ifdef SYSV
6170Sstevel@tonic-gate 		termmodes.c_lflag = termmode.c_lflag;
6180Sstevel@tonic-gate 		termmodes.c_oflag = termmode.c_oflag;
6190Sstevel@tonic-gate 		termmodes.c_iflag = termmode.c_iflag;
6200Sstevel@tonic-gate 		termmodes.c_cflag = termmode.c_cflag;
6210Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
6220Sstevel@tonic-gate 			termmodes.c_cc[i] = termmode.c_cc[i];
6230Sstevel@tonic-gate 	} else
6240Sstevel@tonic-gate 		CurrentBaudRate = speeds[SPEEDS(termmodes)];
6250Sstevel@tonic-gate #endif	/* SYSV */
6260Sstevel@tonic-gate 
6270Sstevel@tonic-gate 	if (xon_xoff) {
6280Sstevel@tonic-gate #ifdef SYSV
6290Sstevel@tonic-gate 		OFLAG(termmodes) &=
630*9354STim.Marsland@Sun.COM 		    ~(NLbits | CRbits | BSbits | FFbits | TBbits);
6310Sstevel@tonic-gate #else	/* SYSV */
6320Sstevel@tonic-gate 		OFLAG(termmode) &=
633*9354STim.Marsland@Sun.COM 		    ~(NLbits | CRbits | BSbits | FFbits | TBbits);
6340Sstevel@tonic-gate #endif	/* SYSV */
6350Sstevel@tonic-gate 	} else {
6360Sstevel@tonic-gate #ifdef SYSV
637*9354STim.Marsland@Sun.COM 		setdelay(getpad(carriage_return),
638*9354STim.Marsland@Sun.COM 		    CRdelay, CRbits, &OFLAG(termmodes));
639*9354STim.Marsland@Sun.COM 		setdelay(getpad(scroll_forward),
640*9354STim.Marsland@Sun.COM 		    NLdelay, NLbits, &OFLAG(termmodes));
641*9354STim.Marsland@Sun.COM 		setdelay(getpad(cursor_left),
642*9354STim.Marsland@Sun.COM 		    BSdelay, BSbits, &OFLAG(termmodes));
643*9354STim.Marsland@Sun.COM 		setdelay(getpad(form_feed),
644*9354STim.Marsland@Sun.COM 		    FFdelay, FFbits, &OFLAG(termmodes));
645*9354STim.Marsland@Sun.COM 		setdelay(getpad(tab),
646*9354STim.Marsland@Sun.COM 		    TBdelay, TBbits, &OFLAG(termmodes));
6470Sstevel@tonic-gate #else	/* SYSV */
648*9354STim.Marsland@Sun.COM 		setdelay(getpad(carriage_return),
649*9354STim.Marsland@Sun.COM 		    CRdelay, CRbits, &OFLAG(termmode));
650*9354STim.Marsland@Sun.COM 		setdelay(getpad(scroll_forward),
651*9354STim.Marsland@Sun.COM 		    NLdelay, NLbits, &OFLAG(termmode));
652*9354STim.Marsland@Sun.COM 		setdelay(getpad(cursor_left),
653*9354STim.Marsland@Sun.COM 		    BSdelay, BSbits, &OFLAG(termmode));
654*9354STim.Marsland@Sun.COM 		setdelay(getpad(form_feed),
655*9354STim.Marsland@Sun.COM 		    FFdelay, FFbits, &OFLAG(termmode));
656*9354STim.Marsland@Sun.COM 		setdelay(getpad(tab),
657*9354STim.Marsland@Sun.COM 		    TBdelay, TBbits, &OFLAG(termmode));
6580Sstevel@tonic-gate #endif	/* SYSV */
6590Sstevel@tonic-gate 	}
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate 	/* If tabs can be sent to the tty, turn off their expansion. */
6620Sstevel@tonic-gate 	if (tab && set_tab || init_tabs == 8) {
6630Sstevel@tonic-gate #ifdef SYSV
6640Sstevel@tonic-gate 		OFLAG(termmodes) &= ~(TAB3);
6650Sstevel@tonic-gate #else	/* SYSV */
6660Sstevel@tonic-gate 		OFLAG(termmode) &= ~(TAB3);
6670Sstevel@tonic-gate #endif	/* SYSV */
6680Sstevel@tonic-gate 	} else {
6690Sstevel@tonic-gate #ifdef SYSV
6700Sstevel@tonic-gate 		OFLAG(termmodes) |= TAB3;
6710Sstevel@tonic-gate #else	/* SYSV */
6720Sstevel@tonic-gate 		OFLAG(termmode) |= TAB3;
6730Sstevel@tonic-gate #endif	/* SYSV */
6740Sstevel@tonic-gate 	}
6750Sstevel@tonic-gate 
6760Sstevel@tonic-gate 	/* Do the changes to the terminal settings */
6770Sstevel@tonic-gate #ifdef SYSV
6780Sstevel@tonic-gate 	if (istermios < 0) {
6790Sstevel@tonic-gate 		int i;
6800Sstevel@tonic-gate 
6810Sstevel@tonic-gate 		termmode.c_lflag = termmodes.c_lflag;
6820Sstevel@tonic-gate 		termmode.c_oflag = termmodes.c_oflag;
6830Sstevel@tonic-gate 		termmode.c_iflag = termmodes.c_iflag;
6840Sstevel@tonic-gate 		termmode.c_cflag = termmodes.c_cflag;
6850Sstevel@tonic-gate 		for (i = 0; i < NCC; i++)
6860Sstevel@tonic-gate 			termmode.c_cc[i] = termmodes.c_cc[i];
6870Sstevel@tonic-gate 		(void) STTY(filedes, &termmode);
6880Sstevel@tonic-gate 	} else
6890Sstevel@tonic-gate 		(void) STTYS(filedes, &termmodes);
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate #else	/* SYSV */
6920Sstevel@tonic-gate 	(void) STTY(filedes, &termmode);
6930Sstevel@tonic-gate #endif	/* SYSV */
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	/* Send first initialization strings. */
6960Sstevel@tonic-gate 	if (init_prog)
6970Sstevel@tonic-gate 	(void) system(init_prog);
6980Sstevel@tonic-gate 
6990Sstevel@tonic-gate 	if (reset && reset_1string) {
7000Sstevel@tonic-gate 		putp(reset_1string);
7010Sstevel@tonic-gate 	} else if (init_1string) {
7020Sstevel@tonic-gate 		putp(init_1string);
7030Sstevel@tonic-gate 	}
7040Sstevel@tonic-gate 
7050Sstevel@tonic-gate 	if (reset && reset_2string) {
7060Sstevel@tonic-gate 		putp(reset_2string);
7070Sstevel@tonic-gate 	} else if (init_2string) {
7080Sstevel@tonic-gate 		putp(init_2string);
7090Sstevel@tonic-gate 	}
7100Sstevel@tonic-gate 
7110Sstevel@tonic-gate 	/* Set up the tabs stops. */
7120Sstevel@tonic-gate 	settabs();
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/* Send out initializing file. */
7150Sstevel@tonic-gate 	if (reset && reset_file) {
7160Sstevel@tonic-gate 		cat(reset_file);
7170Sstevel@tonic-gate 	} else if (init_file) {
7180Sstevel@tonic-gate 		cat(init_file);
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 
7210Sstevel@tonic-gate 	/* Send final initialization strings. */
7220Sstevel@tonic-gate 	if (reset && reset_3string) {
7230Sstevel@tonic-gate 		putp(reset_3string);
7240Sstevel@tonic-gate 	} else if (init_3string) {
7250Sstevel@tonic-gate 		putp(init_3string);
7260Sstevel@tonic-gate 	}
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	if (carriage_return) {
7290Sstevel@tonic-gate 		putp(carriage_return);
7300Sstevel@tonic-gate 	} else {
7310Sstevel@tonic-gate 		(void) putchar('\r');
7320Sstevel@tonic-gate 	}
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	/* Send color initialization strings */
7350Sstevel@tonic-gate 
7360Sstevel@tonic-gate 	if (orig_colors)
7370Sstevel@tonic-gate 		putp(orig_colors);
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 	if (orig_pair)
7400Sstevel@tonic-gate 	putp(orig_pair);
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate 	/* Let the terminal settle down. */
7430Sstevel@tonic-gate 	(void) fflush(stdout);
7440Sstevel@tonic-gate 	(void) sleep(1);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate static void
reset_term()7480Sstevel@tonic-gate reset_term()
7490Sstevel@tonic-gate {
7500Sstevel@tonic-gate 	reset++;
7510Sstevel@tonic-gate 	initterm();
7520Sstevel@tonic-gate }
753