xref: /illumos-gate/usr/src/cmd/vi/port/ex_subr.c (revision ffe7853a)
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
523a1cceaSRoger A. Faulkner  * Common Development and Distribution License (the "License").
623a1cceaSRoger 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  */
2123a1cceaSRoger A. Faulkner 
227c478bd9Sstevel@tonic-gate /*
2323a1cceaSRoger 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	*/
27*ffe7853aSToomas Soome /*	  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 
50*ffe7853aSToomas Soome #ifndef PRESUNEUC
51*ffe7853aSToomas Soome int (*wdwc)(wchar_t);
52*ffe7853aSToomas Soome int (*wdbdg)(wchar_t, wchar_t, int);
53*ffe7853aSToomas Soome wchar_t *(*wddlm)(wchar_t, wchar_t, int);
54*ffe7853aSToomas Soome wchar_t (*mcfllr)(void);
55*ffe7853aSToomas Soome #endif /* PRESUNEUC */
56*ffe7853aSToomas Soome 
577c478bd9Sstevel@tonic-gate /*
587c478bd9Sstevel@tonic-gate  * Random routines, in alphabetical order.
597c478bd9Sstevel@tonic-gate  */
607c478bd9Sstevel@tonic-gate 
61f6db9f27Scf int
any(int c,unsigned char * s)62f6db9f27Scf any(int c, unsigned char *s)
637c478bd9Sstevel@tonic-gate {
64f6db9f27Scf 	int x;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 	while (x = *s++)
677c478bd9Sstevel@tonic-gate 		if (x == c)
687c478bd9Sstevel@tonic-gate 			return (1);
697c478bd9Sstevel@tonic-gate 	return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate 
72f6db9f27Scf int
backtab(int i)73f6db9f27Scf backtab(int i)
747c478bd9Sstevel@tonic-gate {
75f6db9f27Scf 	int j;
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate 	j = i % value(vi_SHIFTWIDTH);
787c478bd9Sstevel@tonic-gate 	if (j == 0)
797c478bd9Sstevel@tonic-gate 		j = value(vi_SHIFTWIDTH);
807c478bd9Sstevel@tonic-gate 	i -= j;
817c478bd9Sstevel@tonic-gate 	if (i < 0)
827c478bd9Sstevel@tonic-gate 		i = 0;
837c478bd9Sstevel@tonic-gate 	return (i);
847c478bd9Sstevel@tonic-gate }
857c478bd9Sstevel@tonic-gate 
86f6db9f27Scf void
change(void)87f6db9f27Scf change(void)
887c478bd9Sstevel@tonic-gate {
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	tchng++;
917c478bd9Sstevel@tonic-gate 	chng = tchng;
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Column returns the number of
967c478bd9Sstevel@tonic-gate  * columns occupied by printing the
977c478bd9Sstevel@tonic-gate  * characters through position cp of the
987c478bd9Sstevel@tonic-gate  * current line.
997c478bd9Sstevel@tonic-gate  */
100f6db9f27Scf int
column(unsigned char * cp)101f6db9f27Scf column(unsigned char *cp)
1027c478bd9Sstevel@tonic-gate {
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate 	if (cp == 0)
1057c478bd9Sstevel@tonic-gate 		cp = &linebuf[LBSIZE - 2];
106f6db9f27Scf 	return (qcolumn(cp, (unsigned char *)0));
1077c478bd9Sstevel@tonic-gate }
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /* lcolumn is same as column except it returns number of columns
1107c478bd9Sstevel@tonic-gate  * occupied by characters before position
1117c478bd9Sstevel@tonic-gate  * cp of the current line
1127c478bd9Sstevel@tonic-gate  */
113f6db9f27Scf int
lcolumn(unsigned char * cp)114f6db9f27Scf lcolumn(unsigned char *cp)
1157c478bd9Sstevel@tonic-gate {
1167c478bd9Sstevel@tonic-gate 
1177c478bd9Sstevel@tonic-gate 	if (cp == 0)
1187c478bd9Sstevel@tonic-gate 		cp = &linebuf[LBSIZE - 2];
119f6db9f27Scf 	return (nqcolumn(lastchr(linebuf, cp), (unsigned char *)0));
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate /*
1237c478bd9Sstevel@tonic-gate  * Ignore a comment to the end of the line.
1247c478bd9Sstevel@tonic-gate  * This routine eats the trailing newline so don't call donewline().
1257c478bd9Sstevel@tonic-gate  */
126f6db9f27Scf void
comment(void)127f6db9f27Scf comment(void)
1287c478bd9Sstevel@tonic-gate {
129f6db9f27Scf 	int c;
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	do {
1327c478bd9Sstevel@tonic-gate 		c = getchar();
1337c478bd9Sstevel@tonic-gate 	} while (c != '\n' && c != EOF);
1347c478bd9Sstevel@tonic-gate 	if (c == EOF)
1357c478bd9Sstevel@tonic-gate 		ungetchar(c);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
138f6db9f27Scf void
Copy(unsigned char * to,unsigned char * from,int size)139f6db9f27Scf Copy(unsigned char *to, unsigned char *from, int size)
1407c478bd9Sstevel@tonic-gate {
1417c478bd9Sstevel@tonic-gate 
1427c478bd9Sstevel@tonic-gate 	if (size > 0)
1437c478bd9Sstevel@tonic-gate 		do
1447c478bd9Sstevel@tonic-gate 			*to++ = *from++;
1457c478bd9Sstevel@tonic-gate 		while (--size > 0);
1467c478bd9Sstevel@tonic-gate }
1477c478bd9Sstevel@tonic-gate 
148f6db9f27Scf void
copyw(line * to,line * from,int size)149f6db9f27Scf copyw(line *to, line *from, int size)
1507c478bd9Sstevel@tonic-gate {
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	if (size > 0)
1537c478bd9Sstevel@tonic-gate 		do
1547c478bd9Sstevel@tonic-gate 			*to++ = *from++;
1557c478bd9Sstevel@tonic-gate 		while (--size > 0);
1567c478bd9Sstevel@tonic-gate }
1577c478bd9Sstevel@tonic-gate 
158f6db9f27Scf void
copywR(line * to,line * from,int size)159f6db9f27Scf copywR(line *to, line *from, int size)
1607c478bd9Sstevel@tonic-gate {
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate 	while (--size >= 0)
1637c478bd9Sstevel@tonic-gate 		to[size] = from[size];
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
166f6db9f27Scf int
ctlof(int c)167f6db9f27Scf ctlof(int c)
1687c478bd9Sstevel@tonic-gate {
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	return (c == DELETE ? '?' : c | ('A' - 1));
1717c478bd9Sstevel@tonic-gate }
1727c478bd9Sstevel@tonic-gate 
173f6db9f27Scf void
dingdong(void)174f6db9f27Scf dingdong(void)
1757c478bd9Sstevel@tonic-gate {
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate 	if (flash_screen && value(vi_FLASH))
178f6db9f27Scf 		putpad((unsigned char *)flash_screen);
1797c478bd9Sstevel@tonic-gate 	else if (value(vi_ERRORBELLS))
180f6db9f27Scf 		putpad((unsigned char *)bell);
1817c478bd9Sstevel@tonic-gate }
1827c478bd9Sstevel@tonic-gate 
183f6db9f27Scf int
fixindent(int indent)184f6db9f27Scf fixindent(int indent)
1857c478bd9Sstevel@tonic-gate {
186f6db9f27Scf 	int i;
187f6db9f27Scf 	unsigned char *cp;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 	i = whitecnt(genbuf);
1907c478bd9Sstevel@tonic-gate 	cp = vpastwh(genbuf);
1917c478bd9Sstevel@tonic-gate 	if (*cp == 0 && i == indent && linebuf[0] == 0) {
1927c478bd9Sstevel@tonic-gate 		genbuf[0] = 0;
1937c478bd9Sstevel@tonic-gate 		return (i);
1947c478bd9Sstevel@tonic-gate 	}
1957c478bd9Sstevel@tonic-gate 	CP(genindent(i), cp);
1967c478bd9Sstevel@tonic-gate 	return (i);
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate 
199f6db9f27Scf void
filioerr(unsigned char * cp)200f6db9f27Scf filioerr(unsigned char *cp)
2017c478bd9Sstevel@tonic-gate {
202f6db9f27Scf 	int oerrno = errno;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	lprintf("\"%s\"", cp);
2057c478bd9Sstevel@tonic-gate 	errno = oerrno;
2067c478bd9Sstevel@tonic-gate 	syserror(1);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate unsigned char *
genindent(indent)2107c478bd9Sstevel@tonic-gate genindent(indent)
211f6db9f27Scf 	int indent;
2127c478bd9Sstevel@tonic-gate {
213f6db9f27Scf 	unsigned char *cp;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	for (cp = genbuf; indent >= value(vi_TABSTOP); indent -= value(vi_TABSTOP))
2167c478bd9Sstevel@tonic-gate 		*cp++ = '\t';
2177c478bd9Sstevel@tonic-gate 	for (; indent > 0; indent--)
2187c478bd9Sstevel@tonic-gate 		*cp++ = ' ';
2197c478bd9Sstevel@tonic-gate 	return (cp);
2207c478bd9Sstevel@tonic-gate }
2217c478bd9Sstevel@tonic-gate 
222f6db9f27Scf void
getDOT(void)223f6db9f27Scf getDOT(void)
2247c478bd9Sstevel@tonic-gate {
2257c478bd9Sstevel@tonic-gate 
22623a1cceaSRoger A. Faulkner 	getaline(*dot);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate line *
getmark(c)2307c478bd9Sstevel@tonic-gate getmark(c)
231f6db9f27Scf 	int c;
2327c478bd9Sstevel@tonic-gate {
233f6db9f27Scf 	line *addr;
2340f1f7826SRichard Lowe 
2357c478bd9Sstevel@tonic-gate 	for (addr = one; addr <= dol; addr++)
2367c478bd9Sstevel@tonic-gate 		if (names[c - 'a'] == (*addr &~ 01)) {
2377c478bd9Sstevel@tonic-gate 			return (addr);
2387c478bd9Sstevel@tonic-gate 		}
2397c478bd9Sstevel@tonic-gate 	return (0);
2407c478bd9Sstevel@tonic-gate }
2417c478bd9Sstevel@tonic-gate 
242f6db9f27Scf void
ignnEOF(void)243f6db9f27Scf ignnEOF(void)
2447c478bd9Sstevel@tonic-gate {
245f6db9f27Scf 	int c = getchar();
2467c478bd9Sstevel@tonic-gate 
2477c478bd9Sstevel@tonic-gate 	if (c == EOF)
2487c478bd9Sstevel@tonic-gate 		ungetchar(c);
2497c478bd9Sstevel@tonic-gate 	else if (c=='"')
2507c478bd9Sstevel@tonic-gate 		comment();
2517c478bd9Sstevel@tonic-gate }
2527c478bd9Sstevel@tonic-gate 
253f6db9f27Scf int
iswhite(int c)254f6db9f27Scf iswhite(int c)
2557c478bd9Sstevel@tonic-gate {
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	return (c == ' ' || c == '\t');
2587c478bd9Sstevel@tonic-gate }
2597c478bd9Sstevel@tonic-gate 
260f6db9f27Scf int
junk(wchar_t c)261f6db9f27Scf junk(wchar_t c)
2627c478bd9Sstevel@tonic-gate {
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	if (c && !value(vi_BEAUTIFY))
2657c478bd9Sstevel@tonic-gate 		return (0);
2667c478bd9Sstevel@tonic-gate 	if (c >= ' ' && c != DELETE)
2677c478bd9Sstevel@tonic-gate 		return (0);
2687c478bd9Sstevel@tonic-gate 	switch (c) {
2697c478bd9Sstevel@tonic-gate 
2707c478bd9Sstevel@tonic-gate 	case '\t':
2717c478bd9Sstevel@tonic-gate 	case '\n':
2727c478bd9Sstevel@tonic-gate 	case '\f':
2737c478bd9Sstevel@tonic-gate 		return (0);
2747c478bd9Sstevel@tonic-gate 
2757c478bd9Sstevel@tonic-gate 	default:
2767c478bd9Sstevel@tonic-gate 		return (1);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate }
2797c478bd9Sstevel@tonic-gate 
280f6db9f27Scf void
killed(void)281f6db9f27Scf killed(void)
2827c478bd9Sstevel@tonic-gate {
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	killcnt(addr2 - addr1 + 1);
2857c478bd9Sstevel@tonic-gate }
2867c478bd9Sstevel@tonic-gate 
287f6db9f27Scf void
killcnt(int cnt)288f6db9f27Scf killcnt(int cnt)
2897c478bd9Sstevel@tonic-gate {
2907c478bd9Sstevel@tonic-gate 	extern char *verbalize();
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	if (inopen) {
2937c478bd9Sstevel@tonic-gate 		notecnt = cnt;
2947c478bd9Sstevel@tonic-gate 		notenam = notesgn = (unsigned char *)"";
2957c478bd9Sstevel@tonic-gate 		return;
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 	if (!notable(cnt))
2987c478bd9Sstevel@tonic-gate 		return;
2997c478bd9Sstevel@tonic-gate 	if (value(vi_TERSE) == 0) {
3007c478bd9Sstevel@tonic-gate 		verbalize(cnt, Command, "");
3017c478bd9Sstevel@tonic-gate 	} else {
3027c478bd9Sstevel@tonic-gate 		if (cnt == 1) {
303f6db9f27Scf 			viprintf(gettext("1 line"), cnt);
3047c478bd9Sstevel@tonic-gate 		} else {
305f6db9f27Scf 			viprintf(gettext("%d lines"), cnt);
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	}
3087c478bd9Sstevel@tonic-gate 	putNFL();
3097c478bd9Sstevel@tonic-gate }
3107c478bd9Sstevel@tonic-gate 
311f6db9f27Scf int
lineno(line * a)312f6db9f27Scf lineno(line *a)
3137c478bd9Sstevel@tonic-gate {
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	return (a - zero);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
318f6db9f27Scf int
lineDOL(void)319f6db9f27Scf lineDOL(void)
3207c478bd9Sstevel@tonic-gate {
3217c478bd9Sstevel@tonic-gate 
3227c478bd9Sstevel@tonic-gate 	return (lineno(dol));
3237c478bd9Sstevel@tonic-gate }
3247c478bd9Sstevel@tonic-gate 
325f6db9f27Scf int
lineDOT(void)326f6db9f27Scf lineDOT(void)
3277c478bd9Sstevel@tonic-gate {
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 	return (lineno(dot));
3307c478bd9Sstevel@tonic-gate }
3317c478bd9Sstevel@tonic-gate 
332f6db9f27Scf void
markDOT(void)333f6db9f27Scf markDOT(void)
3347c478bd9Sstevel@tonic-gate {
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	markpr(dot);
3377c478bd9Sstevel@tonic-gate }
3387c478bd9Sstevel@tonic-gate 
339f6db9f27Scf void
markpr(line * which)340f6db9f27Scf markpr(line *which)
3417c478bd9Sstevel@tonic-gate {
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 	if ((inglobal == 0 || inopen) && which <= endcore) {
3447c478bd9Sstevel@tonic-gate 		names['z'-'a'+1] = *which & ~01;
3457c478bd9Sstevel@tonic-gate 		if (inopen)
3467c478bd9Sstevel@tonic-gate 			ncols['z'-'a'+1] = cursor;
3477c478bd9Sstevel@tonic-gate 	}
3487c478bd9Sstevel@tonic-gate }
3497c478bd9Sstevel@tonic-gate 
350f6db9f27Scf int
markreg(int c)351f6db9f27Scf markreg(int c)
3527c478bd9Sstevel@tonic-gate {
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 	if (c == '\'' || c == '`')
3557c478bd9Sstevel@tonic-gate 		return ('z' + 1);
3567c478bd9Sstevel@tonic-gate 	if (c >= 'a' && c <= 'z')
3577c478bd9Sstevel@tonic-gate 		return (c);
3587c478bd9Sstevel@tonic-gate 	return (0);
3597c478bd9Sstevel@tonic-gate }
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate /*
3627c478bd9Sstevel@tonic-gate  * Mesg decodes the terse/verbose strings. Thus
3637c478bd9Sstevel@tonic-gate  *	'xxx@yyy' -> 'xxx' if terse, else 'xxx yyy'
3647c478bd9Sstevel@tonic-gate  *	'xxx|yyy' -> 'xxx' if terse, else 'yyy'
3657c478bd9Sstevel@tonic-gate  * All others map to themselves.
3667c478bd9Sstevel@tonic-gate  */
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate  * The feature described above was disabled for localizable messaging.
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate unsigned char *
mesg(str)3717c478bd9Sstevel@tonic-gate mesg(str)
372f6db9f27Scf 	unsigned char *str;
3737c478bd9Sstevel@tonic-gate {
374f6db9f27Scf 	unsigned char *cp;
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 	str = (unsigned char *)strcpy(genbuf, str);
3777c478bd9Sstevel@tonic-gate 	/* commented out for localizable messaging */
3787c478bd9Sstevel@tonic-gate /*	for (cp = str; *cp; cp++)
3797c478bd9Sstevel@tonic-gate 		switch (*cp) {
3807c478bd9Sstevel@tonic-gate 
3817c478bd9Sstevel@tonic-gate 		case '@':
3827c478bd9Sstevel@tonic-gate 			if (value(vi_TERSE))
3837c478bd9Sstevel@tonic-gate 				*cp = 0;
3847c478bd9Sstevel@tonic-gate 			else
3857c478bd9Sstevel@tonic-gate 				*cp = ' ';
3867c478bd9Sstevel@tonic-gate 			break;
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 		case '|':
3897c478bd9Sstevel@tonic-gate 			if (value(vi_TERSE) == 0)
3907c478bd9Sstevel@tonic-gate 				return (cp + 1);
3917c478bd9Sstevel@tonic-gate 			*cp = 0;
3927c478bd9Sstevel@tonic-gate 			break;
3937c478bd9Sstevel@tonic-gate 		}	*/
3947c478bd9Sstevel@tonic-gate 	return (str);
3957c478bd9Sstevel@tonic-gate }
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate /*VARARGS2*/
398f6db9f27Scf void
merror(unsigned char * seekpt,int i)399f6db9f27Scf merror(unsigned char *seekpt, int i)
4007c478bd9Sstevel@tonic-gate {
401f6db9f27Scf 	unsigned char *cp = linebuf;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	if (seekpt == 0)
4047c478bd9Sstevel@tonic-gate 		return;
4057c478bd9Sstevel@tonic-gate 	merror1(seekpt);
4067c478bd9Sstevel@tonic-gate 	if (*cp == '\n')
4077c478bd9Sstevel@tonic-gate 		putnl(), cp++;
4087c478bd9Sstevel@tonic-gate 	if (inopen > 0 && clr_eol)
4097c478bd9Sstevel@tonic-gate 		vclreol();
4107c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
411f6db9f27Scf 		putpad((unsigned char *)enter_standout_mode);
4127c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
413f6db9f27Scf 	viprintf(mesg(cp), i);
4147c478bd9Sstevel@tonic-gate #else
415f6db9f27Scf 	viprintf((char *)mesg(cp), i);
4167c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
4177c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
418f6db9f27Scf 		putpad((unsigned char *)exit_bold);
4197c478bd9Sstevel@tonic-gate }
4207c478bd9Sstevel@tonic-gate 
421f6db9f27Scf void
merror1(unsigned char * seekpt)422f6db9f27Scf merror1(unsigned char *seekpt)
4237c478bd9Sstevel@tonic-gate {
4247c478bd9Sstevel@tonic-gate 
4257c478bd9Sstevel@tonic-gate 	strcpy(linebuf, seekpt);
4267c478bd9Sstevel@tonic-gate }
4277c478bd9Sstevel@tonic-gate 
4287c478bd9Sstevel@tonic-gate #define MAXDATA (56*1024)
429f6db9f27Scf int
morelines(void)430f6db9f27Scf morelines(void)
4317c478bd9Sstevel@tonic-gate {
432f6db9f27Scf 	unsigned char *end;
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	if ((int) sbrk(1024 * sizeof (line)) == -1) {
4357c478bd9Sstevel@tonic-gate 		if (endcore >= (line *) MAXDATA)
4367c478bd9Sstevel@tonic-gate 			return -1;
4377c478bd9Sstevel@tonic-gate 		end = (unsigned char *) MAXDATA;
4387c478bd9Sstevel@tonic-gate 		/*
4397c478bd9Sstevel@tonic-gate 		 * Ask for end+2 sice we want end to be the last used location.
4407c478bd9Sstevel@tonic-gate 		 */
4417c478bd9Sstevel@tonic-gate 		while (brk(end+2) == -1)
4427c478bd9Sstevel@tonic-gate 			end -= 64;
4437c478bd9Sstevel@tonic-gate 		if (end <= (unsigned char *) endcore)
4447c478bd9Sstevel@tonic-gate 			return -1;
4457c478bd9Sstevel@tonic-gate 		endcore = (line *) end;
4467c478bd9Sstevel@tonic-gate 	} else {
4477c478bd9Sstevel@tonic-gate 		endcore += 1024;
4487c478bd9Sstevel@tonic-gate 	}
4497c478bd9Sstevel@tonic-gate 	return (0);
4507c478bd9Sstevel@tonic-gate }
4517c478bd9Sstevel@tonic-gate 
452f6db9f27Scf void
nonzero(void)453f6db9f27Scf nonzero(void)
4547c478bd9Sstevel@tonic-gate {
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (addr1 == zero) {
4577c478bd9Sstevel@tonic-gate 		notempty();
4587c478bd9Sstevel@tonic-gate 		error(value(vi_TERSE) ? gettext("Nonzero address required") :
4597c478bd9Sstevel@tonic-gate gettext("Nonzero address required on this command"));
4607c478bd9Sstevel@tonic-gate 	}
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
463f6db9f27Scf int
notable(int i)464f6db9f27Scf notable(int i)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	return (hush == 0 && !inglobal && i > value(vi_REPORT));
4687c478bd9Sstevel@tonic-gate }
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 
471f6db9f27Scf void
notempty(void)472f6db9f27Scf notempty(void)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (dol == zero)
4767c478bd9Sstevel@tonic-gate 		error(value(vi_TERSE) ? gettext("No lines") :
4777c478bd9Sstevel@tonic-gate gettext("No lines in the buffer"));
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate 
481f6db9f27Scf void
netchHAD(int cnt)482f6db9f27Scf netchHAD(int cnt)
4837c478bd9Sstevel@tonic-gate {
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate 	netchange(lineDOL() - cnt);
4867c478bd9Sstevel@tonic-gate }
4877c478bd9Sstevel@tonic-gate 
488f6db9f27Scf void
netchange(int i)489f6db9f27Scf netchange(int i)
4907c478bd9Sstevel@tonic-gate {
491f6db9f27Scf 	unsigned char *cp;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (i > 0)
4947c478bd9Sstevel@tonic-gate 		notesgn = cp = (unsigned char *)"more ";
4957c478bd9Sstevel@tonic-gate 	else
4967c478bd9Sstevel@tonic-gate 		notesgn = cp = (unsigned char *)"fewer ", i = -i;
4977c478bd9Sstevel@tonic-gate 	if (inopen) {
4987c478bd9Sstevel@tonic-gate 		notecnt = i;
4997c478bd9Sstevel@tonic-gate 		notenam = (unsigned char *)"";
5007c478bd9Sstevel@tonic-gate 		return;
5017c478bd9Sstevel@tonic-gate 	}
5027c478bd9Sstevel@tonic-gate 	if (!notable(i))
5037c478bd9Sstevel@tonic-gate 		return;
5047c478bd9Sstevel@tonic-gate 	if (*cp == 'm')	/* for ease of messge localization */
5057c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
506f6db9f27Scf 		viprintf(mesg(value(vi_TERSE) ?
5077c478bd9Sstevel@tonic-gate #else
508f6db9f27Scf 		viprintf((char *)mesg(value(vi_TERSE) ?
5097c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
5107c478bd9Sstevel@tonic-gate gettext("%d more lines") :
5117c478bd9Sstevel@tonic-gate 		/*
5127c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
5137c478bd9Sstevel@tonic-gate 		 *	Reference order of arguments must not
5147c478bd9Sstevel@tonic-gate 		 *	be changed using '%digit$', since vi's
515f6db9f27Scf 		 *	viprintf() does not support it.
5167c478bd9Sstevel@tonic-gate 		 */
5177c478bd9Sstevel@tonic-gate gettext("%d more lines in file after %s")), i, Command);
5187c478bd9Sstevel@tonic-gate 	else
5197c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
520f6db9f27Scf 		viprintf(mesg(value(vi_TERSE) ?
5217c478bd9Sstevel@tonic-gate #else
522f6db9f27Scf 		viprintf((char *)mesg(value(vi_TERSE) ?
5237c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
5247c478bd9Sstevel@tonic-gate gettext("%d fewer lines") :
5257c478bd9Sstevel@tonic-gate 		/*
5267c478bd9Sstevel@tonic-gate 		 * TRANSLATION_NOTE
5277c478bd9Sstevel@tonic-gate 		 *	Reference order of arguments must not
5287c478bd9Sstevel@tonic-gate 		 *	be changed using '%digit$', since vi's
529f6db9f27Scf 		 *	viprintf() does not support it.
5307c478bd9Sstevel@tonic-gate 		 */
5317c478bd9Sstevel@tonic-gate gettext("%d fewer lines in file after %s")), i, Command);
5327c478bd9Sstevel@tonic-gate 	putNFL();
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
535f6db9f27Scf void
putmark(line * addr)536f6db9f27Scf putmark(line *addr)
5377c478bd9Sstevel@tonic-gate {
5387c478bd9Sstevel@tonic-gate 
5397c478bd9Sstevel@tonic-gate 	putmk1(addr, putline());
5407c478bd9Sstevel@tonic-gate }
5417c478bd9Sstevel@tonic-gate 
542f6db9f27Scf void
putmk1(line * addr,int n)543f6db9f27Scf putmk1(line *addr, int n)
5447c478bd9Sstevel@tonic-gate {
545f6db9f27Scf 	line *markp;
546f6db9f27Scf 	int oldglobmk;
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	oldglobmk = *addr & 1;
5497c478bd9Sstevel@tonic-gate 	*addr &= ~1;
5507c478bd9Sstevel@tonic-gate 	for (markp = (anymarks ? names : &names['z'-'a'+1]);
5517c478bd9Sstevel@tonic-gate 	  markp <= &names['z'-'a'+1]; markp++)
5527c478bd9Sstevel@tonic-gate 		if (*markp == *addr)
5537c478bd9Sstevel@tonic-gate 			*markp = n;
5547c478bd9Sstevel@tonic-gate 	*addr = n | oldglobmk;
5557c478bd9Sstevel@tonic-gate }
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate unsigned char *
plural(i)5587c478bd9Sstevel@tonic-gate plural(i)
5597c478bd9Sstevel@tonic-gate 	long i;
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	return (i == 1 ? (unsigned char *)"" : (unsigned char *)"s");
5637c478bd9Sstevel@tonic-gate }
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate int	qcount();
5667c478bd9Sstevel@tonic-gate short	vcntcol;
5677c478bd9Sstevel@tonic-gate 
568f6db9f27Scf int
qcolumn(unsigned char * lim,unsigned char * gp)569f6db9f27Scf qcolumn(unsigned char *lim, unsigned char *gp)
5707c478bd9Sstevel@tonic-gate {
571f6db9f27Scf 	int x, length;
5727c478bd9Sstevel@tonic-gate 	int	col;
5737c478bd9Sstevel@tonic-gate 	wchar_t wchar;
5747c478bd9Sstevel@tonic-gate 	int (*OO)();
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 	OO = Outchar;
5777c478bd9Sstevel@tonic-gate 	Outchar = qcount;
5787c478bd9Sstevel@tonic-gate 	vcntcol = 0;
5797c478bd9Sstevel@tonic-gate 	if (lim != NULL) {
5807c478bd9Sstevel@tonic-gate 		if(lim == linebuf - 1 || lim == &linebuf[LBSIZE-2])
5817c478bd9Sstevel@tonic-gate 			length = 1;
5827c478bd9Sstevel@tonic-gate 		else
5837c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)lim, MULTI_BYTE_MAX);
5847c478bd9Sstevel@tonic-gate 		if(length < 0)
5857c478bd9Sstevel@tonic-gate 			length = 1;
5860f1f7826SRichard Lowe 		x = lim[length];
5877c478bd9Sstevel@tonic-gate 		lim[length] = 0;
5887c478bd9Sstevel@tonic-gate 	}
5897c478bd9Sstevel@tonic-gate 	pline(0);
5907c478bd9Sstevel@tonic-gate 	if (lim != NULL)
5917c478bd9Sstevel@tonic-gate 		lim[length] = x;
5927c478bd9Sstevel@tonic-gate 	if(length > 1 && !gp) {
5937c478bd9Sstevel@tonic-gate 		/* put cursor at beginning of multibyte character */
5947c478bd9Sstevel@tonic-gate 		if ((col = wcwidth(wchar)) < 0)
5957c478bd9Sstevel@tonic-gate 			col = 0;
5967c478bd9Sstevel@tonic-gate 		vcntcol = vcntcol - col + 1;
5977c478bd9Sstevel@tonic-gate 	}
598*ffe7853aSToomas Soome 	if (gp)
5997c478bd9Sstevel@tonic-gate 		while (*gp) {
6007c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)gp, MULTI_BYTE_MAX);
6017c478bd9Sstevel@tonic-gate 			if(length < 0) {
6027c478bd9Sstevel@tonic-gate 				putoctal = 1;
6037c478bd9Sstevel@tonic-gate 				putchar(*gp++);
6047c478bd9Sstevel@tonic-gate 				putoctal = 0;
6057c478bd9Sstevel@tonic-gate 			} else {
6067c478bd9Sstevel@tonic-gate 				putchar(wchar);
6077c478bd9Sstevel@tonic-gate 				gp += length;
6087c478bd9Sstevel@tonic-gate 			}
6097c478bd9Sstevel@tonic-gate 		}
6107c478bd9Sstevel@tonic-gate 	Outchar = OO;
6117c478bd9Sstevel@tonic-gate 	return (vcntcol);
6127c478bd9Sstevel@tonic-gate }
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate /* This routine puts cursor after multibyte character */
615f6db9f27Scf int
nqcolumn(unsigned char * lim,unsigned char * gp)616f6db9f27Scf nqcolumn(unsigned char *lim, unsigned char *gp)
6177c478bd9Sstevel@tonic-gate {
618f6db9f27Scf 	int x, length;
6197c478bd9Sstevel@tonic-gate 	wchar_t wchar;
6207c478bd9Sstevel@tonic-gate 	int (*OO)();
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate 	OO = Outchar;
6237c478bd9Sstevel@tonic-gate 	Outchar = qcount;
6247c478bd9Sstevel@tonic-gate 	vcntcol = 0;
6257c478bd9Sstevel@tonic-gate 	if (lim != NULL) {
6267c478bd9Sstevel@tonic-gate 		if(lim == linebuf - 1 || lim == &linebuf[LBSIZE-2])
6277c478bd9Sstevel@tonic-gate 			length = 1;
6287c478bd9Sstevel@tonic-gate 		else
6297c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)lim, MULTI_BYTE_MAX);
6307c478bd9Sstevel@tonic-gate 		if(length < 0)
6317c478bd9Sstevel@tonic-gate 			length = 1;
6320f1f7826SRichard Lowe 		x = lim[length];
6337c478bd9Sstevel@tonic-gate 		lim[length] = 0;
6347c478bd9Sstevel@tonic-gate 	}
6357c478bd9Sstevel@tonic-gate 	pline(0);
6367c478bd9Sstevel@tonic-gate 	if (lim != NULL)
6377c478bd9Sstevel@tonic-gate 		lim[length] = x;
638*ffe7853aSToomas Soome 	if (gp)
6397c478bd9Sstevel@tonic-gate 		while (*gp) {
6407c478bd9Sstevel@tonic-gate 			length = mbtowc(&wchar, (char *)gp, MULTI_BYTE_MAX);
6417c478bd9Sstevel@tonic-gate 			if(length < 0) {
6427c478bd9Sstevel@tonic-gate 				putoctal = 1;
6437c478bd9Sstevel@tonic-gate 				putchar(*gp++);
6447c478bd9Sstevel@tonic-gate 				putoctal = 0;
6457c478bd9Sstevel@tonic-gate 			} else {
6467c478bd9Sstevel@tonic-gate 				putchar(wchar);
6477c478bd9Sstevel@tonic-gate 				gp += length;
6487c478bd9Sstevel@tonic-gate 			}
6497c478bd9Sstevel@tonic-gate 		}
6507c478bd9Sstevel@tonic-gate 	Outchar = OO;
6517c478bd9Sstevel@tonic-gate 	return (vcntcol);
6527c478bd9Sstevel@tonic-gate }
6537c478bd9Sstevel@tonic-gate 
6547c478bd9Sstevel@tonic-gate int
qcount(c)6557c478bd9Sstevel@tonic-gate qcount(c)
6567c478bd9Sstevel@tonic-gate wchar_t c;
6577c478bd9Sstevel@tonic-gate {
658f6db9f27Scf 	int cols;
6597c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
660f6db9f27Scf 	int remcols;
661f6db9f27Scf 	short OWCOLS;
6627c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
6637c478bd9Sstevel@tonic-gate 
6647c478bd9Sstevel@tonic-gate 	if (c == '\t') {
6657c478bd9Sstevel@tonic-gate 		vcntcol += value(vi_TABSTOP) - vcntcol % value(vi_TABSTOP);
666f6db9f27Scf 		return (0);
6677c478bd9Sstevel@tonic-gate 	}
6687c478bd9Sstevel@tonic-gate #ifdef PRESUNEUC
6697c478bd9Sstevel@tonic-gate 	if ((cols = wcwidth(c)) > 0)
6707c478bd9Sstevel@tonic-gate 		vcntcol += cols;
6717c478bd9Sstevel@tonic-gate #else
6727c478bd9Sstevel@tonic-gate 	if ((cols = wcwidth(c)) < 0)
6737c478bd9Sstevel@tonic-gate 		cols = 0;
6747c478bd9Sstevel@tonic-gate 	OWCOLS = WCOLS;
6757c478bd9Sstevel@tonic-gate 	if (WCOLS == 0)
6767c478bd9Sstevel@tonic-gate 		WCOLS = columns;
6777c478bd9Sstevel@tonic-gate 	if ((mc_wrap) == 1 && (remcols = (WCOLS - (vcntcol % WCOLS))) < cols)
6787c478bd9Sstevel@tonic-gate 		vcntcol += remcols;
6797c478bd9Sstevel@tonic-gate 	WCOLS = OWCOLS;
6807c478bd9Sstevel@tonic-gate 	vcntcol += cols;
6817c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
682f6db9f27Scf 	return (0);
6837c478bd9Sstevel@tonic-gate }
6847c478bd9Sstevel@tonic-gate 
685f6db9f27Scf void
reverse(line * a1,line * a2)686f6db9f27Scf reverse(line *a1, line *a2)
6877c478bd9Sstevel@tonic-gate {
688f6db9f27Scf 	line t;
6897c478bd9Sstevel@tonic-gate 
6907c478bd9Sstevel@tonic-gate 	for (;;) {
6917c478bd9Sstevel@tonic-gate 		t = *--a2;
6927c478bd9Sstevel@tonic-gate 		if (a2 <= a1)
6937c478bd9Sstevel@tonic-gate 			return;
6947c478bd9Sstevel@tonic-gate 		*a2 = *a1;
6957c478bd9Sstevel@tonic-gate 		*a1++ = t;
6967c478bd9Sstevel@tonic-gate 	}
6977c478bd9Sstevel@tonic-gate }
6987c478bd9Sstevel@tonic-gate 
699f6db9f27Scf void
save(line * a1,line * a2)700f6db9f27Scf save(line *a1, line *a2)
7017c478bd9Sstevel@tonic-gate {
702f6db9f27Scf 	int more;
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 	if (!FIXUNDO)
7057c478bd9Sstevel@tonic-gate 		return;
7067c478bd9Sstevel@tonic-gate #ifdef UNDOTRACE
7077c478bd9Sstevel@tonic-gate 	if (trace)
7087c478bd9Sstevel@tonic-gate 		vudump("before save");
7097c478bd9Sstevel@tonic-gate #endif
7107c478bd9Sstevel@tonic-gate 	undkind = UNDNONE;
7117c478bd9Sstevel@tonic-gate 	undadot = dot;
7127c478bd9Sstevel@tonic-gate 	more = (a2 - a1 + 1) - (unddol - dol);
7137c478bd9Sstevel@tonic-gate 	while (more > (endcore - truedol))
7147c478bd9Sstevel@tonic-gate 		if (morelines() < 0)
7157c478bd9Sstevel@tonic-gate 			error(value(vi_TERSE) ? gettext("Out of memory") :
7167c478bd9Sstevel@tonic-gate gettext("Out of memory saving lines for undo - try using ed"));
7177c478bd9Sstevel@tonic-gate 	if (more)
7187c478bd9Sstevel@tonic-gate 		(*(more > 0 ? copywR : copyw))(unddol + more + 1, unddol + 1,
7197c478bd9Sstevel@tonic-gate 		    (truedol - unddol));
7207c478bd9Sstevel@tonic-gate 	unddol += more;
7217c478bd9Sstevel@tonic-gate 	truedol += more;
7227c478bd9Sstevel@tonic-gate 	copyw(dol + 1, a1, a2 - a1 + 1);
7237c478bd9Sstevel@tonic-gate 	undkind = UNDALL;
7247c478bd9Sstevel@tonic-gate 	unddel = a1 - 1;
7257c478bd9Sstevel@tonic-gate 	undap1 = a1;
7267c478bd9Sstevel@tonic-gate 	undap2 = a2 + 1;
7277c478bd9Sstevel@tonic-gate #ifdef UNDOTRACE
7287c478bd9Sstevel@tonic-gate 	if (trace)
7297c478bd9Sstevel@tonic-gate 		vudump("after save");
7307c478bd9Sstevel@tonic-gate #endif
7317c478bd9Sstevel@tonic-gate }
7327c478bd9Sstevel@tonic-gate 
733f6db9f27Scf void
save12(void)734f6db9f27Scf save12(void)
7357c478bd9Sstevel@tonic-gate {
7367c478bd9Sstevel@tonic-gate 
7377c478bd9Sstevel@tonic-gate 	save(addr1, addr2);
7387c478bd9Sstevel@tonic-gate }
7397c478bd9Sstevel@tonic-gate 
740f6db9f27Scf void
saveall(void)741f6db9f27Scf saveall(void)
7427c478bd9Sstevel@tonic-gate {
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 	save(one, dol);
7457c478bd9Sstevel@tonic-gate }
7467c478bd9Sstevel@tonic-gate 
747f6db9f27Scf int
span(void)748f6db9f27Scf span(void)
7497c478bd9Sstevel@tonic-gate {
7507c478bd9Sstevel@tonic-gate 
7517c478bd9Sstevel@tonic-gate 	return (addr2 - addr1 + 1);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
754f6db9f27Scf void
sync(void)755f6db9f27Scf sync(void)
7567c478bd9Sstevel@tonic-gate {
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	chng = 0;
7597c478bd9Sstevel@tonic-gate 	tchng = 0;
7607c478bd9Sstevel@tonic-gate 	xchng = 0;
7617c478bd9Sstevel@tonic-gate }
7627c478bd9Sstevel@tonic-gate 
7637c478bd9Sstevel@tonic-gate 
764f6db9f27Scf int
skipwh(void)765f6db9f27Scf skipwh(void)
7667c478bd9Sstevel@tonic-gate {
767f6db9f27Scf 	int wh;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	wh = 0;
7707c478bd9Sstevel@tonic-gate 	while (iswhite(peekchar())) {
7717c478bd9Sstevel@tonic-gate 		wh++;
7727c478bd9Sstevel@tonic-gate 		ignchar();
7737c478bd9Sstevel@tonic-gate 	}
7747c478bd9Sstevel@tonic-gate 	return (wh);
7757c478bd9Sstevel@tonic-gate }
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate /*VARARGS2*/
778f6db9f27Scf void
smerror(unsigned char * seekpt,unsigned char * cp)779f6db9f27Scf smerror(unsigned char *seekpt, unsigned char *cp)
7807c478bd9Sstevel@tonic-gate {
7817c478bd9Sstevel@tonic-gate 
7827c478bd9Sstevel@tonic-gate 	errcnt++;
7837c478bd9Sstevel@tonic-gate 	merror1(seekpt);
7847c478bd9Sstevel@tonic-gate 	if (inopen && clr_eol)
7857c478bd9Sstevel@tonic-gate 		vclreol();
7867c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
787f6db9f27Scf 		putpad((unsigned char *)enter_standout_mode);
7887c478bd9Sstevel@tonic-gate 	lprintf(mesg(linebuf), cp);
7897c478bd9Sstevel@tonic-gate 	if (enter_standout_mode && exit_bold)
790f6db9f27Scf 		putpad((unsigned char *)exit_bold);
7917c478bd9Sstevel@tonic-gate }
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate unsigned char *
strend(cp)7947c478bd9Sstevel@tonic-gate strend(cp)
795f6db9f27Scf 	unsigned char *cp;
7967c478bd9Sstevel@tonic-gate {
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	while (*cp)
7997c478bd9Sstevel@tonic-gate 		cp++;
8007c478bd9Sstevel@tonic-gate 	return (cp);
8017c478bd9Sstevel@tonic-gate }
8027c478bd9Sstevel@tonic-gate 
803f6db9f27Scf void
strcLIN(unsigned char * dp)804f6db9f27Scf strcLIN(unsigned char *dp)
8057c478bd9Sstevel@tonic-gate {
8067c478bd9Sstevel@tonic-gate 
8077c478bd9Sstevel@tonic-gate 	CP(linebuf, dp);
8087c478bd9Sstevel@tonic-gate }
8097c478bd9Sstevel@tonic-gate 
8107c478bd9Sstevel@tonic-gate /*
8117c478bd9Sstevel@tonic-gate  * A system error has occurred that we need to perror.
8127c478bd9Sstevel@tonic-gate  * danger is true if we are unsure of the contents of
8137c478bd9Sstevel@tonic-gate  * the file or our buffer, e.g. a write error in the
8147c478bd9Sstevel@tonic-gate  * middle of a write operation, or a temp file error.
8157c478bd9Sstevel@tonic-gate  */
816f6db9f27Scf void
syserror(int danger)817f6db9f27Scf syserror(int danger)
8187c478bd9Sstevel@tonic-gate {
819f6db9f27Scf 	int e = errno;
8207c478bd9Sstevel@tonic-gate 	char *errstr;
8217c478bd9Sstevel@tonic-gate 	extern char *strerror();
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	dirtcnt = 0;
8247c478bd9Sstevel@tonic-gate 	putchar(' ');
8257c478bd9Sstevel@tonic-gate 	if (danger)
8267c478bd9Sstevel@tonic-gate 		edited = 0;	/* for temp file errors, for example */
8277c478bd9Sstevel@tonic-gate 	if ((errstr = strerror(e)) != NULL)
8287c478bd9Sstevel@tonic-gate 		error(errstr);
8297c478bd9Sstevel@tonic-gate 	else
8307c478bd9Sstevel@tonic-gate 		error(gettext("System error %d"), e);
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate /*
8347c478bd9Sstevel@tonic-gate  * Return the column number that results from being in column col and
8357c478bd9Sstevel@tonic-gate  * hitting a tab, where tabs are set every ts columns.  Work right for
8367c478bd9Sstevel@tonic-gate  * the case where col > columns, even if ts does not divide columns.
8377c478bd9Sstevel@tonic-gate  */
838f6db9f27Scf int
tabcol(int col,int ts)839f6db9f27Scf tabcol(int col, int ts)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	int offset, result;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	if (col >= columns) {
8447c478bd9Sstevel@tonic-gate 		offset = columns * (col/columns);
8457c478bd9Sstevel@tonic-gate 		col -= offset;
8467c478bd9Sstevel@tonic-gate 	} else
8477c478bd9Sstevel@tonic-gate 		offset = 0;
8487c478bd9Sstevel@tonic-gate 	result = col + ts - (col % ts) + offset;
8497c478bd9Sstevel@tonic-gate 	return (result);
8507c478bd9Sstevel@tonic-gate }
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate unsigned char *
vfindcol(i)8537c478bd9Sstevel@tonic-gate vfindcol(i)
8547c478bd9Sstevel@tonic-gate 	int i;
8557c478bd9Sstevel@tonic-gate {
856f6db9f27Scf 	unsigned char *cp, *oldcp;
857f6db9f27Scf 	int (*OO)() = Outchar;
858f6db9f27Scf 	int length;
8597c478bd9Sstevel@tonic-gate 	unsigned char x;
8607c478bd9Sstevel@tonic-gate 	wchar_t wchar;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 	Outchar = qcount;
863f6db9f27Scf 	(void) qcolumn(linebuf - 1, (unsigned char *)NOSTR);
8647c478bd9Sstevel@tonic-gate 	for (cp = linebuf; *cp && vcntcol < i; ) {
8657c478bd9Sstevel@tonic-gate 		oldcp = cp;
8667c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, (char *)cp, MULTI_BYTE_MAX);
8677c478bd9Sstevel@tonic-gate 		if(length < 0) {
8687c478bd9Sstevel@tonic-gate 			putoctal = 1;
8697c478bd9Sstevel@tonic-gate 			putchar(*cp++);
8707c478bd9Sstevel@tonic-gate 			putoctal = 0;
8717c478bd9Sstevel@tonic-gate 		} else {
8727c478bd9Sstevel@tonic-gate 			putchar(wchar);
8737c478bd9Sstevel@tonic-gate 			cp += length;
8747c478bd9Sstevel@tonic-gate 		}
8757c478bd9Sstevel@tonic-gate 	}
8767c478bd9Sstevel@tonic-gate 	if (cp != linebuf)
8777c478bd9Sstevel@tonic-gate 		cp = oldcp;
8787c478bd9Sstevel@tonic-gate 	Outchar = OO;
8797c478bd9Sstevel@tonic-gate 	return (cp);
8807c478bd9Sstevel@tonic-gate }
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate unsigned char *
vskipwh(cp)8837c478bd9Sstevel@tonic-gate vskipwh(cp)
884f6db9f27Scf 	unsigned char *cp;
8857c478bd9Sstevel@tonic-gate {
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	while (iswhite(*cp) && cp[1])
8887c478bd9Sstevel@tonic-gate 		cp++;
8897c478bd9Sstevel@tonic-gate 	return (cp);
8907c478bd9Sstevel@tonic-gate }
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 
8937c478bd9Sstevel@tonic-gate unsigned char *
vpastwh(cp)8947c478bd9Sstevel@tonic-gate vpastwh(cp)
895f6db9f27Scf 	unsigned char *cp;
8967c478bd9Sstevel@tonic-gate {
8977c478bd9Sstevel@tonic-gate 
8987c478bd9Sstevel@tonic-gate 	while (iswhite(*cp))
8997c478bd9Sstevel@tonic-gate 		cp++;
9007c478bd9Sstevel@tonic-gate 	return (cp);
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
903f6db9f27Scf int
whitecnt(unsigned char * cp)904f6db9f27Scf whitecnt(unsigned char *cp)
9057c478bd9Sstevel@tonic-gate {
906f6db9f27Scf 	int i;
9077c478bd9Sstevel@tonic-gate 
9087c478bd9Sstevel@tonic-gate 	i = 0;
9097c478bd9Sstevel@tonic-gate 	for (;;)
9107c478bd9Sstevel@tonic-gate 		switch (*cp++) {
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 		case '\t':
9137c478bd9Sstevel@tonic-gate 			i += value(vi_TABSTOP) - i % value(vi_TABSTOP);
9147c478bd9Sstevel@tonic-gate 			break;
9157c478bd9Sstevel@tonic-gate 
9167c478bd9Sstevel@tonic-gate 		case ' ':
9177c478bd9Sstevel@tonic-gate 			i++;
9187c478bd9Sstevel@tonic-gate 			break;
9197c478bd9Sstevel@tonic-gate 
9207c478bd9Sstevel@tonic-gate 		default:
9217c478bd9Sstevel@tonic-gate 			return (i);
9227c478bd9Sstevel@tonic-gate 		}
9237c478bd9Sstevel@tonic-gate }
9247c478bd9Sstevel@tonic-gate 
925f6db9f27Scf void
markit(line * addr)926f6db9f27Scf markit(line *addr)
9277c478bd9Sstevel@tonic-gate {
9287c478bd9Sstevel@tonic-gate 
9297c478bd9Sstevel@tonic-gate 	if (addr != dot && addr >= one && addr <= dol)
9307c478bd9Sstevel@tonic-gate 		markDOT();
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * When a hangup occurs our actions are similar to a preserve
9357c478bd9Sstevel@tonic-gate  * command.  If the buffer has not been [Modified], then we do
9367c478bd9Sstevel@tonic-gate  * nothing but remove the temporary files and exit.
9377c478bd9Sstevel@tonic-gate  * Otherwise, we sync the temp file and then attempt a preserve.
9387c478bd9Sstevel@tonic-gate  * If the preserve succeeds, we unlink our temp files.
9397c478bd9Sstevel@tonic-gate  * If the preserve fails, we leave the temp files as they are
9407c478bd9Sstevel@tonic-gate  * as they are a backup even without preservation if they
9417c478bd9Sstevel@tonic-gate  * are not removed.
9427c478bd9Sstevel@tonic-gate  */
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate /*ARGSUSED*/
9450f1f7826SRichard Lowe void
onhup(sig)9467c478bd9Sstevel@tonic-gate onhup(sig)
9477c478bd9Sstevel@tonic-gate int sig;
9487c478bd9Sstevel@tonic-gate {
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	/*
9517c478bd9Sstevel@tonic-gate 	 * USG tty driver can send multiple HUP's!!
9527c478bd9Sstevel@tonic-gate 	 */
9537c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
9547c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
9557c478bd9Sstevel@tonic-gate 	if (chng == 0) {
9567c478bd9Sstevel@tonic-gate 		cleanup(1);
9577c478bd9Sstevel@tonic-gate 		exit(++errcnt);
9587c478bd9Sstevel@tonic-gate 	}
9597c478bd9Sstevel@tonic-gate 	if (setexit() == 0) {
9607c478bd9Sstevel@tonic-gate 		if (preserve()) {
9617c478bd9Sstevel@tonic-gate 			cleanup(1);
9627c478bd9Sstevel@tonic-gate 			exit(++errcnt);
9637c478bd9Sstevel@tonic-gate 		}
9647c478bd9Sstevel@tonic-gate 	}
9657c478bd9Sstevel@tonic-gate 	if (kflag)
9667c478bd9Sstevel@tonic-gate 		crypt_close(perm);
9677c478bd9Sstevel@tonic-gate 	if (xtflag)
9687c478bd9Sstevel@tonic-gate 		crypt_close(tperm);
9697c478bd9Sstevel@tonic-gate 	exit(++errcnt);
9707c478bd9Sstevel@tonic-gate }
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate /*
9737c478bd9Sstevel@tonic-gate  * Similar to onhup.  This happens when any random core dump occurs,
9747c478bd9Sstevel@tonic-gate  * e.g. a bug in vi.  We preserve the file and then generate a core.
9757c478bd9Sstevel@tonic-gate  */
oncore(sig)9767c478bd9Sstevel@tonic-gate void oncore(sig)
9777c478bd9Sstevel@tonic-gate int sig;
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	static int timescalled = 0;
9807c478bd9Sstevel@tonic-gate 	char *messagep;	/* for message localization */
9817c478bd9Sstevel@tonic-gate 
9827c478bd9Sstevel@tonic-gate 	/*
9837c478bd9Sstevel@tonic-gate 	 * USG tty driver can send multiple HUP's!!
9847c478bd9Sstevel@tonic-gate 	 */
9857c478bd9Sstevel@tonic-gate 	signal(SIGINT, SIG_IGN);
9867c478bd9Sstevel@tonic-gate 	signal(SIGHUP, SIG_IGN);
9877c478bd9Sstevel@tonic-gate 	signal(sig, SIG_DFL);	/* Insure that we don't catch it again */
9887c478bd9Sstevel@tonic-gate 	messagep = (char *)gettext("\r\nYour file has been preserved\r\n");
9897c478bd9Sstevel@tonic-gate 	if (timescalled++ == 0 && chng && setexit() == 0) {
9907c478bd9Sstevel@tonic-gate 		if (inopen)
9917c478bd9Sstevel@tonic-gate 			vsave();
992f6db9f27Scf 		(void) preserve();
9937c478bd9Sstevel@tonic-gate 		write(1, messagep, strlen(messagep));
9947c478bd9Sstevel@tonic-gate 	}
9957c478bd9Sstevel@tonic-gate 	if (timescalled < 2) {
9967c478bd9Sstevel@tonic-gate 		normal(normf);
9977c478bd9Sstevel@tonic-gate 		cleanup(2);
9987c478bd9Sstevel@tonic-gate 		kill(getpid(), sig);	/* Resend ourselves the same signal */
9997c478bd9Sstevel@tonic-gate 		/* We won't get past here */
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 	if (kflag)
10027c478bd9Sstevel@tonic-gate 		crypt_close(perm);
10037c478bd9Sstevel@tonic-gate 	if (xtflag)
10047c478bd9Sstevel@tonic-gate 		crypt_close(tperm);
10057c478bd9Sstevel@tonic-gate 	exit(++errcnt);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate /*
10097c478bd9Sstevel@tonic-gate  * An interrupt occurred.  Drain any output which
10107c478bd9Sstevel@tonic-gate  * is still in the output buffering pipeline.
10117c478bd9Sstevel@tonic-gate  * Catch interrupts again.  Unless we are in visual
10127c478bd9Sstevel@tonic-gate  * reset the output state (out of -nl mode, e.g).
10137c478bd9Sstevel@tonic-gate  * Then like a normal error (with the \n before Interrupt
10147c478bd9Sstevel@tonic-gate  * suppressed in visual mode).
10157c478bd9Sstevel@tonic-gate  */
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate /*ARGSUSED*/
10180f1f7826SRichard Lowe void
onintr(sig)10197c478bd9Sstevel@tonic-gate onintr(sig)
10207c478bd9Sstevel@tonic-gate int sig;
10217c478bd9Sstevel@tonic-gate {
10227c478bd9Sstevel@tonic-gate #ifndef CBREAK
10237c478bd9Sstevel@tonic-gate 	signal(SIGINT, onintr);
10247c478bd9Sstevel@tonic-gate #else
10257c478bd9Sstevel@tonic-gate 	signal(SIGINT, inopen ? vintr : onintr);
10267c478bd9Sstevel@tonic-gate #endif
10277c478bd9Sstevel@tonic-gate 	cancelalarm();
10287c478bd9Sstevel@tonic-gate 	draino();
10297c478bd9Sstevel@tonic-gate 	if (!inopen) {
10307c478bd9Sstevel@tonic-gate 		pstop();
10317c478bd9Sstevel@tonic-gate 		setlastchar('\n');
10327c478bd9Sstevel@tonic-gate #ifdef CBREAK
10337c478bd9Sstevel@tonic-gate 	}
10347c478bd9Sstevel@tonic-gate #else
10357c478bd9Sstevel@tonic-gate 	} else
10367c478bd9Sstevel@tonic-gate 		vraw();
10377c478bd9Sstevel@tonic-gate #endif
10387c478bd9Sstevel@tonic-gate 	error(gettext("\nInterrupt") + (inopen!=0));
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate /*
10427c478bd9Sstevel@tonic-gate  * If we are interruptible, enable interrupts again.
10437c478bd9Sstevel@tonic-gate  * In some critical sections we turn interrupts off,
10447c478bd9Sstevel@tonic-gate  * but not very often.
10457c478bd9Sstevel@tonic-gate  */
1046f6db9f27Scf void
setrupt(void)1047f6db9f27Scf setrupt(void)
10487c478bd9Sstevel@tonic-gate {
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	if (ruptible) {
10517c478bd9Sstevel@tonic-gate #ifndef CBREAK
10527c478bd9Sstevel@tonic-gate 		signal(SIGINT, onintr);
10537c478bd9Sstevel@tonic-gate #else
10547c478bd9Sstevel@tonic-gate 		signal(SIGINT, inopen ? vintr : onintr);
10557c478bd9Sstevel@tonic-gate #endif
10567c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
10577c478bd9Sstevel@tonic-gate 		if (dosusp)
10587c478bd9Sstevel@tonic-gate 			signal(SIGTSTP, onsusp);
10597c478bd9Sstevel@tonic-gate #endif
10607c478bd9Sstevel@tonic-gate 	}
10617c478bd9Sstevel@tonic-gate }
10627c478bd9Sstevel@tonic-gate 
1063f6db9f27Scf int
preserve(void)1064f6db9f27Scf preserve(void)
10657c478bd9Sstevel@tonic-gate {
10667c478bd9Sstevel@tonic-gate 
10677c478bd9Sstevel@tonic-gate #ifdef VMUNIX
10687c478bd9Sstevel@tonic-gate 	tflush();
10697c478bd9Sstevel@tonic-gate #endif
10707c478bd9Sstevel@tonic-gate 	synctmp();
10717c478bd9Sstevel@tonic-gate 	pid = fork();
10727c478bd9Sstevel@tonic-gate 	if (pid < 0)
10737c478bd9Sstevel@tonic-gate 		return (0);
10747c478bd9Sstevel@tonic-gate 	if (pid == 0) {
10757c478bd9Sstevel@tonic-gate 		close(0);
10767c478bd9Sstevel@tonic-gate 		dup(tfile);
10777c478bd9Sstevel@tonic-gate 		execlp(EXPRESERVE, "expreserve", (char *) 0);
10787c478bd9Sstevel@tonic-gate 		exit(++errcnt);
10797c478bd9Sstevel@tonic-gate 	}
10807c478bd9Sstevel@tonic-gate 	waitfor();
10817c478bd9Sstevel@tonic-gate 	if (rpid == pid && status == 0)
10827c478bd9Sstevel@tonic-gate 		return (1);
10837c478bd9Sstevel@tonic-gate 	return (0);
10847c478bd9Sstevel@tonic-gate }
10857c478bd9Sstevel@tonic-gate 
10867c478bd9Sstevel@tonic-gate #ifndef V6
exit(i)10877c478bd9Sstevel@tonic-gate void exit(i)
10887c478bd9Sstevel@tonic-gate 	int i;
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 
1091f6db9f27Scf 	extern void _exit(int) __NORETURN;
10927c478bd9Sstevel@tonic-gate #ifdef TRACE
10937c478bd9Sstevel@tonic-gate 	if (trace)
10947c478bd9Sstevel@tonic-gate 		fclose(trace);
10957c478bd9Sstevel@tonic-gate #endif
10967c478bd9Sstevel@tonic-gate 	_exit(i);
10977c478bd9Sstevel@tonic-gate }
10987c478bd9Sstevel@tonic-gate #endif
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate #ifdef SIGTSTP
11017c478bd9Sstevel@tonic-gate /*
11027c478bd9Sstevel@tonic-gate  * We have just gotten a susp.  Suspend and prepare to resume.
11037c478bd9Sstevel@tonic-gate  */
11047c478bd9Sstevel@tonic-gate extern void redraw();
11057c478bd9Sstevel@tonic-gate 
11067c478bd9Sstevel@tonic-gate /*ARGSUSED*/
11070f1f7826SRichard Lowe void
onsusp(sig)11087c478bd9Sstevel@tonic-gate onsusp(sig)
11097c478bd9Sstevel@tonic-gate int sig;
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	ttymode f;
11127c478bd9Sstevel@tonic-gate 	int savenormtty;
11137c478bd9Sstevel@tonic-gate 
11147c478bd9Sstevel@tonic-gate 	f = setty(normf);
11157c478bd9Sstevel@tonic-gate 	vnfl();
1116f6db9f27Scf 	putpad((unsigned char *)exit_ca_mode);
11177c478bd9Sstevel@tonic-gate 	flush();
11187c478bd9Sstevel@tonic-gate 	resetterm();
11197c478bd9Sstevel@tonic-gate 	savenormtty = normtty;
11207c478bd9Sstevel@tonic-gate 	normtty = 0;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	signal(SIGTSTP, SIG_DFL);
11237c478bd9Sstevel@tonic-gate 	kill(0, SIGTSTP);
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate 	/* the pc stops here */
11267c478bd9Sstevel@tonic-gate 
11277c478bd9Sstevel@tonic-gate 	signal(SIGTSTP, onsusp);
11287c478bd9Sstevel@tonic-gate 	normtty = savenormtty;
11297c478bd9Sstevel@tonic-gate 	vcontin(0);
11307c478bd9Sstevel@tonic-gate 	flush();
11317c478bd9Sstevel@tonic-gate 	setty(f);
11327c478bd9Sstevel@tonic-gate 	if (!inopen)
11339097ca5cSToomas Soome 		syserror(0);
11347c478bd9Sstevel@tonic-gate 	else {
11357c478bd9Sstevel@tonic-gate 		if(vcnt < 0) {
11367c478bd9Sstevel@tonic-gate 			vcnt = -vcnt;
11377c478bd9Sstevel@tonic-gate 			if(state == VISUAL)
11387c478bd9Sstevel@tonic-gate 				vclear();
11397c478bd9Sstevel@tonic-gate 			else if(state == CRTOPEN)
11407c478bd9Sstevel@tonic-gate 				vcnt = 0;
11417c478bd9Sstevel@tonic-gate 		}
11427c478bd9Sstevel@tonic-gate 		vdirty(0, lines);
11437c478bd9Sstevel@tonic-gate 		if (sig)
11447c478bd9Sstevel@tonic-gate 			vrepaint(cursor);
11450f1f7826SRichard Lowe 	}
11467c478bd9Sstevel@tonic-gate }
11477c478bd9Sstevel@tonic-gate #endif
11487c478bd9Sstevel@tonic-gate 
nextchr(cursor)11497c478bd9Sstevel@tonic-gate unsigned char *nextchr(cursor)
11507c478bd9Sstevel@tonic-gate unsigned char *cursor;
11517c478bd9Sstevel@tonic-gate {
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate 	wchar_t wchar;
11547c478bd9Sstevel@tonic-gate 	int length;
11557c478bd9Sstevel@tonic-gate 	length = mbtowc(&wchar, (char *)cursor, MULTI_BYTE_MAX);
11567c478bd9Sstevel@tonic-gate 	if(length <= 0)
11577c478bd9Sstevel@tonic-gate 		return(++cursor);
11587c478bd9Sstevel@tonic-gate 	return(cursor + length);
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
lastchr(linebuf,cursor)11617c478bd9Sstevel@tonic-gate unsigned char *lastchr(linebuf, cursor)
11627c478bd9Sstevel@tonic-gate unsigned char *linebuf, *cursor;
11637c478bd9Sstevel@tonic-gate {
11647c478bd9Sstevel@tonic-gate 	wchar_t wchar;
11657c478bd9Sstevel@tonic-gate 	int length;
11667c478bd9Sstevel@tonic-gate 	unsigned char *ccursor, *ocursor;
11677c478bd9Sstevel@tonic-gate 	if(cursor == linebuf)
11687c478bd9Sstevel@tonic-gate 		return(linebuf - 1);
11697c478bd9Sstevel@tonic-gate 	ccursor = ocursor = linebuf;
11707c478bd9Sstevel@tonic-gate 	while(ccursor < cursor) {
11717c478bd9Sstevel@tonic-gate 		length = mbtowc(&wchar, (char *)ccursor, MULTI_BYTE_MAX);
11727c478bd9Sstevel@tonic-gate 		ocursor =  ccursor;
11737c478bd9Sstevel@tonic-gate 		if(length <= 0)
11747c478bd9Sstevel@tonic-gate 			ccursor++;
11757c478bd9Sstevel@tonic-gate 		else
11767c478bd9Sstevel@tonic-gate 			ccursor += length;
11777c478bd9Sstevel@tonic-gate 	}
11787c478bd9Sstevel@tonic-gate 	return(ocursor);
11790f1f7826SRichard Lowe }
11807c478bd9Sstevel@tonic-gate 
1181f6db9f27Scf int
ixlatctl(int flag)1182f6db9f27Scf ixlatctl(int flag)
11837c478bd9Sstevel@tonic-gate {
11847c478bd9Sstevel@tonic-gate 	static struct strioctl sb = {0, 0, 0, 0};
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	if (!(MULTI_BYTE_MAX > 1))
11877c478bd9Sstevel@tonic-gate 		return (0);
11887c478bd9Sstevel@tonic-gate 
11897c478bd9Sstevel@tonic-gate 	switch (flag) {
11907c478bd9Sstevel@tonic-gate 	case 0:
11917c478bd9Sstevel@tonic-gate 		sb.ic_cmd = EUC_MSAVE;
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 1:
11987c478bd9Sstevel@tonic-gate 		sb.ic_cmd = EUC_MREST;
11997c478bd9Sstevel@tonic-gate 		sb.ic_len = 0;
12007c478bd9Sstevel@tonic-gate 		sb.ic_dp = 0;
12017c478bd9Sstevel@tonic-gate 		if (ioctl(0, I_STR, &sb) < 0)
12027c478bd9Sstevel@tonic-gate 			return (-1);
12037c478bd9Sstevel@tonic-gate 		return (0);
12047c478bd9Sstevel@tonic-gate 	case 11:
12057c478bd9Sstevel@tonic-gate 		return (0);
12067c478bd9Sstevel@tonic-gate 	default:
12077c478bd9Sstevel@tonic-gate 		return (-1);
12087c478bd9Sstevel@tonic-gate 	}
12097c478bd9Sstevel@tonic-gate }
12107c478bd9Sstevel@tonic-gate #ifndef PRESUNEUC
12117c478bd9Sstevel@tonic-gate 
12127c478bd9Sstevel@tonic-gate /* locale specific initialization */
1213f6db9f27Scf void
localize(void)1214f6db9f27Scf localize(void)
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	wchar_t fillerchar;
12177c478bd9Sstevel@tonic-gate 	extern int	wdchkind();
12187c478bd9Sstevel@tonic-gate 	extern int	wdbindf();
12197c478bd9Sstevel@tonic-gate 	extern wchar_t	*wddelim();
12207c478bd9Sstevel@tonic-gate 	extern wchar_t	mcfiller();
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate 	wdwc = wdchkind;
12237c478bd9Sstevel@tonic-gate 	wdbdg = wdbindf;
12247c478bd9Sstevel@tonic-gate 	wddlm = wddelim;
12257c478bd9Sstevel@tonic-gate 	mcfllr = mcfiller;
12267c478bd9Sstevel@tonic-gate 	mc_wrap = 1;
12277c478bd9Sstevel@tonic-gate 	fillerchar = mcfiller();
12287c478bd9Sstevel@tonic-gate 	mc_filler = isascii(fillerchar) ? (fillerchar & 0x7f) : '~';
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate #endif /* PRESUNEUC */
1231