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
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.
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]
23 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
31 /* Copyright (c) 1981 Regents of the University of California */
33 #pragma ident "%Z%%M% %I% %E% SMI"
38 static unsigned char allocspace
[256];
39 static unsigned char *freespace
;
42 * Terminal type initialization routines,
43 * and calculation of flags at entry or after
44 * a shell escape which may change them.
57 speed
= (short)cfgetospeed(&tty
);
59 speed
= tty
.c_cflag
& CBAUD
;
61 value(vi_SLOWOPEN
) = (int)(speed
) < B1200
;
64 UPPERCASE
= (tty
.c_iflag
& IUCLC
) != 0;
65 if ((tty
.c_oflag
& TABDLY
) == TAB3
|| teleray_glitch
)
67 NONL
= (tty
.c_oflag
& ONLCR
) == 0;
71 setterm(unsigned char *type
)
74 unsigned char *chrptr
;
78 extern unsigned char termtype
[];
79 extern void setsize();
82 if (cur_term
&& exit_ca_mode
)
83 putpad((unsigned char *)exit_ca_mode
);
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.
90 if (trace
) fprintf(trace
, "CALLED del_curterm with %s\n", termtype
);
92 del_curterm(cur_term
); /* Zap the old data space which was allocated
93 * previously (this is a set command)
97 strcpy(termtype
, type
);
100 use_env(1); /* $LINES and $COLUMNS override terminfo */
103 setupterm(type
, 2, &errret
);
108 setupterm("unknown", 1, &errret
);
110 perror(gettext("Unable to setup term:'unknown' missing in the terminfo database"));
114 else if (errret
== -1) {
115 perror(gettext("Unable to find the terminfo database"));
122 if (trace
) fprintf(trace
, "after setupterm, lines %d, columns %d, clear_screen '%s', cursor_address '%s'\n", lines
, columns
, clear_screen
, cursor_address
);
126 if(exit_attribute_mode
)
127 putpad(exit_attribute_mode
);
129 exit_bold
= (exit_standout_mode
? exit_standout_mode
: (exit_attribute_mode
? exit_attribute_mode
: 0));
133 if (lines
> TUBELINES
)
137 l
= 9; /* including the message line at the bottom */
138 else if (ospeed
< B2400
)
143 * Initialize keypad arrow keys.
145 freespace
= allocspace
;
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");
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");
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");
186 * Handle funny termcap capabilities
188 /* don't understand insert mode with multibyte characters */
190 enter_insert_mode
= NULL
;
191 exit_insert_mode
= NULL
;
193 insert_character
= NULL
;
194 #endif /* PRESUNEUC */
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
="";
203 tab
= back_tab
= NOSTR
;
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).
211 static unsigned char sc
[2];
214 if (!value(vi_NOVICE
)) {
215 sc
[0] = tty
.c_cc
[VSUSP
];
217 if (sc
[0] == CTRL('z')) {
219 if (arrows
[i
].cap
&& arrows
[i
].cap
[0] == CTRL('z'))
220 addmac(sc
, NULL
, NULL
, arrows
);
222 addmac(sc
, "\32", "susp", arrows
);
227 value(vi_WINDOW
) = options
[vi_WINDOW
].odefault
= l
- 1;
229 value(vi_WINDOW
) = defwind
;
230 value(vi_SCROLL
) = options
[vi_SCROLL
].odefault
=
231 hard_copy
? 11 : (value(vi_WINDOW
) / 2);
234 chrptr
=(unsigned char *)tparm(cursor_address
, 2, 2);
235 if (chrptr
==(unsigned char *)0 || chrptr
[0] == 'O') /* OOPS */
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
);
248 /* proper strings to change tty type */
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 */
256 serror((unsigned char *)gettext("%s: Unknown terminal type"),
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.
270 kpboth(map1
, map2
, key
, mapto
, desc
)
271 struct maps
*map1
, *map2
;
272 unsigned char *key
, *mapto
, *desc
;
274 unsigned char surmapto
[30];
279 kpadd(map1
, key
, mapto
, desc
);
280 if (any(*key
, "\b\n "))
282 strcpy(surmapto
, "\33");
283 strcat(surmapto
, mapto
);
286 freespace
+= strlen(surmapto
) + 1;
287 kpadd(map2
, key
, p
, desc
);
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.
297 kpadd(struct maps
*mapstr
, unsigned char *key
, unsigned char *mapto
,
302 for (i
=0; i
<MAXNOMACS
; i
++)
303 if (mapstr
[i
].cap
== 0)
305 if (key
== 0 || i
>= MAXNOMACS
)
308 mapstr
[i
].mapto
= mapto
;
309 mapstr
[i
].descr
= desc
;
317 return ((unsigned char *)NOSTR
);
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
);
331 return ((unsigned char *)NOSTR
);
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.)
356 cost(unsigned char *str
)
359 if (str
== NULL
|| *str
=='O') /* OOPS */
360 return 10000; /* infinity */
362 tputs((char *)str
, lines
, countnum
);