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