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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0