xref: /original-bsd/lib/libcurses/setterm.c (revision 23a40993)
1 /*
2  * Terminal initialization routines.
3  *
4  * 06/01/83 (Berkeley) @(#)setterm.c	1.14
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 	static 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 
82 	if (COLS == 0)
83 		COLS = tgetnum("co");
84 	if (COLS <= 4)
85 		COLS = 80;
86 
87 # ifdef DEBUG
88 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
89 # endif
90 	aoftspace = tspace;
91 	zap();			/* get terminal description		*/
92 	if (tgoto(CM, destcol, destline)[0] == 'O')
93 		CA = FALSE, CM = 0;
94 	else
95 		CA = TRUE;
96 	PC = xPC ? xPC[0] : FALSE;
97 	aoftspace = tspace;
98 	strcpy(ttytype, longname(genbuf, type));
99 	if (unknown)
100 		return ERR;
101 	return OK;
102 }
103 /*
104  *	This routine gets all the terminal flags from the termcap database
105  */
106 zap() {
107 
108 	reg bool	**fp;
109 	reg char	*namp, ***sp;
110 	extern char	*tgetstr();
111 
112 	/*
113 	 * get boolean flags
114 	 */
115  	namp = "ambseohzinmimsncosulxn\0\0";
116 # ifdef FULLDEBUG
117 	fprintf(outf, "ZAP: namp = \"%s\"\n", namp);
118 # endif
119 	fp = sflags;
120 	do {
121 		*(*fp++) = tgetflag(namp);
122 # ifdef FULLDEBUG
123 		fprintf(outf, "ZAP: %.2s = %d", namp, *(*(fp - 1)));
124 # endif
125 		namp += 2;
126 	} while (*namp);
127 
128 	/*
129 	 * get string values
130 	 */
131 	namp = "albcbtcdceclcmcrdcdldmdoedeihoicimipllmandnlpcsesfsosrtatetiucueupusvbvsve";
132 # ifdef FULLDEBUG
133 	fprintf(outf, "ZAP: namp = \"%s\"\n", namp);
134 # endif
135 	sp = sstrs;
136 	do {
137 		*(*sp++) = tgetstr(namp, &aoftspace);
138 # ifdef FULLDEBUG
139 		fprintf(outf, "ZAP: %.2s = \"%s\"\n", namp, *(*(sp-1)));
140 # endif
141 		namp += 2;
142 	} while (*namp);
143 	if (tgetnum("sg") > 0)
144 		SO = NULL;
145 	if (tgetnum("ug") > 0)
146 		US = NULL;
147 	if (!SO && US) {
148 		SO = US;
149 		SE = UE;
150 	}
151 }
152 
153 /*
154  * return a capability from termcap
155  */
156 char *
157 getcap(name)
158 char *name;
159 {
160 	char *tgetstr();
161 
162 	return tgetstr(name, &aoftspace);
163 }
164