summaryrefslogtreecommitdiff
path: root/lib/src/splitkv.c
diff options
context:
space:
mode:
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;
+}