zconf.l 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. %option nostdinit noyywrap never-interactive full ecs
  2. %option 8bit 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. #include "expand_env.h"
  17. #define START_STRSIZE 16
  18. static struct {
  19. struct file *file;
  20. int lineno;
  21. } current_pos;
  22. static char *text;
  23. static int text_size, text_asize;
  24. struct buffer {
  25. struct buffer *parent;
  26. YY_BUFFER_STATE state;
  27. };
  28. struct buffer *current_buf;
  29. static int last_ts, first_ts;
  30. static void zconf_endhelp(void);
  31. static void zconf_endfile(void);
  32. static void new_string(void)
  33. {
  34. text = xmalloc(START_STRSIZE);
  35. text_asize = START_STRSIZE;
  36. text_size = 0;
  37. *text = 0;
  38. }
  39. static void append_string(const char *str, int size)
  40. {
  41. int new_size = text_size + size + 1;
  42. if (new_size > text_asize) {
  43. new_size += START_STRSIZE - 1;
  44. new_size &= -START_STRSIZE;
  45. text = realloc(text, new_size);
  46. text_asize = new_size;
  47. }
  48. memcpy(text + text_size, str, size);
  49. text_size += size;
  50. text[text_size] = 0;
  51. }
  52. static void alloc_string(const char *str, int size)
  53. {
  54. text = xmalloc(size + 1);
  55. memcpy(text, str, size);
  56. text[size] = 0;
  57. }
  58. static void warn_ignored_character(char chr)
  59. {
  60. fprintf(stderr,
  61. "%s:%d:warning: ignoring unsupported character '%c'\n",
  62. zconf_curname(), zconf_lineno(), chr);
  63. }
  64. %}
  65. n [A-Za-z0-9_-]
  66. %%
  67. int str = 0;
  68. int ts, i;
  69. [ \t]*#.*\n |
  70. [ \t]*\n {
  71. current_file->lineno++;
  72. return T_EOL;
  73. }
  74. [ \t]*#.*
  75. [ \t]+ {
  76. BEGIN(COMMAND);
  77. }
  78. . {
  79. unput(yytext[0]);
  80. BEGIN(COMMAND);
  81. }
  82. <COMMAND>{
  83. {n}+ {
  84. const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  85. BEGIN(PARAM);
  86. current_pos.file = current_file;
  87. current_pos.lineno = current_file->lineno;
  88. if (id && id->flags & TF_COMMAND) {
  89. zconflval.id = id;
  90. return id->token;
  91. }
  92. alloc_string(yytext, yyleng);
  93. zconflval.string = text;
  94. return T_WORD;
  95. }
  96. [^\r\n] warn_ignored_character(*yytext);
  97. \r?\n {
  98. BEGIN(INITIAL);
  99. current_file->lineno++;
  100. return T_EOL;
  101. }
  102. }
  103. <PARAM>{
  104. "&&" return T_AND;
  105. "||" return T_OR;
  106. "(" return T_OPEN_PAREN;
  107. ")" return T_CLOSE_PAREN;
  108. "!" return T_NOT;
  109. "=" return T_EQUAL;
  110. "!=" return T_UNEQUAL;
  111. "<=" return T_LESS_EQUAL;
  112. ">=" return T_GREATER_EQUAL;
  113. "<" return T_LESS;
  114. ">" return T_GREATER;
  115. \"|\' {
  116. str = yytext[0];
  117. new_string();
  118. BEGIN(STRING);
  119. }
  120. \r?\n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  121. ({n}|[/.])+ {
  122. const struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  123. if (id && id->flags & TF_PARAM) {
  124. zconflval.id = id;
  125. return id->token;
  126. }
  127. alloc_string(yytext, yyleng);
  128. zconflval.string = text;
  129. return T_WORD;
  130. }
  131. #.* /* comment */
  132. \\\r?\n current_file->lineno++;
  133. [[:blank:]]+
  134. . warn_ignored_character(*yytext);
  135. <<EOF>> {
  136. BEGIN(INITIAL);
  137. }
  138. }
  139. <STRING>{
  140. [^'"\\\n]+/\n {
  141. append_string(yytext, yyleng);
  142. zconflval.string = text;
  143. return T_WORD_QUOTE;
  144. }
  145. [^'"\\\n]+ {
  146. append_string(yytext, yyleng);
  147. }
  148. \\.?/\n {
  149. append_string(yytext + 1, yyleng - 1);
  150. zconflval.string = text;
  151. return T_WORD_QUOTE;
  152. }
  153. \\.? {
  154. append_string(yytext + 1, yyleng - 1);
  155. }
  156. \'|\" {
  157. if (str == yytext[0]) {
  158. BEGIN(PARAM);
  159. zconflval.string = text;
  160. return T_WORD_QUOTE;
  161. } else
  162. append_string(yytext, 1);
  163. }
  164. \r?\n {
  165. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  166. current_file->lineno++;
  167. BEGIN(INITIAL);
  168. return T_EOL;
  169. }
  170. <<EOF>> {
  171. BEGIN(INITIAL);
  172. }
  173. }
  174. <HELP>{
  175. [ \t]+ {
  176. ts = 0;
  177. for (i = 0; i < yyleng; i++) {
  178. if (yytext[i] == '\t')
  179. ts = (ts & ~7) + 8;
  180. else
  181. ts++;
  182. }
  183. last_ts = ts;
  184. if (first_ts) {
  185. if (ts < first_ts) {
  186. zconf_endhelp();
  187. return T_HELPTEXT;
  188. }
  189. ts -= first_ts;
  190. while (ts > 8) {
  191. append_string(" ", 8);
  192. ts -= 8;
  193. }
  194. append_string(" ", ts);
  195. }
  196. }
  197. [ \t]*\r?\n/[^ \t\r\n] {
  198. current_file->lineno++;
  199. zconf_endhelp();
  200. return T_HELPTEXT;
  201. }
  202. [ \t]*\r?\n {
  203. current_file->lineno++;
  204. append_string("\n", 1);
  205. }
  206. [^ \t\r?\n].* {
  207. while (yyleng) {
  208. if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
  209. break;
  210. yyleng--;
  211. }
  212. append_string(yytext, yyleng);
  213. if (!first_ts)
  214. first_ts = last_ts;
  215. }
  216. <<EOF>> {
  217. zconf_endhelp();
  218. return T_HELPTEXT;
  219. }
  220. }
  221. <<EOF>> {
  222. if (current_file) {
  223. zconf_endfile();
  224. return T_EOL;
  225. }
  226. fclose(yyin);
  227. yyterminate();
  228. }
  229. %%
  230. void zconf_starthelp(void)
  231. {
  232. new_string();
  233. last_ts = first_ts = 0;
  234. BEGIN(HELP);
  235. }
  236. static void zconf_endhelp(void)
  237. {
  238. zconflval.string = text;
  239. BEGIN(INITIAL);
  240. }
  241. /*
  242. * Try to open specified file with following names:
  243. * ./name
  244. * $(srctree)/name
  245. * The latter is used when srctree is separate from objtree
  246. * when compiling the kernel.
  247. * Return NULL if file is not found.
  248. */
  249. FILE *zconf_fopen(const char *name)
  250. {
  251. char *env, fullname[PATH_MAX+1];
  252. FILE *f;
  253. f = fopen(name, "r");
  254. if (!f && name != NULL && name[0] != '/') {
  255. env = getenv(SRCTREE);
  256. if (env) {
  257. sprintf(fullname, "%s/%s", env, name);
  258. f = fopen(fullname, "r");
  259. }
  260. }
  261. return f;
  262. }
  263. void zconf_initscan(const char *name)
  264. {
  265. yyin = zconf_fopen(name);
  266. if (!yyin) {
  267. printf("can't find file %s\n", name);
  268. exit(1);
  269. }
  270. current_buf = xmalloc(sizeof(*current_buf));
  271. memset(current_buf, 0, sizeof(*current_buf));
  272. current_file = file_lookup(name);
  273. current_file->lineno = 1;
  274. }
  275. void zconf_nextfile(const char *name)
  276. {
  277. struct file *iter;
  278. struct file *file = file_lookup(name);
  279. struct buffer *buf = xmalloc(sizeof(*buf));
  280. memset(buf, 0, sizeof(*buf));
  281. current_buf->state = YY_CURRENT_BUFFER;
  282. yyin = zconf_fopen(file->name);
  283. if (!yyin) {
  284. printf("%s:%d: can't open file \"%s\"\n",
  285. zconf_curname(), zconf_lineno(), file->name);
  286. exit(1);
  287. }
  288. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  289. buf->parent = current_buf;
  290. current_buf = buf;
  291. for (iter = current_file->parent; iter; iter = iter->parent ) {
  292. if (!strcmp(current_file->name,iter->name) ) {
  293. printf("%s:%d: recursive inclusion detected. "
  294. "Inclusion path:\n current file : '%s'\n",
  295. zconf_curname(), zconf_lineno(),
  296. zconf_curname());
  297. iter = current_file->parent;
  298. while (iter && \
  299. strcmp(iter->name,current_file->name)) {
  300. printf(" included from: '%s:%d'\n",
  301. iter->name, iter->lineno-1);
  302. iter = iter->parent;
  303. }
  304. if (iter)
  305. printf(" included from: '%s:%d'\n",
  306. iter->name, iter->lineno+1);
  307. exit(1);
  308. }
  309. }
  310. file->lineno = 1;
  311. file->parent = current_file;
  312. current_file = file;
  313. }
  314. void zconf_nextfiles(const char *expression)
  315. {
  316. /* Expand environment variables in 'expression' */
  317. char* str = expand_environment(expression, zconf_curname(), zconf_lineno());
  318. /* zconf_nextfile() processes files in LIFO order, so to keep the
  319. files in the order provided we need to process the list backwards
  320. */
  321. if (str != NULL && strlen(str)) {
  322. char* pos = str + strlen(str); // start at null terminator
  323. while (pos != str) {
  324. pos--;
  325. if(*pos == ' ') {
  326. *pos = '\0'; // split buffer into multiple c-strings
  327. if (strlen(pos + 1)) {
  328. zconf_nextfile(pos + 1);
  329. }
  330. }
  331. }
  332. if (strlen(str)) { // re-check as first character may have been a space
  333. zconf_nextfile(str);
  334. }
  335. }
  336. free_expanded(str);
  337. }
  338. static void zconf_endfile(void)
  339. {
  340. struct buffer *parent;
  341. current_file = current_file->parent;
  342. parent = current_buf->parent;
  343. if (parent) {
  344. fclose(yyin);
  345. yy_delete_buffer(YY_CURRENT_BUFFER);
  346. yy_switch_to_buffer(parent->state);
  347. }
  348. free(current_buf);
  349. current_buf = parent;
  350. }
  351. int zconf_lineno(void)
  352. {
  353. return current_pos.lineno;
  354. }
  355. const char *zconf_curname(void)
  356. {
  357. return current_pos.file ? current_pos.file->name : "<none>";
  358. }