From 63eb6c79f2ea9c40fda9aeef2b89f1a16dfa97cf Mon Sep 17 00:00:00 2001 From: wangjianyu3 Date: Tue, 2 Dec 2025 15:35:59 +0800 Subject: [PATCH] system/nxinit: Extract init_parse_config_buffer() Extract the init_parse_config_buffer() interface from the init_parse_config_file() function. Signed-off-by: wangjianyu3 --- system/nxinit/parser.c | 158 ++++++++++++++++++++++++++++------------- 1 file changed, 109 insertions(+), 49 deletions(-) diff --git a/system/nxinit/parser.c b/system/nxinit/parser.c index 7d8052110..4afc5bf61 100644 --- a/system/nxinit/parser.c +++ b/system/nxinit/parser.c @@ -32,11 +32,116 @@ #include #include #include +#include #include #include "init.h" #include "parser.h" +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +static int init_parse_config_lines(FAR const struct parser_s *parser, + FAR const struct parser_s **cur, + FAR size_t *line, + FAR char *buf, FAR size_t *len) +{ + bool create = false; + FAR char *nl; + int ret; + + while ((nl = memchr(buf, '\n', *len))) + { + *(nl++) = '\0'; + *len -= nl - buf; + init_debug("Line %-3zu '%s'", ++*line, buf); + if (*buf == '\0') + { + continue; + } + + /* Skip empty lines and lines containing only whitespace */ + + for (ret = 0; buf[ret] && isblank(buf[ret]); ret++); + + if (buf[ret] == '\0') + { + memmove(buf, nl, *len); + continue; + } + + for (ret = 0; parser[ret].key; ret++) + { + if (!strncmp(parser[ret].key, buf, strlen(parser[ret].key))) + { + create = true; + *cur = &parser[ret]; + init_debug("New section (%s)", parser[ret].key); + break; + } + } + + if (*cur == NULL) + { + return -EINVAL; + } + + ret = (*cur)->parse(*cur, create, buf); + create = false; + if (ret < 0) + { + return ret; + } + + memmove(buf, nl, *len); + } + + return 0; +} + +static int init_parse_config_buffer(FAR const struct parser_s *parser, + FAR const char *buf, size_t len) +{ + char tmp[CONFIG_SYSTEM_NXINIT_RC_LINE_MAX]; + FAR const struct parser_s *cur = NULL; + size_t line = 0; + size_t off = 0; + size_t n = 0; + size_t r; + int ret; + + for (; ; ) + { + r = MIN(len - off, sizeof(tmp)); + memcpy(&tmp[n], &buf[off], r); + if (r == 0) + { + if (n == 0) + { + break; + } + + tmp[n++] = '\n'; + } + + n += r; + off += r; + ret = init_parse_config_lines(parser, &cur, &line, tmp, &n); + if (ret < 0) + { + return ret; + } + + if (n == sizeof(tmp)) + { + return -E2BIG; + } + } + + return 0; +} + /**************************************************************************** * Public Functions ****************************************************************************/ @@ -139,14 +244,10 @@ int init_parse_config_file(FAR const struct parser_s *parser, { char buf[CONFIG_SYSTEM_NXINIT_RC_LINE_MAX]; FAR const struct parser_s *cur = NULL; - bool create = false; - FAR char *nl; + size_t line = 0; size_t n = 0; int ret = 0; int fd; -#ifdef CONFIG_SYSTEM_NXINIT_DEBUG - int line = 0; -#endif init_debug("Parsing %s", file); @@ -181,51 +282,10 @@ int init_parse_config_file(FAR const struct parser_s *parser, } n += r; - while ((nl = memchr(buf, '\n', n))) + ret = init_parse_config_lines(parser, &cur, &line, buf, &n); + if (ret < 0) { - *(nl++) = '\0'; - n -= nl - buf; - init_debug("Line %3d: '%s'", ++line, buf); - if (*buf == '\0') - { - continue; - } - - /* Skip empty lines and lines containing only whitespace */ - - for (ret = 0; buf[ret] && isblank(buf[ret]); ret++); - - if (buf[ret] == '\0') - { - memmove(buf, nl, n); - continue; - } - - for (ret = 0; parser[ret].key; ret++) - { - if (!strncmp(parser[ret].key, buf, strlen(parser[ret].key))) - { - create = true; - cur = &parser[ret]; - init_debug("New section (%s)", parser[ret].key); - break; - } - } - - if (cur == NULL) - { - ret = -EINVAL; - goto out; - } - - ret = cur->parse(cur, create, buf); - create = false; - if (ret < 0) - { - goto out; - } - - memmove(buf, nl, n); + goto out; } if (n == sizeof(buf))