Add `-o` option for all clients loading options from a specific file.

pull/2215/head
Roger Light 5 years ago
parent b4c1f98d62
commit efad820706

@ -27,6 +27,9 @@ Client library:
callback. Closes #2127. callback. Closes #2127.
- Add support for MQTT v5 broker to client topic aliases. - Add support for MQTT v5 broker to client topic aliases.
Clients:
- Add `-o` option for all clients loading options from a specific file.
2.0.9 - 2021-03-xx 2.0.9 - 2021-03-xx
================== ==================

@ -27,7 +27,7 @@ m - PUB,RR (message input)
N - RR,SUB (no end of line) N - RR,SUB (no end of line)
n - PUB,RR (null message) n - PUB,RR (null message)
O O
o - CTRL (options file) o - CTRL,PUB,RR,SUB (options file)
P - PUB,RR,SUB (password) P - PUB,RR,SUB (password)
p - PUB,RR,SUB (port) p - PUB,RR,SUB (port)
Q Q

@ -265,8 +265,32 @@ void client_config_cleanup(struct mosq_config *cfg)
mosquitto_property_free_all(&cfg->unsubscribe_props); mosquitto_property_free_all(&cfg->unsubscribe_props);
mosquitto_property_free_all(&cfg->disconnect_props); mosquitto_property_free_all(&cfg->disconnect_props);
mosquitto_property_free_all(&cfg->will_props); mosquitto_property_free_all(&cfg->will_props);
free(cfg->options_file);
} }
/* Find if there is "-o" in the options */
static int client_config_options_file(struct mosq_config *cfg, int argc, char *argv[])
{
int i;
for(i=1; i<argc; i++){
if(!strcmp(argv[i], "-o")){
if(cfg->options_file){
fprintf(stderr, "Error: Duplicate -o argument given.\n\n");
return 1;
}
if(i==argc-1){
fprintf(stderr, "Error: -o argument given but no options file specified.\n\n");
return 1;
}else{
cfg->options_file = strdup(argv[i+1]);
}
}
}
return 0;
}
int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *argv[]) int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *argv[])
{ {
int rc; int rc;
@ -286,6 +310,11 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
init_config(cfg, pub_or_sub); init_config(cfg, pub_or_sub);
if(client_config_options_file(cfg, argc, argv)){
return 1;
}
if(cfg->options_file == NULL){
/* Default config file */ /* Default config file */
#ifndef WIN32 #ifndef WIN32
env = getenv("XDG_CONFIG_HOME"); env = getenv("XDG_CONFIG_HOME");
@ -343,9 +372,16 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
loc[len-1] = '\0'; loc[len-1] = '\0';
} }
#endif #endif
}
if(loc){ if(cfg->options_file){
fptr = fopen(cfg->options_file, "rt");
}else if(loc){
fptr = fopen(loc, "rt"); fptr = fopen(loc, "rt");
free(loc);
}else{
return 1;
}
if(fptr){ if(fptr){
while(fgets(line, 1024, fptr)){ while(fgets(line, 1024, fptr)){
if(line[0] == '#') continue; /* Comments */ if(line[0] == '#') continue; /* Comments */
@ -373,8 +409,6 @@ int client_config_load(struct mosq_config *cfg, int pub_or_sub, int argc, char *
} }
fclose(fptr); fclose(fptr);
} }
free(loc);
}
/* Deal with real argc/argv */ /* Deal with real argc/argv */
rc = client_config_line_proc(cfg, pub_or_sub, argc, argv); rc = client_config_line_proc(cfg, pub_or_sub, argc, argv);
@ -519,6 +553,7 @@ int cfg_add_topic(struct mosq_config *cfg, int type, char *topic, const char *ar
return 0; return 0;
} }
/* Process a tokenised single line from a file or set of real argc/argv */ /* Process a tokenised single line from a file or set of real argc/argv */
int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, char *argv[]) int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, char *argv[])
{ {
@ -841,6 +876,14 @@ int client_config_line_proc(struct mosq_config *cfg, int pub_or_sub, int argc, c
goto unknown_option; goto unknown_option;
} }
cfg->eol = false; cfg->eol = false;
}else if(!strcmp(argv[i], "-o")){
if(i==argc-1){
fprintf(stderr, "Error: -o argument given but no options file specified.\n\n");
return 1;
}else{
/* Already handled */
}
i++;
}else if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){ }else if(!strcmp(argv[i], "-p") || !strcmp(argv[i], "--port")){
if(i==argc-1){ if(i==argc-1){
fprintf(stderr, "Error: -p argument given but no port specified.\n\n"); fprintf(stderr, "Error: -p argument given but no port specified.\n\n");

@ -126,6 +126,7 @@ struct mosq_config {
bool have_topic_alias; /* pub */ bool have_topic_alias; /* pub */
char *response_topic; /* rr */ char *response_topic; /* rr */
bool tcp_nodelay; bool tcp_nodelay;
char *options_file;
}; };
int client_config_load(struct mosq_config *config, int pub_or_sub, int argc, char *argv[]); int client_config_load(struct mosq_config *config, int pub_or_sub, int argc, char *argv[]);

@ -430,6 +430,7 @@ void print_usage(void)
#endif #endif
printf(" [--property command identifier value]\n"); printf(" [--property command identifier value]\n");
printf(" [-D command identifier value]\n"); printf(" [-D command identifier value]\n");
printf(" [-o options-file]\n");
printf(" mosquitto_pub --help\n\n"); printf(" mosquitto_pub --help\n\n");
printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n"); printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n");
printf(" the client communicates over.\n"); printf(" the client communicates over.\n");
@ -452,6 +453,8 @@ void print_usage(void)
printf(" -m : message payload to send.\n"); printf(" -m : message payload to send.\n");
printf(" -M : the maximum inflight messages for QoS 1/2..\n"); printf(" -M : the maximum inflight messages for QoS 1/2..\n");
printf(" -n : send a null (zero length) message.\n"); printf(" -n : send a null (zero length) message.\n");
printf(" -o : provide options in a file rather than on the command line.\n");
printf(" See the Options section of https://mosquitto.org/man/mosquitto_pub-1.html\n");
printf(" -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.\n"); printf(" -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.\n");
printf(" -P : provide a password\n"); printf(" -P : provide a password\n");
printf(" -q : quality of service level to use for all messages. Defaults to 0.\n"); printf(" -q : quality of service level to use for all messages. Defaults to 0.\n");

@ -224,6 +224,7 @@ void print_usage(void)
printf(" [--proxy socks-url]\n"); printf(" [--proxy socks-url]\n");
#endif #endif
printf(" [-D command identifier value]\n"); printf(" [-D command identifier value]\n");
printf(" [-o options-file]\n");
printf(" mosquitto_rr --help\n\n"); printf(" mosquitto_rr --help\n\n");
printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n"); printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n");
printf(" the client communicates over.\n"); printf(" the client communicates over.\n");
@ -242,6 +243,8 @@ void print_usage(void)
printf(" -L : specify user, password, hostname, port and topic as a URL in the form:\n"); printf(" -L : specify user, password, hostname, port and topic as a URL in the form:\n");
printf(" mqtt(s)://[username[:password]@]host[:port]/topic\n"); printf(" mqtt(s)://[username[:password]@]host[:port]/topic\n");
printf(" -N : do not add an end of line character when printing the payload.\n"); printf(" -N : do not add an end of line character when printing the payload.\n");
printf(" -o : provide options in a file rather than on the command line.\n");
printf(" See the Options section of https://mosquitto.org/man/mosquitto_pub-1.html\n");
printf(" -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.\n"); printf(" -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.\n");
printf(" -P : provide a password\n"); printf(" -P : provide a password\n");
printf(" -q : quality of service level to use for communications. Defaults to 0.\n"); printf(" -q : quality of service level to use for communications. Defaults to 0.\n");

