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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0