mb/razor/blade_stealth_kbl: Clean up dsdt.asl
[coreboot.git] / util / kconfig / toada.c
blob677aaca815b8dee392b84633aa26ab5eb62c433b
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
3 #include <ctype.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdbool.h>
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)
16 unsigned int hexlen;
18 printf(" %-46s : constant := 16#", name);
19 for (hexlen = strlen(val); hexlen > 0;) {
20 const unsigned int len = hexlen % 4 ? : 4;
21 char quad[] = "0000";
22 unsigned int i;
24 for (i = 0; i < len; ++i)
25 quad[4 - len + i] = val[i];
26 printf("%s", quad);
28 val += len;
29 hexlen -= len;
30 if (hexlen > 0)
31 printf("_");
33 printf ("#;\n");
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";
51 if (argc > 3) {
52 fprintf(stderr,
53 "Usage: %s [<package name> [<config prefix>]]\n\n",
54 argv[0]);
55 return 1;
57 if (argc > 2)
58 prefix = argv[2];
59 if (argc > 1)
60 package = argv[1];
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') {
74 fprintf(stderr,
75 "Line longer than %zu chars, skipping...\n",
76 sizeof(line) - 1);
77 while (fgets(line, sizeof(line), stdin)) {
78 if (line[strlen(line) - 1] == '\n')
79 break;
81 continue;
84 if (sscanf(line, unset_fmt, name) == 1) {
85 print_bool(name, false);
86 continue;
89 if (sscanf(line, string_fmt, name, val) == 2) {
90 print_string(name, val);
91 continue;
94 switch (sscanf(line, set_fmt, name, val)) {
95 case 1:
96 /* ignore for now, our Kconfig is full of these atm */
97 /* fprintf(stderr, "unset non-bool: %s=\n", name); */
98 continue;
99 case 2:
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);
108 } else {
109 fprintf(stderr,
110 "couldn't parse value '%s' for '%s'\n",
111 val, name);
113 continue;
114 default:
115 break;
118 unsigned int i = 0;
119 while (isspace(line[i]))
120 ++i;
121 if (line[i] == '#') {
122 printf(" --%s", line + i + 1);
123 continue;
124 } else if (i == strlen(line)) {
125 continue;
128 fprintf(stderr, "spurious line:\n%s", line);
131 printf("\nend %s;\n", package);
132 return 0;