zconf.l 6.8 KB

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