zconf.l 7.5 KB

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