lexer.l 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  4. */
  5. %option nostdinit noyywrap never-interactive full ecs
  6. %option 8bit nodefault yylineno
  7. %x ASSIGN_VAL HELP STRING
  8. %{
  9. #include <assert.h>
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <glob.h>
  15. #include <libgen.h>
  16. #include "lkc.h"
  17. #include "parser.tab.h"
  18. #define YY_DECL static int yylex1(void)
  19. #define START_STRSIZE 16
  20. static struct {
  21. struct file *file;
  22. int lineno;
  23. } current_pos;
  24. static int prev_prev_token = T_EOL;
  25. static int prev_token = T_EOL;
  26. static char *text;
  27. static int text_size, text_asize;
  28. struct buffer {
  29. struct buffer *parent;
  30. YY_BUFFER_STATE state;
  31. };
  32. static struct buffer *current_buf;
  33. static int last_ts, first_ts;
  34. static char *expand_token(const char *in, size_t n);
  35. static void append_expanded_string(const char *in);
  36. static void zconf_endhelp(void);
  37. static void zconf_endfile(void);
  38. static void new_string(void)
  39. {
  40. text = xmalloc(START_STRSIZE);
  41. text_asize = START_STRSIZE;
  42. text_size = 0;
  43. *text = 0;
  44. }
  45. static void append_string(const char *str, int size)
  46. {
  47. int new_size = text_size + size + 1;
  48. if (new_size > text_asize) {
  49. new_size += START_STRSIZE - 1;
  50. new_size &= -START_STRSIZE;
  51. text = xrealloc(text, new_size);
  52. text_asize = new_size;
  53. }
  54. memcpy(text + text_size, str, size);
  55. text_size += size;
  56. text[text_size] = 0;
  57. }
  58. static void alloc_string(const char *str, int size)
  59. {
  60. text = xmalloc(size + 1);
  61. memcpy(text, str, size);
  62. text[size] = 0;
  63. }
  64. static void warn_ignored_character(char chr)
  65. {
  66. fprintf(stderr,
  67. "%s:%d:warning: ignoring unsupported character '%c'\n",
  68. current_file->name, yylineno, chr);
  69. }
  70. %}
  71. n [A-Za-z0-9_-]
  72. %%
  73. int str = 0;
  74. int ts, i;
  75. #.* /* ignore comment */
  76. [ \t]* /* whitespaces */
  77. \\\n /* escaped new line */
  78. \n return T_EOL;
  79. "bool" return T_BOOL;
  80. "choice" return T_CHOICE;
  81. "comment" return T_COMMENT;
  82. "config" return T_CONFIG;
  83. "def_bool" return T_DEF_BOOL;
  84. "def_tristate" return T_DEF_TRISTATE;
  85. "default" return T_DEFAULT;
  86. "depends" return T_DEPENDS;
  87. "endchoice" return T_ENDCHOICE;
  88. "endif" return T_ENDIF;
  89. "endmenu" return T_ENDMENU;
  90. "help" return T_HELP;
  91. "hex" return T_HEX;
  92. "if" return T_IF;
  93. "imply" return T_IMPLY;
  94. "int" return T_INT;
  95. "mainmenu" return T_MAINMENU;
  96. "menu" return T_MENU;
  97. "menuconfig" return T_MENUCONFIG;
  98. "modules" return T_MODULES;
  99. "on" return T_ON;
  100. "optional" return T_OPTIONAL;
  101. "prompt" return T_PROMPT;
  102. "range" return T_RANGE;
  103. "reset" return T_RESET;
  104. "select" return T_SELECT;
  105. "source" return T_SOURCE;
  106. "string" return T_STRING;
  107. "tristate" return T_TRISTATE;
  108. "visible" return T_VISIBLE;
  109. "||" return T_OR;
  110. "&&" return T_AND;
  111. "=" return T_EQUAL;
  112. "!=" return T_UNEQUAL;
  113. "<" return T_LESS;
  114. "<=" return T_LESS_EQUAL;
  115. ">" return T_GREATER;
  116. ">=" return T_GREATER_EQUAL;
  117. "!" return T_NOT;
  118. "(" return T_OPEN_PAREN;
  119. ")" return T_CLOSE_PAREN;
  120. ":=" return T_COLON_EQUAL;
  121. "+=" return T_PLUS_EQUAL;
  122. \"|\' {
  123. str = yytext[0];
  124. new_string();
  125. BEGIN(STRING);
  126. }
  127. ({n}|[/.])+ {
  128. alloc_string(yytext, yyleng);
  129. yylval.string = text;
  130. return T_WORD;
  131. }
  132. ({n}|[/.$])+ {
  133. /* this token includes at least one '$' */
  134. yylval.string = expand_token(yytext, yyleng);
  135. if (strlen(yylval.string))
  136. return T_WORD;
  137. free(yylval.string);
  138. }
  139. . warn_ignored_character(*yytext);
  140. <ASSIGN_VAL>{
  141. [^[:blank:]\n]+.* {
  142. alloc_string(yytext, yyleng);
  143. yylval.string = text;
  144. return T_ASSIGN_VAL;
  145. }
  146. \n { BEGIN(INITIAL); return T_EOL; }
  147. .
  148. }
  149. <STRING>{
  150. "$".* append_expanded_string(yytext);
  151. [^$'"\\\n]+ {
  152. append_string(yytext, yyleng);
  153. }
  154. \\.? {
  155. append_string(yytext + 1, yyleng - 1);
  156. }
  157. \'|\" {
  158. if (str == yytext[0]) {
  159. BEGIN(INITIAL);
  160. yylval.string = text;
  161. return T_WORD_QUOTE;
  162. } else
  163. append_string(yytext, 1);
  164. }
  165. \n {
  166. fprintf(stderr,
  167. "%s:%d:warning: multi-line strings not supported\n",
  168. zconf_curname(), zconf_lineno());
  169. unput('\n');
  170. BEGIN(INITIAL);
  171. yylval.string = text;
  172. return T_WORD_QUOTE;
  173. }
  174. <<EOF>> {
  175. BEGIN(INITIAL);
  176. yylval.string = text;
  177. return T_WORD_QUOTE;
  178. }
  179. }
  180. <HELP>{
  181. [ \t]+ {
  182. ts = 0;
  183. for (i = 0; i < yyleng; i++) {
  184. if (yytext[i] == '\t')
  185. ts = (ts & ~7) + 8;
  186. else
  187. ts++;
  188. }
  189. last_ts = ts;
  190. if (first_ts) {
  191. if (ts < first_ts) {
  192. zconf_endhelp();
  193. return T_HELPTEXT;
  194. }
  195. ts -= first_ts;
  196. while (ts > 8) {
  197. append_string(" ", 8);
  198. ts -= 8;
  199. }
  200. append_string(" ", ts);
  201. }
  202. }
  203. [ \t]*\n/[^ \t\n] {
  204. zconf_endhelp();
  205. return T_HELPTEXT;
  206. }
  207. [ \t]*\n {
  208. append_string("\n", 1);
  209. }
  210. [^ \t\n].* {
  211. while (yyleng) {
  212. if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
  213. break;
  214. yyleng--;
  215. }
  216. append_string(yytext, yyleng);
  217. if (!first_ts)
  218. first_ts = last_ts;
  219. }
  220. <<EOF>> {
  221. zconf_endhelp();
  222. return T_HELPTEXT;
  223. }
  224. }
  225. <<EOF>> {
  226. BEGIN(INITIAL);
  227. if (prev_token != T_EOL && prev_token != T_HELPTEXT)
  228. fprintf(stderr, "%s:%d:warning: no new line at end of file\n",
  229. current_file->name, yylineno);
  230. if (current_file) {
  231. zconf_endfile();
  232. return T_EOL;
  233. }
  234. fclose(yyin);
  235. yyterminate();
  236. }
  237. %%
  238. /* second stage lexer */
  239. int yylex(void)
  240. {
  241. int token;
  242. repeat:
  243. token = yylex1();
  244. if (prev_token == T_EOL || prev_token == T_HELPTEXT) {
  245. if (token == T_EOL) {
  246. /* Do not pass unneeded T_EOL to the parser. */
  247. goto repeat;
  248. } else {
  249. /*
  250. * For the parser, update file/lineno at the first token
  251. * of each statement. Generally, \n is a statement
  252. * terminator in Kconfig, but it is not always true
  253. * because \n could be escaped by a backslash.
  254. */
  255. current_pos.file = current_file;
  256. current_pos.lineno = yylineno;
  257. }
  258. }
  259. if (prev_prev_token == T_EOL && prev_token == T_WORD &&
  260. (token == T_EQUAL || token == T_COLON_EQUAL || token == T_PLUS_EQUAL))
  261. BEGIN(ASSIGN_VAL);
  262. prev_prev_token = prev_token;
  263. prev_token = token;
  264. return token;
  265. }
  266. static char *expand_token(const char *in, size_t n)
  267. {
  268. char *out;
  269. int c;
  270. char c2;
  271. const char *rest, *end;
  272. new_string();
  273. append_string(in, n);
  274. /* get the whole line because we do not know the end of token. */
  275. while ((c = input()) != EOF) {
  276. if (c == '\n') {
  277. unput(c);
  278. break;
  279. }
  280. c2 = c;
  281. append_string(&c2, 1);
  282. }
  283. rest = text;
  284. out = expand_one_token(&rest);
  285. /* push back unused characters to the input stream */
  286. end = rest + strlen(rest);
  287. while (end > rest)
  288. unput(*--end);
  289. free(text);
  290. return out;
  291. }
  292. static void append_expanded_string(const char *str)
  293. {
  294. const char *end;
  295. char *res;
  296. str++;
  297. res = expand_dollar(&str);
  298. /* push back unused characters to the input stream */
  299. end = str + strlen(str);
  300. while (end > str)
  301. unput(*--end);
  302. append_string(res, strlen(res));
  303. free(res);
  304. }
  305. void zconf_starthelp(void)
  306. {
  307. new_string();
  308. last_ts = first_ts = 0;
  309. BEGIN(HELP);
  310. }
  311. static void zconf_endhelp(void)
  312. {
  313. yylval.string = text;
  314. BEGIN(INITIAL);
  315. }
  316. /*
  317. * Try to open specified file with following names:
  318. * ./name
  319. * $(srctree)/name
  320. * The latter is used when srctree is separate from objtree
  321. * when compiling the kernel.
  322. * Return NULL if file is not found.
  323. */
  324. FILE *zconf_fopen(const char *name)
  325. {
  326. char *env, fullname[PATH_MAX+1];
  327. FILE *f;
  328. f = fopen(name, "r");
  329. if (!f && name != NULL && name[0] != '/') {
  330. env = getenv(SRCTREE);
  331. if (env) {
  332. snprintf(fullname, sizeof(fullname),
  333. "%s/%s", env, name);
  334. f = fopen(fullname, "r");
  335. }
  336. }
  337. return f;
  338. }
  339. void zconf_initscan(const char *name)
  340. {
  341. yyin = zconf_fopen(name);
  342. if (!yyin) {
  343. fprintf(stderr, "can't find file %s\n", name);
  344. exit(1);
  345. }
  346. current_buf = xmalloc(sizeof(*current_buf));
  347. memset(current_buf, 0, sizeof(*current_buf));
  348. current_file = file_lookup(name);
  349. yylineno = 1;
  350. }
  351. static void __zconf_nextfile(const char *name)
  352. {
  353. struct file *iter;
  354. struct file *file = file_lookup(name);
  355. struct buffer *buf = xmalloc(sizeof(*buf));
  356. memset(buf, 0, sizeof(*buf));
  357. current_buf->state = YY_CURRENT_BUFFER;
  358. yyin = zconf_fopen(file->name);
  359. if (!yyin) {
  360. fprintf(stderr, "%s:%d: can't open file \"%s\"\n",
  361. zconf_curname(), zconf_lineno(), file->name);
  362. exit(1);
  363. }
  364. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  365. buf->parent = current_buf;
  366. current_buf = buf;
  367. current_file->lineno = yylineno;
  368. file->parent = current_file;
  369. for (iter = current_file; iter; iter = iter->parent) {
  370. if (!strcmp(iter->name, file->name)) {
  371. fprintf(stderr,
  372. "Recursive inclusion detected.\n"
  373. "Inclusion path:\n"
  374. " current file : %s\n", file->name);
  375. iter = file;
  376. do {
  377. iter = iter->parent;
  378. fprintf(stderr, " included from: %s:%d\n",
  379. iter->name, iter->lineno - 1);
  380. } while (strcmp(iter->name, file->name));
  381. exit(1);
  382. }
  383. }
  384. yylineno = 1;
  385. current_file = file;
  386. }
  387. void zconf_nextfile(const char *name)
  388. {
  389. glob_t gl;
  390. int err;
  391. int i;
  392. char path[PATH_MAX], *p;
  393. err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
  394. /* ignore wildcard patterns that return no result */
  395. if (err == GLOB_NOMATCH && strchr(name, '*')) {
  396. err = 0;
  397. gl.gl_pathc = 0;
  398. }
  399. if (err == GLOB_NOMATCH) {
  400. p = strdup(current_file->name);
  401. if (p) {
  402. snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
  403. err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
  404. free(p);
  405. }
  406. }
  407. if (err) {
  408. const char *reason = "unknown error";
  409. switch (err) {
  410. case GLOB_NOSPACE:
  411. reason = "out of memory";
  412. break;
  413. case GLOB_ABORTED:
  414. reason = "read error";
  415. break;
  416. case GLOB_NOMATCH:
  417. reason = "No files found";
  418. break;
  419. default:
  420. break;
  421. }
  422. printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
  423. reason, name);
  424. exit(1);
  425. }
  426. for (i = 0; i < gl.gl_pathc; i++)
  427. __zconf_nextfile(gl.gl_pathv[i]);
  428. }
  429. static void zconf_endfile(void)
  430. {
  431. struct buffer *parent;
  432. current_file = current_file->parent;
  433. if (current_file)
  434. yylineno = current_file->lineno;
  435. parent = current_buf->parent;
  436. if (parent) {
  437. fclose(yyin);
  438. yy_delete_buffer(YY_CURRENT_BUFFER);
  439. yy_switch_to_buffer(parent->state);
  440. }
  441. free(current_buf);
  442. current_buf = parent;
  443. }
  444. int zconf_lineno(void)
  445. {
  446. return current_pos.lineno;
  447. }
  448. const char *zconf_curname(void)
  449. {
  450. return current_pos.file ? current_pos.file->name : "<none>";
  451. }