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

  ViewVC Help
Powered by ViewVC 1.1.0 with CvsGraph 1.7.0