1 /* $NetBSD: ex_global.c,v 1.5 2014/01/26 21:43:45 christos Exp $ */
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
5 * Copyright (c) 1992, 1993, 1994, 1995, 1996
6 * Keith Bostic. All rights reserved.
8 * See the LICENSE file for redistribution information.
13 #include <sys/cdefs.h>
16 static const char sccsid
[] = "Id: ex_global.c,v 10.30 2001/06/25 15:19:16 skimo Exp (Berkeley) Date: 2001/06/25 15:19:16 ";
19 __RCSID("$NetBSD: ex_global.c,v 1.5 2014/01/26 21:43:45 christos Exp $");
22 #include <sys/types.h>
23 #include <sys/queue.h>
25 #include <bitstring.h>
34 #include "../common/common.h"
36 enum which
{GLOBAL
, V
};
38 static int ex_g_setup
__P((SCR
*, EXCMD
*, enum which
));
41 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
42 * Exec on lines matching a pattern.
44 * PUBLIC: int ex_global __P((SCR *, EXCMD *));
47 ex_global(SCR
*sp
, EXCMD
*cmdp
)
49 return (ex_g_setup(sp
,
50 cmdp
, FL_ISSET(cmdp
->iflags
, E_C_FORCE
) ? V
: GLOBAL
));
54 * ex_v -- [line [,line]] v /pattern/ [commands]
55 * Exec on lines not matching a pattern.
57 * PUBLIC: int ex_v __P((SCR *, EXCMD *));
60 ex_v(SCR
*sp
, EXCMD
*cmdp
)
62 return (ex_g_setup(sp
, cmdp
, V
));
67 * Ex global and v commands.
70 ex_g_setup(SCR
*sp
, EXCMD
*cmdp
, enum which cmd
)
77 db_recno_t start
, end
;
85 if (F_ISSET(sp
, SC_EX_GLOBAL
)) {
86 msgq_wstr(sp
, M_ERR
, cmdp
->cmd
->name
,
87 "124|The %s command can't be used as part of a global or v command");
92 * Skip leading white space. Historic vi allowed any non-alphanumeric
93 * to serve as the global command delimiter.
97 for (p
= cmdp
->argv
[0]->bp
; ISBLANK(*p
); ++p
);
98 if (*p
== '\0' || ISALNUM((UCHAR_T
)*p
) ||
99 *p
== '\\' || *p
== '|' || *p
== '\n') {
100 usage
: ex_emsg(sp
, cmdp
->cmd
->usage
, EXM_USAGE
);
106 * Get the pattern string, toss escaped characters.
109 * Only toss an escaped character if it escapes a delimiter.
111 for (ptrn
= t
= p
;;) {
112 if (p
[0] == '\0' || p
[0] == delim
) {
117 * Nul terminate the pattern string -- it's passed
118 * to regcomp which doesn't understand anything else.
123 if (p
[0] == L('\\')) {
126 else if (p
[1] == L('\\'))
132 /* If the pattern string is empty, use the last one. */
133 if (*ptrn
== L('\0')) {
134 if (sp
->re
== NULL
) {
135 ex_emsg(sp
, NULL
, EXM_NOPREVRE
);
139 /* Re-compile the RE if necessary. */
140 if (!F_ISSET(sp
, SC_RE_SEARCH
) &&
141 re_compile(sp
, sp
->re
, sp
->re_len
,
142 NULL
, NULL
, &sp
->re_c
, SEARCH_CSEARCH
| SEARCH_MSG
))
145 /* Compile the RE. */
146 if (re_compile(sp
, ptrn
, t
- ptrn
, &sp
->re
,
147 &sp
->re_len
, &sp
->re_c
, SEARCH_CSEARCH
| SEARCH_MSG
))
151 * Set saved RE. Historic practice is that globals set
152 * direction as well as the RE.
154 sp
->searchdir
= FORWARD
;
157 /* The global commands always set the previous context mark. */
160 if (mark_set(sp
, ABSMARK1
, &myabs
, 1))
163 /* Get an EXCMD structure. */
164 CALLOC_RET(sp
, ecp
, EXCMD
*, 1, sizeof(EXCMD
));
165 TAILQ_INIT(&ecp
->rq
);
168 * Get a copy of the command string; the default command is print.
169 * Don't worry about a set of <blank>s with no command, that will
170 * default to print in the ex parser. We need to have two copies
171 * because the ex parser may step on the command string when it's
174 if ((len
= cmdp
->argv
[0]->len
- (p
- cmdp
->argv
[0]->bp
)) == 0) {
175 p
= __UNCONST(L("p"));
179 MALLOC_GOTO(sp
, ecp
->cp
, CHAR_T
*, (len
* 2) * sizeof(CHAR_T
));
182 MEMCPYW(ecp
->cp
+ len
, p
, len
);
183 ecp
->range_lno
= OOBLNO
;
184 FL_SET(ecp
->agv_flags
, cmd
== GLOBAL
? AGV_GLOBAL
: AGV_V
);
185 LIST_INSERT_HEAD(&sp
->wp
->ecq
, ecp
, q
);
188 * For each line... The semantics of global matching are that we first
189 * have to decide which lines are going to get passed to the command,
190 * and then pass them to the command, ignoring other changes. There's
191 * really no way to do this in a single pass, since arbitrary line
192 * creation, deletion and movement can be done in the ex command. For
193 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
194 * What we do is create linked list of lines that are tracked through
195 * each ex command. There's a callback routine which the DB interface
196 * routines call when a line is created or deleted. This doesn't help
200 cnt
= INTERRUPT_CHECK
;
201 for (start
= cmdp
->addr1
.lno
,
202 end
= cmdp
->addr2
.lno
; start
<= end
; ++start
) {
204 if (INTERRUPTED(sp
)) {
210 search_busy(sp
, btype
);
212 cnt
= INTERRUPT_CHECK
;
214 if (db_get(sp
, start
, DBG_FATAL
, &dbp
, &len
))
217 match
[0].rm_eo
= len
;
219 regexec(&sp
->re_c
, dbp
, 0, match
, REG_STARTEND
)) {
229 re_error(sp
, eval
, &sp
->re_c
);
233 /* If follows the last entry, extend the last entry's range. */
234 if ((rp
= TAILQ_LAST(&ecp
->rq
, _rh
)) != NULL
&&
235 rp
->stop
== start
- 1) {
240 /* Allocate a new range, and append it to the list. */
241 CALLOC(sp
, rp
, RANGE
*, 1, sizeof(RANGE
));
244 rp
->start
= rp
->stop
= start
;
245 TAILQ_INSERT_TAIL(&ecp
->rq
, rp
, q
);
247 search_busy(sp
, BUSY_OFF
);
256 * Update the ranges based on an insertion or deletion.
258 * PUBLIC: int ex_g_insdel __P((SCR *, lnop_t, db_recno_t));
261 ex_g_insdel(SCR
*sp
, lnop_t op
, db_recno_t lno
)
266 /* All insert/append operations are done as inserts. */
267 if (op
== LINE_APPEND
)
270 if (op
== LINE_RESET
)
273 LIST_FOREACH(ecp
, &sp
->wp
->ecq
, q
) {
274 if (!FL_ISSET(ecp
->agv_flags
, AGV_AT
| AGV_GLOBAL
| AGV_V
))
276 TAILQ_FOREACH_SAFE(rp
, &ecp
->rq
, q
, nrp
) {
277 /* If range less than the line, ignore it. */
282 * If range greater than the line, decrement or
283 * increment the range.
285 if (rp
->start
> lno
) {
286 if (op
== LINE_DELETE
) {
297 * Lno is inside the range, decrement the end point
298 * for deletion, and split the range for insertion.
299 * In the latter case, since we're inserting a new
300 * element, neither range can be exhausted.
302 if (op
== LINE_DELETE
) {
303 if (rp
->start
> --rp
->stop
) {
304 TAILQ_REMOVE(&ecp
->rq
, rp
, q
);
308 CALLOC_RET(sp
, nrp
, RANGE
*, 1, sizeof(RANGE
));
309 nrp
->start
= lno
+ 1;
310 nrp
->stop
= rp
->stop
+ 1;
312 TAILQ_INSERT_AFTER(&ecp
->rq
, rp
, nrp
, q
);
317 * If the command deleted/inserted lines, the cursor moves to
318 * the line after the deleted/inserted line.
320 ecp
->range_lno
= lno
;