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
/********************************/
108
112
if (temp == NULL)
111
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));
113
118
if (temp == NULL)
115
120
(void)strncpy(temp, txt + 1, len - 2);
116
121
temp[len - 2] = '\0';
118
/* XXXMYSQL: use non-_r functions for now */
119
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));
120
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));
122
140
pass = getpwnam(temp);
124
free(temp); /* value no more needed */
143
el_free(temp); /* value no more needed */
125
144
if (pass == NULL)
126
return (strdup(txt));
128
147
/* update pointer txt to point at string immedially following */
129
148
/* first slash */
132
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));
133
153
if (temp == NULL)
135
(void)sprintf(temp, "%s/%s", pass->pw_dir, txt);
155
(void)snprintf(temp, len, "%s/%s", pass->pw_dir, txt);
163
nptr = realloc(filename, strlen(temp) + 1);
183
nptr = el_realloc(filename, (strlen(temp) + 1) *
164
185
if (nptr == NULL) {
169
191
(void)strcpy(filename, temp);
170
len = temp - text; /* including last slash */
171
nptr = realloc(dirname, len + 1);
192
len = (size_t)(temp - text); /* including last slash */
194
nptr = el_realloc(dirname, (len + 1) *
172
196
if (nptr == NULL) {
177
202
(void)strncpy(dirname, text, len);
178
203
dirname[len] = '\0';
243
272
len = strlen(entry->d_name);
246
temp = malloc(strlen(dirname) + len + 1);
275
len = strlen(dirname) + len + 1;
276
temp = el_malloc(len * sizeof(*temp));
247
277
if (temp == NULL)
249
(void)sprintf(temp, "%s%s", dirname, entry->d_name);
279
(void)snprintf(temp, len, "%s%s", dirname, entry->d_name);
251
281
(void)closedir(dir);
293
323
char **nmatch_list;
294
324
while (matches + 3 >= match_list_len)
295
325
match_list_len <<= 1;
296
nmatch_list = realloc(match_list,
297
match_list_len * sizeof(char *));
326
nmatch_list = el_realloc(match_list,
327
match_list_len * sizeof(*nmatch_list));
298
328
if (nmatch_list == NULL) {
302
332
match_list = nmatch_list;
350
380
* Display list of strings in columnar format on readline's output stream.
351
* 'matches' is list of strings, 'len' is number of strings in 'matches',
352
* '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].
355
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)
357
int i, idx, limit, count;
358
int screenwidth = el->el_term.t_size.h;
361
* Find out how many entries can be put on one line, count
362
* with two spaces between strings.
364
limit = screenwidth / (max + 2);
368
/* how many lines of output */
370
if (count * limit < len)
373
/* Sort the items if they are not already sorted. */
374
qsort(&matches[1], (size_t)(len - 1), sizeof(char *),
375
_fn_qsort_string_compare);
378
for(; count > 0; count--) {
379
for(i = 0; i < limit && matches[idx]; i++, idx++)
380
(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]);
382
422
(void)fprintf(el->el_outfile, "\n");
399
439
fn_complete(EditLine *el,
400
440
char *(*complet_func)(const char *, int),
401
441
char **(*attempted_completion_function)(const char *, int, int),
402
const char *word_break, const char *special_prefixes,
403
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,
404
444
int *completion_type, int *over, int *point, int *end)
407
char *temp, **matches;
446
const TYPE(LineInfo) *li;
410
451
int what_to_do = '\t';
411
452
int retval = CC_NORM;
423
464
app_func = append_char_function;
425
466
/* We now look backwards for the start of a filename/variable word */
427
ctemp = (const char *) li->cursor;
467
li = FUN(el,line)(el);
428
469
while (ctemp > li->buffer
429
&& !strchr(word_break, ctemp[-1])
430
&& (!special_prefixes || !strchr(special_prefixes, ctemp[-1]) ) )
470
&& !Strchr(word_break, ctemp[-1])
471
&& (!special_prefixes || !Strchr(special_prefixes, ctemp[-1]) ) )
433
len = li->cursor - ctemp;
474
len = (size_t)(li->cursor - ctemp);
434
475
#if defined(__SSP__) || defined(__SSP_ALL__)
435
temp = malloc(len + 1);
476
temp = el_malloc((len + 1) * sizeof(*temp));
437
temp = alloca(len + 1);
478
temp = alloca((len + 1) * sizeof(*temp));
439
(void)strncpy(temp, ctemp, len);
480
(void)Strncpy(temp, ctemp, len);
440
481
temp[len] = '\0';
442
483
/* these can be used by function called in completion_matches() */
443
484
/* or (*attempted_completion_function)() */
445
*point = li->cursor - li->buffer;
486
*point = (int)(li->cursor - li->buffer);
447
*end = li->lastchar - li->buffer;
488
*end = (int)(li->lastchar - li->buffer);
449
490
if (attempted_completion_function) {
450
int cur_off = li->cursor - li->buffer;
451
matches = (*attempted_completion_function) (temp,
452
(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);
455
497
if (!attempted_completion_function ||
456
498
(over != NULL && !*over && !matches))
457
matches = completion_matches(temp, complet_func);
499
matches = completion_matches(
500
ct_encode_string(temp, &el->el_scratch), complet_func);
459
502
if (over != NULL)
464
int matches_num, maxlen, match_len, match_display=1;
507
size_t matches_num, maxlen, match_len, match_display=1;
466
509
retval = CC_REFRESH;
493
for(i=1, maxlen=0; matches[i]; i++) {
539
for(i = 1, maxlen = 0; matches[i]; i++) {
494
540
match_len = strlen(matches[i]);
495
541
if (match_len > maxlen)
496
542
maxlen = match_len;
544
/* matches[1] through matches[i-1] are available */
545
matches_num = (size_t)(i - 1);
500
547
/* newline to get on next line from command line */
501
548
(void)fprintf(el->el_outfile, "\n");
514
561
(void)fprintf(el->el_outfile, "\n");
518
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);
520
575
retval = CC_REDISPLAY;
521
576
} else if (matches[0][0]) {
552
607
_el_fn_complete(EditLine *el, int ch __attribute__((__unused__)))
554
609
return (unsigned char)fn_complete(el, NULL, NULL,
555
break_chars, NULL, NULL, 100,
610
break_chars, NULL, NULL, (size_t)100,
556
611
NULL, NULL, NULL, NULL);