zconf.l 6.6 KB

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