summaryrefslogtreecommitdiff
path: root/scripts/kconfig/zconf.l
Unidiff
Diffstat (limited to 'scripts/kconfig/zconf.l') (more/less context) (show whitespace changes)
-rw-r--r--scripts/kconfig/zconf.l323
1 files changed, 323 insertions, 0 deletions
diff --git a/scripts/kconfig/zconf.l b/scripts/kconfig/zconf.l
new file mode 100644
index 0000000..6d81e5e
--- a/dev/null
+++ b/scripts/kconfig/zconf.l
@@ -0,0 +1,323 @@
1%option backup nostdinit noyywrap full ecs
2%option 8bit backup nodefault perf-report perf-report
3%x COMMAND HELP STRING PARAM
4%{
5/*
6 * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
7 * Released under the terms of the GNU GPL v2.0.
8 */
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <string.h>
13#include <unistd.h>
14
15#define LKC_DIRECT_LINK
16#include "lkc.h"
17#include "zconf.tab.h"
18
19 #define START_STRSIZE16
20
21char *text;
22static char *text_ptr;
23static int text_size, text_asize;
24
25struct buffer {
26 struct buffer *parent;
27 YY_BUFFER_STATE state;
28};
29
30struct buffer *current_buf;
31
32static int last_ts, first_ts;
33
34static void zconf_endhelp(void);
35static struct buffer *zconf_endfile(void);
36
37void new_string(void)
38{
39 text = malloc(START_STRSIZE);
40 text_asize = START_STRSIZE;
41 text_ptr = text;
42 text_size = 0;
43 *text_ptr = 0;
44}
45
46void append_string(const char *str, int size)
47{
48 int new_size = text_size + size + 1;
49 if (new_size > text_asize) {
50 text = realloc(text, new_size);
51 text_asize = new_size;
52 text_ptr = text + text_size;
53 }
54 memcpy(text_ptr, str, size);
55 text_ptr += size;
56 text_size += size;
57 *text_ptr = 0;
58}
59
60void alloc_string(const char *str, int size)
61{
62 text = malloc(size + 1);
63 memcpy(text, str, size);
64 text[size] = 0;
65}
66%}
67
68 ws[ \n\t]
69 n[A-Za-z0-9_]
70
71%%
72 int str = 0;
73 int ts, i;
74
75 [ \t]*#.*\ncurrent_file->lineno++;
76[ \t]*#.*
77
78 [ \t]*\ncurrent_file->lineno++; return T_EOL;
79
80 [ \t]+{
81 BEGIN(COMMAND);
82}
83
84 .{
85 unput(yytext[0]);
86 //printf("new config: ");
87 //symbol_end(NULL);
88 BEGIN(COMMAND);
89}
90
91
92<COMMAND>{
93 "mainmenu" BEGIN(PARAM); return T_MAINMENU;
94 "menu" BEGIN(PARAM); return T_MENU;
95 "endmenu" BEGIN(PARAM); return T_ENDMENU;
96 "source" BEGIN(PARAM); return T_SOURCE;
97 "choice" BEGIN(PARAM); return T_CHOICE;
98 "endchoice" BEGIN(PARAM); return T_ENDCHOICE;
99 "comment" BEGIN(PARAM); return T_COMMENT;
100 "config" BEGIN(PARAM); return T_CONFIG;
101 "help" BEGIN(PARAM); return T_HELP;
102 "if" BEGIN(PARAM); return T_IF;
103 "endif" BEGIN(PARAM); return T_ENDIF;
104 "depends" BEGIN(PARAM); return T_DEPENDS;
105 "requires" BEGIN(PARAM); return T_REQUIRES;
106 "optional" BEGIN(PARAM); return T_OPTIONAL;
107 "default" BEGIN(PARAM); return T_DEFAULT;
108 "prompt" BEGIN(PARAM); return T_PROMPT;
109 "tristate" BEGIN(PARAM); return T_TRISTATE;
110 "bool" BEGIN(PARAM); return T_BOOLEAN;
111 "boolean" BEGIN(PARAM); return T_BOOLEAN;
112 "int" BEGIN(PARAM); return T_INT;
113 "hex" BEGIN(PARAM); return T_HEX;
114 "string" BEGIN(PARAM); return T_STRING;
115 {n}+{
116 alloc_string(yytext, yyleng);
117 zconflval.string = text;
118 return T_WORD;
119 }
120 .
121 \ncurrent_file->lineno++; BEGIN(INITIAL);
122}
123
124<PARAM>{
125 "&&"return T_AND;
126 "||"return T_OR;
127 "("return T_OPEN_PAREN;
128 ")"return T_CLOSE_PAREN;
129 "!"return T_NOT;
130 "="return T_EQUAL;
131 "!="return T_UNEQUAL;
132 "if"return T_IF;
133 "on"return T_ON;
134 \"|\'{
135 str = yytext[0];
136 new_string();
137 BEGIN(STRING);
138 }
139 \nBEGIN(INITIAL); current_file->lineno++; return T_EOL;
140 ---/* ignore */
141 ({n}|[-/.])+{
142 alloc_string(yytext, yyleng);
143 zconflval.string = text;
144 return T_WORD;
145 }
146 .
147}
148
149<STRING>{
150 [^'"\n\\]+{
151 append_string(yytext, yyleng);
152 }
153 \'|\"{
154 if (str == yytext[0]) {
155 BEGIN(PARAM);
156 zconflval.string = text;
157 //printf("s:%s\n", text);
158 return T_STRING;
159 } else
160 append_string(yytext, 1);
161 }
162 \\[ \t]*\nappend_string(yytext+yyleng-1, 1); current_file->lineno++;
163 \\[ \t]*append_string(yytext+1, yyleng-1);
164 \\. append_string(yytext+1, 1);
165 \n{
166 //printf(":%d: open string!\n", current_file->lineno+1);
167 exit(0);
168 }
169 <<EOF>>{
170 //printf(":%d: open string!\n", current_file->lineno+1);
171 exit(0);
172 }
173}
174
175<HELP>{
176 [ \t]+{
177 ts = 0;
178 for (i = 0; i < yyleng; i++) {
179 if (yytext[i] == '\t')
180 ts = (ts & ~7) + 8;
181 else
182 ts++;
183 }
184 last_ts = ts;
185 if (first_ts) {
186 if (ts < first_ts) {
187 zconf_endhelp();
188 return T_HELPTEXT;
189 }
190 ts -= first_ts;
191 while (ts > 8) {
192 append_string(" ", 8);
193 ts -= 8;
194 }
195 append_string(" ", ts);
196 }
197
198 }
199 \n/[^ \t\n] {
200 current_file->lineno++;
201 zconf_endhelp();
202 return T_HELPTEXT;
203 }
204 [ \t]*\n{
205 current_file->lineno++;
206 append_string("\n", 1);
207 }
208 [^ \t\n].* {
209 append_string(yytext, yyleng);
210 if (!first_ts)
211 first_ts = last_ts;
212 }
213 <<EOF>>{
214 zconf_endhelp();
215 return T_HELPTEXT;
216 }
217}
218
219 <<EOF>>{
220 if (current_buf) {
221 zconf_endfile();
222 return T_EOF;
223 }
224 yyterminate();
225}
226
227%%
228void zconf_starthelp(void)
229{
230 new_string();
231 last_ts = first_ts = 0;
232 BEGIN(HELP);
233}
234
235static void zconf_endhelp(void)
236{
237 zconflval.string = text;
238 BEGIN(INITIAL);
239}
240
241void zconf_initscan(const char *name)
242{
243 yyin = fopen(name, "r");
244 if (!yyin) {
245 printf("can't find file %s\n", name);
246 exit(1);
247 }
248 //fprintf(stderr, "zconf_initscan: %s\n", name);
249
250 current_buf = malloc(sizeof(*current_buf));
251 memset(current_buf, 0, sizeof(*current_buf));
252
253 current_file = file_lookup(name);
254 current_file->lineno = 1;
255 current_file->flags = FILE_BUSY;
256}
257
258void zconf_nextfile(const char *name)
259{
260 struct file *file = file_lookup(name);
261 struct buffer *buf = malloc(sizeof(*buf));
262 memset(buf, 0, sizeof(*buf));
263
264 current_buf->state = YY_CURRENT_BUFFER;
265 yyin = fopen(name, "r");
266 if (!yyin) {
267 printf("%s:%d: can't open file \"%s\"\n", zconf_curname(), zconf_lineno(), name);
268 exit(1);
269 }
270 yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
271 buf->parent = current_buf;
272 current_buf = buf;
273
274 //fprintf(stderr, "zconf_nextfile: %s\n", name);
275
276 if (file->flags & FILE_BUSY) {
277 printf("recursive scan (%s)?\n", name);
278 exit(1);
279 }
280 if (file->flags & FILE_SCANNED) {
281 printf("file %s already scanned?\n", name);
282 exit(1);
283 }
284 file->flags |= FILE_BUSY;
285 file->lineno = 1;
286 file->parent = current_file;
287 current_file = file;
288}
289
290static struct buffer *zconf_endfile(void)
291{
292 struct buffer *parent;
293
294 current_file->flags |= FILE_SCANNED;
295 current_file->flags &= ~FILE_BUSY;
296 current_file = current_file->parent;
297
298 parent = current_buf->parent;
299 if (parent) {
300 yy_delete_buffer(YY_CURRENT_BUFFER);
301 yy_switch_to_buffer(parent->state);
302 }
303 free(current_buf);
304 current_buf = parent;
305
306 return parent;
307}
308
309int zconf_lineno(void)
310{
311 if (current_buf)
312 return current_file->lineno;
313 else
314 return 0;
315}
316
317char *zconf_curname(void)
318{
319 if (current_buf)
320 return current_file->name;
321 else
322 return "<none>";
323}