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*60120Selan static char sccsid[] = "@(#)process.c 5.16 (Berkeley) 05/18/93"; 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 *)); 4457733Selan static inline int regexec_e __P((regex_t *, const char *, int, int, size_t)); 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. */ 5556019Sbostic static regex_t *defpreg; 5656079Sbostic size_t maxnsub; 5759073Selan regmatch_t *match; 5856019Sbostic 5959073Selan #define OUT(s) { fwrite(s, sizeof(u_char), psl, stdout); } 6057733Selan 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; 9057733Selan appends[appendx].len = strlen(cp->t); 9155997Sbostic appendx++; 9255997Sbostic break; 9355997Sbostic case 'b': 9455997Sbostic cp = cp->u.c; 9555997Sbostic goto redirect; 9655997Sbostic case 'c': 9755997Sbostic pd = 1; 9855997Sbostic psl = 0; 9955997Sbostic if (cp->a2 == NULL || lastaddr) 10055997Sbostic (void)printf("%s", cp->t); 10155997Sbostic break; 10255997Sbostic case 'd': 10356005Sbostic pd = 1; 10455997Sbostic goto new; 10555997Sbostic case 'D': 10655997Sbostic if (pd) 10755997Sbostic goto new; 108*60120Selan if ((p = memchr(ps, '\n', psl)) == NULL) 10956005Sbostic pd = 1; 11056005Sbostic else { 11155997Sbostic psl -= (p - ps) - 1; 11255997Sbostic memmove(ps, p + 1, psl); 11355997Sbostic } 11455997Sbostic goto new; 11555997Sbostic case 'g': 11656079Sbostic cspace(&PS, hs, hsl, REPLACE); 11755997Sbostic break; 11855997Sbostic case 'G': 11959073Selan cspace(&PS, hs, hsl, 0); 12055997Sbostic break; 12155997Sbostic case 'h': 12256079Sbostic cspace(&HS, ps, psl, REPLACE); 12355997Sbostic break; 12455997Sbostic case 'H': 12559073Selan cspace(&HS, ps, psl, 0); 12655997Sbostic break; 12755997Sbostic case 'i': 12855997Sbostic (void)printf("%s", cp->t); 12955997Sbostic break; 13055997Sbostic case 'l': 13155997Sbostic lputs(ps); 13255997Sbostic break; 13355997Sbostic case 'n': 13455997Sbostic if (!nflag && !pd) 13557733Selan OUT(ps) 13655997Sbostic flush_appends(); 13756079Sbostic r = mf_fgets(&PS, REPLACE); 13855997Sbostic #ifdef HISTORIC_PRACTICE 13956079Sbostic if (!r) 14055997Sbostic exit(0); 14155997Sbostic #endif 14255997Sbostic pd = 0; 14355997Sbostic break; 14455997Sbostic case 'N': 14555997Sbostic flush_appends(); 14659073Selan if (!mf_fgets(&PS, 0)) { 14755997Sbostic if (!nflag && !pd) 14857733Selan OUT(ps) 14955997Sbostic exit(0); 15055997Sbostic } 15155997Sbostic break; 15255997Sbostic case 'p': 15355997Sbostic if (pd) 15455997Sbostic break; 15557733Selan OUT(ps) 15655997Sbostic break; 15755997Sbostic case 'P': 15855997Sbostic if (pd) 15955997Sbostic break; 160*60120Selan if ((p = memchr(ps, '\n', psl)) != NULL) { 16155997Sbostic oldc = *p; 16255997Sbostic *p = '\0'; 16355997Sbostic } 16457733Selan OUT(ps) 16555997Sbostic if (p != NULL) 16655997Sbostic *p = oldc; 16755997Sbostic break; 16855997Sbostic case 'q': 16955997Sbostic if (!nflag && !pd) 17057733Selan OUT(ps) 17155997Sbostic flush_appends(); 17255997Sbostic exit(0); 17355997Sbostic case 'r': 17455997Sbostic if (appendx >= appendnum) 17555997Sbostic appends = xrealloc(appends, 17655997Sbostic sizeof(struct s_appends) * 17755997Sbostic (appendnum *= 2)); 17855997Sbostic appends[appendx].type = AP_FILE; 17955997Sbostic appends[appendx].s = cp->t; 18057733Selan appends[appendx].len = strlen(cp->t); 18155997Sbostic appendx++; 18255997Sbostic break; 18355997Sbostic case 's': 18456949Sbostic sdone |= substitute(cp); 18555997Sbostic break; 18655997Sbostic case 't': 18755997Sbostic if (sdone) { 18855997Sbostic sdone = 0; 18955997Sbostic cp = cp->u.c; 19055997Sbostic goto redirect; 19155997Sbostic } 19255997Sbostic break; 19355997Sbostic case 'w': 19455997Sbostic if (pd) 19555997Sbostic break; 19655997Sbostic if (cp->u.fd == -1 && (cp->u.fd = open(cp->t, 19755997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 19855997Sbostic DEFFILEMODE)) == -1) 19955997Sbostic err(FATAL, "%s: %s\n", 20055997Sbostic cp->t, strerror(errno)); 20159073Selan if (write(cp->u.fd, ps, psl) != psl) 20255997Sbostic err(FATAL, "%s: %s\n", 20355997Sbostic cp->t, strerror(errno)); 20455997Sbostic break; 20555997Sbostic case 'x': 20657421Sbostic if (hs == NULL) 20757421Sbostic cspace(&HS, "", 0, REPLACE); 20855997Sbostic tspace = PS; 20955997Sbostic PS = HS; 21055997Sbostic HS = tspace; 21155997Sbostic break; 21255997Sbostic case 'y': 21355997Sbostic if (pd) 21455997Sbostic break; 21559073Selan for (p = ps, len = psl; --len; ++p) 21655997Sbostic *p = cp->u.y[*p]; 21755997Sbostic break; 21855997Sbostic case ':': 21955997Sbostic case '}': 22055997Sbostic break; 22155997Sbostic case '=': 22255997Sbostic (void)printf("%lu\n", linenum); 22355997Sbostic } 22455997Sbostic cp = cp->next; 22555997Sbostic } /* for all cp */ 22655997Sbostic 22755997Sbostic new: if (!nflag && !pd) 22857733Selan OUT(ps) 22955997Sbostic flush_appends(); 23055997Sbostic } /* for all lines */ 23155997Sbostic } 23255997Sbostic 23355997Sbostic /* 23456019Sbostic * TRUE if the address passed matches the current program state 23556019Sbostic * (lastline, linenumber, ps). 23656019Sbostic */ 23757733Selan #define MATCH(a) \ 23857733Selan (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1, psl) : \ 23956019Sbostic (a)->type == AT_LINE ? linenum == (a)->u.l : lastline 24056019Sbostic 24156019Sbostic /* 24255997Sbostic * Return TRUE if the command applies to the current line. Sets the inrange 24355997Sbostic * flag to process ranges. Interprets the non-select (``!'') flag. 24455997Sbostic */ 24555997Sbostic static inline int 24655997Sbostic applies(cp) 24755997Sbostic struct s_command *cp; 24855997Sbostic { 24955997Sbostic int r; 25055997Sbostic 25155997Sbostic lastaddr = 0; 25255997Sbostic if (cp->a1 == NULL && cp->a2 == NULL) 25355997Sbostic r = 1; 25455997Sbostic else if (cp->a2) 25555997Sbostic if (cp->inrange) { 25656019Sbostic if (MATCH(cp->a2)) { 25755997Sbostic cp->inrange = 0; 25855997Sbostic lastaddr = 1; 25955997Sbostic } 26055997Sbostic r = 1; 26156019Sbostic } else if (MATCH(cp->a1)) { 26255997Sbostic /* 26355997Sbostic * If the second address is a number less than or 26455997Sbostic * equal to the line number first selected, only 26555997Sbostic * one line shall be selected. 26655997Sbostic * -- POSIX 1003.2 26755997Sbostic */ 26855997Sbostic if (cp->a2->type == AT_LINE && 26955997Sbostic linenum >= cp->a2->u.l) 27055997Sbostic lastaddr = 1; 27155997Sbostic else 27255997Sbostic cp->inrange = 1; 27355997Sbostic r = 1; 27455997Sbostic } else 27555997Sbostic r = 0; 27655997Sbostic else 27756019Sbostic r = MATCH(cp->a1); 27855997Sbostic return (cp->nonsel ? ! r : r); 27955997Sbostic } 28055997Sbostic 28155997Sbostic /* 28255997Sbostic * substitute -- 28355997Sbostic * Do substitutions in the pattern space. Currently, we build a 28455997Sbostic * copy of the new pattern space in the substitute space structure 28555997Sbostic * and then swap them. 28655997Sbostic */ 28755997Sbostic static int 28855997Sbostic substitute(cp) 28955997Sbostic struct s_command *cp; 29055997Sbostic { 29155997Sbostic SPACE tspace; 29256019Sbostic regex_t *re; 29357733Selan size_t re_off, slen; 29456091Sbostic int n; 29556657Sbostic char *s; 29655997Sbostic 29755997Sbostic s = ps; 29856019Sbostic re = cp->u.s->re; 29956019Sbostic if (re == NULL) { 30056079Sbostic if (defpreg != NULL && cp->u.s->maxbref > defpreg->re_nsub) { 30156019Sbostic linenum = cp->u.s->linenum; 30256019Sbostic err(COMPILE, "\\%d not defined in the RE", 30356019Sbostic cp->u.s->maxbref); 30456019Sbostic } 30556079Sbostic } 30657733Selan if (!regexec_e(re, s, 0, 0, psl)) 30755997Sbostic return (0); 30855997Sbostic 30955997Sbostic SS.len = 0; /* Clean substitute space. */ 31057733Selan slen = psl; 31155997Sbostic n = cp->u.s->n; 31255997Sbostic switch (n) { 31355997Sbostic case 0: /* Global */ 31455997Sbostic do { 31555997Sbostic /* Locate start of replaced string. */ 31656079Sbostic re_off = match[0].rm_so; 31755997Sbostic /* Copy leading retained string. */ 31856079Sbostic cspace(&SS, s, re_off, APPEND); 31955997Sbostic /* Add in regular expression. */ 32056079Sbostic regsub(&SS, s, cp->u.s->new); 32155997Sbostic /* Move past this match. */ 32256079Sbostic s += match[0].rm_eo; 32357733Selan slen -= match[0].rm_eo; 32457733Selan } while(regexec_e(re, s, REG_NOTBOL, 0, slen)); 32555997Sbostic /* Copy trailing retained string. */ 32657733Selan cspace(&SS, s, slen, APPEND); 32755997Sbostic break; 32855997Sbostic default: /* Nth occurrence */ 32955997Sbostic while (--n) { 33056079Sbostic s += match[0].rm_eo; 33157733Selan slen -= match[0].rm_eo; 33257733Selan if (!regexec_e(re, s, REG_NOTBOL, 0, slen)) 33355997Sbostic return (0); 33455997Sbostic } 33555997Sbostic /* FALLTHROUGH */ 33655997Sbostic case 1: /* 1st occurrence */ 33755997Sbostic /* Locate start of replaced string. */ 33856079Sbostic re_off = match[0].rm_so + (s - ps); 33955997Sbostic /* Copy leading retained string. */ 34056079Sbostic cspace(&SS, ps, re_off, APPEND); 34155997Sbostic /* Add in regular expression. */ 34256079Sbostic regsub(&SS, s, cp->u.s->new); 34355997Sbostic /* Copy trailing retained string. */ 34456079Sbostic s += match[0].rm_eo; 34557733Selan slen -= match[0].rm_eo; 34657733Selan cspace(&SS, s, slen, APPEND); 34755997Sbostic break; 34855997Sbostic } 34955997Sbostic 35055997Sbostic /* 35155997Sbostic * Swap the substitute space and the pattern space, and make sure 35255997Sbostic * that any leftover pointers into stdio memory get lost. 35355997Sbostic */ 35455997Sbostic tspace = PS; 35555997Sbostic PS = SS; 35655997Sbostic SS = tspace; 35755997Sbostic SS.space = SS.back; 35855997Sbostic 35955997Sbostic /* Handle the 'p' flag. */ 36055997Sbostic if (cp->u.s->p) 36157733Selan OUT(ps) 36255997Sbostic 36355997Sbostic /* Handle the 'w' flag. */ 36455997Sbostic if (cp->u.s->wfile && !pd) { 36555997Sbostic if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile, 36655997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) 36755997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 36859073Selan if (write(cp->u.s->wfd, ps, psl) != psl) 36955997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 37055997Sbostic } 37155997Sbostic return (1); 37255997Sbostic } 37355997Sbostic 37455997Sbostic /* 37555997Sbostic * Flush append requests. Always called before reading a line, 37655997Sbostic * therefore it also resets the substitution done (sdone) flag. 37755997Sbostic */ 37855997Sbostic static void 37955997Sbostic flush_appends() 38055997Sbostic { 38155997Sbostic FILE *f; 38255997Sbostic int count, i; 38355997Sbostic char buf[8 * 1024]; 38455997Sbostic 38555997Sbostic for (i = 0; i < appendx; i++) 38655997Sbostic switch (appends[i].type) { 38755997Sbostic case AP_STRING: 38857733Selan fwrite(appends[i].s, sizeof(char), appends[i].len, 38957733Selan stdout); 39055997Sbostic break; 39155997Sbostic case AP_FILE: 39255997Sbostic /* 39355997Sbostic * Read files probably shouldn't be cached. Since 39455997Sbostic * it's not an error to read a non-existent file, 39555997Sbostic * it's possible that another program is interacting 39655997Sbostic * with the sed script through the file system. It 39755997Sbostic * would be truly bizarre, but possible. It's probably 39855997Sbostic * not that big a performance win, anyhow. 39955997Sbostic */ 40055997Sbostic if ((f = fopen(appends[i].s, "r")) == NULL) 40155997Sbostic break; 40259073Selan while (count = fread(buf, sizeof(char), sizeof(buf), f)) 40357733Selan (void)fwrite(buf, sizeof(char), count, stdout); 40455997Sbostic (void)fclose(f); 40555997Sbostic break; 40655997Sbostic } 40755997Sbostic if (ferror(stdout)) 40855997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 40956949Sbostic appendx = sdone = 0; 41055997Sbostic } 41155997Sbostic 41255997Sbostic static void 41355997Sbostic lputs(s) 41455997Sbostic register char *s; 41555997Sbostic { 41655997Sbostic register int count; 41755997Sbostic register char *escapes, *p; 41855997Sbostic struct winsize win; 41955997Sbostic static int termwidth = -1; 42055997Sbostic 42155997Sbostic if (termwidth == -1) 42255997Sbostic if (p = getenv("COLUMNS")) 42355997Sbostic termwidth = atoi(p); 42455997Sbostic else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 42555997Sbostic win.ws_col > 0) 42655997Sbostic termwidth = win.ws_col; 42755997Sbostic else 42855997Sbostic termwidth = 60; 42955997Sbostic 43055997Sbostic for (count = 0; *s; ++s) { 43155997Sbostic if (count >= termwidth) { 43255997Sbostic (void)printf("\\\n"); 43355997Sbostic count = 0; 43455997Sbostic } 43555997Sbostic if (isascii(*s) && isprint(*s) && *s != '\\') { 43655997Sbostic (void)putchar(*s); 43755997Sbostic count++; 43855997Sbostic } else { 43955997Sbostic escapes = "\\\a\b\f\n\r\t\v"; 44055997Sbostic (void)putchar('\\'); 44155997Sbostic if (p = strchr(escapes, *s)) { 44255997Sbostic (void)putchar("\\abfnrtv"[p - escapes]); 44355997Sbostic count += 2; 44455997Sbostic } else { 44559073Selan (void)printf("%03o", *(u_char *)s); 44655997Sbostic count += 4; 44755997Sbostic } 44855997Sbostic } 44955997Sbostic } 45055997Sbostic (void)putchar('$'); 45155997Sbostic (void)putchar('\n'); 45255997Sbostic if (ferror(stdout)) 45355997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 45455997Sbostic } 45555997Sbostic 45656019Sbostic static inline int 45757733Selan regexec_e(preg, string, eflags, nomatch, slen) 45855997Sbostic regex_t *preg; 45955997Sbostic const char *string; 46056079Sbostic int eflags, nomatch; 46157733Selan size_t slen; 46255997Sbostic { 46355997Sbostic int eval; 46457733Selan 46556019Sbostic if (preg == NULL) { 46656019Sbostic if (defpreg == NULL) 46756019Sbostic err(FATAL, "first RE may not be empty"); 46856079Sbostic } else 46956019Sbostic defpreg = preg; 47056019Sbostic 47159077Storek /* Set anchors, discounting trailing newline (if any). */ 47259077Storek if (slen > 0 && string[slen - 1] == '\n') 47359077Storek slen--; 47459073Selan match[0].rm_so = 0; 47559077Storek match[0].rm_eo = slen; 47659073Selan 47756079Sbostic eval = regexec(defpreg, string, 47859073Selan nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND); 47956019Sbostic switch(eval) { 48055997Sbostic case 0: 48156019Sbostic return (1); 48256019Sbostic case REG_NOMATCH: 48355997Sbostic return (0); 48455997Sbostic } 48556019Sbostic err(FATAL, "RE error: %s", strregerror(eval, defpreg)); 48655997Sbostic /* NOTREACHED */ 48755997Sbostic } 48855997Sbostic 48955997Sbostic /* 49055997Sbostic * regsub - perform substitutions after a regexp match 49155997Sbostic * Based on a routine by Henry Spencer 49255997Sbostic */ 49355997Sbostic static void 49456079Sbostic regsub(sp, string, src) 49556079Sbostic SPACE *sp; 49655997Sbostic char *string, *src; 49755997Sbostic { 49855997Sbostic register int len, no; 49955997Sbostic register char c, *dst; 50055997Sbostic 50155997Sbostic #define NEEDSP(reqlen) \ 50255997Sbostic if (sp->len >= sp->blen - (reqlen) - 1) { \ 50355997Sbostic sp->blen += (reqlen) + 1024; \ 50455997Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); \ 50555997Sbostic dst = sp->space + sp->len; \ 50655997Sbostic } 50755997Sbostic 50855997Sbostic dst = sp->space + sp->len; 50955997Sbostic while ((c = *src++) != '\0') { 51055997Sbostic if (c == '&') 51155997Sbostic no = 0; 51255997Sbostic else if (c == '\\' && isdigit(*src)) 51355997Sbostic no = *src++ - '0'; 51455997Sbostic else 51555997Sbostic no = -1; 51655997Sbostic if (no < 0) { /* Ordinary character. */ 51755997Sbostic if (c == '\\' && (*src == '\\' || *src == '&')) 51855997Sbostic c = *src++; 51955997Sbostic NEEDSP(1); 52055997Sbostic *dst++ = c; 52155997Sbostic ++sp->len; 52256079Sbostic } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { 52356079Sbostic len = match[no].rm_eo - match[no].rm_so; 52455997Sbostic NEEDSP(len); 52556079Sbostic memmove(dst, string + match[no].rm_so, len); 52655997Sbostic dst += len; 52755997Sbostic sp->len += len; 52855997Sbostic } 52955997Sbostic } 53055997Sbostic NEEDSP(1); 53155997Sbostic *dst = '\0'; 53255997Sbostic } 53355997Sbostic 53455997Sbostic /* 53555997Sbostic * aspace -- 53655997Sbostic * Append the source space to the destination space, allocating new 53755997Sbostic * space as necessary. 53855997Sbostic */ 53956079Sbostic void 54056079Sbostic cspace(sp, p, len, spflag) 54155997Sbostic SPACE *sp; 54255997Sbostic char *p; 54355997Sbostic size_t len; 54456079Sbostic enum e_spflag spflag; 54555997Sbostic { 54655997Sbostic size_t tlen; 54755997Sbostic 54859073Selan /* Make sure SPACE has enough memory and ramp up quickly. */ 54959073Selan tlen = sp->len + len + 1; 55055997Sbostic if (tlen > sp->blen) { 55155997Sbostic sp->blen = tlen + 1024; 55256079Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); 55355997Sbostic } 55455997Sbostic 55559073Selan if (spflag == REPLACE) 55656079Sbostic sp->len = 0; 55755997Sbostic 55856079Sbostic memmove(sp->space + sp->len, p, len); 55957733Selan 56056079Sbostic sp->space[sp->len += len] = '\0'; 56155997Sbostic } 56255997Sbostic 56355997Sbostic /* 56455997Sbostic * Close all cached opened files and report any errors 56555997Sbostic */ 56655997Sbostic void 56756093Sbostic cfclose(cp, end) 56856093Sbostic register struct s_command *cp, *end; 56955997Sbostic { 57055997Sbostic 57156093Sbostic for (; cp != end; cp = cp->next) 57255997Sbostic switch(cp->code) { 57355997Sbostic case 's': 57455997Sbostic if (cp->u.s->wfd != -1 && close(cp->u.s->wfd)) 57555997Sbostic err(FATAL, 57655997Sbostic "%s: %s", cp->u.s->wfile, strerror(errno)); 57756064Sbostic cp->u.s->wfd = -1; 57855997Sbostic break; 57955997Sbostic case 'w': 58055997Sbostic if (cp->u.fd != -1 && close(cp->u.fd)) 58155997Sbostic err(FATAL, "%s: %s", cp->t, strerror(errno)); 58256064Sbostic cp->u.fd = -1; 58355997Sbostic break; 58455997Sbostic case '{': 58556093Sbostic cfclose(cp->u.c, cp->next); 58655997Sbostic break; 58755997Sbostic } 58855997Sbostic } 589