@matheusbsbs / matheus-bagliano / commits / f462ebb48b
Fork 0

builtin-grep: allow -<n> and -[ABC]<n> notation for context lines.

Signed-off-by: Junio C Hamano <junkio@cox.net>

Junio C Hamano committed May 2, 2006 at 15:17 UTC f462ebb48bf9126335671e878336e3faf3914802
1 file changed +22 -6
builtin-grep.c
+22 -6
@@ -402,18 +402,34 @@ int cmd_grep(int argc, const char **argv, char **envp)
402 opt.name_only = 1;
403 continue;
404 }
405 - if (!strcmp("-A", arg) ||
406 - !strcmp("-B", arg) ||
407 - !strcmp("-C", arg)) {
405 + if (!strncmp("-A", arg, 2) ||
406 + !strncmp("-B", arg, 2) ||
407 + !strncmp("-C", arg, 2) ||
408 + (arg[0] == '-' && '1' <= arg[1] && arg[1] <= '9')) {
409 unsigned num;
409 - if (argc <= 1 ||
410 - sscanf(*++argv, "%u", &num) != 1)
410 + const char *scan;
411 + switch (arg[1]) {
412 + case 'A': case 'B': case 'C':
413 + if (!arg[2]) {
414 + if (argc <= 1)
415 + usage(builtin_grep_usage);
416 + scan = *++argv;
417 + argc--;
418 + }
419 + else
420 + scan = arg + 2;
421 + break;
422 + default:
423 + scan = arg + 1;
424 + break;
425 + }
426 + if (sscanf(scan, "%u", &num) != 1)
427 usage(builtin_grep_usage);
412 - argc--;
428 switch (arg[1]) {
429 case 'A':
430 opt.post_context = num;
431 break;
432 + default:
433 case 'C':
434 opt.post_context = num;
435 case 'B':