adds semi-functional bash completion

Thu, 28 Jun 2018 16:52:54 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 28 Jun 2018 16:52:54 +0200
changeset 431
1e8592657a01
parent 430
b7e415d8bcdd
child 432
a213ecaf3f4a

adds semi-functional bash completion

bsd.mk file | annotate | diff | comparison | revisions
clang.mk file | annotate | diff | comparison | revisions
dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/version.h file | annotate | diff | comparison | revisions
gcc.mk file | annotate | diff | comparison | revisions
mingw.mk file | annotate | diff | comparison | revisions
osx.mk file | annotate | diff | comparison | revisions
scripts/dav-bash-completion.bash file | annotate | diff | comparison | revisions
suncc.mk file | annotate | diff | comparison | revisions
--- a/bsd.mk	Sun Jun 24 12:44:05 2018 +0200
+++ b/bsd.mk	Thu Jun 28 16:52:54 2018 +0200
@@ -31,7 +31,7 @@
 AR = ar
 RM = rm
 
-CFLAGS  = -O2 -c -D_FILE_OFFSET_BITS=64
+CFLAGS  = -g -c -D_FILE_OFFSET_BITS=64
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- a/clang.mk	Sun Jun 24 12:44:05 2018 +0200
+++ b/clang.mk	Thu Jun 28 16:52:54 2018 +0200
@@ -31,7 +31,7 @@
 AR = ar
 RM = rm
 
-CFLAGS  = -O2 -c -D_FILE_OFFSET_BITS=64
+CFLAGS  = -g -c -D_FILE_OFFSET_BITS=64
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- a/dav/main.c	Sun Jun 24 12:44:05 2018 +0200
+++ b/dav/main.c	Thu Jun 28 16:52:54 2018 +0200
@@ -75,7 +75,7 @@
         print_usage(argv[0]);
         return -1;
     }
-    
+       
     char *cmd = argv[1];
     CmdArgs *args = cmd_parse_args(argc - 2, argv + 2);
     if(!args) {
@@ -155,6 +155,8 @@
         } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version")
                 || !strcasecmp(cmd, "--version")) {
             fprintf(stderr, "dav %s\n", DAV_VERSION);
+        } else if(!strcasecmp(cmd, "complete")) {
+            ret = shell_completion(args);
         } else {
             print_usage(argv[0]);
         }
@@ -2007,3 +2009,160 @@
         return -1;
     }
 }
+
+
+int shell_completion(CmdArgs *args) {
+    if(args->argc < 2) {
+        return 1;
+    }
+    
+    char *index_str = args->argv[0];
+    int64_t index = 0;
+    if(!util_strtoint(index_str, &index)) {
+        return 1;
+    }
+    
+    if(index == 1) {
+        sstr_t prefix = { NULL, 0 };
+        if(args->argc > 2) {
+            prefix = sstr(args->argv[2]);
+        }
+        for(int i=0;;i++) {
+            char *str = cmdusageinfo[i];
+            if(!str) {
+                break;
+            }
+            int len = (int)strlen(str);
+            int maxlen = len;
+            for(int w=0;w<len;w++) {
+                if(str[w] == ' ') {
+                    maxlen = w;
+                    break;
+                }
+            }
+            if(prefix.ptr) {
+                if(!sstrprefix(sstrn(str, maxlen), prefix)) {
+                    continue;
+                }
+            }
+            printf("%.*s\n", (int)maxlen, str);
+        }
+        return 0;
+    }
+    
+    char *cmd = args->argv[2];
+    if(!strcmp(cmd, "date")) {
+        return 0;
+    }
+    char *url = args->argv[3];
+    
+    //printf("index: {%s}\n", args->argv[0]);
+    //printf("dav: {%s}\n", args->argv[1]);
+    //printf("cmd: {%s}\n", cmd);
+    //printf("url: {%s}\n", url);
+    //printf("file: {%s}\n", file);
+    
+    if(index == 2) {
+        // url completion
+        return url_completion(url);
+    } else if (index == 3) {
+        if(!strcmp(cmd, "put") || !strcmp(cmd, "import")) {
+            // file completion
+            return 12;            
+        } else if(!strcmp(cmd, "copy") || !strcmp(cmd, "cp") || !strcmp(cmd, "move") || !strcmp(cmd, "mv")) {
+            // url completion
+            return url_completion(url);
+        }
+    }
+    
+    return 0;
+}
+
+int url_completion(char *u) {
+    sstr_t url = sstr(u);
+    
+    // repo completion
+    int repocomp = 1;
+    for(int i=0;i<url.length;i++) {
+        if(url.ptr[i] == '/') {
+            repocomp = 0;
+            break;
+        }
+    }
+    if(repocomp) {
+        UcxList *repos = get_repositories();
+        UCX_FOREACH(elm, repos) {
+            Repository *repo = elm->data;
+            if(sstrprefix(sstr(repo->name), url)) {
+                printf("%s/\n", repo->name);
+            }
+            
+        }
+    } else {
+        // url completion
+        
+        CmdArgs a;
+        memset(&a, 0, sizeof(CmdArgs));
+        a.options = ucx_map_new(4);
+        ucx_map_cstr_put(a.options, "noinput", "");
+        
+        char *path = NULL;
+        Repository *repo = url2repo(u, &path);
+        DavSession *sn = connect_to_repo(repo, &a);
+        ucx_map_free(a.options);
+        if(!sn) {
+            return 0;
+        }
+        
+        size_t plen = strlen(path);
+        
+        sstr_t filter;
+        char *lspath = NULL;
+        if(path[plen-1] == '/') {
+            lspath = strdup(path);
+            filter = S("");
+        } else {
+            lspath = util_parent_path(path);
+            filter = sstr(util_resource_name(path));
+        }
+        
+        DavResource *ls = dav_query(sn, "select - from %s", lspath);
+        DavResource *elm = ls->children;
+        while(elm) {
+            sstr_t name = sstr(elm->name); 
+            if(sstrprefix(name, filter)) {
+                int space = 0;
+                for(int i=0;i<name.length;i++) {
+                    if(name.ptr[i] == ' ') {
+                        space = 1;
+                        break;
+                    }
+                }
+                
+                UcxBuffer *out = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND);
+                ucx_buffer_puts(out, repo->name);
+                if(space) {
+                    size_t l = strlen(elm->path);
+                    for(int i=0;i<l;i++) {
+                        if(elm->path[i] == ' ') {
+                            ucx_buffer_puts(out, "\\ ");
+                        } else {
+                            ucx_buffer_putc(out, elm->path[i]);
+                        }
+                    }
+                } else {
+                    ucx_buffer_puts(out, elm->path);
+                }
+                if(elm->iscollection) {
+                    ucx_buffer_putc(out, '/');
+                }
+                printf("%.*s\n", (int)out->pos, out->space);
+            }
+            elm = elm->next;
+        }
+        
+        
+    }
+    
+    return 10;
+}
--- a/dav/main.h	Sun Jun 24 12:44:05 2018 +0200
+++ b/dav/main.h	Thu Jun 28 16:52:54 2018 +0200
@@ -104,6 +104,10 @@
 
 int cmd_repository_url(CmdArgs *args);
 
