]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/shared/id128-print.c
Split out pretty-print.c and move pager.c and main-func.h to shared/
[thirdparty/systemd.git] / src / shared / id128-print.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #include <stdio.h>
4
5 #include "sd-id128.h"
6
7 #include "alloc-util.h"
8 #include "id128-print.h"
9 #include "log.h"
10 #include "pretty-print.h"
11 #include "terminal-util.h"
12
13 int id128_pretty_print(sd_id128_t id, bool pretty) {
14 unsigned i;
15 _cleanup_free_ char *man_link = NULL, *mod_link = NULL;
16 const char *on, *off;
17
18 if (!pretty) {
19 printf(SD_ID128_FORMAT_STR "\n",
20 SD_ID128_FORMAT_VAL(id));
21 return 0;
22 }
23
24 on = ansi_highlight();
25 off = ansi_normal();
26
27 if (terminal_urlify("man:systemd-id128(1)", "systemd-id128(1)", &man_link) < 0)
28 return log_oom();
29
30 if (terminal_urlify("https://docs.python.org/3/library/uuid.html", "uuid", &mod_link) < 0)
31 return log_oom();
32
33 printf("As string:\n"
34 "%s" SD_ID128_FORMAT_STR "%s\n\n"
35 "As UUID:\n"
36 "%s%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x%s\n\n"
37 "As %s macro:\n"
38 "%s#define MESSAGE_XYZ SD_ID128_MAKE(",
39 on, SD_ID128_FORMAT_VAL(id), off,
40 on, SD_ID128_FORMAT_VAL(id), off,
41 man_link,
42 on);
43 for (i = 0; i < 16; i++)
44 printf("%02x%s", id.bytes[i], i != 15 ? "," : "");
45 printf(")%s\n\n", off);
46
47 printf("As Python constant:\n"
48 ">>> import %s\n"
49 ">>> %sMESSAGE_XYZ = uuid.UUID('" SD_ID128_FORMAT_STR "')%s\n",
50 mod_link,
51 on, SD_ID128_FORMAT_VAL(id), off);
52
53 return 0;
54 }
55
56 int id128_print_new(bool pretty) {
57 sd_id128_t id;
58 int r;
59
60 r = sd_id128_randomize(&id);
61 if (r < 0)
62 return log_error_errno(r, "Failed to generate ID: %m");
63
64 return id128_pretty_print(id, pretty);
65 }