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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0