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