Debian Codegraph @

mingw-w64-tools/genidl/src/genidl_config.c

raw
1 /*
2 genidl - Generate interface defintion language listing from a
3 Portable Executable.
4 Copyright (C) 2009-2016 mingw-w64 project
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "genidl_cfg.h"
21
22 #define TOK_NAME 256
23 #define TOK_DIGIT 257
24 #define TOK_STRING 258
25
26 static sCfgLib *gen_cfglib (const char *);
27 static sCfgLib *has_cfglib (const char *, int);
28 static sCfgAlias *gen_cfglib_alias (sCfgLib *, const char *);
29 static sCfgAlias *has_cfglib_alias (sCfgLib *, const char *);
30 static sCfgItem *has_cfglib_item (sCfgLib *, const char *);
31 static sCfgItem *gen_cfglib_item (sCfgLib *, const char *, const char *);
32
33 static int rCh (void);
34 static void bCh (int r);
35 static int pCh (void);
36 static int addCh (int r);
37 #if 0
38 static void delCh (void);
39 #endif
40 static void clrCh (void);
41 static void printError (const char *fmt, ...);
42 static int lex (void);
43
44 static FILE *conf_fp = NULL;
45 static int last_ch = -1;
46 static int line_no = 1;
47 static int seen_eof = 0;
48 static char *l_buffer = NULL;
49 static size_t l_max, l_cur;
50
51 static sCfgLib *cfg_head = NULL;
52 static int is_modified = 0;
53
54 static sCfgItem *
55 has_cfglib_item (sCfgLib *c, const char *name)
56 {
57 sCfgItem *h;
58 if (!c || c->item == NULL)
59 return NULL;
60 h = c->item;
61 do {
62 if (!strcmp (h->name, name))
63 return h;
64 h = h->next;
65 } while (h != NULL);
66 return NULL;
67 }
68
69 static sCfgItem *
70 gen_cfglib_item (sCfgLib *c, const char *name, const char *type)
71 {
72 sCfgItem *a, *p = NULL, *e = c->item;
73 int is_new = 0;
74 a = has_cfglib_item (c, name);
75 if (!a)
76 {
77 a = (sCfgItem *) malloc (sizeof (sCfgItem) + strlen (name) + 1);
78 memset (a, 0, sizeof (sCfgItem));
79 strcpy (a->name, name);
80 is_new = 1;
81 }
82 is_modified = 1;
83 if (a->type != NULL)
84 free (a->type);
85 a->type = strdup (type);
86 if (!is_new)
87 return a;
88 while (e != NULL)
89 {
90 p = e;
91 e = e->next;
92 }
93 if (!p)
94 c->item = a;
95 else
96 p->next = a;
97 return a;
98 }
99
100 static sCfgAlias *
101 gen_cfglib_alias (sCfgLib *c, const char *name)
102 {
103 sCfgAlias *p, *e;
104 sCfgAlias *a = has_cfglib_alias (c, name);
105 if (a)
106 return a;
107 a = (sCfgAlias *) malloc (sizeof (sCfgAlias) + strlen (name) + 1);
108 memset (a, 0, sizeof (sCfgAlias));
109 strcpy (a->name, name);
110 p = NULL; e = c->alias;
111 while (e != NULL)
112 {
113 p = e;
114 e = e->next;
115 }
116 if (!p)
117 c->alias = a;
118 else
119 p->next = a;
120 is_modified = 1;
121 return a;
122 }
123
124 static sCfgAlias *
125 has_cfglib_alias (sCfgLib *c, const char *name)
126 {
127 sCfgAlias *a = (c ? c->alias : NULL);
128 while (a != NULL)
129 {
130 if (!strcmp (a->name, name))
131 return a;
132 a = a->next;
133 }
134 return NULL;
135 }
136
137 static sCfgLib *
138 has_cfglib (const char *name, int withAlias)
139 {
140 sCfgLib *r = cfg_head;
141 while (r != NULL)
142 {
143 if (!strcmp (r->name, name))
144 return r;
145 if (withAlias && has_cfglib_alias (r, name) != NULL)
146 return r;
147 r = r->next;
148 }
149 return NULL;
150 }
151
152 static sCfgLib *
153 gen_cfglib (const char *name)
154 {
155 sCfgLib *r, *p, *e;
156 if ((r = has_cfglib (name, 0)) != NULL)
157 return r;
158 r = (sCfgLib *) malloc (sizeof (sCfgLib) + strlen (name) + 1);
159 memset (r, 0, sizeof (sCfgLib));
160 strcpy (r->name, name);
161 p = NULL; e = cfg_head;
162 while (e != NULL)
163 {
164 p = e; e = e->next;
165 }
166 if (!p) cfg_head = r;
167 else p->next = r;
168 is_modified = 1;
169 return r;
170 }
171
172 static void
173 printError (const char *fmt, ...)
174 {
175 va_list argp;
176 va_start (argp, fmt);
177 fprintf (stderr, "configfile at ");
178 if (l_buffer[0] == 0 && seen_eof)
179 fprintf (stderr, "end of file");
180 else if (l_buffer[0] == 0)
181 fprintf (stderr, "start of file");
182 else
183 fprintf (stderr, "line %d near ,%s'", line_no, l_buffer);
184 fprintf (stderr, ": ");
185 vfprintf (stderr, fmt, argp);
186 va_end (argp);
187 }
188
189 static int
190 lex (void)
191 {
192 int r;
193
194 rescan:
195 clrCh ();
196 do {
197 r = rCh ();
198 } while (r >= 0 && r <= 0x20);
199
200 if (r == -1)
201 return -1;
202 if (r == '/' && pCh () == '*')
203 {
204 rCh ();
205 while ((r=rCh ()) != -1)
206 {
207 if (r == '*' && pCh () == '/')
208 {
209 rCh ();
210 break;
211 }
212 }
213 goto rescan;
214 }
215 else if (r == '/' && pCh () == '/')
216 {
217 while ((r=rCh ()) != -1)
218 {
219 if (r == '\n')
220 break;
221 }
222 goto rescan;
223 }
224 if (r == '_' || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z'))
225 {
226 bCh (r);
227 do {
228 addCh (rCh ());
229 r = pCh ();
230 } while (r == '_' || (r >= 'a' && r <= 'z') || (r >= 'A' && r <= 'Z')
231 || (r >= '0' && r <= '9') || r == '$' || r == '.');
232 return TOK_NAME;
233 }
234 if (r >= '0' && r <= '9')
235 {
236 addCh (r);
237 if (r == '0')
238 {
239 switch (pCh ()) {
240 case 'x': case 'X': addCh (rCh ()); break;
241 case 'o': case 'O': addCh (rCh ()); break;
242 case 'b': case 'B': addCh (rCh ()); break;
243 default:
244 break;
245 }
246 }
247 return TOK_DIGIT;
248 }
249 if (r == '"')
250 {
251 while ((r = pCh ()) != -1 && r != '"' && r != '\n')
252 addCh (rCh ());
253 if (r != '"')
254 printError ("Missing '\"' at end of string.\n");
255 else
256 rCh ();
257 return TOK_STRING;
258 }
259 addCh (r);
260 switch (r)
261 {
262 case '=': case '{': case '}': case ',':
263 case ';':
264 return r;
265 default:
266 break;
267 }
268 printError ("Illegal character found.\n");
269 goto rescan;
270 }
271
272 static void
273 clrCh (void)
274 {
275 l_cur = 0;
276 l_buffer[0] = 0;
277 }
278
279 #if 0
280 static void
281 delCh (void)
282 {
283 if (l_cur == 0)
284 return;
285 --l_cur;
286 l_buffer[l_cur] = 0;
287 }
288 #endif
289
290 static int
291 addCh (int r)
292 {
293 if (r == -1)
294 return r;
295 if (l_cur == l_max)
296 {
297 char *h = (char *) realloc (l_buffer, l_max + 129);
298 if (!h)
299 abort ();
300 l_max += 128;
301 }
302 l_buffer[l_cur++] = (char) r;
303 l_buffer[l_cur] = 0;
304 return r;
305 }
306
307 static int
308 pCh (void)
309 {
310 int r = last_ch;
311 if (r != -1)
312 return r;
313 r = rCh ();
314 if (r != -1)
315 bCh (r);
316 return r;
317 }
318
319 static void
320 bCh (int r)
321 {
322 if (r == '\n')
323 line_no--;
324 last_ch = r;
325 }
326
327 static int
328 rCh (void)
329 {
330 int r;
331 if ((r = last_ch) == -1)
332 {
333 char ch;
334 if (seen_eof || feof (conf_fp))
335 return -1;
336 if (fread (&ch, 1, 1, conf_fp) != 1)
337 return -1;
338 r = ((int) ch) & 0xff;
339 }
340 else
341 last_ch = -1;
342 if (r == '\r')
343 r = rCh ();
344 if (r == '\n')
345 line_no++;
346 return r;
347 }
348
349 static char **
350 parse_export (sCfgLib *cfg, int *re, const char *tname)
351 {
352 char **ret = NULL;
353 int r = lex ();
354 if (r == '=')
355 r = lex ();
356 if (r != '{')
357 {
358 printError ("Missing '{' for alias in ,%s'\n", tname);
359 *re = r;
360 return ret;
361 }
362 while ((r = lex ()) != -1)
363 {
364 if (r == '}')
365 break;
366 if (r == ',' || r == ';')
367 continue;
368 if (r == TOK_NAME || r == TOK_STRING)
369 {
370 char *left = strdup (l_buffer);
371
372 r = lex ();
373 if (r == ',' || r == '=')
374 r = lex ();
375 if (r != TOK_NAME && r != TOK_STRING)
376 {
377 printError ("Expected in export second string.\n");
378 }
379 else
380 {
381 gen_cfglib_item (cfg, left, l_buffer);
382 }
383 free (left);
384 }
385 else
386 printError ("Ignore token in alias of ,%s'.\n", tname);
387 }
388 return ret;
389 }
390
391 static char **
392 parse_alias (sCfgLib *cfg, int *re, const char *tname)
393 {
394 char **ret = NULL;
395 int r = lex ();
396 if (r == '=')
397 r = lex ();
398 if (r != '{')
399 {
400 printError ("Missing '{' for alias in ,%s'\n", tname);
401 *re = r;
402 return ret;
403 }
404 while ((r = lex ()) != -1)
405 {
406 if (r == '}')
407 break;
408 if (r == ',' || r == ';')
409 continue;
410 if (r == TOK_NAME || r == TOK_STRING)
411 {
412 gen_cfglib_alias (cfg, l_buffer);
413 }
414 else
415 printError ("Ignore token in alias of ,%s'.\n", tname);
416 }
417 return ret;
418 }
419
420 static int
421 parseTableSub (const char *tname)
422 {
423 char **alias = NULL;
424 char **exps;
425 int r = lex ();
426 sCfgLib *cfg = gen_cfglib (tname);
427 while (r != '}')
428 {
429 if (r == ';')
430 {
431 r = lex ();
432 continue;
433 }
434 if (r != TOK_NAME)
435 {
436 printError ("Unknown content in ,%s'\n", tname);
437 break;
438 }
439 if (strcmp (l_buffer, "alias") == 0)
440 {
441 alias = parse_alias (cfg, &r, tname);
442 if (r == -1)
443 break;
444 }
445 else if (strcmp (l_buffer, "export") == 0)
446 {
447 exps = parse_export (cfg, &r, tname);
448 if (r == -1)
449 break;
450 }
451 else
452 {
453 printError ("Unknown command %s in ,%s'\n", l_buffer, tname);
454 while ((r = lex ()) != -1 && r != ';');
455 }
456 r = lex ();
457 }
458 if (r != '}')
459 {
460 }
461 return r;
462 }
463
464 static int
465 parseTable (void)
466 {
467 char *table_name;
468 int r = lex ();
469
470 if (r == ';')
471 return 0;
472
473 switch (r)
474 {
475 case TOK_NAME:
476 case TOK_STRING:
477 table_name = strdup (l_buffer);
478 break;
479 case -1:
480 return -1;
481 default:
482 printError ("Unexpected token.\n");
483 return 0;
484 }
485 r = lex ();
486 if (r == '=')
487 r = lex ();
488 if (r != '{')
489 {
490 printError ("Missing '{' after ,%s'\n", table_name);
491 free (table_name);
492 return 0;
493 }
494 r = parseTableSub (table_name);
495 if (r != '}')
496 printError ("Missing '}' at end of ,%s'\n", table_name);
497 free (table_name);
498 return 0;
499 }
500
501 static void
502 parseStmt (void)
503 {
504 int r;
505
506 while ((r = parseTable ()) != -1)
507 {
508 }
509 }
510
511
512 int
513 genidl_read_config (const char *fname)
514 {
515 if (!fname)
516 return -1;
517 conf_fp = fopen (fname, "rb");
518 if (!conf_fp)
519 return -1;
520 l_buffer = (char *) malloc (129);
521 if (!l_buffer)
522 {
523 fclose (conf_fp);
524 return -1;
525 }
526 l_max = 128;
527 l_cur = 0;
528 l_buffer[0] = 0;
529 line_no = 1;
530 seen_eof = 0;
531 parseStmt ();
532 is_modified = 0;
533 free (l_buffer);
534 fclose (conf_fp);
535 return 0;
536 }
537
538 int
539 genidl_save_config (const char *file)
540 {
541 FILE *fp;
542 int ret;
543
544 if (!genidl_ismodified_config ())
545 return 1;
546 if (!file)
547 return 0;
548 fp = fopen (file, "wb");
549 ret = !genidl_save_config_fp (fp);
550 if (fp)
551 fclose (fp);
552 is_modified = 0;
553 return ret;
554 }
555
556 int
557 genidl_save_config_fp (FILE *fp)
558 {
559 sCfgLib *h = cfg_head;
560 sCfgAlias *alias;
561 sCfgItem *item;
562
563 if (!fp)
564 return 1;
565 fprintf (fp, "/* Configuration of genidl tool. */\n");
566 if (!h)
567 return 0;
568 do {
569 alias = h->alias;
570 item = h->item;
571 fprintf (fp, "\"%s\" = {\n", h->name);
572 if (alias)
573 {
574 fprintf (fp," alias = {\n");
575 do {
576 fprintf (fp, " \"%s\"%s\n", alias->name, (alias->next != NULL ? "," : ""));
577 alias = alias->next;
578 } while (alias);
579 fprintf (fp, " };\n");
580 }
581 if (item)
582 {
583 fprintf (fp, " export = {\n");
584 do {
585 fprintf (fp, " \"%s\" = \"%s\";\n", item->name, item->type);
586 item = item->next;
587 } while (item);
588 fprintf (fp, " };\n");
589 }
590 fprintf (fp, "};\n\n");
591 h = h->next;
592 } while (h != NULL);
593 return 0;
594 }
595
596 int
597 genidl_ismodified_config (void)
598 {
599 return is_modified;
600 }
601
602 int
603 genidl_add_lib (const char *lib)
604 {
605 if (!lib || *lib == 0)
606 return 0;
607 if (gen_cfglib (lib))
608 return 1;
609 return 0;
610 }
611
612 int
613 genidl_add_lib_alias (const char *lib, const char *alias)
614 {
615 sCfgLib *l;
616 if (!lib || *lib == 0 || !alias || *alias == 0)
617 return 0;
618 l = gen_cfglib (lib);
619 if (!l)
620 return 0;
621 if (gen_cfglib_alias (l, alias))
622 return 1;
623 return 0;
624 }
625
626 int
627 genidl_add_lib_item (const char *lib, const char *name, const char *typ)
628 {
629 sCfgLib *l;
630 if (!lib || *lib == 0 || !name || *name == 0 || !typ || *typ == 0)
631 return 0;
632 l = gen_cfglib (lib);
633 if (!l)
634 return 0;
635 if (gen_cfglib_item (l, name, typ))
636 return 1;
637 return 0;
638 }
639
640 const char *
641 genidl_find_type (const char *lib, const char *name)
642 {
643 sCfgLib *l;
644 sCfgItem *h;
645 char *is_tlb;
646
647 if (!lib || *lib == 0)
648 return NULL;
649 is_tlb = strstr (lib, ".tlb");
650 if (is_tlb)
651 {
652 sCfgLib *r = cfg_head;
653 while (r != NULL)
654 {
655 if (!strcmp (r->name, lib))
656 {
657 sCfgAlias *a = r->alias;
658 while (a != NULL)
659 {
660 const char *rs = genidl_find_type (a->name, name);
661 if (rs != NULL)
662 return rs;
663 a = a->next;
664 }
665 }
666 r = r->next;
667 }
668 return NULL;
669 }
670 l = has_cfglib (lib, 1);
671 if (!l)
672 return NULL;
673 h = has_cfglib_item (l, name);
674 if (!h)
675 return NULL;
676 return h->type;
677 }
678
679 int
680 genidl_del_lib_item (const char *lib)
681 {
682 sCfgLib *l;
683 sCfgItem *h;
684 if (!lib || *lib == 0)
685 return 0;
686 l = gen_cfglib (lib);
687 if (!l)
688 return 1;
689 if (l->item == NULL)
690 return 1;
691 while ((h = l->item) != NULL)
692 {
693 l->item = h->next;
694 if (h->type)
695 free (h->type);
696 free (h);
697 }
698 return 1;
699 }