aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Kraai <kraai@debian.org>2002-01-03 21:12:34 +0000
committerMatt Kraai <kraai@debian.org>2002-01-03 21:12:34 +0000
commit5ed78adca590ba9991c55b71f0a9b7f1b31e7934 (patch)
tree12dc218905c2a50d32897b6a5478ad049cfe53f2
parentd2995632570567fc3a77587175d32fc97e4d552a (diff)
downloadbusybox-w32-5ed78adca590ba9991c55b71f0a9b7f1b31e7934.tar.gz
busybox-w32-5ed78adca590ba9991c55b71f0a9b7f1b31e7934.tar.bz2
busybox-w32-5ed78adca590ba9991c55b71f0a9b7f1b31e7934.zip
* editors/sed.c (parse_edit_cmd): Rewrite.
* testsuite/sed/sed-splits-edit-commands-on-command-line: New.
-rw-r--r--editors/sed.c47
-rw-r--r--testsuite/sed/sed-splits-edit-commands-on-command-line9
2 files changed, 22 insertions, 34 deletions
diff --git a/editors/sed.c b/editors/sed.c
index 1c026d30b..e766b3c2f 100644
--- a/editors/sed.c
+++ b/editors/sed.c
@@ -3,6 +3,7 @@
3 * 3 *
4 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley 4 * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org> 5 * Copyright (C) 1999,2000,2001 by Mark Whitley <markw@codepoet.org>
6 * Copyright (C) 2002 Matt Kraai
6 * 7 *
7 * This program is free software; you can redistribute it and/or modify 8 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by 9 * 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)
296 297
297static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr) 298static int parse_edit_cmd(struct sed_cmd *sed_cmd, const char *editstr)
298{ 299{
299 int idx = 0; 300 int i, j;
300 int slashes_eaten = 0;
301 char *ptr; /* shorthand */
302 301
303 /* 302 /*
304 * the string that gets passed to this function should look like this: 303 * 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)
326 error_msg_and_die("bad format in edit expression"); 325 error_msg_and_die("bad format in edit expression");
327 326
328 /* store the edit line text */ 327 /* store the edit line text */
329 /* make editline big enough to accomodate the extra '\n' we will tack on
330 * to the end */
331 sed_cmd->editline = xmalloc(strlen(&editstr[3]) + 2); 328 sed_cmd->editline = xmalloc(strlen(&editstr[3]) + 2);
332 strcpy(sed_cmd->editline, &editstr[3]); 329 for (i = 3, j = 0; editstr[i] != '\0' && strchr("\r\n", editstr[i]) == NULL;
333 ptr = sed_cmd->editline; 330 i++, j++) {
334 331 if (editstr[i] == '\\' && strchr("\n\r", editstr[i+1]) != NULL) {
335 /* now we need to go through * and: s/\\[\r\n]$/\n/g on the edit line */ 332 sed_cmd->editline[j] = '\n';
336 while (ptr[idx]) { 333 i++;
337 while (ptr[idx] != '\\' || (ptr[idx+1] != '\n' && ptr[idx+1] != '\r')) { 334 } else
338 idx++; 335 sed_cmd->editline[j] = editstr[i];
339 if (!ptr[idx]) {
340 goto out;
341 }
342 }
343 /* move the newline over the '\' before it (effectively eats the '\') */
344 move_back(&ptr[idx], 1);
345 slashes_eaten++;
346 /* substitue \r for \n if needed */
347 if (ptr[idx] == '\r')
348 ptr[idx] = '\n';
349 } 336 }
350 337
351out:
352 /* figure out if we need to add a newline */ 338 /* figure out if we need to add a newline */
353 if (ptr[idx-1] != '\n') { 339 if (sed_cmd->editline[j-1] != '\n')
354 ptr[idx] = '\n'; 340 sed_cmd->editline[j++] = '\n';
355 idx++;
356 }
357 341
358 /* terminate string */ 342 /* terminate string */
359 ptr[idx]= 0; 343 sed_cmd->editline[j] = '\0';
360
361 /* this accounts for discrepancies between the modified string and the
362 * original string passed in to this function */
363
364 /* adjust for opening 2 chars [aic]\ */
365 344
366 return idx + slashes_eaten + 2; 345 return i;
367} 346}
368 347
369 348
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 000000000..6421fa552
--- /dev/null
+++ b/testsuite/sed/sed-splits-edit-commands-on-command-line
@@ -0,0 +1,9 @@
1echo 2 | busybox sed -e 'i\
21
3a\
43' > output
5cmp output - <<EOF
61
72
83
9EOF