xref: /titanic_44/usr/src/cmd/vi/port/ex_tty.c (revision ee5416c9d7e449233197d5d20bc6b81e4ff091b2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 
31 /* Copyright (c) 1981 Regents of the University of California */
32 
33 #pragma ident	"%Z%%M%	%I%	%E% SMI"
34 
35 #include "ex.h"
36 #include "ex_tty.h"
37 
38 static unsigned char allocspace[256];
39 static unsigned char *freespace;
40 
41 /*
42  * Terminal type initialization routines,
43  * and calculation of flags at entry or after
44  * a shell escape which may change them.
45  */
46 static short GT;
47 
48 void
49 gettmode(void)
50 {
51 	short speed;
52 
53 	GT = 1;
54 	if(gTTY(2) == -1)
55 		return;
56 	if (termiosflag)
57 		speed = (short)cfgetospeed(&tty);
58 	else
59 		speed = tty.c_cflag & CBAUD;
60 	if (ospeed != speed)
61 		value(vi_SLOWOPEN) = (int)(speed) < B1200;
62 	ospeed = speed;
63 	normf = tty;
64 	UPPERCASE = (tty.c_iflag & IUCLC) != 0;
65 	if ((tty.c_oflag & TABDLY) == TAB3 || teleray_glitch)
66 		GT = 0;
67 	NONL = (tty.c_oflag & ONLCR) == 0;
68 }
69 
70 void
71 setterm(unsigned char *type)
72 {
73 	char *tparm();
74 	unsigned char *chrptr;
75 	int unknown, i;
76 	int l;
77 	int errret;
78 	extern unsigned char termtype[];
79 	extern void setsize();
80 
81 	unknown = 0;
82 	if (cur_term && exit_ca_mode)
83 		putpad((unsigned char *)exit_ca_mode);
84 	/*
85 	 * The code in this if statement is from 4.1.2 and fixes a bug where
86 	 * you couldn't change the term type using the ":set term" command.
87 	 */
88 	if (*termtype) {
89 #ifdef TRACE
90                 if (trace) fprintf(trace, "CALLED del_curterm with %s\n", termtype);
91 #endif
92                 del_curterm(cur_term); /* Zap the old data space which was allocated
93                                         * previously (this is a set command)
94                                         */
95         }
96 	cur_term = 0;
97 	strcpy(termtype, type);
98 
99 #ifdef XPG4
100 	use_env(1);		/* $LINES and $COLUMNS override terminfo */
101 #endif /* XPG4 */
102 
103 	setupterm(type, 2, &errret);
104 	if (errret != 1) {
105 		unknown++;
106 		cur_term = 0;
107 		if (errret == 0) {
108 			setupterm("unknown", 1, &errret);
109 			if (errret == 0) {
110 			  perror(gettext("Unable to setup term:'unknown' missing in the terminfo database"));
111 			  exit(++errcnt);
112 			}
113 		}
114 		else if (errret == -1) {
115 			perror(gettext("Unable to find the terminfo database"));
116 			exit(++errcnt);
117 		}
118 	}
119 	if (errret == 1)
120 		resetterm();
121 #ifdef TRACE
122 	if (trace) fprintf(trace, "after setupterm, lines %d, columns %d, clear_screen '%s', cursor_address '%s'\n", lines, columns, clear_screen, cursor_address);
123 #endif
124 	setsize();
125 #ifdef OLD
126 	if(exit_attribute_mode)
127 		putpad(exit_attribute_mode);
128 #endif
129 	exit_bold = (exit_standout_mode ? exit_standout_mode : (exit_attribute_mode ? exit_attribute_mode : 0));
130 	i = lines;
131 	if (lines <= 1)
132 		lines = 24;
133 	if (lines > TUBELINES)
134 		lines = TUBELINES;
135 	l = lines;
136 	if (ospeed < B1200)
137 		l = 9;	/* including the message line at the bottom */
138 	else if (ospeed < B2400)
139 		l = 17;
140 	if (l > lines)
141 		l = lines;
142 	/*
143 	 * Initialize keypad arrow keys.
144 	 */
145 	freespace = allocspace;
146 
147 #ifdef sun
148 	kpadd(arrows, (unsigned char *)key_ic, (unsigned char *)"i",
149 	    (unsigned char *)"inschar");
150 	kpadd(arrows, (unsigned char *)key_eic, (unsigned char *)"i",
151 	    (unsigned char *)"inschar");
152 	kpadd(arrows, (unsigned char *)key_up, (unsigned char *)"k",
153 	    (unsigned char *)"up");
154 	kpadd(arrows, (unsigned char *)key_down, (unsigned char *)"j",
155 	    (unsigned char *)"down");
156 	kpadd(arrows, (unsigned char *)key_left, (unsigned char *)"h",
157 	    (unsigned char *)"left");
158 	kpadd(arrows, (unsigned char *)key_right, (unsigned char *)"l",
159 	    (unsigned char *)"right");
160 	kpadd(arrows, (unsigned char *)key_home, (unsigned char *)"H",
161 	    (unsigned char *)"home");
162 #else
163 	kpadd(arrows, key_ic, "i", "inschar");
164 	kpadd(immacs, key_ic, "\033", "inschar");
165 	kpadd(arrows, key_eic, "i", "inschar");
166 	kpadd(immacs, key_eic, "\033", "inschar");
167 
168 	kpboth(arrows, immacs, key_up, "k", "up");
169 	kpboth(arrows, immacs, key_down, "j", "down");
170 	kpboth(arrows, immacs, key_left, "h", "left");
171 	kpboth(arrows, immacs, key_right, "l", "right");
172 	kpboth(arrows, immacs, key_home, "H", "home");
173 	kpboth(arrows, immacs, key_il, "o\033", "insline");
174 	kpboth(arrows, immacs, key_dl, "dd", "delline");
175 	kpboth(arrows, immacs, key_clear, "\014", "clear");
176 	kpboth(arrows, immacs, key_eol, "d$", "clreol");
177 	kpboth(arrows, immacs, key_sf, "\005", "scrollf");
178 	kpboth(arrows, immacs, key_dc, "x", "delchar");
179 	kpboth(arrows, immacs, key_npage, "\006", "npage");
180 	kpboth(arrows, immacs, key_ppage, "\002", "ppage");
181 	kpboth(arrows, immacs, key_sr, "\031", "sr");
182 	kpboth(arrows, immacs, key_eos, "dG", "clreos");
183 #endif /* sun */
184 
185 	/*
186 	 * Handle funny termcap capabilities
187 	 */
188 	/* don't understand insert mode with multibyte characters */
189 	if(MB_CUR_MAX > 1) {
190 		enter_insert_mode = NULL;
191 		exit_insert_mode = NULL;
192 #ifndef PRESUNEUC
193 		insert_character = NULL;
194 #endif /* PRESUNEUC */
195 	}
196 
197 	if (change_scroll_region && save_cursor && restore_cursor) insert_line=delete_line="";
198 	if (parm_insert_line && insert_line==NULL) insert_line="";
199 	if (parm_delete_line && delete_line==NULL) delete_line="";
200 	if (insert_character && enter_insert_mode==NULL) enter_insert_mode="";
201 	if (insert_character && exit_insert_mode==NULL) exit_insert_mode="";
202 	if (GT == 0)
203 		tab = back_tab = NOSTR;
204 
205 #ifdef SIGTSTP
206 	/*
207 	 * Now map users susp char to ^Z, being careful that the susp
208 	 * overrides any arrow key, but only for hackers (=new tty driver).
209 	 */
210 	{
211 		static unsigned char sc[2];
212 		int i;
213 
214 		if (!value(vi_NOVICE)) {
215 			sc[0] = tty.c_cc[VSUSP];
216 			sc[1] = 0;
217 			if (sc[0] == CTRL('z')) {
218 				for (i=0; i<=4; i++)
219 					if (arrows[i].cap && arrows[i].cap[0] == CTRL('z'))
220 						addmac(sc, NULL, NULL, arrows);
221 			} else if(sc[0])
222 				addmac(sc, "\32", "susp", arrows);
223 		}
224 	}
225 #endif
226 
227 	value(vi_WINDOW) = options[vi_WINDOW].odefault = l - 1;
228 	if (defwind)
229 		value(vi_WINDOW) = defwind;
230 	value(vi_SCROLL) = options[vi_SCROLL].odefault =
231 		hard_copy ? 11 : (value(vi_WINDOW) / 2);
232 	if (columns <= 4)
233 		columns = 1000;
234 	chrptr=(unsigned char *)tparm(cursor_address, 2, 2);
235 	if (chrptr==(unsigned char *)0 || chrptr[0] == 'O')	/* OOPS */
236 		cursor_address = 0;
237 	else
238 		costCM = cost(tparm(cursor_address, 10, 8));
239 	costSR = cost(scroll_reverse);
240 	costAL = cost(insert_line);
241 	costDP = cost(tparm(parm_down_cursor, 10));
242 	costLP = cost(tparm(parm_left_cursor, 10));
243 	costRP = cost(tparm(parm_right_cursor, 10));
244 	costCE = cost(clr_eol);
245 	costCD = cost(clr_eos);
246 	if (i <= 0)
247 		lines = 2;
248 	/* proper strings to change tty type */
249 	termreset();
250 	gettmode();
251 	value(vi_REDRAW) = insert_line && delete_line;
252 	value(vi_OPTIMIZE) = !cursor_address && !tab;
253 	if (ospeed == B1200 && !value(vi_REDRAW))
254 		value(vi_SLOWOPEN) = 1;	/* see also gettmode above */
255 	if (unknown)
256 		serror((unsigned char *)gettext("%s: Unknown terminal type"),
257 		    type);
258 }
259 
260 #ifndef sun
261 /*
262  * Map both map1 and map2 as below.  map2 surrounded by esc and
263  * the 'i', 'R', or 'a' mode.  However, because we don't know
264  * the mode here we put in the escape and when the map() routine
265  * is called for immacs mapping the mode is appended to the
266  * macro. Therefore when you leave insert mode, to perform a
267  * function key, it will (once the cursor movement is done)
268  * restore you to the proper mode.
269  */
270 kpboth(map1, map2, key, mapto, desc)
271 struct maps *map1, *map2;
272 unsigned char *key, *mapto, *desc;
273 {
274 	unsigned char surmapto[30];
275 	unsigned char *p;
276 
277 	if (key == 0)
278 		return;
279 	kpadd(map1, key, mapto, desc);
280 	if (any(*key, "\b\n "))
281 		return;
282 	strcpy(surmapto, "\33");
283 	strcat(surmapto, mapto);
284 	p = freespace;
285 	strcpy(p, surmapto);
286 	freespace += strlen(surmapto) + 1;
287 	kpadd(map2, key, p, desc);
288 }
289 #endif /* !sun */
290 
291 /*
292  * Define a macro.  mapstr is the structure (mode) in which it applies.
293  * key is the input sequence, mapto what it turns into, and desc is a
294  * human-readable description of what's going on.
295  */
296 void
297 kpadd(struct maps *mapstr, unsigned char *key, unsigned char *mapto,
298 unsigned char *desc)
299 {
300 	int i;
301 
302 	for (i=0; i<MAXNOMACS; i++)
303 		if (mapstr[i].cap == 0)
304 			break;
305 	if (key == 0 || i >= MAXNOMACS)
306 		return;
307 	mapstr[i].cap = key;
308 	mapstr[i].mapto = mapto;
309 	mapstr[i].descr = desc;
310 }
311 
312 unsigned char *
313 fkey(i)
314 	int i;
315 {
316 	if (i < 0 || i > 9)
317 		return ((unsigned char *)NOSTR);
318 	switch (i) {
319 	case 0: return ((unsigned char *)key_f0);
320 	case 1: return ((unsigned char *)key_f1);
321 	case 2: return ((unsigned char *)key_f2);
322 	case 3: return ((unsigned char *)key_f3);
323 	case 4: return ((unsigned char *)key_f4);
324 	case 5: return ((unsigned char *)key_f5);
325 	case 6: return ((unsigned char *)key_f6);
326 	case 7: return ((unsigned char *)key_f7);
327 	case 8: return ((unsigned char *)key_f8);
328 	case 9: return ((unsigned char *)key_f9);
329 	case 10: return ((unsigned char *)key_f0);
330 	}
331 	return ((unsigned char *)NOSTR);
332 }
333 
334 /*
335  * cost figures out how much (in characters) it costs to send the string
336  * str to the terminal.  It takes into account padding information, as
337  * much as it can, for a typical case.  (Right now the typical case assumes
338  * the number of lines affected is the size of the screen, since this is
339  * mainly used to decide if insert_line or scroll_reverse is better, and this always happens
340  * at the top of the screen.  We assume cursor motion (cursor_address) has little
341  * padding, if any, required, so that case, which is really more important
342  * than insert_line vs scroll_reverse, won't be really affected.)
343  */
344 
345 static int costnum;
346 
347 /* ARGSUSED */
348 int
349 countnum(char ch)
350 {
351 	costnum++;
352 	return (0);
353 }
354 
355 int
356 cost(unsigned char *str)
357 {
358 
359 	if (str == NULL || *str=='O')	/* OOPS */
360 		return 10000;	/* infinity */
361 	costnum = 0;
362 	tputs((char *)str, lines, countnum);
363 	return costnum;
364 }
365