aboutsummaryrefslogtreecommitdiff
path: root/lib/src/splitkv.c
diff options
context:
space:
mode:
authorDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-04 14:58:01 +0200
committerDavid Oberhollenzer <david.oberhollenzer@tele2.at>2018-04-04 14:58:01 +0200
commit04a23330e4a2085ee91980c223c5e4f089ebbe97 (patch)
treeda0b02615fb21849a37cf4632c80df1569788dff /lib/src/splitkv.c
parentaa1356cb190a3416936b25f75ecaaa8684c6fcde (diff)
Merge preprocessing of command lines
- Common function for splitting string into argument vector - Preprocess & split command lines while parsing the service file - Specify "before" and "after" dependencies in a single line Signed-off-by: David Oberhollenzer <david.oberhollenzer@tele2.at>
Diffstat (limited to 'lib/src/splitkv.c')
-rw-r--r--lib/src/splitkv.c73
1 files changed, 73 insertions, 0 deletions
diff --git a/lib/src/splitkv.c b/lib/src/splitkv.c
index 3631ea8..7d652d3 100644
--- a/lib/src/splitkv.c
+++ b/lib/src/splitkv.c
@@ -15,7 +15,9 @@
* 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 <stdlib.h>
#include <ctype.h>
+#include <errno.h>
#include "util.h"
@@ -86,3 +88,74 @@ int unescape(char *src)
*(dst++) = '\0';
return 0;
}
+
+char **split_argv(char *str)
+{
+ size_t i = 0, cap = 0, new_cap;
+ char **argv = NULL, **new;
+ char *ptr;
+
+ ptr = str;
+
+ for (;;) {
+ if (*ptr == ' ') {
+ ++ptr;
+ continue;
+ }
+
+ if (i == cap) {
+ new_cap = cap ? cap * 2 : 16;
+ new = realloc(argv, sizeof(argv[0]) * new_cap);
+
+ if (new == NULL) {
+ free(argv);
+ errno = ENOMEM;
+ return NULL;
+ }
+
+ cap = new_cap;
+ argv = new;
+ }
+
+ if (*ptr == '\0') {
+ argv[i++] = NULL;
+ break;
+ }
+
+ argv[i++] = ptr;
+
+ if (*ptr == '"') {
+ ++ptr;
+ while (*ptr != '\0' && *ptr != '"') {
+ if (ptr[0] == '\\' && ptr[1] != '\0')
+ ++ptr;
+
+ ++ptr;
+ }
+
+ if (*ptr == '"')
+ ++ptr;
+
+ if (*ptr == ' ') {
+ *(ptr++) = '\0';
+ } else if (*ptr != '\0') {
+ goto fail_str;
+ }
+
+ if (unescape(argv[i - 1]))
+ goto fail_str;
+ } else {
+ while (*ptr != '\0' && *ptr != ' ')
+ ++ptr;
+
+ if (*ptr == ' ')
+ *(ptr++) = '\0';
+ }
+ }
+
+ return argv;
+fail_str:
+ free(argv);
+ errno = EINVAL;
+ return NULL;
+}