2 * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the University of California, Berkeley nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
16 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include <sys/types.h>
37 #include <ecoli_log.h>
38 #include <ecoli_malloc.h>
39 #include <ecoli_test.h>
40 #include <ecoli_strvec.h>
41 #include <ecoli_string.h>
42 #include <ecoli_node.h>
43 #include <ecoli_parsed.h>
44 #include <ecoli_completed.h>
45 #include <ecoli_node_file.h>
47 EC_LOG_TYPE_REGISTER(node_file);
54 ec_node_file_parse(const struct ec_node *gen_node,
55 struct ec_parsed *state,
56 const struct ec_strvec *strvec)
61 if (ec_strvec_len(strvec) == 0)
62 return EC_PARSED_NOMATCH;
68 * Almost the same than dirname (3) and basename (3) except that:
69 * - it always returns a substring of the given path, which can
71 * - the behavior is different when the path finishes with a '/'
72 * - the path argument is not modified
73 * - the outputs are allocated and must be freed with ec_free().
75 * path dirname basename split_path
76 * /usr/lib /usr lib /usr/ lib
83 static int split_path(const char *path, char **dname_p, char **bname_p)
92 last_slash = strrchr(path, '/');
93 if (last_slash == NULL)
96 dirlen = last_slash - path + 1;
98 dname = ec_strdup(path);
101 dname[dirlen] = '\0';
103 bname = ec_strdup(path + dirlen);
116 ec_node_file_complete(const struct ec_node *gen_node,
117 struct ec_completed *completed,
118 const struct ec_strvec *strvec)
120 char *dname = NULL, *bname = NULL, *effective_dir;
121 struct ec_completed_item *item = NULL;
122 enum ec_completed_type type;
126 struct dirent *de = NULL;
128 char *comp_str = NULL;
129 char *disp_str = NULL;
134 * Example with this file tree:
145 * Input Output completions
146 * / [dir1/, dir2/, file5]
149 * /dir1/ [file1, file2, subdir/]
155 if (ec_strvec_len(strvec) != 1)
158 input = ec_strvec_val(strvec, 0);
159 ret = split_path(input, &dname, &bname);
161 ec_completed_free(completed);
166 if (strcmp(dname, "") == 0)
169 effective_dir = dname;
171 ret = lstat(effective_dir, &st);
176 if (!S_ISDIR(st.st_mode))
179 dir = opendir(effective_dir);
183 bname_len = strlen(bname);
189 if (!ec_str_startswith(de->d_name, bname))
191 if (bname[0] != '.' && de->d_name[0] == '.')
194 /* add '/' if it's a dir */
195 if (de->d_type == DT_DIR) {
197 } else if (de->d_type == DT_UNKNOWN) {
198 int dir_fd = dirfd(dir);
202 ret = fstatat(dir_fd, de->d_name, &st2, 0);
207 if (!S_ISDIR(st2.st_mode))
215 type = EC_COMP_PARTIAL;
216 if (ec_asprintf(&comp_str, "%s%s/", input,
217 &de->d_name[bname_len]) < 0) {
221 if (ec_asprintf(&disp_str, "%s/", de->d_name) < 0) {
227 if (ec_asprintf(&comp_str, "%s%s", input,
228 &de->d_name[bname_len]) < 0) {
232 if (ec_asprintf(&disp_str, "%s", de->d_name) < 0) {
237 ret = ec_completed_add_item(completed, gen_node, &item,
238 type, input, comp_str);
242 /* fix the display string: we don't want to display the full
244 ret = ec_completed_item_set_display(item, disp_str);
267 static struct ec_node_type ec_node_file_type = {
269 .parse = ec_node_file_parse,
270 .complete = ec_node_file_complete,
271 .size = sizeof(struct ec_node_file),
274 EC_NODE_TYPE_REGISTER(ec_node_file_type);
276 /* LCOV_EXCL_START */
277 static int ec_node_file_testcase(void)
279 struct ec_node *node;
282 node = ec_node("file", EC_NO_ID);
284 EC_LOG(EC_LOG_ERR, "cannot create node\n");
287 /* any string matches */
288 ret |= EC_TEST_CHECK_PARSE(node, 1, "foo");
289 ret |= EC_TEST_CHECK_PARSE(node, 1, "/tmp/bar");
290 ret |= EC_TEST_CHECK_PARSE(node, -1);
292 /* test completion */
293 ret |= EC_TEST_CHECK_COMPLETE(node,
296 ret |= EC_TEST_CHECK_COMPLETE(node,
297 "/tmp/toto/t", EC_NODE_ENDLIST,
299 ret |= EC_TEST_CHECK_COMPLETE_PARTIAL(node,
300 "/tmp/toto/t", EC_NODE_ENDLIST,
301 "/tmp/toto/titi/", EC_NODE_ENDLIST);
302 ret |= EC_TEST_CHECK_COMPLETE(node,
303 "/tmp/toto/f", EC_NODE_ENDLIST,
304 "/tmp/toto/foo", EC_NODE_ENDLIST);
305 ret |= EC_TEST_CHECK_COMPLETE(node,
306 "/tmp/toto/b", EC_NODE_ENDLIST,
307 "/tmp/toto/bar", "/tmp/toto/bar2", EC_NODE_ENDLIST);
315 static struct ec_test ec_node_file_test = {
317 .test = ec_node_file_testcase,
320 EC_TEST_REGISTER(ec_node_file_test);