Subversion Repositories planix.SVN

Rev

Rev 118 | 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
{
118 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
};
118 7u83 61
 
105 7u83 62
setterm(type)
63
	char *type;
64
{
65
	char *tgoto();
66
	register int unknown;
118 7u83 67
	char ltcbuf[TCBUFSIZE+4096];
105 7u83 68
 
69
	if (type[0] == 0)
70
		type = "xx";
71
	unknown = 0;
72
	putpad(TE);
116 7u83 73
//#ifdef	NCURSES
105 7u83 74
	snprintf(ltcbuf, TCBUFSIZE, "%s:", type);
116 7u83 75
//#endif
105 7u83 76
	if (tgetent(ltcbuf, type) != 1) {
77
		unknown++;
115 7u83 78
		CP(ltcbuf, "xx|dumb:");
105 7u83 79
	}
118 7u83 80
 
105 7u83 81
	setsize();
82
	aoftspace = tspace;
83
	zap();
84
	/*
85
	 * Initialize keypad arrow keys.
86
	 */
87
	arrows[0].cap = KU; arrows[0].mapto = "k"; arrows[0].descr = "up";
88
	arrows[1].cap = KD; arrows[1].mapto = "j"; arrows[1].descr = "down";
89
	arrows[2].cap = KL; arrows[2].mapto = "h"; arrows[2].descr = "left";
90
	arrows[3].cap = KR; arrows[3].mapto = "l"; arrows[3].descr = "right";
91
	arrows[4].cap = KH; arrows[4].mapto = "H"; arrows[4].descr = "home";
92
 
93
	/*
94
	 * Handle funny termcap capabilities
95
	 */
96
	if (CS && SC && RC) {
97
		if (AL==NULL) AL="";
98
		if (DL==NULL) DL="";
99
	}
100
	if (AL_PARM && AL==NULL) AL="";
101
	if (DL_PARM && DL==NULL) DL="";
102
	if (IC && IM==NULL) IM="";
103
	if (IC && EI==NULL) EI="";
104
	if (!GT) BT=NULL;	/* If we can't tab, we can't backtab either */
105
 
106
#ifdef TIOCLGET
107
	/*
108
	 * Now map users susp char to ^Z, being careful that the susp
109
	 * overrides any arrow key, but only for hackers (=new tty driver).
110
	 */
111
	{
112
		static char sc[2];
113
		int i, fnd;
114
 
115
		ioctl(0, TIOCGETD, &ldisc);
116
		if (ldisc == NTTYDISC) {
117
			sc[0] = olttyc.t_suspc;
118
			sc[1] = 0;
119
			if (olttyc.t_suspc == CTRL('z')) {
120
				for (i=0; i<=4; i++)
121
					if (arrows[i].cap &&
122
					    arrows[i].cap[0] == CTRL('z'))
123
						addmac(sc, NULL, NULL, arrows);
124
			} else
125
				addmac(sc, "\32", "susp", arrows);
126
		}
127
	}
128
#endif
129
 
130
	if (CM != 0) {
131
		if (tgoto(CM, 2, 2)[0] == 'O')	/* OOPS */
132
			CA = 0, CM = 0;
133
		else
134
			CA = 1, costCM = cost(tgoto(CM, 8, 10));
135
	} else {
136
		CA = 0, CM = 0;
137
	}
138
	costSR = cost(SR);
118 7u83 139
 
105 7u83 140
	costAL = cost(AL);
141
	costDP = cost(tgoto(DOWN_PARM, 10, 10));
142
	costLP = cost(tgoto(LEFT_PARM, 10, 10));
143
	costRP = cost(tgoto(RIGHT_PARM, 10, 10));
144
	PC = xPC ? xPC[0] : 0;
145
	aoftspace = tspace;
116 7u83 146
	CP(ex_ttytype, longname(ltcbuf, type));
105 7u83 147
	/* proper strings to change tty type */
148
	termreset();
149
	gettmode();
150
	value(REDRAW) = AL && DL;
151
	value(OPTIMIZE) = !CA && !GT;
152
	if (o_speed == B1200 && !value(REDRAW))
153
		value(SLOWOPEN) = 1;	/* see also gettmode above */
154
	if (unknown)
155
		serror("%s: Unknown terminal type", type);
156
}
157
 
