2 * \file popt/poptparse.c
5 /* (C) 1998-2002 Red Hat, Inc. -- Licensing details are in the COPYING
6 file accompanying popt source distributions, available from
7 ftp://ftp.rpm.org/pub/rpm/dist. */
11 #define POPT_ARGV_ARRAY_GROW_DELTA 5
13 int poptDupArgv(int argc
, const char **argv
,
14 int * argcPtr
, const char *** argvPtr
)
16 size_t nb
= (argc
+ 1) * sizeof(*argv
);
21 if (argc
<= 0 || argv
== NULL
) /* XXX can't happen */
22 return POPT_ERROR_NOARG
;
23 for (i
= 0; i
< argc
; i
++) {
25 return POPT_ERROR_NOARG
;
26 nb
+= strlen(argv
[i
]) + 1;
30 if (dst
== NULL
) /* XXX can't happen */
31 return POPT_ERROR_MALLOC
;
33 dst
+= (argc
+ 1) * sizeof(*argv
);
36 for (i
= 0; i
< argc
; i
++) {
38 dst
= stpcpy(dst
, argv
[i
]);
39 dst
++; /* trailing NUL */
54 int poptParseArgvString(const char * s
, int * argcPtr
, const char *** argvPtr
)
58 int argvAlloced
= POPT_ARGV_ARRAY_GROW_DELTA
;
59 const char ** argv
= malloc(sizeof(*argv
) * argvAlloced
);
61 size_t buflen
= strlen(s
) + 1;
62 char * buf
, * bufOrig
= NULL
;
63 int rc
= POPT_ERROR_MALLOC
;
65 if (argv
== NULL
) return rc
;
66 buf
= bufOrig
= calloc((size_t)1, buflen
);
73 for (src
= s
; *src
!= '\0'; src
++) {
76 } else if (quote
!= '\0') {
80 rc
= POPT_ERROR_BADQUOTE
;
83 if (*src
!= quote
) *buf
++ = '\\';
86 } else if (_isspaceptr(src
)) {
87 if (*argv
[argc
] != '\0') {
89 if (argc
== argvAlloced
) {
90 argvAlloced
+= POPT_ARGV_ARRAY_GROW_DELTA
;
91 argv
= realloc(argv
, sizeof(*argv
) * argvAlloced
);
92 if (argv
== NULL
) goto exit
;
96 } else switch (*src
) {
100 /*@switchbreak@*/ break;
104 rc
= POPT_ERROR_BADQUOTE
;
110 /*@switchbreak@*/ break;
114 if (strlen(argv
[argc
])) {
118 rc
= poptDupArgv(argc
, argv
, argcPtr
, argvPtr
);
121 if (bufOrig
) free(bufOrig
);
122 if (argv
) free(argv
);
126 /* still in the dev stage.
127 * return values, perhaps 1== file erro
131 int poptConfigFileToString(FILE *fp
, char ** argstrp
,
132 /*@unused@*/ UNUSED(int flags
))
141 size_t maxlinelen
= sizeof(line
);
143 size_t maxargvlen
= (size_t)480;
147 /* | this_is = our_line
152 return POPT_ERROR_NULLARG
;
154 argstr
= calloc(maxargvlen
, sizeof(*argstr
));
155 if (argstr
== NULL
) return POPT_ERROR_MALLOC
;
157 while (fgets(line
, (int)maxlinelen
, fp
) != NULL
) {
160 /* loop until first non-space char or EOL */
161 while( *p
!= '\0' && _isspaceptr(p
) )
165 if (linelen
>= maxlinelen
-1) {
167 return POPT_ERROR_OVERFLOW
; /* XXX line too long */
170 if (*p
== '\0' || *p
== '\n') continue; /* line is empty */
171 if (*p
== '#') continue; /* comment line */
175 while (*q
!= '\0' && (!_isspaceptr(q
)) && *q
!= '=')
178 if (_isspaceptr(q
)) {
179 /* a space after the name, find next non space */
181 while( *q
!= '\0' && _isspaceptr(q
) ) q
++;
184 /* single command line option (ie, no name=val, just name) */
185 q
[-1] = '\0'; /* kill off newline from fgets() call */
186 argvlen
+= (t
= (size_t)(q
- p
)) + (sizeof(" --")-1);
187 if (argvlen
>= maxargvlen
) {
188 maxargvlen
= (t
> maxargvlen
) ? t
*2 : maxargvlen
*2;
189 argstr
= realloc(argstr
, maxargvlen
);
190 if (argstr
== NULL
) return POPT_ERROR_MALLOC
;
192 strcat(argstr
, " --");
197 continue; /* XXX for now, silently ignore bogus line */
199 /* *q is an equal sign. */
202 /* find next non-space letter of value */
203 while (*q
!= '\0' && _isspaceptr(q
))
206 continue; /* XXX silently ignore missing value */
208 /* now, loop and strip all ending whitespace */
210 while (_isspaceptr(--x
))
211 *x
= '\0'; /* null out last char if space (including fgets() NL) */
213 /* rest of line accept */
215 argvlen
+= t
+ (sizeof("' --='")-1);
216 if (argvlen
>= maxargvlen
) {
217 maxargvlen
= (t
> maxargvlen
) ? t
*2 : maxargvlen
*2;
218 argstr
= realloc(argstr
, maxargvlen
);
219 if (argstr
== NULL
) return POPT_ERROR_MALLOC
;
221 strcat(argstr
, " --");
223 strcat(argstr
, "=\"");
225 strcat(argstr
, "\"");