[BusyBox] [PATCH] keep initial leading whitespace in sed 'a' cmd (1.0.0-pre8)

Rob Landley rob at landley.net
Thu Apr 1 07:44:16 UTC 2004


On Wednesday 31 March 2004 05:42, Erik Andersen wrote:
> On Tue Mar 30, 2004 at 11:26:51AM -0800, Junio Hamano wrote:
> > The sed command in busybox 1.0.0-pre8 loses leading whitespace
> > in 'a' command ('i' and 'c' commands are also affected).  A
> > patch to fix this is attached at the end of this message.
>
> Applied thanks,
>
>  -Erik

Actually, you just broke:

sed -i "/^config setup/a fred" ipsec.conf

Which works in gnu sed.  (And is _supposed_ to strip all the whitespace before 
"fred".)  For that matter, you broke:

sed -i -e "/^config setup/a \\" -e "   fred" ipsec.conf

I.E. there can legally be spaces between the a and the backslash at the end of 
the line.

And strangely enough, gnu sed accepts the following syntax as well:

busybox/busybox sed -i "/^config setup/a \\  fred" ipsec.conf

Which is a way of having the significant whitespace at the start of the line, 
all on one line.  (But notice that the whitespace BEFORE the slash is still 
stripped, as is the slash itself.  And notice that the naieve placement of 
"\n" there doesn't work, it puts an n at the start of the appended line.  The 
double slashing is for shell escapes because you could escape the quote, you 
see.  It's turned into a single backslash.  But \n there is _not_ turned into 
a newline by the shell.  And if you don't have to stop for a few seconds 
after that and blink, your brain has more caffiene in it than mine does right 
now...)

Eric: could you cc: me on sed changes in the future?  I just read the bug 
report five minutes ago.  (I'm hip-deep implementing online registration for 
www.linucon.org, which is why it took me a day or so to catch up with the 
busybox list enough to see this.  But I DO follow this, and I specifically 
care about sed working right...)

Also, I object to the comment "should never happen".  Bad input is not a 
"should never happen" situation.  Bad input happens all the time.  "Should 
never happen" means (to me) this test should never trigger.  And in this 
case, it's not bad input.  (The stylistic nit-pick that almost none of the 
rest of the code has comments at the end but instead the comments are on the 
line before is, well, just a nit-pick... :)

Here's a patch that seems to fix it for me.  Please let me know what I just 
broke, or if you can think of a way to do this that bloats the code less.  
(I'd check in my sedtests.py command, except it's written in python and I 
need to add about another 30 tests to it.  But I'll attach the file so you 
can see what's it currently tests...)

Rob

--- busybox/editors/sed.old	2004-03-31 05:42:40.000000000 -0600
+++ busybox/editors/sed.c	2004-04-01 01:35:31.006582864 -0600
@@ -6,6 +6,7 @@
  * Copyright (C) 1999,2000,2001 by Mark Whitley <markw at codepoet.org>
  * Copyright (C) 2002  Matt Kraai
  * Copyright (C) 2003 by Glenn McGrath <bug1 at optushome.com.au>
+ * Copyright (C) 2003,2004 by Rob Landley <rob at landley.net>
  *
  * 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
@@ -410,9 +411,13 @@
 		if ((sed_cmd->end_line || sed_cmd->end_match) && sed_cmd->cmd != 'c')
 			bb_error_msg_and_die
 				("only a beginning address can be specified for edit commands");
-		if (*cmdstr != '\n') /* should not happen */
-		  bb_error_msg_and_die("A/I/C backslash not followed by NL?");
-		cmdstr++; /* skip over the NL following the backslash */
+		for(;;) {
+			if(*cmdstr=='\n' || *cmdstr=='\\') {
+				cmdstr++;
+				break;
+			} else if(isspace(*cmdstr)) cmdstr++;
+			else break;
+		}
 		sed_cmd->string = bb_xstrdup(cmdstr);
 		parse_escapes(sed_cmd->string,sed_cmd->string,strlen(cmdstr),0,0);
 		cmdstr += strlen(cmdstr);
-------------- next part --------------
A non-text attachment was scrubbed...
Name: sedtests.py
Type: text/x-python
Size: 4701 bytes
Desc: not available
Url : http://lists.busybox.net/pipermail/busybox/attachments/20040401/2a77d2a4/attachment.py 


More information about the busybox mailing list