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.
13 static const char sccsid
[] = "$Id: ex_global.c,v 10.28 2000/07/16 20:49:32 skimo Exp $ (Berkeley) $Date: 2000/07/16 20:49:32 $";
16 #include <sys/types.h>
17 #include <sys/queue.h>
19 #include <bitstring.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 *));
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 *));
60 return (ex_g_setup(sp
, cmdp
, V
));
65 * Ex global and v commands.
68 ex_g_setup(sp
, cmdp
, cmd
)
78 db_recno_t start
, end
;
87 if (F_ISSET(sp
, SC_EX_GLOBAL
)) {
89 "124|The %s command can't be used as part of a global or v command",
95 * Skip leading white space. Historic vi allowed any non-alphanumeric
96 * to serve as the global command delimiter.
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
);
109 * Get the pattern string, toss escaped characters.
112 * Only toss an escaped character if it escapes a delimiter.
114 for (ptrn
= t
= p
;;) {
115 if (p
[0] == '\0' || p
[0] == delim
) {
120 * Nul terminate the pattern string -- it's passed
121 * to regcomp which doesn't understand anything else.
129 else if (p
[1] == '\\')
134 /* If the pattern string is empty, use the last one. */
136 if (sp
->re
== NULL
) {
137 ex_emsg(sp
, NULL
, EXM_NOPREVRE
);
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
))
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
))
153 * Set saved RE. Historic practice is that globals set
154 * direction as well as the RE.
156 sp
->searchdir
= FORWARD
;
160 /* The global commands always set the previous context mark. */
163 if (mark_set(sp
, ABSMARK1
, &abs
, 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
177 if ((len
= cmdp
->argv
[0]->len
- (p
- cmdp
->argv
[0]->bp
)) == 0) {
178 static CHAR_T pp
[] = {'p', 'p', 0};
183 MALLOC_RET(sp
, ecp
->cp
, CHAR_T
*, (len
* 2) * sizeof(CHAR_T
));
186 MEMCPYW(ecp
->cp
+ len
, p
, len
);
187 ecp
->range_lno
= OOBLNO
;
188 FL_SET(ecp
->agv_flags
, cmd
== GLOBAL
? AGV_GLOBAL
: AGV_V
);
189 LIST_INSERT_HEAD(&sp
->wp
->ecq
, ecp
, q
);
192 * For each line... The semantics of global matching are that we first
193 * have to decide which lines are going to get passed to the command,
194 * and then pass them to the command, ignoring other changes. There's
195 * really no way to do this in a single pass, since arbitrary line
196 * creation, deletion and movement can be done in the ex command. For
197 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
198 * What we do is create linked list of lines that are tracked through
199 * each ex command. There's a callback routine which the DB interface
200 * routines call when a line is created or deleted. This doesn't help
204 cnt
= INTERRUPT_CHECK
;
205 for (start
= cmdp
->addr1
.lno
,
206 end
= cmdp
->addr2
.lno
; start
<= end
; ++start
) {
208 if (INTERRUPTED(sp
)) {
214 search_busy(sp
, btype
);
216 cnt
= INTERRUPT_CHECK
;
218 if (db_get(sp
, start
, DBG_FATAL
, &dbp
, &len
))
221 match
[0].rm_eo
= len
;
223 regexec(&sp
->re_c
, dbp
, 0, match
, REG_STARTEND
)) {
233 re_error(sp
, eval
, &sp
->re_c
);
237 /* If follows the last entry, extend the last entry's range. */
238 if ((rp
= ecp
->rq
.cqh_last
) != (void *)&ecp
->rq
&&
239 rp
->stop
== start
- 1) {
244 /* Allocate a new range, and append it to the list. */
245 CALLOC(sp
, rp
, RANGE
*, 1, sizeof(RANGE
));
248 rp
->start
= rp
->stop
= start
;
249 CIRCLEQ_INSERT_TAIL(&ecp
->rq
, rp
, q
);
251 search_busy(sp
, BUSY_OFF
);
257 * Update the ranges based on an insertion or deletion.
259 * PUBLIC: int ex_g_insdel __P((SCR *, lnop_t, db_recno_t));
262 ex_g_insdel(sp
, op
, lno
)
270 /* All insert/append operations are done as inserts. */
271 if (op
== LINE_APPEND
)
274 if (op
== LINE_RESET
)
277 for (ecp
= sp
->wp
->ecq
.lh_first
; ecp
!= NULL
; ecp
= ecp
->q
.le_next
) {
278 if (!FL_ISSET(ecp
->agv_flags
, AGV_AT
| AGV_GLOBAL
| AGV_V
))
280 for (rp
= ecp
->rq
.cqh_first
; rp
!= (void *)&ecp
->rq
; rp
= nrp
) {
281 nrp
= rp
->q
.cqe_next
;
283 /* If range less than the line, ignore it. */
288 * If range greater than the line, decrement or
289 * increment the range.
291 if (rp
->start
> lno
) {
292 if (op
== LINE_DELETE
) {
303 * Lno is inside the range, decrement the end point
304 * for deletion, and split the range for insertion.
305 * In the latter case, since we're inserting a new
306 * element, neither range can be exhausted.
308 if (op
== LINE_DELETE
) {
309 if (rp
->start
> --rp
->stop
) {
310 CIRCLEQ_REMOVE(&ecp
->rq
, rp
, q
);
314 CALLOC_RET(sp
, nrp
, RANGE
*, 1, sizeof(RANGE
));
315 nrp
->start
= lno
+ 1;
316 nrp
->stop
= rp
->stop
+ 1;
318 CIRCLEQ_INSERT_AFTER(&ecp
->rq
, rp
, nrp
, q
);
324 * If the command deleted/inserted lines, the cursor moves to
325 * the line after the deleted/inserted line.
327 ecp
->range_lno
= lno
;