From 17c09be9112c874c1b72e2f43278769058016cc4 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 16 May 2015 21:14:34 +0200 Subject: add -x option to create command, and -n option to broadcast command MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/cmdline.c | 119 ++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 79 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/cmdline.c b/src/cmdline.c index 69adae2..97da729 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -292,14 +292,14 @@ parse_owner(char *str, uid_t *uid, gid_t *gid) * * @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 - * chmod # change permissions - * chown [:] # change ownership - * chgrp # change group + * create [-x] [--] [] # create a bus + * remove [--] # remove a bus + * listen [--] # listen for new messages + * wait [--] # listen for one new message + * broadcast [-n] [--] # broadcast a message + * chmod [--] # change permissions + * chown [--] [:] # change ownership + * chgrp [--] # change group * will be spawned with $arg set to the message * @return 0 on sucess, 1 on error, 2 on invalid command */ @@ -312,69 +312,108 @@ main(int argc, char *argv[]) uid_t uid; gid_t gid; mode_t mode_andnot, mode_or; + int opt_x = 0, opt_n = 0; + const char *arg; + char **nonoptv = alloca(argc * sizeof(char*)); + int nonoptc = 0; + + argv0 = *argv++; + argc--; + + /* Parse arguments. */ + while (argc) { + if (!strcmp(*argv, "--")) { + argv++; + argc--; + break; + } else if (**argv == '-') { + arg = *argv++; + argc--; + for (arg++; *arg; arg++) { + if (*arg == 'x') + opt_x = 1; + else if (*arg == 'n') + opt_n = 1; + else + return -2; + } + } else { + *nonoptv++ = *argv++; + nonoptc++; + argc--; + } + } + while (argc) { + *nonoptv++ = *argv++; + nonoptc++; + argc--; + } + nonoptv -= nonoptc; - argv0 = *argv; + /* Check options. */ + if (opt_x && strcmp(nonoptv[0], "create") && (nonoptc != 2)) + return 2; + if (opt_n && strcmp(nonoptv[0], "broadcast") && (nonoptc != 3)) + return 2; /* Create a new bus with selected name. */ - if ((argc == 3) && !strcmp(argv[1], "create")) { - t(bus_create(argv[2], 0, NULL)); - /* TODO add -x */ + if ((nonoptc == 2) && !strcmp(nonoptv[0], "create")) { + t(bus_create(nonoptv[1], opt_x * BUS_EXCL, NULL)); /* Create a new bus with random name. */ - } else if ((argc == 2) && !strcmp(argv[1], "create")) { + } else if ((nonoptc == 1) && !strcmp(nonoptv[0], "create")) { t(bus_create(NULL, 0, &file)); printf("%s\n", file); free(file); /* Remove a bus. */ - } else if ((argc == 3) && !strcmp(argv[1], "remove")) { - t(bus_unlink(argv[2])); + } else if ((nonoptc == 2) && !strcmp(nonoptv[0], "remove")) { + t(bus_unlink(nonoptv[1])); /* 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)); + } else if ((nonoptc == 3) && !strcmp(nonoptv[0], "listen")) { + command = nonoptv[2]; + t(bus_open(&bus, nonoptv[1], 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)); + } else if ((nonoptc == 3) && !strcmp(nonoptv[0], "wait")) { + command = nonoptv[2]; + t(bus_open(&bus, nonoptv[1], 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], 0)); + } else if ((nonoptc == 3) && !strcmp(nonoptv[0], "broadcast")) { + t(bus_open(&bus, nonoptv[1], BUS_WRONLY)); + t(bus_write(&bus, nonoptv[2], opt_n * BUS_NOWAIT)); t(bus_close(&bus)); - /* TODO add -n */ /* Change permissions. */ - } else if ((argc == 4) && !strcmp(argv[1], "chmod")) { - t(parse_mode(argv[2], &mode_andnot, &mode_or)); - t(stat(argv[3], &attr)); + } else if ((nonoptc == 3) && !strcmp(nonoptv[0], "chmod")) { + t(parse_mode(nonoptv[1], &mode_andnot, &mode_or)); + t(stat(nonoptv[2], &attr)); attr.st_mode &= ~mode_andnot; attr.st_mode |= mode_or; - t(bus_chmod(argv[3], attr.st_mode)); + t(bus_chmod(nonoptv[2], attr.st_mode)); /* Change ownership. */ - } else if ((argc == 4) && !strcmp(argv[1], "chown")) { - if (strchr(argv[2], ':')) { - t(parse_owner(argv[2], &uid, &gid)); - t(bus_chown(argv[3], uid, gid)); + } else if ((nonoptc == 3) && !strcmp(nonoptv[0], "chown")) { + if (strchr(nonoptv[1], ':')) { + t(parse_owner(nonoptv[1], &uid, &gid)); + t(bus_chown(nonoptv[2], uid, gid)); } else { - t(parse_owner(argv[2], &uid, NULL)); - t(stat(argv[3], &attr)); - t(bus_chown(argv[3], uid, attr.st_gid)); + t(parse_owner(nonoptv[1], &uid, NULL)); + t(stat(nonoptv[2], &attr)); + t(bus_chown(nonoptv[2], uid, attr.st_gid)); } /* Change group. */ - } else if ((argc == 4) && !strcmp(argv[1], "chgrp")) { - t(parse_owner(argv[2], NULL, &gid)); - t(stat(argv[3], &attr)); - t(bus_chown(argv[3], attr.st_uid, gid)); + } else if ((nonoptc == 3) && !strcmp(nonoptv[0], "chgrp")) { + t(parse_owner(nonoptv[1], NULL, &gid)); + t(stat(nonoptv[2], &attr)); + t(bus_chown(nonoptv[2], attr.st_uid, gid)); } else return 2; -- cgit v1.2.3-70-g09d2