diff options
author | Mark Whitley <markw@lineo.com> | 2000-11-03 22:47:00 +0300 |
---|---|---|
committer | Mark Whitley <markw@lineo.com> | 2000-11-03 22:47:00 +0300 |
commit | 2dc192fd991ff24a2e1e469b988391d7830ad4b6 (patch) | |
tree | f093280113a03e33ad105928373a4bb2546f0d2c | |
parent | a75466e9819a7172ba48b06958a868022f4510a9 (diff) |
Re-worked the support for s///p, fixed a backref limit buglet, and cleaned up
a few other ugly places (do_subst_command got a much-needed overhaul). Also
took out BB_FEATURE_SED_PATTERN_SPACE from Config.h[.Hurd] as the 'p' is now a
standard feature (adds almost no bloat).
-rw-r--r-- | Config.h | 3 | ||||
-rw-r--r-- | Config.h.Hurd | 3 | ||||
-rw-r--r-- | editors/sed.c | 157 | ||||
-rw-r--r-- | sed.c | 157 |
4 files changed, 172 insertions, 148 deletions
@@ -223,9 +223,6 @@ // Enable support for "--exclude" for excluding files #define BB_FEATURE_TAR_EXCLUDE // -// Enable support for s///p pattern matching -#define BB_FEATURE_SED_PATTERN_SPACE -// //// Enable reverse sort #define BB_FEATURE_SORT_REVERSE // diff --git a/Config.h.Hurd b/Config.h.Hurd index 79fdff6ad..9238761a2 100644 --- a/Config.h.Hurd +++ b/Config.h.Hurd @@ -218,9 +218,6 @@ // Enable support for "--exclude" for excluding files #define BB_FEATURE_TAR_EXCLUDE // -// Enable support for s///p pattern matching -#define BB_FEATURE_SED_PATTERN_SPACE -// //// Enable reverse sort #define BB_FEATURE_SORT_REVERSE // diff --git a/editors/sed.c b/editors/sed.c index 75435f67a..eaca9ada5 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -62,13 +62,12 @@ extern char *optarg; /* ditto */ /* options */ static int be_quiet = 0; -static const int SUB_G = 1 << 0; -#ifdef BB_FEATURE_SED_PATTERN_SPACE -static const int SUB_P = 1 << 1; -#endif struct sed_cmd { + + /* GENERAL FIELDS */ + /* address storage */ int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */ int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */ @@ -78,19 +77,21 @@ struct sed_cmd { /* the command */ char cmd; /* p,d,s (add more at your leisure :-) */ - /* substitution command specific fields */ - regex_t *sub_match; /* sed -e 's/sub_match/replace/' */ - char *replace; /* sed -e 's/sub_match/replace/' XXX: who will hold the \1 \2 \3s? */ + + /* SUBSTITUTION COMMAND SPECIFIC FIELDS */ + + /* sed -e 's/sub_match/replace/' */ + regex_t *sub_match; + char *replace; unsigned int num_backrefs:4; /* how many back references (\1..\9) */ /* Note: GNU/POSIX sed does not save more than nine backrefs, so * we only use 4 bits to hold the number */ -#ifndef BB_FEATURE_SED_PATTERN_SPACE - unsigned int sub_flags:1; /* sed -e 's/foo/bar/g' (global) */ -#else - unsigned int sub_flags:2; /* sed -e 's/foo/bar/gp' (global/pattern) */ -#endif + unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */ + unsigned int sub_p:2; /* sed -e 's/foo/bar/p' (print substitution) */ + + + /* EDIT COMMAND (a,i,c) SPEICIFIC FIELDS */ - /* edit command (a,i,c) speicific field */ char *editline; }; @@ -278,7 +279,7 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) /* sed_cmd->num_backrefs = 0; */ /* XXX: not needed? --apparently not */ for (j = 0; match[j]; j++) { /* GNU/POSIX sed does not save more than nine backrefs */ - if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs < 9) + if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs <= 9) sed_cmd->num_backrefs++; } @@ -293,16 +294,14 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) while (substr[++idx]) { switch (substr[idx]) { case 'g': - sed_cmd->sub_flags = SUB_G; + sed_cmd->sub_g = 1; break; case 'I': cflags |= REG_ICASE; break; -#ifdef BB_FEATURE_SED_PATTERN_SPACE case 'p': - sed_cmd->sub_flags = SUB_P; + sed_cmd->sub_p = 1; break; -#endif default: /* any whitespace or semicolon trailing after a s/// is ok */ if (strchr("; \t\v\n\r", substr[idx])) @@ -534,59 +533,53 @@ static void print_subst_w_backrefs(const char *line, const char *replace, regmat static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) { + char *hackline = (char *)line; int altered = 0; + regmatch_t *regmatch = NULL; - /* we only substitute if the substitution 'search' expression matches */ - if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { - regmatch_t *regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1)); - int i; - char *ptr = (char *)line; - - while (*ptr) { - /* if we can match the search string... */ - if (regexec(sed_cmd->sub_match, ptr, sed_cmd->num_backrefs+1, regmatch, 0) == 0) { - /* print everything before the match, */ - for (i = 0; i < regmatch[0].rm_so; i++) { -#ifdef BB_FEATURE_SED_PATTERN_SPACE - if(!be_quiet || (sed_cmd->sub_flags & SUB_P)) -#endif - fputc(ptr[i], stdout); - } + /* if the user specified that they didn't want anything printed (i.e. a -n + * flag and no 'p' flag after the s///), then there's really no point doing + * anything here. */ + if (be_quiet && !sed_cmd->sub_p) + return 0; - /* then print the substitution in its place */ -#ifdef BB_FEATURE_SED_PATTERN_SPACE - if(!be_quiet || (sed_cmd->sub_flags & SUB_P)) -#endif - print_subst_w_backrefs(ptr, sed_cmd->replace, regmatch); + /* we only proceed if the substitution 'search' expression matches */ + if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == REG_NOMATCH) + return 0; - /* then advance past the match */ - ptr += regmatch[0].rm_eo; + /* whaddaya know, it matched. get the number of back references */ + regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1)); - /* and flag that something has changed */ - altered++; + /* and now, as long as we've got a line to try matching and if we can match + * the search string, we make substitutions */ + while (*hackline && (regexec(sed_cmd->sub_match, hackline, + sed_cmd->num_backrefs+1, regmatch, 0) == 0) ) { + int i; - /* if we're not doing this globally... */ - if (!sed_cmd->sub_flags & SUB_G) - break; - } - /* if we COULD NOT match the search string (meaning we've gone past - * all previous instances), get out */ - else - break; - } + /* print everything before the match */ + for (i = 0; i < regmatch[0].rm_so; i++) + fputc(hackline[i], stdout); - /* is there anything left to print? */ -#ifdef BB_FEATURE_SED_PATTERN_SPACE - if (*ptr && (!be_quiet || sed_cmds->sub_flags & SUB_P)) -#else - if (*ptr) -#endif - fputs(ptr, stdout); + /* then print the substitution string */ + print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch); + + /* advance past the match */ + hackline += regmatch[0].rm_eo; + /* flag that something has changed */ + altered++; - /* cleanup */ - free(regmatch); + /* if we're not doing this globally, get out now */ + if (!sed_cmd->sub_g) + break; } + /* if there's anything left of the line, print it */ + if (*hackline) + fputs(hackline, stdout); + + /* cleanup */ + free(regmatch); + return altered; } @@ -605,7 +598,32 @@ static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) break; case 's': - altered = do_subst_command(sed_cmd, line); + + /* + * Some special cases for 's' printing to make it compliant with + * GNU sed printing behavior (aka "The -n | s///p Matrix"): + * + * -n ONLY = never print anything regardless of any successful + * substitution + * + * s///p ONLY = always print successful substitutions, even if + * the line is going to be printed anyway (line will be printed + * twice). + * + * -n AND s///p = print ONLY a successful substitution ONE TIME; + * no other lines are printed - this is the reason why the 'p' + * flag exists in the first place. + */ + + /* we print the line once, unless we were told to be quiet */ + if (!be_quiet) + altered = do_subst_command(sed_cmd, line); + + /* we also print the line if we were given the 'p' flag + * (this is quite possibly the second printing) */ + if (sed_cmd->sub_p) + altered = do_subst_command(sed_cmd, line); + break; case 'a': @@ -662,7 +680,8 @@ static void process_file(FILE *file) /* are we acting on a range of line numbers? */ else if (sed_cmds[i].beg_line > 0 && sed_cmds[i].end_line != 0) { - if (linenum >= sed_cmds[i].beg_line && (sed_cmds[i].end_line == -1 || linenum <= sed_cmds[i].end_line)) + if (linenum >= sed_cmds[i].beg_line && + (sed_cmds[i].end_line == -1 || linenum <= sed_cmds[i].end_line)) line_altered += do_sed_command(&sed_cmds[i], line); } @@ -678,14 +697,10 @@ static void process_file(FILE *file) } - /* we will print the line unless we were told to be quiet or if - * the line was altered (via a 'd'elete or 's'ubstitution) */ -#ifndef BB_FEATURE_SED_PATTERN_SPACE - if (!be_quiet &&!line_altered) -#else - /* we where specificly requested to print the output */ - if ((!be_quiet || (sed_cmds[i].sub_flags & SUB_P)) && !line_altered) -#endif + /* we will print the line unless we were told to be quiet or if the + * line was altered (via a 'd'elete or 's'ubstitution), in which case + * the altered line was already printed */ + if (!be_quiet && !line_altered) fputs(line, stdout); free(line); @@ -62,13 +62,12 @@ extern char *optarg; /* ditto */ /* options */ static int be_quiet = 0; -static const int SUB_G = 1 << 0; -#ifdef BB_FEATURE_SED_PATTERN_SPACE -static const int SUB_P = 1 << 1; -#endif struct sed_cmd { + + /* GENERAL FIELDS */ + /* address storage */ int beg_line; /* 'sed 1p' 0 == no begining line, apply commands to all lines */ int end_line; /* 'sed 1,3p' 0 == no end line, use only beginning. -1 == $ */ @@ -78,19 +77,21 @@ struct sed_cmd { /* the command */ char cmd; /* p,d,s (add more at your leisure :-) */ - /* substitution command specific fields */ - regex_t *sub_match; /* sed -e 's/sub_match/replace/' */ - char *replace; /* sed -e 's/sub_match/replace/' XXX: who will hold the \1 \2 \3s? */ + + /* SUBSTITUTION COMMAND SPECIFIC FIELDS */ + + /* sed -e 's/sub_match/replace/' */ + regex_t *sub_match; + char *replace; unsigned int num_backrefs:4; /* how many back references (\1..\9) */ /* Note: GNU/POSIX sed does not save more than nine backrefs, so * we only use 4 bits to hold the number */ -#ifndef BB_FEATURE_SED_PATTERN_SPACE - unsigned int sub_flags:1; /* sed -e 's/foo/bar/g' (global) */ -#else - unsigned int sub_flags:2; /* sed -e 's/foo/bar/gp' (global/pattern) */ -#endif + unsigned int sub_g:1; /* sed -e 's/foo/bar/g' (global) */ + unsigned int sub_p:2; /* sed -e 's/foo/bar/p' (print substitution) */ + + + /* EDIT COMMAND (a,i,c) SPEICIFIC FIELDS */ - /* edit command (a,i,c) speicific field */ char *editline; }; @@ -278,7 +279,7 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) /* sed_cmd->num_backrefs = 0; */ /* XXX: not needed? --apparently not */ for (j = 0; match[j]; j++) { /* GNU/POSIX sed does not save more than nine backrefs */ - if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs < 9) + if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs <= 9) sed_cmd->num_backrefs++; } @@ -293,16 +294,14 @@ static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr) while (substr[++idx]) { switch (substr[idx]) { case 'g': - sed_cmd->sub_flags = SUB_G; + sed_cmd->sub_g = 1; break; case 'I': cflags |= REG_ICASE; break; -#ifdef BB_FEATURE_SED_PATTERN_SPACE case 'p': - sed_cmd->sub_flags = SUB_P; + sed_cmd->sub_p = 1; break; -#endif default: /* any whitespace or semicolon trailing after a s/// is ok */ if (strchr("; \t\v\n\r", substr[idx])) @@ -534,59 +533,53 @@ static void print_subst_w_backrefs(const char *line, const char *replace, regmat static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) { + char *hackline = (char *)line; int altered = 0; + regmatch_t *regmatch = NULL; - /* we only substitute if the substitution 'search' expression matches */ - if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { - regmatch_t *regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1)); - int i; - char *ptr = (char *)line; - - while (*ptr) { - /* if we can match the search string... */ - if (regexec(sed_cmd->sub_match, ptr, sed_cmd->num_backrefs+1, regmatch, 0) == 0) { - /* print everything before the match, */ - for (i = 0; i < regmatch[0].rm_so; i++) { -#ifdef BB_FEATURE_SED_PATTERN_SPACE - if(!be_quiet || (sed_cmd->sub_flags & SUB_P)) -#endif - fputc(ptr[i], stdout); - } + /* if the user specified that they didn't want anything printed (i.e. a -n + * flag and no 'p' flag after the s///), then there's really no point doing + * anything here. */ + if (be_quiet && !sed_cmd->sub_p) + return 0; - /* then print the substitution in its place */ -#ifdef BB_FEATURE_SED_PATTERN_SPACE - if(!be_quiet || (sed_cmd->sub_flags & SUB_P)) -#endif - print_subst_w_backrefs(ptr, sed_cmd->replace, regmatch); + /* we only proceed if the substitution 'search' expression matches */ + if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == REG_NOMATCH) + return 0; - /* then advance past the match */ - ptr += regmatch[0].rm_eo; + /* whaddaya know, it matched. get the number of back references */ + regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1)); - /* and flag that something has changed */ - altered++; + /* and now, as long as we've got a line to try matching and if we can match + * the search string, we make substitutions */ + while (*hackline && (regexec(sed_cmd->sub_match, hackline, + sed_cmd->num_backrefs+1, regmatch, 0) == 0) ) { + int i; - /* if we're not doing this globally... */ - if (!sed_cmd->sub_flags & SUB_G) - break; - } - /* if we COULD NOT match the search string (meaning we've gone past - * all previous instances), get out */ - else - break; - } + /* print everything before the match */ + for (i = 0; i < regmatch[0].rm_so; i++) + fputc(hackline[i], stdout); - /* is there anything left to print? */ -#ifdef BB_FEATURE_SED_PATTERN_SPACE - if (*ptr && (!be_quiet || sed_cmds->sub_flags & SUB_P)) -#else - if (*ptr) -#endif - fputs(ptr, stdout); + /* then print the substitution string */ + print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch); + + /* advance past the match */ + hackline += regmatch[0].rm_eo; + /* flag that something has changed */ + altered++; - /* cleanup */ - free(regmatch); + /* if we're not doing this globally, get out now */ + if (!sed_cmd->sub_g) + break; } + /* if there's anything left of the line, print it */ + if (*hackline) + fputs(hackline, stdout); + + /* cleanup */ + free(regmatch); + return altered; } @@ -605,7 +598,32 @@ static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) break; case 's': - altered = do_subst_command(sed_cmd, line); + + /* + * Some special cases for 's' printing to make it compliant with + * GNU sed printing behavior (aka "The -n | s///p Matrix"): + * + * -n ONLY = never print anything regardless of any successful + * substitution + * + * s///p ONLY = always print successful substitutions, even if + * the line is going to be printed anyway (line will be printed + * twice). + * + * -n AND s///p = print ONLY a successful substitution ONE TIME; + * no other lines are printed - this is the reason why the 'p' + * flag exists in the first place. + */ + + /* we print the line once, unless we were told to be quiet */ + if (!be_quiet) + altered = do_subst_command(sed_cmd, line); + + /* we also print the line if we were given the 'p' flag + * (this is quite possibly the second printing) */ + if (sed_cmd->sub_p) + altered = do_subst_command(sed_cmd, line); + break; case 'a': @@ -662,7 +680,8 @@ static void process_file(FILE *file) /* are we acting on a range of line numbers? */ else if (sed_cmds[i].beg_line > 0 && sed_cmds[i].end_line != 0) { - if (linenum >= sed_cmds[i].beg_line && (sed_cmds[i].end_line == -1 || linenum <= sed_cmds[i].end_line)) + if (linenum >= sed_cmds[i].beg_line && + (sed_cmds[i].end_line == -1 || linenum <= sed_cmds[i].end_line)) line_altered += do_sed_command(&sed_cmds[i], line); } @@ -678,14 +697,10 @@ static void process_file(FILE *file) } - /* we will print the line unless we were told to be quiet or if - * the line was altered (via a 'd'elete or 's'ubstitution) */ -#ifndef BB_FEATURE_SED_PATTERN_SPACE - if (!be_quiet &&!line_altered) -#else - /* we where specificly requested to print the output */ - if ((!be_quiet || (sed_cmds[i].sub_flags & SUB_P)) && !line_altered) -#endif + /* we will print the line unless we were told to be quiet or if the + * line was altered (via a 'd'elete or 's'ubstitution), in which case + * the altered line was already printed */ + if (!be_quiet && !line_altered) fputs(line, stdout); free(line); |