zconf.l 6.5 KB

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