Home | History | Annotate | Line # | Download | only in sed
main.c revision 1.11
      1 /*	$NetBSD: main.c,v 1.11 2002/01/23 19:07:34 atatat Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1992 Diomidis Spinellis.
      5  * Copyright (c) 1992, 1993
      6  *	The Regents of the University of California.  All rights reserved.
      7  *
      8  * This code is derived from software contributed to Berkeley by
      9  * Diomidis Spinellis of Imperial College, University of London.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the University of
     22  *	California, Berkeley and its contributors.
     23  * 4. Neither the name of the University nor the names of its contributors
     24  *    may be used to endorse or promote products derived from this software
     25  *    without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     37  * SUCH DAMAGE.
     38  */
     39 
     40 #include <sys/cdefs.h>
     41 #ifndef lint
     42 __COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
     43 	The Regents of the University of California.  All rights reserved.\n");
     44 #endif /* not lint */
     45 
     46 #ifndef lint
     47 #if 0
     48 static char sccsid[] = "@(#)main.c	8.2 (Berkeley) 1/3/94";
     49 #else
     50 __RCSID("$NetBSD: main.c,v 1.11 2002/01/23 19:07:34 atatat Exp $");
     51 #endif
     52 #endif /* not lint */
     53 
     54 #include <sys/types.h>
     55 
     56 #include <ctype.h>
     57 #include <errno.h>
     58 #include <fcntl.h>
     59 #include <regex.h>
     60 #include <stddef.h>
     61 #include <stdio.h>
     62 #include <stdlib.h>
     63 #include <string.h>
     64 #include <unistd.h>
     65 
     66 #include "defs.h"
     67 #include "extern.h"
     68 
     69 /*
     70  * Linked list of units (strings and files) to be compiled
     71  */
     72 struct s_compunit {
     73 	struct s_compunit *next;
     74 	enum e_cut {CU_FILE, CU_STRING} type;
     75 	char *s;			/* Pointer to string or fname */
     76 };
     77 
     78 /*
     79  * Linked list pointer to compilation units and pointer to current
     80  * next pointer.
     81  */
     82 static struct s_compunit *script, **cu_nextp = &script;
     83 
     84 /*
     85  * Linked list of files to be processed
     86  */
     87 struct s_flist {
     88 	char *fname;
     89 	struct s_flist *next;
     90 };
     91 
     92 /*
     93  * Linked list pointer to files and pointer to current
     94  * next pointer.
     95  */
     96 static struct s_flist *files, **fl_nextp = &files;
     97 
     98 int aflag, eflag, nflag, ere;
     99 
    100 /*
    101  * Current file and line number; line numbers restart across compilation
    102  * units, but span across input files.
    103  */
    104 char *fname;			/* File name. */
    105 u_long linenum;
    106 int lastline;			/* TRUE on the last line of the last file */
    107 
    108 static void add_compunit __P((enum e_cut, char *));
    109 static void add_file __P((char *));
    110 int	main __P((int, char **));
    111 
    112 int
    113 main(argc, argv)
    114 	int argc;
    115 	char *argv[];
    116 {
    117 	int c, fflag;
    118 
    119 	fflag = 0;
    120 	while ((c = getopt(argc, argv, "ae:f:nE")) != -1)
    121 		switch (c) {
    122 		case 'a':
    123 			aflag = 1;
    124 			break;
    125 		case 'e':
    126 			eflag = 1;
    127 			add_compunit(CU_STRING, optarg);
    128 			break;
    129 		case 'f':
    130 			fflag = 1;
    131 			add_compunit(CU_FILE, optarg);
    132 			break;
    133 		case 'n':
    134 			nflag = 1;
    135 			break;
    136 		case 'E':
    137 			ere = REG_EXTENDED;
    138 			break;
    139 		default:
    140 		case '?':
    141 			(void)fprintf(stderr,
    142 "usage:\t%p script [-anE] [file ...]\n\tsed [-an] [-e script] ... [-f script_file] ... [file ...]\n",
    143 			    getprogname());
    144 			exit(1);
    145 		}
    146 	argc -= optind;
    147 	argv += optind;
    148 
    149 	/* First usage case; script is the first arg */
    150 	if (!eflag && !fflag && *argv) {
    151 		add_compunit(CU_STRING, *argv);
    152 		argv++;
    153 	}
    154 
    155 	compile();
    156 
    157 	/* Continue with first and start second usage */
    158 	if (*argv)
    159 		for (; *argv; argv++)
    160 			add_file(*argv);
    161 	else
    162 		add_file(NULL);
    163 	process();
    164 	cfclose(prog, NULL);
    165 	if (fclose(stdout))
    166 		err(FATAL, "stdout: %s", strerror(errno));
    167 	exit (0);
    168 }
    169 
    170 /*
    171  * Like fgets, but go through the chain of compilation units chaining them
    172  * together.  Empty strings and files are ignored.
    173  */
    174 char *
    175 cu_fgets(buf, n)
    176 	char *buf;
    177 	int n;
    178 {
    179 	static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
    180 	static FILE *f;		/* Current open file */
    181 	static char *s;		/* Current pointer inside string */
    182 	static char string_ident[30];
    183 	char *p;
    184 
    185 again:
    186 	switch (state) {
    187 	case ST_EOF:
    188 		if (script == NULL)
    189 			return (NULL);
    190 		linenum = 0;
    191 		switch (script->type) {
    192 		case CU_FILE:
    193 			if ((f = fopen(script->s, "r")) == NULL)
    194 				err(FATAL,
    195 				    "%s: %s", script->s, strerror(errno));
    196 			fname = script->s;
    197 			state = ST_FILE;
    198 			goto again;
    199 		case CU_STRING:
    200 			if ((snprintf(string_ident,
    201 			    sizeof(string_ident), "\"%s\"", script->s)) >=
    202 			    sizeof(string_ident) - 1)
    203 				(void)strcpy(string_ident +
    204 				    sizeof(string_ident) - 6, " ...\"");
    205 			fname = string_ident;
    206 			s = script->s;
    207 			state = ST_STRING;
    208 			goto again;
    209 		}
    210 	case ST_FILE:
    211 		if ((p = fgets(buf, n, f)) != NULL) {
    212 			linenum++;
    213 			if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
    214 				nflag = 1;
    215 			return (p);
    216 		}
    217 		script = script->next;
    218 		(void)fclose(f);
    219 		state = ST_EOF;
    220 		goto again;
    221 	case ST_STRING:
    222 		if (linenum == 0 && s[0] == '#' && s[1] == 'n')
    223 			nflag = 1;
    224 		p = buf;
    225 		for (;;) {
    226 			if (n-- <= 1) {
    227 				*p = '\0';
    228 				linenum++;
    229 				return (buf);
    230 			}
    231 			switch (*s) {
    232 			case '\0':
    233 				state = ST_EOF;
    234 				if (s == script->s) {
    235 					script = script->next;
    236 					goto again;
    237 				} else {
    238 					script = script->next;
    239 					*p = '\0';
    240 					linenum++;
    241 					return (buf);
    242 				}
    243 			case '\n':
    244 				*p++ = '\n';
    245 				*p = '\0';
    246 				s++;
    247 				linenum++;
    248 				return (buf);
    249 			default:
    250 				*p++ = *s++;
    251 			}
    252 		}
    253 	}
    254 	/* NOTREACHED */
    255 	return (NULL);
    256 }
    257 
    258 /*
    259  * Like fgets, but go through the list of files chaining them together.
    260  * Set len to the length of the line.
    261  */
    262 int
    263 mf_fgets(sp, spflag)
    264 	SPACE *sp;
    265 	enum e_spflag spflag;
    266 {
    267 	static FILE *f;		/* Current open file */
    268 	size_t len;
    269 	char *p;
    270 	int c;
    271 
    272 	if (f == NULL)
    273 		/* Advance to first non-empty file */
    274 		for (;;) {
    275 			if (files == NULL) {
    276 				lastline = 1;
    277 				return (0);
    278 			}
    279 			if (files->fname == NULL) {
    280 				f = stdin;
    281 				fname = "stdin";
    282 			} else {
    283 				fname = files->fname;
    284 				if ((f = fopen(fname, "r")) == NULL)
    285 					err(FATAL, "%s: %s",
    286 					    fname, strerror(errno));
    287 			}
    288 			if ((c = getc(f)) != EOF) {
    289 				(void)ungetc(c, f);
    290 				break;
    291 			}
    292 			(void)fclose(f);
    293 			files = files->next;
    294 		}
    295 
    296 	if (lastline) {
    297 		sp->len = 0;
    298 		return (0);
    299 	}
    300 
    301 	/*
    302 	 * Use fgetln so that we can handle essentially infinite input data.
    303 	 * Can't use the pointer into the stdio buffer as the process space
    304 	 * because the ungetc() can cause it to move.
    305 	 */
    306 	p = fgetln(f, &len);
    307 	if (ferror(f))
    308 		err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
    309 	cspace(sp, p, len, spflag);
    310 
    311 	linenum++;
    312 	/* Advance to next non-empty file */
    313 	while ((c = getc(f)) == EOF) {
    314 		(void)fclose(f);
    315 		files = files->next;
    316 		if (files == NULL) {
    317 			lastline = 1;
    318 			return (1);
    319 		}
    320 		if (files->fname == NULL) {
    321 			f = stdin;
    322 			fname = "stdin";
    323 		} else {
    324 			fname = files->fname;
    325 			if ((f = fopen(fname, "r")) == NULL)
    326 				err(FATAL, "%s: %s", fname, strerror(errno));
    327 		}
    328 	}
    329 	(void)ungetc(c, f);
    330 	return (1);
    331 }
    332 
    333 /*
    334  * Add a compilation unit to the linked list
    335  */
    336 static void
    337 add_compunit(type, s)
    338 	enum e_cut type;
    339 	char *s;
    340 {
    341 	struct s_compunit *cu;
    342 
    343 	cu = xmalloc(sizeof(struct s_compunit));
    344 	cu->type = type;
    345 	cu->s = s;
    346 	cu->next = NULL;
    347 	*cu_nextp = cu;
    348 	cu_nextp = &cu->next;
    349 }
    350 
    351 /*
    352  * Add a file to the linked list
    353  */
    354 static void
    355 add_file(s)
    356 	char *s;
    357 {
    358 	struct s_flist *fp;
    359 
    360 	fp = xmalloc(sizeof(struct s_flist));
    361 	fp->next = NULL;
    362 	*fl_nextp = fp;
    363 	fp->fname = s;
    364 	fl_nextp = &fp->next;
    365 }
    366