xref: /freebsd/contrib/nvi/ex/ex_global.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
1 /*-
2  * Copyright (c) 1992, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1992, 1993, 1994, 1995, 1996
5  *	Keith Bostic.  All rights reserved.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9 
10 #include "config.h"
11 
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15 
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24 
25 #include "../common/common.h"
26 
27 enum which {GLOBAL, V};
28 
29 static int ex_g_setup(SCR *, EXCMD *, enum which);
30 
31 /*
32  * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
33  *	Exec on lines matching a pattern.
34  *
35  * PUBLIC: int ex_global(SCR *, EXCMD *);
36  */
37 int
38 ex_global(SCR *sp, EXCMD *cmdp)
39 {
40 	return (ex_g_setup(sp,
41 	    cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL));
42 }
43 
44 /*
45  * ex_v -- [line [,line]] v /pattern/ [commands]
46  *	Exec on lines not matching a pattern.
47  *
48  * PUBLIC: int ex_v(SCR *, EXCMD *);
49  */
50 int
51 ex_v(SCR *sp, EXCMD *cmdp)
52 {
53 	return (ex_g_setup(sp, cmdp, V));
54 }
55 
56 /*
57  * ex_g_setup --
58  *	Ex global and v commands.
59  */
60 static int
61 ex_g_setup(SCR *sp, EXCMD *cmdp, enum which cmd)
62 {
63 	CHAR_T *ptrn, *p, *t;
64 	EXCMD *ecp;
65 	MARK abs;
66 	RANGE *rp;
67 	busy_t btype;
68 	recno_t start, end;
69 	regex_t *re;
70 	regmatch_t match[1];
71 	size_t len;
72 	int cnt, delim, eval;
73 	CHAR_T *dbp;
74 
75 	NEEDFILE(sp, cmdp);
76 
77 	if (F_ISSET(sp, SC_EX_GLOBAL)) {
78 		msgq_wstr(sp, M_ERR, cmdp->cmd->name,
79 	"124|The %s command can't be used as part of a global or v command");
80 		return (1);
81 	}
82 
83 	/*
84 	 * Skip leading white space.  Historic vi allowed any non-alphanumeric
85 	 * to serve as the global command delimiter.
86 	 */
87 	if (cmdp->argc == 0)
88 		goto usage;
89 	for (p = cmdp->argv[0]->bp; cmdskip(*p); ++p);
90 	if (*p == '\0' || is09azAZ(*p) ||
91 	    *p == '\\' || *p == '|' || *p == '\n') {
92 usage:		ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
93 		return (1);
94 	}
95 	delim = *p++;
96 
97 	/*
98 	 * Get the pattern string, toss escaped characters.
99 	 *
100 	 * QUOTING NOTE:
101 	 * Only toss an escaped character if it escapes a delimiter.
102 	 */
103 	for (ptrn = t = p;;) {
104 		if (p[0] == '\0' || p[0] == delim) {
105 			if (p[0] == delim)
106 				++p;
107 			/*
108 			 * !!!
109 			 * Nul terminate the pattern string -- it's passed
110 			 * to regcomp which doesn't understand anything else.
111 			 */
112 			*t = '\0';
113 			break;
114 		}
115 		if (p[0] == '\\') {
116 			if (p[1] == delim)
117 				++p;
118 			else if (p[1] == '\\')
119 				*t++ = *p++;
120 		}
121 		*t++ = *p++;
122 	}
123 
124 	/* If the pattern string is empty, use the last one. */
125 	if (*ptrn == '\0') {
126 		if (sp->re == NULL) {
127 			ex_emsg(sp, NULL, EXM_NOPREVRE);
128 			return (1);
129 		}
130 
131 		/* Re-compile the RE if necessary. */
132 		if (!F_ISSET(sp, SC_RE_SEARCH) &&
133 		    re_compile(sp, sp->re, sp->re_len,
134 		    NULL, NULL, &sp->re_c, RE_C_SEARCH))
135 			return (1);
136 	} else {
137 		/* Compile the RE. */
138 		if (re_compile(sp, ptrn, t - ptrn, &sp->re,
139 		    &sp->re_len, &sp->re_c, RE_C_SEARCH))
140 			return (1);
141 
142 		/*
143 		 * Set saved RE.  Historic practice is that globals set
144 		 * direction as well as the RE.
145 		 */
146 		sp->searchdir = FORWARD;
147 	}
148 	re = &sp->re_c;
149 
150 	/* The global commands always set the previous context mark. */
151 	abs.lno = sp->lno;
152 	abs.cno = sp->cno;
153 	if (mark_set(sp, ABSMARK1, &abs, 1))
154 		return (1);
155 
156 	/* Get an EXCMD structure. */
157 	CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
158 	TAILQ_INIT(ecp->rq);
159 
160 	/*
161 	 * Get a copy of the command string; the default command is print.
162 	 * Don't worry about a set of <blank>s with no command, that will
163 	 * default to print in the ex parser.  We need to have two copies
164 	 * because the ex parser may step on the command string when it's
165 	 * parsing it.
166 	 */
167 	if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
168 		p = L("p");
169 		len = 1;
170 	}
171 
172 	MALLOC_RET(sp, ecp->cp, (len * 2) * sizeof(CHAR_T));
173 	ecp->o_cp = ecp->cp;
174 	ecp->o_clen = len;
175 	MEMCPY(ecp->cp + len, p, len);
176 	ecp->range_lno = OOBLNO;
177 	FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V);
178 	SLIST_INSERT_HEAD(sp->gp->ecq, ecp, q);
179 
180 	/*
181 	 * For each line...  The semantics of global matching are that we first
182 	 * have to decide which lines are going to get passed to the command,
183 	 * and then pass them to the command, ignoring other changes.  There's
184 	 * really no way to do this in a single pass, since arbitrary line
185 	 * creation, deletion and movement can be done in the ex command.  For
186 	 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
187 	 * What we do is create linked list of lines that are tracked through
188 	 * each ex command.  There's a callback routine which the DB interface
189 	 * routines call when a line is created or deleted.  This doesn't help
190 	 * the layering much.
191 	 */
192 	btype = BUSY_ON;
193 	cnt = INTERRUPT_CHECK;
194 	for (start = cmdp->addr1.lno,
195 	    end = cmdp->addr2.lno; start <= end; ++start) {
196 		if (cnt-- == 0) {
197 			if (INTERRUPTED(sp)) {
198 				SLIST_REMOVE_HEAD(sp->gp->ecq, q);
199 				free(ecp->cp);
200 				free(ecp);
201 				break;
202 			}
203 			search_busy(sp, btype);
204 			btype = BUSY_UPDATE;
205 			cnt = INTERRUPT_CHECK;
206 		}
207 		if (db_get(sp, start, DBG_FATAL, &dbp, &len))
208 			return (1);
209 		match[0].rm_so = 0;
210 		match[0].rm_eo = len;
211 		switch (eval =
212 		    regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) {
213 		case 0:
214 			if (cmd == V)
215 				continue;
216 			break;
217 		case REG_NOMATCH:
218 			if (cmd == GLOBAL)
219 				continue;
220 			break;
221 		default:
222 			re_error(sp, eval, &sp->re_c);
223 			break;
224 		}
225 
226 		/* If follows the last entry, extend the last entry's range. */
227 		if ((rp = TAILQ_LAST(ecp->rq, _rh)) != NULL &&
228 		    rp->stop == start - 1) {
229 			++rp->stop;
230 			continue;
231 		}
232 
233 		/* Allocate a new range, and append it to the list. */
234 		CALLOC(sp, rp, 1, sizeof(RANGE));
235 		if (rp == NULL)
236 			return (1);
237 		rp->start = rp->stop = start;
238 		TAILQ_INSERT_TAIL(ecp->rq, rp, q);
239 	}
240 	search_busy(sp, BUSY_OFF);
241 	return (0);
242 }
243 
244 /*
245  * ex_g_insdel --
246  *	Update the ranges based on an insertion or deletion.
247  *
248  * PUBLIC: int ex_g_insdel(SCR *, lnop_t, recno_t);
249  */
250 int
251 ex_g_insdel(SCR *sp, lnop_t op, recno_t lno)
252 {
253 	EXCMD *ecp;
254 	RANGE *nrp, *rp;
255 
256 	/* All insert/append operations are done as inserts. */
257 	if (op == LINE_APPEND)
258 		abort();
259 
260 	if (op == LINE_RESET)
261 		return (0);
262 
263 	SLIST_FOREACH(ecp, sp->gp->ecq, q) {
264 		if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
265 			continue;
266 		TAILQ_FOREACH_SAFE(rp, ecp->rq, q, nrp) {
267 			/* If range less than the line, ignore it. */
268 			if (rp->stop < lno)
269 				continue;
270 
271 			/*
272 			 * If range greater than the line, decrement or
273 			 * increment the range.
274 			 */
275 			if (rp->start > lno) {
276 				if (op == LINE_DELETE) {
277 					--rp->start;
278 					--rp->stop;
279 				} else {
280 					++rp->start;
281 					++rp->stop;
282 				}
283 				continue;
284 			}
285 
286 			/*
287 			 * Lno is inside the range, decrement the end point
288 			 * for deletion, and split the range for insertion.
289 			 * In the latter case, since we're inserting a new
290 			 * element, neither range can be exhausted.
291 			 */
292 			if (op == LINE_DELETE) {
293 				if (rp->start > --rp->stop) {
294 					TAILQ_REMOVE(ecp->rq, rp, q);
295 					free(rp);
296 				}
297 			} else {
298 				CALLOC_RET(sp, nrp, 1, sizeof(RANGE));
299 				nrp->start = lno + 1;
300 				nrp->stop = rp->stop + 1;
301 				rp->stop = lno - 1;
302 				TAILQ_INSERT_AFTER(ecp->rq, rp, nrp, q);
303 			}
304 		}
305 
306 		/*
307 		 * If the command deleted/inserted lines, the cursor moves to
308 		 * the line after the deleted/inserted line.
309 		 */
310 		ecp->range_lno = lno;
311 	}
312 	return (0);
313 }
314