1
0

zconf.l 6.5 KB

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