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*56079Sbostic static char sccsid[] = "@(#)process.c 5.6 (Berkeley) 08/28/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 *)); 42*56079Sbostic static void cspace __P((SPACE *, char *, size_t, enum e_spflag)); 4355997Sbostic static void flush_appends __P((void)); 4455997Sbostic static void lputs __P((char *)); 45*56079Sbostic static inline int regexec_e __P((regex_t *, const char *, int, int)); 46*56079Sbostic static void regsub __P((SPACE *, char *, char *)); 4755997Sbostic static int substitute __P((struct s_command *)); 4855997Sbostic 4955997Sbostic struct s_appends *appends; /* Array of pointers to strings to append. */ 5055997Sbostic static int appendx; /* Index into appends array. */ 5155997Sbostic int appendnum; /* Size of appends array. */ 5255997Sbostic 5355997Sbostic static int lastaddr; /* Set by applies if last address of a range. */ 5455997Sbostic static int sdone; /* If any substitutes since last line input. */ 5555997Sbostic /* Iov structure for 'w' commands. */ 5655997Sbostic static struct iovec iov[2] = { NULL, 0, "\n", 1 }; 5755997Sbostic 5856019Sbostic static regex_t *defpreg; 59*56079Sbostic size_t maxnsub; 60*56079Sbostic regmatch_t *match; 6156019Sbostic 6255997Sbostic void 6355997Sbostic process() 6455997Sbostic { 6555997Sbostic struct s_command *cp; 6655997Sbostic SPACE tspace; 6755997Sbostic size_t len; 68*56079Sbostic int r; 6955997Sbostic char oldc, *p; 7055997Sbostic 71*56079Sbostic for (linenum = 0; mf_fgets(&PS, REPLACE);) { 7255997Sbostic pd = 0; 7355997Sbostic cp = prog; 7455997Sbostic redirect: 7555997Sbostic while (cp != NULL) { 7655997Sbostic if (!applies(cp)) { 7755997Sbostic cp = cp->next; 7855997Sbostic continue; 7955997Sbostic } 8055997Sbostic switch (cp->code) { 8155997Sbostic case '{': 8255997Sbostic cp = cp->u.c; 8355997Sbostic goto redirect; 8455997Sbostic case 'a': 8555997Sbostic if (appendx >= appendnum) 8655997Sbostic appends = xrealloc(appends, 8755997Sbostic sizeof(struct s_appends) * 8855997Sbostic (appendnum *= 2)); 8955997Sbostic appends[appendx].type = AP_STRING; 9055997Sbostic appends[appendx].s = 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; 10856005Sbostic if ((p = strchr(ps, '\n')) == NULL) 10956005Sbostic pd = 1; 11056005Sbostic else { 11155997Sbostic psl -= (p - ps) - 1; 11255997Sbostic memmove(ps, p + 1, psl); 11355997Sbostic } 11455997Sbostic goto new; 11555997Sbostic case 'g': 116*56079Sbostic cspace(&PS, hs, hsl, REPLACE); 11755997Sbostic break; 11855997Sbostic case 'G': 119*56079Sbostic cspace(&PS, hs, hsl, APPENDNL); 12055997Sbostic break; 12155997Sbostic case 'h': 122*56079Sbostic cspace(&HS, ps, psl, REPLACE); 12355997Sbostic break; 12455997Sbostic case 'H': 125*56079Sbostic cspace(&HS, ps, psl, APPENDNL); 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) 13555997Sbostic (void)printf("%s\n", ps); 13655997Sbostic flush_appends(); 137*56079Sbostic r = mf_fgets(&PS, REPLACE); 13855997Sbostic #ifdef HISTORIC_PRACTICE 139*56079Sbostic if (!r) 14055997Sbostic exit(0); 14155997Sbostic #endif 14255997Sbostic pd = 0; 14355997Sbostic break; 14455997Sbostic case 'N': 14555997Sbostic flush_appends(); 146*56079Sbostic if (!mf_fgets(&PS, APPENDNL)) { 14755997Sbostic if (!nflag && !pd) 14855997Sbostic (void)printf("%s\n", ps); 14955997Sbostic exit(0); 15055997Sbostic } 15155997Sbostic break; 15255997Sbostic case 'p': 15355997Sbostic if (pd) 15455997Sbostic break; 15555997Sbostic (void)printf("%s\n", ps); 15655997Sbostic break; 15755997Sbostic case 'P': 15855997Sbostic if (pd) 15955997Sbostic break; 16055997Sbostic if ((p = strchr(ps, '\n')) != NULL) { 16155997Sbostic oldc = *p; 16255997Sbostic *p = '\0'; 16355997Sbostic } 16455997Sbostic (void)printf("%s\n", ps); 16555997Sbostic if (p != NULL) 16655997Sbostic *p = oldc; 16755997Sbostic break; 16855997Sbostic case 'q': 16955997Sbostic if (!nflag && !pd) 17055997Sbostic (void)printf("%s\n", 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; 18055997Sbostic appendx++; 18155997Sbostic break; 18255997Sbostic case 's': 18355997Sbostic sdone = substitute(cp); 18455997Sbostic break; 18555997Sbostic case 't': 18655997Sbostic if (sdone) { 18755997Sbostic sdone = 0; 18855997Sbostic cp = cp->u.c; 18955997Sbostic goto redirect; 19055997Sbostic } 19155997Sbostic break; 19255997Sbostic case 'w': 19355997Sbostic if (pd) 19455997Sbostic break; 19555997Sbostic if (cp->u.fd == -1 && (cp->u.fd = open(cp->t, 19655997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, 19755997Sbostic DEFFILEMODE)) == -1) 19855997Sbostic err(FATAL, "%s: %s\n", 19955997Sbostic cp->t, strerror(errno)); 20055997Sbostic iov[0].iov_base = ps; 20155997Sbostic iov[0].iov_len = psl; 20255997Sbostic if (writev(cp->u.fd, iov, 2) != psl + 1) 20355997Sbostic err(FATAL, "%s: %s\n", 20455997Sbostic cp->t, strerror(errno)); 20555997Sbostic break; 20655997Sbostic case 'x': 20755997Sbostic tspace = PS; 20855997Sbostic PS = HS; 20955997Sbostic HS = tspace; 21055997Sbostic break; 21155997Sbostic case 'y': 21255997Sbostic if (pd) 21355997Sbostic break; 21455997Sbostic for (p = ps, len = psl; len--; ++p) 21555997Sbostic *p = cp->u.y[*p]; 21655997Sbostic break; 21755997Sbostic case ':': 21855997Sbostic case '}': 21955997Sbostic break; 22055997Sbostic case '=': 22155997Sbostic (void)printf("%lu\n", linenum); 22255997Sbostic } 22355997Sbostic cp = cp->next; 22455997Sbostic } /* for all cp */ 22555997Sbostic 22655997Sbostic new: if (!nflag && !pd) 22755997Sbostic (void)printf("%s\n", ps); 22855997Sbostic flush_appends(); 22955997Sbostic } /* for all lines */ 23055997Sbostic } 23155997Sbostic 23255997Sbostic /* 23356019Sbostic * TRUE if the address passed matches the current program state 23456019Sbostic * (lastline, linenumber, ps). 23556019Sbostic */ 23656019Sbostic #define MATCH(a) \ 237*56079Sbostic (a)->type == AT_RE ? regexec_e((a)->u.r, ps, 0, 1) : \ 23856019Sbostic (a)->type == AT_LINE ? linenum == (a)->u.l : lastline 23956019Sbostic 24056019Sbostic /* 24155997Sbostic * Return TRUE if the command applies to the current line. Sets the inrange 24255997Sbostic * flag to process ranges. Interprets the non-select (``!'') flag. 24355997Sbostic */ 24455997Sbostic static inline int 24555997Sbostic applies(cp) 24655997Sbostic struct s_command *cp; 24755997Sbostic { 24855997Sbostic int r; 24955997Sbostic 25055997Sbostic lastaddr = 0; 25155997Sbostic if (cp->a1 == NULL && cp->a2 == NULL) 25255997Sbostic r = 1; 25355997Sbostic else if (cp->a2) 25455997Sbostic if (cp->inrange) { 25556019Sbostic if (MATCH(cp->a2)) { 25655997Sbostic cp->inrange = 0; 25755997Sbostic lastaddr = 1; 25855997Sbostic } 25955997Sbostic r = 1; 26056019Sbostic } else if (MATCH(cp->a1)) { 26155997Sbostic /* 26255997Sbostic * If the second address is a number less than or 26355997Sbostic * equal to the line number first selected, only 26455997Sbostic * one line shall be selected. 26555997Sbostic * -- POSIX 1003.2 26655997Sbostic */ 26755997Sbostic if (cp->a2->type == AT_LINE && 26855997Sbostic linenum >= cp->a2->u.l) 26955997Sbostic lastaddr = 1; 27055997Sbostic else 27155997Sbostic cp->inrange = 1; 27255997Sbostic r = 1; 27355997Sbostic } else 27455997Sbostic r = 0; 27555997Sbostic else 27656019Sbostic r = MATCH(cp->a1); 27755997Sbostic return (cp->nonsel ? ! r : r); 27855997Sbostic } 27955997Sbostic 28055997Sbostic /* 28155997Sbostic * substitute -- 28255997Sbostic * Do substitutions in the pattern space. Currently, we build a 28355997Sbostic * copy of the new pattern space in the substitute space structure 28455997Sbostic * and then swap them. 28555997Sbostic */ 28655997Sbostic static int 28755997Sbostic substitute(cp) 28855997Sbostic struct s_command *cp; 28955997Sbostic { 29055997Sbostic SPACE tspace; 29156019Sbostic regex_t *re; 29255997Sbostic int n, re_off; 29355997Sbostic char *endp, *s; 29455997Sbostic 29555997Sbostic s = ps; 29656019Sbostic re = cp->u.s->re; 29756019Sbostic if (re == NULL) { 298*56079Sbostic 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 } 303*56079Sbostic } 304*56079Sbostic 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. */ 313*56079Sbostic re_off = match[0].rm_so; 31455997Sbostic /* Locate end of replaced string + 1. */ 315*56079Sbostic endp = s + match[0].rm_eo; 31655997Sbostic /* Copy leading retained string. */ 317*56079Sbostic cspace(&SS, s, re_off, APPEND); 31855997Sbostic /* Add in regular expression. */ 319*56079Sbostic regsub(&SS, s, cp->u.s->new); 32055997Sbostic /* Move past this match. */ 321*56079Sbostic s += match[0].rm_eo; 322*56079Sbostic } while(regexec_e(re, s, REG_NOTBOL, 0)); 32355997Sbostic /* Copy trailing retained string. */ 324*56079Sbostic cspace(&SS, s, strlen(s), APPEND); 32555997Sbostic break; 32655997Sbostic default: /* Nth occurrence */ 32755997Sbostic while (--n) { 328*56079Sbostic s += match[0].rm_eo; 329*56079Sbostic 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. */ 335*56079Sbostic re_off = match[0].rm_so + (s - ps); 33655997Sbostic /* Copy leading retained string. */ 337*56079Sbostic cspace(&SS, ps, re_off, APPEND); 33855997Sbostic /* Add in regular expression. */ 339*56079Sbostic regsub(&SS, s, cp->u.s->new); 34055997Sbostic /* Copy trailing retained string. */ 341*56079Sbostic s += match[0].rm_eo; 342*56079Sbostic 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 455*56079Sbostic regexec_e(preg, string, eflags, nomatch) 45655997Sbostic regex_t *preg; 45755997Sbostic const char *string; 458*56079Sbostic 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"); 465*56079Sbostic } else 46656019Sbostic defpreg = preg; 46756019Sbostic 468*56079Sbostic eval = regexec(defpreg, string, 469*56079Sbostic 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 485*56079Sbostic regsub(sp, string, src) 486*56079Sbostic 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; 513*56079Sbostic } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { 514*56079Sbostic len = match[no].rm_eo - match[no].rm_so; 51555997Sbostic NEEDSP(len); 516*56079Sbostic 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 */ 530*56079Sbostic void 531*56079Sbostic cspace(sp, p, len, spflag) 53255997Sbostic SPACE *sp; 53355997Sbostic char *p; 53455997Sbostic size_t len; 535*56079Sbostic enum e_spflag spflag; 53655997Sbostic { 53755997Sbostic size_t tlen; 53855997Sbostic 53955997Sbostic /* 540*56079Sbostic * Make sure SPACE has enough memory and ramp up quickly. Appends 541*56079Sbostic * need two extra bytes, one for the newline, one for a terminating 542*56079Sbostic * NULL. 54355997Sbostic */ 544*56079Sbostic tlen = sp->len + len + spflag == APPENDNL ? 2 : 1; 54555997Sbostic if (tlen > sp->blen) { 54655997Sbostic sp->blen = tlen + 1024; 547*56079Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); 54855997Sbostic } 54955997Sbostic 550*56079Sbostic if (spflag == APPENDNL) 55155997Sbostic sp->space[sp->len++] = '\n'; 552*56079Sbostic else if (spflag == REPLACE) 553*56079Sbostic sp->len = 0; 55455997Sbostic 555*56079Sbostic memmove(sp->space + sp->len, p, len); 556*56079Sbostic 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