Expand PMF_FN_* macros.
[netbsd-mini2440.git] / dist / nvi / ex / ex_global.c
blob8fd0aa4d24d914ce5201590dfe425d2d481eb941
1 /* $NetBSD: ex_global.c,v 1.2 2008/12/05 22:51:42 christos Exp $ */
3 /*-
4 * Copyright (c) 1992, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1992, 1993, 1994, 1995, 1996
7 * Keith Bostic. All rights reserved.
9 * See the LICENSE file for redistribution information.
12 #include "config.h"
14 #ifndef lint
15 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";
16 #endif /* not lint */
18 #include <sys/types.h>
19 #include <sys/queue.h>
21 #include <bitstring.h>
22 #include <ctype.h>
23 #include <errno.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 #include "../common/common.h"
32 enum which {GLOBAL, V};
34 static int ex_g_setup __P((SCR *, EXCMD *, enum which));
37 * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
38 * Exec on lines matching a pattern.
40 * PUBLIC: int ex_global __P((SCR *, EXCMD *));
42 int
43 ex_global(SCR *sp, 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(SCR *sp, EXCMD *cmdp)
58 return (ex_g_setup(sp, cmdp, V));
62 * ex_g_setup --
63 * Ex global and v commands.
65 static int
66 ex_g_setup(SCR *sp, EXCMD *cmdp, enum which cmd)
68 CHAR_T *ptrn, *p, *t;
69 EXCMD *ecp;
70 MARK myabs;
71 RANGE *rp;
72 busy_t btype;
73 db_recno_t start, end;
74 regex_t *re;
75 regmatch_t match[1];
76 size_t len;
77 int cnt, delim, eval;
78 CHAR_T *dbp;
80 NEEDFILE(sp, cmdp);
82 if (F_ISSET(sp, SC_EX_GLOBAL)) {
83 msgq_wstr(sp, M_ERR, cmdp->cmd->name,
84 "124|The %s command can't be used as part of a global or v command");
85 return (1);
89 * Skip leading white space. Historic vi allowed any non-alphanumeric
90 * to serve as the global command delimiter.
92 if (cmdp->argc == 0)
93 goto usage;
94 for (p = cmdp->argv[0]->bp; ISBLANK(*p); ++p);
95 if (*p == '\0' || ISALNUM(*p) ||
96 *p == '\\' || *p == '|' || *p == '\n') {
97 usage: ex_emsg(sp, cmdp->cmd->usage, EXM_USAGE);
98 return (1);
100 delim = *p++;
103 * Get the pattern string, toss escaped characters.
105 * QUOTING NOTE:
106 * Only toss an escaped character if it escapes a delimiter.
108 for (ptrn = t = p;;) {
109 if (p[0] == '\0' || p[0] == delim) {
110 if (p[0] == delim)
111 ++p;
113 * !!!
114 * Nul terminate the pattern string -- it's passed
115 * to regcomp which doesn't understand anything else.
117 *t = L('\0');
118 break;
120 if (p[0] == L('\\')) {
121 if (p[1] == delim)
122 ++p;
123 else if (p[1] == L('\\'))
124 *t++ = *p++;
126 *t++ = *p++;
129 /* If the pattern string is empty, use the last one. */
130 if (*ptrn == L('\0')) {
131 if (sp->re == NULL) {
132 ex_emsg(sp, NULL, EXM_NOPREVRE);
133 return (1);
136 /* Re-compile the RE if necessary. */
137 if (!F_ISSET(sp, SC_RE_SEARCH) &&
138 re_compile(sp, sp->re, sp->re_len,
139 NULL, NULL, &sp->re_c, SEARCH_CSEARCH | SEARCH_MSG))
140 return (1);
141 } else {
142 /* Compile the RE. */
143 if (re_compile(sp, ptrn, t - ptrn, &sp->re,
144 &sp->re_len, &sp->re_c, SEARCH_CSEARCH | SEARCH_MSG))
145 return (1);
148 * Set saved RE. Historic practice is that globals set
149 * direction as well as the RE.
151 sp->searchdir = FORWARD;
153 re = &sp->re_c;
155 /* The global commands always set the previous context mark. */
156 myabs.lno = sp->lno;
157 myabs.cno = sp->cno;
158 if (mark_set(sp, ABSMARK1, &myabs, 1))
159 return (1);
161 /* Get an EXCMD structure. */
162 CALLOC_RET(sp, ecp, EXCMD *, 1, sizeof(EXCMD));
163 CIRCLEQ_INIT(&ecp->rq);
166 * Get a copy of the command string; the default command is print.
167 * Don't worry about a set of <blank>s with no command, that will
168 * default to print in the ex parser. We need to have two copies
169 * because the ex parser may step on the command string when it's
170 * parsing it.
172 if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
173 p = __UNCONST(L("p"));
174 len = 1;
177 MALLOC_RET(sp, ecp->cp, CHAR_T *, (len * 2) * sizeof(CHAR_T));
178 ecp->o_cp = ecp->cp;
179 ecp->o_clen = len;
180 MEMCPYW(ecp->cp + len, p, len);
181 ecp->range_lno = OOBLNO;
182 FL_SET(ecp->agv_flags, cmd == GLOBAL ? AGV_GLOBAL : AGV_V);
183 LIST_INSERT_HEAD(&sp->wp->ecq, ecp, q);
186 * For each line... The semantics of global matching are that we first
187 * have to decide which lines are going to get passed to the command,
188 * and then pass them to the command, ignoring other changes. There's
189 * really no way to do this in a single pass, since arbitrary line
190 * creation, deletion and movement can be done in the ex command. For
191 * example, a good vi clone test is ":g/X/mo.-3", or "g/X/.,.+1d".
192 * What we do is create linked list of lines that are tracked through
193 * each ex command. There's a callback routine which the DB interface
194 * routines call when a line is created or deleted. This doesn't help
195 * the layering much.
197 btype = BUSY_ON;
198 cnt = INTERRUPT_CHECK;
199 for (start = cmdp->addr1.lno,
200 end = cmdp->addr2.lno; start <= end; ++start) {
201 if (cnt-- == 0) {
202 if (INTERRUPTED(sp)) {
203 LIST_REMOVE(ecp, q);
204 free(ecp->cp);
205 free(ecp);
206 break;
208 search_busy(sp, btype);
209 btype = BUSY_UPDATE;
210 cnt = INTERRUPT_CHECK;
212 if (db_get(sp, start, DBG_FATAL, &dbp, &len))
213 return (1);
214 match[0].rm_so = 0;
215 match[0].rm_eo = len;
216 switch (eval =
217 regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) {
218 case 0:
219 if (cmd == V)
220 continue;
221 break;
222 case REG_NOMATCH:
223 if (cmd == GLOBAL)
224 continue;
225 break;
226 default:
227 re_error(sp, eval, &sp->re_c);
228 break;
231 /* If follows the last entry, extend the last entry's range. */
232 if ((rp = ecp->rq.cqh_last) != (void *)&ecp->rq &&
233 rp->stop == start - 1) {
234 ++rp->stop;
235 continue;
238 /* Allocate a new range, and append it to the list. */
239 CALLOC(sp, rp, RANGE *, 1, sizeof(RANGE));
240 if (rp == NULL)
241 return (1);
242 rp->start = rp->stop = start;
243 CIRCLEQ_INSERT_TAIL(&ecp->rq, rp, q);
245 search_busy(sp, BUSY_OFF);
246 return (0);
250 * ex_g_insdel --
251 * Update the ranges based on an insertion or deletion.
253 * PUBLIC: int ex_g_insdel __P((SCR *, lnop_t, db_recno_t));
256 ex_g_insdel(SCR *sp, lnop_t op, db_recno_t lno)
258 EXCMD *ecp;
259 RANGE *nrp, *rp;
261 /* All insert/append operations are done as inserts. */
262 if (op == LINE_APPEND)
263 abort();
265 if (op == LINE_RESET)
266 return (0);
268 for (ecp = sp->wp->ecq.lh_first; ecp != NULL; ecp = ecp->q.le_next) {
269 if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
270 continue;
271 for (rp = ecp->rq.cqh_first; rp != (void *)&ecp->rq; rp = nrp) {
272 nrp = rp->q.cqe_next;
274 /* If range less than the line, ignore it. */
275 if (rp->stop < lno)
276 continue;
279 * If range greater than the line, decrement or
280 * increment the range.
282 if (rp->start > lno) {
283 if (op == LINE_DELETE) {
284 --rp->start;
285 --rp->stop;
286 } else {
287 ++rp->start;
288 ++rp->stop;
290 continue;
294 * Lno is inside the range, decrement the end point
295 * for deletion, and split the range for insertion.
296 * In the latter case, since we're inserting a new
297 * element, neither range can be exhausted.
299 if (op == LINE_DELETE) {
300 if (rp->start > --rp->stop) {
301 CIRCLEQ_REMOVE(&ecp->rq, rp, q);
302 free(rp);
304 } else {
305 CALLOC_RET(sp, nrp, RANGE *, 1, sizeof(RANGE));
306 nrp->start = lno + 1;
307 nrp->stop = rp->stop + 1;
308 rp->stop = lno - 1;
309 CIRCLEQ_INSERT_AFTER(&ecp->rq, rp, nrp, q);
310 rp = nrp;
315 * If the command deleted/inserted lines, the cursor moves to
316 * the line after the deleted/inserted line.
318 ecp->range_lno = lno;
320 return (0);