Skip to content

Commit

Permalink
Export device_parser struct and functions
Browse files Browse the repository at this point in the history
Export the functions and struct to enable devices to parse
yaml aswell via the parse_options op to be stored in the
device control_options device property.

Signed-off-by: Neil Armstrong <[email protected]>
  • Loading branch information
superna9999 committed Oct 30, 2023
1 parent fbdc677 commit 55902b7
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 deletions.
3 changes: 3 additions & 0 deletions device.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -28,6 +30,7 @@ struct console_ops {
struct device {
char *board;
char *control_dev;
void *control_options;
char *console_dev;
char *name;
char *serial;
Expand Down
49 changes: 26 additions & 23 deletions device_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include <yaml.h>

#include "device.h"
#include "device_parser.h"

#define TOKEN_LENGTH 16384

Expand All @@ -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);
Expand All @@ -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;
}

Expand Down Expand Up @@ -103,30 +106,30 @@ 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);

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);
Expand Down Expand Up @@ -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);
Expand Down
7 changes: 7 additions & 0 deletions device_parser.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 55902b7

Please sign in to comment.