process.c revision 1.14 1 1.1 alm /*-
2 1.1 alm * Copyright (c) 1992 Diomidis Spinellis.
3 1.8 cgd * Copyright (c) 1992, 1993
4 1.8 cgd * The Regents of the University of California. All rights reserved.
5 1.1 alm *
6 1.1 alm * This code is derived from software contributed to Berkeley by
7 1.1 alm * Diomidis Spinellis of Imperial College, University of London.
8 1.1 alm *
9 1.1 alm * Redistribution and use in source and binary forms, with or without
10 1.1 alm * modification, are permitted provided that the following conditions
11 1.1 alm * are met:
12 1.1 alm * 1. Redistributions of source code must retain the above copyright
13 1.1 alm * notice, this list of conditions and the following disclaimer.
14 1.1 alm * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 alm * notice, this list of conditions and the following disclaimer in the
16 1.1 alm * documentation and/or other materials provided with the distribution.
17 1.1 alm * 3. All advertising materials mentioning features or use of this software
18 1.1 alm * must display the following acknowledgement:
19 1.1 alm * This product includes software developed by the University of
20 1.1 alm * California, Berkeley and its contributors.
21 1.1 alm * 4. Neither the name of the University nor the names of its contributors
22 1.1 alm * may be used to endorse or promote products derived from this software
23 1.1 alm * without specific prior written permission.
24 1.1 alm *
25 1.1 alm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 1.1 alm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 1.1 alm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 1.1 alm * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 1.1 alm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 1.1 alm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 1.1 alm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 1.1 alm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 1.1 alm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 1.1 alm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 1.1 alm * SUCH DAMAGE.
36 1.1 alm */
37 1.1 alm
38 1.1 alm #ifndef lint
39 1.9 cgd /* from: static char sccsid[] = "@(#)process.c 8.1 (Berkeley) 6/6/93"; */
40 1.14 mycroft static char *rcsid = "$Id: process.c,v 1.14 1995/03/15 11:13:30 mycroft Exp $";
41 1.1 alm #endif /* not lint */
42 1.1 alm
43 1.1 alm #include <sys/types.h>
44 1.1 alm #include <sys/stat.h>
45 1.1 alm #include <sys/ioctl.h>
46 1.1 alm #include <sys/uio.h>
47 1.1 alm
48 1.1 alm #include <ctype.h>
49 1.1 alm #include <errno.h>
50 1.1 alm #include <fcntl.h>
51 1.1 alm #include <limits.h>
52 1.1 alm #include <regex.h>
53 1.1 alm #include <stdio.h>
54 1.1 alm #include <stdlib.h>
55 1.1 alm #include <string.h>
56 1.1 alm #include <unistd.h>
57 1.1 alm
58 1.1 alm #include "defs.h"
59 1.1 alm #include "extern.h"
60 1.1 alm
61 1.8 cgd static SPACE HS, PS, SS;
62 1.1 alm #define pd PS.deleted
63 1.1 alm #define ps PS.space
64 1.1 alm #define psl PS.len
65 1.1 alm #define hs HS.space
66 1.1 alm #define hsl HS.len
67 1.1 alm
68 1.1 alm static inline int applies __P((struct s_command *));
69 1.1 alm static void flush_appends __P((void));
70 1.1 alm static void lputs __P((char *));
71 1.8 cgd static inline int regexec_e __P((regex_t *, const char *, int, int, size_t));
72 1.1 alm static void regsub __P((SPACE *, char *, char *));
73 1.1 alm static int substitute __P((struct s_command *));
74 1.1 alm
75 1.1 alm struct s_appends *appends; /* Array of pointers to strings to append. */
76 1.1 alm static int appendx; /* Index into appends array. */
77 1.1 alm int appendnum; /* Size of appends array. */
78 1.1 alm
79 1.1 alm static int lastaddr; /* Set by applies if last address of a range. */
80 1.1 alm static int sdone; /* If any substitutes since last line input. */
81 1.1 alm /* Iov structure for 'w' commands. */
82 1.1 alm static regex_t *defpreg;
83 1.1 alm size_t maxnsub;
84 1.1 alm regmatch_t *match;
85 1.1 alm
86 1.8 cgd #define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); }
87 1.8 cgd
88 1.1 alm void
89 1.1 alm process()
90 1.1 alm {
91 1.1 alm struct s_command *cp;
92 1.1 alm SPACE tspace;
93 1.1 alm size_t len;
94 1.1 alm char oldc, *p;
95 1.1 alm
96 1.1 alm for (linenum = 0; mf_fgets(&PS, REPLACE);) {
97 1.1 alm pd = 0;
98 1.1 alm cp = prog;
99 1.1 alm redirect:
100 1.1 alm while (cp != NULL) {
101 1.1 alm if (!applies(cp)) {
102 1.1 alm cp = cp->next;
103 1.1 alm continue;
104 1.1 alm }
105 1.1 alm switch (cp->code) {
106 1.1 alm case '{':
107 1.1 alm cp = cp->u.c;
108 1.1 alm goto redirect;
109 1.1 alm case 'a':
110 1.1 alm if (appendx >= appendnum)
111 1.1 alm appends = xrealloc(appends,
112 1.1 alm sizeof(struct s_appends) *
113 1.1 alm (appendnum *= 2));
114 1.1 alm appends[appendx].type = AP_STRING;
115 1.1 alm appends[appendx].s = cp->t;
116 1.8 cgd appends[appendx].len = strlen(cp->t);
117 1.1 alm appendx++;
118 1.1 alm break;
119 1.1 alm case 'b':
120 1.1 alm cp = cp->u.c;
121 1.1 alm goto redirect;
122 1.1 alm case 'c':
123 1.1 alm pd = 1;
124 1.1 alm psl = 0;
125 1.1 alm if (cp->a2 == NULL || lastaddr)
126 1.1 alm (void)printf("%s", cp->t);
127 1.1 alm break;
128 1.1 alm case 'd':
129 1.1 alm pd = 1;
130 1.1 alm goto new;
131 1.1 alm case 'D':
132 1.1 alm if (pd)
133 1.1 alm goto new;
134 1.8 cgd if ((p = memchr(ps, '\n', psl)) == NULL)
135 1.1 alm pd = 1;
136 1.1 alm else {
137 1.13 mycroft psl -= (p + 1) - ps;
138 1.1 alm memmove(ps, p + 1, psl);
139 1.1 alm }
140 1.1 alm goto new;
141 1.1 alm case 'g':
142 1.1 alm cspace(&PS, hs, hsl, REPLACE);
143 1.1 alm break;
144 1.1 alm case 'G':
145 1.8 cgd cspace(&PS, hs, hsl, 0);
146 1.1 alm break;
147 1.1 alm case 'h':
148 1.1 alm cspace(&HS, ps, psl, REPLACE);
149 1.1 alm break;
150 1.1 alm case 'H':
151 1.8 cgd cspace(&HS, ps, psl, 0);
152 1.1 alm break;
153 1.1 alm case 'i':
154 1.1 alm (void)printf("%s", cp->t);
155 1.1 alm break;
156 1.1 alm case 'l':
157 1.1 alm lputs(ps);
158 1.1 alm break;
159 1.1 alm case 'n':
160 1.1 alm if (!nflag && !pd)
161 1.8 cgd OUT(ps)
162 1.1 alm flush_appends();
163 1.14 mycroft if (!mf_fgets(&PS, REPLACE))
164 1.1 alm exit(0);
165 1.1 alm pd = 0;
166 1.1 alm break;
167 1.1 alm case 'N':
168 1.1 alm flush_appends();
169 1.8 cgd if (!mf_fgets(&PS, 0)) {
170 1.1 alm if (!nflag && !pd)
171 1.8 cgd OUT(ps)
172 1.1 alm exit(0);
173 1.1 alm }
174 1.1 alm break;
175 1.1 alm case 'p':
176 1.1 alm if (pd)
177 1.1 alm break;
178 1.8 cgd OUT(ps)
179 1.1 alm break;
180 1.1 alm case 'P':
181 1.1 alm if (pd)
182 1.1 alm break;
183 1.8 cgd if ((p = memchr(ps, '\n', psl)) != NULL) {
184 1.1 alm oldc = *p;
185 1.1 alm *p = '\0';
186 1.1 alm }
187 1.8 cgd OUT(ps)
188 1.1 alm if (p != NULL)
189 1.1 alm *p = oldc;
190 1.1 alm break;
191 1.1 alm case 'q':
192 1.1 alm if (!nflag && !pd)
193 1.8 cgd OUT(ps)
194 1.1 alm flush_appends();
195 1.1 alm exit(0);
196 1.1 alm case 'r':
197 1.1 alm if (appendx >= appendnum)
198 1.1 alm appends = xrealloc(appends,
199 1.1 alm sizeof(struct s_appends) *
200 1.1 alm (appendnum *= 2));
201 1.1 alm appends[appendx].type = AP_FILE;
202 1.1 alm appends[appendx].s = cp->t;
203 1.8 cgd appends[appendx].len = strlen(cp->t);
204 1.1 alm appendx++;
205 1.1 alm break;
206 1.1 alm case 's':
207 1.1 alm sdone |= substitute(cp);
208 1.1 alm break;
209 1.1 alm case 't':
210 1.1 alm if (sdone) {
211 1.1 alm sdone = 0;
212 1.1 alm cp = cp->u.c;
213 1.1 alm goto redirect;
214 1.1 alm }
215 1.1 alm break;
216 1.1 alm case 'w':
217 1.1 alm if (pd)
218 1.1 alm break;
219 1.1 alm if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
220 1.1 alm O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
221 1.1 alm DEFFILEMODE)) == -1)
222 1.1 alm err(FATAL, "%s: %s\n",
223 1.1 alm cp->t, strerror(errno));
224 1.8 cgd if (write(cp->u.fd, ps, psl) != psl)
225 1.1 alm err(FATAL, "%s: %s\n",
226 1.1 alm cp->t, strerror(errno));
227 1.1 alm break;
228 1.1 alm case 'x':
229 1.8 cgd if (hs == NULL)
230 1.8 cgd cspace(&HS, "", 0, REPLACE);
231 1.1 alm tspace = PS;
232 1.1 alm PS = HS;
233 1.1 alm HS = tspace;
234 1.1 alm break;
235 1.1 alm case 'y':
236 1.1 alm if (pd)
237 1.1 alm break;
238 1.8 cgd for (p = ps, len = psl; --len; ++p)
239 1.1 alm *p = cp->u.y[*p];
240 1.1 alm break;
241 1.1 alm case ':':
242 1.1 alm case '}':
243 1.1 alm break;
244 1.1 alm case '=':
245 1.1 alm (void)printf("%lu\n", linenum);
246 1.1 alm }
247 1.1 alm cp = cp->next;
248 1.1 alm } /* for all cp */
249 1.1 alm
250 1.1 alm new: if (!nflag && !pd)
251 1.8 cgd OUT(ps)
252 1.1 alm flush_appends();
253 1.1 alm } /* for all lines */
254 1.1 alm }
255 1.1 alm
256 1.1 alm /*
257 1.1 alm * TRUE if the address passed matches the current program state
258 1.1 alm * (lastline, linenumber, ps).
259 1.1 alm */
260 1.8 cgd #define MATCH(a) \
261 1.8 cgd (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) : \
262 1.1 alm (a)->type == AT_LINE ? linenum == (a)->u.l : lastline
263 1.1 alm
264 1.1 alm /*
265 1.1 alm * Return TRUE if the command applies to the current line. Sets the inrange
266 1.1 alm * flag to process ranges. Interprets the non-select (``!'') flag.
267 1.1 alm */
268 1.1 alm static inline int
269 1.1 alm applies(cp)
270 1.1 alm struct s_command *cp;
271 1.1 alm {
272 1.1 alm int r;
273 1.1 alm
274 1.1 alm lastaddr = 0;
275 1.1 alm if (cp->a1 == NULL && cp->a2 == NULL)
276 1.1 alm r = 1;
277 1.1 alm else if (cp->a2)
278 1.1 alm if (cp->inrange) {
279 1.1 alm if (MATCH(cp->a2)) {
280 1.1 alm cp->inrange = 0;
281 1.1 alm lastaddr = 1;
282 1.1 alm }
283 1.1 alm r = 1;
284 1.1 alm } else if (MATCH(cp->a1)) {
285 1.1 alm /*
286 1.1 alm * If the second address is a number less than or
287 1.1 alm * equal to the line number first selected, only
288 1.1 alm * one line shall be selected.
289 1.1 alm * -- POSIX 1003.2
290 1.1 alm */
291 1.1 alm if (cp->a2->type == AT_LINE &&
292 1.1 alm linenum >= cp->a2->u.l)
293 1.1 alm lastaddr = 1;
294 1.1 alm else
295 1.1 alm cp->inrange = 1;
296 1.1 alm r = 1;
297 1.1 alm } else
298 1.1 alm r = 0;
299 1.1 alm else
300 1.1 alm r = MATCH(cp->a1);
301 1.1 alm return (cp->nonsel ? ! r : r);
302 1.1 alm }
303 1.1 alm
304 1.1 alm /*
305 1.1 alm * substitute --
306 1.1 alm * Do substitutions in the pattern space. Currently, we build a
307 1.1 alm * copy of the new pattern space in the substitute space structure
308 1.1 alm * and then swap them.
309 1.1 alm */
310 1.1 alm static int
311 1.1 alm substitute(cp)
312 1.1 alm struct s_command *cp;
313 1.1 alm {
314 1.1 alm SPACE tspace;
315 1.1 alm regex_t *re;
316 1.8 cgd size_t re_off, slen;
317 1.12 cgd int n, lastempty;
318 1.1 alm char *s;
319 1.1 alm
320 1.1 alm s = ps;
321 1.1 alm re = cp->u.s->re;
322 1.1 alm if (re == NULL) {
323 1.1 alm if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
324 1.1 alm linenum = cp->u.s->linenum;
325 1.1 alm err(COMPILE, "\\%d not defined in the RE",
326 1.1 alm cp->u.s->maxbref);
327 1.1 alm }
328 1.1 alm }
329 1.8 cgd if (!regexec_e(re, s, 0, 0, psl))
330 1.1 alm return (0);
331 1.1 alm
332 1.1 alm SS.len = 0; /* Clean substitute space. */
333 1.8 cgd slen = psl;
334 1.1 alm n = cp->u.s->n;
335 1.12 cgd lastempty = 1;
336 1.12 cgd
337 1.1 alm switch (n) {
338 1.1 alm case 0: /* Global */
339 1.1 alm do {
340 1.12 cgd if (lastempty || match[0].rm_so != match[0].rm_eo) {
341 1.12 cgd /* Locate start of replaced string. */
342 1.12 cgd re_off = match[0].rm_so;
343 1.12 cgd /* Copy leading retained string. */
344 1.12 cgd cspace(&SS, s, re_off, APPEND);
345 1.12 cgd /* Add in regular expression. */
346 1.12 cgd regsub(&SS, s, cp->u.s->new);
347 1.12 cgd }
348 1.12 cgd
349 1.1 alm /* Move past this match. */
350 1.12 cgd if (match[0].rm_so != match[0].rm_eo) {
351 1.12 cgd s += match[0].rm_eo;
352 1.12 cgd slen -= match[0].rm_eo;
353 1.12 cgd lastempty = 0;
354 1.12 cgd } else {
355 1.12 cgd if (match[0].rm_so == 0)
356 1.12 cgd cspace(&SS, s, match[0].rm_so + 1,
357 1.12 cgd APPEND);
358 1.12 cgd else
359 1.12 cgd cspace(&SS, s + match[0].rm_so, 1,
360 1.12 cgd APPEND);
361 1.12 cgd s += match[0].rm_so + 1;
362 1.12 cgd slen -= match[0].rm_so + 1;
363 1.12 cgd lastempty = 1;
364 1.12 cgd }
365 1.12 cgd } while (slen > 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
366 1.1 alm /* Copy trailing retained string. */
367 1.12 cgd if (slen > 0)
368 1.12 cgd cspace(&SS, s, slen, APPEND);
369 1.1 alm break;
370 1.1 alm default: /* Nth occurrence */
371 1.1 alm while (--n) {
372 1.1 alm s += match[0].rm_eo;
373 1.8 cgd slen -= match[0].rm_eo;
374 1.8 cgd if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
375 1.1 alm return (0);
376 1.1 alm }
377 1.1 alm /* FALLTHROUGH */
378 1.1 alm case 1: /* 1st occurrence */
379 1.1 alm /* Locate start of replaced string. */
380 1.1 alm re_off = match[0].rm_so + (s - ps);
381 1.1 alm /* Copy leading retained string. */
382 1.1 alm cspace(&SS, ps, re_off, APPEND);
383 1.1 alm /* Add in regular expression. */
384 1.1 alm regsub(&SS, s, cp->u.s->new);
385 1.1 alm /* Copy trailing retained string. */
386 1.1 alm s += match[0].rm_eo;
387 1.8 cgd slen -= match[0].rm_eo;
388 1.8 cgd cspace(&SS, s, slen, APPEND);
389 1.1 alm break;
390 1.1 alm }
391 1.1 alm
392 1.1 alm /*
393 1.1 alm * Swap the substitute space and the pattern space, and make sure
394 1.1 alm * that any leftover pointers into stdio memory get lost.
395 1.1 alm */
396 1.1 alm tspace = PS;
397 1.1 alm PS = SS;
398 1.1 alm SS = tspace;
399 1.1 alm SS.space = SS.back;
400 1.1 alm
401 1.1 alm /* Handle the 'p' flag. */
402 1.1 alm if (cp->u.s->p)
403 1.8 cgd OUT(ps)
404 1.1 alm
405 1.1 alm /* Handle the 'w' flag. */
406 1.1 alm if (cp->u.s->wfile && !pd) {
407 1.1 alm if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
408 1.1 alm O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
409 1.1 alm err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
410 1.8 cgd if (write(cp->u.s->wfd, ps, psl) != psl)
411 1.1 alm err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno));
412 1.1 alm }
413 1.1 alm return (1);
414 1.1 alm }
415 1.1 alm
416 1.1 alm /*
417 1.1 alm * Flush append requests. Always called before reading a line,
418 1.1 alm * therefore it also resets the substitution done (sdone) flag.
419 1.1 alm */
420 1.1 alm static void
421 1.1 alm flush_appends()
422 1.1 alm {
423 1.1 alm FILE *f;
424 1.1 alm int count, i;
425 1.1 alm char buf[8 * 1024];
426 1.1 alm
427 1.1 alm for (i = 0; i < appendx; i++)
428 1.1 alm switch (appends[i].type) {
429 1.1 alm case AP_STRING:
430 1.8 cgd fwrite(appends[i].s, sizeof(char), appends[i].len,
431 1.8 cgd stdout);
432 1.1 alm break;
433 1.1 alm case AP_FILE:
434 1.1 alm /*
435 1.1 alm * Read files probably shouldn't be cached. Since
436 1.1 alm * it's not an error to read a non-existent file,
437 1.1 alm * it's possible that another program is interacting
438 1.1 alm * with the sed script through the file system. It
439 1.1 alm * would be truly bizarre, but possible. It's probably
440 1.1 alm * not that big a performance win, anyhow.
441 1.1 alm */
442 1.1 alm if ((f = fopen(appends[i].s, "r")) == NULL)
443 1.1 alm break;
444 1.8 cgd while (count = fread(buf, sizeof(char), sizeof(buf), f))
445 1.8 cgd (void)fwrite(buf, sizeof(char), count, stdout);
446 1.1 alm (void)fclose(f);
447 1.1 alm break;
448 1.1 alm }
449 1.1 alm if (ferror(stdout))
450 1.1 alm err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
451 1.1 alm appendx = sdone = 0;
452 1.1 alm }
453 1.1 alm
454 1.1 alm static void
455 1.1 alm lputs(s)
456 1.1 alm register char *s;
457 1.1 alm {
458 1.1 alm register int count;
459 1.1 alm register char *escapes, *p;
460 1.1 alm struct winsize win;
461 1.1 alm static int termwidth = -1;
462 1.1 alm
463 1.1 alm if (termwidth == -1)
464 1.1 alm if (p = getenv("COLUMNS"))
465 1.1 alm termwidth = atoi(p);
466 1.1 alm else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
467 1.1 alm win.ws_col > 0)
468 1.1 alm termwidth = win.ws_col;
469 1.1 alm else
470 1.1 alm termwidth = 60;
471 1.1 alm
472 1.1 alm for (count = 0; *s; ++s) {
473 1.1 alm if (count >= termwidth) {
474 1.1 alm (void)printf("\\\n");
475 1.1 alm count = 0;
476 1.1 alm }
477 1.1 alm if (isascii(*s) && isprint(*s) && *s != '\\') {
478 1.1 alm (void)putchar(*s);
479 1.1 alm count++;
480 1.1 alm } else {
481 1.1 alm escapes = "\\\a\b\f\n\r\t\v";
482 1.1 alm (void)putchar('\\');
483 1.1 alm if (p = strchr(escapes, *s)) {
484 1.1 alm (void)putchar("\\abfnrtv"[p - escapes]);
485 1.1 alm count += 2;
486 1.1 alm } else {
487 1.8 cgd (void)printf("%03o", *(u_char *)s);
488 1.1 alm count += 4;
489 1.1 alm }
490 1.1 alm }
491 1.1 alm }
492 1.1 alm (void)putchar('$');
493 1.1 alm (void)putchar('\n');
494 1.1 alm if (ferror(stdout))
495 1.1 alm err(FATAL, "stdout: %s", strerror(errno ? errno : EIO));
496 1.1 alm }
497 1.1 alm
498 1.1 alm static inline int
499 1.8 cgd regexec_e(preg, string, eflags, nomatch, slen)
500 1.1 alm regex_t *preg;
501 1.1 alm const char *string;
502 1.1 alm int eflags, nomatch;
503 1.8 cgd size_t slen;
504 1.1 alm {
505 1.1 alm int eval;
506 1.8 cgd
507 1.1 alm if (preg == NULL) {
508 1.1 alm if (defpreg == NULL)
509 1.1 alm err(FATAL, "first RE may not be empty");
510 1.1 alm } else
511 1.1 alm defpreg = preg;
512 1.1 alm
513 1.8 cgd /* Set anchors, discounting trailing newline (if any). */
514 1.8 cgd if (slen > 0 && string[slen - 1] == '\n')
515 1.8 cgd slen--;
516 1.8 cgd match[0].rm_so = 0;
517 1.8 cgd match[0].rm_eo = slen;
518 1.8 cgd
519 1.1 alm eval = regexec(defpreg, string,
520 1.8 cgd nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
521 1.1 alm switch(eval) {
522 1.1 alm case 0:
523 1.1 alm return (1);
524 1.1 alm case REG_NOMATCH:
525 1.1 alm return (0);
526 1.1 alm }
527 1.1 alm err(FATAL, "RE error: %s", strregerror(eval, defpreg));
528 1.1 alm /* NOTREACHED */
529 1.1 alm }
530 1.1 alm
531 1.1 alm /*
532 1.1 alm * regsub - perform substitutions after a regexp match
533 1.1 alm * Based on a routine by Henry Spencer
534 1.1 alm */
535 1.1 alm static void
536 1.1 alm regsub(sp, string, src)
537 1.1 alm SPACE *sp;
538 1.1 alm char *string, *src;
539 1.1 alm {
540 1.1 alm register int len, no;
541 1.1 alm register char c, *dst;
542 1.1 alm
543 1.1 alm #define NEEDSP(reqlen) \
544 1.1 alm if (sp->len >= sp->blen - (reqlen) - 1) { \
545 1.1 alm sp->blen += (reqlen) + 1024; \
546 1.1 alm sp->space = sp->back = xrealloc(sp->back, sp->blen); \
547 1.1 alm dst = sp->space + sp->len; \
548 1.1 alm }
549 1.1 alm
550 1.1 alm dst = sp->space + sp->len;
551 1.1 alm while ((c = *src++) != '\0') {
552 1.1 alm if (c == '&')
553 1.1 alm no = 0;
554 1.1 alm else if (c == '\\' && isdigit(*src))
555 1.1 alm no = *src++ - '0';
556 1.1 alm else
557 1.1 alm no = -1;
558 1.1 alm if (no < 0) { /* Ordinary character. */
559 1.1 alm if (c == '\\' && (*src == '\\' || *src == '&'))
560 1.1 alm c = *src++;
561 1.1 alm NEEDSP(1);
562 1.1 alm *dst++ = c;
563 1.1 alm ++sp->len;
564 1.1 alm } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
565 1.1 alm len = match[no].rm_eo - match[no].rm_so;
566 1.1 alm NEEDSP(len);
567 1.1 alm memmove(dst, string + match[no].rm_so, len);
568 1.1 alm dst += len;
569 1.1 alm sp->len += len;
570 1.1 alm }
571 1.1 alm }
572 1.1 alm NEEDSP(1);
573 1.1 alm *dst = '\0';
574 1.1 alm }
575 1.1 alm
576 1.1 alm /*
577 1.1 alm * aspace --
578 1.1 alm * Append the source space to the destination space, allocating new
579 1.1 alm * space as necessary.
580 1.1 alm */
581 1.1 alm void
582 1.1 alm cspace(sp, p, len, spflag)
583 1.1 alm SPACE *sp;
584 1.1 alm char *p;
585 1.1 alm size_t len;
586 1.1 alm enum e_spflag spflag;
587 1.1 alm {
588 1.1 alm size_t tlen;
589 1.1 alm
590 1.8 cgd /* Make sure SPACE has enough memory and ramp up quickly. */
591 1.8 cgd tlen = sp->len + len + 1;
592 1.1 alm if (tlen > sp->blen) {
593 1.1 alm sp->blen = tlen + 1024;
594 1.1 alm sp->space = sp->back = xrealloc(sp->back, sp->blen);
595 1.1 alm }
596 1.1 alm
597 1.8 cgd if (spflag == REPLACE)
598 1.1 alm sp->len = 0;
599 1.1 alm
600 1.1 alm memmove(sp->space + sp->len, p, len);
601 1.8 cgd
602 1.1 alm sp->space[sp->len += len] = '\0';
603 1.1 alm }
604 1.1 alm
605 1.1 alm /*
606 1.1 alm * Close all cached opened files and report any errors
607 1.1 alm */
608 1.1 alm void
609 1.1 alm cfclose(cp, end)
610 1.1 alm register struct s_command *cp, *end;
611 1.1 alm {
612 1.1 alm
613 1.1 alm for (; cp != end; cp = cp->next)
614 1.1 alm switch(cp->code) {
615 1.1 alm case 's':
616 1.1 alm if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
617 1.1 alm err(FATAL,
618 1.1 alm "%s: %s", cp->u.s->wfile, strerror(errno));
619 1.1 alm cp->u.s->wfd = -1;
620 1.1 alm break;
621 1.1 alm case 'w':
622 1.1 alm if (cp->u.fd != -1 && close(cp->u.fd))
623 1.1 alm err(FATAL, "%s: %s", cp->t, strerror(errno));
624 1.1 alm cp->u.fd = -1;
625 1.1 alm break;
626 1.1 alm case '{':
627 1.1 alm cfclose(cp->u.c, cp->next);
628 1.1 alm break;
629 1.1 alm }
630 1.1 alm }
631