2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-28 16:06:25 +00:00
|
|
|
/*
|
|
|
|
* Mini sed implementation for busybox
|
|
|
|
*
|
|
|
|
*
|
2000-04-13 01:18:56 +00:00
|
|
|
* Copyright (C) 1999,2000 by Lineo, inc.
|
1999-10-28 16:06:25 +00:00
|
|
|
* Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
|
|
|
|
*
|
1999-12-29 22:19:46 +00:00
|
|
|
* Modifications for addresses and append command have been
|
|
|
|
* written by Marco Pantaleoni <panta@prosa.it>, <panta@elasticworld.org>
|
|
|
|
* and are:
|
|
|
|
* Copyright (C) 1999 Marco Pantaleoni.
|
|
|
|
*
|
1999-10-28 16:06:25 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "internal.h"
|
|
|
|
#include "regexp.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
|
1999-12-29 22:19:46 +00:00
|
|
|
static const char sed_usage[] =
|
2000-05-12 19:41:47 +00:00
|
|
|
"sed [-n] -e script [file...]\n"
|
|
|
|
#ifndef BB_FEATURE_TRIVIAL_HELP
|
|
|
|
"\nAllowed sed scripts come in the following form:\n"
|
2000-02-08 19:58:47 +00:00
|
|
|
"\t'ADDR [!] COMMAND'\n\n"
|
|
|
|
"\twhere address ADDR can be:\n"
|
|
|
|
"\t NUMBER Match specified line number\n"
|
|
|
|
"\t $ Match last line\n"
|
|
|
|
"\t /REGEXP/ Match specified regexp\n"
|
|
|
|
"\t (! inverts the meaning of the match)\n\n"
|
|
|
|
"\tand COMMAND can be:\n"
|
|
|
|
"\t s/regexp/replacement/[igp]\n"
|
|
|
|
"\t which attempt to match regexp against the pattern space\n"
|
|
|
|
"\t and if successful replaces the matched portion with replacement.\n\n"
|
|
|
|
"\t aTEXT\n"
|
|
|
|
"\t which appends TEXT after the pattern space\n"
|
|
|
|
"Options:\n"
|
|
|
|
"-e\tadd the script to the commands to be executed\n"
|
|
|
|
"-n\tsuppress automatic printing of pattern space\n\n"
|
1999-10-28 16:06:25 +00:00
|
|
|
#if defined BB_REGEXP
|
2000-06-16 19:57:44 +00:00
|
|
|
"This version of sed matches full regular expressions.\n";
|
1999-10-28 16:06:25 +00:00
|
|
|
#else
|
2000-06-16 19:57:44 +00:00
|
|
|
"This version of sed matches strings (not full regular expressions).\n"
|
1999-10-28 16:06:25 +00:00
|
|
|
#endif
|
2000-05-12 19:41:47 +00:00
|
|
|
#endif
|
|
|
|
;
|
1999-10-28 16:06:25 +00:00
|
|
|
|
1999-12-29 22:19:46 +00:00
|
|
|
/* Flags & variables */
|
|
|
|
|
|
|
|
typedef enum { f_none, f_replace, f_append } sed_function;
|
|
|
|
|
|
|
|
#define NO_LINE -2
|
|
|
|
#define LAST_LINE -1
|
|
|
|
static int addr_line = NO_LINE;
|
|
|
|
static char *addr_pattern = NULL;
|
|
|
|
static int negated = 0;
|
|
|
|
|
|
|
|
#define SKIPSPACES(p) do { while (isspace(*(p))) (p)++; } while (0)
|
|
|
|
|
|
|
|
#define BUFSIZE 1024
|
|
|
|
|
|
|
|
static inline int at_last(FILE * fp)
|
1999-11-09 01:47:36 +00:00
|
|
|
{
|
2000-02-08 19:58:47 +00:00
|
|
|
int res = 0;
|
|
|
|
|
|
|
|
if (feof(fp))
|
|
|
|
return 1;
|
|
|
|
else {
|
2000-02-10 07:31:15 +00:00
|
|
|
int ch;
|
2000-02-08 19:58:47 +00:00
|
|
|
|
|
|
|
if ((ch = fgetc(fp)) == EOF)
|
|
|
|
res++;
|
|
|
|
ungetc(ch, fp);
|
|
|
|
}
|
|
|
|
return res;
|
1999-12-29 22:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_sed_repl(FILE * fp, char *needle, char *newNeedle,
|
2000-02-08 19:58:47 +00:00
|
|
|
int ignoreCase, int printFlag, int quietFlag)
|
1999-12-29 22:19:46 +00:00
|
|
|
{
|
2000-02-08 19:58:47 +00:00
|
|
|
int foundOne = FALSE;
|
|
|
|
char haystack[BUFSIZE];
|
|
|
|
int line = 1, doit;
|
|
|
|
|
|
|
|
while (fgets(haystack, BUFSIZE - 1, fp)) {
|
|
|
|
doit = 0;
|
|
|
|
if (addr_pattern) {
|
|
|
|
doit = !find_match(haystack, addr_pattern, FALSE);
|
|
|
|
} else if (addr_line == NO_LINE)
|
|
|
|
doit = 1;
|
|
|
|
else if (addr_line == LAST_LINE) {
|
|
|
|
if (at_last(fp))
|
|
|
|
doit = 1;
|
|
|
|
} else {
|
|
|
|
if (line == addr_line)
|
|
|
|
doit = 1;
|
|
|
|
}
|
|
|
|
if (negated)
|
|
|
|
doit = 1 - doit;
|
|
|
|
if (doit) {
|
|
|
|
foundOne =
|
|
|
|
replace_match(haystack, needle, newNeedle, ignoreCase);
|
|
|
|
|
|
|
|
if (foundOne == TRUE && printFlag == TRUE) {
|
|
|
|
fprintf(stdout, haystack);
|
|
|
|
}
|
|
|
|
}
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
if (quietFlag == FALSE) {
|
|
|
|
fprintf(stdout, haystack);
|
|
|
|
}
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
line++;
|
|
|
|
}
|
1999-12-29 22:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void do_sed_append(FILE * fp, char *appendline, int quietFlag)
|
|
|
|
{
|
2000-02-08 19:58:47 +00:00
|
|
|
char buffer[BUFSIZE];
|
|
|
|
int line = 1, doit;
|
|
|
|
|
|
|
|
while (fgets(buffer, BUFSIZE - 1, fp)) {
|
|
|
|
doit = 0;
|
|
|
|
if (addr_pattern) {
|
|
|
|
doit = !find_match(buffer, addr_pattern, FALSE);
|
|
|
|
} else if (addr_line == NO_LINE)
|
|
|
|
doit = 1;
|
|
|
|
else if (addr_line == LAST_LINE) {
|
|
|
|
if (at_last(fp))
|
|
|
|
doit = 1;
|
|
|
|
} else {
|
|
|
|
if (line == addr_line)
|
|
|
|
doit = 1;
|
|
|
|
}
|
|
|
|
if (negated)
|
|
|
|
doit = 1 - doit;
|
|
|
|
if (quietFlag == FALSE) {
|
|
|
|
fprintf(stdout, buffer);
|
|
|
|
}
|
|
|
|
if (doit) {
|
|
|
|
fputs(appendline, stdout);
|
|
|
|
fputc('\n', stdout);
|
|
|
|
}
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
line++;
|
|
|
|
}
|
1999-11-09 01:47:36 +00:00
|
|
|
}
|
1999-10-28 16:06:25 +00:00
|
|
|
|
1999-12-29 22:19:46 +00:00
|
|
|
extern int sed_main(int argc, char **argv)
|
1999-10-28 16:06:25 +00:00
|
|
|
{
|
2000-02-08 19:58:47 +00:00
|
|
|
FILE *fp;
|
|
|
|
char *needle = NULL, *newNeedle = NULL;
|
|
|
|
char *name;
|
|
|
|
char *cp;
|
|
|
|
int ignoreCase = FALSE;
|
|
|
|
int printFlag = FALSE;
|
|
|
|
int quietFlag = FALSE;
|
|
|
|
int stopNow;
|
|
|
|
char *line_s = NULL, saved;
|
|
|
|
char *appendline = NULL;
|
|
|
|
char *pos;
|
|
|
|
sed_function sed_f = f_none;
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
argc--;
|
|
|
|
argv++;
|
|
|
|
if (argc < 1) {
|
|
|
|
usage(sed_usage);
|
|
|
|
}
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-10 07:31:15 +00:00
|
|
|
while (argc > 1) {
|
2000-03-06 19:20:35 +00:00
|
|
|
if (**argv != '-')
|
|
|
|
usage(sed_usage);
|
|
|
|
argc--;
|
|
|
|
cp = *argv++;
|
|
|
|
stopNow = FALSE;
|
|
|
|
|
|
|
|
while (*++cp && stopNow == FALSE) {
|
|
|
|
switch (*cp) {
|
|
|
|
case 'n':
|
|
|
|
quietFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (*(cp + 1) == 0 && --argc < 0) {
|
|
|
|
usage(sed_usage);
|
|
|
|
}
|
|
|
|
if (*++cp != 's')
|
|
|
|
cp = *argv++;
|
|
|
|
|
|
|
|
/* Read address if present */
|
|
|
|
SKIPSPACES(cp);
|
|
|
|
if (*cp == '$') {
|
|
|
|
addr_line = LAST_LINE;
|
|
|
|
cp++;
|
|
|
|
} else {
|
|
|
|
if (isdigit(*cp)) { /* LINE ADDRESS */
|
|
|
|
line_s = cp;
|
|
|
|
while (isdigit(*cp))
|
|
|
|
cp++;
|
|
|
|
if (cp > line_s) {
|
|
|
|
/* numeric line */
|
|
|
|
saved = *cp;
|
|
|
|
*cp = '\0';
|
|
|
|
addr_line = atoi(line_s);
|
|
|
|
*cp = saved;
|
|
|
|
}
|
|
|
|
} else if (*cp == '/') { /* PATTERN ADDRESS */
|
|
|
|
pos = addr_pattern = cp + 1;
|
|
|
|
pos = strchr(pos, '/');
|
|
|
|
if (!pos)
|
|
|
|
usage(sed_usage);
|
|
|
|
*pos = '\0';
|
|
|
|
cp = pos + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SKIPSPACES(cp);
|
|
|
|
if (*cp == '!') {
|
|
|
|
negated++;
|
|
|
|
cp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Read command */
|
|
|
|
|
|
|
|
SKIPSPACES(cp);
|
|
|
|
switch (*cp) {
|
|
|
|
case 's': /* REPLACE */
|
|
|
|
if (strlen(cp) <= 3 || *(cp + 1) != '/')
|
|
|
|
break;
|
|
|
|
sed_f = f_replace;
|
|
|
|
|
|
|
|
pos = needle = cp + 2;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
pos = strchr(pos, '/');
|
|
|
|
if (pos == NULL) {
|
|
|
|
usage(sed_usage);
|
|
|
|
}
|
|
|
|
if (*(pos - 1) == '\\') {
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*pos = 0;
|
|
|
|
newNeedle = ++pos;
|
|
|
|
for (;;) {
|
|
|
|
pos = strchr(pos, '/');
|
|
|
|
if (pos == NULL) {
|
|
|
|
usage(sed_usage);
|
|
|
|
}
|
|
|
|
if (*(pos - 1) == '\\') {
|
|
|
|
pos++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
*pos = 0;
|
|
|
|
if (pos + 2 != 0) {
|
|
|
|
while (*++pos) {
|
|
|
|
switch (*pos) {
|
|
|
|
case 'i':
|
|
|
|
ignoreCase = TRUE;
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
printFlag = TRUE;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage(sed_usage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cp = pos;
|
|
|
|
/* fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); */
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a': /* APPEND */
|
|
|
|
if (strlen(cp) < 2)
|
|
|
|
break;
|
|
|
|
sed_f = f_append;
|
|
|
|
appendline = ++cp;
|
|
|
|
/* fprintf(stderr, "append '%s'\n", appendline); */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stopNow = TRUE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
usage(sed_usage);
|
|
|
|
}
|
|
|
|
}
|
2000-02-10 07:31:15 +00:00
|
|
|
}
|
2000-02-08 19:58:47 +00:00
|
|
|
|
|
|
|
if (argc == 0) {
|
|
|
|
switch (sed_f) {
|
|
|
|
case f_none:
|
1999-12-29 22:19:46 +00:00
|
|
|
break;
|
2000-02-08 19:58:47 +00:00
|
|
|
case f_replace:
|
|
|
|
do_sed_repl(stdin, needle, newNeedle, ignoreCase, printFlag,
|
|
|
|
quietFlag);
|
1999-12-29 22:19:46 +00:00
|
|
|
break;
|
2000-02-08 19:58:47 +00:00
|
|
|
case f_append:
|
|
|
|
do_sed_append(stdin, appendline, quietFlag);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (argc-- > 0) {
|
|
|
|
name = *argv++;
|
|
|
|
|
|
|
|
fp = fopen(name, "r");
|
|
|
|
if (fp == NULL) {
|
|
|
|
perror(name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (sed_f) {
|
|
|
|
case f_none:
|
1999-12-29 22:19:46 +00:00
|
|
|
break;
|
2000-02-08 19:58:47 +00:00
|
|
|
case f_replace:
|
|
|
|
do_sed_repl(fp, needle, newNeedle, ignoreCase, printFlag,
|
|
|
|
quietFlag);
|
1999-12-29 22:19:46 +00:00
|
|
|
break;
|
2000-02-08 19:58:47 +00:00
|
|
|
case f_append:
|
|
|
|
do_sed_append(fp, appendline, quietFlag);
|
1999-12-29 22:19:46 +00:00
|
|
|
break;
|
1999-10-29 23:09:13 +00:00
|
|
|
}
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
if (ferror(fp))
|
|
|
|
perror(name);
|
1999-12-29 22:19:46 +00:00
|
|
|
|
2000-02-08 19:58:47 +00:00
|
|
|
fclose(fp);
|
|
|
|
}
|
1999-11-09 01:47:36 +00:00
|
|
|
}
|
2000-06-19 17:25:40 +00:00
|
|
|
return(TRUE);
|
1999-10-28 16:06:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* END CODE */
|