10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
23*802Scf46844 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate
310Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
340Sstevel@tonic-gate
350Sstevel@tonic-gate #include "ex.h"
360Sstevel@tonic-gate #include "ex_tty.h"
370Sstevel@tonic-gate
380Sstevel@tonic-gate static unsigned char allocspace[256];
390Sstevel@tonic-gate static unsigned char *freespace;
400Sstevel@tonic-gate
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate * Terminal type initialization routines,
430Sstevel@tonic-gate * and calculation of flags at entry or after
440Sstevel@tonic-gate * a shell escape which may change them.
450Sstevel@tonic-gate */
460Sstevel@tonic-gate static short GT;
470Sstevel@tonic-gate
48*802Scf46844 void
gettmode(void)49*802Scf46844 gettmode(void)
500Sstevel@tonic-gate {
510Sstevel@tonic-gate short speed;
520Sstevel@tonic-gate
530Sstevel@tonic-gate GT = 1;
540Sstevel@tonic-gate if(gTTY(2) == -1)
550Sstevel@tonic-gate return;
560Sstevel@tonic-gate if (termiosflag)
570Sstevel@tonic-gate speed = (short)cfgetospeed(&tty);
580Sstevel@tonic-gate else
590Sstevel@tonic-gate speed = tty.c_cflag & CBAUD;
600Sstevel@tonic-gate if (ospeed != speed)
610Sstevel@tonic-gate value(vi_SLOWOPEN) = (int)(speed) < B1200;
620Sstevel@tonic-gate ospeed = speed;
630Sstevel@tonic-gate normf = tty;
640Sstevel@tonic-gate UPPERCASE = (tty.c_iflag & IUCLC) != 0;
650Sstevel@tonic-gate if ((tty.c_oflag & TABDLY) == TAB3 || teleray_glitch)
660Sstevel@tonic-gate GT = 0;
670Sstevel@tonic-gate NONL = (tty.c_oflag & ONLCR) == 0;
680Sstevel@tonic-gate }
690Sstevel@tonic-gate
70*802Scf46844 void
setterm(unsigned char * type)71*802Scf46844 setterm(unsigned char *type)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate char *tparm();
740Sstevel@tonic-gate unsigned char *chrptr;
75*802Scf46844 int unknown, i;
76*802Scf46844 int l;
770Sstevel@tonic-gate int errret;
780Sstevel@tonic-gate extern unsigned char termtype[];
790Sstevel@tonic-gate extern void setsize();
800Sstevel@tonic-gate
810Sstevel@tonic-gate unknown = 0;
820Sstevel@tonic-gate if (cur_term && exit_ca_mode)
83*802Scf46844 putpad((unsigned char *)exit_ca_mode);
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate * The code in this if statement is from 4.1.2 and fixes a bug where
860Sstevel@tonic-gate * you couldn't change the term type using the ":set term" command.
870Sstevel@tonic-gate */
880Sstevel@tonic-gate if (*termtype) {
890Sstevel@tonic-gate #ifdef TRACE
900Sstevel@tonic-gate if (trace) fprintf(trace, "CALLED del_curterm with %s\n", termtype);
910Sstevel@tonic-gate #endif
920Sstevel@tonic-gate del_curterm(cur_term); /* Zap the old data space which was allocated
930Sstevel@tonic-gate * previously (this is a set command)
940Sstevel@tonic-gate */
950Sstevel@tonic-gate }
960Sstevel@tonic-gate cur_term = 0;
970Sstevel@tonic-gate strcpy(termtype, type);
980Sstevel@tonic-gate
990Sstevel@tonic-gate #ifdef XPG4
1000Sstevel@tonic-gate use_env(1); /* $LINES and $COLUMNS override terminfo */
1010Sstevel@tonic-gate #endif /* XPG4 */
1020Sstevel@tonic-gate
1030Sstevel@tonic-gate setupterm(type, 2, &errret);
1040Sstevel@tonic-gate if (errret != 1) {
1050Sstevel@tonic-gate unknown++;
1060Sstevel@tonic-gate cur_term = 0;
1070Sstevel@tonic-gate if (errret == 0) {
1080Sstevel@tonic-gate setupterm("unknown", 1, &errret);
1090Sstevel@tonic-gate if (errret == 0) {
1100Sstevel@tonic-gate perror(gettext("Unable to setup term:'unknown' missing in the terminfo database"));
1110Sstevel@tonic-gate exit(++errcnt);
1120Sstevel@tonic-gate }
1130Sstevel@tonic-gate }
1140Sstevel@tonic-gate else if (errret == -1) {
1150Sstevel@tonic-gate perror(gettext("Unable to find the terminfo database"));
1160Sstevel@tonic-gate exit(++errcnt);
1170Sstevel@tonic-gate }
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate if (errret == 1)
1200Sstevel@tonic-gate resetterm();
1210Sstevel@tonic-gate #ifdef TRACE
1220Sstevel@tonic-gate if (trace) fprintf(trace, "after setupterm, lines %d, columns %d, clear_screen '%s', cursor_address '%s'\n", lines, columns, clear_screen, cursor_address);
1230Sstevel@tonic-gate #endif
1240Sstevel@tonic-gate setsize();
1250Sstevel@tonic-gate #ifdef OLD
1260Sstevel@tonic-gate if(exit_attribute_mode)
1270Sstevel@tonic-gate putpad(exit_attribute_mode);
1280Sstevel@tonic-gate #endif
1290Sstevel@tonic-gate exit_bold = (exit_standout_mode ? exit_standout_mode : (exit_attribute_mode ? exit_attribute_mode : 0));
1300Sstevel@tonic-gate i = lines;
1310Sstevel@tonic-gate if (lines <= 1)
1320Sstevel@tonic-gate lines = 24;
1330Sstevel@tonic-gate if (lines > TUBELINES)
1340Sstevel@tonic-gate lines = TUBELINES;
1350Sstevel@tonic-gate l = lines;
1360Sstevel@tonic-gate if (ospeed < B1200)
1370Sstevel@tonic-gate l = 9; /* including the message line at the bottom */
1380Sstevel@tonic-gate else if (ospeed < B2400)
1390Sstevel@tonic-gate l = 17;
1400Sstevel@tonic-gate if (l > lines)
1410Sstevel@tonic-gate l = lines;
1420Sstevel@tonic-gate /*
1430Sstevel@tonic-gate * Initialize keypad arrow keys.
1440Sstevel@tonic-gate */
1450Sstevel@tonic-gate freespace = allocspace;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate #ifdef sun
148*802Scf46844 kpadd(arrows, (unsigned char *)key_ic, (unsigned char *)"i",
149*802Scf46844 (unsigned char *)"inschar");
150*802Scf46844 kpadd(arrows, (unsigned char *)key_eic, (unsigned char *)"i",
151*802Scf46844 (unsigned char *)"inschar");
152*802Scf46844 kpadd(arrows, (unsigned char *)key_up, (unsigned char *)"k",
153*802Scf46844 (unsigned char *)"up");
154*802Scf46844 kpadd(arrows, (unsigned char *)key_down, (unsigned char *)"j",
155*802Scf46844 (unsigned char *)"down");
156*802Scf46844 kpadd(arrows, (unsigned char *)key_left, (unsigned char *)"h",
157*802Scf46844 (unsigned char *)"left");
158*802Scf46844 kpadd(arrows, (unsigned char *)key_right, (unsigned char *)"l",
159*802Scf46844 (unsigned char *)"right");
160*802Scf46844 kpadd(arrows, (unsigned char *)key_home, (unsigned char *)"H",
161*802Scf46844 (unsigned char *)"home");
1620Sstevel@tonic-gate #else
1630Sstevel@tonic-gate kpadd(arrows, key_ic, "i", "inschar");
1640Sstevel@tonic-gate kpadd(immacs, key_ic, "\033", "inschar");
1650Sstevel@tonic-gate kpadd(arrows, key_eic, "i", "inschar");
1660Sstevel@tonic-gate kpadd(immacs, key_eic, "\033", "inschar");
1670Sstevel@tonic-gate
1680Sstevel@tonic-gate kpboth(arrows, immacs, key_up, "k", "up");
1690Sstevel@tonic-gate kpboth(arrows, immacs, key_down, "j", "down");
1700Sstevel@tonic-gate kpboth(arrows, immacs, key_left, "h", "left");
1710Sstevel@tonic-gate kpboth(arrows, immacs, key_right, "l", "right");
1720Sstevel@tonic-gate kpboth(arrows, immacs, key_home, "H", "home");
1730Sstevel@tonic-gate kpboth(arrows, immacs, key_il, "o\033", "insline");
1740Sstevel@tonic-gate kpboth(arrows, immacs, key_dl, "dd", "delline");
1750Sstevel@tonic-gate kpboth(arrows, immacs, key_clear, "\014", "clear");
1760Sstevel@tonic-gate kpboth(arrows, immacs, key_eol, "d$", "clreol");
1770Sstevel@tonic-gate kpboth(arrows, immacs, key_sf, "\005", "scrollf");
1780Sstevel@tonic-gate kpboth(arrows, immacs, key_dc, "x", "delchar");
1790Sstevel@tonic-gate kpboth(arrows, immacs, key_npage, "\006", "npage");
1800Sstevel@tonic-gate kpboth(arrows, immacs, key_ppage, "\002", "ppage");
1810Sstevel@tonic-gate kpboth(arrows, immacs, key_sr, "\031", "sr");
1820Sstevel@tonic-gate kpboth(arrows, immacs, key_eos, "dG", "clreos");
183*802Scf46844 #endif /* sun */
1840Sstevel@tonic-gate
1850Sstevel@tonic-gate /*
1860Sstevel@tonic-gate * Handle funny termcap capabilities
1870Sstevel@tonic-gate */
1880Sstevel@tonic-gate /* don't understand insert mode with multibyte characters */
1890Sstevel@tonic-gate if(MB_CUR_MAX > 1) {
1900Sstevel@tonic-gate enter_insert_mode = NULL;
1910Sstevel@tonic-gate exit_insert_mode = NULL;
1920Sstevel@tonic-gate #ifndef PRESUNEUC
1930Sstevel@tonic-gate insert_character = NULL;
1940Sstevel@tonic-gate #endif /* PRESUNEUC */
1950Sstevel@tonic-gate }
1960Sstevel@tonic-gate
1970Sstevel@tonic-gate if (change_scroll_region && save_cursor && restore_cursor) insert_line=delete_line="";
1980Sstevel@tonic-gate if (parm_insert_line && insert_line==NULL) insert_line="";
1990Sstevel@tonic-gate if (parm_delete_line && delete_line==NULL) delete_line="";
2000Sstevel@tonic-gate if (insert_character && enter_insert_mode==NULL) enter_insert_mode="";
2010Sstevel@tonic-gate if (insert_character && exit_insert_mode==NULL) exit_insert_mode="";
2020Sstevel@tonic-gate if (GT == 0)
2030Sstevel@tonic-gate tab = back_tab = NOSTR;
2040Sstevel@tonic-gate
2050Sstevel@tonic-gate #ifdef SIGTSTP
2060Sstevel@tonic-gate /*
2070Sstevel@tonic-gate * Now map users susp char to ^Z, being careful that the susp
2080Sstevel@tonic-gate * overrides any arrow key, but only for hackers (=new tty driver).
2090Sstevel@tonic-gate */
2100Sstevel@tonic-gate {
2110Sstevel@tonic-gate static unsigned char sc[2];
212*802Scf46844 int i;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate if (!value(vi_NOVICE)) {
2150Sstevel@tonic-gate sc[0] = tty.c_cc[VSUSP];
2160Sstevel@tonic-gate sc[1] = 0;
2170Sstevel@tonic-gate if (sc[0] == CTRL('z')) {
2180Sstevel@tonic-gate for (i=0; i<=4; i++)
2190Sstevel@tonic-gate if (arrows[i].cap && arrows[i].cap[0] == CTRL('z'))
2200Sstevel@tonic-gate addmac(sc, NULL, NULL, arrows);
2210Sstevel@tonic-gate } else if(sc[0])
2220Sstevel@tonic-gate addmac(sc, "\32", "susp", arrows);
2230Sstevel@tonic-gate }
2240Sstevel@tonic-gate }
2250Sstevel@tonic-gate #endif
2260Sstevel@tonic-gate
2270Sstevel@tonic-gate value(vi_WINDOW) = options[vi_WINDOW].odefault = l - 1;
2280Sstevel@tonic-gate if (defwind)
2290Sstevel@tonic-gate value(vi_WINDOW) = defwind;
2300Sstevel@tonic-gate value(vi_SCROLL) = options[vi_SCROLL].odefault =
2310Sstevel@tonic-gate hard_copy ? 11 : (value(vi_WINDOW) / 2);
2320Sstevel@tonic-gate if (columns <= 4)
2330Sstevel@tonic-gate columns = 1000;
2340Sstevel@tonic-gate chrptr=(unsigned char *)tparm(cursor_address, 2, 2);
2350Sstevel@tonic-gate if (chrptr==(unsigned char *)0 || chrptr[0] == 'O') /* OOPS */
2360Sstevel@tonic-gate cursor_address = 0;
2370Sstevel@tonic-gate else
2380Sstevel@tonic-gate costCM = cost(tparm(cursor_address, 10, 8));
2390Sstevel@tonic-gate costSR = cost(scroll_reverse);
2400Sstevel@tonic-gate costAL = cost(insert_line);
2410Sstevel@tonic-gate costDP = cost(tparm(parm_down_cursor, 10));
2420Sstevel@tonic-gate costLP = cost(tparm(parm_left_cursor, 10));
2430Sstevel@tonic-gate costRP = cost(tparm(parm_right_cursor, 10));
2440Sstevel@tonic-gate costCE = cost(clr_eol);
2450Sstevel@tonic-gate costCD = cost(clr_eos);
2460Sstevel@tonic-gate if (i <= 0)
2470Sstevel@tonic-gate lines = 2;
2480Sstevel@tonic-gate /* proper strings to change tty type */
2490Sstevel@tonic-gate termreset();
2500Sstevel@tonic-gate gettmode();
2510Sstevel@tonic-gate value(vi_REDRAW) = insert_line && delete_line;
2520Sstevel@tonic-gate value(vi_OPTIMIZE) = !cursor_address && !tab;
2530Sstevel@tonic-gate if (ospeed == B1200 && !value(vi_REDRAW))
2540Sstevel@tonic-gate value(vi_SLOWOPEN) = 1; /* see also gettmode above */
2550Sstevel@tonic-gate if (unknown)
256*802Scf46844 serror((unsigned char *)gettext("%s: Unknown terminal type"),
257*802Scf46844 type);
2580Sstevel@tonic-gate }
2590Sstevel@tonic-gate
2600Sstevel@tonic-gate #ifndef sun
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * Map both map1 and map2 as below. map2 surrounded by esc and
2630Sstevel@tonic-gate * the 'i', 'R', or 'a' mode. However, because we don't know
2640Sstevel@tonic-gate * the mode here we put in the escape and when the map() routine
2650Sstevel@tonic-gate * is called for immacs mapping the mode is appended to the
2660Sstevel@tonic-gate * macro. Therefore when you leave insert mode, to perform a
2670Sstevel@tonic-gate * function key, it will (once the cursor movement is done)
2680Sstevel@tonic-gate * restore you to the proper mode.
2690Sstevel@tonic-gate */
2700Sstevel@tonic-gate kpboth(map1, map2, key, mapto, desc)
2710Sstevel@tonic-gate struct maps *map1, *map2;
2720Sstevel@tonic-gate unsigned char *key, *mapto, *desc;
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate unsigned char surmapto[30];
2750Sstevel@tonic-gate unsigned char *p;
2760Sstevel@tonic-gate
2770Sstevel@tonic-gate if (key == 0)
2780Sstevel@tonic-gate return;
2790Sstevel@tonic-gate kpadd(map1, key, mapto, desc);
2800Sstevel@tonic-gate if (any(*key, "\b\n "))
2810Sstevel@tonic-gate return;
2820Sstevel@tonic-gate strcpy(surmapto, "\33");
2830Sstevel@tonic-gate strcat(surmapto, mapto);
2840Sstevel@tonic-gate p = freespace;
2850Sstevel@tonic-gate strcpy(p, surmapto);
2860Sstevel@tonic-gate freespace += strlen(surmapto) + 1;
2870Sstevel@tonic-gate kpadd(map2, key, p, desc);
2880Sstevel@tonic-gate }
289*802Scf46844 #endif /* !sun */
2900Sstevel@tonic-gate
2910Sstevel@tonic-gate /*
2920Sstevel@tonic-gate * Define a macro. mapstr is the structure (mode) in which it applies.
2930Sstevel@tonic-gate * key is the input sequence, mapto what it turns into, and desc is a
2940Sstevel@tonic-gate * human-readable description of what's going on.
2950Sstevel@tonic-gate */
296*802Scf46844 void
kpadd(struct maps * mapstr,unsigned char * key,unsigned char * mapto,unsigned char * desc)297*802Scf46844 kpadd(struct maps *mapstr, unsigned char *key, unsigned char *mapto,
298*802Scf46844 unsigned char *desc)
2990Sstevel@tonic-gate {
3000Sstevel@tonic-gate int i;
3010Sstevel@tonic-gate
3020Sstevel@tonic-gate for (i=0; i<MAXNOMACS; i++)
3030Sstevel@tonic-gate if (mapstr[i].cap == 0)
3040Sstevel@tonic-gate break;
3050Sstevel@tonic-gate if (key == 0 || i >= MAXNOMACS)
3060Sstevel@tonic-gate return;
3070Sstevel@tonic-gate mapstr[i].cap = key;
3080Sstevel@tonic-gate mapstr[i].mapto = mapto;
3090Sstevel@tonic-gate mapstr[i].descr = desc;
3100Sstevel@tonic-gate }
3110Sstevel@tonic-gate
3120Sstevel@tonic-gate unsigned char *
fkey(i)3130Sstevel@tonic-gate fkey(i)
3140Sstevel@tonic-gate int i;
3150Sstevel@tonic-gate {
3160Sstevel@tonic-gate if (i < 0 || i > 9)
317*802Scf46844 return ((unsigned char *)NOSTR);
3180Sstevel@tonic-gate switch (i) {
319*802Scf46844 case 0: return ((unsigned char *)key_f0);
320*802Scf46844 case 1: return ((unsigned char *)key_f1);
321*802Scf46844 case 2: return ((unsigned char *)key_f2);
322*802Scf46844 case 3: return ((unsigned char *)key_f3);
323*802Scf46844 case 4: return ((unsigned char *)key_f4);
324*802Scf46844 case 5: return ((unsigned char *)key_f5);
325*802Scf46844 case 6: return ((unsigned char *)key_f6);
326*802Scf46844 case 7: return ((unsigned char *)key_f7);
327*802Scf46844 case 8: return ((unsigned char *)key_f8);
328*802Scf46844 case 9: return ((unsigned char *)key_f9);
329*802Scf46844 case 10: return ((unsigned char *)key_f0);
3300Sstevel@tonic-gate }
331*802Scf46844 return ((unsigned char *)NOSTR);
3320Sstevel@tonic-gate }
3330Sstevel@tonic-gate
3340Sstevel@tonic-gate /*
3350Sstevel@tonic-gate * cost figures out how much (in characters) it costs to send the string
3360Sstevel@tonic-gate * str to the terminal. It takes into account padding information, as
3370Sstevel@tonic-gate * much as it can, for a typical case. (Right now the typical case assumes
3380Sstevel@tonic-gate * the number of lines affected is the size of the screen, since this is
3390Sstevel@tonic-gate * mainly used to decide if insert_line or scroll_reverse is better, and this always happens
3400Sstevel@tonic-gate * at the top of the screen. We assume cursor motion (cursor_address) has little
3410Sstevel@tonic-gate * padding, if any, required, so that case, which is really more important
3420Sstevel@tonic-gate * than insert_line vs scroll_reverse, won't be really affected.)
3430Sstevel@tonic-gate */
3440Sstevel@tonic-gate
3450Sstevel@tonic-gate static int costnum;
3460Sstevel@tonic-gate
3470Sstevel@tonic-gate /* ARGSUSED */
3480Sstevel@tonic-gate int
countnum(char ch)3490Sstevel@tonic-gate countnum(char ch)
3500Sstevel@tonic-gate {
3510Sstevel@tonic-gate costnum++;
352*802Scf46844 return (0);
3530Sstevel@tonic-gate }
3540Sstevel@tonic-gate
355*802Scf46844 int
cost(unsigned char * str)356*802Scf46844 cost(unsigned char *str)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate
3590Sstevel@tonic-gate if (str == NULL || *str=='O') /* OOPS */
3600Sstevel@tonic-gate return 10000; /* infinity */
3610Sstevel@tonic-gate costnum = 0;
3620Sstevel@tonic-gate tputs((char *)str, lines, countnum);
3630Sstevel@tonic-gate return costnum;
3640Sstevel@tonic-gate }
365