Repository navigation
Expand file tree
/
Copy pathcomplete.c
More file actions
354 lines (322 loc) · 6.48 KB
/
Copy pathcomplete.c
File metadata and controls
354 lines (322 loc) · 6.48 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
/*
complete.c
Created: July 1995 by Philip Homburg <philip@cs.vu.nl>
*/
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "myhistedit.h"
#include "shell.h"
#include "complete.h"
#include "error.h"
#include "expand.h"
#include "nodes.h"
#include "memalloc.h"
static char **getlist(EditLine *el, int *baselen, int *isdir);
static char **getlist_tilde(char *prefix);
static int vstrcmp(const void *v1, const void *v2);
static void print_list(char **list);
static int install_extra(EditLine *el, char **list, int baselen, int isdir);
unsigned char complete(EditLine *el, int ch)
{
struct stackmark mark;
const LineInfo *lf;
char **list;
int baselen, prefix, isdir;
/* Direct the cursor the the end of the word. */
for(;;)
{
lf = el_line(el);
if (lf->cursor < lf->lastchar &&
!isspace((unsigned char)*lf->cursor))
{
(*(char **)&lf->cursor)++; /* XXX */
}
else
break;
}
setstackmark(&mark);
list= getlist(el, &baselen, &isdir);
if (list)
{
prefix= install_extra(el, list, baselen, isdir);
el_push(el, "i");
}
popstackmark(&mark);
if (list)
return CC_REFRESH;
else
return CC_ERROR;
}
unsigned char complete_list(EditLine *el, int ch)
{
struct stackmark mark;
char **list;
setstackmark(&mark);
list= getlist(el, NULL, NULL);
if (list)
{
print_list(list);
re_goto_bottom(el);
}
popstackmark(&mark);
if (list)
{
return CC_REFRESH;
}
else
return CC_ERROR;
}
unsigned char complete_or_list(EditLine *el, int ch)
{
struct stackmark mark;
const LineInfo *lf;
char **list;
int baselen, prefix, isdir;
setstackmark(&mark);
list= getlist(el, &baselen, &isdir);
if (list)
{
prefix= install_extra(el, list, baselen, isdir);
if (prefix == baselen)
{
print_list(list);
re_goto_bottom(el);
}
}
popstackmark(&mark);
if (list)
return CC_REFRESH;
else
return CC_ERROR;
}
unsigned char complete_expand(EditLine *el, int ch)
{
printf("complete_expand\n");
return CC_ERROR;
}
static char **getlist(EditLine *el, int *baselen, int *isdir)
{
const LineInfo *lf;
const char *begin, *end;
char *dirnam, *basenam;
union node arg;
struct arglist arglist;
DIR *dir;
struct dirent *dirent;
int i, l, n;
char *p, **list;
struct strlist *slp, *nslp;
struct stat sb;
lf = el_line(el);
/* Try to find to begin and end of the word that we have to comple. */
begin= lf->cursor;
while (begin > lf->buffer && !isspace((unsigned char)begin[-1]))
begin--;
end= lf->cursor;
while (end < lf->lastchar && !isspace((unsigned char)end[0]))
end++;
*(const char **)&lf->cursor= end; /* XXX */
/* Copy the word to a string */
dirnam= stalloc(end-begin+1);
strncpy(dirnam, begin, end-begin);
dirnam[end-begin]= '\0';
/* Cut the word in two pieces: a path and a (partial) component. */
basenam= strrchr(dirnam, '/');
if (basenam)
{
basenam++;
p= stalloc(strlen(basenam) + 1);
strcpy(p, basenam);
*basenam= '\0';
basenam= p;
}
else
{
if (dirnam[0] == '~')
return getlist_tilde(dirnam);
basenam= dirnam;
dirnam= "./";
}
if (baselen)
*baselen= strlen(basenam);
arg.type= NARG;
arg.narg.next= NULL;
arg.narg.text= dirnam;
arg.narg.backquote= NULL;
arglist.list= NULL;
arglist.lastp= &arglist.list;
expandarg(&arg, &arglist, EXP_TILDE);
INTOFF;
list= NULL;
dir= opendir(arglist.list->text);
if (dir)
{
slp= NULL;
n= 0;
l= strlen(basenam);
while(dirent= readdir(dir))
{
if (strncmp(dirent->d_name, basenam, l) != 0)
continue;
if (l == 0 && dirent->d_name[0] == '.')
continue;
nslp= stalloc(sizeof(*nslp));
nslp->next= slp;
slp= nslp;
slp->text= stalloc(strlen(dirent->d_name)+1);
strcpy(slp->text, dirent->d_name);
n++;
if (n == 1 && isdir != NULL)
{
/* Try to findout whether this entry is a
* file or a directory.
*/
p= stalloc(strlen(arglist.list->text) +
strlen(dirent->d_name) + 1);
strcpy(p, arglist.list->text);
strcat(p, dirent->d_name);
if (stat(p, &sb) == -1)
printf("stat '%s' failed: %s\n",
p, strerror(errno));
if (stat(p, &sb) == 0 && S_ISDIR(sb.st_mode))
*isdir= 1;
else
*isdir= 0;
}
}
closedir(dir);
if (n != 0)
{
list= stalloc((n+1)*sizeof(*list));
for(i= 0; slp; i++, slp= slp->next)
list[i]= slp->text;
if (i != n)
error("complete'make_list: i != n");
list[i]= NULL;
qsort(list, n, sizeof(*list), vstrcmp);
}
}
INTON;
return list;
}
static char **getlist_tilde(char *prefix)
{
printf("should ~-complete '%s'\n", prefix);
return NULL;
}
static int vstrcmp(const void *v1, const void *v2)
{
return strcmp(*(char **)v1, *(char **)v2);
}
#define MAXCOLS 40
#define SEPWIDTH 4
static void print_list(char **list)
{
struct
{
int cols;
int start[MAXCOLS+1];
int width[MAXCOLS];
} best, next;
int e, i, j, l, n, o, cols, maxw, width;
int linewidth= 80;
/* Count the number of entries. */
for (n= 0; list[n]; n++)
; /* do nothing */
if (n == 0)
error("complete'print_list: n= 0");
/* Try to maximize the number of columns */
for (cols= 1; cols<= MAXCOLS; cols++)
{
next.cols= cols;
o= 0;
width= 0;
for(j= 0; j<cols; j++)
{
next.start[j]= o;
/* Number of entries in this column. */
e= (n-o)/(cols-j);
if ((n-o)%(cols-j))
e++;
maxw= 0;
for (i= 0; i<e; i++)
{
l= strlen(list[o+i]);
if (l < 6)
l= 6;
l += SEPWIDTH;
if (l > maxw)
maxw= l;
}
next.width[j]= maxw;
width += maxw;
o += e;
}
next.start[j]= o;
if (cols > 1 && width-SEPWIDTH>linewidth)
break;
best= next;
}
cols= best.cols;
e= best.start[1];
printf("\n");
for(i= 0; i<e; i++)
{
for (j= 0; j<cols; j++)
{
if (best.start[j]+i == best.start[j+1])
continue;
if (j < cols-1)
{
printf("%-*s", best.width[j],
list[best.start[j]+i]);
}
else
{
printf("%s", list[best.start[j]+i]);
}
}
if (i < e-1)
printf("\n");
}
fflush(stdout);
}
static int install_extra(EditLine *el, char **list, int baselen, int isdir)
{
int l;
char *p, **lp;
l= strlen(list[0]);
for (lp= &list[1]; *lp; lp++)
{
while(l>0)
{
if (strncmp(list[0], *lp, l) != 0)
l--;
else
break;
}
}
if (l > baselen || list[1] == NULL)
{
p= stalloc(l-baselen+2);
strncpy(p, list[0]+baselen, l-baselen);
if (list[1] == NULL)
{
p[l-baselen]= isdir ? '/' : ' ';
p[l-baselen+1]= '\0';
l++;
}
else
p[l-baselen]= '\0';
if (el_insertstr(el, p) == -1)
return -1;
}
return l;
}
/*
* $PchId: complete.c,v 1.2 2006/04/10 14:35:53 philip Exp $
*/