+int shell_completion(CmdArgs *args);
+
+int url_completion(char *url);
+
 #ifdef	__cplusplus
 }
 #endif
--- a/dav/version.h	Sun Jun 24 12:44:05 2018 +0200
+++ b/dav/version.h	Thu Jun 28 16:52:54 2018 +0200
@@ -27,6 +27,6 @@
  */
 
 #ifndef DAV_VERSION
-#define DAV_VERSION "1.2"
+#define DAV_VERSION "1.3 dev"
 #endif
 
--- a/gcc.mk	Sun Jun 24 12:44:05 2018 +0200
+++ b/gcc.mk	Thu Jun 28 16:52:54 2018 +0200
@@ -31,7 +31,7 @@
 AR = ar
 RM = rm
 
-CFLAGS  = -std=gnu99 -O2 -c -D_FILE_OFFSET_BITS=64
+CFLAGS  = -std=gnu99 -g -c -D_FILE_OFFSET_BITS=64
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- a/mingw.mk	Sun Jun 24 12:44:05 2018 +0200
+++ b/mingw.mk	Thu Jun 28 16:52:54 2018 +0200
@@ -31,7 +31,7 @@
 AR = ar
 RM = rm
 
-CFLAGS  = -std=gnu99 -O2 -c 
+CFLAGS  = -std=gnu99 -g -c 
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- a/osx.mk	Sun Jun 24 12:44:05 2018 +0200
+++ b/osx.mk	Thu Jun 28 16:52:54 2018 +0200
@@ -31,7 +31,7 @@
 AR = ar
 RM = rm
 
-CFLAGS  = -O2 -c -Wno-deprecated -Wno-format
+CFLAGS  = -g -c -Wno-deprecated -Wno-format
 COFLAGS = -o
 LDFLAGS = 
 LOFLAGS = -o
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scripts/dav-bash-completion.bash	Thu Jun 28 16:52:54 2018 +0200
@@ -0,0 +1,18 @@
+dav_completion() {
+	OUT=$( /export/home/olaf/Projekte/dav/build/dav complete $COMP_CWORD "${COMP_WORDS[@]}" )
+	CMD_RES=$?
+	if [ $CMD_RES == 10 ]; then
+		compopt -o nospace
+	fi
+	if [ $CMD_RES == 12 ]; then
+		compopt -o default
+		COMPREPLY=()
+	else
+		TMP_IFS=$IFS
+		IFS='
+		'
+		COMPREPLY=( $OUT )
+		IFS=$TMP_IFS
+	fi
+}
+
--- a/suncc.mk	Sun Jun 24 12:44:05 2018 +0200
+++ b/suncc.mk	Thu Jun 28 16:52:54 2018 +0200
@@ -31,7 +31,7 @@
 AR = ar
 RM = rm
 
-CFLAGS  =  -O2 -c -xc99 -D_FILE_OFFSET_BITS=64
+CFLAGS  =  -g -c -xc99 -D_FILE_OFFSET_BITS=64
 COFLAGS = -o
 LDFLAGS = -lmd -lm
 LOFLAGS = -o

mercurial