blob: 9a11b0449867e588672eba924713c118836648ea [file] [log] [blame]
Nico Huber533bc0a2019-01-01 19:03:33 +01001/*
2 * This file is part of the coreboot project.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <ctype.h>
16#include <stdio.h>
17#include <string.h>
18#include <stdbool.h>
19
20static void print_bool(const char *const name, const bool val)
21{
22 printf(" %-46s : constant boolean := %s;\n",
23 name, val ? "true" : "false");
24}
25
26static void print_hex(const char *const name, const char *val)
27{
28 unsigned int hexlen;
29
30 printf(" %-46s : constant := 16#", name);
31 for (hexlen = strlen(val); hexlen > 0;) {
32 const unsigned int len = hexlen % 4 ? : 4;
33 char quad[] = "0000";
34 unsigned int i;
35
36 for (i = 0; i < len; ++i)
37 quad[4 - len + i] = val[i];
38 printf("%s", quad);
39
40 val += len;
41 hexlen -= len;
42 if (hexlen > 0)
43 printf("_");
44 }
45 printf ("#;\n");
46}
47
48static void print_dec(const char *const name, const char *const val)
49{
50 printf(" %-46s : constant := %s;\n", name, val);
51}
52
53static void print_string(const char *const name, const char *const val)
54{
55 printf(" %-46s : constant string := \"%s\";\n", name, val);
56}
57
58int main(int argc, char *argv[])
59{
60 char unset_fmt[256], string_fmt[256], set_fmt[256], line[256];
61 char *prefix = "CONFIG", *package = "KConfig";
62
63 if (argc > 3) {
64 fprintf(stderr,
65 "Usage: %s [<package name> [<config prefix>]]\n\n",
66 argv[0]);
67 return 1;
68 }
69 if (argc > 2)
70 prefix = argv[2];
71 if (argc > 1)
72 package = argv[1];
73
74 snprintf(set_fmt, sizeof(set_fmt), "%s_%%255[^=]=%%255s", prefix);
75 snprintf(string_fmt, sizeof(string_fmt),
76 "%s_%%255[^=]=\"%%255[^\"]\"", prefix);
77 snprintf(unset_fmt, sizeof(unset_fmt),
78 "# %s_%%255s is not set", prefix);
79
80 printf("package %s is\n\n", package);
81
82 while (fgets(line, sizeof(line), stdin)) {
83 char name[256], val[256];
84
85 if (line[strlen(line) - 1] != '\n') {
86 fprintf(stderr,
87 "Line longer than %zu chars, skipping...\n",
88 sizeof(line) - 1);
89 while (fgets(line, sizeof(line), stdin)) {
90 if (line[strlen(line) - 1] == '\n')
91 break;
92 }
93 continue;
94 }
95
96 if (sscanf(line, unset_fmt, name) == 1) {
97 print_bool(name, false);
98 continue;
99 }
100
101 if (sscanf(line, string_fmt, name, val) == 2) {
102 print_string(name, val);
103 continue;
104 }
105
106 switch (sscanf(line, set_fmt, name, val)) {
107 case 1:
108 /* ignore for now, our Kconfig is full of these atm */
109 /* fprintf(stderr, "unset non-bool: %s=\n", name); */
110 continue;
111 case 2:
112 if (strcmp(val, "\"\"") == 0) {
113 print_string(name, "");
114 } else if (strcmp(val, "y") == 0) {
115 print_bool(name, true);
116 } else if (strncmp(val, "0x", 2) == 0) {
117 print_hex(name, val + 2);
118 } else if (isdigit(val[0])) {
119 print_dec(name, val);
120 } else {
121 fprintf(stderr,
122 "couldn't parse value '%s' for '%s'\n",
123 val, name);
124 }
125 continue;
126 default:
127 break;
128 }
129
130 unsigned int i = 0;
131 while (isspace(line[i]))
132 ++i;
133 if (line[i] == '#') {
134 printf(" --%s", line + i + 1);
135 continue;
136 } else if (i == strlen(line)) {
137 continue;
138 }
139
140 fprintf(stderr, "spurious line:\n%s", line);
141 }
142
143 printf("\nend %s;\n", package);
144 return 0;
145}