78
78
#include "histedit.h"
79
79
#include "filecomplete.h"
81
static char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@', '$',
82
'>', '<', '=', ';', '|', '&', '{', '(', '\0' };
81
static const Char break_chars[] = { ' ', '\t', '\n', '"', '\\', '\'', '`', '@',
82
'$', '>', '<', '=', ';', '|', '&', '{', '(', '\0' };
85
85
/********************************/
109
112
if (temp == NULL)
112
len = temp - txt + 1; /* text until string after slash */
115
/* text until string after slash */
116
len = (size_t)(temp - txt + 1);
117
temp = el_malloc(len * sizeof(*temp));
114
118
if (temp == NULL)
116
120
(void)strncpy(temp, txt + 1, len - 2);
117
121
temp[len - 2] = '\0';
119
/* XXXMYSQL: use non-_r functions for now */
120
123
if (temp[0] == 0) {
124
#ifdef HAVE_GETPW_R_POSIX
125
if (getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf),
128
#elif HAVE_GETPW_R_DRAFT
129
pass = getpwuid_r(getuid(), &pwres, pwbuf, sizeof(pwbuf));
121
131
pass = getpwuid(getuid());
134
#ifdef HAVE_GETPW_R_POSIX
135
if (getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf), &pass) != 0)
137
#elif HAVE_GETPW_R_DRAFT
138
pass = getpwnam_r(temp, &pwres, pwbuf, sizeof(pwbuf));
123
140
pass = getpwnam(temp);
125
free(temp); /* value no more needed */
143
el_free(temp); /* value no more needed */
126
144
if (pass == NULL)
127
return (strdup(txt));
129
147
/* update pointer txt to point at string immedially following */
130
148
/* first slash */
133
temp = malloc(strlen(pass->pw_dir) + 1 + strlen(txt) + 1);
151
len = strlen(pass->pw_dir) + 1 + strlen(txt) + 1;
152
temp = el_malloc(len * sizeof(*temp));
134
153
if (temp == NULL)
136
(void)sprintf(temp, "%s/%s", pass->pw_dir, txt);
155
(void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
164
nptr = realloc(filename, strlen(temp) + 1);
183
nptr = el_realloc(filename, (strlen(temp) + 1) *
165
185
if (nptr == NULL) {
170
191
(void)strcpy(filename, temp);
171
len = temp - text; /* including last slash */
172
nptr = realloc(dirname, len + 1);
192
len = (size_t)(temp - text); /* including last slash */
194
nptr = el_realloc(dirname, (len + 1) *
173
196
if (nptr == NULL) {
178
202
(void)strncpy(dirname, text, len);
179
203
dirname[len] = '\0';
244
272
len = strlen(entry->d_name);
247
temp = malloc(strlen(dirname) + len + 1);
275
len = strlen(dirname) + len + 1;
276
temp = el_malloc(len * sizeof(*temp));
248
277
if (temp == NULL)
250
(void)sprintf(temp, "%s%s", dirname, entry->d_name);
279
(void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
252
281
(void)closedir(dir);
294
323
char **nmatch_list;
295
324
while (matches + 3 >= match_list_len)
296
325
match_list_len <<= 1;
297
nmatch_list = realloc(match_list,
298
match_list_len * sizeof(char *));
326
nmatch_list = el_realloc(match_list,
327
match_list_len * sizeof(*nmatch_list));
299
328
if (nmatch_list == NULL) {
303
332
match_list = nmatch_list;
351
380
* Display list of strings in columnar format on readline's output stream.
352
* 'matches' is list of strings, 'len' is number of strings in 'matches',
353
* 'max' is maximum length of string in 'matches'.
381
* 'matches' is list of strings, 'num' is number of strings in 'matches',
382
* 'width' is maximum length of string in 'matches'.
384
* matches[0] is not one of the match strings, but it is counted in
385
* num, so the strings are matches[1] *through* matches[num-1].
356
fn_display_match_list (EditLine *el, char **matches, int len, int max)
388
fn_display_match_list (EditLine *el, char **matches, size_t num, size_t width)
358
int i, idx, limit, count;
359
int screenwidth = el->el_term.t_size.h;
362
* Find out how many entries can be put on one line, count
363
* with two spaces between strings.
365
limit = screenwidth / (max + 2);
369
/* how many lines of output */
371
if (count * limit < len)
374
/* Sort the items if they are not already sorted. */
375
qsort(&matches[1], (size_t)(len - 1), sizeof(char *),
376
_fn_qsort_string_compare);
379
for(; count > 0; count--) {
380
for(i = 0; i < limit && matches[idx]; i++, idx++)
381
(void)fprintf(el->el_outfile, "%-*s ", max,
390
size_t line, lines, col, cols, thisguy;
391
int screenwidth = el->el_terminal.t_size.h;
393
/* Ignore matches[0]. Avoid 1-based array logic below. */
398
* Find out how many entries can be put on one line; count
399
* with one space between strings the same way it's printed.
401
cols = (size_t)screenwidth / (width + 1);
405
/* how many lines of output, rounded up */
406
lines = (num + cols - 1) / cols;
408
/* Sort the items. */
409
qsort(matches, num, sizeof(char *), _fn_qsort_string_compare);
412
* On the ith line print elements i, i+lines, i+lines*2, etc.
414
for (line = 0; line < lines; line++) {
415
for (col = 0; col < cols; col++) {
416
thisguy = line + col * lines;
419
(void)fprintf(el->el_outfile, "%s%-*s",
420
col == 0 ? "" : " ", (int)width, matches[thisguy]);
383
422
(void)fprintf(el->el_outfile, "\n");
400
439
fn_complete(EditLine *el,
401
440
char *(*complet_func)(const char *, int),
402
441
char **(*attempted_completion_function)(const char *, int, int),
403
const char *word_break, const char *special_prefixes,
404
const char *(*app_func)(const char *), int query_items,
442
const Char *word_break, const Char *special_prefixes,
443
const char *(*app_func)(const char *), size_t query_items,
405
444
int *completion_type, int *over, int *point, int *end)
408
char *temp, **matches;
446
const TYPE(LineInfo) *li;
411
451
int what_to_do = '\t';
412
452
int retval = CC_NORM;
424
464
app_func = append_char_function;
426
466
/* We now look backwards for the start of a filename/variable word */
428
ctemp = (const char *) li->cursor;
467
li = FUN(el,line)(el);
429
469
while (ctemp > li->buffer
430
&& !strchr(word_break, ctemp[-1])
431
&& (!special_prefixes || !strchr(special_prefixes, ctemp[-1]) ) )
470
&& !Strchr(word_break, ctemp[-1])
471
&& (!special_prefixes || !Strchr(special_prefixes, ctemp[-1]) ) )
434
len = li->cursor - ctemp;
474
len = (size_t)(li->cursor - ctemp);
435
475
#if defined(__SSP__) || defined(__SSP_ALL__)
436
temp = malloc(len + 1);
476
temp = el_malloc((len + 1) * sizeof(*temp));
438
temp = alloca(len + 1);
478
temp = alloca((len + 1) * sizeof(*temp));
440
(void)strncpy(temp, ctemp, len);
480
(void)Strncpy(temp, ctemp, len);
441
481
temp[len] = '\0';
443
483
/* these can be used by function called in completion_matches() */
444
484
/* or (*attempted_completion_function)() */
446
*point = li->cursor - li->buffer;
486
*point = (int)(li->cursor - li->buffer);
448
*end = li->lastchar - li->buffer;
488
*end = (int)(li->lastchar - li->buffer);
450
490
if (attempted_completion_function) {
451
int cur_off = li->cursor - li->buffer;
452
matches = (*attempted_completion_function) (temp,
453
(int)(cur_off - len), cur_off);
491
int cur_off = (int)(li->cursor - li->buffer);
492
matches = (*attempted_completion_function)(
493
ct_encode_string(temp, &el->el_scratch),
494
cur_off - (int)len, cur_off);
456
497
if (!attempted_completion_function ||
457
498
(over != NULL && !*over && !matches))
458
matches = completion_matches(temp, complet_func);
499
matches = completion_matches(
500
ct_encode_string(temp, &el->el_scratch), complet_func);
460
502
if (over != NULL)
465
int matches_num, maxlen, match_len, match_display=1;
507
size_t matches_num, maxlen, match_len, match_display=1;
467
509
retval = CC_REFRESH;
494
for(i=1, maxlen=0; matches[i]; i++) {
539
for(i = 1, maxlen = 0; matches[i]; i++) {
495
540
match_len = strlen(matches[i]);
496
541
if (match_len > maxlen)
497
542
maxlen = match_len;
544
/* matches[1] through matches[i-1] are available */
545
matches_num = (size_t)(i - 1);
501
547
/* newline to get on next line from command line */
502
548
(void)fprintf(el->el_outfile, "\n");
515
561
(void)fprintf(el->el_outfile, "\n");
519
fn_display_match_list(el, matches, matches_num,
566
* Interface of this function requires the
567
* strings be matches[1..num-1] for compat.
568
* We have matches_num strings not counting
569
* the prefix in matches[0], so we need to
570
* add 1 to matches_num for the call.
572
fn_display_match_list(el, matches,
573
matches_num+1, maxlen);
521
575
retval = CC_REDISPLAY;
522
576
} else if (matches[0][0]) {
553
607
_el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
555
609
return (unsigned char)fn_complete(el, NULL, NULL,
556
break_chars, NULL, NULL, 100,
610
break_chars, NULL, NULL, (size_t)100,
557
611
NULL, NULL, NULL, NULL);