153 || !strcasecmp(cmd, "repo-url")) { |
153 || !strcasecmp(cmd, "repo-url")) { |
154 ret = cmd_repository_url(args); |
154 ret = cmd_repository_url(args); |
155 } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version") |
155 } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version") |
156 || !strcasecmp(cmd, "--version")) { |
156 || !strcasecmp(cmd, "--version")) { |
157 fprintf(stderr, "dav %s\n", DAV_VERSION); |
157 fprintf(stderr, "dav %s\n", DAV_VERSION); |
|
158 } else if(!strcasecmp(cmd, "complete")) { |
|
159 ret = shell_completion(args); |
158 } else { |
160 } else { |
159 print_usage(argv[0]); |
161 print_usage(argv[0]); |
160 } |
162 } |
161 } |
163 } |
162 |
164 |
2005 } else { |
2007 } else { |
2006 fprintf(stderr, "Repository %s does not exist.\n", reponame.ptr); |
2008 fprintf(stderr, "Repository %s does not exist.\n", reponame.ptr); |
2007 return -1; |
2009 return -1; |
2008 } |
2010 } |
2009 } |
2011 } |
|
2012 |
|
2013 |
|
2014 int shell_completion(CmdArgs *args) { |
|
2015 if(args->argc < 2) { |
|
2016 return 1; |
|
2017 } |
|
2018 |
|
2019 char *index_str = args->argv[0]; |
|
2020 int64_t index = 0; |
|
2021 if(!util_strtoint(index_str, &index)) { |
|
2022 return 1; |
|
2023 } |
|
2024 |
|
2025 if(index == 1) { |
|
2026 sstr_t prefix = { NULL, 0 }; |
|
2027 if(args->argc > 2) { |
|
2028 prefix = sstr(args->argv[2]); |
|
2029 } |
|
2030 for(int i=0;;i++) { |
|
2031 char *str = cmdusageinfo[i]; |
|
2032 if(!str) { |
|
2033 break; |
|
2034 } |
|
2035 int len = (int)strlen(str); |
|
2036 int maxlen = len; |
|
2037 for(int w=0;w<len;w++) { |
|
2038 if(str[w] == ' ') { |
|
2039 maxlen = w; |
|
2040 break; |
|
2041 } |
|
2042 } |
|
2043 if(prefix.ptr) { |
|
2044 if(!sstrprefix(sstrn(str, maxlen), prefix)) { |
|
2045 continue; |
|
2046 } |
|
2047 } |
|
2048 printf("%.*s\n", (int)maxlen, str); |
|
2049 } |
|
2050 return 0; |
|
2051 } |
|
2052 |
|
2053 char *cmd = args->argv[2]; |
|
2054 if(!strcmp(cmd, "date")) { |
|
2055 return 0; |
|
2056 } |
|
2057 char *url = args->argv[3]; |
|
2058 |
|
2059 //printf("index: {%s}\n", args->argv[0]); |
|
2060 //printf("dav: {%s}\n", args->argv[1]); |
|
2061 //printf("cmd: {%s}\n", cmd); |
|
2062 //printf("url: {%s}\n", url); |
|
2063 //printf("file: {%s}\n", file); |
|
2064 |
|
2065 if(index == 2) { |
|
2066 // url completion |
|
2067 return url_completion(url); |
|
2068 } else if (index == 3) { |
|
2069 if(!strcmp(cmd, "put") || !strcmp(cmd, "import")) { |
|
2070 // file completion |
|
2071 return 12; |
|
2072 } else if(!strcmp(cmd, "copy") || !strcmp(cmd, "cp") || !strcmp(cmd, "move") || !strcmp(cmd, "mv")) { |
|
2073 // url completion |
|
2074 return url_completion(url); |
|
2075 } |
|
2076 } |
|
2077 |
|
2078 return 0; |
|
2079 } |
|
2080 |
|
2081 int url_completion(char *u) { |
|
2082 sstr_t url = sstr(u); |
|
2083 |
|
2084 // repo completion |
|
2085 int repocomp = 1; |
|
2086 for(int i=0;i<url.length;i++) { |
|
2087 if(url.ptr[i] == '/') { |
|
2088 repocomp = 0; |
|
2089 break; |
|
2090 } |
|
2091 } |
|
2092 if(repocomp) { |
|
2093 UcxList *repos = get_repositories(); |
|
2094 UCX_FOREACH(elm, repos) { |
|
2095 Repository *repo = elm->data; |
|
2096 if(sstrprefix(sstr(repo->name), url)) { |
|
2097 printf("%s/\n", repo->name); |
|
2098 } |
|
2099 |
|
2100 } |
|
2101 } else { |
|
2102 // url completion |
|
2103 |
|
2104 CmdArgs a; |
|
2105 memset(&a, 0, sizeof(CmdArgs)); |
|
2106 a.options = ucx_map_new(4); |
|
2107 ucx_map_cstr_put(a.options, "noinput", ""); |
|
2108 |
|
2109 char *path = NULL; |
|
2110 Repository *repo = url2repo(u, &path); |
|
2111 DavSession *sn = connect_to_repo(repo, &a); |
|
2112 ucx_map_free(a.options); |
|
2113 if(!sn) { |
|
2114 return 0; |
|
2115 } |
|
2116 |
|
2117 size_t plen = strlen(path); |
|
2118 |
|
2119 sstr_t filter; |
|
2120 char *lspath = NULL; |
|
2121 if(path[plen-1] == '/') { |
|
2122 lspath = strdup(path); |
|
2123 filter = S(""); |
|
2124 } else { |
|
2125 lspath = util_parent_path(path); |
|
2126 filter = sstr(util_resource_name(path)); |
|
2127 } |
|
2128 |
|
2129 DavResource *ls = dav_query(sn, "select - from %s", lspath); |
|
2130 DavResource *elm = ls->children; |
|
2131 while(elm) { |
|
2132 sstr_t name = sstr(elm->name); |
|
2133 if(sstrprefix(name, filter)) { |
|
2134 int space = 0; |
|
2135 for(int i=0;i<name.length;i++) { |
|
2136 if(name.ptr[i] == ' ') { |
|
2137 space = 1; |
|
2138 break; |
|
2139 } |
|
2140 } |
|
2141 |
|
2142 UcxBuffer *out = ucx_buffer_new(NULL, 512, UCX_BUFFER_AUTOEXTEND); |
|
2143 ucx_buffer_puts(out, repo->name); |
|
2144 if(space) { |
|
2145 size_t l = strlen(elm->path); |
|
2146 for(int i=0;i<l;i++) { |
|
2147 if(elm->path[i] == ' ') { |
|
2148 ucx_buffer_puts(out, "\\ "); |
|
2149 } else { |
|
2150 ucx_buffer_putc(out, elm->path[i]); |
|
2151 } |
|
2152 } |
|
2153 } else { |
|
2154 ucx_buffer_puts(out, elm->path); |
|
2155 } |
|
2156 if(elm->iscollection) { |
|
2157 ucx_buffer_putc(out, '/'); |
|
2158 } |
|
2159 printf("%.*s\n", (int)out->pos, out->space); |
|
2160 } |
|
2161 elm = elm->next; |
|
2162 } |
|
2163 |
|
2164 |
|
2165 } |
|
2166 |
|
2167 return 10; |
|
2168 } |