summaryrefslogtreecommitdiff
path: root/editors
diff options
context:
space:
mode:
authorMatt Kraai2002-01-03 21:12:34 +0000
committerMatt Kraai2002-01-03 21:12:34 +0000
commit5ed78adca590ba9991c55b71f0a9b7f1b31e7934 (patch)
tree12dc218905c2a50d32897b6a5478ad049cfe53f2 /editors
parentd2995632570567fc3a77587175d32fc97e4d552a (diff)
downloadbusybox-5ed78adca590ba9991c55b71f0a9b7f1b31e7934.zip
busybox-5ed78adca590ba9991c55b71f0a9b7f1b31e7934.tar.gz
* editors/sed.c (parse_edit_cmd): Rewrite.
* testsuite/sed/sed-splits-edit-commands-on-command-line: New.
Diffstat (limited to 'editors')
-rw-r--r--editors/sed.c47
1 files changed, 13 insertions, 34 deletions
diff --git a/editors/sed.c b/editors/sed.c
index 1c026d3..e766b3c 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -3,6 +3,7 @@
*
* Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
* Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
+ * Copyright (C) 2002 Matt Kraai
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -296,9 +297,7 @@ static void move_back(char *str, int offset)
static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr)
{
- int idx = 0;
- int slashes_eaten = 0;
- char *ptr; /* shorthand */
+ int i, j;
/*
* the string that gets passed to this function should look like this:
@@ -326,44 +325,24 @@ static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr)
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 '\') */
- move_back(&ptr[idx], 1);
- slashes_eaten++;
- /* substitue \r for \n if needed */
- if (ptr[idx] == '\r')
- ptr[idx] = '\n';
+ for (i = 3, j = 0; editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL;
+ i++, j++) {
+ if (editstr[i] == '\\' && strchr("\n\r", editstr[i+1]) != NULL) {
+ sed_cmd->editline[j] = '\n';
+ i++;
+ } else
+ sed_cmd->editline[j] = editstr[i];
}
-out:
/* figure out if we need to add a newline */
- if (ptr[idx-1] != '\n') {
- ptr[idx] = '\n';
- idx++;
- }
+ if (sed_cmd->editline[j-1] != '\n')
+ sed_cmd->editline[j++] = '\n';
/* terminate string */
- ptr[idx]= 0;
-
- /* this accounts for discrepancies between the modified string and the
- * original string passed in to this function */
-
- /* adjust for opening 2 chars [aic]\ */
+ sed_cmd->editline[j] = '\0';
- return idx + slashes_eaten + 2;
+ return i;
}