diff options
author | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-07-19 18:42:58 +0200 |
---|---|---|
committer | Matthias Schiffer <mschiffer@universe-factory.net> | 2013-07-19 18:42:58 +0200 |
commit | f0a655e9db3a46ab3f0c8bba7af548ff440ab98e (patch) | |
tree | cac01d2c5f0884809e0699a7897ff0a844f75bba /src | |
parent | ca648e7a3b76d71817b38d5bc7d765fc994f40ab (diff) | |
download | fastd-f0a655e9db3a46ab3f0c8bba7af548ff440ab98e.tar fastd-f0a655e9db3a46ab3f0c8bba7af548ff440ab98e.zip |
Nicer command line option specification
Diffstat (limited to 'src')
-rw-r--r-- | src/config.c | 90 | ||||
-rw-r--r-- | src/options.def.h | 29 |
2 files changed, 58 insertions, 61 deletions
diff --git a/src/config.c b/src/config.c index 95e22db..16a9a80 100644 --- a/src/config.c +++ b/src/config.c @@ -552,39 +552,6 @@ static void count_peers(fastd_context_t *ctx, fastd_config_t *conf) { } } - -#define OPTIONS \ - OPTION(usage, "--help" OR "-h", "Shows this help text") \ - OPTION(version, "--version" OR "-v", "Shows the fastd version") \ - OPTION(option_daemon, "--daemon" OR "-d", "Runs fastd in the background") \ - OPTION_ARG(option_user, "--user", "<user>", "Sets the user to run fastd as") \ - OPTION_ARG(option_group, "--group", "<group>", "Sets the group to run fastd as") \ - OPTION_ARG(option_pid_file, "--pid-file", "<filename>", "Writes fastd's PID to the specified file") \ - OPTION_ARG(option_log_level, "--log-level", "error|warn|info|verbose|debug", "Sets the stderr log level; default is info, if no alternative log destination is configured") \ - OPTION_ARG(option_syslog_level, "--syslog-level", "error|warn|info|verbose|debug", "Sets the log level for syslog output; default is not to use syslog") \ - OPTION_ARG(option_syslog_ident, "--syslog-ident", "<ident>", "Sets the syslog identification; default is 'fastd'") \ - OPTION(option_hide_ip_addresses, "--hide-ip-addresses", "Hides IP addresses in log output") \ - OPTION(option_hide_mac_addresses, "--hide-mac-addresses", "Hides MAC addresses in log output") \ - OPTION_ARG(option_config, "--config" OR "-c", "<filename>", "Loads a config file") \ - OPTION_ARG(option_config_peer, "--config-peer", "<filename>", "Loads a config file for a single peer") \ - OPTION_ARG(option_config_peer_dir, "--config-peer-dir", "<dir>", "Loads all files from a directory as peer configs") \ - OPTION_ARG(option_mode, "--mode" OR "-m", "tap|tun", "Sets the mode of the interface") \ - OPTION_ARG(option_interface, "--interface" OR "-i", "<name>", "Sets the name of the TUN/TAP interface to use") \ - OPTION_ARG(option_mtu, "--mtu" OR "-M", "<mtu>", "Sets the MTU; must be at least 576") \ - OPTION_ARG(option_bind, "--bind" OR "-b", "<address>:<port>", "Sets the bind address") \ - OPTION_ARG(option_protocol, "--protocol" OR "-p", "<protocol>", "Sets the protocol") \ - OPTION_ARG(option_method, "--method", "<method>", "Sets the encryption method") \ - OPTION(option_forward, "--forward", "Enables forwarding of packets between peers; read the documentation before use!") \ - OPTION_ARG(option_on_up, "--on-up", "<command>", "Sets a shell command to execute after interface creation") \ - OPTION_ARG(option_on_down, "--on-down", "<command>", "Sets a shell command to execute before interface destruction") \ - OPTION_ARG(option_on_establish, "--on-establish", "<command>", "Sets a shell command to execute when a new connection is established") \ - OPTION_ARG(option_on_disestablish, "--on-disestablish", "<command>", "Sets a shell command to execute when a connection is lost") \ - OPTION_ARG(option_on_verify, "--on-verify", "<command>", "Sets a shell command to execute to check a connection attempt by an unknown peer") \ - OPTION(option_generate_key, "--generate-key", "Generates a new keypair") \ - OPTION(option_show_key, "--show-key", "Shows the public key corresponding to the configured secret") \ - OPTION(option_machine_readable, "--machine-readable", "Suppresses output of explaining text in the --show-key and --generate-key commands") - - static void print_usage(const char *options, const char *message) { /* 28 spaces */ static const char spaces[] = " "; @@ -602,18 +569,17 @@ static void print_usage(const char *options, const char *message) { } static void usage(fastd_context_t *ctx, fastd_config_t *conf) { -#define OR ", " -#define OPTION(func, options, message) print_usage(" " options, message); -#define OPTION_ARG(func, options, arg, message) print_usage(" " options " " arg, message); - puts("fastd (Fast and Secure Tunnelling Daemon) " FASTD_VERSION " usage:\n"); - OPTIONS - exit(0); - +#define OR ", " +#define OPTION(func, options, message) print_usage(" " options, message) +#define OPTION_ARG(func, options, arg, message) print_usage(" " options " " arg, message) +#include "options.def.h" #undef OR #undef OPTION #undef OPTION_ARG + + exit(0); } static void version(fastd_context_t *ctx, fastd_config_t *conf) { @@ -913,27 +879,33 @@ static void configure_user(fastd_context_t *ctx, fastd_config_t *conf) { } void fastd_configure(fastd_context_t *ctx, fastd_config_t *conf, int argc, char *const argv[]) { -#define OR , -#define OPTION(func, options, message) \ - if(config_match(argv[i], options, NULL)) { \ - i++; \ - func(ctx, conf); \ - continue; \ - } -#define OPTION_ARG(func, options, arg, message) \ - if(config_match(argv[i], options, NULL)) { \ - i+=2; \ - if (i > argc) \ - exit_error(ctx, "config error: option `%s' needs an argument; see --help for usage", argv[i-2]); \ - func(ctx, conf, argv[i-1]); \ - continue; \ - } - default_config(conf); int i = 1; while (i < argc) { - OPTIONS +#define OR , +#define OPTION(func, options, message) \ + ({ \ + if(config_match(argv[i], options, NULL)) { \ + i++; \ + func(ctx, conf); \ + continue; \ + } \ + }) +#define OPTION_ARG(func, options, arg, message) \ + ({ \ + if(config_match(argv[i], options, NULL)) { \ + i+=2; \ + if (i > argc) \ + exit_error(ctx, "config error: option `%s' needs an argument; see --help for usage", argv[i-2]); \ + func(ctx, conf, argv[i-1]); \ + continue; \ + } \ + }) +#include "options.def.h" +#undef OR +#undef OPTION +#undef OPTION_ARG exit_error(ctx, "config error: unknown option `%s'; see --help for usage", argv[i]); } @@ -960,10 +932,6 @@ void fastd_configure(fastd_context_t *ctx, fastd_config_t *conf, int argc, char exit_error(ctx, "config error: neither fixed peers nor peer dirs have been configured"); configure_user(ctx, conf); - -#undef OR -#undef OPTION -#undef OPTION_ARG } static void peer_dirs_read_peer_group(fastd_context_t *ctx, fastd_config_t *new_conf) { diff --git a/src/options.def.h b/src/options.def.h new file mode 100644 index 0000000..42e6d6f --- /dev/null +++ b/src/options.def.h @@ -0,0 +1,29 @@ +OPTION(usage, "--help" OR "-h", "Shows this help text"); +OPTION(version, "--version" OR "-v", "Shows the fastd version"); +OPTION(option_daemon, "--daemon" OR "-d", "Runs fastd in the background"); +OPTION_ARG(option_user, "--user", "<user>", "Sets the user to run fastd as"); +OPTION_ARG(option_group, "--group", "<group>", "Sets the group to run fastd as"); +OPTION_ARG(option_pid_file, "--pid-file", "<filename>", "Writes fastd's PID to the specified file"); +OPTION_ARG(option_log_level, "--log-level", "error|warn|info|verbose|debug", "Sets the stderr log level; default is info, if no alternative log destination is configured"); +OPTION_ARG(option_syslog_level, "--syslog-level", "error|warn|info|verbose|debug", "Sets the log level for syslog output; default is not to use syslog"); +OPTION_ARG(option_syslog_ident, "--syslog-ident", "<ident>", "Sets the syslog identification; default is 'fastd'"); +OPTION(option_hide_ip_addresses, "--hide-ip-addresses", "Hides IP addresses in log output"); +OPTION(option_hide_mac_addresses, "--hide-mac-addresses", "Hides MAC addresses in log output"); +OPTION_ARG(option_config, "--config" OR "-c", "<filename>", "Loads a config file"); +OPTION_ARG(option_config_peer, "--config-peer", "<filename>", "Loads a config file for a single peer"); +OPTION_ARG(option_config_peer_dir, "--config-peer-dir", "<dir>", "Loads all files from a directory as peer configs"); +OPTION_ARG(option_mode, "--mode" OR "-m", "tap|tun", "Sets the mode of the interface"); +OPTION_ARG(option_interface, "--interface" OR "-i", "<name>", "Sets the name of the TUN/TAP interface to use"); +OPTION_ARG(option_mtu, "--mtu" OR "-M", "<mtu>", "Sets the MTU; must be at least 576"); +OPTION_ARG(option_bind, "--bind" OR "-b", "<address>:<port>", "Sets the bind address"); +OPTION_ARG(option_protocol, "--protocol" OR "-p", "<protocol>", "Sets the protocol"); +OPTION_ARG(option_method, "--method", "<method>", "Sets the encryption method"); +OPTION(option_forward, "--forward", "Enables forwarding of packets between peers; read the documentation before use!"); +OPTION_ARG(option_on_up, "--on-up", "<command>", "Sets a shell command to execute after interface creation"); +OPTION_ARG(option_on_down, "--on-down", "<command>", "Sets a shell command to execute before interface destruction"); +OPTION_ARG(option_on_establish, "--on-establish", "<command>", "Sets a shell command to execute when a new connection is established"); +OPTION_ARG(option_on_disestablish, "--on-disestablish", "<command>", "Sets a shell command to execute when a connection is lost"); +OPTION_ARG(option_on_verify, "--on-verify", "<command>", "Sets a shell command to execute to check a connection attempt by an unknown peer"); +OPTION(option_generate_key, "--generate-key", "Generates a new keypair"); +OPTION(option_show_key, "--show-key", "Shows the public key corresponding to the configured secret"); +OPTION(option_machine_readable, "--machine-readable", "Suppresses output of explaining text in the --show-key and --generate-key commands"); |