-
Notifications
You must be signed in to change notification settings - Fork 134
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add the m option (from grep). #93
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
|
||
/* Short options. */ | ||
static char const short_options[] = | ||
"cd:e:hiklnqsvwyBD:E:HI:MS:V0123456789-:"; | ||
"cd:e:hiklm:nqsvwyBD:E:HI:MS:V0123456789-:"; | ||
|
||
static int show_help; | ||
char *program_name; | ||
|
@@ -72,6 +72,7 @@ static struct option const long_options[] = | |
{"insert-cost", required_argument, NULL, 'I'}, | ||
{"invert-match", no_argument, NULL, 'v'}, | ||
{"line-number", no_argument, NULL, 'n'}, | ||
{"max-count", required_argument, NULL, 'm'}, | ||
{"literal", no_argument, NULL, 'k'}, | ||
{"max-errors", required_argument, NULL, 'E'}, | ||
{"no-filename", no_argument, NULL, 'h'}, | ||
|
@@ -139,6 +140,7 @@ Output control:\n\ | |
-H, --with-filename print the filename for each match\n\ | ||
-l, --files-with-matches only print FILE names containing matches\n\ | ||
-M, --delimiter-after print record delimiter after record if -d is used\n\ | ||
-m, --max-count=NUM stop after NUM selected lines\n\ | ||
-n, --record-number print record number with output\n\ | ||
--line-number same as -n\n\ | ||
-q, --quiet, --silent suppress all normal output\n\ | ||
|
@@ -185,6 +187,8 @@ static int print_filename; /* Output filename. */ | |
static int print_recnum; /* Output record number. */ | ||
static int print_cost; /* Output match cost. */ | ||
static int count_matches; /* Count matching records. */ | ||
static int max_count; /* Max number of selected | ||
lines from an input file. */ | ||
static int list_files; /* List matching files. */ | ||
static int color_option; /* Highlight matches. */ | ||
static int print_position; /* Show start and end offsets for matches. */ | ||
|
@@ -449,6 +453,11 @@ tre_agrep_handle_file(const char *filename) | |
printf("%.*s", record_len, record); | ||
} | ||
} | ||
/* exit after max_count match(es) */ | ||
if((max_count > 0)&&(count > (max_count-1))) { | ||
if(count_matches) break;/* to get the result */ | ||
else exit(0);/* to leave the stream "as is" */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exiting here produces the wrong result when multiple inputs are provided and at least one of them has There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I added the ugly There were some trouble either when piping or using multiple files, but I can't recall what problems exactly, sorry m(_ _)m |
||
} | ||
} | ||
} | ||
|
||
|
@@ -498,6 +507,7 @@ main(int argc, char **argv) | |
be_silent = 0; | ||
tre_regaparams_default(&match_params); | ||
match_params.max_cost = 0; | ||
max_count = -1; | ||
|
||
/* Parse command line options. */ | ||
while (1) | ||
|
@@ -542,6 +552,10 @@ main(int argc, char **argv) | |
/* Only print files that contain matches. */ | ||
list_files = 1; | ||
break; | ||
case 'm': | ||
/* stop after n selected lines */ | ||
max_count = atoi(optarg); | ||
break; | ||
case 'n': | ||
/* Print record number of matching record. */ | ||
print_recnum = 1; | ||
|
@@ -652,6 +666,10 @@ Copyright (c) 2001-2009 Ville Laurikari <vl@iki.fi>.\n")); | |
if (show_help) | ||
tre_agrep_usage(0); | ||
|
||
/* Fail without reading if count number is 0 */ | ||
if (max_count == 0) | ||
return EXIT_FAILURE; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Then would |
||
|
||
if (color_option) | ||
{ | ||
char *user_highlight = getenv("GREP_COLOR"); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This entire hunk is plagiarized from the GNU grep manual and cannot be used. Not just because of the blatant copyright violation, but also because it does not accurately describe the added functionality.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, it was included by accident I think, sorry for that m(_ _)m
I had decided to not include any change to the manual page, but it must have slip through somehow.
Also, I don't get why there are so many changes...
Sorry again, as I said below, you can safely ignore this PR.