Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ Makefile.in
/depcomp
/install-sh
/missing
config.guess
config.sub

# Files generated by an in-tree build
Makefile
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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.

Expand All @@ -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
Expand Down
29 changes: 25 additions & 4 deletions src/mq.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
;

Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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;
Expand Down