File: completion.c

package info (click to toggle)
yafc 1.1.1.dfsg.1-4
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 3,248 kB
  • ctags: 1,679
  • sloc: ansic: 19,338; sh: 10,365; makefile: 155; perl: 38; ruby: 33
file content (446 lines) | stat: -rw-r--r-- 10,001 bytes parent folder | download | duplicates (4)
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
/* $Id: completion.c,v 1.10 2003/07/12 10:25:41 mhe Exp $
 *
 * completion.c -- readline completion functions
 *
 * Yet Another FTP Client
 * Copyright (C) 1998-2001, Martin Hedenfalk <mhe@stacken.kth.se>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. See COPYING for more details.
 */

#include "syshdr.h"
#include "strq.h"
#include "alias.h"
#include "commands.h"
#include "input.h"
#include "gvars.h"
#include "set.h"
#include "cmd.h"
#include "url.h"
#include "bashline.h"

cpl_t force_completion_type = cpUnset;

#ifdef HAVE_LIBREADLINE

static bool remote_dir_only = false;

char *no_completion_function(const char *text, int state)
{
	return 0;
}

static char *alias_completion_function(const char *text, int state)
{
    static int len;
	alias *ap;
	static listitem *lip = 0;

	if(!gvAliases)
		return 0;

    if(!state) {
		len = strlen(text);
		lip = gvAliases->first;
	}

    while(lip) {
		ap = (alias *)lip->data;
		lip = lip->next;
		if(strncmp(ap->name, text, len) == 0)
			return xstrdup(ap->name);
    }
    return 0;
}

static char *bookmark_complete_internal(const char *text, int state,
										bool skip_domains)
{
    static int len;
	url_t *url;
	static listitem *lip = 0;

	if(!gvBookmarks)
		return 0;

    if(!state) {
		len = strlen(text);
		lip = gvBookmarks->first;
	}

    while(lip) {
		url = (url_t *)lip->data;
		lip = lip->next;
		if(skip_domains && url->hostname[0] == '.')
			continue;
		if(url->alias) {
			if(strncmp(url->alias, text, len) == 0)
				return xstrdup(url->alias);
		} else if(strncmp(url->hostname, text, len) == 0)
				return xstrdup(url->hostname);
    }
    return 0;
}

static char *bookmark_completion_function(const char *text, int state)
{
	return bookmark_complete_internal(text, state, false);
}


static char *hostname_completion_function(const char *text, int state)
{
	return bookmark_complete_internal(text, state, true);
}

/* Generator function for command completion.  STATE lets us know whether
 * to start from scratch; without any state (i.e. STATE == 0), then we
 * start at the top of the list.
 */
static char *command_completion_function(const char *text, int state)
{
    static int list_index, len;
	static int alias_state;
    char *e;

    if(!state) {
		list_index = 0;
		len = strlen(text);
		alias_state = 0;
    }

    /* Return the next name which partially matches from the command list. */
    while((e = cmds[list_index].cmd) != 0) {
		list_index++;
		if(strncasecmp(e, text, len) == 0)
			return xstrdup(e);
    }
	e = alias_completion_function(text, alias_state);
	alias_state = 1;
	return e;
}

