+
+ 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;