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*56091Sbostic static char sccsid[] = "@(#)process.c 5.7 (Berkeley) 08/29/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; 291*56091Sbostic size_t re_off; 292*56091Sbostic int n; 29355997Sbostic char *endp, *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 /* Locate end of replaced string + 1. */ 31556079Sbostic endp = s + match[0].rm_eo; 31655997Sbostic /* Copy leading retained string. */ 31756079Sbostic cspace(&SS, s, re_off, APPEND); 31855997Sbostic /* Add in regular expression. */ 31956079Sbostic regsub(&SS, s, cp->u.s->new); 32055997Sbostic /* Move past this match. */ 32156079Sbostic s += match[0].rm_eo; 32256079Sbostic } while(regexec_e(re, s, REG_NOTBOL, 0)); 32355997Sbostic /* Copy trailing retained string. */ 32456079Sbostic cspace(&SS, s, strlen(s), APPEND); 32555997Sbostic break; 32655997Sbostic default: /* Nth occurrence */ 32755997Sbostic while (--n) { 32856079Sbostic s += match[0].rm_eo; 32956079Sbostic if (!regexec_e(re, s, REG_NOTBOL, 0)) 33055997Sbostic return (0); 33155997Sbostic } 33255997Sbostic /* FALLTHROUGH */ 33355997Sbostic case 1: /* 1st occurrence */ 33455997Sbostic /* Locate start of replaced string. */ 33556079Sbostic re_off = match[0].rm_so + (s - ps); 33655997Sbostic /* Copy leading retained string. */ 33756079Sbostic cspace(&SS, ps, re_off, APPEND); 33855997Sbostic /* Add in regular expression. */ 33956079Sbostic regsub(&SS, s, cp->u.s->new); 34055997Sbostic /* Copy trailing retained string. */ 34156079Sbostic s += match[0].rm_eo; 34256079Sbostic cspace(&SS, s, strlen(s), APPEND); 34355997Sbostic break; 34455997Sbostic } 34555997Sbostic 34655997Sbostic /* 34755997Sbostic * Swap the substitute space and the pattern space, and make sure 34855997Sbostic * that any leftover pointers into stdio memory get lost. 34955997Sbostic */ 35055997Sbostic tspace = PS; 35155997Sbostic PS = SS; 35255997Sbostic SS = tspace; 35355997Sbostic SS.space = SS.back; 35455997Sbostic 35555997Sbostic /* Handle the 'p' flag. */ 35655997Sbostic if (cp->u.s->p) 35755997Sbostic (void)printf("%s\n", ps); 35855997Sbostic 35955997Sbostic /* Handle the 'w' flag. */ 36055997Sbostic if (cp->u.s->wfile && !pd) { 36155997Sbostic if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile, 36255997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) 36355997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 36455997Sbostic iov[0].iov_base = ps; 36555997Sbostic iov[0].iov_len = psl; 36655997Sbostic if (writev(cp->u.s->wfd, iov, 2) != psl + 1) 36755997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 36855997Sbostic } 36955997Sbostic return (1); 37055997Sbostic } 37155997Sbostic 37255997Sbostic /* 37355997Sbostic * Flush append requests. Always called before reading a line, 37455997Sbostic * therefore it also resets the substitution done (sdone) flag. 37555997Sbostic */ 37655997Sbostic static void 37755997Sbostic flush_appends() 37855997Sbostic { 37955997Sbostic FILE *f; 38055997Sbostic int count, i; 38155997Sbostic char buf[8 * 1024]; 38255997Sbostic 38355997Sbostic for (i = 0; i < appendx; i++) 38455997Sbostic switch (appends[i].type) { 38555997Sbostic case AP_STRING: 38655997Sbostic (void)printf("%s", appends[i].s); 38755997Sbostic break; 38855997Sbostic case AP_FILE: 38955997Sbostic /* 39055997Sbostic * Read files probably shouldn't be cached. Since 39155997Sbostic * it's not an error to read a non-existent file, 39255997Sbostic * it's possible that another program is interacting 39355997Sbostic * with the sed script through the file system. It 39455997Sbostic * would be truly bizarre, but possible. It's probably 39555997Sbostic * not that big a performance win, anyhow. 39655997Sbostic */ 39755997Sbostic if ((f = fopen(appends[i].s, "r")) == NULL) 39855997Sbostic break; 39955997Sbostic while (count = fread(buf, 1, sizeof(buf), f)) 40055997Sbostic (void)fwrite(buf, 1, count, stdout); 40155997Sbostic (void)fclose(f); 40255997Sbostic break; 40355997Sbostic } 40455997Sbostic if (ferror(stdout)) 40555997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 40655997Sbostic appendx = 0; 40755997Sbostic sdone = 0; 40855997Sbostic } 40955997Sbostic 41055997Sbostic static void 41155997Sbostic lputs(s) 41255997Sbostic register char *s; 41355997Sbostic { 41455997Sbostic register int count; 41555997Sbostic register char *escapes, *p; 41655997Sbostic struct winsize win; 41755997Sbostic static int termwidth = -1; 41855997Sbostic 41955997Sbostic if (termwidth == -1) 42055997Sbostic if (p = getenv("COLUMNS")) 42155997Sbostic termwidth = atoi(p); 42255997Sbostic else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 42355997Sbostic win.ws_col > 0) 42455997Sbostic termwidth = win.ws_col; 42555997Sbostic else 42655997Sbostic termwidth = 60; 42755997Sbostic 42855997Sbostic for (count = 0; *s; ++s) { 42955997Sbostic if (count >= termwidth) { 43055997Sbostic (void)printf("\\\n"); 43155997Sbostic count = 0; 43255997Sbostic } 43355997Sbostic if (isascii(*s) && isprint(*s) && *s != '\\') { 43455997Sbostic (void)putchar(*s); 43555997Sbostic count++; 43655997Sbostic } else { 43755997Sbostic escapes = "\\\a\b\f\n\r\t\v"; 43855997Sbostic (void)putchar('\\'); 43955997Sbostic if (p = strchr(escapes, *s)) { 44055997Sbostic (void)putchar("\\abfnrtv"[p - escapes]); 44155997Sbostic count += 2; 44255997Sbostic } else { 44355997Sbostic (void)printf("%03o", (u_char)*s); 44455997Sbostic count += 4; 44555997Sbostic } 44655997Sbostic } 44755997Sbostic } 44855997Sbostic (void)putchar('$'); 44955997Sbostic (void)putchar('\n'); 45055997Sbostic if (ferror(stdout)) 45155997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 45255997Sbostic } 45355997Sbostic 45456019Sbostic static inline int 45556079Sbostic regexec_e(preg, string, eflags, nomatch) 45655997Sbostic regex_t *preg; 45755997Sbostic const char *string; 45856079Sbostic int eflags, nomatch; 45955997Sbostic { 46055997Sbostic int eval; 46155997Sbostic 46256019Sbostic if (preg == NULL) { 46356019Sbostic if (defpreg == NULL) 46456019Sbostic err(FATAL, "first RE may not be empty"); 46556079Sbostic } else 46656019Sbostic defpreg = preg; 46756019Sbostic 46856079Sbostic eval = regexec(defpreg, string, 46956079Sbostic nomatch ? 0 : maxnsub + 1, match, eflags); 47056019Sbostic switch(eval) { 47155997Sbostic case 0: 47256019Sbostic return (1); 47356019Sbostic case REG_NOMATCH: 47455997Sbostic return (0); 47555997Sbostic } 47656019Sbostic err(FATAL, "RE error: %s", strregerror(eval, defpreg)); 47755997Sbostic /* NOTREACHED */ 47855997Sbostic } 47955997Sbostic 48055997Sbostic /* 48155997Sbostic * regsub - perform substitutions after a regexp match 48255997Sbostic * Based on a routine by Henry Spencer 48355997Sbostic */ 48455997Sbostic static void 48556079Sbostic regsub(sp, string, src) 48656079Sbostic SPACE *sp; 48755997Sbostic char *string, *src; 48855997Sbostic { 48955997Sbostic register int len, no; 49055997Sbostic register char c, *dst; 49155997Sbostic 49255997Sbostic #define NEEDSP(reqlen) \ 49355997Sbostic if (sp->len >= sp->blen - (reqlen) - 1) { \ 49455997Sbostic sp->blen += (reqlen) + 1024; \ 49555997Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); \ 49655997Sbostic dst = sp->space + sp->len; \ 49755997Sbostic } 49855997Sbostic 49955997Sbostic dst = sp->space + sp->len; 50055997Sbostic while ((c = *src++) != '\0') { 50155997Sbostic if (c == '&') 50255997Sbostic no = 0; 50355997Sbostic else if (c == '\\' && isdigit(*src)) 50455997Sbostic no = *src++ - '0'; 50555997Sbostic else 50655997Sbostic no = -1; 50755997Sbostic if (no < 0) { /* Ordinary character. */ 50855997Sbostic if (c == '\\' && (*src == '\\' || *src == '&')) 50955997Sbostic c = *src++; 51055997Sbostic NEEDSP(1); 51155997Sbostic *dst++ = c; 51255997Sbostic ++sp->len; 51356079Sbostic } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { 51456079Sbostic len = match[no].rm_eo - match[no].rm_so; 51555997Sbostic NEEDSP(len); 51656079Sbostic memmove(dst, string + match[no].rm_so, len); 51755997Sbostic dst += len; 51855997Sbostic sp->len += len; 51955997Sbostic } 52055997Sbostic } 52155997Sbostic NEEDSP(1); 52255997Sbostic *dst = '\0'; 52355997Sbostic } 52455997Sbostic 52555997Sbostic /* 52655997Sbostic * aspace -- 52755997Sbostic * Append the source space to the destination space, allocating new 52855997Sbostic * space as necessary. 52955997Sbostic */ 53056079Sbostic void 53156079Sbostic cspace(sp, p, len, spflag) 53255997Sbostic SPACE *sp; 53355997Sbostic char *p; 53455997Sbostic size_t len; 53556079Sbostic enum e_spflag spflag; 53655997Sbostic { 53755997Sbostic size_t tlen; 53855997Sbostic 53955997Sbostic /* 54056079Sbostic * Make sure SPACE has enough memory and ramp up quickly. Appends 54156079Sbostic * need two extra bytes, one for the newline, one for a terminating 54256079Sbostic * NULL. 54355997Sbostic */ 54456079Sbostic tlen = sp->len + len + spflag == APPENDNL ? 2 : 1; 54555997Sbostic if (tlen > sp->blen) { 54655997Sbostic sp->blen = tlen + 1024; 54756079Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); 54855997Sbostic } 54955997Sbostic 55056079Sbostic if (spflag == APPENDNL) 55155997Sbostic sp->space[sp->len++] = '\n'; 55256079Sbostic else if (spflag == REPLACE) 55356079Sbostic sp->len = 0; 55455997Sbostic 55556079Sbostic memmove(sp->space + sp->len, p, len); 55656079Sbostic sp->space[sp->len += len] = '\0'; 55755997Sbostic } 55855997Sbostic 55955997Sbostic /* 56055997Sbostic * Close all cached opened files and report any errors 56155997Sbostic */ 56255997Sbostic void 56355997Sbostic cfclose(cp) 56455997Sbostic register struct s_command *cp; 56555997Sbostic { 56655997Sbostic 56755997Sbostic for (; cp != NULL; cp = cp->next) 56855997Sbostic switch(cp->code) { 56955997Sbostic case 's': 57055997Sbostic if (cp->u.s->wfd != -1 && close(cp->u.s->wfd)) 57155997Sbostic err(FATAL, 57255997Sbostic "%s: %s", cp->u.s->wfile, strerror(errno)); 57356064Sbostic cp->u.s->wfd = -1; 57455997Sbostic break; 57555997Sbostic case 'w': 57655997Sbostic if (cp->u.fd != -1 && close(cp->u.fd)) 57755997Sbostic err(FATAL, "%s: %s", cp->t, strerror(errno)); 57856064Sbostic cp->u.fd = -1; 57955997Sbostic break; 58055997Sbostic case '{': 58155997Sbostic cfclose(cp->u.c); 58255997Sbostic break; 58355997Sbostic } 58455997Sbostic } 585