2018-03-24 22:31:05 +01:00
|
|
|
/* 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/>.
|
|
|
|
*/
|
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;
|
|
|
|
}
|