xref: /original-bsd/lib/libcurses/setterm.c (revision 1eec400c)
1 /*
2  * Terminal initialization routines.
3  *
4  * 04/01/82 (Berkeley) @(#)setterm.c	1.11
5  */
6 
7 # include	"curses.ext"
8 
9 static bool	*sflags[]	= {
10 			&AM, &BS, &EO, &HZ, &IN, &MI, &MS, &NC, &OS, &UL, &XN
11 		};
12 
13 static char	*xPC,
14 		**sstrs[]	= {
15 			&AL, &BC, &BT, &CD,  &CE, &CL, &CM, &CR, &DC,
16 			&DL, &DM, &DO, &ED,  &EI, &HO, &IC, &IM, &IP,
17 			&LL, &MA, &ND, &NL, &xPC, &SE, &SF, &SO, &SR,
18 			&TA, &TE, &TI, &UC,  &UE, &UP, &US, &VB, &VS,
19 			&VE
20 		},
21 		*tgoto();
22 
23 static char	tspace[256],		/* Space for capability strings */
24 		*aoftspace;		/* Address of tspace for relocation */
25 
26 static int	destcol, destline;
27 
28 /*
29  *	This routine does terminal type initialization routines, and
30  * calculation of flags at entry.  It is almost entirely stolen from
31  * Bill Joy's ex version 2.6.
32  */
33 short	ospeed = -1;
34 
35 gettmode() {
36 
37 	if (gtty(_tty_ch, &_tty) < 0)
38 		return;
39 	savetty();
40 	if (stty(_tty_ch, &_tty) < 0)
41 		_tty.sg_flags = _res_flg;
42 	ospeed = _tty.sg_ospeed;
43 	_res_flg = _tty.sg_flags;
44 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
45 	GT = ((_tty.sg_flags & XTABS) == 0);
46 	NONL = ((_tty.sg_flags & CRMOD) == 0);
47 	_tty.sg_flags &= ~XTABS;
48 	stty(_tty_ch, &_tty);
49 # ifdef DEBUG
50 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
51 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
52 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
53 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
54 # endif
55 }
56 
57 setterm(type)
58 reg char	*type; {
59 
60 	reg int	unknown;
61 	char	genbuf[1024];
62 
63 # ifdef DEBUG
64 	fprintf(outf, "SETTERM(\"%s\")\n", type);
65 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
66 # endif
67 	if (type[0] == '\0')
68 		type = "xx";
69 	unknown = FALSE;
70 	if (tgetent(genbuf, type) != 1) {
71 		unknown++;
72 		strcpy(genbuf, "xx|dumb:");
73 	}
74 # ifdef DEBUG
75 	fprintf(outf, "SETTERM: tty = %s\n", type);
76 # endif
77 	if (LINES == 0)
78 		LINES = tgetnum("li");
79 	if (LINES <= 5)
80 		LINES = 24;
81 	else if (LINES > 48)
82 		LINES = 48;
83 
84 	if (COLS == 0)
85 		COLS = tgetnum("co");
86 	if (COLS <= 4)
87 		COLS = 80;
88 	else if (COLS > 1000)
89 		COLS = 1000;
90 # ifdef DEBUG
91 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
92 # endif
93 	aoftspace = tspace;
94 	zap();			/* get terminal description		*/
95 	if (tgoto(CM, destcol, destline)[0] == 'O')
96 		CA = FALSE, CM = 0;
97 	else
98 		CA = TRUE;
99 	PC = xPC ? xPC[0] : FALSE;
100 	aoftspace = tspace;
101 	strcpy(ttytype, longname(genbuf, type));
102 	if (unknown)
103 		return ERR;
104 	return OK;
105 }
106 /*
107  *	This routine gets all the terminal flags from the termcap database
108  */
109 zap() {
110 
111 	reg bool	**fp;
112 	reg char	*namp, ***sp;
113 	reg int		SG, UG;
114 	extern char	*tgetstr();
115 
116 	/*
117 	 * get boolean flags
118 	 */
119  	namp = "ambseohzinmimsncosulxn\0\0";
120 # ifdef FULLDEBUG
121 	fprintf(outf, "ZAP: namp = \"%s\"\n", namp);
122 # endif
123 	fp = sflags;
124 	do {
125 		*(*fp++) = tgetflag(namp);
126 # ifdef FULLDEBUG
127 		fprintf(outf, "ZAP: %.2s = %d", namp, *(*(fp - 1)));
128 # endif
129 		namp += 2;
130 	} while (*namp);
131 
132 	/*
133 	 * get string values
134 	 */
135 	namp = "albcbtcdceclcmcrdcdldmdoedeihoicimipllmandnlpcsesfsosrtatetiucueupusvbvsve";
136 # ifdef FULLDEBUG
137 	fprintf(outf, "ZAP: namp = \"%s\"\n", namp);
138 # endif
139 	sp = sstrs;
140 	do {
141 		*(*sp++) = tgetstr(namp, &aoftspace);
142 # ifdef FULLDEBUG
143 		fprintf(outf, "ZAP: %.2s = \"%s\"\n", namp, *(*(sp-1)));
144 # endif
145 		namp += 2;
146 	} while (*namp);
147 	SG = tgetnum("sg");
148 	UG = tgetnum("ug");
149 	if ((SG > 0 || !SO) && (UG <= 0 && US)) {
150 		SO = US;
151 		SE = UE;
152 	}
153 }
154 
155 /*
156  * return a capability from termcap
157  */
158 char *
159 getcap(name)
160 char *name;
161 {
162 	char *tgetstr();
163 
164 	return tgetstr(name, &aoftspace);
165 }
166