From e214e7e6c68ee492027240140a8e12d4e155d3b3 Mon Sep 17 00:00:00 2001 From: Zoltan Fridrich Date: Mon, 29 May 2023 15:49:17 +0200 Subject: [PATCH] refactoring Signed-off-by: Zoltan Fridrich --- common/Makefile.am | 1 + common/meson.build | 1 + common/print.c | 127 +++++++++++++++++++++++++++++++++++++++++++ common/print.h | 67 +++++++++++++++++++++++ common/tool.c | 6 -- p11-kit/lists.c | 73 +++++++++---------------- trust/check-format.c | 13 +++-- trust/list.c | 35 +++++------- 8 files changed, 243 insertions(+), 80 deletions(-) create mode 100644 common/print.c create mode 100644 common/print.h diff --git a/common/Makefile.am b/common/Makefile.am index 3a5f73dbd..b389d5a3f 100644 --- a/common/Makefile.am +++ b/common/Makefile.am @@ -44,6 +44,7 @@ libp11_test_la_SOURCES = \ $(NULL) libp11_tool_la_SOURCES = \ + common/print.c common/print.h \ common/tool.c common/tool.h \ $(NULL) diff --git a/common/meson.build b/common/meson.build index 5510a1ade..65ad0d866 100644 --- a/common/meson.build +++ b/common/meson.build @@ -49,6 +49,7 @@ if get_option('test') endif libp11_tool_sources = [ + 'print.c', 'tool.c' ] diff --git a/common/print.c b/common/print.c new file mode 100644 index 000000000..65fae4a50 --- /dev/null +++ b/common/print.c @@ -0,0 +1,127 @@ +/* + * Copyright (c) 2023 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Zoltan Fridrich + */ + +#include "config.h" + +#include "print.h" + +#include +#include + +static const char * +style_to_sgr (enum p11_print_style style) +{ + switch (style) { + case P11_PRINT_BOLD: return "1"; + case P11_PRINT_BLACK: return "30"; + case P11_PRINT_RED: return "31"; + case P11_PRINT_GREEN: return "32"; + case P11_PRINT_YELLOW: return "33"; + case P11_PRINT_BLUE: return "34"; + case P11_PRINT_MAGENTA: return "35"; + case P11_PRINT_CYAN: return "36"; + case P11_PRINT_WHITE: return "37"; + default: return "0"; + } +} + +void +p11_highlight_word (FILE *fp, + const char *string) +{ + if (isatty (fileno (fp))) + fprintf (fp, "\e]8;;%s\e\\\033[36m%s\033[0m\e]8;;\e\\\n", string, string); + else + fputs (string, fp); +} + +void +p11_print_word (FILE *fp, + const char *string, + int n_styles, + ...) +{ + size_t i; + va_list args; + enum p11_print_style style; + + if (n_styles == 0 || !isatty (fileno (fp))) { + fputs (string, fp); + return; + } + + fputs ("\033[", fp); + va_start(args, n_styles); + for (i = 0; i < n_styles; ++i) { + style = va_arg(args, enum p11_print_style); + fputs (style_to_sgr (style), fp); + fputc (i + 1 == n_styles ? 'm' : ';', fp); + } + va_end(args); + fputs (string, fp); + fputs ("\033[0m", fp); +} + +void +p11_print_value (FILE *fp, + const char *key, + const char *value, + size_t indent, + int n_styles, + ...) +{ + size_t i; + va_list args; + enum p11_print_style style; + + for (i = 0; i < indent; ++i) + fputc (' ', fp); + + if (n_styles == 0 || !isatty (fileno (fp))) { + fprintf (fp, "%s: %s\n", key, value); + return; + } + + fputs ("\033[", fp); + va_start(args, n_styles); + for (i = 0; i < n_styles; ++i) { + style = va_arg(args, enum p11_print_style); + fputs (style_to_sgr (style), fp); + fputc (i + 1 == n_styles ? 'm' : ';', fp); + } + va_end(args); + fprintf (fp, "%s:", key); + fprintf (fp, "\033[0m"); + fprintf (fp, " %s\n", value); +} diff --git a/common/print.h b/common/print.h new file mode 100644 index 000000000..ad819709e --- /dev/null +++ b/common/print.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2023 Red Hat Inc. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * * Redistributions of source code must retain the above + * copyright notice, this list of conditions and the + * following disclaimer. + * * Redistributions in binary form must reproduce the + * above copyright notice, this list of conditions and + * the following disclaimer in the documentation and/or + * other materials provided with the distribution. + * * The names of contributors to this software may not be + * used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF + * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * Author: Zoltan Fridrich + */ + +#ifndef P11_PRINT_H_ +#define P11_PRINT_H_ + +#include + +enum p11_print_style { + P11_PRINT_BOLD, + P11_PRINT_BLACK, + P11_PRINT_RED, + P11_PRINT_GREEN, + P11_PRINT_YELLOW, + P11_PRINT_BLUE, + P11_PRINT_MAGENTA, + P11_PRINT_CYAN, + P11_PRINT_WHITE +}; + +void p11_highlight_word (FILE *fp, + const char *string); + +void p11_print_word (FILE *fp, + const char *string, + int n_styles, + ...); + +void p11_print_value (FILE *fp, + const char *key, + const char *value, + size_t indent, + int n_styles, + ...); + +#endif /* P11_PRINT_H_ */ diff --git a/common/tool.c b/common/tool.c index f0eada113..5bd2a956a 100644 --- a/common/tool.c +++ b/common/tool.c @@ -61,9 +61,6 @@ #include "tool.h" -bool isatty_out; -bool isatty_err; - static char short_option (int opt) { @@ -235,9 +232,6 @@ p11_tool_main (int argc, textdomain (PACKAGE_NAME); #endif - isatty_out = isatty (fileno (stdout)); - isatty_err = isatty (fileno (stderr)); - /* Print messages by default. */ p11_message_loud (); diff --git a/p11-kit/lists.c b/p11-kit/lists.c index 28656f490..dd0776424 100644 --- a/p11-kit/lists.c +++ b/p11-kit/lists.c @@ -48,6 +48,7 @@ #include "message.h" #include "p11-kit.h" +#include "print.h" #include "tool.h" #include "uri.h" @@ -61,7 +62,6 @@ int p11_kit_list_modules (int argc, char *argv[]); -extern bool isatty_out; bool verbose = false; static const char HEXC_LOWER[] = "0123456789abcdef"; @@ -120,49 +120,39 @@ print_token_info (CK_FUNCTION_LIST_PTR module, CK_SLOT_ID slot_id) } value = p11_kit_space_strdup (info.label, sizeof (info.label)); - printf (isatty_out ? - " \033[32;1mtoken:\033[0m %s\n" : - " token: %s\n", value); + p11_print_value (stdout, "token", value, 4, 2, P11_PRINT_BOLD, P11_PRINT_GREEN); free (value); value = p11_kit_space_strdup (info.manufacturerID, sizeof (info.manufacturerID)); - printf (isatty_out ? - " \033[1mmanufacturer:\033[0m %s\n" : - " manufacturer: %s\n", value); + p11_print_value (stdout, "manufacturer", value, 8, 1, P11_PRINT_BOLD); free (value); value = p11_kit_space_strdup (info.model, sizeof (info.model)); - printf (isatty_out ? - " \033[1mmodel:\033[0m %s\n" : - " model: %s\n", value); + p11_print_value (stdout, "model", value, 8, 1, P11_PRINT_BOLD); free (value); if (is_ascii_string (info.serialNumber, sizeof (info.serialNumber))) value = p11_kit_space_strdup (info.serialNumber, sizeof (info.serialNumber)); else value = hex_encode (info.serialNumber, sizeof (info.serialNumber)); - printf (isatty_out ? - " \033[1mserial-number:\033[0m %s\n" : - " serial-number: %s\n", value); + p11_print_value (stdout, "serial-number", value, 8, 1, P11_PRINT_BOLD); free (value); - if (info.hardwareVersion.major || info.hardwareVersion.minor) - printf (isatty_out ? - " \033[1mhardware-version:\033[0m %d.%d\n" : - " hardware-version: %d.%d\n", - info.hardwareVersion.major, - info.hardwareVersion.minor); - - if (info.firmwareVersion.major || info.firmwareVersion.minor) - printf (isatty_out ? - " \033[1mfirmware-version:\033[0m %d.%d\n" : - " firmware-version: %d.%d\n", - info.firmwareVersion.major, - info.firmwareVersion.minor); - - printf (isatty_out ? - " \033[1mflags:\033[0m\n" : - " flags:\n"); + if (info.hardwareVersion.major || info.hardwareVersion.minor) { + printf (" "); + p11_print_word (stdout, "hardware-version:", 1, P11_PRINT_BOLD); + printf (" %d.%d\n", info.hardwareVersion.major, info.hardwareVersion.minor); + } + + if (info.firmwareVersion.major || info.firmwareVersion.minor) { + printf (" "); + p11_print_word (stdout, "firmware-version:", 1, P11_PRINT_BOLD); + printf (" %d.%d\n", info.firmwareVersion.major, info.firmwareVersion.minor); + } + + printf (" "); + p11_print_word (stdout, "flags:", 1, P11_PRINT_BOLD); + printf ("\n"); #define X(x, y) if (info.flags & (x)) printf (" %s\n", (y)) X(CKF_RNG, "rng"); X(CKF_WRITE_PROTECTED, "write-protected"); @@ -202,23 +192,17 @@ print_module_info (CK_FUNCTION_LIST_PTR module) value = p11_kit_space_strdup (info.libraryDescription, sizeof (info.libraryDescription)); - printf (isatty_out ? - " \033[1mlibrary-description:\033[0m %s\n" : - " library-description: %s\n", value); + p11_print_value (stdout, "library-description", value, 4, 1, P11_PRINT_BOLD); free (value); value = p11_kit_space_strdup (info.manufacturerID, sizeof (info.manufacturerID)); - printf (isatty_out ? - " \033[1mlibrary-manufacturer:\033[0m %s\n" : - " library-manufacturer: %s\n", value); + p11_print_value (stdout, "library-manufacturer", value, 4, 1, P11_PRINT_BOLD); free (value); - printf (isatty_out ? - " \033[1mlibrary-version:\033[0m %d.%d\n" : - " library-version: %d.%d\n", - info.libraryVersion.major, - info.libraryVersion.minor); + printf (" "); + p11_print_word (stdout, "library-version:", 1, P11_PRINT_BOLD); + printf (" %d.%d\n", info.libraryVersion.major, info.libraryVersion.minor); count = sizeof (slot_list) / sizeof (slot_list[0]); rv = (module->C_GetSlotList) (CK_TRUE, slot_list, &count); @@ -247,11 +231,8 @@ print_modules (void) name = p11_kit_module_get_name (module_list[i]); path = p11_kit_config_option (module_list[i], "module"); - printf (isatty ? - "\033[34;1m%s:\033[0m %s\n" : - "%s: %s\n", - name ? name : "(null)", - path ? path : "(null)"); + p11_print_value (stdout, name ? name : "(null)", path ? path : "(null)", + 0, 2, P11_PRINT_BOLD, P11_PRINT_BLUE); print_module_info (module_list[i]); free (name); diff --git a/trust/check-format.c b/trust/check-format.c index 534c83e50..469a3d9e1 100644 --- a/trust/check-format.c +++ b/trust/check-format.c @@ -40,6 +40,7 @@ #include "debug.h" #include "message.h" #include "persist.h" +#include "print.h" #include "tool.h" #include @@ -55,8 +56,6 @@ #define _(x) (x) #endif -extern bool isatty_out; - enum format_result { FORMAT_OK, FORMAT_FAIL, @@ -67,21 +66,23 @@ static inline void print_result (enum format_result result, const char *filename) { - printf (isatty_out ? "\033[1m%s:\033[0m " : "%s: ", filename); + p11_print_word (stdout, filename, 1, P11_PRINT_BOLD); + p11_print_word (stdout, ": ", 1, P11_PRINT_BOLD); switch (result) { case FORMAT_OK: - printf (isatty_out ? "\033[1;32mOK\033[0m\n" : "OK\n"); + p11_print_word (stdout, "OK", 2, P11_PRINT_BOLD, P11_PRINT_GREEN); break; case FORMAT_FAIL: - printf (isatty_out ? "\033[1;31mFAIL\033[0m\n" : "FAIL\n"); + p11_print_word (stdout, "FAIL", 2, P11_PRINT_BOLD, P11_PRINT_RED); break; case FORMAT_ERROR: - printf (isatty_out ? "\033[1;31mERROR\033[0m\n" : "ERROR\n"); + p11_print_word (stdout, "ERROR", 2, P11_PRINT_BOLD, P11_PRINT_RED); break; default: assert_not_reached (); break; } + putchar ('\n'); } static enum format_result diff --git a/trust/list.c b/trust/list.c index cbe1a77ab..40c579ea6 100644 --- a/trust/list.c +++ b/trust/list.c @@ -43,6 +43,7 @@ #include "list.h" #include "message.h" #include "pkcs11x.h" +#include "print.h" #include "tool.h" #include "url.h" @@ -59,8 +60,6 @@ #define _(x) (x) #endif -extern bool isatty_out; - static char * format_uri (p11_enumerate *ex, int flags) @@ -125,42 +124,33 @@ list_iterate (p11_enumerate *ex, continue; } - if (isatty_out) - printf ("\e]8;;%s\e\\\033[36m%s\033[0m\e]8;;\e\\\n", string, string); - else - printf ("%s\n", string); + p11_highlight_word (stdout, string); free (string); if (p11_attrs_find_ulong (ex->attrs, CKA_CLASS, &klass)) { nick = p11_constant_nick (p11_constant_classes, klass); - if (nick != NULL) { - printf (isatty_out ? " \033[1mtype:\033[0m " : " type: "); - printf ("%s\n", nick); - } + if (nick != NULL) + p11_print_value (stdout, "type", nick, 4, 1, P11_PRINT_BOLD); } attr = p11_attrs_find_valid (ex->attrs, CKA_LABEL); if (attr && attr->pValue && attr->ulValueLen) { string = strndup (attr->pValue, attr->ulValueLen); - printf (isatty_out ? " \033[1mlabel:\033[0m " : " label: "); - printf ("%s\n", string); + p11_print_value (stdout, "label", string, 4, 1, P11_PRINT_BOLD); free (string); } - printf (isatty_out ? " \033[1mtrust:\033[0m " : " trust: "); if (p11_attrs_find_bool (ex->attrs, CKA_X_DISTRUSTED, &val) && val) - printf ("distrusted\n"); + p11_print_value (stdout, "trust", "distrusted", 4, 1, P11_PRINT_BOLD); else if (p11_attrs_find_bool (ex->attrs, CKA_TRUSTED, &val) && val) - printf ("anchor\n"); + p11_print_value (stdout, "trust", "anchor", 4, 1, P11_PRINT_BOLD); else - printf ("unspecified\n"); + p11_print_value (stdout, "trust", "unspecified", 4, 1, P11_PRINT_BOLD); if (p11_attrs_find_ulong (ex->attrs, CKA_CERTIFICATE_CATEGORY, &category)) { nick = p11_constant_nick (p11_constant_categories, category); - if (nick != NULL) { - printf (isatty_out ? " \033[1mcategory:\033[0m " : " category: "); - printf ("%s\n", nick); - } + if (nick != NULL) + p11_print_value (stdout, "category", nick, 4, 1, P11_PRINT_BOLD); } if (details) { @@ -169,8 +159,9 @@ list_iterate (p11_enumerate *ex, p11_buffer_init (&buf, 1024); bytes = attr->pValue; p11_url_encode (bytes, bytes + attr->ulValueLen, "", &buf); - printf (isatty_out ? " \033[1mpublic-key-info:\033[0m " : " public-key-info: "); - printf ("%.*s\n", (int)buf.len, (char *)buf.data); + printf (" "); + p11_print_word (stdout, "public-key-info:", 1, P11_PRINT_BOLD); + printf (" %.*s\n", (int)buf.len, (char *)buf.data); p11_buffer_uninit (&buf); } }