2018-11-23 13:13:23 +01:00
|
|
|
/* SPDX-License-Identifier: ISC */
|
2018-04-04 14:58:01 +02:00
|
|
|
#include <stdlib.h>
|
2018-04-11 15:57:01 +02:00
|
|
|
#include <string.h>
|
2018-02-25 14:33:19 +01:00
|
|
|
#include <ctype.h>
|
2018-04-04 14:58:01 +02:00
|
|
|
#include <errno.h>
|
2018-04-11 15:57:01 +02:00
|
|
|
#include <stdio.h>
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-05-23 22:26:17 +02:00
|
|
|
#include "libcfg.h"
|
2018-02-25 14:33:19 +01:00
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
int pack_argv(char *str)
|
2018-04-04 14:58:01 +02:00
|
|
|
{
|
2018-04-11 15:57:01 +02:00
|
|
|
char *dst, *start;
|
|
|
|
int count = 0;
|
2018-04-04 14:58:01 +02:00
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
dst = str;
|
2018-04-04 14:58:01 +02:00
|
|
|
|
|
|
|
for (;;) {
|
2018-04-11 15:57:01 +02:00
|
|
|
while (*str == ' ')
|
|
|
|
++str;
|
2018-04-04 14:58:01 +02:00
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
if (*str == '\0')
|
2018-04-04 14:58:01 +02:00
|
|
|
break;
|
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
if (*str == '"') {
|
|
|
|
start = dst;
|
|
|
|
*(dst++) = *(str++);
|
2018-04-04 14:58:01 +02:00
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
while (*str != '"') {
|
|
|
|
if (*str == '\0')
|
|
|
|
goto fail_str;
|
|
|
|
if (str[0] == '\\' && str[1] != '\0')
|
|
|
|
*(dst++) = *(str++);
|
|
|
|
*(dst++) = *(str++);
|
2018-04-04 14:58:01 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
*(dst++) = *(str++);
|
2018-04-04 14:58:01 +02:00
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
if (*str != ' ' && *str != '\0')
|
2018-04-04 14:58:01 +02:00
|
|
|
goto fail_str;
|
2018-04-11 15:57:01 +02:00
|
|
|
if (*str == ' ')
|
|
|
|
++str;
|
2018-04-04 14:58:01 +02:00
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
*(dst++) = '\0';
|
|
|
|
|
|
|
|
if (unescape(start))
|
2018-04-04 14:58:01 +02:00
|
|
|
goto fail_str;
|
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
dst = start + strlen(start) + 1;
|
|
|
|
} else {
|
|
|
|
while (*str != '\0' && *str != ' ')
|
|
|
|
*(dst++) = *(str++);
|
|
|
|
if (*str == ' ') {
|
|
|
|
++str;
|
|
|
|
*(dst++) = '\0';
|
|
|
|
}
|
2018-04-04 14:58:01 +02:00
|
|
|
}
|
2018-04-11 15:57:01 +02:00
|
|
|
|
|
|
|
++count;
|
2018-04-04 14:58:01 +02:00
|
|
|
}
|
|
|
|
|
2018-04-11 15:57:01 +02:00
|
|
|
*dst = '\0';
|
|
|
|
return count;
|
2018-04-04 14:58:01 +02:00
|
|
|
fail_str:
|
|
|
|
errno = EINVAL;
|
2018-04-11 15:57:01 +02:00
|
|
|
return -1;
|
|
|
|
}
|