Add long format name --torrc-file for command line option -f. #40324

This commit is contained in:
Daniel Pinto 2021-03-28 03:56:31 +01:00
parent 6c14f9076f
commit ce60454afd
4 changed files with 30 additions and 9 deletions

3
changes/ticket40324 Normal file
View File

@ -0,0 +1,3 @@
o Minor features (cmdline):
- Add long format name --torrc-file for command line option -f. Closes
ticket 40324. Patch by Daniel Pinto.

View File

@ -67,7 +67,7 @@ The following options in this section are only recognized on the
[[opt-h]] **`-h`**, **`--help`**::
Display a short help message and exit.
[[opt-f]] **`-f`** __FILE__::
[[opt-f]] **`-f`**, **`--torrc-file`** __FILE__::
Specify a new configuration file to contain further Tor configuration
options, or pass *-* to make Tor read its configuration from standard
input. (Default: **`@CONFDIR@/torrc`**, or **`$HOME/.torrc`** if

View File

@ -2432,6 +2432,8 @@ typedef enum {
static const struct {
/** The string that the user has to provide. */
const char *name;
/** Optional short name. */
const char *short_name;
/** Does this option accept an argument? */
takes_argument_t takes_argument;
/** If not CMD_RUN_TOR, what should Tor do when it starts? */
@ -2439,7 +2441,8 @@ static const struct {
/** If nonzero, set the quiet level to this. 1 is "hush", 2 is "quiet" */
int quiet;
} CMDLINE_ONLY_OPTIONS[] = {
{ .name="-f",
{ .name="--torrc-file",
.short_name="-f",
.takes_argument=ARGUMENT_NECESSARY },
{ .name="--allow-missing-torrc" },
{ .name="--defaults-torrc",
@ -2482,10 +2485,8 @@ static const struct {
{ .name="--library-versions",
.command=CMD_IMMEDIATE,
.quiet=QUIET_HUSH },
{ .name="-h",
.command=CMD_IMMEDIATE,
.quiet=QUIET_HUSH },
{ .name="--help",
.short_name="-h",
.command=CMD_IMMEDIATE,
.quiet=QUIET_HUSH },
{ .name="--list-torrc-options",
@ -2529,7 +2530,9 @@ config_parse_commandline(int argc, char **argv, int ignore_errors)
bool is_a_command = false;
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) ||
(CMDLINE_ONLY_OPTIONS[j].short_name &&
!strcmp(argv[i], CMDLINE_ONLY_OPTIONS[j].short_name))) {
is_cmdline = 1;
want_arg = CMDLINE_ONLY_OPTIONS[j].takes_argument;
if (CMDLINE_ONLY_OPTIONS[j].command != CMD_RUN_TOR) {
@ -4307,6 +4310,8 @@ find_torrc_filename(const config_line_t *cmd_arg,
char *fname=NULL;
const config_line_t *p_index;
const char *fname_opt = defaults_file ? "--defaults-torrc" : "-f";
const char *fname_long_opt = defaults_file ? "--defaults-torrc" :
"--torrc-file";
const char *ignore_opt = defaults_file ? NULL : "--ignore-missing-torrc";
const char *keygen_opt = "--keygen";
@ -4314,10 +4319,12 @@ find_torrc_filename(const config_line_t *cmd_arg,
*ignore_missing_torrc = 1;
for (p_index = cmd_arg; p_index; p_index = p_index->next) {
if (!strcmp(p_index->key, fname_opt)) {
// options_init_from_torrc ensures only the short or long name is present
if (!strcmp(p_index->key, fname_opt) ||
!strcmp(p_index->key, fname_long_opt)) {
if (fname) {
log_warn(LD_CONFIG, "Duplicate %s options on command line.",
fname_opt);
p_index->key);
tor_free(fname);
}
fname = expand_filename(p_index->value);
@ -4521,6 +4528,16 @@ options_init_from_torrc(int argc, char **argv)
} else {
cf_defaults = load_torrc_from_disk(cmdline_only_options, 1);
const config_line_t *f_line = config_line_find(cmdline_only_options, "-f");
const config_line_t *f_line_long = config_line_find(cmdline_only_options,
"--torrc-file");
if (f_line && f_line_long) {
log_err(LD_CONFIG, "-f and --torrc-file cannot be used together.");
retval = -1;
goto err;
} else if (f_line_long) {
f_line = f_line_long;
}
const int read_torrc_from_stdin =
(f_line != NULL && strcmp(f_line->value, "-") == 0);

View File

@ -500,7 +500,8 @@ nt_service_command_line(int *using_default_torrc)
if (!strcmp(backup_argv[i], "--options") ||
!strcmp(backup_argv[i], "-options")) {
while (++i < backup_argc) {
if (!strcmp(backup_argv[i], "-f"))
if (!strcmp(backup_argv[i], "-f") ||
!strcmp(backup_argv[i], "--torrc-file"))
*using_default_torrc = 0;
smartlist_add(sl, backup_argv[i]);
}