Subversion Repositories planix.SVN

Rev

Rev 107 | Rev 115 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
105 7u83 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
#if	!defined(lint) && defined(DOSCCS)
8
static char *sccsid = "@(#)ex_tty.c	7.10.1 (2.11BSD GTE) 12/9/94";
9
#endif
10
 
11
#include "ex.h"
12
#include "ex_tty.h"
13
 
14
/*
15
 * Terminal type initialization routines,
16
 * and calculation of flags at entry or after
17
 * a shell escape which may change them.
18
 */
19
/* short	ospeed = -1;	mjm: def also in tputs.c of termcap.a  */
20
short	o_speed = -1;
21
 
22
gettmode()
23
{
107 7u83 24
/*a XXX
105 7u83 25
#ifndef USG3TTY
26
	if (gtty(1, &tty) < 0)
27
		return;
28
	if (o_speed != tty.sg_ospeed)
29
		value(SLOWOPEN) = tty.sg_ospeed < B1200;
30
	o_speed = tty.sg_ospeed;
31
	normf = tty.sg_flags;
32
	GT = (tty.sg_flags & XTABS) != XTABS && !XT;
33
	NONL = (tty.sg_flags & CRMOD) == 0;
34
#else
35
	if (ioctl(1, TCGETA, &tty) < 0)
36
		return;
107 7u83 37
	if (o_speed != (tty.c_cflag & CBAUD))	// mjm 
105 7u83 38
		value(SLOWOPEN) = (tty.c_cflag & CBAUD) < B1200;
39
	o_speed = tty.c_cflag & CBAUD;
40
	normf = tty;
41
	GT = (tty.c_oflag & TABDLY) != TAB3 && !XT;
42
	NONL = (tty.c_oflag & ONLCR) == 0;
107 7u83 43
#endif */
105 7u83 44
}
45
 
46
char *xPC;
47
char **sstrs[] = {
48
	&AL, &BC, &BT, &CD, &CE, &CL, &CM, &xCR, &CS, &DC, &DL, &DM, &DO,
49
	&ED, &EI, &F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9,
50
	&HO, &IC, &IM, &IP, &KD, &KE, &KH, &KL, &KR, &KS, &KU, &LL, &ND, &xNL,
51
	&xPC, &RC, &SC, &SE, &SF, &SO, &SR, &TA, &TE, &TI, &UP, &VB, &VS, &VE,
52
	&AL_PARM, &DL_PARM, &UP_PARM, &DOWN_PARM, &LEFT_PARM, &RIGHT_PARM
53
};
54
bool *sflags[] = {
55
	&AM, &BS, &DA, &DB, &EO, &HC, &IN, &MI, &NC, &NS, &OS, &UL,
56
	&XB, &XN, &XT, &XX
57
};
58
char **fkeys[10] = {
59
	&F0, &F1, &F2, &F3, &F4, &F5, &F6, &F7, &F8, &F9
60
};
61
setterm(type)
62
	char *type;
63
{
64
	char *tgoto();
65
	register int unknown;
66
	char ltcbuf[TCBUFSIZE];
67
 
68
	if (type[0] == 0)
69
		type = "xx";
70
	unknown = 0;
71
	putpad(TE);
72
#ifdef	NCURSES
73
	snprintf(ltcbuf, TCBUFSIZE, "%s:", type);
74
#endif
75
	if (tgetent(ltcbuf, type) != 1) {
76
		unknown++;
113 7u83 77
		strcpy(ltcbuf, "xx|dumb:");
105 7u83 78
	}
79
	setsize();
80
	aoftspace = tspace;
81
	zap();
82
	/*
83
	 * Initialize keypad arrow keys.
84
	 */
85
	arrows[0].cap = KU; arrows[0].mapto = "k"; arrows[0].descr = "up";
86
	arrows[1].cap = KD; arrows[1].mapto = "j"; arrows[1].descr = "down";
87
	arrows[2].cap = KL; arrows[2].mapto = "h"; arrows[2].descr = "left";
88
	arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right";
89
	arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home";
90
 
91
	/*
92
	 * Handle funny termcap capabilities
93
	 */
94
	if (CS && SC && RC) {
95
		if (AL==NULL) AL="";
96
		if (DL==NULL) DL="";
97
	}
98
	if (AL_PARM && AL==NULL) AL="";
99
	if (DL_PARM && DL==NULL) DL="";
100
	if (IC && IM==NULL) IM="";
101
	if (IC && EI==NULL) EI="";
102
	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
103
 
104
#ifdef TIOCLGET
105
	/*
106
	 * Now map users susp char to ^Z, being careful that the susp
107
	 * overrides any arrow key, but only for hackers (=new tty driver).
108
	 */
109
	{
110
		static char sc[2];
111
		int i, fnd;
112
 
113
		ioctl(0, TIOCGETD, &ldisc);
114
		if (ldisc == NTTYDISC) {
115
			sc[0] = olttyc.t_suspc;
116
			sc[1] = 0;
117
			if (olttyc.t_suspc == CTRL('z')) {
118
				for (i=0; i<=4; i++)
119
					if (arrows[i].cap &&
120
					    arrows[i].cap[0] == CTRL('z'))
121
						addmac(sc, NULL, NULL, arrows);
122
			} else
123
				addmac(sc, "\32", "susp", arrows);
124
		}
125
	}
126
#endif
127
 
128
	if (CM != 0) {
129
		if (tgoto(CM, 2, 2)[0] == 'O')	/* OOPS */
130
			CA = 0, CM = 0;
131
		else
132
			CA = 1, costCM = cost(tgoto(CM, 8, 10));
133
	} else {
134
		CA = 0, CM = 0;
135
	}
136
	costSR = cost(SR);
137
	costAL = cost(AL);
138
	costDP = cost(tgoto(DOWN_PARM, 10, 10));
139
	costLP = cost(tgoto(LEFT_PARM, 10, 10));
140
	costRP = cost(tgoto(RIGHT_PARM, 10, 10));
141
	PC = xPC ? xPC[0] : 0;
142
	aoftspace = tspace;
113 7u83 143
	strcpy(ttytype, longname(ltcbuf, type));
105 7u83 144
	/* proper strings to change tty type */
145
	termreset();
146
	gettmode();
147
	value(REDRAW) = AL && DL;
148
	value(OPTIMIZE) = !CA && !GT;
149
	if (o_speed == B1200 && !value(REDRAW))
150
		value(SLOWOPEN) = 1;	/* see also gettmode above */
151
	if (unknown)
152
		serror("%s: Unknown terminal type", type);
153
}
154
 
