+static void add_cmd_str(const char *cmdstr)
+{
+ char *mystr = (char *)cmdstr;
+
+ do {
+
+ /* trim leading whitespace and semicolons */
+ memmove(mystr, &mystr[strspn(mystr, "; \n\r\t\v")], strlen(mystr));
+ /* if we ate the whole thing, that means there was just trailing
+ * whitespace or a final / no-op semicolon. either way, get out */
+ if (strlen(mystr) == 0)
+ return;
+ /* if this is a comment, jump past it and keep going */
+ if (mystr[0] == '#') {
+ mystr = strpbrk(mystr, ";\n\r");
+ continue;
+ }
+ /* grow the array */
+ sed_cmds = xrealloc(sed_cmds, sizeof(struct sed_cmd) * (++ncmds));
+ /* zero new element */
+ memset(&sed_cmds[ncmds-1], 0, sizeof(struct sed_cmd));
+ /* load command string into new array element, get remainder */
+ mystr = parse_cmd_str(&sed_cmds[ncmds-1], mystr);
+
+ } while (mystr && strlen(mystr));
+}
+
+
+static void load_cmd_file(char *filename)
+{
+ FILE *cmdfile;
+ char *line;
+ char *nextline;
+
+ cmdfile = xfopen(filename, "r");
+
+ while ((line = get_line_from_file(cmdfile)) != NULL) {
+ /* if a line ends with '\' it needs the next line appended to it */
+ while (line[strlen(line)-2] == '\\' &&
+ (nextline = get_line_from_file(cmdfile)) != NULL) {
+ line = xrealloc(line, strlen(line) + strlen(nextline) + 1);
+ strcat(line, nextline);
+ free(nextline);
+ }
+ /* eat trailing newline (if any) --if I don't do this, edit commands
+ * (aic) will print an extra newline */
+ chomp(line);
+ add_cmd_str(line);
+ free(line);
+ }
+}
+
+static void print_subst_w_backrefs(const char *line, const char *replace, regmatch_t *regmatch)
+{
+ int i;
+
+ /* go through the replacement string */
+ for (i = 0; replace[i]; i++) {
+ /* if we find a backreference (\1, \2, etc.) print the backref'ed * text */
+ if (replace[i] == '\\' && isdigit(replace[i+1])) {
+ int j;
+ char tmpstr[2];
+ int backref;
+ ++i; /* i now indexes the backref number, instead of the leading slash */
+ tmpstr[0] = replace[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 we find a backslash escaped character, print the character */
+ else if (replace[i] == '\\') {
+ ++i;
+ fputc(replace[i], stdout);
+ }
+
+ /* if we find an unescaped '&' print out the whole matched text.
+ * fortunately, regmatch[0] contains the indicies to the whole matched
+ * expression (kinda seems like it was designed for just such a
+ * purpose...) */
+ else if (replace[i] == '&' && replace[i-1] != '\\') {
+ int j;
+ for (j = regmatch[0].rm_so; j < regmatch[0].rm_eo; j++)
+ fputc(line[j], stdout);
+ }
+ /* nothing special, just print this char of the replacement string to stdout */
+ else
+ fputc(replace[i], stdout);
+ }
+}
+
+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 proceed if the substitution 'search' expression matches */
+ if (regexec(sed_cmd->sub_match, line, 0, NULL, 0) == REG_NOMATCH)
+ return 0;
+
+ /* whaddaya know, it matched. get the number of back references */
+ regmatch = xmalloc(sizeof(regmatch_t) * (sed_cmd->num_backrefs+1));
+
+ /* 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;
+
+ /* print everything before the match */
+ for (i = 0; i < regmatch[0].rm_so; i++)
+ fputc(hackline[i], 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++;
+
+ /* if we're not doing this globally, get out now */
+ if (!sed_cmd->sub_g)
+ break;