commit ce60454afd4b3792b2dc3bf3ed68948bb52fe49a
Author: Daniel Pinto <danielpinto52(a)gmail.com>
Date: Sun Mar 28 03:56:31 2021 +0100
Add long format name --torrc-file for command line option -f. #40324
---
changes/ticket40324 | 3 +++
doc/man/tor.1.txt | 2 +-
src/app/config/config.c | 31 ++++++++++++++++++++++++-------
src/app/main/ntmain.c | 3 ++-
4 files changed, 30 insertions(+), 9 deletions(-)
diff --git a/changes/ticket40324 b/changes/ticket40324
new file mode 100644
index 0000000000..21c05c6e53
--- /dev/null
+++ b/changes/ticket40324
@@ -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.
diff --git a/doc/man/tor.1.txt b/doc/man/tor.1.txt
index 8761237aff..7222cd0548 100644
--- a/doc/man/tor.1.txt
+++ b/doc/man/tor.1.txt
@@ -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
diff --git a/src/app/config/config.c b/src/app/config/config.c
index b6ad1e0808..5115835a0c 100644
--- a/src/app/config/config.c
+++ b/src/app/config/config.c
@@ -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);
diff --git a/src/app/main/ntmain.c b/src/app/main/ntmain.c
index 480fba8650..9f2f52fb2e 100644
--- a/src/app/main/ntmain.c
+++ b/src/app/main/ntmain.c
@@ -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]);
}