19b50d902SRodney W. Grimes /*- 29b50d902SRodney W. Grimes * Copyright (c) 1992 Diomidis Spinellis. 39b50d902SRodney W. Grimes * Copyright (c) 1992, 1993 49b50d902SRodney W. Grimes * The Regents of the University of California. All rights reserved. 59b50d902SRodney W. Grimes * 69b50d902SRodney W. Grimes * This code is derived from software contributed to Berkeley by 79b50d902SRodney W. Grimes * Diomidis Spinellis of Imperial College, University of London. 89b50d902SRodney W. Grimes * 99b50d902SRodney W. Grimes * Redistribution and use in source and binary forms, with or without 109b50d902SRodney W. Grimes * modification, are permitted provided that the following conditions 119b50d902SRodney W. Grimes * are met: 129b50d902SRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 139b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer. 149b50d902SRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 159b50d902SRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 169b50d902SRodney W. Grimes * documentation and/or other materials provided with the distribution. 179b50d902SRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 189b50d902SRodney W. Grimes * must display the following acknowledgement: 199b50d902SRodney W. Grimes * This product includes software developed by the University of 209b50d902SRodney W. Grimes * California, Berkeley and its contributors. 219b50d902SRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 229b50d902SRodney W. Grimes * may be used to endorse or promote products derived from this software 239b50d902SRodney W. Grimes * without specific prior written permission. 249b50d902SRodney W. Grimes * 259b50d902SRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 269b50d902SRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 279b50d902SRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 289b50d902SRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 299b50d902SRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 309b50d902SRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 319b50d902SRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 329b50d902SRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 339b50d902SRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 349b50d902SRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 359b50d902SRodney W. Grimes * SUCH DAMAGE. 369b50d902SRodney W. Grimes */ 379b50d902SRodney W. Grimes 389b50d902SRodney W. Grimes #ifndef lint 3973a08bb2SPhilippe Charnier static const char copyright[] = 409b50d902SRodney W. Grimes "@(#) Copyright (c) 1992, 1993\n\ 419b50d902SRodney W. Grimes The Regents of the University of California. All rights reserved.\n"; 429b50d902SRodney W. Grimes #endif /* not lint */ 439b50d902SRodney W. Grimes 449b50d902SRodney W. Grimes #ifndef lint 4573a08bb2SPhilippe Charnier #if 0 469b50d902SRodney W. Grimes static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94"; 4773a08bb2SPhilippe Charnier #endif 4873a08bb2SPhilippe Charnier static const char rcsid[] = 49c3aac50fSPeter Wemm "$FreeBSD$"; 509b50d902SRodney W. Grimes #endif /* not lint */ 519b50d902SRodney W. Grimes 529b50d902SRodney W. Grimes #include <sys/types.h> 539b50d902SRodney W. Grimes 5473a08bb2SPhilippe Charnier #include <err.h> 559b50d902SRodney W. Grimes #include <errno.h> 569b50d902SRodney W. Grimes #include <fcntl.h> 57726aebe5SAndrey A. Chernov #include <locale.h> 589b50d902SRodney W. Grimes #include <regex.h> 599b50d902SRodney W. Grimes #include <stddef.h> 609b50d902SRodney W. Grimes #include <stdio.h> 619b50d902SRodney W. Grimes #include <stdlib.h> 629b50d902SRodney W. Grimes #include <string.h> 639b50d902SRodney W. Grimes #include <unistd.h> 649b50d902SRodney W. Grimes 659b50d902SRodney W. Grimes #include "defs.h" 669b50d902SRodney W. Grimes #include "extern.h" 679b50d902SRodney W. Grimes 689b50d902SRodney W. Grimes /* 699b50d902SRodney W. Grimes * Linked list of units (strings and files) to be compiled 709b50d902SRodney W. Grimes */ 719b50d902SRodney W. Grimes struct s_compunit { 729b50d902SRodney W. Grimes struct s_compunit *next; 739b50d902SRodney W. Grimes enum e_cut {CU_FILE, CU_STRING} type; 749b50d902SRodney W. Grimes char *s; /* Pointer to string or fname */ 759b50d902SRodney W. Grimes }; 769b50d902SRodney W. Grimes 779b50d902SRodney W. Grimes /* 789b50d902SRodney W. Grimes * Linked list pointer to compilation units and pointer to current 799b50d902SRodney W. Grimes * next pointer. 809b50d902SRodney W. Grimes */ 819b50d902SRodney W. Grimes static struct s_compunit *script, **cu_nextp = &script; 829b50d902SRodney W. Grimes 839b50d902SRodney W. Grimes /* 849b50d902SRodney W. Grimes * Linked list of files to be processed 859b50d902SRodney W. Grimes */ 869b50d902SRodney W. Grimes struct s_flist { 879b50d902SRodney W. Grimes char *fname; 889b50d902SRodney W. Grimes struct s_flist *next; 899b50d902SRodney W. Grimes }; 909b50d902SRodney W. Grimes 919b50d902SRodney W. Grimes /* 929b50d902SRodney W. Grimes * Linked list pointer to files and pointer to current 939b50d902SRodney W. Grimes * next pointer. 949b50d902SRodney W. Grimes */ 959b50d902SRodney W. Grimes static struct s_flist *files, **fl_nextp = &files; 969b50d902SRodney W. Grimes 979b50d902SRodney W. Grimes int aflag, eflag, nflag; 98175de1e6SBrian Feldman int rflags = 0; 999b50d902SRodney W. Grimes 1009b50d902SRodney W. Grimes /* 1019b50d902SRodney W. Grimes * Current file and line number; line numbers restart across compilation 1029b50d902SRodney W. Grimes * units, but span across input files. 1039b50d902SRodney W. Grimes */ 1049b50d902SRodney W. Grimes char *fname; /* File name. */ 1059b50d902SRodney W. Grimes u_long linenum; 1069b50d902SRodney W. Grimes int lastline; /* TRUE on the last line of the last file */ 1079b50d902SRodney W. Grimes 1089b50d902SRodney W. Grimes static void add_compunit __P((enum e_cut, char *)); 1099b50d902SRodney W. Grimes static void add_file __P((char *)); 11073a08bb2SPhilippe Charnier static void usage __P((void)); 1119b50d902SRodney W. Grimes 1129b50d902SRodney W. Grimes int 1139b50d902SRodney W. Grimes main(argc, argv) 1149b50d902SRodney W. Grimes int argc; 1159b50d902SRodney W. Grimes char *argv[]; 1169b50d902SRodney W. Grimes { 1179b50d902SRodney W. Grimes int c, fflag; 1189b50d902SRodney W. Grimes 119726aebe5SAndrey A. Chernov (void) setlocale(LC_ALL, ""); 120726aebe5SAndrey A. Chernov 1219b50d902SRodney W. Grimes fflag = 0; 122175de1e6SBrian Feldman while ((c = getopt(argc, argv, "Eae:f:n")) != -1) 1239b50d902SRodney W. Grimes switch (c) { 124175de1e6SBrian Feldman case 'E': 125175de1e6SBrian Feldman rflags = REG_EXTENDED; 126175de1e6SBrian Feldman break; 1279b50d902SRodney W. Grimes case 'a': 1289b50d902SRodney W. Grimes aflag = 1; 1299b50d902SRodney W. Grimes break; 1309b50d902SRodney W. Grimes case 'e': 1319b50d902SRodney W. Grimes eflag = 1; 1329b50d902SRodney W. Grimes add_compunit(CU_STRING, optarg); 1339b50d902SRodney W. Grimes break; 1349b50d902SRodney W. Grimes case 'f': 1359b50d902SRodney W. Grimes fflag = 1; 1369b50d902SRodney W. Grimes add_compunit(CU_FILE, optarg); 1379b50d902SRodney W. Grimes break; 1389b50d902SRodney W. Grimes case 'n': 1399b50d902SRodney W. Grimes nflag = 1; 1409b50d902SRodney W. Grimes break; 1419b50d902SRodney W. Grimes default: 1429b50d902SRodney W. Grimes case '?': 14373a08bb2SPhilippe Charnier usage(); 1449b50d902SRodney W. Grimes } 1459b50d902SRodney W. Grimes argc -= optind; 1469b50d902SRodney W. Grimes argv += optind; 1479b50d902SRodney W. Grimes 1489b50d902SRodney W. Grimes /* First usage case; script is the first arg */ 1499b50d902SRodney W. Grimes if (!eflag && !fflag && *argv) { 1509b50d902SRodney W. Grimes add_compunit(CU_STRING, *argv); 1519b50d902SRodney W. Grimes argv++; 1529b50d902SRodney W. Grimes } 1539b50d902SRodney W. Grimes 1549b50d902SRodney W. Grimes compile(); 1559b50d902SRodney W. Grimes 1569b50d902SRodney W. Grimes /* Continue with first and start second usage */ 1579b50d902SRodney W. Grimes if (*argv) 1589b50d902SRodney W. Grimes for (; *argv; argv++) 1599b50d902SRodney W. Grimes add_file(*argv); 1609b50d902SRodney W. Grimes else 1619b50d902SRodney W. Grimes add_file(NULL); 1629b50d902SRodney W. Grimes process(); 1639b50d902SRodney W. Grimes cfclose(prog, NULL); 1649b50d902SRodney W. Grimes if (fclose(stdout)) 16573a08bb2SPhilippe Charnier err(1, "stdout"); 1669b50d902SRodney W. Grimes exit (0); 1679b50d902SRodney W. Grimes } 1689b50d902SRodney W. Grimes 16973a08bb2SPhilippe Charnier static void 17073a08bb2SPhilippe Charnier usage() 17173a08bb2SPhilippe Charnier { 17273a08bb2SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n", 173175de1e6SBrian Feldman "usage: sed script [-Ean] [file ...]", 17473a08bb2SPhilippe Charnier " sed [-an] [-e script] ... [-f script_file] ... [file ...]"); 17573a08bb2SPhilippe Charnier exit(1); 17673a08bb2SPhilippe Charnier } 17773a08bb2SPhilippe Charnier 1789b50d902SRodney W. Grimes /* 1799b50d902SRodney W. Grimes * Like fgets, but go through the chain of compilation units chaining them 1809b50d902SRodney W. Grimes * together. Empty strings and files are ignored. 1819b50d902SRodney W. Grimes */ 1829b50d902SRodney W. Grimes char * 183c56690efSArchie Cobbs cu_fgets(buf, n, more) 1849b50d902SRodney W. Grimes char *buf; 1859b50d902SRodney W. Grimes int n; 186c56690efSArchie Cobbs int *more; 1879b50d902SRodney W. Grimes { 1889b50d902SRodney W. Grimes static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 1899b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 1909b50d902SRodney W. Grimes static char *s; /* Current pointer inside string */ 1919b50d902SRodney W. Grimes static char string_ident[30]; 1929b50d902SRodney W. Grimes char *p; 1939b50d902SRodney W. Grimes 1949b50d902SRodney W. Grimes again: 1959b50d902SRodney W. Grimes switch (state) { 1969b50d902SRodney W. Grimes case ST_EOF: 197c56690efSArchie Cobbs if (script == NULL) { 198c56690efSArchie Cobbs if (more != NULL) 199c56690efSArchie Cobbs *more = 0; 2009b50d902SRodney W. Grimes return (NULL); 201c56690efSArchie Cobbs } 2029b50d902SRodney W. Grimes linenum = 0; 2039b50d902SRodney W. Grimes switch (script->type) { 2049b50d902SRodney W. Grimes case CU_FILE: 2059b50d902SRodney W. Grimes if ((f = fopen(script->s, "r")) == NULL) 20673a08bb2SPhilippe Charnier err(1, "%s", script->s); 2079b50d902SRodney W. Grimes fname = script->s; 2089b50d902SRodney W. Grimes state = ST_FILE; 2099b50d902SRodney W. Grimes goto again; 2109b50d902SRodney W. Grimes case CU_STRING: 2119b50d902SRodney W. Grimes if ((snprintf(string_ident, 2129b50d902SRodney W. Grimes sizeof(string_ident), "\"%s\"", script->s)) >= 2139b50d902SRodney W. Grimes sizeof(string_ident) - 1) 2149b50d902SRodney W. Grimes (void)strcpy(string_ident + 2159b50d902SRodney W. Grimes sizeof(string_ident) - 6, " ...\""); 2169b50d902SRodney W. Grimes fname = string_ident; 2179b50d902SRodney W. Grimes s = script->s; 2189b50d902SRodney W. Grimes state = ST_STRING; 2199b50d902SRodney W. Grimes goto again; 2209b50d902SRodney W. Grimes } 2219b50d902SRodney W. Grimes case ST_FILE: 2229b50d902SRodney W. Grimes if ((p = fgets(buf, n, f)) != NULL) { 2239b50d902SRodney W. Grimes linenum++; 2249b50d902SRodney W. Grimes if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 2259b50d902SRodney W. Grimes nflag = 1; 226c56690efSArchie Cobbs if (more != NULL) 227c56690efSArchie Cobbs *more = !feof(f); 2289b50d902SRodney W. Grimes return (p); 2299b50d902SRodney W. Grimes } 2309b50d902SRodney W. Grimes script = script->next; 2319b50d902SRodney W. Grimes (void)fclose(f); 2329b50d902SRodney W. Grimes state = ST_EOF; 2339b50d902SRodney W. Grimes goto again; 2349b50d902SRodney W. Grimes case ST_STRING: 2359b50d902SRodney W. Grimes if (linenum == 0 && s[0] == '#' && s[1] == 'n') 2369b50d902SRodney W. Grimes nflag = 1; 2379b50d902SRodney W. Grimes p = buf; 2389b50d902SRodney W. Grimes for (;;) { 2399b50d902SRodney W. Grimes if (n-- <= 1) { 2409b50d902SRodney W. Grimes *p = '\0'; 2419b50d902SRodney W. Grimes linenum++; 242c56690efSArchie Cobbs if (more != NULL) 243c56690efSArchie Cobbs *more = 1; 2449b50d902SRodney W. Grimes return (buf); 2459b50d902SRodney W. Grimes } 2469b50d902SRodney W. Grimes switch (*s) { 2479b50d902SRodney W. Grimes case '\0': 2489b50d902SRodney W. Grimes state = ST_EOF; 2499b50d902SRodney W. Grimes if (s == script->s) { 2509b50d902SRodney W. Grimes script = script->next; 2519b50d902SRodney W. Grimes goto again; 2529b50d902SRodney W. Grimes } else { 2539b50d902SRodney W. Grimes script = script->next; 2549b50d902SRodney W. Grimes *p = '\0'; 2559b50d902SRodney W. Grimes linenum++; 256c56690efSArchie Cobbs if (more != NULL) 257c56690efSArchie Cobbs *more = 0; 2589b50d902SRodney W. Grimes return (buf); 2599b50d902SRodney W. Grimes } 2609b50d902SRodney W. Grimes case '\n': 2619b50d902SRodney W. Grimes *p++ = '\n'; 2629b50d902SRodney W. Grimes *p = '\0'; 2639b50d902SRodney W. Grimes s++; 2649b50d902SRodney W. Grimes linenum++; 265c56690efSArchie Cobbs if (more != NULL) 266c56690efSArchie Cobbs *more = 0; 2679b50d902SRodney W. Grimes return (buf); 2689b50d902SRodney W. Grimes default: 2699b50d902SRodney W. Grimes *p++ = *s++; 2709b50d902SRodney W. Grimes } 2719b50d902SRodney W. Grimes } 2729b50d902SRodney W. Grimes } 2739b50d902SRodney W. Grimes /* NOTREACHED */ 27473a08bb2SPhilippe Charnier return (NULL); 2759b50d902SRodney W. Grimes } 2769b50d902SRodney W. Grimes 2779b50d902SRodney W. Grimes /* 2789b50d902SRodney W. Grimes * Like fgets, but go through the list of files chaining them together. 2799b50d902SRodney W. Grimes * Set len to the length of the line. 2809b50d902SRodney W. Grimes */ 2819b50d902SRodney W. Grimes int 2829b50d902SRodney W. Grimes mf_fgets(sp, spflag) 2839b50d902SRodney W. Grimes SPACE *sp; 2849b50d902SRodney W. Grimes enum e_spflag spflag; 2859b50d902SRodney W. Grimes { 2869b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 2879b50d902SRodney W. Grimes size_t len; 2885f4cf81eSWolfram Schneider char *p; 2895f4cf81eSWolfram Schneider int c; 2909b50d902SRodney W. Grimes 2919b50d902SRodney W. Grimes if (f == NULL) 2929b50d902SRodney W. Grimes /* Advance to first non-empty file */ 2939b50d902SRodney W. Grimes for (;;) { 2949b50d902SRodney W. Grimes if (files == NULL) { 2959b50d902SRodney W. Grimes lastline = 1; 2969b50d902SRodney W. Grimes return (0); 2979b50d902SRodney W. Grimes } 2989b50d902SRodney W. Grimes if (files->fname == NULL) { 2999b50d902SRodney W. Grimes f = stdin; 3009b50d902SRodney W. Grimes fname = "stdin"; 3019b50d902SRodney W. Grimes } else { 3029b50d902SRodney W. Grimes fname = files->fname; 3039b50d902SRodney W. Grimes if ((f = fopen(fname, "r")) == NULL) 30473a08bb2SPhilippe Charnier err(1, "%s", fname); 3059b50d902SRodney W. Grimes } 3065f4cf81eSWolfram Schneider if ((c = getc(f)) != EOF) { 3075f4cf81eSWolfram Schneider (void)ungetc(c, f); 3089b50d902SRodney W. Grimes break; 3099b50d902SRodney W. Grimes } 3109b50d902SRodney W. Grimes (void)fclose(f); 3119b50d902SRodney W. Grimes files = files->next; 3129b50d902SRodney W. Grimes } 3139b50d902SRodney W. Grimes 3149b50d902SRodney W. Grimes if (lastline) { 3159b50d902SRodney W. Grimes sp->len = 0; 3169b50d902SRodney W. Grimes return (0); 3179b50d902SRodney W. Grimes } 3189b50d902SRodney W. Grimes 3199b50d902SRodney W. Grimes /* 3209b50d902SRodney W. Grimes * Use fgetln so that we can handle essentially infinite input data. 3215f4cf81eSWolfram Schneider * Can't use the pointer into the stdio buffer as the process space 3225f4cf81eSWolfram Schneider * because the ungetc() can cause it to move. 3239b50d902SRodney W. Grimes */ 3249b50d902SRodney W. Grimes p = fgetln(f, &len); 3259b50d902SRodney W. Grimes if (ferror(f)) 32673a08bb2SPhilippe Charnier errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 3279b50d902SRodney W. Grimes cspace(sp, p, len, spflag); 3289b50d902SRodney W. Grimes 3299b50d902SRodney W. Grimes linenum++; 3309b50d902SRodney W. Grimes /* Advance to next non-empty file */ 3315f4cf81eSWolfram Schneider while ((c = getc(f)) == EOF) { 3329b50d902SRodney W. Grimes (void)fclose(f); 3339b50d902SRodney W. Grimes files = files->next; 3349b50d902SRodney W. Grimes if (files == NULL) { 3359b50d902SRodney W. Grimes lastline = 1; 3369b50d902SRodney W. Grimes return (1); 3379b50d902SRodney W. Grimes } 3389b50d902SRodney W. Grimes if (files->fname == NULL) { 3399b50d902SRodney W. Grimes f = stdin; 3409b50d902SRodney W. Grimes fname = "stdin"; 3419b50d902SRodney W. Grimes } else { 3429b50d902SRodney W. Grimes fname = files->fname; 3439b50d902SRodney W. Grimes if ((f = fopen(fname, "r")) == NULL) 34473a08bb2SPhilippe Charnier err(1, "%s", fname); 3459b50d902SRodney W. Grimes } 3469b50d902SRodney W. Grimes } 3475f4cf81eSWolfram Schneider (void)ungetc(c, f); 3489b50d902SRodney W. Grimes return (1); 3499b50d902SRodney W. Grimes } 3509b50d902SRodney W. Grimes 3519b50d902SRodney W. Grimes /* 3529b50d902SRodney W. Grimes * Add a compilation unit to the linked list 3539b50d902SRodney W. Grimes */ 3549b50d902SRodney W. Grimes static void 3559b50d902SRodney W. Grimes add_compunit(type, s) 3569b50d902SRodney W. Grimes enum e_cut type; 3579b50d902SRodney W. Grimes char *s; 3589b50d902SRodney W. Grimes { 3599b50d902SRodney W. Grimes struct s_compunit *cu; 3609b50d902SRodney W. Grimes 3619b50d902SRodney W. Grimes cu = xmalloc(sizeof(struct s_compunit)); 3629b50d902SRodney W. Grimes cu->type = type; 3639b50d902SRodney W. Grimes cu->s = s; 3649b50d902SRodney W. Grimes cu->next = NULL; 3659b50d902SRodney W. Grimes *cu_nextp = cu; 3669b50d902SRodney W. Grimes cu_nextp = &cu->next; 3679b50d902SRodney W. Grimes } 3689b50d902SRodney W. Grimes 3699b50d902SRodney W. Grimes /* 3709b50d902SRodney W. Grimes * Add a file to the linked list 3719b50d902SRodney W. Grimes */ 3729b50d902SRodney W. Grimes static void 3739b50d902SRodney W. Grimes add_file(s) 3749b50d902SRodney W. Grimes char *s; 3759b50d902SRodney W. Grimes { 3769b50d902SRodney W. Grimes struct s_flist *fp; 3779b50d902SRodney W. Grimes 3789b50d902SRodney W. Grimes fp = xmalloc(sizeof(struct s_flist)); 3799b50d902SRodney W. Grimes fp->next = NULL; 3809b50d902SRodney W. Grimes *fl_nextp = fp; 3819b50d902SRodney W. Grimes fp->fname = s; 3829b50d902SRodney W. Grimes fl_nextp = &fp->next; 3839b50d902SRodney W. Grimes } 384