blob: 4dfc0246c9e87a9b54033a587ad842dbb99ea8b9 [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[] =
Eric Andersend73dc5b1999-11-10 23:13:02 +000035"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"
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
Eric Andersend73dc5b1999-11-10 23:13:02 +000044"This version of sed matches full regular expresions.\n";
Eric Andersen6b6b3f61999-10-28 16:06:25 +000045#else
Eric Andersend73dc5b1999-11-10 23:13:02 +000046"This version of sed matches strings (not full regular expresions).\n";
Eric Andersen6b6b3f61999-10-28 16:06:25 +000047#endif
Eric Andersen50d63601999-11-09 01:47:36 +000048
Eric Andersen6b6b3f61999-10-28 16:06:25 +000049
Eric Andersen50d63601999-11-09 01:47:36 +000050static void do_sed(FILE *fp, char *needle, char *newNeedle, int ignoreCase, int printFlag, int quietFlag)
51{
52 int foundOne=FALSE;
53 char haystack[1024];
Eric Andersen6b6b3f61999-10-28 16:06:25 +000054
Eric Andersen50d63601999-11-09 01:47:36 +000055 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}
Eric Andersen6b6b3f61999-10-28 16:06:25 +000065
66extern int sed_main (int argc, char **argv)
67{
68 FILE *fp;
Eric Andersenc1525e81999-10-29 00:07:31 +000069 char *needle=NULL, *newNeedle=NULL;
70 char *name;
71 char *cp;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000072 int ignoreCase=FALSE;
Eric Andersen7f1acfd1999-10-29 23:09:13 +000073 int printFlag=FALSE;
74 int quietFlag=FALSE;
Eric Andersenc1525e81999-10-29 00:07:31 +000075 int stopNow;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000076
77 argc--;
78 argv++;
79 if (argc < 1) {
Eric Andersenc1525e81999-10-29 00:07:31 +000080 usage(sed_usage);
Eric Andersen6b6b3f61999-10-28 16:06:25 +000081 }
82
83 if (**argv == '-') {
84 argc--;
85 cp = *argv++;
Eric Andersenc1525e81999-10-29 00:07:31 +000086 stopNow=FALSE;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000087
Eric Andersen50d63601999-11-09 01:47:36 +000088 while (*++cp && stopNow==FALSE) {
Eric Andersen6b6b3f61999-10-28 16:06:25 +000089 switch (*cp) {
90 case 'n':
Eric Andersen7f1acfd1999-10-29 23:09:13 +000091 quietFlag = TRUE;
Eric Andersen6b6b3f61999-10-28 16:06:25 +000092 break;
93 case 'e':
Eric Andersenc1525e81999-10-29 00:07:31 +000094 if (*(cp+1)==0 && --argc < 0) {
Eric Andersenc1525e81999-10-29 00:07:31 +000095 usage( sed_usage);
Eric Andersen6b6b3f61999-10-28 16:06:25 +000096 }
Eric Andersen50d63601999-11-09 01:47:36 +000097 if ( *++cp != 's')
98 cp = *argv++;
Eric Andersenc1525e81999-10-29 00:07:31 +000099 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) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000105 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) {
Eric Andersenc1525e81999-10-29 00:07:31 +0000118 usage( sed_usage);
119 }
120 if (*(pos-1) == '\\') {
121 pos++;
122 continue;
123 }
124 break;
125 }
126 *pos=0;
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000127 if (pos+2 != 0) {
128 while (*++pos) {
Eric Andersen7f1acfd1999-10-29 23:09:13 +0000129 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 }
Eric Andersenc1525e81999-10-29 00:07:31 +0000143 }
144 cp++;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000145 }
Eric Andersen50d63601999-11-09 01:47:36 +0000146 //fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle);
Eric Andersenc1525e81999-10-29 00:07:31 +0000147 stopNow=TRUE;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000148 break;
149
150 default:
Eric Andersenc1525e81999-10-29 00:07:31 +0000151 usage(sed_usage);
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000152 }
Eric Andersen50d63601999-11-09 01:47:36 +0000153 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000154 }
155
Eric Andersen50d63601999-11-09 01:47:36 +0000156 if (argc==0) {
157 do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag);
158 } else {
159 while (argc-- > 0) {
160 name = *argv++;
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000161
Eric Andersen50d63601999-11-09 01:47:36 +0000162 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);
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000174 }
Eric Andersen6b6b3f61999-10-28 16:06:25 +0000175 }
176 exit( TRUE);
177}
178
179
180/* END CODE */
181
182