8typedef const char **(*cmdline_insert_fn_t)(
const char **result,
size_t result_capacity,
char *cmdline,
size_t *result_count);
12 if (*result_count == result_capacity)
15 result[*result_count] = cmdline;
23 argv = krealloc(
argv,
sizeof(
char *) * (*result_count + 1));
31 char *buf_start = start;
43 } quote_type = Q_NONE;
45 for (
char *c = start; c != &buf_start[length + 1]; c++)
59 if (*c ==
'\'' && quote_type != Q_DOUBLE)
61 quote_type = quote_type == Q_SINGLE ? Q_NONE : Q_SINGLE;
65 if (*c ==
'"' && quote_type != Q_SINGLE)
67 quote_type = quote_type == Q_DOUBLE ? Q_NONE : Q_DOUBLE;
71 if (*c ==
' ' && quote_type == Q_NONE)
74 if (*c ==
'\0' && quote_type != Q_NONE)
81 *argv_ptr = insert(*argv_ptr, cmdline_max, start, out_count);
93bool cmdline_parse_inplace(
char *inbuf,
size_t length,
size_t cmdline_max,
size_t *out_count,
const char **out_cmdlines)
98const char **
cmdline_parse(
const char **inargv,
char *inbuf,
size_t length,
size_t *out_count)
114 if (quote !=
'\'' && quote !=
'"')
117 if (str[len - 1] != quote)
120 for (
size_t i = 1; i < len - 1; i++)
124 if (str[i + 1] == quote)
126 memmove(&str[i], &str[i + 1], len - i);
129 else if (str[i + 1] ==
'\\')
131 memmove(&str[i], &str[i + 1], len - i);
138 memmove(str, &str[1], len - 1);
MOSAPI char * strdup(const char *src)
MOSAPI void * memmove(void *dest, const void *src, size_t n)
const char **(* cmdline_insert_fn_t)(const char **result, size_t result_capacity, char *cmdline, size_t *result_count)
static const char ** cmdline_static_array_insert(const char **result, size_t result_capacity, char *cmdline, size_t *result_count)
bool cmdline_parse_inplace(char *inbuf, size_t length, size_t cmdline_max, size_t *out_count, const char **out_cmdlines)
void string_unquote(char *str)
static bool cmdline_parse_generic(char *start, size_t length, size_t cmdline_max, size_t *out_count, const char ***argv_ptr, cmdline_insert_fn_t insert)
static const char ** cmdline_dynamic_array_insert(const char **argv, size_t result_capacity, char *cmdline, size_t *result_count)
const char ** cmdline_parse(const char **inargv, char *inbuf, size_t length, size_t *out_count)
static size_t strlen(const char *s)