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