[patch] size reduction of grep

Bernhard Fischer rep.nop at aon.at
Sun Sep 18 22:22:28 UTC 2005


Hi,

$ size findutils/grep.o*
   text	   data	    bss	    dec	    hex	filename
   1545	      0	     40	   1585	    631	findutils/grep.o
   1697	      0	     40	   1737	    6c9	findutils/grep.o.oorig


tested with make check.
PS: Rob, i'm aware that you don't like such defines, but the patch would
have been considerably bigger otherwise. Is that acceptable?

Bernhard
-------------- next part --------------
diff -X excl -rduNp busybox.oorig/findutils/grep.c busybox/findutils/grep.c
--- busybox.oorig/findutils/grep.c	2005-09-15 22:00:44.000000000 +0200
+++ busybox/findutils/grep.c	2005-09-19 00:07:22.000000000 +0200
@@ -1,23 +1,11 @@
+/* vi: set sw=4 ts=4: */
 /*
  * Mini grep implementation for busybox using libc regex.
  *
  * Copyright (C) 1999,2000,2001 by Lineo, inc. and Mark Whitley
  * Copyright (C) 1999,2000,2001 by Mark Whitley <markw at codepoet.org>
  *
- * 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
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under the GPL v2, see the file LICENSE in this tarball.
  */
 /*
  * Apr 2004 by Vladimir Oleynik <dzo at simtreas.ru> -
@@ -34,30 +22,31 @@
 
 
 /* options */
-#define GREP_OPTS "lnqvscFiHhe:f:L"
+static unsigned long opt;
+#define GREP_OPTS "lnqvscFiHhe:f:Lr"
 #define GREP_OPT_l (1<<0)
-static char print_files_with_matches;
+#define print_files_with_matches (opt & GREP_OPT_l)
 #define GREP_OPT_n (1<<1)
-static char print_line_num;
+#define print_line_num (opt & GREP_OPT_n)
 #define GREP_OPT_q (1<<2)
-static char be_quiet;
+#define be_quiet (opt & GREP_OPT_q)
 #define GREP_OPT_v (1<<3)
 typedef char invert_search_t;
 static invert_search_t invert_search;
 #define GREP_OPT_s (1<<4)
-static char suppress_err_msgs;
+#define suppress_err_msgs (opt & GREP_OPT_s)
 #define GREP_OPT_c (1<<5)
-static char print_match_counts;
+#define print_match_counts (opt & GREP_OPT_c)
 #define GREP_OPT_F (1<<6)
-static char fgrep_flag;
+#define fgrep_flag (opt & GREP_OPT_F)
 #define GREP_OPT_i (1<<7)
 #define GREP_OPT_H (1<<8)
 #define GREP_OPT_h (1<<9)
 #define GREP_OPT_e (1<<10)
 #define GREP_OPT_f (1<<11)
 #define GREP_OPT_L (1<<12)
-static char print_files_without_matches;
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#define print_files_without_matches ((opt & GREP_OPT_L) != 0)
+#if ENABLE_FEATURE_GREP_CONTEXT
 #define GREP_OPT_CONTEXT "A:B:C"
 #define GREP_OPT_A (1<<13)
 #define GREP_OPT_B (1<<14)
@@ -65,9 +54,12 @@ static char print_files_without_matches;
 #define GREP_OPT_E (1<<16)
 #else
 #define GREP_OPT_CONTEXT ""
+#define GREP_OPT_A (0)
+#define GREP_OPT_B (0)
+#define GREP_OPT_C (0)
 #define GREP_OPT_E (1<<13)
 #endif
-#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
+#if ENABLE_FEATURE_GREP_EGREP_ALIAS
 # define OPT_EGREP "E"
 #else
 # define OPT_EGREP ""
@@ -76,12 +68,12 @@ static char print_files_without_matches;
 static int reflags;
 static int print_filename;
 
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#if ENABLE_FEATURE_GREP_CONTEXT
 static int lines_before;
 static int lines_after;
 static char **before_buf;
 static int last_line_printed;
-#endif /* CONFIG_FEATURE_GREP_CONTEXT */
+#endif /* ENABLE_FEATURE_GREP_CONTEXT */
 
 /* globals used internally */
 static llist_t *pattern_head;   /* growable list of patterns to match */
