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