zconf.l 7.0 KB

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