Added default * **/* glob handling when no files provided

This commit is contained in:
Bruce Hill 2020-09-13 15:55:09 -07:00
parent 12d748c76c
commit ba5b5196ce
2 changed files with 47 additions and 34 deletions

74
bpeg.c
View File

@ -32,6 +32,7 @@
* ; <name> = <pat> <name> is defined to be <pat> * ; <name> = <pat> <name> is defined to be <pat>
*/ */
#include <fcntl.h> #include <fcntl.h>
#include <glob.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@ -66,6 +67,30 @@ static char *getflag(const char *flag, char *argv[], int *i)
} }
return NULL; return NULL;
} }
static int run_match(grammar_t *g, const char *filename, vm_op_t *pattern, int verbose)
{
char *input;
if (filename == NULL || streq(filename, "-")) {
input = readfile(STDIN_FILENO);
} else {
int fd = open(filename, O_RDONLY);
check(fd >= 0, "Couldn't open file: %s", filename);
input = readfile(fd);
}
match_t *m = match(g, input, pattern);
if (m != NULL && m->end > m->start + 1) {
if (isatty(STDOUT_FILENO)) printf("\033[1;4;33m%s\033[0m\n", filename);
else printf("%s\n", filename);
print_match(m, isatty(STDOUT_FILENO) ? "\033[0m" : NULL, verbose);
freefile(input);
return 0;
} else {
freefile(input);
return 1;
}
}
#define FLAG(f) (flag=getflag((f), argv, &i)) #define FLAG(f) (flag=getflag((f), argv, &i))
int main(int argc, char *argv[]) int main(int argc, char *argv[])
@ -157,41 +182,28 @@ int main(int argc, char *argv[])
print_pattern(pattern); print_pattern(pattern);
} }
char *input; int ret = 0;
if (i == argc) { if (i < argc) {
// Force stdin if no files given // Files pass in as command line args:
input = readfile(STDIN_FILENO); for (int nfiles = 0; i < argc; nfiles++, i++) {
goto run_match; ret |= run_match(g, argv[i], pattern, verbose);
}
for (int nfiles = 0; i < argc; nfiles++, i++) {
if (argv[i] == NULL || streq(argv[i], "-")) {
input = readfile(STDIN_FILENO);
} else {
int fd = open(argv[i], O_RDONLY);
check(fd >= 0, "Couldn't open file: %s", argv[i]);
input = readfile(fd);
if (nfiles > 0) printf("\n");
printf("\033[1;4;33m%s\033[0m\n", argv[i]);
} }
} else if (isatty(STDIN_FILENO)) {
run_match:; // No files, no piped in input, so use * **/*:
// Ensure string has a null byte to the left: glob_t globbuf;
char *lpadded = calloc(sizeof(char), strlen(input)+2); glob("*", 0, NULL, &globbuf);
stpcpy(&lpadded[1], input); glob("**/*", GLOB_APPEND, NULL, &globbuf);
input = &lpadded[1]; for (size_t i = 0; i < globbuf.gl_pathc; i++) {
ret |= run_match(g, globbuf.gl_pathv[i], pattern, verbose);
match_t *m = match(g, input, pattern);
if (m == NULL) {
printf("No match\n");
return 1;
} else {
print_match(m, isatty(STDOUT_FILENO) ? "\033[0m" : NULL, verbose);
//printf("\033[0;2m%s\n", m->end);
} }
freefile(input); globfree(&globbuf);
} else {
// Piped in input:
ret |= run_match(g, NULL, pattern, verbose);
} }
return 0;
return ret;
} }
// vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1 // vim: ts=4 sw=0 et cino=L2,l1,(0,W4,m1

View File

@ -1,10 +1,11 @@
# Meta-rules for acting on everything # Meta-rules for acting on everything
pattern = !(/); # Not defined by default pattern = !(/); # Not defined by default
replacement = {!(/)=>}; # Not defined by default replacement = {!(/)=>}; # Not defined by default
replace-all = *&&@replacement &&$$; replace-all = +&&@replacement &&$$;
find-all = *(matching-line / {&&(\n/$$)=>}); find-all = {&&>matching-line=>} +(matching-line/non-matching-line);
only-matches = *{&&@pattern=>'@1\n'}; only-matches = +{&&@pattern=>'@1\n'};
matching-line = +&@pattern *. $ ?\n; matching-line = +&@pattern *. $ ?\n;
non-matching-line = {&&(\n/$$)=>};
# Helper definitions (commonly used) # Helper definitions (commonly used)
crlf = \r\n; crlf = \r\n;