zconf.l 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. %option nostdinit noyywrap never-interactive full ecs
  2. %option 8bit nodefault perf-report perf-report
  3. %option noinput
  4. %x COMMAND HELP STRING PARAM
  5. %{
  6. /*
  7. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  8. * Released under the terms of the GNU GPL v2.0.
  9. */
  10. #include <limits.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <unistd.h>
  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. static void new_string(void)
  32. {
  33. text = xmalloc(START_STRSIZE);
  34. text_asize = START_STRSIZE;
  35. text_size = 0;
  36. *text = 0;
  37. }
  38. static 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. static void alloc_string(const char *str, int size)
  52. {
  53. text = xmalloc(size + 1);
  54. memcpy(text, str, size);
  55. text[size] = 0;
  56. }
  57. static void warn_ignored_character(char chr)
  58. {
  59. fprintf(stderr,
  60. "%s:%d:warning: ignoring unsupported character '%c'\n",
  61. current_file->name, yylineno, chr);
  62. }
  63. %}
  64. n [A-Za-z0-9_-]
  65. %%
  66. int str = 0;
  67. int ts, i;
  68. [ \t]*#.*\n |
  69. [ \t]*\n {
  70. current_file->lineno++;
  71. return T_EOL;
  72. }
  73. [ \t]*#.*
  74. [ \t]+ {
  75. BEGIN(COMMAND);
  76. }
  77. . {
  78. unput(yytext[0]);
  79. BEGIN(COMMAND);
  80. }
  81. <COMMAND>{
  82. {n}+ {
  83. const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  84. BEGIN(PARAM);
  85. current_pos.file = current_file;
  86. current_pos.lineno = current_file->lineno;
  87. if (id && id->flags & TF_COMMAND) {
  88. zconflval.id = id;
  89. return id->token;
  90. }
  91. alloc_string(yytext, yyleng);
  92. zconflval.string = text;
  93. return T_WORD;
  94. }
  95. . warn_ignored_character(*yytext);
  96. \n {
  97. BEGIN(INITIAL);
  98. current_file->lineno++;
  99. return T_EOL;
  100. }
  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. "<=" return T_LESS_EQUAL;
  111. ">=" return T_GREATER_EQUAL;
  112. "<" return T_LESS;
  113. ">" return T_GREATER;
  114. \"|\' {
  115. str = yytext[0];
  116. new_string();
  117. BEGIN(STRING);
  118. }
  119. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  120. ({n}|[/.])+ {
  121. const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  122. if (id && id->flags & TF_PARAM) {
  123. zconflval.id = id;
  124. return id->token;
  125. }
  126. alloc_string(yytext, yyleng);
  127. zconflval.string = text;
  128. return T_WORD;
  129. }
  130. #.* /* comment */
  131. \\\n current_file->lineno++;
  132. [[:blank:]]+
  133. . warn_ignored_character(*yytext);
  134. <<EOF>> {
  135. BEGIN(INITIAL);
  136. }
  137. }
  138. <STRING>{
  139. [^'"\\\n]+/\n {
  140. append_string(yytext, yyleng);
  141. zconflval.string = text;
  142. return T_WORD_QUOTE;
  143. }
  144. [^'"\\\n]+ {
  145. append_string(yytext, yyleng);
  146. }
  147. \\.?/\n {
  148. append_string(yytext + 1, yyleng - 1);
  149. zconflval.string = text;
  150. return T_WORD_QUOTE;
  151. }
  152. \\.? {
  153. append_string(yytext + 1, yyleng - 1);
  154. }
  155. \'|\" {
  156. if (str == yytext[0]) {
  157. BEGIN(PARAM);
  158. zconflval.string = text;
  159. return T_WORD_QUOTE;
  160. } else
  161. append_string(yytext, 1);
  162. }
  163. \n {
  164. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  165. current_file->lineno++;
  166. BEGIN(INITIAL);
  167. return T_EOL;
  168. }
  169. <<EOF>> {
  170. BEGIN(INITIAL);
  171. yylval.string = text;
  172. return T_WORD_QUOTE;
  173. }
  174. }
  175. <HELP>{
  176. [ \t]+ {
  177. ts = 0;
  178. for (i = 0; i < yyleng; i++) {
  179. if (yytext[i] == '\t')
  180. ts = (ts & ~7) + 8;
  181. else
  182. ts++;
  183. }
  184. last_ts = ts;
  185. if (first_ts) {
  186. if (ts < first_ts) {
  187. zconf_endhelp();
  188. return T_HELPTEXT;
  189. }
  190. ts -= first_ts;
  191. while (ts > 8) {
  192. append_string(" ", 8);
  193. ts -= 8;
  194. }
  195. append_string(" ", ts);
  196. }
  197. }
  198. [ \t]*\n/[^ \t\n] {
  199. current_file->lineno++;
  200. zconf_endhelp();
  201. return T_HELPTEXT;
  202. }
  203. [ \t]*\n {
  204. current_file->lineno++;
  205. append_string("\n", 1);
  206. }
  207. [^ \t\n].* {
  208. while (yyleng) {
  209. if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
  210. break;
  211. yyleng--;
  212. }
  213. append_string(yytext, yyleng);
  214. if (!first_ts)
  215. first_ts = last_ts;
  216. }
  217. <<EOF>> {
  218. zconf_endhelp();
  219. return T_HELPTEXT;
  220. }
  221. }
  222. <<EOF>> {
  223. if (current_file) {
  224. zconf_endfile();
  225. return T_EOL;
  226. }
  227. fclose(yyin);
  228. yyterminate();
  229. }
  230. %%
  231. void zconf_starthelp(void)
  232. {
  233. new_string();
  234. last_ts = first_ts = 0;
  235. BEGIN(HELP);
  236. }
  237. static void zconf_endhelp(void)
  238. {
  239. zconflval.string = text;
  240. BEGIN(INITIAL);
  241. }
  242. /*
  243. * Try to open specified file with following names:
  244. * ./name
  245. * $(srctree)/name
  246. * The latter is used when srctree is separate from objtree
  247. * when compiling the kernel.
  248. * Return NULL if file is not found.
  249. */
  250. FILE *zconf_fopen(const char *name)
  251. {
  252. char *env, fullname[PATH_MAX+1];
  253. FILE *f;
  254. f = fopen(name, "r");
  255. if (!f && name != NULL && name[0] != '/') {
  256. env = getenv(SRCTREE);
  257. if (env) {
  258. sprintf(fullname, "%s/%s", env, name);
  259. f = fopen(fullname, "r");
  260. }
  261. }
  262. return f;
  263. }
  264. void zconf_initscan(const char *name)
  265. {
  266. yyin = zconf_fopen(name);
  267. if (!yyin) {
  268. printf("can't find file %s\n", name);
  269. exit(1);
  270. }
  271. current_buf = xmalloc(sizeof(*current_buf));
  272. memset(current_buf, 0, sizeof(*current_buf));
  273. current_file = file_lookup(name);
  274. current_file->lineno = 1;
  275. }
  276. void zconf_nextfile(const char *name)
  277. {
  278. struct file *iter;
  279. struct file *file = file_lookup(name);
  280. struct buffer *buf = xmalloc(sizeof(*buf));
  281. memset(buf, 0, sizeof(*buf));
  282. current_buf->state = YY_CURRENT_BUFFER;
  283. yyin = zconf_fopen(file->name);
  284. if (!yyin) {
  285. printf("%s:%d: can't open file \"%s\"\n",
  286. zconf_curname(), zconf_lineno(), file->name);
  287. exit(1);
  288. }
  289. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  290. buf->parent = current_buf;
  291. current_buf = buf;
  292. for (iter = current_file->parent; iter; iter = iter->parent ) {
  293. if (!strcmp(current_file->name,iter->name) ) {
  294. printf("%s:%d: recursive inclusion detected. "
  295. "Inclusion path:\n current file : '%s'\n",
  296. zconf_curname(), zconf_lineno(),
  297. zconf_curname());
  298. iter = current_file->parent;
  299. while (iter && \
  300. strcmp(iter->name,current_file->name)) {
  301. printf(" included from: '%s:%d'\n",
  302. iter->name, iter->lineno-1);
  303. iter = iter->parent;
  304. }
  305. if (iter)
  306. printf(" included from: '%s:%d'\n",
  307. iter->name, iter->lineno+1);
  308. exit(1);
  309. }
  310. }
  311. file->lineno = 1;
  312. file->parent = current_file;
  313. current_file = file;
  314. }
  315. static void zconf_endfile(void)
  316. {
  317. struct buffer *parent;
  318. current_file = current_file->parent;
  319. parent = current_buf->parent;
  320. if (parent) {
  321. fclose(yyin);
  322. yy_delete_buffer(YY_CURRENT_BUFFER);
  323. yy_switch_to_buffer(parent->state);
  324. }
  325. free(current_buf);
  326. current_buf = parent;
  327. }
  328. int zconf_lineno(void)
  329. {
  330. return current_pos.lineno;
  331. }
  332. const char *zconf_curname(void)
  333. {
  334. return current_pos.file ? current_pos.file->name : "<none>";
  335. }