/src/systemd/src/shared/pretty-print.h
Line | Count | Source |
1 | | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | | #pragma once |
3 | | |
4 | | #include <stdio.h> |
5 | | #include <string.h> |
6 | | |
7 | | #include "ansi-color.h" |
8 | | #include "fileio.h" |
9 | | #include "shared-forward.h" |
10 | | #include "glyph-util.h" |
11 | | |
12 | | #define CYLON_BUFFER_EXTRA (2*STRLEN(ANSI_RED) + STRLEN(ANSI_HIGHLIGHT_RED) + 2*STRLEN(ANSI_NORMAL)) |
13 | | |
14 | | void draw_cylon(char buffer[], size_t buflen, unsigned width, unsigned pos); |
15 | | |
16 | | void print_separator(void); |
17 | | |
18 | | int file_url_from_path(const char *path, char **ret); |
19 | | |
20 | | bool urlify_enabled(void); |
21 | | |
22 | | int terminal_urlify(const char *url, const char *text, char **ret); |
23 | | int terminal_urlify_path(const char *path, const char *text, char **ret); |
24 | | int terminal_urlify_man(const char *page, const char *section, char **ret); |
25 | | |
26 | | typedef enum CatFlags { |
27 | | CAT_CONFIG_OFF = 0, |
28 | | CAT_CONFIG_ON = 1 << 0, |
29 | | CAT_FORMAT_HAS_SECTIONS = 1 << 1, /* Sections are meaningful for this file format */ |
30 | | CAT_TLDR = 1 << 2, /* Only print comments and relevant section headers */ |
31 | | } CatFlags; |
32 | | |
33 | | int cat_files_full(const ConfFile *file, ConfFile * const *dropins, size_t n_dropins, CatFlags flags); |
34 | | int cat_files(const char *file, char **dropins, CatFlags flags); |
35 | | int conf_files_cat(const char *root, const char *name, CatFlags flags); |
36 | | |
37 | | #define RED_CROSS_MARK_MAX (STRLEN(ANSI_HIGHLIGHT_RED) + STRLEN("✗") + STRLEN(ANSI_NORMAL) + 1) |
38 | | #define GREEN_CHECK_MARK_MAX (STRLEN(ANSI_HIGHLIGHT_GREEN) + STRLEN("✓") + STRLEN(ANSI_NORMAL) + 1) |
39 | | |
40 | 0 | static inline const char* red_cross_mark_internal(char buffer[static RED_CROSS_MARK_MAX]) { |
41 | 0 | assert(buffer); |
42 | 0 | assert_se(stpcpy(stpcpy(stpcpy(buffer, ansi_highlight_red()), glyph(GLYPH_CROSS_MARK)), ansi_normal()) < buffer + RED_CROSS_MARK_MAX); |
43 | 0 | return buffer; |
44 | 0 | } Unexecuted instantiation: systemctl-compat-halt.c:red_cross_mark_internal Unexecuted instantiation: systemctl-compat-shutdown.c:red_cross_mark_internal Unexecuted instantiation: bootspec.c:red_cross_mark_internal Unexecuted instantiation: format-table.c:red_cross_mark_internal Unexecuted instantiation: help-util.c:red_cross_mark_internal Unexecuted instantiation: id128-print.c:red_cross_mark_internal Unexecuted instantiation: logs-show.c:red_cross_mark_internal Unexecuted instantiation: pretty-print.c:red_cross_mark_internal Unexecuted instantiation: prompt-util.c:red_cross_mark_internal Unexecuted instantiation: ptyfwd.c:red_cross_mark_internal Unexecuted instantiation: service-util.c:red_cross_mark_internal Unexecuted instantiation: user-record-show.c:red_cross_mark_internal Unexecuted instantiation: manager.c:red_cross_mark_internal |
45 | | |
46 | 0 | static inline const char* green_check_mark_internal(char buffer[static GREEN_CHECK_MARK_MAX]) { |
47 | 0 | assert(buffer); |
48 | 0 | assert_se(stpcpy(stpcpy(stpcpy(buffer, ansi_highlight_green()), glyph(GLYPH_CHECK_MARK)), ansi_normal()) < buffer + GREEN_CHECK_MARK_MAX); |
49 | 0 | return buffer; |
50 | 0 | } Unexecuted instantiation: systemctl-compat-halt.c:green_check_mark_internal Unexecuted instantiation: systemctl-compat-shutdown.c:green_check_mark_internal Unexecuted instantiation: bootspec.c:green_check_mark_internal Unexecuted instantiation: format-table.c:green_check_mark_internal Unexecuted instantiation: help-util.c:green_check_mark_internal Unexecuted instantiation: id128-print.c:green_check_mark_internal Unexecuted instantiation: logs-show.c:green_check_mark_internal Unexecuted instantiation: pretty-print.c:green_check_mark_internal Unexecuted instantiation: prompt-util.c:green_check_mark_internal Unexecuted instantiation: ptyfwd.c:green_check_mark_internal Unexecuted instantiation: service-util.c:green_check_mark_internal Unexecuted instantiation: user-record-show.c:green_check_mark_internal Unexecuted instantiation: manager.c:green_check_mark_internal |
51 | | |
52 | | #define RED_CROSS_MARK() red_cross_mark_internal((char[RED_CROSS_MARK_MAX]) {}) |
53 | | #define GREEN_CHECK_MARK() green_check_mark_internal((char[GREEN_CHECK_MARK_MAX]) {}) |
54 | | |
55 | | #define COLOR_MARK_BOOL(b) ((b) ? GREEN_CHECK_MARK() : RED_CROSS_MARK()) |
56 | | |
57 | | int terminal_tint_color(double hue, char **ret); |
58 | | |
59 | | bool shall_tint_background(void); |
60 | | |
61 | | void draw_progress_bar(const char *prefix, double percentage); |
62 | | int draw_progress_barf(double percentage, const char *prefixf, ...) _printf_(2, 3); |
63 | | void clear_progress_bar(const char *prefix); |
64 | | void draw_progress_bar_unbuffered(const char *prefix, double percentage); |
65 | | void clear_progress_bar_unbuffered(const char *prefix); |
66 | | |
67 | 0 | static inline FILE* enable_buffering(FILE *f, char *buffer, size_t size) { |
68 | 0 | assert(f); |
69 | 0 | assert(buffer); |
70 | 0 | assert(size > 0); |
71 | |
|
72 | 0 | if (setvbuf(f, buffer, _IOFBF, size) != 0) |
73 | 0 | return NULL; |
74 | | |
75 | 0 | return f; |
76 | 0 | } Unexecuted instantiation: systemctl-compat-halt.c:enable_buffering Unexecuted instantiation: systemctl-compat-shutdown.c:enable_buffering Unexecuted instantiation: bootspec.c:enable_buffering Unexecuted instantiation: format-table.c:enable_buffering Unexecuted instantiation: help-util.c:enable_buffering Unexecuted instantiation: id128-print.c:enable_buffering Unexecuted instantiation: logs-show.c:enable_buffering Unexecuted instantiation: pretty-print.c:enable_buffering Unexecuted instantiation: prompt-util.c:enable_buffering Unexecuted instantiation: ptyfwd.c:enable_buffering Unexecuted instantiation: service-util.c:enable_buffering Unexecuted instantiation: user-record-show.c:enable_buffering Unexecuted instantiation: manager.c:enable_buffering |
77 | | |
78 | 0 | static inline void fflush_and_disable_bufferingp(FILE **p) { |
79 | 0 | assert(p); |
80 | |
|
81 | 0 | if (*p) { |
82 | 0 | fflush(*p); |
83 | 0 | setvbuf(*p, NULL, _IONBF, 0); /* Disable buffering again. */ |
84 | 0 | } |
85 | 0 | } Unexecuted instantiation: systemctl-compat-halt.c:fflush_and_disable_bufferingp Unexecuted instantiation: systemctl-compat-shutdown.c:fflush_and_disable_bufferingp Unexecuted instantiation: bootspec.c:fflush_and_disable_bufferingp Unexecuted instantiation: format-table.c:fflush_and_disable_bufferingp Unexecuted instantiation: help-util.c:fflush_and_disable_bufferingp Unexecuted instantiation: id128-print.c:fflush_and_disable_bufferingp Unexecuted instantiation: logs-show.c:fflush_and_disable_bufferingp Unexecuted instantiation: pretty-print.c:fflush_and_disable_bufferingp Unexecuted instantiation: prompt-util.c:fflush_and_disable_bufferingp Unexecuted instantiation: ptyfwd.c:fflush_and_disable_bufferingp Unexecuted instantiation: service-util.c:fflush_and_disable_bufferingp Unexecuted instantiation: user-record-show.c:fflush_and_disable_bufferingp Unexecuted instantiation: manager.c:fflush_and_disable_bufferingp |
86 | | |
87 | | /* Even though the macro below is slightly generic, but it may not work most streams except for stderr, |
88 | | * as stdout is buffered and fopen() enables buffering by default. */ |
89 | | #define _WITH_BUFFERED_STREAM(f, size, p) \ |
90 | 0 | _unused_ _cleanup_(fflush_and_disable_bufferingp) FILE *p = \ |
91 | 0 | enable_buffering(f, (char[size]) {}, size) |
92 | | |
93 | | #define WITH_BUFFERED_STDERR \ |
94 | 0 | _WITH_BUFFERED_STREAM(stderr, LONG_LINE_MAX, UNIQ_T(p, UNIQ)) |
95 | | |
96 | | #define WITH_BUFFERED_STDOUT \ |
97 | 0 | _WITH_BUFFERED_STREAM(stdout, LONG_LINE_MAX, UNIQ_T(p, UNIQ)) |