summaryrefslogtreecommitdiff
path: root/lib/src
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-04 11:55:59 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-04 11:55:59 +0200
commitca7b7c15c5ecd344e844411e4b8409e552a3b06d (patch)
tree7bebbde8559d67e1f5450657f8a44ab05e48feac /lib/src
parent352a9060b6813c41527f0c5da43f0c86aecfde2a (diff)
Merge argument substitution into rdline
Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'lib/src')
-rw-r--r--lib/src/rdline.c60
-rw-r--r--lib/src/rdsvc.c31
-rw-r--r--lib/src/strexpand.c72
3 files changed, 68 insertions, 95 deletions
diff --git a/lib/src/rdline.c b/lib/src/rdline.c
index 4e72a25..d6c4078 100644
--- a/lib/src/rdline.c
+++ b/lib/src/rdline.c
@@ -27,33 +27,45 @@ enum {
STATE_STRING = 1,
STATE_STRING_ESC = 2,
STATE_COMMENT = 3,
+ STATE_ARG = 4,
};
-char *rdline(int fd)
+char *rdline(int fd, int argc, const char *const *argv)
{
size_t i = 0, bufsiz = 0, newsz;
int ret, state = STATE_INITIAL;
char c, *new, *buffer = NULL;
+ const char *argstr = NULL;
for (;;) {
- switch (read(fd, &c, 1)) {
- case 0:
- if (i == 0) {
- errno = 0;
- return NULL;
- }
- c = '\0';
- break;
- case 1:
- if (c == '\n')
+ if (argstr == NULL) {
+ switch (read(fd, &c, 1)) {
+ case 0:
+ if (i == 0) {
+ errno = 0;
+ return NULL;
+ }
c = '\0';
- break;
- default:
- if (errno == EINTR)
+ break;
+ case 1:
+ break;
+ default:
+ if (errno == EINTR)
+ continue;
+ goto fail;
+ }
+ } else {
+ c = *(argstr++);
+
+ if (c == '\0') {
+ argstr = NULL;
continue;
- goto fail;
+ }
}
+ if (c == '\n')
+ c = '\0';
+
switch (state) {
case STATE_STRING:
if (c == '\\')
@@ -68,6 +80,20 @@ char *rdline(int fd)
if (c != '\0')
continue;
break;
+ case STATE_ARG:
+ state = STATE_INITIAL;
+ if (c == '%')
+ break;
+ if (!isdigit(c) || (c - '0') >= argc) {
+ errno = EINVAL;
+ goto fail;
+ }
+ if (argstr != NULL) {
+ errno = ELOOP;
+ goto fail;
+ }
+ argstr = argv[c - '0'];
+ continue;
default:
if (isspace(c))
c = ' ';
@@ -77,6 +103,10 @@ char *rdline(int fd)
state = STATE_COMMENT;
continue;
}
+ if (c == '%') {
+ state = STATE_ARG;
+ continue;
+ }
if (c == '"')
state = STATE_STRING;
break;
diff --git a/lib/src/rdsvc.c b/lib/src/rdsvc.c
index ee898c3..b186cd9 100644
--- a/lib/src/rdsvc.c
+++ b/lib/src/rdsvc.c
@@ -171,7 +171,7 @@ static const struct {
service_t *rdsvc(int dirfd, const char *filename)
{
- const char *arg, *args[1];
+ const char *arg, *args[1], *error;
char *line, *key, *value;
size_t i, argc, lineno;
service_t *svc;
@@ -213,15 +213,30 @@ service_t *rdsvc(int dirfd, const char *filename)
for (lineno = 1; ; ++lineno) {
errno = 0;
- line = rdline(fd);
+ line = rdline(fd, argc, args);
if (line == NULL) {
- if (errno != 0) {
- fprintf(stderr, "read: %s: %zu: %s\n",
- filename, lineno, strerror(errno));
- goto fail;
+ if (errno == 0)
+ 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;
}
- break;
+
+ fprintf(stderr, "%s: %zu: %s\n",
+ filename, lineno, error);
+ goto fail;
}
if (splitkv(line, &key, &value)) {
@@ -258,7 +273,7 @@ service_t *rdsvc(int dirfd, const char *filename)
goto fail_line;
}
- value = strexpand(value, argc, args);
+ value = strdup(value);
if (value == NULL) {
fputs("out of memory", stderr);
goto fail_line;
diff --git a/lib/src/strexpand.c b/lib/src/strexpand.c
deleted file mode 100644
index 58b69a0..0000000
--- a/lib/src/strexpand.c
+++ /dev/null
@@ -1,72 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-or-later */
-/*
- * Copyright (C) 2018 - David Oberhollenzer
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
-#include <string.h>
-#include <stdlib.h>
-#include <ctype.h>
-
-#include "util.h"
-
-char *strexpand(const char *inp, size_t argc, const char *const *argv)
-{
- char *out, *dst;
- const char *ptr;
- size_t i, len;
-
- ptr = inp;
- len = 0;
-
- while (*ptr != '\0') {
- if (ptr[0] == '%' && isdigit(ptr[1])) {
- i = ptr[1] - '0';
- if (i < argc)
- len += strlen(argv[i]);
- ptr += 2;
- } else if (ptr[0] == '%' && ptr[1] == '%') {
- ptr += 2;
- len += 1;
- } else {
- ++ptr;
- ++len;
- }
- }
-
- out = calloc(1, len + 1);
- if (out == NULL)
- return NULL;
-
- dst = out;
-
- while (*inp != '\0') {
- if (inp[0] == '%' && isdigit(inp[1])) {
- i = inp[1] - '0';
- if (i < argc) {
- len = strlen(argv[i]);
- memcpy(dst, argv[i], len);
- dst += len;
- }
- inp += 2;
- } else if (inp[0] == '%' && inp[1] == '%') {
- *(dst++) = '%';
- inp += 2;
- } else {
- *(dst++) = *(inp++);
- }
- }
-
- return out;
-}