]> Dogcows Code - chaz/openbox/blob - openbox/cparse.l
5e4b6d3ef75b969e962edc78caa77474410d5eca
[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 g_print("identifier: %s\n", yytext);
60 entry.name = g_strdup(yytext);
61 entry.type = -1;
62 }
63
64 static void newline()
65 {
66 if (!haserror && entry.name != NULL && (signed)entry.type >= 0) {
67 if (!config_set(entry.name, entry.type, entry.value))
68 g_warning("Invalid option in config file: '%s'\n", entry.name);
69 } else {
70 printf("Parser error in config file on line %d\n", yylineno);
71 }
72 g_free(entry.name);
73 entry.name = NULL;
74 if (entry.type == Config_String)
75 g_free(entry.value.string);
76 entry.type = -1;
77
78 haserror = FALSE;
79 ++yylineno;
80 }
81
82 static int yywrap()
83 {
84 g_free(entry.name);
85 entry.name = NULL;
86 if (entry.type == Config_String)
87 g_free(entry.value.string);
88 return 1;
89 }
90
91 void cparse_go(FILE *file)
92 {
93 yyin = file;
94 yylex();
95 }
This page took 0.035672 seconds and 4 git commands to generate.