+static int parse_subst_cmd(struct sed_cmd *sed_cmd, const char *substr)
+{
+ int oldidx, cflags = REG_NEWLINE;
+ char *match;
+ int idx = 0;
+ int j;
+
+ /*
+ * the string that gets passed to this function should look like this:
+ * s/match/replace/gIp
+ * || | |||
+ * mandatory optional
+ *
+ * (all three of the '/' slashes are mandatory)
+ */
+
+ /* verify that the 's' is followed by something. That something
+ * (typically a 'slash') is now our regexp delimiter... */
+ if (!substr[++idx])
+ error_msg_and_die("bad format in substitution expression");
+ else
+ sed_cmd->delimiter=substr[idx];
+
+ /* save the match string */
+ oldidx = idx+1;
+ idx = index_of_next_unescaped_regexp_delim(sed_cmd, substr, ++idx);
+ if (idx == -1)
+ error_msg_and_die("bad format in substitution expression");
+ match = strdup_substr(substr, oldidx, idx);
+
+ /* determine the number of back references in the match string */
+ /* Note: we compute this here rather than in the do_subst_command()
+ * function to save processor time, at the expense of a little more memory
+ * (4 bits) per sed_cmd */
+
+ /* sed_cmd->num_backrefs = 0; */ /* XXX: not needed? --apparently not */
+ for (j = 0; match[j]; j++) {
+ /* GNU/POSIX sed does not save more than nine backrefs */
+ if (match[j] == '\\' && match[j+1] == '(' && sed_cmd->num_backrefs <= 9)
+ sed_cmd->num_backrefs++;
+ }
+
+ /* save the replacement string */
+ oldidx = idx+1;
+ idx = index_of_next_unescaped_regexp_delim(sed_cmd, substr, ++idx);
+ if (idx == -1)
+ error_msg_and_die("bad format in substitution expression");
+ sed_cmd->replace = strdup_substr(substr, oldidx, idx);
+
+ /* process the flags */
+ while (substr[++idx]) {
+ switch (substr[idx]) {
+ case 'g':
+ sed_cmd->sub_g = 1;
+ break;
+ case 'I':
+ cflags |= REG_ICASE;
+ break;
+ case 'p':
+ sed_cmd->sub_p = 1;
+ break;
+ default:
+ /* any whitespace or semicolon trailing after a s/// is ok */
+ if (strchr("; \t\v\n\r", substr[idx]))
+ goto out;
+ /* else */
+ error_msg_and_die("bad option in substitution expression");
+ }
+ }
+
+out:
+ /* compile the match string into a regex */
+ sed_cmd->sub_match = (regex_t *)xmalloc(sizeof(regex_t));
+ xregcomp(sed_cmd->sub_match, match, cflags);
+ free(match);
+
+ return idx;
+}
+
+static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr)
+{
+ int idx = 0;
+ int slashes_eaten = 0;
+ char *ptr; /* shorthand */
+
+ /*
+ * the string that gets passed to this function should look like this:
+ *
+ * need one of these
+ * |
+ * | this backslash (immediately following the edit command) is mandatory
+ * | |
+ * [aic]\
+ * TEXT1\
+ * TEXT2\
+ * TEXTN
+ *
+ * as soon as we hit a TEXT line that has no trailing '\', we're done.
+ * this means a command like:
+ *
+ * i\
+ * INSERTME
+ *
+ * is a-ok.
+ *
+ */
+
+ if (editstr[1] != '\\' && (editstr[2] != '\n' || editstr[2] != '\r'))
+ error_msg_and_die("bad format in edit expression");
+
+ /* store the edit line text */
+ /* make editline big enough to accomodate the extra '\n' we will tack on
+ * to the end */
+ sed_cmd->editline = xmalloc(strlen(&editstr[3]) + 2);
+ strcpy(sed_cmd->editline, &editstr[3]);
+ ptr = sed_cmd->editline;
+
+ /* now we need to go through * and: s/\\[\r\n]$/\n/g on the edit line */
+ while (ptr[idx]) {
+ while (ptr[idx] != '\\' && (ptr[idx+1] != '\n' || ptr[idx+1] != '\r')) {
+ idx++;
+ if (!ptr[idx]) {
+ goto out;
+ }
+ }
+ /* move the newline over the '\' before it (effectively eats the '\') */
+ memmove(&ptr[idx], &ptr[idx+1], strlen(&ptr[idx+1]));
+ ptr[strlen(ptr)-1] = 0;
+ slashes_eaten++;
+ /* substitue \r for \n if needed */
+ if (ptr[idx] == '\r')
+ ptr[idx] = '\n';
+ }
+
+out:
+ ptr[idx] = '\n';
+ ptr[idx+1] = 0;
+
+ /* this accounts for discrepancies between the modified string and the
+ * original string passed in to this function */
+ idx += slashes_eaten;
+
+ /* this accounts for the fact that A) we started at index 3, not at index
+ * 0 and B) that we added an extra '\n' at the end (if you think the next
+ * line should read 'idx += 4' remember, arrays are zero-based) */
+
+ idx += 3;
+
+ return idx;
+}
+
+static char *parse_cmd_str(struct sed_cmd *sed_cmd, const char *cmdstr)