1
0

zconf.l 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. %option backup nostdinit noyywrap never-interactive 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. #include <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <glob.h>
  15. #define LKC_DIRECT_LINK
  16. #include "lkc.h"
  17. #define START_STRSIZE 16
  18. char *text;
  19. static char *text_ptr;
  20. static int text_size, text_asize;
  21. struct buffer {
  22. struct buffer *parent;
  23. YY_BUFFER_STATE state;
  24. };
  25. struct buffer *current_buf;
  26. static int last_ts, first_ts;
  27. static void zconf_endhelp(void);
  28. static struct buffer *zconf_endfile(void);
  29. void new_string(void)
  30. {
  31. text = malloc(START_STRSIZE);
  32. text_asize = START_STRSIZE;
  33. text_ptr = text;
  34. text_size = 0;
  35. *text_ptr = 0;
  36. }
  37. void append_string(const char *str, int size)
  38. {
  39. int new_size = text_size + size + 1;
  40. if (new_size > text_asize) {
  41. text = realloc(text, new_size);
  42. text_asize = new_size;
  43. text_ptr = text + text_size;
  44. }
  45. memcpy(text_ptr, str, size);
  46. text_ptr += size;
  47. text_size += size;
  48. *text_ptr = 0;
  49. }
  50. void alloc_string(const char *str, int size)
  51. {
  52. text = malloc(size + 1);
  53. memcpy(text, str, size);
  54. text[size] = 0;
  55. }
  56. %}
  57. ws [ \n\t]
  58. n [A-Za-z0-9_]
  59. %%
  60. int str = 0;
  61. int ts, i;
  62. [ \t]*#.*\n current_file->lineno++;
  63. [ \t]*#.*
  64. [ \t]*\n current_file->lineno++; return T_EOL;
  65. [ \t]+ {
  66. BEGIN(COMMAND);
  67. }
  68. . {
  69. unput(yytext[0]);
  70. BEGIN(COMMAND);
  71. }
  72. <COMMAND>{
  73. "mainmenu" BEGIN(PARAM); return T_MAINMENU;
  74. "menu" BEGIN(PARAM); return T_MENU;
  75. "endmenu" BEGIN(PARAM); return T_ENDMENU;
  76. "source" BEGIN(PARAM); return T_SOURCE;
  77. "choice" BEGIN(PARAM); return T_CHOICE;
  78. "endchoice" BEGIN(PARAM); return T_ENDCHOICE;
  79. "comment" BEGIN(PARAM); return T_COMMENT;
  80. "config" BEGIN(PARAM); return T_CONFIG;
  81. "menuconfig" BEGIN(PARAM); return T_MENUCONFIG;
  82. "help" BEGIN(PARAM); return T_HELP;
  83. "if" BEGIN(PARAM); return T_IF;
  84. "endif" BEGIN(PARAM); return T_ENDIF;
  85. "depends" BEGIN(PARAM); return T_DEPENDS;
  86. "requires" BEGIN(PARAM); return T_REQUIRES;
  87. "optional" BEGIN(PARAM); return T_OPTIONAL;
  88. "default" BEGIN(PARAM); return T_DEFAULT;
  89. "prompt" BEGIN(PARAM); return T_PROMPT;
  90. "tristate" BEGIN(PARAM); return T_TRISTATE;
  91. "def_tristate" BEGIN(PARAM); return T_DEF_TRISTATE;
  92. "bool" BEGIN(PARAM); return T_BOOLEAN;
  93. "boolean" BEGIN(PARAM); return T_BOOLEAN;
  94. "def_bool" BEGIN(PARAM); return T_DEF_BOOLEAN;
  95. "def_boolean" BEGIN(PARAM); return T_DEF_BOOLEAN;
  96. "int" BEGIN(PARAM); return T_INT;
  97. "hex" BEGIN(PARAM); return T_HEX;
  98. "string" BEGIN(PARAM); return T_STRING;
  99. "select" BEGIN(PARAM); return T_SELECT;
  100. "enable" BEGIN(PARAM); return T_SELECT;
  101. "range" BEGIN(PARAM); return T_RANGE;
  102. {n}+ {
  103. alloc_string(yytext, yyleng);
  104. zconflval.string = text;
  105. return T_WORD;
  106. }
  107. .
  108. \n current_file->lineno++; BEGIN(INITIAL);
  109. }
  110. <PARAM>{
  111. "&&" return T_AND;
  112. "||" return T_OR;
  113. "(" return T_OPEN_PAREN;
  114. ")" return T_CLOSE_PAREN;
  115. "!" return T_NOT;
  116. "=" return T_EQUAL;
  117. "!=" return T_UNEQUAL;
  118. "if" return T_IF;
  119. "on" return T_ON;
  120. \"|\' {
  121. str = yytext[0];
  122. new_string();
  123. BEGIN(STRING);
  124. }
  125. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  126. --- /* ignore */
  127. ({n}|[-/.])+ {
  128. alloc_string(yytext, yyleng);
  129. zconflval.string = text;
  130. return T_WORD;
  131. }
  132. #.* /* comment */
  133. \\\n current_file->lineno++;
  134. .
  135. <<EOF>> {
  136. BEGIN(INITIAL);
  137. }
  138. }
  139. <STRING>{
  140. [^'"\\\n]+/\n {
  141. append_string(yytext, yyleng);
  142. zconflval.string = text;
  143. return T_WORD_QUOTE;
  144. }
  145. [^'"\\\n]+ {
  146. append_string(yytext, yyleng);
  147. }
  148. \\.?/\n {
  149. append_string(yytext + 1, yyleng - 1);
  150. zconflval.string = text;
  151. return T_WORD_QUOTE;
  152. }
  153. \\.? {
  154. append_string(yytext + 1, yyleng - 1);
  155. }
  156. \'|\" {
  157. if (str == yytext[0]) {
  158. BEGIN(PARAM);
  159. zconflval.string = text;
  160. return T_WORD_QUOTE;
  161. } else
  162. append_string(yytext, 1);
  163. }
  164. \n {
  165. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  166. current_file->lineno++;
  167. BEGIN(INITIAL);
  168. return T_EOL;
  169. }
  170. <<EOF>> {
  171. BEGIN(INITIAL);
  172. }
  173. }
  174. <HELP>{
  175. [ \t]+ {
  176. ts = 0;
  177. for (i = 0; i < yyleng; i++) {
  178. if (yytext[i] == '\t')
  179. ts = (ts & ~7) + 8;
  180. else
  181. ts++;
  182. }
  183. last_ts = ts;
  184. if (first_ts) {
  185. if (ts < first_ts) {
  186. zconf_endhelp();
  187. return T_HELPTEXT;
  188. }
  189. ts -= first_ts;
  190. while (ts > 8) {
  191. append_string(" ", 8);
  192. ts -= 8;
  193. }
  194. append_string(" ", ts);
  195. }
  196. }
  197. [ \t]*\n/[^ \t\n] {
  198. current_file->lineno++;
  199. zconf_endhelp();
  200. return T_HELPTEXT;
  201. }
  202. [ \t]*\n {
  203. current_file->lineno++;
  204. append_string("\n", 1);
  205. }
  206. [^ \t\n].* {
  207. append_string(yytext, yyleng);
  208. if (!first_ts)
  209. first_ts = last_ts;
  210. }
  211. <<EOF>> {
  212. zconf_endhelp();
  213. return T_HELPTEXT;
  214. }
  215. }
  216. <<EOF>> {
  217. if (current_buf) {
  218. zconf_endfile();
  219. return T_EOF;
  220. }
  221. fclose(yyin);
  222. yyterminate();
  223. }
  224. %%
  225. void zconf_starthelp(void)
  226. {
  227. new_string();
  228. last_ts = first_ts = 0;
  229. BEGIN(HELP);
  230. }
  231. static void zconf_endhelp(void)
  232. {
  233. zconflval.string = text;
  234. BEGIN(INITIAL);
  235. }
  236. /*
  237. * Try to open specified file with following names:
  238. * ./name
  239. * $(srctree)/name
  240. * The latter is used when srctree is separate from objtree
  241. * when compiling the kernel.
  242. * Return NULL if file is not found.
  243. */
  244. FILE *zconf_fopen(const char *name)
  245. {
  246. char *env, fullname[PATH_MAX+1];
  247. FILE *f;
  248. f = fopen(name, "r");
  249. if (!f && name[0] != '/') {
  250. env = getenv(SRCTREE);
  251. if (env) {
  252. sprintf(fullname, "%s/%s", env, name);
  253. f = fopen(fullname, "r");
  254. }
  255. }
  256. return f;
  257. }
  258. void zconf_initscan(const char *name)
  259. {
  260. yyin = zconf_fopen(name);
  261. if (!yyin) {
  262. printf("can't find file %s\n", name);
  263. exit(1);
  264. }
  265. current_buf = malloc(sizeof(*current_buf));
  266. memset(current_buf, 0, sizeof(*current_buf));
  267. current_file = file_lookup(name);
  268. current_file->lineno = 1;
  269. current_file->flags = FILE_BUSY;
  270. }
  271. void zconf_nextfile(const char *name)
  272. {
  273. size_t i;
  274. int retval;
  275. glob_t files;
  276. char *filename;
  277. struct file *file;
  278. struct buffer *buf;
  279. retval = glob(name, GLOB_ERR | GLOB_MARK, NULL, &files);
  280. if (retval == GLOB_NOSPACE || retval == GLOB_ABORTED || retval == GLOB_NOMATCH) {
  281. printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
  282. retval == GLOB_NOSPACE ? "failed to allocate memory" :
  283. retval == GLOB_ABORTED ? "read error" : "no match",
  284. name);
  285. exit(1);
  286. }
  287. for (i = files.gl_pathc-1; i != (size_t)-1; --i) {
  288. filename = files.gl_pathv[i];
  289. file = file_lookup(filename);
  290. buf = malloc(sizeof(*buf));
  291. memset(buf, 0, sizeof(*buf));
  292. current_buf->state = YY_CURRENT_BUFFER;
  293. zconfin = zconf_fopen(filename);
  294. if (!zconfin) {
  295. printf("%s:%d: can't open file \"%s\"\n",
  296. zconf_curname(), zconf_lineno(), filename);
  297. exit(1);
  298. }
  299. zconf_switch_to_buffer(zconf_create_buffer(zconfin,YY_BUF_SIZE));
  300. buf->parent = current_buf;
  301. current_buf = buf;
  302. if (file->flags & FILE_BUSY) {
  303. printf("recursive scan (%s)?\n", filename);
  304. exit(1);
  305. }
  306. if (file->flags & FILE_SCANNED) {
  307. printf("file %s already scanned?\n", filename);
  308. exit(1);
  309. }
  310. file->flags |= FILE_BUSY;
  311. file->lineno = 1;
  312. file->parent = current_file;
  313. current_file = file;
  314. }
  315. }
  316. static struct buffer *zconf_endfile(void)
  317. {
  318. struct buffer *parent;
  319. current_file->flags |= FILE_SCANNED;
  320. current_file->flags &= ~FILE_BUSY;
  321. current_file = current_file->parent;
  322. parent = current_buf->parent;
  323. if (parent) {
  324. fclose(yyin);
  325. yy_delete_buffer(YY_CURRENT_BUFFER);
  326. yy_switch_to_buffer(parent->state);
  327. }
  328. free(current_buf);
  329. current_buf = parent;
  330. return parent;
  331. }
  332. int zconf_lineno(void)
  333. {
  334. if (current_buf)
  335. return current_file->lineno - 1;
  336. else
  337. return 0;
  338. }
  339. char *zconf_curname(void)
  340. {
  341. if (current_buf)
  342. return current_file->name;
  343. else
  344. return "<none>";
  345. }