@ -230,6 +230,7 @@ void print_usage(void)
printf(" [--proxy socks-url]\n"); printf(" [--proxy socks-url]\n");
#endif #endif
printf(" [-D command identifier value]\n"); printf(" [-D command identifier value]\n");
printf(" [-o options-file]\n");
printf(" mosquitto_sub --help\n\n"); printf(" mosquitto_sub --help\n\n");
printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n"); printf(" -A : bind the outgoing socket to this host/ip address. Use to control which interface\n");
printf(" the client communicates over.\n"); printf(" the client communicates over.\n");
@ -251,6 +252,8 @@ void print_usage(void)
printf(" -L : specify user, password, hostname, port and topic as a URL in the form:\n"); printf(" -L : specify user, password, hostname, port and topic as a URL in the form:\n");
printf(" mqtt(s)://[username[:password]@]host[:port]/topic\n"); printf(" mqtt(s)://[username[:password]@]host[:port]/topic\n");
printf(" -N : do not add an end of line character when printing the payload.\n"); printf(" -N : do not add an end of line character when printing the payload.\n");
printf(" -o : provide options in a file rather than on the command line.\n");
printf(" See the Options section of https://mosquitto.org/man/mosquitto_pub-1.html\n");
printf(" -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.\n"); printf(" -p : network port to connect to. Defaults to 1883 for plain MQTT and 8883 for MQTT over TLS.\n");
printf(" -P : provide a password\n"); printf(" -P : provide a password\n");
printf(" -q : quality of service level to use for the subscription. Defaults to 0.\n"); printf(" -q : quality of service level to use for the subscription. Defaults to 0.\n");

