Merge commit '315633ecce31cd5af92112ab9903f6e7e8ae8df7'
[free-mc.git] / src / poptparse.c
blobc06a8d4a3a6f6f34b781628a66e9a0566af590f7
1 /* (C) 1998 Red Hat Software, Inc. -- Licensing details are in the COPYING
2 file accompanying popt source distributions, available from
3 ftp://ftp.redhat.com/pub/code/popt */
5 #ifdef HAVE_CONFIG_H
6 #include "config.h"
7 #endif
9 #include "poptalloca.h"
10 #include <ctype.h>
11 #include <stdlib.h>
12 #include <string.h>
14 #include "popt.h"
16 #define POPT_ARGV_ARRAY_GROW_DELTA 5
18 int poptParseArgvString(const char * s, int * argcPtr, char *** argvPtr) {
19 char * buf, * bufStart, * dst;
20 const char * src;
21 char quote = '\0';
22 int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
23 char ** argv = malloc(sizeof(*argv) * argvAlloced);
24 char ** argv2;
25 int argc = 0;
26 int i, buflen;
28 buflen = strlen(s) + 1;
29 bufStart = buf = alloca(buflen);
30 memset(buf, '\0', buflen);
32 src = s;
33 argv[argc] = buf;
35 while (*src) {
36 if (quote == *src) {
37 quote = '\0';
38 } else if (quote) {
39 if (*src == '\\') {
40 src++;
41 if (!*src) {
42 free(argv);
43 return POPT_ERROR_BADQUOTE;
45 if (*src != quote) *buf++ = '\\';
47 *buf++ = *src;
48 } else if (isspace((unsigned char) *src)) {
49 if (*argv[argc]) {
50 buf++, argc++;
51 if (argc == argvAlloced) {
52 argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
53 argv = realloc(argv, sizeof(*argv) * argvAlloced);
55 argv[argc] = buf;
57 } else switch (*src) {
58 case '"':
59 case '\'':
60 quote = *src;
61 break;
62 case '\\':
63 src++;
64 if (!*src) {
65 free(argv);
66 return POPT_ERROR_BADQUOTE;
68 /* fallthrough */
69 default:
70 *buf++ = *src;
73 src++;
76 if (strlen(argv[argc])) {
77 argc++, buf++;
80 dst = malloc(argc * sizeof(*argv) + (buf - bufStart));
81 argv2 = (void *) dst;
82 dst += argc * sizeof(*argv);
83 memcpy(argv2, argv, argc * sizeof(*argv));
84 memcpy(dst, bufStart, buf - bufStart);
86 for (i = 0; i < argc; i++) {
87 argv2[i] = dst + (argv[i] - bufStart);
90 free(argv);
92 *argvPtr = argv2;
93 *argcPtr = argc;
95 return 0;