I have attached two patches for discussion:

- abduco_default.diff: run a default action when no flag is given
('A', but configurable in config.h)

- abduco_force.diff: remove -C mode, in favour of a -f flag that works
with -c, -n or -A, which forces the creation of a new session when
there is an existing but already terminated session of the same name.

So, the "old":

    abduco -C session command

is replaced by:

    abduco -f -c session command

To always connect to a session, creating it if it does not exist or
was terminated, you can do:

    abduco -f session command

Both features are independent. I think the force flag is a real
improvement over -C, but the default action is just a convenience. It
can be argued that an alias solves the same problem better. At the
end, it is mostly a style decision and I don't have a strong opinion.


-- 
- yiyus || JGL .
diff --git a/abduco.1 b/abduco.1
index e9a7448..b2d2a59 100644
--- a/abduco.1
+++ b/abduco.1
@@ -6,6 +6,13 @@ abduco - terminal session manager
 .B abduco
 .RB [ \-e
 .IR detachkey ]
+.RB name
+.RB command
+.RI [ args \ ... "" ]
+.br
+.B abduco
+.RB [ \-e
+.IR detachkey ]
 .RB \-c
 .RB name
 .RB command
@@ -106,6 +113,7 @@ Create a new session but do not attach to it.
 .TP
 .BI \-A
 Try to connect to an existing session, upon failure create said session and 
attach immediately to it.
+This is the default action when no flags are specified.
 .TP
 .BI \-a
 Attach to an existing session.
diff --git a/abduco.c b/abduco.c
index 84fa382..ed1e139 100644
--- a/abduco.c
+++ b/abduco.c
@@ -477,7 +477,7 @@ static int list_session(void) {
 }
 
 int main(int argc, char *argv[]) {
-       char **cmd = NULL, action = '\0';
+       char **cmd = NULL, action = ACTION_DEFAULT;
        server.name = basename(argv[0]);
        gethostname(server.host+1, sizeof(server.host) - 1);
        if (argc == 1)
@@ -527,7 +527,7 @@ int main(int argc, char *argv[]) {
                        cmd[0] = "dvtm";
        }
 
-       if (!action || !server.session_name ||
+       if (!server.session_name ||
           ((action == 'c' || action == 'C' || action == 'A') && 
client.readonly))
                usage();
 
diff --git a/config.def.h b/config.def.h
index b52452e..7e67cbe 100644
--- a/config.def.h
+++ b/config.def.h
@@ -1,2 +1,3 @@
 static char KEY_DETACH = CTRL('\\');
 static char KEY_REDRAW = 0;
+static char ACTION_DEFAULT = 'A';
diff --git a/abduco.1 b/abduco.1
index b2d2a59..026ef77 100644
--- a/abduco.1
+++ b/abduco.1
@@ -19,14 +19,6 @@ 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 ]
@@ -100,13 +92,12 @@ Readonly session, i.e. user input is ignored.
 .BI \-e \ detachkey
 Set the key to detach which by default is set to CTRL+\\ i.e. ^\\ to detachkey.
 .TP
-.BI \-c
-Create a new session and attach immediately to it.
+.BI \-f
+Force creation of session when there is an already terminated session of the 
same name,
+after showing its exit status.
 .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
+Create a new session and attach immediately to it.
 .TP
 .BI \-n
 Create a new session but do not attach to it.
diff --git a/abduco.c b/abduco.c
index ed1e139..1d97fd4 100644
--- a/abduco.c
+++ b/abduco.c
@@ -431,7 +431,7 @@ static bool attach_session(const char *name, const bool 
terminate) {
                        exit(status);
        }
 
-       return true;
+       return terminate;
 }
 
 static int session_filter(const struct dirent *d) {
@@ -477,6 +477,7 @@ static int list_session(void) {
 }
 
 int main(int argc, char *argv[]) {
+       bool force = false;
        char **cmd = NULL, action = ACTION_DEFAULT;
        server.name = basename(argv[0]);
        gethostname(server.host+1, sizeof(server.host) - 1);
@@ -510,6 +511,9 @@ int main(int argc, char *argv[]) {
                                *esc = CTRL(esc[1]);
                        KEY_DETACH = *esc;
                        break;
+               case 'f':
+                       force = true;
+                       break;
                case 'r':
                        client.readonly = true;
                        break;
@@ -545,8 +549,9 @@ int main(int argc, char *argv[]) {
 
        redo:
        switch (action) {
-       case 'C':
-               if (set_socket_name(&sockaddr, server.session_name)) {
+       case 'n':
+       case 'c':
+               if (force && 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) {
@@ -557,17 +562,17 @@ int main(int argc, char *argv[]) {
                                        return 1;
                                }
                        }
+                       force = false;
                }
-       case 'n':
-       case 'c':
                if (!create_session(server.session_name, cmd))
                        die("create-session");
                if (action == 'n')
                        break;
        case 'a':
        case 'A':
-               if (!attach_session(server.session_name, true)) {
+               if (!attach_session(server.session_name, !force || action == 
'a')) {
                        if (action == 'A') {
+                               force = false;
                                action = 'c';
                                goto redo;
                        }

Reply via email to