2015-03-01 04:00:07 +03:00
|
|
|
/* $NetBSD: main.c,v 1.33 2015/03/01 01:00:07 asau Exp $ */
|
1997-01-09 23:18:21 +03:00
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
/*-
|
2014-06-06 04:13:13 +04:00
|
|
|
* Copyright (c) 2013 Johann 'Myrkraverk' Oskarsson.
|
|
|
|
* Copyright (c) 1992 Diomidis Spinellis.
|
1994-02-04 02:32:10 +03:00
|
|
|
* Copyright (c) 1992, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
1993-04-14 03:49:12 +04:00
|
|
|
*
|
|
|
|
* This code is derived from software contributed to Berkeley by
|
|
|
|
* Diomidis Spinellis of Imperial College, University of London.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
2003-08-07 15:13:06 +04:00
|
|
|
* 3. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2006-06-18 09:16:41 +04:00
|
|
|
#if HAVE_NBTOOL_CONFIG_H
|
|
|
|
#include "nbtool_config.h"
|
|
|
|
#endif
|
|
|
|
|
1997-10-20 03:05:11 +04:00
|
|
|
#include <sys/cdefs.h>
|
2015-03-01 04:00:07 +03:00
|
|
|
__RCSID("$NetBSD: main.c,v 1.33 2015/03/01 01:00:07 asau Exp $");
|
2014-06-06 04:13:13 +04:00
|
|
|
#ifdef __FBSDID
|
|
|
|
__FBSDID("$FreeBSD: head/usr.bin/sed/main.c 252231 2013-06-26 04:14:19Z pfg $");
|
|
|
|
#endif
|
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
#ifndef lint
|
2008-07-21 18:19:20 +04:00
|
|
|
__COPYRIGHT("@(#) Copyright (c) 1992, 1993\
|
2014-06-06 04:13:13 +04:00
|
|
|
The Regents of the University of California. All rights reserved.");
|
|
|
|
#endif
|
1993-04-14 03:49:12 +04:00
|
|
|
|
2014-06-26 06:14:32 +04:00
|
|
|
#if 0
|
|
|
|
static const char sccsid[] = "@(#)main.c 8.2 (Berkeley) 1/3/94";
|
|
|
|
#endif
|
|
|
|
|
1993-04-14 03:49:12 +04:00
|
|
|
#include <sys/types.h>
|
2014-06-06 04:13:13 +04:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/stat.h>
|
1993-04-14 03:49:12 +04:00
|
|
|
|
2014-06-06 04:13:13 +04:00
|
|
|
#include <err.h>
|
1993-04-14 03:49:12 +04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2010-02-19 19:35:27 +03:00
|
|
|
#include <limits.h>
|
2014-06-06 04:13:13 +04:00
|
|
|
#include <locale.h>
|
1993-04-14 03:49:12 +04:00
|
|
|
#include <regex.h>
|
|
|
|
#include <stddef.h>
|
2014-06-06 04:13:13 +04:00
|
|
|
#define _WITH_GETLINE
|
1993-04-14 03:49:12 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "defs.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Linked list pointer to compilation units and pointer to current
|
|
|
|
* next pointer.
|
|
|
|
*/
|
2015-03-01 00:56:53 +03:00
|
|
|
struct s_compunit *script;
|
|
|
|
static struct s_compunit **cu_nextp = &script;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Linked list pointer to files and pointer to current
|
|
|
|
* next pointer.
|
|
|
|
*/
|
2015-03-01 00:56:53 +03:00
|
|
|
struct s_flist *files;
|
|
|
|
static struct s_flist **fl_nextp = &files;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
2015-03-01 00:56:53 +03:00
|
|
|
int aflag;
|
|
|
|
static int eflag;
|
|
|
|
int nflag;
|
2014-06-06 04:13:13 +04:00
|
|
|
int rflags = 0;
|
|
|
|
|
2015-03-01 00:56:53 +03:00
|
|
|
int ispan; /* Whether inplace editing spans across files */
|
|
|
|
const char *inplace; /* Inplace edit file extension. */
|
1993-04-14 03:49:12 +04:00
|
|
|
|
2002-06-14 04:41:42 +04:00
|
|
|
static void add_compunit(enum e_cut, char *);
|
|
|
|
static void add_file(char *);
|
2014-06-06 16:46:54 +04:00
|
|
|
static void usage(void) __dead;
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
int
|
2002-06-14 04:41:42 +04:00
|
|
|
main(int argc, char *argv[])
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
2015-03-01 00:56:53 +03:00
|
|
|
int rval; /* Exit status */
|
1993-04-14 03:49:12 +04:00
|
|
|
int c, fflag;
|
2014-06-06 04:13:13 +04:00
|
|
|
char *temp_arg;
|
|
|
|
|
|
|
|
setprogname(argv[0]);
|
|
|
|
(void) setlocale(LC_ALL, "");
|
1993-04-14 03:49:12 +04:00
|
|
|
|
|
|
|
fflag = 0;
|
2014-06-06 04:13:13 +04:00
|
|
|
inplace = NULL;
|
|
|
|
|
2014-06-17 20:39:02 +04:00
|
|
|
while ((c = getopt(argc, argv, "EI::ae:f:i::lnru")) != -1)
|
1993-04-14 03:49:12 +04:00
|
|
|
switch (c) {
|
2014-06-06 04:13:13 +04:00
|
|
|
case 'r': /* Gnu sed compat */
|
|
|
|
case 'E':
|
|
|
|
rflags = REG_EXTENDED;
|
|
|
|
break;
|
|
|
|
case 'I':
|
|
|
|
inplace = optarg ? optarg : __UNCONST("");
|
|
|
|
ispan = 1; /* span across input files */
|
|
|
|
break;
|
1993-04-14 03:49:12 +04:00
|
|
|
case 'a':
|
|
|
|
aflag = 1;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
eflag = 1;
|
2014-06-06 04:13:13 +04:00
|
|
|
temp_arg = xmalloc(strlen(optarg) + 2);
|
|
|
|
strcpy(temp_arg, optarg);
|
|
|
|
strcat(temp_arg, "\n");
|
|
|
|
add_compunit(CU_STRING, temp_arg);
|
1993-04-14 03:49:12 +04:00
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
fflag = 1;
|
|
|
|
add_compunit(CU_FILE, optarg);
|
|
|
|
break;
|
2014-06-06 04:13:13 +04:00
|
|
|
case 'i':
|
|
|
|
inplace = optarg ? optarg : __UNCONST("");
|
|
|
|
ispan = 0; /* don't span across input files */
|
|
|
|
break;
|
|
|
|
case 'l':
|
2014-06-06 05:45:11 +04:00
|
|
|
#ifdef _IOLBF
|
|
|
|
c = setvbuf(stdout, NULL, _IOLBF, 0);
|
|
|
|
#else
|
|
|
|
c = setlinebuf(stdout);
|
|
|
|
#endif
|
|
|
|
if (c)
|
|
|
|
warn("setting line buffered output failed");
|
2014-06-06 04:13:13 +04:00
|
|
|
break;
|
1993-04-14 03:49:12 +04:00
|
|
|
case 'n':
|
|
|
|
nflag = 1;
|
|
|
|
break;
|
2014-06-17 20:39:02 +04:00
|
|
|
case 'u':
|
|
|
|
#ifdef _IONBF
|
|
|
|
c = setvbuf(stdout, NULL, _IONBF, 0);
|
|
|
|
#else
|
|
|
|
c = -1;
|
|
|
|
errno = EOPNOTSUPP;
|
|
|
|
#endif
|
|
|
|
if (c)
|
|
|
|
warn("setting unbuffered output failed");
|
|
|
|
break;
|
1993-04-14 03:49:12 +04:00
|
|
|
default:
|
|
|
|
case '?':
|
2014-06-06 04:13:13 +04:00
|
|
|
usage();
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
/* First usage case; script is the first arg */
|
|
|
|
if (!eflag && !fflag && *argv) {
|
|
|
|
add_compunit(CU_STRING, *argv);
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
compile();
|
|
|
|
|
|
|
|
/* Continue with first and start second usage */
|
|
|
|
if (*argv)
|
|
|
|
for (; *argv; argv++)
|
|
|
|
add_file(*argv);
|
|
|
|
else
|
|
|
|
add_file(NULL);
|
2015-03-01 00:56:53 +03:00
|
|
|
rval = process();
|
1993-04-14 03:49:12 +04:00
|
|
|
if (fclose(stdout))
|
2014-06-06 04:13:13 +04:00
|
|
|
err(1, "stdout");
|
|
|
|
exit(rval);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2014-06-17 20:39:02 +04:00
|
|
|
(void)fprintf(stderr,
|
2014-06-18 13:41:29 +04:00
|
|
|
"Usage: %s [-aElnru] command [file ...]\n"
|
2014-06-18 18:15:50 +04:00
|
|
|
"\t%s [-aElnru] [-e command] [-f command_file] [-I[extension]]\n"
|
|
|
|
"\t [-i[extension]] [file ...]\n", getprogname(), getprogname());
|
2014-06-06 04:13:13 +04:00
|
|
|
exit(1);
|
1993-04-14 03:49:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a compilation unit to the linked list
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-14 04:41:42 +04:00
|
|
|
add_compunit(enum e_cut type, char *s)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add a file to the linked list
|
|
|
|
*/
|
|
|
|
static void
|
2002-06-14 04:41:42 +04:00
|
|
|
add_file(char *s)
|
1993-04-14 03:49:12 +04:00
|
|
|
{
|
|
|
|
struct s_flist *fp;
|
|
|
|
|
|
|
|
fp = xmalloc(sizeof(struct s_flist));
|
|
|
|
fp->next = NULL;
|
|
|
|
*fl_nextp = fp;
|
|
|
|
fp->fname = s;
|
|
|
|
fl_nextp = &fp->next;
|
|
|
|
}
|