[PATCH 4/6] kconfig: introduce getline_stripped() helper

From: Masahiro Yamada
Date: Sat Nov 18 2023 - 02:59:34 EST


Currently, newline characters are stripped away in multiple places
on the caller.

Doing that in the callee is helpful for further cleanups.

Signed-off-by: Masahiro Yamada <masahiroy@xxxxxxxxxx>
---

scripts/kconfig/confdata.c | 40 +++++++++++++++++++++++++-------------
1 file changed, 26 insertions(+), 14 deletions(-)

diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
index b6a90f6baea1..795ac6c9378f 100644
--- a/scripts/kconfig/confdata.c
+++ b/scripts/kconfig/confdata.c
@@ -337,12 +337,32 @@ static ssize_t compat_getline(char **lineptr, size_t *n, FILE *stream)
return -1;
}

+/* like getline(), but the newline character is stripped away */
+static ssize_t getline_stripped(char **lineptr, size_t *n, FILE *stream)
+{
+ ssize_t len;
+
+ len = compat_getline(lineptr, n, stream);
+
+ if (len > 0 && (*lineptr)[len - 1] == '\n') {
+ len--;
+ (*lineptr)[len] = '\0';
+
+ if (len > 0 && (*lineptr)[len - 1] == '\r') {
+ len--;
+ (*lineptr)[len] = '\0';
+ }
+ }
+
+ return len;
+}
+
int conf_read_simple(const char *name, int def)
{
FILE *in = NULL;
char *line = NULL;
size_t line_asize = 0;
- char *p, *p2, *val;
+ char *p, *val;
struct symbol *sym;
int i, def_flags;
const char *warn_unknown, *werror, *sym_name;
@@ -421,7 +441,7 @@ int conf_read_simple(const char *name, int def)
}
}

- while (compat_getline(&line, &line_asize, in) != -1) {
+ while (getline_stripped(&line, &line_asize, in) != -1) {
conf_lineno++;
if (line[0] == '#') {
if (line[1] != ' ')
@@ -443,19 +463,11 @@ int conf_read_simple(const char *name, int def)
p = strchr(sym_name, '=');
if (!p)
continue;
- *p++ = 0;
- val = p;
- p2 = strchr(p, '\n');
- if (p2) {
- *p2-- = 0;
- if (*p2 == '\r')
- *p2 = 0;
- }
+ *p = 0;
+ val = p + 1;
} else {
- if (line[0] != '\r' && line[0] != '\n')
- conf_warning("unexpected data: %.*s",
- (int)strcspn(line, "\r\n"), line);
-
+ if (line[0] != '\0')
+ conf_warning("unexpected data: %s", line);
continue;
}

--
2.40.1