xonecas.io
7814
Ta

tiny-c-agent / files

tiny-c-agent / src / tui / complete.c

3.9 KB
1#include "complete.h"
2
3#include "util.h"
4
5#include <dirent.h>
6#include <limits.h>
7#include <stdbool.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11#include <sys/stat.h>
12
13char *tiny_common_prefix(const char *a, const char *b) {
14    size_t i = 0;
15    while (a[i] && b[i] && a[i] == b[i]) i++;
16    char *out = malloc(i + 1);
17    if (!out) abort();
18    memcpy(out, a, i);
19    out[i] = '\0';
20    return out;
21}
22
23// A word must carry path syntax to be completed: a leading `~`, `./`, `../`,
24// `.`/`..`, `/`, or a word segment followed by a slash.
25static bool path_like(const char *w) {
26    if (w[0] == '~' || w[0] == '/') return true;
27    if (w[0] == '.' && (w[1] == '\0' || w[1] == '/' ||
28                        (w[1] == '.' && (w[2] == '\0' || w[2] == '/'))))
29        return true;
30    const char *slash = strchr(w, '/');
31    if (slash && slash != w) return true;
32    return false;
33}
34
35char *tiny_complete_path(const char *word, const char *cwd) {
36    if (!path_like(word)) return NULL;
37
38    const char *slashp = strrchr(word, '/');
39    const char *prefix = slashp ? slashp + 1 : word;
40    size_t prefix_len = strlen(prefix);
41
42    // Locate the directory to list.
43    char dir[PATH_MAX];
44    const char *home = getenv("HOME");
45    if (word[0] == '~' && home) {
46        size_t n = slashp ? (size_t)(slashp - (word + 1)) : strlen(word + 1);
47        if (n >= sizeof dir) return NULL;
48        snprintf(dir, sizeof dir, "%s%.*s", home, (int)n, word + 1);
49    } else if (slashp) {
50        size_t n = (size_t)(slashp - word);
51        if (n == 0) {
52            snprintf(dir, sizeof dir, "/");
53        } else if (word[0] == '/') {
54            if (n >= sizeof dir) return NULL;
55            snprintf(dir, sizeof dir, "%.*s", (int)n, word);
56        } else {
57            if (n + strlen(cwd) + 2 >= sizeof dir) return NULL;
58            snprintf(dir, sizeof dir, "%s/%.*s", cwd, (int)n, word);
59        }
60    } else {
61        snprintf(dir, sizeof dir, "%s", cwd);
62    }
63
64    DIR *d = opendir(dir);
65    if (!d) return NULL;
66
67    // Candidates: matching names, dirs grown a trailing slash.
68    char **cands = NULL;
69    size_t n = 0, cap = 0;
70    struct dirent *ent;
71    while ((ent = readdir(d)) != NULL) {
72        const char *name = ent->d_name;
73        if (strncmp(name, prefix, prefix_len) != 0) continue;
74        if (name[0] == '.' && prefix[0] != '.') continue;
75        char path[PATH_MAX];
76        bool isdir = false;
77        if (snprintf(path, sizeof path, "%s/%s", dir, name) < (int)sizeof path) {
78            struct stat st;
79            if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) isdir = true;
80        }
81        size_t nl = strlen(name);
82        char *cand = malloc(nl + 2);
83        if (!cand) abort();
84        memcpy(cand, name, nl);
85        if (isdir) cand[nl++] = '/';
86        cand[nl] = '\0';
87        if (n == cap) {
88            cap = cap ? cap * 2 : 16;
89            cands = realloc(cands, cap * sizeof *cands);
90            if (!cands) abort();
91        }
92        cands[n++] = cand;
93    }
94    closedir(d);
95
96    char *result = NULL;
97    // The bytes before the prefix; equal to the word length minus the prefix.
98    size_t keep = strlen(word) - prefix_len;
99    if (n == 1) {
100        if (strcmp(cands[0], prefix) != 0) {
101            tui_buf b;
102            buf_init(&b);
103            buf_add(&b, word, keep);
104            buf_adds(&b, cands[0]);
105            result = b.data ? buf_steal(&b) : NULL;
106        }
107    } else if (n > 1) {
108        char *shared = strdup(cands[0]);
109        for (size_t i = 1; i < n; i++) {
110            char *next = tiny_common_prefix(shared, cands[i]);
111            free(shared);
112            shared = next;
113        }
114        if (strlen(shared) > prefix_len) {
115            tui_buf b;
116            buf_init(&b);
117            buf_add(&b, word, keep);
118            buf_adds(&b, shared);
119            result = b.data ? buf_steal(&b) : NULL;
120        }
121        free(shared);
122    }
123    for (size_t i = 0; i < n; i++) free(cands[i]);
124    free(cands);
125    return result;
126}