2 * Mini sed implementation for busybox
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
34 static const char sed_usage[] =
35 "sed [-n] [-e script] [file...]\n\n"
36 "Allowed sed scripts come in the following form:\n"
37 "\t's/regexp/replacement/[gp]'\n"
38 "which attempt to match regexp against the pattern space\n"
39 "and if successful replaces the matched portion with replacement.\n\n"
41 "-e\tadd the script to the commands to be executed\n"
42 "-n\tsuppress automatic printing of pattern space\n\n"
44 "This version of sed matches full regular expresions.\n";
46 "This version of sed matches strings (not full regular expresions).\n";
50 static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag)
55 while (fgets (haystack, 1023, fp)) {
56 foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
57 if (foundOne==TRUE && printFlag==TRUE) {
58 fprintf(stdout, haystack);
60 if (quietFlag==FALSE) {
61 fprintf(stdout, haystack);
66 extern int sed_main (int argc, char **argv)
69 char *needle=NULL, *newNeedle=NULL;
88 while (*++cp && stopNow==FALSE) {
94 if (*(cp+1)==0 && --argc < 0) {
100 if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') {
101 char* pos=needle=cp+2;
103 pos = strchr(pos, '/');
107 if (*(pos-1) == '\\') {
116 pos = strchr(pos, '/');
120 if (*(pos-1) == '\\') {
146 //fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
157 do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag);
162 fp = fopen (name, "r");
168 do_sed( fp, needle, newNeedle, ignoreCase, printFlag, quietFlag);