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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0