xref: /titanic_50/usr/src/cmd/vi/port/ex_subr.c (revision 0f1f78266c1aea63b41bde99723de7aa3fa9f7cc)
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
5*23a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
6*23a1cceaSRoger A. Faulkner  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21*23a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
23*23a1cceaSRoger A. Faulkner  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
277c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
317c478bd9Sstevel@tonic-gate 
327c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
337c478bd9Sstevel@tonic-gate #include <sys/eucioctl.h>
347c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
357c478bd9Sstevel@tonic-gate #include <locale.h>
367c478bd9Sstevel@tonic-gate /* Undef putchar/getchar if they're defined. */
377c478bd9Sstevel@tonic-gate #ifdef putchar
387c478bd9Sstevel@tonic-gate #	undef putchar
397c478bd9Sstevel@tonic-gate #endif
407c478bd9Sstevel@tonic-gate #ifdef getchar
417c478bd9Sstevel@tonic-gate #	undef getchar
427c478bd9Sstevel@tonic-gate #endif
437c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
447c478bd9Sstevel@tonic-gate 
457c478bd9Sstevel@tonic-gate #include "ex.h"
467c478bd9Sstevel@tonic-gate #include "ex_re.h"
477c478bd9Sstevel@tonic-gate #include "ex_tty.h"
487c478bd9Sstevel@tonic-gate #include "ex_vis.h"
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate /*
517c478bd9Sstevel@tonic-gate  * Random routines, in alphabetical order.
527c478bd9Sstevel@tonic-gate  */
537c478bd9Sstevel@tonic-gate 
54f6db9f27Scf46844 int
any(int c,unsigned char * s)55f6db9f27Scf46844 any(int c, unsigned char *s)
567c478bd9Sstevel@tonic-gate {
57f6db9f27Scf46844 	int x;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	while (x = *s++)
607c478bd9Sstevel@tonic-gate 		if (x == c)
617c478bd9Sstevel@tonic-gate 			return (1);
627c478bd9Sstevel@tonic-gate 	return (0);
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
65f6db9f27Scf46844 int
backtab(int i)66f6db9f27Scf46844 backtab(int i)
677c478bd9Sstevel@tonic-gate {
68f6db9f27Scf46844 	int j;
697c478bd9Sstevel@tonic-gate 
707c478bd9Sstevel@tonic-gate 	j = i % value(vi_SHIFTWIDTH);
717c478bd9Sstevel@tonic-gate 	if (j == 0)
727c478bd9Sstevel@tonic-gate 		j = value(vi_SHIFTWIDTH);
737c478bd9Sstevel@tonic-gate 	i -= j;
747c478bd9Sstevel@tonic-gate 	if (i < 0)
757c478bd9Sstevel@tonic-gate 		i = 0;
767c478bd9Sstevel@tonic-gate 	return (i);
777c478bd9Sstevel@tonic-gate }
787c478bd9Sstevel@tonic-gate 
79f6db9f27Scf46844 void
change(void)80f6db9f27Scf46844 change(void)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	tchng++;
847c478bd9Sstevel@tonic-gate 	chng = tchng;
857c478bd9Sstevel@tonic-gate }
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * Column returns the number of
897c478bd9Sstevel@tonic-gate  * columns occupied by printing the
907c478bd9Sstevel@tonic-gate  * characters through position cp of the
917c478bd9Sstevel@tonic-gate  * current line.
927c478bd9Sstevel@tonic-gate  */
93f6db9f27Scf46844 int
column(unsigned char * cp)94f6db9f27Scf46844 column(unsigned char *cp)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	if (cp == 0)
987c478bd9Sstevel@tonic-gate 		cp = &linebuf[LBSIZE - 2];
99f6db9f27Scf46844 	return (qcolumn(cp, (unsigned char *)0));
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate /* lcolumn is same as column except it returns number of columns
1037c478bd9Sstevel@tonic-gate  * occupied by characters before position
1047c478bd9Sstevel@tonic-gate  * cp of the current line
1057c478bd9Sstevel@tonic-gate  */
106f6db9f27Scf46844 int
lcolumn(unsigned char * cp)107f6db9f27Scf46844 lcolumn(unsigned char *cp)
1087c478bd9Sstevel@tonic-gate {
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	if (cp == 0)
1117c478bd9Sstevel@tonic-gate 		cp = &linebuf[LBSIZE - 2];
112f6db9f27Scf46844 	return (nqcolumn(lastchr(linebuf, cp), (unsigned char *)0));
1137c478bd9Sstevel@tonic-gate }
1147c478bd9Sstevel@tonic-gate 
1157c478bd9Sstevel@tonic-gate /*
1167c478bd9Sstevel@tonic-gate  * Ignore a comment to the end of the line.
1177c478bd9Sstevel@tonic-gate  * This routine eats the trailing newline so don't call donewline().
1187c478bd9Sstevel@tonic-gate  */
119f6db9f27Scf46844 void
comment(void)120f6db9f27Scf46844 comment(void)
1217c478bd9Sstevel@tonic-gate {
122f6db9f27Scf46844 	int c;
1237c478bd9Sstevel@tonic-gate 
1247c478bd9Sstevel@tonic-gate 	do {
1257c478bd9Sstevel@tonic-gate 		c = getchar();
1267c478bd9Sstevel@tonic-gate 	} while (c != '\n' && c != EOF);
1277c478bd9Sstevel@tonic-gate 	if (c == EOF)
1287c478bd9Sstevel@tonic-gate 		ungetchar(c);
1297c478bd9Sstevel@tonic-gate }
1307c478bd9Sstevel@tonic-gate 
131f6db9f27Scf46844 void
Copy(unsigned char * to,unsigned char * from,int size)132f6db9f27Scf46844 Copy(unsigned char *to, unsigned char *from, int size)
1337c478bd9Sstevel@tonic-gate {
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	if (size > 0)
1367c478bd9Sstevel@tonic-gate 		do
1377c478bd9Sstevel@tonic-gate 			*to++ = *from++;
1387c478bd9Sstevel@tonic-gate 		while (--size > 0);
1397c478bd9Sstevel@tonic-gate }
1407c478bd9Sstevel@tonic-gate 
141f6db9f27Scf46844 void
copyw(line * to,line * from,int size)142f6db9f27Scf46844 copyw(line *to, line *from, int size)
1437c478bd9Sstevel@tonic-gate {
1447c478bd9Sstevel@tonic-gate 
1457c478bd9Sstevel@tonic-gate 	if (size > 0)
1467c478bd9Sstevel@tonic-gate 		do
1477c478bd9Sstevel@tonic-gate 			*to++ = *from++;
1487c478bd9Sstevel@tonic-gate 		while (--size > 0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
151f6db9f27Scf46844 void
copywR(line * to,line * from,int size)152f6db9f27Scf46844 copywR(line *to, line *from, int size)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 
1557c478bd9Sstevel@tonic-gate 	while (--size >= 0)
1567c478bd9Sstevel@tonic-gate 		to[size] = from[size];
1577c478bd9Sstevel@tonic-gate }
1587c478bd9Sstevel@tonic-gate 
159f6db9f27Scf46844 int
ctlof(int c)160f6db9f27Scf46844 ctlof(int c)
1617c478bd9Sstevel@tonic-gate {
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	return (c == DELETE ? '?' : c | ('A' - 1));
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
166f6db9f27Scf46844 void
dingdong(void)167f6db9f27Scf46844 dingdong(void)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	if (flash_screen && value(vi_FLASH))
171f6db9f27Scf46844 		putpad((unsigned char *)flash_screen);
1727c478bd9Sstevel@tonic-gate 	else if (value(vi_ERRORBELLS))
173f6db9f27Scf46844 		putpad((unsigned char *)bell);
1747c478bd9Sstevel@tonic-gate }
1757c478bd9Sstevel@tonic-gate 
176f6db9f27Scf46844 int
fixindent(int indent)177f6db9f27Scf46844 fixindent(int indent)
1787c478bd9Sstevel@tonic-gate {
179f6db9f27Scf46844 	int i;
180f6db9f27Scf46844 	unsigned char *cp;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	i = whitecnt(genbuf);
1837c478bd9Sstevel@tonic-gate 	cp = vpastwh(genbuf);
1847c478bd9Sstevel@tonic-gate 	if (*cp == 0 && i == indent && linebuf[0] == 0) {
1857c478bd9Sstevel@tonic-gate 		genbuf[0] = 0;
1867c478bd9Sstevel@tonic-gate 		return (i);
1877c478bd9Sstevel@tonic-gate 	}
1887c478bd9Sstevel@tonic-gate 	CP(genindent(i), cp);
1897c478bd9Sstevel@tonic-gate 	return (i);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate 
192f6db9f27Scf46844 void
filioerr(unsigned char * cp)193f6db9f27Scf46844 filioerr(unsigned char *cp)
1947c478bd9Sstevel@tonic-gate {
195f6db9f27Scf46844 	int oerrno = errno;
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 	lprintf("\"%s\"", cp);
1987c478bd9Sstevel@tonic-gate 	errno = oerrno;
1997c478bd9Sstevel@tonic-gate 	syserror(1);
2007c478bd9Sstevel@tonic-gate }
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate unsigned char *
genindent(indent)2037c478bd9Sstevel@tonic-gate genindent(indent)
204f6db9f27Scf46844 	int indent;
2057c478bd9Sstevel@tonic-gate {
206f6db9f27Scf46844 	unsigned char *cp;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	for (cp = genbuf; indent >= value(vi_TABSTOP); indent -= value(vi_TABSTOP))
2097c478bd9Sstevel@tonic-gate 		*cp++ = '\t';
2107c478bd9Sstevel@tonic-gate 	for (; indent > 0; indent--)
2117c478bd9Sstevel@tonic-gate 		*cp++ = ' ';
2127c478bd9Sstevel@tonic-gate 	return (cp);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
215f6db9f27Scf46844 void
getDOT(void)216f6db9f27Scf46844 getDOT(void)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 
219*23a1cceaSRoger A. Faulkner 	getaline(*dot);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate line *
getmark(c)2237c478bd9Sstevel@tonic-gate getmark(c)
224f6db9f27Scf46844 	int c;
2257c478bd9Sstevel@tonic-gate {
226f6db9f27Scf46844 	line *addr;
2277c478bd9Sstevel@tonic-gate 
2287c478bd9Sstevel@tonic-gate 	for (addr = one; addr <= dol; addr++)
2297c478bd9Sstevel@tonic-gate 		if (names[c - 'a'] == (*addr &~ 01)) {
2307c478bd9Sstevel@tonic-gate 			return (addr);
2317c478bd9Sstevel@tonic-gate 		}
2327c478bd9Sstevel@tonic-gate 	return (0);
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
235f6db9f27Scf46844 void
ignnEOF(void)236f6db9f27Scf46844 ignnEOF(void)
2377c478bd9Sstevel@tonic-gate {
238f6db9f27Scf46844 	int c = getchar();
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (c == EOF)
2417c478bd9Sstevel@tonic-gate 		ungetchar(c);
2427c478bd9Sstevel@tonic-gate 	else if (c=='"')
2437c478bd9Sstevel@tonic-gate 		comment();
2447c478bd9Sstevel@tonic-gate }
2457c478bd9Sstevel@tonic-gate 
246f6db9f27Scf46844 int
iswhite(int c)247f6db9f27Scf46844 iswhite(int c)
2487c478bd9Sstevel@tonic-gate {
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	return (c == ' ' || c == '\t');
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
253f6db9f27Scf46844 int
junk(wchar_t c)254f6db9f27Scf46844 junk(wchar_t c)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (c && !value(vi_BEAUTIFY))
2587c478bd9Sstevel@tonic-gate 		return (0);
2597c478bd9Sstevel@tonic-gate 	if (c >= ' ' && c != DELETE)
2607c478bd9Sstevel@tonic-gate 		return (0);
2617c478bd9Sstevel@tonic-gate 	switch (c) {
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	case '\t':
2647c478bd9Sstevel@tonic-gate 	case '\n':
2657c478bd9Sstevel@tonic-gate 	case '\f':
2667c478bd9Sstevel@tonic-gate 		return (0);
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	default:
2697c478bd9Sstevel@tonic-gate 		return (1);
2707c478bd9Sstevel@tonic-gate 	}
2717c478bd9Sstevel@tonic-gate }
2727c478bd9Sstevel@tonic-gate 
273f6db9f27Scf46844 void
killed(void)274f6db9f27Scf46844 killed(void)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 
2777c478bd9Sstevel@tonic-gate 	killcnt(addr2 - addr1 + 1);
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
280f6db9f27Scf46844 void
killcnt(int cnt)281f6db9f27Scf46844 killcnt(int cnt)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 	extern char *verbalize();
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate 	if (inopen) {
2867c478bd9Sstevel@tonic-gate 		notecnt = cnt;
2877c478bd9Sstevel@tonic-gate 		notenam = notesgn = (unsigned char *)"";
2887c478bd9Sstevel@tonic-gate 		return;
2897c478bd9Sstevel@tonic-gate 	}
2907c478bd9Sstevel@tonic-gate 	if (!notable(cnt))
2917c478bd9Sstevel@tonic-gate 		return;
2927c478bd9Sstevel@tonic-gate 	if (value(vi_TERSE) == 0) {
2937c478bd9Sstevel@tonic-gate 		verbalize(cnt, Command, "");
2947c478bd9Sstevel@tonic-gate 	} else {
2957c478bd9Sstevel@tonic-gate 		if (cnt == 1) {
296f6db9f27Scf46844 			viprintf(gettext("1 line"), cnt);
2977c478bd9Sstevel@tonic-gate 		} else {
298f6db9f27Scf46844 			viprintf(gettext("%d lines"), cnt);
2997c478bd9Sstevel@tonic-gate 		}
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 	putNFL();
3027c478bd9Sstevel@tonic-gate }
3037c478bd9Sstevel@tonic-gate 
304f6db9f27Scf46844 int
lineno(line * a)305f6db9f27Scf46844 lineno(line *a)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate 	return (a - zero);
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
311f6db9f27Scf46844 int
lineDOL(void)312f6db9f27Scf46844 lineDOL(void)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (lineno(dol));
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
318f6db9f27Scf46844 int
lineDOT(void)319f6db9f27Scf46844 lineDOT(void)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (lineno(dot));
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
325f6db9f27Scf46844 void
markDOT(void)326f6db9f27Scf46844 markDOT(void)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	markpr(dot);
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
332f6db9f27Scf46844 void
markpr(line * which)333f6db9f27Scf46844 markpr(line *which)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	if ((inglobal == 0 || inopen) && which <= endcore) {
3377c478bd9Sstevel@tonic-gate 		names['z'-'a'+1] = *which & ~01;
3387c478bd9Sstevel@tonic-gate 		if (inopen)
3397c478bd9Sstevel@tonic-gate 			ncols['z'-'a'+1] = cursor;
3407c478bd9Sstevel@tonic-gate 	}
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
343f6db9f27Scf46844 int
markreg(int c)344f6db9f27Scf46844 markreg(int c)
3457c478bd9Sstevel@tonic-gate {
3467c478bd9Sstevel@tonic-gate 
3477c478bd9Sstevel@tonic-gate 	if (c == '\'' || c == '`')
3487c478bd9Sstevel@tonic-gate 		return ('z' + 1);
3497c478bd9Sstevel@tonic-gate 	if (c >= 'a' && c <= 'z')
3507c478bd9Sstevel@tonic-gate 		return (c);
3517c478bd9Sstevel@tonic-gate 	return (0);
3527c478bd9Sstevel@tonic-gate }
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate /*
3557c478bd9Sstevel@tonic-gate  * Mesg decodes the terse/verbose strings. Thus
3567c478bd9Sstevel@tonic-gate  *	'xxx@yyy' -> 'xxx' if terse, else 'xxx yyy'
3577c478bd9Sstevel@tonic-gate  *	'xxx|yyy' -> 'xxx' if terse, else 'yyy'
3587c478bd9Sstevel@tonic-gate  * All others map to themselves.
3597c478bd9Sstevel@tonic-gate  */
3607c478bd9Sstevel@tonic-gate /*
3617c478bd9Sstevel@tonic-gate  * The feature described above was disabled for localizable messaging.
3627c478bd9Sstevel@tonic-gate  */
3637c478bd9Sstevel@tonic-gate unsigned char *
mesg(str)3647c478bd9Sstevel@tonic-gate mesg(str)
365f6db9f27Scf46844 	unsigned char *str;
3667c478bd9Sstevel@tonic-gate {
367f6db9f27Scf46844 	unsigned char *cp;
3687c478bd9Sstevel@tonic-gate 
3697c478bd9Sstevel@tonic-gate 	str = (unsigned char *)strcpy(genbuf, str);
3707c478bd9Sstevel@tonic-gate 	/* commented out for localizable messaging */
3717c478bd9Sstevel@tonic-gate /*	for (cp = str; *cp; cp++)
3727c478bd9Sstevel@tonic-gate 		switch (*cp) {
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 		case '@':
3757c478bd9Sstevel@tonic-gate 			if (value(vi_TERSE))
3767c478bd9Sstevel@tonic-gate 				*cp = 0;
3777c478bd9Sstevel@tonic-gate 			else
3787c478bd9Sstevel@tonic-gate 				*cp = ' ';
3797c478bd9Sstevel@tonic-gate 			break;
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		case '|':
3827c478bd9Sstevel@tonic-gate 			if (value(vi_TERSE) == 0)
3837c478bd9Sstevel@tonic-gate 				return (cp + 1);
3847c478bd9Sstevel@tonic-gate 			*cp = 0;
3857c478bd9Sstevel@tonic-gate 			break;
3867c478bd9Sstevel@tonic-gate 		}	*/
3877c478bd9Sstevel@tonic-gate 	return (str);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*VARARGS2*/
391f6db9f27Scf46844 void
merror(unsigned char * seekpt,int i)392f6db9f27Scf46844 merror(unsigned char *seekpt, int i)
3937c478bd9Sstevel@tonic-gate {
394f6db9f27Scf46844 	unsigned char *cp = linebuf;
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (seekpt == 0)
3977c478bd9Sstevel@tonic-gate 		return;
3987c478bd9Sstevel@tonic-gate 	merror1(seekpt);
3997c478bd9Sstevel@tonic-gate 	if (*cp == '\n')
4007c478bd9Sstevel@tonic-gate 		putnl(), cp++;
4017c478bd9Sstevel@tonic-gate 	if (inopen > 0 && clr_eol)
4027c478bd9Sstevel@tonic-gate 		vclreol();
4037c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
404f6db9f27Scf46844 		putpad((unsigned char *)enter_standout_mode);
4057c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
406f6db9f27Scf46844 	viprintf(mesg(cp), i);
4077c478bd9Sstevel@tonic-gate #else
408f6db9f27Scf46844 	viprintf((char *)mesg(cp), i);
4097c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
4107c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
411f6db9f27Scf46844 		putpad((unsigned char *)exit_bold);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
414f6db9f27Scf46844 void
merror1(unsigned char * seekpt)415f6db9f27Scf46844 merror1(unsigned char *seekpt)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	strcpy(linebuf, seekpt);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate #define MAXDATA (56*1024)
422f6db9f27Scf46844 int
morelines(void)423f6db9f27Scf46844 morelines(void)
4247c478bd9Sstevel@tonic-gate {
425f6db9f27Scf46844 	unsigned char *end;
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	if ((int) sbrk(1024 * sizeof (line)) == -1) {
4287c478bd9Sstevel@tonic-gate 		if (endcore >= (line *) MAXDATA)
4297c478bd9Sstevel@tonic-gate 			return -1;
4307c478bd9Sstevel@tonic-gate 		end = (unsigned char *) MAXDATA;
4317c478bd9Sstevel@tonic-gate 		/*
4327c478bd9Sstevel@tonic-gate 		 * Ask for end+2 sice we want end to be the last used location.
4337c478bd9Sstevel@tonic-gate 		 */
4347c478bd9Sstevel@tonic-gate 		while (brk(end+2) == -1)
4357c478bd9Sstevel@tonic-gate 			end -= 64;
4367c478bd9Sstevel@tonic-gate 		if (end <= (unsigned char *) endcore)
4377c478bd9Sstevel@tonic-gate 			return -1;
4387c478bd9Sstevel@tonic-gate 		endcore = (line *) end;
4397c478bd9Sstevel@tonic-gate 	} else {
4407c478bd9Sstevel@tonic-gate 		endcore += 1024;
4417c478bd9Sstevel@tonic-gate 	}
4427c478bd9Sstevel@tonic-gate 	return (0);
4437c478bd9Sstevel@tonic-gate }
4447c478bd9Sstevel@tonic-gate 
445f6db9f27Scf46844 void
nonzero(void)446f6db9f27Scf46844 nonzero(void)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	if (addr1 == zero) {
4507c478bd9Sstevel@tonic-gate 		notempty();
4517c478bd9Sstevel@tonic-gate 		error(value(vi_TERSE) ? gettext("Nonzero address required") :
4527c478bd9Sstevel@tonic-gate gettext("Nonzero address required on this command"));
4537c478bd9Sstevel@tonic-gate 	}
4547c478bd9Sstevel@tonic-gate }
4557c478bd9Sstevel@tonic-gate 
456f6db9f27Scf46844 int
notable(int i)457f6db9f27Scf46844 notable(int i)
4587c478bd9Sstevel@tonic-gate {
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	return (hush == 0 && !inglobal && i > value(vi_REPORT));
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 
464f6db9f27Scf46844 void
notempty(void)465f6db9f27Scf46844 notempty(void)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate 	if (dol == zero)
4697c478bd9Sstevel@tonic-gate 		error(value(vi_TERSE) ? gettext("No lines") :
4707c478bd9Sstevel@tonic-gate gettext("No lines in the buffer"));
4717c478bd9Sstevel@tonic-gate }
4727c478bd9Sstevel@tonic-gate 
4737c478bd9Sstevel@tonic-gate 
474f6db9f27Scf46844 void
netchHAD(int cnt)475f6db9f27Scf46844 netchHAD(int cnt)
4767c478bd9Sstevel@tonic-gate {
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate 	netchange(lineDOL() - cnt);
4797c478bd9Sstevel@tonic-gate }
4807c478bd9Sstevel@tonic-gate 
481f6db9f27Scf46844 void
netchange(int i)482f6db9f27Scf46844 netchange(int i)
4837c478bd9Sstevel@tonic-gate {
484f6db9f27Scf46844 	unsigned char *cp;
4857c478bd9Sstevel@tonic-gate 
4867c478bd9Sstevel@tonic-gate 	if (i > 0)
4877c478bd9Sstevel@tonic-gate 		notesgn = cp = (unsigned char *)"more ";
4887c478bd9Sstevel@tonic-gate 	else
4897c478bd9Sstevel@tonic-gate 		notesgn = cp = (unsigned char *)"fewer ", i = -i;
4907c478bd9Sstevel@tonic-gate 	if (inopen) {
4917c478bd9Sstevel@tonic-gate 		notecnt = i;
4927c478bd9Sstevel@tonic-gate 		notenam = (unsigned char *)"";
4937c478bd9Sstevel@tonic-gate 		return;
4947c478bd9Sstevel@tonic-gate 	}
4957c478bd9Sstevel@tonic-gate 	if (!notable(i))
4967c478bd9Sstevel@tonic-gate 		return;
4977c478bd9Sstevel@tonic-gate 	if (*cp == 'm')	/* for ease of messge localization */
4987c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
499f6db9f27Scf46844 		viprintf(mesg(value(vi_TERSE) ?
5007c478bd9Sstevel@tonic-gate #else
501f6db9f27Scf46844 		viprintf((char *)mesg(value(vi_TERSE) ?
5027c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
5037c478bd9Sstevel@tonic-gate gettext("%d more lines") :
5047c478bd9Sstevel@tonic-gate 		/*
5057c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
5067c478bd9Sstevel@tonic-gate 		 *	Reference order of arguments must not
5077c478bd9Sstevel@tonic-gate 		 *	be changed using '%digit$', since vi's
508f6db9f27Scf46844 		 *	viprintf() does not support it.
5097c478bd9Sstevel@tonic-gate 		 */
5107c478bd9Sstevel@tonic-gate gettext("%d more lines in file after %s")), i, Command);
5117c478bd9Sstevel@tonic-gate 	else
5127c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
513f6db9f27Scf46844 		viprintf(mesg(value(vi_TERSE) ?
5147c478bd9Sstevel@tonic-gate #else
515f6db9f27Scf46844 		viprintf((char *)mesg(value(vi_TERSE) ?
5167c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
5177c478bd9Sstevel@tonic-gate gettext("%d fewer lines") :
5187c478bd9Sstevel@tonic-gate 		/*
5197c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
5207c478bd9Sstevel@tonic-gate 		 *	Reference order of arguments must not
5217c478bd9Sstevel@tonic-gate 		 *	be changed using '%digit$', since vi's
522f6db9f27Scf46844 		 *	viprintf() does not support it.
5237c478bd9Sstevel@tonic-gate 		 */
5247c478bd9Sstevel@tonic-gate gettext("%d fewer lines in file after %s")), i, Command);
5257c478bd9Sstevel@tonic-gate 	putNFL();
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
528f6db9f27Scf46844 void
putmark(line * addr)529f6db9f27Scf46844 putmark(line *addr)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 
5327c478bd9Sstevel@tonic-gate 	putmk1(addr, putline());
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
535f6db9f27Scf46844 void
putmk1(line * addr,int n)536f6db9f27Scf46844 putmk1(line *addr, int n)
5377c478bd9Sstevel@tonic-gate {
538f6db9f27Scf46844 	line *markp;
539f6db9f27Scf46844 	int oldglobmk;
5407c478bd9Sstevel@tonic-gate 
5417c478bd9Sstevel@tonic-gate 	oldglobmk = *addr & 1;
5427c478bd9Sstevel@tonic-gate 	*addr &= ~1;
5437c478bd9Sstevel@tonic-gate 	for (markp = (anymarks ? names : &names['z'-'a'+1]);
5447c478bd9Sstevel@tonic-gate 	  markp <= &names['z'-'a'+1]; markp++)
5457c478bd9Sstevel@tonic-gate 		if (*markp == *addr)
5467c478bd9Sstevel@tonic-gate 			*markp = n;
5477c478bd9Sstevel@tonic-gate 	*addr = n | oldglobmk;
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate unsigned char *
plural(i)5517c478bd9Sstevel@tonic-gate plural(i)
5527c478bd9Sstevel@tonic-gate 	long i;
5537c478bd9Sstevel@tonic-gate {
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	return (i == 1 ? (unsigned char *)"" : (unsigned char *)"s");
5567c478bd9Sstevel@tonic-gate }
5577c478bd9Sstevel@tonic-gate 
5587c478bd9Sstevel@tonic-gate int	qcount();
5597c478bd9Sstevel@tonic-gate short	vcntcol;
5607c478bd9Sstevel@tonic-gate 
561f6db9f27Scf46844 int
qcolumn(unsigned char * lim,unsigned char * gp)562f6db9f27Scf46844 qcolumn(unsigned char *lim, unsigned char *gp)
5637c478bd9Sstevel@tonic-gate {
564f6db9f27Scf46844 	int x, length;
5657c478bd9Sstevel@tonic-gate 	int	col;
5667c478bd9Sstevel@tonic-gate 	wchar_t wchar;
5677c478bd9Sstevel@tonic-gate 	int (*OO)();
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	OO = Outchar;
5707c478bd9Sstevel@tonic-gate 	Outchar = qcount;
5717c478bd9Sstevel@tonic-gate 	vcntcol = 0;
5727c478bd9Sstevel@tonic-gate 	if (lim != NULL) {
5737c478bd9Sstevel@tonic-gate 		if(lim == linebuf - 1 || lim == &linebuf[LBSIZE-2])
5747c478bd9Sstevel@tonic-gate 			length = 1;
5757c478bd9Sstevel@tonic-gate 		else
5767c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)lim, MULTI_BYTE_MAX);
5777c478bd9Sstevel@tonic-gate 		if(length < 0)
5787c478bd9Sstevel@tonic-gate 			length = 1;
5797c478bd9Sstevel@tonic-gate 		x = lim[length];
5807c478bd9Sstevel@tonic-gate 		lim[length] = 0;
5817c478bd9Sstevel@tonic-gate 	}
5827c478bd9Sstevel@tonic-gate 	pline(0);
5837c478bd9Sstevel@tonic-gate 	if (lim != NULL)
5847c478bd9Sstevel@tonic-gate 		lim[length] = x;
5857c478bd9Sstevel@tonic-gate 	if(length > 1 && !gp) {
5867c478bd9Sstevel@tonic-gate 		/* put cursor at beginning of multibyte character */
5877c478bd9Sstevel@tonic-gate 		if ((col = wcwidth(wchar)) < 0)
5887c478bd9Sstevel@tonic-gate 			col = 0;
5897c478bd9Sstevel@tonic-gate 		vcntcol = vcntcol - col + 1;
5907c478bd9Sstevel@tonic-gate 	}
5917c478bd9Sstevel@tonic-gate  	if (gp)
5927c478bd9Sstevel@tonic-gate 		while (*gp) {
5937c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)gp, MULTI_BYTE_MAX);
5947c478bd9Sstevel@tonic-gate 			if(length < 0) {
5957c478bd9Sstevel@tonic-gate 				putoctal = 1;
5967c478bd9Sstevel@tonic-gate 				putchar(*gp++);
5977c478bd9Sstevel@tonic-gate 				putoctal = 0;
5987c478bd9Sstevel@tonic-gate 			} else {
5997c478bd9Sstevel@tonic-gate 				putchar(wchar);
6007c478bd9Sstevel@tonic-gate 				gp += length;
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 	Outchar = OO;
6047c478bd9Sstevel@tonic-gate 	return (vcntcol);
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate /* This routine puts cursor after multibyte character */
608f6db9f27Scf46844 int
nqcolumn(unsigned char * lim,unsigned char * gp)609f6db9f27Scf46844 nqcolumn(unsigned char *lim, unsigned char *gp)
6107c478bd9Sstevel@tonic-gate {
611f6db9f27Scf46844 	int x, length;
6127c478bd9Sstevel@tonic-gate 	wchar_t wchar;
6137c478bd9Sstevel@tonic-gate 	int (*OO)();
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 	OO = Outchar;
6167c478bd9Sstevel@tonic-gate 	Outchar = qcount;
6177c478bd9Sstevel@tonic-gate 	vcntcol = 0;
6187c478bd9Sstevel@tonic-gate 	if (lim != NULL) {
6197c478bd9Sstevel@tonic-gate 		if(lim == linebuf - 1 || lim == &linebuf[LBSIZE-2])
6207c478bd9Sstevel@tonic-gate 			length = 1;
6217c478bd9Sstevel@tonic-gate 		else
6227c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)lim, MULTI_BYTE_MAX);
6237c478bd9Sstevel@tonic-gate 		if(length < 0)
6247c478bd9Sstevel@tonic-gate 			length = 1;
6257c478bd9Sstevel@tonic-gate 		x = lim[length];
6267c478bd9Sstevel@tonic-gate 		lim[length] = 0;
6277c478bd9Sstevel@tonic-gate 	}
6287c478bd9Sstevel@tonic-gate 	pline(0);
6297c478bd9Sstevel@tonic-gate 	if (lim != NULL)
6307c478bd9Sstevel@tonic-gate 		lim[length] = x;
6317c478bd9Sstevel@tonic-gate  	if (gp)
6327c478bd9Sstevel@tonic-gate 		while (*gp) {
6337c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)gp, MULTI_BYTE_MAX);
6347c478bd9Sstevel@tonic-gate 			if(length < 0) {
6357c478bd9Sstevel@tonic-gate 				putoctal = 1;
6367c478bd9Sstevel@tonic-gate 				putchar(*gp++);
6377c478bd9Sstevel@tonic-gate 				putoctal = 0;
6387c478bd9Sstevel@tonic-gate 			} else {
6397c478bd9Sstevel@tonic-gate 				putchar(wchar);
6407c478bd9Sstevel@tonic-gate 				gp += length;
6417c478bd9Sstevel@tonic-gate 			}
6427c478bd9Sstevel@tonic-gate 		}
6437c478bd9Sstevel@tonic-gate 	Outchar = OO;
6447c478bd9Sstevel@tonic-gate 	return (vcntcol);
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate 
6477c478bd9Sstevel@tonic-gate int
qcount(c)6487c478bd9Sstevel@tonic-gate qcount(c)
6497c478bd9Sstevel@tonic-gate wchar_t c;
6507c478bd9Sstevel@tonic-gate {
651f6db9f27Scf46844 	int cols;
6527c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
653f6db9f27Scf46844 	int remcols;
654f6db9f27Scf46844 	short OWCOLS;
6557c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 	if (c == '\t') {
6587c478bd9Sstevel@tonic-gate 		vcntcol += value(vi_TABSTOP) - vcntcol % value(vi_TABSTOP);
659f6db9f27Scf46844 		return (0);
6607c478bd9Sstevel@tonic-gate 	}
6617c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
6627c478bd9Sstevel@tonic-gate 	if ((cols = wcwidth(c)) > 0)
6637c478bd9Sstevel@tonic-gate 		vcntcol += cols;
6647c478bd9Sstevel@tonic-gate #else
6657c478bd9Sstevel@tonic-gate 	if ((cols = wcwidth(c)) < 0)
6667c478bd9Sstevel@tonic-gate 		cols = 0;
6677c478bd9Sstevel@tonic-gate 	OWCOLS = WCOLS;
6687c478bd9Sstevel@tonic-gate 	if (WCOLS == 0)
6697c478bd9Sstevel@tonic-gate 		WCOLS = columns;
6707c478bd9Sstevel@tonic-gate 	if ((mc_wrap) == 1 && (remcols = (WCOLS - (vcntcol % WCOLS))) < cols)
6717c478bd9Sstevel@tonic-gate 		vcntcol += remcols;
6727c478bd9Sstevel@tonic-gate 	WCOLS = OWCOLS;
6737c478bd9Sstevel@tonic-gate 	vcntcol += cols;
6747c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
675f6db9f27Scf46844 	return (0);
6767c478bd9Sstevel@tonic-gate }
6777c478bd9Sstevel@tonic-gate 
678f6db9f27Scf46844 void
reverse(line * a1,line * a2)679f6db9f27Scf46844 reverse(line *a1, line *a2)
6807c478bd9Sstevel@tonic-gate {
681f6db9f27Scf46844 	line t;
6827c478bd9Sstevel@tonic-gate 
6837c478bd9Sstevel@tonic-gate 	for (;;) {
6847c478bd9Sstevel@tonic-gate 		t = *--a2;
6857c478bd9Sstevel@tonic-gate 		if (a2 <= a1)
6867c478bd9Sstevel@tonic-gate 			return;
6877c478bd9Sstevel@tonic-gate 		*a2 = *a1;
6887c478bd9Sstevel@tonic-gate 		*a1++ = t;
6897c478bd9Sstevel@tonic-gate 	}
6907c478bd9Sstevel@tonic-gate }
6917c478bd9Sstevel@tonic-gate 
692f6db9f27Scf46844 void
save(line * a1,line * a2)693f6db9f27Scf46844 save(line *a1, line *a2)
6947c478bd9Sstevel@tonic-gate {
695f6db9f27Scf46844 	int more;
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	if (!FIXUNDO)
6987c478bd9Sstevel@tonic-gate 		return;
6997c478bd9Sstevel@tonic-gate #ifdef UNDOTRACE
7007c478bd9Sstevel@tonic-gate 	if (trace)
7017c478bd9Sstevel@tonic-gate 		vudump("before save");
7027c478bd9Sstevel@tonic-gate #endif
7037c478bd9Sstevel@tonic-gate 	undkind = UNDNONE;
7047c478bd9Sstevel@tonic-gate 	undadot = dot;
7057c478bd9Sstevel@tonic-gate 	more = (a2 - a1 + 1) - (unddol - dol);
7067c478bd9Sstevel@tonic-gate 	while (more > (endcore - truedol))
7077c478bd9Sstevel@tonic-gate 		if (morelines() < 0)
7087c478bd9Sstevel@tonic-gate 			error(value(vi_TERSE) ? gettext("Out of memory") :
7097c478bd9Sstevel@tonic-gate gettext("Out of memory saving lines for undo - try using ed"));
7107c478bd9Sstevel@tonic-gate 	if (more)
7117c478bd9Sstevel@tonic-gate 		(*(more > 0 ? copywR : copyw))(unddol + more + 1, unddol + 1,
7127c478bd9Sstevel@tonic-gate 		    (truedol - unddol));
7137c478bd9Sstevel@tonic-gate 	unddol += more;
7147c478bd9Sstevel@tonic-gate 	truedol += more;
7157c478bd9Sstevel@tonic-gate 	copyw(dol + 1, a1, a2 - a1 + 1);
7167c478bd9Sstevel@tonic-gate 	undkind = UNDALL;
7177c478bd9Sstevel@tonic-gate 	unddel = a1 - 1;
7187c478bd9Sstevel@tonic-gate 	undap1 = a1;
7197c478bd9Sstevel@tonic-gate 	undap2 = a2 + 1;
7207c478bd9Sstevel@tonic-gate #ifdef UNDOTRACE
7217c478bd9Sstevel@tonic-gate 	if (trace)
7227c478bd9Sstevel@tonic-gate 		vudump("after save");
7237c478bd9Sstevel@tonic-gate #endif
7247c478bd9Sstevel@tonic-gate }
7257c478bd9Sstevel@tonic-gate 
726f6db9f27Scf46844 void
save12(void)727f6db9f27Scf46844 save12(void)
7287c478bd9Sstevel@tonic-gate {
7297c478bd9Sstevel@tonic-gate 
7307c478bd9Sstevel@tonic-gate 	save(addr1, addr2);
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
733f6db9f27Scf46844 void
saveall(void)734f6db9f27Scf46844 saveall(void)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	save(one, dol);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
740f6db9f27Scf46844 int
span(void)741f6db9f27Scf46844 span(void)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	return (addr2 - addr1 + 1);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
747f6db9f27Scf46844 void
sync(void)748f6db9f27Scf46844 sync(void)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	chng = 0;
7527c478bd9Sstevel@tonic-gate 	tchng = 0;
7537c478bd9Sstevel@tonic-gate 	xchng = 0;
7547c478bd9Sstevel@tonic-gate }
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate 
757f6db9f27Scf46844 int
skipwh(void)758f6db9f27Scf46844 skipwh(void)
7597c478bd9Sstevel@tonic-gate {
760f6db9f27Scf46844 	int wh;
7617c478bd9Sstevel@tonic-gate 
7627c478bd9Sstevel@tonic-gate 	wh = 0;
7637c478bd9Sstevel@tonic-gate 	while (iswhite(peekchar())) {
7647c478bd9Sstevel@tonic-gate 		wh++;
7657c478bd9Sstevel@tonic-gate 		ignchar();
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 	return (wh);
7687c478bd9Sstevel@tonic-gate }
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate /*VARARGS2*/
771f6db9f27Scf46844 void
smerror(unsigned char * seekpt,unsigned char * cp)772f6db9f27Scf46844 smerror(unsigned char *seekpt, unsigned char *cp)
7737c478bd9Sstevel@tonic-gate {
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	errcnt++;
7767c478bd9Sstevel@tonic-gate 	merror1(seekpt);
7777c478bd9Sstevel@tonic-gate 	if (inopen && clr_eol)
7787c478bd9Sstevel@tonic-gate 		vclreol();
7797c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
780f6db9f27Scf46844 		putpad((unsigned char *)enter_standout_mode);
7817c478bd9Sstevel@tonic-gate 	lprintf(mesg(linebuf), cp);
7827c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
783f6db9f27Scf46844 		putpad((unsigned char *)exit_bold);
7847c478bd9Sstevel@tonic-gate }
7857c478bd9Sstevel@tonic-gate 
7867c478bd9Sstevel@tonic-gate unsigned char *
strend(cp)7877c478bd9Sstevel@tonic-gate strend(cp)
788f6db9f27Scf46844 	unsigned char *cp;
7897c478bd9Sstevel@tonic-gate {
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	while (*cp)
7927c478bd9Sstevel@tonic-gate 		cp++;
7937c478bd9Sstevel@tonic-gate 	return (cp);
7947c478bd9Sstevel@tonic-gate }
7957c478bd9Sstevel@tonic-gate 
796f6db9f27Scf46844 void
strcLIN(unsigned char * dp)797f6db9f27Scf46844 strcLIN(unsigned char *dp)
7987c478bd9Sstevel@tonic-gate {
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	CP(linebuf, dp);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate /*
8047c478bd9Sstevel@tonic-gate  * A system error has occurred that we need to perror.
8057c478bd9Sstevel@tonic-gate  * danger is true if we are unsure of the contents of
8067c478bd9Sstevel@tonic-gate  * the file or our buffer, e.g. a write error in the
8077c478bd9Sstevel@tonic-gate  * middle of a write operation, or a temp file error.
8087c478bd9Sstevel@tonic-gate  */
809f6db9f27Scf46844 void
syserror(int danger)810f6db9f27Scf46844 syserror(int danger)
8117c478bd9Sstevel@tonic-gate {
812f6db9f27Scf46844 	int e = errno;
8137c478bd9Sstevel@tonic-gate 	char *errstr;
8147c478bd9Sstevel@tonic-gate 	extern char *strerror();
8157c478bd9Sstevel@tonic-gate 
8167c478bd9Sstevel@tonic-gate 	dirtcnt = 0;
8177c478bd9Sstevel@tonic-gate 	putchar(' ');
8187c478bd9Sstevel@tonic-gate 	if (danger)
8197c478bd9Sstevel@tonic-gate 		edited = 0;	/* for temp file errors, for example */
8207c478bd9Sstevel@tonic-gate 	if ((errstr = strerror(e)) != NULL)
8217c478bd9Sstevel@tonic-gate 		error(errstr);
8227c478bd9Sstevel@tonic-gate 	else
8237c478bd9Sstevel@tonic-gate 		error(gettext("System error %d"), e);
8247c478bd9Sstevel@tonic-gate }
8257c478bd9Sstevel@tonic-gate 
8267c478bd9Sstevel@tonic-gate /*
8277c478bd9Sstevel@tonic-gate  * Return the column number that results from being in column col and
8287c478bd9Sstevel@tonic-gate  * hitting a tab, where tabs are set every ts columns.  Work right for
8297c478bd9Sstevel@tonic-gate  * the case where col > columns, even if ts does not divide columns.
8307c478bd9Sstevel@tonic-gate  */
831f6db9f27Scf46844 int
tabcol(int col,int ts)832f6db9f27Scf46844 tabcol(int col, int ts)
8337c478bd9Sstevel@tonic-gate {
8347c478bd9Sstevel@tonic-gate 	int offset, result;
8357c478bd9Sstevel@tonic-gate 
8367c478bd9Sstevel@tonic-gate 	if (col >= columns) {
8377c478bd9Sstevel@tonic-gate 		offset = columns * (col/columns);
8387c478bd9Sstevel@tonic-gate 		col -= offset;
8397c478bd9Sstevel@tonic-gate 	} else
8407c478bd9Sstevel@tonic-gate 		offset = 0;
8417c478bd9Sstevel@tonic-gate 	result = col + ts - (col % ts) + offset;
8427c478bd9Sstevel@tonic-gate 	return (result);
8437c478bd9Sstevel@tonic-gate }
8447c478bd9Sstevel@tonic-gate 
8457c478bd9Sstevel@tonic-gate unsigned char *
vfindcol(i)8467c478bd9Sstevel@tonic-gate vfindcol(i)
8477c478bd9Sstevel@tonic-gate 	int i;
8487c478bd9Sstevel@tonic-gate {
849f6db9f27Scf46844 	unsigned char *cp, *oldcp;
850f6db9f27Scf46844 	int (*OO)() = Outchar;
851f6db9f27Scf46844 	int length;
8527c478bd9Sstevel@tonic-gate 	unsigned char x;
8537c478bd9Sstevel@tonic-gate 	wchar_t wchar;
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	Outchar = qcount;
856f6db9f27Scf46844 	(void) qcolumn(linebuf - 1, (unsigned char *)NOSTR);
8577c478bd9Sstevel@tonic-gate 	for (cp = linebuf; *cp && vcntcol < i; ) {
8587c478bd9Sstevel@tonic-gate 		oldcp = cp;
8597c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX);
8607c478bd9Sstevel@tonic-gate 		if(length < 0) {
8617c478bd9Sstevel@tonic-gate 			putoctal = 1;
8627c478bd9Sstevel@tonic-gate 			putchar(*cp++);
8637c478bd9Sstevel@tonic-gate 			putoctal = 0;
8647c478bd9Sstevel@tonic-gate 		} else {
8657c478bd9Sstevel@tonic-gate 			putchar(wchar);
8667c478bd9Sstevel@tonic-gate 			cp += length;
8677c478bd9Sstevel@tonic-gate 		}
8687c478bd9Sstevel@tonic-gate 	}
8697c478bd9Sstevel@tonic-gate 	if (cp != linebuf)
8707c478bd9Sstevel@tonic-gate 		cp = oldcp;
8717c478bd9Sstevel@tonic-gate 	Outchar = OO;
8727c478bd9Sstevel@tonic-gate 	return (cp);
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate unsigned char *
vskipwh(cp)8767c478bd9Sstevel@tonic-gate vskipwh(cp)
877f6db9f27Scf46844 	unsigned char *cp;
8787c478bd9Sstevel@tonic-gate {
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	while (iswhite(*cp) && cp[1])
8817c478bd9Sstevel@tonic-gate 		cp++;
8827c478bd9Sstevel@tonic-gate 	return (cp);
8837c478bd9Sstevel@tonic-gate }
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate unsigned char *
vpastwh(cp)8877c478bd9Sstevel@tonic-gate vpastwh(cp)
888f6db9f27Scf46844 	unsigned char *cp;
8897c478bd9Sstevel@tonic-gate {
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	while (iswhite(*cp))
8927c478bd9Sstevel@tonic-gate 		cp++;
8937c478bd9Sstevel@tonic-gate 	return (cp);
8947c478bd9Sstevel@tonic-gate }
8957c478bd9Sstevel@tonic-gate 
896f6db9f27Scf46844 int
whitecnt(unsigned char * cp)897f6db9f27Scf46844 whitecnt(unsigned char *cp)
8987c478bd9Sstevel@tonic-gate {
899f6db9f27Scf46844 	int i;
9007c478bd9Sstevel@tonic-gate 
9017c478bd9Sstevel@tonic-gate 	i = 0;
9027c478bd9Sstevel@tonic-gate 	for (;;)
9037c478bd9Sstevel@tonic-gate 		switch (*cp++) {
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 		case '\t':
9067c478bd9Sstevel@tonic-gate 			i += value(vi_TABSTOP) - i % value(vi_TABSTOP);
9077c478bd9Sstevel@tonic-gate 			break;
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate 		case ' ':
9107c478bd9Sstevel@tonic-gate 			i++;
9117c478bd9Sstevel@tonic-gate 			break;
9127c478bd9Sstevel@tonic-gate 
9137c478bd9Sstevel@tonic-gate 		default:
9147c478bd9Sstevel@tonic-gate 			return (i);
9157c478bd9Sstevel@tonic-gate 		}
9167c478bd9Sstevel@tonic-gate }
9177c478bd9Sstevel@tonic-gate 
918f6db9f27Scf46844 void
markit(line * addr)919f6db9f27Scf46844 markit(line *addr)
9207c478bd9Sstevel@tonic-gate {
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 	if (addr != dot && addr >= one && addr <= dol)
9237c478bd9Sstevel@tonic-gate 		markDOT();
9247c478bd9Sstevel@tonic-gate }
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate /*
9277c478bd9Sstevel@tonic-gate  * When a hangup occurs our actions are similar to a preserve
9287c478bd9Sstevel@tonic-gate  * command.  If the buffer has not been [Modified], then we do
9297c478bd9Sstevel@tonic-gate  * nothing but remove the temporary files and exit.
9307c478bd9Sstevel@tonic-gate  * Otherwise, we sync the temp file and then attempt a preserve.
9317c478bd9Sstevel@tonic-gate  * If the preserve succeeds, we unlink our temp files.
9327c478bd9Sstevel@tonic-gate  * If the preserve fails, we leave the temp files as they are
9337c478bd9Sstevel@tonic-gate  * as they are a backup even without preservation if they
9347c478bd9Sstevel@tonic-gate  * are not removed.
9357c478bd9Sstevel@tonic-gate  */
9367c478bd9Sstevel@tonic-gate 
9377c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9387c478bd9Sstevel@tonic-gate void
onhup(sig)9397c478bd9Sstevel@tonic-gate onhup(sig)
9407c478bd9Sstevel@tonic-gate int sig;
9417c478bd9Sstevel@tonic-gate {
9427c478bd9Sstevel@tonic-gate 
9437c478bd9Sstevel@tonic-gate 	/*
9447c478bd9Sstevel@tonic-gate 	 * USG tty driver can send multiple HUP's!!
9457c478bd9Sstevel@tonic-gate 	 */
9467c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
9477c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
9487c478bd9Sstevel@tonic-gate 	if (chng == 0) {
9497c478bd9Sstevel@tonic-gate 		cleanup(1);
9507c478bd9Sstevel@tonic-gate 		exit(++errcnt);
9517c478bd9Sstevel@tonic-gate 	}
9527c478bd9Sstevel@tonic-gate 	if (setexit() == 0) {
9537c478bd9Sstevel@tonic-gate 		if (preserve()) {
9547c478bd9Sstevel@tonic-gate 			cleanup(1);
9557c478bd9Sstevel@tonic-gate 			exit(++errcnt);
9567c478bd9Sstevel@tonic-gate 		}
9577c478bd9Sstevel@tonic-gate 	}
9587c478bd9Sstevel@tonic-gate 	if (kflag)
9597c478bd9Sstevel@tonic-gate 		crypt_close(perm);
9607c478bd9Sstevel@tonic-gate 	if (xtflag)
9617c478bd9Sstevel@tonic-gate 		crypt_close(tperm);
9627c478bd9Sstevel@tonic-gate 	exit(++errcnt);
9637c478bd9Sstevel@tonic-gate }
9647c478bd9Sstevel@tonic-gate 
9657c478bd9Sstevel@tonic-gate /*
9667c478bd9Sstevel@tonic-gate  * Similar to onhup.  This happens when any random core dump occurs,
9677c478bd9Sstevel@tonic-gate  * e.g. a bug in vi.  We preserve the file and then generate a core.
9687c478bd9Sstevel@tonic-gate  */
oncore(sig)9697c478bd9Sstevel@tonic-gate void oncore(sig)
9707c478bd9Sstevel@tonic-gate int sig;
9717c478bd9Sstevel@tonic-gate {
9727c478bd9Sstevel@tonic-gate 	static int timescalled = 0;
9737c478bd9Sstevel@tonic-gate 	char *messagep;	/* for message localization */
9747c478bd9Sstevel@tonic-gate 
9757c478bd9Sstevel@tonic-gate 	/*
9767c478bd9Sstevel@tonic-gate 	 * USG tty driver can send multiple HUP's!!
9777c478bd9Sstevel@tonic-gate 	 */
9787c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
9797c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
9807c478bd9Sstevel@tonic-gate 	signal(sig, SIG_DFL);	/* Insure that we don't catch it again */
9817c478bd9Sstevel@tonic-gate 	messagep = (char *)gettext("\r\nYour file has been preserved\r\n");
9827c478bd9Sstevel@tonic-gate 	if (timescalled++ == 0 && chng && setexit() == 0) {
9837c478bd9Sstevel@tonic-gate 		if (inopen)
9847c478bd9Sstevel@tonic-gate 			vsave();
985f6db9f27Scf46844 		(void) preserve();
9867c478bd9Sstevel@tonic-gate 		write(1, messagep, strlen(messagep));
9877c478bd9Sstevel@tonic-gate 	}
9887c478bd9Sstevel@tonic-gate 	if (timescalled < 2) {
9897c478bd9Sstevel@tonic-gate 		normal(normf);
9907c478bd9Sstevel@tonic-gate 		cleanup(2);
9917c478bd9Sstevel@tonic-gate 		kill(getpid(), sig);	/* Resend ourselves the same signal */
9927c478bd9Sstevel@tonic-gate 		/* We won't get past here */
9937c478bd9Sstevel@tonic-gate 	}
9947c478bd9Sstevel@tonic-gate 	if (kflag)
9957c478bd9Sstevel@tonic-gate 		crypt_close(perm);
9967c478bd9Sstevel@tonic-gate 	if (xtflag)
9977c478bd9Sstevel@tonic-gate 		crypt_close(tperm);
9987c478bd9Sstevel@tonic-gate 	exit(++errcnt);
9997c478bd9Sstevel@tonic-gate }
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate /*
10027c478bd9Sstevel@tonic-gate  * An interrupt occurred.  Drain any output which
10037c478bd9Sstevel@tonic-gate  * is still in the output buffering pipeline.
10047c478bd9Sstevel@tonic-gate  * Catch interrupts again.  Unless we are in visual
10057c478bd9Sstevel@tonic-gate  * reset the output state (out of -nl mode, e.g).
10067c478bd9Sstevel@tonic-gate  * Then like a normal error (with the \n before Interrupt
10077c478bd9Sstevel@tonic-gate  * suppressed in visual mode).
10087c478bd9Sstevel@tonic-gate  */
10097c478bd9Sstevel@tonic-gate 
10107c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10117c478bd9Sstevel@tonic-gate void
onintr(sig)10127c478bd9Sstevel@tonic-gate onintr(sig)
10137c478bd9Sstevel@tonic-gate int sig;
10147c478bd9Sstevel@tonic-gate {
10157c478bd9Sstevel@tonic-gate #ifndef CBREAK
10167c478bd9Sstevel@tonic-gate 	signal(SIGINT, onintr);
10177c478bd9Sstevel@tonic-gate #else
10187c478bd9Sstevel@tonic-gate 	signal(SIGINT, inopen ? vintr : onintr);
10197c478bd9Sstevel@tonic-gate #endif
10207c478bd9Sstevel@tonic-gate 	cancelalarm();
10217c478bd9Sstevel@tonic-gate 	draino();
10227c478bd9Sstevel@tonic-gate 	if (!inopen) {
10237c478bd9Sstevel@tonic-gate 		pstop();
10247c478bd9Sstevel@tonic-gate 		setlastchar('\n');
10257c478bd9Sstevel@tonic-gate #ifdef CBREAK
10267c478bd9Sstevel@tonic-gate 	}
10277c478bd9Sstevel@tonic-gate #else
10287c478bd9Sstevel@tonic-gate 	} else
10297c478bd9Sstevel@tonic-gate 		vraw();
10307c478bd9Sstevel@tonic-gate #endif
10317c478bd9Sstevel@tonic-gate 	error(gettext("\nInterrupt") + (inopen!=0));
10327c478bd9Sstevel@tonic-gate }
10337c478bd9Sstevel@tonic-gate 
10347c478bd9Sstevel@tonic-gate /*
10357c478bd9Sstevel@tonic-gate  * If we are interruptible, enable interrupts again.
10367c478bd9Sstevel@tonic-gate  * In some critical sections we turn interrupts off,
10377c478bd9Sstevel@tonic-gate  * but not very often.
10387c478bd9Sstevel@tonic-gate  */
1039f6db9f27Scf46844 void
setrupt(void)1040f6db9f27Scf46844 setrupt(void)
10417c478bd9Sstevel@tonic-gate {
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	if (ruptible) {
10447c478bd9Sstevel@tonic-gate #ifndef CBREAK
10457c478bd9Sstevel@tonic-gate 		signal(SIGINT, onintr);
10467c478bd9Sstevel@tonic-gate #else
10477c478bd9Sstevel@tonic-gate 		signal(SIGINT, inopen ? vintr : onintr);
10487c478bd9Sstevel@tonic-gate #endif
10497c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
10507c478bd9Sstevel@tonic-gate 		if (dosusp)
10517c478bd9Sstevel@tonic-gate 			signal(SIGTSTP, onsusp);
10527c478bd9Sstevel@tonic-gate #endif
10537c478bd9Sstevel@tonic-gate 	}
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
1056f6db9f27Scf46844 int
preserve(void)1057f6db9f27Scf46844 preserve(void)
10587c478bd9Sstevel@tonic-gate {
10597c478bd9Sstevel@tonic-gate 
10607c478bd9Sstevel@tonic-gate #ifdef VMUNIX
10617c478bd9Sstevel@tonic-gate 	tflush();
10627c478bd9Sstevel@tonic-gate #endif
10637c478bd9Sstevel@tonic-gate 	synctmp();
10647c478bd9Sstevel@tonic-gate 	pid = fork();
10657c478bd9Sstevel@tonic-gate 	if (pid < 0)
10667c478bd9Sstevel@tonic-gate 		return (0);
10677c478bd9Sstevel@tonic-gate 	if (pid == 0) {
10687c478bd9Sstevel@tonic-gate 		close(0);
10697c478bd9Sstevel@tonic-gate 		dup(tfile);
10707c478bd9Sstevel@tonic-gate 		execlp(EXPRESERVE, "expreserve", (char *) 0);
10717c478bd9Sstevel@tonic-gate 		exit(++errcnt);
10727c478bd9Sstevel@tonic-gate 	}
10737c478bd9Sstevel@tonic-gate 	waitfor();
10747c478bd9Sstevel@tonic-gate 	if (rpid == pid && status == 0)
10757c478bd9Sstevel@tonic-gate 		return (1);
10767c478bd9Sstevel@tonic-gate 	return (0);
10777c478bd9Sstevel@tonic-gate }
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate #ifndef V6
exit(i)10807c478bd9Sstevel@tonic-gate void exit(i)
10817c478bd9Sstevel@tonic-gate 	int i;
10827c478bd9Sstevel@tonic-gate {
10837c478bd9Sstevel@tonic-gate 
1084f6db9f27Scf46844 	extern void _exit(int) __NORETURN;
10857c478bd9Sstevel@tonic-gate #ifdef TRACE
10867c478bd9Sstevel@tonic-gate 	if (trace)
10877c478bd9Sstevel@tonic-gate 		fclose(trace);
10887c478bd9Sstevel@tonic-gate #endif
10897c478bd9Sstevel@tonic-gate 	_exit(i);
10907c478bd9Sstevel@tonic-gate }
10917c478bd9Sstevel@tonic-gate #endif
10927c478bd9Sstevel@tonic-gate 
10937c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
10947c478bd9Sstevel@tonic-gate /*
10957c478bd9Sstevel@tonic-gate  * We have just gotten a susp.  Suspend and prepare to resume.
10967c478bd9Sstevel@tonic-gate  */
10977c478bd9Sstevel@tonic-gate extern void redraw();
10987c478bd9Sstevel@tonic-gate 
10997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11007c478bd9Sstevel@tonic-gate void
onsusp(sig)11017c478bd9Sstevel@tonic-gate onsusp(sig)
11027c478bd9Sstevel@tonic-gate int sig;
11037c478bd9Sstevel@tonic-gate {
11047c478bd9Sstevel@tonic-gate 	ttymode f;
11057c478bd9Sstevel@tonic-gate 	int savenormtty;
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	f = setty(normf);
11087c478bd9Sstevel@tonic-gate 	vnfl();
1109f6db9f27Scf46844 	putpad((unsigned char *)exit_ca_mode);
11107c478bd9Sstevel@tonic-gate 	flush();
11117c478bd9Sstevel@tonic-gate 	resetterm();
11127c478bd9Sstevel@tonic-gate 	savenormtty = normtty;
11137c478bd9Sstevel@tonic-gate 	normtty = 0;
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 	signal(SIGTSTP, SIG_DFL);
11167c478bd9Sstevel@tonic-gate 	kill(0, SIGTSTP);
11177c478bd9Sstevel@tonic-gate 
11187c478bd9Sstevel@tonic-gate 	/* the pc stops here */
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 	signal(SIGTSTP, onsusp);
11217c478bd9Sstevel@tonic-gate 	normtty = savenormtty;
11227c478bd9Sstevel@tonic-gate 	vcontin(0);
11237c478bd9Sstevel@tonic-gate 	flush();
11247c478bd9Sstevel@tonic-gate 	setty(f);
11257c478bd9Sstevel@tonic-gate 	if (!inopen)
11267c478bd9Sstevel@tonic-gate 		error(0);
11277c478bd9Sstevel@tonic-gate 	else {
11287c478bd9Sstevel@tonic-gate 		if(vcnt < 0) {
11297c478bd9Sstevel@tonic-gate 			vcnt = -vcnt;
11307c478bd9Sstevel@tonic-gate 			if(state == VISUAL)
11317c478bd9Sstevel@tonic-gate 				vclear();
11327c478bd9Sstevel@tonic-gate 			else if(state == CRTOPEN)
11337c478bd9Sstevel@tonic-gate 				vcnt = 0;
11347c478bd9Sstevel@tonic-gate 		}
11357c478bd9Sstevel@tonic-gate 		vdirty(0, lines);
11367c478bd9Sstevel@tonic-gate 		if (sig)
11377c478bd9Sstevel@tonic-gate 			vrepaint(cursor);
11387c478bd9Sstevel@tonic-gate 	}
11397c478bd9Sstevel@tonic-gate }
11407c478bd9Sstevel@tonic-gate #endif
11417c478bd9Sstevel@tonic-gate 
nextchr(cursor)11427c478bd9Sstevel@tonic-gate unsigned char *nextchr(cursor)
11437c478bd9Sstevel@tonic-gate unsigned char *cursor;
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 
11467c478bd9Sstevel@tonic-gate 	wchar_t wchar;
11477c478bd9Sstevel@tonic-gate 	int length;
11487c478bd9Sstevel@tonic-gate 	length = mbtowc(&wchar, (char *)cursor, MULTI_BYTE_MAX);
11497c478bd9Sstevel@tonic-gate 	if(length <= 0)
11507c478bd9Sstevel@tonic-gate 		return(++cursor);
11517c478bd9Sstevel@tonic-gate 	return(cursor + length);
11527c478bd9Sstevel@tonic-gate }
11537c478bd9Sstevel@tonic-gate 
lastchr(linebuf,cursor)11547c478bd9Sstevel@tonic-gate unsigned char *lastchr(linebuf, cursor)
11557c478bd9Sstevel@tonic-gate unsigned char *linebuf, *cursor;
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	wchar_t wchar;
11587c478bd9Sstevel@tonic-gate 	int length;
11597c478bd9Sstevel@tonic-gate 	unsigned char *ccursor, *ocursor;
11607c478bd9Sstevel@tonic-gate 	if(cursor == linebuf)
11617c478bd9Sstevel@tonic-gate 		return(linebuf - 1);
11627c478bd9Sstevel@tonic-gate 	ccursor = ocursor = linebuf;
11637c478bd9Sstevel@tonic-gate 	while(ccursor < cursor) {
11647c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, (char *)ccursor, MULTI_BYTE_MAX);
11657c478bd9Sstevel@tonic-gate 		ocursor =  ccursor;
11667c478bd9Sstevel@tonic-gate 		if(length <= 0)
11677c478bd9Sstevel@tonic-gate 			ccursor++;
11687c478bd9Sstevel@tonic-gate 		else
11697c478bd9Sstevel@tonic-gate 			ccursor += length;
11707c478bd9Sstevel@tonic-gate 	}
11717c478bd9Sstevel@tonic-gate 	return(ocursor);
11727c478bd9Sstevel@tonic-gate }
11737c478bd9Sstevel@tonic-gate 
1174f6db9f27Scf46844 int
ixlatctl(int flag)1175f6db9f27Scf46844 ixlatctl(int flag)
11767c478bd9Sstevel@tonic-gate {
11777c478bd9Sstevel@tonic-gate 	static struct strioctl sb = {0, 0, 0, 0};
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate 	if (!(MULTI_BYTE_MAX > 1))
11807c478bd9Sstevel@tonic-gate 		return (0);
11817c478bd9Sstevel@tonic-gate 
11827c478bd9Sstevel@tonic-gate 	switch (flag) {
11837c478bd9Sstevel@tonic-gate 	case 0:
11847c478bd9Sstevel@tonic-gate 		sb.ic_cmd = EUC_MSAVE;
11857c478bd9Sstevel@tonic-gate 		sb.ic_len = 0;
11867c478bd9Sstevel@tonic-gate 		sb.ic_dp = 0;
11877c478bd9Sstevel@tonic-gate 		if (ioctl(0, I_STR, &sb) < 0)
11887c478bd9Sstevel@tonic-gate 			return (-1);
11897c478bd9Sstevel@tonic-gate 		return (0);
11907c478bd9Sstevel@tonic-gate 	case 1:
11917c478bd9Sstevel@tonic-gate 		sb.ic_cmd = EUC_MREST;
11927c478bd9Sstevel@tonic-gate 		sb.ic_len = 0;
11937c478bd9Sstevel@tonic-gate 		sb.ic_dp = 0;
11947c478bd9Sstevel@tonic-gate 		if (ioctl(0, I_STR, &sb) < 0)
11957c478bd9Sstevel@tonic-gate 			return (-1);
11967c478bd9Sstevel@tonic-gate 		return (0);
11977c478bd9Sstevel@tonic-gate 	case 11:
11987c478bd9Sstevel@tonic-gate 		return (0);
11997c478bd9Sstevel@tonic-gate 	default:
12007c478bd9Sstevel@tonic-gate 		return (-1);
12017c478bd9Sstevel@tonic-gate 	}
12027c478bd9Sstevel@tonic-gate }
12037c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate /* locale specific initialization */
1206f6db9f27Scf46844 void
localize(void)1207f6db9f27Scf46844 localize(void)
12087c478bd9Sstevel@tonic-gate {
12097c478bd9Sstevel@tonic-gate 	wchar_t fillerchar;
12107c478bd9Sstevel@tonic-gate 	extern int	wdchkind();
12117c478bd9Sstevel@tonic-gate 	extern int	wdbindf();
12127c478bd9Sstevel@tonic-gate 	extern wchar_t	*wddelim();
12137c478bd9Sstevel@tonic-gate 	extern wchar_t	mcfiller();
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	wdwc = wdchkind;
12167c478bd9Sstevel@tonic-gate 	wdbdg = wdbindf;
12177c478bd9Sstevel@tonic-gate 	wddlm = wddelim;
12187c478bd9Sstevel@tonic-gate 	mcfllr = mcfiller;
12197c478bd9Sstevel@tonic-gate 	mc_wrap = 1;
12207c478bd9Sstevel@tonic-gate 	fillerchar = mcfiller();
12217c478bd9Sstevel@tonic-gate 	mc_filler = isascii(fillerchar) ? (fillerchar & 0x7f) : '~';
12227c478bd9Sstevel@tonic-gate }
12237c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
1224