4dfc0246c9e87a9b54033a587ad842dbb99ea8b9
[oweals/busybox.git] / sed.c
1 /*
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
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"
40 "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 regular expresions.\n";
45 #else
46 "This version of sed matches strings (not full regular expresions).\n";
47 #endif
48     
49
50 static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag)
51 {
52     int foundOne=FALSE;
53     char haystack[1024];
54
55     while (fgets (haystack, 1023, fp)) {
56         foundOne = replace_match(haystack, needle, newNeedle, ignoreCase);
57         if (foundOne==TRUE && printFlag==TRUE) {
58             fprintf(stdout, haystack);
59         }
60         if (quietFlag==FALSE) {
61             fprintf(stdout, haystack);
62         }
63     }
64 }
65
66 extern int sed_main (int argc, char **argv)
67 {
68     FILE *fp;
69     char *needle=NULL, *newNeedle=NULL;
70     char *name;
71     char *cp;
72     int ignoreCase=FALSE;
73     int printFlag=FALSE;
74     int quietFlag=FALSE;
75     int stopNow;
76
77     argc--;
78     argv++;
79     if (argc < 1) {
80         usage(sed_usage);
81     }
82
83     if (**argv == '-') {
84         argc--;
85         cp = *argv++;
86         stopNow=FALSE;
87
88         while (*++cp && stopNow==FALSE) {
89             switch (*cp) {
90             case 'n':
91                 quietFlag = TRUE;
92                 break;
93             case 'e':
94                 if (*(cp+1)==0 && --argc < 0) {
95                     usage( sed_usage);
96                 }
97                 if ( *++cp != 's')
98                     cp = *argv++;
99                 while( *cp ) {
100                     if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') {
101                         char* pos=needle=cp+2;
102                         for(;;) {
103                             pos = strchr(pos, '/');
104                             if (pos==NULL) {
105                                 usage( sed_usage);
106                             }
107                             if (*(pos-1) == '\\') {
108                                 pos++;
109                                 continue;
110                             }
111                             break;
112                         }
113                         *pos=0;
114                         newNeedle=++pos;
115                         for(;;) {
116                             pos = strchr(pos, '/');
117                             if (pos==NULL) {
118                                 usage( sed_usage);
119                             }
120                             if (*(pos-1) == '\\') {
121                                 pos++;
122                                 continue;
123                             }
124                             break;
125                         }
126                         *pos=0;
127                         if (pos+2 != 0) {
128                             while (*++pos) {
129                                 switch (*pos) {
130                                     case 'i':
131                                         ignoreCase=TRUE;
132                                         break;
133                                     case 'p':
134                                         printFlag=TRUE;
135                                         break;
136                                     case 'g':
137                                         break;
138                                     default:
139                                         usage( sed_usage);
140                                 }
141                             }
142                         }
143                     }
144                     cp++;
145                 }
146                 //fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
147                 stopNow=TRUE;
148                 break;
149
150             default:
151                 usage(sed_usage);
152             }
153         }
154     }
155
156     if (argc==0) {
157         do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag);
158     } else {
159         while (argc-- > 0) {
160             name = *argv++;
161
162             fp = fopen (name, "r");
163             if (fp == NULL) {
164                 perror (name);
165                 continue;
166             }
167
168             do_sed( fp, needle, newNeedle, ignoreCase, printFlag, quietFlag);
169
170             if (ferror (fp))
171                 perror (name);
172
173             fclose (fp);
174         }
175     }
176     exit( TRUE);
177 }
178
179
180 /* END CODE */
181
182