2 * Copyright (c) 2011 Tim Kientzle
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"
27 __FBSDID("$FreeBSD$");
29 #include "archive_options_private.h"
32 parse_option(const char **str,
33 const char **mod, const char **opt, const char **val);
36 _archive_set_option(struct archive *a,
37 const char *m, const char *o, const char *v,
38 int magic, const char *fn, option_handler use_option)
40 const char *mp, *op, *vp;
42 archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
44 mp = m != NULL && m[0] == '\0' ? NULL : m;
45 op = o != NULL && o[0] == '\0' ? NULL : o;
46 vp = v != NULL && v[0] == '\0' ? NULL : v;
48 if (op == NULL && vp == NULL)
51 return (ARCHIVE_FAILED);
53 return use_option(a, mp, op, vp);
57 _archive_set_either_option(struct archive *a, const char *m, const char *o, const char *v,
58 option_handler use_format_option, option_handler use_filter_option)
62 if (o == NULL && v == NULL)
65 return (ARCHIVE_FAILED);
67 r1 = use_format_option(a, m, o, v);
68 if (r1 == ARCHIVE_FATAL)
69 return (ARCHIVE_FATAL);
71 r2 = use_filter_option(a, m, o, v);
72 if (r2 == ARCHIVE_FATAL)
73 return (ARCHIVE_FATAL);
75 return r1 > r2 ? r1 : r2;
79 _archive_set_options(struct archive *a, const char *options,
80 int magic, const char *fn, option_handler use_option)
82 int allok = 1, anyok = 0, r;
84 const char *s, *mod, *opt, *val;
86 archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
88 if (options == NULL || options[0] == '\0')
91 data = (char *)malloc(strlen(options) + 1);
92 strcpy(data, options);
93 s = (const char *)data;
96 mod = opt = val = NULL;
98 parse_option(&s, &mod, &opt, &val);
100 r = use_option(a, mod, opt, val);
101 if (r == ARCHIVE_FATAL) {
103 return (ARCHIVE_FATAL);
112 return allok ? ARCHIVE_OK : anyok ? ARCHIVE_WARN : ARCHIVE_FAILED;
116 parse_option(const char **s, const char **m, const char **o, const char **v)
118 const char *end, *mod, *opt, *val;
126 p = strchr(opt, ',');
130 end = ((const char *)p) + 1;
133 if (0 == strlen(opt)) {
141 p = strchr(opt, ':');
148 p = strchr(opt, '=');
152 } else if (opt[0] == '!') {