1*0a6a1f1dSLionel Sambuc /* $NetBSD: process.c,v 1.52 2015/03/12 12:40:41 christos Exp $ */
2f789fee2SBen Gras
3f789fee2SBen Gras /*-
4*0a6a1f1dSLionel Sambuc * Copyright (c) 1992 Diomidis Spinellis.
5f789fee2SBen Gras * Copyright (c) 1992, 1993, 1994
6f789fee2SBen Gras * The Regents of the University of California. All rights reserved.
7f789fee2SBen Gras *
8f789fee2SBen Gras * This code is derived from software contributed to Berkeley by
9f789fee2SBen Gras * Diomidis Spinellis of Imperial College, University of London.
10f789fee2SBen Gras *
11f789fee2SBen Gras * Redistribution and use in source and binary forms, with or without
12f789fee2SBen Gras * modification, are permitted provided that the following conditions
13f789fee2SBen Gras * are met:
14f789fee2SBen Gras * 1. Redistributions of source code must retain the above copyright
15f789fee2SBen Gras * notice, this list of conditions and the following disclaimer.
16f789fee2SBen Gras * 2. Redistributions in binary form must reproduce the above copyright
17f789fee2SBen Gras * notice, this list of conditions and the following disclaimer in the
18f789fee2SBen Gras * documentation and/or other materials provided with the distribution.
19f789fee2SBen Gras * 3. Neither the name of the University nor the names of its contributors
20f789fee2SBen Gras * may be used to endorse or promote products derived from this software
21f789fee2SBen Gras * without specific prior written permission.
22f789fee2SBen Gras *
23f789fee2SBen Gras * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24f789fee2SBen Gras * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25f789fee2SBen Gras * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26f789fee2SBen Gras * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27f789fee2SBen Gras * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28f789fee2SBen Gras * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29f789fee2SBen Gras * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30f789fee2SBen Gras * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31f789fee2SBen Gras * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32f789fee2SBen Gras * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33f789fee2SBen Gras * SUCH DAMAGE.
34f789fee2SBen Gras */
35f789fee2SBen Gras
36f789fee2SBen Gras #if HAVE_NBTOOL_CONFIG_H
37f789fee2SBen Gras #include "nbtool_config.h"
38f789fee2SBen Gras #endif
39f789fee2SBen Gras
40f789fee2SBen Gras #include <sys/cdefs.h>
41*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: process.c,v 1.52 2015/03/12 12:40:41 christos Exp $");
42*0a6a1f1dSLionel Sambuc #ifdef __FBSDID
43*0a6a1f1dSLionel Sambuc __FBSDID("$FreeBSD: head/usr.bin/sed/process.c 192732 2009-05-25 06:45:33Z brian $");
44f789fee2SBen Gras #endif
45*0a6a1f1dSLionel Sambuc
46*0a6a1f1dSLionel Sambuc #if 0
47*0a6a1f1dSLionel Sambuc static const char sccsid[] = "@(#)process.c 8.6 (Berkeley) 4/20/94";
48*0a6a1f1dSLionel Sambuc #endif
49f789fee2SBen Gras
50f789fee2SBen Gras #include <sys/types.h>
51f789fee2SBen Gras #include <sys/stat.h>
52f789fee2SBen Gras #include <sys/ioctl.h>
53f789fee2SBen Gras #include <sys/uio.h>
5484d9c625SLionel Sambuc
55f789fee2SBen Gras #include <ctype.h>
56*0a6a1f1dSLionel Sambuc #include <err.h>
57f789fee2SBen Gras #include <errno.h>
58f789fee2SBen Gras #include <fcntl.h>
59f789fee2SBen Gras #include <limits.h>
60f789fee2SBen Gras #include <regex.h>
61f789fee2SBen Gras #include <stdio.h>
62f789fee2SBen Gras #include <stdlib.h>
63f789fee2SBen Gras #include <string.h>
64f789fee2SBen Gras #include <unistd.h>
65*0a6a1f1dSLionel Sambuc #include <wchar.h>
66*0a6a1f1dSLionel Sambuc #include <wctype.h>
67f789fee2SBen Gras
68f789fee2SBen Gras #include "defs.h"
69f789fee2SBen Gras #include "extern.h"
70f789fee2SBen Gras
71*0a6a1f1dSLionel Sambuc static SPACE HS, PS, SS, YS;
72f789fee2SBen Gras #define pd PS.deleted
73f789fee2SBen Gras #define ps PS.space
74f789fee2SBen Gras #define psl PS.len
75f789fee2SBen Gras #define hs HS.space
76f789fee2SBen Gras #define hsl HS.len
77f789fee2SBen Gras
78*0a6a1f1dSLionel Sambuc static __inline int applies(struct s_command *);
79*0a6a1f1dSLionel Sambuc static void do_tr(struct s_tr *);
80f789fee2SBen Gras static void flush_appends(void);
81*0a6a1f1dSLionel Sambuc static void lputs(char *, size_t);
82*0a6a1f1dSLionel Sambuc static __inline int regexec_e(regex_t *, const char *, int, int, size_t);
83f789fee2SBen Gras static void regsub(SPACE *, char *, char *);
84f789fee2SBen Gras static int substitute(struct s_command *);
85f789fee2SBen Gras
86f789fee2SBen Gras struct s_appends *appends; /* Array of pointers to strings to append. */
87*0a6a1f1dSLionel Sambuc static size_t appendx; /* Index into appends array. */
88*0a6a1f1dSLionel Sambuc size_t appendnum; /* Size of appends array. */
89f789fee2SBen Gras
90f789fee2SBen Gras static int lastaddr; /* Set by applies if last address of a range. */
91f789fee2SBen Gras static int sdone; /* If any substitutes since last line input. */
92f789fee2SBen Gras /* Iov structure for 'w' commands. */
93f789fee2SBen Gras static regex_t *defpreg;
94f789fee2SBen Gras size_t maxnsub;
95f789fee2SBen Gras regmatch_t *match;
96f789fee2SBen Gras
97*0a6a1f1dSLionel Sambuc #define OUT() do {fwrite(ps, 1, psl, outfile); fputc('\n', outfile);} while (0)
98f789fee2SBen Gras
99f789fee2SBen Gras void
process(void)100f789fee2SBen Gras process(void)
101f789fee2SBen Gras {
102f789fee2SBen Gras struct s_command *cp;
103f789fee2SBen Gras SPACE tspace;
104*0a6a1f1dSLionel Sambuc size_t oldpsl = 0;
105f789fee2SBen Gras char *p;
106f789fee2SBen Gras
107*0a6a1f1dSLionel Sambuc p = NULL;
108*0a6a1f1dSLionel Sambuc
109f789fee2SBen Gras for (linenum = 0; mf_fgets(&PS, REPLACE);) {
110f789fee2SBen Gras pd = 0;
111f789fee2SBen Gras top:
112f789fee2SBen Gras cp = prog;
113f789fee2SBen Gras redirect:
114f789fee2SBen Gras while (cp != NULL) {
115f789fee2SBen Gras if (!applies(cp)) {
116f789fee2SBen Gras cp = cp->next;
117f789fee2SBen Gras continue;
118f789fee2SBen Gras }
119f789fee2SBen Gras switch (cp->code) {
120f789fee2SBen Gras case '{':
121f789fee2SBen Gras cp = cp->u.c;
122f789fee2SBen Gras goto redirect;
123f789fee2SBen Gras case 'a':
124*0a6a1f1dSLionel Sambuc if (appendx >= appendnum)
125f789fee2SBen Gras appends = xrealloc(appends,
126f789fee2SBen Gras sizeof(struct s_appends) *
127*0a6a1f1dSLionel Sambuc (appendnum *= 2));
128f789fee2SBen Gras appends[appendx].type = AP_STRING;
129f789fee2SBen Gras appends[appendx].s = cp->t;
130f789fee2SBen Gras appends[appendx].len = strlen(cp->t);
131f789fee2SBen Gras appendx++;
132f789fee2SBen Gras break;
133f789fee2SBen Gras case 'b':
134f789fee2SBen Gras cp = cp->u.c;
135f789fee2SBen Gras goto redirect;
136f789fee2SBen Gras case 'c':
137f789fee2SBen Gras pd = 1;
138f789fee2SBen Gras psl = 0;
139*0a6a1f1dSLionel Sambuc if (cp->a2 == NULL || lastaddr || lastline())
140*0a6a1f1dSLionel Sambuc (void)fprintf(outfile, "%s", cp->t);
14184d9c625SLionel Sambuc goto new;
142f789fee2SBen Gras case 'd':
143f789fee2SBen Gras pd = 1;
144f789fee2SBen Gras goto new;
145f789fee2SBen Gras case 'D':
146f789fee2SBen Gras if (pd)
147f789fee2SBen Gras goto new;
148*0a6a1f1dSLionel Sambuc if (psl == 0 ||
149*0a6a1f1dSLionel Sambuc (p = memchr(ps, '\n', psl - 1)) == NULL) {
150f789fee2SBen Gras pd = 1;
151f789fee2SBen Gras goto new;
152f789fee2SBen Gras } else {
153*0a6a1f1dSLionel Sambuc psl -= (size_t)((p + 1) - ps);
154f789fee2SBen Gras memmove(ps, p + 1, psl);
155f789fee2SBen Gras goto top;
156f789fee2SBen Gras }
157f789fee2SBen Gras case 'g':
158f789fee2SBen Gras cspace(&PS, hs, hsl, REPLACE);
159f789fee2SBen Gras break;
160f789fee2SBen Gras case 'G':
161*0a6a1f1dSLionel Sambuc cspace(&PS, "\n", 1, APPEND);
162*0a6a1f1dSLionel Sambuc cspace(&PS, hs, hsl, APPEND);
163f789fee2SBen Gras break;
164f789fee2SBen Gras case 'h':
165f789fee2SBen Gras cspace(&HS, ps, psl, REPLACE);
166f789fee2SBen Gras break;
167f789fee2SBen Gras case 'H':
168*0a6a1f1dSLionel Sambuc cspace(&HS, "\n", 1, APPEND);
169*0a6a1f1dSLionel Sambuc cspace(&HS, ps, psl, APPEND);
170f789fee2SBen Gras break;
171f789fee2SBen Gras case 'i':
172*0a6a1f1dSLionel Sambuc (void)fprintf(outfile, "%s", cp->t);
173f789fee2SBen Gras break;
174f789fee2SBen Gras case 'l':
175*0a6a1f1dSLionel Sambuc lputs(ps, psl);
176f789fee2SBen Gras break;
177f789fee2SBen Gras case 'n':
178f789fee2SBen Gras if (!nflag && !pd)
179*0a6a1f1dSLionel Sambuc OUT();
180f789fee2SBen Gras flush_appends();
181f789fee2SBen Gras if (!mf_fgets(&PS, REPLACE))
182f789fee2SBen Gras exit(0);
183f789fee2SBen Gras pd = 0;
184f789fee2SBen Gras break;
185f789fee2SBen Gras case 'N':
186f789fee2SBen Gras flush_appends();
187*0a6a1f1dSLionel Sambuc cspace(&PS, "\n", 1, APPEND);
188*0a6a1f1dSLionel Sambuc if (!mf_fgets(&PS, APPEND))
189f789fee2SBen Gras exit(0);
190f789fee2SBen Gras break;
191f789fee2SBen Gras case 'p':
192f789fee2SBen Gras if (pd)
193f789fee2SBen Gras break;
194*0a6a1f1dSLionel Sambuc OUT();
195f789fee2SBen Gras break;
196f789fee2SBen Gras case 'P':
197f789fee2SBen Gras if (pd)
198f789fee2SBen Gras break;
199f789fee2SBen Gras if ((p = memchr(ps, '\n', psl - 1)) != NULL) {
200f789fee2SBen Gras oldpsl = psl;
201*0a6a1f1dSLionel Sambuc psl = (size_t)(p - ps);
202f789fee2SBen Gras }
203*0a6a1f1dSLionel Sambuc OUT();
204f789fee2SBen Gras if (p != NULL)
205f789fee2SBen Gras psl = oldpsl;
206f789fee2SBen Gras break;
207f789fee2SBen Gras case 'q':
208f789fee2SBen Gras if (!nflag && !pd)
209*0a6a1f1dSLionel Sambuc OUT();
210f789fee2SBen Gras flush_appends();
211f789fee2SBen Gras exit(0);
212f789fee2SBen Gras case 'r':
213*0a6a1f1dSLionel Sambuc if (appendx >= appendnum)
214f789fee2SBen Gras appends = xrealloc(appends,
215f789fee2SBen Gras sizeof(struct s_appends) *
216*0a6a1f1dSLionel Sambuc (appendnum *= 2));
217f789fee2SBen Gras appends[appendx].type = AP_FILE;
218f789fee2SBen Gras appends[appendx].s = cp->t;
219f789fee2SBen Gras appends[appendx].len = strlen(cp->t);
220f789fee2SBen Gras appendx++;
221f789fee2SBen Gras break;
222f789fee2SBen Gras case 's':
223f789fee2SBen Gras sdone |= substitute(cp);
224f789fee2SBen Gras break;
225f789fee2SBen Gras case 't':
226f789fee2SBen Gras if (sdone) {
227f789fee2SBen Gras sdone = 0;
228f789fee2SBen Gras cp = cp->u.c;
229f789fee2SBen Gras goto redirect;
230f789fee2SBen Gras }
231f789fee2SBen Gras break;
232f789fee2SBen Gras case 'w':
233f789fee2SBen Gras if (pd)
234f789fee2SBen Gras break;
235f789fee2SBen Gras if (cp->u.fd == -1 && (cp->u.fd = open(cp->t,
236f789fee2SBen Gras O_WRONLY|O_APPEND|O_CREAT|O_TRUNC,
237f789fee2SBen Gras DEFFILEMODE)) == -1)
238*0a6a1f1dSLionel Sambuc err(1, "%s", cp->t);
239*0a6a1f1dSLionel Sambuc if (write(cp->u.fd, ps, psl) != (ssize_t)psl ||
240*0a6a1f1dSLionel Sambuc write(cp->u.fd, "\n", 1) != 1)
241*0a6a1f1dSLionel Sambuc err(1, "%s", cp->t);
242f789fee2SBen Gras break;
243f789fee2SBen Gras case 'x':
244*0a6a1f1dSLionel Sambuc /*
245*0a6a1f1dSLionel Sambuc * If the hold space is null, make it empty
246*0a6a1f1dSLionel Sambuc * but not null. Otherwise the pattern space
247*0a6a1f1dSLionel Sambuc * will become null after the swap, which is
248*0a6a1f1dSLionel Sambuc * an abnormal condition.
249*0a6a1f1dSLionel Sambuc */
250f789fee2SBen Gras if (hs == NULL)
251*0a6a1f1dSLionel Sambuc cspace(&HS, "", 0, REPLACE);
252f789fee2SBen Gras tspace = PS;
253f789fee2SBen Gras PS = HS;
254f789fee2SBen Gras HS = tspace;
255f789fee2SBen Gras break;
256f789fee2SBen Gras case 'y':
257*0a6a1f1dSLionel Sambuc if (pd || psl == 0)
258f789fee2SBen Gras break;
259*0a6a1f1dSLionel Sambuc do_tr(cp->u.y);
260f789fee2SBen Gras break;
261f789fee2SBen Gras case ':':
262f789fee2SBen Gras case '}':
263f789fee2SBen Gras break;
264f789fee2SBen Gras case '=':
265*0a6a1f1dSLionel Sambuc (void)fprintf(outfile, "%lu\n", linenum);
266f789fee2SBen Gras }
267f789fee2SBen Gras cp = cp->next;
268f789fee2SBen Gras } /* for all cp */
269f789fee2SBen Gras
270f789fee2SBen Gras new: if (!nflag && !pd)
271*0a6a1f1dSLionel Sambuc OUT();
272f789fee2SBen Gras flush_appends();
273f789fee2SBen Gras } /* for all lines */
274f789fee2SBen Gras }
275f789fee2SBen Gras
276f789fee2SBen Gras /*
277f789fee2SBen Gras * TRUE if the address passed matches the current program state
278f789fee2SBen Gras * (lastline, linenumber, ps).
279f789fee2SBen Gras */
280f789fee2SBen Gras #define MATCH(a) \
281*0a6a1f1dSLionel Sambuc ((a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) : \
282*0a6a1f1dSLionel Sambuc (a)->type == AT_LINE ? linenum == (a)->u.l : lastline())
283f789fee2SBen Gras
284f789fee2SBen Gras /*
285*0a6a1f1dSLionel Sambuc * Return TRUE if the command applies to the current line. Sets the start
286*0a6a1f1dSLionel Sambuc * line for process ranges. Interprets the non-select (``!'') flag.
287f789fee2SBen Gras */
288*0a6a1f1dSLionel Sambuc static __inline int
applies(struct s_command * cp)289f789fee2SBen Gras applies(struct s_command *cp)
290f789fee2SBen Gras {
291f789fee2SBen Gras int r;
292f789fee2SBen Gras
293f789fee2SBen Gras lastaddr = 0;
294f789fee2SBen Gras if (cp->a1 == NULL && cp->a2 == NULL)
295f789fee2SBen Gras r = 1;
296*0a6a1f1dSLionel Sambuc else if (cp->a2)
297*0a6a1f1dSLionel Sambuc if (cp->startline > 0) {
298*0a6a1f1dSLionel Sambuc switch (cp->a2->type) {
299*0a6a1f1dSLionel Sambuc case AT_RELLINE:
300*0a6a1f1dSLionel Sambuc if (linenum - cp->startline <= cp->a2->u.l)
301f789fee2SBen Gras r = 1;
302*0a6a1f1dSLionel Sambuc else {
303*0a6a1f1dSLionel Sambuc cp->startline = 0;
304*0a6a1f1dSLionel Sambuc r = 0;
305*0a6a1f1dSLionel Sambuc }
306*0a6a1f1dSLionel Sambuc break;
307*0a6a1f1dSLionel Sambuc default:
308*0a6a1f1dSLionel Sambuc if (MATCH(cp->a2)) {
309*0a6a1f1dSLionel Sambuc cp->startline = 0;
310*0a6a1f1dSLionel Sambuc lastaddr = 1;
311*0a6a1f1dSLionel Sambuc r = 1;
312*0a6a1f1dSLionel Sambuc } else if (cp->a2->type == AT_LINE &&
313*0a6a1f1dSLionel Sambuc linenum > cp->a2->u.l) {
314*0a6a1f1dSLionel Sambuc /*
315*0a6a1f1dSLionel Sambuc * We missed the 2nd address due to a
316*0a6a1f1dSLionel Sambuc * branch, so just close the range and
317*0a6a1f1dSLionel Sambuc * return false.
318*0a6a1f1dSLionel Sambuc */
319*0a6a1f1dSLionel Sambuc cp->startline = 0;
320*0a6a1f1dSLionel Sambuc r = 0;
321*0a6a1f1dSLionel Sambuc } else
322*0a6a1f1dSLionel Sambuc r = 1;
323*0a6a1f1dSLionel Sambuc }
324f789fee2SBen Gras } else if (cp->a1 && MATCH(cp->a1)) {
325f789fee2SBen Gras /*
326f789fee2SBen Gras * If the second address is a number less than or
327f789fee2SBen Gras * equal to the line number first selected, only
328f789fee2SBen Gras * one line shall be selected.
329f789fee2SBen Gras * -- POSIX 1003.2
330*0a6a1f1dSLionel Sambuc * Likewise if the relative second line address is zero.
331f789fee2SBen Gras */
332*0a6a1f1dSLionel Sambuc if ((cp->a2->type == AT_LINE &&
333*0a6a1f1dSLionel Sambuc linenum >= cp->a2->u.l) ||
334*0a6a1f1dSLionel Sambuc (cp->a2->type == AT_RELLINE && cp->a2->u.l == 0))
335f789fee2SBen Gras lastaddr = 1;
336*0a6a1f1dSLionel Sambuc else {
337*0a6a1f1dSLionel Sambuc cp->startline = linenum;
338*0a6a1f1dSLionel Sambuc }
339f789fee2SBen Gras r = 1;
340f789fee2SBen Gras } else
341f789fee2SBen Gras r = 0;
342*0a6a1f1dSLionel Sambuc else
343f789fee2SBen Gras r = MATCH(cp->a1);
344f789fee2SBen Gras return (cp->nonsel ? ! r : r);
345f789fee2SBen Gras }
346f789fee2SBen Gras
347f789fee2SBen Gras /*
348*0a6a1f1dSLionel Sambuc * Reset the sed processor to its initial state.
349*0a6a1f1dSLionel Sambuc */
350*0a6a1f1dSLionel Sambuc void
resetstate(void)351*0a6a1f1dSLionel Sambuc resetstate(void)
352*0a6a1f1dSLionel Sambuc {
353*0a6a1f1dSLionel Sambuc struct s_command *cp;
354*0a6a1f1dSLionel Sambuc
355*0a6a1f1dSLionel Sambuc /*
356*0a6a1f1dSLionel Sambuc * Reset all in-range markers.
357*0a6a1f1dSLionel Sambuc */
358*0a6a1f1dSLionel Sambuc for (cp = prog; cp; cp = cp->code == '{' ? cp->u.c : cp->next)
359*0a6a1f1dSLionel Sambuc if (cp->a2)
360*0a6a1f1dSLionel Sambuc cp->startline = 0;
361*0a6a1f1dSLionel Sambuc
362*0a6a1f1dSLionel Sambuc /*
363*0a6a1f1dSLionel Sambuc * Clear out the hold space.
364*0a6a1f1dSLionel Sambuc */
365*0a6a1f1dSLionel Sambuc cspace(&HS, "", 0, REPLACE);
366*0a6a1f1dSLionel Sambuc }
367*0a6a1f1dSLionel Sambuc
368*0a6a1f1dSLionel Sambuc /*
369f789fee2SBen Gras * substitute --
370f789fee2SBen Gras * Do substitutions in the pattern space. Currently, we build a
371f789fee2SBen Gras * copy of the new pattern space in the substitute space structure
372f789fee2SBen Gras * and then swap them.
373f789fee2SBen Gras */
374f789fee2SBen Gras static int
substitute(struct s_command * cp)375f789fee2SBen Gras substitute(struct s_command *cp)
376f789fee2SBen Gras {
377f789fee2SBen Gras SPACE tspace;
378f789fee2SBen Gras regex_t *re;
379*0a6a1f1dSLionel Sambuc regoff_t re_off, slen;
380f789fee2SBen Gras int lastempty, n;
381f789fee2SBen Gras char *s;
382f789fee2SBen Gras
383f789fee2SBen Gras s = ps;
384f789fee2SBen Gras re = cp->u.s->re;
385f789fee2SBen Gras if (re == NULL) {
386*0a6a1f1dSLionel Sambuc if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) {
387f789fee2SBen Gras linenum = cp->u.s->linenum;
388*0a6a1f1dSLionel Sambuc errx(1, "%lu: %s: \\%u not defined in the RE",
389*0a6a1f1dSLionel Sambuc linenum, fname, cp->u.s->maxbref);
390f789fee2SBen Gras }
391f789fee2SBen Gras }
392f789fee2SBen Gras if (!regexec_e(re, s, 0, 0, psl))
393f789fee2SBen Gras return (0);
394f789fee2SBen Gras
395f789fee2SBen Gras SS.len = 0; /* Clean substitute space. */
396*0a6a1f1dSLionel Sambuc slen = (regoff_t)psl;
397f789fee2SBen Gras n = cp->u.s->n;
398f789fee2SBen Gras lastempty = 1;
399f789fee2SBen Gras
400f789fee2SBen Gras switch (n) {
401f789fee2SBen Gras case 0: /* Global */
402f789fee2SBen Gras do {
403f789fee2SBen Gras if (lastempty || match[0].rm_so != match[0].rm_eo) {
404f789fee2SBen Gras /* Locate start of replaced string. */
405f789fee2SBen Gras re_off = match[0].rm_so;
406f789fee2SBen Gras /* Copy leading retained string. */
407*0a6a1f1dSLionel Sambuc cspace(&SS, s, (size_t)re_off, APPEND);
408f789fee2SBen Gras /* Add in regular expression. */
409f789fee2SBen Gras regsub(&SS, s, cp->u.s->new);
410f789fee2SBen Gras }
411f789fee2SBen Gras
412f789fee2SBen Gras /* Move past this match. */
413f789fee2SBen Gras if (match[0].rm_so != match[0].rm_eo) {
414f789fee2SBen Gras s += match[0].rm_eo;
415f789fee2SBen Gras slen -= match[0].rm_eo;
416f789fee2SBen Gras lastempty = 0;
417f789fee2SBen Gras } else {
418*0a6a1f1dSLionel Sambuc if (match[0].rm_so < slen)
419*0a6a1f1dSLionel Sambuc cspace(&SS, s + match[0].rm_so, 1,
420*0a6a1f1dSLionel Sambuc APPEND);
421f789fee2SBen Gras s += match[0].rm_so + 1;
422f789fee2SBen Gras slen -= match[0].rm_so + 1;
423f789fee2SBen Gras lastempty = 1;
424f789fee2SBen Gras }
425*0a6a1f1dSLionel Sambuc } while (slen >= 0 && regexec_e(re, s, REG_NOTBOL, 0, (size_t)slen));
426f789fee2SBen Gras /* Copy trailing retained string. */
427f789fee2SBen Gras if (slen > 0)
428*0a6a1f1dSLionel Sambuc cspace(&SS, s, (size_t)slen, APPEND);
429f789fee2SBen Gras break;
430f789fee2SBen Gras default: /* Nth occurrence */
431f789fee2SBen Gras while (--n) {
432*0a6a1f1dSLionel Sambuc if (match[0].rm_eo == match[0].rm_so)
433*0a6a1f1dSLionel Sambuc match[0].rm_eo = match[0].rm_so + 1;
434f789fee2SBen Gras s += match[0].rm_eo;
435f789fee2SBen Gras slen -= match[0].rm_eo;
436*0a6a1f1dSLionel Sambuc if (slen < 0)
437*0a6a1f1dSLionel Sambuc return (0);
438*0a6a1f1dSLionel Sambuc if (!regexec_e(re, s, REG_NOTBOL, 0, (size_t)slen))
439f789fee2SBen Gras return (0);
440f789fee2SBen Gras }
441f789fee2SBen Gras /* FALLTHROUGH */
442f789fee2SBen Gras case 1: /* 1st occurrence */
443f789fee2SBen Gras /* Locate start of replaced string. */
444f789fee2SBen Gras re_off = match[0].rm_so + (s - ps);
445f789fee2SBen Gras /* Copy leading retained string. */
446*0a6a1f1dSLionel Sambuc cspace(&SS, ps, (size_t)re_off, APPEND);
447f789fee2SBen Gras /* Add in regular expression. */
448f789fee2SBen Gras regsub(&SS, s, cp->u.s->new);
449f789fee2SBen Gras /* Copy trailing retained string. */
450f789fee2SBen Gras s += match[0].rm_eo;
451f789fee2SBen Gras slen -= match[0].rm_eo;
452*0a6a1f1dSLionel Sambuc cspace(&SS, s, (size_t)slen, APPEND);
453f789fee2SBen Gras break;
454f789fee2SBen Gras }
455f789fee2SBen Gras
456f789fee2SBen Gras /*
457f789fee2SBen Gras * Swap the substitute space and the pattern space, and make sure
458f789fee2SBen Gras * that any leftover pointers into stdio memory get lost.
459f789fee2SBen Gras */
460f789fee2SBen Gras tspace = PS;
461f789fee2SBen Gras PS = SS;
462f789fee2SBen Gras SS = tspace;
463f789fee2SBen Gras SS.space = SS.back;
464f789fee2SBen Gras
465f789fee2SBen Gras /* Handle the 'p' flag. */
466f789fee2SBen Gras if (cp->u.s->p)
467*0a6a1f1dSLionel Sambuc OUT();
468f789fee2SBen Gras
469f789fee2SBen Gras /* Handle the 'w' flag. */
470f789fee2SBen Gras if (cp->u.s->wfile && !pd) {
471f789fee2SBen Gras if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile,
472f789fee2SBen Gras O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1)
473*0a6a1f1dSLionel Sambuc err(1, "%s", cp->u.s->wfile);
474*0a6a1f1dSLionel Sambuc if (write(cp->u.s->wfd, ps, psl) != (ssize_t)psl ||
475*0a6a1f1dSLionel Sambuc write(cp->u.s->wfd, "\n", 1) != 1)
476*0a6a1f1dSLionel Sambuc err(1, "%s", cp->u.s->wfile);
477f789fee2SBen Gras }
478f789fee2SBen Gras return (1);
479f789fee2SBen Gras }
480f789fee2SBen Gras
481f789fee2SBen Gras /*
482*0a6a1f1dSLionel Sambuc * do_tr --
483*0a6a1f1dSLionel Sambuc * Perform translation ('y' command) in the pattern space.
484*0a6a1f1dSLionel Sambuc */
485*0a6a1f1dSLionel Sambuc static void
do_tr(struct s_tr * y)486*0a6a1f1dSLionel Sambuc do_tr(struct s_tr *y)
487*0a6a1f1dSLionel Sambuc {
488*0a6a1f1dSLionel Sambuc SPACE tmp;
489*0a6a1f1dSLionel Sambuc char c, *p;
490*0a6a1f1dSLionel Sambuc size_t clen, left;
491*0a6a1f1dSLionel Sambuc size_t i;
492*0a6a1f1dSLionel Sambuc
493*0a6a1f1dSLionel Sambuc if (MB_CUR_MAX == 1) {
494*0a6a1f1dSLionel Sambuc /*
495*0a6a1f1dSLionel Sambuc * Single-byte encoding: perform in-place translation
496*0a6a1f1dSLionel Sambuc * of the pattern space.
497*0a6a1f1dSLionel Sambuc */
498*0a6a1f1dSLionel Sambuc for (p = ps; p < &ps[psl]; p++)
499*0a6a1f1dSLionel Sambuc *p = (char)y->bytetab[(u_char)*p];
500*0a6a1f1dSLionel Sambuc } else {
501*0a6a1f1dSLionel Sambuc /*
502*0a6a1f1dSLionel Sambuc * Multi-byte encoding: perform translation into the
503*0a6a1f1dSLionel Sambuc * translation space, then swap the translation and
504*0a6a1f1dSLionel Sambuc * pattern spaces.
505*0a6a1f1dSLionel Sambuc */
506*0a6a1f1dSLionel Sambuc /* Clean translation space. */
507*0a6a1f1dSLionel Sambuc YS.len = 0;
508*0a6a1f1dSLionel Sambuc for (p = ps, left = psl; left > 0; p += clen, left -= clen) {
509*0a6a1f1dSLionel Sambuc if ((c = (char)y->bytetab[(u_char)*p]) != '\0') {
510*0a6a1f1dSLionel Sambuc cspace(&YS, &c, 1, APPEND);
511*0a6a1f1dSLionel Sambuc clen = 1;
512*0a6a1f1dSLionel Sambuc continue;
513*0a6a1f1dSLionel Sambuc }
514*0a6a1f1dSLionel Sambuc for (i = 0; i < y->nmultis; i++)
515*0a6a1f1dSLionel Sambuc if (left >= y->multis[i].fromlen &&
516*0a6a1f1dSLionel Sambuc memcmp(p, y->multis[i].from,
517*0a6a1f1dSLionel Sambuc y->multis[i].fromlen) == 0)
518*0a6a1f1dSLionel Sambuc break;
519*0a6a1f1dSLionel Sambuc if (i < y->nmultis) {
520*0a6a1f1dSLionel Sambuc cspace(&YS, y->multis[i].to,
521*0a6a1f1dSLionel Sambuc y->multis[i].tolen, APPEND);
522*0a6a1f1dSLionel Sambuc clen = y->multis[i].fromlen;
523*0a6a1f1dSLionel Sambuc } else {
524*0a6a1f1dSLionel Sambuc cspace(&YS, p, 1, APPEND);
525*0a6a1f1dSLionel Sambuc clen = 1;
526*0a6a1f1dSLionel Sambuc }
527*0a6a1f1dSLionel Sambuc }
528*0a6a1f1dSLionel Sambuc /* Swap the translation space and the pattern space. */
529*0a6a1f1dSLionel Sambuc tmp = PS;
530*0a6a1f1dSLionel Sambuc PS = YS;
531*0a6a1f1dSLionel Sambuc YS = tmp;
532*0a6a1f1dSLionel Sambuc YS.space = YS.back;
533*0a6a1f1dSLionel Sambuc }
534*0a6a1f1dSLionel Sambuc }
535*0a6a1f1dSLionel Sambuc
536*0a6a1f1dSLionel Sambuc /*
537f789fee2SBen Gras * Flush append requests. Always called before reading a line,
538f789fee2SBen Gras * therefore it also resets the substitution done (sdone) flag.
539f789fee2SBen Gras */
540f789fee2SBen Gras static void
flush_appends(void)541f789fee2SBen Gras flush_appends(void)
542f789fee2SBen Gras {
543f789fee2SBen Gras FILE *f;
544*0a6a1f1dSLionel Sambuc size_t count, i;
545f789fee2SBen Gras char buf[8 * 1024];
546f789fee2SBen Gras
547f789fee2SBen Gras for (i = 0; i < appendx; i++)
548f789fee2SBen Gras switch (appends[i].type) {
549f789fee2SBen Gras case AP_STRING:
550f789fee2SBen Gras fwrite(appends[i].s, sizeof(char), appends[i].len,
551*0a6a1f1dSLionel Sambuc outfile);
552f789fee2SBen Gras break;
553f789fee2SBen Gras case AP_FILE:
554f789fee2SBen Gras /*
555f789fee2SBen Gras * Read files probably shouldn't be cached. Since
556f789fee2SBen Gras * it's not an error to read a non-existent file,
557f789fee2SBen Gras * it's possible that another program is interacting
558f789fee2SBen Gras * with the sed script through the filesystem. It
559f789fee2SBen Gras * would be truly bizarre, but possible. It's probably
560f789fee2SBen Gras * not that big a performance win, anyhow.
561f789fee2SBen Gras */
562f789fee2SBen Gras if ((f = fopen(appends[i].s, "r")) == NULL)
563f789fee2SBen Gras break;
564*0a6a1f1dSLionel Sambuc while ((count = fread(buf, sizeof(char), sizeof(buf), f)))
565*0a6a1f1dSLionel Sambuc (void)fwrite(buf, sizeof(char), count, outfile);
566f789fee2SBen Gras (void)fclose(f);
567f789fee2SBen Gras break;
568f789fee2SBen Gras }
569*0a6a1f1dSLionel Sambuc if (ferror(outfile))
570*0a6a1f1dSLionel Sambuc errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO));
571*0a6a1f1dSLionel Sambuc appendx = 0;
572*0a6a1f1dSLionel Sambuc sdone = 0;
573f789fee2SBen Gras }
574f789fee2SBen Gras
575f789fee2SBen Gras static void
lputs(char * s,size_t len)576*0a6a1f1dSLionel Sambuc lputs(char *s, size_t len)
577f789fee2SBen Gras {
578*0a6a1f1dSLionel Sambuc static const char escapes[] = "\\\a\b\f\r\t\v";
579*0a6a1f1dSLionel Sambuc int c;
580*0a6a1f1dSLionel Sambuc size_t col, width;
581*0a6a1f1dSLionel Sambuc const char *p;
582*0a6a1f1dSLionel Sambuc #ifdef TIOCGWINSZ
583f789fee2SBen Gras struct winsize win;
584f789fee2SBen Gras #endif
585*0a6a1f1dSLionel Sambuc static size_t termwidth = (size_t)-1;
586*0a6a1f1dSLionel Sambuc size_t clen, i;
587*0a6a1f1dSLionel Sambuc wchar_t wc;
588*0a6a1f1dSLionel Sambuc mbstate_t mbs;
589f789fee2SBen Gras
590*0a6a1f1dSLionel Sambuc if (outfile != stdout)
591*0a6a1f1dSLionel Sambuc termwidth = 60;
592*0a6a1f1dSLionel Sambuc if (termwidth == (size_t)-1) {
593*0a6a1f1dSLionel Sambuc if ((p = getenv("COLUMNS")) && *p != '\0')
594*0a6a1f1dSLionel Sambuc termwidth = (size_t)atoi(p);
595*0a6a1f1dSLionel Sambuc #ifdef TIOCGWINSZ
596f789fee2SBen Gras else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 &&
597f789fee2SBen Gras win.ws_col > 0)
598f789fee2SBen Gras termwidth = win.ws_col;
599f789fee2SBen Gras #endif
600f789fee2SBen Gras else
601f789fee2SBen Gras termwidth = 60;
602f789fee2SBen Gras }
603*0a6a1f1dSLionel Sambuc if (termwidth == 0)
604*0a6a1f1dSLionel Sambuc termwidth = 1;
605*0a6a1f1dSLionel Sambuc
606*0a6a1f1dSLionel Sambuc memset(&mbs, 0, sizeof(mbs));
607*0a6a1f1dSLionel Sambuc col = 0;
608*0a6a1f1dSLionel Sambuc while (len != 0) {
609*0a6a1f1dSLionel Sambuc clen = mbrtowc(&wc, s, len, &mbs);
610*0a6a1f1dSLionel Sambuc if (clen == 0)
611*0a6a1f1dSLionel Sambuc clen = 1;
612*0a6a1f1dSLionel Sambuc if (clen == (size_t)-1 || clen == (size_t)-2) {
613*0a6a1f1dSLionel Sambuc wc = (unsigned char)*s;
614*0a6a1f1dSLionel Sambuc clen = 1;
615*0a6a1f1dSLionel Sambuc memset(&mbs, 0, sizeof(mbs));
616f789fee2SBen Gras }
617*0a6a1f1dSLionel Sambuc if (wc == '\n') {
618*0a6a1f1dSLionel Sambuc if (col + 1 >= termwidth)
619*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\\n");
620*0a6a1f1dSLionel Sambuc fputc('$', outfile);
621*0a6a1f1dSLionel Sambuc fputc('\n', outfile);
622*0a6a1f1dSLionel Sambuc col = 0;
623*0a6a1f1dSLionel Sambuc } else if (iswprint(wc)) {
624*0a6a1f1dSLionel Sambuc width = (size_t)wcwidth(wc);
625*0a6a1f1dSLionel Sambuc if (col + width >= termwidth) {
626*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\\n");
627*0a6a1f1dSLionel Sambuc col = 0;
628*0a6a1f1dSLionel Sambuc }
629*0a6a1f1dSLionel Sambuc fwrite(s, 1, clen, outfile);
630*0a6a1f1dSLionel Sambuc col += width;
631*0a6a1f1dSLionel Sambuc } else if (wc != L'\0' && (c = wctob(wc)) != EOF &&
632*0a6a1f1dSLionel Sambuc (p = strchr(escapes, c)) != NULL) {
633*0a6a1f1dSLionel Sambuc if (col + 2 >= termwidth) {
634*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\\n");
635*0a6a1f1dSLionel Sambuc col = 0;
636*0a6a1f1dSLionel Sambuc }
637*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\%c", "\\abfrtv"[p - escapes]);
638*0a6a1f1dSLionel Sambuc col += 2;
639f789fee2SBen Gras } else {
640*0a6a1f1dSLionel Sambuc if (col + 4 * clen >= termwidth) {
641*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\\n");
642*0a6a1f1dSLionel Sambuc col = 0;
643f789fee2SBen Gras }
644*0a6a1f1dSLionel Sambuc for (i = 0; i < clen; i++)
645*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\%03o",
646*0a6a1f1dSLionel Sambuc (int)(unsigned char)s[i]);
647*0a6a1f1dSLionel Sambuc col += 4 * clen;
648f789fee2SBen Gras }
649*0a6a1f1dSLionel Sambuc s += clen;
650*0a6a1f1dSLionel Sambuc len -= clen;
651f789fee2SBen Gras }
652*0a6a1f1dSLionel Sambuc if (col + 1 >= termwidth)
653*0a6a1f1dSLionel Sambuc fprintf(outfile, "\\\n");
654*0a6a1f1dSLionel Sambuc (void)fputc('$', outfile);
655*0a6a1f1dSLionel Sambuc (void)fputc('\n', outfile);
656*0a6a1f1dSLionel Sambuc if (ferror(outfile))
657*0a6a1f1dSLionel Sambuc errx(1, "%s: %s", outfname, strerror(errno ? errno : EIO));
658f789fee2SBen Gras }
659f789fee2SBen Gras
660*0a6a1f1dSLionel Sambuc static __inline int
regexec_e(regex_t * preg,const char * string,int eflags,int nomatch,size_t slen)661*0a6a1f1dSLionel Sambuc regexec_e(regex_t *preg, const char *string, int eflags, int nomatch,
662*0a6a1f1dSLionel Sambuc size_t slen)
663f789fee2SBen Gras {
664f789fee2SBen Gras int eval;
665f789fee2SBen Gras #ifndef REG_STARTEND
666f789fee2SBen Gras char *buf;
667f789fee2SBen Gras #endif
668f789fee2SBen Gras
669f789fee2SBen Gras if (preg == NULL) {
670f789fee2SBen Gras if (defpreg == NULL)
671*0a6a1f1dSLionel Sambuc errx(1, "first RE may not be empty");
672f789fee2SBen Gras } else
673f789fee2SBen Gras defpreg = preg;
674f789fee2SBen Gras
675*0a6a1f1dSLionel Sambuc /* Set anchors */
676f789fee2SBen Gras #ifndef REG_STARTEND
677*0a6a1f1dSLionel Sambuc buf = xmalloc(slen + 1);
678f789fee2SBen Gras (void)memcpy(buf, string, slen);
679f789fee2SBen Gras buf[slen] = '\0';
680f789fee2SBen Gras eval = regexec(defpreg, buf,
681f789fee2SBen Gras nomatch ? 0 : maxnsub + 1, match, eflags);
682f789fee2SBen Gras free(buf);
683f789fee2SBen Gras #else
684f789fee2SBen Gras match[0].rm_so = 0;
685*0a6a1f1dSLionel Sambuc match[0].rm_eo = (regoff_t)slen;
686f789fee2SBen Gras eval = regexec(defpreg, string,
687f789fee2SBen Gras nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND);
688f789fee2SBen Gras #endif
689f789fee2SBen Gras switch(eval) {
690f789fee2SBen Gras case 0:
691f789fee2SBen Gras return (1);
692f789fee2SBen Gras case REG_NOMATCH:
693f789fee2SBen Gras return (0);
694f789fee2SBen Gras }
695*0a6a1f1dSLionel Sambuc errx(1, "RE error: %s", strregerror(eval, defpreg));
696f789fee2SBen Gras /* NOTREACHED */
697f789fee2SBen Gras }
698f789fee2SBen Gras
699f789fee2SBen Gras /*
700f789fee2SBen Gras * regsub - perform substitutions after a regexp match
701f789fee2SBen Gras * Based on a routine by Henry Spencer
702f789fee2SBen Gras */
703f789fee2SBen Gras static void
regsub(SPACE * sp,char * string,char * src)704f789fee2SBen Gras regsub(SPACE *sp, char *string, char *src)
705f789fee2SBen Gras {
706*0a6a1f1dSLionel Sambuc size_t len;
707*0a6a1f1dSLionel Sambuc int no;
708f789fee2SBen Gras char c, *dst;
709f789fee2SBen Gras
710f789fee2SBen Gras #define NEEDSP(reqlen) \
711*0a6a1f1dSLionel Sambuc /* XXX What is the +1 for? */ \
712f789fee2SBen Gras if (sp->len + (reqlen) + 1 >= sp->blen) { \
713*0a6a1f1dSLionel Sambuc sp->blen += (reqlen) + 1024; \
714*0a6a1f1dSLionel Sambuc sp->space = sp->back = xrealloc(sp->back, sp->blen); \
715f789fee2SBen Gras dst = sp->space + sp->len; \
716f789fee2SBen Gras }
717f789fee2SBen Gras
718f789fee2SBen Gras dst = sp->space + sp->len;
719f789fee2SBen Gras while ((c = *src++) != '\0') {
720f789fee2SBen Gras if (c == '&')
721f789fee2SBen Gras no = 0;
722f789fee2SBen Gras else if (c == '\\' && isdigit((unsigned char)*src))
723f789fee2SBen Gras no = *src++ - '0';
724f789fee2SBen Gras else
725f789fee2SBen Gras no = -1;
726f789fee2SBen Gras if (no < 0) { /* Ordinary character. */
727f789fee2SBen Gras if (c == '\\' && (*src == '\\' || *src == '&'))
728f789fee2SBen Gras c = *src++;
729f789fee2SBen Gras NEEDSP(1);
730f789fee2SBen Gras *dst++ = c;
731f789fee2SBen Gras ++sp->len;
732f789fee2SBen Gras } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) {
733*0a6a1f1dSLionel Sambuc len = (size_t)(match[no].rm_eo - match[no].rm_so);
734f789fee2SBen Gras NEEDSP(len);
735f789fee2SBen Gras memmove(dst, string + match[no].rm_so, len);
736f789fee2SBen Gras dst += len;
737f789fee2SBen Gras sp->len += len;
738f789fee2SBen Gras }
739f789fee2SBen Gras }
740f789fee2SBen Gras NEEDSP(1);
741f789fee2SBen Gras *dst = '\0';
742f789fee2SBen Gras }
743f789fee2SBen Gras
744f789fee2SBen Gras /*
745*0a6a1f1dSLionel Sambuc * cspace --
746*0a6a1f1dSLionel Sambuc * Concatenate space: append the source space to the destination space,
747*0a6a1f1dSLionel Sambuc * allocating new space as necessary.
748f789fee2SBen Gras */
749f789fee2SBen Gras void
cspace(SPACE * sp,const char * p,size_t len,enum e_spflag spflag)750f789fee2SBen Gras cspace(SPACE *sp, const char *p, size_t len, enum e_spflag spflag)
751f789fee2SBen Gras {
752f789fee2SBen Gras size_t tlen;
753f789fee2SBen Gras
754f789fee2SBen Gras /* Make sure SPACE has enough memory and ramp up quickly. */
755f789fee2SBen Gras tlen = sp->len + len + 1;
756f789fee2SBen Gras if (tlen > sp->blen) {
757*0a6a1f1dSLionel Sambuc sp->blen = tlen + 1024;
758*0a6a1f1dSLionel Sambuc sp->space = sp->back = xrealloc(sp->back, sp->blen);
759f789fee2SBen Gras }
760f789fee2SBen Gras
761f789fee2SBen Gras if (spflag == REPLACE)
762f789fee2SBen Gras sp->len = 0;
763f789fee2SBen Gras
764f789fee2SBen Gras memmove(sp->space + sp->len, p, len);
765f789fee2SBen Gras
766f789fee2SBen Gras sp->space[sp->len += len] = '\0';
767f789fee2SBen Gras }
768f789fee2SBen Gras
769f789fee2SBen Gras /*
770f789fee2SBen Gras * Close all cached opened files and report any errors
771f789fee2SBen Gras */
772f789fee2SBen Gras void
cfclose(struct s_command * cp,struct s_command * end)773f789fee2SBen Gras cfclose(struct s_command *cp, struct s_command *end)
774f789fee2SBen Gras {
775f789fee2SBen Gras
776f789fee2SBen Gras for (; cp != end; cp = cp->next)
777f789fee2SBen Gras switch(cp->code) {
778f789fee2SBen Gras case 's':
779f789fee2SBen Gras if (cp->u.s->wfd != -1 && close(cp->u.s->wfd))
780*0a6a1f1dSLionel Sambuc err(1, "%s", cp->u.s->wfile);
781f789fee2SBen Gras cp->u.s->wfd = -1;
782f789fee2SBen Gras break;
783f789fee2SBen Gras case 'w':
784f789fee2SBen Gras if (cp->u.fd != -1 && close(cp->u.fd))
785*0a6a1f1dSLionel Sambuc err(1, "%s", cp->t);
786f789fee2SBen Gras cp->u.fd = -1;
787f789fee2SBen Gras break;
788f789fee2SBen Gras case '{':
789f789fee2SBen Gras cfclose(cp->u.c, cp->next);
790f789fee2SBen Gras break;
791f789fee2SBen Gras }
792f789fee2SBen Gras }
793