From: Lennart Poettering Date: Thu, 11 Jun 2020 10:34:31 +0000 (+0200) Subject: journalctl: don't print QR codes if we can't/shan't use colors X-Git-Tag: v247-rc1~458^2~2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=0523022d5b021db64238d7005f98ba31d6da8e92;p=thirdparty%2Fsystemd.git journalctl: don't print QR codes if we can't/shan't use colors We need them to display things properly, hence treat color-less terminals the same way as UTF-8-less systems: avoid the QR code. --- diff --git a/src/journal/journal-qrcode.c b/src/journal/journal-qrcode.c index 0d7af0bca6a..706d33c38a9 100644 --- a/src/journal/journal-qrcode.c +++ b/src/journal/journal-qrcode.c @@ -10,27 +10,29 @@ #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); diff --git a/src/journal/journal-qrcode.h b/src/journal/journal-qrcode.h index 0774608edf4..24ae9d32eeb 100644 --- a/src/journal/journal-qrcode.h +++ b/src/journal/journal-qrcode.h @@ -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); diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index 46a9f9fa0c1..4864014a38c 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -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 }