1 /* This file is part of the coreboot project. */
2 /* SPDX-License-Identifier: GPL-2.0-or-later */
9 static void print_bool(const char *const name
, const bool val
)
11 printf(" %-46s : constant boolean := %s;\n",
12 name
, val
? "true" : "false");
15 static void print_hex(const char *const name
, const char *val
)
19 printf(" %-46s : constant := 16#", name
);
20 for (hexlen
= strlen(val
); hexlen
> 0;) {
21 const unsigned int len
= hexlen
% 4 ? : 4;
25 for (i
= 0; i
< len
; ++i
)
26 quad
[4 - len
+ i
] = val
[i
];
37 static void print_dec(const char *const name
, const char *const val
)
39 printf(" %-46s : constant := %s;\n", name
, val
);
42 static void print_string(const char *const name
, const char *const val
)
44 printf(" %-46s : constant string := \"%s\";\n", name
, val
);
47 int main(int argc
, char *argv
[])
49 char unset_fmt
[256], string_fmt
[256], set_fmt
[256], line
[256];
50 char *prefix
= "CONFIG", *package
= "KConfig";
54 "Usage: %s [<package name> [<config prefix>]]\n\n",
63 snprintf(set_fmt
, sizeof(set_fmt
), "%s_%%255[^=]=%%255s", prefix
);
64 snprintf(string_fmt
, sizeof(string_fmt
),
65 "%s_%%255[^=]=\"%%255[^\"]\"", prefix
);
66 snprintf(unset_fmt
, sizeof(unset_fmt
),
67 "# %s_%%255s is not set", prefix
);
69 printf("package %s is\n\n", package
);
71 while (fgets(line
, sizeof(line
), stdin
)) {
72 char name
[256], val
[256];
74 if (line
[strlen(line
) - 1] != '\n') {
76 "Line longer than %zu chars, skipping...\n",
78 while (fgets(line
, sizeof(line
), stdin
)) {
79 if (line
[strlen(line
) - 1] == '\n')
85 if (sscanf(line
, unset_fmt
, name
) == 1) {
86 print_bool(name
, false);
90 if (sscanf(line
, string_fmt
, name
, val
) == 2) {
91 print_string(name
, val
);
95 switch (sscanf(line
, set_fmt
, name
, val
)) {
97 /* ignore for now, our Kconfig is full of these atm */
98 /* fprintf(stderr, "unset non-bool: %s=\n", name); */
101 if (strcmp(val
, "\"\"") == 0) {
102 print_string(name
, "");
103 } else if (strcmp(val
, "y") == 0) {
104 print_bool(name
, true);
105 } else if (strncmp(val
, "0x", 2) == 0) {
106 print_hex(name
, val
+ 2);
107 } else if (isdigit(val
[0])) {
108 print_dec(name
, val
);
111 "couldn't parse value '%s' for '%s'\n",
120 while (isspace(line
[i
]))
122 if (line
[i
] == '#') {
123 printf(" --%s", line
+ i
+ 1);
125 } else if (i
== strlen(line
)) {
129 fprintf(stderr
, "spurious line:\n%s", line
);
132 printf("\nend %s;\n", package
);