xref: /onnv-gate/usr/src/cmd/vi/port/ex_addr.c (revision 802:73b56fb6544b)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
70Sstevel@tonic-gate  * with the License.
80Sstevel@tonic-gate  *
90Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate  * See the License for the specific language governing permissions
120Sstevel@tonic-gate  * and limitations under the License.
130Sstevel@tonic-gate  *
140Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate  *
200Sstevel@tonic-gate  * CDDL HEADER END
210Sstevel@tonic-gate  */
22*802Scf46844 /*
23*802Scf46844  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*802Scf46844  * Use is subject to license terms.
25*802Scf46844  */
26*802Scf46844 
270Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
300Sstevel@tonic-gate 
310Sstevel@tonic-gate /* Copyright (c) 1981 Regents of the University of California */
320Sstevel@tonic-gate 
33*802Scf46844 #pragma ident	"%Z%%M%	%I%	%E% SMI"
340Sstevel@tonic-gate #include "ex.h"
350Sstevel@tonic-gate #include "ex_re.h"
360Sstevel@tonic-gate 
370Sstevel@tonic-gate /*
380Sstevel@tonic-gate  * Routines for address parsing and assignment and checking of address bounds
390Sstevel@tonic-gate  * in command mode.  The routine address is called from ex_cmds.c
400Sstevel@tonic-gate  * to parse each component of a command (terminated by , ; or the beginning
410Sstevel@tonic-gate  * of the command itself.  It is also called by the scanning routine
420Sstevel@tonic-gate  * in ex_voperate.c from within open/visual.
430Sstevel@tonic-gate  *
440Sstevel@tonic-gate  * Other routines here manipulate the externals addr1 and addr2.
450Sstevel@tonic-gate  * These are the first and last lines for the current command.
460Sstevel@tonic-gate  *
470Sstevel@tonic-gate  * The variable bigmove remembers whether a non-local glitch of . was
480Sstevel@tonic-gate  * involved in an address expression, so we can set the previous context
490Sstevel@tonic-gate  * mark '' when such a motion occurs.
500Sstevel@tonic-gate  */
510Sstevel@tonic-gate 
520Sstevel@tonic-gate static	bool bigmove;
530Sstevel@tonic-gate 
540Sstevel@tonic-gate /*
550Sstevel@tonic-gate  * Set up addr1 and addr2 for commands whose default address is dot.
560Sstevel@tonic-gate  */
57*802Scf46844 void
setdot(void)58*802Scf46844 setdot(void)
590Sstevel@tonic-gate {
600Sstevel@tonic-gate 
610Sstevel@tonic-gate 	setdot1();
620Sstevel@tonic-gate 	if (bigmove)
630Sstevel@tonic-gate 		markDOT();
640Sstevel@tonic-gate }
650Sstevel@tonic-gate 
660Sstevel@tonic-gate /*
670Sstevel@tonic-gate  * Call setdot1 to set up default addresses without ever
680Sstevel@tonic-gate  * setting the previous context mark.
690Sstevel@tonic-gate  */
70*802Scf46844 void
setdot1(void)71*802Scf46844 setdot1(void)
720Sstevel@tonic-gate {
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (addr2 == 0)
750Sstevel@tonic-gate 		addr1 = addr2 = dot;
760Sstevel@tonic-gate 	if (addr1 > addr2) {
770Sstevel@tonic-gate 		notempty();
780Sstevel@tonic-gate 		error(value(vi_TERSE) ?
790Sstevel@tonic-gate 			gettext("Addr1 > addr2") :
800Sstevel@tonic-gate 			gettext("First address exceeds second"));
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate }
830Sstevel@tonic-gate 
840Sstevel@tonic-gate /*
850Sstevel@tonic-gate  * Ex allows you to say
860Sstevel@tonic-gate  *	delete 5
870Sstevel@tonic-gate  * to delete 5 lines, etc.
880Sstevel@tonic-gate  * Such nonsense is implemented by setcount.
890Sstevel@tonic-gate  */
90*802Scf46844 void
setcount(void)91*802Scf46844 setcount(void)
920Sstevel@tonic-gate {
93*802Scf46844 	int cnt;
940Sstevel@tonic-gate 
950Sstevel@tonic-gate 	pastwh();
960Sstevel@tonic-gate 	if (!isdigit(peekchar())) {
970Sstevel@tonic-gate 		setdot();
980Sstevel@tonic-gate 		return;
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 	addr1 = addr2;
1010Sstevel@tonic-gate 	setdot();
1020Sstevel@tonic-gate 	cnt = getnum();
1030Sstevel@tonic-gate 	if (cnt <= 0)
1040Sstevel@tonic-gate 		error(value(vi_TERSE) ?
1050Sstevel@tonic-gate 			gettext("Bad count") :
1060Sstevel@tonic-gate 			gettext("Nonzero count required"));
1070Sstevel@tonic-gate 	addr2 += cnt - 1;
1080Sstevel@tonic-gate 	if (addr2 > dol)
1090Sstevel@tonic-gate 		addr2 = dol;
1100Sstevel@tonic-gate 	nonzero();
1110Sstevel@tonic-gate }
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate #ifdef XPG4
1140Sstevel@tonic-gate /*
1150Sstevel@tonic-gate  * setcount2():	a version of setcount() which sets addr2 based on addr1 + cnt.
1160Sstevel@tonic-gate  * description:
1170Sstevel@tonic-gate  *	this routine is responsible for setting addr1 (possibly) and addr2
1180Sstevel@tonic-gate  *	(always); using the [count] to compute addr2.
1190Sstevel@tonic-gate  *
1200Sstevel@tonic-gate  *	this is similar setcount(), but it differs in that setcount() sets
1210Sstevel@tonic-gate  *	addr1 based upon addr2; here we set addr2 based upon addr1 and the
1220Sstevel@tonic-gate  *	[count].
1230Sstevel@tonic-gate  *
1240Sstevel@tonic-gate  *	the reason for this is because some commands, of the form:
1250Sstevel@tonic-gate  *		[range] command [count]
1260Sstevel@tonic-gate  *	will use [count] to modify the range. E.g.:
1270Sstevel@tonic-gate  *		change, delete, join, list, yank.
1280Sstevel@tonic-gate  */
129*802Scf46844 void
setcount2(void)130*802Scf46844 setcount2(void)
1310Sstevel@tonic-gate {
132*802Scf46844 	int cnt;
1330Sstevel@tonic-gate 
1340Sstevel@tonic-gate 	pastwh();
1350Sstevel@tonic-gate 	if (!isdigit(peekchar())) {
1360Sstevel@tonic-gate 		setdot();
1370Sstevel@tonic-gate 		return;
1380Sstevel@tonic-gate 	}
1390Sstevel@tonic-gate 	setdot();
1400Sstevel@tonic-gate 	cnt = getnum();
1410Sstevel@tonic-gate 	if (cnt <= 0)
1420Sstevel@tonic-gate 		error(value(vi_TERSE) ?
1430Sstevel@tonic-gate 			gettext("Bad count") :
1440Sstevel@tonic-gate 			gettext("Nonzero count required"));
1450Sstevel@tonic-gate 	addr2 = addr1 + (cnt - 1);
1460Sstevel@tonic-gate 	if (addr2 > dol)
1470Sstevel@tonic-gate 		addr2 = dol;
1480Sstevel@tonic-gate 	if (addr2 < zero) {
1490Sstevel@tonic-gate 		addr1 = addr2 = zero;
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 	nonzero();
1520Sstevel@tonic-gate }
1530Sstevel@tonic-gate 
1540Sstevel@tonic-gate #endif /* XPG4 */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
1570Sstevel@tonic-gate  * Parse a number out of the command input stream.
1580Sstevel@tonic-gate  */
159*802Scf46844 int
getnum(void)160*802Scf46844 getnum(void)
1610Sstevel@tonic-gate {
162*802Scf46844 	int cnt;
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate 	/*CSTYLED*/
1650Sstevel@tonic-gate 	for (cnt = 0; isdigit(peekcd());)
1660Sstevel@tonic-gate 		cnt = cnt * 10 + getchar() - '0';
1670Sstevel@tonic-gate 	return (cnt);
1680Sstevel@tonic-gate }
1690Sstevel@tonic-gate 
1700Sstevel@tonic-gate /*
1710Sstevel@tonic-gate  * Set the default addresses for commands which use the whole
1720Sstevel@tonic-gate  * buffer as default, notably write.
1730Sstevel@tonic-gate  */
174*802Scf46844 void
setall(void)175*802Scf46844 setall(void)
1760Sstevel@tonic-gate {
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 	if (addr2 == 0) {
1790Sstevel@tonic-gate 		addr1 = one;
1800Sstevel@tonic-gate 		addr2 = dol;
1810Sstevel@tonic-gate 		if (dol == zero) {
1820Sstevel@tonic-gate 			dot = zero;
1830Sstevel@tonic-gate 			return;
1840Sstevel@tonic-gate 		}
1850Sstevel@tonic-gate 	}
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * Don't want to set previous context mark so use setdot1().
1880Sstevel@tonic-gate 	 */
1890Sstevel@tonic-gate 	setdot1();
1900Sstevel@tonic-gate }
1910Sstevel@tonic-gate 
1920Sstevel@tonic-gate /*
1930Sstevel@tonic-gate  * No address allowed on, e.g. the file command.
1940Sstevel@tonic-gate  */
195*802Scf46844 void
setnoaddr(void)196*802Scf46844 setnoaddr(void)
1970Sstevel@tonic-gate {
1980Sstevel@tonic-gate 
1990Sstevel@tonic-gate 	if (addr2 != 0)
2000Sstevel@tonic-gate 		error(value(vi_TERSE) ?
2010Sstevel@tonic-gate 			gettext("No address allowed") :
2020Sstevel@tonic-gate 			gettext("No address allowed on this command"));
2030Sstevel@tonic-gate }
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate /*
2060Sstevel@tonic-gate  * Parse an address.
2070Sstevel@tonic-gate  * Just about any sequence of address characters is legal.
2080Sstevel@tonic-gate  *
2090Sstevel@tonic-gate  * If you are tricky you can use this routine and the = command
2100Sstevel@tonic-gate  * to do simple addition and subtraction of cardinals less
2110Sstevel@tonic-gate  * than the number of lines in the file.
2120Sstevel@tonic-gate  */
2130Sstevel@tonic-gate line *
address(inputline)2140Sstevel@tonic-gate address(inputline)
2150Sstevel@tonic-gate 	unsigned char *inputline;
2160Sstevel@tonic-gate {
217*802Scf46844 	line *addr;
218*802Scf46844 	int offset, c;
2190Sstevel@tonic-gate 	short lastsign;
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	bigmove = 0;
2220Sstevel@tonic-gate 	lastsign = 0;
2230Sstevel@tonic-gate 	offset = 0;
2240Sstevel@tonic-gate 	addr = 0;
2250Sstevel@tonic-gate 	for (;;) {
2260Sstevel@tonic-gate 		if (isdigit(peekcd())) {
2270Sstevel@tonic-gate 			if (addr == 0) {
2280Sstevel@tonic-gate 				addr = zero;
2290Sstevel@tonic-gate 				bigmove = 1;
2300Sstevel@tonic-gate 			}
2310Sstevel@tonic-gate 			loc1 = 0;
2320Sstevel@tonic-gate 			addr += offset;
2330Sstevel@tonic-gate 			offset = getnum();
2340Sstevel@tonic-gate 			if (lastsign >= 0)
2350Sstevel@tonic-gate 				addr += offset;
2360Sstevel@tonic-gate 			else
2370Sstevel@tonic-gate 				addr -= offset;
2380Sstevel@tonic-gate 			lastsign = 0;
2390Sstevel@tonic-gate 			offset = 0;
2400Sstevel@tonic-gate 		}
2410Sstevel@tonic-gate 		switch (c = getcd()) {
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 		case '?':
2440Sstevel@tonic-gate 		case '/':
2450Sstevel@tonic-gate 		case '$':
2460Sstevel@tonic-gate 		case '\'':
2470Sstevel@tonic-gate 		case '\\':
2480Sstevel@tonic-gate 			bigmove++;
2490Sstevel@tonic-gate 		case '.':
2500Sstevel@tonic-gate 			if (addr || offset)
2510Sstevel@tonic-gate 				error(gettext("Badly formed address"));
2520Sstevel@tonic-gate 		}
2530Sstevel@tonic-gate 		offset += lastsign;
2540Sstevel@tonic-gate 		lastsign = 0;
2550Sstevel@tonic-gate 		switch (c) {
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 		case ' ':
2580Sstevel@tonic-gate 		case '\t':
2590Sstevel@tonic-gate 			continue;
2600Sstevel@tonic-gate 		case ':':
2610Sstevel@tonic-gate 			while (peekchar() == ':')
2620Sstevel@tonic-gate 				ignchar();
2630Sstevel@tonic-gate 			continue;
2640Sstevel@tonic-gate 		case '+':
2650Sstevel@tonic-gate 			lastsign = 1;
2660Sstevel@tonic-gate 			if (addr == 0)
2670Sstevel@tonic-gate 				addr = dot;
2680Sstevel@tonic-gate 			continue;
2690Sstevel@tonic-gate 
2700Sstevel@tonic-gate 		case '^':
2710Sstevel@tonic-gate 		case '-':
2720Sstevel@tonic-gate 			lastsign = -1;
2730Sstevel@tonic-gate 			if (addr == 0)
2740Sstevel@tonic-gate 				addr = dot;
2750Sstevel@tonic-gate 			continue;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 		case '\\':
2780Sstevel@tonic-gate 		case '?':
2790Sstevel@tonic-gate 		case '/':
2800Sstevel@tonic-gate 			c = vi_compile(c, 1);
2810Sstevel@tonic-gate 			notempty();
2820Sstevel@tonic-gate 			savere(&scanre);
2830Sstevel@tonic-gate 			addr = dot;
2840Sstevel@tonic-gate 			if (inputline && execute(0, dot)) {
2850Sstevel@tonic-gate 				if (c == '/') {
2860Sstevel@tonic-gate 					while (loc1 <= (char *)inputline) {
2870Sstevel@tonic-gate 						if (loc1 == loc2)
2880Sstevel@tonic-gate 							loc2++;
2890Sstevel@tonic-gate 						if (!execute(1))
2900Sstevel@tonic-gate 							goto nope;
2910Sstevel@tonic-gate 					}
2920Sstevel@tonic-gate 					break;
2930Sstevel@tonic-gate 				} else if (loc1 < (char *)inputline) {
2940Sstevel@tonic-gate 					unsigned char *last;
2950Sstevel@tonic-gate doques:
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 					do {
2980Sstevel@tonic-gate 						last = (unsigned char *)loc1;
2990Sstevel@tonic-gate 						if (loc1 == loc2)
3000Sstevel@tonic-gate 							loc2++;
3010Sstevel@tonic-gate 						if (!execute(1))
3020Sstevel@tonic-gate 							break;
3030Sstevel@tonic-gate 					} while (loc1 < (char *)inputline);
3040Sstevel@tonic-gate 					loc1 = (char *)last;
3050Sstevel@tonic-gate 					break;
3060Sstevel@tonic-gate 				}
3070Sstevel@tonic-gate 			}
3080Sstevel@tonic-gate nope:
3090Sstevel@tonic-gate 			for (;;) {
3100Sstevel@tonic-gate 				if (c == '/') {
3110Sstevel@tonic-gate 					addr++;
3120Sstevel@tonic-gate 					if (addr > dol) {
3130Sstevel@tonic-gate 						if (value(vi_WRAPSCAN) == 0)
3140Sstevel@tonic-gate error(value(vi_TERSE) ?
3150Sstevel@tonic-gate 	gettext("No match to BOTTOM") :
3160Sstevel@tonic-gate 	gettext("Address search hit BOTTOM without matching pattern"));
3170Sstevel@tonic-gate 						addr = zero;
3180Sstevel@tonic-gate 					}
3190Sstevel@tonic-gate 				} else {
3200Sstevel@tonic-gate 					addr--;
3210Sstevel@tonic-gate 					if (addr < zero) {
3220Sstevel@tonic-gate 						if (value(vi_WRAPSCAN) == 0)
3230Sstevel@tonic-gate error(value(vi_TERSE) ?
3240Sstevel@tonic-gate 	gettext("No match to TOP") :
3250Sstevel@tonic-gate 	gettext("Address search hit TOP without matching pattern"));
3260Sstevel@tonic-gate 						addr = dol;
3270Sstevel@tonic-gate 					}
3280Sstevel@tonic-gate 				}
3290Sstevel@tonic-gate 				if (execute(0, addr)) {
3300Sstevel@tonic-gate 					if (inputline && c == '?') {
3310Sstevel@tonic-gate 						inputline = &linebuf[LBSIZE];
3320Sstevel@tonic-gate 						goto doques;
3330Sstevel@tonic-gate 					}
3340Sstevel@tonic-gate 					break;
3350Sstevel@tonic-gate 				}
3360Sstevel@tonic-gate 				if (addr == dot)
3370Sstevel@tonic-gate 					error(value(vi_TERSE) ?
3380Sstevel@tonic-gate 						gettext("Fail") :
3390Sstevel@tonic-gate 						gettext("Pattern not found"));
3400Sstevel@tonic-gate 			}
3410Sstevel@tonic-gate 			continue;
3420Sstevel@tonic-gate 
3430Sstevel@tonic-gate 		case '$':
3440Sstevel@tonic-gate 			addr = dol;
3450Sstevel@tonic-gate 			continue;
3460Sstevel@tonic-gate 
3470Sstevel@tonic-gate 		case '.':
3480Sstevel@tonic-gate 			addr = dot;
3490Sstevel@tonic-gate 			continue;
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		case '\'':
3520Sstevel@tonic-gate 			c = markreg(getchar());
3530Sstevel@tonic-gate 			if (c == 0)
3540Sstevel@tonic-gate 				error(gettext("Marks are ' and a-z"));
3550Sstevel@tonic-gate 			addr = getmark(c);
3560Sstevel@tonic-gate 			if (addr == 0)
3570Sstevel@tonic-gate 				error(value(vi_TERSE) ?
3580Sstevel@tonic-gate 				    gettext("Undefined mark") :
3590Sstevel@tonic-gate 				    gettext("Undefined mark referenced"));
3600Sstevel@tonic-gate 			break;
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		default:
3630Sstevel@tonic-gate 			ungetchar(c);
3640Sstevel@tonic-gate 			if (offset) {
3650Sstevel@tonic-gate 				if (addr == 0)
3660Sstevel@tonic-gate 					addr = dot;
3670Sstevel@tonic-gate 				addr += offset;
3680Sstevel@tonic-gate 				loc1 = 0;
3690Sstevel@tonic-gate 			}
3700Sstevel@tonic-gate 			if (addr == 0) {
3710Sstevel@tonic-gate 				bigmove = 0;
3720Sstevel@tonic-gate 				return (0);
3730Sstevel@tonic-gate 			}
3740Sstevel@tonic-gate 			if (addr != zero)
3750Sstevel@tonic-gate 				notempty();
3760Sstevel@tonic-gate 			addr += lastsign;
3770Sstevel@tonic-gate 			if (addr < zero)
3780Sstevel@tonic-gate 				error(value(vi_TERSE) ?
3790Sstevel@tonic-gate 				    gettext("Negative address") :
3800Sstevel@tonic-gate 				    gettext("Negative address - "
3810Sstevel@tonic-gate 					"first buffer line is 1"));
3820Sstevel@tonic-gate 			if (addr > dol)
3830Sstevel@tonic-gate 				error(value(vi_TERSE) ?
3840Sstevel@tonic-gate 				    gettext("Not that many lines") :
3850Sstevel@tonic-gate 				    gettext("Not that many lines in buffer"));
3860Sstevel@tonic-gate 			return (addr);
3870Sstevel@tonic-gate 		}
3880Sstevel@tonic-gate 	}
3890Sstevel@tonic-gate }
3900Sstevel@tonic-gate 
3910Sstevel@tonic-gate /*
3920Sstevel@tonic-gate  * Abbreviations to make code smaller
3930Sstevel@tonic-gate  * Left over from squashing ex version 1.1 into
3940Sstevel@tonic-gate  * 11/34's and 11/40's.
3950Sstevel@tonic-gate  */
396*802Scf46844 void
setCNL(void)397*802Scf46844 setCNL(void)
3980Sstevel@tonic-gate {
3990Sstevel@tonic-gate 
4000Sstevel@tonic-gate 	setcount();
4010Sstevel@tonic-gate 	donewline();
4020Sstevel@tonic-gate }
4030Sstevel@tonic-gate 
404*802Scf46844 void
setNAEOL(void)405*802Scf46844 setNAEOL(void)
4060Sstevel@tonic-gate {
4070Sstevel@tonic-gate 
4080Sstevel@tonic-gate 	setnoaddr();
4090Sstevel@tonic-gate 	eol();
4100Sstevel@tonic-gate }
411