Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 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" |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 36 | "Allowed scripts come in the following form:\n\n" |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 37 | "'s/regexp/replacement/[gp]'\n" |
| 38 | "\tattempt to match regexp against the pattern space\n" |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 39 | "\tand if successful replaces the matched portion with replacement.\n\n" |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 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 regexps.\n"; |
| 45 | #else |
| 46 | "This version of sed matches strings (not full regexps).\n"; |
| 47 | #endif |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 48 | |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 49 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 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]; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 54 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 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 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 65 | |
| 66 | extern int sed_main (int argc, char **argv) |
| 67 | { |
| 68 | FILE *fp; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 69 | char *needle=NULL, *newNeedle=NULL; |
| 70 | char *name; |
| 71 | char *cp; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 72 | int ignoreCase=FALSE; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 73 | int printFlag=FALSE; |
| 74 | int quietFlag=FALSE; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 75 | int stopNow; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 76 | |
| 77 | argc--; |
| 78 | argv++; |
| 79 | if (argc < 1) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 80 | usage(sed_usage); |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | if (**argv == '-') { |
| 84 | argc--; |
| 85 | cp = *argv++; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 86 | stopNow=FALSE; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 87 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 88 | while (*++cp && stopNow==FALSE) { |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 89 | switch (*cp) { |
| 90 | case 'n': |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 91 | quietFlag = TRUE; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 92 | break; |
| 93 | case 'e': |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 94 | if (*(cp+1)==0 && --argc < 0) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 95 | usage( sed_usage); |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 96 | } |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 97 | if ( *++cp != 's') |
| 98 | cp = *argv++; |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 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) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 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) { |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 118 | usage( sed_usage); |
| 119 | } |
| 120 | if (*(pos-1) == '\\') { |
| 121 | pos++; |
| 122 | continue; |
| 123 | } |
| 124 | break; |
| 125 | } |
| 126 | *pos=0; |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 127 | if (pos+2 != 0) { |
| 128 | while (*++pos) { |
Eric Andersen | 7f1acfd | 1999-10-29 23:09:13 +0000 | [diff] [blame] | 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 | } |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 143 | } |
| 144 | cp++; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 145 | } |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 146 | //fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 147 | stopNow=TRUE; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 148 | break; |
| 149 | |
| 150 | default: |
Eric Andersen | c1525e8 | 1999-10-29 00:07:31 +0000 | [diff] [blame] | 151 | usage(sed_usage); |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 152 | } |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 153 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 154 | } |
| 155 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 156 | if (argc==0) { |
| 157 | do_sed( stdin, needle, newNeedle, ignoreCase, printFlag, quietFlag); |
| 158 | } else { |
| 159 | while (argc-- > 0) { |
| 160 | name = *argv++; |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 161 | |
Eric Andersen | 50d6360 | 1999-11-09 01:47:36 +0000 | [diff] [blame^] | 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); |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 174 | } |
Eric Andersen | 6b6b3f6 | 1999-10-28 16:06:25 +0000 | [diff] [blame] | 175 | } |
| 176 | exit( TRUE); |
| 177 | } |
| 178 | |
| 179 | |
| 180 | /* END CODE */ |
| 181 | |
| 182 | |