155997Sbostic /*- 255997Sbostic * Copyright (c) 1992 Diomidis Spinellis. 366817Sbostic * Copyright (c) 1992, 1993, 1994 462229Sbostic * The Regents of the University of California. 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*67038Sbostic static char sccsid[] = "@(#)process.c 8.5 (Berkeley) 04/20/94"; 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; 10860120Selan if ((p = memchr(ps, '\n', psl)) == NULL) 10956005Sbostic pd = 1; 11056005Sbostic else { 11167036Sbostic 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; 16060120Selan 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; 294*67038Sbostic int lastempty, 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 309*67038Sbostic SS.len = 0; /* Clean substitute space. */ 310*67038Sbostic slen = psl; 311*67038Sbostic n = cp->u.s->n; 312*67038Sbostic lastempty = 1; 31366120Sbostic 314*67038Sbostic switch (n) { 315*67038Sbostic case 0: /* Global */ 316*67038Sbostic do { 317*67038Sbostic if (lastempty || match[0].rm_so != match[0].rm_eo) { 318*67038Sbostic /* Locate start of replaced string. */ 319*67038Sbostic re_off = match[0].rm_so; 320*67038Sbostic /* Copy leading retained string. */ 321*67038Sbostic cspace(&SS, s, re_off, APPEND); 322*67038Sbostic /* Add in regular expression. */ 323*67038Sbostic regsub(&SS, s, cp->u.s->new); 324*67038Sbostic } 325*67038Sbostic 326*67038Sbostic /* Move past this match. */ 327*67038Sbostic if (match[0].rm_so != match[0].rm_eo) { 328*67038Sbostic s += match[0].rm_eo; 329*67038Sbostic slen -= match[0].rm_eo; 330*67038Sbostic lastempty = 0; 331*67038Sbostic } else { 332*67038Sbostic if (match[0].rm_so == 0) 333*67038Sbostic cspace(&SS, 334*67038Sbostic s, match[0].rm_so + 1, APPEND); 335*67038Sbostic else 336*67038Sbostic cspace(&SS, 337*67038Sbostic s + match[0].rm_so, 1, APPEND); 338*67038Sbostic s += match[0].rm_so + 1; 339*67038Sbostic slen -= match[0].rm_so + 1; 340*67038Sbostic lastempty = 1; 341*67038Sbostic } 342*67038Sbostic } while (slen > 0 && regexec_e(re, s, REG_NOTBOL, 0, slen)); 34355997Sbostic /* Copy trailing retained string. */ 344*67038Sbostic if (slen > 0) 345*67038Sbostic cspace(&SS, s, slen, APPEND); 346*67038Sbostic break; 34755997Sbostic default: /* Nth occurrence */ 34855997Sbostic while (--n) { 34956079Sbostic s += match[0].rm_eo; 35057733Selan slen -= match[0].rm_eo; 35157733Selan if (!regexec_e(re, s, REG_NOTBOL, 0, slen)) 35255997Sbostic return (0); 35355997Sbostic } 35455997Sbostic /* FALLTHROUGH */ 35555997Sbostic case 1: /* 1st occurrence */ 35655997Sbostic /* Locate start of replaced string. */ 35756079Sbostic re_off = match[0].rm_so + (s - ps); 35855997Sbostic /* Copy leading retained string. */ 35956079Sbostic cspace(&SS, ps, re_off, APPEND); 36055997Sbostic /* Add in regular expression. */ 36156079Sbostic regsub(&SS, s, cp->u.s->new); 36255997Sbostic /* Copy trailing retained string. */ 36356079Sbostic s += match[0].rm_eo; 36457733Selan slen -= match[0].rm_eo; 36557733Selan cspace(&SS, s, slen, APPEND); 36655997Sbostic break; 36755997Sbostic } 36855997Sbostic 36955997Sbostic /* 37055997Sbostic * Swap the substitute space and the pattern space, and make sure 37155997Sbostic * that any leftover pointers into stdio memory get lost. 37255997Sbostic */ 37355997Sbostic tspace = PS; 37455997Sbostic PS = SS; 37555997Sbostic SS = tspace; 37655997Sbostic SS.space = SS.back; 37755997Sbostic 37855997Sbostic /* Handle the 'p' flag. */ 37955997Sbostic if (cp->u.s->p) 38057733Selan OUT(ps) 38155997Sbostic 38255997Sbostic /* Handle the 'w' flag. */ 38355997Sbostic if (cp->u.s->wfile && !pd) { 38455997Sbostic if (cp->u.s->wfd == -1 && (cp->u.s->wfd = open(cp->u.s->wfile, 38555997Sbostic O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, DEFFILEMODE)) == -1) 38655997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 38759073Selan if (write(cp->u.s->wfd, ps, psl) != psl) 38855997Sbostic err(FATAL, "%s: %s\n", cp->u.s->wfile, strerror(errno)); 38955997Sbostic } 39055997Sbostic return (1); 39155997Sbostic } 39255997Sbostic 39355997Sbostic /* 39455997Sbostic * Flush append requests. Always called before reading a line, 39555997Sbostic * therefore it also resets the substitution done (sdone) flag. 39655997Sbostic */ 39755997Sbostic static void 39855997Sbostic flush_appends() 39955997Sbostic { 40055997Sbostic FILE *f; 40155997Sbostic int count, i; 40255997Sbostic char buf[8 * 1024]; 40355997Sbostic 40455997Sbostic for (i = 0; i < appendx; i++) 40555997Sbostic switch (appends[i].type) { 40655997Sbostic case AP_STRING: 40757733Selan fwrite(appends[i].s, sizeof(char), appends[i].len, 40857733Selan stdout); 40955997Sbostic break; 41055997Sbostic case AP_FILE: 41155997Sbostic /* 41255997Sbostic * Read files probably shouldn't be cached. Since 41355997Sbostic * it's not an error to read a non-existent file, 41455997Sbostic * it's possible that another program is interacting 41555997Sbostic * with the sed script through the file system. It 41655997Sbostic * would be truly bizarre, but possible. It's probably 41755997Sbostic * not that big a performance win, anyhow. 41855997Sbostic */ 41955997Sbostic if ((f = fopen(appends[i].s, "r")) == NULL) 42055997Sbostic break; 42159073Selan while (count = fread(buf, sizeof(char), sizeof(buf), f)) 42257733Selan (void)fwrite(buf, sizeof(char), count, stdout); 42355997Sbostic (void)fclose(f); 42455997Sbostic break; 42555997Sbostic } 42655997Sbostic if (ferror(stdout)) 42755997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 42856949Sbostic appendx = sdone = 0; 42955997Sbostic } 43055997Sbostic 43155997Sbostic static void 43255997Sbostic lputs(s) 43355997Sbostic register char *s; 43455997Sbostic { 43555997Sbostic register int count; 43655997Sbostic register char *escapes, *p; 43755997Sbostic struct winsize win; 43855997Sbostic static int termwidth = -1; 43955997Sbostic 44055997Sbostic if (termwidth == -1) 44155997Sbostic if (p = getenv("COLUMNS")) 44255997Sbostic termwidth = atoi(p); 44355997Sbostic else if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &win) == 0 && 44455997Sbostic win.ws_col > 0) 44555997Sbostic termwidth = win.ws_col; 44655997Sbostic else 44755997Sbostic termwidth = 60; 44855997Sbostic 44955997Sbostic for (count = 0; *s; ++s) { 45055997Sbostic if (count >= termwidth) { 45155997Sbostic (void)printf("\\\n"); 45255997Sbostic count = 0; 45355997Sbostic } 45455997Sbostic if (isascii(*s) && isprint(*s) && *s != '\\') { 45555997Sbostic (void)putchar(*s); 45655997Sbostic count++; 45755997Sbostic } else { 45855997Sbostic escapes = "\\\a\b\f\n\r\t\v"; 45955997Sbostic (void)putchar('\\'); 46055997Sbostic if (p = strchr(escapes, *s)) { 46155997Sbostic (void)putchar("\\abfnrtv"[p - escapes]); 46255997Sbostic count += 2; 46355997Sbostic } else { 46459073Selan (void)printf("%03o", *(u_char *)s); 46555997Sbostic count += 4; 46655997Sbostic } 46755997Sbostic } 46855997Sbostic } 46955997Sbostic (void)putchar('$'); 47055997Sbostic (void)putchar('\n'); 47155997Sbostic if (ferror(stdout)) 47255997Sbostic err(FATAL, "stdout: %s", strerror(errno ? errno : EIO)); 47355997Sbostic } 47455997Sbostic 47556019Sbostic static inline int 47657733Selan regexec_e(preg, string, eflags, nomatch, slen) 47755997Sbostic regex_t *preg; 47855997Sbostic const char *string; 47956079Sbostic int eflags, nomatch; 48057733Selan size_t slen; 48155997Sbostic { 48255997Sbostic int eval; 48357733Selan 48456019Sbostic if (preg == NULL) { 48556019Sbostic if (defpreg == NULL) 48656019Sbostic err(FATAL, "first RE may not be empty"); 48756079Sbostic } else 48856019Sbostic defpreg = preg; 48956019Sbostic 49059077Storek /* Set anchors, discounting trailing newline (if any). */ 49159077Storek if (slen > 0 && string[slen - 1] == '\n') 49259077Storek slen--; 49359073Selan match[0].rm_so = 0; 49459077Storek match[0].rm_eo = slen; 49559073Selan 49656079Sbostic eval = regexec(defpreg, string, 49759073Selan nomatch ? 0 : maxnsub + 1, match, eflags | REG_STARTEND); 49856019Sbostic switch(eval) { 49955997Sbostic case 0: 50056019Sbostic return (1); 50156019Sbostic case REG_NOMATCH: 50255997Sbostic return (0); 50355997Sbostic } 50456019Sbostic err(FATAL, "RE error: %s", strregerror(eval, defpreg)); 50555997Sbostic /* NOTREACHED */ 50655997Sbostic } 50755997Sbostic 50855997Sbostic /* 50955997Sbostic * regsub - perform substitutions after a regexp match 51055997Sbostic * Based on a routine by Henry Spencer 51155997Sbostic */ 51255997Sbostic static void 51356079Sbostic regsub(sp, string, src) 51456079Sbostic SPACE *sp; 51555997Sbostic char *string, *src; 51655997Sbostic { 51755997Sbostic register int len, no; 51855997Sbostic register char c, *dst; 51955997Sbostic 52055997Sbostic #define NEEDSP(reqlen) \ 52155997Sbostic if (sp->len >= sp->blen - (reqlen) - 1) { \ 52255997Sbostic sp->blen += (reqlen) + 1024; \ 52355997Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); \ 52455997Sbostic dst = sp->space + sp->len; \ 52555997Sbostic } 52655997Sbostic 52755997Sbostic dst = sp->space + sp->len; 52855997Sbostic while ((c = *src++) != '\0') { 52955997Sbostic if (c == '&') 53055997Sbostic no = 0; 53155997Sbostic else if (c == '\\' && isdigit(*src)) 53255997Sbostic no = *src++ - '0'; 53355997Sbostic else 53455997Sbostic no = -1; 53555997Sbostic if (no < 0) { /* Ordinary character. */ 53655997Sbostic if (c == '\\' && (*src == '\\' || *src == '&')) 53755997Sbostic c = *src++; 53855997Sbostic NEEDSP(1); 53955997Sbostic *dst++ = c; 54055997Sbostic ++sp->len; 54156079Sbostic } else if (match[no].rm_so != -1 && match[no].rm_eo != -1) { 54256079Sbostic len = match[no].rm_eo - match[no].rm_so; 54355997Sbostic NEEDSP(len); 54456079Sbostic memmove(dst, string + match[no].rm_so, len); 54555997Sbostic dst += len; 54655997Sbostic sp->len += len; 54755997Sbostic } 54855997Sbostic } 54955997Sbostic NEEDSP(1); 55055997Sbostic *dst = '\0'; 55155997Sbostic } 55255997Sbostic 55355997Sbostic /* 55455997Sbostic * aspace -- 55555997Sbostic * Append the source space to the destination space, allocating new 55655997Sbostic * space as necessary. 55755997Sbostic */ 55856079Sbostic void 55956079Sbostic cspace(sp, p, len, spflag) 56055997Sbostic SPACE *sp; 56155997Sbostic char *p; 56255997Sbostic size_t len; 56356079Sbostic enum e_spflag spflag; 56455997Sbostic { 56555997Sbostic size_t tlen; 56655997Sbostic 56759073Selan /* Make sure SPACE has enough memory and ramp up quickly. */ 56859073Selan tlen = sp->len + len + 1; 56955997Sbostic if (tlen > sp->blen) { 57055997Sbostic sp->blen = tlen + 1024; 57156079Sbostic sp->space = sp->back = xrealloc(sp->back, sp->blen); 57255997Sbostic } 57355997Sbostic 57459073Selan if (spflag == REPLACE) 57556079Sbostic sp->len = 0; 57655997Sbostic 57756079Sbostic memmove(sp->space + sp->len, p, len); 57857733Selan 57956079Sbostic sp->space[sp->len += len] = '\0'; 58055997Sbostic } 58155997Sbostic 58255997Sbostic /* 58355997Sbostic * Close all cached opened files and report any errors 58455997Sbostic */ 58555997Sbostic void 58656093Sbostic cfclose(cp, end) 58756093Sbostic register struct s_command *cp, *end; 58855997Sbostic { 58955997Sbostic 59056093Sbostic for (; cp != end; cp = cp->next) 59155997Sbostic switch(cp->code) { 59255997Sbostic case 's': 59355997Sbostic if (cp->u.s->wfd != -1 && close(cp->u.s->wfd)) 59455997Sbostic err(FATAL, 59555997Sbostic "%s: %s", cp->u.s->wfile, strerror(errno)); 59656064Sbostic cp->u.s->wfd = -1; 59755997Sbostic break; 59855997Sbostic case 'w': 59955997Sbostic if (cp->u.fd != -1 && close(cp->u.fd)) 60055997Sbostic err(FATAL, "%s: %s", cp->t, strerror(errno)); 60156064Sbostic cp->u.fd = -1; 60255997Sbostic break; 60355997Sbostic case '{': 60456093Sbostic cfclose(cp->u.c, cp->next); 60555997Sbostic break; 60655997Sbostic } 60755997Sbostic } 608