diff --git a/device.h b/device.h index b41e66a..28166b1 100644 --- a/device.h +++ b/device.h @@ -8,8 +8,10 @@ struct cdb_assist; struct fastboot; struct fastboot_ops; struct device; +struct device_parser; struct control_ops { + void *(*parse_options)(struct device_parser *dp); void *(*open)(struct device *dev); void (*close)(struct device *dev); int (*power)(struct device *dev, bool on); @@ -28,6 +30,7 @@ struct console_ops { struct device { char *board; char *control_dev; + void *control_options; char *console_dev; char *name; char *serial; diff --git a/device_parser.c b/device_parser.c index d9f2800..4d9ab2d 100644 --- a/device_parser.c +++ b/device_parser.c @@ -33,6 +33,7 @@ #include #include "device.h" +#include "device_parser.h" #define TOKEN_LENGTH 16384 @@ -49,12 +50,13 @@ static void nextsym(struct device_parser *dp) } } -static int accept(struct device_parser *dp, int type, char *scalar) +int device_parser_accept(struct device_parser *dp, int type, + char *scalar, size_t scalar_len) { if (dp->event.type == type) { - if (scalar) { - strncpy(scalar, (char *)dp->event.data.scalar.value, TOKEN_LENGTH - 1); - scalar[TOKEN_LENGTH - 1] = '\0'; + if (scalar && scalar_len > 0) { + strncpy(scalar, (char *)dp->event.data.scalar.value, scalar_len - 1); + scalar[scalar_len - 1] = '\0'; } yaml_event_delete(&dp->event); @@ -65,9 +67,10 @@ static int accept(struct device_parser *dp, int type, char *scalar) } } -static bool expect(struct device_parser *dp, int type, char *scalar) +bool device_parser_expect(struct device_parser *dp, int type, + char *scalar, size_t scalar_len) { - if (accept(dp, type, scalar)) { + if (device_parser_accept(dp, type, scalar, scalar_len)) { return true; } @@ -103,17 +106,17 @@ static void parse_board(struct device_parser *dp) dev = calloc(1, sizeof(*dev)); - while (accept(dp, YAML_SCALAR_EVENT, key)) { + while (device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) { if (!strcmp(key, "users")) { dev->users = calloc(1, sizeof(*dev->users)); list_init(dev->users); - if (accept(dp, YAML_SCALAR_EVENT, value)) + if (device_parser_accept(dp, YAML_SCALAR_EVENT, value, 0)) continue; - expect(dp, YAML_SEQUENCE_START_EVENT, NULL); + device_parser_expect(dp, YAML_SEQUENCE_START_EVENT, NULL, 0); - while (accept(dp, YAML_SCALAR_EVENT, key)) { + while (device_parser_accept(dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) { struct device_user *user = calloc(1, sizeof(*user)); user->username = strdup(key); @@ -121,12 +124,12 @@ static void parse_board(struct device_parser *dp) list_add(dev->users, &user->node); } - expect(dp, YAML_SEQUENCE_END_EVENT, NULL); + device_parser_expect(dp, YAML_SEQUENCE_END_EVENT, NULL, 0); continue; } - expect(dp, YAML_SCALAR_EVENT, value); + device_parser_expect(dp, YAML_SCALAR_EVENT, value, TOKEN_LENGTH); if (!strcmp(key, "board")) { dev->board = strdup(value); @@ -212,25 +215,25 @@ int device_parser(const char *path) nextsym(&dp); - expect(&dp, YAML_STREAM_START_EVENT, NULL); + device_parser_expect(&dp, YAML_STREAM_START_EVENT, NULL, 0); - expect(&dp, YAML_DOCUMENT_START_EVENT, NULL); - expect(&dp, YAML_MAPPING_START_EVENT, NULL); + device_parser_expect(&dp, YAML_DOCUMENT_START_EVENT, NULL, 0); + device_parser_expect(&dp, YAML_MAPPING_START_EVENT, NULL, 0); - if (accept(&dp, YAML_SCALAR_EVENT, key)) { - expect(&dp, YAML_SEQUENCE_START_EVENT, NULL); + if (device_parser_accept(&dp, YAML_SCALAR_EVENT, key, TOKEN_LENGTH)) { + device_parser_expect(&dp, YAML_SEQUENCE_START_EVENT, NULL, 0); - while (accept(&dp, YAML_MAPPING_START_EVENT, NULL)) { + while (device_parser_accept(&dp, YAML_MAPPING_START_EVENT, NULL, 0)) { parse_board(&dp); - expect(&dp, YAML_MAPPING_END_EVENT, NULL); + device_parser_expect(&dp, YAML_MAPPING_END_EVENT, NULL, 0); } - expect(&dp, YAML_SEQUENCE_END_EVENT, NULL); + device_parser_expect(&dp, YAML_SEQUENCE_END_EVENT, NULL, 0); } - expect(&dp, YAML_MAPPING_END_EVENT, NULL); - expect(&dp, YAML_DOCUMENT_END_EVENT, NULL); - expect(&dp, YAML_STREAM_END_EVENT, NULL); + device_parser_expect(&dp, YAML_MAPPING_END_EVENT, NULL, 0); + device_parser_expect(&dp, YAML_DOCUMENT_END_EVENT, NULL, 0); + device_parser_expect(&dp, YAML_STREAM_END_EVENT, NULL, 0); yaml_event_delete(&dp.event); yaml_parser_delete(&dp.parser); diff --git a/device_parser.h b/device_parser.h index 2fcd770..15f6790 100644 --- a/device_parser.h +++ b/device_parser.h @@ -1,6 +1,13 @@ #ifndef __DEVICE_PARSER_H__ #define __DEVICE_PARSER_H__ +struct device_parser; + +int device_parser_accept(struct device_parser *dp, int type, + char *scalar, size_t scalar_len); +bool device_parser_expect(struct device_parser *dp, int type, + char *scalar, size_t scalar_len); + int device_parser(const char *path); #endif