]> Dogcows Code - chaz/openbox/blob - openbox/cparse.l
make better?
[chaz/openbox] / openbox / cparse.l
1 %{
2 #include <glib.h>
3 #include "config.h"
4
5 static char *filename;
6 static int lineno = 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 cparsewrap();
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 ^{white}# 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(cparsetext+1); /* drop the left quote */
46 if (entry.value.string[cparseleng-2] != '"')
47 g_warning("improperly terminated string on line %d",
48 lineno);
49 else
50 entry.value.string[cparseleng-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(cparsetext);
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", cparsetext) ||
73 !g_ascii_strcasecmp("yes", cparsetext) ||
74 !g_ascii_strcasecmp("on", cparsetext));
75 } else
76 haserror = TRUE;
77 }
78 }
79
80 static void identifier()
81 {
82 if (!comment) {
83 entry.name = g_strdup(cparsetext);
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("Parser error in '%s' on line %d\n", filename,
94 lineno);
95 } else if (haserror || entry.name != NULL || (signed)entry.type >= 0) {
96 g_warning("Parser error in '%s' on line %d", filename, lineno);
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 ++lineno;
108 }
109
110 static int cparsewrap()
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 *fname, FILE *file)
120 {
121 filename = fname;
122 cparsein = file;
123 cparselex();
124 }
This page took 0.042294 seconds and 4 git commands to generate.