From 5ed78adca590ba9991c55b71f0a9b7f1b31e7934 Mon Sep 17 00:00:00 2001 From: Matt Kraai Date: Thu, 3 Jan 2002 21:12:34 +0000 Subject: [PATCH] * editors/sed.c (parse_edit_cmd): Rewrite. * testsuite/sed/sed-splits-edit-commands-on-command-line: New. --- editors/sed.c | 47 ++++++---------------- .../sed/sed-splits-edit-commands-on-command-line | 9 +++++ 2 files changed, 22 insertions(+), 34 deletions(-) create mode 100644 testsuite/sed/sed-splits-edit-commands-on-command-line 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 + * 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; } diff --git a/testsuite/sed/sed-splits-edit-commands-on-command-line b/testsuite/sed/sed-splits-edit-commands-on-command-line new file mode 100644 index 0000000..6421fa5 --- /dev/null +++ b/testsuite/sed/sed-splits-edit-commands-on-command-line @@ -0,0 +1,9 @@ +echo 2 | busybox sed -e 'i\ +1 +a\ +3' > output +cmp output - <