From ce98d70d2b89347a4f6309f582ba03d898fee3fc Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Thu, 16 Apr 2015 05:47:40 +0200 Subject: misc MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/cmdline.c | 86 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 53 insertions(+), 33 deletions(-) (limited to 'src/cmdline.c') 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 +#include +#include + #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: + * create [] # create a bus + * remove # remove a bus + * listen # listen for new messages + * wait # listen for one new message + * broadcast # broadcast a message + * 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 - -- cgit v1.2.3-70-g09d2