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[] = 49c56690efSArchie Cobbs "$Id: main.c,v 1.7 1997/08/11 07:21:03 charnier Exp $"; 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; 989b50d902SRodney W. Grimes 999b50d902SRodney W. Grimes /* 1009b50d902SRodney W. Grimes * Current file and line number; line numbers restart across compilation 1019b50d902SRodney W. Grimes * units, but span across input files. 1029b50d902SRodney W. Grimes */ 1039b50d902SRodney W. Grimes char *fname; /* File name. */ 1049b50d902SRodney W. Grimes u_long linenum; 1059b50d902SRodney W. Grimes int lastline; /* TRUE on the last line of the last file */ 1069b50d902SRodney W. Grimes 1079b50d902SRodney W. Grimes static void add_compunit __P((enum e_cut, char *)); 1089b50d902SRodney W. Grimes static void add_file __P((char *)); 10973a08bb2SPhilippe Charnier static void usage __P((void)); 1109b50d902SRodney W. Grimes 1119b50d902SRodney W. Grimes int 1129b50d902SRodney W. Grimes main(argc, argv) 1139b50d902SRodney W. Grimes int argc; 1149b50d902SRodney W. Grimes char *argv[]; 1159b50d902SRodney W. Grimes { 1169b50d902SRodney W. Grimes int c, fflag; 1179b50d902SRodney W. Grimes 118726aebe5SAndrey A. Chernov (void) setlocale(LC_ALL, ""); 119726aebe5SAndrey A. Chernov 1209b50d902SRodney W. Grimes fflag = 0; 1211c8af878SWarner Losh while ((c = getopt(argc, argv, "ae:f:n")) != -1) 1229b50d902SRodney W. Grimes switch (c) { 1239b50d902SRodney W. Grimes case 'a': 1249b50d902SRodney W. Grimes aflag = 1; 1259b50d902SRodney W. Grimes break; 1269b50d902SRodney W. Grimes case 'e': 1279b50d902SRodney W. Grimes eflag = 1; 1289b50d902SRodney W. Grimes add_compunit(CU_STRING, optarg); 1299b50d902SRodney W. Grimes break; 1309b50d902SRodney W. Grimes case 'f': 1319b50d902SRodney W. Grimes fflag = 1; 1329b50d902SRodney W. Grimes add_compunit(CU_FILE, optarg); 1339b50d902SRodney W. Grimes break; 1349b50d902SRodney W. Grimes case 'n': 1359b50d902SRodney W. Grimes nflag = 1; 1369b50d902SRodney W. Grimes break; 1379b50d902SRodney W. Grimes default: 1389b50d902SRodney W. Grimes case '?': 13973a08bb2SPhilippe Charnier usage(); 1409b50d902SRodney W. Grimes } 1419b50d902SRodney W. Grimes argc -= optind; 1429b50d902SRodney W. Grimes argv += optind; 1439b50d902SRodney W. Grimes 1449b50d902SRodney W. Grimes /* First usage case; script is the first arg */ 1459b50d902SRodney W. Grimes if (!eflag && !fflag && *argv) { 1469b50d902SRodney W. Grimes add_compunit(CU_STRING, *argv); 1479b50d902SRodney W. Grimes argv++; 1489b50d902SRodney W. Grimes } 1499b50d902SRodney W. Grimes 1509b50d902SRodney W. Grimes compile(); 1519b50d902SRodney W. Grimes 1529b50d902SRodney W. Grimes /* Continue with first and start second usage */ 1539b50d902SRodney W. Grimes if (*argv) 1549b50d902SRodney W. Grimes for (; *argv; argv++) 1559b50d902SRodney W. Grimes add_file(*argv); 1569b50d902SRodney W. Grimes else 1579b50d902SRodney W. Grimes add_file(NULL); 1589b50d902SRodney W. Grimes process(); 1599b50d902SRodney W. Grimes cfclose(prog, NULL); 1609b50d902SRodney W. Grimes if (fclose(stdout)) 16173a08bb2SPhilippe Charnier err(1, "stdout"); 1629b50d902SRodney W. Grimes exit (0); 1639b50d902SRodney W. Grimes } 1649b50d902SRodney W. Grimes 16573a08bb2SPhilippe Charnier static void 16673a08bb2SPhilippe Charnier usage() 16773a08bb2SPhilippe Charnier { 16873a08bb2SPhilippe Charnier (void)fprintf(stderr, "%s\n%s\n", 16973a08bb2SPhilippe Charnier "usage: sed script [-an] [file ...]", 17073a08bb2SPhilippe Charnier " sed [-an] [-e script] ... [-f script_file] ... [file ...]"); 17173a08bb2SPhilippe Charnier exit(1); 17273a08bb2SPhilippe Charnier } 17373a08bb2SPhilippe Charnier 1749b50d902SRodney W. Grimes /* 1759b50d902SRodney W. Grimes * Like fgets, but go through the chain of compilation units chaining them 1769b50d902SRodney W. Grimes * together. Empty strings and files are ignored. 1779b50d902SRodney W. Grimes */ 1789b50d902SRodney W. Grimes char * 179c56690efSArchie Cobbs cu_fgets(buf, n, more) 1809b50d902SRodney W. Grimes char *buf; 1819b50d902SRodney W. Grimes int n; 182c56690efSArchie Cobbs int *more; 1839b50d902SRodney W. Grimes { 1849b50d902SRodney W. Grimes static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF; 1859b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 1869b50d902SRodney W. Grimes static char *s; /* Current pointer inside string */ 1879b50d902SRodney W. Grimes static char string_ident[30]; 1889b50d902SRodney W. Grimes char *p; 1899b50d902SRodney W. Grimes 1909b50d902SRodney W. Grimes again: 1919b50d902SRodney W. Grimes switch (state) { 1929b50d902SRodney W. Grimes case ST_EOF: 193c56690efSArchie Cobbs if (script == NULL) { 194c56690efSArchie Cobbs if (more != NULL) 195c56690efSArchie Cobbs *more = 0; 1969b50d902SRodney W. Grimes return (NULL); 197c56690efSArchie Cobbs } 1989b50d902SRodney W. Grimes linenum = 0; 1999b50d902SRodney W. Grimes switch (script->type) { 2009b50d902SRodney W. Grimes case CU_FILE: 2019b50d902SRodney W. Grimes if ((f = fopen(script->s, "r")) == NULL) 20273a08bb2SPhilippe Charnier err(1, "%s", script->s); 2039b50d902SRodney W. Grimes fname = script->s; 2049b50d902SRodney W. Grimes state = ST_FILE; 2059b50d902SRodney W. Grimes goto again; 2069b50d902SRodney W. Grimes case CU_STRING: 2079b50d902SRodney W. Grimes if ((snprintf(string_ident, 2089b50d902SRodney W. Grimes sizeof(string_ident), "\"%s\"", script->s)) >= 2099b50d902SRodney W. Grimes sizeof(string_ident) - 1) 2109b50d902SRodney W. Grimes (void)strcpy(string_ident + 2119b50d902SRodney W. Grimes sizeof(string_ident) - 6, " ...\""); 2129b50d902SRodney W. Grimes fname = string_ident; 2139b50d902SRodney W. Grimes s = script->s; 2149b50d902SRodney W. Grimes state = ST_STRING; 2159b50d902SRodney W. Grimes goto again; 2169b50d902SRodney W. Grimes } 2179b50d902SRodney W. Grimes case ST_FILE: 2189b50d902SRodney W. Grimes if ((p = fgets(buf, n, f)) != NULL) { 2199b50d902SRodney W. Grimes linenum++; 2209b50d902SRodney W. Grimes if (linenum == 1 && buf[0] == '#' && buf[1] == 'n') 2219b50d902SRodney W. Grimes nflag = 1; 222c56690efSArchie Cobbs if (more != NULL) 223c56690efSArchie Cobbs *more = !feof(f); 2249b50d902SRodney W. Grimes return (p); 2259b50d902SRodney W. Grimes } 2269b50d902SRodney W. Grimes script = script->next; 2279b50d902SRodney W. Grimes (void)fclose(f); 2289b50d902SRodney W. Grimes state = ST_EOF; 2299b50d902SRodney W. Grimes goto again; 2309b50d902SRodney W. Grimes case ST_STRING: 2319b50d902SRodney W. Grimes if (linenum == 0 && s[0] == '#' && s[1] == 'n') 2329b50d902SRodney W. Grimes nflag = 1; 2339b50d902SRodney W. Grimes p = buf; 2349b50d902SRodney W. Grimes for (;;) { 2359b50d902SRodney W. Grimes if (n-- <= 1) { 2369b50d902SRodney W. Grimes *p = '\0'; 2379b50d902SRodney W. Grimes linenum++; 238c56690efSArchie Cobbs if (more != NULL) 239c56690efSArchie Cobbs *more = 1; 2409b50d902SRodney W. Grimes return (buf); 2419b50d902SRodney W. Grimes } 2429b50d902SRodney W. Grimes switch (*s) { 2439b50d902SRodney W. Grimes case '\0': 2449b50d902SRodney W. Grimes state = ST_EOF; 2459b50d902SRodney W. Grimes if (s == script->s) { 2469b50d902SRodney W. Grimes script = script->next; 2479b50d902SRodney W. Grimes goto again; 2489b50d902SRodney W. Grimes } else { 2499b50d902SRodney W. Grimes script = script->next; 2509b50d902SRodney W. Grimes *p = '\0'; 2519b50d902SRodney W. Grimes linenum++; 252c56690efSArchie Cobbs if (more != NULL) 253c56690efSArchie Cobbs *more = 0; 2549b50d902SRodney W. Grimes return (buf); 2559b50d902SRodney W. Grimes } 2569b50d902SRodney W. Grimes case '\n': 2579b50d902SRodney W. Grimes *p++ = '\n'; 2589b50d902SRodney W. Grimes *p = '\0'; 2599b50d902SRodney W. Grimes s++; 2609b50d902SRodney W. Grimes linenum++; 261c56690efSArchie Cobbs if (more != NULL) 262c56690efSArchie Cobbs *more = 0; 2639b50d902SRodney W. Grimes return (buf); 2649b50d902SRodney W. Grimes default: 2659b50d902SRodney W. Grimes *p++ = *s++; 2669b50d902SRodney W. Grimes } 2679b50d902SRodney W. Grimes } 2689b50d902SRodney W. Grimes } 2699b50d902SRodney W. Grimes /* NOTREACHED */ 27073a08bb2SPhilippe Charnier return (NULL); 2719b50d902SRodney W. Grimes } 2729b50d902SRodney W. Grimes 2739b50d902SRodney W. Grimes /* 2749b50d902SRodney W. Grimes * Like fgets, but go through the list of files chaining them together. 2759b50d902SRodney W. Grimes * Set len to the length of the line. 2769b50d902SRodney W. Grimes */ 2779b50d902SRodney W. Grimes int 2789b50d902SRodney W. Grimes mf_fgets(sp, spflag) 2799b50d902SRodney W. Grimes SPACE *sp; 2809b50d902SRodney W. Grimes enum e_spflag spflag; 2819b50d902SRodney W. Grimes { 2829b50d902SRodney W. Grimes static FILE *f; /* Current open file */ 2839b50d902SRodney W. Grimes size_t len; 2845f4cf81eSWolfram Schneider char *p; 2855f4cf81eSWolfram Schneider int c; 2869b50d902SRodney W. Grimes 2879b50d902SRodney W. Grimes if (f == NULL) 2889b50d902SRodney W. Grimes /* Advance to first non-empty file */ 2899b50d902SRodney W. Grimes for (;;) { 2909b50d902SRodney W. Grimes if (files == NULL) { 2919b50d902SRodney W. Grimes lastline = 1; 2929b50d902SRodney W. Grimes return (0); 2939b50d902SRodney W. Grimes } 2949b50d902SRodney W. Grimes if (files->fname == NULL) { 2959b50d902SRodney W. Grimes f = stdin; 2969b50d902SRodney W. Grimes fname = "stdin"; 2979b50d902SRodney W. Grimes } else { 2989b50d902SRodney W. Grimes fname = files->fname; 2999b50d902SRodney W. Grimes if ((f = fopen(fname, "r")) == NULL) 30073a08bb2SPhilippe Charnier err(1, "%s", fname); 3019b50d902SRodney W. Grimes } 3025f4cf81eSWolfram Schneider if ((c = getc(f)) != EOF) { 3035f4cf81eSWolfram Schneider (void)ungetc(c, f); 3049b50d902SRodney W. Grimes break; 3059b50d902SRodney W. Grimes } 3069b50d902SRodney W. Grimes (void)fclose(f); 3079b50d902SRodney W. Grimes files = files->next; 3089b50d902SRodney W. Grimes } 3099b50d902SRodney W. Grimes 3109b50d902SRodney W. Grimes if (lastline) { 3119b50d902SRodney W. Grimes sp->len = 0; 3129b50d902SRodney W. Grimes return (0); 3139b50d902SRodney W. Grimes } 3149b50d902SRodney W. Grimes 3159b50d902SRodney W. Grimes /* 3169b50d902SRodney W. Grimes * Use fgetln so that we can handle essentially infinite input data. 3175f4cf81eSWolfram Schneider * Can't use the pointer into the stdio buffer as the process space 3185f4cf81eSWolfram Schneider * because the ungetc() can cause it to move. 3199b50d902SRodney W. Grimes */ 3209b50d902SRodney W. Grimes p = fgetln(f, &len); 3219b50d902SRodney W. Grimes if (ferror(f)) 32273a08bb2SPhilippe Charnier errx(1, "%s: %s", fname, strerror(errno ? errno : EIO)); 3239b50d902SRodney W. Grimes cspace(sp, p, len, spflag); 3249b50d902SRodney W. Grimes 3259b50d902SRodney W. Grimes linenum++; 3269b50d902SRodney W. Grimes /* Advance to next non-empty file */ 3275f4cf81eSWolfram Schneider while ((c = getc(f)) == EOF) { 3289b50d902SRodney W. Grimes (void)fclose(f); 3299b50d902SRodney W. Grimes files = files->next; 3309b50d902SRodney W. Grimes if (files == NULL) { 3319b50d902SRodney W. Grimes lastline = 1; 3329b50d902SRodney W. Grimes return (1); 3339b50d902SRodney W. Grimes } 3349b50d902SRodney W. Grimes if (files->fname == NULL) { 3359b50d902SRodney W. Grimes f = stdin; 3369b50d902SRodney W. Grimes fname = "stdin"; 3379b50d902SRodney W. Grimes } else { 3389b50d902SRodney W. Grimes fname = files->fname; 3399b50d902SRodney W. Grimes if ((f = fopen(fname, "r")) == NULL) 34073a08bb2SPhilippe Charnier err(1, "%s", fname); 3419b50d902SRodney W. Grimes } 3429b50d902SRodney W. Grimes } 3435f4cf81eSWolfram Schneider (void)ungetc(c, f); 3449b50d902SRodney W. Grimes return (1); 3459b50d902SRodney W. Grimes } 3469b50d902SRodney W. Grimes 3479b50d902SRodney W. Grimes /* 3489b50d902SRodney W. Grimes * Add a compilation unit to the linked list 3499b50d902SRodney W. Grimes */ 3509b50d902SRodney W. Grimes static void 3519b50d902SRodney W. Grimes add_compunit(type, s) 3529b50d902SRodney W. Grimes enum e_cut type; 3539b50d902SRodney W. Grimes char *s; 3549b50d902SRodney W. Grimes { 3559b50d902SRodney W. Grimes struct s_compunit *cu; 3569b50d902SRodney W. Grimes 3579b50d902SRodney W. Grimes cu = xmalloc(sizeof(struct s_compunit)); 3589b50d902SRodney W. Grimes cu->type = type; 3599b50d902SRodney W. Grimes cu->s = s; 3609b50d902SRodney W. Grimes cu->next = NULL; 3619b50d902SRodney W. Grimes *cu_nextp = cu; 3629b50d902SRodney W. Grimes cu_nextp = &cu->next; 3639b50d902SRodney W. Grimes } 3649b50d902SRodney W. Grimes 3659b50d902SRodney W. Grimes /* 3669b50d902SRodney W. Grimes * Add a file to the linked list 3679b50d902SRodney W. Grimes */ 3689b50d902SRodney W. Grimes static void 3699b50d902SRodney W. Grimes add_file(s) 3709b50d902SRodney W. Grimes char *s; 3719b50d902SRodney W. Grimes { 3729b50d902SRodney W. Grimes struct s_flist *fp; 3739b50d902SRodney W. Grimes 3749b50d902SRodney W. Grimes fp = xmalloc(sizeof(struct s_flist)); 3759b50d902SRodney W. Grimes fp->next = NULL; 3769b50d902SRodney W. Grimes *fl_nextp = fp; 3779b50d902SRodney W. Grimes fp->fname = s; 3789b50d902SRodney W. Grimes fl_nextp = &fp->next; 3799b50d902SRodney W. Grimes } 380