@@ -90,7 +82,7 @@ static char *cur_file;          /* the c
 
 static void print_line(const char *line, int linenum, char decoration)
 {
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#if ENABLE_FEATURE_GREP_CONTEXT
 	/* possibly print the little '--' separator */
 	if ((lines_before || lines_after) && last_line_printed &&
 			last_line_printed < linenum - 1) {
@@ -112,11 +104,11 @@ static int grep_file(FILE *file)
 	invert_search_t ret;
 	int linenum = 0;
 	int nmatches = 0;
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#if ENABLE_FEATURE_GREP_CONTEXT
 	int print_n_lines_after = 0;
 	int curpos = 0; /* track where we are in the circular 'before' buffer */
 	int idx = 0; /* used for iteration through the circular buffer */
-#endif /* CONFIG_FEATURE_GREP_CONTEXT */
+#endif /* ENABLE_FEATURE_GREP_CONTEXT */
 
 	while ((line = bb_get_chomped_line_from_file(file)) != NULL) {
 		llist_t *pattern_ptr = pattern_head;
@@ -159,7 +151,7 @@ static int grep_file(FILE *file)
 
 				/* print the matched line */
 				if (print_match_counts == 0) {
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#if ENABLE_FEATURE_GREP_CONTEXT
 					int prevpos = (curpos == 0) ? lines_before - 1 : curpos - 1;
 
 					/* if we were told to print 'before' lines and there is at least
@@ -188,11 +180,11 @@ static int grep_file(FILE *file)
 
 					/* make a note that we need to print 'after' lines */
 					print_n_lines_after = lines_after;
-#endif /* CONFIG_FEATURE_GREP_CONTEXT */
+#endif
 					print_line(line, linenum, ':');
 				}
 			}
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#if ENABLE_FEATURE_GREP_CONTEXT
 			else { /* no match */
 				/* Add the line to the circular 'before' buffer */
 				if(lines_before) {
@@ -207,7 +199,7 @@ static int grep_file(FILE *file)
 				print_line(line, linenum, '-');
 				print_n_lines_after--;
 			}
-#endif /* CONFIG_FEATURE_GREP_CONTEXT */
+#endif /* ENABLE_FEATURE_GREP_CONTEXT */
 		free(line);
 	}
 
@@ -258,13 +250,11 @@ extern int grep_main(int argc, char **ar
 {
 	FILE *file;
 	int matched;
-	unsigned long opt;
 	llist_t *fopt = NULL;
 
 	/* do normal option parsing */
-#ifdef CONFIG_FEATURE_GREP_CONTEXT
+#if ENABLE_FEATURE_GREP_CONTEXT
   {
-	char *junk;
 	char *slines_after;
 	char *slines_before;
 	char *Copt;
@@ -285,15 +275,11 @@ extern int grep_main(int argc, char **ar
 		opt |= GREP_OPT_A|GREP_OPT_B;   /* set for parse now */
 	}
 	if(opt & GREP_OPT_A) {
-		lines_after = strtoul(slines_after, &junk, 10);
-				if(*junk != '\0')
-					bb_error_msg_and_die("invalid context length argument");
+		lines_after = bb_xgetularg10(slines_after);
 	}
 	if(opt & GREP_OPT_B) {
-		lines_before = strtoul(slines_before, &junk, 10);
-				if(*junk != '\0')
-					bb_error_msg_and_die("invalid context length argument");
-		}
+		lines_before = bb_xgetularg10(slines_before);
+	}
 	/* sanity checks after parse may be invalid numbers ;-) */
 	if ((opt & (GREP_OPT_c|GREP_OPT_q|GREP_OPT_l|GREP_OPT_L))) {
 		opt &= ~GREP_OPT_n;
@@ -307,16 +293,9 @@ extern int grep_main(int argc, char **ar
 	bb_opt_complementally = "H-h:e*:f*:c-n:q-n:l-n";
 	opt = bb_getopt_ulflags(argc, argv, GREP_OPTS OPT_EGREP,
 		&pattern_head, &fopt);
-
 #endif
-	print_files_with_matches = opt & GREP_OPT_l;
-	print_files_without_matches = (opt & GREP_OPT_L) != 0;
-	print_line_num = opt & GREP_OPT_n;
-	be_quiet = opt & GREP_OPT_q;
 	invert_search = (opt & GREP_OPT_v) != 0;        /* 0 | 1 */
-	suppress_err_msgs = opt & GREP_OPT_s;
-	print_match_counts = opt & GREP_OPT_c;
-	fgrep_flag = opt & GREP_OPT_F;
+
 	if(opt & GREP_OPT_H)
 		print_filename++;
 	if(opt & GREP_OPT_h)
@@ -324,16 +303,13 @@ extern int grep_main(int argc, char **ar
 	if(opt & GREP_OPT_f)
 		load_regexes_from_file(fopt);
 
-#ifdef CONFIG_FEATURE_GREP_FGREP_ALIAS
-	if(bb_applet_name[0] == 'f')
-		fgrep_flag = 1;
-#endif
+	if(ENABLE_FEATURE_GREP_FGREP_ALIAS && bb_applet_name[0] == 'f')
+		opt |= GREP_OPT_F;
 
-#ifdef CONFIG_FEATURE_GREP_EGREP_ALIAS
-	if(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E))
+	if(ENABLE_FEATURE_GREP_EGREP_ALIAS &&
+			(bb_applet_name[0] == 'e' || (opt & GREP_OPT_E)))
 		reflags = REG_EXTENDED | REG_NOSUB;
 	else
-#endif
 		reflags = REG_NOSUB;
 
 	if(opt & GREP_OPT_i)
@@ -386,15 +362,14 @@ extern int grep_main(int argc, char **ar
 			}
 		}
 
-#ifdef CONFIG_FEATURE_CLEAN_UP
 	/* destroy all the elments in the pattern list */
+	if (ENABLE_FEATURE_CLEAN_UP)
 	while (pattern_head) {
 		llist_t *pattern_head_ptr = pattern_head;
 
 		pattern_head = pattern_head->link;
 		free(pattern_head_ptr);
 	}
-#endif
 
 	return !matched; /* invert return value 0 = success, 1 = failed */
 }


More information about the busybox mailing list