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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0