xref: /onnv-gate/usr/src/cmd/vi/port/ex_cmds.c (revision 13093:48f2dbca79a2)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
54488Scf46844  * Common Development and Distribution License (the "License").
64488Scf46844  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*13093SRoger.Faulkner@Oracle.COM 
22500Sceastha /*
23*13093SRoger.Faulkner@Oracle.COM  * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
24500Sceastha  */
25500Sceastha 
260Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
270Sstevel@tonic-gate /*	  All Rights Reserved  	*/
280Sstevel@tonic-gate 
290Sstevel@tonic-gate 
300Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
310Sstevel@tonic-gate 
320Sstevel@tonic-gate #include "ex.h"
330Sstevel@tonic-gate #include "ex_argv.h"
340Sstevel@tonic-gate #include "ex_temp.h"
350Sstevel@tonic-gate #include "ex_tty.h"
360Sstevel@tonic-gate #include "ex_vis.h"
370Sstevel@tonic-gate 
380Sstevel@tonic-gate bool	pflag, nflag;
390Sstevel@tonic-gate int	poffset;
400Sstevel@tonic-gate 
410Sstevel@tonic-gate #define	nochng()	lchng = chng
420Sstevel@tonic-gate 
430Sstevel@tonic-gate 
440Sstevel@tonic-gate /*
450Sstevel@tonic-gate  * Main loop for command mode command decoding.
460Sstevel@tonic-gate  * A few commands are executed here, but main function
470Sstevel@tonic-gate  * is to strip command addresses, do a little address oriented
480Sstevel@tonic-gate  * processing and call command routines to do the real work.
490Sstevel@tonic-gate  */
500Sstevel@tonic-gate extern unsigned char *Version;
51802Scf46844 void
commands(noprompt,exitoneof)520Sstevel@tonic-gate commands(noprompt, exitoneof)
530Sstevel@tonic-gate 	bool noprompt, exitoneof;
540Sstevel@tonic-gate {
550Sstevel@tonic-gate 	line *addr;
560Sstevel@tonic-gate 	int c;
570Sstevel@tonic-gate 	int lchng;
580Sstevel@tonic-gate 	int given;
590Sstevel@tonic-gate 	int seensemi;
600Sstevel@tonic-gate 	int cnt;
610Sstevel@tonic-gate 	bool hadpr;
620Sstevel@tonic-gate 	bool gotfile;
630Sstevel@tonic-gate #ifdef XPG4
640Sstevel@tonic-gate 	int d;
650Sstevel@tonic-gate #endif /* XPG4 */
660Sstevel@tonic-gate 	unsigned char *vgetpass();
670Sstevel@tonic-gate 
680Sstevel@tonic-gate 	resetflav();
690Sstevel@tonic-gate 	nochng();
700Sstevel@tonic-gate 	for (;;) {
710Sstevel@tonic-gate 		if (!firstpat)
720Sstevel@tonic-gate 			laste = 0;
730Sstevel@tonic-gate 		/*
740Sstevel@tonic-gate 		 * If dot at last command
750Sstevel@tonic-gate 		 * ended up at zero, advance to one if there is a such.
760Sstevel@tonic-gate 		 */
770Sstevel@tonic-gate 		if (dot <= zero) {
780Sstevel@tonic-gate 			dot = zero;
790Sstevel@tonic-gate 			if (dol > zero)
800Sstevel@tonic-gate 				dot = one;
810Sstevel@tonic-gate 		}
820Sstevel@tonic-gate 		shudclob = 0;
830Sstevel@tonic-gate 
840Sstevel@tonic-gate 		/*
850Sstevel@tonic-gate 		 * If autoprint or trailing print flags,
860Sstevel@tonic-gate 		 * print the line at the specified offset
870Sstevel@tonic-gate 		 * before the next command.
880Sstevel@tonic-gate 		 */
890Sstevel@tonic-gate 		if ((pflag || lchng != chng && value(vi_AUTOPRINT) &&
900Sstevel@tonic-gate 		    !inglobal && !inopen && endline) || poffset != 0) {
910Sstevel@tonic-gate 			pflag = 0;
920Sstevel@tonic-gate 			nochng();
930Sstevel@tonic-gate 			if (dol != zero) {
940Sstevel@tonic-gate 				addr1 = addr2 = dot + poffset;
950Sstevel@tonic-gate 				poffset = 0;
960Sstevel@tonic-gate 				if (addr1 < one || addr1 > dol)
970Sstevel@tonic-gate 					error(value(vi_TERSE) ?
980Sstevel@tonic-gate 					    gettext("Offset out-of-bounds") :
990Sstevel@tonic-gate 					    gettext("Offset after command "
1000Sstevel@tonic-gate 						"too large"));
1010Sstevel@tonic-gate 				dot = addr1;
1020Sstevel@tonic-gate 				setdot1();
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 				goto print;
1050Sstevel@tonic-gate 			}
1060Sstevel@tonic-gate 		}
1070Sstevel@tonic-gate 		nochng();
1080Sstevel@tonic-gate 
1090Sstevel@tonic-gate 		/*
1100Sstevel@tonic-gate 		 * Print prompt if appropriate.
1110Sstevel@tonic-gate 		 * If not in global flush output first to prevent
1120Sstevel@tonic-gate 		 * going into pfast mode unreasonably.
1130Sstevel@tonic-gate 		 */
1140Sstevel@tonic-gate 		if (inglobal == 0) {
1150Sstevel@tonic-gate 			flush();
1160Sstevel@tonic-gate 			if (!hush && value(vi_PROMPT) && !globp &&
1170Sstevel@tonic-gate 			    !noprompt && endline) {
1180Sstevel@tonic-gate 				putchar(':');
1190Sstevel@tonic-gate 				hadpr = 1;
1200Sstevel@tonic-gate 			}
1210Sstevel@tonic-gate 			TSYNC();
1220Sstevel@tonic-gate 		}
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 		/*
1250Sstevel@tonic-gate 		 * Gobble up the address.
1260Sstevel@tonic-gate 		 * Degenerate addresses yield ".".
1270Sstevel@tonic-gate 		 */
1280Sstevel@tonic-gate 		addr2 = 0;
1290Sstevel@tonic-gate 		given = seensemi = 0;
1300Sstevel@tonic-gate 		do {
1310Sstevel@tonic-gate 			addr1 = addr2;
1320Sstevel@tonic-gate 			addr = address(0);
1330Sstevel@tonic-gate 			c = getcd();
1340Sstevel@tonic-gate 			if (addr == 0) {
1350Sstevel@tonic-gate 				if (c == ',' || c == ';')
1360Sstevel@tonic-gate 					addr = dot;
1370Sstevel@tonic-gate 				else if (addr1 != 0) {
1380Sstevel@tonic-gate 					addr2 = dot;
1390Sstevel@tonic-gate 					break;
1400Sstevel@tonic-gate 				} else
1410Sstevel@tonic-gate 					break;
1420Sstevel@tonic-gate 			}
1430Sstevel@tonic-gate 			addr2 = addr;
1440Sstevel@tonic-gate 			given++;
1450Sstevel@tonic-gate 			if (c == ';') {
1460Sstevel@tonic-gate 				c = ',';
1470Sstevel@tonic-gate 				dot = addr;
1480Sstevel@tonic-gate 				seensemi = 1;
1490Sstevel@tonic-gate 			}
1500Sstevel@tonic-gate 		} while (c == ',');
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 		if (c == '%') {
1530Sstevel@tonic-gate 			/* %: same as 1,$ */
1540Sstevel@tonic-gate 			addr1 = one;
1550Sstevel@tonic-gate 			addr2 = dol;
1560Sstevel@tonic-gate 			given = 2;
1570Sstevel@tonic-gate 			c = getchar();
1580Sstevel@tonic-gate 		}
1590Sstevel@tonic-gate 		if (addr1 == 0)
1600Sstevel@tonic-gate 			addr1 = addr2;
1610Sstevel@tonic-gate 
1620Sstevel@tonic-gate 		/*
1630Sstevel@tonic-gate 		 * eat multiple colons
1640Sstevel@tonic-gate 		 */
1650Sstevel@tonic-gate 		while (c == ':')
1660Sstevel@tonic-gate 			c = getchar();
1670Sstevel@tonic-gate 		/*
1680Sstevel@tonic-gate 		 * Set command name for special character commands.
1690Sstevel@tonic-gate 		 */
1700Sstevel@tonic-gate 		tailspec(c);
1710Sstevel@tonic-gate 
1720Sstevel@tonic-gate 		/*
1730Sstevel@tonic-gate 		 * If called via : escape from open or visual, limit
1740Sstevel@tonic-gate 		 * the set of available commands here to save work below.
1750Sstevel@tonic-gate 		 */
1760Sstevel@tonic-gate 		if (inopen) {
1770Sstevel@tonic-gate 			if (c == '\n' || c == '\r' ||
1780Sstevel@tonic-gate 			    c == CTRL('d') || c == EOF) {
1790Sstevel@tonic-gate 				if (addr2)
1800Sstevel@tonic-gate 					dot = addr2;
1810Sstevel@tonic-gate 				if (c == EOF)
1820Sstevel@tonic-gate 					return;
1830Sstevel@tonic-gate 				continue;
1840Sstevel@tonic-gate 			}
1850Sstevel@tonic-gate 			if (any(c, "o"))
1860Sstevel@tonic-gate notinvis:
1870Sstevel@tonic-gate 				tailprim(Command, 1, 1);
1880Sstevel@tonic-gate 		}
1890Sstevel@tonic-gate 		switch (c) {
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 		case 'a':
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 			switch (peekchar()) {
1940Sstevel@tonic-gate 			case 'b':
1950Sstevel@tonic-gate /* abbreviate */
1960Sstevel@tonic-gate 				tail("abbreviate");
1970Sstevel@tonic-gate 				setnoaddr();
1980Sstevel@tonic-gate 				mapcmd(0, 1);
1990Sstevel@tonic-gate 				anyabbrs = 1;
2000Sstevel@tonic-gate 				continue;
2010Sstevel@tonic-gate 			case 'r':
2020Sstevel@tonic-gate /* args */
2030Sstevel@tonic-gate 				tail("args");
2040Sstevel@tonic-gate 				setnoaddr();
2050Sstevel@tonic-gate 				eol();
2060Sstevel@tonic-gate 				pargs();
2070Sstevel@tonic-gate 				continue;
2080Sstevel@tonic-gate 			}
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /* append */
2110Sstevel@tonic-gate 			if (inopen)
2120Sstevel@tonic-gate 				goto notinvis;
2130Sstevel@tonic-gate 			tail("append");
2140Sstevel@tonic-gate 			setdot();
2150Sstevel@tonic-gate 			aiflag = exclam();
2160Sstevel@tonic-gate 			donewline();
2170Sstevel@tonic-gate 			vmacchng(0);
2180Sstevel@tonic-gate 			deletenone();
2190Sstevel@tonic-gate 			setin(addr2);
2200Sstevel@tonic-gate 			inappend = 1;
2210Sstevel@tonic-gate 			(void) append(gettty, addr2);
2220Sstevel@tonic-gate 			inappend = 0;
2230Sstevel@tonic-gate 			nochng();
2240Sstevel@tonic-gate 			continue;
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 		case 'c':
2270Sstevel@tonic-gate 			switch (peekchar()) {
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate /* copy */
2300Sstevel@tonic-gate 			case 'o':
2310Sstevel@tonic-gate 				tail("copy");
2320Sstevel@tonic-gate 				vmacchng(0);
2330Sstevel@tonic-gate 				vi_move();
2340Sstevel@tonic-gate 				continue;
2350Sstevel@tonic-gate 
2360Sstevel@tonic-gate /* crypt */
2370Sstevel@tonic-gate 			case 'r':
2380Sstevel@tonic-gate 				tail("crypt");
2390Sstevel@tonic-gate 				crflag = -1;
2400Sstevel@tonic-gate 			ent_crypt:
2410Sstevel@tonic-gate 				setnoaddr();
2420Sstevel@tonic-gate 				xflag = 1;
2430Sstevel@tonic-gate 				if (permflag)
2440Sstevel@tonic-gate 					(void) crypt_close(perm);
2450Sstevel@tonic-gate 				permflag = 1;
2460Sstevel@tonic-gate 				if ((kflag = run_setkey(perm,
2470Sstevel@tonic-gate 				    (key = vgetpass(
2480Sstevel@tonic-gate 					gettext("Enter key:"))))) == -1) {
2490Sstevel@tonic-gate 					xflag = 0;
2500Sstevel@tonic-gate 					kflag = 0;
2510Sstevel@tonic-gate 					crflag = 0;
2520Sstevel@tonic-gate 					smerror(gettext("Encryption facility "
2530Sstevel@tonic-gate 						    "not available\n"));
2540Sstevel@tonic-gate 				}
2550Sstevel@tonic-gate 				if (kflag == 0)
2560Sstevel@tonic-gate 					crflag = 0;
2570Sstevel@tonic-gate 				continue;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate /* cd */
2600Sstevel@tonic-gate 			case 'd':
2610Sstevel@tonic-gate 				tail("cd");
2620Sstevel@tonic-gate 				goto changdir;
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate /* chdir */
2650Sstevel@tonic-gate 			case 'h':
2660Sstevel@tonic-gate 				ignchar();
2670Sstevel@tonic-gate 				if (peekchar() == 'd') {
268802Scf46844 					unsigned char *p;
2690Sstevel@tonic-gate 					tail2of("chdir");
2700Sstevel@tonic-gate changdir:
2710Sstevel@tonic-gate 					if (savedfile[0] == '/' ||
2720Sstevel@tonic-gate 					    !value(vi_WARN))
2730Sstevel@tonic-gate 						(void) exclam();
2740Sstevel@tonic-gate 					else
2750Sstevel@tonic-gate 						(void) quickly();
2760Sstevel@tonic-gate 					if (skipend()) {
2770Sstevel@tonic-gate 						p = (unsigned char *)
2780Sstevel@tonic-gate 						    getenv("HOME");
2790Sstevel@tonic-gate 						if (p == NULL)
2800Sstevel@tonic-gate 							error(gettext(
2810Sstevel@tonic-gate 								"Home directory"
2820Sstevel@tonic-gate 								    /*CSTYLED*/
2830Sstevel@tonic-gate 								    " unknown"));
2840Sstevel@tonic-gate 					} else
2850Sstevel@tonic-gate 						getone(), p = file;
2860Sstevel@tonic-gate 					eol();
287802Scf46844 					if (chdir((char *)p) < 0)
2880Sstevel@tonic-gate 						filioerr(p);
2890Sstevel@tonic-gate 					if (savedfile[0] != '/')
2900Sstevel@tonic-gate 						edited = 0;
2910Sstevel@tonic-gate 					continue;
2920Sstevel@tonic-gate 				}
2930Sstevel@tonic-gate 				if (inopen)
294802Scf46844 					tailprim((unsigned char *)"change",
295802Scf46844 					    2, 1);
2960Sstevel@tonic-gate 				tail2of("change");
2970Sstevel@tonic-gate 				break;
2980Sstevel@tonic-gate 
2990Sstevel@tonic-gate 			default:
3000Sstevel@tonic-gate 				if (inopen)
3010Sstevel@tonic-gate 					goto notinvis;
3020Sstevel@tonic-gate 				tail("change");
3030Sstevel@tonic-gate 				break;
3040Sstevel@tonic-gate 			}
3050Sstevel@tonic-gate /* change */
3060Sstevel@tonic-gate 			aiflag = exclam();
3070Sstevel@tonic-gate #ifdef XPG4ONLY
3080Sstevel@tonic-gate 			setcount2();
3090Sstevel@tonic-gate 			donewline();
3100Sstevel@tonic-gate #else /* XPG6 and Solaris */
3110Sstevel@tonic-gate 			setCNL();
3120Sstevel@tonic-gate #endif /* XPG4ONLY */
3130Sstevel@tonic-gate 			vmacchng(0);
3140Sstevel@tonic-gate 			setin(addr1);
315802Scf46844 			(void) delete(0);
3160Sstevel@tonic-gate 			inappend = 1;
3170Sstevel@tonic-gate 			if (append(gettty, addr1 - 1) == 0) {
3180Sstevel@tonic-gate #ifdef XPG4
3190Sstevel@tonic-gate 				/*
3200Sstevel@tonic-gate 				 * P2003.2/D9:5.10.7.2.4, p. 646,
3210Sstevel@tonic-gate 				 * assertion 214(A). If nothing changed,
3220Sstevel@tonic-gate 				 * set dot to the line preceding the lines
3230Sstevel@tonic-gate 				 * to be changed.
3240Sstevel@tonic-gate 				 */
3250Sstevel@tonic-gate 				dot = addr1 - 1;
3260Sstevel@tonic-gate #else /* XPG4 */
3270Sstevel@tonic-gate 				dot = addr1;
3280Sstevel@tonic-gate #endif /* XPG4 */
3290Sstevel@tonic-gate 				if (dot > dol)
3300Sstevel@tonic-gate 					dot = dol;
3310Sstevel@tonic-gate 			}
3320Sstevel@tonic-gate 			inappend = 0;
3330Sstevel@tonic-gate 			nochng();
3340Sstevel@tonic-gate 			continue;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate /* delete */
3370Sstevel@tonic-gate 		case 'd':
3380Sstevel@tonic-gate 			/*
3390Sstevel@tonic-gate 			 * Caution: dp and dl have special meaning already.
3400Sstevel@tonic-gate 			 */
3410Sstevel@tonic-gate 			tail("delete");
3420Sstevel@tonic-gate 			c = cmdreg();
3430Sstevel@tonic-gate #ifdef XPG4ONLY
3440Sstevel@tonic-gate 			setcount2();
3450Sstevel@tonic-gate 			donewline();
3460Sstevel@tonic-gate #else /* XPG6 and Solaris */
3470Sstevel@tonic-gate 			setCNL();
3480Sstevel@tonic-gate #endif /* XPG4ONLY */
3490Sstevel@tonic-gate 			vmacchng(0);
3500Sstevel@tonic-gate 			if (c)
351802Scf46844 				(void) YANKreg(c);
352802Scf46844 			(void) delete(0);
3530Sstevel@tonic-gate 			appendnone();
3540Sstevel@tonic-gate 			continue;
3550Sstevel@tonic-gate 
3560Sstevel@tonic-gate /* edit */
3570Sstevel@tonic-gate /* ex */
3580Sstevel@tonic-gate 		case 'e':
3590Sstevel@tonic-gate 			if (crflag == 2 || crflag == -2)
3600Sstevel@tonic-gate 				crflag = -1;
3610Sstevel@tonic-gate 			tail(peekchar() == 'x' ? "ex" : "edit");
3620Sstevel@tonic-gate editcmd:
3630Sstevel@tonic-gate 			if (!exclam() && chng)
3640Sstevel@tonic-gate 				c = 'E';
3650Sstevel@tonic-gate 			gotfile = 0;
3660Sstevel@tonic-gate 			if (c == 'E') {
3670Sstevel@tonic-gate 				if (inopen && !value(vi_AUTOWRITE)) {
3680Sstevel@tonic-gate 					filename(c);
3690Sstevel@tonic-gate 					gotfile = 1;
3700Sstevel@tonic-gate 				}
3710Sstevel@tonic-gate 				ungetchar(lastchar());
3720Sstevel@tonic-gate 				if (!exclam()) {
3730Sstevel@tonic-gate 					ckaw();
3740Sstevel@tonic-gate 					if (chng && dol > zero) {
3750Sstevel@tonic-gate 						xchng = 0;
3760Sstevel@tonic-gate 						error(value(vi_TERSE) ?
3770Sstevel@tonic-gate 						    gettext("No write") :
3780Sstevel@tonic-gate 						    gettext("No write since "
3790Sstevel@tonic-gate 							"last change (:%s! "
3800Sstevel@tonic-gate 							"overrides)"),
3810Sstevel@tonic-gate 						    Command);
3820Sstevel@tonic-gate 					}
3830Sstevel@tonic-gate 				}
3840Sstevel@tonic-gate 
3850Sstevel@tonic-gate 			}
3860Sstevel@tonic-gate 			if (gotfile == 0)
3870Sstevel@tonic-gate 				filename(c);
3880Sstevel@tonic-gate 			setnoaddr();
3890Sstevel@tonic-gate doecmd:
3900Sstevel@tonic-gate 			init();
3910Sstevel@tonic-gate 			addr2 = zero;
3920Sstevel@tonic-gate 			laste++;
3930Sstevel@tonic-gate 			sync();
3940Sstevel@tonic-gate 			rop(c);
3950Sstevel@tonic-gate 			nochng();
3960Sstevel@tonic-gate 			continue;
3970Sstevel@tonic-gate 
3980Sstevel@tonic-gate /* file */
3990Sstevel@tonic-gate 		case 'f':
4000Sstevel@tonic-gate 			tail("file");
4010Sstevel@tonic-gate 			setnoaddr();
4020Sstevel@tonic-gate 			filename(c);
4030Sstevel@tonic-gate 			noonl();
4040Sstevel@tonic-gate /*
4050Sstevel@tonic-gate  *			synctmp();
4060Sstevel@tonic-gate  */
4070Sstevel@tonic-gate 			continue;
4080Sstevel@tonic-gate 
4090Sstevel@tonic-gate /* global */
4100Sstevel@tonic-gate 		case 'g':
4110Sstevel@tonic-gate 			tail("global");
4120Sstevel@tonic-gate 			global(!exclam());
4130Sstevel@tonic-gate 			nochng();
4140Sstevel@tonic-gate 			continue;
4150Sstevel@tonic-gate 
4160Sstevel@tonic-gate /* insert */
4170Sstevel@tonic-gate 		case 'i':
4180Sstevel@tonic-gate 			if (inopen)
4190Sstevel@tonic-gate 				goto notinvis;
4200Sstevel@tonic-gate 			tail("insert");
4210Sstevel@tonic-gate 			setdot();
4220Sstevel@tonic-gate 			nonzero();
4230Sstevel@tonic-gate 			aiflag = exclam();
4240Sstevel@tonic-gate 			donewline();
4250Sstevel@tonic-gate 			vmacchng(0);
4260Sstevel@tonic-gate 			deletenone();
4270Sstevel@tonic-gate 			setin(addr2);
4280Sstevel@tonic-gate 			inappend = 1;
4290Sstevel@tonic-gate 			(void) append(gettty, addr2 - 1);
4300Sstevel@tonic-gate 			inappend = 0;
4310Sstevel@tonic-gate 			if (dot == zero && dol > zero)
4320Sstevel@tonic-gate 				dot = one;
4330Sstevel@tonic-gate 			nochng();
4340Sstevel@tonic-gate 			continue;
4350Sstevel@tonic-gate 
4360Sstevel@tonic-gate /* join */
4370Sstevel@tonic-gate 		case 'j':
4380Sstevel@tonic-gate 			tail("join");
4390Sstevel@tonic-gate 			c = exclam();
4400Sstevel@tonic-gate 			setcount();
4410Sstevel@tonic-gate 			nonzero();
4420Sstevel@tonic-gate 			donewline();
4430Sstevel@tonic-gate 			vmacchng(0);
4440Sstevel@tonic-gate #ifdef XPG4ONLY
4450Sstevel@tonic-gate 			/*
4460Sstevel@tonic-gate 			 * if no count was specified, addr1 == addr2. if only
4470Sstevel@tonic-gate 			 * 1 range arg was specified, inc addr2 to allow
4480Sstevel@tonic-gate 			 * joining of the next line.
4490Sstevel@tonic-gate 			 */
4500Sstevel@tonic-gate 			if (given < 2 && (addr1 == addr2) && (addr2 != dol))
4510Sstevel@tonic-gate 				addr2++;
4520Sstevel@tonic-gate 
4530Sstevel@tonic-gate #else /* XPG6 and Solaris */
4540Sstevel@tonic-gate 			if (given < 2 && addr2 != dol)
4550Sstevel@tonic-gate 				addr2++;
4560Sstevel@tonic-gate #endif /* XPG4ONLY */
457802Scf46844 			(void) join(c);
4580Sstevel@tonic-gate 			continue;
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate /* k */
4610Sstevel@tonic-gate 		case 'k':
4620Sstevel@tonic-gate casek:
4630Sstevel@tonic-gate 			pastwh();
4640Sstevel@tonic-gate 			c = getchar();
4650Sstevel@tonic-gate 			if (endcmd(c))
466802Scf46844 				serror((vi_TERSE) ?
467802Scf46844 				    (unsigned char *)gettext("Mark what?") :
468802Scf46844 				    (unsigned char *)
469802Scf46844 				    gettext("%s requires following "
470802Scf46844 				    "letter"), Command);
4710Sstevel@tonic-gate 			donewline();
4720Sstevel@tonic-gate 			if (!islower(c))
4730Sstevel@tonic-gate 				error((vi_TERSE) ? gettext("Bad mark") :
4740Sstevel@tonic-gate 					gettext("Mark must specify a letter"));
4750Sstevel@tonic-gate 			setdot();
4760Sstevel@tonic-gate 			nonzero();
4770Sstevel@tonic-gate 			names[c - 'a'] = *addr2 &~ 01;
4780Sstevel@tonic-gate 			anymarks = 1;
4790Sstevel@tonic-gate 			continue;
4800Sstevel@tonic-gate 
4810Sstevel@tonic-gate /* list */
4820Sstevel@tonic-gate 		case 'l':
4830Sstevel@tonic-gate 			tail("list");
4840Sstevel@tonic-gate #ifdef XPG4ONLY
4850Sstevel@tonic-gate 			setcount2();
4860Sstevel@tonic-gate 			donewline();
4870Sstevel@tonic-gate #else /* XPG6 and Solaris */
4880Sstevel@tonic-gate 			setCNL();
4890Sstevel@tonic-gate #endif /* XPG4ONLY */
4900Sstevel@tonic-gate 			(void) setlist(1);
4910Sstevel@tonic-gate 			pflag = 0;
4920Sstevel@tonic-gate 			goto print;
4930Sstevel@tonic-gate 
4940Sstevel@tonic-gate 		case 'm':
4950Sstevel@tonic-gate 			if (peekchar() == 'a') {
4960Sstevel@tonic-gate 				ignchar();
4970Sstevel@tonic-gate 				if (peekchar() == 'p') {
4980Sstevel@tonic-gate /* map */
4990Sstevel@tonic-gate 					tail2of("map");
5000Sstevel@tonic-gate 					setnoaddr();
5010Sstevel@tonic-gate 					mapcmd(0, 0);
5020Sstevel@tonic-gate 					continue;
5030Sstevel@tonic-gate 				}
5040Sstevel@tonic-gate /* mark */
5050Sstevel@tonic-gate 				tail2of("mark");
5060Sstevel@tonic-gate 				goto casek;
5070Sstevel@tonic-gate 			}
5080Sstevel@tonic-gate /* move */
5090Sstevel@tonic-gate 			tail("move");
5100Sstevel@tonic-gate 			vmacchng(0);
5110Sstevel@tonic-gate 			vi_move();
5120Sstevel@tonic-gate 			continue;
5130Sstevel@tonic-gate 
5140Sstevel@tonic-gate 		case 'n':
5150Sstevel@tonic-gate 			if (peekchar() == 'u') {
5160Sstevel@tonic-gate 				tail("number");
5170Sstevel@tonic-gate 				goto numberit;
5180Sstevel@tonic-gate 			}
5190Sstevel@tonic-gate /* next */
5200Sstevel@tonic-gate 			tail("next");
5210Sstevel@tonic-gate 			setnoaddr();
5220Sstevel@tonic-gate 			if (!exclam()) {
5230Sstevel@tonic-gate 				ckaw();
5240Sstevel@tonic-gate 				if (chng && dol > zero) {
5250Sstevel@tonic-gate 					xchng = 0;
5260Sstevel@tonic-gate 					error(value(vi_TERSE) ?
5270Sstevel@tonic-gate 					    gettext("No write") :
5280Sstevel@tonic-gate 					    gettext("No write since last "
5290Sstevel@tonic-gate 						"change (:%s! overrides)"),
5300Sstevel@tonic-gate 					    Command);
5310Sstevel@tonic-gate 				}
5320Sstevel@tonic-gate 			}
5330Sstevel@tonic-gate 
5340Sstevel@tonic-gate 			if (getargs())
5350Sstevel@tonic-gate 				makargs();
5360Sstevel@tonic-gate 			next();
5370Sstevel@tonic-gate 			c = 'e';
5380Sstevel@tonic-gate 			filename(c);
5390Sstevel@tonic-gate 			goto doecmd;
5400Sstevel@tonic-gate 
5410Sstevel@tonic-gate /* open */
5420Sstevel@tonic-gate 		case 'o':
5430Sstevel@tonic-gate 			tail("open");
5440Sstevel@tonic-gate 			oop();
5450Sstevel@tonic-gate 			pflag = 0;
5460Sstevel@tonic-gate 			nochng();
5470Sstevel@tonic-gate 			continue;
5480Sstevel@tonic-gate 
5490Sstevel@tonic-gate 		case 'p':
5500Sstevel@tonic-gate 		case 'P':
5510Sstevel@tonic-gate 			switch (peekchar()) {
5520Sstevel@tonic-gate #ifdef TAG_STACK
5530Sstevel@tonic-gate /* pop */
5540Sstevel@tonic-gate 			case 'o':
5550Sstevel@tonic-gate 				tail("pop");
5560Sstevel@tonic-gate 				poptag(exclam());
5570Sstevel@tonic-gate 				if (!inopen)
5580Sstevel@tonic-gate 					lchng = chng - 1;
5590Sstevel@tonic-gate 				else
5600Sstevel@tonic-gate 					nochng();
5610Sstevel@tonic-gate 				continue;
5620Sstevel@tonic-gate #endif
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /* put */
5650Sstevel@tonic-gate 			case 'u':
5660Sstevel@tonic-gate 				tail("put");
5670Sstevel@tonic-gate 				setdot();
5680Sstevel@tonic-gate 				c = cmdreg();
5690Sstevel@tonic-gate 				eol();
5700Sstevel@tonic-gate 				vmacchng(0);
5710Sstevel@tonic-gate 				if (c)
572802Scf46844 					(void) putreg(c);
5730Sstevel@tonic-gate 				else
574802Scf46844 					(void) put();
5750Sstevel@tonic-gate 				continue;
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 			case 'r':
5780Sstevel@tonic-gate 				ignchar();
5790Sstevel@tonic-gate 				if (peekchar() == 'e') {
5800Sstevel@tonic-gate /* preserve */
5810Sstevel@tonic-gate 					tail2of("preserve");
5820Sstevel@tonic-gate 					eol();
5830Sstevel@tonic-gate 					if (preserve() == 0)
5840Sstevel@tonic-gate 						error(gettext(
5850Sstevel@tonic-gate 						    "Preserve failed!"));
5860Sstevel@tonic-gate 					else {
5870Sstevel@tonic-gate #ifdef XPG4
5880Sstevel@tonic-gate 						/*
5890Sstevel@tonic-gate 						 * error() incs errcnt. this is
5900Sstevel@tonic-gate 						 * misleading here; and a
5910Sstevel@tonic-gate 						 * violation of POSIX. so call
5920Sstevel@tonic-gate 						 * noerror() instead.
5930Sstevel@tonic-gate 						 * this is for assertion ex:222.
5940Sstevel@tonic-gate 						 */
5950Sstevel@tonic-gate 						noerror(
5960Sstevel@tonic-gate 						    gettext("File preserved."));
5970Sstevel@tonic-gate 
5980Sstevel@tonic-gate #else /* XPG4 */
5990Sstevel@tonic-gate 						error(
6000Sstevel@tonic-gate 						    gettext("File preserved."));
6010Sstevel@tonic-gate #endif /* XPG4 */
6020Sstevel@tonic-gate 					}
6030Sstevel@tonic-gate 				}
6040Sstevel@tonic-gate 				tail2of("print");
6050Sstevel@tonic-gate 				break;
6060Sstevel@tonic-gate 
6070Sstevel@tonic-gate 			default:
6080Sstevel@tonic-gate 				tail("print");
6090Sstevel@tonic-gate 				break;
6100Sstevel@tonic-gate 			}
6110Sstevel@tonic-gate /* print */
6120Sstevel@tonic-gate 			setCNL();
6130Sstevel@tonic-gate 			pflag = 0;
6140Sstevel@tonic-gate print:
6150Sstevel@tonic-gate 			nonzero();
6160Sstevel@tonic-gate 			if (clear_screen && span() > lines) {
6170Sstevel@tonic-gate 				flush1();
6180Sstevel@tonic-gate 				vclear();
6190Sstevel@tonic-gate 			}
6200Sstevel@tonic-gate 			/*
6210Sstevel@tonic-gate 			 * poffset is nonzero if trailing + or - flags
6220Sstevel@tonic-gate 			 * were given, and in that case we need to
6230Sstevel@tonic-gate 			 * adjust dot before printing a line.
6240Sstevel@tonic-gate 			 */
6250Sstevel@tonic-gate 			if (poffset == 0)
6260Sstevel@tonic-gate 				plines(addr1, addr2, 1);
6270Sstevel@tonic-gate 			else
6280Sstevel@tonic-gate 				dot = addr2;
6290Sstevel@tonic-gate 			continue;
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /* quit */
6320Sstevel@tonic-gate 		case 'q':
6330Sstevel@tonic-gate 			tail("quit");
6340Sstevel@tonic-gate 			setnoaddr();
6350Sstevel@tonic-gate 			c = quickly();
6360Sstevel@tonic-gate 			eol();
6370Sstevel@tonic-gate 			if (!c)
6380Sstevel@tonic-gate quit:
6390Sstevel@tonic-gate 				if (nomore())
6400Sstevel@tonic-gate 					continue;
6410Sstevel@tonic-gate 			if (inopen) {
6420Sstevel@tonic-gate 				vgoto(WECHO, 0);
6430Sstevel@tonic-gate 				if (!ateopr())
6440Sstevel@tonic-gate 					vnfl();
6450Sstevel@tonic-gate 				else {
6460Sstevel@tonic-gate 					tostop();
6470Sstevel@tonic-gate 				}
6480Sstevel@tonic-gate 				flush();
6490Sstevel@tonic-gate 				setty(normf);
6500Sstevel@tonic-gate 				ixlatctl(1);
6510Sstevel@tonic-gate 			}
6520Sstevel@tonic-gate 			cleanup(1);
6530Sstevel@tonic-gate 			exit(errcnt);
6540Sstevel@tonic-gate 
6550Sstevel@tonic-gate 		case 'r':
6560Sstevel@tonic-gate 			if (peekchar() == 'e') {
6570Sstevel@tonic-gate 				ignchar();
6580Sstevel@tonic-gate 				switch (peekchar()) {
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate /* rewind */
6610Sstevel@tonic-gate 				case 'w':
6620Sstevel@tonic-gate 					tail2of("rewind");
6630Sstevel@tonic-gate 					setnoaddr();
6640Sstevel@tonic-gate 					if (!exclam()) {
6650Sstevel@tonic-gate 						ckaw();
6660Sstevel@tonic-gate 						if (chng && dol > zero)
6670Sstevel@tonic-gate 							error((vi_TERSE) ?
6680Sstevel@tonic-gate 							    /*CSTYLED*/
6690Sstevel@tonic-gate 							    gettext("No write") :
6700Sstevel@tonic-gate 							    gettext("No write "
6710Sstevel@tonic-gate 								"since last "
6720Sstevel@tonic-gate 								"change (:rewi"
6730Sstevel@tonic-gate 								/*CSTYLED*/
6740Sstevel@tonic-gate 								"nd! overrides)"));
6750Sstevel@tonic-gate 					}
6760Sstevel@tonic-gate 					eol();
6770Sstevel@tonic-gate 					erewind();
6780Sstevel@tonic-gate 					next();
6790Sstevel@tonic-gate 					c = 'e';
6800Sstevel@tonic-gate 					ungetchar(lastchar());
6810Sstevel@tonic-gate 					filename(c);
6820Sstevel@tonic-gate 					goto doecmd;
6830Sstevel@tonic-gate 
6840Sstevel@tonic-gate /* recover */
6850Sstevel@tonic-gate 				case 'c':
6860Sstevel@tonic-gate 					tail2of("recover");
6870Sstevel@tonic-gate 					setnoaddr();
6880Sstevel@tonic-gate 					c = 'e';
6890Sstevel@tonic-gate 					if (!exclam() && chng)
6900Sstevel@tonic-gate 						c = 'E';
6910Sstevel@tonic-gate 					filename(c);
6920Sstevel@tonic-gate 					if (c == 'E') {
6930Sstevel@tonic-gate 						ungetchar(lastchar());
6940Sstevel@tonic-gate 						(void) quickly();
6950Sstevel@tonic-gate 					}
6960Sstevel@tonic-gate 					init();
6970Sstevel@tonic-gate 					addr2 = zero;
6980Sstevel@tonic-gate 					laste++;
6990Sstevel@tonic-gate 					sync();
7000Sstevel@tonic-gate 					recover();
7010Sstevel@tonic-gate 					rop2();
7020Sstevel@tonic-gate 					revocer();
7030Sstevel@tonic-gate 					if (status == 0)
7040Sstevel@tonic-gate 						rop3(c);
7050Sstevel@tonic-gate 					if (dol != zero)
7060Sstevel@tonic-gate 						change();
7070Sstevel@tonic-gate 					nochng();
7080Sstevel@tonic-gate 					continue;
7090Sstevel@tonic-gate 				}
7100Sstevel@tonic-gate 				tail2of("read");
7110Sstevel@tonic-gate 			} else
7120Sstevel@tonic-gate 				tail("read");
7130Sstevel@tonic-gate /* read */
7140Sstevel@tonic-gate 			if (crflag == 2 || crflag == -2)
7150Sstevel@tonic-gate 			/* restore crflag for new input text */
7160Sstevel@tonic-gate 				crflag = -1;
7170Sstevel@tonic-gate 			if (savedfile[0] == 0 && dol == zero)
7180Sstevel@tonic-gate 				c = 'e';
7190Sstevel@tonic-gate 			pastwh();
7200Sstevel@tonic-gate 			vmacchng(0);
7210Sstevel@tonic-gate 			if (peekchar() == '!') {
7220Sstevel@tonic-gate 				setdot();
7230Sstevel@tonic-gate 				ignchar();
724500Sceastha 				unix0(0, 1);
725802Scf46844 				(void) vi_filter(0);
7260Sstevel@tonic-gate 				continue;
7270Sstevel@tonic-gate 			}
7280Sstevel@tonic-gate 			filename(c);
7290Sstevel@tonic-gate 			rop(c);
7300Sstevel@tonic-gate 			nochng();
7310Sstevel@tonic-gate 			if (inopen && endline && addr1 > zero && addr1 < dol)
7320Sstevel@tonic-gate 				dot = addr1 + 1;
7330Sstevel@tonic-gate 			continue;
7340Sstevel@tonic-gate 
7350Sstevel@tonic-gate 		case 's':
7360Sstevel@tonic-gate 			switch (peekchar()) {
7370Sstevel@tonic-gate 			/*
7380Sstevel@tonic-gate 			 * Caution: 2nd char cannot be c, g, or r
7390Sstevel@tonic-gate 			 * because these have meaning to substitute.
7400Sstevel@tonic-gate 			 */
7410Sstevel@tonic-gate 
7420Sstevel@tonic-gate /* set */
7430Sstevel@tonic-gate 			case 'e':
7440Sstevel@tonic-gate 				tail("set");
7450Sstevel@tonic-gate 				setnoaddr();
7460Sstevel@tonic-gate 				set();
7470Sstevel@tonic-gate 				continue;
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate /* shell */
7500Sstevel@tonic-gate 			case 'h':
7510Sstevel@tonic-gate 				tail("shell");
7520Sstevel@tonic-gate 				setNAEOL();
7530Sstevel@tonic-gate 				vnfl();
754802Scf46844 				putpad((unsigned char *)exit_ca_mode);
7550Sstevel@tonic-gate 				flush();
7560Sstevel@tonic-gate 				resetterm();
7570Sstevel@tonic-gate 				unixwt(1, unixex("-i", (char *)0, 0, 0));
7580Sstevel@tonic-gate 				vcontin(0);
7590Sstevel@tonic-gate 				continue;
7600Sstevel@tonic-gate 
7610Sstevel@tonic-gate /* source */
7620Sstevel@tonic-gate 			case 'o':
7630Sstevel@tonic-gate #ifdef notdef
7640Sstevel@tonic-gate 				if (inopen)
7650Sstevel@tonic-gate 					goto notinvis;
7660Sstevel@tonic-gate #endif
7670Sstevel@tonic-gate 				tail("source");
7680Sstevel@tonic-gate 				setnoaddr();
7690Sstevel@tonic-gate 				getone();
7700Sstevel@tonic-gate 				eol();
7710Sstevel@tonic-gate 				source(file, 0);
7720Sstevel@tonic-gate 				continue;
7730Sstevel@tonic-gate #ifdef SIGTSTP
7740Sstevel@tonic-gate /* stop, suspend */
7750Sstevel@tonic-gate 			case 't':
7760Sstevel@tonic-gate 				tail("stop");
7770Sstevel@tonic-gate 				goto suspend;
7780Sstevel@tonic-gate 			case 'u':
7790Sstevel@tonic-gate #ifdef XPG4
7800Sstevel@tonic-gate 				/*
7810Sstevel@tonic-gate 				 * for POSIX, "su" with no other distinguishing
7820Sstevel@tonic-gate 				 * characteristics, maps to "s". Re. P1003.D11,
7830Sstevel@tonic-gate 				 * 5.10.7.3.
7840Sstevel@tonic-gate 				 *
7850Sstevel@tonic-gate 				 * so, unless the "su" is followed by a "s" or
7860Sstevel@tonic-gate 				 * a "!", we assume that the user means "s".
7870Sstevel@tonic-gate 				 */
7880Sstevel@tonic-gate 				switch (d = peekchar()) {
7890Sstevel@tonic-gate 				case 's':
7900Sstevel@tonic-gate 				case '!':
7910Sstevel@tonic-gate #endif /* XPG4 */
7920Sstevel@tonic-gate 					tail("suspend");
7930Sstevel@tonic-gate suspend:
7940Sstevel@tonic-gate 					c = exclam();
7950Sstevel@tonic-gate 					eol();
7960Sstevel@tonic-gate 					if (!c)
7970Sstevel@tonic-gate 						ckaw();
7980Sstevel@tonic-gate 					onsusp(0);
7990Sstevel@tonic-gate 					continue;
8000Sstevel@tonic-gate #ifdef XPG4
8010Sstevel@tonic-gate 				}
8020Sstevel@tonic-gate #endif /* XPG4 */
8030Sstevel@tonic-gate #endif
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 			}
8060Sstevel@tonic-gate 			/* fall into ... */
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate /* & */
8090Sstevel@tonic-gate /* ~ */
8100Sstevel@tonic-gate /* substitute */
8110Sstevel@tonic-gate 		case '&':
8120Sstevel@tonic-gate 		case '~':
8130Sstevel@tonic-gate 			Command = (unsigned char *)"substitute";
8140Sstevel@tonic-gate 			if (c == 's')
8150Sstevel@tonic-gate 				tail(Command);
8160Sstevel@tonic-gate 			vmacchng(0);
8170Sstevel@tonic-gate 			if (!substitute(c))
8180Sstevel@tonic-gate 				pflag = 0;
8190Sstevel@tonic-gate 			continue;
8200Sstevel@tonic-gate 
8210Sstevel@tonic-gate /* t */
8220Sstevel@tonic-gate 		case 't':
8230Sstevel@tonic-gate 			if (peekchar() == 'a') {
8244488Scf46844 				tagflg = 1; /* :tag command */
8250Sstevel@tonic-gate 				tail("tag");
8260Sstevel@tonic-gate 				tagfind(exclam());
8274488Scf46844 				tagflg = 0;
8280Sstevel@tonic-gate 				if (!inopen)
8290Sstevel@tonic-gate 					lchng = chng - 1;
8300Sstevel@tonic-gate 				else
8310Sstevel@tonic-gate 					nochng();
8320Sstevel@tonic-gate 				continue;
8330Sstevel@tonic-gate 			}
8340Sstevel@tonic-gate 			tail("t");
8350Sstevel@tonic-gate 			vmacchng(0);
8360Sstevel@tonic-gate 			vi_move();
8370Sstevel@tonic-gate 			continue;
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate 		case 'u':
8400Sstevel@tonic-gate 			if (peekchar() == 'n') {
8410Sstevel@tonic-gate 				ignchar();
8420Sstevel@tonic-gate 				switch (peekchar()) {
8430Sstevel@tonic-gate /* unmap */
8440Sstevel@tonic-gate 				case 'm':
8450Sstevel@tonic-gate 					tail2of("unmap");
8460Sstevel@tonic-gate 					setnoaddr();
8470Sstevel@tonic-gate 					mapcmd(1, 0);
8480Sstevel@tonic-gate 					continue;
8490Sstevel@tonic-gate /* unabbreviate */
8500Sstevel@tonic-gate 				case 'a':
8510Sstevel@tonic-gate 					tail2of("unabbreviate");
8520Sstevel@tonic-gate 					setnoaddr();
8530Sstevel@tonic-gate 					mapcmd(1, 1);
8540Sstevel@tonic-gate 					anyabbrs = 1;
8550Sstevel@tonic-gate 					continue;
8560Sstevel@tonic-gate 				}
8570Sstevel@tonic-gate /* undo */
8580Sstevel@tonic-gate 				tail2of("undo");
8590Sstevel@tonic-gate 			} else
8600Sstevel@tonic-gate 				tail("undo");
8610Sstevel@tonic-gate 			setnoaddr();
8620Sstevel@tonic-gate 			markDOT();
8630Sstevel@tonic-gate 			c = exclam();
8640Sstevel@tonic-gate 			donewline();
8650Sstevel@tonic-gate 			undo(c);
8660Sstevel@tonic-gate 			continue;
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate 		case 'v':
8690Sstevel@tonic-gate 			switch (peekchar()) {
8700Sstevel@tonic-gate 
8710Sstevel@tonic-gate 			case 'e':
8720Sstevel@tonic-gate /* version */
8730Sstevel@tonic-gate 				tail("version");
8740Sstevel@tonic-gate 				setNAEOL();
875802Scf46844 				viprintf("%s", Version);
8760Sstevel@tonic-gate 				noonl();
8770Sstevel@tonic-gate 				continue;
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate /* visual */
8800Sstevel@tonic-gate 			case 'i':
8810Sstevel@tonic-gate 				tail("visual");
8820Sstevel@tonic-gate 				if (inopen) {
8830Sstevel@tonic-gate 					c = 'e';
8840Sstevel@tonic-gate 					goto editcmd;
8850Sstevel@tonic-gate 				}
8860Sstevel@tonic-gate 				vop();
8870Sstevel@tonic-gate 				pflag = 0;
8880Sstevel@tonic-gate 				nochng();
8890Sstevel@tonic-gate 				continue;
8900Sstevel@tonic-gate 			}
8910Sstevel@tonic-gate /* v */
8920Sstevel@tonic-gate 			tail("v");
8930Sstevel@tonic-gate 			global(0);
8940Sstevel@tonic-gate 			nochng();
8950Sstevel@tonic-gate 			continue;
8960Sstevel@tonic-gate 
8970Sstevel@tonic-gate /* write */
8980Sstevel@tonic-gate 		case 'w':
8990Sstevel@tonic-gate 			c = peekchar();
9000Sstevel@tonic-gate 			tail(c == 'q' ? "wq" : "write");
9010Sstevel@tonic-gate wq:
9020Sstevel@tonic-gate 			if (skipwh() && peekchar() == '!') {
9030Sstevel@tonic-gate 				pofix();
9040Sstevel@tonic-gate 				ignchar();
9050Sstevel@tonic-gate 				setall();
906500Sceastha 				unix0(0, 1);
907802Scf46844 				(void) vi_filter(1);
9080Sstevel@tonic-gate 			} else {
9090Sstevel@tonic-gate 				setall();
9100Sstevel@tonic-gate 				if (c == 'q')
9110Sstevel@tonic-gate 					write_quit = 1;
9120Sstevel@tonic-gate 				else
9130Sstevel@tonic-gate 					write_quit = 0;
9140Sstevel@tonic-gate 				wop(1);
9150Sstevel@tonic-gate 				nochng();
9160Sstevel@tonic-gate 			}
9170Sstevel@tonic-gate 			if (c == 'q')
9180Sstevel@tonic-gate 				goto quit;
9190Sstevel@tonic-gate 			continue;
9200Sstevel@tonic-gate /* X: crypt */
9210Sstevel@tonic-gate 		case 'X':
9220Sstevel@tonic-gate 			crflag = -1; /* determine if file is encrypted */
9230Sstevel@tonic-gate 			goto ent_crypt;
9240Sstevel@tonic-gate 
9250Sstevel@tonic-gate 		case 'C':
9260Sstevel@tonic-gate 			crflag = 1;  /* assume files read in are encrypted */
9270Sstevel@tonic-gate 			goto ent_crypt;
9280Sstevel@tonic-gate 
9290Sstevel@tonic-gate /* xit */
9300Sstevel@tonic-gate 		case 'x':
9310Sstevel@tonic-gate 			tail("xit");
9320Sstevel@tonic-gate 			if (!chng)
9330Sstevel@tonic-gate 				goto quit;
9340Sstevel@tonic-gate 			c = 'q';
9350Sstevel@tonic-gate 			goto wq;
9360Sstevel@tonic-gate 
9370Sstevel@tonic-gate /* yank */
9380Sstevel@tonic-gate 		case 'y':
9390Sstevel@tonic-gate 			tail("yank");
9400Sstevel@tonic-gate 			c = cmdreg();
9410Sstevel@tonic-gate #ifdef XPG4ONLY
9420Sstevel@tonic-gate 			setcount2();
9430Sstevel@tonic-gate #else /* XPG6 and Solaris */
9440Sstevel@tonic-gate 			setcount();
9450Sstevel@tonic-gate #endif /* XPG4ONLY */
9460Sstevel@tonic-gate 			eol();
9470Sstevel@tonic-gate 			vmacchng(0);
9480Sstevel@tonic-gate 			if (c)
949802Scf46844 				(void) YANKreg(c);
9500Sstevel@tonic-gate 			else
951802Scf46844 				(void) yank();
9520Sstevel@tonic-gate 			continue;
9530Sstevel@tonic-gate 
9540Sstevel@tonic-gate /* z */
9550Sstevel@tonic-gate 		case 'z':
9560Sstevel@tonic-gate 			zop(0);
9570Sstevel@tonic-gate 			pflag = 0;
9580Sstevel@tonic-gate 			continue;
9590Sstevel@tonic-gate 
9600Sstevel@tonic-gate /* * */
9610Sstevel@tonic-gate /* @ */
9620Sstevel@tonic-gate 		case '*':
9630Sstevel@tonic-gate 		case '@':
9640Sstevel@tonic-gate 			c = getchar();
9650Sstevel@tonic-gate 			if (c == '\n' || c == '\r')
9660Sstevel@tonic-gate 				ungetchar(c);
9670Sstevel@tonic-gate 			if (any(c, "@*\n\r"))
9680Sstevel@tonic-gate 				c = lastmac;
9690Sstevel@tonic-gate 			if (isupper(c))
9700Sstevel@tonic-gate 				c = tolower(c);
9710Sstevel@tonic-gate 			if (!islower(c))
9720Sstevel@tonic-gate 				error(gettext("Bad register"));
9730Sstevel@tonic-gate 			donewline();
9740Sstevel@tonic-gate 			setdot();
9750Sstevel@tonic-gate 			cmdmac(c);
9760Sstevel@tonic-gate 			continue;
9770Sstevel@tonic-gate 
9780Sstevel@tonic-gate /* | */
9790Sstevel@tonic-gate 		case '|':
9800Sstevel@tonic-gate 			endline = 0;
9810Sstevel@tonic-gate 			goto caseline;
9820Sstevel@tonic-gate 
9830Sstevel@tonic-gate /* \n */
9840Sstevel@tonic-gate 		case '\n':
9850Sstevel@tonic-gate 			endline = 1;
9860Sstevel@tonic-gate caseline:
9870Sstevel@tonic-gate 			notempty();
9880Sstevel@tonic-gate 			if (addr2 == 0) {
9890Sstevel@tonic-gate 				if (cursor_up != NOSTR && c == '\n' &&
9900Sstevel@tonic-gate 				    !inglobal)
9910Sstevel@tonic-gate 					c = CTRL('k');
9920Sstevel@tonic-gate 				if (inglobal)
9930Sstevel@tonic-gate 					addr1 = addr2 = dot;
9940Sstevel@tonic-gate 				else {
9950Sstevel@tonic-gate 					if (dot == dol)
9960Sstevel@tonic-gate 						error((vi_TERSE) ?
9970Sstevel@tonic-gate 						    gettext("At EOF") :
9980Sstevel@tonic-gate 						    gettext("At end-of-file"));
9990Sstevel@tonic-gate 					addr1 = addr2 = dot + 1;
10000Sstevel@tonic-gate 				}
10010Sstevel@tonic-gate 			}
10020Sstevel@tonic-gate 			setdot();
10030Sstevel@tonic-gate 			nonzero();
10040Sstevel@tonic-gate 			if (seensemi)
10050Sstevel@tonic-gate 				addr1 = addr2;
1006*13093SRoger.Faulkner@Oracle.COM 			getaline(*addr1);
10070Sstevel@tonic-gate 			if (c == CTRL('k')) {
10080Sstevel@tonic-gate 				flush1();
10090Sstevel@tonic-gate 				destline--;
10100Sstevel@tonic-gate 				if (hadpr)
10110Sstevel@tonic-gate 					shudclob = 1;
10120Sstevel@tonic-gate 			}
10130Sstevel@tonic-gate 			plines(addr1, addr2, 1);
10140Sstevel@tonic-gate 			continue;
10150Sstevel@tonic-gate 
10160Sstevel@tonic-gate /* " */
10170Sstevel@tonic-gate 		case '"':
10180Sstevel@tonic-gate 			comment();
10190Sstevel@tonic-gate 			continue;
10200Sstevel@tonic-gate 
10210Sstevel@tonic-gate /* # */
10220Sstevel@tonic-gate 		case '#':
10230Sstevel@tonic-gate numberit:
10240Sstevel@tonic-gate 			setCNL();
10250Sstevel@tonic-gate 			(void) setnumb(1);
10260Sstevel@tonic-gate 			pflag = 0;
10270Sstevel@tonic-gate 			goto print;
10280Sstevel@tonic-gate 
10290Sstevel@tonic-gate /* = */
10300Sstevel@tonic-gate 		case '=':
10310Sstevel@tonic-gate 			donewline();
10320Sstevel@tonic-gate 			setall();
10330Sstevel@tonic-gate 			if (inglobal == 2)
10340Sstevel@tonic-gate 				pofix();
1035802Scf46844 			viprintf("%d", lineno(addr2));
10360Sstevel@tonic-gate 			noonl();
10370Sstevel@tonic-gate 			continue;
10380Sstevel@tonic-gate 
10390Sstevel@tonic-gate /* ! */
10400Sstevel@tonic-gate 		case '!':
10410Sstevel@tonic-gate 			if (addr2 != 0) {
10420Sstevel@tonic-gate 				vmacchng(0);
1043500Sceastha 				unix0(0, 1);
10440Sstevel@tonic-gate 				setdot();
1045802Scf46844 				(void) vi_filter(2);
10460Sstevel@tonic-gate 			} else {
1047500Sceastha 				unix0(1, 1);
10480Sstevel@tonic-gate 				pofix();
1049802Scf46844 				putpad((unsigned char *)exit_ca_mode);
10500Sstevel@tonic-gate 				flush();
10510Sstevel@tonic-gate 				resetterm();
10524488Scf46844 				if (!tagflg) {
10534488Scf46844 					unixwt(1, unixex("-c", uxb, 0, 0));
10544488Scf46844 				} else {
10554488Scf46844 					error(gettext("Invalid tags file:"
10564488Scf46844 					    " contains shell escape"));
10574488Scf46844 				}
10580Sstevel@tonic-gate 				vclrech(1);	/* vcontin(0); */
10590Sstevel@tonic-gate 				nochng();
10600Sstevel@tonic-gate 			}
10610Sstevel@tonic-gate 			continue;
10620Sstevel@tonic-gate 
10630Sstevel@tonic-gate /* < */
10640Sstevel@tonic-gate /* > */
10650Sstevel@tonic-gate 		case '<':
10660Sstevel@tonic-gate 		case '>':
10670Sstevel@tonic-gate 			for (cnt = 1; peekchar() == c; cnt++)
10680Sstevel@tonic-gate 				ignchar();
10690Sstevel@tonic-gate 			setCNL();
10700Sstevel@tonic-gate 			vmacchng(0);
10710Sstevel@tonic-gate 			shift(c, cnt);
10720Sstevel@tonic-gate 			continue;
10730Sstevel@tonic-gate 
10740Sstevel@tonic-gate /* ^D */
10750Sstevel@tonic-gate /* EOF */
10760Sstevel@tonic-gate 		case CTRL('d'):
10770Sstevel@tonic-gate 		case EOF:
10780Sstevel@tonic-gate 			if (exitoneof) {
10790Sstevel@tonic-gate 				if (addr2 != 0)
10800Sstevel@tonic-gate 					dot = addr2;
10810Sstevel@tonic-gate 				return;
10820Sstevel@tonic-gate 			}
10830Sstevel@tonic-gate 			if (!isatty(0)) {
10840Sstevel@tonic-gate 				if (intty)
10850Sstevel@tonic-gate 					/*
10860Sstevel@tonic-gate 					 * Chtty sys call at UCB may cause a
10870Sstevel@tonic-gate 					 * input which was a tty to suddenly be
10880Sstevel@tonic-gate 					 * turned into /dev/null.
10890Sstevel@tonic-gate 					 */
10900Sstevel@tonic-gate 					onhup(0);
10910Sstevel@tonic-gate 				return;
10920Sstevel@tonic-gate 			}
10930Sstevel@tonic-gate 			if (addr2 != 0) {
10940Sstevel@tonic-gate 				setlastchar('\n');
10950Sstevel@tonic-gate 				putnl();
10960Sstevel@tonic-gate 			}
10970Sstevel@tonic-gate 			if (dol == zero) {
10980Sstevel@tonic-gate 				if (addr2 == 0)
10990Sstevel@tonic-gate 					putnl();
11000Sstevel@tonic-gate 				notempty();
11010Sstevel@tonic-gate 			}
11020Sstevel@tonic-gate 			ungetchar(EOF);
11030Sstevel@tonic-gate 			zop(hadpr);
11040Sstevel@tonic-gate 			continue;
11050Sstevel@tonic-gate 		default:
11060Sstevel@tonic-gate 			if (!isalpha(c) || !isascii(c))
11070Sstevel@tonic-gate 				break;
11080Sstevel@tonic-gate 			ungetchar(c);
1109802Scf46844 			tailprim((unsigned char *)"", 0, 0);
11100Sstevel@tonic-gate 		}
11110Sstevel@tonic-gate 		ungetchar(c);
11120Sstevel@tonic-gate 		{
11130Sstevel@tonic-gate 			int length;
11140Sstevel@tonic-gate 			char multic[MULTI_BYTE_MAX];
11150Sstevel@tonic-gate 			wchar_t wchar;
11160Sstevel@tonic-gate 			length = _mbftowc(multic, &wchar, getchar, &peekc);
11170Sstevel@tonic-gate 			if (length < 0)
11180Sstevel@tonic-gate 				length = -length;
11190Sstevel@tonic-gate 			multic[length] = '\0';
11200Sstevel@tonic-gate 			error((vi_TERSE) ? gettext("What?") :
11210Sstevel@tonic-gate 				gettext("Unknown command character '%s'"),
11220Sstevel@tonic-gate 			    multic);
11230Sstevel@tonic-gate 		}
11240Sstevel@tonic-gate 	}
11250Sstevel@tonic-gate }
1126