diff options
author | Eric Andersen <andersen@codepoet.org> | 1999-10-29 00:07:31 +0000 |
---|---|---|
committer | Eric Andersen <andersen@codepoet.org> | 1999-10-29 00:07:31 +0000 |
commit | c1525e84dd6a3ac8252ce10e6ae605bd37d60797 (patch) | |
tree | bef01229299c4ad89f2e353df0e88af292a8795c | |
parent | 6b6b3f6ef2f44898a8bddfaae93cc4ef3aa79661 (diff) | |
download | busybox-w32-c1525e84dd6a3ac8252ce10e6ae605bd37d60797.tar.gz busybox-w32-c1525e84dd6a3ac8252ce10e6ae605bd37d60797.tar.bz2 busybox-w32-c1525e84dd6a3ac8252ce10e6ae605bd37d60797.zip |
Stuff
-rw-r--r-- | applets/busybox.c | 3 | ||||
-rw-r--r-- | busybox.c | 3 | ||||
-rw-r--r-- | busybox.def.h | 1 | ||||
-rw-r--r-- | editors/sed.c | 110 | ||||
-rw-r--r-- | init.c | 18 | ||||
-rw-r--r-- | init/init.c | 18 | ||||
-rw-r--r-- | internal.h | 1 | ||||
-rw-r--r-- | regexp.c | 22 | ||||
-rw-r--r-- | regexp.h | 1 | ||||
-rw-r--r-- | sed.c | 110 | ||||
-rw-r--r-- | utility.c | 101 |
11 files changed, 229 insertions, 159 deletions
diff --git a/applets/busybox.c b/applets/busybox.c index 05144c472..29a112b12 100644 --- a/applets/busybox.c +++ b/applets/busybox.c | |||
@@ -142,6 +142,9 @@ static const struct Applet applets[] = { | |||
142 | {"fdisk", sfdisk_main}, | 142 | {"fdisk", sfdisk_main}, |
143 | {"sfdisk", sfdisk_main}, | 143 | {"sfdisk", sfdisk_main}, |
144 | #endif | 144 | #endif |
145 | #ifdef BB_SED //bin | ||
146 | {"sed", sed_main}, | ||
147 | #endif | ||
145 | #ifdef BB_SLEEP //bin | 148 | #ifdef BB_SLEEP //bin |
146 | {"sleep", sleep_main}, | 149 | {"sleep", sleep_main}, |
147 | #endif | 150 | #endif |
@@ -142,6 +142,9 @@ static const struct Applet applets[] = { | |||
142 | {"fdisk", sfdisk_main}, | 142 | {"fdisk", sfdisk_main}, |
143 | {"sfdisk", sfdisk_main}, | 143 | {"sfdisk", sfdisk_main}, |
144 | #endif | 144 | #endif |
145 | #ifdef BB_SED //bin | ||
146 | {"sed", sed_main}, | ||
147 | #endif | ||
145 | #ifdef BB_SLEEP //bin | 148 | #ifdef BB_SLEEP //bin |
146 | {"sleep", sleep_main}, | 149 | {"sleep", sleep_main}, |
147 | #endif | 150 | #endif |
diff --git a/busybox.def.h b/busybox.def.h index a79eaaf4f..b71c53923 100644 --- a/busybox.def.h +++ b/busybox.def.h | |||
@@ -47,6 +47,7 @@ | |||
47 | #define BB_RM | 47 | #define BB_RM |
48 | #define BB_RMDIR | 48 | #define BB_RMDIR |
49 | //#define BB_SFDISK | 49 | //#define BB_SFDISK |
50 | #define BB_SED | ||
50 | #define BB_SLEEP | 51 | #define BB_SLEEP |
51 | #define BB_SWAPONOFF | 52 | #define BB_SWAPONOFF |
52 | #define BB_SYNC | 53 | #define BB_SYNC |
diff --git a/editors/sed.c b/editors/sed.c index 4dd552a2d..a3e635d3a 100644 --- a/editors/sed.c +++ b/editors/sed.c | |||
@@ -33,12 +33,10 @@ | |||
33 | 33 | ||
34 | static const char sed_usage[] = | 34 | static const char sed_usage[] = |
35 | "sed [-n] [-e script] [file...]\n" | 35 | "sed [-n] [-e script] [file...]\n" |
36 | "Allowed scripts come in two forms:\n" | 36 | "Allowed scripts come in the following form:\n\n" |
37 | "'/regexp/[gp]'\n" | ||
38 | "\tattempt to match regexp against the pattern space\n" | ||
39 | "'s/regexp/replacement/[gp]'\n" | 37 | "'s/regexp/replacement/[gp]'\n" |
40 | "\tattempt to match regexp against the pattern space\n" | 38 | "\tattempt to match regexp against the pattern space\n" |
41 | "\tand if successful replaces the matched portion with replacement." | 39 | "\tand if successful replaces the matched portion with replacement.\n\n" |
42 | "Options:\n" | 40 | "Options:\n" |
43 | "-e\tadd the script to the commands to be executed\n" | 41 | "-e\tadd the script to the commands to be executed\n" |
44 | "-n\tsuppress automatic printing of pattern space\n\n" | 42 | "-n\tsuppress automatic printing of pattern space\n\n" |
@@ -49,64 +47,86 @@ static const char sed_usage[] = | |||
49 | #endif | 47 | #endif |
50 | 48 | ||
51 | 49 | ||
52 | static int replaceFlag = FALSE; | ||
53 | static int noprintFlag = FALSE; | ||
54 | 50 | ||
55 | 51 | ||
56 | extern int sed_main (int argc, char **argv) | 52 | extern int sed_main (int argc, char **argv) |
57 | { | 53 | { |
58 | FILE *fp; | 54 | FILE *fp; |
59 | const char *needle; | 55 | char *needle=NULL, *newNeedle=NULL; |
60 | const char *name; | 56 | char *name; |
61 | const char *cp; | 57 | char *cp; |
62 | int tellName=TRUE; | ||
63 | int ignoreCase=FALSE; | 58 | int ignoreCase=FALSE; |
64 | int tellLine=FALSE; | 59 | int foundOne=FALSE; |
65 | long line; | 60 | int noprintFlag=FALSE; |
66 | char haystack[BUF_SIZE]; | 61 | int stopNow; |
67 | 62 | char *haystack; | |
68 | ignoreCase = FALSE; | ||
69 | tellLine = FALSE; | ||
70 | 63 | ||
71 | argc--; | 64 | argc--; |
72 | argv++; | 65 | argv++; |
73 | if (argc < 1) { | 66 | if (argc < 1) { |
74 | usage(grep_usage); | 67 | usage(sed_usage); |
75 | } | 68 | } |
76 | 69 | ||
77 | if (**argv == '-') { | 70 | if (**argv == '-') { |
78 | argc--; | 71 | argc--; |
79 | cp = *argv++; | 72 | cp = *argv++; |
73 | stopNow=FALSE; | ||
80 | 74 | ||
81 | while (*++cp) | 75 | while (*++cp && stopNow==FALSE) |
82 | switch (*cp) { | 76 | switch (*cp) { |
83 | case 'n': | 77 | case 'n': |
84 | noprintFlag = TRUE; | 78 | noprintFlag = TRUE; |
85 | break; | 79 | break; |
86 | case 'e': | 80 | case 'e': |
87 | if (*(*argv)+1 != '\'' && **argv != '\"') { | 81 | if (*(cp+1)==0 && --argc < 0) { |
88 | if (--argc == 0) | 82 | fprintf(stderr, "A\n"); |
89 | usage( mkdir_usage); | 83 | usage( sed_usage); |
90 | ++argv; | ||
91 | if (*(*argv)+1 != '\'' && **argv != '\"') { | ||
92 | usage( mkdir_usage); | ||
93 | } | 84 | } |
94 | /* Find the specified modes */ | 85 | cp = *argv++; |
95 | mode = 0; | 86 | while( *cp ) { |
96 | if ( parse_mode(*(++argv), &mode) == FALSE ) { | 87 | if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') { |
97 | fprintf(stderr, "Unknown mode: %s\n", *argv); | 88 | char* pos=needle=cp+2; |
98 | exit( FALSE); | 89 | for(;;) { |
90 | pos = strchr(pos, '/'); | ||
91 | if (pos==NULL) { | ||
92 | fprintf(stderr, "B\n"); | ||
93 | usage( sed_usage); | ||
94 | } | ||
95 | if (*(pos-1) == '\\') { | ||
96 | pos++; | ||
97 | continue; | ||
98 | } | ||
99 | break; | ||
100 | } | ||
101 | *pos=0; | ||
102 | newNeedle=++pos; | ||
103 | for(;;) { | ||
104 | pos = strchr(pos, '/'); | ||
105 | if (pos==NULL) { | ||
106 | fprintf(stderr, "C\n"); | ||
107 | usage( sed_usage); | ||
108 | } | ||
109 | if (*(pos-1) == '\\') { | ||
110 | pos++; | ||
111 | continue; | ||
112 | } | ||
113 | break; | ||
114 | } | ||
115 | *pos=0; | ||
116 | } | ||
117 | cp++; | ||
99 | } | 118 | } |
119 | fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); | ||
120 | stopNow=TRUE; | ||
100 | break; | 121 | break; |
101 | 122 | ||
102 | default: | 123 | default: |
103 | usage(grep_usage); | 124 | fprintf(stderr, "D\n"); |
125 | usage(sed_usage); | ||
104 | } | 126 | } |
105 | } | 127 | } |
106 | 128 | ||
107 | needle = *argv++; | 129 | fprintf(stderr, "argc=%d\n", argc); |
108 | argc--; | ||
109 | |||
110 | while (argc-- > 0) { | 130 | while (argc-- > 0) { |
111 | name = *argv++; | 131 | name = *argv++; |
112 | 132 | ||
@@ -115,25 +135,19 @@ extern int sed_main (int argc, char **argv) | |||
115 | perror (name); | 135 | perror (name); |
116 | continue; | 136 | continue; |
117 | } | 137 | } |
138 | fprintf(stderr, "filename is '%s'\n", name); | ||
118 | 139 | ||
119 | line = 0; | 140 | haystack = (char*)malloc( 80); |
120 | |||
121 | while (fgets (haystack, sizeof (haystack), fp)) { | 141 | 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 | 142 | ||
143 | foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); | ||
144 | if (noprintFlag==TRUE && foundOne==TRUE) | ||
135 | fputs (haystack, stdout); | 145 | fputs (haystack, stdout); |
136 | } | 146 | else |
147 | fputs (haystack, stdout); | ||
148 | /* Avoid any mem leaks */ | ||
149 | free(haystack); | ||
150 | haystack = (char*)malloc( BUF_SIZE); | ||
137 | } | 151 | } |
138 | 152 | ||
139 | if (ferror (fp)) | 153 | if (ferror (fp)) |
@@ -41,8 +41,6 @@ | |||
41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | #define DEBUG_INIT | ||
45 | |||
46 | #define CONSOLE "/dev/console" /* Logical system console */ | 44 | #define CONSOLE "/dev/console" /* Logical system console */ |
47 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 45 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
48 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 46 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
@@ -272,7 +270,6 @@ static pid_t run(const char * const* command, | |||
272 | } | 270 | } |
273 | 271 | ||
274 | 272 | ||
275 | #ifndef DEBUG_INIT | ||
276 | static void shutdown_system(void) | 273 | static void shutdown_system(void) |
277 | { | 274 | { |
278 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 275 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; |
@@ -318,12 +315,9 @@ static void reboot_signal(int sig) | |||
318 | exit(0); | 315 | exit(0); |
319 | } | 316 | } |
320 | 317 | ||
321 | #endif | ||
322 | |||
323 | extern int init_main(int argc, char **argv) | 318 | extern int init_main(int argc, char **argv) |
324 | { | 319 | { |
325 | int run_rc = TRUE; | 320 | int run_rc = TRUE; |
326 | pid_t pid = 0; | ||
327 | pid_t pid1 = 0; | 321 | pid_t pid1 = 0; |
328 | pid_t pid2 = 0; | 322 | pid_t pid2 = 0; |
329 | struct stat statbuf; | 323 | struct stat statbuf; |
@@ -333,13 +327,10 @@ extern int init_main(int argc, char **argv) | |||
333 | const char* const* tty0_commands = init_commands; | 327 | const char* const* tty0_commands = init_commands; |
334 | const char* const* tty1_commands = shell_commands; | 328 | const char* const* tty1_commands = shell_commands; |
335 | char *hello_msg_format = | 329 | char *hello_msg_format = |
336 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | 330 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
337 | const char *no_memory = | 331 | const char *no_memory = |
338 | "Sorry, your computer does not have enough memory.\r\n"; | 332 | "Sorry, your computer does not have enough memory.\r\n"; |
339 | 333 | ||
340 | pid = getpid(); | ||
341 | |||
342 | #ifndef DEBUG_INIT | ||
343 | /* Set up sig handlers */ | 334 | /* Set up sig handlers */ |
344 | signal(SIGUSR1, halt_signal); | 335 | signal(SIGUSR1, halt_signal); |
345 | signal(SIGSEGV, halt_signal); | 336 | signal(SIGSEGV, halt_signal); |
@@ -353,7 +344,6 @@ extern int init_main(int argc, char **argv) | |||
353 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 344 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
354 | * SIGINT on CAD so we can shut things down gracefully... */ | 345 | * SIGINT on CAD so we can shut things down gracefully... */ |
355 | reboot(RB_DISABLE_CAD); | 346 | reboot(RB_DISABLE_CAD); |
356 | #endif | ||
357 | 347 | ||
358 | /* Figure out where the default console should be */ | 348 | /* Figure out where the default console should be */ |
359 | console_init(); | 349 | console_init(); |
@@ -370,8 +360,8 @@ extern int init_main(int argc, char **argv) | |||
370 | putenv(PATH_DEFAULT); | 360 | putenv(PATH_DEFAULT); |
371 | 361 | ||
372 | /* Hello world */ | 362 | /* Hello world */ |
373 | message(log, hello_msg_format, pid, BB_VER, BB_BT); | 363 | message(log, hello_msg_format, BB_VER, BB_BT); |
374 | fprintf(stderr, hello_msg_format, pid, BB_VER, BB_BT); | 364 | fprintf(stderr, hello_msg_format, BB_VER, BB_BT); |
375 | 365 | ||
376 | 366 | ||
377 | /* Mount /proc */ | 367 | /* Mount /proc */ |
@@ -385,7 +375,7 @@ extern int init_main(int argc, char **argv) | |||
385 | 375 | ||
386 | /* Make sure there is enough memory to do something useful */ | 376 | /* Make sure there is enough memory to do something useful */ |
387 | set_free_pages(); | 377 | set_free_pages(); |
388 | if (mem_total() < 2000) { | 378 | if (mem_total() < 3500) { |
389 | int retval; | 379 | int retval; |
390 | retval = stat("/etc/fstab", &statbuf); | 380 | retval = stat("/etc/fstab", &statbuf); |
391 | if (retval) { | 381 | if (retval) { |
diff --git a/init/init.c b/init/init.c index d950e2bef..b8cbbf64e 100644 --- a/init/init.c +++ b/init/init.c | |||
@@ -41,8 +41,6 @@ | |||
41 | #include <sys/vt.h> /* for vt_stat */ | 41 | #include <sys/vt.h> /* for vt_stat */ |
42 | #include <sys/ioctl.h> | 42 | #include <sys/ioctl.h> |
43 | 43 | ||
44 | #define DEBUG_INIT | ||
45 | |||
46 | #define CONSOLE "/dev/console" /* Logical system console */ | 44 | #define CONSOLE "/dev/console" /* Logical system console */ |
47 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ | 45 | #define VT_PRIMARY "/dev/tty0" /* Virtual console master */ |
48 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ | 46 | #define VT_SECONDARY "/dev/tty1" /* Virtual console master */ |
@@ -272,7 +270,6 @@ static pid_t run(const char * const* command, | |||
272 | } | 270 | } |
273 | 271 | ||
274 | 272 | ||
275 | #ifndef DEBUG_INIT | ||
276 | static void shutdown_system(void) | 273 | static void shutdown_system(void) |
277 | { | 274 | { |
278 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; | 275 | const char* const swap_off_cmd[] = { "/bin/swapoff", "-a", 0}; |
@@ -318,12 +315,9 @@ static void reboot_signal(int sig) | |||
318 | exit(0); | 315 | exit(0); |
319 | } | 316 | } |
320 | 317 | ||
321 | #endif | ||
322 | |||
323 | extern int init_main(int argc, char **argv) | 318 | extern int init_main(int argc, char **argv) |
324 | { | 319 | { |
325 | int run_rc = TRUE; | 320 | int run_rc = TRUE; |
326 | pid_t pid = 0; | ||
327 | pid_t pid1 = 0; | 321 | pid_t pid1 = 0; |
328 | pid_t pid2 = 0; | 322 | pid_t pid2 = 0; |
329 | struct stat statbuf; | 323 | struct stat statbuf; |
@@ -333,13 +327,10 @@ extern int init_main(int argc, char **argv) | |||
333 | const char* const* tty0_commands = init_commands; | 327 | const char* const* tty0_commands = init_commands; |
334 | const char* const* tty1_commands = shell_commands; | 328 | const char* const* tty1_commands = shell_commands; |
335 | char *hello_msg_format = | 329 | char *hello_msg_format = |
336 | "init(%d) started: BusyBox v%s (%s) multi-call binary\r\n"; | 330 | "init started: BusyBox v%s (%s) multi-call binary\r\n"; |
337 | const char *no_memory = | 331 | const char *no_memory = |
338 | "Sorry, your computer does not have enough memory.\r\n"; | 332 | "Sorry, your computer does not have enough memory.\r\n"; |
339 | 333 | ||
340 | pid = getpid(); | ||
341 | |||
342 | #ifndef DEBUG_INIT | ||
343 | /* Set up sig handlers */ | 334 | /* Set up sig handlers */ |
344 | signal(SIGUSR1, halt_signal); | 335 | signal(SIGUSR1, halt_signal); |
345 | signal(SIGSEGV, halt_signal); | 336 | signal(SIGSEGV, halt_signal); |
@@ -353,7 +344,6 @@ extern int init_main(int argc, char **argv) | |||
353 | /* Turn off rebooting via CTL-ALT-DEL -- we get a | 344 | /* Turn off rebooting via CTL-ALT-DEL -- we get a |
354 | * SIGINT on CAD so we can shut things down gracefully... */ | 345 | * SIGINT on CAD so we can shut things down gracefully... */ |
355 | reboot(RB_DISABLE_CAD); | 346 | reboot(RB_DISABLE_CAD); |
356 | #endif | ||
357 | 347 | ||
358 | /* Figure out where the default console should be */ | 348 | /* Figure out where the default console should be */ |
359 | console_init(); | 349 | console_init(); |
@@ -370,8 +360,8 @@ extern int init_main(int argc, char **argv) | |||
370 | putenv(PATH_DEFAULT); | 360 | putenv(PATH_DEFAULT); |
371 | 361 | ||
372 | /* Hello world */ | 362 | /* Hello world */ |
373 | message(log, hello_msg_format, pid, BB_VER, BB_BT); | 363 | message(log, hello_msg_format, BB_VER, BB_BT); |
374 | fprintf(stderr, hello_msg_format, pid, BB_VER, BB_BT); | 364 | fprintf(stderr, hello_msg_format, BB_VER, BB_BT); |
375 | 365 | ||
376 | 366 | ||
377 | /* Mount /proc */ | 367 | /* Mount /proc */ |
@@ -385,7 +375,7 @@ extern int init_main(int argc, char **argv) | |||
385 | 375 | ||
386 | /* Make sure there is enough memory to do something useful */ | 376 | /* Make sure there is enough memory to do something useful */ |
387 | set_free_pages(); | 377 | set_free_pages(); |
388 | if (mem_total() < 2000) { | 378 | if (mem_total() < 3500) { |
389 | int retval; | 379 | int retval; |
390 | retval = stat("/etc/fstab", &statbuf); | 380 | retval = stat("/etc/fstab", &statbuf); |
391 | if (retval) { | 381 | if (retval) { |
diff --git a/internal.h b/internal.h index 35f990aa7..81271be94 100644 --- a/internal.h +++ b/internal.h | |||
@@ -102,6 +102,7 @@ extern int rm_main(int argc, char** argv); | |||
102 | extern int scan_partitions_main(int argc, char** argv); | 102 | extern int scan_partitions_main(int argc, char** argv); |
103 | extern int sh_main(int argc, char** argv); | 103 | extern int sh_main(int argc, char** argv); |
104 | extern int sfdisk_main(int argc, char** argv); | 104 | extern int sfdisk_main(int argc, char** argv); |
105 | extern int sed_main(int argc, char** argv); | ||
105 | extern int sleep_main(int argc, char** argv); | 106 | extern int sleep_main(int argc, char** argv); |
106 | extern int swap_on_off_main(int argc, char** argv); | 107 | extern int swap_on_off_main(int argc, char** argv); |
107 | extern int sync_main(int argc, char** argv); | 108 | extern int sync_main(int argc, char** argv); |
@@ -25,6 +25,28 @@ extern int find_match(char *haystack, char *needle, int ignoreCase) | |||
25 | return( status); | 25 | return( status); |
26 | } | 26 | } |
27 | 27 | ||
28 | /* This performs substitutions after a regexp match has been found. | ||
29 | * The new string is returned. It is malloc'ed, and do must be freed. */ | ||
30 | extern char* replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) | ||
31 | { | ||
32 | int status; | ||
33 | char* newHaystack; | ||
34 | struct regexp* re; | ||
35 | newHaystack = (char *)malloc((unsigned)(strlen(haystack) - | ||
36 | strlen(needle) + strlen(newNeedle)); | ||
37 | re = regcomp( needle); | ||
38 | status = regexec(re, haystack, FALSE, ignoreCase); | ||
39 | |||
40 | return( newHaystack) | ||
41 | } | ||
42 | |||
43 | |||
44 | extern void regsub(regexp* re, char* src, char* dst) | ||
45 | |||
46 | free( re); | ||
47 | return( status); | ||
48 | } | ||
49 | |||
28 | 50 | ||
29 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to | 51 | /* code swiped from elvis-tiny 1.4 (a clone of vi) and adjusted to |
30 | * suit the needs of busybox by Erik Andersen. | 52 | * suit the needs of busybox by Erik Andersen. |
@@ -43,6 +43,7 @@ extern int regexec(struct regexp* re, char* str, int bol, int ignoreCase); | |||
43 | extern void regsub(struct regexp* re, char* src, char* dst); | 43 | extern void regsub(struct regexp* re, char* src, char* dst); |
44 | 44 | ||
45 | extern int find_match(char *haystack, char *needle, int ignoreCase); | 45 | extern int find_match(char *haystack, char *needle, int ignoreCase); |
46 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase); | ||
46 | 47 | ||
47 | #endif | 48 | #endif |
48 | 49 | ||
@@ -33,12 +33,10 @@ | |||
33 | 33 | ||
34 | static const char sed_usage[] = | 34 | static const char sed_usage[] = |
35 | "sed [-n] [-e script] [file...]\n" | 35 | "sed [-n] [-e script] [file...]\n" |
36 | "Allowed scripts come in two forms:\n" | 36 | "Allowed scripts come in the following form:\n\n" |
37 | "'/regexp/[gp]'\n" | ||
38 | "\tattempt to match regexp against the pattern space\n" | ||
39 | "'s/regexp/replacement/[gp]'\n" | 37 | "'s/regexp/replacement/[gp]'\n" |
40 | "\tattempt to match regexp against the pattern space\n" | 38 | "\tattempt to match regexp against the pattern space\n" |
41 | "\tand if successful replaces the matched portion with replacement." | 39 | "\tand if successful replaces the matched portion with replacement.\n\n" |
42 | "Options:\n" | 40 | "Options:\n" |
43 | "-e\tadd the script to the commands to be executed\n" | 41 | "-e\tadd the script to the commands to be executed\n" |
44 | "-n\tsuppress automatic printing of pattern space\n\n" | 42 | "-n\tsuppress automatic printing of pattern space\n\n" |
@@ -49,64 +47,86 @@ static const char sed_usage[] = | |||
49 | #endif | 47 | #endif |
50 | 48 | ||
51 | 49 | ||
52 | static int replaceFlag = FALSE; | ||
53 | static int noprintFlag = FALSE; | ||
54 | 50 | ||
55 | 51 | ||
56 | extern int sed_main (int argc, char **argv) | 52 | extern int sed_main (int argc, char **argv) |
57 | { | 53 | { |
58 | FILE *fp; | 54 | FILE *fp; |
59 | const char *needle; | 55 | char *needle=NULL, *newNeedle=NULL; |
60 | const char *name; | 56 | char *name; |
61 | const char *cp; | 57 | char *cp; |
62 | int tellName=TRUE; | ||
63 | int ignoreCase=FALSE; | 58 | int ignoreCase=FALSE; |
64 | int tellLine=FALSE; | 59 | int foundOne=FALSE; |
65 | long line; | 60 | int noprintFlag=FALSE; |
66 | char haystack[BUF_SIZE]; | 61 | int stopNow; |
67 | 62 | char *haystack; | |
68 | ignoreCase = FALSE; | ||
69 | tellLine = FALSE; | ||
70 | 63 | ||
71 | argc--; | 64 | argc--; |
72 | argv++; | 65 | argv++; |
73 | if (argc < 1) { | 66 | if (argc < 1) { |
74 | usage(grep_usage); | 67 | usage(sed_usage); |
75 | } | 68 | } |
76 | 69 | ||
77 | if (**argv == '-') { | 70 | if (**argv == '-') { |
78 | argc--; | 71 | argc--; |
79 | cp = *argv++; | 72 | cp = *argv++; |
73 | stopNow=FALSE; | ||
80 | 74 | ||
81 | while (*++cp) | 75 | while (*++cp && stopNow==FALSE) |
82 | switch (*cp) { | 76 | switch (*cp) { |
83 | case 'n': | 77 | case 'n': |
84 | noprintFlag = TRUE; | 78 | noprintFlag = TRUE; |
85 | break; | 79 | break; |
86 | case 'e': | 80 | case 'e': |
87 | if (*(*argv)+1 != '\'' && **argv != '\"') { | 81 | if (*(cp+1)==0 && --argc < 0) { |
88 | if (--argc == 0) | 82 | fprintf(stderr, "A\n"); |
89 | usage( mkdir_usage); | 83 | usage( sed_usage); |
90 | ++argv; | ||
91 | if (*(*argv)+1 != '\'' && **argv != '\"') { | ||
92 | usage( mkdir_usage); | ||
93 | } | 84 | } |
94 | /* Find the specified modes */ | 85 | cp = *argv++; |
95 | mode = 0; | 86 | while( *cp ) { |
96 | if ( parse_mode(*(++argv), &mode) == FALSE ) { | 87 | if (*cp == 's' && strlen(cp) > 3 && *(cp+1) == '/') { |
97 | fprintf(stderr, "Unknown mode: %s\n", *argv); | 88 | char* pos=needle=cp+2; |
98 | exit( FALSE); | 89 | for(;;) { |
90 | pos = strchr(pos, '/'); | ||
91 | if (pos==NULL) { | ||
92 | fprintf(stderr, "B\n"); | ||
93 | usage( sed_usage); | ||
94 | } | ||
95 | if (*(pos-1) == '\\') { | ||
96 | pos++; | ||
97 | continue; | ||
98 | } | ||
99 | break; | ||
100 | } | ||
101 | *pos=0; | ||
102 | newNeedle=++pos; | ||
103 | for(;;) { | ||
104 | pos = strchr(pos, '/'); | ||
105 | if (pos==NULL) { | ||
106 | fprintf(stderr, "C\n"); | ||
107 | usage( sed_usage); | ||
108 | } | ||
109 | if (*(pos-1) == '\\') { | ||
110 | pos++; | ||
111 | continue; | ||
112 | } | ||
113 | break; | ||
114 | } | ||
115 | *pos=0; | ||
116 | } | ||
117 | cp++; | ||
99 | } | 118 | } |
119 | fprintf(stderr, "replace '%s' with '%s'\n", needle, newNeedle); | ||
120 | stopNow=TRUE; | ||
100 | break; | 121 | break; |
101 | 122 | ||
102 | default: | 123 | default: |
103 | usage(grep_usage); | 124 | fprintf(stderr, "D\n"); |
125 | usage(sed_usage); | ||
104 | } | 126 | } |
105 | } | 127 | } |
106 | 128 | ||
107 | needle = *argv++; | 129 | fprintf(stderr, "argc=%d\n", argc); |
108 | argc--; | ||
109 | |||
110 | while (argc-- > 0) { | 130 | while (argc-- > 0) { |
111 | name = *argv++; | 131 | name = *argv++; |
112 | 132 | ||
@@ -115,25 +135,19 @@ extern int sed_main (int argc, char **argv) | |||
115 | perror (name); | 135 | perror (name); |
116 | continue; | 136 | continue; |
117 | } | 137 | } |
138 | fprintf(stderr, "filename is '%s'\n", name); | ||
118 | 139 | ||
119 | line = 0; | 140 | haystack = (char*)malloc( 80); |
120 | |||
121 | while (fgets (haystack, sizeof (haystack), fp)) { | 141 | 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 | 142 | ||
143 | foundOne = replace_match(haystack, needle, newNeedle, ignoreCase); | ||
144 | if (noprintFlag==TRUE && foundOne==TRUE) | ||
135 | fputs (haystack, stdout); | 145 | fputs (haystack, stdout); |
136 | } | 146 | else |
147 | fputs (haystack, stdout); | ||
148 | /* Avoid any mem leaks */ | ||
149 | free(haystack); | ||
150 | haystack = (char*)malloc( BUF_SIZE); | ||
137 | } | 151 | } |
138 | 152 | ||
139 | if (ferror (fp)) | 153 | if (ferror (fp)) |
@@ -686,41 +686,6 @@ my_getgrgid(char* group, gid_t gid) | |||
686 | 686 | ||
687 | 687 | ||
688 | 688 | ||
689 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND ) | ||
690 | /* This tries to find a needle in a haystack, but does so by | ||
691 | * only trying to match literal strings (look 'ma, no regexps!) | ||
692 | * This is short, sweet, and carries _very_ little baggage, | ||
693 | * unlike its beefier cousin a few lines down... | ||
694 | * -Erik Andersen | ||
695 | */ | ||
696 | extern int find_match(char *haystack, char *needle, int ignoreCase) | ||
697 | { | ||
698 | |||
699 | if (ignoreCase == FALSE) { | ||
700 | haystack = strstr (haystack, needle); | ||
701 | if (haystack == NULL) | ||
702 | return FALSE; | ||
703 | return TRUE; | ||
704 | } else { | ||
705 | int i; | ||
706 | char needle1[BUF_SIZE]; | ||
707 | char haystack1[BUF_SIZE]; | ||
708 | |||
709 | strncpy( haystack1, haystack, sizeof(haystack1)); | ||
710 | strncpy( needle1, needle, sizeof(needle1)); | ||
711 | for( i=0; i<sizeof(haystack1) && haystack1[i]; i++) | ||
712 | haystack1[i]=tolower( haystack1[i]); | ||
713 | for( i=0; i<sizeof(needle1) && needle1[i]; i++) | ||
714 | needle1[i]=tolower( needle1[i]); | ||
715 | haystack = strstr (haystack1, needle1); | ||
716 | if (haystack == NULL) | ||
717 | return FALSE; | ||
718 | return TRUE; | ||
719 | } | ||
720 | } | ||
721 | #endif | ||
722 | |||
723 | |||
724 | 689 | ||
725 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) | 690 | #if (defined BB_CHVT) || (defined BB_DEALLOCVT) |
726 | 691 | ||
@@ -812,5 +777,71 @@ int get_console_fd(char* tty_name) | |||
812 | #endif | 777 | #endif |
813 | 778 | ||
814 | 779 | ||
780 | #if !defined BB_REGEXP && (defined BB_GREP || defined BB_FIND ) | ||
781 | /* This tries to find a needle in a haystack, but does so by | ||
782 | * only trying to match literal strings (look 'ma, no regexps!) | ||
783 | * This is short, sweet, and carries _very_ little baggage, | ||
784 | * unlike its beefier cousin a few lines down... | ||
785 | * -Erik Andersen | ||
786 | */ | ||
787 | extern int find_match(char *haystack, char *needle, int ignoreCase) | ||
788 | { | ||
789 | |||
790 | if (ignoreCase == FALSE) { | ||
791 | haystack = strstr (haystack, needle); | ||
792 | if (haystack == NULL) | ||
793 | return FALSE; | ||
794 | return TRUE; | ||
795 | } else { | ||
796 | int i; | ||
797 | char needle1[BUF_SIZE]; | ||
798 | char haystack1[BUF_SIZE]; | ||
799 | |||
800 | strncpy( haystack1, haystack, sizeof(haystack1)); | ||
801 | strncpy( needle1, needle, sizeof(needle1)); | ||
802 | for( i=0; i<sizeof(haystack1) && haystack1[i]; i++) | ||
803 | haystack1[i]=tolower( haystack1[i]); | ||
804 | for( i=0; i<sizeof(needle1) && needle1[i]; i++) | ||
805 | needle1[i]=tolower( needle1[i]); | ||
806 | haystack = strstr (haystack1, needle1); | ||
807 | if (haystack == NULL) | ||
808 | return FALSE; | ||
809 | return TRUE; | ||
810 | } | ||
811 | } | ||
812 | |||
813 | |||
814 | /* This performs substitutions after a regexp match has been found. */ | ||
815 | extern int replace_match(char *haystack, char *needle, char *newNeedle, int ignoreCase) | ||
816 | { | ||
817 | int foundOne; | ||
818 | char *where, *slider; | ||
819 | |||
820 | if (ignoreCase == FALSE) { | ||
821 | /*Find needle in haystack */ | ||
822 | where = strstr (haystack, needle); | ||
823 | while(where!=NULL) { | ||
824 | foundOne++; | ||
825 | fprintf(stderr, "A match: haystack='%s'\n", haystack); | ||
826 | haystack = (char *)realloc(haystack, (unsigned)(strlen(haystack) - | ||
827 | strlen(needle) + strlen(newNeedle))); | ||
828 | for(slider=haystack;slider!=where;slider++); | ||
829 | *slider=0; | ||
830 | haystack=strcat(haystack, newNeedle); | ||
831 | slider+=1+sizeof(newNeedle); | ||
832 | haystack = strcat(haystack, slider); | ||
833 | where = strstr (where+1, needle); | ||
834 | } | ||
835 | } else { | ||
836 | // FIXME | ||
837 | |||
838 | } | ||
839 | if (foundOne) | ||
840 | return TRUE; | ||
841 | else | ||
842 | return FALSE; | ||
843 | } | ||
844 | |||
845 | #endif | ||
815 | /* END CODE */ | 846 | /* END CODE */ |
816 | 847 | ||