More translates of doc/man/es/mc.1.in
[midnight-commander.git] / src / poptparse.c
blob54cadf34a7ca90851c9ea3d216d4185942faeb3b
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 */
6 /** \file poptparse.c
7 * \brief Source: popt parser
8 */
10 #ifdef HAVE_CONFIG_H
11 #include "config.h"
12 #endif
14 #include "poptalloca.h"
15 #include <ctype.h>
16 #include <stdlib.h>
17 #include <string.h>
19 #include "popt.h"
21 #define POPT_ARGV_ARRAY_GROW_DELTA 5
23 int poptParseArgvString(const char * s, int * argcPtr, char *** argvPtr) {
24 char * buf, * bufStart, * dst;
25 const char * src;
26 char quote = '\0';
27 int argvAlloced = POPT_ARGV_ARRAY_GROW_DELTA;
28 char ** argv = malloc(sizeof(*argv) * argvAlloced);
29 char ** argv2;
30 int argc = 0;
31 int i, buflen;
33 buflen = strlen(s) + 1;
34 bufStart = buf = alloca(buflen);
35 memset(buf, '\0', buflen);
37 src = s;
38 argv[argc] = buf;
40 while (*src) {
41 if (quote == *src) {
42 quote = '\0';
43 } else if (quote) {
44 if (*src == '\\') {
45 src++;
46 if (!*src) {
47 free(argv);
48 return POPT_ERROR_BADQUOTE;
50 if (*src != quote) *buf++ = '\\';
52 *buf++ = *src;
53 } else if (isspace((unsigned char) *src)) {
54 if (*argv[argc]) {
55 buf++, argc++;
56 if (argc == argvAlloced) {
57 argvAlloced += POPT_ARGV_ARRAY_GROW_DELTA;
58 argv = realloc(argv, sizeof(*argv) * argvAlloced);
60 argv[argc] = buf;
62 } else switch (*src) {
63 case '"':
64 case '\'':
65 quote = *src;
66 break;
67 case '\\':
68 src++;
69 if (!*src) {
70 free(argv);
71 return POPT_ERROR_BADQUOTE;
73 /* fallthrough */
74 default:
75 *buf++ = *src;
78 src++;
81 if (strlen(argv[argc])) {
82 argc++, buf++;
85 dst = malloc(argc * sizeof(*argv) + (buf - bufStart));
86 argv2 = (void *) dst;
87 dst += argc * sizeof(*argv);
88 memcpy(argv2, argv, argc * sizeof(*argv));
89 memcpy(dst, bufStart, buf - bufStart);
91 for (i = 0; i < argc; i++) {
92 argv2[i] = dst + (argv[i] - bufStart);
95 free(argv);
97 *argvPtr = argv2;
98 *argcPtr = argc;
100 return 0;