zconf.l 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. %option backup nostdinit noyywrap never-interactive full ecs
  2. %option 8bit backup nodefault perf-report perf-report
  3. %x COMMAND HELP STRING PARAM
  4. %{
  5. /*
  6. * Copyright (C) 2002 Roman Zippel <zippel@linux-m68k.org>
  7. * Released under the terms of the GNU GPL v2.0.
  8. */
  9. #include <limits.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #define LKC_DIRECT_LINK
  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. void new_string(void)
  32. {
  33. text = malloc(START_STRSIZE);
  34. text_asize = START_STRSIZE;
  35. text_size = 0;
  36. *text = 0;
  37. }
  38. 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 = realloc(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. void alloc_string(const char *str, int size)
  52. {
  53. text = malloc(size + 1);
  54. memcpy(text, str, size);
  55. text[size] = 0;
  56. }
  57. %}
  58. ws [ \n\t]
  59. n [A-Za-z0-9_]
  60. %%
  61. int str = 0;
  62. int ts, i;
  63. [ \t]*#.*\n |
  64. [ \t]*\n {
  65. current_file->lineno++;
  66. return T_EOL;
  67. }
  68. [ \t]*#.*
  69. [ \t]+ {
  70. BEGIN(COMMAND);
  71. }
  72. . {
  73. unput(yytext[0]);
  74. BEGIN(COMMAND);
  75. }
  76. <COMMAND>{
  77. {n}+ {
  78. struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  79. BEGIN(PARAM);
  80. current_pos.file = current_file;
  81. current_pos.lineno = current_file->lineno;
  82. if (id && id->flags & TF_COMMAND) {
  83. zconflval.id = id;
  84. return id->token;
  85. }
  86. alloc_string(yytext, yyleng);
  87. zconflval.string = text;
  88. return T_WORD;
  89. }
  90. .
  91. \n {
  92. BEGIN(INITIAL);
  93. current_file->lineno++;
  94. return T_EOL;
  95. }
  96. }
  97. <PARAM>{
  98. "&&" return T_AND;
  99. "||" return T_OR;
  100. "(" return T_OPEN_PAREN;
  101. ")" return T_CLOSE_PAREN;
  102. "!" return T_NOT;
  103. "=" return T_EQUAL;
  104. "!=" return T_UNEQUAL;
  105. \"|\' {
  106. str = yytext[0];
  107. new_string();
  108. BEGIN(STRING);
  109. }
  110. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  111. --- /* ignore */
  112. ({n}|[-/.])+ {
  113. struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  114. if (id && id->flags & TF_PARAM) {
  115. zconflval.id = id;
  116. return id->token;
  117. }
  118. alloc_string(yytext, yyleng);
  119. zconflval.string = text;
  120. return T_WORD;
  121. }
  122. #.* /* comment */
  123. \\\n current_file->lineno++;
  124. .
  125. <<EOF>> {
  126. BEGIN(INITIAL);
  127. }
  128. }
  129. <STRING>{
  130. [^'"\\\n]+/\n {
  131. append_string(yytext, yyleng);
  132. zconflval.string = text;
  133. return T_WORD_QUOTE;
  134. }
  135. [^'"\\\n]+ {
  136. append_string(yytext, yyleng);
  137. }
  138. \\.?/\n {
  139. append_string(yytext + 1, yyleng - 1);
  140. zconflval.string = text;
  141. return T_WORD_QUOTE;
  142. }
  143. \\.? {
  144. append_string(yytext + 1, yyleng - 1);
  145. }
  146. \'|\" {
  147. if (str == yytext[0]) {
  148. BEGIN(PARAM);
  149. zconflval.string = text;
  150. return T_WORD_QUOTE;
  151. } else
  152. append_string(yytext, 1);
  153. }
  154. \n {
  155. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  156. current_file->lineno++;
  157. BEGIN(INITIAL);
  158. return T_EOL;
  159. }
  160. <<EOF>> {
  161. BEGIN(INITIAL);
  162. }
  163. }
  164. <HELP>{
  165. [ \t]+ {
  166. ts = 0;
  167. for (i = 0; i < yyleng; i++) {
  168. if (yytext[i] == '\t')
  169. ts = (ts & ~7) + 8;
  170. else
  171. ts++;
  172. }
  173. last_ts = ts;
  174. if (first_ts) {
  175. if (ts < first_ts) {
  176. zconf_endhelp();
  177. return T_HELPTEXT;
  178. }
  179. ts -= first_ts;
  180. while (ts > 8) {
  181. append_string(" ", 8);
  182. ts -= 8;
  183. }
  184. append_string(" ", ts);
  185. }
  186. }
  187. [ \t]*\n/[^ \t\n] {
  188. current_file->lineno++;
  189. zconf_endhelp();
  190. return T_HELPTEXT;
  191. }
  192. [ \t]*\n {
  193. current_file->lineno++;
  194. append_string("\n", 1);
  195. }
  196. [^ \t\n].* {
  197. while (yyleng) {
  198. if ((yytext[yyleng-1] != ' ') && (yytext[yyleng-1] != '\t'))
  199. break;
  200. yyleng--;
  201. }
  202. append_string(yytext, yyleng);
  203. if (!first_ts)
  204. first_ts = last_ts;
  205. }
  206. <<EOF>> {
  207. zconf_endhelp();
  208. return T_HELPTEXT;
  209. }
  210. }
  211. <<EOF>> {
  212. if (current_file) {
  213. zconf_endfile();
  214. return T_EOL;
  215. }
  216. fclose(yyin);
  217. yyterminate();
  218. }
  219. %%
  220. void zconf_starthelp(void)
  221. {
  222. new_string();
  223. last_ts = first_ts = 0;
  224. BEGIN(HELP);
  225. }
  226. static void zconf_endhelp(void)
  227. {
  228. zconflval.string = text;
  229. BEGIN(INITIAL);
  230. }
  231. /*
  232. * Try to open specified file with following names:
  233. * ./name
  234. * $(srctree)/name
  235. * The latter is used when srctree is separate from objtree
  236. * when compiling the kernel.
  237. * Return NULL if file is not found.
  238. */
  239. FILE *zconf_fopen(const char *name)
  240. {
  241. char *env, fullname[PATH_MAX+1];
  242. FILE *f;
  243. f = fopen(name, "r");
  244. if (!f && name != NULL && name[0] != '/') {
  245. env = getenv(SRCTREE);
  246. if (env) {
  247. sprintf(fullname, "%s/%s", env, name);
  248. f = fopen(fullname, "r");
  249. }
  250. }
  251. return f;
  252. }
  253. void zconf_initscan(const char *name)
  254. {
  255. yyin = zconf_fopen(name);
  256. if (!yyin) {
  257. printf("can't find file %s\n", name);
  258. exit(1);
  259. }
  260. current_buf = malloc(sizeof(*current_buf));
  261. memset(current_buf, 0, sizeof(*current_buf));
  262. current_file = file_lookup(name);
  263. current_file->lineno = 1;
  264. current_file->flags = FILE_BUSY;
  265. }
  266. void zconf_nextfile(const char *name)
  267. {
  268. struct file *file = file_lookup(name);
  269. struct buffer *buf = malloc(sizeof(*buf));
  270. memset(buf, 0, sizeof(*buf));
  271. current_buf->state = YY_CURRENT_BUFFER;
  272. yyin = zconf_fopen(name);
  273. if (!yyin) {
  274. printf("%s:%d: can't open file \"%s\"\n", zconf_curname(), zconf_lineno(), name);
  275. exit(1);
  276. }
  277. yy_switch_to_buffer(yy_create_buffer(yyin, YY_BUF_SIZE));
  278. buf->parent = current_buf;
  279. current_buf = buf;
  280. if (file->flags & FILE_BUSY) {
  281. printf("recursive scan (%s)?\n", name);
  282. exit(1);
  283. }
  284. if (file->flags & FILE_SCANNED) {
  285. printf("file %s already scanned?\n", name);
  286. exit(1);
  287. }
  288. file->flags |= FILE_BUSY;
  289. file->lineno = 1;
  290. file->parent = current_file;
  291. current_file = file;
  292. }
  293. static void zconf_endfile(void)
  294. {
  295. struct buffer *parent;
  296. current_file->flags |= FILE_SCANNED;
  297. current_file->flags &= ~FILE_BUSY;
  298. current_file = current_file->parent;
  299. parent = current_buf->parent;
  300. if (parent) {
  301. fclose(yyin);
  302. yy_delete_buffer(YY_CURRENT_BUFFER);
  303. yy_switch_to_buffer(parent->state);
  304. }
  305. free(current_buf);
  306. current_buf = parent;
  307. }
  308. int zconf_lineno(void)
  309. {
  310. return current_pos.lineno;
  311. }
  312. char *zconf_curname(void)
  313. {
  314. return current_pos.file ? current_pos.file->name : "<none>";
  315. }