[PATCH v2] Improve support for long options to grep

Walter Lozano walter.lozano at collabora.com
Wed Jan 26 15:59:45 UTC 2022


Hi David,

Thank you for your feedback, let me comment.

On 1/26/22 11:25, David Laight wrote:
> From: Walter Lozano
>> Sent: 26 January 2022 14:14
>>
>> In order to improve compatibility with GNU grep improve support for long
>> options to busybox grep.
> 
> You've broken backwards compatibility by removing --color from
> default builds.

You are right about this, however, I was not sure about how to proceed, 
since previously the code seems to silently make use of long options 
even if LONG_OPTS was not enabled, which seems to be odd.

I don't have problems in keeping the old behavior, but it will be odd to 
check for LONG_OPTS for adding the new options, but keep the --color 
support if LONG_OPTS are not enabled.

 From my understanding either, they should be decoupled as in this patch 
or force LONG_OPTS to be enabled if FEATURE_GREP_CONTEXT is enabled. 
Since FEATURE_GREP_CONTEXT does not depend on LONG_OPTS I thought it is 
not the proper solution.

> There is also unnecessary duplication of source code.
> 
> Just use a #define and C string concatenation to add the extra
> long option strings.

Yes, I totally agree you. However I used this approach since a previous 
patch I submitted was rewritten in this way [1] when applying to master.

Regards,

Walter

[1] 
https://git.busybox.net/busybox/commit/?id=6dd6a6c42d1465d8cca2539476f6bffd5e1353dd

>> ---
>>
>> 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
> 
> -
> Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
> Registration No: 1397386 (Wales)
> 

-- 
Walter Lozano
Collabora Ltd.


More information about the busybox mailing list