xref: /onnv-gate/usr/src/cmd/vi/port/ex_put.c (revision 802:73b56fb6544b)
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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
330Sstevel@tonic-gate 
340Sstevel@tonic-gate #include "ex.h"
350Sstevel@tonic-gate #include "ex_tty.h"
360Sstevel@tonic-gate #include "ex_vis.h"
370Sstevel@tonic-gate #ifndef PRESUNEUC
380Sstevel@tonic-gate #include <wctype.h>
390Sstevel@tonic-gate /* Undef putchar/getchar if they're defined. */
400Sstevel@tonic-gate #ifdef putchar
410Sstevel@tonic-gate #	undef putchar
420Sstevel@tonic-gate #endif
430Sstevel@tonic-gate #ifdef getchar
440Sstevel@tonic-gate #	undef getchar
450Sstevel@tonic-gate #endif
460Sstevel@tonic-gate #endif /* PRESUNEUC */
470Sstevel@tonic-gate 
480Sstevel@tonic-gate /*
490Sstevel@tonic-gate  * Terminal driving and line formatting routines.
500Sstevel@tonic-gate  * Basic motion optimizations are done here as well
510Sstevel@tonic-gate  * as formatting of lines (printing of control characters,
520Sstevel@tonic-gate  * line numbering and the like).
530Sstevel@tonic-gate  */
540Sstevel@tonic-gate 
550Sstevel@tonic-gate /*
560Sstevel@tonic-gate  * The routines outchar, putchar and pline are actually
570Sstevel@tonic-gate  * variables, and these variables point at the current definitions
580Sstevel@tonic-gate  * of the routines.  See the routine setflav.
590Sstevel@tonic-gate  * We sometimes make outchar be routines which catch the characters
600Sstevel@tonic-gate  * to be printed, e.g. if we want to see how long a line is.
610Sstevel@tonic-gate  * During open/visual, outchar and putchar will be set to
620Sstevel@tonic-gate  * routines in the file ex_vput.c (vputchar, vinschar, etc.).
630Sstevel@tonic-gate  */
640Sstevel@tonic-gate int	(*Outchar)() = termchar;
650Sstevel@tonic-gate int	(*Putchar)() = normchar;
660Sstevel@tonic-gate int	(*Pline)() = normline;
670Sstevel@tonic-gate static unsigned char multic[MULTI_BYTE_MAX];
680Sstevel@tonic-gate bool putoctal; /* flag to say if byte should be printed as octal */
690Sstevel@tonic-gate int termiosflag = -1; /* flag for using termios ioctl
700Sstevel@tonic-gate 			      * structure */
710Sstevel@tonic-gate 
720Sstevel@tonic-gate int (*
730Sstevel@tonic-gate setlist(t))()
740Sstevel@tonic-gate 	bool t;
750Sstevel@tonic-gate {
76*802Scf46844 	int (*P)();
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	listf = t;
790Sstevel@tonic-gate 	P = Putchar;
800Sstevel@tonic-gate 	Putchar = t ? listchar : normchar;
810Sstevel@tonic-gate 	return (P);
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate int (*
850Sstevel@tonic-gate setnumb(t))()
860Sstevel@tonic-gate 	bool t;
870Sstevel@tonic-gate {
88*802Scf46844 	int (*P)();
890Sstevel@tonic-gate 
900Sstevel@tonic-gate 	numberf = t;
910Sstevel@tonic-gate 	P = Pline;
92*802Scf46844 	Pline = t ? (int (*)())numbline : normline;
930Sstevel@tonic-gate 	return (P);
940Sstevel@tonic-gate }
950Sstevel@tonic-gate 
960Sstevel@tonic-gate /*
970Sstevel@tonic-gate  * Format c for list mode; leave things in common
980Sstevel@tonic-gate  * with normal print mode to be done by normchar.
990Sstevel@tonic-gate  */
100*802Scf46844 int
listchar(wchar_t c)101*802Scf46844 listchar(wchar_t c)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	c &= (int)(TRIM|QUOTE);
1050Sstevel@tonic-gate 	switch (c) {
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	case '\t':
1080Sstevel@tonic-gate 	case '\b':
1090Sstevel@tonic-gate 		outchar('^');
1100Sstevel@tonic-gate 		c = ctlof(c);
1110Sstevel@tonic-gate 		break;
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	case '\n':
1140Sstevel@tonic-gate 		break;
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate 	case (int)('\n' | QUOTE):
1170Sstevel@tonic-gate 		outchar('$');
1180Sstevel@tonic-gate 		break;
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate 	default:
1210Sstevel@tonic-gate 		if((int)(c & QUOTE))
1220Sstevel@tonic-gate 			break;
1230Sstevel@tonic-gate 		if (c < ' ' && c != '\n' || c == DELETE)
1240Sstevel@tonic-gate 			outchar('^'), c = ctlof(c);
1250Sstevel@tonic-gate 	}
126*802Scf46844 	(void) normchar(c);
127*802Scf46844 	return (0);
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate /*
1310Sstevel@tonic-gate  * Format c for printing.  Handle funnies of upper case terminals
1320Sstevel@tonic-gate  * and hazeltines which don't have ~.
1330Sstevel@tonic-gate  */
134*802Scf46844 int
normchar(wchar_t c)135*802Scf46844 normchar(wchar_t c)
1360Sstevel@tonic-gate {
137*802Scf46844 	char *colp;
1380Sstevel@tonic-gate 
1390Sstevel@tonic-gate 	c &= (int)(TRIM|QUOTE);
1400Sstevel@tonic-gate 	if (c == '~' && tilde_glitch) {
141*802Scf46844 		(void) normchar('\\');
1420Sstevel@tonic-gate 		c = '^';
1430Sstevel@tonic-gate 	}
1440Sstevel@tonic-gate 	if ((int)(c & QUOTE))
1450Sstevel@tonic-gate 		switch (c) {
1460Sstevel@tonic-gate 
1470Sstevel@tonic-gate 		case (int)(' ' | QUOTE):
1480Sstevel@tonic-gate 		case (int)('\b' | QUOTE):
1490Sstevel@tonic-gate 			break;
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate 		case (int)QUOTE:
152*802Scf46844 			return (0);
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate 		default:
1550Sstevel@tonic-gate 			c &= (int)TRIM;
1560Sstevel@tonic-gate 		}
1570Sstevel@tonic-gate 	else if (c < ' ' && (c != '\b' || !over_strike) && c != '\n' && c != '\t' || c == DELETE)
1580Sstevel@tonic-gate 		putchar('^'), c = ctlof(c);
1590Sstevel@tonic-gate 	else if (c >= 0200 && (putoctal || !iswprint(c))) {
1600Sstevel@tonic-gate 		outchar('\\');
1610Sstevel@tonic-gate 		outchar(((c >> 6) & 07) + '0');
1620Sstevel@tonic-gate 		outchar(((c >> 3) & 07) + '0');
1630Sstevel@tonic-gate 		outchar((c & 07) + '0');
164*802Scf46844 		return (0);
1650Sstevel@tonic-gate 	} else if (UPPERCASE)
1660Sstevel@tonic-gate 		if (isupper(c)) {
1670Sstevel@tonic-gate 			outchar('\\');
1680Sstevel@tonic-gate 			c = tolower(c);
1690Sstevel@tonic-gate 		} else {
1700Sstevel@tonic-gate 			colp = "({)}!|^~'`";
1710Sstevel@tonic-gate 			while (*colp++)
1720Sstevel@tonic-gate 				if (c == *colp++) {
1730Sstevel@tonic-gate 					outchar('\\');
1740Sstevel@tonic-gate 					c = colp[-2];
1750Sstevel@tonic-gate 					break;
1760Sstevel@tonic-gate 				}
1770Sstevel@tonic-gate 		}
1780Sstevel@tonic-gate 	outchar(c);
179*802Scf46844 	return (0);
1800Sstevel@tonic-gate }
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate /*
1830Sstevel@tonic-gate  * Print a line with a number.
1840Sstevel@tonic-gate  */
185*802Scf46844 int
numbline(int i)186*802Scf46844 numbline(int i)
1870Sstevel@tonic-gate {
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate 	if (shudclob)
1900Sstevel@tonic-gate 		slobber(' ');
191*802Scf46844 	viprintf("%6d  ", i);
192*802Scf46844 	(void) normline();
193*802Scf46844 	return (0);
1940Sstevel@tonic-gate }
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate /*
1970Sstevel@tonic-gate  * Normal line output, no numbering.
1980Sstevel@tonic-gate  */
199*802Scf46844 int
normline(void)200*802Scf46844 normline(void)
2010Sstevel@tonic-gate {
202*802Scf46844 	unsigned char *cp;
203*802Scf46844 	int n;
2040Sstevel@tonic-gate 	wchar_t wchar;
2050Sstevel@tonic-gate 	if (shudclob)
2060Sstevel@tonic-gate 		slobber(linebuf[0]);
2070Sstevel@tonic-gate 	/* pdp-11 doprnt is not reentrant so can't use "printf" here
2080Sstevel@tonic-gate 	   in case we are tracing */
2090Sstevel@tonic-gate 	for (cp = linebuf; *cp;)
2100Sstevel@tonic-gate 		if((n = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX)) < 0) {
2110Sstevel@tonic-gate 			putoctal = 1;
2120Sstevel@tonic-gate 			putchar(*cp++);
2130Sstevel@tonic-gate 			putoctal = 0;
2140Sstevel@tonic-gate 		} else {
2150Sstevel@tonic-gate 			cp += n;
2160Sstevel@tonic-gate 			putchar(wchar);
2170Sstevel@tonic-gate 		}
2180Sstevel@tonic-gate 	if (!inopen)
2190Sstevel@tonic-gate 		putchar((int)('\n' | QUOTE));
220*802Scf46844 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Given c at the beginning of a line, determine whether
2250Sstevel@tonic-gate  * the printing of the line will erase or otherwise obliterate
2260Sstevel@tonic-gate  * the prompt which was printed before.  If it won't, do it now.
2270Sstevel@tonic-gate  */
228*802Scf46844 void
slobber(int c)229*802Scf46844 slobber(int c)
2300Sstevel@tonic-gate {
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	shudclob = 0;
2330Sstevel@tonic-gate 	switch (c) {
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	case '\t':
2360Sstevel@tonic-gate 		if (Putchar == listchar)
2370Sstevel@tonic-gate 			return;
2380Sstevel@tonic-gate 		break;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	default:
2410Sstevel@tonic-gate 		return;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	case ' ':
2440Sstevel@tonic-gate 	case 0:
2450Sstevel@tonic-gate 		break;
2460Sstevel@tonic-gate 	}
2470Sstevel@tonic-gate 	if (over_strike)
2480Sstevel@tonic-gate 		return;
2490Sstevel@tonic-gate 	flush();
250*802Scf46844 	(void) putch(' ');
2510Sstevel@tonic-gate 	tputs(cursor_left, 0, putch);
2520Sstevel@tonic-gate }
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate /*
2550Sstevel@tonic-gate  * The output buffer is initialized with a useful error
2560Sstevel@tonic-gate  * message so we don't have to keep it in data space.
2570Sstevel@tonic-gate  */
2580Sstevel@tonic-gate static	wchar_t linb[66];
2590Sstevel@tonic-gate wchar_t *linp = linb;
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate  * Phadnl records when we have already had a complete line ending with \n.
2630Sstevel@tonic-gate  * If another line starts without a flush, and the terminal suggests it,
2640Sstevel@tonic-gate  * we switch into -nl mode so that we can send linefeeds to avoid
2650Sstevel@tonic-gate  * a lot of spacing.
2660Sstevel@tonic-gate  */
2670Sstevel@tonic-gate static	bool phadnl;
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate /*
2700Sstevel@tonic-gate  * Indirect to current definition of putchar.
2710Sstevel@tonic-gate  */
272*802Scf46844 int
putchar(int c)2730Sstevel@tonic-gate putchar(int c)
2740Sstevel@tonic-gate {
275*802Scf46844 	return ((*Putchar)((wchar_t)c));
2760Sstevel@tonic-gate }
2770Sstevel@tonic-gate 
2780Sstevel@tonic-gate /*
2790Sstevel@tonic-gate  * Termchar routine for command mode.
2800Sstevel@tonic-gate  * Watch for possible switching to -nl mode.
2810Sstevel@tonic-gate  * Otherwise flush into next level of buffering when
2820Sstevel@tonic-gate  * small buffer fills or at a newline.
2830Sstevel@tonic-gate  */
284*802Scf46844 int
termchar(wchar_t c)285*802Scf46844 termchar(wchar_t c)
2860Sstevel@tonic-gate {
2870Sstevel@tonic-gate 
2880Sstevel@tonic-gate 	if (pfast == 0 && phadnl)
2890Sstevel@tonic-gate 		pstart();
2900Sstevel@tonic-gate 	if (c == '\n')
2910Sstevel@tonic-gate 		phadnl = 1;
2920Sstevel@tonic-gate 	else if (linp >= &linb[63])
2930Sstevel@tonic-gate 		flush1();
2940Sstevel@tonic-gate 	*linp++ = c;
2950Sstevel@tonic-gate 	if (linp >= &linb[63]) {
2960Sstevel@tonic-gate 		fgoto();
2970Sstevel@tonic-gate 		flush1();
2980Sstevel@tonic-gate 	}
299*802Scf46844 	return (0);
3000Sstevel@tonic-gate }
3010Sstevel@tonic-gate 
302*802Scf46844 void
flush(void)303*802Scf46844 flush(void)
3040Sstevel@tonic-gate {
3050Sstevel@tonic-gate 
3060Sstevel@tonic-gate 	flush1();
3070Sstevel@tonic-gate 	flush2();
3080Sstevel@tonic-gate }
3090Sstevel@tonic-gate 
3100Sstevel@tonic-gate /*
3110Sstevel@tonic-gate  * Flush from small line buffer into output buffer.
3120Sstevel@tonic-gate  * Work here is destroying motion into positions, and then
3130Sstevel@tonic-gate  * letting fgoto do the optimized motion.
3140Sstevel@tonic-gate  */
315*802Scf46844 void
flush1(void)316*802Scf46844 flush1(void)
3170Sstevel@tonic-gate {
318*802Scf46844 	wchar_t *lp;
319*802Scf46844 	wchar_t c;
3200Sstevel@tonic-gate #ifdef PRESUNEUC
3210Sstevel@tonic-gate 	/* used for multibyte characters split between lines */
322*802Scf46844 	int splitcnt = 0;
3230Sstevel@tonic-gate #else
3240Sstevel@tonic-gate 	/* used for multicolumn character substitution and padding */
325*802Scf46844 	int fillercnt = 0;
3260Sstevel@tonic-gate #endif /* PRESUNEUC */
3270Sstevel@tonic-gate 	*linp = 0;
3280Sstevel@tonic-gate 	lp = linb;
3290Sstevel@tonic-gate 	while (*lp)
3300Sstevel@tonic-gate 		switch (c = *lp++) {
3310Sstevel@tonic-gate 
3320Sstevel@tonic-gate 		case '\r':
3330Sstevel@tonic-gate 			destline += destcol / columns;
3340Sstevel@tonic-gate 			destcol = 0;
3350Sstevel@tonic-gate 			continue;
3360Sstevel@tonic-gate 
3370Sstevel@tonic-gate 		case '\b':
3380Sstevel@tonic-gate 			if (destcol)
3390Sstevel@tonic-gate 				destcol--;
3400Sstevel@tonic-gate 			continue;
3410Sstevel@tonic-gate 
3420Sstevel@tonic-gate 		case ' ':
3430Sstevel@tonic-gate 			destcol++;
3440Sstevel@tonic-gate 			continue;
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 		case '\t':
3470Sstevel@tonic-gate 			destcol += value(vi_TABSTOP) - destcol % value(vi_TABSTOP);
3480Sstevel@tonic-gate 			continue;
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 		case '\n':
3510Sstevel@tonic-gate 			destline += destcol / columns + 1;
3520Sstevel@tonic-gate 			if (destcol != 0 && destcol % columns == 0)
3530Sstevel@tonic-gate 				destline--;
3540Sstevel@tonic-gate 			destcol = 0;
3550Sstevel@tonic-gate 			continue;
3560Sstevel@tonic-gate 
3570Sstevel@tonic-gate 		default:
3580Sstevel@tonic-gate 			fgoto();
3590Sstevel@tonic-gate 			for (;;) {
3600Sstevel@tonic-gate 				int length, length2;
3610Sstevel@tonic-gate 				unsigned char *p;
3620Sstevel@tonic-gate 				c &= TRIM;
3630Sstevel@tonic-gate 				if ((length = wcwidth(c)) < 0)
3640Sstevel@tonic-gate 					length = 0;
3650Sstevel@tonic-gate 				if (auto_right_margin == 0 && outcol >= columns)
3660Sstevel@tonic-gate 					fgoto();
3670Sstevel@tonic-gate 				if((destcol % columns) + length - 1 >= columns) {
3680Sstevel@tonic-gate #ifdef PRESUNEUC
3690Sstevel@tonic-gate 					/* represent split chars by '>' */
3700Sstevel@tonic-gate 					splitcnt = length - 1;
3710Sstevel@tonic-gate 					c = '>';
3720Sstevel@tonic-gate #else
3730Sstevel@tonic-gate 					/* substitute/wrap multicolumn char */
3740Sstevel@tonic-gate 					if(mc_wrap) {
3750Sstevel@tonic-gate 						fillercnt = columns -
3760Sstevel@tonic-gate 							    (destcol % columns);
3770Sstevel@tonic-gate 						while(fillercnt) {
378*802Scf46844 							(void) putch(mc_filler);
3790Sstevel@tonic-gate 							outcol++;
3800Sstevel@tonic-gate 							destcol++;
3810Sstevel@tonic-gate 							fillercnt--;
3820Sstevel@tonic-gate 						}
3830Sstevel@tonic-gate 					} else {
3840Sstevel@tonic-gate 						fillercnt = length - 1;
3850Sstevel@tonic-gate 						c = mc_filler;
3860Sstevel@tonic-gate 					}
3870Sstevel@tonic-gate #endif /* PRESUNEUC */
3880Sstevel@tonic-gate 					continue;
3890Sstevel@tonic-gate 				}
3900Sstevel@tonic-gate 				length2 = wctomb((char *)multic, c);
3910Sstevel@tonic-gate 				p = multic;
3920Sstevel@tonic-gate 				while(length2--)
393*802Scf46844 					(void) putch(*p++);
3940Sstevel@tonic-gate 				if (c == '\b') {
3950Sstevel@tonic-gate 					outcol--;
3960Sstevel@tonic-gate 					destcol--;
3970Sstevel@tonic-gate 				} else if (c >= ' ' && c != DELETE) {
3980Sstevel@tonic-gate 					outcol += length;
3990Sstevel@tonic-gate 					destcol += length;
4000Sstevel@tonic-gate 					if (eat_newline_glitch && outcol % columns == 0)
401*802Scf46844 						(void) putch('\r'),
402*802Scf46844 						    (void) putch('\n');
4030Sstevel@tonic-gate 				}
4040Sstevel@tonic-gate #ifdef PRESUNEUC
4050Sstevel@tonic-gate 				if(splitcnt) {
4060Sstevel@tonic-gate 					splitcnt--;
4070Sstevel@tonic-gate 					c = '>';
4080Sstevel@tonic-gate 				} else
4090Sstevel@tonic-gate 					c = *lp++;
4100Sstevel@tonic-gate #else
4110Sstevel@tonic-gate 				if(fillercnt) {
4120Sstevel@tonic-gate 					fillercnt--;
4130Sstevel@tonic-gate 					c = mc_filler;
4140Sstevel@tonic-gate 					if(c == ' ')
4150Sstevel@tonic-gate 						continue;
4160Sstevel@tonic-gate 				} else
4170Sstevel@tonic-gate 					c = *lp++;
4180Sstevel@tonic-gate #endif /* PRESUNEUC */
4190Sstevel@tonic-gate 				if (c <= ' ')
4200Sstevel@tonic-gate 					break;
4210Sstevel@tonic-gate 			}
4220Sstevel@tonic-gate 			--lp;
4230Sstevel@tonic-gate 			continue;
4240Sstevel@tonic-gate 		}
4250Sstevel@tonic-gate 	linp = linb;
4260Sstevel@tonic-gate }
4270Sstevel@tonic-gate 
428*802Scf46844 void
flush2(void)429*802Scf46844 flush2(void)
4300Sstevel@tonic-gate {
4310Sstevel@tonic-gate 
4320Sstevel@tonic-gate 	fgoto();
4330Sstevel@tonic-gate 	flusho();
4340Sstevel@tonic-gate 	pstop();
4350Sstevel@tonic-gate }
4360Sstevel@tonic-gate 
4370Sstevel@tonic-gate /*
4380Sstevel@tonic-gate  * Sync the position of the output cursor.
4390Sstevel@tonic-gate  * Most work here is rounding for terminal boundaries getting the
4400Sstevel@tonic-gate  * column position implied by wraparound or the lack thereof and
4410Sstevel@tonic-gate  * rolling up the screen to get destline on the screen.
4420Sstevel@tonic-gate  */
443*802Scf46844 void
fgoto(void)444*802Scf46844 fgoto(void)
4450Sstevel@tonic-gate {
446*802Scf46844 	int l, c;
4470Sstevel@tonic-gate 
4480Sstevel@tonic-gate 	if (destcol > columns - 1) {
4490Sstevel@tonic-gate 		destline += destcol / columns;
4500Sstevel@tonic-gate 		destcol %= columns;
4510Sstevel@tonic-gate 	}
4520Sstevel@tonic-gate 	if (outcol > columns - 1) {
4530Sstevel@tonic-gate 		l = (outcol + 1) / columns;
4540Sstevel@tonic-gate 		outline += l;
4550Sstevel@tonic-gate 		outcol %= columns;
4560Sstevel@tonic-gate 		if (auto_right_margin == 0) {
4570Sstevel@tonic-gate 			while (l > 0) {
4580Sstevel@tonic-gate 				if (pfast)
4590Sstevel@tonic-gate 					tputs(carriage_return, 0, putch);
4600Sstevel@tonic-gate 				tputs(cursor_down, 0, putch);
4610Sstevel@tonic-gate 				l--;
4620Sstevel@tonic-gate 			}
4630Sstevel@tonic-gate 			outcol = 0;
4640Sstevel@tonic-gate 		}
4650Sstevel@tonic-gate 		if (outline > lines - 1) {
4660Sstevel@tonic-gate 			destline -= outline - (lines - 1);
4670Sstevel@tonic-gate 			outline = lines - 1;
4680Sstevel@tonic-gate 		}
4690Sstevel@tonic-gate 	}
4700Sstevel@tonic-gate 	if (destline > lines - 1) {
4710Sstevel@tonic-gate 		l = destline;
4720Sstevel@tonic-gate 		destline = lines - 1;
4730Sstevel@tonic-gate 		if (outline < lines - 1) {
4740Sstevel@tonic-gate 			c = destcol;
4750Sstevel@tonic-gate 			if (pfast == 0 && (!cursor_address || holdcm))
4760Sstevel@tonic-gate 				destcol = 0;
4770Sstevel@tonic-gate 			fgoto();
4780Sstevel@tonic-gate 			destcol = c;
4790Sstevel@tonic-gate 		}
4800Sstevel@tonic-gate 		while (l > lines - 1) {
4810Sstevel@tonic-gate 			/*
4820Sstevel@tonic-gate 			 * The following linefeed (or simulation thereof)
4830Sstevel@tonic-gate 			 * is supposed to scroll up the screen, since we
4840Sstevel@tonic-gate 			 * are on the bottom line.
4850Sstevel@tonic-gate 			 *
4860Sstevel@tonic-gate 			 * Superbee glitch:  in the middle of the screen we
4870Sstevel@tonic-gate 			 * have to use esc B (down) because linefeed messes up
4880Sstevel@tonic-gate 			 * in "Efficient Paging" mode (which is essential in
4890Sstevel@tonic-gate 			 * some SB's because CRLF mode puts garbage
4900Sstevel@tonic-gate 			 * in at end of memory), but you must use linefeed to
4910Sstevel@tonic-gate 			 * scroll since down arrow won't go past memory end.
4920Sstevel@tonic-gate 			 * I turned this off after receiving Paul Eggert's
4930Sstevel@tonic-gate 			 * Superbee description which wins better.
4940Sstevel@tonic-gate 			 */
4950Sstevel@tonic-gate 			if (scroll_forward /* && !beehive_glitch */ && pfast)
4960Sstevel@tonic-gate 				tputs(scroll_forward, 0, putch);
4970Sstevel@tonic-gate 			else
498*802Scf46844 				(void) putch('\n');
4990Sstevel@tonic-gate 			l--;
5000Sstevel@tonic-gate 			if (pfast == 0)
5010Sstevel@tonic-gate 				outcol = 0;
5020Sstevel@tonic-gate 		}
5030Sstevel@tonic-gate 	}
5040Sstevel@tonic-gate 	if (destline < outline && !(cursor_address && !holdcm || cursor_up || cursor_home))
5050Sstevel@tonic-gate 		destline = outline;
5060Sstevel@tonic-gate 	if (cursor_address && !holdcm)
5070Sstevel@tonic-gate 		if (plod(costCM) > 0)
5080Sstevel@tonic-gate 			plod(0);
5090Sstevel@tonic-gate 		else
5100Sstevel@tonic-gate 			tputs(tparm(cursor_address, destline, destcol), 0, putch);
5110Sstevel@tonic-gate 	else
5120Sstevel@tonic-gate 		plod(0);
5130Sstevel@tonic-gate 	outline = destline;
5140Sstevel@tonic-gate 	outcol = destcol;
5150Sstevel@tonic-gate }
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate /*
5180Sstevel@tonic-gate  * Tab to column col by flushing and then setting destcol.
5190Sstevel@tonic-gate  * Used by "set all".
5200Sstevel@tonic-gate  */
521*802Scf46844 void
gotab(int col)522*802Scf46844 gotab(int col)
5230Sstevel@tonic-gate {
5240Sstevel@tonic-gate 
5250Sstevel@tonic-gate 	flush1();
5260Sstevel@tonic-gate 	destcol = col;
5270Sstevel@tonic-gate }
5280Sstevel@tonic-gate 
5290Sstevel@tonic-gate /*
5300Sstevel@tonic-gate  * Move (slowly) to destination.
5310Sstevel@tonic-gate  * Hard thing here is using home cursor on really deficient terminals.
5320Sstevel@tonic-gate  * Otherwise just use cursor motions, hacking use of tabs and overtabbing
5330Sstevel@tonic-gate  * and backspace.
5340Sstevel@tonic-gate  */
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate static int plodcnt, plodflg;
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate int
5390Sstevel@tonic-gate #ifdef __STDC__
plodput(char c)5400Sstevel@tonic-gate plodput(char c)
5410Sstevel@tonic-gate #else
5420Sstevel@tonic-gate plodput(c)
5430Sstevel@tonic-gate char c;
5440Sstevel@tonic-gate #endif
5450Sstevel@tonic-gate {
5460Sstevel@tonic-gate 
5470Sstevel@tonic-gate 	if (plodflg)
5480Sstevel@tonic-gate 		plodcnt--;
5490Sstevel@tonic-gate 	else
550*802Scf46844 		(void) putch(c);
551*802Scf46844 	return (0);
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate 
554*802Scf46844 int
plod(int cnt)555*802Scf46844 plod(int cnt)
5560Sstevel@tonic-gate {
557*802Scf46844 	int i, j, k;
558*802Scf46844 	int soutcol, soutline;
5590Sstevel@tonic-gate 
5600Sstevel@tonic-gate 	plodcnt = plodflg = cnt;
5610Sstevel@tonic-gate 	soutcol = outcol;
5620Sstevel@tonic-gate 	soutline = outline;
5630Sstevel@tonic-gate 	/*
5640Sstevel@tonic-gate 	 * Consider homing and moving down/right from there, vs moving
5650Sstevel@tonic-gate 	 * directly with local motions to the right spot.
5660Sstevel@tonic-gate 	 */
5670Sstevel@tonic-gate 	if (cursor_home) {
5680Sstevel@tonic-gate 		/*
5690Sstevel@tonic-gate 		 * i is the cost to home and tab/space to the right to
5700Sstevel@tonic-gate 		 * get to the proper column.  This assumes cursor_right costs
5710Sstevel@tonic-gate 		 * 1 char.  So i+destcol is cost of motion with home.
5720Sstevel@tonic-gate 		 */
5730Sstevel@tonic-gate 		if (tab && value(vi_HARDTABS))
5740Sstevel@tonic-gate 			i = (destcol / value(vi_HARDTABS)) + (destcol % value(vi_HARDTABS));
5750Sstevel@tonic-gate 		else
5760Sstevel@tonic-gate 			i = destcol;
5770Sstevel@tonic-gate 		/*
5780Sstevel@tonic-gate 		 * j is cost to move locally without homing
5790Sstevel@tonic-gate 		 */
5800Sstevel@tonic-gate 		if (destcol >= outcol) {	/* if motion is to the right */
5810Sstevel@tonic-gate 			if (value(vi_HARDTABS)) {
5820Sstevel@tonic-gate 				j = destcol / value(vi_HARDTABS) - outcol / value(vi_HARDTABS);
5830Sstevel@tonic-gate 				if (tab && j)
5840Sstevel@tonic-gate 					j += destcol % value(vi_HARDTABS);
5850Sstevel@tonic-gate 				else
5860Sstevel@tonic-gate 					j = destcol - outcol;
5870Sstevel@tonic-gate 			} else
5880Sstevel@tonic-gate 				j = destcol - outcol;
5890Sstevel@tonic-gate 		} else
5900Sstevel@tonic-gate 			/* leftward motion only works if we can backspace. */
5910Sstevel@tonic-gate 			if (outcol - destcol <= i && (cursor_left))
5920Sstevel@tonic-gate 				i = j = outcol - destcol; /* cheaper to backspace */
5930Sstevel@tonic-gate 			else
5940Sstevel@tonic-gate 				j = i + 1; /* impossibly expensive */
5950Sstevel@tonic-gate 
5960Sstevel@tonic-gate 		/* k is the absolute value of vertical distance */
5970Sstevel@tonic-gate 		k = outline - destline;
5980Sstevel@tonic-gate 		if (k < 0)
5990Sstevel@tonic-gate 			k = -k;
6000Sstevel@tonic-gate 		j += k;
6010Sstevel@tonic-gate 
6020Sstevel@tonic-gate 		/*
6030Sstevel@tonic-gate 		 * Decision.  We may not have a choice if no cursor_up.
6040Sstevel@tonic-gate 		 */
6050Sstevel@tonic-gate 		if (i + destline < j || (!cursor_up && destline < outline)) {
6060Sstevel@tonic-gate 			/*
6070Sstevel@tonic-gate 			 * Cheaper to home.  Do it now and pretend it's a
6080Sstevel@tonic-gate 			 * regular local motion.
6090Sstevel@tonic-gate 			 */
6100Sstevel@tonic-gate 			tputs(cursor_home, 0, plodput);
6110Sstevel@tonic-gate 			outcol = outline = 0;
6120Sstevel@tonic-gate 		} else if (cursor_to_ll) {
6130Sstevel@tonic-gate 			/*
6140Sstevel@tonic-gate 			 * Quickly consider homing down and moving from there.
6150Sstevel@tonic-gate 			 * Assume cost of cursor_to_ll is 2.
6160Sstevel@tonic-gate 			 */
6170Sstevel@tonic-gate 			k = (lines - 1) - destline;
6180Sstevel@tonic-gate 			if (i + k + 2 < j && (k<=0 || cursor_up)) {
6190Sstevel@tonic-gate 				tputs(cursor_to_ll, 0, plodput);
6200Sstevel@tonic-gate 				outcol = 0;
6210Sstevel@tonic-gate 				outline = lines - 1;
6220Sstevel@tonic-gate 			}
6230Sstevel@tonic-gate 		}
6240Sstevel@tonic-gate 	} else
6250Sstevel@tonic-gate 		/*
6260Sstevel@tonic-gate 		 * No home and no up means it's impossible, so we return an
6270Sstevel@tonic-gate 		 * incredibly big number to make cursor motion win out.
6280Sstevel@tonic-gate 		 */
6290Sstevel@tonic-gate 		if (!cursor_up && destline < outline)
6300Sstevel@tonic-gate 			return (500);
6310Sstevel@tonic-gate 	if (tab && value(vi_HARDTABS))
6320Sstevel@tonic-gate 		i = destcol % value(vi_HARDTABS)
6330Sstevel@tonic-gate 		    + destcol / value(vi_HARDTABS);
6340Sstevel@tonic-gate 	else
6350Sstevel@tonic-gate 		i = destcol;
6360Sstevel@tonic-gate /*
6370Sstevel@tonic-gate 	if (back_tab && outcol > destcol && (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
6380Sstevel@tonic-gate 		j *= (k = strlen(back_tab));
6390Sstevel@tonic-gate 		if ((k += (destcol&7)) > 4)
6400Sstevel@tonic-gate 			j += 8 - (destcol&7);
6410Sstevel@tonic-gate 		else
6420Sstevel@tonic-gate 			j += k;
6430Sstevel@tonic-gate 	} else
6440Sstevel@tonic-gate */
6450Sstevel@tonic-gate 		j = outcol - destcol;
6460Sstevel@tonic-gate 	/*
6470Sstevel@tonic-gate 	 * If we will later need a \n which will turn into a \r\n by
6480Sstevel@tonic-gate 	 * the system or the terminal, then don't bother to try to \r.
6490Sstevel@tonic-gate 	 */
6500Sstevel@tonic-gate 	if ((NONL || !pfast) && outline < destline)
6510Sstevel@tonic-gate 		goto dontcr;
6520Sstevel@tonic-gate 	/*
6530Sstevel@tonic-gate 	 * If the terminal will do a \r\n and there isn't room for it,
6540Sstevel@tonic-gate 	 * then we can't afford a \r.
6550Sstevel@tonic-gate 	 */
6560Sstevel@tonic-gate 	if (!carriage_return && outline >= destline)
6570Sstevel@tonic-gate 		goto dontcr;
6580Sstevel@tonic-gate 	/*
6590Sstevel@tonic-gate 	 * If it will be cheaper, or if we can't back up, then send
6600Sstevel@tonic-gate 	 * a return preliminarily.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	if (j > i + 1 || outcol > destcol && !cursor_left) {
6630Sstevel@tonic-gate 		/*
6640Sstevel@tonic-gate 		 * BUG: this doesn't take the (possibly long) length
6650Sstevel@tonic-gate 		 * of carriage_return into account.
6660Sstevel@tonic-gate 		 */
6670Sstevel@tonic-gate 		if (carriage_return) {
6680Sstevel@tonic-gate 			tputs(carriage_return, 0, plodput);
6690Sstevel@tonic-gate 			outcol = 0;
6700Sstevel@tonic-gate 		} else if (newline) {
6710Sstevel@tonic-gate 			tputs(newline, 0, plodput);
6720Sstevel@tonic-gate 			outline++;
6730Sstevel@tonic-gate 			outcol = 0;
6740Sstevel@tonic-gate 		}
6750Sstevel@tonic-gate 	}
6760Sstevel@tonic-gate dontcr:
6770Sstevel@tonic-gate 	/* Move down, if necessary, until we are at the desired line */
6780Sstevel@tonic-gate 	while (outline < destline) {
6790Sstevel@tonic-gate 		j = destline - outline;
6800Sstevel@tonic-gate 		if (j > costDP && parm_down_cursor) {
6810Sstevel@tonic-gate 			/* Win big on Tek 4025 */
6820Sstevel@tonic-gate 			tputs(tparm(parm_down_cursor, j), j, plodput);
6830Sstevel@tonic-gate 			outline += j;
6840Sstevel@tonic-gate 		}
6850Sstevel@tonic-gate 		else {
6860Sstevel@tonic-gate 			outline++;
6870Sstevel@tonic-gate 			if (cursor_down && pfast)
6880Sstevel@tonic-gate 				tputs(cursor_down, 0, plodput);
6890Sstevel@tonic-gate 			else
690*802Scf46844 				(void) plodput('\n');
6910Sstevel@tonic-gate 		}
6920Sstevel@tonic-gate 		if (plodcnt < 0)
6930Sstevel@tonic-gate 			goto out;
6940Sstevel@tonic-gate 		if (NONL || pfast == 0)
6950Sstevel@tonic-gate 			outcol = 0;
6960Sstevel@tonic-gate 	}
6970Sstevel@tonic-gate 	if (back_tab)
6980Sstevel@tonic-gate 		k = strlen(back_tab);	/* should probably be cost(back_tab) and moved out */
6990Sstevel@tonic-gate 	/* Move left, if necessary, to desired column */
7000Sstevel@tonic-gate 	while (outcol > destcol) {
7010Sstevel@tonic-gate 		if (plodcnt < 0)
7020Sstevel@tonic-gate 			goto out;
7030Sstevel@tonic-gate 		if (back_tab && !insmode && outcol - destcol > 4+k) {
7040Sstevel@tonic-gate 			tputs(back_tab, 0, plodput);
7050Sstevel@tonic-gate 			outcol--;
7060Sstevel@tonic-gate 			if (value(vi_HARDTABS))
7070Sstevel@tonic-gate 				outcol -= outcol % value(vi_HARDTABS); /* outcol &= ~7; */
7080Sstevel@tonic-gate 			continue;
7090Sstevel@tonic-gate 		}
7100Sstevel@tonic-gate 		j = outcol - destcol;
7110Sstevel@tonic-gate 		if (j > costLP && parm_left_cursor) {
7120Sstevel@tonic-gate 			tputs(tparm(parm_left_cursor, j), j, plodput);
7130Sstevel@tonic-gate 			outcol -= j;
7140Sstevel@tonic-gate 		}
7150Sstevel@tonic-gate 		else {
7160Sstevel@tonic-gate 			outcol--;
7170Sstevel@tonic-gate 			tputs(cursor_left, 0, plodput);
7180Sstevel@tonic-gate 		}
7190Sstevel@tonic-gate 	}
7200Sstevel@tonic-gate 	/* Move up, if necessary, to desired row */
7210Sstevel@tonic-gate 	while (outline > destline) {
7220Sstevel@tonic-gate 		j = outline - destline;
7230Sstevel@tonic-gate 		if (parm_up_cursor && j > 1) {
7240Sstevel@tonic-gate 			/* Win big on Tek 4025 */
7250Sstevel@tonic-gate 			tputs(tparm(parm_up_cursor, j), j, plodput);
7260Sstevel@tonic-gate 			outline -= j;
7270Sstevel@tonic-gate 		}
7280Sstevel@tonic-gate 		else {
7290Sstevel@tonic-gate 			outline--;
7300Sstevel@tonic-gate 			tputs(cursor_up, 0, plodput);
7310Sstevel@tonic-gate 		}
7320Sstevel@tonic-gate 		if (plodcnt < 0)
7330Sstevel@tonic-gate 			goto out;
7340Sstevel@tonic-gate 	}
7350Sstevel@tonic-gate 	/*
7360Sstevel@tonic-gate 	 * Now move to the right, if necessary.  We first tab to
7370Sstevel@tonic-gate 	 * as close as we can get.
7380Sstevel@tonic-gate 	 */
7390Sstevel@tonic-gate 	if (value(vi_HARDTABS) && tab && !insmode && destcol - outcol > 1) {
7400Sstevel@tonic-gate 		/* tab to right as far as possible without passing col */
7410Sstevel@tonic-gate 		for (;;) {
7420Sstevel@tonic-gate 			i = tabcol(outcol, value(vi_HARDTABS));
7430Sstevel@tonic-gate 			if (i > destcol)
7440Sstevel@tonic-gate 				break;
7450Sstevel@tonic-gate 			if (tab)
7460Sstevel@tonic-gate 				tputs(tab, 0, plodput);
7470Sstevel@tonic-gate 			else
748*802Scf46844 				(void) plodput('\t');
7490Sstevel@tonic-gate 			outcol = i;
7500Sstevel@tonic-gate 		}
7510Sstevel@tonic-gate 		/* consider another tab and then some backspaces */
7520Sstevel@tonic-gate 		if (destcol - outcol > 4 && i < columns && cursor_left) {
7530Sstevel@tonic-gate 			tputs(tab, 0, plodput);
7540Sstevel@tonic-gate 			outcol = i;
7550Sstevel@tonic-gate 			/*
7560Sstevel@tonic-gate 			 * Back up.  Don't worry about parm_left_cursor because
7570Sstevel@tonic-gate 			 * it's never more than 4 spaces anyway.
7580Sstevel@tonic-gate 			 */
7590Sstevel@tonic-gate 			while (outcol > destcol) {
7600Sstevel@tonic-gate 				outcol--;
7610Sstevel@tonic-gate 				tputs(cursor_left, 0, plodput);
7620Sstevel@tonic-gate 			}
7630Sstevel@tonic-gate 		}
7640Sstevel@tonic-gate 	}
7650Sstevel@tonic-gate 	/*
7660Sstevel@tonic-gate 	 * We've tabbed as much as possible.  If we still need to go
7670Sstevel@tonic-gate 	 * further (not exact or can't tab) space over.  This is a
7680Sstevel@tonic-gate 	 * very common case when moving to the right with space.
7690Sstevel@tonic-gate 	 */
7700Sstevel@tonic-gate 	while (outcol < destcol) {
7710Sstevel@tonic-gate 		j = destcol - outcol;
7720Sstevel@tonic-gate 		if (j > costRP && parm_right_cursor) {
7730Sstevel@tonic-gate 			/*
7740Sstevel@tonic-gate 			 * This probably happens rarely, if at all.
7750Sstevel@tonic-gate 			 * It seems mainly useful for ANSI terminals
7760Sstevel@tonic-gate 			 * with no hardware tabs, and I don't know
7770Sstevel@tonic-gate 			 * of any such terminal at the moment.
7780Sstevel@tonic-gate 			 */
7790Sstevel@tonic-gate 			tputs(tparm(parm_right_cursor, j), j, plodput);
7800Sstevel@tonic-gate 			outcol += j;
7810Sstevel@tonic-gate 		}
7820Sstevel@tonic-gate 		else {
7830Sstevel@tonic-gate 			/*
7840Sstevel@tonic-gate 			 * move one char to the right.  We don't use right
7850Sstevel@tonic-gate 			 * because it's better to just print the char we are
7860Sstevel@tonic-gate 			 * moving over.  There are various exceptions, however.
7870Sstevel@tonic-gate 			 * If !inopen, vtube contains garbage.  If the char is
7880Sstevel@tonic-gate 			 * a null or a tab we want to print a space.  Other
7890Sstevel@tonic-gate 			 * random chars we use space for instead, too.
7900Sstevel@tonic-gate 			 */
791*802Scf46844 			wchar_t wchar;
792*802Scf46844 			int length, scrlength;
7930Sstevel@tonic-gate 			unsigned char multic[MB_LEN_MAX];
7940Sstevel@tonic-gate 
7950Sstevel@tonic-gate 			if (!inopen || vtube[outline]==NULL ||
7960Sstevel@tonic-gate 				(wchar=vtube[outline][outcol]) < ' ')
7970Sstevel@tonic-gate 				wchar = ' ';
7980Sstevel@tonic-gate 			if((int)(wchar & QUOTE))	/* no sign extension on 3B */
7990Sstevel@tonic-gate 				wchar = ' ';
8000Sstevel@tonic-gate 			length = wctomb((char *)multic, wchar);
8010Sstevel@tonic-gate 			if ((scrlength = wcwidth(wchar)) < 0)
8020Sstevel@tonic-gate 				scrlength = 0;
8030Sstevel@tonic-gate 			/* assume multibyte terminals have cursor_right */
8040Sstevel@tonic-gate 			if (insmode && cursor_right || length > 1 || wchar == FILLER) {
8050Sstevel@tonic-gate 				int diff = destcol - outcol;
8060Sstevel@tonic-gate 				j = (wchar == FILLER ? 1 : scrlength > diff ? diff : scrlength);
8070Sstevel@tonic-gate 				while(j--) {
8080Sstevel@tonic-gate 					outcol++;
8090Sstevel@tonic-gate 					tputs(cursor_right, 0, plodput);
8100Sstevel@tonic-gate 				}
8110Sstevel@tonic-gate 			} else {
812*802Scf46844 				(void) plodput((char)multic[0]);
8130Sstevel@tonic-gate 				outcol++;
8140Sstevel@tonic-gate 			}
8150Sstevel@tonic-gate 		}
8160Sstevel@tonic-gate 		if (plodcnt < 0)
8170Sstevel@tonic-gate 			goto out;
8180Sstevel@tonic-gate 	}
8190Sstevel@tonic-gate out:
8200Sstevel@tonic-gate 	if(plodflg) {
8210Sstevel@tonic-gate 		outcol = soutcol;
8220Sstevel@tonic-gate 		outline = soutline;
8230Sstevel@tonic-gate 	}
8240Sstevel@tonic-gate 	return(plodcnt);
8250Sstevel@tonic-gate }
8260Sstevel@tonic-gate 
8270Sstevel@tonic-gate /*
8280Sstevel@tonic-gate  * An input line arrived.
8290Sstevel@tonic-gate  * Calculate new (approximate) screen line position.
8300Sstevel@tonic-gate  * Approximate because kill character echoes newline with
8310Sstevel@tonic-gate  * no feedback and also because of long input lines.
8320Sstevel@tonic-gate  */
833*802Scf46844 void
noteinp(void)834*802Scf46844 noteinp(void)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 
8370Sstevel@tonic-gate 	outline++;
8380Sstevel@tonic-gate 	if (outline > lines - 1)
8390Sstevel@tonic-gate 		outline = lines - 1;
8400Sstevel@tonic-gate 	destline = outline;
8410Sstevel@tonic-gate 	destcol = outcol = 0;
8420Sstevel@tonic-gate }
8430Sstevel@tonic-gate 
8440Sstevel@tonic-gate /*
8450Sstevel@tonic-gate  * Something weird just happened and we
8460Sstevel@tonic-gate  * lost track of what's happening out there.
8470Sstevel@tonic-gate  * Since we can't, in general, read where we are
8480Sstevel@tonic-gate  * we just reset to some known state.
8490Sstevel@tonic-gate  * On cursor addressable terminals setting to unknown
8500Sstevel@tonic-gate  * will force a cursor address soon.
8510Sstevel@tonic-gate  */
852*802Scf46844 void
termreset(void)853*802Scf46844 termreset(void)
8540Sstevel@tonic-gate {
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	endim();
8570Sstevel@tonic-gate 	if (enter_ca_mode)
858*802Scf46844 		putpad((unsigned char *)enter_ca_mode);
8590Sstevel@tonic-gate 	destcol = 0;
8600Sstevel@tonic-gate 	destline = lines - 1;
8610Sstevel@tonic-gate 	if (cursor_address) {
8620Sstevel@tonic-gate 		outcol = UKCOL;
8630Sstevel@tonic-gate 		outline = UKCOL;
8640Sstevel@tonic-gate 	} else {
8650Sstevel@tonic-gate 		outcol = destcol;
8660Sstevel@tonic-gate 		outline = destline;
8670Sstevel@tonic-gate 	}
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate  * Low level buffering, with the ability to drain
8720Sstevel@tonic-gate  * buffered output without printing it.
8730Sstevel@tonic-gate  */
8740Sstevel@tonic-gate unsigned char	*obp = obuf;
8750Sstevel@tonic-gate 
876*802Scf46844 void
draino(void)877*802Scf46844 draino(void)
8780Sstevel@tonic-gate {
8790Sstevel@tonic-gate 
8800Sstevel@tonic-gate 	obp = obuf;
8810Sstevel@tonic-gate }
8820Sstevel@tonic-gate 
883*802Scf46844 void
flusho(void)884*802Scf46844 flusho(void)
8850Sstevel@tonic-gate {
8860Sstevel@tonic-gate 	if (obp != obuf) {
8870Sstevel@tonic-gate 		write(1, obuf, obp - obuf);
8880Sstevel@tonic-gate #ifdef TRACE
8890Sstevel@tonic-gate 		if (trace)
8900Sstevel@tonic-gate 			fwrite(obuf, 1, obp-obuf, trace);
8910Sstevel@tonic-gate #endif
8920Sstevel@tonic-gate 		obp = obuf;
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate }
8950Sstevel@tonic-gate 
896*802Scf46844 void
putnl(void)897*802Scf46844 putnl(void)
8980Sstevel@tonic-gate {
8990Sstevel@tonic-gate 
9000Sstevel@tonic-gate 	putchar('\n');
9010Sstevel@tonic-gate }
9020Sstevel@tonic-gate 
903*802Scf46844 void
putS(unsigned char * cp)904*802Scf46844 putS(unsigned char *cp)
9050Sstevel@tonic-gate {
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate 	if (cp == NULL)
9080Sstevel@tonic-gate 		return;
9090Sstevel@tonic-gate 	while (*cp)
910*802Scf46844 		(void) putch(*cp++);
9110Sstevel@tonic-gate }
9120Sstevel@tonic-gate 
9130Sstevel@tonic-gate int
putch(char c)9140Sstevel@tonic-gate putch(char c)
9150Sstevel@tonic-gate {
9160Sstevel@tonic-gate 
9170Sstevel@tonic-gate #ifdef OLD3BTTY
9180Sstevel@tonic-gate 	if(c == '\n')	/* Fake "\n\r" for '\n' til fix in 3B firmware */
919*802Scf46844 		(void) putch('\r'); /* vi does "stty -icanon" => -onlcr !! */
9200Sstevel@tonic-gate #endif
9210Sstevel@tonic-gate 	*obp++ = c;
9220Sstevel@tonic-gate 	if (obp >= &obuf[sizeof obuf])
9230Sstevel@tonic-gate 		flusho();
924*802Scf46844 	return (0);
9250Sstevel@tonic-gate }
9260Sstevel@tonic-gate 
9270Sstevel@tonic-gate /*
9280Sstevel@tonic-gate  * Miscellaneous routines related to output.
9290Sstevel@tonic-gate  */
9300Sstevel@tonic-gate 
9310Sstevel@tonic-gate /*
9320Sstevel@tonic-gate  * Put with padding
9330Sstevel@tonic-gate  */
934*802Scf46844 void
putpad(unsigned char * cp)935*802Scf46844 putpad(unsigned char *cp)
9360Sstevel@tonic-gate {
9370Sstevel@tonic-gate 
9380Sstevel@tonic-gate 	flush();
9390Sstevel@tonic-gate 	tputs((char *)cp, 0, putch);
9400Sstevel@tonic-gate }
9410Sstevel@tonic-gate 
9420Sstevel@tonic-gate /*
9430Sstevel@tonic-gate  * Set output through normal command mode routine.
9440Sstevel@tonic-gate  */
945*802Scf46844 void
setoutt(void)946*802Scf46844 setoutt(void)
9470Sstevel@tonic-gate {
9480Sstevel@tonic-gate 
9490Sstevel@tonic-gate 	Outchar = termchar;
9500Sstevel@tonic-gate }
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate /*
9530Sstevel@tonic-gate  * Printf (temporarily) in list mode.
9540Sstevel@tonic-gate  */
9550Sstevel@tonic-gate /*VARARGS2*/
956*802Scf46844 void
lprintf(unsigned char * cp,unsigned char * dp,...)957*802Scf46844 lprintf(unsigned char *cp, unsigned char *dp, ...)
9580Sstevel@tonic-gate {
959*802Scf46844 	int (*P)();
9600Sstevel@tonic-gate 
9610Sstevel@tonic-gate 	P = setlist(1);
9620Sstevel@tonic-gate #ifdef PRESUNEUC
963*802Scf46844 	viprintf(cp, dp);
9640Sstevel@tonic-gate #else
965*802Scf46844 	viprintf((char *)cp, (char *)dp);
9660Sstevel@tonic-gate #endif /* PRESUNEUC */
9670Sstevel@tonic-gate 	Putchar = P;
9680Sstevel@tonic-gate }
9690Sstevel@tonic-gate 
9700Sstevel@tonic-gate /*
9710Sstevel@tonic-gate  * Newline + flush.
9720Sstevel@tonic-gate  */
973*802Scf46844 void
putNFL()9740Sstevel@tonic-gate putNFL()
9750Sstevel@tonic-gate {
9760Sstevel@tonic-gate 
9770Sstevel@tonic-gate 	putnl();
9780Sstevel@tonic-gate 	flush();
9790Sstevel@tonic-gate }
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate /*
9820Sstevel@tonic-gate  * Try to start -nl mode.
9830Sstevel@tonic-gate  */
984*802Scf46844 void
pstart(void)985*802Scf46844 pstart(void)
9860Sstevel@tonic-gate {
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate 	if (NONL)
9890Sstevel@tonic-gate 		return;
9900Sstevel@tonic-gate  	if (!value(vi_OPTIMIZE))
9910Sstevel@tonic-gate 		return;
9920Sstevel@tonic-gate 	if (ruptible == 0 || pfast)
9930Sstevel@tonic-gate 		return;
9940Sstevel@tonic-gate 	fgoto();
9950Sstevel@tonic-gate 	flusho();
9960Sstevel@tonic-gate 	pfast = 1;
9970Sstevel@tonic-gate 	normtty++;
9980Sstevel@tonic-gate 	tty = normf;
9990Sstevel@tonic-gate 	tty.c_oflag &= ~(ONLCR|TAB3);
10000Sstevel@tonic-gate 	tty.c_lflag &= ~ECHO;
10010Sstevel@tonic-gate 	saveterm();
10020Sstevel@tonic-gate 	sTTY(2);
10030Sstevel@tonic-gate }
10040Sstevel@tonic-gate 
10050Sstevel@tonic-gate /*
10060Sstevel@tonic-gate  * Stop -nl mode.
10070Sstevel@tonic-gate  */
1008*802Scf46844 void
pstop(void)1009*802Scf46844 pstop(void)
10100Sstevel@tonic-gate {
10110Sstevel@tonic-gate 
10120Sstevel@tonic-gate 	if (inopen)
10130Sstevel@tonic-gate 		return;
10140Sstevel@tonic-gate 	phadnl = 0;
10150Sstevel@tonic-gate 	linp = linb;
10160Sstevel@tonic-gate 	draino();
10170Sstevel@tonic-gate 	normal(normf);
10180Sstevel@tonic-gate 	pfast &= ~1;
10190Sstevel@tonic-gate }
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /*
10220Sstevel@tonic-gate  * Prep tty for open mode.
10230Sstevel@tonic-gate  */
10240Sstevel@tonic-gate ttymode
ostart()10250Sstevel@tonic-gate ostart()
10260Sstevel@tonic-gate {
10270Sstevel@tonic-gate 	ttymode f;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate 	/*
10300Sstevel@tonic-gate 	if (!intty)
10310Sstevel@tonic-gate 		error("Open and visual must be used interactively");
10320Sstevel@tonic-gate 	*/
1033*802Scf46844 	(void) gTTY(2);
10340Sstevel@tonic-gate 	normtty++;
10350Sstevel@tonic-gate 	f = tty;
10360Sstevel@tonic-gate 	tty = normf;
10370Sstevel@tonic-gate 	tty.c_iflag &= ~ICRNL;
10380Sstevel@tonic-gate 	tty.c_lflag &= ~(ECHO|ICANON);
10390Sstevel@tonic-gate 	tty.c_oflag &= ~(TAB3|ONLCR);
10400Sstevel@tonic-gate 	tty.c_cc[VMIN] = 1;
10410Sstevel@tonic-gate 	tty.c_cc[VTIME] = 1;
10420Sstevel@tonic-gate 	ttcharoff();
10430Sstevel@tonic-gate 	sTTY(2);
10440Sstevel@tonic-gate 	tostart();
10450Sstevel@tonic-gate 	pfast |= 2;
10460Sstevel@tonic-gate 	saveterm();
10470Sstevel@tonic-gate 	return (f);
10480Sstevel@tonic-gate }
10490Sstevel@tonic-gate 
10500Sstevel@tonic-gate /* actions associated with putting the terminal in open mode */
1051*802Scf46844 void
tostart(void)1052*802Scf46844 tostart(void)
10530Sstevel@tonic-gate {
1054*802Scf46844 	putpad((unsigned char *)cursor_visible);
1055*802Scf46844 	putpad((unsigned char *)keypad_xmit);
10560Sstevel@tonic-gate 	if (!value(vi_MESG)) {
10570Sstevel@tonic-gate 		if (ttynbuf[0] == 0) {
1058*802Scf46844 			char *tn;
10590Sstevel@tonic-gate 			if ((tn=ttyname(2)) == NULL &&
10600Sstevel@tonic-gate 			    (tn=ttyname(1)) == NULL &&
10610Sstevel@tonic-gate 			    (tn=ttyname(0)) == NULL)
10620Sstevel@tonic-gate 				ttynbuf[0] = 1;
10630Sstevel@tonic-gate 			else
10640Sstevel@tonic-gate 				strcpy(ttynbuf, tn);
10650Sstevel@tonic-gate 		}
10660Sstevel@tonic-gate 		if (ttynbuf[0] != 1) {
10670Sstevel@tonic-gate 			struct stat64 sbuf;
10680Sstevel@tonic-gate 			stat64((char *)ttynbuf, &sbuf);
10690Sstevel@tonic-gate 			ttymesg = FMODE(sbuf) & 0777;
10700Sstevel@tonic-gate 			chmod((char *)ttynbuf, 0600);
10710Sstevel@tonic-gate 		}
10720Sstevel@tonic-gate 	}
10730Sstevel@tonic-gate }
10740Sstevel@tonic-gate 
10750Sstevel@tonic-gate /*
10760Sstevel@tonic-gate  * Turn off start/stop chars if they aren't the default ^S/^Q.
10770Sstevel@tonic-gate  * This is so people who make esc their start/stop don't lose.
10780Sstevel@tonic-gate  * We always turn off quit since datamedias send ^\ for their
10790Sstevel@tonic-gate  * right arrow key.
10800Sstevel@tonic-gate  */
10810Sstevel@tonic-gate 
1082*802Scf46844 void
ttcharoff(void)1083*802Scf46844 ttcharoff(void)
10840Sstevel@tonic-gate {
10850Sstevel@tonic-gate 	/*
10860Sstevel@tonic-gate 	 * use 200 instead of 377 because 377 is y-umlaut
10870Sstevel@tonic-gate 	 * in ISO 8859/1
10880Sstevel@tonic-gate 	 */
10890Sstevel@tonic-gate 	tty.c_cc[VQUIT] = termiosflag ? _POSIX_VDISABLE : '\200';
10900Sstevel@tonic-gate 	if (tty.c_cc[VSTART] != CTRL('q'))
10910Sstevel@tonic-gate 		tty.c_cc[VSTART] = _POSIX_VDISABLE;
10920Sstevel@tonic-gate 	if (tty.c_cc[VSTOP] != CTRL('s'))
10930Sstevel@tonic-gate 		tty.c_cc[VSTOP] = _POSIX_VDISABLE;
10940Sstevel@tonic-gate 	/* We will read ^z and suspend ourselves via kill */
10950Sstevel@tonic-gate 	tty.c_cc[VSUSP] = _POSIX_VDISABLE;
10960Sstevel@tonic-gate 	tty.c_cc[VDSUSP] = _POSIX_VDISABLE;
10970Sstevel@tonic-gate 	tty.c_cc[VREPRINT] = _POSIX_VDISABLE;
10980Sstevel@tonic-gate 	tty.c_cc[VDISCARD] = _POSIX_VDISABLE;
10990Sstevel@tonic-gate 	tty.c_cc[VWERASE] = _POSIX_VDISABLE;
11000Sstevel@tonic-gate 	tty.c_cc[VLNEXT] = _POSIX_VDISABLE;
11010Sstevel@tonic-gate }
11020Sstevel@tonic-gate 
11030Sstevel@tonic-gate /*
11040Sstevel@tonic-gate  * Stop open, restoring tty modes.
11050Sstevel@tonic-gate  */
1106*802Scf46844 void
ostop(ttymode f)1107*802Scf46844 ostop(ttymode f)
11080Sstevel@tonic-gate {
11090Sstevel@tonic-gate 
11100Sstevel@tonic-gate 	pfast = (f.c_oflag & ONLCR) == 0;
11110Sstevel@tonic-gate 	termreset(), fgoto(), flusho();
11120Sstevel@tonic-gate 	normal(f);
11130Sstevel@tonic-gate 	tostop();
11140Sstevel@tonic-gate }
11150Sstevel@tonic-gate 
11160Sstevel@tonic-gate /* Actions associated with putting the terminal in the right mode. */
1117*802Scf46844 void
tostop(void)1118*802Scf46844 tostop(void)
11190Sstevel@tonic-gate {
1120*802Scf46844 	putpad((unsigned char *)clr_eos);
1121*802Scf46844 	putpad((unsigned char *)cursor_normal);
1122*802Scf46844 	putpad((unsigned char *)keypad_local);
11230Sstevel@tonic-gate 	if (!value(vi_MESG) && ttynbuf[0]>1)
11240Sstevel@tonic-gate 		chmod((char *)ttynbuf, ttymesg);
11250Sstevel@tonic-gate }
11260Sstevel@tonic-gate 
11270Sstevel@tonic-gate #ifndef CBREAK
11280Sstevel@tonic-gate /*
11290Sstevel@tonic-gate  * Into cooked mode for interruptibility.
11300Sstevel@tonic-gate  */
vcook()11310Sstevel@tonic-gate vcook()
11320Sstevel@tonic-gate {
11330Sstevel@tonic-gate 
11340Sstevel@tonic-gate 	tty.sg_flags &= ~RAW;
11350Sstevel@tonic-gate 	sTTY(2);
11360Sstevel@tonic-gate }
11370Sstevel@tonic-gate 
11380Sstevel@tonic-gate /*
11390Sstevel@tonic-gate  * Back into raw mode.
11400Sstevel@tonic-gate  */
vraw()11410Sstevel@tonic-gate vraw()
11420Sstevel@tonic-gate {
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	tty.sg_flags |= RAW;
11450Sstevel@tonic-gate 	sTTY(2);
11460Sstevel@tonic-gate }
11470Sstevel@tonic-gate #endif
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * Restore flags to normal state f.
11510Sstevel@tonic-gate  */
1152*802Scf46844 void
normal(ttymode f)1153*802Scf46844 normal(ttymode f)
11540Sstevel@tonic-gate {
11550Sstevel@tonic-gate 
11560Sstevel@tonic-gate 	if (normtty > 0) {
11570Sstevel@tonic-gate 		setty(f);
11580Sstevel@tonic-gate 		normtty--;
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate }
11610Sstevel@tonic-gate 
11620Sstevel@tonic-gate /*
11630Sstevel@tonic-gate  * Straight set of flags to state f.
11640Sstevel@tonic-gate  */
11650Sstevel@tonic-gate ttymode
setty(f)11660Sstevel@tonic-gate setty(f)
11670Sstevel@tonic-gate 	ttymode f;
11680Sstevel@tonic-gate {
11690Sstevel@tonic-gate 	int isnorm = 0;
11700Sstevel@tonic-gate 	ttymode ot;
11710Sstevel@tonic-gate 	ot = tty;
11720Sstevel@tonic-gate 
11730Sstevel@tonic-gate 	if (tty.c_lflag & ICANON)
11740Sstevel@tonic-gate 		ttcharoff();
11750Sstevel@tonic-gate 	else
11760Sstevel@tonic-gate 		isnorm = 1;
11770Sstevel@tonic-gate 	tty = f;
11780Sstevel@tonic-gate 	sTTY(2);
11790Sstevel@tonic-gate 	if (!isnorm)
11800Sstevel@tonic-gate 		saveterm();
11810Sstevel@tonic-gate 	return (ot);
11820Sstevel@tonic-gate }
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate static struct termio termio;
11850Sstevel@tonic-gate 
1186*802Scf46844 int
gTTY(int i)1187*802Scf46844 gTTY(int i)
11880Sstevel@tonic-gate {
11890Sstevel@tonic-gate 	if(termiosflag < 0) {
11900Sstevel@tonic-gate 		if(ioctl(i, TCGETS, &tty) == 0)
11910Sstevel@tonic-gate 			termiosflag = 1;
11920Sstevel@tonic-gate 		else  {
11930Sstevel@tonic-gate 			termiosflag = 0;
11940Sstevel@tonic-gate 			if(ioctl(i, TCGETA, &termio) < 0)
1195*802Scf46844 				return (-1);
11960Sstevel@tonic-gate 			tty.c_iflag = termio.c_iflag;
11970Sstevel@tonic-gate 			tty.c_oflag = termio.c_oflag;
11980Sstevel@tonic-gate 			tty.c_cflag = termio.c_cflag;
11990Sstevel@tonic-gate 			tty.c_lflag = termio.c_lflag;
12000Sstevel@tonic-gate 			for(i = 0; i < NCC; i++)
12010Sstevel@tonic-gate 				tty.c_cc[i] = termio.c_cc[i];
12020Sstevel@tonic-gate 		}
1203*802Scf46844 		return (0);
12040Sstevel@tonic-gate 	}
12050Sstevel@tonic-gate 	if(termiosflag)
1206*802Scf46844 		return (ioctl(i, TCGETS, &tty));
12070Sstevel@tonic-gate 	if(ioctl(i, TCGETA, &termio) < 0)
1208*802Scf46844 		return (-1);
12090Sstevel@tonic-gate 	tty.c_iflag = termio.c_iflag;
12100Sstevel@tonic-gate 	tty.c_oflag = termio.c_oflag;
12110Sstevel@tonic-gate 	tty.c_cflag = termio.c_cflag;
12120Sstevel@tonic-gate 	tty.c_lflag = termio.c_lflag;
12130Sstevel@tonic-gate 	for(i = 0; i < NCC; i++)
12140Sstevel@tonic-gate 		tty.c_cc[i] = termio.c_cc[i];
1215*802Scf46844 	return (0);
12160Sstevel@tonic-gate }
12170Sstevel@tonic-gate 
12180Sstevel@tonic-gate /*
12190Sstevel@tonic-gate  * sTTY: set the tty modes on file descriptor i to be what's
12200Sstevel@tonic-gate  * currently in global "tty".  (Also use nttyc if needed.)
12210Sstevel@tonic-gate  */
1222*802Scf46844 void
sTTY(int i)1223*802Scf46844 sTTY(int i)
12240Sstevel@tonic-gate {
12250Sstevel@tonic-gate 	int j;
12260Sstevel@tonic-gate 	if(termiosflag)
12270Sstevel@tonic-gate 		ioctl(i, TCSETSW, &tty);
12280Sstevel@tonic-gate 	else {
12290Sstevel@tonic-gate 		termio.c_iflag = tty.c_iflag;
12300Sstevel@tonic-gate 		termio.c_oflag = tty.c_oflag;
12310Sstevel@tonic-gate 		termio.c_cflag = tty.c_cflag;
12320Sstevel@tonic-gate 		termio.c_lflag = tty.c_lflag;
12330Sstevel@tonic-gate 		for(j = 0; j < NCC; j++)
12340Sstevel@tonic-gate 			termio.c_cc[j] = tty.c_cc[j];
12350Sstevel@tonic-gate 		ioctl(i, TCSETAW, &termio);
12360Sstevel@tonic-gate 	}
12370Sstevel@tonic-gate }
12380Sstevel@tonic-gate 
12390Sstevel@tonic-gate /*
12400Sstevel@tonic-gate  * Print newline, or blank if in open/visual
12410Sstevel@tonic-gate  */
1242*802Scf46844 void
noonl(void)1243*802Scf46844 noonl(void)
12440Sstevel@tonic-gate {
12450Sstevel@tonic-gate 
12460Sstevel@tonic-gate 	putchar(Outchar != termchar ? ' ' : '\n');
12470Sstevel@tonic-gate }
1248