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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0