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;
43 archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
45 mp = m != NULL && m[0] == '\0' ? NULL : m;
46 op = o != NULL && o[0] == '\0' ? NULL : o;
47 vp = v != NULL && v[0] == '\0' ? NULL : v;
49 if (op == NULL && vp == NULL)
52 archive_set_error(a, ARCHIVE_ERRNO_MISC, "Empty option");
53 return (ARCHIVE_FAILED);
56 r = use_option(a, mp, op, vp);
57 if (r == ARCHIVE_WARN - 1) {
58 archive_set_error(a, ARCHIVE_ERRNO_MISC,
59 "Unknown module name: `%s'", mp);
60 return (ARCHIVE_FAILED);
62 if (r == ARCHIVE_WARN) {
63 archive_set_error(a, ARCHIVE_ERRNO_MISC,
64 "Undefined option: `%s%s%s%s%s%s'",
65 vp?"":"!", mp?mp:"", mp?":":"", op, vp?"=":"", vp?vp:"");
66 return (ARCHIVE_FAILED);
72 _archive_set_either_option(struct archive *a, const char *m, const char *o, const char *v,
73 option_handler use_format_option, option_handler use_filter_option)
77 if (o == NULL && v == NULL)
80 return (ARCHIVE_FAILED);
82 r1 = use_format_option(a, m, o, v);
83 if (r1 == ARCHIVE_FATAL)
84 return (ARCHIVE_FATAL);
86 r2 = use_filter_option(a, m, o, v);
87 if (r2 == ARCHIVE_FATAL)
88 return (ARCHIVE_FATAL);
90 return r1 > r2 ? r1 : r2;
94 _archive_set_options(struct archive *a, const char *options,
95 int magic, const char *fn, option_handler use_option)
97 int allok = 1, anyok = 0, r;
99 const char *s, *mod, *opt, *val;
101 archive_check_magic(a, magic, ARCHIVE_STATE_NEW, fn);
103 if (options == NULL || options[0] == '\0')
106 data = (char *)malloc(strlen(options) + 1);
107 strcpy(data, options);
108 s = (const char *)data;
111 mod = opt = val = NULL;
113 parse_option(&s, &mod, &opt, &val);
115 r = use_option(a, mod, opt, val);
116 if (r == ARCHIVE_FATAL) {
118 return (ARCHIVE_FATAL);
120 if (r == ARCHIVE_FAILED && mod != NULL) {
122 return (ARCHIVE_FAILED);
124 if (r == ARCHIVE_WARN - 1) {
125 /* The module name is wrong. */
126 archive_set_error(a, ARCHIVE_ERRNO_MISC,
127 "Unknown module name: `%s'", mod);
129 return (ARCHIVE_FAILED);
131 if (r == ARCHIVE_WARN) {
132 /* The option name is wrong. No-one used this. */
133 archive_set_error(a, ARCHIVE_ERRNO_MISC,
134 "Undefined option: `%s%s%s'",
135 mod?mod:"", mod?":":"", opt);
137 return (ARCHIVE_FAILED);
146 return allok ? ARCHIVE_OK : anyok ? ARCHIVE_WARN : ARCHIVE_FAILED;
150 parse_option(const char **s, const char **m, const char **o, const char **v)
152 const char *end, *mod, *opt, *val;
160 p = strchr(opt, ',');
164 end = ((const char *)p) + 1;
167 if (0 == strlen(opt)) {
175 p = strchr(opt, ':');
182 p = strchr(opt, '=');
186 } else if (opt[0] == '!') {