Stuf
[oweals/busybox.git] / editors / 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"
36 "Allowed scripts come in two forms:\n"
37 "'/regexp/[gp]'\n"
38 "\tattempt to match regexp against the pattern space\n"
39 "'s/regexp/replacement/[gp]'\n"
40 "\tattempt to match regexp against the pattern space\n"
41 "\tand if successful replaces the matched portion with replacement."
42 "Options:\n"
43 "-e\tadd the script to the commands to be executed\n"
44 "-n\tsuppress automatic printing of pattern space\n\n"
45 #if defined BB_REGEXP
46 "This version of sed matches full regexps.\n";
47 #else
48 "This version of sed matches strings (not full regexps).\n";
49 #endif
50
51
52 static int replaceFlag = FALSE;
53 static int noprintFlag = FALSE;
54
55
56 extern int sed_main (int argc, char **argv)
57 {
58     FILE *fp;
59     const char *needle;
60     const char *name;
61     const char *cp;
62     int tellName=TRUE;
63     int ignoreCase=FALSE;
64     int tellLine=FALSE;
65     long line;
66     char haystack[BUF_SIZE];
67
68     ignoreCase = FALSE;
69     tellLine = FALSE;
70
71     argc--;
72     argv++;
73     if (argc < 1) {
74         usage(grep_usage);
75     }
76
77     if (**argv == '-') {
78         argc--;
79         cp = *argv++;
80
81         while (*++cp)
82             switch (*cp) {
83             case 'n':
84                 noprintFlag = TRUE;
85                 break;
86             case 'e':
87                 if (*(*argv)+1 != '\'' && **argv != '\"') {
88                     if (--argc == 0)
89                         usage( mkdir_usage);
90                     ++argv;
91                     if (*(*argv)+1 != '\'' && **argv != '\"') {
92                         usage( mkdir_usage);
93                 }
94                 /* Find the specified modes */
95                 mode = 0;
96                 if ( parse_mode(*(++argv), &mode) == FALSE ) {
97                     fprintf(stderr, "Unknown mode: %s\n", *argv);
98                     exit( FALSE);
99                 }
100                 break;
101
102             default:
103                 usage(grep_usage);
104             }
105     }
106
107     needle = *argv++;
108     argc--;
109
110     while (argc-- > 0) {
111         name = *argv++;
112
113         fp = fopen (name, "r");
114         if (fp == NULL) {
115             perror (name);
116             continue;
117         }
118
119         line = 0;
120
121         while (fgets (haystack, sizeof (haystack), fp)) {
122             line++;
123             cp = &haystack[strlen (haystack) - 1];
124
125             if (*cp != '\n')
126                 fprintf (stderr, "%s: Line too long\n", name);
127
128             if (find_match(haystack, needle, ignoreCase) == TRUE) {
129                 if (tellName==TRUE)
130                     printf ("%s: ", name);
131
132                 if (tellLine==TRUE)
133                     printf ("%ld: ", line);
134
135                 fputs (haystack, stdout);
136             }
137         }
138
139         if (ferror (fp))
140             perror (name);
141
142         fclose (fp);
143     }
144     exit( TRUE);
145 }
146
147
148 /* END CODE */
149
150