abduco

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | LICENSE

commit e08d7acc3af177bd46a3ee6703f57cb66bf82ac8
parent 15ae398ea51f9adb282d7cf93a998a8ea401fd21
Author: David Phillips <dbphillipsnz@gmail.com>
Date:   Fri,  7 Aug 2015 16:58:11 +0200

Allow squashing of command line flags together

Diffstat:
abduco.c | 36+++++++++++++++++-------------------
1 file changed, 17 insertions(+), 19 deletions(-)

diff --git a/abduco.c b/abduco.c @@ -574,38 +574,28 @@ static int list_session(void) { } int main(int argc, char *argv[]) { + int opt; bool force = false; char **cmd = NULL, action = '\0'; server.name = basename(argv[0]); gethostname(server.host+1, sizeof(server.host) - 1); if (argc == 1) exit(list_session()); - for (int arg = 1; arg < argc; arg++) { - if (argv[arg][0] != '-') { - if (!server.session_name) { - server.session_name = argv[arg]; - continue; - } else if (!cmd) { - cmd = &argv[arg]; - break; - } - } - if (server.session_name) - usage(); - switch (argv[arg][1]) { + + while ((opt = getopt(argc, argv, "aAcne:frv")) != -1) { + switch (opt) { case 'a': case 'A': case 'c': case 'n': - action = argv[arg][1]; + action = opt; break; case 'e': - if (arg + 1 >= argc) + if (!optarg) usage(); - char *esc = argv[++arg]; - if (esc[0] == '^' && esc[1]) - *esc = CTRL(esc[1]); - KEY_DETACH = *esc; + if (optarg[0] == '^' && optarg[1]) + optarg[0] = CTRL(optarg[1]); + KEY_DETACH = optarg[0]; break; case 'f': force = true; @@ -621,6 +611,14 @@ int main(int argc, char *argv[]) { } } + /* collect the session name if trailing args */ + if (optind < argc) + server.session_name = argv[optind]; + + /* if yet more trailing arguments, they must be the command */ + if (optind + 1 < argc) + cmd = &argv[optind + 1]; + if (!cmd) { cmd = (char*[]){ getenv("ABDUCO_CMD"), NULL }; if (!cmd[0])