3
0

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 (size > 70) {
  42. fprintf (stderr, "%s:%d error: Overlong line\n",
  43. current_file->name, current_file->lineno);
  44. }
  45. if (new_size > text_asize) {
  46. new_size += START_STRSIZE - 1;
  47. new_size &= -START_STRSIZE;
  48. text = realloc(text, new_size);
  49. text_asize = new_size;
  50. }
  51. memcpy(text + text_size, str, size);
  52. text_size += size;
  53. text[text_size] = 0;
  54. }
  55. void alloc_string(const char *str, int size)
  56. {
  57. text = malloc(size + 1);
  58. memcpy(text, str, size);
  59. text[size] = 0;
  60. }
  61. %}
  62. ws [ \n\t]
  63. n [A-Za-z0-9_]
  64. %%
  65. int str = 0;
  66. int ts, i;
  67. [ \t]*#.*\n |
  68. [ \t]*\n {
  69. current_file->lineno++;
  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. struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  83. BEGIN(PARAM);
  84. current_pos.file = current_file;
  85. current_pos.lineno = current_file->lineno;
  86. if (id && id->flags & TF_COMMAND) {
  87. zconflval.id = id;
  88. return id->token;
  89. }
  90. alloc_string(yytext, yyleng);
  91. zconflval.string = text;
  92. return T_WORD;
  93. }
  94. .
  95. \n {
  96. BEGIN(INITIAL);
  97. current_file->lineno++;
  98. return T_EOL;
  99. }
  100. }
  101. <PARAM>{
  102. "&&" return T_AND;
  103. "||" return T_OR;
  104. "(" return T_OPEN_PAREN;
  105. ")" return T_CLOSE_PAREN;
  106. "!" return T_NOT;
  107. "=" return T_EQUAL;
  108. "!=" return T_UNEQUAL;
  109. \"|\' {
  110. str = yytext[0];
  111. new_string();
  112. BEGIN(STRING);
  113. }
  114. \n BEGIN(INITIAL); current_file->lineno++; return T_EOL;
  115. --- /* ignore */
  116. ({n}|[-/.])+ {
  117. struct kconf_id *id = kconf_id_lookup(yytext, yyleng);
  118. if (id && id->flags & TF_PARAM) {
  119. zconflval.id = id;
  120. return id->token;
  121. }
  122. alloc_string(yytext, yyleng);
  123. zconflval.string = text;
  124. return T_WORD;
  125. }
  126. #.* /* comment */
  127. \\\n current_file->lineno++;
  128. .
  129. <<EOF>> {
  130. BEGIN(INITIAL);
  131. }
  132. }
  133. <STRING>{
  134. [^'"\\\n]+/\n {
  135. append_string(yytext, yyleng);
  136. zconflval.string = text;
  137. return T_WORD_QUOTE;
  138. }
  139. [^'"\\\n]+ {
  140. append_string(yytext, yyleng);
  141. }
  142. \\.?/\n {
  143. append_string(yytext + 1, yyleng - 1);
  144. zconflval.string = text;
  145. return T_WORD_QUOTE;
  146. }
  147. \\.? {
  148. append_string(yytext + 1, yyleng - 1);
  149. }
  150. \'|\" {
  151. if (str == yytext[0]) {
  152. BEGIN(PARAM);
  153. zconflval.string = text;
  154. return T_WORD_QUOTE;
  155. } else
  156. append_string(yytext, 1);
  157. }
  158. \n {
  159. printf("%s:%d:warning: multi-line strings not supported\n", zconf_curname(), zconf_lineno());
  160. current_file->lineno++;
  161. BEGIN(INITIAL);
  162. return T_EOL;
  163. }
  164. <<EOF>> {
  165. BEGIN(INITIAL);
  166. }
  167. }
  168. <HELP>{
  169. [ \t]+ {
  170. ts = 0;
  171. for (i = 0; i < yyleng; i++) {
  172. if (yytext[i] == '\t')
  173. ts = (ts & ~7) + 8;
  174. else
  175. ts++;
  176. }
  177. last_ts = ts;
  178. if (first_ts) {
  179. if (ts < first_ts) {
  180. zconf_endhelp();
  181. return T_HELPTEXT;
  182. }
  183. ts -= first_ts;
  184. while (ts > 8) {
  185. append_string(" ", 8);
  186. ts -= 8;
  187. }
  188. append_string(" ", ts);
  189. }
  190. }
  191. [ \t]*\n/[^ \t\n] {
  192. current_file->lineno++;
  193. zconf_endhelp();
  194. return T_HELPTEXT;
  195. }
  196. [ \t]*\n {
  197. current_file->lineno++;
  198. append_string("\n", 1);
  199. }
  200. [^ \t\n].* {
  201. append_string(yytext, yyleng);
  202. if (!first_ts)
  203. first_ts = last_ts;
  204. }
  205. <<EOF>> {
  206. zconf_endhelp();
  207. return T_HELPTEXT;
  208. }
  209. }
  210. <<EOF>> {
  211. if (current_file) {
  212. zconf_endfile();
  213. return T_EOL;
  214. }
  215. fclose(yyin);
  216. yyterminate();
  217. }
  218. %%
  219. void zconf_starthelp(void)
  220. {
  221. new_string();
  222. last_ts = first_ts = 0;
  223. BEGIN(HELP);
  224. }
  225. static void zconf_endhelp(void)
  226. {
  227. zconflval.string = text;
  228. BEGIN(INITIAL);
  229. }
  230. /*
  231. * Try to open specified file with following names:
  232. * ./name
  233. * $(srctree)/name
  234. * The latter is used when srctree is separate from objtree
  235. * when compiling the kernel.
  236. * Return NULL if file is not found.
  237. */
  238. FILE *zconf_fopen(const char *name)
  239. {
  240. char *env;
  241. FILE *f;
  242. f = fopen(name, "r");
  243. if (!f && name[0] != '/') {
  244. env = getenv(SRCTREE);
  245. if (env) {
  246. char *fullname = alloca(strlen(env) + strlen(name) + 2);
  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. }