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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0