}
}
-static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch)
+static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch, int matches)
{
int i;
tmpstr[1] = 0;
backref = atoi(tmpstr);
/* print out the text held in regmatch[backref] */
- for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
- fputc(line[j], stdout);
+ if (backref <= matches && regmatch[backref].rm_so != -1)
+ for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
+ fputc(line[j], stdout);
}
/* if we find a backslash escaped character, print the character */
fputc(hackline[i], stdout);
/* then print the substitution string */
- print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch);
+ print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch,
+ sed_cmd->num_backrefs);
/* advance past the match */
hackline += regmatch[0].rm_eo;
}
}
-static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch)
+static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch, int matches)
{
int i;
tmpstr[1] = 0;
backref = atoi(tmpstr);
/* print out the text held in regmatch[backref] */
- for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
- fputc(line[j], stdout);
+ if (backref <= matches && regmatch[backref].rm_so != -1)
+ for (j = regmatch[backref].rm_so; j < regmatch[backref].rm_eo; j++)
+ fputc(line[j], stdout);
}
/* if we find a backslash escaped character, print the character */
fputc(hackline[i], stdout);
/* then print the substitution string */
- print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch);
+ print_subst_w_backrefs(hackline, sed_cmd->replace, regmatch,
+ sed_cmd->num_backrefs);
/* advance past the match */
hackline += regmatch[0].rm_eo;