#include "git-compat-util.h"
#include "parse-options.h"
#include "cache.h"
#include "config.h"
#include "commit.h"
#include "color.h"
#include "utf8.h"
static int disallow_abbreviated_options;
enum opt_parsed {
OPT_LONG = 0,
OPT_SHORT = 1<<0,
OPT_UNSET = 1<<1,
};
static int optbug(const struct option *opt, const char *reason)
{
if (opt->long_name) {
if (opt->short_name)
return error("BUG: switch '%c' (--%s) %s",
opt->short_name, opt->long_name, reason);
return error("BUG: option '%s' %s", opt->long_name, reason);
}
return error("BUG: switch '%c' %s", opt->short_name, reason);
}
static const char *optname(const struct option *opt, enum opt_parsed flags)
{
static struct strbuf sb = STRBUF_INIT;
strbuf_reset(&sb);
if (flags & OPT_SHORT)
strbuf_addf(&sb, "switch `%c'", opt->short_name);
else if (flags & OPT_UNSET)
strbuf_addf(&sb, "option `no-%s'", opt->long_name);
else if (flags == OPT_LONG)
strbuf_addf(&sb, "option `%s'", opt->long_name);
else
BUG("optname() got unknown flags %d", flags);
return sb.buf;
}
static enum parse_opt_result get_arg(struct parse_opt_ctx_t *p,
const struct option *opt,
enum opt_parsed flags, const char **arg)
{
if (p->opt) {
*arg = p->opt;
p->opt = NULL;
} else if (p->argc == 1 && (opt->flags & PARSE_OPT_LASTARG_DEFAULT)) {
*arg = (const char *)opt->defval;
} else if (p->argc > 1) {
p->argc--;
*arg = *++p->argv;
} else
return error(_("%s requires a value"), optname(opt, flags));
return 0;
}
static void fix_filename(const char *prefix, const char **file)
{
if (!file || !*file || !prefix || is_absolute_path(*file)
|| !strcmp("-", *file))
return;
*file = prefix_filename(prefix, *file);
}
static enum parse_opt_result opt_command_mode_error(
const struct option *opt,
const struct option *all_opts,
enum opt_parsed flags)
{
const struct option *that;
struct strbuf that_name = STRBUF_INIT;
/*
* Find the other option that was used to set the variable
* already, and report that this is not compatible with it.
*/
for (that = all_opts; that->type != OPTION_END; that++) {
if (that == opt ||
!(that->flags & PARSE_OPT_CMDMODE) ||
that->value != opt->value ||
that->defval != *(int *)opt->value)
continue;
if (that->long_name)
strbuf_addf(&that_name, "--%s", that->long_name);
else
strbuf_addf(&that_name, "-%c", that->short_name);
error(_("%s is incompatible with %s"),
optname(opt, flags), that_name.buf);
strbuf_release(&that_name);
return PARSE_OPT_ERROR;
}
return error(_("%s : incompatible with something else"),
optname(opt, flags));
}
static enum parse_opt_result get_value(struct parse_opt_ctx_t *p,
const struct option *opt,
const struct option *all_opts,
enum opt_parsed flags)
{
const char *s, *arg;
const int unset = flags & OPT_UNSET;
int err;
if (unset && p->opt)
return error(_("%s takes no value"), optname(opt, flags));
if (unset && (opt->flags & PARSE_OPT_NONEG))
return error(_("%s isn't available"), optname(opt, flags));
if (!(flags & OPT_SHORT) && p->opt && (opt->flags & PARSE_OPT_NOARG))
return error(_("%s takes no value"), optname(opt, flags));
/*
* Giving the same mode option twice, although unnecessary,
* is not a grave error, so let it pass.
*/
if ((opt->flags & PARSE_OPT_CMDMODE) &&
*(int *)opt->value && *(int *)opt->value != opt->defval)
return opt_command_mode_error(opt, all_opts, flags);
switch (opt->type) {
case OPTION_LOWLEVEL_CALLBACK:
return opt->ll_callback(p, opt, NULL, unset);
case OPTION_BIT:
if (unset)
*(int *)opt->value &= ~opt->defval;
else
*(int *)opt->value |= opt->defval;
return 0;
case OPTION_NEGBIT:
if (unset)
*(int *)opt->value |= opt->defval;
else
*(int *)opt->value &= ~opt->defval;
return 0;
case OPTION_BITOP:
if (unset)
BUG("BITOP can't have unset form");
*(int *)opt->value &= ~opt->extra;
*(int *)opt->value |= opt->defval;
return 0;
case OPTION_COUNTUP:
if (*(int *)opt->value < 0)
*(int *)opt->value = 0;
*(int *)opt->value = unset ? 0 : *(int *)opt->value + 1;
return 0;
case OPTION_SET_INT:
*(int *)opt->value = unset ? 0 : opt->defval;
return 0;
case OPTION_STRING:
if (unset)
*(const char **)opt->value = NULL;
else if (opt->flags & PARSE_OPT_OPTARG && !p->opt)
*(const char **)opt->value = (const char *)opt->defval;
else
return get_arg(p, opt, flags, (const char **)opt->value);
|