Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #23 from ggreer/named_pipes

Support for named pipes
  • Loading branch information...
commit 1ad63c0bb0bf51fca2b878beb166818515d70ed3 2 parents 91dc40b + b29602d
@ggreer authored
View
2  src/main.c
@@ -64,7 +64,7 @@ int main(int argc, char **argv) {
}
}
- if (opts.search_stdin) {
+ if (opts.search_stream) {
search_stdin(re, re_extra);
}
else {
View
6 src/options.c
@@ -132,7 +132,7 @@ void parse_options(int argc, char **argv, char **query, char **path) {
/* stdin isn't a tty. something's probably being piped to ag */
if (!isatty(fileno(stdin))) {
- opts.search_stdin = 1;
+ opts.search_stream = 1;
}
/* If we're not outputting to a terminal. change output to:
@@ -260,7 +260,7 @@ void parse_options(int argc, char **argv, char **query, char **path) {
opts.color = 0;
opts.print_break = 1;
group = 1;
- opts.search_stdin = 0;
+ opts.search_stream = 0;
}
if (opts.print_heading == 0 || opts.print_break == 0) {
@@ -278,7 +278,7 @@ void parse_options(int argc, char **argv, char **query, char **path) {
skip_group:
- if (opts.search_stdin) {
+ if (opts.search_stream) {
opts.print_break = 0;
opts.print_heading = 0;
opts.print_line_numbers = 0;
View
2  src/options.h
@@ -44,7 +44,7 @@ typedef struct {
int recurse_dirs;
int search_all_files;
int search_binary_files;
- int search_stdin; /* true if tail -F blah | ag */
+ int search_stream; /* true if tail -F blah | ag */
int search_unrestricted;
int stats;
char *query;
View
2  src/print.c
@@ -112,7 +112,7 @@ void print_file_matches(const char* path, const char* buf, const int buf_len, co
if (buf[i] == '\n' || i == buf_len) {
if (lines_since_last_match == 0) {
- if (opts.print_heading == 0 && !opts.search_stdin) {
+ if (opts.print_heading == 0 && !opts.search_stream) {
print_path(path, ':');
}
View
47 src/search.c
@@ -90,14 +90,23 @@ void search_buf(const pcre *re, const pcre_extra *re_extra,
}
}
-/* TODO: this will only match single lines. multi-line regexes silently don't match */
void search_stdin(const pcre *re, const pcre_extra *re_extra) {
+ search_stream(re, re_extra, stdin, "");
+}
+
+/* TODO: this will only match single lines. multi-line regexes silently don't match */
+void search_stream(const pcre *re, const pcre_extra *re_extra, FILE *stream, const char *path) {
char *line = NULL;
ssize_t line_length = 0;
size_t line_cap = 0;
- while ((line_length = getline(&line, &line_cap, stdin)) > 0) {
- search_buf(re, re_extra, line, line_length, "");
+ opts.print_break = 0;
+ opts.print_heading = 0;
+ opts.print_line_numbers = 0;
+ opts.search_stream = 1;
+
+ while ((line_length = getline(&line, &line_cap, stream)) > 0) {
+ search_buf(re, re_extra, line, line_length, path);
}
free(line);
@@ -109,6 +118,7 @@ void search_file(const pcre *re, const pcre_extra *re_extra, const char *file_fu
char *buf = NULL;
struct stat statbuf;
int rv = 0;
+ FILE *pipe = NULL;
fd = open(file_full_path, O_RDONLY);
if (fd < 0) {
@@ -122,20 +132,33 @@ void search_file(const pcre *re, const pcre_extra *re_extra, const char *file_fu
goto cleanup;
}
- f_len = statbuf.st_size;
-
- if (f_len == 0) {
- log_debug("File %s is empty, skipping.", file_full_path);
+ if ((statbuf.st_mode & S_IFMT) == 0) {
+ log_err("%s is not a file. Mode %u. Skipping...", file_full_path, statbuf.st_mode);
goto cleanup;
}
- buf = mmap(0, f_len, PROT_READ, MAP_SHARED, fd, 0);
- if (buf == MAP_FAILED) {
- log_err("File %s failed to load: %s.", file_full_path, strerror(errno));
- goto cleanup;
+ if (statbuf.st_mode & S_IFIFO) {
+ log_debug("%s is a named pipe. stream searching", file_full_path);
+ pipe = fdopen(fd, "r");
+ search_stream(re, re_extra, pipe, file_full_path);
+ fclose(pipe);
}
+ else {
+ f_len = statbuf.st_size;
- search_buf(re, re_extra, buf, (int)f_len, file_full_path);
+ if (f_len == 0) {
+ log_debug("File %s is empty, skipping.", file_full_path);
+ goto cleanup;
+ }
+
+ buf = mmap(0, f_len, PROT_READ, MAP_SHARED, fd, 0);
+ if (buf == MAP_FAILED) {
+ log_err("File %s failed to load: %s.", file_full_path, strerror(errno));
+ goto cleanup;
+ }
+
+ search_buf(re, re_extra, buf, (int)f_len, file_full_path);
+ }
cleanup:
if (fd != -1) {
View
1  src/search.h
@@ -29,6 +29,7 @@ void search_buf(const pcre *re, const pcre_extra *re_extra,
const char *buf, const int buf_len,
const char *dir_full_path);
void search_stdin(const pcre *re, const pcre_extra *re_extra);
+void search_stream(const pcre *re, const pcre_extra *re_extra, FILE *stream, const char *path);
void search_file(const pcre *re, const pcre_extra *re_extra, const char *file_full_path);
void search_dir(const pcre *re, const pcre_extra *re_extra, const char* path, const int depth);
Please sign in to comment.
Something went wrong with that request. Please try again.