# HG changeset patch # User Olaf Wintermann # Date 1530197574 -7200 # Node ID 1e8592657a01633ee9c5e7920a76165cddf5d184 # Parent b7e415d8bcdde9e65c0c882b93c824a04d41d291 adds semi-functional bash completion diff -r b7e415d8bcdd -r 1e8592657a01 bsd.mk --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 clang.mk --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 dav/main.c --- 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;wargv[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;idata; + 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;iname); + if(space) { + size_t l = strlen(elm->path); + for(int i=0;ipath[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; +} diff -r b7e415d8bcdd -r 1e8592657a01 dav/main.h --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 dav/version.h --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 gcc.mk --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 mingw.mk --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 osx.mk --- 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 diff -r b7e415d8bcdd -r 1e8592657a01 scripts/dav-bash-completion.bash --- /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 +} + diff -r b7e415d8bcdd -r 1e8592657a01 suncc.mk --- 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