]> Dogcows Code - chaz/openbox/blob - openbox/cparse.l
properly parse comments!
[chaz/openbox] / 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 boolvalue();
14 static void identifier();
15 static void newline();
16 static int yywrap();
17 %}
18
19 number [0-9]+
20 string \"[^"\n]*\"
21 identifier [a-zA-Z][a-zA-Z0-9_.]*
22 white [ \t]*
23 assign {white}={white}
24 bool ([tT][rR][uU][eE]|[fF][aA][lL][sS][eE]|[yY][eE][sS]|[nN][oO]|[oO][nN]|[oO][fF][fF])
25
26 %%
27
28 ^# comment = TRUE;
29 {bool}/{white}\n boolvalue();
30 {string}/{white}\n stringvalue();
31 {number}/{white}\n numbervalue();
32 ^{identifier}/{assign} identifier();
33 \n newline();
34 =
35 [ \t]
36 . if (!comment) haserror = TRUE;
37
38 %%
39
40 static void stringvalue()
41 {
42 if (!comment) {
43 if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
44 entry.type = Config_String;
45 entry.value.string = g_strdup(yytext+1); /* drop the left quote */
46 if (entry.value.string[yyleng-2] != '"')
47 printf("warning: improperly terminated string on line %d\n",
48 yylineno);
49 else
50 entry.value.string[yyleng-2] = '\0';
51 } else
52 haserror = TRUE;
53 }
54 }
55
56 static void numbervalue()
57 {
58 if (!comment) {
59 if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
60 entry.type = Config_Integer;
61 entry.value.integer = atoi(yytext);
62 } else
63 haserror = TRUE;
64 }
65 }
66
67 static void boolvalue()
68 {
69 if (!comment) {
70 if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
71 entry.type = Config_Bool;
72 entry.value.bool = (!g_ascii_strcasecmp("true", yytext) ||
73 !g_ascii_strcasecmp("yes", yytext) ||
74 !g_ascii_strcasecmp("on", yytext));
75 } else
76 haserror = TRUE;
77 }
78 }
79
80 static void identifier()
81 {
82 if (!comment) {
83 entry.name = g_strdup(yytext);
84 entry.type = -1;
85 }
86 }
87
88 static void newline()
89 {
90 if (!comment) {
91 if (!haserror && entry.name != NULL && (signed)entry.type >= 0) {
92 if (!config_set(entry.name, entry.type, entry.value))
93 g_warning("Invalid option in '%s': '%s'\n",
94 yyfilename, entry.name);
95 } else {
96 printf("Parser error in '%s' on line %d\n", yyfilename, yylineno);
97 }
98 g_free(entry.name);
99 entry.name = NULL;
100 if (entry.type == Config_String)
101 g_free(entry.value.string);
102 entry.type = -1;
103
104 haserror = FALSE;
105 }
106 comment = FALSE;
107 ++yylineno;
108 }
109
110 static int yywrap()
111 {
112 g_free(entry.name);
113 entry.name = NULL;
114 if (entry.type == Config_String)
115 g_free(entry.value.string);
116 return 1;
117 }
118
119 void cparse_go(char *filename, FILE *file)
120 {
121 yyfilename = filename;
122 yyin = file;
123 yylex();
124 }
This page took 0.040229 seconds and 4 git commands to generate.