155
setsize()
156
{
107 7u83 157
 
158
/* XXX	register int l, i;
105 7u83 159
	struct winsize win;
160
 
161
	if (ioctl(0, TIOCGWINSZ, &win) < 0) {
162
		i = LINES = tgetnum("li");
163
		COLUMNS = tgetnum("co");
164
	} else {
165
		if ((LINES = winsz.ws_row = win.ws_row) == 0)
166
			LINES = tgetnum("li");
167
		i = LINES;
168
		if ((COLUMNS = winsz.ws_col = win.ws_col) == 0)
169
			COLUMNS = tgetnum("co");
170
	}
171
	if (LINES <= 5)
172
		LINES = 24;
173
	if (LINES > TUBELINES)
174
		LINES = TUBELINES;
175
	l = LINES;
176
	if (o_speed < B1200)
107 7u83 177
		l = 9;	// including the message line at the bottom 
105 7u83 178
	else if (o_speed < B2400)
179
		l = 17;
180
	if (l > LINES)
181
		l = LINES;
182
	if (COLUMNS <= 4)
183
		COLUMNS = 1000;
184
	options[WINDOW].ovalue = options[WINDOW].odefault = l - 1;
185
	if (defwind) options[WINDOW].ovalue = defwind;
186
	options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2);
187
	if (i <= 0)
188
		LINES = 2;
107 7u83 189
*/
105 7u83 190
}
191
 
192
zap()
193
{
194
	register char *namp;
195
	register bool **fp;
196
	register char ***sp;
197
	int flag;
198
	char *string;
199
 
200
	namp = "ambsdadbeohcinmincnsosulxbxnxtxx";
201
	fp = sflags;
202
	do {
203
		flag = tgetflag(namp);
204
		*(*fp++) = flag;
205
		namp += 2;
206
	} while (*namp);
207
	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI";
208
	sp = sstrs;
209
	do {
210
		string = tgetstr(namp, &aoftspace);
211
		*(*sp++) = string;
212
		namp += 2;
213
	} while (*namp);
214
}
215
 
216
char *
217
longname(bp, def)
218
	register char *bp;
219
	char *def;
220
{
221
	register char *cp;
222
 
223
	while (*bp && *bp != ':' && *bp != '|')
224
		bp++;
225
	if (*bp == '|') {
226
		bp++;
227
		cp = bp;
228
		while (*cp && *cp != ':' && *cp != '|')
229
			cp++;
230
		*cp = 0;
231
		return (bp);
232
	}
233
	return (def);
234
}
235
 
236
char *
237
fkey(i)
238
	int i;
239
{
240
	if (0 <= i && i <= 9)
241
		return(*fkeys[i]);
242
	else
243
		return(NOSTR);
244
}
245
 
246
/*
247
 * cost figures out how much (in characters) it costs to send the string
248
 * str to the terminal.  It takes into account padding information, as
249
 * much as it can, for a typical case.  (Right now the typical case assumes
250
 * the number of lines affected is the size of the screen, since this is
251
 * mainly used to decide if AL or SR is better, and this always happens
252
 * at the top of the screen.  We assume cursor motion (CM) has little
253
 * padding, if any, required, so that case, which is really more important
254
 * than AL vs SR, won't be really affected.)
255
 */
256
static int costnum;
257
cost(str)
258
char *str;
259
{
260
	int countnum();
261
 
262
	if (str == NULL || *str=='O')	/* OOPS */
263
		return 10000;	/* infinity */
264
	costnum = 0;
265
	tputs(str, LINES, countnum);
266
	return costnum;
267
}
268
 
269
/* ARGSUSED */
270
countnum(ch)
271
char ch;
272
{
273
	costnum++;
274
}