diff options
author | Mattias Andrée <maandree@operamail.com> | 2015-04-16 05:47:40 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2015-04-16 05:47:40 +0200 |
commit | ce98d70d2b89347a4f6309f582ba03d898fee3fc (patch) | |
tree | 5c23808d32ca3f62d74a7535cd65c8f962449337 /src/cmdline.c | |
parent | m doc (diff) | |
download | bus-ce98d70d2b89347a4f6309f582ba03d898fee3fc.tar.gz bus-ce98d70d2b89347a4f6309f582ba03d898fee3fc.tar.bz2 bus-ce98d70d2b89347a4f6309f582ba03d898fee3fc.tar.xz |
misc
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/cmdline.c')
-rw-r--r-- | src/cmdline.c | 86 |
1 files changed, 53 insertions, 33 deletions
diff --git a/src/cmdline.c b/src/cmdline.c index d3b2a07..fbee2d6 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -21,48 +21,42 @@ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER * DEALINGS IN THE SOFTWARE. */ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> + #include "bus.h" + /** * Statement wrapper that goes to `fail` on failure */ #define t(inst) if ((inst) == -1) goto fail -char *argv0; -static const char *command; - - -static int -get_keys(void) -{ - int saved_errno; - char *line; - size_t len; - line = NULL, len = 0; - t(getline(&line, &len, stdin)); - t(key_sem = (key_t)atoll(line)); - free(line); - - line = NULL, len = 0; - t(getline(&line, &len, stdin)); - t(key_shm = (key_t)atoll(line)); - free(line); +/** + * The name of the process + */ +char *argv0; - return 0; +/** + * The command to spawn when a message is received + */ +static const char *command; -fail: - saved_errno = errno; - free(line); - errno = saved_errno; - return -1; -} +/** + * Spawn a command because a message has been received + * + * @param message The received message + * @param user_data Not used + * @return 1 (continue listening) on success, -1 on error + */ static int -spawn_continue(const char *message) +spawn_continue(const char *message, void *user_data) { pid_t pid = fork(); if (pid) @@ -71,11 +65,19 @@ spawn_continue(const char *message) execlp("sh", "sh", "-c", command, NULL); perror(argv0); exit(1); + (void) user_data; } +/** + * Spawn a command because a message has been received + * + * @param message The received message + * @param user_data Not used + * @return 0 (stop listening) on success, -1 on error + */ static int -spawn_break(const char *message) +spawn_break(const char *message, void *user_data) { pid_t pid = fork(); if (pid) @@ -84,9 +86,24 @@ spawn_break(const char *message) execlp("sh", "sh", "-c", command, NULL); perror(argv0); exit(1); + (void) user_data; } + +/** + * Main function of the command line interface for the bus system + * + * @param argc The number of elements in `argv` + * @param argv The command. Valid commands: + * <argv0> create [<path>] # create a bus + * <argv0> remove <path> # remove a bus + * <argv0> listen <path> <command> # listen for new messages + * <argv0> wait <path> <command> # listen for one new message + * <argv0> broadcast <path> <message> # broadcast a message + * <command> will be spawned with $arg set to the message + * @return 0 on sucess, 1 on error, 2 on invalid command + */ int main(int argc, char *argv[]) { @@ -94,32 +111,38 @@ main(int argc, char *argv[]) argv0 = *argv; + /* Create a new bus with selected name. */ if ((argc == 3) && !strcmp(argv[1], "create")) { t(bus_create(argv[2], 0) ? 0 : -1); + /* Create a new bus with random name. */ } else if ((argc == 2) && !strcmp(argv[1], "create")) { - char *file = bus_create(NULL, 0); + const char *file = bus_create(NULL, 0); t(file ? 0 : -1); printf("%s\n", file); + /* Remove a bus. */ } else if ((argc == 3) && !strcmp(argv[1], "remove")) { t(bus_unlink(argv[2])); + /* Listen on a bus in a loop. */ } else if ((argc == 4) && !strcmp(argv[1], "listen")) { command = argv[3]; t(bus_open(&bus, argv[2], BUS_RDONLY)); t(bus_read(&bus, spawn_continue, NULL)); t(bus_close(&bus)); + /* Listen on a bus for one message. */ } else if ((argc == 4) && !strcmp(argv[1], "wait")) { command = argv[3]; t(bus_open(&bus, argv[2], BUS_RDONLY)); t(bus_read(&bus, spawn_break, NULL)); t(bus_close(&bus)); + /* Broadcast a message on a bus. */ } else if ((argc == 4) && !strcmp(argv[1], "broadcast")) { t(bus_open(&bus, argv[2], BUS_WRONLY)); - t(bus_write(&bus, argv[3])) + t(bus_write(&bus, argv[3])); t(bus_close(&bus)); } else @@ -132,6 +155,3 @@ fail: return 1; } - -#undef t - |