From 4f7fe77d07294586687382597d04fb46a24092e5 Mon Sep 17 00:00:00 2001 From: Mark Whitley Date: Thu, 13 Jul 2000 20:01:58 +0000 Subject: (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. --- editors/sed.c | 80 ++++++++++++++++++++++++++++++++--------------------------- sed.c | 80 ++++++++++++++++++++++++++++++++--------------------------- 2 files changed, 86 insertions(+), 74 deletions(-) diff --git a/editors/sed.c b/editors/sed.c index 8ae34f8c7..22d642ee9 100644 --- a/editors/sed.c +++ b/editors/sed.c @@ -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; } diff --git a/sed.c b/sed.c index 8ae34f8c7..22d642ee9 100644 --- a/sed.c +++ b/sed.c @@ -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; } -- cgit v1.2.3