#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1992, 1993\n\
The Regents of the University of California. All rights reserved.\n");
#endif
#ifndef lint
#if 0
static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
#else
__RCSID("$NetBSD: main.c,v 1.10 1997/10/19 23:05:14 lukem Exp $");
#endif
#endif
#include <sys/types.h>
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <regex.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "defs.h"
#include "extern.h"
struct s_compunit {
struct s_compunit *next;
enum e_cut {CU_FILE, CU_STRING} type;
char *s;
};
static struct s_compunit *script, **cu_nextp = &script;
struct s_flist {
char *fname;
struct s_flist *next;
};
static struct s_flist *files, **fl_nextp = &files;
int aflag, eflag, nflag;
char *fname;
u_long linenum;
int lastline;
static void add_compunit __P((enum e_cut, char *));
static void add_file __P((char *));
int main __P((int, char **));
int
main(argc, argv)
int argc;
char *argv[];
{
int c, fflag;
fflag = 0;
while ((c = getopt(argc, argv, "ae:f:n")) != -1)
switch (c) {
case 'a':
aflag = 1;
break;
case 'e':
eflag = 1;
add_compunit(CU_STRING, optarg);
break;
case 'f':
fflag = 1;
add_compunit(CU_FILE, optarg);
break;
case 'n':
nflag = 1;
break;
default:
case '?':
(void)fprintf(stderr,
"usage:\tsed script [-an] [file ...]\n\tsed [-an] [-e script] ... [-f script_file] ... [file ...]\n");
exit(1);
}
argc -= optind;
argv += optind;
if (!eflag && !fflag && *argv) {
add_compunit(CU_STRING, *argv);
argv++;
}
compile();
if (*argv)
for (; *argv; argv++)
add_file(*argv);
else
add_file(NULL);
process();
cfclose(prog, NULL);
if (fclose(stdout))
err(FATAL, "stdout: %s", strerror(errno));
exit (0);
}
char *
cu_fgets(buf, n)
char *buf;
int n;
{
static enum {ST_EOF, ST_FILE, ST_STRING} state = ST_EOF;
static FILE *f;
static char *s;
static char string_ident[30];
char *p;
again:
switch (state) {
case ST_EOF:
if (script == NULL)
return (NULL);
linenum = 0;
switch (script->type) {
case CU_FILE:
if ((f = fopen(script->s, "r")) == NULL)
err(FATAL,
"%s: %s", script->s, strerror(errno));
fname = script->s;
state = ST_FILE;
goto again;
case CU_STRING:
if ((snprintf(string_ident,
sizeof(string_ident), "\"%s\"", script->s)) >=
sizeof(string_ident) - 1)
(void)strcpy(string_ident +
sizeof(string_ident) - 6, " ...\"");
fname = string_ident;
s = script->s;
state = ST_STRING;
goto again;
}
case ST_FILE:
if ((p = fgets(buf, n, f)) != NULL) {
linenum++;
if (linenum == 1 && buf[0] == '#' && buf[1] == 'n')
nflag = 1;
return (p);
}
script = script->next;
(void)fclose(f);
state = ST_EOF;
goto again;
case ST_STRING:
if (linenum == 0 && s[0] == '#' && s[1] == 'n')
nflag = 1;
p = buf;
for (;;) {
if (n-- <= 1) {
*p = '\0';
linenum++;
return (buf);
}
switch (*s) {
case '\0':
state = ST_EOF;
if (s == script->s) {
script = script->next;
goto again;
} else {
script = script->next;
*p = '\0';
linenum++;
return (buf);
}
case '\n':
*p++ = '\n';
*p = '\0';
s++;
linenum++;
return (buf);
default:
*p++ = *s++;
}
}
}
return (NULL);
}
int
mf_fgets(sp, spflag)
SPACE *sp;
enum e_spflag spflag;
{
static FILE *f;
size_t len;
char *p;
int c;
if (f == NULL)
for (;;) {
if (files == NULL) {
lastline = 1;
return (0);
}
if (files->fname == NULL) {
f = stdin;
fname = "stdin";
} else {
fname = files->fname;
if ((f = fopen(fname, "r")) == NULL)
err(FATAL, "%s: %s",
fname, strerror(errno));
}
if ((c = getc(f)) != EOF) {
(void)ungetc(c, f);
break;
}
(void)fclose(f);
files = files->next;
}
if (lastline) {
sp->len = 0;
return (0);
}
p = fgetln(f, &len);
if (ferror(f))
err(FATAL, "%s: %s", fname, strerror(errno ? errno : EIO));
cspace(sp, p, len, spflag);
linenum++;
while ((c = getc(f)) == EOF) {
(void)fclose(f);
files = files->next;
if (files == NULL) {
lastline = 1;
return (1);
}
if (files->fname == NULL) {
f = stdin;
fname = "stdin";
} else {
fname = files->fname;
if ((f = fopen(fname, "r")) == NULL)
err(FATAL, "%s: %s", fname, strerror(errno));
}
}
(void)ungetc(c, f);
return (1);
}
static void
add_compunit(type, s)
enum e_cut type;
char *s;
{
struct s_compunit *cu;
cu = xmalloc(sizeof(struct s_compunit));
cu->type = type;
cu->s = s;
cu->next = NULL;
*cu_nextp = cu;
cu_nextp = &cu->next;
}
static void
add_file(s)
char *s;
{
struct s_flist *fp;
fp = xmalloc(sizeof(struct s_flist));
fp->next = NULL;
*fl_nextp = fp;
fp->fname = s;
fl_nextp = &fp->next;
}