zconf.l 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. %option backup nostdinit noyywrap never-interactive full ecs
  2. %option 8bit backup 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. #define LKC_DIRECT_LINK
  16. #include "lkc.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. void new_string(void)
  33. {
  34. text = malloc(START_STRSIZE);
  35. text_asize = START_STRSIZE;
  36. text_size = 0;
  37. *text = 0;
  38. }
  39. 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. void alloc_string(const char *str, int size)
  53. {
  54. text = malloc(size + 1);
  55. memcpy(text, str, size);
  56. text[size] = 0;
  57. }
  58. %}
  59. ws [ \n\t]
  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. 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. 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 = malloc(sizeof(*current_buf));
  262. memset(current_buf, 0, sizeof(*current_buf));
  263. current_file = file_lookup(name);
  264. current_file->lineno = 1;
  265. current_file->flags = FILE_BUSY;
  266. }
  267. void zconf_nextfile(const char *name)
  268. {
  269. struct file *file = file_lookup(name);
  270. struct buffer *buf = malloc(sizeof(*buf));
  271. memset(buf, 0, sizeof(*buf));
  272. current_buf->state = YY_CURRENT_BUFFER;
  273. yyin = zconf_fopen(name);
  274. if (!yyin) {
  275. printf("%s:%d: can't open file \"%s\"\n", zconf_curname(), zconf_lineno(), name);
  276. exit(1);
  277. }
  278. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  279. buf->parent = current_buf;
  280. current_buf = buf;
  281. if (file->flags & FILE_BUSY) {
  282. printf("recursive scan (%s)?\n", name);
  283. exit(1);
  284. }
  285. if (file->flags & FILE_SCANNED) {
  286. printf("file %s already scanned?\n", name);
  287. exit(1);
  288. }
  289. file->flags |= FILE_BUSY;
  290. file->lineno = 1;
  291. file->parent = current_file;
  292. current_file = file;
  293. }
  294. static void zconf_endfile(void)
  295. {
  296. struct buffer *parent;
  297. current_file->flags |= FILE_SCANNED;
  298. current_file->flags &= ~FILE_BUSY;
  299. current_file = current_file->parent;
  300. parent = current_buf->parent;
  301. if (parent) {
  302. fclose(yyin);
  303. yy_delete_buffer(YY_CURRENT_BUFFER);
  304. yy_switch_to_buffer(parent->state);
  305. }
  306. free(current_buf);
  307. current_buf = parent;
  308. }
  309. int zconf_lineno(void)
  310. {
  311. return current_pos.lineno;
  312. }
  313. char *zconf_curname(void)
  314. {
  315. return current_pos.file ? current_pos.file->name : "<none>";
  316. }