static char *remote_completion_function(const char *text, int state)
{
    static int len;            /* length of unquoted */
	static char *dir;          /* any initial directory in text */
    const char *name;
	static char *unquoted = 0; /* the unquoted filename (or beginning of it) */
	rfile *fp = 0;
	static listitem *lip = 0;
	static rdirectory *rdir = 0; /* the cached remote directory */
	static char *merge_fmt = "%s/%s";

	if(!ftp_loggedin())
		return 0;

	/* this is not really true, this is for local filename completion,
	 * but it works here too (sort of), and it looks nicer, since
	 * the whole path is not printed by readline, ie
	 * only foo is printed and not /bar/fu/foo (if cwd == /bar/fu)
	 * readline appends a class character (ie /,@,*) in _local_ filenames
	 */
	rl_filename_completion_desired = 1;
#if (HAVE_LIBREADLINE >= 210)
	rl_filename_quoting_desired = 1;
#endif

	if(!state) {
		bool dir_is_cached;
		char *ap;

		dir = base_dir_xptr(text);
		if(dir) {
			char *e;

			stripslash(dir);
			e = strchr(dir, 0);
			if(e[-1]=='\"')
				e[-1] = 0;
			unquote(dir);
			if(strcmp(dir, "/") == 0)
				merge_fmt = "%s%s";
			else
				merge_fmt = "%s/%s";
		}
		if(gvWaitingDots) {
			rl_insert_text("..."); /* show dots while waiting, like ncftp */
			rl_redisplay();
		}
		
		ap = ftp_path_absolute(dir);
		rdir = ftp_cache_get_directory(ap);
		dir_is_cached = (rdir != 0);
		if(!rdir)
			rdir = ftp_read_directory(ap);
		free(ap);

		if(gvWaitingDots)
			rl_do_undo(); /* remove the dots */

		if(!dir_is_cached && ftp_get_verbosity() >= vbCommand)
			rl_forced_update_display();

		if(!rdir) {
			free(dir);
			return 0;
		}
		unquoted = bash_dequote_filename(base_name_ptr(text), 0);
		if(!unquoted)
			unquoted = (char *)xmalloc(1);
		len = strlen(unquoted);
		lip = rdir->files->first;
	}

	while(lip) {
		int isdir; /* 0 = not dir, 1 = dir, 2 = link (maybe dir) */

		fp = (rfile *)lip->data;
		lip = lip->next;

		isdir = ftp_maybe_isdir(fp);

		if(remote_dir_only && isdir == 0)
			continue;

		name = base_name_ptr(fp->path);

		/* skip dotdirs in completion */
		if(strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
			continue;

		if(strncmp(name, unquoted, len) == 0) {
			char *ret;
			if(dir)
				asprintf(&ret, merge_fmt, dir, name);
			else
				ret = xstrdup(name);
			if(isdir == 1) {
#if (HAVE_LIBREADLINE >= 210)
				rl_completion_append_character =  '/';
#else
				char *tmp;
				tmp = ret;
				asprintf(&ret, "%s/", tmp);
				free(tmp);
#endif
			} else {
#if (HAVE_LIBREADLINE >= 210)
				rl_completion_append_character =  ' ';
#endif
			}
			return ret;
		}
    }
	free(unquoted);
	free(dir);
	return 0;
}

static char *variable_completion_function(const char *text, int state)
{
    static int list_index, len;
    char *name;

    if(!state) {
		list_index = 0;
		len = strlen(text);
    }

    while((name = setvariables[list_index].name) != 0) {
		list_index++;
		if(strncmp(name, text, len) == 0)
			return xstrdup(name);
    }
    return 0;
}

static char *ftplist_completion_function(const char *text, int state)
{
	static int len;
	static listitem *lip = 0;

	if(!state) {
		len = strlen(text);
		lip = gvFtpList->first;
	}

	while(lip) {
		char *name;
		Ftp *f = (Ftp *)lip->data;
		lip = lip->next;
		if(f->url == 0)
			/* Ftp not connected */
			continue;
		name = f->url->alias ? f->url->alias : f->url->hostname;
		if(strncmp(name, text, len) == 0)
			return bash_backslash_quote(name);
	}
	return 0;
}

static char *taglist_completion_function(const char *text, int state)
{
	static int len;
	static listitem *lip = 0;

	if(!ftp->taglist)
		return 0;

	if(!state) {
		len = strlen(text);
		lip = ftp->taglist->first;
	}

	rl_filename_completion_desired = 1;
#if (HAVE_LIBREADLINE >= 210)
	rl_filename_quoting_desired = 1;
#endif

	while(lip) {
		rfile *f = (rfile *)lip->data;
		lip = lip->next;
		if(strncmp(f->path, text, len) == 0)
			return xstrdup(f->path);
	}
	return 0;
}

static char *local_taglist_completion_function(const char *text, int state)
{
	static int len;
	static listitem *lip = 0;

	if(!gvLocalTagList)
		return 0;

	if(!state) {
		len = strlen(text);
		lip = gvLocalTagList->first;
	}

	rl_filename_completion_desired = 1;
#if (HAVE_LIBREADLINE >= 210)
	rl_filename_quoting_desired = 1;
#endif

	while(lip) {
		char *p = (char *)lip->data;
		lip = lip->next;
		if(strncmp(p, text, len) == 0)
			return xstrdup(p); /* bash_backslash_quote(p); */
	}
	return 0;
}

/* Attempt to complete on the contents of TEXT.  START and END bound the
 * region of rl_line_buffer that contains the word to complete.  TEXT is
 * the word to complete.  We can use the entire contents of rl_line_buffer
 * in case we want to do some simple parsing.  Return the array of matches,
 * or NULL if there aren't any.
 */
char **the_complete_function(char *text, int start, int end)
{
    char **matches = 0;
	cmd_t *cp;
	char *e, *c, *orgc;
	cpl_t cpl;
	char quoted;

#if (HAVE_LIBREADLINE >= 210)
	rl_completion_append_character = ' ';
#endif

	if(force_completion_type == cpUnset) {
		int i;
		int cmd_start;

		orgc = c = xstrdup(rl_line_buffer);

		if(c) {
			/* find the start index of the command to complete */
			i = start;
			while(c[i] != ';' && i>0)
				i--;
			cmd_start = i;
			if(c[i] == ';')
				cmd_start++;
		} else
			/* rl_line_buffer is empty, no command yet */
			cmd_start = start;

		/* is this an argument to a command? */
		if(start != cmd_start) {
			if(c[cmd_start] == '!')
				cpl = cpLocalFile;
			else {
				c += cmd_start;
				e = strqsep(&c, ' ');
				cp = find_func(e, false);
				if(!cp) {
					free(orgc);
					return 0;
				}
				cpl = cp->cpltype;
			}
		} else
			cpl = cpCommand;
		free(orgc);
	} else
		cpl = force_completion_type;

	quoted = ((char_is_quoted(rl_line_buffer, start) &&
			   strchr(rl_completer_quote_characters, rl_line_buffer[start-1]))
			  ? rl_line_buffer[start-1] : 0);
	text = bash_dequote_filename(text, quoted);

	remote_dir_only = false;
	switch(cpl) {
	  case cpUnset:
	  case cpNone:
		break;
	  case cpCommand:
		matches = rl_completion_matches(text, command_completion_function);
		break;
	  case cpLocalFile:
		matches = rl_completion_matches(text, rl_filename_completion_function);
		break;
	  case cpRemoteDir:
		remote_dir_only = true;
		/* fall through */
	  case cpRemoteFile:
		if(!gvRemoteCompletion)
			break;
		matches = rl_completion_matches(text, remote_completion_function);
		break;
	  case cpHostname:
		matches = rl_completion_matches(text, hostname_completion_function);
		break;
	  case cpBookmark:
		matches = rl_completion_matches(text, bookmark_completion_function);
		break;
	  case cpAlias:
		matches = rl_completion_matches(text, alias_completion_function);
		break;
	  case cpVariable:
		matches = rl_completion_matches(text, variable_completion_function);
		break;
	  case cpFtpList:
		matches = rl_completion_matches(text, ftplist_completion_function);
		break;
	  case cpTaglist:
		matches = rl_completion_matches(text, taglist_completion_function);
		break;
	  case cpLocalTaglist:
		matches = rl_completion_matches(text,
										local_taglist_completion_function);
		break;
	}
	free(text);
    return matches;
}

#endif /* HAVE_LIBREADLINE */