aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMark Whitley <markw@lineo.com>2000-08-21 21:29:20 +0000
committerMark Whitley <markw@lineo.com>2000-08-21 21:29:20 +0000
commit0e4cec0be89fcfd6f1e0a6ab3686e51a6ce7abe2 (patch)
treed4695bf1c726563c70fa826dec84cc4cc4e37323
parentdc746169cfd07a67cf99d1767743bfe0f94fdcb5 (diff)
downloadbusybox-w32-0e4cec0be89fcfd6f1e0a6ab3686e51a6ce7abe2.tar.gz
busybox-w32-0e4cec0be89fcfd6f1e0a6ab3686e51a6ce7abe2.tar.bz2
busybox-w32-0e4cec0be89fcfd6f1e0a6ab3686e51a6ce7abe2.zip
Applied patch from Mark Ferrell <mferrell@mvista.com> to use the 'p' option in
substitution expressions.
-rw-r--r--busybox.def.h3
-rw-r--r--editors/sed.c48
-rw-r--r--sed.c48
3 files changed, 81 insertions, 18 deletions
diff --git a/busybox.def.h b/busybox.def.h
index d9477d41c..fd4302a84 100644
--- a/busybox.def.h
+++ b/busybox.def.h
@@ -217,6 +217,9 @@
217// Enable support for "--exclude" for excluding files 217// Enable support for "--exclude" for excluding files
218#define BB_FEATURE_TAR_EXCLUDE 218#define BB_FEATURE_TAR_EXCLUDE
219// 219//
220// Enable support for s///p pattern matching
221#define BB_FEATURE_SED_PATTERN_SPACE
222//
220//// Enable reverse sort 223//// Enable reverse sort
221#define BB_FEATURE_SORT_REVERSE 224#define BB_FEATURE_SORT_REVERSE
222// 225//
diff --git a/editors/sed.c b/editors/sed.c
index 60b1e8d2e..f3c3262e4 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -63,6 +63,11 @@ extern char *optarg; /* ditto */
63/* options */ 63/* options */
64static int be_quiet = 0; 64static int be_quiet = 0;
65 65
66static const int SUB_G = 1 << 0;
67#ifdef BB_FEATURE_SED_PATTERN_SPACE
68static const int SUB_P = 1 << 1;
69#endif
70
66struct sed_cmd { 71struct sed_cmd {
67 72
68 /* address storage */ 73 /* address storage */
@@ -80,7 +85,11 @@ struct sed_cmd {
80 unsigned int num_backrefs:4; /* how many back references (\1..\9) */ 85 unsigned int num_backrefs:4; /* how many back references (\1..\9) */
81 /* Note: GNU/POSIX sed does not save more than nine backrefs, so 86 /* Note: GNU/POSIX sed does not save more than nine backrefs, so
82 * we only use 4 bits to hold the number */ 87 * we only use 4 bits to hold the number */
83 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */ 88#ifndef BB_FEATURE_SED_PATTERN_SPACE
89 unsigned int sub_flags:1; /* sed -e 's/foo/bar/g' (global) */
90#else
91 unsigned int sub_flags:2; /* sed -e 's/foo/bar/gp' (global/pattern) */
92#endif
84 93
85 /* edit command (a,i,c) speicific field */ 94 /* edit command (a,i,c) speicific field */
86 char *editline; 95 char *editline;
@@ -244,8 +253,8 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
244 253
245 /* 254 /*
246 * the string that gets passed to this function should look like this: 255 * the string that gets passed to this function should look like this:
247 * s/match/replace/gI 256 * s/match/replace/gIp
248 * || | || 257 * || | |||
249 * mandatory optional 258 * mandatory optional
250 * 259 *
251 * (all three of the '/' slashes are mandatory) 260 * (all three of the '/' slashes are mandatory)
@@ -285,11 +294,16 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
285 while (substr[++idx]) { 294 while (substr[++idx]) {
286 switch (substr[idx]) { 295 switch (substr[idx]) {
287 case 'g': 296 case 'g':
288 sed_cmd->sub_g = 1; 297 sed_cmd->sub_flags = SUB_G;
289 break; 298 break;
290 case 'I': 299 case 'I':
291 cflags |= REG_ICASE; 300 cflags |= REG_ICASE;
292 break; 301 break;
302#ifdef BB_FEATURE_SED_PATTERN_SPACE
303 case 'p':
304 sed_cmd->sub_flags = SUB_P;
305 break;
306#endif
293 default: 307 default:
294 /* any whitespace or semicolon trailing after a s/// is ok */ 308 /* any whitespace or semicolon trailing after a s/// is ok */
295 if (strchr("; \t\v\n\r", substr[idx])) 309 if (strchr("; \t\v\n\r", substr[idx]))
@@ -535,10 +549,17 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line)
535 /* if we can match the search string... */ 549 /* if we can match the search string... */
536 if (regexec(sed_cmd->sub_match, ptr, sed_cmd->num_backrefs+1, regmatch, 0) == 0) { 550 if (regexec(sed_cmd->sub_match, ptr, sed_cmd->num_backrefs+1, regmatch, 0) == 0) {
537 /* print everything before the match, */ 551 /* print everything before the match, */
538 for (i = 0; i < regmatch[0].rm_so; i++) 552 for (i = 0; i < regmatch[0].rm_so; i++) {
553#ifdef BB_FEATURE_SED_PATTERN_SPACE
554 if(!be_quiet || (sed_cmd->sub_flags & SUB_P))
555#endif
539 fputc(ptr[i], stdout); 556 fputc(ptr[i], stdout);
557 }
540 558
541 /* then print the substitution in its place */ 559 /* then print the substitution in its place */
560#ifdef BB_FEATURE_SED_PATTERN_SPACE
561 if(!be_quiet || (sed_cmd->sub_flags & SUB_P))
562#endif
542 print_subst_w_backrefs(ptr, sed_cmd->replace, regmatch); 563 print_subst_w_backrefs(ptr, sed_cmd->replace, regmatch);
543 564
544 /* then advance past the match */ 565 /* then advance past the match */
@@ -548,7 +569,7 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line)
548 altered++; 569 altered++;
549 570
550 /* if we're not doing this globally... */ 571 /* if we're not doing this globally... */
551 if (!sed_cmd->sub_g) 572 if (!sed_cmd->sub_flags & SUB_G)
552 break; 573 break;
553 } 574 }
554 /* if we COULD NOT match the search string (meaning we've gone past 575 /* if we COULD NOT match the search string (meaning we've gone past
@@ -558,7 +579,11 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line)
558 } 579 }
559 580
560 /* is there anything left to print? */ 581 /* is there anything left to print? */
582#ifdef BB_FEATURE_SED_PATTERN_SPACE
583 if (*ptr && (!be_quiet || sed_cmds->sub_flags & SUB_P))
584#else
561 if (*ptr) 585 if (*ptr)
586#endif
562 fputs(ptr, stdout); 587 fputs(ptr, stdout);
563 588
564 /* cleanup */ 589 /* cleanup */
@@ -656,9 +681,14 @@ static void process_file(FILE *file)
656 681
657 } 682 }
658 683
659 /* we will print the line unless we were told to be quiet or if the 684 /* we will print the line unless we were told to be quiet or if
660 * line was altered (via a 'd'elete or 's'ubstitution) */ 685 * the line was altered (via a 'd'elete or 's'ubstitution) */
661 if (!be_quiet && !line_altered) 686#ifndef BB_FEATURE_SED_PATTERN_SPACE
687 if (!be_quiet &&!line_altered)
688#else
689 /* we where specificly requested to print the output */
690 if ((!be_quiet || (sed_cmds[i].sub_flags & SUB_P)) && !line_altered)
691#endif
662 fputs(line, stdout); 692 fputs(line, stdout);
663 693
664 free(line); 694 free(line);
diff --git a/sed.c b/sed.c
index 60b1e8d2e..f3c3262e4 100644
--- a/sed.c
+++ b/sed.c
@@ -63,6 +63,11 @@ extern char *optarg; /* ditto */
63/* options */ 63/* options */
64static int be_quiet = 0; 64static int be_quiet = 0;
65 65
66static const int SUB_G = 1 << 0;
67#ifdef BB_FEATURE_SED_PATTERN_SPACE
68static const int SUB_P = 1 << 1;
69#endif
70
66struct sed_cmd { 71struct sed_cmd {
67 72
68 /* address storage */ 73 /* address storage */
@@ -80,7 +85,11 @@ struct sed_cmd {
80 unsigned int num_backrefs:4; /* how many back references (\1..\9) */ 85 unsigned int num_backrefs:4; /* how many back references (\1..\9) */
81 /* Note: GNU/POSIX sed does not save more than nine backrefs, so 86 /* Note: GNU/POSIX sed does not save more than nine backrefs, so
82 * we only use 4 bits to hold the number */ 87 * we only use 4 bits to hold the number */
83 unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */ 88#ifndef BB_FEATURE_SED_PATTERN_SPACE
89 unsigned int sub_flags:1; /* sed -e 's/foo/bar/g' (global) */
90#else
91 unsigned int sub_flags:2; /* sed -e 's/foo/bar/gp' (global/pattern) */
92#endif
84 93
85 /* edit command (a,i,c) speicific field */ 94 /* edit command (a,i,c) speicific field */
86 char *editline; 95 char *editline;
@@ -244,8 +253,8 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
244 253
245 /* 254 /*
246 * the string that gets passed to this function should look like this: 255 * the string that gets passed to this function should look like this:
247 * s/match/replace/gI 256 * s/match/replace/gIp
248 * || | || 257 * || | |||
249 * mandatory optional 258 * mandatory optional
250 * 259 *
251 * (all three of the '/' slashes are mandatory) 260 * (all three of the '/' slashes are mandatory)
@@ -285,11 +294,16 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
285 while (substr[++idx]) { 294 while (substr[++idx]) {
286 switch (substr[idx]) { 295 switch (substr[idx]) {
287 case 'g': 296 case 'g':
288 sed_cmd->sub_g = 1; 297 sed_cmd->sub_flags = SUB_G;
289 break; 298 break;
290 case 'I': 299 case 'I':
291 cflags |= REG_ICASE; 300 cflags |= REG_ICASE;
292 break; 301 break;
302#ifdef BB_FEATURE_SED_PATTERN_SPACE
303 case 'p':
304 sed_cmd->sub_flags = SUB_P;
305 break;
306#endif
293 default: 307 default:
294 /* any whitespace or semicolon trailing after a s/// is ok */ 308 /* any whitespace or semicolon trailing after a s/// is ok */
295 if (strchr("; \t\v\n\r", substr[idx])) 309 if (strchr("; \t\v\n\r", substr[idx]))
@@ -535,10 +549,17 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line)
535 /* if we can match the search string... */ 549 /* if we can match the search string... */
536 if (regexec(sed_cmd->sub_match, ptr, sed_cmd->num_backrefs+1, regmatch, 0) == 0) { 550 if (regexec(sed_cmd->sub_match, ptr, sed_cmd->num_backrefs+1, regmatch, 0) == 0) {
537 /* print everything before the match, */ 551 /* print everything before the match, */
538 for (i = 0; i < regmatch[0].rm_so; i++) 552 for (i = 0; i < regmatch[0].rm_so; i++) {
553#ifdef BB_FEATURE_SED_PATTERN_SPACE
554 if(!be_quiet || (sed_cmd->sub_flags & SUB_P))
555#endif
539 fputc(ptr[i], stdout); 556 fputc(ptr[i], stdout);
557 }
540 558
541 /* then print the substitution in its place */ 559 /* then print the substitution in its place */
560#ifdef BB_FEATURE_SED_PATTERN_SPACE
561 if(!be_quiet || (sed_cmd->sub_flags & SUB_P))
562#endif
542 print_subst_w_backrefs(ptr, sed_cmd->replace, regmatch); 563 print_subst_w_backrefs(ptr, sed_cmd->replace, regmatch);
543 564
544 /* then advance past the match */ 565 /* then advance past the match */
@@ -548,7 +569,7 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line)
548 altered++; 569 altered++;
549 570
550 /* if we're not doing this globally... */ 571 /* if we're not doing this globally... */
551 if (!sed_cmd->sub_g) 572 if (!sed_cmd->sub_flags & SUB_G)
552 break; 573 break;
553 } 574 }
554 /* if we COULD NOT match the search string (meaning we've gone past 575 /* if we COULD NOT match the search string (meaning we've gone past
@@ -558,7 +579,11 @@ static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line)
558 } 579 }
559 580
560 /* is there anything left to print? */ 581 /* is there anything left to print? */
582#ifdef BB_FEATURE_SED_PATTERN_SPACE
583 if (*ptr && (!be_quiet || sed_cmds->sub_flags & SUB_P))
584#else
561 if (*ptr) 585 if (*ptr)
586#endif
562 fputs(ptr, stdout); 587 fputs(ptr, stdout);
563 588
564 /* cleanup */ 589 /* cleanup */
@@ -656,9 +681,14 @@ static void process_file(FILE *file)
656 681
657 } 682 }
658 683
659 /* we will print the line unless we were told to be quiet or if the 684 /* we will print the line unless we were told to be quiet or if
660 * line was altered (via a 'd'elete or 's'ubstitution) */ 685 * the line was altered (via a 'd'elete or 's'ubstitution) */
661 if (!be_quiet && !line_altered) 686#ifndef BB_FEATURE_SED_PATTERN_SPACE
687 if (!be_quiet &&!line_altered)
688#else
689 /* we where specificly requested to print the output */
690 if ((!be_quiet || (sed_cmds[i].sub_flags & SUB_P)) && !line_altered)
691#endif
662 fputs(line, stdout); 692 fputs(line, stdout);
663 693
664 free(line); 694 free(line);