zconf.l 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 <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. 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. "help" BEGIN(PARAM); return T_HELP;
  81. "if" BEGIN(PARAM); return T_IF;
  82. "endif" BEGIN(PARAM); return T_ENDIF;
  83. "depends" BEGIN(PARAM); return T_DEPENDS;
  84. "requires" BEGIN(PARAM); return T_REQUIRES;
  85. "optional" BEGIN(PARAM); return T_OPTIONAL;
  86. "default" BEGIN(PARAM); return T_DEFAULT;
  87. "prompt" BEGIN(PARAM); return T_PROMPT;
  88. "tristate" BEGIN(PARAM); return T_TRISTATE;
  89. "bool" BEGIN(PARAM); return T_BOOLEAN;
  90. "boolean" BEGIN(PARAM); return T_BOOLEAN;
  91. "int" BEGIN(PARAM); return T_INT;
  92. "hex" BEGIN(PARAM); return T_HEX;
  93. "string" BEGIN(PARAM); return T_STRING;
  94. {n}+ {
  95. alloc_string(yytext, yyleng);
  96. zconflval.string = text;
  97. return T_WORD;
  98. }
  99. .
  100. \n current_file->lineno++; BEGIN(INITIAL);
  101. }
  102. <PARAM>{
  103. "&&" return T_AND;
  104. "||" return T_OR;
  105. "(" return T_OPEN_PAREN;
  106. ")" return T_CLOSE_PAREN;
  107. "!" return T_NOT;
  108. "=" return T_EQUAL;
  109. "!=" return T_UNEQUAL;
  110. "if" return T_IF;
  111. "on" return T_ON;
  112. \"|\' {
  113. str = yytext[0];
  114. new_string();
  115. BEGIN(STRING);
  116. }
  117. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  118. --- /* ignore */
  119. ({n}|[-/.])+ {
  120. alloc_string(yytext, yyleng);
  121. zconflval.string = text;
  122. return T_WORD;
  123. }
  124. .
  125. <<EOF>> {
  126. BEGIN(INITIAL);
  127. }
  128. }
  129. <STRING>{
  130. [^'"\\\n]+/\n {
  131. append_string(yytext, yyleng);
  132. zconflval.string = text;
  133. return T_STRING;
  134. }
  135. [^'"\\\n]+ {
  136. append_string(yytext, yyleng);
  137. }
  138. \\.?/\n {
  139. append_string(yytext+1, yyleng);
  140. zconflval.string = text;
  141. return T_STRING;
  142. }
  143. \\.? {
  144. append_string(yytext+1, yyleng);
  145. }
  146. \'|\" {
  147. if (str == yytext[0]) {
  148. BEGIN(PARAM);
  149. zconflval.string = text;
  150. return T_STRING;
  151. } else
  152. append_string(yytext, 1);
  153. }
  154. \n {
  155. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  156. BEGIN(INITIAL);
  157. return T_EOL;
  158. }
  159. <<EOF>> {
  160. BEGIN(INITIAL);
  161. }
  162. }
  163. <HELP>{
  164. [ \t]+ {
  165. ts = 0;
  166. for (i = 0; i < yyleng; i++) {
  167. if (yytext[i] == '\t')
  168. ts = (ts & ~7) + 8;
  169. else
  170. ts++;
  171. }
  172. last_ts = ts;
  173. if (first_ts) {
  174. if (ts < first_ts) {
  175. zconf_endhelp();
  176. return T_HELPTEXT;
  177. }
  178. ts -= first_ts;
  179. while (ts > 8) {
  180. append_string(" ", 8);
  181. ts -= 8;
  182. }
  183. append_string(" ", ts);
  184. }
  185. }
  186. \n/[^ \t\n] {
  187. current_file->lineno++;
  188. zconf_endhelp();
  189. return T_HELPTEXT;
  190. }
  191. [ \t]*\n {
  192. current_file->lineno++;
  193. append_string("\n", 1);
  194. }
  195. [^ \t\n].* {
  196. append_string(yytext, yyleng);
  197. if (!first_ts)
  198. first_ts = last_ts;
  199. }
  200. <<EOF>> {
  201. zconf_endhelp();
  202. return T_HELPTEXT;
  203. }
  204. }
  205. <<EOF>> {
  206. if (current_buf) {
  207. zconf_endfile();
  208. return T_EOF;
  209. }
  210. fclose(yyin);
  211. yyterminate();
  212. }
  213. %%
  214. void zconf_starthelp(void)
  215. {
  216. new_string();
  217. last_ts = first_ts = 0;
  218. BEGIN(HELP);
  219. }
  220. static void zconf_endhelp(void)
  221. {
  222. zconflval.string = text;
  223. BEGIN(INITIAL);
  224. }
  225. void zconf_initscan(const char *name)
  226. {
  227. yyin = fopen(name, "r");
  228. if (!yyin) {
  229. printf("can't find file %s\n", name);
  230. exit(1);
  231. }
  232. current_buf = malloc(sizeof(*current_buf));
  233. memset(current_buf, 0, sizeof(*current_buf));
  234. current_file = file_lookup(name);
  235. current_file->lineno = 1;
  236. current_file->flags = FILE_BUSY;
  237. }
  238. void zconf_nextfile(const char *name)
  239. {
  240. struct file *file = file_lookup(name);
  241. struct buffer *buf = malloc(sizeof(*buf));
  242. memset(buf, 0, sizeof(*buf));
  243. current_buf->state = YY_CURRENT_BUFFER;
  244. yyin = fopen(name, "r");
  245. if (!yyin) {
  246. printf("%s:%d: can't open file \"%s\"\n", zconf_curname(), zconf_lineno(), name);
  247. exit(1);
  248. }
  249. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  250. buf->parent = current_buf;
  251. current_buf = buf;
  252. if (file->flags & FILE_BUSY) {
  253. printf("recursive scan (%s)?\n", name);
  254. exit(1);
  255. }
  256. if (file->flags & FILE_SCANNED) {
  257. printf("file %s already scanned?\n", name);
  258. exit(1);
  259. }
  260. file->flags |= FILE_BUSY;
  261. file->lineno = 1;
  262. file->parent = current_file;
  263. current_file = file;
  264. }
  265. static struct buffer *zconf_endfile(void)
  266. {
  267. struct buffer *parent;
  268. current_file->flags |= FILE_SCANNED;
  269. current_file->flags &= ~FILE_BUSY;
  270. current_file = current_file->parent;
  271. parent = current_buf->parent;
  272. if (parent) {
  273. fclose(yyin);
  274. yy_delete_buffer(YY_CURRENT_BUFFER);
  275. yy_switch_to_buffer(parent->state);
  276. }
  277. free(current_buf);
  278. current_buf = parent;
  279. return parent;
  280. }
  281. int zconf_lineno(void)
  282. {
  283. if (current_buf)
  284. return current_file->lineno;
  285. else
  286. return 0;
  287. }
  288. char *zconf_curname(void)
  289. {
  290. if (current_buf)
  291. return current_file->name;
  292. else
  293. return "<none>";
  294. }