diff options
author | Mark Whitley <markw@lineo.com> | 2000-07-14 00:01:58 +0400 |
---|---|---|
committer | Mark Whitley <markw@lineo.com> | 2000-07-14 00:01:58 +0400 |
commit | 4f7fe77d07294586687382597d04fb46a24092e5 (patch) | |
tree | 596c63765bf27c51c8a4cec40849fd5769202213 /sed.c | |
parent | 06f3529ada8c564673eb2d551b9979b3d65a7475 (diff) |
(Something I should have done in the previous checkin...) Also broke out
substitution command execution from do_sed_command() and put it in it's own
do_subst_command() function.
Diffstat (limited to 'sed.c')
-rw-r--r-- | sed.c | 80 |
1 files changed, 43 insertions, 37 deletions
@@ -340,6 +340,47 @@ static void load_cmd_file(char *filename) } } +static int do_subst_command(const struct sed_cmd *sed_cmd, const char *line) +{ + int altered = 0; + + /* we only substitute if the substitution 'search' expression matches */ + if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { + regmatch_t regmatch; + int i; + char *ptr = (char *)line; + + while (*ptr) { + /* if we can match the search string... */ + if (regexec(sed_cmd->sub_match, ptr, 1, ®match, 0) == 0) { + /* print everything before the match, */ + for (i = 0; i < regmatch.rm_so; i++) + fputc(ptr[i], stdout); + /* then print the substitution in its place */ + fputs(sed_cmd->replace, stdout); + /* then advance past the match */ + ptr += regmatch.rm_eo; + /* and let the calling function know that something + * has been changed */ + altered++; + + /* if we're not doing this globally... */ + if (!sed_cmd->sub_g) + break; + } + /* if we COULD NOT match the search string (meaning we've gone past + * all previous instances), get out */ + else + break; + } + + /* is there anything left to print? */ + if (*ptr) + fputs(ptr, stdout); + } + + return altered; +} static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) { @@ -355,43 +396,8 @@ static int do_sed_command(const struct sed_cmd *sed_cmd, const char *line) altered++; break; - case 's': /* oo, a fun one :-) */ - - /* we only substitute if the substitution 'search' expression matches */ - if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == 0) { - regmatch_t regmatch; - int i; - char *ptr = (char *)line; - - while (*ptr) { - /* if we can match the search string... */ - if (regexec(sed_cmd->sub_match, ptr, 1, ®match, 0) == 0) { - /* print everything before the match, */ - for (i = 0; i < regmatch.rm_so; i++) - fputc(ptr[i], stdout); - /* then print the substitution in its place */ - fputs(sed_cmd->replace, stdout); - /* then advance past the match */ - ptr += regmatch.rm_eo; - /* and let the calling function know that something - * has been changed */ - altered++; - - /* if we're not doing this globally... */ - if (!sed_cmd->sub_g) - break; - } - /* if we COULD NOT match the search string (meaning we've gone past - * all previous instances), get out */ - else - break; - } - - /* is there anything left to print? */ - if (*ptr) - fputs(ptr, stdout); - } - + case 's': + altered = do_subst_command(sed_cmd, line); break; } |