diff --git a/.gitignore b/.gitignore index c4884b0..d454c55 100644 --- a/.gitignore +++ b/.gitignore @@ -8,6 +8,8 @@ Makefile.in /depcomp /install-sh /missing +config.guess +config.sub # Files generated by an in-tree build Makefile diff --git a/README.md b/README.md index 206e0ff..1cc32ab 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Usage: mq [OPTION...] create QNAME or: mq [OPTION...] info QNAME or: mq [OPTION...] unlink QNAME - or: mq [OPTION...] send QNAME MESSAGE + or: mq [OPTION...] send QNAME [MESSAGE] or: mq [OPTION...] recv QNAME A command line tool to use Posix Message Queues from the shell @@ -30,6 +30,8 @@ A command line tool to use Posix Message Queues from the shell --usage Give a short usage message -V, --version Print program version +If the MESSAGE parameter is not provided for the send command, the message data will be read from standard input. + Mandatory or optional arguments to long options are also mandatory or optional for any corresponding short options. @@ -43,6 +45,7 @@ Commands: Examples: mq create /myqueue mq send /myqueue "hello" -n + echo "hello" | mq send /myqueue mq info /myqueue mq recv /myqueue mq unlink /myqueue diff --git a/src/mq.c b/src/mq.c index a5e5b8b..8c58d52 100644 --- a/src/mq.c +++ b/src/mq.c @@ -39,7 +39,7 @@ static char args_doc[] = "create QNAME\n" "info QNAME\n" "unlink QNAME\n" - "send QNAME MESSAGE\n" + "send QNAME [MESSAGE]\n" "recv QNAME" ; @@ -172,7 +172,6 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state) case ARGP_KEY_END: if (!args->command) argp_usage(state); if (!args->qname) argp_usage(state); - if (0 == strcmp(args->command, "send") && !args->message) argp_usage(state); return ARGP_ERR_UNKNOWN; default: @@ -253,8 +252,30 @@ static int cmd_send(const struct arguments *args) LOG_VERBOSE_HEXA(args, (const uint8_t *)args->message, args->msglen); - /* Send */ - int ret = mq_send(queue, args->message, args->msglen, args->priority); + int ret; + // Read the message from standard input if no message argument is passed + if (!args->message) { + char *message = malloc(sizeof(char) * args->msgsize); + if (! message) { + LOG_ERR("mq_send error: failed to allocate stdin message buffer: details %s", strerror(errno)); + return 1; + } + + ssize_t message_length = read(STDIN_FILENO, message, args->msgsize); + if (message_length == -1) { + LOG_ERR("mq_send error: failed to read from stdin: details: %s", strerror(errno)); + return 1; + } + /* Send message*/ + ret = mq_send(queue, message, message_length, args->priority); + free(message); + } + // Otherwise use the message argument + else { + /* Send message*/ + ret = mq_send(queue, args->message, args->msglen, args->priority); + } + if (0 != ret) { LOG_ERR("mq_send error: %s", strerror(errno)); ret = 1;