xref: /illumos-gate/usr/src/cmd/vi/port/ex_put.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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 /*
23*f6db9f27Scf46844  * 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 #include "ex_vis.h"
367c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
377c478bd9Sstevel@tonic-gate #include <wctype.h>
387c478bd9Sstevel@tonic-gate /* Undef putchar/getchar if they're defined. */
397c478bd9Sstevel@tonic-gate #ifdef putchar
407c478bd9Sstevel@tonic-gate #	undef putchar
417c478bd9Sstevel@tonic-gate #endif
427c478bd9Sstevel@tonic-gate #ifdef getchar
437c478bd9Sstevel@tonic-gate #	undef getchar
447c478bd9Sstevel@tonic-gate #endif
457c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate /*
487c478bd9Sstevel@tonic-gate  * Terminal driving and line formatting routines.
497c478bd9Sstevel@tonic-gate  * Basic motion optimizations are done here as well
507c478bd9Sstevel@tonic-gate  * as formatting of lines (printing of control characters,
517c478bd9Sstevel@tonic-gate  * line numbering and the like).
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /*
557c478bd9Sstevel@tonic-gate  * The routines outchar, putchar and pline are actually
567c478bd9Sstevel@tonic-gate  * variables, and these variables point at the current definitions
577c478bd9Sstevel@tonic-gate  * of the routines.  See the routine setflav.
587c478bd9Sstevel@tonic-gate  * We sometimes make outchar be routines which catch the characters
597c478bd9Sstevel@tonic-gate  * to be printed, e.g. if we want to see how long a line is.
607c478bd9Sstevel@tonic-gate  * During open/visual, outchar and putchar will be set to
617c478bd9Sstevel@tonic-gate  * routines in the file ex_vput.c (vputchar, vinschar, etc.).
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate int	(*Outchar)() = termchar;
647c478bd9Sstevel@tonic-gate int	(*Putchar)() = normchar;
657c478bd9Sstevel@tonic-gate int	(*Pline)() = normline;
667c478bd9Sstevel@tonic-gate static unsigned char multic[MULTI_BYTE_MAX];
677c478bd9Sstevel@tonic-gate bool putoctal; /* flag to say if byte should be printed as octal */
687c478bd9Sstevel@tonic-gate int termiosflag = -1; /* flag for using termios ioctl
697c478bd9Sstevel@tonic-gate 			      * structure */
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate int (*
727c478bd9Sstevel@tonic-gate setlist(t))()
737c478bd9Sstevel@tonic-gate 	bool t;
747c478bd9Sstevel@tonic-gate {
75*f6db9f27Scf46844 	int (*P)();
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	listf = t;
787c478bd9Sstevel@tonic-gate 	P = Putchar;
797c478bd9Sstevel@tonic-gate 	Putchar = t ? listchar : normchar;
807c478bd9Sstevel@tonic-gate 	return (P);
817c478bd9Sstevel@tonic-gate }
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate int (*
847c478bd9Sstevel@tonic-gate setnumb(t))()
857c478bd9Sstevel@tonic-gate 	bool t;
867c478bd9Sstevel@tonic-gate {
87*f6db9f27Scf46844 	int (*P)();
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 	numberf = t;
907c478bd9Sstevel@tonic-gate 	P = Pline;
91*f6db9f27Scf46844 	Pline = t ? (int (*)())numbline : normline;
927c478bd9Sstevel@tonic-gate 	return (P);
937c478bd9Sstevel@tonic-gate }
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate /*
967c478bd9Sstevel@tonic-gate  * Format c for list mode; leave things in common
977c478bd9Sstevel@tonic-gate  * with normal print mode to be done by normchar.
987c478bd9Sstevel@tonic-gate  */
99*f6db9f27Scf46844 int
listchar(wchar_t c)100*f6db9f27Scf46844 listchar(wchar_t c)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate 	c &= (int)(TRIM|QUOTE);
1047c478bd9Sstevel@tonic-gate 	switch (c) {
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	case '\t':
1077c478bd9Sstevel@tonic-gate 	case '\b':
1087c478bd9Sstevel@tonic-gate 		outchar('^');
1097c478bd9Sstevel@tonic-gate 		c = ctlof(c);
1107c478bd9Sstevel@tonic-gate 		break;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	case '\n':
1137c478bd9Sstevel@tonic-gate 		break;
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate 	case (int)('\n' | QUOTE):
1167c478bd9Sstevel@tonic-gate 		outchar('$');
1177c478bd9Sstevel@tonic-gate 		break;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	default:
1207c478bd9Sstevel@tonic-gate 		if((int)(c & QUOTE))
1217c478bd9Sstevel@tonic-gate 			break;
1227c478bd9Sstevel@tonic-gate 		if (c < ' ' && c != '\n' || c == DELETE)
1237c478bd9Sstevel@tonic-gate 			outchar('^'), c = ctlof(c);
1247c478bd9Sstevel@tonic-gate 	}
125*f6db9f27Scf46844 	(void) normchar(c);
126*f6db9f27Scf46844 	return (0);
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Format c for printing.  Handle funnies of upper case terminals
1317c478bd9Sstevel@tonic-gate  * and hazeltines which don't have ~.
1327c478bd9Sstevel@tonic-gate  */
133*f6db9f27Scf46844 int
normchar(wchar_t c)134*f6db9f27Scf46844 normchar(wchar_t c)
1357c478bd9Sstevel@tonic-gate {
136*f6db9f27Scf46844 	char *colp;
1377c478bd9Sstevel@tonic-gate 
1387c478bd9Sstevel@tonic-gate 	c &= (int)(TRIM|QUOTE);
1397c478bd9Sstevel@tonic-gate 	if (c == '~' && tilde_glitch) {
140*f6db9f27Scf46844 		(void) normchar('\\');
1417c478bd9Sstevel@tonic-gate 		c = '^';
1427c478bd9Sstevel@tonic-gate 	}
1437c478bd9Sstevel@tonic-gate 	if ((int)(c & QUOTE))
1447c478bd9Sstevel@tonic-gate 		switch (c) {
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 		case (int)(' ' | QUOTE):
1477c478bd9Sstevel@tonic-gate 		case (int)('\b' | QUOTE):
1487c478bd9Sstevel@tonic-gate 			break;
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate 		case (int)QUOTE:
151*f6db9f27Scf46844 			return (0);
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate 		default:
1547c478bd9Sstevel@tonic-gate 			c &= (int)TRIM;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	else if (c < ' ' && (c != '\b' || !over_strike) && c != '\n' && c != '\t' || c == DELETE)
1577c478bd9Sstevel@tonic-gate 		putchar('^'), c = ctlof(c);
1587c478bd9Sstevel@tonic-gate 	else if (c >= 0200 && (putoctal || !iswprint(c))) {
1597c478bd9Sstevel@tonic-gate 		outchar('\\');
1607c478bd9Sstevel@tonic-gate 		outchar(((c >> 6) & 07) + '0');
1617c478bd9Sstevel@tonic-gate 		outchar(((c >> 3) & 07) + '0');
1627c478bd9Sstevel@tonic-gate 		outchar((c & 07) + '0');
163*f6db9f27Scf46844 		return (0);
1647c478bd9Sstevel@tonic-gate 	} else if (UPPERCASE)
1657c478bd9Sstevel@tonic-gate 		if (isupper(c)) {
1667c478bd9Sstevel@tonic-gate 			outchar('\\');
1677c478bd9Sstevel@tonic-gate 			c = tolower(c);
1687c478bd9Sstevel@tonic-gate 		} else {
1697c478bd9Sstevel@tonic-gate 			colp = "({)}!|^~'`";
1707c478bd9Sstevel@tonic-gate 			while (*colp++)
1717c478bd9Sstevel@tonic-gate 				if (c == *colp++) {
1727c478bd9Sstevel@tonic-gate 					outchar('\\');
1737c478bd9Sstevel@tonic-gate 					c = colp[-2];
1747c478bd9Sstevel@tonic-gate 					break;
1757c478bd9Sstevel@tonic-gate 				}
1767c478bd9Sstevel@tonic-gate 		}
1777c478bd9Sstevel@tonic-gate 	outchar(c);
178*f6db9f27Scf46844 	return (0);
1797c478bd9Sstevel@tonic-gate }
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate /*
1827c478bd9Sstevel@tonic-gate  * Print a line with a number.
1837c478bd9Sstevel@tonic-gate  */
184*f6db9f27Scf46844 int
numbline(int i)185*f6db9f27Scf46844 numbline(int i)
1867c478bd9Sstevel@tonic-gate {
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (shudclob)
1897c478bd9Sstevel@tonic-gate 		slobber(' ');
190*f6db9f27Scf46844 	viprintf("%6d  ", i);
191*f6db9f27Scf46844 	(void) normline();
192*f6db9f27Scf46844 	return (0);
1937c478bd9Sstevel@tonic-gate }
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate /*
1967c478bd9Sstevel@tonic-gate  * Normal line output, no numbering.
1977c478bd9Sstevel@tonic-gate  */
198*f6db9f27Scf46844 int
normline(void)199*f6db9f27Scf46844 normline(void)
2007c478bd9Sstevel@tonic-gate {
201*f6db9f27Scf46844 	unsigned char *cp;
202*f6db9f27Scf46844 	int n;
2037c478bd9Sstevel@tonic-gate 	wchar_t wchar;
2047c478bd9Sstevel@tonic-gate 	if (shudclob)
2057c478bd9Sstevel@tonic-gate 		slobber(linebuf[0]);
2067c478bd9Sstevel@tonic-gate 	/* pdp-11 doprnt is not reentrant so can't use "printf" here
2077c478bd9Sstevel@tonic-gate 	   in case we are tracing */
2087c478bd9Sstevel@tonic-gate 	for (cp = linebuf; *cp;)
2097c478bd9Sstevel@tonic-gate 		if((n = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX)) < 0) {
2107c478bd9Sstevel@tonic-gate 			putoctal = 1;
2117c478bd9Sstevel@tonic-gate 			putchar(*cp++);
2127c478bd9Sstevel@tonic-gate 			putoctal = 0;
2137c478bd9Sstevel@tonic-gate 		} else {
2147c478bd9Sstevel@tonic-gate 			cp += n;
2157c478bd9Sstevel@tonic-gate 			putchar(wchar);
2167c478bd9Sstevel@tonic-gate 		}
2177c478bd9Sstevel@tonic-gate 	if (!inopen)
2187c478bd9Sstevel@tonic-gate 		putchar((int)('\n' | QUOTE));
219*f6db9f27Scf46844 	return (0);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate /*
2237c478bd9Sstevel@tonic-gate  * Given c at the beginning of a line, determine whether
2247c478bd9Sstevel@tonic-gate  * the printing of the line will erase or otherwise obliterate
2257c478bd9Sstevel@tonic-gate  * the prompt which was printed before.  If it won't, do it now.
2267c478bd9Sstevel@tonic-gate  */
227*f6db9f27Scf46844 void
slobber(int c)228*f6db9f27Scf46844 slobber(int c)
2297c478bd9Sstevel@tonic-gate {
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	shudclob = 0;
2327c478bd9Sstevel@tonic-gate 	switch (c) {
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	case '\t':
2357c478bd9Sstevel@tonic-gate 		if (Putchar == listchar)
2367c478bd9Sstevel@tonic-gate 			return;
2377c478bd9Sstevel@tonic-gate 		break;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	default:
2407c478bd9Sstevel@tonic-gate 		return;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	case ' ':
2437c478bd9Sstevel@tonic-gate 	case 0:
2447c478bd9Sstevel@tonic-gate 		break;
2457c478bd9Sstevel@tonic-gate 	}
2467c478bd9Sstevel@tonic-gate 	if (over_strike)
2477c478bd9Sstevel@tonic-gate 		return;
2487c478bd9Sstevel@tonic-gate 	flush();
249*f6db9f27Scf46844 	(void) putch(' ');
2507c478bd9Sstevel@tonic-gate 	tputs(cursor_left, 0, putch);
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
2537c478bd9Sstevel@tonic-gate /*
2547c478bd9Sstevel@tonic-gate  * The output buffer is initialized with a useful error
2557c478bd9Sstevel@tonic-gate  * message so we don't have to keep it in data space.
2567c478bd9Sstevel@tonic-gate  */
2577c478bd9Sstevel@tonic-gate static	wchar_t linb[66];
2587c478bd9Sstevel@tonic-gate wchar_t *linp = linb;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate /*
2617c478bd9Sstevel@tonic-gate  * Phadnl records when we have already had a complete line ending with \n.
2627c478bd9Sstevel@tonic-gate  * If another line starts without a flush, and the terminal suggests it,
2637c478bd9Sstevel@tonic-gate  * we switch into -nl mode so that we can send linefeeds to avoid
2647c478bd9Sstevel@tonic-gate  * a lot of spacing.
2657c478bd9Sstevel@tonic-gate  */
2667c478bd9Sstevel@tonic-gate static	bool phadnl;
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate /*
2697c478bd9Sstevel@tonic-gate  * Indirect to current definition of putchar.
2707c478bd9Sstevel@tonic-gate  */
271*f6db9f27Scf46844 int
putchar(int c)2727c478bd9Sstevel@tonic-gate putchar(int c)
2737c478bd9Sstevel@tonic-gate {
274*f6db9f27Scf46844 	return ((*Putchar)((wchar_t)c));
2757c478bd9Sstevel@tonic-gate }
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate /*
2787c478bd9Sstevel@tonic-gate  * Termchar routine for command mode.
2797c478bd9Sstevel@tonic-gate  * Watch for possible switching to -nl mode.
2807c478bd9Sstevel@tonic-gate  * Otherwise flush into next level of buffering when
2817c478bd9Sstevel@tonic-gate  * small buffer fills or at a newline.
2827c478bd9Sstevel@tonic-gate  */
283*f6db9f27Scf46844 int
termchar(wchar_t c)284*f6db9f27Scf46844 termchar(wchar_t c)
2857c478bd9Sstevel@tonic-gate {
2867c478bd9Sstevel@tonic-gate 
2877c478bd9Sstevel@tonic-gate 	if (pfast == 0 && phadnl)
2887c478bd9Sstevel@tonic-gate 		pstart();
2897c478bd9Sstevel@tonic-gate 	if (c == '\n')
2907c478bd9Sstevel@tonic-gate 		phadnl = 1;
2917c478bd9Sstevel@tonic-gate 	else if (linp >= &linb[63])
2927c478bd9Sstevel@tonic-gate 		flush1();
2937c478bd9Sstevel@tonic-gate 	*linp++ = c;
2947c478bd9Sstevel@tonic-gate 	if (linp >= &linb[63]) {
2957c478bd9Sstevel@tonic-gate 		fgoto();
2967c478bd9Sstevel@tonic-gate 		flush1();
2977c478bd9Sstevel@tonic-gate 	}
298*f6db9f27Scf46844 	return (0);
2997c478bd9Sstevel@tonic-gate }
3007c478bd9Sstevel@tonic-gate 
301*f6db9f27Scf46844 void
flush(void)302*f6db9f27Scf46844 flush(void)
3037c478bd9Sstevel@tonic-gate {
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	flush1();
3067c478bd9Sstevel@tonic-gate 	flush2();
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate /*
3107c478bd9Sstevel@tonic-gate  * Flush from small line buffer into output buffer.
3117c478bd9Sstevel@tonic-gate  * Work here is destroying motion into positions, and then
3127c478bd9Sstevel@tonic-gate  * letting fgoto do the optimized motion.
3137c478bd9Sstevel@tonic-gate  */
314*f6db9f27Scf46844 void
flush1(void)315*f6db9f27Scf46844 flush1(void)
3167c478bd9Sstevel@tonic-gate {
317*f6db9f27Scf46844 	wchar_t *lp;
318*f6db9f27Scf46844 	wchar_t c;
3197c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
3207c478bd9Sstevel@tonic-gate 	/* used for multibyte characters split between lines */
321*f6db9f27Scf46844 	int splitcnt = 0;
3227c478bd9Sstevel@tonic-gate #else
3237c478bd9Sstevel@tonic-gate 	/* used for multicolumn character substitution and padding */
324*f6db9f27Scf46844 	int fillercnt = 0;
3257c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
3267c478bd9Sstevel@tonic-gate 	*linp = 0;
3277c478bd9Sstevel@tonic-gate 	lp = linb;
3287c478bd9Sstevel@tonic-gate 	while (*lp)
3297c478bd9Sstevel@tonic-gate 		switch (c = *lp++) {
3307c478bd9Sstevel@tonic-gate 
3317c478bd9Sstevel@tonic-gate 		case '\r':
3327c478bd9Sstevel@tonic-gate 			destline += destcol / columns;
3337c478bd9Sstevel@tonic-gate 			destcol = 0;
3347c478bd9Sstevel@tonic-gate 			continue;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 		case '\b':
3377c478bd9Sstevel@tonic-gate 			if (destcol)
3387c478bd9Sstevel@tonic-gate 				destcol--;
3397c478bd9Sstevel@tonic-gate 			continue;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 		case ' ':
3427c478bd9Sstevel@tonic-gate 			destcol++;
3437c478bd9Sstevel@tonic-gate 			continue;
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 		case '\t':
3467c478bd9Sstevel@tonic-gate 			destcol += value(vi_TABSTOP) - destcol % value(vi_TABSTOP);
3477c478bd9Sstevel@tonic-gate 			continue;
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 		case '\n':
3507c478bd9Sstevel@tonic-gate 			destline += destcol / columns + 1;
3517c478bd9Sstevel@tonic-gate 			if (destcol != 0 && destcol % columns == 0)
3527c478bd9Sstevel@tonic-gate 				destline--;
3537c478bd9Sstevel@tonic-gate 			destcol = 0;
3547c478bd9Sstevel@tonic-gate 			continue;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		default:
3577c478bd9Sstevel@tonic-gate 			fgoto();
3587c478bd9Sstevel@tonic-gate 			for (;;) {
3597c478bd9Sstevel@tonic-gate 				int length, length2;
3607c478bd9Sstevel@tonic-gate 				unsigned char *p;
3617c478bd9Sstevel@tonic-gate 				c &= TRIM;
3627c478bd9Sstevel@tonic-gate 				if ((length = wcwidth(c)) < 0)
3637c478bd9Sstevel@tonic-gate 					length = 0;
3647c478bd9Sstevel@tonic-gate 				if (auto_right_margin == 0 && outcol >= columns)
3657c478bd9Sstevel@tonic-gate 					fgoto();
3667c478bd9Sstevel@tonic-gate 				if((destcol % columns) + length - 1 >= columns) {
3677c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
3687c478bd9Sstevel@tonic-gate 					/* represent split chars by '>' */
3697c478bd9Sstevel@tonic-gate 					splitcnt = length - 1;
3707c478bd9Sstevel@tonic-gate 					c = '>';
3717c478bd9Sstevel@tonic-gate #else
3727c478bd9Sstevel@tonic-gate 					/* substitute/wrap multicolumn char */
3737c478bd9Sstevel@tonic-gate 					if(mc_wrap) {
3747c478bd9Sstevel@tonic-gate 						fillercnt = columns -
3757c478bd9Sstevel@tonic-gate 							    (destcol % columns);
3767c478bd9Sstevel@tonic-gate 						while(fillercnt) {
377*f6db9f27Scf46844 							(void) putch(mc_filler);
3787c478bd9Sstevel@tonic-gate 							outcol++;
3797c478bd9Sstevel@tonic-gate 							destcol++;
3807c478bd9Sstevel@tonic-gate 							fillercnt--;
3817c478bd9Sstevel@tonic-gate 						}
3827c478bd9Sstevel@tonic-gate 					} else {
3837c478bd9Sstevel@tonic-gate 						fillercnt = length - 1;
3847c478bd9Sstevel@tonic-gate 						c = mc_filler;
3857c478bd9Sstevel@tonic-gate 					}
3867c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
3877c478bd9Sstevel@tonic-gate 					continue;
3887c478bd9Sstevel@tonic-gate 				}
3897c478bd9Sstevel@tonic-gate 				length2 = wctomb((char *)multic, c);
3907c478bd9Sstevel@tonic-gate 				p = multic;
3917c478bd9Sstevel@tonic-gate 				while(length2--)
392*f6db9f27Scf46844 					(void) putch(*p++);
3937c478bd9Sstevel@tonic-gate 				if (c == '\b') {
3947c478bd9Sstevel@tonic-gate 					outcol--;
3957c478bd9Sstevel@tonic-gate 					destcol--;
3967c478bd9Sstevel@tonic-gate 				} else if (c >= ' ' && c != DELETE) {
3977c478bd9Sstevel@tonic-gate 					outcol += length;
3987c478bd9Sstevel@tonic-gate 					destcol += length;
3997c478bd9Sstevel@tonic-gate 					if (eat_newline_glitch && outcol % columns == 0)
400*f6db9f27Scf46844 						(void) putch('\r'),
401*f6db9f27Scf46844 						    (void) putch('\n');
4027c478bd9Sstevel@tonic-gate 				}
4037c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
4047c478bd9Sstevel@tonic-gate 				if(splitcnt) {
4057c478bd9Sstevel@tonic-gate 					splitcnt--;
4067c478bd9Sstevel@tonic-gate 					c = '>';
4077c478bd9Sstevel@tonic-gate 				} else
4087c478bd9Sstevel@tonic-gate 					c = *lp++;
4097c478bd9Sstevel@tonic-gate #else
4107c478bd9Sstevel@tonic-gate 				if(fillercnt) {
4117c478bd9Sstevel@tonic-gate 					fillercnt--;
4127c478bd9Sstevel@tonic-gate 					c = mc_filler;
4137c478bd9Sstevel@tonic-gate 					if(c == ' ')
4147c478bd9Sstevel@tonic-gate 						continue;
4157c478bd9Sstevel@tonic-gate 				} else
4167c478bd9Sstevel@tonic-gate 					c = *lp++;
4177c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
4187c478bd9Sstevel@tonic-gate 				if (c <= ' ')
4197c478bd9Sstevel@tonic-gate 					break;
4207c478bd9Sstevel@tonic-gate 			}
4217c478bd9Sstevel@tonic-gate 			--lp;
4227c478bd9Sstevel@tonic-gate 			continue;
4237c478bd9Sstevel@tonic-gate 		}
4247c478bd9Sstevel@tonic-gate 	linp = linb;
4257c478bd9Sstevel@tonic-gate }
4267c478bd9Sstevel@tonic-gate 
427*f6db9f27Scf46844 void
flush2(void)428*f6db9f27Scf46844 flush2(void)
4297c478bd9Sstevel@tonic-gate {
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate 	fgoto();
4327c478bd9Sstevel@tonic-gate 	flusho();
4337c478bd9Sstevel@tonic-gate 	pstop();
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
4367c478bd9Sstevel@tonic-gate /*
4377c478bd9Sstevel@tonic-gate  * Sync the position of the output cursor.
4387c478bd9Sstevel@tonic-gate  * Most work here is rounding for terminal boundaries getting the
4397c478bd9Sstevel@tonic-gate  * column position implied by wraparound or the lack thereof and
4407c478bd9Sstevel@tonic-gate  * rolling up the screen to get destline on the screen.
4417c478bd9Sstevel@tonic-gate  */
442*f6db9f27Scf46844 void
fgoto(void)443*f6db9f27Scf46844 fgoto(void)
4447c478bd9Sstevel@tonic-gate {
445*f6db9f27Scf46844 	int l, c;
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	if (destcol > columns - 1) {
4487c478bd9Sstevel@tonic-gate 		destline += destcol / columns;
4497c478bd9Sstevel@tonic-gate 		destcol %= columns;
4507c478bd9Sstevel@tonic-gate 	}
4517c478bd9Sstevel@tonic-gate 	if (outcol > columns - 1) {
4527c478bd9Sstevel@tonic-gate 		l = (outcol + 1) / columns;
4537c478bd9Sstevel@tonic-gate 		outline += l;
4547c478bd9Sstevel@tonic-gate 		outcol %= columns;
4557c478bd9Sstevel@tonic-gate 		if (auto_right_margin == 0) {
4567c478bd9Sstevel@tonic-gate 			while (l > 0) {
4577c478bd9Sstevel@tonic-gate 				if (pfast)
4587c478bd9Sstevel@tonic-gate 					tputs(carriage_return, 0, putch);
4597c478bd9Sstevel@tonic-gate 				tputs(cursor_down, 0, putch);
4607c478bd9Sstevel@tonic-gate 				l--;
4617c478bd9Sstevel@tonic-gate 			}
4627c478bd9Sstevel@tonic-gate 			outcol = 0;
4637c478bd9Sstevel@tonic-gate 		}
4647c478bd9Sstevel@tonic-gate 		if (outline > lines - 1) {
4657c478bd9Sstevel@tonic-gate 			destline -= outline - (lines - 1);
4667c478bd9Sstevel@tonic-gate 			outline = lines - 1;
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 	if (destline > lines - 1) {
4707c478bd9Sstevel@tonic-gate 		l = destline;
4717c478bd9Sstevel@tonic-gate 		destline = lines - 1;
4727c478bd9Sstevel@tonic-gate 		if (outline < lines - 1) {
4737c478bd9Sstevel@tonic-gate 			c = destcol;
4747c478bd9Sstevel@tonic-gate 			if (pfast == 0 && (!cursor_address || holdcm))
4757c478bd9Sstevel@tonic-gate 				destcol = 0;
4767c478bd9Sstevel@tonic-gate 			fgoto();
4777c478bd9Sstevel@tonic-gate 			destcol = c;
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		while (l > lines - 1) {
4807c478bd9Sstevel@tonic-gate 			/*
4817c478bd9Sstevel@tonic-gate 			 * The following linefeed (or simulation thereof)
4827c478bd9Sstevel@tonic-gate 			 * is supposed to scroll up the screen, since we
4837c478bd9Sstevel@tonic-gate 			 * are on the bottom line.
4847c478bd9Sstevel@tonic-gate 			 *
4857c478bd9Sstevel@tonic-gate 			 * Superbee glitch:  in the middle of the screen we
4867c478bd9Sstevel@tonic-gate 			 * have to use esc B (down) because linefeed messes up
4877c478bd9Sstevel@tonic-gate 			 * in "Efficient Paging" mode (which is essential in
4887c478bd9Sstevel@tonic-gate 			 * some SB's because CRLF mode puts garbage
4897c478bd9Sstevel@tonic-gate 			 * in at end of memory), but you must use linefeed to
4907c478bd9Sstevel@tonic-gate 			 * scroll since down arrow won't go past memory end.
4917c478bd9Sstevel@tonic-gate 			 * I turned this off after receiving Paul Eggert's
4927c478bd9Sstevel@tonic-gate 			 * Superbee description which wins better.
4937c478bd9Sstevel@tonic-gate 			 */
4947c478bd9Sstevel@tonic-gate 			if (scroll_forward /* && !beehive_glitch */ && pfast)
4957c478bd9Sstevel@tonic-gate 				tputs(scroll_forward, 0, putch);
4967c478bd9Sstevel@tonic-gate 			else
497*f6db9f27Scf46844 				(void) putch('\n');
4987c478bd9Sstevel@tonic-gate 			l--;
4997c478bd9Sstevel@tonic-gate 			if (pfast == 0)
5007c478bd9Sstevel@tonic-gate 				outcol = 0;
5017c478bd9Sstevel@tonic-gate 		}
5027c478bd9Sstevel@tonic-gate 	}
5037c478bd9Sstevel@tonic-gate 	if (destline < outline && !(cursor_address && !holdcm || cursor_up || cursor_home))
5047c478bd9Sstevel@tonic-gate 		destline = outline;
5057c478bd9Sstevel@tonic-gate 	if (cursor_address && !holdcm)
5067c478bd9Sstevel@tonic-gate 		if (plod(costCM) > 0)
5077c478bd9Sstevel@tonic-gate 			plod(0);
5087c478bd9Sstevel@tonic-gate 		else
5097c478bd9Sstevel@tonic-gate 			tputs(tparm(cursor_address, destline, destcol), 0, putch);
5107c478bd9Sstevel@tonic-gate 	else
5117c478bd9Sstevel@tonic-gate 		plod(0);
5127c478bd9Sstevel@tonic-gate 	outline = destline;
5137c478bd9Sstevel@tonic-gate 	outcol = destcol;
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate /*
5177c478bd9Sstevel@tonic-gate  * Tab to column col by flushing and then setting destcol.
5187c478bd9Sstevel@tonic-gate  * Used by "set all".
5197c478bd9Sstevel@tonic-gate  */
520*f6db9f27Scf46844 void
gotab(int col)521*f6db9f27Scf46844 gotab(int col)
5227c478bd9Sstevel@tonic-gate {
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	flush1();
5257c478bd9Sstevel@tonic-gate 	destcol = col;
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate /*
5297c478bd9Sstevel@tonic-gate  * Move (slowly) to destination.
5307c478bd9Sstevel@tonic-gate  * Hard thing here is using home cursor on really deficient terminals.
5317c478bd9Sstevel@tonic-gate  * Otherwise just use cursor motions, hacking use of tabs and overtabbing
5327c478bd9Sstevel@tonic-gate  * and backspace.
5337c478bd9Sstevel@tonic-gate  */
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate static int plodcnt, plodflg;
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate int
5387c478bd9Sstevel@tonic-gate #ifdef __STDC__
plodput(char c)5397c478bd9Sstevel@tonic-gate plodput(char c)
5407c478bd9Sstevel@tonic-gate #else
5417c478bd9Sstevel@tonic-gate plodput(c)
5427c478bd9Sstevel@tonic-gate char c;
5437c478bd9Sstevel@tonic-gate #endif
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	if (plodflg)
5477c478bd9Sstevel@tonic-gate 		plodcnt--;
5487c478bd9Sstevel@tonic-gate 	else
549*f6db9f27Scf46844 		(void) putch(c);
550*f6db9f27Scf46844 	return (0);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate 
553*f6db9f27Scf46844 int
plod(int cnt)554*f6db9f27Scf46844 plod(int cnt)
5557c478bd9Sstevel@tonic-gate {
556*f6db9f27Scf46844 	int i, j, k;
557*f6db9f27Scf46844 	int soutcol, soutline;
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	plodcnt = plodflg = cnt;
5607c478bd9Sstevel@tonic-gate 	soutcol = outcol;
5617c478bd9Sstevel@tonic-gate 	soutline = outline;
5627c478bd9Sstevel@tonic-gate 	/*
5637c478bd9Sstevel@tonic-gate 	 * Consider homing and moving down/right from there, vs moving
5647c478bd9Sstevel@tonic-gate 	 * directly with local motions to the right spot.
5657c478bd9Sstevel@tonic-gate 	 */
5667c478bd9Sstevel@tonic-gate 	if (cursor_home) {
5677c478bd9Sstevel@tonic-gate 		/*
5687c478bd9Sstevel@tonic-gate 		 * i is the cost to home and tab/space to the right to
5697c478bd9Sstevel@tonic-gate 		 * get to the proper column.  This assumes cursor_right costs
5707c478bd9Sstevel@tonic-gate 		 * 1 char.  So i+destcol is cost of motion with home.
5717c478bd9Sstevel@tonic-gate 		 */
5727c478bd9Sstevel@tonic-gate 		if (tab && value(vi_HARDTABS))
5737c478bd9Sstevel@tonic-gate 			i = (destcol / value(vi_HARDTABS)) + (destcol % value(vi_HARDTABS));
5747c478bd9Sstevel@tonic-gate 		else
5757c478bd9Sstevel@tonic-gate 			i = destcol;
5767c478bd9Sstevel@tonic-gate 		/*
5777c478bd9Sstevel@tonic-gate 		 * j is cost to move locally without homing
5787c478bd9Sstevel@tonic-gate 		 */
5797c478bd9Sstevel@tonic-gate 		if (destcol >= outcol) {	/* if motion is to the right */
5807c478bd9Sstevel@tonic-gate 			if (value(vi_HARDTABS)) {
5817c478bd9Sstevel@tonic-gate 				j = destcol / value(vi_HARDTABS) - outcol / value(vi_HARDTABS);
5827c478bd9Sstevel@tonic-gate 				if (tab && j)
5837c478bd9Sstevel@tonic-gate 					j += destcol % value(vi_HARDTABS);
5847c478bd9Sstevel@tonic-gate 				else
5857c478bd9Sstevel@tonic-gate 					j = destcol - outcol;
5867c478bd9Sstevel@tonic-gate 			} else
5877c478bd9Sstevel@tonic-gate 				j = destcol - outcol;
5887c478bd9Sstevel@tonic-gate 		} else
5897c478bd9Sstevel@tonic-gate 			/* leftward motion only works if we can backspace. */
5907c478bd9Sstevel@tonic-gate 			if (outcol - destcol <= i && (cursor_left))
5917c478bd9Sstevel@tonic-gate 				i = j = outcol - destcol; /* cheaper to backspace */
5927c478bd9Sstevel@tonic-gate 			else
5937c478bd9Sstevel@tonic-gate 				j = i + 1; /* impossibly expensive */
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 		/* k is the absolute value of vertical distance */
5967c478bd9Sstevel@tonic-gate 		k = outline - destline;
5977c478bd9Sstevel@tonic-gate 		if (k < 0)
5987c478bd9Sstevel@tonic-gate 			k = -k;
5997c478bd9Sstevel@tonic-gate 		j += k;
6007c478bd9Sstevel@tonic-gate 
6017c478bd9Sstevel@tonic-gate 		/*
6027c478bd9Sstevel@tonic-gate 		 * Decision.  We may not have a choice if no cursor_up.
6037c478bd9Sstevel@tonic-gate 		 */
6047c478bd9Sstevel@tonic-gate 		if (i + destline < j || (!cursor_up && destline < outline)) {
6057c478bd9Sstevel@tonic-gate 			/*
6067c478bd9Sstevel@tonic-gate 			 * Cheaper to home.  Do it now and pretend it's a
6077c478bd9Sstevel@tonic-gate 			 * regular local motion.
6087c478bd9Sstevel@tonic-gate 			 */
6097c478bd9Sstevel@tonic-gate 			tputs(cursor_home, 0, plodput);
6107c478bd9Sstevel@tonic-gate 			outcol = outline = 0;
6117c478bd9Sstevel@tonic-gate 		} else if (cursor_to_ll) {
6127c478bd9Sstevel@tonic-gate 			/*
6137c478bd9Sstevel@tonic-gate 			 * Quickly consider homing down and moving from there.
6147c478bd9Sstevel@tonic-gate 			 * Assume cost of cursor_to_ll is 2.
6157c478bd9Sstevel@tonic-gate 			 */
6167c478bd9Sstevel@tonic-gate 			k = (lines - 1) - destline;
6177c478bd9Sstevel@tonic-gate 			if (i + k + 2 < j && (k<=0 || cursor_up)) {
6187c478bd9Sstevel@tonic-gate 				tputs(cursor_to_ll, 0, plodput);
6197c478bd9Sstevel@tonic-gate 				outcol = 0;
6207c478bd9Sstevel@tonic-gate 				outline = lines - 1;
6217c478bd9Sstevel@tonic-gate 			}
6227c478bd9Sstevel@tonic-gate 		}
6237c478bd9Sstevel@tonic-gate 	} else
6247c478bd9Sstevel@tonic-gate 		/*
6257c478bd9Sstevel@tonic-gate 		 * No home and no up means it's impossible, so we return an
6267c478bd9Sstevel@tonic-gate 		 * incredibly big number to make cursor motion win out.
6277c478bd9Sstevel@tonic-gate 		 */
6287c478bd9Sstevel@tonic-gate 		if (!cursor_up && destline < outline)
6297c478bd9Sstevel@tonic-gate 			return (500);
6307c478bd9Sstevel@tonic-gate 	if (tab && value(vi_HARDTABS))
6317c478bd9Sstevel@tonic-gate 		i = destcol % value(vi_HARDTABS)
6327c478bd9Sstevel@tonic-gate 		    + destcol / value(vi_HARDTABS);
6337c478bd9Sstevel@tonic-gate 	else
6347c478bd9Sstevel@tonic-gate 		i = destcol;
6357c478bd9Sstevel@tonic-gate /*
6367c478bd9Sstevel@tonic-gate 	if (back_tab && outcol > destcol && (j = (((outcol+7) & ~7) - destcol - 1) >> 3)) {
6377c478bd9Sstevel@tonic-gate 		j *= (k = strlen(back_tab));
6387c478bd9Sstevel@tonic-gate 		if ((k += (destcol&7)) > 4)
6397c478bd9Sstevel@tonic-gate 			j += 8 - (destcol&7);
6407c478bd9Sstevel@tonic-gate 		else
6417c478bd9Sstevel@tonic-gate 			j += k;
6427c478bd9Sstevel@tonic-gate 	} else
6437c478bd9Sstevel@tonic-gate */
6447c478bd9Sstevel@tonic-gate 		j = outcol - destcol;
6457c478bd9Sstevel@tonic-gate 	/*
6467c478bd9Sstevel@tonic-gate 	 * If we will later need a \n which will turn into a \r\n by
6477c478bd9Sstevel@tonic-gate 	 * the system or the terminal, then don't bother to try to \r.
6487c478bd9Sstevel@tonic-gate 	 */
6497c478bd9Sstevel@tonic-gate 	if ((NONL || !pfast) && outline < destline)
6507c478bd9Sstevel@tonic-gate 		goto dontcr;
6517c478bd9Sstevel@tonic-gate 	/*
6527c478bd9Sstevel@tonic-gate 	 * If the terminal will do a \r\n and there isn't room for it,
6537c478bd9Sstevel@tonic-gate 	 * then we can't afford a \r.
6547c478bd9Sstevel@tonic-gate 	 */
6557c478bd9Sstevel@tonic-gate 	if (!carriage_return && outline >= destline)
6567c478bd9Sstevel@tonic-gate 		goto dontcr;
6577c478bd9Sstevel@tonic-gate 	/*
6587c478bd9Sstevel@tonic-gate 	 * If it will be cheaper, or if we can't back up, then send
6597c478bd9Sstevel@tonic-gate 	 * a return preliminarily.
6607c478bd9Sstevel@tonic-gate 	 */
6617c478bd9Sstevel@tonic-gate 	if (j > i + 1 || outcol > destcol && !cursor_left) {
6627c478bd9Sstevel@tonic-gate 		/*
6637c478bd9Sstevel@tonic-gate 		 * BUG: this doesn't take the (possibly long) length
6647c478bd9Sstevel@tonic-gate 		 * of carriage_return into account.
6657c478bd9Sstevel@tonic-gate 		 */
6667c478bd9Sstevel@tonic-gate 		if (carriage_return) {
6677c478bd9Sstevel@tonic-gate 			tputs(carriage_return, 0, plodput);
6687c478bd9Sstevel@tonic-gate 			outcol = 0;
6697c478bd9Sstevel@tonic-gate 		} else if (newline) {
6707c478bd9Sstevel@tonic-gate 			tputs(newline, 0, plodput);
6717c478bd9Sstevel@tonic-gate 			outline++;
6727c478bd9Sstevel@tonic-gate 			outcol = 0;
6737c478bd9Sstevel@tonic-gate 		}
6747c478bd9Sstevel@tonic-gate 	}
6757c478bd9Sstevel@tonic-gate dontcr:
6767c478bd9Sstevel@tonic-gate 	/* Move down, if necessary, until we are at the desired line */
6777c478bd9Sstevel@tonic-gate 	while (outline < destline) {
6787c478bd9Sstevel@tonic-gate 		j = destline - outline;
6797c478bd9Sstevel@tonic-gate 		if (j > costDP && parm_down_cursor) {
6807c478bd9Sstevel@tonic-gate 			/* Win big on Tek 4025 */
6817c478bd9Sstevel@tonic-gate 			tputs(tparm(parm_down_cursor, j), j, plodput);
6827c478bd9Sstevel@tonic-gate 			outline += j;
6837c478bd9Sstevel@tonic-gate 		}
6847c478bd9Sstevel@tonic-gate 		else {
6857c478bd9Sstevel@tonic-gate 			outline++;
6867c478bd9Sstevel@tonic-gate 			if (cursor_down && pfast)
6877c478bd9Sstevel@tonic-gate 				tputs(cursor_down, 0, plodput);
6887c478bd9Sstevel@tonic-gate 			else
689*f6db9f27Scf46844 				(void) plodput('\n');
6907c478bd9Sstevel@tonic-gate 		}
6917c478bd9Sstevel@tonic-gate 		if (plodcnt < 0)
6927c478bd9Sstevel@tonic-gate 			goto out;
6937c478bd9Sstevel@tonic-gate 		if (NONL || pfast == 0)
6947c478bd9Sstevel@tonic-gate 			outcol = 0;
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 	if (back_tab)
6977c478bd9Sstevel@tonic-gate 		k = strlen(back_tab);	/* should probably be cost(back_tab) and moved out */
6987c478bd9Sstevel@tonic-gate 	/* Move left, if necessary, to desired column */
6997c478bd9Sstevel@tonic-gate 	while (outcol > destcol) {
7007c478bd9Sstevel@tonic-gate 		if (plodcnt < 0)
7017c478bd9Sstevel@tonic-gate 			goto out;
7027c478bd9Sstevel@tonic-gate 		if (back_tab && !insmode && outcol - destcol > 4+k) {
7037c478bd9Sstevel@tonic-gate 			tputs(back_tab, 0, plodput);
7047c478bd9Sstevel@tonic-gate 			outcol--;
7057c478bd9Sstevel@tonic-gate 			if (value(vi_HARDTABS))
7067c478bd9Sstevel@tonic-gate 				outcol -= outcol % value(vi_HARDTABS); /* outcol &= ~7; */
7077c478bd9Sstevel@tonic-gate 			continue;
7087c478bd9Sstevel@tonic-gate 		}
7097c478bd9Sstevel@tonic-gate 		j = outcol - destcol;
7107c478bd9Sstevel@tonic-gate 		if (j > costLP && parm_left_cursor) {
7117c478bd9Sstevel@tonic-gate 			tputs(tparm(parm_left_cursor, j), j, plodput);
7127c478bd9Sstevel@tonic-gate 			outcol -= j;
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 		else {
7157c478bd9Sstevel@tonic-gate 			outcol--;
7167c478bd9Sstevel@tonic-gate 			tputs(cursor_left, 0, plodput);
7177c478bd9Sstevel@tonic-gate 		}
7187c478bd9Sstevel@tonic-gate 	}
7197c478bd9Sstevel@tonic-gate 	/* Move up, if necessary, to desired row */
7207c478bd9Sstevel@tonic-gate 	while (outline > destline) {
7217c478bd9Sstevel@tonic-gate 		j = outline - destline;
7227c478bd9Sstevel@tonic-gate 		if (parm_up_cursor && j > 1) {
7237c478bd9Sstevel@tonic-gate 			/* Win big on Tek 4025 */
7247c478bd9Sstevel@tonic-gate 			tputs(tparm(parm_up_cursor, j), j, plodput);
7257c478bd9Sstevel@tonic-gate 			outline -= j;
7267c478bd9Sstevel@tonic-gate 		}
7277c478bd9Sstevel@tonic-gate 		else {
7287c478bd9Sstevel@tonic-gate 			outline--;
7297c478bd9Sstevel@tonic-gate 			tputs(cursor_up, 0, plodput);
7307c478bd9Sstevel@tonic-gate 		}
7317c478bd9Sstevel@tonic-gate 		if (plodcnt < 0)
7327c478bd9Sstevel@tonic-gate 			goto out;
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 	/*
7357c478bd9Sstevel@tonic-gate 	 * Now move to the right, if necessary.  We first tab to
7367c478bd9Sstevel@tonic-gate 	 * as close as we can get.
7377c478bd9Sstevel@tonic-gate 	 */
7387c478bd9Sstevel@tonic-gate 	if (value(vi_HARDTABS) && tab && !insmode && destcol - outcol > 1) {
7397c478bd9Sstevel@tonic-gate 		/* tab to right as far as possible without passing col */
7407c478bd9Sstevel@tonic-gate 		for (;;) {
7417c478bd9Sstevel@tonic-gate 			i = tabcol(outcol, value(vi_HARDTABS));
7427c478bd9Sstevel@tonic-gate 			if (i > destcol)
7437c478bd9Sstevel@tonic-gate 				break;
7447c478bd9Sstevel@tonic-gate 			if (tab)
7457c478bd9Sstevel@tonic-gate 				tputs(tab, 0, plodput);
7467c478bd9Sstevel@tonic-gate 			else
747*f6db9f27Scf46844 				(void) plodput('\t');
7487c478bd9Sstevel@tonic-gate 			outcol = i;
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 		/* consider another tab and then some backspaces */
7517c478bd9Sstevel@tonic-gate 		if (destcol - outcol > 4 && i < columns && cursor_left) {
7527c478bd9Sstevel@tonic-gate 			tputs(tab, 0, plodput);
7537c478bd9Sstevel@tonic-gate 			outcol = i;
7547c478bd9Sstevel@tonic-gate 			/*
7557c478bd9Sstevel@tonic-gate 			 * Back up.  Don't worry about parm_left_cursor because
7567c478bd9Sstevel@tonic-gate 			 * it's never more than 4 spaces anyway.
7577c478bd9Sstevel@tonic-gate 			 */
7587c478bd9Sstevel@tonic-gate 			while (outcol > destcol) {
7597c478bd9Sstevel@tonic-gate 				outcol--;
7607c478bd9Sstevel@tonic-gate 				tputs(cursor_left, 0, plodput);
7617c478bd9Sstevel@tonic-gate 			}
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 	}
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * We've tabbed as much as possible.  If we still need to go
7667c478bd9Sstevel@tonic-gate 	 * further (not exact or can't tab) space over.  This is a
7677c478bd9Sstevel@tonic-gate 	 * very common case when moving to the right with space.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 	while (outcol < destcol) {
7707c478bd9Sstevel@tonic-gate 		j = destcol - outcol;
7717c478bd9Sstevel@tonic-gate 		if (j > costRP && parm_right_cursor) {
7727c478bd9Sstevel@tonic-gate 			/*
7737c478bd9Sstevel@tonic-gate 			 * This probably happens rarely, if at all.
7747c478bd9Sstevel@tonic-gate 			 * It seems mainly useful for ANSI terminals
7757c478bd9Sstevel@tonic-gate 			 * with no hardware tabs, and I don't know
7767c478bd9Sstevel@tonic-gate 			 * of any such terminal at the moment.
7777c478bd9Sstevel@tonic-gate 			 */
7787c478bd9Sstevel@tonic-gate 			tputs(tparm(parm_right_cursor, j), j, plodput);
7797c478bd9Sstevel@tonic-gate 			outcol += j;
7807c478bd9Sstevel@tonic-gate 		}
7817c478bd9Sstevel@tonic-gate 		else {
7827c478bd9Sstevel@tonic-gate 			/*
7837c478bd9Sstevel@tonic-gate 			 * move one char to the right.  We don't use right
7847c478bd9Sstevel@tonic-gate 			 * because it's better to just print the char we are
7857c478bd9Sstevel@tonic-gate 			 * moving over.  There are various exceptions, however.
7867c478bd9Sstevel@tonic-gate 			 * If !inopen, vtube contains garbage.  If the char is
7877c478bd9Sstevel@tonic-gate 			 * a null or a tab we want to print a space.  Other
7887c478bd9Sstevel@tonic-gate 			 * random chars we use space for instead, too.
7897c478bd9Sstevel@tonic-gate 			 */
790*f6db9f27Scf46844 			wchar_t wchar;
791*f6db9f27Scf46844 			int length, scrlength;
7927c478bd9Sstevel@tonic-gate 			unsigned char multic[MB_LEN_MAX];
7937c478bd9Sstevel@tonic-gate 
7947c478bd9Sstevel@tonic-gate 			if (!inopen || vtube[outline]==NULL ||
7957c478bd9Sstevel@tonic-gate 				(wchar=vtube[outline][outcol]) < ' ')
7967c478bd9Sstevel@tonic-gate 				wchar = ' ';
7977c478bd9Sstevel@tonic-gate 			if((int)(wchar & QUOTE))	/* no sign extension on 3B */
7987c478bd9Sstevel@tonic-gate 				wchar = ' ';
7997c478bd9Sstevel@tonic-gate 			length = wctomb((char *)multic, wchar);
8007c478bd9Sstevel@tonic-gate 			if ((scrlength = wcwidth(wchar)) < 0)
8017c478bd9Sstevel@tonic-gate 				scrlength = 0;
8027c478bd9Sstevel@tonic-gate 			/* assume multibyte terminals have cursor_right */
8037c478bd9Sstevel@tonic-gate 			if (insmode && cursor_right || length > 1 || wchar == FILLER) {
8047c478bd9Sstevel@tonic-gate 				int diff = destcol - outcol;
8057c478bd9Sstevel@tonic-gate 				j = (wchar == FILLER ? 1 : scrlength > diff ? diff : scrlength);
8067c478bd9Sstevel@tonic-gate 				while(j--) {
8077c478bd9Sstevel@tonic-gate 					outcol++;
8087c478bd9Sstevel@tonic-gate 					tputs(cursor_right, 0, plodput);
8097c478bd9Sstevel@tonic-gate 				}
8107c478bd9Sstevel@tonic-gate 			} else {
811*f6db9f27Scf46844 				(void) plodput((char)multic[0]);
8127c478bd9Sstevel@tonic-gate 				outcol++;
8137c478bd9Sstevel@tonic-gate 			}
8147c478bd9Sstevel@tonic-gate 		}
8157c478bd9Sstevel@tonic-gate 		if (plodcnt < 0)
8167c478bd9Sstevel@tonic-gate 			goto out;
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate out:
8197c478bd9Sstevel@tonic-gate 	if(plodflg) {
8207c478bd9Sstevel@tonic-gate 		outcol = soutcol;
8217c478bd9Sstevel@tonic-gate 		outline = soutline;
8227c478bd9Sstevel@tonic-gate 	}
8237c478bd9Sstevel@tonic-gate 	return(plodcnt);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * An input line arrived.
8287c478bd9Sstevel@tonic-gate  * Calculate new (approximate) screen line position.
8297c478bd9Sstevel@tonic-gate  * Approximate because kill character echoes newline with
8307c478bd9Sstevel@tonic-gate  * no feedback and also because of long input lines.
8317c478bd9Sstevel@tonic-gate  */
832*f6db9f27Scf46844 void
noteinp(void)833*f6db9f27Scf46844 noteinp(void)
8347c478bd9Sstevel@tonic-gate {
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	outline++;
8377c478bd9Sstevel@tonic-gate 	if (outline > lines - 1)
8387c478bd9Sstevel@tonic-gate 		outline = lines - 1;
8397c478bd9Sstevel@tonic-gate 	destline = outline;
8407c478bd9Sstevel@tonic-gate 	destcol = outcol = 0;
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * Something weird just happened and we
8457c478bd9Sstevel@tonic-gate  * lost track of what's happening out there.
8467c478bd9Sstevel@tonic-gate  * Since we can't, in general, read where we are
8477c478bd9Sstevel@tonic-gate  * we just reset to some known state.
8487c478bd9Sstevel@tonic-gate  * On cursor addressable terminals setting to unknown
8497c478bd9Sstevel@tonic-gate  * will force a cursor address soon.
8507c478bd9Sstevel@tonic-gate  */
851*f6db9f27Scf46844 void
termreset(void)852*f6db9f27Scf46844 termreset(void)
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	endim();
8567c478bd9Sstevel@tonic-gate 	if (enter_ca_mode)
857*f6db9f27Scf46844 		putpad((unsigned char *)enter_ca_mode);
8587c478bd9Sstevel@tonic-gate 	destcol = 0;
8597c478bd9Sstevel@tonic-gate 	destline = lines - 1;
8607c478bd9Sstevel@tonic-gate 	if (cursor_address) {
8617c478bd9Sstevel@tonic-gate 		outcol = UKCOL;
8627c478bd9Sstevel@tonic-gate 		outline = UKCOL;
8637c478bd9Sstevel@tonic-gate 	} else {
8647c478bd9Sstevel@tonic-gate 		outcol = destcol;
8657c478bd9Sstevel@tonic-gate 		outline = destline;
8667c478bd9Sstevel@tonic-gate 	}
8677c478bd9Sstevel@tonic-gate }
8687c478bd9Sstevel@tonic-gate 
8697c478bd9Sstevel@tonic-gate /*
8707c478bd9Sstevel@tonic-gate  * Low level buffering, with the ability to drain
8717c478bd9Sstevel@tonic-gate  * buffered output without printing it.
8727c478bd9Sstevel@tonic-gate  */
8737c478bd9Sstevel@tonic-gate unsigned char	*obp = obuf;
8747c478bd9Sstevel@tonic-gate 
875*f6db9f27Scf46844 void
draino(void)876*f6db9f27Scf46844 draino(void)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 
8797c478bd9Sstevel@tonic-gate 	obp = obuf;
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
882*f6db9f27Scf46844 void
flusho(void)883*f6db9f27Scf46844 flusho(void)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	if (obp != obuf) {
8867c478bd9Sstevel@tonic-gate 		write(1, obuf, obp - obuf);
8877c478bd9Sstevel@tonic-gate #ifdef TRACE
8887c478bd9Sstevel@tonic-gate 		if (trace)
8897c478bd9Sstevel@tonic-gate 			fwrite(obuf, 1, obp-obuf, trace);
8907c478bd9Sstevel@tonic-gate #endif
8917c478bd9Sstevel@tonic-gate 		obp = obuf;
8927c478bd9Sstevel@tonic-gate 	}
8937c478bd9Sstevel@tonic-gate }
8947c478bd9Sstevel@tonic-gate 
895*f6db9f27Scf46844 void
putnl(void)896*f6db9f27Scf46844 putnl(void)
8977c478bd9Sstevel@tonic-gate {
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	putchar('\n');
9007c478bd9Sstevel@tonic-gate }
9017c478bd9Sstevel@tonic-gate 
902*f6db9f27Scf46844 void
putS(unsigned char * cp)903*f6db9f27Scf46844 putS(unsigned char *cp)
9047c478bd9Sstevel@tonic-gate {
9057c478bd9Sstevel@tonic-gate 
9067c478bd9Sstevel@tonic-gate 	if (cp == NULL)
9077c478bd9Sstevel@tonic-gate 		return;
9087c478bd9Sstevel@tonic-gate 	while (*cp)
909*f6db9f27Scf46844 		(void) putch(*cp++);
9107c478bd9Sstevel@tonic-gate }
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate int
putch(char c)9137c478bd9Sstevel@tonic-gate putch(char c)
9147c478bd9Sstevel@tonic-gate {
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate #ifdef OLD3BTTY
9177c478bd9Sstevel@tonic-gate 	if(c == '\n')	/* Fake "\n\r" for '\n' til fix in 3B firmware */
918*f6db9f27Scf46844 		(void) putch('\r'); /* vi does "stty -icanon" => -onlcr !! */
9197c478bd9Sstevel@tonic-gate #endif
9207c478bd9Sstevel@tonic-gate 	*obp++ = c;
9217c478bd9Sstevel@tonic-gate 	if (obp >= &obuf[sizeof obuf])
9227c478bd9Sstevel@tonic-gate 		flusho();
923*f6db9f27Scf46844 	return (0);
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate  * Miscellaneous routines related to output.
9287c478bd9Sstevel@tonic-gate  */
9297c478bd9Sstevel@tonic-gate 
9307c478bd9Sstevel@tonic-gate /*
9317c478bd9Sstevel@tonic-gate  * Put with padding
9327c478bd9Sstevel@tonic-gate  */
933*f6db9f27Scf46844 void
putpad(unsigned char * cp)934*f6db9f27Scf46844 putpad(unsigned char *cp)
9357c478bd9Sstevel@tonic-gate {
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate 	flush();
9387c478bd9Sstevel@tonic-gate 	tputs((char *)cp, 0, putch);
9397c478bd9Sstevel@tonic-gate }
9407c478bd9Sstevel@tonic-gate 
9417c478bd9Sstevel@tonic-gate /*
9427c478bd9Sstevel@tonic-gate  * Set output through normal command mode routine.
9437c478bd9Sstevel@tonic-gate  */
944*f6db9f27Scf46844 void
setoutt(void)945*f6db9f27Scf46844 setoutt(void)
9467c478bd9Sstevel@tonic-gate {
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	Outchar = termchar;
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
9527c478bd9Sstevel@tonic-gate  * Printf (temporarily) in list mode.
9537c478bd9Sstevel@tonic-gate  */
9547c478bd9Sstevel@tonic-gate /*VARARGS2*/
955*f6db9f27Scf46844 void
lprintf(unsigned char * cp,unsigned char * dp,...)956*f6db9f27Scf46844 lprintf(unsigned char *cp, unsigned char *dp, ...)
9577c478bd9Sstevel@tonic-gate {
958*f6db9f27Scf46844 	int (*P)();
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate 	P = setlist(1);
9617c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
962*f6db9f27Scf46844 	viprintf(cp, dp);
9637c478bd9Sstevel@tonic-gate #else
964*f6db9f27Scf46844 	viprintf((char *)cp, (char *)dp);
9657c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
9667c478bd9Sstevel@tonic-gate 	Putchar = P;
9677c478bd9Sstevel@tonic-gate }
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate /*
9707c478bd9Sstevel@tonic-gate  * Newline + flush.
9717c478bd9Sstevel@tonic-gate  */
972*f6db9f27Scf46844 void
putNFL()9737c478bd9Sstevel@tonic-gate putNFL()
9747c478bd9Sstevel@tonic-gate {
9757c478bd9Sstevel@tonic-gate 
9767c478bd9Sstevel@tonic-gate 	putnl();
9777c478bd9Sstevel@tonic-gate 	flush();
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
9807c478bd9Sstevel@tonic-gate /*
9817c478bd9Sstevel@tonic-gate  * Try to start -nl mode.
9827c478bd9Sstevel@tonic-gate  */
983*f6db9f27Scf46844 void
pstart(void)984*f6db9f27Scf46844 pstart(void)
9857c478bd9Sstevel@tonic-gate {
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 	if (NONL)
9887c478bd9Sstevel@tonic-gate 		return;
9897c478bd9Sstevel@tonic-gate  	if (!value(vi_OPTIMIZE))
9907c478bd9Sstevel@tonic-gate 		return;
9917c478bd9Sstevel@tonic-gate 	if (ruptible == 0 || pfast)
9927c478bd9Sstevel@tonic-gate 		return;
9937c478bd9Sstevel@tonic-gate 	fgoto();
9947c478bd9Sstevel@tonic-gate 	flusho();
9957c478bd9Sstevel@tonic-gate 	pfast = 1;
9967c478bd9Sstevel@tonic-gate 	normtty++;
9977c478bd9Sstevel@tonic-gate 	tty = normf;
9987c478bd9Sstevel@tonic-gate 	tty.c_oflag &= ~(ONLCR|TAB3);
9997c478bd9Sstevel@tonic-gate 	tty.c_lflag &= ~ECHO;
10007c478bd9Sstevel@tonic-gate 	saveterm();
10017c478bd9Sstevel@tonic-gate 	sTTY(2);
10027c478bd9Sstevel@tonic-gate }
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate /*
10057c478bd9Sstevel@tonic-gate  * Stop -nl mode.
10067c478bd9Sstevel@tonic-gate  */
1007*f6db9f27Scf46844 void
pstop(void)1008*f6db9f27Scf46844 pstop(void)
10097c478bd9Sstevel@tonic-gate {
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 	if (inopen)
10127c478bd9Sstevel@tonic-gate 		return;
10137c478bd9Sstevel@tonic-gate 	phadnl = 0;
10147c478bd9Sstevel@tonic-gate 	linp = linb;
10157c478bd9Sstevel@tonic-gate 	draino();
10167c478bd9Sstevel@tonic-gate 	normal(normf);
10177c478bd9Sstevel@tonic-gate 	pfast &= ~1;
10187c478bd9Sstevel@tonic-gate }
10197c478bd9Sstevel@tonic-gate 
10207c478bd9Sstevel@tonic-gate /*
10217c478bd9Sstevel@tonic-gate  * Prep tty for open mode.
10227c478bd9Sstevel@tonic-gate  */
10237c478bd9Sstevel@tonic-gate ttymode
ostart()10247c478bd9Sstevel@tonic-gate ostart()
10257c478bd9Sstevel@tonic-gate {
10267c478bd9Sstevel@tonic-gate 	ttymode f;
10277c478bd9Sstevel@tonic-gate 
10287c478bd9Sstevel@tonic-gate 	/*
10297c478bd9Sstevel@tonic-gate 	if (!intty)
10307c478bd9Sstevel@tonic-gate 		error("Open and visual must be used interactively");
10317c478bd9Sstevel@tonic-gate 	*/
1032*f6db9f27Scf46844 	(void) gTTY(2);
10337c478bd9Sstevel@tonic-gate 	normtty++;
10347c478bd9Sstevel@tonic-gate 	f = tty;
10357c478bd9Sstevel@tonic-gate 	tty = normf;
10367c478bd9Sstevel@tonic-gate 	tty.c_iflag &= ~ICRNL;
10377c478bd9Sstevel@tonic-gate 	tty.c_lflag &= ~(ECHO|ICANON);
10387c478bd9Sstevel@tonic-gate 	tty.c_oflag &= ~(TAB3|ONLCR);
10397c478bd9Sstevel@tonic-gate 	tty.c_cc[VMIN] = 1;
10407c478bd9Sstevel@tonic-gate 	tty.c_cc[VTIME] = 1;
10417c478bd9Sstevel@tonic-gate 	ttcharoff();
10427c478bd9Sstevel@tonic-gate 	sTTY(2);
10437c478bd9Sstevel@tonic-gate 	tostart();
10447c478bd9Sstevel@tonic-gate 	pfast |= 2;
10457c478bd9Sstevel@tonic-gate 	saveterm();
10467c478bd9Sstevel@tonic-gate 	return (f);
10477c478bd9Sstevel@tonic-gate }
10487c478bd9Sstevel@tonic-gate 
10497c478bd9Sstevel@tonic-gate /* actions associated with putting the terminal in open mode */
1050*f6db9f27Scf46844 void
tostart(void)1051*f6db9f27Scf46844 tostart(void)
10527c478bd9Sstevel@tonic-gate {
1053*f6db9f27Scf46844 	putpad((unsigned char *)cursor_visible);
1054*f6db9f27Scf46844 	putpad((unsigned char *)keypad_xmit);
10557c478bd9Sstevel@tonic-gate 	if (!value(vi_MESG)) {
10567c478bd9Sstevel@tonic-gate 		if (ttynbuf[0] == 0) {
1057*f6db9f27Scf46844 			char *tn;
10587c478bd9Sstevel@tonic-gate 			if ((tn=ttyname(2)) == NULL &&
10597c478bd9Sstevel@tonic-gate 			    (tn=ttyname(1)) == NULL &&
10607c478bd9Sstevel@tonic-gate 			    (tn=ttyname(0)) == NULL)
10617c478bd9Sstevel@tonic-gate 				ttynbuf[0] = 1;
10627c478bd9Sstevel@tonic-gate 			else
10637c478bd9Sstevel@tonic-gate 				strcpy(ttynbuf, tn);
10647c478bd9Sstevel@tonic-gate 		}
10657c478bd9Sstevel@tonic-gate 		if (ttynbuf[0] != 1) {
10667c478bd9Sstevel@tonic-gate 			struct stat64 sbuf;
10677c478bd9Sstevel@tonic-gate 			stat64((char *)ttynbuf, &sbuf);
10687c478bd9Sstevel@tonic-gate 			ttymesg = FMODE(sbuf) & 0777;
10697c478bd9Sstevel@tonic-gate 			chmod((char *)ttynbuf, 0600);
10707c478bd9Sstevel@tonic-gate 		}
10717c478bd9Sstevel@tonic-gate 	}
10727c478bd9Sstevel@tonic-gate }
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate /*
10757c478bd9Sstevel@tonic-gate  * Turn off start/stop chars if they aren't the default ^S/^Q.
10767c478bd9Sstevel@tonic-gate  * This is so people who make esc their start/stop don't lose.
10777c478bd9Sstevel@tonic-gate  * We always turn off quit since datamedias send ^\ for their
10787c478bd9Sstevel@tonic-gate  * right arrow key.
10797c478bd9Sstevel@tonic-gate  */
10807c478bd9Sstevel@tonic-gate 
1081*f6db9f27Scf46844 void
ttcharoff(void)1082*f6db9f27Scf46844 ttcharoff(void)
10837c478bd9Sstevel@tonic-gate {
10847c478bd9Sstevel@tonic-gate 	/*
10857c478bd9Sstevel@tonic-gate 	 * use 200 instead of 377 because 377 is y-umlaut
10867c478bd9Sstevel@tonic-gate 	 * in ISO 8859/1
10877c478bd9Sstevel@tonic-gate 	 */
10887c478bd9Sstevel@tonic-gate 	tty.c_cc[VQUIT] = termiosflag ? _POSIX_VDISABLE : '\200';
10897c478bd9Sstevel@tonic-gate 	if (tty.c_cc[VSTART] != CTRL('q'))
10907c478bd9Sstevel@tonic-gate 		tty.c_cc[VSTART] = _POSIX_VDISABLE;
10917c478bd9Sstevel@tonic-gate 	if (tty.c_cc[VSTOP] != CTRL('s'))
10927c478bd9Sstevel@tonic-gate 		tty.c_cc[VSTOP] = _POSIX_VDISABLE;
10937c478bd9Sstevel@tonic-gate 	/* We will read ^z and suspend ourselves via kill */
10947c478bd9Sstevel@tonic-gate 	tty.c_cc[VSUSP] = _POSIX_VDISABLE;
10957c478bd9Sstevel@tonic-gate 	tty.c_cc[VDSUSP] = _POSIX_VDISABLE;
10967c478bd9Sstevel@tonic-gate 	tty.c_cc[VREPRINT] = _POSIX_VDISABLE;
10977c478bd9Sstevel@tonic-gate 	tty.c_cc[VDISCARD] = _POSIX_VDISABLE;
10987c478bd9Sstevel@tonic-gate 	tty.c_cc[VWERASE] = _POSIX_VDISABLE;
10997c478bd9Sstevel@tonic-gate 	tty.c_cc[VLNEXT] = _POSIX_VDISABLE;
11007c478bd9Sstevel@tonic-gate }
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate /*
11037c478bd9Sstevel@tonic-gate  * Stop open, restoring tty modes.
11047c478bd9Sstevel@tonic-gate  */
1105*f6db9f27Scf46844 void
ostop(ttymode f)1106*f6db9f27Scf46844 ostop(ttymode f)
11077c478bd9Sstevel@tonic-gate {
11087c478bd9Sstevel@tonic-gate 
11097c478bd9Sstevel@tonic-gate 	pfast = (f.c_oflag & ONLCR) == 0;
11107c478bd9Sstevel@tonic-gate 	termreset(), fgoto(), flusho();
11117c478bd9Sstevel@tonic-gate 	normal(f);
11127c478bd9Sstevel@tonic-gate 	tostop();
11137c478bd9Sstevel@tonic-gate }
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate /* Actions associated with putting the terminal in the right mode. */
1116*f6db9f27Scf46844 void
tostop(void)1117*f6db9f27Scf46844 tostop(void)
11187c478bd9Sstevel@tonic-gate {
1119*f6db9f27Scf46844 	putpad((unsigned char *)clr_eos);
1120*f6db9f27Scf46844 	putpad((unsigned char *)cursor_normal);
1121*f6db9f27Scf46844 	putpad((unsigned char *)keypad_local);
11227c478bd9Sstevel@tonic-gate 	if (!value(vi_MESG) && ttynbuf[0]>1)
11237c478bd9Sstevel@tonic-gate 		chmod((char *)ttynbuf, ttymesg);
11247c478bd9Sstevel@tonic-gate }
11257c478bd9Sstevel@tonic-gate 
11267c478bd9Sstevel@tonic-gate #ifndef CBREAK
11277c478bd9Sstevel@tonic-gate /*
11287c478bd9Sstevel@tonic-gate  * Into cooked mode for interruptibility.
11297c478bd9Sstevel@tonic-gate  */
vcook()11307c478bd9Sstevel@tonic-gate vcook()
11317c478bd9Sstevel@tonic-gate {
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	tty.sg_flags &= ~RAW;
11347c478bd9Sstevel@tonic-gate 	sTTY(2);
11357c478bd9Sstevel@tonic-gate }
11367c478bd9Sstevel@tonic-gate 
11377c478bd9Sstevel@tonic-gate /*
11387c478bd9Sstevel@tonic-gate  * Back into raw mode.
11397c478bd9Sstevel@tonic-gate  */
vraw()11407c478bd9Sstevel@tonic-gate vraw()
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 
11437c478bd9Sstevel@tonic-gate 	tty.sg_flags |= RAW;
11447c478bd9Sstevel@tonic-gate 	sTTY(2);
11457c478bd9Sstevel@tonic-gate }
11467c478bd9Sstevel@tonic-gate #endif
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate /*
11497c478bd9Sstevel@tonic-gate  * Restore flags to normal state f.
11507c478bd9Sstevel@tonic-gate  */
1151*f6db9f27Scf46844 void
normal(ttymode f)1152*f6db9f27Scf46844 normal(ttymode f)
11537c478bd9Sstevel@tonic-gate {
11547c478bd9Sstevel@tonic-gate 
11557c478bd9Sstevel@tonic-gate 	if (normtty > 0) {
11567c478bd9Sstevel@tonic-gate 		setty(f);
11577c478bd9Sstevel@tonic-gate 		normtty--;
11587c478bd9Sstevel@tonic-gate 	}
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate /*
11627c478bd9Sstevel@tonic-gate  * Straight set of flags to state f.
11637c478bd9Sstevel@tonic-gate  */
11647c478bd9Sstevel@tonic-gate ttymode
setty(f)11657c478bd9Sstevel@tonic-gate setty(f)
11667c478bd9Sstevel@tonic-gate 	ttymode f;
11677c478bd9Sstevel@tonic-gate {
11687c478bd9Sstevel@tonic-gate 	int isnorm = 0;
11697c478bd9Sstevel@tonic-gate 	ttymode ot;
11707c478bd9Sstevel@tonic-gate 	ot = tty;
11717c478bd9Sstevel@tonic-gate 
11727c478bd9Sstevel@tonic-gate 	if (tty.c_lflag & ICANON)
11737c478bd9Sstevel@tonic-gate 		ttcharoff();
11747c478bd9Sstevel@tonic-gate 	else
11757c478bd9Sstevel@tonic-gate 		isnorm = 1;
11767c478bd9Sstevel@tonic-gate 	tty = f;
11777c478bd9Sstevel@tonic-gate 	sTTY(2);
11787c478bd9Sstevel@tonic-gate 	if (!isnorm)
11797c478bd9Sstevel@tonic-gate 		saveterm();
11807c478bd9Sstevel@tonic-gate 	return (ot);
11817c478bd9Sstevel@tonic-gate }
11827c478bd9Sstevel@tonic-gate 
11837c478bd9Sstevel@tonic-gate static struct termio termio;
11847c478bd9Sstevel@tonic-gate 
1185*f6db9f27Scf46844 int
gTTY(int i)1186*f6db9f27Scf46844 gTTY(int i)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	if(termiosflag < 0) {
11897c478bd9Sstevel@tonic-gate 		if(ioctl(i, TCGETS, &tty) == 0)
11907c478bd9Sstevel@tonic-gate 			termiosflag = 1;
11917c478bd9Sstevel@tonic-gate 		else  {
11927c478bd9Sstevel@tonic-gate 			termiosflag = 0;
11937c478bd9Sstevel@tonic-gate 			if(ioctl(i, TCGETA, &termio) < 0)
1194*f6db9f27Scf46844 				return (-1);
11957c478bd9Sstevel@tonic-gate 			tty.c_iflag = termio.c_iflag;
11967c478bd9Sstevel@tonic-gate 			tty.c_oflag = termio.c_oflag;
11977c478bd9Sstevel@tonic-gate 			tty.c_cflag = termio.c_cflag;
11987c478bd9Sstevel@tonic-gate 			tty.c_lflag = termio.c_lflag;
11997c478bd9Sstevel@tonic-gate 			for(i = 0; i < NCC; i++)
12007c478bd9Sstevel@tonic-gate 				tty.c_cc[i] = termio.c_cc[i];
12017c478bd9Sstevel@tonic-gate 		}
1202*f6db9f27Scf46844 		return (0);
12037c478bd9Sstevel@tonic-gate 	}
12047c478bd9Sstevel@tonic-gate 	if(termiosflag)
1205*f6db9f27Scf46844 		return (ioctl(i, TCGETS, &tty));
12067c478bd9Sstevel@tonic-gate 	if(ioctl(i, TCGETA, &termio) < 0)
1207*f6db9f27Scf46844 		return (-1);
12087c478bd9Sstevel@tonic-gate 	tty.c_iflag = termio.c_iflag;
12097c478bd9Sstevel@tonic-gate 	tty.c_oflag = termio.c_oflag;
12107c478bd9Sstevel@tonic-gate 	tty.c_cflag = termio.c_cflag;
12117c478bd9Sstevel@tonic-gate 	tty.c_lflag = termio.c_lflag;
12127c478bd9Sstevel@tonic-gate 	for(i = 0; i < NCC; i++)
12137c478bd9Sstevel@tonic-gate 		tty.c_cc[i] = termio.c_cc[i];
1214*f6db9f27Scf46844 	return (0);
12157c478bd9Sstevel@tonic-gate }
12167c478bd9Sstevel@tonic-gate 
12177c478bd9Sstevel@tonic-gate /*
12187c478bd9Sstevel@tonic-gate  * sTTY: set the tty modes on file descriptor i to be what's
12197c478bd9Sstevel@tonic-gate  * currently in global "tty".  (Also use nttyc if needed.)
12207c478bd9Sstevel@tonic-gate  */
1221*f6db9f27Scf46844 void
sTTY(int i)1222*f6db9f27Scf46844 sTTY(int i)
12237c478bd9Sstevel@tonic-gate {
12247c478bd9Sstevel@tonic-gate 	int j;
12257c478bd9Sstevel@tonic-gate 	if(termiosflag)
12267c478bd9Sstevel@tonic-gate 		ioctl(i, TCSETSW, &tty);
12277c478bd9Sstevel@tonic-gate 	else {
12287c478bd9Sstevel@tonic-gate 		termio.c_iflag = tty.c_iflag;
12297c478bd9Sstevel@tonic-gate 		termio.c_oflag = tty.c_oflag;
12307c478bd9Sstevel@tonic-gate 		termio.c_cflag = tty.c_cflag;
12317c478bd9Sstevel@tonic-gate 		termio.c_lflag = tty.c_lflag;
12327c478bd9Sstevel@tonic-gate 		for(j = 0; j < NCC; j++)
12337c478bd9Sstevel@tonic-gate 			termio.c_cc[j] = tty.c_cc[j];
12347c478bd9Sstevel@tonic-gate 		ioctl(i, TCSETAW, &termio);
12357c478bd9Sstevel@tonic-gate 	}
12367c478bd9Sstevel@tonic-gate }
12377c478bd9Sstevel@tonic-gate 
12387c478bd9Sstevel@tonic-gate /*
12397c478bd9Sstevel@tonic-gate  * Print newline, or blank if in open/visual
12407c478bd9Sstevel@tonic-gate  */
1241*f6db9f27Scf46844 void
noonl(void)1242*f6db9f27Scf46844 noonl(void)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 
12457c478bd9Sstevel@tonic-gate 	putchar(Outchar != termchar ? ' ' : '\n');
12467c478bd9Sstevel@tonic-gate }
1247