zconf.l 6.0 KB

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