158
setsize()
159
{
119 7u83 160
	int i,l;
161
 
162
	get_size(&COLUMNS,&LINES);
163
 
164
	i = LINES;
165
	printf ("We have before %d x %d tube: %d\n",COLUMNS,LINES,TUBELINES);
166
 
167
/*	register int l, i;
105 7u83 168
	struct winsize win;
169
 
170
	if (ioctl(0, TIOCGWINSZ, &win) < 0) {
171
		i = LINES = tgetnum("li");
172
		COLUMNS = tgetnum("co");
173
	} else {
174
		if ((LINES = winsz.ws_row = win.ws_row) == 0)
175
			LINES = tgetnum("li");
176
		i = LINES;
177
		if ((COLUMNS = winsz.ws_col = win.ws_col) == 0)
178
			COLUMNS = tgetnum("co");
179
	}
119 7u83 180
*/
181
 
182
 
183
 
105 7u83 184
	if (LINES <= 5)
185
		LINES = 24;
186
	if (LINES > TUBELINES)
187
		LINES = TUBELINES;
188
	l = LINES;
189
	if (o_speed < B1200)
107 7u83 190
		l = 9;	// including the message line at the bottom 
105 7u83 191
	else if (o_speed < B2400)
192
		l = 17;
193
	if (l > LINES)
194
		l = LINES;
195
	if (COLUMNS <= 4)
196
		COLUMNS = 1000;
197
	options[WINDOW].ovalue = options[WINDOW].odefault = l - 1;
198
	if (defwind) options[WINDOW].ovalue = defwind;
199
	options[SCROLL].ovalue = options[SCROLL].odefault = HC ? 11 : ((l-1) / 2);
200
	if (i <= 0)
201
		LINES = 2;
119 7u83 202
 
203
 
204
	printf ("We have now %d x %d\n",COLUMNS,LINES);
105 7u83 205
}
206
 
207
zap()
208
{
209
	register char *namp;
210
	register bool **fp;
211
	register char ***sp;
212
	int flag;
213
	char *string;
214
 
215
	namp = "ambsdadbeohcinmincnsosulxbxnxtxx";
216
	fp = sflags;
217
	do {
218
		flag = tgetflag(namp);
219
		*(*fp++) = flag;
220
		namp += 2;
221
	} while (*namp);
222
	namp = "albcbtcdceclcmcrcsdcdldmdoedeik0k1k2k3k4k5k6k7k8k9hoicimipkdkekhklkrkskullndnlpcrcscsesfsosrtatetiupvbvsveALDLUPDOLERI";
223
	sp = sstrs;
224
	do {
225
		string = tgetstr(namp, &aoftspace);
118 7u83 226
 
105 7u83 227
		*(*sp++) = string;
228
		namp += 2;
229
	} while (*namp);
230
}
231
 
232
char *
233
longname(bp, def)
234
	register char *bp;
235
	char *def;
236
{
237
	register char *cp;
238
 
239
	while (*bp && *bp != ':' && *bp != '|')
240
		bp++;
241
	if (*bp == '|') {
242
		bp++;
243
		cp = bp;
244
		while (*cp && *cp != ':' && *cp != '|')
245
			cp++;
246
		*cp = 0;
247
		return (bp);
248
	}
249
	return (def);
250
}
251
 
252
char *
253
fkey(i)
254
	int i;
255
{
256
	if (0 <= i && i <= 9)
257
		return(*fkeys[i]);
258
	else
259
		return(NOSTR);
260
}
261
 
262
/*
263
 * cost figures out how much (in characters) it costs to send the string
264
 * str to the terminal.  It takes into account padding information, as
265
 * much as it can, for a typical case.  (Right now the typical case assumes
266
 * the number of lines affected is the size of the screen, since this is
267
 * mainly used to decide if AL or SR is better, and this always happens
268
 * at the top of the screen.  We assume cursor motion (CM) has little
269
 * padding, if any, required, so that case, which is really more important
270
 * than AL vs SR, won't be really affected.)
271
 */
272
static int costnum;
273
cost(str)
274
char *str;
275
{
276
	int countnum();
277
 
118 7u83 278
 
279
 
280
	if (str == NULL || *str=='O'){	/* OOPS */
105 7u83 281
		return 10000;	/* infinity */
118 7u83 282
	}
105 7u83 283
	costnum = 0;
284
	tputs(str, LINES, countnum);
285
	return costnum;
286
}
287
 
288
/* ARGSUSED */
289
countnum(ch)
290
char ch;
291
{
292
	costnum++;
293
}