main.c revision c530934a778b6ba8ad55ddb0e3bbee8faa6260b0
/*
* Copyright 2010 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 1992 Diomidis Spinellis.
* Copyright (c) 1992, 1993
* The Regents of the University of California. All rights reserved.
*
* 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
* 4. 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.
*/
#include <err.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <locale.h>
#include <regex.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libintl.h>
#include "defs.h"
#include "extern.h"
/*
* Linked list of units (strings and files) to be compiled
*/
struct s_compunit {
struct s_compunit *next;
char *s; /* Pointer to string or fname */
};
/*
* Linked list pointer to compilation units and pointer to current
* next pointer.
*/
/*
* Linked list of files to be processed
*/
struct s_flist {
char *fname;
};
/*
* Linked list pointer to files and pointer to current
* next pointer.
*/
int rflags = 0;
static int rval; /* Exit status */
static int ispan; /* Whether inplace editing spans across files */
/*
* Current file and line number; line numbers restart across compilation
* units, but span across input files. The latter is optional if editing
* in place.
*/
const char *fname; /* File name. */
const char *outfname; /* Output file name */
static const char *inplace; /* Inplace edit file extension. */
static void add_compunit(enum e_cut, char *);
static void add_file(char *);
static void usage(void);
int
{
int c, fflag;
char *temp_arg;
#ifndef TEXT_DOMAIN
#define TEXT_DOMAIN "SYS_TEST"
#endif
(void) textdomain(TEXT_DOMAIN);
fflag = 0;
switch (c) {
case 'r': /* Gnu sed compat */
case 'E':
break;
case 'I':
break;
case 'a':
aflag = 1;
break;
case 'e':
eflag = 1;
break;
case 'f':
fflag = 1;
break;
case 'i':
ispan = 0; /* don't span across input files */
break;
case 'l':
/* On SunOS, setlinebuf "returns no useful value */
(void) setlinebuf(stdout);
break;
case 'n':
nflag = 1;
break;
default:
case '?':
usage();
}
/* First usage case; script is the first arg */
argv++;
}
compile();
/* Continue with first and start second usage */
if (*argv)
else
process();
return (rval);
}
static void
usage(void)
{
(void) fputs(_("usage: sed script [-Ealn] [-i extension] [file ...]\n"
" sed [-Ealn] [-i extension] [-e script] ... "
"[-f script_file] ... [file ...]"),
stderr);
exit(1);
}
/*
* Like fgets, but go through the chain of compilation units chaining them
* together. Empty strings and files are ignored.
*/
char *
{
static FILE *f; /* Current open file */
static char *s; /* Current pointer inside string */
static char string_ident[30];
char *p;
switch (state) {
case ST_EOF:
*more = 0;
return (NULL);
}
linenum = 0;
case CU_FILE:
goto again;
case CU_STRING:
sizeof (string_ident) - 1)
(void) strcpy(string_ident +
s = script->s;
goto again;
}
/*NOTREACHED*/
case ST_FILE:
linenum++;
nflag = 1;
return (p);
}
(void) fclose(f);
goto again;
case ST_STRING:
nflag = 1;
p = buf;
for (;;) {
if (n-- <= 1) {
*p = '\0';
linenum++;
*more = 1;
return (buf);
}
switch (*s) {
case '\0':
if (s == script->s) {
goto again;
} else {
*p = '\0';
linenum++;
*more = 0;
return (buf);
}
case '\n':
*p++ = '\n';
*p = '\0';
s++;
linenum++;
*more = 0;
return (buf);
default:
*p++ = *s++;
}
}
}
/* NOTREACHED */
return (NULL);
}
/*
* Like fgets, but go through the list of files chaining them together.
* Set len to the length of the line.
*/
int
{
char *p;
int c;
static int firstfile;
/* stdin? */
errx(1,
_("-I or -i may not be used with stdin"));
fname = "stdin";
outfname = "stdout";
}
firstfile = 1;
}
for (;;) {
break;
}
/* If we are here then either eof or no files are open yet */
return (0);
}
if (*oldfname != '\0') {
/* if there was a backup file, remove it */
/*
* Backup the original. Note that hard links
* are not supported on all filesystems.
*/
warn("rename()");
if (*tmpfname)
exit(1);
}
*oldfname = '\0';
}
if (*tmpfname != '\0') {
warn("fclose()");
exit(1);
}
/* this should not happen really! */
warn("rename()");
exit(1);
}
*tmpfname = '\0';
}
}
if (firstfile == 0)
else
firstfile = 0;
return (0);
}
fatal(_("in-place editing only "
"works for regular files"));
if (*inplace != '\0') {
sizeof (oldfname));
sizeof (oldfname));
fatal(_("name too long"));
}
fatal(_("name too long"));
/*
* Some file systems don't support chown or
* permissions will already be set to what
* they need to be.
*/
warn("fstat()");
}
!= 0))
warn("fchown()");
warn("fchmod()");
if (!ispan) {
linenum = 0;
resetstate();
}
} else {
outfname = "stdout";
}
rval = 1;
continue;
}
}
/*
* We are here only when infile is open and we still have something
* to read from it.
*
* Use fgetln so that we can handle essentially infinite input data.
* Can't use the pointer into the stdio buffer as the process space
* because the ungetc() can cause it to move.
*/
len--;
linenum++;
return (1);
}
/*
* Add a compilation unit to the linked list
*/
static void
{
struct s_compunit *cu;
cu->s = s;
}
/*
* Add a file to the linked list
*/
static void
add_file(char *s)
{
}
int
lastline(void)
{
int ch;
return (0);
return (1);
return (0);
}
char *
{
for (;;) {
char *nb;
}
}
/* END OF FILE */
break;
}
/* got the new line */
break;
}
}
return (buffer);
}