diff --git a/Makefile b/Makefile index 77e25af99451d05814b50dd5bd3bb1bd45b95d3f..ec15b0ab6ddbd883bc4ca0ed71b4eb9dd5c51c95 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ CCFLAGS+=-Wall -Werror -I. -g LDFLAGS+=-Lbuild/ -lmoberg MODULES:=$(wildcard modules/*) -LDFLAGS_parse_config=-ldl moberg_driver.o +LDFLAGS_parse_config=-ldl -export-dynamic all: $(LIBRARIES:%=build/%) $(MODULES) test/test_c parse_config echo $(MODULES) @@ -14,11 +14,11 @@ build/libmoberg.so: moberg.c Makefile | build build: mkdir $@ -%: %.o - $(CC) $(LDFLAGS) $(LDFLAGS_$(*)) -o $@ $< +%: build/%.o Makefile + $(CC) $(LDFLAGS) $(LDFLAGS_$(*)) -o $@ $(filter %.o,$^) -%.o: %.c - $(CC) $(CCFLAGS) -c $< +build/%.o: %.c Makefile + $(CC) $(CCFLAGS) -c -o $@ $< .PHONY: $(MODULES) @@ -40,5 +40,6 @@ clean: rm build/* -parse_config: moberg_config_parser.h -parse_config: moberg_driver.o +build/parse_config.o: moberg_config_parser.h +parse_config: build/moberg_driver.o +parse_config: build/parse_config.o diff --git a/moberg_config_parser.h b/moberg_config_parser.h index 806af0de6a7919501873271c5d80c2b9413ef5b1..ab68b415a3cb8af9f6f6c6fb8003c1f7e87405f5 100644 --- a/moberg_config_parser.h +++ b/moberg_config_parser.h @@ -7,13 +7,13 @@ struct moberg_config_parser_token; enum moberg_config_parser_token_kind { tok_none, tok_LBRACE = '{', - tok_RBRACE, - tok_LBRACKET, - tok_RBRACKET, - tok_EQUAL, - tok_COLON, - tok_SEMICOLON, - tok_INTEGER, + tok_RBRACE = '}', + tok_LBRACKET = '[', + tok_RBRACKET = ']', + tok_EQUAL = '=', + tok_COLON = ':', + tok_SEMICOLON = ';', + tok_INTEGER = 256, tok_CONFIG, tok_MAP, tok_ANALOGIN, @@ -22,15 +22,19 @@ enum moberg_config_parser_token_kind { tok_DIGITALOUT, tok_ENCODERIN, tok_IDENT, + tok_STRING, +}; + +struct moberg_config_parser_ident { + int length; + const char *value; }; struct moberg_config_parser_token { enum moberg_config_parser_token_kind kind; union { - struct moberg_config_parser_ident { - int length; - const char *value; - } ident; + struct moberg_config_parser_ident ident; + struct moberg_config_parser_ident string; struct moberg_config_parser_integer { int value; } integer; @@ -42,4 +46,8 @@ int moberg_config_parser_acceptsym( enum moberg_config_parser_token_kind kind, struct moberg_config_parser_token *token); +int moberg_config_parser_peeksym( + struct moberg_config_parser_context *c, + struct moberg_config_parser_token *token); + #endif diff --git a/modules/comedi/comedi.c b/modules/comedi/comedi.c index a34e526b05e21e971d9f648bceb3e6a454178b4a..c40aef5e676f47f832673a90a5958f1b57266a0c 100644 --- a/modules/comedi/comedi.c +++ b/modules/comedi/comedi.c @@ -1,20 +1,50 @@ #include <moberg_config_parser.h> #include <moberg_driver.h> #include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#define acceptsym moberg_config_parser_acceptsym +#define peeksym moberg_config_parser_peeksym + +typedef struct moberg_config_parser_token token_t; +typedef struct moberg_config_parser_ident ident_t; static struct moberg_driver_config parse_config( - struct moberg_config_parser_context *context) + struct moberg_config_parser_context *c) { struct moberg_driver_config result; - + token_t t; printf("PARSE_CONFIG %s\n", __FILE__); -/* - const char *buf = context->buf; - while (*buf && *buf != '}') { - buf++; + printf("LBRACE %d", acceptsym(c, tok_LBRACE, &t)); + for (;;) { + if (acceptsym(c, tok_IDENT, &t) || + acceptsym(c, tok_CONFIG, &t)) { + const char *v = t.u.ident.value; + int l = t.u.ident.length; + if (strncmp("device", v, l) == 0) { + printf("DEVICE\n"); + } else if (strncmp("modprobe", v, l) == 0) { + printf("MODPROBE\n"); + } else if (strncmp("comedi", v, l) == 0) { + printf("CONFIG\n"); + } + acceptsym(c, tok_EQUAL, NULL); + for (;;) { + peeksym(c, &t); + acceptsym(c, t.kind, NULL); + printf("%d\n", t.kind); + if (t.kind == tok_SEMICOLON) { break; } + } + } else if (acceptsym(c, tok_RBRACE, NULL)) { + break; + } else { + goto err; + } } - context->buf = buf + 1; -*/ + return result; +err: + exit(1); return result; } diff --git a/parse_config.c b/parse_config.c index 45b6f654567875fec2025c6c19a964ac7fa860e1..1f65d463479293c8dd3fd780b30b63caf3a10f2c 100644 --- a/parse_config.c +++ b/parse_config.c @@ -65,6 +65,7 @@ out: ; static const void nextsym_integer(context_t *c) { + c->token.kind = tok_INTEGER; c->token.u.integer.value = 0; while (*c->p && '0' <= *c->p && *c->p <= '9') { c->token.u.integer.value *= 10; @@ -74,6 +75,27 @@ static const void nextsym_integer(context_t *c) printf("INTEGER: %d\n", c->token.u.integer.value); } +static const void nextsym_string(context_t *c) +{ + printf("STTRING"); + c->token.kind = tok_STRING; + c->p++; + c->token.u.string.value = c->p; + c->token.u.string.length = 0; + while (*c->p && *c->p != '"') { + if (*c->p == '\\') { + c->token.u.string.length++; + c->p++; + } + if (*c->p) { + c->token.u.string.length++; + c->p++; + } + } + c->p++; + printf("STRING: %.*s\n", c->token.u.string.length, c->token.u.string.value); +} + static int nextsym(context_t *c) { c->token.kind = tok_none; @@ -124,6 +146,9 @@ static int nextsym(context_t *c) c->token.kind = tok_SEMICOLON; c->p++; break; + case '"': + nextsym_string(c); + break; case 'a'...'z': case 'A'...'Z': case '_': @@ -138,7 +163,7 @@ static int nextsym(context_t *c) break; } } - printf("TOKEN %d\n\n", c->token.kind); + printf("TOKEN %d %c\n\n", c->token.kind, c->token.kind<255?c->token.kind:' '); if (c->token.kind != tok_none) { return 1; } else { @@ -160,6 +185,15 @@ int moberg_config_parser_acceptsym(context_t *c, return 0; } +int moberg_config_parser_peeksym(context_t *c, + token_t *token) +{ + if (token) { + *token = c->token; + } + return *c->p != 0; +} + static int parse_map_range(context_t *c) { token_t low, high; @@ -260,6 +294,7 @@ static int parse_config(context_t *c) return 1; err: printf("Failed!!"); + exit (1); return 0; } diff --git a/test/a/moberg.conf b/test/a/moberg.conf index 39fedd1d115916c074957f2bf754e93adf03a623..270ba54699276ef36f7a0fe9c04201f44bb7fbc0 100644 --- a/test/a/moberg.conf +++ b/test/a/moberg.conf @@ -1,7 +1,7 @@ comedi { config { /* Parsed by parse_config in libmoberg_comedi.so */ - device = /dev/comedi0 ; + device = "/dev/comedi0" ; modprobe = [ comedi 8255 comedi_fc mite ni_tio ni_tiocmd ni_pcimio ] ; config = [ ni_pcimio ] ; } @@ -12,7 +12,7 @@ comedi { serial2002 { config { /* Parsed by parse_config in libmoberg_serial2002.so */ - device = /dev/ttyS0 ; + device = "/dev/ttyS0" ; baud = 115200 ; } /* Moberg mapping[indices] = {driver specific}[indices] diff --git a/test/test_c b/test/test_c deleted file mode 100755 index c438e6bf1ca8839f32a19270463fce4a7e5704a8..0000000000000000000000000000000000000000 Binary files a/test/test_c and /dev/null differ