/[CvsGraph]/cvsgraph/readconf.c
ViewVC logotype

Annotate of /cvsgraph/readconf.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.3 - (show annotations)
Thu Feb 14 23:18:10 2002 UTC (15 years, 8 months ago) by bertho
Branch: MAIN
CVS Tags: REL_1_2_0
Changes since 1.2: +11 -3 lines
File MIME type: text/plain
*** empty log message ***
1 /*
2 * CvsGraph graphical representation generator of brances and revisions
3 * of a file in cvs/rcs.
4 *
5 * Copyright (C) 2001,2002 B. Stultiens
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 /*#define DEBUG 1*/
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <ctype.h>
28 #include <assert.h>
29
30 #include <gd.h>
31 #include <gdfontt.h>
32 #include <gdfonts.h>
33 #include <gdfontmb.h>
34 #include <gdfontl.h>
35 #include <gdfontg.h>
36
37 #include "utils.h"
38 #include "cvsgraph.h"
39 #include "readconf.h"
40
41 int line_number;
42
43 typedef struct
44 {
45 char *keyword;
46 int type;
47 union {
48 void *v; /* join of other values */
49 int *i;
50 font_t *f;
51 char **s;
52 color_t *c;
53 double *d;
54 int val; /* FIXME: Oops, sizeof(int) must be sizeof(void *) */
55 } confref;
56 } keyword_t;
57
58 typedef union {
59 keyword_t *kw;
60 int i;
61 double d;
62 char *str;
63 } YYSTYPE;
64
65 static YYSTYPE yylval;
66
67 static int nstacked_opts;
68 static char **stacked_opts;
69
70 enum {
71 TYPE_dummy = 256,
72 TYPE_KEYWORD,
73 TYPE_NUMBER,
74 TYPE_VALUE,
75 TYPE_BOOLEAN,
76 TYPE_COLOR,
77 TYPE_FONT,
78 TYPE_STRING,
79 TYPE_DOUBLE
80 };
81
82 static keyword_t keywords[] = {
83 { "branch_bgcolor", TYPE_COLOR, { &conf.branch_bgcolor } },
84 { "branch_bspace", TYPE_NUMBER, { &conf.branch_bspace } },
85 { "branch_color", TYPE_COLOR, { &conf.branch_color } },
86 { "branch_font", TYPE_FONT, { &conf.branch_font.gdfont } },
87 { "branch_ttfont", TYPE_STRING, { &conf.branch_font.ttfont } },
88 { "branch_ttsize", TYPE_DOUBLE, { &conf.branch_font.ttsize } },
89 { "branch_tag_color", TYPE_COLOR, { &conf.branch_tag_color } },
90 { "branch_tag_font", TYPE_FONT, { &conf.branch_tag_font.gdfont } },
91 { "branch_tag_ttfont", TYPE_STRING, { &conf.branch_tag_font.ttfont } },
92 { "branch_tag_ttsize", TYPE_DOUBLE, { &conf.branch_tag_font.ttsize } },
93 { "branch_lspace", TYPE_NUMBER, { &conf.branch_lspace } },
94 { "branch_rspace", TYPE_NUMBER, { &conf.branch_rspace } },
95 { "branch_tspace", TYPE_NUMBER, { &conf.branch_tspace } },
96 { "branch_connect", TYPE_NUMBER, { &conf.branch_connect } },
97 { "branch_margin", TYPE_NUMBER, { &conf.branch_margin } },
98 { "branch_dupbox", TYPE_BOOLEAN, { &conf.branch_dupbox } },
99 { "upside_down", TYPE_BOOLEAN, { &conf.upside_down } },
100 { "auto_stretch", TYPE_BOOLEAN, { &conf.auto_stretch } },
101 { "color_bg", TYPE_COLOR, { &conf.color_bg } },
102 { "transparent_bg", TYPE_BOOLEAN, { &conf.transparent_bg } },
103 { "cvsmodule", TYPE_STRING, { &conf.cvsmodule } },
104 { "cvsroot", TYPE_STRING, { &conf.cvsroot } },
105 { "date_format", TYPE_STRING, { &conf.date_format } },
106 { "box_shadow", TYPE_BOOLEAN, { &conf.box_shadow } },
107 { "strip_untagged", TYPE_BOOLEAN, { &conf.strip_untagged } },
108 { "strip_first_rev", TYPE_BOOLEAN, { &conf.strip_first_rev } },
109 { "anti_alias", TYPE_BOOLEAN, { &conf.anti_alias } },
110 { "use_ttf", TYPE_BOOLEAN, { &conf.use_ttf } },
111 { "thick_lines", TYPE_NUMBER, { &conf.thick_lines } },
112 { "rev_color", TYPE_COLOR, { &conf.rev_color } },
113 { "rev_bgcolor", TYPE_COLOR, { &conf.rev_bgcolor } },
114 { "rev_font", TYPE_FONT, { &conf.rev_font.gdfont } },
115 { "rev_ttfont", TYPE_STRING, { &conf.rev_font.ttfont } },
116 { "rev_ttsize", TYPE_DOUBLE, { &conf.rev_font.ttsize } },
117 { "rev_separator", TYPE_NUMBER, { &conf.rev_separator } },
118 { "rev_minline", TYPE_NUMBER, { &conf.rev_minline } },
119 { "rev_maxline", TYPE_NUMBER, { &conf.rev_maxline } },
120 { "rev_lspace", TYPE_NUMBER, { &conf.rev_lspace } },
121 { "rev_rspace", TYPE_NUMBER, { &conf.rev_rspace } },
122 { "rev_tspace", TYPE_NUMBER, { &conf.rev_tspace } },
123 { "rev_bspace", TYPE_NUMBER, { &conf.rev_bspace } },
124 { "rev_text", TYPE_STRING, { &conf.rev_text } },
125 { "rev_text_color", TYPE_COLOR, { &conf.rev_text_color } },
126 { "rev_text_font", TYPE_FONT, { &conf.rev_text_font.gdfont } },
127 { "rev_text_ttfont", TYPE_STRING, { &conf.rev_text_font.ttfont } },
128 { "rev_text_ttsize", TYPE_DOUBLE, { &conf.rev_text_font.ttsize } },
129 { "tag_color", TYPE_COLOR, { &conf.tag_color } },
130 { "tag_font", TYPE_FONT, { &conf.tag_font.gdfont } },
131 { "tag_ttfont", TYPE_STRING, { &conf.tag_font.ttfont } },
132 { "tag_ttsize", TYPE_DOUBLE, { &conf.tag_font.ttsize } },
133 { "title", TYPE_STRING, { &conf.title } },
134 { "title_x", TYPE_NUMBER, { &conf.title_x } },
135 { "title_y", TYPE_NUMBER, { &conf.title_y } },
136 { "title_font", TYPE_FONT, { &conf.title_font.gdfont } },
137 { "title_ttfont", TYPE_STRING, { &conf.title_font.ttfont } },
138 { "title_ttsize", TYPE_DOUBLE, { &conf.title_font.ttsize } },
139 { "title_align", TYPE_NUMBER, { &conf.title_align } },
140 { "title_color", TYPE_COLOR, { &conf.title_color } },
141 { "margin_top", TYPE_NUMBER, { &conf.margin_top } },
142 { "margin_bottom", TYPE_NUMBER, { &conf.margin_bottom } },
143 { "margin_left", TYPE_NUMBER, { &conf.margin_left } },
144 { "margin_right", TYPE_NUMBER, { &conf.margin_right } },
145 { "image_type", TYPE_NUMBER, { &conf.image_type } },
146 { "image_quality", TYPE_NUMBER, { &conf.image_quality } },
147 { "map_name", TYPE_STRING, { &conf.map_name } },
148 { "map_branch_href", TYPE_STRING, { &conf.map_branch_href } },
149 { "map_branch_alt", TYPE_STRING, { &conf.map_branch_alt } },
150 { "map_rev_href", TYPE_STRING, { &conf.map_rev_href } },
151 { "map_rev_alt", TYPE_STRING, { &conf.map_rev_alt } },
152 { "map_diff_href", TYPE_STRING, { &conf.map_diff_href } },
153 { "map_diff_alt", TYPE_STRING, { &conf.map_diff_alt } },
154 { "jpeg", TYPE_VALUE, { (void *)IMAGE_JPEG } },
155 { "png", TYPE_VALUE, { (void *)IMAGE_PNG } },
156 { "gif", TYPE_VALUE, { (void *)IMAGE_GIF } },
157 { "true", TYPE_VALUE, { (void *)1 } },
158 { "false", TYPE_VALUE, { (void *)0 } },
159 { "not", TYPE_VALUE, { (void *)-1 } },
160 { "left", TYPE_VALUE, { (void *)0 } },
161 { "center", TYPE_VALUE, { (void *)1 } },
162 { "right", TYPE_VALUE, { (void *)2 } },
163 { "tiny", TYPE_VALUE, { (void *)0 } },
164 { "small", TYPE_VALUE, { (void *)1 } },
165 { "medium", TYPE_VALUE, { (void *)2 } },
166 { "large", TYPE_VALUE, { (void *)3 } },
167 { "giant", TYPE_VALUE, { (void *)4 } },
168 };
169
170 #define NKEYWORDS (sizeof(keywords) / sizeof(keywords[0]))
171
172 static int cmp_kw(const void *k1, const void *k2)
173 {
174 return strcmp(((keyword_t *)k1)->keyword, ((keyword_t *)k2)->keyword);
175 }
176
177 /*
178 **************************************************************************
179 * Debug routines
180 **************************************************************************
181 */
182 #ifdef DEBUG
183 #define DEBUGSTREAM stdout
184 static void debug_pname(const char *n)
185 {
186 fprintf(DEBUGSTREAM, "%-16s: ", n);
187 }
188
189 static void debug_pstring(const char *n, const char *a)
190 {
191 debug_pname(n);
192 if(!a)
193 fprintf(DEBUGSTREAM, "<not-set>\n");
194 else
195 {
196 fputc('\'', DEBUGSTREAM);
197 for(; *a; a++)
198 {
199 if(isprint(*a))
200 fputc(*a, DEBUGSTREAM);
201 else
202 {
203 fputc('\\', DEBUGSTREAM);
204 switch(*a)
205 {
206 case '\a': fputc('a', DEBUGSTREAM); break;
207 case '\b': fputc('b', DEBUGSTREAM); break;
208 case '\f': fputc('f', DEBUGSTREAM); break;
209 case '\n': fputc('n', DEBUGSTREAM); break;
210 case '\r': fputc('r', DEBUGSTREAM); break;
211 case '\t': fputc('t', DEBUGSTREAM); break;
212 case '\v': fputc('v', DEBUGSTREAM); break;
213 default:
214 fprintf(DEBUGSTREAM, "x%02x", (unsigned char)*a);
215 }
216 }
217 }
218 fprintf(DEBUGSTREAM, "'\n");
219 }
220 }
221
222 static void debug_pbool(const char *n, int b)
223 {
224 debug_pname(n);
225 fprintf(DEBUGSTREAM, "%s\n", b ? "true" : "false");
226 }
227
228 static void debug_pint(const char *n, int i)
229 {
230 debug_pname(n);
231 fprintf(DEBUGSTREAM, "%i\n", i);
232 }
233
234 static void debug_pdouble(const char *n, double d)
235 {
236 debug_pname(n);
237 fprintf(DEBUGSTREAM, "%g\n", d);
238 }
239
240 static void debug_pfont(const char *n, gdFontPtr f)
241 {
242 const char *s = "<Unknown font>";
243 debug_pname(n);
244 if(f == gdFontTiny)
245 s = "gdFontTiny";
246 else if(f == gdFontSmall)
247 s = "gdFontSmall";
248 else if(f == gdFontMediumBold)
249 s = "gdFontMediumBold";
250 else if(f == gdFontLarge)
251 s = "gdFontLarge";
252 else if(f == gdFontGiant)
253 s = "gdFontGiant";
254 fprintf(DEBUGSTREAM, "%s\n", s);
255 }
256
257 static void debug_pcolor(const char *n, color_t *c)
258 {
259 debug_pname(n);
260 fprintf(DEBUGSTREAM, "#%02x%02x%02x\n", c->r, c->g, c->b);
261 }
262
263 void dump_config(void)
264 {
265 debug_pstring("cvsroot", conf.cvsroot);
266 debug_pstring("cvsmodule", conf.cvsmodule);
267 debug_pstring("date_format", conf.date_format);
268
269 debug_pcolor("color_bg", &conf.color_bg);
270 debug_pbool("box_shadow", conf.box_shadow);
271 debug_pbool("upside_down", conf.upside_down);
272 debug_pbool("strip_untagged", conf.strip_untagged);
273 debug_pbool("strip_first_rev", conf.strip_first_rev);
274 debug_pbool("auto_stretch", conf.auto_stretch);
275 debug_pbool("anti_alias", conf.anti_alias);
276 debug_pbool("use_ttf", conf.use_ttf);
277 debug_pint("thick_lines", conf.thick_lines);
278
279 debug_pfont("tag_font", conf.tag_font.gdfont);
280 debug_pstring("tag_ttfont", conf.tag_font.ttfont);
281 debug_psouble("tag_ttsize", conf.tag_font.ttsize);
282 debug_pcolor("tag_color", &conf.tag_color);
283
284 debug_pfont("rev_font", conf.rev_font.gdfont);
285 debug_pstring("rev_ttfont", conf.rev_font.ttfont);
286 debug_pdouble("rev_ttsize", conf.rev_font.ttsize);
287 debug_pcolor("rev_color", &conf.rev_color);
288 debug_pcolor("rev_bgcolor", &conf.rev_bgcolor);
289 debug_pint("rev_separator", conf.rev_separator);
290 debug_pint("rev_minline", conf.rev_minline);
291 debug_pint("rev_maxline", conf.rev_maxline);
292 debug_pint("rev_lspace", conf.rev_lspace);
293 debug_pint("rev_rspace", conf.rev_rspace);
294 debug_pint("rev_tspace", conf.rev_tspace);
295 debug_pint("rev_bspace", conf.rev_bspace);
296 debug_pstring("rev_text", conf.rev_text);
297 debug_pcolor("rev_text_color", &conf.rev_text_color);
298 debug_pfont("rev_text_font", conf.rev_text_font.gdfont);
299 debug_pstring("rev_text_ttfont", conf.rev_text_font.ttfont);
300 debug_pdouble("rev_text_ttsize", conf.rev_text_font.ttsize);
301
302 debug_pfont("branch_font", conf.branch_font.gdfont);
303 debug_pstring("branch_ttfont", conf.branch_font.ttfont);
304 debug_pdouble("branch_ttsize", conf.branch_font.ttsize);
305 debug_pcolor("branch_color", &conf.branch_color);
306 debug_pfont("branch_tag_font", conf.branch_tag_font.gdfont);
307 debug_pstring("branch_tag_ttfont", conf.branch_tag_font.ttfont);
308 debug_pdouble("branch_tag_ttsize", conf.branch_tag_font.ttsize);
309 debug_pcolor("branch_tag_color", &conf.branch_tag_color);
310 debug_pcolor("branch_bgcolor", &conf.branch_bgcolor);
311 debug_pint("branch_lspace", conf.branch_lspace);
312 debug_pint("branch_rspace", conf.branch_rspace);
313 debug_pint("branch_tspace", conf.branch_tspace);
314 debug_pint("branch_bspace", conf.branch_bspace);
315 debug_pint("branch_connect", conf.branch_connect);
316 debug_pint("branch_margin", conf.branch_margin);
317 debug_pint("branch_dupbox", conf.branch_dupbox);
318
319 debug_pstring("title", conf.title);
320 debug_pint("title_x", conf.title_x);
321 debug_pint("title_y", conf.title_y);
322 debug_pfont("title_font", conf.title_font.gdfont);
323 debug_pstring("title_ttfont", conf.title_font.ttfont);
324 debug_pdouble("title_ttsize", conf.title_font.ttsize);
325 debug_pint("title_align", conf.title_align);
326 debug_pcolor("title_color", &conf.title_color);
327
328 debug_pint("margin_top", conf.margin_top);
329 debug_pint("margin_bottom", conf.margin_bottom);
330 debug_pint("margin_left", conf.margin_left);
331 debug_pint("margin_right", conf.margin_right);
332
333 debug_pint("image_type", conf.image_type);
334 debug_pint("image_quality", conf.image_quality);
335
336 debug_pstring("map_name", conf.map_name);
337 debug_pstring("map_branch_href", conf.map_branch_href);
338 debug_pstring("map_branch_alt", conf.map_branch_alt);
339 debug_pstring("map_rev_href", conf.map_rev_href);
340 debug_pstring("map_rev_alt", conf.map_rev_alt);
341 debug_pstring("map_diff_href", conf.map_diff_href);
342 debug_pstring("map_diff_alt", conf.map_diff_alt);
343
344 debug_pstring("expand[0]", conf.expand[0]);
345 debug_pstring("expand[1]", conf.expand[1]);
346 debug_pstring("expand[2]", conf.expand[2]);
347 debug_pstring("expand[3]", conf.expand[3]);
348 debug_pstring("expand[4]", conf.expand[4]);
349 debug_pstring("expand[5]", conf.expand[5]);
350 debug_pstring("expand[6]", conf.expand[6]);
351 debug_pstring("expand[7]", conf.expand[7]);
352 debug_pstring("expand[8]", conf.expand[8]);
353 debug_pstring("expand[9]", conf.expand[9]);
354 }
355 #endif
356
357 /*
358 **************************************************************************
359 * String collection routines
360 **************************************************************************
361 */
362 #define STRALLOCSIZE 128
363 static char *str;
364 static int nstr;
365 static int nastr;
366
367 static void reset_str(void)
368 {
369 nstr = 0;
370 }
371
372 static void add_str(int c)
373 {
374 if(nstr + 1 + 1 > nastr)
375 {
376 str = xrealloc(str, nastr+STRALLOCSIZE);
377 nastr += STRALLOCSIZE;
378 }
379 str[nstr++] = c;
380 }
381
382 static char *get_str(void)
383 {
384 if(!str)
385 return xstrdup("");
386
387 str[nstr] = '\0';
388 return xstrdup(str);
389 }
390
391 /*
392 **************************************************************************
393 * Input routines
394 **************************************************************************
395 */
396 static char *buf = NULL;
397 static int bufsize = 0;
398 static int bufalloc = 0;
399 static int bufpos = 0;
400 static int bufunput = -1;
401 static FILE *buffp;
402
403 static void set_input(FILE *fp, char *s)
404 {
405 assert((fp == NULL && s != NULL) || (fp != NULL && s == NULL));
406 buffp = fp;
407 bufsize = bufpos = 0;
408 if(s)
409 {
410 if(!buf)
411 {
412 bufalloc = 8192;
413 buf = xmalloc(bufalloc * sizeof(*buf));
414 }
415 bufsize = strlen(s);
416 assert(bufsize < bufalloc);
417 strcpy(buf, s);
418 }
419 }
420
421 static int get_input(void)
422 {
423 if(bufunput != -1)
424 {
425 int c = bufunput;
426 bufunput = -1;
427 return c;
428 }
429
430 if(bufpos < bufsize)
431 {
432 assert(buf != NULL);
433 retry_input:
434 return (int)((unsigned char)buf[bufpos++]);
435 }
436
437 if(!buf)
438 {
439 bufalloc = 8192;
440 buf = xmalloc(bufalloc * sizeof(*buf));
441 bufsize = bufpos = 0;
442 }
443 if(buffp)
444 {
445 bufsize = fread(buf, 1, bufalloc, buffp);
446 bufpos = 0;
447 if(!bufsize)
448 return EOF;
449 goto retry_input;
450 }
451 return EOF;
452 }
453
454 static void unget_input(int c)
455 {
456 bufunput = c;
457 }
458
459 /*
460 **************************************************************************
461 * Lexical scanner
462 **************************************************************************
463 */
464 static int config_lex(void)
465 {
466 int ch;
467 while(1)
468 {
469 ch = get_input();
470 if(ch == '\n')
471 line_number++;
472
473 if(isspace(ch))
474 continue;
475
476 switch(ch)
477 {
478 case EOF:
479 case '=':
480 case ';':
481 return ch;
482
483 case '#': /* Comment */
484 while((ch = get_input()) != '\n' && ch != EOF)
485 ;
486 if(ch != EOF)
487 unget_input(ch);
488 break;
489
490 case '"':
491 reset_str();
492 while(1)
493 {
494 char c[4];
495 ch = get_input();
496 switch(ch)
497 {
498 case '\\': /* Start an escape sequence */
499 switch(ch = get_input())
500 {
501 default: /* This includes '\\', '"' and embedded newlines */
502 add_str(ch);
503 break;
504 case 'a': add_str('\a'); break;
505 case 'b': add_str('\b'); break;
506 case 'f': add_str('\f'); break;
507 case 'n': add_str('\n'); break;
508 case 'r': add_str('\r'); break;
509 case 't': add_str('\t'); break;
510 case 'v': add_str('\v'); break;
511 case 'x':
512 case 'X': /* Hex escape */
513 c[0] = get_input();
514 c[1] = get_input();
515 c[2] = '\0';
516 if(!isxdigit(c[0]) || !isxdigit(c[1]))
517 yyerror("Invalid hex escape");
518 add_str((int)strtol(c, NULL, 16));
519 break;
520 case '0':
521 case '1':
522 case '2': /* Octal escape */
523 c[0] = ch;
524 c[1] = c[2] = c[3] = '\0';
525 if((ch = get_input()) >= '0' && ch <= '7')
526 c[1] = ch;
527 else
528 unget_input(ch);
529 if((ch = get_input()) >= '0' && ch <= '7')
530 c[2] = ch;
531 else
532 unget_input(ch);
533 add_str((int)strtol(c, NULL, 8));
534 break;
535 case EOF:
536 yyerror("Unexpected EOF in escape");
537 break;
538 }
539 break;
540 case '"':
541 yylval.str = get_str();
542 return TYPE_STRING;
543 case '\n':
544 yyerror("Newline in string");
545 break;
546 case EOF:
547 yyerror("Unexpected EOF in string");
548 break;
549 default:
550 add_str(ch);
551 break;
552 }
553 }
554 break;
555
556 default:
557 if(isalpha(ch) || ch == '_')
558 {
559 keyword_t skw;
560 keyword_t *kw;
561 /* Collect keyword */
562 reset_str();
563 add_str(ch);
564 while(1)
565 {
566 ch = get_input();
567 if(isalpha(ch) || ch == '_')
568 add_str(ch);
569 else
570 {
571 unget_input(ch);
572 break;
573 }
574 }
575 skw.keyword = get_str();
576 kw = bsearch(&skw, keywords, NKEYWORDS, sizeof(keywords[0]), cmp_kw);
577 if(!kw)
578 yyerror("Unknown keyword '%s'", skw.keyword);
579 xfree(skw.keyword);
580 if(kw->type == TYPE_VALUE)
581 {
582 yylval.i = kw->confref.val;
583 return TYPE_NUMBER;
584 }
585 yylval.kw = kw;
586 return TYPE_KEYWORD;
587 }
588 else if(isdigit(ch) || ch == '+' || ch == '-')
589 {
590 char *s;
591 char *eptr;
592 int type = TYPE_NUMBER;
593 /* Collect number */
594 reset_str();
595 add_str(ch);
596 while(1)
597 {
598 ch = get_input();
599 if(isxdigit(ch) || ch == 'x' || ch == 'X' || ch == '.') /* Not exact, but close enough */
600 add_str(ch);
601 else
602 {
603 unget_input(ch);
604 break;
605 }
606 if(ch == '.')
607 type = TYPE_DOUBLE;
608 }
609 s = get_str();
610 if(type == TYPE_DOUBLE)
611 {
612 yylval.d = strtod(s, &eptr);
613 if(*eptr)
614 yyerror("Invalid floating point number");
615 }
616 else
617 {
618 yylval.i = strtol(s, &eptr, 0);
619 if(*eptr)
620 yyerror("Invalid number");
621 }
622 xfree(s);
623 return type;
624 }
625 else
626 yyerror("Unmatched text '%c' (0x%02x)", isprint(ch) ? ch : ' ', ch);
627 break;
628 }
629 }
630 }
631
632 static void set_color(color_t *c, char *s)
633 {
634 char *cptr;
635 if(*s != '#' || strlen(s) != 7)
636 {
637 colorerror:
638 yyerror("Invalid color value");
639 }
640 c->b = strtol(s+5, &cptr, 16);
641 if(*cptr)
642 goto colorerror;
643 s[5] = '\0';
644 c->g = strtol(s+3, &cptr, 16);
645 if(*cptr)
646 goto colorerror;
647 s[3] = '\0';
648 c->r = strtol(s+1, &cptr, 16);
649 if(*cptr)
650 goto colorerror;
651 }
652
653 static gdFontPtr get_font(int id)
654 {
655 switch(id)
656 {
657 case 0: return gdFontTiny;
658 case 1: return gdFontSmall;
659 default:
660 case 2: return gdFontMediumBold;
661 case 3: return gdFontLarge;
662 case 4: return gdFontGiant;
663 }
664 }
665
666 /*
667 **************************************************************************
668 * The config parser
669 * Grammar:
670 * file : <Empty>
671 * | lines
672 * ;
673 *
674 * lines : line
675 * | lines line
676 * ;
677 *
678 * line : <keyword> '=' <value> ';'
679 * | ';'
680 * ;
681 **************************************************************************
682 */
683 static void config_parse(void)
684 {
685 int state = 0;
686 int token;
687 int t;
688 keyword_t *kw = NULL;
689
690 while(1)
691 {
692 token = config_lex();
693 if(token == EOF)
694 {
695 if(state)
696 yyerror("Unexpected EOF");
697 break;
698 }
699
700 switch(state)
701 {
702 case 0:
703 if(token == TYPE_KEYWORD)
704 {
705 kw = yylval.kw;
706 state = 1;
707 }
708 else if(token != ';')
709 yyerror("Keyword expected");
710 break;
711 case 1:
712 if(token != '=')
713 yyerror("'=' expected");
714 state = 2;
715 break;
716 case 2:
717 if(kw->type == TYPE_FONT || kw->type == TYPE_BOOLEAN)
718 t = TYPE_NUMBER;
719 else if(kw->type == TYPE_COLOR)
720 t = TYPE_STRING;
721 else
722 t = kw->type;
723
724 if(token == TYPE_NUMBER && kw->type == TYPE_DOUBLE)
725 {
726 /* Auto promote numbers to doubles if required */
727 yylval.d = (double)yylval.i;
728 token = TYPE_DOUBLE;
729 }
730
731 if(token != t)
732 {
733 char *e;
734 switch(kw->type)
735 {
736 case TYPE_STRING: e = "String"; break;
737 case TYPE_NUMBER: e = "Number"; break;
738 case TYPE_COLOR: e = "Color"; break;
739 case TYPE_FONT: e = "Font"; break;
740 case TYPE_BOOLEAN: e = "Boolean"; break;
741 case TYPE_DOUBLE: e = "Double"; break;
742 default: e = "Internal error: Unknown type"; break;
743 }
744 yyerror("%s expected", e);
745 }
746 #ifdef DEBUG
747 printf("processing: '%s'\n", kw->keyword);
748 #endif
749 switch(kw->type)
750 {
751 case TYPE_STRING:
752 *kw->confref.s = yylval.str;
753 break;
754 case TYPE_NUMBER:
755 *kw->confref.i = yylval.i;
756 break;
757 case TYPE_BOOLEAN:
758 if(yylval.i == -1)
759 *kw->confref.i = !*kw->confref.i;
760 else
761 *kw->confref.i = yylval.i != 0;
762 break;
763 case TYPE_COLOR:
764 set_color(kw->confref.c, yylval.str);
765 break;
766 case TYPE_FONT:
767 kw->confref.f->gdfont = get_font(yylval.i);
768 break;
769 case TYPE_DOUBLE:
770 *kw->confref.d = yylval.d;
771 break;
772 default:
773 yyerror("Internal error: Unknown type passed %d", kw->type);
774 break;
775 }
776 state = 3;
777 break;
778 case 3:
779 if(token != ';')
780 yyerror("';' expected");
781 state = 0;
782 break;
783 default:
784 yyerror("Internal error: invalid state %d", state);
785 break;
786 }
787 }
788 }
789
790 /*
791 **************************************************************************
792 * Configuration
793 **************************************************************************
794 */
795 void stack_option(const char *opt)
796 {
797 stacked_opts = xrealloc(stacked_opts, sizeof(*stacked_opts) * (nstacked_opts + 1));
798 stacked_opts[nstacked_opts] = xmalloc(strlen(opt) + 2);
799 strcpy(stacked_opts[nstacked_opts], opt);
800 strcat(stacked_opts[nstacked_opts], ";");
801 nstacked_opts++;
802 #ifdef DEBUG
803 printf("stacking option: '%s'\n", stacked_opts[nstacked_opts-1]);
804 #endif
805 }
806
807 void read_config(const char *path)
808 {
809 FILE *fp;
810
811 /* Make sure we have them sorted for bsearch */
812 qsort(keywords, NKEYWORDS, sizeof(keywords[0]), cmp_kw);
813
814 if(path)
815 {
816 if((fp = fopen(path, "r")) != NULL)
817 input_file = path;
818 }
819 else
820 {
821 if((fp = fopen("./" CONFFILENAME, "r")) == NULL)
822 {
823 if((fp = fopen(ETCDIR "/" CONFFILENAME, "r")) != NULL)
824 input_file = ETCDIR "/" CONFFILENAME;
825 }
826 else
827 input_file = "./" CONFFILENAME;
828 }
829
830 if(fp)
831 {
832 line_number = 1;
833 set_input(fp, NULL);
834 config_parse();
835 fclose(fp);
836 input_file = NULL;
837 }
838
839 if(nstacked_opts)
840 {
841 int i;
842 for(i = 0; i < nstacked_opts; i++)
843 {
844 line_number = 0;
845 set_input(NULL, stacked_opts[i]);
846 input_file = stacked_opts[i];
847 #ifdef DEBUG
848 printf("parsing stacked option: '%s'\n", stacked_opts[i]);
849 #endif
850 config_parse();
851 }
852 input_file = NULL;
853 }
854 #ifdef DEBUG
855 dump_config();
856 #endif
857 }

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0