155997Sbostic /*- 255997Sbostic * Copyright (c) 1992 Diomidis Spinellis. 355997Sbostic * Copyright (c) 1992 The Regents of the University of California. 455997Sbostic * All rights reserved. 555997Sbostic * 655997Sbostic * This code is derived from software contributed to Berkeley by 755997Sbostic * Diomidis Spinellis of Imperial College, University of London. 855997Sbostic * 955997Sbostic * %sccs.include.redist.c% 1055997Sbostic */ 1155997Sbostic 1255997Sbostic #ifndef lint 13*56657Sbostic static char sccsid[] = "@(#)process.c 5.9 (Berkeley) 11/01/92"; 1455997Sbostic #endif /* not lint */ 1555997Sbostic 1655997Sbostic #include <sys/types.h> 1755997Sbostic #include <sys/stat.h> 1855997Sbostic #include <sys/ioctl.h> 1955997Sbostic #include <sys/uio.h> 2055997Sbostic 2155997Sbostic #include <ctype.h> 2255997Sbostic #include <errno.h> 2355997Sbostic #include <fcntl.h> 2455997Sbostic #include <limits.h> 2555997Sbostic #include <regex.h> 2655997Sbostic #include <stdio.h> 2755997Sbostic #include <stdlib.h> 2855997Sbostic #include <string.h> 2955997Sbostic #include <unistd.h> 3055997Sbostic 3155997Sbostic #include "defs.h" 3255997Sbostic #include "extern.h" 3355997Sbostic 3455997Sbostic static SPACE HS, PS, SS; 3555997Sbostic #define pd PS.deleted 3655997Sbostic #define ps PS.space 3755997Sbostic #define psl PS.len 3855997Sbostic #define hs HS.space 3955997Sbostic #define hsl HS.len 4055997Sbostic 4155997Sbostic static inline int applies __P((struct s_command *)); 4255997Sbostic static void flush_appends __P((void)); 4355997Sbostic static void lputs __P((char *)); 4456079Sbostic static inline int regexec_e __P((regex_t *, const char *, int, int)); 4556079Sbostic static void regsub __P((SPACE *, char *, char *)); 4655997Sbostic static int substitute __P((struct s_command *)); 4755997Sbostic 4855997Sbostic struct s_appends *appends; /* Array of pointers to strings to append. */ 4955997Sbostic static int appendx; /* Index into appends array. */ 5055997Sbostic int appendnum; /* Size of appends array. */ 5155997Sbostic 5255997Sbostic static int lastaddr; /* Set by applies if last address of a range. */ 5355997Sbostic static int sdone; /* If any substitutes since last line input. */ 5455997Sbostic /* Iov structure for 'w' commands. */ 5555997Sbostic static struct iovec iov[2] = { NULL, 0, "\n", 1 }; 5655997Sbostic 5756019Sbostic static regex_t *defpreg; 5856079Sbostic size_t maxnsub; 5956079Sbostic regmatch_t *match; 6056019Sbostic 6155997Sbostic void 6255997Sbostic process() 6355997Sbostic { 6455997Sbostic struct s_command *cp; 6555997Sbostic SPACE tspace; 6655997Sbostic size_t len; 6756079Sbostic int r; 6855997Sbostic char oldc, *p; 6955997Sbostic 7056079Sbostic for (linenum = 0; mf_fgets(&PS, REPLACE);) { 7155997Sbostic pd = 0; 7255997Sbostic cp = prog; 7355997Sbostic redirect: 7455997Sbostic while (cp != NULL) { 7555997Sbostic if (!applies(cp)) { 7655997Sbostic cp = cp->next; 7755997Sbostic continue; 7855997Sbostic } 7955997Sbostic switch (cp->code) { 8055997Sbostic case '{': 8155997Sbostic cp = cp->u.c; 8255997Sbostic goto redirect; 8355997Sbostic case 'a': 8455997Sbostic if (appendx >= appendnum) 8555997Sbostic appends = xrealloc(appends, 8655997Sbostic sizeof(struct s_appends) * 8755997Sbostic (appendnum *= 2)); 8855997Sbostic appends[appendx].type = AP_STRING; 8955997Sbostic appends[appendx].s = cp->t; 9055997Sbostic appendx++; 9155997Sbostic break; 9255997Sbostic case 'b': 9355997Sbostic cp = cp->u.c; 9455997Sbostic goto redirect; 9555997Sbostic case 'c': 9655997Sbostic pd = 1; 9755997Sbostic psl = 0; 9855997Sbostic if (cp->a2 == NULL || lastaddr) 9955997Sbostic (void)printf("%s", cp->t); 10055997Sbostic break; 10155997Sbostic case 'd': 10256005Sbostic pd = 1; 10355997Sbostic goto new; 10455997Sbostic case 'D': 10555997Sbostic if (pd) 10655997Sbostic goto new; 10756005Sbostic if ((p = strchr(ps, '\n')) == NULL) 10856005Sbostic pd = 1; 10956005Sbostic else { 11055997Sbostic psl -= (p - ps) - 1; 11155997Sbostic memmove(ps, p + 1, psl); 11255997Sbostic } 11355997Sbostic goto new; 11455997Sbostic case 'g': 11556079Sbostic cspace(&PS, hs, hsl, REPLACE); 11655997Sbostic break; 11755997Sbostic case 'G': 11856079Sbostic cspace(&PS, hs, hsl, APPENDNL); 11955997Sbostic break; 12055997Sbostic case 'h': 12156079Sbostic cspace(&HS, ps, psl, REPLACE); 12255997Sbostic break; 12355997Sbostic case 'H': 12456079Sbostic cspace(&HS, ps, psl, APPENDNL); 12555997Sbostic break; 12655997Sbostic case 'i': 12755997Sbostic (void)printf("%s", cp->t); 12855997Sbostic break; 12955997Sbostic case 'l': 13055997Sbostic lputs(ps); 13155997Sbostic break; 13255997Sbostic case 'n': 13355997Sbostic if (!nflag && !pd) 13455997Sbostic (void)printf("%s\n", ps); 13555997Sbostic flush_appends(); 13656079Sbostic r = mf_fgets(&PS, REPLACE); 13755997Sbostic #ifdef HISTORIC_PRACTICE 13856079Sbostic if (!r) 13955997Sbostic exit(0); 14055997Sbostic #endif 14155997Sbostic pd = 0; 14255997Sbostic break; 14355997Sbostic case 'N': 14455997Sbostic flush_appends(); 14556079Sbostic if (!mf_fgets(&PS, APPENDNL)) { 14655997Sbostic if (!nflag && !pd) 14755997Sbostic (void)printf("%s\n", ps); 14855997Sbostic exit(0); 14955997Sbostic } 15055997Sbostic break; 15155997Sbostic case 'p': 15255997Sbostic if (pd) 15355997Sbostic break; 15455997Sbostic (void)printf("%s\n", ps); 15555997Sbostic break; 15655997Sbostic case 'P': 15755997Sbostic if (pd) 15855997Sbostic break; 15955997Sbostic if ((p = strchr(ps, '\n')) != NULL) { 16055997Sbostic oldc = *p; 16155997Sbostic *p = '\0'; 16255997Sbostic } 16355997Sbostic (void)printf("%s\n", ps); 16455997Sbostic if (p != NULL) 16555997Sbostic *p = oldc; 16655997Sbostic break; 16755997Sbostic case 'q': 16855997Sbostic if (!nflag && !pd) 16955997Sbostic (void)printf("%s\n", ps); 17055997Sbostic flush_appends(); 17155997Sbostic exit(0); 17255997Sbostic case 'r': 17355997Sbostic if (appendx >= appendnum) 17455997Sbostic appends = xrealloc(appends, 17555997Sbostic sizeof(struct s_appends) * 17655997Sbostic (appendnum *= 2)); 17755997Sbostic appends[appendx].type = AP_FILE; 17855997Sbostic appends[appendx].s = cp->t; 17955997Sbostic appendx++; 18055997Sbostic break; 18155997Sbostic case 's': 18255997Sbostic sdone = substitute(cp); 18355997Sbostic break; 18455997Sbostic case 't': 18555997Sbostic if (sdone) { 18655997Sbostic sdone = 0; 18755997Sbostic cp = cp->u.c; 18855997Sbostic goto redirect; 18955997Sbostic } 19055997Sbostic break; 19155997Sbostic case 'w': 19255997Sbostic if (pd) 19355997Sbostic break; 19455997Sbostic if (cp->u.fd == -1 && (cp->u.fd = open(cp->t, 19555997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 19655997Sbostic DEFFILEMODE)) == -1) 19755997Sbostic err(FATAL, "%s: %s\n", 19855997Sbostic cp->t, strerror(errno)); 19955997Sbostic iov[0].iov_base = ps; 20055997Sbostic iov[0].iov_len = psl; 20155997Sbostic if (writev(cp->u.fd, iov, 2) != psl + 1) 20255997Sbostic err(FATAL, "%s: %s\n", 20355997Sbostic cp->t, strerror(errno)); 20455997Sbostic break; 20555997Sbostic case 'x': 20655997Sbostic tspace = PS; 20755997Sbostic PS = HS; 20855997Sbostic HS = tspace; 20955997Sbostic break; 21055997Sbostic case 'y': 21155997Sbostic if (pd) 21255997Sbostic break; 21355997Sbostic for (p = ps, len = psl; len--; ++p) 21455997Sbostic *p = cp->u.y[*p]; 21555997Sbostic break; 21655997Sbostic case ':': 21755997Sbostic case '}': 21855997Sbostic break; 21955997Sbostic case '=': 22055997Sbostic (void)printf("%lu\n", linenum); 22155997Sbostic } 22255997Sbostic cp = cp->next; 22355997Sbostic } /* for all cp */ 22455997Sbostic 22555997Sbostic new: if (!nflag && !pd) 22655997Sbostic (void)printf("%s\n", ps); 22755997Sbostic flush_appends(); 22855997Sbostic } /* for all lines */ 22955997Sbostic } 23055997Sbostic 23155997Sbostic /* 23256019Sbostic * TRUE if the address passed matches the current program state 23356019Sbostic * (lastline, linenumber, ps). 23456019Sbostic */ 23556019Sbostic #define MATCH(a) \ 23656079Sbostic (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1) : \ 23756019Sbostic (a)->type == AT_LINE ? linenum == (a)->u.l : lastline 23856019Sbostic 23956019Sbostic /* 24055997Sbostic * Return TRUE if the command applies to the current line. Sets the inrange 24155997Sbostic * flag to process ranges. Interprets the non-select (``!'') flag. 24255997Sbostic */ 24355997Sbostic static inline int 24455997Sbostic applies(cp) 24555997Sbostic struct s_command *cp; 24655997Sbostic { 24755997Sbostic int r; 24855997Sbostic 24955997Sbostic lastaddr = 0; 25055997Sbostic if (cp->a1 == NULL && cp->a2 == NULL) 25155997Sbostic r = 1; 25255997Sbostic else if (cp->a2) 25355997Sbostic if (cp->inrange) { 25456019Sbostic if (MATCH(cp->a2)) { 25555997Sbostic cp->inrange = 0; 25655997Sbostic lastaddr = 1; 25755997Sbostic } 25855997Sbostic r = 1; 25956019Sbostic } else if (MATCH(cp->a1)) { 26055997Sbostic /* 26155997Sbostic * If the second address is a number less than or 26255997Sbostic * equal to the line number first selected, only 26355997Sbostic * one line shall be selected. 26455997Sbostic * -- POSIX 1003.2 26555997Sbostic */ 26655997Sbostic if (cp->a2->type == AT_LINE && 26755997Sbostic linenum >= cp->a2->u.l) 26855997Sbostic lastaddr = 1; 26955997Sbostic else 27055997Sbostic cp->inrange = 1; 27155997Sbostic r = 1; 27255997Sbostic } else 27355997Sbostic r = 0; 27455997Sbostic else 27556019Sbostic r = MATCH(cp->a1); 27655997Sbostic return (cp->nonsel ? ! r : r); 27755997Sbostic } 27855997Sbostic 27955997Sbostic /* 28055997Sbostic * substitute -- 28155997Sbostic * Do substitutions in the pattern space. Currently, we build a 28255997Sbostic * copy of the new pattern space in the substitute space structure 28355997Sbostic * and then swap them. 28455997Sbostic */ 28555997Sbostic static int 28655997Sbostic substitute(cp) 28755997Sbostic struct s_command *cp; 28855997Sbostic { 28955997Sbostic SPACE tspace; 29056019Sbostic regex_t *re; 29156091Sbostic size_t re_off; 29256091Sbostic int n; 293*56657Sbostic char *s; 29455997Sbostic 29555997Sbostic s = ps; 29656019Sbostic re = cp->u.s->re; 29756019Sbostic if (re == NULL) { 29856079Sbostic if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) { 29956019Sbostic linenum = cp->u.s->linenum; 30056019Sbostic err(COMPILE, "\\%d not defined in the RE", 30156019Sbostic cp->u.s->maxbref); 30256019Sbostic } 30356079Sbostic } 30456079Sbostic if (!regexec_e(re, s, 0, 0)) 30555997Sbostic return (0); 30655997Sbostic 30755997Sbostic SS.len = 0; /* Clean substitute space. */ 30855997Sbostic n = cp->u.s->n; 30955997Sbostic switch (n) { 31055997Sbostic case 0: /* Global */ 31155997Sbostic do { 31255997Sbostic /* Locate start of replaced string. */ 31356079Sbostic re_off = match[0].rm_so; 31455997Sbostic /* Copy leading retained string. */ 31556079Sbostic cspace(&SS, s, re_off, APPEND); 31655997Sbostic /* Add in regular expression. */ 31756079Sbostic regsub(&SS, s, cp->u.s->new); 31855997Sbostic /* Move past this match. */ 31956079Sbostic s += match[0].rm_eo; 32056079Sbostic } while(regexec_e(re, s, REG_NOTBOL, 0)); 32155997Sbostic /* Copy trailing retained string. */ 32256079Sbostic cspace(&SS, s, strlen(s), APPEND); 32355997Sbostic break; 32455997Sbostic default: /* Nth occurrence */ 32555997Sbostic while (--n) { 32656079Sbostic s += match[0].rm_eo; 32756079Sbostic if (!regexec_e(re, s, REG_NOTBOL, 0)) 32855997Sbostic return (0); 32955997Sbostic } 33055997Sbostic /* FALLTHROUGH */ 33155997Sbostic case 1: /* 1st occurrence */ 33255997Sbostic /* Locate start of replaced string. */ 33356079Sbostic re_off = match[0].rm_so + (s - ps); 33455997Sbostic /* Copy leading retained string. */ 33556079Sbostic cspace(&SS, ps, re_off, APPEND); 33655997Sbostic /* Add in regular expression. */ 33756079Sbostic regsub(&SS, s, cp->u.s->new); 33855997Sbostic /* Copy trailing retained string. */ 33956079Sbostic s += match[0].rm_eo; 34056079Sbostic cspace(&SS, s, strlen(s), APPEND); 34155997Sbostic break; 34255997Sbostic } 34355997Sbostic 34455997Sbostic /* 34555997Sbostic * Swap the substitute space and the pattern space, and make sure 34655997Sbostic * that any leftover pointers into stdio memory get lost. 34755997Sbostic */ 34855997Sbostic tspace = PS; 34955997Sbostic PS = SS; 35055997Sbostic SS = tspace; 35155997Sbostic SS.space = SS.back; 35255997Sbostic 35355997Sbostic /* Handle the 'p' flag. */ 35455997Sbostic if (cp->u.s->p) 35555997Sbostic (void)printf("%s\n", ps); 35655997Sbostic 35755997Sbostic /* Handle the 'w' flag. */ 35855997Sbostic if (cp->u.s->wfile && !pd) { 35955997Sbostic if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile, 36055997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) 36155997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 36255997Sbostic iov[0].iov_base = ps; 36355997Sbostic iov[0].iov_len = psl; 36455997Sbostic if (writev(cp->u.s->wfd, iov, 2) != psl + 1) 36555997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 36655997Sbostic } 36755997Sbostic return (1); 36855997Sbostic } 36955997Sbostic 37055997Sbostic /* 37155997Sbostic * Flush append requests. Always called before reading a line, 37255997Sbostic * therefore it also resets the substitution done (sdone) flag. 37355997Sbostic */ 37455997Sbostic static void 37555997Sbostic flush_appends() 37655997Sbostic { 37755997Sbostic FILE *f; 37855997Sbostic int count, i; 37955997Sbostic char buf[8 * 1024]; 38055997Sbostic 38155997Sbostic for (i = 0; i < appendx; i++) 38255997Sbostic switch (appends[i].type) { 38355997Sbostic case AP_STRING: 38455997Sbostic (void)printf("%s", appends[i].s); 38555997Sbostic break; 38655997Sbostic case AP_FILE: 38755997Sbostic /* 38855997Sbostic * Read files probably shouldn't be cached. Since 38955997Sbostic * it's not an error to read a non-existent file, 39055997Sbostic * it's possible that another program is interacting 39155997Sbostic * with the sed script through the file system. It 39255997Sbostic * would be truly bizarre, but possible. It's probably 39355997Sbostic * not that big a performance win, anyhow. 39455997Sbostic */ 39555997Sbostic if ((f = fopen(appends[i].s, "r")) == NULL) 39655997Sbostic break; 39755997Sbostic while (count = fread(buf, 1, sizeof(buf), f)) 39855997Sbostic (void)fwrite(buf, 1, count, stdout); 39955997Sbostic (void)fclose(f); 40055997Sbostic break; 40155997Sbostic } 40255997Sbostic if (ferror(stdout)) 40355997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 40455997Sbostic appendx = 0; 40555997Sbostic sdone = 0; 40655997Sbostic } 40755997Sbostic 40855997Sbostic static void 40955997Sbostic lputs(s) 41055997Sbostic register char *s; 41155997Sbostic { 41255997Sbostic register int count; 41355997Sbostic register char *escapes, *p; 41455997Sbostic struct winsize win; 41555997Sbostic static int termwidth = -1; 41655997Sbostic 41755997Sbostic if (termwidth == -1) 41855997Sbostic if (p = getenv("COLUMNS")) 41955997Sbostic termwidth = atoi(p); 42055997Sbostic else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 42155997Sbostic win.ws_col > 0) 42255997Sbostic termwidth = win.ws_col; 42355997Sbostic else 42455997Sbostic termwidth = 60; 42555997Sbostic 42655997Sbostic for (count = 0; *s; ++s) { 42755997Sbostic if (count >= termwidth) { 42855997Sbostic (void)printf("\\\n"); 42955997Sbostic count = 0; 43055997Sbostic } 43155997Sbostic if (isascii(*s) && isprint(*s) && *s != '\\') { 43255997Sbostic (void)putchar(*s); 43355997Sbostic count++; 43455997Sbostic } else { 43555997Sbostic escapes = "\\\a\b\f\n\r\t\v"; 43655997Sbostic (void)putchar('\\'); 43755997Sbostic if (p = strchr(escapes, *s)) { 43855997Sbostic (void)putchar("\\abfnrtv"[p - escapes]); 43955997Sbostic count += 2; 44055997Sbostic } else { 44155997Sbostic (void)printf("%03o", (u_char)*s); 44255997Sbostic count += 4; 44355997Sbostic } 44455997Sbostic } 44555997Sbostic } 44655997Sbostic (void)putchar('$'); 44755997Sbostic (void)putchar('\n'); 44855997Sbostic if (ferror(stdout)) 44955997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 45055997Sbostic } 45155997Sbostic 45256019Sbostic static inline int 45356079Sbostic regexec_e(preg, string, eflags, nomatch) 45455997Sbostic regex_t *preg; 45555997Sbostic const char *string; 45656079Sbostic int eflags, nomatch; 45755997Sbostic { 45855997Sbostic int eval; 45955997Sbostic 46056019Sbostic if (preg == NULL) { 46156019Sbostic if (defpreg == NULL) 46256019Sbostic err(FATAL, "first RE may not be empty"); 46356079Sbostic } else 46456019Sbostic defpreg = preg; 46556019Sbostic 46656079Sbostic eval = regexec(defpreg, string, 46756079Sbostic nomatch ? 0 : maxnsub + 1, match, eflags); 46856019Sbostic switch(eval) { 46955997Sbostic case 0: 47056019Sbostic return (1); 47156019Sbostic case REG_NOMATCH: 47255997Sbostic return (0); 47355997Sbostic } 47456019Sbostic err(FATAL, "RE error: %s", strregerror(eval, defpreg)); 47555997Sbostic /* NOTREACHED */ 47655997Sbostic } 47755997Sbostic 47855997Sbostic /* 47955997Sbostic * regsub - perform substitutions after a regexp match 48055997Sbostic * Based on a routine by Henry Spencer 48155997Sbostic */ 48255997Sbostic static void 48356079Sbostic regsub(sp, string, src) 48456079Sbostic SPACE *sp; 48555997Sbostic char *string, *src; 48655997Sbostic { 48755997Sbostic register int len, no; 48855997Sbostic register char c, *dst; 48955997Sbostic 49055997Sbostic #define NEEDSP(reqlen) \ 49155997Sbostic if (sp->len >= sp->blen - (reqlen) - 1) { \ 49255997Sbostic sp->blen += (reqlen) + 1024; \ 49355997Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); \ 49455997Sbostic dst = sp->space + sp->len; \ 49555997Sbostic } 49655997Sbostic 49755997Sbostic dst = sp->space + sp->len; 49855997Sbostic while ((c = *src++) != '\0') { 49955997Sbostic if (c == '&') 50055997Sbostic no = 0; 50155997Sbostic else if (c == '\\' && isdigit(*src)) 50255997Sbostic no = *src++ - '0'; 50355997Sbostic else 50455997Sbostic no = -1; 50555997Sbostic if (no < 0) { /* Ordinary character. */ 50655997Sbostic if (c == '\\' && (*src == '\\' || *src == '&')) 50755997Sbostic c = *src++; 50855997Sbostic NEEDSP(1); 50955997Sbostic *dst++ = c; 51055997Sbostic ++sp->len; 51156079Sbostic } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { 51256079Sbostic len = match[no].rm_eo - match[no].rm_so; 51355997Sbostic NEEDSP(len); 51456079Sbostic memmove(dst, string + match[no].rm_so, len); 51555997Sbostic dst += len; 51655997Sbostic sp->len += len; 51755997Sbostic } 51855997Sbostic } 51955997Sbostic NEEDSP(1); 52055997Sbostic *dst = '\0'; 52155997Sbostic } 52255997Sbostic 52355997Sbostic /* 52455997Sbostic * aspace -- 52555997Sbostic * Append the source space to the destination space, allocating new 52655997Sbostic * space as necessary. 52755997Sbostic */ 52856079Sbostic void 52956079Sbostic cspace(sp, p, len, spflag) 53055997Sbostic SPACE *sp; 53155997Sbostic char *p; 53255997Sbostic size_t len; 53356079Sbostic enum e_spflag spflag; 53455997Sbostic { 53555997Sbostic size_t tlen; 53655997Sbostic 53755997Sbostic /* 53856079Sbostic * Make sure SPACE has enough memory and ramp up quickly. Appends 53956079Sbostic * need two extra bytes, one for the newline, one for a terminating 54056079Sbostic * NULL. 54155997Sbostic */ 54256079Sbostic tlen = sp->len + len + spflag == APPENDNL ? 2 : 1; 54355997Sbostic if (tlen > sp->blen) { 54455997Sbostic sp->blen = tlen + 1024; 54556079Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); 54655997Sbostic } 54755997Sbostic 54856079Sbostic if (spflag == APPENDNL) 54955997Sbostic sp->space[sp->len++] = '\n'; 55056079Sbostic else if (spflag == REPLACE) 55156079Sbostic sp->len = 0; 55255997Sbostic 55356079Sbostic memmove(sp->space + sp->len, p, len); 55456079Sbostic sp->space[sp->len += len] = '\0'; 55555997Sbostic } 55655997Sbostic 55755997Sbostic /* 55855997Sbostic * Close all cached opened files and report any errors 55955997Sbostic */ 56055997Sbostic void 56156093Sbostic cfclose(cp, end) 56256093Sbostic register struct s_command *cp, *end; 56355997Sbostic { 56455997Sbostic 56556093Sbostic for (; cp != end; cp = cp->next) 56655997Sbostic switch(cp->code) { 56755997Sbostic case 's': 56855997Sbostic if (cp->u.s->wfd != -1 && close(cp->u.s->wfd)) 56955997Sbostic err(FATAL, 57055997Sbostic "%s: %s", cp->u.s->wfile, strerror(errno)); 57156064Sbostic cp->u.s->wfd = -1; 57255997Sbostic break; 57355997Sbostic case 'w': 57455997Sbostic if (cp->u.fd != -1 && close(cp->u.fd)) 57555997Sbostic err(FATAL, "%s: %s", cp->t, strerror(errno)); 57656064Sbostic cp->u.fd = -1; 57755997Sbostic break; 57855997Sbostic case '{': 57956093Sbostic cfclose(cp->u.c, cp->next); 58055997Sbostic break; 58155997Sbostic } 58255997Sbostic } 583