zconf.l 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. zconf_curname(), zconf_lineno(), 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. }
  172. }
  173. <HELP>{
  174. [ \t]+ {
  175. ts = 0;
  176. for (i = 0; i < yyleng; i++) {
  177. if (yytext[i] == '\t')
  178. ts = (ts & ~7) + 8;
  179. else
  180. ts++;
  181. }
  182. last_ts = ts;
  183. if (first_ts) {
  184. if (ts < first_ts) {
  185. zconf_endhelp();
  186. return T_HELPTEXT;
  187. }
  188. ts -= first_ts;
  189. while (ts > 8) {
  190. append_string(" ", 8);
  191. ts -= 8;
  192. }
  193. append_string(" ", ts);
  194. }
  195. }
  196. [ \t]*\n/[^ \t\n] {
  197. current_file->lineno++;
  198. zconf_endhelp();
  199. return T_HELPTEXT;
  200. }
  201. [ \t]*\n {
  202. current_file->lineno++;
  203. append_string("\n", 1);
  204. }
  205. [^ \t\n].* {
  206. while (yyleng) {
  207. if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
  208. break;
  209. yyleng--;
  210. }
  211. append_string(yytext, yyleng);
  212. if (!first_ts)
  213. first_ts = last_ts;
  214. }
  215. <<EOF>> {
  216. zconf_endhelp();
  217. return T_HELPTEXT;
  218. }
  219. }
  220. <<EOF>> {
  221. if (current_file) {
  222. zconf_endfile();
  223. return T_EOL;
  224. }
  225. fclose(yyin);
  226. yyterminate();
  227. }
  228. %%
  229. void zconf_starthelp(void)
  230. {
  231. new_string();
  232. last_ts = first_ts = 0;
  233. BEGIN(HELP);
  234. }
  235. static void zconf_endhelp(void)
  236. {
  237. zconflval.string = text;
  238. BEGIN(INITIAL);
  239. }
  240. /*
  241. * Try to open specified file with following names:
  242. * ./name
  243. * $(srctree)/name
  244. * The latter is used when srctree is separate from objtree
  245. * when compiling the kernel.
  246. * Return NULL if file is not found.
  247. */
  248. FILE *zconf_fopen(const char *name)
  249. {
  250. char *env, fullname[PATH_MAX+1];
  251. FILE *f;
  252. f = fopen(name, "r");
  253. if (!f && name != NULL && name[0] != '/') {
  254. env = getenv(SRCTREE);
  255. if (env) {
  256. sprintf(fullname, "%s/%s", env, name);
  257. f = fopen(fullname, "r");
  258. }
  259. }
  260. return f;
  261. }
  262. void zconf_initscan(const char *name)
  263. {
  264. yyin = zconf_fopen(name);
  265. if (!yyin) {
  266. printf("can't find file %s\n", name);
  267. exit(1);
  268. }
  269. current_buf = xmalloc(sizeof(*current_buf));
  270. memset(current_buf, 0, sizeof(*current_buf));
  271. current_file = file_lookup(name);
  272. current_file->lineno = 1;
  273. }
  274. void zconf_nextfile(const char *name)
  275. {
  276. struct file *iter;
  277. struct file *file = file_lookup(name);
  278. struct buffer *buf = xmalloc(sizeof(*buf));
  279. memset(buf, 0, sizeof(*buf));
  280. current_buf->state = YY_CURRENT_BUFFER;
  281. yyin = zconf_fopen(file->name);
  282. if (!yyin) {
  283. printf("%s:%d: can't open file \"%s\"\n",
  284. zconf_curname(), zconf_lineno(), file->name);
  285. exit(1);
  286. }
  287. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  288. buf->parent = current_buf;
  289. current_buf = buf;
  290. for (iter = current_file->parent; iter; iter = iter->parent ) {
  291. if (!strcmp(current_file->name,iter->name) ) {
  292. printf("%s:%d: recursive inclusion detected. "
  293. "Inclusion path:\n current file : '%s'\n",
  294. zconf_curname(), zconf_lineno(),
  295. zconf_curname());
  296. iter = current_file->parent;
  297. while (iter && \
  298. strcmp(iter->name,current_file->name)) {
  299. printf(" included from: '%s:%d'\n",
  300. iter->name, iter->lineno-1);
  301. iter = iter->parent;
  302. }
  303. if (iter)
  304. printf(" included from: '%s:%d'\n",
  305. iter->name, iter->lineno+1);
  306. exit(1);
  307. }
  308. }
  309. file->lineno = 1;
  310. file->parent = current_file;
  311. current_file = file;
  312. }
  313. static void zconf_endfile(void)
  314. {
  315. struct buffer *parent;
  316. current_file = current_file->parent;
  317. parent = current_buf->parent;
  318. if (parent) {
  319. fclose(yyin);
  320. yy_delete_buffer(YY_CURRENT_BUFFER);
  321. yy_switch_to_buffer(parent->state);
  322. }
  323. free(current_buf);
  324. current_buf = parent;
  325. }
  326. int zconf_lineno(void)
  327. {
  328. return current_pos.lineno;
  329. }
  330. const char *zconf_curname(void)
  331. {
  332. return current_pos.file ? current_pos.file->name : "<none>";
  333. }