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