xref: /illumos-gate/usr/src/cmd/vi/port/ex_tty.c (revision 2a8bcb4e)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
23f6db9f27Scf  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate 
317c478bd9Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include "ex.h"
347c478bd9Sstevel@tonic-gate #include "ex_tty.h"
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate static unsigned char allocspace[256];
377c478bd9Sstevel@tonic-gate static unsigned char *freespace;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate /*
407c478bd9Sstevel@tonic-gate  * Terminal type initialization routines,
417c478bd9Sstevel@tonic-gate  * and calculation of flags at entry or after
427c478bd9Sstevel@tonic-gate  * a shell escape which may change them.
437c478bd9Sstevel@tonic-gate  */
447c478bd9Sstevel@tonic-gate static short GT;
457c478bd9Sstevel@tonic-gate 
46f6db9f27Scf void
gettmode(void)47f6db9f27Scf gettmode(void)
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	short speed;
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate 	GT = 1;
527c478bd9Sstevel@tonic-gate 	if(gTTY(2) == -1)
537c478bd9Sstevel@tonic-gate 		return;
547c478bd9Sstevel@tonic-gate 	if (termiosflag)
557c478bd9Sstevel@tonic-gate 		speed = (short)cfgetospeed(&tty);
567c478bd9Sstevel@tonic-gate 	else
577c478bd9Sstevel@tonic-gate 		speed = tty.c_cflag & CBAUD;
587c478bd9Sstevel@tonic-gate 	if (ospeed != speed)
597c478bd9Sstevel@tonic-gate 		value(vi_SLOWOPEN) = (int)(speed) < B1200;
607c478bd9Sstevel@tonic-gate 	ospeed = speed;
617c478bd9Sstevel@tonic-gate 	normf = tty;
627c478bd9Sstevel@tonic-gate 	UPPERCASE = (tty.c_iflag & IUCLC) != 0;
637c478bd9Sstevel@tonic-gate 	if ((tty.c_oflag & TABDLY) == TAB3 || teleray_glitch)
647c478bd9Sstevel@tonic-gate 		GT = 0;
657c478bd9Sstevel@tonic-gate 	NONL = (tty.c_oflag & ONLCR) == 0;
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate 
68f6db9f27Scf void
setterm(unsigned char * type)69f6db9f27Scf setterm(unsigned char *type)
707c478bd9Sstevel@tonic-gate {
71*2a8bcb4eSToomas Soome 	char *tparm();
727c478bd9Sstevel@tonic-gate 	unsigned char *chrptr;
73f6db9f27Scf 	int unknown, i;
74f6db9f27Scf 	int l;
757c478bd9Sstevel@tonic-gate 	int errret;
767c478bd9Sstevel@tonic-gate 	extern unsigned char termtype[];
777c478bd9Sstevel@tonic-gate 	extern void setsize();
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate 	unknown = 0;
807c478bd9Sstevel@tonic-gate 	if (cur_term && exit_ca_mode)
81f6db9f27Scf 		putpad((unsigned char *)exit_ca_mode);
827c478bd9Sstevel@tonic-gate 	/*
837c478bd9Sstevel@tonic-gate 	 * The code in this if statement is from 4.1.2 and fixes a bug where
847c478bd9Sstevel@tonic-gate 	 * you couldn't change the term type using the ":set term" command.
857c478bd9Sstevel@tonic-gate 	 */
867c478bd9Sstevel@tonic-gate 	if (*termtype) {
877c478bd9Sstevel@tonic-gate #ifdef TRACE
887c478bd9Sstevel@tonic-gate                 if (trace) fprintf(trace, "CALLED del_curterm with %s\n", termtype);
897c478bd9Sstevel@tonic-gate #endif
907c478bd9Sstevel@tonic-gate                 del_curterm(cur_term); /* Zap the old data space which was allocated
917c478bd9Sstevel@tonic-gate                                         * previously (this is a set command)
927c478bd9Sstevel@tonic-gate                                         */
937c478bd9Sstevel@tonic-gate         }
947c478bd9Sstevel@tonic-gate 	cur_term = 0;
957c478bd9Sstevel@tonic-gate 	strcpy(termtype, type);
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate #ifdef XPG4
987c478bd9Sstevel@tonic-gate 	use_env(1);		/* $LINES and $COLUMNS override terminfo */
997c478bd9Sstevel@tonic-gate #endif /* XPG4 */
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	setupterm(type, 2, &errret);
1027c478bd9Sstevel@tonic-gate 	if (errret != 1) {
1037c478bd9Sstevel@tonic-gate 		unknown++;
1047c478bd9Sstevel@tonic-gate 		cur_term = 0;
1057c478bd9Sstevel@tonic-gate 		if (errret == 0) {
1067c478bd9Sstevel@tonic-gate 			setupterm("unknown", 1, &errret);
1077c478bd9Sstevel@tonic-gate 			if (errret == 0) {
1087c478bd9Sstevel@tonic-gate 			  perror(gettext("Unable to setup term:'unknown' missing in the terminfo database"));
1097c478bd9Sstevel@tonic-gate 			  exit(++errcnt);
1107c478bd9Sstevel@tonic-gate 			}
1117c478bd9Sstevel@tonic-gate 		}
1127c478bd9Sstevel@tonic-gate 		else if (errret == -1) {
1137c478bd9Sstevel@tonic-gate 			perror(gettext("Unable to find the terminfo database"));
1147c478bd9Sstevel@tonic-gate 			exit(++errcnt);
1157c478bd9Sstevel@tonic-gate 		}
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	if (errret == 1)
1187c478bd9Sstevel@tonic-gate 		resetterm();
1197c478bd9Sstevel@tonic-gate #ifdef TRACE
1207c478bd9Sstevel@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);
1217c478bd9Sstevel@tonic-gate #endif
1227c478bd9Sstevel@tonic-gate 	setsize();
1237c478bd9Sstevel@tonic-gate #ifdef OLD
1247c478bd9Sstevel@tonic-gate 	if(exit_attribute_mode)
1257c478bd9Sstevel@tonic-gate 		putpad(exit_attribute_mode);
1267c478bd9Sstevel@tonic-gate #endif
1277c478bd9Sstevel@tonic-gate 	exit_bold = (exit_standout_mode ? exit_standout_mode : (exit_attribute_mode ? exit_attribute_mode : 0));
1287c478bd9Sstevel@tonic-gate 	i = lines;
1297c478bd9Sstevel@tonic-gate 	if (lines <= 1)
1307c478bd9Sstevel@tonic-gate 		lines = 24;
1317c478bd9Sstevel@tonic-gate 	if (lines > TUBELINES)
1327c478bd9Sstevel@tonic-gate 		lines = TUBELINES;
1337c478bd9Sstevel@tonic-gate 	l = lines;
1347c478bd9Sstevel@tonic-gate 	if (ospeed < B1200)
1357c478bd9Sstevel@tonic-gate 		l = 9;	/* including the message line at the bottom */
1367c478bd9Sstevel@tonic-gate 	else if (ospeed < B2400)
1377c478bd9Sstevel@tonic-gate 		l = 17;
1387c478bd9Sstevel@tonic-gate 	if (l > lines)
1397c478bd9Sstevel@tonic-gate 		l = lines;
1407c478bd9Sstevel@tonic-gate 	/*
1417c478bd9Sstevel@tonic-gate 	 * Initialize keypad arrow keys.
1427c478bd9Sstevel@tonic-gate 	 */
1437c478bd9Sstevel@tonic-gate 	freespace = allocspace;
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate #ifdef sun
146f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_ic, (unsigned char *)"i",
147f6db9f27Scf 	    (unsigned char *)"inschar");
148f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_eic, (unsigned char *)"i",
149f6db9f27Scf 	    (unsigned char *)"inschar");
150f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_up, (unsigned char *)"k",
151f6db9f27Scf 	    (unsigned char *)"up");
152f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_down, (unsigned char *)"j",
153f6db9f27Scf 	    (unsigned char *)"down");
154f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_left, (unsigned char *)"h",
155f6db9f27Scf 	    (unsigned char *)"left");
156f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_right, (unsigned char *)"l",
157f6db9f27Scf 	    (unsigned char *)"right");
158f6db9f27Scf 	kpadd(arrows, (unsigned char *)key_home, (unsigned char *)"H",
159f6db9f27Scf 	    (unsigned char *)"home");
1607c478bd9Sstevel@tonic-gate #else
1617c478bd9Sstevel@tonic-gate 	kpadd(arrows, key_ic, "i", "inschar");
1627c478bd9Sstevel@tonic-gate 	kpadd(immacs, key_ic, "\033", "inschar");
1637c478bd9Sstevel@tonic-gate 	kpadd(arrows, key_eic, "i", "inschar");
1647c478bd9Sstevel@tonic-gate 	kpadd(immacs, key_eic, "\033", "inschar");
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_up, "k", "up");
1677c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_down, "j", "down");
1687c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_left, "h", "left");
1697c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_right, "l", "right");
1707c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_home, "H", "home");
1717c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_il, "o\033", "insline");
1727c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_dl, "dd", "delline");
1737c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_clear, "\014", "clear");
1747c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_eol, "d$", "clreol");
1757c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_sf, "\005", "scrollf");
1767c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_dc, "x", "delchar");
1777c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_npage, "\006", "npage");
1787c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_ppage, "\002", "ppage");
1797c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_sr, "\031", "sr");
1807c478bd9Sstevel@tonic-gate 	kpboth(arrows, immacs, key_eos, "dG", "clreos");
181f6db9f27Scf #endif /* sun */
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/*
1847c478bd9Sstevel@tonic-gate 	 * Handle funny termcap capabilities
1857c478bd9Sstevel@tonic-gate 	 */
1867c478bd9Sstevel@tonic-gate 	/* don't understand insert mode with multibyte characters */
1877c478bd9Sstevel@tonic-gate 	if(MB_CUR_MAX > 1) {
1887c478bd9Sstevel@tonic-gate 		enter_insert_mode = NULL;
1897c478bd9Sstevel@tonic-gate 		exit_insert_mode = NULL;
1907c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
1917c478bd9Sstevel@tonic-gate 		insert_character = NULL;
1927c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
1937c478bd9Sstevel@tonic-gate 	}
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	if (change_scroll_region && save_cursor && restore_cursor) insert_line=delete_line="";
1967c478bd9Sstevel@tonic-gate 	if (parm_insert_line && insert_line==NULL) insert_line="";
1977c478bd9Sstevel@tonic-gate 	if (parm_delete_line && delete_line==NULL) delete_line="";
1987c478bd9Sstevel@tonic-gate 	if (insert_character && enter_insert_mode==NULL) enter_insert_mode="";
1997c478bd9Sstevel@tonic-gate 	if (insert_character && exit_insert_mode==NULL) exit_insert_mode="";
2007c478bd9Sstevel@tonic-gate 	if (GT == 0)
2017c478bd9Sstevel@tonic-gate 		tab = back_tab = NOSTR;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
2047c478bd9Sstevel@tonic-gate 	/*
2057c478bd9Sstevel@tonic-gate 	 * Now map users susp char to ^Z, being careful that the susp
2067c478bd9Sstevel@tonic-gate 	 * overrides any arrow key, but only for hackers (=new tty driver).
2077c478bd9Sstevel@tonic-gate 	 */
2087c478bd9Sstevel@tonic-gate 	{
2097c478bd9Sstevel@tonic-gate 		static unsigned char sc[2];
210f6db9f27Scf 		int i;
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 		if (!value(vi_NOVICE)) {
2137c478bd9Sstevel@tonic-gate 			sc[0] = tty.c_cc[VSUSP];
2147c478bd9Sstevel@tonic-gate 			sc[1] = 0;
2157c478bd9Sstevel@tonic-gate 			if (sc[0] == CTRL('z')) {
2167c478bd9Sstevel@tonic-gate 				for (i=0; i<=4; i++)
2177c478bd9Sstevel@tonic-gate 					if (arrows[i].cap && arrows[i].cap[0] == CTRL('z'))
2187c478bd9Sstevel@tonic-gate 						addmac(sc, NULL, NULL, arrows);
2197c478bd9Sstevel@tonic-gate 			} else if(sc[0])
2207c478bd9Sstevel@tonic-gate 				addmac(sc, "\32", "susp", arrows);
2217c478bd9Sstevel@tonic-gate 		}
2227c478bd9Sstevel@tonic-gate 	}
2237c478bd9Sstevel@tonic-gate #endif
2247c478bd9Sstevel@tonic-gate 
2257c478bd9Sstevel@tonic-gate 	value(vi_WINDOW) = options[vi_WINDOW].odefault = l - 1;
2267c478bd9Sstevel@tonic-gate 	if (defwind)
2277c478bd9Sstevel@tonic-gate 		value(vi_WINDOW) = defwind;
2287c478bd9Sstevel@tonic-gate 	value(vi_SCROLL) = options[vi_SCROLL].odefault =
2297c478bd9Sstevel@tonic-gate 		hard_copy ? 11 : (value(vi_WINDOW) / 2);
2307c478bd9Sstevel@tonic-gate 	if (columns <= 4)
2317c478bd9Sstevel@tonic-gate 		columns = 1000;
2327c478bd9Sstevel@tonic-gate 	chrptr=(unsigned char *)tparm(cursor_address, 2, 2);
2337c478bd9Sstevel@tonic-gate 	if (chrptr==(unsigned char *)0 || chrptr[0] == 'O')	/* OOPS */
2347c478bd9Sstevel@tonic-gate 		cursor_address = 0;
2357c478bd9Sstevel@tonic-gate 	else
2367c478bd9Sstevel@tonic-gate 		costCM = cost(tparm(cursor_address, 10, 8));
2377c478bd9Sstevel@tonic-gate 	costSR = cost(scroll_reverse);
2387c478bd9Sstevel@tonic-gate 	costAL = cost(insert_line);
2397c478bd9Sstevel@tonic-gate 	costDP = cost(tparm(parm_down_cursor, 10));
2407c478bd9Sstevel@tonic-gate 	costLP = cost(tparm(parm_left_cursor, 10));
2417c478bd9Sstevel@tonic-gate 	costRP = cost(tparm(parm_right_cursor, 10));
2427c478bd9Sstevel@tonic-gate 	costCE = cost(clr_eol);
2437c478bd9Sstevel@tonic-gate 	costCD = cost(clr_eos);
2447c478bd9Sstevel@tonic-gate 	if (i <= 0)
2457c478bd9Sstevel@tonic-gate 		lines = 2;
2467c478bd9Sstevel@tonic-gate 	/* proper strings to change tty type */
2477c478bd9Sstevel@tonic-gate 	termreset();
2487c478bd9Sstevel@tonic-gate 	gettmode();
2497c478bd9Sstevel@tonic-gate 	value(vi_REDRAW) = insert_line && delete_line;
2507c478bd9Sstevel@tonic-gate 	value(vi_OPTIMIZE) = !cursor_address && !tab;
2517c478bd9Sstevel@tonic-gate 	if (ospeed == B1200 && !value(vi_REDRAW))
2527c478bd9Sstevel@tonic-gate 		value(vi_SLOWOPEN) = 1;	/* see also gettmode above */
2537c478bd9Sstevel@tonic-gate 	if (unknown)
254f6db9f27Scf 		serror((unsigned char *)gettext("%s: Unknown terminal type"),
255f6db9f27Scf 		    type);
2567c478bd9Sstevel@tonic-gate }
2577c478bd9Sstevel@tonic-gate 
2587c478bd9Sstevel@tonic-gate #ifndef sun
2597c478bd9Sstevel@tonic-gate /*
260*2a8bcb4eSToomas Soome  * Map both map1 and map2 as below.  map2 surrounded by esc and
2617c478bd9Sstevel@tonic-gate  * the 'i', 'R', or 'a' mode.  However, because we don't know
2627c478bd9Sstevel@tonic-gate  * the mode here we put in the escape and when the map() routine
2637c478bd9Sstevel@tonic-gate  * is called for immacs mapping the mode is appended to the
2647c478bd9Sstevel@tonic-gate  * macro. Therefore when you leave insert mode, to perform a
2657c478bd9Sstevel@tonic-gate  * function key, it will (once the cursor movement is done)
2667c478bd9Sstevel@tonic-gate  * restore you to the proper mode.
2677c478bd9Sstevel@tonic-gate  */
2687c478bd9Sstevel@tonic-gate kpboth(map1, map2, key, mapto, desc)
2697c478bd9Sstevel@tonic-gate struct maps *map1, *map2;
2707c478bd9Sstevel@tonic-gate unsigned char *key, *mapto, *desc;
2717c478bd9Sstevel@tonic-gate {
2727c478bd9Sstevel@tonic-gate 	unsigned char surmapto[30];
2737c478bd9Sstevel@tonic-gate 	unsigned char *p;
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	if (key == 0)
2767c478bd9Sstevel@tonic-gate 		return;
2777c478bd9Sstevel@tonic-gate 	kpadd(map1, key, mapto, desc);
2787c478bd9Sstevel@tonic-gate 	if (any(*key, "\b\n "))
2797c478bd9Sstevel@tonic-gate 		return;
2807c478bd9Sstevel@tonic-gate 	strcpy(surmapto, "\33");
2817c478bd9Sstevel@tonic-gate 	strcat(surmapto, mapto);
2827c478bd9Sstevel@tonic-gate 	p = freespace;
2837c478bd9Sstevel@tonic-gate 	strcpy(p, surmapto);
2847c478bd9Sstevel@tonic-gate 	freespace += strlen(surmapto) + 1;
2857c478bd9Sstevel@tonic-gate 	kpadd(map2, key, p, desc);
2867c478bd9Sstevel@tonic-gate }
287f6db9f27Scf #endif /* !sun */
2887c478bd9Sstevel@tonic-gate 
2897c478bd9Sstevel@tonic-gate /*
2907c478bd9Sstevel@tonic-gate  * Define a macro.  mapstr is the structure (mode) in which it applies.
2917c478bd9Sstevel@tonic-gate  * key is the input sequence, mapto what it turns into, and desc is a
2927c478bd9Sstevel@tonic-gate  * human-readable description of what's going on.
2937c478bd9Sstevel@tonic-gate  */
294f6db9f27Scf void
kpadd(struct maps * mapstr,unsigned char * key,unsigned char * mapto,unsigned char * desc)295f6db9f27Scf kpadd(struct maps *mapstr, unsigned char *key, unsigned char *mapto,
296f6db9f27Scf unsigned char *desc)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int i;
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	for (i=0; i<MAXNOMACS; i++)
3017c478bd9Sstevel@tonic-gate 		if (mapstr[i].cap == 0)
3027c478bd9Sstevel@tonic-gate 			break;
3037c478bd9Sstevel@tonic-gate 	if (key == 0 || i >= MAXNOMACS)
3047c478bd9Sstevel@tonic-gate 		return;
3057c478bd9Sstevel@tonic-gate 	mapstr[i].cap = key;
3067c478bd9Sstevel@tonic-gate 	mapstr[i].mapto = mapto;
3077c478bd9Sstevel@tonic-gate 	mapstr[i].descr = desc;
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate unsigned char *
fkey(i)3117c478bd9Sstevel@tonic-gate fkey(i)
3127c478bd9Sstevel@tonic-gate 	int i;
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 	if (i < 0 || i > 9)
315f6db9f27Scf 		return ((unsigned char *)NOSTR);
3167c478bd9Sstevel@tonic-gate 	switch (i) {
317f6db9f27Scf 	case 0: return ((unsigned char *)key_f0);
318f6db9f27Scf 	case 1: return ((unsigned char *)key_f1);
319f6db9f27Scf 	case 2: return ((unsigned char *)key_f2);
320f6db9f27Scf 	case 3: return ((unsigned char *)key_f3);
321f6db9f27Scf 	case 4: return ((unsigned char *)key_f4);
322f6db9f27Scf 	case 5: return ((unsigned char *)key_f5);
323f6db9f27Scf 	case 6: return ((unsigned char *)key_f6);
324f6db9f27Scf 	case 7: return ((unsigned char *)key_f7);
325f6db9f27Scf 	case 8: return ((unsigned char *)key_f8);
326f6db9f27Scf 	case 9: return ((unsigned char *)key_f9);
327f6db9f27Scf 	case 10: return ((unsigned char *)key_f0);
3287c478bd9Sstevel@tonic-gate 	}
329f6db9f27Scf 	return ((unsigned char *)NOSTR);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate /*
3337c478bd9Sstevel@tonic-gate  * cost figures out how much (in characters) it costs to send the string
3347c478bd9Sstevel@tonic-gate  * str to the terminal.  It takes into account padding information, as
3357c478bd9Sstevel@tonic-gate  * much as it can, for a typical case.  (Right now the typical case assumes
3367c478bd9Sstevel@tonic-gate  * the number of lines affected is the size of the screen, since this is
3377c478bd9Sstevel@tonic-gate  * mainly used to decide if insert_line or scroll_reverse is better, and this always happens
3387c478bd9Sstevel@tonic-gate  * at the top of the screen.  We assume cursor motion (cursor_address) has little
3397c478bd9Sstevel@tonic-gate  * padding, if any, required, so that case, which is really more important
3407c478bd9Sstevel@tonic-gate  * than insert_line vs scroll_reverse, won't be really affected.)
3417c478bd9Sstevel@tonic-gate  */
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate static int costnum;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate /* ARGSUSED */
3467c478bd9Sstevel@tonic-gate int
countnum(char ch)3477c478bd9Sstevel@tonic-gate countnum(char ch)
3487c478bd9Sstevel@tonic-gate {
3497c478bd9Sstevel@tonic-gate 	costnum++;
350f6db9f27Scf 	return (0);
3517c478bd9Sstevel@tonic-gate }
3527c478bd9Sstevel@tonic-gate 
353f6db9f27Scf int
cost(unsigned char * str)354f6db9f27Scf cost(unsigned char *str)
3557c478bd9Sstevel@tonic-gate {
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	if (str == NULL || *str=='O')	/* OOPS */
3587c478bd9Sstevel@tonic-gate 		return 10000;	/* infinity */
3597c478bd9Sstevel@tonic-gate 	costnum = 0;
3607c478bd9Sstevel@tonic-gate 	tputs((char *)str, lines, countnum);
3617c478bd9Sstevel@tonic-gate 	return costnum;
3627c478bd9Sstevel@tonic-gate }
363