zconf.l 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #define LKC_DIRECT_LINK
  14. #include "lkc.h"
  15. #include "zconf.tab.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. //printf("new config: ");
  70. //symbol_end(NULL);
  71. BEGIN(COMMAND);
  72. }
  73. <COMMAND>{
  74. "mainmenu" BEGIN(PARAM); return T_MAINMENU;
  75. "menu" BEGIN(PARAM); return T_MENU;
  76. "endmenu" BEGIN(PARAM); return T_ENDMENU;
  77. "source" BEGIN(PARAM); return T_SOURCE;
  78. "choice" BEGIN(PARAM); return T_CHOICE;
  79. "endchoice" BEGIN(PARAM); return T_ENDCHOICE;
  80. "comment" BEGIN(PARAM); return T_COMMENT;
  81. "config" BEGIN(PARAM); return T_CONFIG;
  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. "bool" BEGIN(PARAM); return T_BOOLEAN;
  92. "boolean" BEGIN(PARAM); return T_BOOLEAN;
  93. "int" BEGIN(PARAM); return T_INT;
  94. "hex" BEGIN(PARAM); return T_HEX;
  95. "string" BEGIN(PARAM); return T_STRING;
  96. {n}+ {
  97. alloc_string(yytext, yyleng);
  98. zconflval.string = text;
  99. return T_WORD;
  100. }
  101. .
  102. \n current_file->lineno++; BEGIN(INITIAL);
  103. }
  104. <PARAM>{
  105. "&&" return T_AND;
  106. "||" return T_OR;
  107. "(" return T_OPEN_PAREN;
  108. ")" return T_CLOSE_PAREN;
  109. "!" return T_NOT;
  110. "=" return T_EQUAL;
  111. "!=" return T_UNEQUAL;
  112. "if" return T_IF;
  113. "on" return T_ON;
  114. \"|\' {
  115. str = yytext[0];
  116. new_string();
  117. BEGIN(STRING);
  118. }
  119. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  120. --- /* ignore */
  121. ({n}|[-/.])+ {
  122. alloc_string(yytext, yyleng);
  123. zconflval.string = text;
  124. return T_WORD;
  125. }
  126. .
  127. }
  128. <STRING>{
  129. [^'"\n\\]+ {
  130. append_string(yytext, yyleng);
  131. }
  132. \'|\" {
  133. if (str == yytext[0]) {
  134. BEGIN(PARAM);
  135. zconflval.string = text;
  136. //printf("s:%s\n", text);
  137. return T_STRING;
  138. } else
  139. append_string(yytext, 1);
  140. }
  141. \\[ \t]*\n append_string(yytext+yyleng-1, 1); current_file->lineno++;
  142. \\[ \t]* append_string(yytext+1, yyleng-1);
  143. \\. append_string(yytext+1, 1);
  144. \n {
  145. //printf(":%d: open string!\n", current_file->lineno+1);
  146. exit(0);
  147. }
  148. <<EOF>> {
  149. //printf(":%d: open string!\n", current_file->lineno+1);
  150. exit(0);
  151. }
  152. }
  153. <HELP>{
  154. [ \t]+ {
  155. ts = 0;
  156. for (i = 0; i < yyleng; i++) {
  157. if (yytext[i] == '\t')
  158. ts = (ts & ~7) + 8;
  159. else
  160. ts++;
  161. }
  162. last_ts = ts;
  163. if (first_ts) {
  164. if (ts < first_ts) {
  165. zconf_endhelp();
  166. return T_HELPTEXT;
  167. }
  168. ts -= first_ts;
  169. while (ts > 8) {
  170. append_string(" ", 8);
  171. ts -= 8;
  172. }
  173. append_string(" ", ts);
  174. }
  175. }
  176. \n/[^ \t\n] {
  177. current_file->lineno++;
  178. zconf_endhelp();
  179. return T_HELPTEXT;
  180. }
  181. [ \t]*\n {
  182. current_file->lineno++;
  183. append_string("\n", 1);
  184. }
  185. [^ \t\n].* {
  186. append_string(yytext, yyleng);
  187. if (!first_ts)
  188. first_ts = last_ts;
  189. }
  190. <<EOF>> {
  191. zconf_endhelp();
  192. return T_HELPTEXT;
  193. }
  194. }
  195. <<EOF>> {
  196. if (current_buf) {
  197. zconf_endfile();
  198. return T_EOF;
  199. }
  200. yyterminate();
  201. }
  202. %%
  203. void zconf_starthelp(void)
  204. {
  205. new_string();
  206. last_ts = first_ts = 0;
  207. BEGIN(HELP);
  208. }
  209. static void zconf_endhelp(void)
  210. {
  211. zconflval.string = text;
  212. BEGIN(INITIAL);
  213. }
  214. void zconf_initscan(const char *name)
  215. {
  216. yyin = fopen(name, "r");
  217. if (!yyin) {
  218. printf("can't find file %s\n", name);
  219. exit(1);
  220. }
  221. //fprintf(stderr, "zconf_initscan: %s\n", name);
  222. current_buf = malloc(sizeof(*current_buf));
  223. memset(current_buf, 0, sizeof(*current_buf));
  224. current_file = file_lookup(name);
  225. current_file->lineno = 1;
  226. current_file->flags = FILE_BUSY;
  227. }
  228. void zconf_nextfile(const char *name)
  229. {
  230. struct file *file = file_lookup(name);
  231. struct buffer *buf = malloc(sizeof(*buf));
  232. memset(buf, 0, sizeof(*buf));
  233. current_buf->state = YY_CURRENT_BUFFER;
  234. yyin = fopen(name, "r");
  235. if (!yyin) {
  236. printf("%s:%d: can't open file \"%s\"\n", zconf_curname(), zconf_lineno(), name);
  237. exit(1);
  238. }
  239. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  240. buf->parent = current_buf;
  241. current_buf = buf;
  242. //fprintf(stderr, "zconf_nextfile: %s\n", name);
  243. if (file->flags & FILE_BUSY) {
  244. printf("recursive scan (%s)?\n", name);
  245. exit(1);
  246. }
  247. if (file->flags & FILE_SCANNED) {
  248. printf("file %s already scanned?\n", name);
  249. exit(1);
  250. }
  251. file->flags |= FILE_BUSY;
  252. file->lineno = 1;
  253. file->parent = current_file;
  254. current_file = file;
  255. }
  256. static struct buffer *zconf_endfile(void)
  257. {
  258. struct buffer *parent;
  259. current_file->flags |= FILE_SCANNED;
  260. current_file->flags &= ~FILE_BUSY;
  261. current_file = current_file->parent;
  262. parent = current_buf->parent;
  263. if (parent) {
  264. yy_delete_buffer(YY_CURRENT_BUFFER);
  265. yy_switch_to_buffer(parent->state);
  266. }
  267. free(current_buf);
  268. current_buf = parent;
  269. return parent;
  270. }
  271. int zconf_lineno(void)
  272. {
  273. if (current_buf)
  274. return current_file->lineno;
  275. else
  276. return 0;
  277. }
  278. char *zconf_curname(void)
  279. {
  280. if (current_buf)
  281. return current_file->name;
  282. else
  283. return "<none>";
  284. }