-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Zoltan Fridrich <[email protected]>
- Loading branch information
1 parent
b30ac2c
commit e214e7e
Showing
8 changed files
with
243 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ if get_option('test') | |
endif | ||
|
||
libp11_tool_sources = [ | ||
'print.c', | ||
'tool.c' | ||
] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.