[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [BusyBox] [patch] Simplify the heck out of the sed newline hack.
From:       Rob Landley <rob () landley ! net>
Date:       2003-09-25 9:55:29
[Download RAW message or body]

Okay, instead of turning the length two string "\n" into a length three string 
"\\n", why not just turn the \n into an ascii 10 in the search pattern, and 
rip out the conversion/reversion stuff on the strings we're testing against?

The following patch does that.  It fixes some bugs I encountered in the 
binutils build from the newline hackage basically screwing things up (the 
test case I posted earlier tonight), AND makes the code smaller and simpler.  
I don't think it needs to be conditional anymore (it isn't in gnu), but I 
haven't touched menuconfig.

Treat with extreme skepticism: it worked for me configuring binutils (leaving 
me with the "s/blah/blah/;$s/blah/blah/" bug that's still blocking, but as 
far as I know that's the ONLY remaining bug, and the newline matches seem to 
be working), but I'm not entirely sure what the code I'm ripping out was 
trying to accomplish, so...

Rob



["patch-newline" (text/x-diff)]

--- busybox/editors/sed.c	2003-09-16 00:25:40.000000000 -0500
+++ busybox-new/editors/sed.c	2003-09-25 04:20:03.410479016 -0500
@@ -601,27 +601,30 @@
 
 static void add_cmd_str(const char *cmdstr)
 {
-	char *cmdstr_expanded = strdup(cmdstr);
-	char *cmdstr_ptr;
-
-#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
-	cmdstr_ptr = cmdstr_expanded;
-	/* HACK: convert "\n" to match tranlated '\n' string */
-	while ((cmdstr_ptr = strstr(cmdstr_ptr, "\\n")) != NULL) {
-		int length = strlen(cmdstr) + 2;
-		cmdstr_expanded = realloc(cmdstr_expanded, length);
-		cmdstr_ptr = strstr(cmdstr_expanded, "\\n");
-		memmove(cmdstr_ptr + 1, cmdstr_ptr, strlen(cmdstr_ptr) + 1);
-		cmdstr_ptr[0] = '\\';
-		cmdstr_ptr += 3;
+	char *cmdstr_new = malloc(strlen(cmdstr)+1);
+	char *cmdstr_ptr = cmdstr_new;
+	const char *cmdstr_from;
+
+	/* convert "\n" escape sequences into newline characters. */
+	cmdstr_ptr = cmdstr_new;
+	cmdstr_from = cmdstr;
+	while(*cmdstr_from) {
+		if(*cmdstr_ptr=='\\') {
+                	if(*(cmdstr_ptr+1)=='n') {
+				*(cmdstr_ptr++)='\n';
+				cmdstr_from+=2;
+			} else *(cmdstr_ptr++)=*(cmdstr_from++);
+		}
+		*(cmdstr_ptr++)=*(cmdstr_from++);
 	}
-#endif
-	cmdstr_ptr = cmdstr_expanded;
+	*cmdstr_ptr=0;
+
+	cmdstr_ptr = cmdstr_new;
 	do {
 		cmdstr_ptr = add_cmd(cmdstr_ptr);
 	} while (cmdstr_ptr && strlen(cmdstr_ptr));
 
-	free(cmdstr_expanded);
+	free(cmdstr_new);
 }
 
 
@@ -907,35 +910,8 @@
 					 *    flag exists in the first place.
 					 */
 
-#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
-					/* HACK: escape newlines twice so regex can match them */
-				{
-					int offset = 0;
-					char *tmp = strchr(pattern_space + offset, '\n');
-					while ((tmp = strchr(pattern_space + offset, '\n')) != NULL) {
-						offset = tmp - pattern_space;
-						pattern_space =	xrealloc(pattern_space,	strlen(pattern_space) + 2);
-						tmp = pattern_space + offset;
-						memmove(tmp + 1, tmp, strlen(tmp) + 1);
-						tmp[0] = '\\';
-						tmp[1] = 'n';
-						offset += 2;
-					}
-				}
-#endif
 					/* we print the pattern_space once, unless we were told to be quiet */
 					substituted |= do_subst_command(sed_cmd, &pattern_space);
-#ifdef CONFIG_FEATURE_SED_EMBEDED_NEWLINE
-					/* undo HACK: escape newlines twice so regex can match them */
-					{
-						char *tmp = pattern_space;
-
-						while ((tmp = strstr(tmp, "\\n")) != NULL) {
-							memmove(tmp, tmp + 1, strlen(tmp + 1) + 1);
-							tmp[0] = '\n';
-						}
-					}
-#endif
 					if (!be_quiet && substituted && ((sed_cmd->next == NULL)
 							|| (sed_cmd->next->cmd != 's'))) {
 						force_print = 1;


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic