1 /* SPDX-License-Identifier: GPL-2.0-or-later */
8 static void print_bool(const char *const name
, const bool val
)
10 printf(" %-46s : constant boolean := %s;\n",
11 name
, val
? "true" : "false");
14 static void print_hex(const char *const name
, const char *val
)
18 printf(" %-46s : constant := 16#", name
);
19 for (hexlen
= strlen(val
); hexlen
> 0;) {
20 const unsigned int len
= hexlen
% 4 ? : 4;
24 for (i
= 0; i
< len
; ++i
)
25 quad
[4 - len
+ i
] = val
[i
];
36 static void print_dec(const char *const name
, const char *const val
)
38 printf(" %-46s : constant := %s;\n", name
, val
);
41 static void print_string(const char *const name
, const char *const val
)
43 printf(" %-46s : constant string := \"%s\";\n", name
, val
);
46 int main(int argc
, char *argv
[])
48 char unset_fmt
[256], string_fmt
[256], set_fmt
[256], line
[256];
49 char *prefix
= "CONFIG", *package
= "KConfig";
53 "Usage: %s [<package name> [<config prefix>]]\n\n",
62 snprintf(set_fmt
, sizeof(set_fmt
), "%s_%%255[^=]=%%255s", prefix
);
63 snprintf(string_fmt
, sizeof(string_fmt
),
64 "%s_%%255[^=]=\"%%255[^\"]\"", prefix
);
65 snprintf(unset_fmt
, sizeof(unset_fmt
),
66 "# %s_%%255s is not set", prefix
);
68 printf("package %s is\n\n", package
);
70 while (fgets(line
, sizeof(line
), stdin
)) {
71 char name
[256], val
[256];
73 if (line
[strlen(line
) - 1] != '\n') {
75 "Line longer than %zu chars, skipping...\n",
77 while (fgets(line
, sizeof(line
), stdin
)) {
78 if (line
[strlen(line
) - 1] == '\n')
84 if (sscanf(line
, unset_fmt
, name
) == 1) {
85 print_bool(name
, false);
89 if (sscanf(line
, string_fmt
, name
, val
) == 2) {
90 print_string(name
, val
);
94 switch (sscanf(line
, set_fmt
, name
, val
)) {
96 /* ignore for now, our Kconfig is full of these atm */
97 /* fprintf(stderr, "unset non-bool: %s=\n", name); */
100 if (strcmp(val
, "\"\"") == 0) {
101 print_string(name
, "");
102 } else if (strcmp(val
, "y") == 0) {
103 print_bool(name
, true);
104 } else if (strncmp(val
, "0x", 2) == 0) {
105 print_hex(name
, val
+ 2);
106 } else if (isdigit(val
[0])) {
107 print_dec(name
, val
);
110 "couldn't parse value '%s' for '%s'\n",
119 while (isspace(line
[i
]))
121 if (line
[i
] == '#') {
122 printf(" --%s", line
+ i
+ 1);
124 } else if (i
== strlen(line
)) {
128 fprintf(stderr
, "spurious line:\n%s", line
);
131 printf("\nend %s;\n", package
);