]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
journalctl: don't print QR codes if we can't/shan't use colors
authorLennart Poettering <lennart@poettering.net>
Thu, 11 Jun 2020 10:34:31 +0000 (12:34 +0200)
committerLennart Poettering <lennart@poettering.net>
Thu, 25 Jun 2020 07:28:18 +0000 (09:28 +0200)
We need them to display things properly, hence treat color-less
terminals the same way as UTF-8-less systems: avoid the QR code.

src/journal/journal-qrcode.c
src/journal/journal-qrcode.h
src/journal/journalctl.c

index 0d7af0bca6affe1357c42530eed7666e2391a8dc..706d33c38a97995bd0c466259654b4e969521414 100644 (file)
 #include "fd-util.h"
 #include "fileio.h"
 #include "journal-qrcode.h"
+#include "locale-util.h"
 #include "macro.h"
+#include "terminal-util.h"
 
-#define WHITE_ON_BLACK "\033[40;37;1m"
-#define NORMAL "\033[0m"
+#define ANSI_WHITE_ON_BLACK "\033[40;37;1m"
 
 static void print_border(FILE *output, unsigned width) {
         unsigned x, y;
 
         /* Four rows of border */
         for (y = 0; y < 4; y += 2) {
-                fputs(WHITE_ON_BLACK, output);
+                fputs(ANSI_WHITE_ON_BLACK, output);
 
                 for (x = 0; x < 4 + width + 4; x++)
                         fputs("\342\226\210", output);
 
-                fputs(NORMAL "\n", output);
+                fputs(ANSI_NORMAL "\n", output);
         }
 }
 
 int print_qr_code(
                 FILE *output,
+                const char *prefix_text,
                 const void *seed,
                 size_t seed_size,
                 uint64_t start,
@@ -48,6 +50,11 @@ int print_qr_code(
         assert(seed);
         assert(seed_size > 0);
 
+        /* If this is not an UTF-8 system or ANSI colors aren't supported/disabled don't print any QR
+         * codes */
+        if (!is_locale_utf8() || !colors_enabled())
+                return -EOPNOTSUPP;
+
         f = open_memstream_unlocked(&url, &url_size);
         if (!f)
                 return -ENOMEM;
@@ -78,6 +85,9 @@ int print_qr_code(
         if (!qr)
                 return -ENOMEM;
 
+        if (prefix_text)
+                fputs(prefix_text, output);
+
         print_border(output, qr->width);
 
         for (y = 0; y < (unsigned) qr->width; y += 2) {
@@ -86,7 +96,7 @@ int print_qr_code(
                 row1 = qr->data + qr->width * y;
                 row2 = row1 + qr->width;
 
-                fputs(WHITE_ON_BLACK, output);
+                fputs(ANSI_WHITE_ON_BLACK, output);
                 for (x = 0; x < 4; x++)
                         fputs("\342\226\210", output);
 
@@ -108,7 +118,7 @@ int print_qr_code(
 
                 for (x = 0; x < 4; x++)
                         fputs("\342\226\210", output);
-                fputs(NORMAL "\n", output);
+                fputs(ANSI_NORMAL "\n", output);
         }
 
         print_border(output, qr->width);
index 0774608edf4f7a078700631a0b1441f4c7d12b82..24ae9d32eebd7f523cd3ec5ffee00b11e8e0d369 100644 (file)
@@ -6,4 +6,4 @@
 
 #include "sd-id128.h"
 
-int print_qr_code(FILE *f, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t machine);
+int print_qr_code(FILE *f, const char *prefix_text, const void *seed, size_t seed_size, uint64_t start, uint64_t interval, const char *hn, sd_id128_t machine);
index 46a9f9fa0c1f5ca05cace19f15f7622130e9266d..4864014a38c385ce75168efac4e6111f6b32a73e 100644 (file)
@@ -1922,11 +1922,11 @@ static int setup_keys(void) {
                         fprintf(stderr, "\nThe keys have been generated for host " SD_ID128_FORMAT_STR ".\n", SD_ID128_FORMAT_VAL(machine));
 
 #if HAVE_QRENCODE
-                /* If this is not an UTF-8 system don't print any QR codes */
-                if (is_locale_utf8()) {
-                        fputs("\nTo transfer the verification key to your phone please scan the QR code below:\n\n", stderr);
-                        print_qr_code(stderr, seed, seed_size, n, arg_interval, hn, machine);
-                }
+                (void) print_qr_code(stderr,
+                                     "\nTo transfer the verification key to your phone please scan the QR code below:\n\n",
+                                     seed, seed_size,
+                                     n, arg_interval,
+                                     hn, machine);
 #endif
         }