]> icculus.org git repositories - dana/openbox.git/blob - openbox/cparse.l
parse lines that start with '#' as comments
[dana/openbox.git] / openbox / cparse.l
1 %{
2 #include <glib.h>
3 #include "config.h"
4
5 static char *yyfilename;
6 static int yylineno = 1;
7 static gboolean haserror = FALSE;
8 static gboolean comment = FALSE;
9 static ConfigEntry entry = { NULL, -1 };
10
11 static void stringvalue();
12 static void numbervalue();
13 static void identifier();
14 static void newline();
15 static int yywrap();
16 %}
17
18 number [0-9]+
19 string \"[^"\n]*\"
20 identifier [a-zA-Z][a-zA-Z0-9_.]*
21 white [ \t]*
22 assign {white}={white}
23
24 %%
25
26 {string}/{white}\n stringvalue();
27 {number}/{white}\n numbervalue();
28 ^{identifier}/{assign} identifier();
29 \n newline();
30 ^# comment = TRUE;
31 =
32 [ \t]
33 . haserror = TRUE;
34
35 %%
36
37 static void stringvalue()
38 {
39     if (!comment) {
40         if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
41             entry.type = Config_String;
42             entry.value.string = g_strdup(yytext+1); /* drop the left quote */
43             if (entry.value.string[yyleng-2] != '"')
44                 printf("warning: improperly terminated string on line %d\n",
45                        yylineno);
46             else
47                 entry.value.string[yyleng-2] = '\0';
48         } else
49             haserror = TRUE;
50     }
51 }
52
53 static void numbervalue()
54 {
55     if (!comment) {
56         if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
57             entry.type = Config_Integer;
58             entry.value.integer = atoi(yytext);
59         } else
60             haserror = TRUE;
61     }
62 }
63
64 static void identifier()
65 {
66     if (!comment) {
67         entry.name = g_strdup(yytext);
68         entry.type = -1;
69     }
70 }
71
72 static void newline()
73 {
74     if (!comment) {
75         if (!haserror && entry.name != NULL && (signed)entry.type >= 0) {
76             if (!config_set(entry.name, entry.type, entry.value))
77                 g_warning("Invalid option in '%s': '%s'\n",
78                           yyfilename, entry.name);
79         } else {
80             printf("Parser error in '%s' on line %d\n", yyfilename, yylineno);
81         }
82         g_free(entry.name);
83         entry.name = NULL;
84         if (entry.type == Config_String)
85             g_free(entry.value.string);
86         entry.type = -1;
87
88         haserror = FALSE;
89     }
90     ++yylineno;
91 }
92
93 static int yywrap()
94 {
95     g_free(entry.name);
96     entry.name = NULL;
97     if (entry.type == Config_String)
98         g_free(entry.value.string);
99     return 1;
100 }
101
102 void cparse_go(char *filename, FILE *file)
103 {
104     yyfilename = filename;
105     yyin = file;
106     yylex();
107 }