bsdgrep(1): Do some less dirty things with return types
Neither procfile nor grep_tree return anything meaningful to their callers. None of the callers actually care about how many lines were matched in all of the files they processed; it's all about "did anything match?" This is generally just a light refactoring to remind me of what actually matters as I'm rewriting these bits to care less about 'stuff'.
This commit is contained in:
parent
470f228f62
commit
cbfff13fa2
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=334806
@ -339,6 +339,7 @@ main(int argc, char *argv[])
|
|||||||
long long l;
|
long long l;
|
||||||
unsigned int aargc, eargc, i;
|
unsigned int aargc, eargc, i;
|
||||||
int c, lastc, needpattern, newarg, prevoptind;
|
int c, lastc, needpattern, newarg, prevoptind;
|
||||||
|
bool matched;
|
||||||
|
|
||||||
setlocale(LC_ALL, "");
|
setlocale(LC_ALL, "");
|
||||||
|
|
||||||
@ -725,15 +726,16 @@ main(int argc, char *argv[])
|
|||||||
exit(!procfile("-"));
|
exit(!procfile("-"));
|
||||||
|
|
||||||
if (dirbehave == DIR_RECURSE)
|
if (dirbehave == DIR_RECURSE)
|
||||||
c = grep_tree(aargv);
|
matched = grep_tree(aargv);
|
||||||
else
|
else
|
||||||
for (c = 0; aargc--; ++aargv) {
|
for (matched = false; aargc--; ++aargv) {
|
||||||
if ((finclude || fexclude) && !file_matching(*aargv))
|
if ((finclude || fexclude) && !file_matching(*aargv))
|
||||||
continue;
|
continue;
|
||||||
c+= procfile(*aargv);
|
if (procfile(*aargv))
|
||||||
|
matched = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Find out the correct return value according to the
|
/* Find out the correct return value according to the
|
||||||
results and the command line option. */
|
results and the command line option. */
|
||||||
exit(c ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
|
exit(matched ? (file_err ? (qflag ? 0 : 2) : 0) : (file_err ? 2 : 1));
|
||||||
}
|
}
|
||||||
|
@ -125,8 +125,8 @@ extern char re_error[RE_ERROR_BUF + 1]; /* Seems big enough */
|
|||||||
|
|
||||||
/* util.c */
|
/* util.c */
|
||||||
bool file_matching(const char *fname);
|
bool file_matching(const char *fname);
|
||||||
int procfile(const char *fn);
|
bool procfile(const char *fn);
|
||||||
int grep_tree(char **argv);
|
bool grep_tree(char **argv);
|
||||||
void *grep_malloc(size_t size);
|
void *grep_malloc(size_t size);
|
||||||
void *grep_calloc(size_t nmemb, size_t size);
|
void *grep_calloc(size_t nmemb, size_t size);
|
||||||
void *grep_realloc(void *ptr, size_t size);
|
void *grep_realloc(void *ptr, size_t size);
|
||||||
|
@ -139,16 +139,17 @@ dir_matching(const char *dname)
|
|||||||
* Processes a directory when a recursive search is performed with
|
* Processes a directory when a recursive search is performed with
|
||||||
* the -R option. Each appropriate file is passed to procfile().
|
* the -R option. Each appropriate file is passed to procfile().
|
||||||
*/
|
*/
|
||||||
int
|
bool
|
||||||
grep_tree(char **argv)
|
grep_tree(char **argv)
|
||||||
{
|
{
|
||||||
FTS *fts;
|
FTS *fts;
|
||||||
FTSENT *p;
|
FTSENT *p;
|
||||||
int c, fts_flags;
|
int c, fts_flags;
|
||||||
bool ok;
|
bool matched, ok;
|
||||||
const char *wd[] = { ".", NULL };
|
const char *wd[] = { ".", NULL };
|
||||||
|
|
||||||
c = fts_flags = 0;
|
c = fts_flags = 0;
|
||||||
|
matched = false;
|
||||||
|
|
||||||
switch(linkbehave) {
|
switch(linkbehave) {
|
||||||
case LINK_EXPLICIT:
|
case LINK_EXPLICIT:
|
||||||
@ -195,14 +196,14 @@ grep_tree(char **argv)
|
|||||||
if (fexclude || finclude)
|
if (fexclude || finclude)
|
||||||
ok &= file_matching(p->fts_path);
|
ok &= file_matching(p->fts_path);
|
||||||
|
|
||||||
if (ok)
|
if (ok && procfile(p->fts_path))
|
||||||
c += procfile(p->fts_path);
|
matched = true;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fts_close(fts);
|
fts_close(fts);
|
||||||
return (c);
|
return (matched);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
@ -288,7 +289,7 @@ procmatches(struct mprintc *mc, struct parsec *pc, bool matched)
|
|||||||
* Opens a file and processes it. Each file is processed line-by-line
|
* Opens a file and processes it. Each file is processed line-by-line
|
||||||
* passing the lines to procline().
|
* passing the lines to procline().
|
||||||
*/
|
*/
|
||||||
int
|
bool
|
||||||
procfile(const char *fn)
|
procfile(const char *fn)
|
||||||
{
|
{
|
||||||
struct parsec pc;
|
struct parsec pc;
|
||||||
@ -296,7 +297,7 @@ procfile(const char *fn)
|
|||||||
struct file *f;
|
struct file *f;
|
||||||
struct stat sb;
|
struct stat sb;
|
||||||
mode_t s;
|
mode_t s;
|
||||||
int c, t;
|
int lines, t;
|
||||||
|
|
||||||
if (strcmp(fn, "-") == 0) {
|
if (strcmp(fn, "-") == 0) {
|
||||||
fn = label != NULL ? label : errstr[1];
|
fn = label != NULL ? label : errstr[1];
|
||||||
@ -306,10 +307,10 @@ procfile(const char *fn)
|
|||||||
/* Check if we need to process the file */
|
/* Check if we need to process the file */
|
||||||
s = sb.st_mode & S_IFMT;
|
s = sb.st_mode & S_IFMT;
|
||||||
if (dirbehave == DIR_SKIP && s == S_IFDIR)
|
if (dirbehave == DIR_SKIP && s == S_IFDIR)
|
||||||
return (0);
|
return (false);
|
||||||
if (devbehave == DEV_SKIP && (s == S_IFIFO ||
|
if (devbehave == DEV_SKIP && (s == S_IFIFO ||
|
||||||
s == S_IFCHR || s == S_IFBLK || s == S_IFSOCK))
|
s == S_IFCHR || s == S_IFBLK || s == S_IFSOCK))
|
||||||
return (0);
|
return (false);
|
||||||
}
|
}
|
||||||
f = grep_open(fn);
|
f = grep_open(fn);
|
||||||
}
|
}
|
||||||
@ -317,7 +318,7 @@ procfile(const char *fn)
|
|||||||
file_err = true;
|
file_err = true;
|
||||||
if (!sflag)
|
if (!sflag)
|
||||||
warn("%s", fn);
|
warn("%s", fn);
|
||||||
return (0);
|
return (false);
|
||||||
}
|
}
|
||||||
|
|
||||||
pc.ln.file = grep_strdup(fn);
|
pc.ln.file = grep_strdup(fn);
|
||||||
@ -335,7 +336,7 @@ procfile(const char *fn)
|
|||||||
mc.doctx = true;
|
mc.doctx = true;
|
||||||
mcount = mlimit;
|
mcount = mlimit;
|
||||||
|
|
||||||
for (c = 0; c == 0 || !(lflag || qflag); ) {
|
for (lines = 0; lines == 0 || !(lflag || qflag); ) {
|
||||||
/*
|
/*
|
||||||
* XXX TODO: We need to revisit this in a chunking world. We're
|
* XXX TODO: We need to revisit this in a chunking world. We're
|
||||||
* not going to be doing per-line statistics because of the
|
* not going to be doing per-line statistics because of the
|
||||||
@ -365,7 +366,7 @@ procfile(const char *fn)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if ((t = procline(&pc)) == 0)
|
if ((t = procline(&pc)) == 0)
|
||||||
++c;
|
++lines;
|
||||||
|
|
||||||
/* Halt processing if we hit our match limit */
|
/* Halt processing if we hit our match limit */
|
||||||
if (!procmatches(&mc, &pc, t == 0))
|
if (!procmatches(&mc, &pc, t == 0))
|
||||||
@ -378,19 +379,19 @@ procfile(const char *fn)
|
|||||||
if (cflag) {
|
if (cflag) {
|
||||||
if (!hflag)
|
if (!hflag)
|
||||||
printf("%s:", pc.ln.file);
|
printf("%s:", pc.ln.file);
|
||||||
printf("%u\n", c);
|
printf("%u\n", lines);
|
||||||
}
|
}
|
||||||
if (lflag && !qflag && c != 0)
|
if (lflag && !qflag && lines != 0)
|
||||||
printf("%s%c", fn, nullflag ? 0 : '\n');
|
printf("%s%c", fn, nullflag ? 0 : '\n');
|
||||||
if (Lflag && !qflag && c == 0)
|
if (Lflag && !qflag && lines == 0)
|
||||||
printf("%s%c", fn, nullflag ? 0 : '\n');
|
printf("%s%c", fn, nullflag ? 0 : '\n');
|
||||||
if (c && !cflag && !lflag && !Lflag &&
|
if (lines != 0 && !cflag && !lflag && !Lflag &&
|
||||||
binbehave == BINFILE_BIN && f->binary && !qflag)
|
binbehave == BINFILE_BIN && f->binary && !qflag)
|
||||||
printf(errstr[7], fn);
|
printf(errstr[7], fn);
|
||||||
|
|
||||||
free(pc.ln.file);
|
free(pc.ln.file);
|
||||||
free(f);
|
free(f);
|
||||||
return (c);
|
return (lines != 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef WITH_INTERNAL_NOSPEC
|
#ifdef WITH_INTERNAL_NOSPEC
|
||||||
|
Loading…
Reference in New Issue
Block a user