@ -117,18 +117,41 @@
<refsect1> <refsect1>
<title>Options</title> <title>Options</title>
<para>The options below may be given on the command line, but may also <para>
be placed in a config file located at There are three ways to provide options to mosquitto_pub: the
default config file, a specified config file, or options on
the command line.
</para>
<para>
The default config file is located at
<option>$XDG_CONFIG_HOME/mosquitto_pub</option> or <option>$XDG_CONFIG_HOME/mosquitto_pub</option> or
<option>$HOME/.config/mosquitto_pub</option> with one pair of <option>$HOME/.config/mosquitto_pub</option> on POSIX systems, or
<option>-option <replaceable>value</replaceable></option> <option>%USERPROFILE%\mosquitto_pub</option> on Windows.
per line. The values in the config file will be used as defaults </para>
and can be overridden by using the command line. The exceptions to <para>
this are the message type options, of which only one can be A config file can be specified on the command line using
specified. Note also that currently some options cannot be negated, <option>-o <replaceable>config-file</replaceable></option>.
e.g. <option>-S</option>. Config file lines that have a If the <option>-o</option> option is used, the default config
file will not be loaded.
</para>
<para>
In both cases, the contents of the config file should consist of
options, one per line in the format:
<option>-option <replaceable>value</replaceable></option>. If
options are also specified on the command line, those
options will override the same options set in the config
file. The exceptions to this are the message type options, of which
only one can be specified. Note also that currently some options
cannot be negated, e.g. <option>-S</option>. Config file lines that have a
<option>#</option> as the first character are treated as comments <option>#</option> as the first character are treated as comments
and not processed any further.</para> and not processed any further.
</para>
<para>
It is suggested that config files are primarily used for
authentication purposes. Use of a config file allows you to
authenticate without the need to show the username and password
on the command line.
</para>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><option>-A</option></term> <term><option>-A</option></term>

@ -128,18 +128,41 @@
<refsect1> <refsect1>
<title>Options</title> <title>Options</title>
<para>The options below may be given on the command line, but may also <para>
be placed in a config file located at There are three ways to provide options to mosquitto_rr: the
default config file, a specified config file, or options on
the command line.
</para>
<para>
The default config file is located at
<option>$XDG_CONFIG_HOME/mosquitto_rr</option> or <option>$XDG_CONFIG_HOME/mosquitto_rr</option> or
<option>$HOME/.config/mosquitto_rr</option> with one pair of <option>$HOME/.config/mosquitto_rr</option> on POSIX systems, or
<option>-option <replaceable>value</replaceable></option> <option>%USERPROFILE%\mosquitto_rr</option> on Windows.
per line. The values in the config file will be used as defaults </para>
and can be overridden by using the command line. The exceptions to <para>
this is <option>-t</option>, which if given in the config file will A config file can be specified on the command line using
not be overridden. Note also that currently some options cannot be <option>-o <replaceable>config-file</replaceable></option>.
negated, e.g. <option>-S</option>. Config file lines that have a If the <option>-o</option> option is used, the default config
file will not be loaded.
</para>
<para>
In both cases, the contents of the config file should consist of
options, one per line in the format:
<option>-option <replaceable>value</replaceable></option>. If
options are also specified on the command line, those
options will override the same options set in the config
file. The exceptions to this are the message type options, of which
only one can be specified. Note also that currently some options
cannot be negated, e.g. <option>-S</option>. Config file lines that have a
<option>#</option> as the first character are treated as comments <option>#</option> as the first character are treated as comments
and not processed any further.</para> and not processed any further.
</para>
<para>
It is suggested that config files are primarily used for
authentication purposes. Use of a config file allows you to
authenticate without the need to show the username and password
on the command line.
</para>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><option>-A</option></term> <term><option>-A</option></term>

@ -129,19 +129,41 @@
<refsect1> <refsect1>
<title>Options</title> <title>Options</title>
<para>The options below may be given on the command line, but may also <para>
be placed in a config file located at There are three ways to provide options to mosquitto_sub: the
default config file, a specified config file, or options on
the command line.
</para>
<para>
The default config file is located at
<option>$XDG_CONFIG_HOME/mosquitto_sub</option> or <option>$XDG_CONFIG_HOME/mosquitto_sub</option> or
<option>$HOME/.config/mosquitto_sub</option> with one pair of <option>$HOME/.config/mosquitto_sub</option> on POSIX systems, or
<option>-option <replaceable>value</replaceable></option> <option>%USERPROFILE%\mosquitto_sub</option> on Windows.
per line. The values in the config file will be used as defaults </para>
and can be overridden by using the command line. The exceptions to <para>
this are <option>-t</option> and <option>-T</option>, which if A config file can be specified on the command line using
given in the config file will not be overridden. Note also that <option>-o <replaceable>config-file</replaceable></option>.
currently some options cannot be negated, e.g. If the <option>-o</option> option is used, the default config
<option>-S</option>. Config file lines that have a file will not be loaded.
</para>
<para>
In both cases, the contents of the config file should consist of
options, one per line in the format:
<option>-option <replaceable>value</replaceable></option>. If
options are also specified on the command line, those
options will override the same options set in the config
file. The exceptions to this are the message type options, of which
only one can be specified. Note also that currently some options
cannot be negated, e.g. <option>-S</option>. Config file lines that have a
<option>#</option> as the first character are treated as comments <option>#</option> as the first character are treated as comments
and not processed any further.</para> and not processed any further.
</para>
<para>
It is suggested that config files are primarily used for
authentication purposes. Use of a config file allows you to
authenticate without the need to show the username and password
on the command line.
</para>
<variablelist> <variablelist>
<varlistentry> <varlistentry>
<term><option>-A</option></term> <term><option>-A</option></term>

Loading…
Cancel
Save