date and time created 83/05/31 00:19:54 by sam
[unix-history] / usr / src / lib / libcurses / setterm.c
CommitLineData
658815ab
KA
1/*
2 * Terminal initialization routines.
3 *
623c4d86 4 * %G% (Berkeley) @(#)setterm.c 1.13
658815ab
KA
5 */
6
658815ab 7# include "curses.ext"
658815ab
KA
8
9static bool *sflags[] = {
10 &AM, &BS, &EO, &HZ, &IN, &MI, &MS, &NC, &OS, &UL, &XN
11 };
12
13static char *xPC,
14 **sstrs[] = {
00334184
KA
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
658815ab 20 },
0d0dfbce 21 *tgoto();
658815ab 22
ed023db2 23static char tspace[256], /* Space for capability strings */
658815ab
KA
24 *aoftspace; /* Address of tspace for relocation */
25
26static 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 */
33short ospeed = -1;
34
35gettmode() {
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);
d581dac5 47 _tty.sg_flags &= ~XTABS;
8ec7ff8b 48 stty(_tty_ch, &_tty);
658815ab
KA
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
57setterm(type)
58reg char *type; {
59
1fe7814b
KA
60 reg int unknown;
61 static char genbuf[1024];
658815ab
KA
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;
658815ab
KA
81
82 if (COLS == 0)
83 COLS = tgetnum("co");
84 if (COLS <= 4)
85 COLS = 80;
623c4d86 86
658815ab
KA
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/*
0c1e0ce7 104 * This routine gets all the terminal flags from the termcap database
658815ab
KA
105 */
106zap() {
107
108 reg bool **fp;
109 reg char *namp, ***sp;
0c1e0ce7 110 reg int SG, UG;
658815ab
KA
111 extern char *tgetstr();
112
113 /*
114 * get boolean flags
115 */
116 namp = "ambseohzinmimsncosulxn\0\0";
117# ifdef FULLDEBUG
118 fprintf(outf, "ZAP: namp = \"%s\"\n", namp);
119# endif
120 fp = sflags;
121 do {
122 *(*fp++) = tgetflag(namp);
123# ifdef FULLDEBUG
124 fprintf(outf, "ZAP: %.2s = %d", namp, *(*(fp - 1)));
125# endif
126 namp += 2;
127 } while (*namp);
128
129 /*
130 * get string values
131 */
00334184 132 namp = "albcbtcdceclcmcrdcdldmdoedeihoicimipllmandnlpcsesfsosrtatetiucueupusvbvsve";
658815ab
KA
133# ifdef FULLDEBUG
134 fprintf(outf, "ZAP: namp = \"%s\"\n", namp);
135# endif
136 sp = sstrs;
137 do {
138 *(*sp++) = tgetstr(namp, &aoftspace);
139# ifdef FULLDEBUG
140 fprintf(outf, "ZAP: %.2s = \"%s\"\n", namp, *(*(sp-1)));
141# endif
142 namp += 2;
143 } while (*namp);
0c1e0ce7
KA
144 SG = tgetnum("sg");
145 UG = tgetnum("ug");
2119a323 146 if ((SG > 0 || !SO) && (UG <= 0 && US)) {
658815ab
KA
147 SO = US;
148 SE = UE;
149 }
150}
f81f846b
KA
151
152/*
153 * return a capability from termcap
154 */
155char *
156getcap(name)
157char *name;
158{
0c1e0ce7 159 char *tgetstr();
6cc9d15d 160
0c1e0ce7 161 return tgetstr(name, &aoftspace);
f81f846b 162}