morpheus-base

morpheus base system
git clone git://git.2f30.org/morpheus-base
Log | Files | Refs

main.c (8200B)


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