zconf.l 6.0 KB

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