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"
36 "Allowed scripts come in the following form:\n\n"
37 "'s/regexp/replacement/[gp]'\n"
38 "\tattempt to match regexp against the pattern space\n"
39 "\tand 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 regexps.\n";
46 "This version of sed matches strings (not full regexps).\n";
52 extern int sed_main (int argc, char **argv)
55 char *needle=NULL, *newNeedle=NULL;
76 while (*++cp && stopNow==FALSE)
82 if (*(cp+1)==0 && --argc < 0) {
83 fprintf(stderr, "A\n");
88 if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') {
89 char* pos=needle=cp+2;
91 pos = strchr(pos, '/');
93 fprintf(stderr, "B\n");
96 if (*(pos-1) == '\\') {
105 pos = strchr(pos, '/');
107 fprintf(stderr, "C\n");
110 if (*(pos-1) == '\\') {
119 fprintf(stderr, "pos='%s'\n", pos);
137 fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
142 fprintf(stderr, "D\n");
150 fp = fopen (name, "r");
156 haystack = (char*)malloc( 1024);
157 while (fgets (haystack, 1023, fp)) {
159 foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
160 if (foundOne==TRUE && printFlag==TRUE)
161 fputs (haystack, stdout);
162 if (quietFlag==FALSE)
163 fputs (haystack, stdout);
164 /* Avoid any mem leaks */
166 haystack = (char*)malloc( BUF_SIZE);