3
0

zconf.l 6.1 KB

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