/*-
* Copyright (c) 2007 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
if (is_opt) {
/* Do we already have "-o" at the beginning of outargs? */
if (outargs->argc >= 3 && strcmp(outargs->argv[1], "-o") == 0) {
/* Append the option to the comma-separated list. */
if (fuse_opt_add_opt_escaped(&outargs->argv[2], arg) == -1)
return -1;
}
else {
/* Insert -o arg at the beginning. */
if (fuse_opt_insert_arg(outargs, 1, "-o") == -1)
return -1;
if (fuse_opt_insert_arg(outargs, 2, arg) == -1)
return -1;
}
}
else {
if (fuse_opt_add_arg(outargs, arg) == -1)
return -1;
}
return 0;
}
/* Skip the current argv if possible. */
static int next_arg(const struct fuse_args *args, int *i)
{
if (*i + 1 >= args->argc) {
(void)fprintf(stderr, "fuse: missing argument"
" after '%s'\n", args->argv[*i]);
return -1;
}
else {
*i += 1;
return 0;
}
}
/* Parse a single argument with a matched template. */
static int
parse_matched_arg(const char* arg, struct fuse_args *outargs,
const struct fuse_opt* opt, ssize_t sep_idx, void* data,
fuse_opt_proc_t proc, bool is_opt)
{
if (opt->offset == -1) {
/* The option description does not want any variables to be
* updated.*/
if (call_proc(proc, data, arg, opt->value, outargs, is_opt) == -1)
return -1;
}
else {
void *var = (char*)data + opt->offset;
if (opt->templ[sep_idx + 2] == 's') {
char* dup = strdup(param);
if (dup == NULL)
return -1;
*(char **)var = dup;
}
else {
/* The format string is not a literal. We all know
* this is a bad idea but it's exactly what fuse_opt
* wants to do... */
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wformat-nonliteral"
if (sscanf(param, &opt->templ[sep_idx + 1], var) == -1) {
#pragma GCC diagnostic pop
(void)fprintf(stderr, "fuse: '%s' is not a "
"valid parameter for option '%.*s'\n",
param, (int)sep_idx, opt->templ);
return -1;
}
}
}
else {
/* No parameter format is given. */
*(int *)var = opt->value;
}
}
return 0;
}
/* Parse a single argument with matching templates. */
static int
parse_arg(struct fuse_args* args, int *argi, const char* arg,
struct fuse_args *outargs, void *data,
const struct fuse_opt *opts, fuse_opt_proc_t proc, bool is_opt)
{
ssize_t sep_idx;
const struct fuse_opt *opt = find_opt(opts, arg, &sep_idx);
if (opt) {
/* An argument can match to multiple templates. Process them
* all. */
for (; opt != NULL && opt->templ != NULL;
opt = find_opt(++opt, arg, &sep_idx)) {
if (sep_idx > 0 && opt->templ[sep_idx] == ' ' &&
arg[sep_idx] == '\0') {
/* The template "-x %y" requests a separate
* parameter "%y". Try to find one. */
char *new_arg;
int rv;
if (rv == -1)
return -1;
}
else {
int rv;
rv = parse_matched_arg(arg, outargs, opt, sep_idx,
data, proc, is_opt);
if (rv == -1)
return -1;
}
}
return 0;
}
else {
/* No templates matched to it so just invoke the callback. */
return call_proc(proc, data, arg, FUSE_OPT_KEY_OPT, outargs, is_opt);
}
}
/* Parse a comma-separated list of options which possibly has escaped
* characters. */
static int
parse_opts(struct fuse_args *args, int *argi, const char* arg,
struct fuse_args *outargs, void *data,
const struct fuse_opt *opts, fuse_opt_proc_t proc)
{
char *opt;
size_t i, opt_len = 0;
/* An unescaped option can never be longer than the original
* list. */
if ((opt = malloc(strlen(arg) + 1)) == NULL)
return -1;
for (i = 0; i < strlen(arg); i++) {
if (arg[i] == ',') {
opt[opt_len] = '\0';
if (parse_arg(args, argi, opt, outargs,
data, opts, proc, true) == -1) {
free(opt);
return -1;
}
/* Start a new option. */
opt_len = 0;
}
else if (arg[i] == '\\' && arg[i+1] != '\0') {
/* Unescape it. */
opt[opt_len++] = arg[i+1];
i++;
}
else {
opt[opt_len++] = arg[i];
}
}
/* Parse the last option here. */
opt[opt_len] = '\0';
if (parse_arg(args, argi, opt, outargs, data, opts, proc, true) == -1) {
free(opt);
return -1;
}
free(opt);
return 0;
}
static int
parse_all(struct fuse_args *args, struct fuse_args *outargs, void *data,
const struct fuse_opt *opts, fuse_opt_proc_t proc)
{
bool nonopt = false; /* Have we seen the "--" marker? */
int i;
/* The first argument, the program name, is implicitly
* FUSE_OPT_KEY_KEEP. */
if (args->argc > 0) {
if (fuse_opt_add_arg(outargs, args->argv[0]) == -1)
return -1;
}
/* the real loop to process the arguments */
for (i = 1; i < args->argc; i++) {
const char *arg = args->argv[i];
/* The "--" marker at the last of outargs should be removed */
if (nonopt && strcmp(outargs->argv[outargs->argc - 1], "--") == 0) {
free(outargs->argv[outargs->argc - 1]);
outargs->argv[--outargs->argc] = NULL;
}