[BusyBox] Patch for head,tail,sed

Simon Krahnke simon at lisa.de
Fri May 31 10:18:03 UTC 2002


On Wed, May 29, 2002 at 08:33:02PM +0200, Robert Griebl wrote:
> Am Mittwoch, 29. Mai 2002 19:11 schrieben Sie:
>> Hi, I'm not subscribed to this list.

Still not. :-)

>> I wrote a patch for busybox 0.60.2 for our company's (www.lisa.de)
>> private use. It's more or less dirty and adds the shortcut option
>> of "-12" instedad of "-n 12" style to head and tail. To sed it adds
>> the '!'-inversion of addresses.
> 
> The -<num> style flags for head and tail are already in CVS HEAD (a somewhat 
> nicer solution though =) ) -- see busybox mailing list.

Oh, found it meanwhile. It would be helpful, if "A new snapshot of the
source is made daily and is available as a GNU gzipped tarball" would
actually be "made daily", and "a GNU gzipped tarball".

Anyway, I reworked the sed patch to match the current CVS version, are we
at Lisa the only ones missing the inversion feature? Patch is attached.

mfg, simon
-------------- next part --------------
diff -Naur busybox/editors/sed.c busybox-lisa/editors/sed.c
--- busybox/editors/sed.c	Fri May 31 15:59:05 2002
+++ busybox-lisa/editors/sed.c	Fri May 31 15:57:51 2002
@@ -96,6 +96,9 @@
 
 	/* the command */
 	char cmd; /* p,d,s (add more at your leisure :-) */
+
+        /* inversion flag */
+        int invert;         /* the '!' after the address */ 
 };
 
 /* globals */
@@ -405,6 +408,17 @@
 	while (isspace(cmdstr[idx]))
 		idx++;
 
+	/* there my be the inversion flag between part2 and part3 */
+	sed_cmd->invert = 0;
+	if (cmdstr[idx] == '!') {
+	        sed_cmd->invert = 1;
+                idx++;
+
+		/* skip whitespace before the command */
+		while (isspace(cmdstr[idx]))
+		        idx++;
+	}
+
 	/* last part (mandatory) will be a command */
 	if (cmdstr[idx] == '\0')
 		error_msg_and_die("missing command");
@@ -642,12 +656,12 @@
 		/* for every line, go through all the commands */
 		for (i = 0; i < ncmds; i++) {
 			struct sed_cmd *sed_cmd = &sed_cmds[i];
-
+			int deleted = 0;
 
 			/*
 			 * entry point into sedding...
 			 */
-			if (
+			int matched = (
 					/* no range necessary */
 					(sed_cmd->beg_line == 0 && sed_cmd->end_line == 0 &&
 					 sed_cmd->beg_match == NULL &&
@@ -658,8 +672,9 @@
 					(sed_cmd->beg_match && (regexec(sed_cmd->beg_match, line, 0, NULL, 0) == 0)) ||
 					/* we are currently within the beginning & ending address range */
 					still_in_range
-			   ) {
-				int deleted = 0;
+			   );
+
+			if (sed_cmd->invert ^ matched) {
 
 				/*
 				 * actual sedding
@@ -746,13 +761,15 @@
 								  /* else if we couldn't open the output file,
 								   * no biggie, just don't print anything */
 								  altered++;
-							  }
+						  }
 							  break;
 				}
+			}
 
-				/*
-				 * exit point from sedding...
-				 */
+			/*
+			 * exit point from sedding...
+			 */
+			if (matched) {
 				if (
 					/* this is a single-address command or... */
 					(sed_cmd->end_line == 0 && sed_cmd->end_match == NULL) || (
@@ -774,10 +791,10 @@
 				else {
 					still_in_range = 1;
 				}
-
-				if (deleted)
-					break;
 			}
+
+			if (deleted)
+			        break;
 		}
 
 		/* we will print the line unless we were told to be quiet or if the


More information about the busybox mailing list