From 40b2714221ab22163419308b0d0322fd3ea64c82 Mon Sep 17 00:00:00 2001 From: "Ingraham, Daniel James (GRC-LTV0)" Date: Thu, 23 Sep 2021 19:34:17 -0400 Subject: [PATCH] Add -d flag to detect if a session exists --- abduco.1 | 6 ++++++ abduco.c | 10 +++++++++- testsuite.sh | 28 ++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/abduco.1 b/abduco.1 index c122308..a828417 100644 --- a/abduco.1 +++ b/abduco.1 @@ -25,6 +25,10 @@ .Cm command Op args ... . .Nm +.Fl d +.Cm name +. +.Nm .Fl n .Op options ... .Cm name @@ -84,6 +88,8 @@ Attach to an existing session. Try to connect to an existing session, upon failure create said session and attach immediately to it. .It Fl c Create a new session and attach immediately to it. +.It Fl d +Detect if the specified session exists, returning a successful exit status if true. .It Fl n Create a new session but do not attach to it. .El diff --git a/abduco.c b/abduco.c index b56e6f4..7f0dfc3 100644 --- a/abduco.c +++ b/abduco.c @@ -606,11 +606,12 @@ int main(int argc, char *argv[]) { server.name = basename(argv[0]); gethostname(server.host+1, sizeof(server.host) - 1); - while ((opt = getopt(argc, argv, "aAclne:fpqrv")) != -1) { + while ((opt = getopt(argc, argv, "aAcdlne:fpqrv")) != -1) { switch (opt) { case 'a': case 'A': case 'c': + case 'd': case 'n': action = opt; break; @@ -712,6 +713,13 @@ int main(int argc, char *argv[]) { goto redo; } break; + case 'd': + if (session_exists(server.session_name)) { + return EXIT_SUCCESS; + } else { + return EXIT_FAILURE; + } + break; } return 0; diff --git a/testsuite.sh b/testsuite.sh index d6c73be..92594d5 100755 --- a/testsuite.sh +++ b/testsuite.sh @@ -171,6 +171,33 @@ run_test_dvtm() { fi } +run_test_detect_session() { + check_environment || return 1; + + local name="$1" + local cmd="$2" + local output="$name.out" + local output_expected="$name.expected" + + TESTS_RUN=$((TESTS_RUN + 1)) + echo -n "Running test: $name " + $cmd >/dev/null 2>&1 + expected_abduco_epilog "$name" $? > "$output_expected" 2>&1 + + if detach | $ABDUCO $ABDUCO_OPTS -c "$name" $cmd >/dev/null 2>&1 && sleep 3 && + $ABDUCO -d "$name" && + $ABDUCO -a "$name" 2>&1 | tail -1 | sed 's/.$//' > "$output" && + diff -u "$output_expected" "$output" && check_environment; then + rm "$output" "$output_expected" + TESTS_OK=$((TESTS_OK + 1)) + echo "OK" + return 0 + else + echo "FAIL" + return 1 + fi +} + test_non_existing_command || echo "Execution of non existing command FAILED" run_test_attached "awk" "awk 'BEGIN {for(i=1;i<=1000;i++) print i}'" @@ -207,6 +234,7 @@ EOT chmod +x long-running.sh run_test_attached_detached "attach-detach" "./long-running.sh" +run_test_detect_session "detect-session" "./long-running.sh" rm ./long-running.sh