|
|
1.1 root 1: /*
2: * Copyright (c) 1980 Regents of the University of California.
3: * All rights reserved. The Berkeley software License Agreement
4: * specifies the terms and conditions for redistribution.
5: */
6:
7: #ifndef lint
8: static char sccsid[] = "@(#)cr_tty.c 5.2 (Berkeley) 11/8/85";
9: #endif not lint
10:
11: /*
12: * Terminal initialization routines.
13: *
14: */
15:
16: # include "curses.ext"
17:
18: static bool *sflags[] = {
19: &AM, &BS, &DA, &DB, &EO, &HC, &HZ, &IN, &MI,
20: &MS, &NC, &NS, &OS, &UL, &XB, &XN, &XT, &XS,
21: &XX
22: };
23:
24: static char *_PC,
25: **sstrs[] = {
26: &AL, &BC, &BT, &CD, &CE, &CL, &CM, &CR, &CS,
27: &DC, &DL, &DM, &DO, &ED, &EI, &K0, &K1, &K2,
28: &K3, &K4, &K5, &K6, &K7, &K8, &K9, &HO, &IC,
29: &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU,
30: &LL, &MA, &ND, &NL, &_PC, &RC, &SC, &SE, &SF,
31: &SO, &SR, &TA, &TE, &TI, &UC, &UE, &UP, &US,
32: &VB, &VS, &VE, &AL_PARM, &DL_PARM, &UP_PARM,
33: &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM,
34: },
35: *tgoto();
36:
37: char _tspace[2048]; /* Space for capability strings */
38:
39: static char *aoftspace; /* Address of _tspace for relocation */
40:
41: static int destcol, destline;
42:
43: /*
44: * This routine does terminal type initialization routines, and
45: * calculation of flags at entry. It is almost entirely stolen from
46: * Bill Joy's ex version 2.6.
47: */
48: short ospeed = -1;
49:
50: gettmode() {
51:
52: if (gtty(_tty_ch, &_tty) < 0)
53: return;
54: savetty();
55: if (stty(_tty_ch, &_tty) < 0)
56: _tty.sg_flags = _res_flg;
57: ospeed = _tty.sg_ospeed;
58: _res_flg = _tty.sg_flags;
59: UPPERCASE = (_tty.sg_flags & LCASE) != 0;
60: GT = ((_tty.sg_flags & XTABS) == 0);
61: NONL = ((_tty.sg_flags & CRMOD) == 0);
62: _tty.sg_flags &= ~XTABS;
63: stty(_tty_ch, &_tty);
64: # ifdef DEBUG
65: fprintf(outf, "GETTMODE: UPPERCASE = %s\n", UPPERCASE ? "TRUE":"FALSE");
66: fprintf(outf, "GETTMODE: GT = %s\n", GT ? "TRUE" : "FALSE");
67: fprintf(outf, "GETTMODE: NONL = %s\n", NONL ? "TRUE" : "FALSE");
68: fprintf(outf, "GETTMODE: ospeed = %d\n", ospeed);
69: # endif
70: }
71:
72: setterm(type)
73: reg char *type; {
74:
75: reg int unknown;
76: static char genbuf[1024];
77: # ifdef TIOCGWINSZ
78: struct winsize win;
79: # endif
80:
81: # ifdef DEBUG
82: fprintf(outf, "SETTERM(\"%s\")\n", type);
83: fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
84: # endif
85: if (type[0] == '\0')
86: type = "xx";
87: unknown = FALSE;
88: if (tgetent(genbuf, type) != 1) {
89: unknown++;
90: strcpy(genbuf, "xx|dumb:");
91: }
92: # ifdef DEBUG
93: fprintf(outf, "SETTERM: tty = %s\n", type);
94: # endif
95: # ifdef TIOCGWINSZ
96: if (ioctl(_tty_ch, TIOCGWINSZ, &win) >= 0) {
97: if (LINES == 0)
98: LINES = win.ws_row;
99: if (COLS == 0)
100: COLS = win.ws_col;
101: }
102: # endif
103:
104: if (LINES == 0)
105: LINES = tgetnum("li");
106: if (LINES <= 5)
107: LINES = 24;
108:
109: if (COLS == 0)
110: COLS = tgetnum("co");
111: if (COLS <= 4)
112: COLS = 80;
113:
114: # ifdef DEBUG
115: fprintf(outf, "SETTERM: LINES = %d, COLS = %d\n", LINES, COLS);
116: # endif
117: aoftspace = _tspace;
118: zap(); /* get terminal description */
119:
120: /*
121: * Handle funny termcap capabilities
122: */
123: if (CS && SC && RC) AL=DL="";
124: if (AL_PARM && AL==NULL) AL="";
125: if (DL_PARM && DL==NULL) DL="";
126: if (IC && IM==NULL) IM="";
127: if (IC && EI==NULL) EI="";
128: if (!GT) BT=NULL; /* If we can't tab, we can't backtab either */
129:
130: if (tgoto(CM, destcol, destline)[0] == 'O')
131: CA = FALSE, CM = 0;
132: else
133: CA = TRUE;
134:
135: PC = _PC ? _PC[0] : FALSE;
136: aoftspace = _tspace;
137: strncpy(ttytype, longname(genbuf, type), sizeof(ttytype) - 1);
138: ttytype[sizeof(ttytype) - 1] = '\0';
139: if (unknown)
140: return ERR;
141: return OK;
142: }
143:
144: /*
145: * This routine gets all the terminal flags from the termcap database
146: */
147:
148: zap()
149: {
150: register char *namp;
151: register bool **fp;
152: register char ***sp;
153: #ifdef DEBUG
154: register char *cp;
155: #endif
156: extern char *tgetstr();
157:
158: namp = "ambsdadbeohchzinmimsncnsosulxbxnxtxsxx";
159: fp = sflags;
160: do {
161: *(*fp++) = tgetflag(namp);
162: #ifdef DEBUG
163: fprintf(outf, "%2.2s = %s\n", namp, *fp[-1] ? "TRUE" : "FALSE");
164: #endif
165: namp += 2;
166: } while (*namp);
167: namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullmandnlpcrcscsesfsosrtatetiucueupusvbvsveALDLUPDOLERI";
168: sp = sstrs;
169: do {
170: *(*sp++) = tgetstr(namp, &aoftspace);
171: #ifdef DEBUG
172: fprintf(outf, "%2.2s = %s", namp, *sp[-1] == NULL ? "NULL\n" : "\"");
173: if (*sp[-1] != NULL) {
174: for (cp = *sp[-1]; *cp; cp++)
175: fprintf(outf, "%s", unctrl(*cp));
176: fprintf(outf, "\"\n");
177: }
178: #endif
179: namp += 2;
180: } while (*namp);
181: if (XS)
182: SO = SE = NULL;
183: else {
184: if (tgetnum("sg") > 0)
185: SO = NULL;
186: if (tgetnum("ug") > 0)
187: US = NULL;
188: if (!SO && US) {
189: SO = US;
190: SE = UE;
191: }
192: }
193: }
194:
195: /*
196: * return a capability from termcap
197: */
198: char *
199: getcap(name)
200: char *name;
201: {
202: char *tgetstr();
203:
204: return tgetstr(name, &aoftspace);
205: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.