2 * Copyright (c) 2012 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
28 __FBSDID("$FreeBSD$");
38 #include "archive_cmdline_private.h"
39 #include "archive_string.h"
41 static int cmdline_set_path(struct archive_cmdline *, const char *);
42 static int cmdline_add_arg(struct archive_cmdline *, const char *);
45 extract_quotation(struct archive_string *as, const char *p)
49 for (s = p + 1; *s;) {
52 archive_strappend_char(as, s[1]);
59 archive_strappend_char(as, s[0]);
64 return (ARCHIVE_FAILED);/* Invalid sequence. */
65 return ((ssize_t)(s + 1 - p));
69 get_argument(struct archive_string *as, const char *p)
73 archive_string_empty(as);
75 /* Skip beginning space characters. */
76 while (*s != '\0' && *s == ' ')
78 /* Copy non-space characters. */
79 while (*s != '\0' && *s != ' ') {
82 archive_strappend_char(as, s[1]);
85 s++;/* Ignore this character.*/
88 } else if (*s == '"') {
89 ssize_t q = extract_quotation(as, s);
91 return (ARCHIVE_FAILED);/* Invalid sequence. */
94 archive_strappend_char(as, s[0]);
98 return ((ssize_t)(s - p));
102 * Set up command line arguments.
103 * Returns ARCHIVE_OK if everything okey.
104 * Returns ARCHIVE_FAILED if there is a lack of the `"' terminator or an
105 * empty command line.
106 * Returns ARCHIVE_FATAL if no memory.
109 __archive_cmdline_parse(struct archive_cmdline *data, const char *cmd)
111 struct archive_string as;
116 archive_string_init(&as);
118 /* Get first argument as a command path. */
119 al = get_argument(&as, cmd);
121 r = ARCHIVE_FAILED;/* Invalid sequence. */
124 if (archive_strlen(&as) == 0) {
125 r = ARCHIVE_FAILED;/* An empty command path. */
128 r = cmdline_set_path(data, as.s);
131 p = strrchr(as.s, '/');
136 r = cmdline_add_arg(data, p);
142 al = get_argument(&as, cmd);
144 r = ARCHIVE_FAILED;/* Invalid sequence. */
150 if (archive_strlen(&as) == 0 && *cmd == '\0')
152 r = cmdline_add_arg(data, as.s);
158 archive_string_free(&as);
163 * Set the program path.
166 cmdline_set_path(struct archive_cmdline *data, const char *path)
170 newptr = realloc(data->path, strlen(path) + 1);
172 return (ARCHIVE_FATAL);
174 strcpy(data->path, path);
179 * Add a argument for the program.
182 cmdline_add_arg(struct archive_cmdline *data, const char *arg)
186 if (data->path == NULL)
187 return (ARCHIVE_FAILED);
189 newargv = realloc(data->argv, (data->argc + 2) * sizeof(char *));
191 return (ARCHIVE_FATAL);
192 data->argv = newargv;
193 data->argv[data->argc] = strdup(arg);
194 if (data->argv[data->argc] == NULL)
195 return (ARCHIVE_FATAL);
196 /* Set the terminator of argv. */
197 data->argv[++data->argc] = NULL;
201 struct archive_cmdline *
202 __archive_cmdline_allocate(void)
204 return (struct archive_cmdline *)
205 calloc(1, sizeof(struct archive_cmdline));
209 * Release the resources.
212 __archive_cmdline_free(struct archive_cmdline *data)
217 if (data->argv != NULL) {
219 for (i = 0; data->argv[i] != NULL; i++)