From 77725291efd609339cc560bec646782bc09ffb90 Mon Sep 17 00:00:00 2001 From: David Oberhollenzer Date: Wed, 11 Apr 2018 00:04:33 +0200 Subject: Make line buffer static, move error reporting to rdline - Expose rdline_t struct and move a lot of extra processing to rdline code - Make line buffer statically allocated - Simplify rdsvc code Signed-off-by: David Oberhollenzer --- lib/src/rdsvc.c | 191 ++++++++++++++++++++++++-------------------------------- 1 file changed, 83 insertions(+), 108 deletions(-) (limited to 'lib/src/rdsvc.c') diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c index 29c9019..e911724 100644 --- a/lib/src/rdsvc.c +++ b/lib/src/rdsvc.c @@ -28,17 +28,17 @@ #include "service.h" #include "util.h" -static int try_unescape(char *arg, const char *filename, size_t lineno) +static int try_unescape(char *arg, rdline_t *rd) { if (unescape(arg)) { fprintf(stderr, "%s: %zu: malformed string constant\n", - filename, lineno); + rd->filename, rd->lineno); return -1; } return 0; } -static char **try_split_argv(char *str, const char *filename, size_t lineno) +static char **try_split_argv(char *str, rdline_t *rd) { char **argv = split_argv(str); @@ -46,11 +46,11 @@ static char **try_split_argv(char *str, const char *filename, size_t lineno) switch (errno) { case EINVAL: fprintf(stderr, "%s: %zu: malformed string constant\n", - filename, lineno); + rd->filename, rd->lineno); break; default: - fprintf(stderr, "%s: %zu: %s\n", filename, lineno, - strerror(errno)); + fprintf(stderr, "%s: %zu: %s\n", + rd->filename, rd->lineno, strerror(errno)); break; } } @@ -58,43 +58,56 @@ static char **try_split_argv(char *str, const char *filename, size_t lineno) return argv; } -static int svc_desc(service_t *svc, char *arg, - const char *filename, size_t lineno) +static char *try_strdup(const char *str, rdline_t *rd) { - if (try_unescape(arg, filename, lineno)) + char *out = strdup(str); + + if (out == NULL) { + fprintf(stderr, "%s: %zu: out of memory\n", + rd->filename, rd->lineno); + } + return out; +} + +static int svc_desc(service_t *svc, char *arg, rdline_t *rd) +{ + if (try_unescape(arg, rd)) return -1; - svc->desc = arg; - return 0; + svc->desc = try_strdup(arg, rd); + return svc->desc == NULL ? -1 : 0; } -static int svc_tty(service_t *svc, char *arg, - const char *filename, size_t lineno) +static int svc_tty(service_t *svc, char *arg, rdline_t *rd) { - if (try_unescape(arg, filename, lineno)) + if (try_unescape(arg, rd)) return -1; - svc->ctty = arg; - return 0; + svc->ctty = try_strdup(arg, rd); + return svc->ctty == NULL ? -1 : 0; } -static int svc_exec(service_t *svc, char *arg, - const char *filename, size_t lineno) +static int svc_exec(service_t *svc, char *arg, rdline_t *rd) { exec_t *e, *end; - char **argv; - - argv = try_split_argv(arg, filename, lineno); - if (argv == NULL) - return -1; e = calloc(1, sizeof(*e)); if (e == NULL) { - fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno); - free(argv); + fprintf(stderr, "%s: %zu: out of memory\n", + rd->filename, rd->lineno); return -1; } - e->argv = argv; - e->raw_argv = arg; + e->raw_argv = try_strdup(arg, rd); + if (e->raw_argv == NULL) { + free(e); + return -1; + } + + e->argv = try_split_argv(e->raw_argv, rd); + if (e->argv == NULL) { + free(e->raw_argv); + free(e); + return -1; + } if (svc->exec == NULL) { svc->exec = e; @@ -106,47 +119,50 @@ static int svc_exec(service_t *svc, char *arg, return 0; } -static int svc_before(service_t *svc, char *arg, - const char *filename, size_t lineno) +static int svc_before(service_t *svc, char *arg, rdline_t *rd) { if (svc->before != NULL) { fprintf(stderr, "%s: %zu: 'before' dependencies respecified\n", - filename, lineno); + rd->filename, rd->lineno); return -1; } - svc->before = try_split_argv(arg, filename, lineno); + svc->raw_before = try_strdup(arg, rd); + if (svc->raw_before == NULL) + return -1; + + svc->before = try_split_argv(svc->raw_before, rd); if (svc->before == NULL) return -1; - svc->raw_before = arg; return 0; } -static int svc_after(service_t *svc, char *arg, - const char *filename, size_t lineno) +static int svc_after(service_t *svc, char *arg, rdline_t *rd) { if (svc->after != NULL) { fprintf(stderr, "%s: %zu: 'after' dependencies respecified\n", - filename, lineno); + rd->filename, rd->lineno); return -1; } - svc->after = try_split_argv(arg, filename, lineno); + svc->raw_after = try_strdup(arg, rd); + if (svc->raw_after == NULL) + return -1; + + svc->after = try_split_argv(svc->raw_after, rd); if (svc->after == NULL) return -1; - svc->raw_after = arg; return 0; } -static int svc_type(service_t *svc, char *arg, - const char *filename, size_t lineno) +static int svc_type(service_t *svc, char *arg, rdline_t *rd) { char **args; int i, type; - args = try_split_argv(arg, filename, lineno); + args = try_split_argv(arg, rd); if (args == NULL) return -1; @@ -155,7 +171,7 @@ static int svc_type(service_t *svc, char *arg, if (type == -1) { fprintf(stderr, "%s: %zu: unknown service type '%s'\n", - filename, lineno, args[0]); + rd->filename, rd->lineno, args[0]); free(args); return -1; } @@ -183,48 +199,45 @@ static int svc_type(service_t *svc, char *arg, /* fall-through */ default: fprintf(stderr, "%s: %zu: unexpected extra arguments " - "for type '%s'\n", filename, lineno, arg); + "for type '%s'\n", + rd->filename, rd->lineno, arg); return -1; } } svc->type = type; free(args); - free(arg); return 0; fail_limit: fprintf(stderr, "%s: %zu: expected 'limit ' after 'respawn'\n", - filename, lineno); + rd->filename, rd->lineno); free(args); return -1; } -static int svc_target(service_t *svc, char *arg, - const char *filename, size_t lineno) +static int svc_target(service_t *svc, char *arg, rdline_t *rd) { int target; - if (try_unescape(arg, filename, lineno)) + if (try_unescape(arg, rd)) return -1; target = svc_target_from_string(arg); if (target == -1) { fprintf(stderr, "%s: %zu: unknown service target '%s'\n", - filename, lineno, arg); + rd->filename, rd->lineno, arg); return -1; } svc->target = target; - free(arg); return 0; } static const struct svc_param { const char *key; - int (*handle)(service_t *svc, char *arg, - const char *filename, size_t lineno); + int (*handle)(service_t *svc, char *arg, rdline_t *rd); } svc_params[] = { { "description", svc_desc }, { "exec", svc_exec }, @@ -235,49 +248,15 @@ static const struct svc_param { { "after", svc_after }, }; - -static char *get_line(int fd, const char *filename, size_t *lineno, - int argc, const char *const *args) +static int splitkv(rdline_t *rd, char **k, char **v) { - const char *error; - char *line; - int ret; -retry: - errno = 0; - line = rdline(fd, argc, args); - ret = errno; - - if (line == NULL && errno != 0) { - switch (errno) { - case EINVAL: error = "error in argument expansion"; break; - case ELOOP: error = "recursive argument expansion"; break; - case EILSEQ: error = "missing \""; break; - default: error = strerror(errno); break; - } - - fprintf(stderr, "%s: %zu: %s\n", filename, *lineno, error); - } - - if (line != NULL && strlen(line) == 0) { - free(line); - (*lineno) += 1; - goto retry; - } - - errno = ret; - return line; -} - -static int splitkv(const char *filename, size_t lineno, - char *line, char **k, char **v) -{ - char *key = line, *value = line; + char *key = rd->buffer, *value = rd->buffer; while (*value != ' ' && *value != '\0') { if (!isalpha(*value)) { fprintf(stderr, "%s: %zu: unexpected '%c' in keyword\n", - filename, lineno, *value); + rd->filename, rd->lineno, *value); return -1; } ++value; @@ -285,7 +264,7 @@ static int splitkv(const char *filename, size_t lineno, if (*value != ' ') { fprintf(stderr, "%s: %zu: expected argument after '%s'\n", - filename, lineno, key); + rd->filename, rd->lineno, key); return -1; } @@ -296,8 +275,7 @@ static int splitkv(const char *filename, size_t lineno, return 0; } -static const struct svc_param *find_param(const char *filename, size_t lineno, - const char *name) +static const struct svc_param *find_param(rdline_t *rd, const char *name) { size_t i; @@ -307,19 +285,20 @@ static const struct svc_param *find_param(const char *filename, size_t lineno, } fprintf(stderr, "%s: %zu: unknown keyword '%s'\n", - filename, lineno, name); + rd->filename, rd->lineno, name); return NULL; } service_t *rdsvc(int dirfd, const char *filename) { - char *line = NULL, *key, *value; const struct svc_param *p; const char *arg, *args[1]; service_t *svc = NULL; - size_t argc, lineno; - int fd; + char *key, *value; + size_t argc; + rdline_t rd; + int fd, ret; fd = openat(dirfd, filename, O_RDONLY); if (fd < 0) { @@ -335,6 +314,8 @@ service_t *rdsvc(int dirfd, const char *filename) argc = 0; } + rdline_init(&rd, fd, filename, argc, args); + svc = calloc(1, sizeof(*svc)); if (svc == NULL) goto fail_oom; @@ -348,32 +329,26 @@ service_t *rdsvc(int dirfd, const char *filename) if (svc->name == NULL) goto fail_oom; - for (lineno = 1; ; ++lineno) { - line = get_line(fd, filename, &lineno, argc, args); - if (line == NULL) { - if (errno == 0) - break; - goto fail; - } - - if (splitkv(filename, lineno, line, &key, &value)) + while ((ret = rdline(&rd)) == 0) { + if (splitkv(&rd, &key, &value)) goto fail; - p = find_param(filename, lineno, key); + p = find_param(&rd, key); if (p == NULL) goto fail; - memmove(line, value, strlen(value) + 1); - if (p->handle(svc, line, filename, lineno)) + if (p->handle(svc, value, &rd)) goto fail; } + if (ret < 0) + goto fail; + close(fd); return svc; fail_oom: fputs("out of memory\n", stderr); fail: - free(line); delsvc(svc); close(fd); return NULL; -- cgit v1.2.3