From 1946d8387ad99bb46a0ff1c93040b50f202ae8f4 Mon Sep 17 00:00:00 2001 From: David Date: Mon, 22 Dec 2014 12:54:57 +0100 Subject: [PATCH] Added -C option for collection of exit status and creation of new session in one go --- abduco.1 | 13 +++++++++++++ abduco.c | 28 ++++++++++++++++++++++------ 2 files changed, 35 insertions(+), 6 deletions(-) diff --git a/abduco.1 b/abduco.1 index 9594e74..e9a7448 100644 --- a/abduco.1 +++ b/abduco.1 @@ -12,6 +12,14 @@ abduco - terminal session manager .RI [ args \ ... "" ] .br .B abduco +.RB [ \-e +.IR detachkey ] +.RB \-C +.RB name +.RB command +.RI [ args \ ... "" ] +.br +.B abduco .RB [ \-r ] .RB [ \-e .IR detachkey ] @@ -88,6 +96,11 @@ Set the key to detach which by default is set to CTRL+\\ i.e. ^\\ to detachkey. .BI \-c Create a new session and attach immediately to it. .TP +.BI \-C +Show the exit status of an already terminated session, and create a new session under the same name. +If the session does not exist, it acts like +.BI \-c +.TP .BI \-n Create a new session but do not attach to it. .TP diff --git a/abduco.c b/abduco.c index 1eeec14..a8c4919 100644 --- a/abduco.c +++ b/abduco.c @@ -210,7 +210,7 @@ static void die(const char *s) { } static void usage(void) { - fprintf(stderr, "usage: abduco [-a|-A|-c|-n] [-r] [-e detachkey] name command\n"); + fprintf(stderr, "usage: abduco [-a|-A|-c|-C|-n] [-r] [-e detachkey] name command\n"); exit(EXIT_FAILURE); } @@ -391,7 +391,7 @@ static bool create_session(const char *name, char * const argv[]) { return true; } -static bool attach_session(const char *name) { +static bool attach_session(const char *name, const bool terminate) { if (server.socket > 0) close(server.socket); if (!set_socket_name(&sockaddr, name) || (server.socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) @@ -430,7 +430,8 @@ static bool attach_session(const char *name) { info("exited due to I/O errors"); } else { info("session terminated with exit status %d", status); - exit(status); + if (terminate) + exit(status); } return true; @@ -500,6 +501,7 @@ int main(int argc, char *argv[]) { case 'a': case 'A': case 'c': + case 'C': case 'n': action = argv[arg][1]; break; @@ -528,7 +530,8 @@ int main(int argc, char *argv[]) { cmd[0] = "dvtm"; } - if (!action || !server.session_name || ((action == 'c' || action == 'A') && client.readonly)) + if (!action || !server.session_name || + ((action == 'c' || action == 'C' || action == 'A') && client.readonly)) usage(); if (tcgetattr(STDIN_FILENO, &orig_term) != -1) { @@ -543,8 +546,21 @@ int main(int argc, char *argv[]) { server.read_pty = (action == 'n'); - switch (action) { redo: + switch (action) { + case 'C': + if (set_socket_name(&sockaddr, server.session_name)) { + struct stat sb; + if (stat(sockaddr.sun_path, &sb) == 0 && S_ISSOCK(sb.st_mode)) { + if (sb.st_mode & S_IXGRP) { + /* Attach session in order to print old exit status */ + attach_session(server.session_name, false); + } else { + info("session not yet terminated"); + return 1; + } + } + } case 'n': case 'c': if (!create_session(server.session_name, cmd)) @@ -553,7 +569,7 @@ int main(int argc, char *argv[]) { break; case 'a': case 'A': - if (!attach_session(server.session_name)) { + if (!attach_session(server.session_name, true)) { if (action == 'A') { action = 'c'; goto redo; -- 2.11.4.GIT