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