From 67c741868bac4d4b845426d8b7e6126f4ec41232 Mon Sep 17 00:00:00 2001 From: "Steinar H. Gunderson" Date: Tue, 5 Oct 2021 23:02:09 +0200 Subject: [PATCH] Implement the -N/--literal option. This matches the GNU coreutils option to turn off the automatic quoting of special characters when printing to a tty. --- options.h | 1 + plocate.1 | 10 ++++++++++ plocate.cpp | 8 +++++++- serializer.cpp | 2 +- 4 files changed, 19 insertions(+), 2 deletions(-) diff --git a/options.h b/options.h index eba4900..39b0978 100644 --- a/options.h +++ b/options.h @@ -14,5 +14,6 @@ extern bool check_existence; extern int64_t limit_matches; extern int64_t limit_left; // Not strictly an option. extern bool stdout_is_tty; // Same. +extern bool literal_printing; #endif // !defined(_OPTIONS_H) diff --git a/plocate.1 b/plocate.1 index cc0e843..3d02f2f 100644 --- a/plocate.1 +++ b/plocate.1 @@ -106,6 +106,16 @@ matches have been found. If .B \-\-count is given, the number printed out will be at most \fILIMIT\fR. +.TP +\fB\-N\fR, \fB\-\-literal\fR +Print entry names without quoting. Normally, +.B plocate +will escape special characters in filenames, so that they are safe for +consumption by typical shells (similar to the GNU coreutils +.I shell-escape-always +quoting style), unless printing to a pipe, but this options will +turn off such quoting. + .TP \fB\-0\fR, \fB\-\-null\fR Instead of writing a newline after every match, write a NUL diff --git a/plocate.cpp b/plocate.cpp index d021cc8..1c0b3d0 100644 --- a/plocate.cpp +++ b/plocate.cpp @@ -58,6 +58,7 @@ bool check_existence = false; int64_t limit_matches = numeric_limits::max(); int64_t limit_left = numeric_limits::max(); bool stdout_is_tty = false; +bool literal_printing = false; static bool in_forked_child = false; steady_clock::time_point start; @@ -809,6 +810,7 @@ void usage() " -i, --ignore-case search case-insensitively\n" " -l, --limit LIMIT stop after LIMIT matches\n" " -0, --null delimit matches by NUL instead of newline\n" + " -N, --literal do not quote filenames, even if printing to a tty\n" " -r, --regexp interpret patterns as basic regexps (slow)\n" " --regex interpret patterns as extended regexps (slow)\n" " -w, --wholename search the entire path name (default; see -b)\n" @@ -840,6 +842,7 @@ int main(int argc, char **argv) { "existing", no_argument, 0, 'e' }, { "ignore-case", no_argument, 0, 'i' }, { "limit", required_argument, 0, 'l' }, + { "literal", no_argument, 0, 'N' }, { "null", no_argument, 0, '0' }, { "version", no_argument, 0, 'V' }, { "regexp", no_argument, 0, 'r' }, @@ -854,7 +857,7 @@ int main(int argc, char **argv) setlocale(LC_ALL, ""); for (;;) { int option_index = 0; - int c = getopt_long(argc, argv, "bcd:ehil:n:0rwVD", long_options, &option_index); + int c = getopt_long(argc, argv, "bcd:ehil:n:N0rwVD", long_options, &option_index); if (c == -1) { break; } @@ -885,6 +888,9 @@ int main(int argc, char **argv) exit(1); } break; + case 'N': + literal_printing = true; + break; case '0': print_nul = true; break; diff --git a/serializer.cpp b/serializer.cpp index f27a865..7f3e888 100644 --- a/serializer.cpp +++ b/serializer.cpp @@ -32,7 +32,7 @@ void print_possibly_escaped(const string &str) if (print_nul) { printf("%s%c", str.c_str(), 0); return; - } else if (!stdout_is_tty) { + } else if (literal_printing || !stdout_is_tty) { printf("%s\n", str.c_str()); return; } -- 2.39.2