diff options
author | Thomas Voss <mail@thomasvoss.com> | 2024-01-20 18:53:21 +0100 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2024-01-20 18:53:21 +0100 |
commit | 99a3838242604a4cc888ea7f5019207cfb1d003a (patch) | |
tree | 9a5df14659271c7b1337e70a3de565ce6b15eaf9 | |
parent | 0d3d39996597fcb9b8728f933c6c96ac2a1a8ff3 (diff) |
Add the -s/--strip-newline flag
-rw-r--r-- | man/grab.1 | 20 | ||||
-rw-r--r-- | src/grab.c | 34 |
2 files changed, 39 insertions, 15 deletions
@@ -6,14 +6,16 @@ .Nd search for patterns in files .Sh SYNOPSIS .Nm -.Op Fl cfnUz +.Op Fl s | z +.Op Fl cfnU .Ar pattern .Op Ar .Nm .Fl h .Pp .Nm "git grab" -.Op Fl cnUz +.Op Fl s | z +.Op Fl cnU .Ar pattern .Op Ar glob ... .Nm "git grab" @@ -111,6 +113,16 @@ is not compiled with PCRE support. See .Sx CAVEATS for more information. +.It Fl s , Fl Fl strip\-newline +Don’t print a newline at the end of a match if the match already ends in +a newline. +This can make output seem more +.Sq natural , +as many matches will already have terminating newlines. +.Pp +This option is mutually exclusive with the +.Fl z +option. .It Fl U , Fl Fl no\-unicode Don’t use Unicode properties when matching \ed, \ew, etc. Recognize only ASCII values instead. @@ -130,6 +142,10 @@ If combined with the option, or if two or more files were provided as arguments, filenames and matches will be separated by null bytes instead of colons. +.Pp +This option is mutually exclusive with the +.Fl s +option. .El .Ss Regular Expression Syntax By default @@ -80,7 +80,7 @@ static bool xisspace(char); static char *xstrchrnul(const char *, char); static int filecnt, rv; -static bool cflag, nflag, Uflag, zflag; +static bool cflag, nflag, sflag, Uflag, zflag; static bool fflag = #if GIT_GRAB true; @@ -100,9 +100,9 @@ usage(const char *s) { fprintf(stderr, #if GIT_GRAB - "Usage: %s [-cnUz] pattern [glob ...]\n" + "Usage: %s [-s | -z] [-cnU] pattern [glob ...]\n" #else - "Usage: %s [-cfnUz] pattern [file ...]\n" + "Usage: %s [-s | -z] [-cfnU] pattern [file ...]\n" #endif " %s -h\n", s, s); @@ -115,15 +115,16 @@ main(int argc, char **argv) int opt; struct ops ops; struct option longopts[] = { - {"color", no_argument, 0, 'c'}, + {"color", no_argument, nullptr, 'c'}, #if GIT_GRAB - {"filenames", no_argument, 0, 'f'}, + {"filenames", no_argument, nullptr, 'f'}, #endif - {"help", no_argument, 0, 'h'}, - {"newline", no_argument, 0, 'n'}, - {"no-unicode", no_argument, 0, 'U'}, - {"zero", no_argument, 0, 'z'}, - {nullptr, 0, 0, 0 }, + {"help", no_argument, nullptr, 'h'}, + {"newline", no_argument, nullptr, 'n'}, + {"strip-newline", no_argument, nullptr, 's'}, + {"no-unicode", no_argument, nullptr, 'U'}, + {"zero", no_argument, nullptr, 'z'}, + {nullptr, 0, nullptr, 0 }, }; #if GIT_GRAB @@ -131,9 +132,9 @@ main(int argc, char **argv) size_t len; ssize_t nr; FILE *flist; - const char *opts = "chnUz"; + const char *opts = "chnsUz"; #else - const char *opts = "cfhnUz"; + const char *opts = "cfhnsUz"; #endif argv[0] = basename(argv[0]); @@ -155,6 +156,9 @@ main(int argc, char **argv) case 'n': nflag = true; break; + case 's': + sflag = true; + break; case 'U': #if GRAB_DO_PCRE Uflag = true; @@ -173,6 +177,9 @@ main(int argc, char **argv) } } + if (sflag && zflag) + usage(argv[0]); + argc -= optind; argv += optind; filecnt = argc - 1; @@ -441,7 +448,8 @@ putm(struct sv sv, const char *filename) printf("%s%c", filename, zflag ? '\0' : ':'); } fwrite(sv.p, 1, sv.len, stdout); - putchar(zflag ? '\0' : '\n'); + if (!(sflag && sv.p[sv.len - 1] == '\n')) + putchar(zflag ? '\0' : '\n'); } bool |