-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplete.c
More file actions
366 lines (313 loc) · 10.1 KB
/
Copy pathcomplete.c
File metadata and controls
366 lines (313 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#pragma once
#include "global.h"
#include <assert.h>
#include <dirent.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#include <unistd.h>
#define COMPLETION_MAX 4096
#define COMPLETION_MAX_MATCHES 100
#define COMPLETION_NAME_LEN 1024
static char g_cache_cmds[COMPLETION_MAX][COMPLETION_NAME_LEN]; // 4MB
static int g_cache_cmd_count = 0;
static bool g_cache_ready = false;
static char g_matches[COMPLETION_MAX_MATCHES][COMPLETION_NAME_LEN]; // ~100KB
static int g_match_count = 0;
static int g_match_idx = 0;
static char g_last_prefix[COMPLETION_NAME_LEN] = "";
static bool g_completing = false;
int complete_match_count(void) {
return g_match_count;
}
int complete_match_idx(void) {
return g_match_idx;
}
bool complete_is_active(void) {
return g_completing;
}
static int complete_cmp(const void *a, const void *b) {
if (OPT_IGNORE_CASE)
return strcasecmp((const char *)a, (const char *)b);
return strcmp((const char *)a, (const char *)b);
}
static void escape_path(const char *src, char *dst, size_t dst_size) {
while (*src && dst_size > 1) {
if (*src == '\\' || *src == ' ') {
if (dst_size < 3)
break;
*dst++ = '\\';
*dst++ = *src;
dst_size -= 2;
} else {
*dst++ = *src;
dst_size--;
}
src++;
}
*dst = '\0';
}
static const char *complete_shell_name(void) {
const char *shell = getenv("SHELL");
if (!shell)
return "sh";
const char *name = strrchr(shell, '/');
return name ? name + 1 : shell;
}
void complete_cache(void) {
if (g_cache_ready)
return;
g_cache_cmd_count = 0;
const char *path_env = getenv("PATH");
if (path_env) {
char path_copy[BUF_SIZE];
strlcpy(path_copy, path_env, sizeof path_copy);
char *dir = strtok(path_copy, ":");
while (dir && g_cache_cmd_count < COMPLETION_MAX) {
struct dirent **entries = NULL;
int n = scandir(dir, &entries, NULL, alphasort);
if (n > 0) {
for (int i = 0; i < n; i++) {
if (entries[i]->d_name[0] == '.') {
free(entries[i]);
continue;
}
char full[PATH_MAX];
int sn = snprintf(full, sizeof full, "%s/%s", dir, entries[i]->d_name);
if (sn >= 0 && (size_t)sn < sizeof full && access(full, X_OK) == 0) {
if (g_cache_cmd_count < COMPLETION_MAX) {
strlcpy(g_cache_cmds[g_cache_cmd_count], entries[i]->d_name, COMPLETION_NAME_LEN);
g_cache_cmd_count++;
}
}
free(entries[i]);
}
free(entries);
}
dir = strtok(NULL, ":");
}
}
if (OPT_FULL_SHELL) {
const char *shell_name = complete_shell_name();
const char *alias_cmd = NULL;
if (strcmp(shell_name, "bash") == 0) {
alias_cmd = "bash -i -c 'compgen -a' 2>/dev/null";
} else if (strcmp(shell_name, "zsh") == 0) {
alias_cmd = "zsh -i -c 'print -l ${(k)aliases}' 2>/dev/null";
}
if (alias_cmd) {
FILE *fp = popen(alias_cmd, "r");
if (fp) {
char line[512];
while (fgets(line, sizeof line, fp) && g_cache_cmd_count < COMPLETION_MAX) {
size_t len = strlen(line);
if (len > 0 && line[len - 1] == '\n')
line[--len] = '\0';
if (len == 0)
continue;
strlcpy(g_cache_cmds[g_cache_cmd_count], line, COMPLETION_NAME_LEN);
g_cache_cmd_count++;
}
pclose(fp);
}
}
}
if (g_cache_cmd_count > 1) {
qsort(g_cache_cmds, g_cache_cmd_count, COMPLETION_NAME_LEN, complete_cmp);
int j = 0;
for (int i = 0; i < g_cache_cmd_count; i++) {
if (j == 0 || strcmp(g_cache_cmds[j - 1], g_cache_cmds[i]) != 0) {
if (i != j)
strcpy(g_cache_cmds[j], g_cache_cmds[i]);
j++;
}
}
g_cache_cmd_count = j;
}
g_cache_ready = true;
}
static bool is_escaped_space(const char *buf, const int space_pos) {
int bs = 0;
for (int i = space_pos - 1; i >= 0 && buf[i] == '\\'; i--)
bs++;
return bs % 2 == 1;
}
static void complete_get_word_bounds(const char *buf, const int cursor, int *start, int *end) {
*start = cursor;
while (*start > 0) {
if (buf[*start - 1] == ' ' && !is_escaped_space(buf, *start - 1))
break;
(*start)--;
}
*end = cursor;
while (buf[*end] != '\0') {
if (buf[*end] == ' ' && !is_escaped_space(buf, *end))
break;
(*end)++;
}
}
static void complete_word_at_cursor(const char *buf, const int cursor, char *word, const int word_size,
bool *is_first) {
int start, end;
complete_get_word_bounds(buf, cursor, &start, &end);
int len = end - start;
if (len >= word_size)
len = word_size - 1;
if (len > 0)
memcpy(word, buf + start, len);
word[len] = '\0';
*is_first = (start == 0);
}
static void complete_generate_commands(const char *prefix) {
const int prefix_len = strlen(prefix);
for (int i = 0; i < g_cache_cmd_count && g_match_count < COMPLETION_MAX_MATCHES; i++) {
if (strncasecmp(g_cache_cmds[i], prefix, prefix_len) == 0) {
strlcpy(g_matches[g_match_count], g_cache_cmds[i], COMPLETION_NAME_LEN);
g_match_count++;
}
}
}
static bool entry_is_dir(const struct dirent *entry, const char *dir_part) {
if (entry->d_type == DT_DIR)
return true;
if (entry->d_type != DT_LNK && entry->d_type != DT_UNKNOWN)
return false;
char full_path[PATH_MAX * 2];
snprintf(full_path, sizeof full_path, "%s/%s", dir_part, entry->d_name);
struct stat st;
return stat(full_path, &st) == 0 && S_ISDIR(st.st_mode);
}
static void complete_generate_paths(const char *prefix, const bool dirs_only) {
char dir_buf[PATH_MAX];
const char *dir_part;
const char *file_part;
bool use_tilde = false;
char expanded_prefix[PATH_MAX];
const char *scan_prefix = prefix;
if (prefix[0] == '~' && (prefix[1] == '/' || prefix[1] == '\0')) {
assert(g_homepath && g_homepath[0]);
if (prefix[1] == '/')
snprintf(expanded_prefix, sizeof expanded_prefix, "%s%s", g_homepath, prefix + 1);
else
snprintf(expanded_prefix, sizeof expanded_prefix, "%s/", g_homepath);
scan_prefix = expanded_prefix;
use_tilde = true;
}
const char *slash = strrchr(scan_prefix, '/');
if (slash) {
file_part = slash + 1;
size_t dir_len = slash - scan_prefix;
if (dir_len == 0) {
dir_buf[0] = '/';
dir_buf[1] = '\0';
} else {
memcpy(dir_buf, scan_prefix, dir_len);
dir_buf[dir_len] = '\0';
}
dir_part = dir_buf;
} else {
file_part = scan_prefix;
dir_part = ".";
}
struct dirent **entries = NULL;
int n = scandir(dir_part, &entries, NULL, NULL);
if (n < 0)
return;
const int file_part_len = strlen(file_part);
const bool dir_is_dot = (dir_part[0] == '.' && dir_part[1] == '\0');
for (int i = 0; i < n; i++) {
const char *name = entries[i]->d_name;
if (name[0] == '.' && file_part[0] != '.') {
free(entries[i]);
continue;
}
if (dirs_only && !entry_is_dir(entries[i], dir_part)) {
free(entries[i]);
continue;
}
if (strncasecmp(name, file_part, file_part_len) == 0 && g_match_count < COMPLETION_MAX_MATCHES) {
if (use_tilde) {
const char *rest = dir_part + g_homepath_len;
char path_buf[PATH_MAX * 2];
if (rest[0] == '\0')
snprintf(path_buf, sizeof path_buf, "~/%s", name);
else
snprintf(path_buf, sizeof path_buf, "~%s/%s", rest, name);
escape_path(path_buf, g_matches[g_match_count], COMPLETION_NAME_LEN);
} else if (dir_is_dot) {
escape_path(name, g_matches[g_match_count], COMPLETION_NAME_LEN);
} else {
char path_buf[PATH_MAX * 2];
snprintf(path_buf, sizeof path_buf, "%s/%s", dir_part, name);
escape_path(path_buf, g_matches[g_match_count], COMPLETION_NAME_LEN);
}
g_match_count++;
}
free(entries[i]);
}
free(entries);
}
static void complete_generate(const char *word, const bool is_first) {
g_match_count = 0;
if (is_first) {
complete_generate_commands(word);
} else {
bool dirs_only = strncmp(g_current_command.chars, "cd ", 3) == 0 ||
strncmp(g_current_command.chars, "pushd ", 6) == 0 ||
strncmp(g_current_command.chars, "rmdir ", 6) == 0;
complete_generate_paths(word, dirs_only);
}
if (g_match_count > 1) {
qsort(g_matches, g_match_count, COMPLETION_NAME_LEN, complete_cmp);
}
g_match_idx = g_match_count > 0 ? 0 : -1;
g_completing = g_match_count > 0;
}
static void complete_apply(const char *completion) {
int start, end;
complete_get_word_bounds(g_current_command.chars, g_current_command.cursor, &start, &end);
const int new_len = strlen(completion);
const int tail_len = strlen(g_current_command.chars) - end;
const int new_total = start + new_len + tail_len;
if (new_total >= (int)sizeof(g_current_command.chars))
return;
memmove(g_current_command.chars + start + new_len, g_current_command.chars + end, tail_len + 1);
memcpy(g_current_command.chars + start, completion, new_len);
g_current_command.len = new_total;
g_current_command.cursor = start + new_len;
}
void complete_reset(void) {
g_match_count = 0;
g_match_idx = -1;
g_last_prefix[0] = '\0';
g_completing = false;
}
bool complete_handle_tab(void) {
complete_cache();
char word[COMPLETION_NAME_LEN];
bool is_first;
complete_word_at_cursor(g_current_command.chars, g_current_command.cursor, word, sizeof word, &is_first);
if (g_completing && strcmp(word, g_last_prefix) == 0) {
g_match_idx = (g_match_idx + 1) % g_match_count;
complete_apply(g_matches[g_match_idx]);
strlcpy(g_last_prefix, g_matches[g_match_idx], sizeof g_last_prefix);
return true;
}
complete_generate(word, is_first);
if (g_match_count == 0)
return false;
complete_apply(g_matches[0]);
strlcpy(g_last_prefix, g_matches[0], sizeof g_last_prefix);
g_match_idx = 0;
return true;
}
bool complete_handle_shift_tab(void) {
if (!g_completing || g_match_count == 0)
return false;
g_match_idx = (g_match_idx - 1 + g_match_count) % g_match_count;
complete_apply(g_matches[g_match_idx]);
strlcpy(g_last_prefix, g_matches[g_match_idx], sizeof g_last_prefix);
return true;
}