]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/nvi/ex/ex_global.c
Update nvi to 2.2.0
[FreeBSD/FreeBSD.git] / contrib / nvi / ex / ex_global.c
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.
6  *
7  * See the LICENSE file for redistribution information.
8  */
9
10 #include "config.h"
11
12 #include <sys/types.h>
13 #include <sys/queue.h>
14 #include <sys/time.h>
15
16 #include <bitstring.h>
17 #include <ctype.h>
18 #include <errno.h>
19 #include <limits.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <unistd.h>
24
25 #include "../common/common.h"
26
27 enum which {GLOBAL, V};
28
29 static int ex_g_setup(SCR *, EXCMD *, enum which);
30
31 /*
32  * ex_global -- [line [,line]] g[lobal][!] /pattern/ [commands]
33  *      Exec on lines matching a pattern.
34  *
35  * PUBLIC: int ex_global(SCR *, EXCMD *);
36  */
37 int
38 ex_global(SCR *sp, EXCMD *cmdp)
39 {
40         return (ex_g_setup(sp,
41             cmdp, FL_ISSET(cmdp->iflags, E_C_FORCE) ? V : GLOBAL));
42 }
43
44 /*
45  * ex_v -- [line [,line]] v /pattern/ [commands]
46  *      Exec on lines not matching a pattern.
47  *
48  * PUBLIC: int ex_v(SCR *, EXCMD *);
49  */
50 int
51 ex_v(SCR *sp, EXCMD *cmdp)
52 {
53         return (ex_g_setup(sp, cmdp, V));
54 }
55
56 /*
57  * ex_g_setup --
58  *      Ex global and v commands.
59  */
60 static int
61 ex_g_setup(SCR *sp, EXCMD *cmdp, enum which cmd)
62 {
63         CHAR_T *ptrn, *p, *t;
64         EXCMD *ecp;
65         MARK abs;
66         RANGE *rp;
67         busy_t btype;
68         recno_t start, end;
69         regex_t *re;
70         regmatch_t match[1];
71         size_t len;
72         int cnt, delim, eval;
73         CHAR_T *dbp;
74
75         NEEDFILE(sp, cmdp);
76
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");
80                 return (1);
81         }
82
83         /*
84          * Skip leading white space.  Historic vi allowed any non-alphanumeric
85          * to serve as the global command delimiter.
86          */
87         if (cmdp->argc == 0)
88                 goto usage;
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);
93                 return (1);
94         }
95         delim = *p++;
96
97         /*
98          * Get the pattern string, toss escaped characters.
99          *
100          * QUOTING NOTE:
101          * Only toss an escaped character if it escapes a delimiter.
102          */
103         for (ptrn = t = p;;) {
104                 if (p[0] == '\0' || p[0] == delim) {
105                         if (p[0] == delim)
106                                 ++p;
107                         /*
108                          * !!!
109                          * Nul terminate the pattern string -- it's passed
110                          * to regcomp which doesn't understand anything else.
111                          */
112                         *t = '\0';
113                         break;
114                 }
115                 if (p[0] == '\\')
116                         if (p[1] == delim)
117                                 ++p;
118                         else if (p[1] == '\\')
119                                 *t++ = *p++;
120                 *t++ = *p++;
121         }
122
123         /* If the pattern string is empty, use the last one. */
124         if (*ptrn == '\0') {
125                 if (sp->re == NULL) {
126                         ex_emsg(sp, NULL, EXM_NOPREVRE);
127                         return (1);
128                 }
129
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))
134                         return (1);
135         } else {
136                 /* Compile the RE. */
137                 if (re_compile(sp, ptrn, t - ptrn, &sp->re,
138                     &sp->re_len, &sp->re_c, RE_C_SEARCH))
139                         return (1);
140
141                 /*
142                  * Set saved RE.  Historic practice is that globals set
143                  * direction as well as the RE.
144                  */
145                 sp->searchdir = FORWARD;
146         }
147         re = &sp->re_c;
148
149         /* The global commands always set the previous context mark. */
150         abs.lno = sp->lno;
151         abs.cno = sp->cno;
152         if (mark_set(sp, ABSMARK1, &abs, 1))
153                 return (1);
154
155         /* Get an EXCMD structure. */
156         CALLOC_RET(sp, ecp, 1, sizeof(EXCMD));
157         TAILQ_INIT(ecp->rq);
158
159         /*
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
164          * parsing it.
165          */
166         if ((len = cmdp->argv[0]->len - (p - cmdp->argv[0]->bp)) == 0) {
167                 p = L("p");
168                 len = 1;
169         }
170
171         MALLOC_RET(sp, ecp->cp, (len * 2) * sizeof(CHAR_T));
172         ecp->o_cp = ecp->cp;
173         ecp->o_clen = len;
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);
178
179         /*
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
189          * the layering much.
190          */
191         btype = BUSY_ON;
192         cnt = INTERRUPT_CHECK;
193         for (start = cmdp->addr1.lno,
194             end = cmdp->addr2.lno; start <= end; ++start) {
195                 if (cnt-- == 0) {
196                         if (INTERRUPTED(sp)) {
197                                 SLIST_REMOVE_HEAD(sp->gp->ecq, q);
198                                 free(ecp->cp);
199                                 free(ecp);
200                                 break;
201                         }
202                         search_busy(sp, btype);
203                         btype = BUSY_UPDATE;
204                         cnt = INTERRUPT_CHECK;
205                 }
206                 if (db_get(sp, start, DBG_FATAL, &dbp, &len))
207                         return (1);
208                 match[0].rm_so = 0;
209                 match[0].rm_eo = len;
210                 switch (eval =
211                     regexec(&sp->re_c, dbp, 0, match, REG_STARTEND)) {
212                 case 0:
213                         if (cmd == V)
214                                 continue;
215                         break;
216                 case REG_NOMATCH:
217                         if (cmd == GLOBAL)
218                                 continue;
219                         break;
220                 default:
221                         re_error(sp, eval, &sp->re_c);
222                         break;
223                 }
224
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) {
228                         ++rp->stop;
229                         continue;
230                 }
231
232                 /* Allocate a new range, and append it to the list. */
233                 CALLOC(sp, rp, 1, sizeof(RANGE));
234                 if (rp == NULL)
235                         return (1);
236                 rp->start = rp->stop = start;
237                 TAILQ_INSERT_TAIL(ecp->rq, rp, q);
238         }
239         search_busy(sp, BUSY_OFF);
240         return (0);
241 }
242
243 /*
244  * ex_g_insdel --
245  *      Update the ranges based on an insertion or deletion.
246  *
247  * PUBLIC: int ex_g_insdel(SCR *, lnop_t, recno_t);
248  */
249 int
250 ex_g_insdel(SCR *sp, lnop_t op, recno_t lno)
251 {
252         EXCMD *ecp;
253         RANGE *nrp, *rp;
254
255         /* All insert/append operations are done as inserts. */
256         if (op == LINE_APPEND)
257                 abort();
258
259         if (op == LINE_RESET)
260                 return (0);
261
262         SLIST_FOREACH(ecp, sp->gp->ecq, q) {
263                 if (!FL_ISSET(ecp->agv_flags, AGV_AT | AGV_GLOBAL | AGV_V))
264                         continue;
265                 TAILQ_FOREACH_SAFE(rp, ecp->rq, q, nrp) {
266                         /* If range less than the line, ignore it. */
267                         if (rp->stop < lno)
268                                 continue;
269                         
270                         /*
271                          * If range greater than the line, decrement or
272                          * increment the range.
273                          */
274                         if (rp->start > lno) {
275                                 if (op == LINE_DELETE) {
276                                         --rp->start;
277                                         --rp->stop;
278                                 } else {
279                                         ++rp->start;
280                                         ++rp->stop;
281                                 }
282                                 continue;
283                         }
284
285                         /*
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.
290                          */
291                         if (op == LINE_DELETE) {
292                                 if (rp->start > --rp->stop) {
293                                         TAILQ_REMOVE(ecp->rq, rp, q);
294                                         free(rp);
295                                 }
296                         } else {
297                                 CALLOC_RET(sp, nrp, 1, sizeof(RANGE));
298                                 nrp->start = lno + 1;
299                                 nrp->stop = rp->stop + 1;
300                                 rp->stop = lno - 1;
301                                 TAILQ_INSERT_AFTER(ecp->rq, rp, nrp, q);
302                         }
303                 }
304
305                 /*
306                  * If the command deleted/inserted lines, the cursor moves to
307                  * the line after the deleted/inserted line.
308                  */
309                 ecp->range_lno = lno;
310         }
311         return (0);
312 }