adopt latest version of fgoto() and plod() from vi
[unix-history] / usr / src / lib / libcurses / setterm.c
CommitLineData
658815ab
KA
1/*
2 * Terminal initialization routines.
3 *
00334184 4 * %G% (Berkeley) @(#)setterm.c 1.10
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
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/*
0c1e0ce7 107 * This routine gets all the terminal flags from the termcap database
658815ab
KA
108 */
109zap() {
110
111 reg bool **fp;
112 reg char *namp, ***sp;
0c1e0ce7 113 reg int SG, UG;
658815ab
KA
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 */
00334184 135 namp = "albcbtcdceclcmcrdcdldmdoedeihoicimipllmandnlpcsesfsosrtatetiucueupusvbvsve";
658815ab
KA
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);
0c1e0ce7
KA
147 SG = tgetnum("sg");
148 UG = tgetnum("ug");
149 if ((SG || !SO) && (!UG && US)) {
658815ab
KA
150 SO = US;
151 SE = UE;
152 }
153}
f81f846b
KA
154
155/*
156 * return a capability from termcap
157 */
158char *
159getcap(name)
160char *name;
161{
0c1e0ce7 162 char *tgetstr();
6cc9d15d 163
0c1e0ce7 164 return tgetstr(name, &aoftspace);
f81f846b 165}