From 1c3b2019bda2eca09e8791a2e579ac3f90f6c689 Mon Sep 17 00:00:00 2001 From: Christian Neukirchen Date: Tue, 2 Aug 2016 17:03:32 +0200 Subject: [PATCH] magrep: add -m --- magrep.c | 32 ++++++++++++++++++-------------- man/magrep.1 | 8 ++++++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/magrep.c b/magrep.c index 321a481..f28f1c0 100644 --- a/magrep.c +++ b/magrep.c @@ -17,6 +17,7 @@ static int dflag; static int iflag; static int qflag; static int vflag; +static long mflag; static long matches; static regex_t pattern; @@ -29,9 +30,11 @@ match(char *file, char *s) if (vflag ^ (regexec(&pattern, s, 0, 0, 0) == 0)) { if (qflag) exit(0); + matches++; if (!cflag) printf("%s\n", file); - matches++; + if (mflag && matches >= mflag) + exit(0); return 1; } @@ -55,7 +58,7 @@ match_part(int depth, struct message *msg, char *body, size_t bodylen) strcasecmp(charset, "utf-8") == 0 || strcasecmp(charset, "utf8") == 0 || strcasecmp(charset, "us-ascii") == 0) { - (void) bodylen; /* XXX */ + (void) bodylen; /* XXX */ if (match(curfile, body)) r = MIME_STOP; } else { @@ -72,7 +75,7 @@ match_body(char *file) { curfile = file; while (*curfile == ' ' || *curfile == '\t') - curfile++; + curfile++; struct message *msg = blaze822_file(curfile); if (!msg) @@ -96,7 +99,7 @@ magrep(char *file) char *filename = file; while (*filename == ' ' || *filename == '\t') - filename++; + filename++; struct message *msg = blaze822(filename); if (!msg) @@ -110,7 +113,7 @@ magrep(char *file) match(file, d); } else if (aflag) { char *disp, *addr; - while ((v = blaze822_addr(v, &disp, &addr))) { + while ((v = blaze822_addr(v, &disp, &addr))) { if (addr && match(file, addr)) break; } @@ -126,18 +129,19 @@ int main(int argc, char *argv[]) { int c; - while ((c = getopt(argc, argv, "acdiqv")) != -1) + while ((c = getopt(argc, argv, "acdim:qv")) != -1) switch(c) { - case 'a': aflag = 1; break; - case 'c': cflag = 1; break; - case 'd': dflag = 1; break; - case 'i': iflag = REG_ICASE; break; - case 'q': qflag = 1; break; - case 'v': vflag = 1; break; + case 'a': aflag = 1; break; + case 'c': cflag = 1; break; + case 'd': dflag = 1; break; + case 'i': iflag = REG_ICASE; break; + case 'm': mflag = atol(optarg); break; + case 'q': qflag = 1; break; + case 'v': vflag = 1; break; default: usage: fprintf(stderr, -"Usage: magrep [-c|-q] [-v] [-i] [-a|-d] header:regex [msgs...]\n"); +"Usage: magrep [-c|-q|-m max] [-v] [-i] [-a|-d] header:regex [msgs...]\n"); exit(2); } @@ -163,7 +167,7 @@ main(int argc, char *argv[]) else blaze822_loop(argc-optind, argv+optind, magrep); - if (cflag) + if (cflag && !qflag && !mflag) printf("%ld\n", matches); return !matches; diff --git a/man/magrep.1 b/man/magrep.1 index 6d41292..df16470 100644 --- a/man/magrep.1 +++ b/man/magrep.1 @@ -6,7 +6,7 @@ .Nd print mails matching a pattern .Sh SYNOPSIS .Nm -.Op Fl c | Fl q +.Op Fl c | Fl q | Fl m Ar max .Op Fl v .Op Fl i .Op Fl a | Fl d @@ -32,7 +32,7 @@ If is .Sq Li "/" , .Nm -will instead match inside the text parts of the +will instead search the plain text parts of the .Em body of the messages. .Pp @@ -65,6 +65,10 @@ according to RFC 2047 first. Match .Ar regex case insensitively. +.It Fl m Ar max +Do not show more than +.Ar max +matches. .It Fl q Quiet mode: do not print anything, quit as soon as possible.