summaryrefslogtreecommitdiff
path: root/openbox/cparse.l
diff options
context:
space:
mode:
authorDana Jansens <danakj@orodu.net>2003-03-22 14:41:19 +0000
committerDana Jansens <danakj@orodu.net>2003-03-22 14:41:19 +0000
commit56dc0446cd8a9a2685e1ffadb58b781e52e1a95a (patch)
treebb5743d179f9061979c1d01430c860843db377fd /openbox/cparse.l
parent4502cad55033f4a703cb562d27d637f41a05c5c6 (diff)
start the config system, add the config file parser
Diffstat (limited to 'openbox/cparse.l')
-rw-r--r--openbox/cparse.l95
1 files changed, 95 insertions, 0 deletions
diff --git a/openbox/cparse.l b/openbox/cparse.l
new file mode 100644
index 00000000..5e4b6d3e
--- /dev/null
+++ b/openbox/cparse.l
@@ -0,0 +1,95 @@
+%{
+#include <glib.h>
+#include "config.h"
+
+static int yylineno = 1;
+static gboolean haserror = FALSE;
+static ConfigEntry entry = { NULL, -1 };
+
+static void stringvalue();
+static void numbervalue();
+static void identifier();
+static void newline();
+static int yywrap();
+%}
+
+number [0-9]+
+string \"[^"\n]*\"
+identifier [a-zA-Z][a-zA-Z0-9_]*
+white [ \t]*
+assign {white}={white}
+
+%%
+
+{string}/{white}\n stringvalue();
+{number}/{white}\n numbervalue();
+^{identifier}/{assign} identifier();
+\n newline();
+=
+[ \t]
+. haserror = TRUE;
+
+%%
+
+static void stringvalue()
+{
+ if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
+ entry.type = Config_String;
+ entry.value.string = g_strdup(yytext+1); /* drop the left quote */
+ if (entry.value.string[yyleng-2] != '"')
+ printf("warning: improperly terminated string on line %d\n",
+ yylineno);
+ else
+ entry.value.string[yyleng-2] = '\0';
+ } else
+ haserror = TRUE;
+}
+
+static void numbervalue()
+{
+ if (!haserror && entry.name != NULL && (signed)entry.type < 0) {
+ entry.type = Config_Integer;
+ entry.value.integer = atoi(yytext);
+ } else
+ haserror = TRUE;
+}
+
+static void identifier()
+{
+ g_print("identifier: %s\n", yytext);
+ entry.name = g_strdup(yytext);
+ entry.type = -1;
+}
+
+static void newline()
+{
+ if (!haserror && entry.name != NULL && (signed)entry.type >= 0) {
+ if (!config_set(entry.name, entry.type, entry.value))
+ g_warning("Invalid option in config file: '%s'\n", entry.name);
+ } else {
+ printf("Parser error in config file on line %d\n", yylineno);
+ }
+ g_free(entry.name);
+ entry.name = NULL;
+ if (entry.type == Config_String)
+ g_free(entry.value.string);
+ entry.type = -1;
+
+ haserror = FALSE;
+ ++yylineno;
+}
+
+static int yywrap()
+{
+ g_free(entry.name);
+ entry.name = NULL;
+ if (entry.type == Config_String)
+ g_free(entry.value.string);
+ return 1;
+}
+
+void cparse_go(FILE *file)
+{
+ yyin = file;
+ yylex();
+}