(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.
This commit is contained in:
parent
06f3529ada
commit
4f7fe77d07
@ -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;
|
||||
}
|
||||
|
||||
|
80
sed.c
80
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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user