zconf.l 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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 <glob.h>
  16. #include <libgen.h>
  17. #include "lkc.h"
  18. #define START_STRSIZE 16
  19. static struct {
  20. struct file *file;
  21. int lineno;
  22. } current_pos;
  23. static char *text;
  24. static int text_size, text_asize;
  25. struct buffer {
  26. struct buffer *parent;
  27. YY_BUFFER_STATE state;
  28. };
  29. struct buffer *current_buf;
  30. static int last_ts, first_ts;
  31. static void zconf_endhelp(void);
  32. static void zconf_endfile(void);
  33. static void new_string(void)
  34. {
  35. text = xmalloc(START_STRSIZE);
  36. text_asize = START_STRSIZE;
  37. text_size = 0;
  38. *text = 0;
  39. }
  40. static void append_string(const char *str, int size)
  41. {
  42. int new_size = text_size + size + 1;
  43. if (new_size > text_asize) {
  44. new_size += START_STRSIZE - 1;
  45. new_size &= -START_STRSIZE;
  46. text = realloc(text, new_size);
  47. text_asize = new_size;
  48. }
  49. memcpy(text + text_size, str, size);
  50. text_size += size;
  51. text[text_size] = 0;
  52. }
  53. static void alloc_string(const char *str, int size)
  54. {
  55. text = xmalloc(size + 1);
  56. memcpy(text, str, size);
  57. text[size] = 0;
  58. }
  59. static void warn_ignored_character(char chr)
  60. {
  61. fprintf(stderr,
  62. "%s:%d:warning: ignoring unsupported character '%c'\n",
  63. zconf_curname(), zconf_lineno(), chr);
  64. }
  65. %}
  66. n [A-Za-z0-9_-]
  67. %%
  68. int str = 0;
  69. int ts, i;
  70. [ \t]*#.*\n |
  71. [ \t]*\n {
  72. current_file->lineno++;
  73. return T_EOL;
  74. }
  75. [ \t]*#.*
  76. [ \t]+ {
  77. BEGIN(COMMAND);
  78. }
  79. . {
  80. unput(yytext[0]);
  81. BEGIN(COMMAND);
  82. }
  83. <COMMAND>{
  84. {n}+ {
  85. const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  86. BEGIN(PARAM);
  87. current_pos.file = current_file;
  88. current_pos.lineno = current_file->lineno;
  89. if (id && id->flags & TF_COMMAND) {
  90. zconflval.id = id;
  91. return id->token;
  92. }
  93. alloc_string(yytext, yyleng);
  94. zconflval.string = text;
  95. return T_WORD;
  96. }
  97. . warn_ignored_character(*yytext);
  98. \n {
  99. BEGIN(INITIAL);
  100. current_file->lineno++;
  101. return T_EOL;
  102. }
  103. }
  104. <PARAM>{
  105. "&&" return T_AND;
  106. "||" return T_OR;
  107. "(" return T_OPEN_PAREN;
  108. ")" return T_CLOSE_PAREN;
  109. "!" return T_NOT;
  110. "=" return T_EQUAL;
  111. "!=" return T_UNEQUAL;
  112. "<=" return T_LESS_EQUAL;
  113. ">=" return T_GREATER_EQUAL;
  114. "<" return T_LESS;
  115. ">" return T_GREATER;
  116. \"|\' {
  117. str = yytext[0];
  118. new_string();
  119. BEGIN(STRING);
  120. }
  121. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  122. ({n}|[/.])+ {
  123. const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  124. if (id && id->flags & TF_PARAM) {
  125. zconflval.id = id;
  126. return id->token;
  127. }
  128. alloc_string(yytext, yyleng);
  129. zconflval.string = text;
  130. return T_WORD;
  131. }
  132. #.* /* comment */
  133. \\\n current_file->lineno++;
  134. [[:blank:]]+
  135. . warn_ignored_character(*yytext);
  136. <<EOF>> {
  137. BEGIN(INITIAL);
  138. }
  139. }
  140. <STRING>{
  141. [^'"\\\n]+/\n {
  142. append_string(yytext, yyleng);
  143. zconflval.string = text;
  144. return T_WORD_QUOTE;
  145. }
  146. [^'"\\\n]+ {
  147. append_string(yytext, yyleng);
  148. }
  149. \\.?/\n {
  150. append_string(yytext + 1, yyleng - 1);
  151. zconflval.string = text;
  152. return T_WORD_QUOTE;
  153. }
  154. \\.? {
  155. append_string(yytext + 1, yyleng - 1);
  156. }
  157. \'|\" {
  158. if (str == yytext[0]) {
  159. BEGIN(PARAM);
  160. zconflval.string = text;
  161. return T_WORD_QUOTE;
  162. } else
  163. append_string(yytext, 1);
  164. }
  165. \n {
  166. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  167. current_file->lineno++;
  168. BEGIN(INITIAL);
  169. return T_EOL;
  170. }
  171. <<EOF>> {
  172. BEGIN(INITIAL);
  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. static 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. void zconf_nextfile(const char *name)
  316. {
  317. glob_t gl;
  318. int err;
  319. int i;
  320. char path[PATH_MAX], *p;
  321. err = glob(name, GLOB_ERR | GLOB_MARK, NULL, &gl);
  322. /* ignore wildcard patterns that return no result */
  323. if (err == GLOB_NOMATCH && strchr(name, '*')) {
  324. err = 0;
  325. gl.gl_pathc = 0;
  326. }
  327. if (err == GLOB_NOMATCH) {
  328. p = strdup(current_file->name);
  329. if (p) {
  330. snprintf(path, sizeof(path), "%s/%s", dirname(p), name);
  331. err = glob(path, GLOB_ERR | GLOB_MARK, NULL, &gl);
  332. free(p);
  333. }
  334. }
  335. if (err) {
  336. const char *reason = "unknown error";
  337. switch (err) {
  338. case GLOB_NOSPACE:
  339. reason = "out of memory";
  340. break;
  341. case GLOB_ABORTED:
  342. reason = "read error";
  343. break;
  344. case GLOB_NOMATCH:
  345. reason = "No files found";
  346. break;
  347. default:
  348. break;
  349. }
  350. printf("%s:%d: glob failed: %s \"%s\"\n", zconf_curname(), zconf_lineno(),
  351. reason, name);
  352. exit(1);
  353. }
  354. for (i = 0; i < gl.gl_pathc; i++)
  355. __zconf_nextfile(gl.gl_pathv[i]);
  356. }
  357. static void zconf_endfile(void)
  358. {
  359. struct buffer *parent;
  360. current_file = current_file->parent;
  361. parent = current_buf->parent;
  362. if (parent) {
  363. fclose(yyin);
  364. yy_delete_buffer(YY_CURRENT_BUFFER);
  365. yy_switch_to_buffer(parent->state);
  366. }
  367. free(current_buf);
  368. current_buf = parent;
  369. }
  370. int zconf_lineno(void)
  371. {
  372. return current_pos.lineno;
  373. }
  374. const char *zconf_curname(void)
  375. {
  376. return current_pos.file ? current_pos.file->name : "<none>";
  377. }