2000-08-28 03:40:35 +08:00
|
|
|
/** \ingroup popt
|
|
|
|
* \file popt/poptparse.c
|
|
|
|
*/
|
|
|
|
|
2000-12-20 07:22:00 +08:00
|
|
|
/* (C) 1998-2000 Red Hat, Inc. -- Licensing details are in the COPYING
|
1998-10-17 03:05:12 +08:00
|
|
|
file accompanying popt source distributions, available from
|
2000-12-20 07:22:00 +08:00
|
|
|
ftp://ftp.rpm.org/pub/rpm/dist. */
|
1998-10-17 03:05:12 +08:00
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
#include "system.h"
|
1998-10-08 19:19:57 +08:00
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
#define POPT_ARGV_ARRAY_GROW_DELTA 5
|
1999-03-31 22:58:43 +08:00
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
int poptDupArgv(int argc, const char **argv,
|
|
|
|
int * argcPtr, const char *** argvPtr)
|
|
|
|
{
|
|
|
|
size_t nb = (argc + 1) * sizeof(*argv);
|
|
|
|
const char ** argv2;
|
|
|
|
char * dst;
|
|
|
|
int i;
|
1998-09-27 23:56:18 +08:00
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
if (argv[i] == NULL)
|
|
|
|
return POPT_ERROR_NOARG;
|
|
|
|
nb += strlen(argv[i]) + 1;
|
|
|
|
}
|
|
|
|
|
1999-10-26 03:07:14 +08:00
|
|
|
dst = malloc(nb);
|
|
|
|
argv2 = (void *) dst;
|
1999-10-26 02:22:19 +08:00
|
|
|
dst += (argc + 1) * sizeof(*argv);
|
1998-12-29 10:52:47 +08:00
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
for (i = 0; i < argc; i++) {
|
|
|
|
argv2[i] = dst;
|
|
|
|
dst += strlen(strcpy(dst, argv[i])) + 1;
|
|
|
|
}
|
|
|
|
argv2[argc] = NULL;
|
|
|
|
|
|
|
|
*argvPtr = argv2;
|
|
|
|
*argcPtr = argc;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int poptParseArgvString(const char * s, int * argcPtr, const char *** argvPtr)
|
|
|
|
{
|
1998-12-29 10:52:47 +08:00
|
|
|
const char * src;
|
1998-09-27 23:56:18 +08:00
|
|
|
char quote = '\0';
|
1999-04-08 02:57:23 +08:00
|
|
|
int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
|
1999-10-26 02:22:19 +08:00
|
|
|
const char ** argv = malloc(sizeof(*argv) * argvAlloced);
|
1998-09-27 23:56:18 +08:00
|
|
|
int argc = 0;
|
1999-10-26 02:22:19 +08:00
|
|
|
int buflen = strlen(s) + 1;
|
|
|
|
char * buf = memset(alloca(buflen), 0, buflen);
|
1998-09-27 23:56:18 +08:00
|
|
|
|
|
|
|
argv[argc] = buf;
|
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
for (src = s; *src; src++) {
|
1998-09-27 23:56:18 +08:00
|
|
|
if (quote == *src) {
|
|
|
|
quote = '\0';
|
|
|
|
} else if (quote) {
|
|
|
|
if (*src == '\\') {
|
|
|
|
src++;
|
|
|
|
if (!*src) {
|
|
|
|
free(argv);
|
|
|
|
return POPT_ERROR_BADQUOTE;
|
|
|
|
}
|
|
|
|
if (*src != quote) *buf++ = '\\';
|
|
|
|
}
|
|
|
|
*buf++ = *src;
|
|
|
|
} else if (isspace(*src)) {
|
|
|
|
if (*argv[argc]) {
|
|
|
|
buf++, argc++;
|
|
|
|
if (argc == argvAlloced) {
|
1999-04-08 02:57:23 +08:00
|
|
|
argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
|
1998-09-27 23:56:18 +08:00
|
|
|
argv = realloc(argv, sizeof(*argv) * argvAlloced);
|
|
|
|
}
|
|
|
|
argv[argc] = buf;
|
|
|
|
}
|
|
|
|
} else switch (*src) {
|
|
|
|
case '"':
|
|
|
|
case '\'':
|
|
|
|
quote = *src;
|
|
|
|
break;
|
|
|
|
case '\\':
|
|
|
|
src++;
|
|
|
|
if (!*src) {
|
|
|
|
free(argv);
|
|
|
|
return POPT_ERROR_BADQUOTE;
|
|
|
|
}
|
1999-10-22 02:20:12 +08:00
|
|
|
/*@fallthrough@*/
|
1998-09-27 23:56:18 +08:00
|
|
|
default:
|
|
|
|
*buf++ = *src;
|
1999-10-22 02:20:12 +08:00
|
|
|
break;
|
1998-09-27 23:56:18 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (strlen(argv[argc])) {
|
|
|
|
argc++, buf++;
|
|
|
|
}
|
|
|
|
|
1999-10-26 02:22:19 +08:00
|
|
|
(void) poptDupArgv(argc, argv, argcPtr, argvPtr);
|
1998-09-27 23:56:18 +08:00
|
|
|
|
|
|
|
free(argv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|