2012-05-03 18:59:14 +00:00
|
|
|
/*
|
|
|
|
* argvsplit.c
|
|
|
|
* argv_split() routine
|
|
|
|
*
|
2012-07-26 02:09:31 +00:00
|
|
|
* Copyright (c) 2012 pkgconf authors (see AUTHORS).
|
2012-05-03 18:59:14 +00:00
|
|
|
*
|
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
|
|
|
*
|
2012-07-20 19:29:58 +00:00
|
|
|
* This software is provided 'as is' and without any warranty, express or
|
|
|
|
* implied. In no event shall the authors be liable for any damages arising
|
|
|
|
* from the use of this software.
|
2012-05-03 18:59:14 +00:00
|
|
|
*/
|
|
|
|
|
2015-09-06 14:35:08 +00:00
|
|
|
#include <libpkgconf/libpkgconf.h>
|
2012-05-03 18:59:14 +00:00
|
|
|
|
2016-12-11 00:56:09 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* libpkgconf `argvsplit` module
|
|
|
|
* =============================
|
|
|
|
*
|
|
|
|
* This is a lowlevel module which provides parsing of strings into argument vectors,
|
|
|
|
* similar to what a shell would do.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: void pkgconf_argv_free(char **argv)
|
|
|
|
*
|
|
|
|
* Frees an argument vector.
|
|
|
|
*
|
|
|
|
* :param char** argv: The argument vector to free.
|
|
|
|
* :return: nothing
|
|
|
|
*/
|
2012-05-07 03:21:11 +00:00
|
|
|
void
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_free(char **argv)
|
2012-05-07 02:09:40 +00:00
|
|
|
{
|
|
|
|
free(argv[0]);
|
|
|
|
free(argv);
|
|
|
|
}
|
|
|
|
|
2016-12-11 00:56:09 +00:00
|
|
|
/*
|
|
|
|
* !doc
|
|
|
|
*
|
|
|
|
* .. c:function:: int pkgconf_argv_split(const char *src, int *argc, char ***argv)
|
|
|
|
*
|
|
|
|
* Splits a string into an argument vector.
|
|
|
|
*
|
|
|
|
* :param char* src: The string to split.
|
|
|
|
* :param int* argc: A pointer to an integer to store the argument count.
|
|
|
|
* :param char*** argv: A pointer to a pointer for an argument vector.
|
|
|
|
* :return: 0 on success, -1 on error.
|
|
|
|
* :rtype: int
|
|
|
|
*/
|
2012-05-07 03:21:11 +00:00
|
|
|
int
|
2015-09-06 15:48:24 +00:00
|
|
|
pkgconf_argv_split(const char *src, int *argc, char ***argv)
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
|
|
|
char *buf = malloc(strlen(src) + 1);
|
|
|
|
const char *src_iter;
|
|
|
|
char *dst_iter;
|
|
|
|
int argc_count = 0;
|
|
|
|
int argv_size = 5;
|
|
|
|
char quote = 0;
|
|
|
|
|
|
|
|
src_iter = src;
|
|
|
|
dst_iter = buf;
|
|
|
|
|
2012-05-07 08:26:44 +00:00
|
|
|
memset(buf, 0, strlen(src) + 1);
|
2012-05-03 18:59:14 +00:00
|
|
|
|
|
|
|
*argv = calloc(sizeof (void *), argv_size);
|
|
|
|
(*argv)[argc_count] = dst_iter;
|
|
|
|
|
|
|
|
while (*src_iter)
|
|
|
|
{
|
2015-09-27 21:07:06 +00:00
|
|
|
if (quote == *src_iter)
|
2012-05-03 18:59:14 +00:00
|
|
|
quote = 0;
|
|
|
|
else if (quote)
|
|
|
|
{
|
|
|
|
if (*src_iter == '\\')
|
|
|
|
{
|
|
|
|
src_iter++;
|
|
|
|
if (!*src_iter)
|
|
|
|
{
|
|
|
|
free(*argv);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*src_iter != quote)
|
|
|
|
*dst_iter++ = '\\';
|
|
|
|
}
|
|
|
|
*dst_iter++ = *src_iter;
|
|
|
|
}
|
2015-12-02 11:50:04 +00:00
|
|
|
else if (isspace((unsigned int)*src_iter))
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
2012-07-25 22:49:54 +00:00
|
|
|
if ((*argv)[argc_count] != NULL)
|
2012-05-03 18:59:14 +00:00
|
|
|
{
|
|
|
|
argc_count++, dst_iter++;
|
|
|
|
|
|
|
|
if (argc_count == argv_size)
|
|
|
|
{
|
|
|
|
argv_size += 5;
|
|
|
|
*argv = realloc(*argv, sizeof(void *) * argv_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
(*argv)[argc_count] = dst_iter;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else switch(*src_iter)
|
|
|
|
{
|
|
|
|
case '\\':
|
|
|
|
src_iter++;
|
|
|
|
|
|
|
|
if (!*src_iter)
|
|
|
|
{
|
|
|
|
free(argv);
|
|
|
|
free(buf);
|
|
|
|
return -1;
|
2017-01-23 05:04:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-08-14 09:12:01 +00:00
|
|
|
*dst_iter++ = '\\';
|
2017-01-23 05:04:01 +00:00
|
|
|
*dst_iter++ = *src_iter;
|
2012-05-03 18:59:14 +00:00
|
|
|
}
|
2017-01-23 05:04:01 +00:00
|
|
|
|
|
|
|
break;
|
2012-05-03 18:59:14 +00:00
|
|
|
default:
|
|
|
|
*dst_iter++ = *src_iter;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
src_iter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen((*argv)[argc_count])) {
|
|
|
|
argc_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*argc = argc_count;
|
|
|
|
return 0;
|
|
|
|
}
|