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.
12 #include <sys/types.h>
13 #include <sys/queue.h>
16 #include <bitstring.h>
25 #include "../common/common.h"
27 enum which {GLOBAL, V};
29 static int ex_g_setup(SCR *, EXCMD *, enum which);
32 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
33 * Exec on lines matching a pattern.
35 * PUBLIC: int ex_global(SCR *, EXCMD *);
38 ex_global(SCR *sp, EXCMD *cmdp)
40 return (ex_g_setup(sp,
41 cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL));
45 * ex_v -- [line [,line]] v /pattern/ [commands]
46 * Exec on lines not matching a pattern.
48 * PUBLIC: int ex_v(SCR *, EXCMD *);
51 ex_v(SCR *sp, EXCMD *cmdp)
53 return (ex_g_setup(sp, cmdp, V));
58 * Ex global and v commands.
61 ex_g_setup(SCR *sp, EXCMD *cmdp, enum which cmd)
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");
84 * Skip leading white space. Historic vi allowed any non-alphanumeric
85 * to serve as the global command delimiter.
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);
98 * Get the pattern string, toss escaped characters.
101 * Only toss an escaped character if it escapes a delimiter.
103 for (ptrn = t = p;;) {
104 if (p[0] == '\0' || p[0] == delim) {
109 * Nul terminate the pattern string -- it's passed
110 * to regcomp which doesn't understand anything else.
118 else if (p[1] == '\\')
123 /* If the pattern string is empty, use the last one. */
125 if (sp->re == NULL) {
126 ex_emsg(sp, NULL, EXM_NOPREVRE);
130 /* Re-compile the RE if necessary. */
131 if (!F_ISSET(sp, SC_RE_SEARCH) &&
132 re_compile(sp, sp->re, sp->re_len,
133 NULL, NULL, &sp->re_c, RE_C_SEARCH))
136 /* Compile the RE. */
137 if (re_compile(sp, ptrn, t - ptrn, &sp->re,
138 &sp->re_len, &sp->re_c, RE_C_SEARCH))
142 * Set saved RE. Historic practice is that globals set
143 * direction as well as the RE.
145 sp->searchdir = FORWARD;
149 /* The global commands always set the previous context mark. */
152 if (mark_set(sp, ABSMARK1, &abs, 1))
155 /* Get an EXCMD structure. */
156 CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
160 * Get a copy of the command string; the default command is print.
161 * Don't worry about a set of <blank>s with no command, that will
162 * default to print in the ex parser. We need to have two copies
163 * because the ex parser may step on the command string when it's
166 if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
171 MALLOC_RET(sp, ecp->cp, (len * 2) * sizeof(CHAR_T));
174 MEMCPY(ecp->cp + len, p, len);
175 ecp->range_lno = OOBLNO;
176 FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V);
177 SLIST_INSERT_HEAD(sp->gp->ecq, ecp, q);
180 * For each line... The semantics of global matching are that we first
181 * have to decide which lines are going to get passed to the command,
182 * and then pass them to the command, ignoring other changes. There's
183 * really no way to do this in a single pass, since arbitrary line
184 * creation, deletion and movement can be done in the ex command. For
185 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
186 * What we do is create linked list of lines that are tracked through
187 * each ex command. There's a callback routine which the DB interface
188 * routines call when a line is created or deleted. This doesn't help
192 cnt = INTERRUPT_CHECK;
193 for (start = cmdp->addr1.lno,
194 end = cmdp->addr2.lno; start <= end; ++start) {
196 if (INTERRUPTED(sp)) {
197 SLIST_REMOVE_HEAD(sp->gp->ecq, q);
202 search_busy(sp, btype);
204 cnt = INTERRUPT_CHECK;
206 if (db_get(sp, start, DBG_FATAL, &dbp, &len))
209 match[0].rm_eo = len;
211 regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) {
221 re_error(sp, eval, &sp->re_c);
225 /* If follows the last entry, extend the last entry's range. */
226 if ((rp = TAILQ_LAST(ecp->rq, _rh)) != NULL &&
227 rp->stop == start - 1) {
232 /* Allocate a new range, and append it to the list. */
233 CALLOC(sp, rp, 1, sizeof(RANGE));
236 rp->start = rp->stop = start;
237 TAILQ_INSERT_TAIL(ecp->rq, rp, q);
239 search_busy(sp, BUSY_OFF);
245 * Update the ranges based on an insertion or deletion.
247 * PUBLIC: int ex_g_insdel(SCR *, lnop_t, recno_t);
250 ex_g_insdel(SCR *sp, lnop_t op, recno_t lno)
255 /* All insert/append operations are done as inserts. */
256 if (op == LINE_APPEND)
259 if (op == LINE_RESET)
262 SLIST_FOREACH(ecp, sp->gp->ecq, q) {
263 if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
265 TAILQ_FOREACH_SAFE(rp, ecp->rq, q, nrp) {
266 /* If range less than the line, ignore it. */
271 * If range greater than the line, decrement or
272 * increment the range.
274 if (rp->start > lno) {
275 if (op == LINE_DELETE) {
286 * Lno is inside the range, decrement the end point
287 * for deletion, and split the range for insertion.
288 * In the latter case, since we're inserting a new
289 * element, neither range can be exhausted.
291 if (op == LINE_DELETE) {
292 if (rp->start > --rp->stop) {
293 TAILQ_REMOVE(ecp->rq, rp, q);
297 CALLOC_RET(sp, nrp, 1, sizeof(RANGE));
298 nrp->start = lno + 1;
299 nrp->stop = rp->stop + 1;
301 TAILQ_INSERT_AFTER(ecp->rq, rp, nrp, q);
306 * If the command deleted/inserted lines, the cursor moves to
307 * the line after the deleted/inserted line.
309 ecp->range_lno = lno;