1
0
Fork 0
mirror of https://github.com/pygos/init.git synced 2024-11-22 11:19:45 +01:00

Cleanup: split rdsvc into multiple functions

Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
This commit is contained in:
David Oberhollenzer 2018-04-08 13:30:20 +02:00
parent b81668e045
commit 7d8ceb810f
2 changed files with 108 additions and 89 deletions

View file

@ -23,6 +23,9 @@ void delsvc(service_t *svc)
{ {
exec_t *e; exec_t *e;
if (svc == NULL)
return;
while (svc->exec != NULL) { while (svc->exec != NULL) {
e = svc->exec; e = svc->exec;
svc->exec = e->next; svc->exec = e->next;

View file

@ -220,7 +220,7 @@ static int svc_target(service_t *svc, char *arg,
return 0; return 0;
} }
static const struct { static const struct svc_param {
const char *key; const char *key;
int (*handle)(service_t *svc, char *arg, int (*handle)(service_t *svc, char *arg,
@ -236,12 +236,97 @@ static const struct {
}; };
static char *get_line(int fd, const char *filename, size_t *lineno,
int argc, const char *const *args)
{
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;
*k = *v = NULL;
while (*value != ' ' && *value != '\0') {
if (!isalpha(*value)) {
fprintf(stderr,
"%s: %zu: unexpected '%c' in keyword\n",
filename, lineno, *value);
return -1;
}
++value;
}
if (*value != ' ') {
fprintf(stderr, "%s: %zu: expected argument after '%s'\n",
filename, lineno, key);
return -1;
}
*(value++) = '\0';
value = strdup(value);
if (value == NULL) {
fprintf(stderr, "%s: %zu: out of memory\n", filename, lineno);
return -1;
}
*k = key;
*v = value;
return 0;
}
static const struct svc_param *find_param(const char *filename, size_t lineno,
const char *name)
{
size_t i;
for (i = 0; i < ARRAY_SIZE(svc_params); ++i) {
if (!strcmp(svc_params[i].key, name))
return svc_params + i;
}
fprintf(stderr, "%s: %zu: unknown keyword '%s'\n",
filename, lineno, name);
return NULL;
}
service_t *rdsvc(int dirfd, const char *filename) service_t *rdsvc(int dirfd, const char *filename)
{ {
const char *arg, *args[1], *error; char *line = NULL, *key, *value = NULL;
char *line, *key, *value; const struct svc_param *p;
size_t i, argc, lineno; const char *arg, *args[1];
service_t *svc; service_t *svc = NULL;
size_t argc, lineno;
int fd; int fd;
fd = openat(dirfd, filename, O_RDONLY); fd = openat(dirfd, filename, O_RDONLY);
@ -259,11 +344,8 @@ service_t *rdsvc(int dirfd, const char *filename)
} }
svc = calloc(1, sizeof(*svc)); svc = calloc(1, sizeof(*svc));
if (svc == NULL) { if (svc == NULL)
fputs("out of memory\n", stderr); goto fail_oom;
close(fd);
return NULL;
}
if (arg != NULL) { if (arg != NULL) {
svc->name = strndup(filename, arg - filename); svc->name = strndup(filename, arg - filename);
@ -271,101 +353,35 @@ service_t *rdsvc(int dirfd, const char *filename)
svc->name = strdup(filename); svc->name = strdup(filename);
} }
if (svc->name == NULL) { if (svc->name == NULL)
free(svc); goto fail_oom;
fputs("out of memory\n", stderr);
close(fd);
return NULL;
}
for (lineno = 1; ; ++lineno) { for (lineno = 1; ; ++lineno) {
errno = 0; line = get_line(fd, filename, &lineno, argc, args);
line = rdline(fd, argc, args);
if (line == NULL) { if (line == NULL) {
if (errno == 0) if (errno == 0)
break; break;
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);
goto fail; goto fail;
} }
if (!strlen(line)) { if (splitkv(filename, lineno, line, &key, &value))
free(line); goto fail;
continue;
}
key = value = line; p = find_param(filename, lineno, key);
if (p == NULL || p->handle(svc, value, filename, lineno) != 0)
while (*value != ' ' && *value != '\0') { goto fail;
if (!isalpha(*value)) {
fprintf(stderr, "%s: %zu: unexpected '%c' in "
"keyword\n", filename, lineno, *value);
goto fail_line;
}
++value;
}
if (*value == ' ') {
*(value++) = '\0';
} else {
value = NULL;
}
for (i = 0; i < ARRAY_SIZE(svc_params); ++i) {
if (!strcmp(svc_params[i].key, key))
break;
}
if (i >= ARRAY_SIZE(svc_params)) {
fprintf(stderr, "%s: %zu: unknown keyword '%s'\n",
filename, lineno, key);
goto fail_line;
}
if (value == NULL) {
fprintf(stderr,
"%s: %zu: expected argument after '%s'\n",
filename, lineno, key);
goto fail_line;
}
value = strdup(value);
if (value == NULL) {
fputs("out of memory", stderr);
goto fail_line;
}
if (svc_params[i].handle(svc, value, filename, lineno)) {
free(value);
goto fail_line;
}
free(line); free(line);
} }
close(fd); close(fd);
return svc; return svc;
fail_line: fail_oom:
free(line); fputs("out of memory\n", stderr);
fail: fail:
close(fd); free(value);
free(line);
delsvc(svc); delsvc(svc);
close(fd);
return NULL; return NULL;
} }