blob: f0a6a3b48c50392d4e2f9d2a03b0a1c3ab70f040 [file] [log] [blame]
Eric Andersen6b6b3f61999-10-28 16:06:25 +00001/*
2 * Mini sed implementation for busybox
3 *
4 *
5 * Copyright (C) 1999 by Lineo, inc.
6 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 *
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.
12 *
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.
17 *
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
21 *
22 */
23
24#include "internal.h"
25#include "regexp.h"
26#include <stdio.h>
27#include <dirent.h>
28#include <errno.h>
29#include <fcntl.h>
30#include <signal.h>
31#include <time.h>
32#include <ctype.h>
33
34static const char sed_usage[] =
35"sed [-n] [-e script] [file...]\n"
Eric Andersenc1525e81999-10-29 00:07:31 +000036"Allowed scripts come in the following form:\n\n"
Eric Andersen6b6b3f61999-10-28 16:06:25 +000037"'s/regexp/replacement/[gp]'\n"
38"\tattempt to match regexp against the pattern space\n"
Eric Andersenc1525e81999-10-29 00:07:31 +000039"\tand if successful replaces the matched portion with replacement.\n\n"
Eric Andersen6b6b3f61999-10-28 16:06:25 +000040"Options:\n"
41"-e\tadd the script to the commands to be executed\n"
42"-n\tsuppress automatic printing of pattern space\n\n"
43#if defined BB_REGEXP
44"This version of sed matches full regexps.\n";
45#else
46"This version of sed matches strings (not full regexps).\n";
47#endif
48
49
Eric Andersen6b6b3f61999-10-28 16:06:25 +000050
51
52extern int sed_main (int argc, char **argv)
53{
54 FILE *fp;
Eric Andersenc1525e81999-10-29 00:07:31 +000055 char *needle=NULL, *newNeedle=NULL;
56 char *name;
57 char *cp;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000058 int ignoreCase=FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +000059 int foundOne=FALSE;
Eric Andersen7f1acfd1999-10-29 23:09:13 +000060 int printFlag=FALSE;
61 int quietFlag=FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +000062 int stopNow;
63 char *haystack;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000064
65 argc--;
66 argv++;
67 if (argc < 1) {
Eric Andersenc1525e81999-10-29 00:07:31 +000068 usage(sed_usage);
Eric Andersen6b6b3f61999-10-28 16:06:25 +000069 }
70
71 if (**argv == '-') {
72 argc--;
73 cp = *argv++;
Eric Andersenc1525e81999-10-29 00:07:31 +000074 stopNow=FALSE;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000075
Eric Andersenc1525e81999-10-29 00:07:31 +000076 while (*++cp && stopNow==FALSE)
Eric Andersen6b6b3f61999-10-28 16:06:25 +000077 switch (*cp) {
78 case 'n':
Eric Andersen7f1acfd1999-10-29 23:09:13 +000079 quietFlag = TRUE;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000080 break;
81 case 'e':
Eric Andersenc1525e81999-10-29 00:07:31 +000082 if (*(cp+1)==0 && --argc < 0) {
83 fprintf(stderr, "A\n");
84 usage( sed_usage);
Eric Andersen6b6b3f61999-10-28 16:06:25 +000085 }
Eric Andersenc1525e81999-10-29 00:07:31 +000086 cp = *argv++;
87 while( *cp ) {
88 if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') {
89 char* pos=needle=cp+2;
90 for(;;) {
91 pos = strchr(pos, '/');
92 if (pos==NULL) {
93 fprintf(stderr, "B\n");
94 usage( sed_usage);
95 }
96 if (*(pos-1) == '\\') {
97 pos++;
98 continue;
99 }
100 break;
101 }
102 *pos=0;
103 newNeedle=++pos;
104 for(;;) {
105 pos = strchr(pos, '/');
106 if (pos==NULL) {
107 fprintf(stderr, "C\n");
108 usage( sed_usage);
109 }
110 if (*(pos-1) == '\\') {
111 pos++;
112 continue;
113 }
114 break;
115 }
116 *pos=0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000117 if (pos+2 != 0) {
118 while (*++pos) {
119 fprintf(stderr, "pos='%s'\n", pos);
120 switch (*pos) {
121 case 'i':
122 ignoreCase=TRUE;
123 break;
124 case 'p':
125 printFlag=TRUE;
126 break;
127 case 'g':
128 break;
129 default:
130 usage( sed_usage);
131 }
132 }
133 }
Eric Andersenc1525e81999-10-29 00:07:31 +0000134 }
135 cp++;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000136 }
Eric Andersenc1525e81999-10-29 00:07:31 +0000137 fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
138 stopNow=TRUE;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000139 break;
140
141 default:
Eric Andersenc1525e81999-10-29 00:07:31 +0000142 fprintf(stderr, "D\n");
143 usage(sed_usage);
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000144 }
145 }
146
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000147 while (argc-- > 0) {
148 name = *argv++;
149
150 fp = fopen (name, "r");
151 if (fp == NULL) {
152 perror (name);
153 continue;
154 }
155
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000156 haystack = (char*)malloc( 1024);
157 while (fgets (haystack, 1023, fp)) {
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000158
Eric Andersenc1525e81999-10-29 00:07:31 +0000159 foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000160 if (foundOne==TRUE && printFlag==TRUE)
161 fputs (haystack, stdout);
162 if (quietFlag==FALSE)
Eric Andersenc1525e81999-10-29 00:07:31 +0000163 fputs (haystack, stdout);
164 /* Avoid any mem leaks */
165 free(haystack);
166 haystack = (char*)malloc( BUF_SIZE);
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000167 }
168
169 if (ferror (fp))
170 perror (name);
171
172 fclose (fp);
173 }
174 exit( TRUE);
175}
176
177
178/* END CODE */
179
180