diff options
Diffstat (limited to 'clients/common/nm-client-utils.c')
| -rw-r--r-- | clients/common/nm-client-utils.c | 117 |
1 files changed, 94 insertions, 23 deletions
diff --git a/clients/common/nm-client-utils.c b/clients/common/nm-client-utils.c index 0a155dae..4d356edb 100644 --- a/clients/common/nm-client-utils.c +++ b/clients/common/nm-client-utils.c @@ -1,20 +1,6 @@ -/* nmcli - command-line tool to control NetworkManager - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. - * - * Copyright 2010 - 2017 Red Hat, Inc. +// SPDX-License-Identifier: GPL-2.0+ +/* + * Copyright (C) 2010 - 2017 Red Hat, Inc. */ #include "nm-default.h" @@ -448,14 +434,14 @@ nmc_activation_get_effective_state (NMActiveConnection *active, } static gboolean -can_show_graphics (void) +can_show_utf8 (void) { - static gboolean can_show_graphics_set = FALSE; - gboolean can_show_graphics = TRUE; + static gboolean can_show_utf8_set = FALSE; + static gboolean can_show_utf8 = TRUE; char *locale_str; - if (G_LIKELY (can_show_graphics_set)) - return can_show_graphics; + if (G_LIKELY (can_show_utf8_set)) + return can_show_utf8; if (!g_get_charset (NULL)) { /* Non-UTF-8 locale */ @@ -463,9 +449,24 @@ can_show_graphics (void) if (locale_str) g_free (locale_str); else - can_show_graphics = FALSE; + can_show_utf8 = FALSE; } + return can_show_utf8; +} + + +static gboolean +can_show_graphics (void) +{ + static gboolean can_show_graphics_set = FALSE; + static gboolean can_show_graphics = TRUE; + + if (G_LIKELY (can_show_graphics_set)) + return can_show_graphics; + + can_show_graphics = can_show_utf8 (); + /* The linux console font typically doesn't have characters we need */ if (g_strcmp0 (g_getenv ("TERM"), "linux") == 0) can_show_graphics = FALSE; @@ -515,3 +516,73 @@ nmc_password_subst_char (void) else return "*"; } + +/* + * We actually use a small part of qrcodegen.c, but we'd prefer to keep it + * intact. Include it instead of linking to it to give the compiler a + * chance to optimize bits we don't need away. + */ + +#pragma GCC visibility push(hidden) +NM_PRAGMA_WARNING_DISABLE("-Wdeclaration-after-statement") +#define NDEBUG +#include "qrcodegen.c" +NM_PRAGMA_WARNING_REENABLE +#pragma GCC visibility pop + +void +nmc_print_qrcode (const char *str) +{ + uint8_t tempBuffer[qrcodegen_BUFFER_LEN_FOR_VERSION (qrcodegen_VERSION_MAX)]; + uint8_t qrcode[qrcodegen_BUFFER_LEN_FOR_VERSION (qrcodegen_VERSION_MAX)]; + gboolean term_linux; + int size; + int x; + int y; + + term_linux = g_strcmp0 (g_getenv ("TERM"), "linux") == 0; + if (!term_linux && !can_show_graphics ()) + return; + + if (!qrcodegen_encodeText (str, + tempBuffer, + qrcode, + qrcodegen_Ecc_LOW, + qrcodegen_VERSION_MIN, + qrcodegen_VERSION_MAX, + qrcodegen_Mask_AUTO, + FALSE)) { + return; + } + + size = qrcodegen_getSize (qrcode); + + g_print ("\n"); + + if (term_linux) { + /* G1 alternate character set on Linux console. */ + for (y = -1; y < size + 1; y += 1) { + g_print (" \033[37;40;1m\016"); + for (x = -1; x < size + 1; x++) { + g_print ( qrcodegen_getModule (qrcode, x, y) + ? " " : "\060\060"); + } + g_print ("\017\033[0m\n"); + } + } else { + /* UTF-8 */ + for (y = -2; y < size + 2; y += 2) { + g_print (" \033[37;40m"); + for (x = -2; x < size + 2; x++) { + bool top = qrcodegen_getModule (qrcode, x, y); + bool bottom = qrcodegen_getModule (qrcode, x, y + 1); + if (top) { + g_print (bottom ? " " : "\u2584"); + } else { + g_print (bottom ? "\u2580" : "\u2588"); + } + } + g_print ("\033[0m\n"); + } + } +} |