add VI_SEL_END, VI_SEL_START
[nvi.git] / ex / ex_global.c
blobf0251acbb0412ec8281474befbacd3e17903039a
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.
7 * See the LICENSE file for redistribution information.
8 */
10 #include "config.h"
12 #ifndef lint
13 static const char sccsid[] = "$Id: ex_global.c,v 10.23 1996/12/11 13:04:37 bostic Exp $ (Berkeley) $Date: 1996/12/11 13:04:37 $";
14 #endif /* not lint */
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.h>
20 #include <ctype.h>
21 #include <errno.h>
22 #include <limits.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
28 #include "../common/common.h"
30 enum which {GLOBAL, V};
32 static int ex_g_setup __P((SCR *, EXCMD *, enum which));
35 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
36 * Exec on lines matching a pattern.
38 * PUBLIC: int ex_global __P((SCR *, EXCMD *));
40 int
41 ex_global(sp, cmdp)
42 SCR *sp;
43 EXCMD *cmdp;
45 return (ex_g_setup(sp,
46 cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL));
50 * ex_v -- [line [,line]] v /pattern/ [commands]
51 * Exec on lines not matching a pattern.
53 * PUBLIC: int ex_v __P((SCR *, EXCMD *));
55 int
56 ex_v(sp, cmdp)
57 SCR *sp;
58 EXCMD *cmdp;
60 return (ex_g_setup(sp, cmdp, V));
64 * ex_g_setup --
65 * Ex global and v commands.
67 static int
68 ex_g_setup(sp, cmdp, cmd)
69 SCR *sp;
70 EXCMD *cmdp;
71 enum which cmd;
73 CHAR_T *ptrn, *p, *t;
74 EXCMD *ecp;
75 MARK abs;
76 RANGE *rp;
77 busy_t btype;
78 recno_t start, end;
79 regex_t *re;
80 regmatch_t match[1];
81 size_t len;
82 int cnt, delim, eval;
83 char *dbp;
85 NEEDFILE(sp, cmdp);
87 if (F_ISSET(sp, SC_EX_GLOBAL)) {
88 msgq(sp, M_ERR,
89 "124|The %s command can't be used as part of a global or v command",
90 cmdp->cmd->name);
91 return (1);
95 * Skip leading white space. Historic vi allowed any non-alphanumeric
96 * to serve as the global command delimiter.
98 if (cmdp->argc == 0)
99 goto usage;
100 for (p = cmdp->argv[0]->bp; isblank(*p); ++p);
101 if (*p == '\0' || isalnum(*p) ||
102 *p == '\\' || *p == '|' || *p == '\n') {
103 usage: ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
104 return (1);
106 delim = *p++;
109 * Get the pattern string, toss escaped characters.
111 * QUOTING NOTE:
112 * Only toss an escaped character if it escapes a delimiter.
114 for (ptrn = t = p;;) {
115 if (p[0] == '\0' || p[0] == delim) {
116 if (p[0] == delim)
117 ++p;
119 * !!!
120 * Nul terminate the pattern string -- it's passed
121 * to regcomp which doesn't understand anything else.
123 *t = '\0';
124 break;
126 if (p[0] == '\\')
127 if (p[1] == delim)
128 ++p;
129 else if (p[1] == '\\')
130 *t++ = *p++;
131 *t++ = *p++;
134 /* If the pattern string is empty, use the last one. */
135 if (*ptrn == '\0') {
136 if (sp->re == NULL) {
137 ex_emsg(sp, NULL, EXM_NOPREVRE);
138 return (1);
141 /* Re-compile the RE if necessary. */
142 if (!F_ISSET(sp, SC_RE_SEARCH) &&
143 re_compile(sp, sp->re, sp->re_len,
144 NULL, NULL, &sp->re_c, SEARCH_CSEARCH | SEARCH_MSG))
145 return (1);
146 } else {
147 /* Compile the RE. */
148 if (re_compile(sp, ptrn, t - ptrn, &sp->re,
149 &sp->re_len, &sp->re_c, SEARCH_CSEARCH | SEARCH_MSG))
150 return (1);
153 * Set saved RE. Historic practice is that globals set
154 * direction as well as the RE.
156 sp->searchdir = FORWARD;
158 re = &sp->re_c;
160 /* The global commands always set the previous context mark. */
161 abs.lno = sp->lno;
162 abs.cno = sp->cno;
163 if (mark_set(sp, ABSMARK1, &abs, 1))
164 return (1);
166 /* Get an EXCMD structure. */
167 CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
168 CIRCLEQ_INIT(&ecp->rq);
171 * Get a copy of the command string; the default command is print.
172 * Don't worry about a set of <blank>s with no command, that will
173 * default to print in the ex parser. We need to have two copies
174 * because the ex parser may step on the command string when it's
175 * parsing it.
177 if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
178 p = "pp";
179 len = 1;
182 MALLOC_RET(sp, ecp->cp, char *, len * 2);
183 ecp->o_cp = ecp->cp;
184 ecp->o_clen = len;
185 memcpy(ecp->cp + len, p, len);
186 ecp->range_lno = OOBLNO;
187 FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V);
188 LIST_INSERT_HEAD(&sp->gp->ecq, ecp, q);
191 * For each line... The semantics of global matching are that we first
192 * have to decide which lines are going to get passed to the command,
193 * and then pass them to the command, ignoring other changes. There's
194 * really no way to do this in a single pass, since arbitrary line
195 * creation, deletion and movement can be done in the ex command. For
196 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
197 * What we do is create linked list of lines that are tracked through
198 * each ex command. There's a callback routine which the DB interface
199 * routines call when a line is created or deleted. This doesn't help
200 * the layering much.
202 btype = BUSY_ON;
203 cnt = INTERRUPT_CHECK;
204 for (start = cmdp->addr1.lno,
205 end = cmdp->addr2.lno; start <= end; ++start) {
206 if (cnt-- == 0) {
207 if (INTERRUPTED(sp)) {
208 LIST_REMOVE(ecp, q);
209 free(ecp->cp);
210 free(ecp);
211 break;
213 search_busy(sp, btype);
214 btype = BUSY_UPDATE;
215 cnt = INTERRUPT_CHECK;
217 if (db_get(sp, start, DBG_FATAL, &dbp, &len))
218 return (1);
219 match[0].rm_so = 0;
220 match[0].rm_eo = len;
221 switch (eval =
222 regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) {
223 case 0:
224 if (cmd == V)
225 continue;
226 break;
227 case REG_NOMATCH:
228 if (cmd == GLOBAL)
229 continue;
230 break;
231 default:
232 re_error(sp, eval, &sp->re_c);
233 break;
236 /* If follows the last entry, extend the last entry's range. */
237 if ((rp = ecp->rq.cqh_last) != (void *)&ecp->rq &&
238 rp->stop == start - 1) {
239 ++rp->stop;
240 continue;
243 /* Allocate a new range, and append it to the list. */
244 CALLOC(sp, rp, RANGE *, 1, sizeof(RANGE));
245 if (rp == NULL)
246 return (1);
247 rp->start = rp->stop = start;
248 CIRCLEQ_INSERT_TAIL(&ecp->rq, rp, q);
250 search_busy(sp, BUSY_OFF);
251 return (0);
255 * ex_g_insdel --
256 * Update the ranges based on an insertion or deletion.
258 * PUBLIC: int ex_g_insdel __P((SCR *, lnop_t, recno_t));
261 ex_g_insdel(sp, op, lno)
262 SCR *sp;
263 lnop_t op;
264 recno_t lno;
266 EXCMD *ecp;
267 RANGE *nrp, *rp;
269 /* All insert/append operations are done as inserts. */
270 if (op == LINE_APPEND)
271 abort();
273 if (op == LINE_RESET)
274 return (0);
276 for (ecp = sp->gp->ecq.lh_first; ecp != NULL; ecp = ecp->q.le_next) {
277 if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
278 continue;
279 for (rp = ecp->rq.cqh_first; rp != (void *)&ecp->rq; rp = nrp) {
280 nrp = rp->q.cqe_next;
282 /* If range less than the line, ignore it. */
283 if (rp->stop < lno)
284 continue;
287 * If range greater than the line, decrement or
288 * increment the range.
290 if (rp->start > lno) {
291 if (op == LINE_DELETE) {
292 --rp->start;
293 --rp->stop;
294 } else {
295 ++rp->start;
296 ++rp->stop;
298 continue;
302 * Lno is inside the range, decrement the end point
303 * for deletion, and split the range for insertion.
304 * In the latter case, since we're inserting a new
305 * element, neither range can be exhausted.
307 if (op == LINE_DELETE) {
308 if (rp->start > --rp->stop) {
309 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
310 free(rp);
312 } else {
313 CALLOC_RET(sp, nrp, RANGE *, 1, sizeof(RANGE));
314 nrp->start = lno + 1;
315 nrp->stop = rp->stop + 1;
316 rp->stop = lno - 1;
317 CIRCLEQ_INSERT_AFTER(&ecp->rq, rp, nrp, q);
318 rp = nrp;
323 * If the command deleted/inserted lines, the cursor moves to
324 * the line after the deleted/inserted line.
326 ecp->range_lno = lno;
328 return (0);