From b25feb3386a1435d78d3918742cff1df291cfcdb Mon Sep 17 00:00:00 2001 From: Stanislav Zidek Date: Sat, 31 Oct 2020 21:53:34 +0100 Subject: New API for parsing configuration from string Fixes T248 Signed-off-by: Stanislav Zidek Reviewed-by: Jakub Jelen Reviewed-by: Anderson Toshiyuki Sasaki --- src/config.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'src/config.c') diff --git a/src/config.c b/src/config.c index 79a64339..198a4726 100644 --- a/src/config.c +++ b/src/config.c @@ -1170,3 +1170,57 @@ int ssh_config_parse_file(ssh_session session, const char *filename) fclose(f); return 0; } + +/* @brief Parse configuration string and set the options to the given session + * + * @params[in] session The ssh session + * @params[in] input Null terminated string containing the configuration + * + * @returns SSH_OK on successful parsing the configuration string, + * SSH_ERROR on error + */ +int ssh_config_parse_string(ssh_session session, const char *input) +{ + char line[MAX_LINE_SIZE] = {0}; + const char *c = input, *line_start = input; + unsigned int line_num = 0, line_len; + int parsing, rv; + + SSH_LOG(SSH_LOG_DEBUG, "Reading configuration data from string:"); + SSH_LOG(SSH_LOG_DEBUG, "START\n%s\nEND", input); + + parsing = 1; + while (1) { + line_num++; + line_start = c; + c = strchr(line_start, '\n'); + if (c == NULL) { + /* if there is no newline in the end of the string */ + c = strchr(line_start, '\0'); + } + if (c == NULL) { + /* should not happen, would mean a string without trailing '\0' */ + SSH_LOG(SSH_LOG_WARN, "No trailing '\\0' in config string"); + return SSH_ERROR; + } + line_len = c - line_start; + if (line_len > MAX_LINE_SIZE - 1) { + SSH_LOG(SSH_LOG_WARN, "Line %u too long: %u characters", + line_num, line_len); + return SSH_ERROR; + } + memcpy(line, line_start, line_len); + line[line_len] = '\0'; + SSH_LOG(SSH_LOG_DEBUG, "Line %u: %s", line_num, line); + rv = ssh_config_parse_line(session, line, line_num, &parsing); + if (rv < 0) { + return SSH_ERROR; + } + if (*c == '\0') { + break; + } + c++; + } + + return SSH_OK; +} -- cgit v1.2.3