Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
Signed-off-by: Zoltan Fridrich <[email protected]>
  • Loading branch information
ZoltanFridrich committed May 29, 2023
1 parent b30ac2c commit e214e7e
Show file tree
Hide file tree
Showing 8 changed files with 243 additions and 80 deletions.
1 change: 1 addition & 0 deletions common/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
1 change: 1 addition & 0 deletions common/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ if get_option('test')
endif

libp11_tool_sources = [
'print.c',
'tool.c'
]

Expand Down
127 changes: 127 additions & 0 deletions common/print.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#include "config.h"

#include "print.h"

#include <stdarg.h>
#include <unistd.h>

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);
}
67 changes: 67 additions & 0 deletions common/print.h
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*/

#ifndef P11_PRINT_H_
#define P11_PRINT_H_

#include <stdio.h>

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_ */
6 changes: 0 additions & 6 deletions common/tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,6 @@

#include "tool.h"

bool isatty_out;
bool isatty_err;

static char
short_option (int opt)
{
Expand Down Expand Up @@ -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 ();

Expand Down
73 changes: 27 additions & 46 deletions p11-kit/lists.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@

#include "message.h"
#include "p11-kit.h"
#include "print.h"
#include "tool.h"
#include "uri.h"

Expand All @@ -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";
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading

0 comments on commit e214e7e

Please sign in to comment.