File: print.c

package info (click to toggle)
p11-kit 0.26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 12,088 kB
  • sloc: ansic: 73,585; sh: 7,776; xml: 1,953; makefile: 1,200; python: 675; sed: 39
file content (190 lines) | stat: -rw-r--r-- 4,794 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/*
 * 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 <zfridric@redhat.com>
 */

#include "config.h"

#include "print.h"

#include "compat.h"
#include "debug.h"
#include <stdarg.h>

static const char *
color_to_sgr (p11_color color)
{
	switch (color) {
	case P11_COLOR_BLACK:   return "30";
	case P11_COLOR_RED:     return "31";
	case P11_COLOR_GREEN:   return "32";
	case P11_COLOR_YELLOW:  return "33";
	case P11_COLOR_BLUE:    return "34";
	case P11_COLOR_MAGENTA: return "35";
	case P11_COLOR_CYAN:    return "36";
	case P11_COLOR_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
		fprintf (fp, "%s\n", string);
}

void
p11_print_word (FILE *fp,
		const char *string,
		p11_color color,
		p11_font font)
{
	if (!isatty (fileno (fp))) {
		fputs (string, fp);
		return;
	}

	fprintf (fp, "\033[%s", color_to_sgr (color));
	if (font & P11_FONT_BOLD)
		fprintf (fp, ";1");
	if (font & P11_FONT_UNDERLINE)
		fprintf (fp, ";4");
	fprintf (fp, "m%s\033[0m", string);
}

/* List of colors used for nested section headers */
static p11_color header_colors[] = {
	P11_COLOR_BLUE,
	P11_COLOR_GREEN
};

void
p11_list_printer_init (p11_list_printer *printer, FILE *fp, size_t depth)
{
	printer->fp = fp;
	printer->use_color = isatty (fileno (fp));
	printer->depth = depth;
}

static inline void
print_indent (FILE *fp, size_t depth)
{
	size_t i;

	for (i = 0; i < depth; ++i)
		fputs ("    ", fp);
}

void
p11_list_printer_start_section (p11_list_printer *printer,
			   const char *header,
			   const char *value_fmt,
			   ...)
{
	va_list args;

	return_if_fail (printer->depth < sizeof(header_colors) / sizeof(*header_colors));

	print_indent (printer->fp, printer->depth);

	if (printer->use_color) {
		fprintf (printer->fp, "\033[%s;1m%s\033[0m: ",
			 color_to_sgr (header_colors[printer->depth]),
			 header);
	} else {
		fprintf (printer->fp, "%s: ", header);
	}

	va_start (args, value_fmt);
	vfprintf (printer->fp, value_fmt, args);
	va_end (args);

	fputc ('\n', printer->fp);

	printer->depth++;
}

void
p11_list_printer_end_section (p11_list_printer *printer)
{
	printer->depth--;
}

void
p11_list_printer_write_value (p11_list_printer *printer,
			 const char *name,
			 const char *value_fmt,
			 ...)
{
	va_list args;

	print_indent (printer->fp, printer->depth);

	if (printer->use_color) {
		fprintf (printer->fp, "\033[0;1m%s\033[0m: ", name);
	} else {
		fprintf (printer->fp, "%s: ", name);
	}

	va_start (args, value_fmt);
	vfprintf (printer->fp, value_fmt, args);
	va_end (args);

	fputc ('\n', printer->fp);
}

void
p11_list_printer_write_array (p11_list_printer *printer,
			 const char *name,
			 const p11_array *array)
{
	size_t i;

	print_indent (printer->fp, printer->depth);

	if (printer->use_color) {
		fprintf (printer->fp, "\033[0;1m%s\033[0m:\n", name);
	} else {
		fprintf (printer->fp, "%s:\n", name);
	}

	for (i = 0; i < array->num; i++) {
		print_indent (printer->fp, printer->depth + 1);

		/* List elements are preceded by a couple of additional spaces */
		fprintf (printer->fp, "  %s\n", (const char *)array->elem[i]);
	}
}