Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/external/bsd/tre Add some patches which were in an older tre...
details: https://anonhg.NetBSD.org/src/rev/e474651d4131
branches: trunk
changeset: 345828:e474651d4131
user: agc <agc%NetBSD.org@localhost>
date: Fri Jun 10 05:11:17 2016 +0000
description:
Add some patches which were in an older tree, from January 29 2015:
+ add a -r argument to agrep(1) and document it. This has the same
effect as the -r flag to grep(1) - perform a recursive search
through sub-directories.
+ if a match is found in a "binary" file, then do the same as grep(1)
and just print the message that a match was found in the file, without
attempting to print the match in full.
diffstat:
external/bsd/tre/bin/agrep.1 | 7 ++-
external/bsd/tre/dist/src/agrep.c | 97 +++++++++++++++++++++++++++++++++++++-
2 files changed, 98 insertions(+), 6 deletions(-)
diffs (189 lines):
diff -r b0de38aea758 -r e474651d4131 external/bsd/tre/bin/agrep.1
--- a/external/bsd/tre/bin/agrep.1 Fri Jun 10 03:42:13 2016 +0000
+++ b/external/bsd/tre/bin/agrep.1 Fri Jun 10 05:11:17 2016 +0000
@@ -1,5 +1,5 @@
-.\" $NetBSD: agrep.1,v 1.1 2011/11/05 22:39:13 christos Exp $
-.Dd November 21, 2004
+.\" $NetBSD: agrep.1,v 1.2 2016/06/10 05:11:17 agc Exp $
+.Dd January 29, 2015
.Dt AGREP 1
.Os
.Sh NAME
@@ -97,6 +97,9 @@
Select non-matching records instead of matching records.
.It Fl V , Fl Fl version
Print version information and exit.
+.It Fl r , Fl Fl recursive
+If a directory is given as one of the command line arguments,
+look in every directory entry in the subdirectory, recursively.
.It Fl y , Fl Fl nothing
Does nothing.
This options exists only for compatibility with the
diff -r b0de38aea758 -r e474651d4131 external/bsd/tre/dist/src/agrep.c
--- a/external/bsd/tre/dist/src/agrep.c Fri Jun 10 03:42:13 2016 +0000
+++ b/external/bsd/tre/dist/src/agrep.c Fri Jun 10 05:11:17 2016 +0000
@@ -42,7 +42,7 @@
/* Short options. */
static char const short_options[] =
-"cd:e:hiklnqsvwyBD:E:HI:MS:V0123456789-:";
+"cd:e:hiklnqrsvwyBD:E:HI:MS:V0123456789-:";
static int show_help;
char *program_name;
@@ -76,6 +76,7 @@
{"nothing", no_argument, NULL, 'y'},
{"quiet", no_argument, NULL, 'q'},
{"record-number", no_argument, NULL, 'n'},
+ {"recursive", no_argument, NULL, 'r'},
{"regexp", required_argument, NULL, 'e'},
{"show-cost", no_argument, NULL, 's'},
{"show-position", no_argument, NULL, SHOW_POSITION_OPTION},
@@ -126,6 +127,7 @@
-d, --delimiter=PATTERN set the record delimiter regular expression\n\
-v, --invert-match select non-matching records\n\
-V, --version print version information and exit\n\
+ -r, --recursive also search in any subdirectories\n\
-y, --nothing does nothing (for compatibility with the non-free\n\
agrep program)\n\
--help display this help and exit\n\
@@ -186,6 +188,7 @@
static int list_files; /* List matching files. */
static int color_option; /* Highlight matches. */
static int print_position; /* Show start and end offsets for matches. */
+static int recursive; /* Search in subdirectories too */
static int best_match; /* Output only best matches. */
static int best_cost; /* Best match cost found so far. */
@@ -307,6 +310,82 @@
}
}
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <sys/mman.h>
+
+#include <dirent.h>
+
+static int
+isbinaryfile(const char *filename)
+{
+ struct stat st;
+ size_t size;
+ size_t i;
+ char *mapped;
+ FILE *fp;
+ int isbin;
+
+ if ((fp = fopen(filename, "r")) == NULL) {
+ return 1;
+ }
+ fstat(fileno(fp), &st);
+ isbin = 0;
+ if ((st.st_mode & S_IFMT) != S_IFREG) {
+ isbin = 1;
+ } else {
+ size = (size_t)st.st_size;
+ mapped = mmap(NULL, size, PROT_READ, MAP_SHARED, fileno(fp), 0);
+ for (i = 0 ; !isbin && i < size ; i++) {
+ if (mapped[i] == 0x0) {
+ isbin = 1;
+ }
+ }
+ munmap(mapped, size);
+ }
+ fclose(fp);
+ return isbin;
+}
+
+static int tre_agrep_handle_file(const char */*filename*/);
+
+static int
+tre_agrep_handle_dirent(const char *ent)
+{
+ struct dirent storage;
+ struct dirent *dp;
+ struct stat st;
+ char path[8192];
+ DIR *dirp;
+ int ret;
+ int ok;
+
+ if (ent == NULL || strcmp(ent, "-") == 0) {
+ return tre_agrep_handle_file(ent);
+ }
+ if (lstat(ent, &st) < 0) {
+ return tre_agrep_handle_file(ent);
+ }
+ if ((st.st_mode & S_IFMT) == S_IFDIR && recursive) {
+ if ((dirp = opendir(ent)) == NULL) {
+ fprintf(stderr, "can't open directory '%s'\n", ent);
+ return 0;
+ }
+ for (ret = 0 ; readdir_r(dirp, &storage, &dp) == 0 && dp != NULL ; ) {
+ if (strcmp(dp->d_name, ".") == 0 ||
+ strcmp(dp->d_name, "..") == 0) {
+ continue;
+ }
+ snprintf(path, sizeof(path), "%s/%s", ent, dp->d_name);
+ if ((ok = tre_agrep_handle_dirent(path)) != 0) {
+ ret = ok;
+ }
+ }
+ closedir(dirp);
+ return ret;
+ }
+ return tre_agrep_handle_file(ent);
+}
static int
tre_agrep_handle_file(const char *filename)
@@ -405,6 +484,11 @@
printf("%s\n", filename);
break;
}
+ else if (!count_matches && isbinaryfile(filename))
+ {
+ if (print_filename)
+ printf("Binary file %s matches\n", filename);
+ }
else if (!count_matches)
{
if (print_filename)
@@ -547,6 +631,11 @@
case 'q':
be_silent = 1;
break;
+ case 'r':
+ /* also search in sub-directories */
+ recursive = 1;
+ print_filename = 1;
+ break;
case 's':
/* Print match cost of matching record. */
print_cost = 1;
@@ -797,7 +886,7 @@
/* Scan all files once without outputting anything, searching
for the best matches. */
while (optind < argc)
- tre_agrep_handle_file(argv[optind++]);
+ tre_agrep_handle_dirent(argv[optind++]);
/* If there were no matches, bail out now. */
if (best_cost == INT_MAX)
@@ -810,13 +899,13 @@
best_match = 2;
optind = first_ind;
while (optind < argc)
- tre_agrep_handle_file(argv[optind++]);
+ tre_agrep_handle_dirent(argv[optind++]);
}
else
{
/* Normal mode. */
while (optind < argc)
- tre_agrep_handle_file(argv[optind++]);
+ tre_agrep_handle_dirent(argv[optind++]);
}
return have_matches == 0;
Home |
Main Index |
Thread Index |
Old Index