[PATCH v3] Improve support for long options to grep

Walter Lozano walter.lozano at collabora.com
Fri Jun 24 20:01:00 UTC 2022


In order to improve compatibility with GNU grep improve support for long
options to busybox grep.

Signed-off-by: Walter Lozano <walter.lozano at collabora.com>

---

Changes in v3:
- Rebase on top of latest master

Changes in v2:
- Decouple FEATURE_GREP_CONTEXT from LONG_OPTS.

 findutils/grep.c | 43 +++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)

diff --git a/findutils/grep.c b/findutils/grep.c
index 0b72812f1..4ae070a01 100644
--- a/findutils/grep.c
+++ b/findutils/grep.c
@@ -720,17 +720,56 @@ int grep_main(int argc UNUSED_PARAM, char **argv)
 
 	/* do normal option parsing */
 #if ENABLE_FEATURE_GREP_CONTEXT
+#if !ENABLE_LONG_OPTS
 	/* -H unsets -h; -C unsets -A,-B */
-	opts = getopt32long(argv, "^"
+	opts = getopt32(argv, "^"
 		OPTSTR_GREP
 			"\0"
 			"H-h:C-AB",
-		"color\0" Optional_argument "\xff",
 		&pattern_head, &fopt, &max_matches,
 		&lines_after, &lines_before, &Copt
 		, NULL
 	);
+#else
+	static const char grep_longopts[] ALIGN1 =
+		"with-filename\0"        No_argument         "H"
+		"no-filename\0"          No_argument         "h"
+		"line-number\0"          No_argument         "n"
+		"files-without-match\0"  No_argument         "L"
+		"files-with-matches\0"   No_argument         "l"
+		"count\0"                No_argument         "c"
+		"only-matching\0"        No_argument         "o"
+		"quiet\0"                No_argument         "q"
+		"silent\0"               No_argument         "q"
+		"invert-match\0"         No_argument         "v"
+		"no-messages\0"          No_argument         "s"
+		"recursive\0"            No_argument         "r"
+		"ignore-case\0"          No_argument         "i"
+		"word-regexp\0"          No_argument         "w"
+		"line-regexp\0"          No_argument         "x"
+		"fixed-strings\0"        No_argument         "F"
+		"extended-regexp\0"      No_argument         "E"
+		"null-data\0"            No_argument         "z"
+		"max-count\0"            Required_argument   "m"
+		"after-context\0"        Required_argument   "A"
+		"before-context\0"       Required_argument   "B"
+		"context\0"              Required_argument   "C"
+		"regexp\0"               Required_argument   "e"
+		"file\0"                 Required_argument   "f"
+		"color\0"                Optional_argument   "\xff"
+		;
 
+	/* -H unsets -h; -C unsets -A,-B */
+	opts = getopt32long(argv, "^"
+		OPTSTR_GREP
+			"\0"
+			"H-h:C-AB",
+		grep_longopts,
+		&pattern_head, &fopt, &max_matches,
+		&lines_after, &lines_before, &Copt
+		, NULL
+	);
+#endif
 	if (opts & OPT_C) {
 		/* -C unsets prev -A and -B, but following -A or -B
 		 * may override it */
-- 
2.25.1



More information about the busybox mailing list