mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-11 05:33:47 +01:00
Make the command-line parser understand "commands".
Previously these were implemented with a search in options_init_from_torrc(), but that led to each option being declared more than needed: once to say that it was a valid option, and once to say what it meant.
This commit is contained in:
parent
9826fb1981
commit
c529b3f8f1
@ -2457,6 +2457,8 @@ static const struct {
|
|||||||
const char *name;
|
const char *name;
|
||||||
/** Does this option accept an argument? */
|
/** Does this option accept an argument? */
|
||||||
takes_argument_t takes_argument;
|
takes_argument_t takes_argument;
|
||||||
|
/** If not CMD_RUN_TOR, what should Tor do when it starts? */
|
||||||
|
tor_cmdline_mode_t command;
|
||||||
} CMDLINE_ONLY_OPTIONS[] = {
|
} CMDLINE_ONLY_OPTIONS[] = {
|
||||||
{ .name="-f",
|
{ .name="-f",
|
||||||
.takes_argument=ARGUMENT_NECESSARY },
|
.takes_argument=ARGUMENT_NECESSARY },
|
||||||
@ -2464,31 +2466,44 @@ static const struct {
|
|||||||
{ .name="--defaults-torrc",
|
{ .name="--defaults-torrc",
|
||||||
.takes_argument=ARGUMENT_NECESSARY },
|
.takes_argument=ARGUMENT_NECESSARY },
|
||||||
{ .name="--hash-password",
|
{ .name="--hash-password",
|
||||||
.takes_argument=ARGUMENT_NECESSARY },
|
.takes_argument=ARGUMENT_NECESSARY,
|
||||||
|
.command=CMD_HASH_PASSWORD },
|
||||||
{ .name="--dump-config",
|
{ .name="--dump-config",
|
||||||
.takes_argument=ARGUMENT_OPTIONAL },
|
.takes_argument=ARGUMENT_OPTIONAL,
|
||||||
{ .name="--list-fingerprint" },
|
.command=CMD_DUMP_CONFIG },
|
||||||
{ .name="--keygen" },
|
{ .name="--list-fingerprint",
|
||||||
|
.command=CMD_LIST_FINGERPRINT },
|
||||||
|
{ .name="--keygen",
|
||||||
|
.command=CMD_KEYGEN },
|
||||||
{ .name="--key-expiration",
|
{ .name="--key-expiration",
|
||||||
.takes_argument=ARGUMENT_OPTIONAL },
|
.takes_argument=ARGUMENT_OPTIONAL,
|
||||||
|
.command=CMD_KEY_EXPIRATION },
|
||||||
{ .name="--newpass" },
|
{ .name="--newpass" },
|
||||||
{ .name="--no-passphrase" },
|
{ .name="--no-passphrase" },
|
||||||
{ .name="--passphrase-fd",
|
{ .name="--passphrase-fd",
|
||||||
.takes_argument=ARGUMENT_NECESSARY },
|
.takes_argument=ARGUMENT_NECESSARY },
|
||||||
{ .name="--verify-config" },
|
{ .name="--verify-config",
|
||||||
|
.command=CMD_VERIFY_CONFIG },
|
||||||
{ .name="--ignore-missing-torrc" },
|
{ .name="--ignore-missing-torrc" },
|
||||||
{ .name="--quiet" },
|
{ .name="--quiet" },
|
||||||
{ .name="--hush" },
|
{ .name="--hush" },
|
||||||
{ .name="--version" },
|
{ .name="--version",
|
||||||
{ .name="--list-modules" },
|
.command=CMD_OTHER },
|
||||||
{ .name="--library-versions" },
|
{ .name="--list-modules",
|
||||||
{ .name="-h" },
|
.command=CMD_OTHER },
|
||||||
{ .name="--help" },
|
{ .name="--library-versions",
|
||||||
{ .name="--list-torrc-options" },
|
.command=CMD_OTHER },
|
||||||
{ .name="--list-deprecated-options" },
|
{ .name="-h",
|
||||||
|
.command=CMD_OTHER },
|
||||||
|
{ .name="--help",
|
||||||
|
.command=CMD_OTHER },
|
||||||
|
{ .name="--list-torrc-options",
|
||||||
|
.command=CMD_OTHER },
|
||||||
|
{ .name="--list-deprecated-options",
|
||||||
|
.command=CMD_OTHER },
|
||||||
{ .name="--nt-service" },
|
{ .name="--nt-service" },
|
||||||
{ .name="-nt-service" },
|
{ .name="-nt-service" },
|
||||||
{ NULL, 0 },
|
{ .name=NULL },
|
||||||
};
|
};
|
||||||
|
|
||||||
/** Helper: Read a list of configuration options from the command line. If
|
/** Helper: Read a list of configuration options from the command line. If
|
||||||
@ -2502,6 +2517,7 @@ parsed_cmdline_t *
|
|||||||
config_parse_commandline(int argc, char **argv, int ignore_errors)
|
config_parse_commandline(int argc, char **argv, int ignore_errors)
|
||||||
{
|
{
|
||||||
parsed_cmdline_t *result = tor_malloc_zero(sizeof(parsed_cmdline_t));
|
parsed_cmdline_t *result = tor_malloc_zero(sizeof(parsed_cmdline_t));
|
||||||
|
result->command = CMD_RUN_TOR;
|
||||||
config_line_t *param = NULL;
|
config_line_t *param = NULL;
|
||||||
|
|
||||||
config_line_t **new_cmdline = &result->cmdline_opts;
|
config_line_t **new_cmdline = &result->cmdline_opts;
|
||||||
@ -2515,11 +2531,16 @@ config_parse_commandline(int argc, char **argv, int ignore_errors)
|
|||||||
takes_argument_t want_arg = ARGUMENT_NECESSARY;
|
takes_argument_t want_arg = ARGUMENT_NECESSARY;
|
||||||
int is_cmdline = 0;
|
int is_cmdline = 0;
|
||||||
int j;
|
int j;
|
||||||
|
bool is_a_command = false;
|
||||||
|
|
||||||
for (j = 0; CMDLINE_ONLY_OPTIONS[j].name != NULL; ++j) {
|
for (j = 0; CMDLINE_ONLY_OPTIONS[j].name != NULL; ++j) {
|
||||||
if (!strcmp(argv[i], CMDLINE_ONLY_OPTIONS[j].name)) {
|
if (!strcmp(argv[i], CMDLINE_ONLY_OPTIONS[j].name)) {
|
||||||
is_cmdline = 1;
|
is_cmdline = 1;
|
||||||
want_arg = CMDLINE_ONLY_OPTIONS[j].takes_argument;
|
want_arg = CMDLINE_ONLY_OPTIONS[j].takes_argument;
|
||||||
|
if (CMDLINE_ONLY_OPTIONS[j].command != CMD_RUN_TOR) {
|
||||||
|
is_a_command = true;
|
||||||
|
result->command = CMDLINE_ONLY_OPTIONS[j].command;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2569,6 +2590,10 @@ config_parse_commandline(int argc, char **argv, int ignore_errors)
|
|||||||
log_debug(LD_CONFIG, "command line: parsed keyword '%s', value '%s'",
|
log_debug(LD_CONFIG, "command line: parsed keyword '%s', value '%s'",
|
||||||
param->key, param->value);
|
param->key, param->value);
|
||||||
|
|
||||||
|
if (is_a_command) {
|
||||||
|
result->command_arg = param->value;
|
||||||
|
}
|
||||||
|
|
||||||
if (is_cmdline) {
|
if (is_cmdline) {
|
||||||
*new_cmdline = param;
|
*new_cmdline = param;
|
||||||
new_cmdline = &((*new_cmdline)->next);
|
new_cmdline = &((*new_cmdline)->next);
|
||||||
@ -3020,7 +3045,9 @@ is_local_addr, (const tor_addr_t *addr))
|
|||||||
or_options_t *
|
or_options_t *
|
||||||
options_new(void)
|
options_new(void)
|
||||||
{
|
{
|
||||||
return config_new(get_options_mgr());
|
or_options_t *options = config_new(get_options_mgr());
|
||||||
|
options->command = CMD_RUN_TOR;
|
||||||
|
return options;
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Set <b>options</b> to hold reasonable defaults for most options.
|
/** Set <b>options</b> to hold reasonable defaults for most options.
|
||||||
@ -5277,25 +5304,10 @@ options_init_from_torrc(int argc, char **argv)
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
command = CMD_RUN_TOR;
|
int command = global_cmdline->command;
|
||||||
for (p_index = cmdline_only_options; p_index; p_index = p_index->next) {
|
const char *command_arg = global_cmdline->command_arg;
|
||||||
if (!strcmp(p_index->key,"--keygen")) {
|
/* "Other" has already been handled by this point. */
|
||||||
command = CMD_KEYGEN;
|
tor_assert(command != CMD_OTHER);
|
||||||
} else if (!strcmp(p_index->key, "--key-expiration")) {
|
|
||||||
command = CMD_KEY_EXPIRATION;
|
|
||||||
command_arg = p_index->value;
|
|
||||||
} else if (!strcmp(p_index->key,"--list-fingerprint")) {
|
|
||||||
command = CMD_LIST_FINGERPRINT;
|
|
||||||
} else if (!strcmp(p_index->key, "--hash-password")) {
|
|
||||||
command = CMD_HASH_PASSWORD;
|
|
||||||
command_arg = p_index->value;
|
|
||||||
} else if (!strcmp(p_index->key, "--dump-config")) {
|
|
||||||
command = CMD_DUMP_CONFIG;
|
|
||||||
command_arg = p_index->value;
|
|
||||||
} else if (!strcmp(p_index->key, "--verify-config")) {
|
|
||||||
command = CMD_VERIFY_CONFIG;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (command == CMD_HASH_PASSWORD) {
|
if (command == CMD_HASH_PASSWORD) {
|
||||||
cf_defaults = tor_strdup("");
|
cf_defaults = tor_strdup("");
|
||||||
|
@ -204,6 +204,10 @@ typedef struct {
|
|||||||
/** List of other options, to be handled by the general Tor configuration
|
/** List of other options, to be handled by the general Tor configuration
|
||||||
system. */
|
system. */
|
||||||
struct config_line_t *other_opts;
|
struct config_line_t *other_opts;
|
||||||
|
/** Subcommand that Tor has been told to run */
|
||||||
|
tor_cmdline_mode_t command;
|
||||||
|
/** Argument for the command mode, if any. */
|
||||||
|
const char *command_arg;
|
||||||
} parsed_cmdline_t;
|
} parsed_cmdline_t;
|
||||||
|
|
||||||
parsed_cmdline_t *config_parse_commandline(int argc, char **argv,
|
parsed_cmdline_t *config_parse_commandline(int argc, char **argv,
|
||||||
|
@ -16,7 +16,7 @@
|
|||||||
* Enumeration to describe which command Tor is running. These commands
|
* Enumeration to describe which command Tor is running. These commands
|
||||||
* are controlled by command-line options.
|
* are controlled by command-line options.
|
||||||
**/
|
**/
|
||||||
typedef enum tor_cmdline_mode_t {
|
typedef enum {
|
||||||
CMD_RUN_TOR=0, /**< The default: run Tor as a daemon. */
|
CMD_RUN_TOR=0, /**< The default: run Tor as a daemon. */
|
||||||
CMD_LIST_FINGERPRINT, /**< Running --list-fingerprint. */
|
CMD_LIST_FINGERPRINT, /**< Running --list-fingerprint. */
|
||||||
CMD_HASH_PASSWORD, /**< Running --hash-password. */
|
CMD_HASH_PASSWORD, /**< Running --hash-password. */
|
||||||
@ -24,6 +24,8 @@ typedef enum tor_cmdline_mode_t {
|
|||||||
CMD_DUMP_CONFIG, /**< Running --dump-config. */
|
CMD_DUMP_CONFIG, /**< Running --dump-config. */
|
||||||
CMD_KEYGEN, /**< Running --keygen */
|
CMD_KEYGEN, /**< Running --keygen */
|
||||||
CMD_KEY_EXPIRATION, /**< Running --key-expiration */
|
CMD_KEY_EXPIRATION, /**< Running --key-expiration */
|
||||||
|
CMD_OTHER, /**< Special value: indicates a command that is handled
|
||||||
|
* immediately during configuration processing. */
|
||||||
CMD_RUN_UNITTESTS, /**< Special value: indicates that we have entered
|
CMD_RUN_UNITTESTS, /**< Special value: indicates that we have entered
|
||||||
* the Tor code from the unit tests, not from the
|
* the Tor code from the unit tests, not from the
|
||||||
* regular Tor binary at all. */
|
* regular Tor binary at all. */
|
||||||
|
@ -1357,6 +1357,7 @@ tor_run_main(const tor_main_configuration_t *tor_cfg)
|
|||||||
result = do_dump_config();
|
result = do_dump_config();
|
||||||
break;
|
break;
|
||||||
case CMD_RUN_UNITTESTS: /* only set by test.c */
|
case CMD_RUN_UNITTESTS: /* only set by test.c */
|
||||||
|
case CMD_OTHER: /* Handled in config.c */
|
||||||
default:
|
default:
|
||||||
log_warn(LD_BUG,"Illegal command number %d: internal error.",
|
log_warn(LD_BUG,"Illegal command number %d: internal error.",
|
||||||
get_options()->command);
|
get_options()->command);
|
||||||
|
@ -340,6 +340,7 @@ nt_service_main(void)
|
|||||||
"or --key-expiration) in NT service.");
|
"or --key-expiration) in NT service.");
|
||||||
break;
|
break;
|
||||||
case CMD_RUN_UNITTESTS:
|
case CMD_RUN_UNITTESTS:
|
||||||
|
case CMD_OTHER:
|
||||||
default:
|
default:
|
||||||
log_err(LD_CONFIG, "Illegal command number %d: internal error.",
|
log_err(LD_CONFIG, "Illegal command number %d: internal error.",
|
||||||
get_options()->command);
|
get_options()->command);
|
||||||
|
Loading…
Reference in New Issue
Block a user