]> git.sesse.net Git - plocate/blobdiff - serializer.cpp
Release plocate 1.1.22.
[plocate] / serializer.cpp
index f20c304de54e690a5c41852127da93b6b8c3c2bb..356094fde659f86715a730d76cf5801135782af0 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;
        }
@@ -56,11 +56,14 @@ void print_possibly_escaped(const string &str)
                        break;  // EOF.
                } else if (pwc < 32 || pwc == '\'' || pwc == '"' || pwc == '\\') {
                        all_safe = false;
+               } else if (pwc == '`') {
+                       // A rather odd case; ls quotes this but does not escape it.
+                       all_safe = false;
                } else {
                        ptr += ret;
                        len -= ret;
                }
-       } while (all_safe);
+       } while (all_safe && *ptr != '\0');
 
        if (all_safe) {
                printf("%s\n", str.c_str());
@@ -77,17 +80,18 @@ void print_possibly_escaped(const string &str)
        mbtowc(nullptr, 0, 0);
        ptr = str.data();
        len = str.size();
-       for (;;) {
+       while (*ptr != '\0') {
                int ret = mbtowc(nullptr, ptr, len);
                if (ret == -1) {
                        // Malformed data.
                        printf("?");
                        ++ptr;
                        --len;
+                       continue;
                } else if (ret == 0) {
                        break;  // EOF.
                }
-               if (*ptr < 32 || *ptr == '\'' || *ptr == '"' || *ptr == '\\') {
+               if ((unsigned char)*ptr < 32 || *ptr == '\'' || *ptr == '"' || *ptr == '\\') {
                        if (!in_escaped_mode) {
                                printf("'$'");
                                in_escaped_mode = true;