xref: /original-bsd/lib/libcurses/setterm.c (revision 6c57d260)
1 /*
2  * Terminal initialization routines.
3  *
4  * 05/15/81 (Berkeley) @(#)setterm.c	1.4
5  */
6 
7 # undef	DEBUG
8 
9 # include	"curses.ext"
10 # include	"cr_ex.h"
11 
12 static bool	*sflags[]	= {
13 			&AM, &BS, &EO, &HZ, &IN, &MI, &MS, &NC, &OS, &UL, &XN
14 		};
15 
16 static char	*xPC,
17 		**sstrs[]	= {
18 			&AL, &BC,  &BT, &CD, &CE, &CL, &CM, &DC, &DL,
19 			&DM, &DO,  &ED, &EI, &HO, &IC, &IM, &IP, &LL,
20 			&MA, &ND, &xPC, &SE, &SF, &SO, &SR, &TA, &TE,
21 			&TI, &UC,  &UE, &UP, &US, &VB, &VS, &VE
22 		},
23 		*tgoto();
24 
25 static char	tspace[128],		/* Space for capability strings */
26 		*aoftspace;		/* Address of tspace for relocation */
27 
28 static int	destcol, destline;
29 
30 /*
31  *	This routine does terminal type initialization routines, and
32  * calculation of flags at entry.  It is almost entirely stolen from
33  * Bill Joy's ex version 2.6.
34  */
35 short	ospeed = -1;
36 
37 gettmode() {
38 
39 	if (gtty(_tty_ch, &_tty) < 0)
40 		return;
41 	savetty();
42 	if (stty(_tty_ch, &_tty) < 0)
43 		_tty.sg_flags = _res_flg;
44 	ospeed = _tty.sg_ospeed;
45 	_res_flg = _tty.sg_flags;
46 	UPPERCASE = (_tty.sg_flags & LCASE) != 0;
47 	GT = ((_tty.sg_flags & XTABS) == 0);
48 	NONL = ((_tty.sg_flags & CRMOD) == 0);
49 	_tty.sg_flags &= ~XTABS;
50 # ifdef DEBUG
51 	fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
52 	fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
53 	fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
54 	fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
55 # endif
56 }
57 
58 setterm(type)
59 reg char	*type; {
60 
61 	reg int	unknown;
62 	char	genbuf[1024];
63 
64 # ifdef DEBUG
65 	fprintf(outf, "SETTERM(\"%s\")\n", type);
66 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
67 # endif
68 	if (type[0] == '\0')
69 		type = "xx";
70 	unknown = FALSE;
71 	if (tgetent(genbuf, type) != 1) {
72 		unknown++;
73 		strcpy(genbuf, "xx|dumb:");
74 	}
75 # ifdef DEBUG
76 	fprintf(outf, "SETTERM: tty = %s\n", type);
77 # endif
78 	if (LINES == 0)
79 		LINES = tgetnum("li");
80 	if (LINES <= 5)
81 		LINES = 24;
82 	else if (LINES > 48)
83 		LINES = 48;
84 
85 	if (COLS == 0)
86 		COLS = tgetnum("co");
87 	if (COLS <= 4)
88 		COLS = 80;
89 	else if (COLS > 1000)
90 		COLS = 1000;
91 # ifdef DEBUG
92 	fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
93 # endif
94 	aoftspace = tspace;
95 	zap();			/* get terminal description		*/
96 	if (tgoto(CM, destcol, destline)[0] == 'O')
97 		CA = FALSE, CM = 0;
98 	else
99 		CA = TRUE;
100 	PC = xPC ? xPC[0] : FALSE;
101 	aoftspace = tspace;
102 	strcpy(ttytype, longname(genbuf, type));
103 	if (unknown)
104 		return ERR;
105 	return OK;
106 }
107 /*
108  *	This routine gets all the terminal falgs from the termcap database
109  */
110 zap() {
111 
112 	reg bool	**fp;
113 	reg char	*namp, ***sp;
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 = "albcbtcdceclcmdcdldmdoedeihoicimipllmandpcsesfsosrtatetiucueupusvbvsve";
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 	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 	return tgetent(name, &aoftspace);
161 }
162