From 9b550ad2043dee35b42d053899fed62b6ac53c92 Mon Sep 17 00:00:00 2001 From: Natanael Copa Date: Wed, 4 Dec 2013 15:27:47 +0100 Subject: [PATCH] implement -v/--verbose to optionally pass the the topic --- mqtt-exec.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/mqtt-exec.c b/mqtt-exec.c index be52ba4..e6ab0ee 100644 --- a/mqtt-exec.c +++ b/mqtt-exec.c @@ -25,9 +25,12 @@ void message_cb(struct mosquitto *mosq, void *obj, const struct mosquitto_message *msg) { struct userdata *ud = (struct userdata *)obj; - if (msg->payloadlen) { + if (msg->payloadlen || ud->verbose) { if (ud->command_argv && fork() == 0) { - ud->command_argv[ud->command_argc-1] = msg->payload; + if (ud->verbose) + ud->command_argv[ud->command_argc-2] = msg->topic; + ud->command_argv[ud->command_argc-1] = + msg->payloadlen ? msg->payload : NULL; execv(ud->command_argv[0], ud->command_argv); perror(ud->command_argv[0]); _exit(1); @@ -74,6 +77,7 @@ int main(int argc, char *argv[]) {"keepalive", required_argument, 0, 'k' }, {"port", required_argument, 0, 'p' }, {"topic", required_argument, 0, 't' }, + {"verbose", no_argument, 0, 'v' }, { 0, 0, 0, 0} }; int debug = 0; @@ -91,7 +95,7 @@ int main(int argc, char *argv[]) memset(hostname, 0, sizeof(hostname)); memset(id, 0, sizeof(id)); - while ((c = getopt_long(argc, argv, "dh:k:p:t:", opts, &i)) != -1) { + while ((c = getopt_long(argc, argv, "dh:k:p:t:v", opts, &i)) != -1) { switch(c) { case 'd': debug = 1; @@ -108,6 +112,9 @@ int main(int argc, char *argv[]) case 't': ud.topic = optarg; break; + case 'v': + ud.verbose = 1; + break; case '?': return usage(1); } @@ -116,15 +123,13 @@ int main(int argc, char *argv[]) if ((ud.topic == NULL) || (optind == argc)) return usage(1); - ud.command_argc = (argc - optind) + 1; + ud.command_argc = (argc - optind) + 1 + ud.verbose; ud.command_argv = malloc((ud.command_argc + 1) * sizeof(char *)); if (ud.command_argv == NULL) return perror_ret("malloc"); - for (i=0; i < ud.command_argc; i++) - ud.command_argv[i] = argv[optind+i]; - ud.command_argv[ud.command_argc-1] = NULL; - ud.command_argv[ud.command_argc] = NULL; + for (i=0; i <= ud.command_argc; i++) + ud.command_argv[i] = optind+i < argc ? argv[optind+i] : NULL; /* generate an id */ gethostname(hostname, sizeof(hostname)-1); -- 1.8.5.1