lexer.l 10 KB

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