]> git.sesse.net Git - plocate/commitdiff
Implement the -N/--literal option.
authorSteinar H. Gunderson <steinar+git@gunderson.no>
Tue, 5 Oct 2021 21:02:09 +0000 (23:02 +0200)
committerSteinar H. Gunderson <steinar+git@gunderson.no>
Tue, 5 Oct 2021 21:02:09 +0000 (23:02 +0200)
This matches the GNU coreutils option to turn off the automatic
quoting of special characters when printing to a tty.

options.h
plocate.1
plocate.cpp
serializer.cpp

index eba4900c08a4b6818718caeadd8d88e9ef238e03..39b0978b1c15e6fc35d5d23c16ca35d4df5a796d 100644 (file)
--- 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)
index cc0e8430b4a6889c990e26e3dde60e79a399cc2d..3d02f2f606380489a23ffd108163dd0b767bd66f 100644 (file)
--- 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
index d021cc89d6281bf90a74cc56a005dd83b29b293c..1c0b3d0463f09ea04a5f9a39156d91a1e089c7fe 100644 (file)
@@ -58,6 +58,7 @@ bool check_existence = false;
 int64_t limit_matches = numeric_limits<int64_t>::max();
 int64_t limit_left = numeric_limits<int64_t>::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;
index f27a865a06e997bec1de0a408624b322c05a6dfb..7f3e88842da24ab9bf5d84360a8284bc1cf2061f 100644 (file)
@@ -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;
        }