1 /* pcomplete.c - functions to generate lists of matches for programmable
4 /* Copyright (C) 1999-2004 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
24 #if defined (PROGRAMMABLE_COMPLETION)
26 #include "bashtypes.h"
27 #include "posixstat.h"
29 #if defined (HAVE_UNISTD_H)
35 #if defined (PREFER_STDARG)
46 #include "pcomplete.h"
49 #include "execute_cmd.h"
52 #if defined (JOB_CONTROL)
61 #include "builtins/common.h"
63 #include <glob/glob.h>
64 #include <glob/strmatch.h>
66 #include <readline/rlconf.h>
67 #include <readline/readline.h>
68 #include <readline/history.h>
73 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
75 typedef SHELL_VAR **SVFUNC ();
78 extern char *strpbrk __P((char *, char *));
81 extern int array_needs_making;
82 extern STRING_INT_ALIST word_token_alist[];
83 extern char *signal_names[];
86 #if defined (PREFER_STDARG)
87 static void debug_printf (const char *, ...) __attribute__((__format__ (printf, 1, 2)));
91 static int it_init_joblist __P((ITEMLIST *, int));
93 static int it_init_aliases __P((ITEMLIST *));
94 static int it_init_arrayvars __P((ITEMLIST *));
95 static int it_init_bindings __P((ITEMLIST *));
96 static int it_init_builtins __P((ITEMLIST *));
97 static int it_init_disabled __P((ITEMLIST *));
98 static int it_init_enabled __P((ITEMLIST *));
99 static int it_init_exported __P((ITEMLIST *));
100 static int it_init_functions __P((ITEMLIST *));
101 static int it_init_hostnames __P((ITEMLIST *));
102 static int it_init_jobs __P((ITEMLIST *));
103 static int it_init_running __P((ITEMLIST *));
104 static int it_init_stopped __P((ITEMLIST *));
105 static int it_init_keywords __P((ITEMLIST *));
106 static int it_init_signals __P((ITEMLIST *));
107 static int it_init_variables __P((ITEMLIST *));
108 static int it_init_setopts __P((ITEMLIST *));
109 static int it_init_shopts __P((ITEMLIST *));
111 static int shouldexp_filterpat __P((char *));
112 static char *preproc_filterpat __P((char *, char *));
114 static void init_itemlist_from_varlist __P((ITEMLIST *, SVFUNC *));
116 static STRINGLIST *gen_matches_from_itemlist __P((ITEMLIST *, const char *));
117 static STRINGLIST *gen_action_completions __P((COMPSPEC *, const char *));
118 static STRINGLIST *gen_globpat_matches __P((COMPSPEC *, const char *));
119 static STRINGLIST *gen_wordlist_matches __P((COMPSPEC *, const char *));
120 static STRINGLIST *gen_shell_function_matches __P((COMPSPEC *, const char *,
121 char *, int, WORD_LIST *,
123 static STRINGLIST *gen_command_matches __P((COMPSPEC *, const char *, char *,
124 int, WORD_LIST *, int, int));
126 static char *pcomp_filename_completion_function __P((const char *, int));
128 #if defined (ARRAY_VARS)
129 static SHELL_VAR *bind_comp_words __P((WORD_LIST *));
131 static void bind_compfunc_variables __P((char *, int, WORD_LIST *, int, int));
132 static void unbind_compfunc_variables __P((int));
133 static WORD_LIST *build_arg_list __P((char *, const char *, WORD_LIST *, int));
134 static WORD_LIST *command_line_to_word_list __P((char *, int, int, int *, int *));
137 static int progcomp_debug = 0;
140 int prog_completion_enabled = 1;
142 /* These are used to manage the arrays of strings for possible completions. */
143 ITEMLIST it_aliases = { 0, it_init_aliases, (STRINGLIST *)0 };
144 ITEMLIST it_arrayvars = { LIST_DYNAMIC, it_init_arrayvars, (STRINGLIST *)0 };
145 ITEMLIST it_bindings = { 0, it_init_bindings, (STRINGLIST *)0 };
146 ITEMLIST it_builtins = { 0, it_init_builtins, (STRINGLIST *)0 };
147 ITEMLIST it_commands = { LIST_DYNAMIC }; /* unused */
148 ITEMLIST it_directories = { LIST_DYNAMIC }; /* unused */
149 ITEMLIST it_disabled = { 0, it_init_disabled, (STRINGLIST *)0 };
150 ITEMLIST it_enabled = { 0, it_init_enabled, (STRINGLIST *)0 };
151 ITEMLIST it_exports = { LIST_DYNAMIC, it_init_exported, (STRINGLIST *)0 };
152 ITEMLIST it_files = { LIST_DYNAMIC }; /* unused */
153 ITEMLIST it_functions = { 0, it_init_functions, (STRINGLIST *)0 };
154 ITEMLIST it_hostnames = { LIST_DYNAMIC, it_init_hostnames, (STRINGLIST *)0 };
155 ITEMLIST it_groups = { LIST_DYNAMIC }; /* unused */
156 ITEMLIST it_jobs = { LIST_DYNAMIC, it_init_jobs, (STRINGLIST *)0 };
157 ITEMLIST it_keywords = { 0, it_init_keywords, (STRINGLIST *)0 };
158 ITEMLIST it_running = { LIST_DYNAMIC, it_init_running, (STRINGLIST *)0 };
159 ITEMLIST it_services = { LIST_DYNAMIC }; /* unused */
160 ITEMLIST it_setopts = { 0, it_init_setopts, (STRINGLIST *)0 };
161 ITEMLIST it_shopts = { 0, it_init_shopts, (STRINGLIST *)0 };
162 ITEMLIST it_signals = { 0, it_init_signals, (STRINGLIST *)0 };
163 ITEMLIST it_stopped = { LIST_DYNAMIC, it_init_stopped, (STRINGLIST *)0 };
164 ITEMLIST it_users = { LIST_DYNAMIC }; /* unused */
165 ITEMLIST it_variables = { LIST_DYNAMIC, it_init_variables, (STRINGLIST *)0 };
170 #if defined (PREFER_STDARG)
171 debug_printf (const char *format, ...)
173 debug_printf (format, va_alist)
180 if (progcomp_debug == 0)
183 SH_VA_START (args, format);
185 fprintf (stdout, "DEBUG: ");
186 vfprintf (stdout, format, args);
187 fprintf (stdout, "\n");
195 /* Functions to manage the item lists */
198 set_itemlist_dirty (it)
201 it->flags |= LIST_DIRTY;
205 initialize_itemlist (itp)
208 (*itp->list_getter) (itp);
209 itp->flags |= LIST_INITIALIZED;
210 itp->flags &= ~LIST_DIRTY;
222 if ((itp->flags & (LIST_DONTFREEMEMBERS|LIST_DONTFREE)) == 0)
223 strvec_flush (sl->list);
224 if ((itp->flags & LIST_DONTFREE) == 0)
228 itp->slist = (STRINGLIST *)NULL;
229 itp->flags &= ~(LIST_DONTFREE|LIST_DONTFREEMEMBERS|LIST_INITIALIZED|LIST_DIRTY);
234 shouldexp_filterpat (s)
239 for (p = s; p && *p; p++)
249 /* Replace any instance of `&' in PAT with TEXT. Backslash may be used to
250 quote a `&' and inhibit substitution. Returns a new string. This just
251 calls stringlib.c:strcreplace(). */
253 preproc_filterpat (pat, text)
259 ret = strcreplace (pat, '&', text, 1);
263 /* Remove any match of FILTERPAT from SL. A `&' in FILTERPAT is replaced by
264 TEXT. A leading `!' in FILTERPAT negates the pattern; in this case
265 any member of SL->list that does *not* match will be removed. This returns
266 a new STRINGLIST with the matching members of SL *copied*. Any
267 non-matching members of SL->list are *freed*. */
269 filter_stringlist (sl, filterpat, text)
271 char *filterpat, *text;
277 if (sl == 0 || sl->list == 0 || sl->list_len == 0)
280 npat = shouldexp_filterpat (filterpat) ? preproc_filterpat (filterpat, text) : filterpat;
282 not = (npat[0] == '!');
283 t = not ? npat + 1 : npat;
285 ret = strlist_create (sl->list_size);
286 for (i = 0; i < sl->list_len; i++)
288 m = strmatch (t, sl->list[i], FNMATCH_EXTFLAG);
289 if ((not && m == FNM_NOMATCH) || (not == 0 && m != FNM_NOMATCH))
292 ret->list[ret->list_len++] = sl->list[i];
295 ret->list[ret->list_len] = (char *)NULL;
296 if (npat != filterpat)
302 /* Turn an array of strings returned by rl_completion_matches into a STRINGLIST.
303 This understands how rl_completion_matches sets matches[0] (the lcd of the
304 strings in the list, unless it's the only match). */
306 completions_to_stringlist (matches)
312 mlen = (matches == 0) ? 0 : strvec_len (matches);
313 sl = strlist_create (mlen + 1);
315 if (matches == 0 || matches[0] == 0)
320 sl->list[0] = STRDUP (matches[0]);
321 sl->list[sl->list_len = 1] = (char *)NULL;
325 for (i = 1, n = 0; i < mlen; i++, n++)
326 sl->list[n] = STRDUP (matches[i]);
328 sl->list[n] = (char *)NULL;
333 /* Functions to manage the various ITEMLISTs that we populate internally.
334 The caller is responsible for setting ITP->flags correctly. */
337 it_init_aliases (itp)
341 alias_t **alias_list;
345 alias_list = all_aliases ();
348 itp->slist = (STRINGLIST *)NULL;
351 for (n = 0; alias_list[n]; n++)
353 sl = strlist_create (n+1);
354 for (i = 0; i < n; i++)
355 sl->list[i] = STRDUP (alias_list[i]->name);
356 sl->list[n] = (char *)NULL;
357 sl->list_size = sl->list_len = n;
360 itp->slist = (STRINGLIST *)NULL;
366 init_itemlist_from_varlist (itp, svfunc)
374 vlist = (*svfunc) ();
377 itp->slist = (STRINGLIST *)NULL;
380 for (n = 0; vlist[n]; n++)
382 sl = strlist_create (n+1);
383 for (i = 0; i < n; i++)
384 sl->list[i] = savestring (vlist[i]->name);
385 sl->list[sl->list_len = n] = (char *)NULL;
390 it_init_arrayvars (itp)
393 #if defined (ARRAY_VARS)
394 init_itemlist_from_varlist (itp, all_array_variables);
402 it_init_bindings (itp)
408 /* rl_funmap_names allocates blist, but not its members */
409 blist = (char **)rl_funmap_names (); /* XXX fix const later */
410 sl = strlist_create (0);
413 sl->list_len = strvec_len (sl->list);
414 itp->flags |= LIST_DONTFREEMEMBERS;
421 it_init_builtins (itp)
427 sl = strlist_create (num_shell_builtins);
428 for (i = n = 0; i < num_shell_builtins; i++)
429 if (shell_builtins[i].function)
430 sl->list[n++] = shell_builtins[i].name;
431 sl->list[sl->list_len = n] = (char *)NULL;
432 itp->flags |= LIST_DONTFREEMEMBERS;
438 it_init_enabled (itp)
444 sl = strlist_create (num_shell_builtins);
445 for (i = n = 0; i < num_shell_builtins; i++)
447 if (shell_builtins[i].function && (shell_builtins[i].flags & BUILTIN_ENABLED))
448 sl->list[n++] = shell_builtins[i].name;
450 sl->list[sl->list_len = n] = (char *)NULL;
451 itp->flags |= LIST_DONTFREEMEMBERS;
457 it_init_disabled (itp)
463 sl = strlist_create (num_shell_builtins);
464 for (i = n = 0; i < num_shell_builtins; i++)
466 if (shell_builtins[i].function && ((shell_builtins[i].flags & BUILTIN_ENABLED) == 0))
467 sl->list[n++] = shell_builtins[i].name;
469 sl->list[sl->list_len = n] = (char *)NULL;
470 itp->flags |= LIST_DONTFREEMEMBERS;
476 it_init_exported (itp)
479 init_itemlist_from_varlist (itp, all_exported_variables);
484 it_init_functions (itp)
487 init_itemlist_from_varlist (itp, all_visible_functions);
492 it_init_hostnames (itp)
497 sl = strlist_create (0);
498 sl->list = get_hostname_list ();
499 sl->list_len = sl->list ? strvec_len (sl->list) : 0;
500 sl->list_size = sl->list_len;
502 itp->flags |= LIST_DONTFREEMEMBERS|LIST_DONTFREE;
507 it_init_joblist (itp, jstate)
511 #if defined (JOB_CONTROL)
520 else if (jstate == 1)
523 sl = strlist_create (job_slots);
524 for (i = job_slots - 1; i >= 0; i--)
529 if (jstate == -1 || JOBSTATE(i) == js)
531 s = savestring (p->command);
532 t = strpbrk (s, " \t\n");
535 sl->list[sl->list_len++] = s;
540 itp->slist = (STRINGLIST *)NULL;
549 return (it_init_joblist (itp, -1));
553 it_init_running (itp)
556 return (it_init_joblist (itp, 0));
560 it_init_stopped (itp)
563 return (it_init_joblist (itp, 1));
567 it_init_keywords (itp)
573 for (n = 0; word_token_alist[n].word; n++)
575 sl = strlist_create (n);
576 for (i = 0; i < n; i++)
577 sl->list[i] = word_token_alist[i].word;
578 sl->list[sl->list_len = i] = (char *)NULL;
579 itp->flags |= LIST_DONTFREEMEMBERS;
585 it_init_signals (itp)
590 sl = strlist_create (0);
591 sl->list = signal_names;
592 sl->list_len = strvec_len (sl->list);
593 itp->flags |= LIST_DONTFREE;
599 it_init_variables (itp)
602 init_itemlist_from_varlist (itp, all_visible_variables);
607 it_init_setopts (itp)
612 sl = strlist_create (0);
613 sl->list = get_minus_o_opts ();
614 sl->list_len = strvec_len (sl->list);
616 itp->flags |= LIST_DONTFREEMEMBERS;
626 sl = strlist_create (0);
627 sl->list = get_shopt_options ();
628 sl->list_len = strvec_len (sl->list);
630 itp->flags |= LIST_DONTFREEMEMBERS;
634 /* Generate a list of all matches for TEXT using the STRINGLIST in itp->slist
635 as the list of possibilities. If the itemlist has been marked dirty or
636 it should be regenerated every time, destroy the old STRINGLIST and make a
637 new one before trying the match. TEXT is dequoted before attempting a
640 gen_matches_from_itemlist (itp, text)
644 STRINGLIST *ret, *sl;
648 if ((itp->flags & (LIST_DIRTY|LIST_DYNAMIC)) ||
649 (itp->flags & LIST_INITIALIZED) == 0)
651 if (itp->flags & (LIST_DIRTY | LIST_DYNAMIC))
652 clean_itemlist (itp);
653 if ((itp->flags & LIST_INITIALIZED) == 0)
654 initialize_itemlist (itp);
657 return ((STRINGLIST *)NULL);
658 ret = strlist_create (itp->slist->list_len+1);
661 ntxt = bash_dequote_text (text);
662 tlen = STRLEN (ntxt);
664 for (i = n = 0; i < sl->list_len; i++)
666 if (tlen == 0 || STREQN (sl->list[i], ntxt, tlen))
667 ret->list[n++] = STRDUP (sl->list[i]);
669 ret->list[ret->list_len = n] = (char *)NULL;
675 /* A wrapper for rl_filename_completion_function that dequotes the filename
676 before attempting completions. */
678 pcomp_filename_completion_function (text, state)
682 static char *dfn; /* dequoted filename */
688 /* remove backslashes quoting special characters in filenames. */
689 if (rl_filename_dequoting_function)
692 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
694 /* Use rl_completion_quote_character because any single or
695 double quotes have been removed by the time TEXT makes it
696 here, and we don't want to remove backslashes inside
698 qc = rl_dispatching ? rl_completion_quote_character : 0;
700 dfn = (*rl_filename_dequoting_function) ((char *)text, qc);
703 dfn = savestring (text);
706 return (rl_filename_completion_function (dfn, state));
709 #define GEN_COMPS(bmap, flag, it, text, glist, tlist) \
713 tlist = gen_matches_from_itemlist (it, text); \
716 glist = strlist_append (glist, tlist); \
717 strlist_dispose (tlist); \
722 #define GEN_XCOMPS(bmap, flag, text, func, cmatches, glist, tlist) \
726 cmatches = rl_completion_matches (text, func); \
727 tlist = completions_to_stringlist (cmatches); \
728 glist = strlist_append (glist, tlist); \
729 strvec_dispose (cmatches); \
730 strlist_dispose (tlist); \
734 /* Functions to generate lists of matches from the actions member of CS. */
737 gen_action_completions (cs, text)
741 STRINGLIST *ret, *tmatches;
742 char **cmatches; /* from rl_completion_matches ... */
745 ret = tmatches = (STRINGLIST *)NULL;
748 GEN_COMPS (flags, CA_ALIAS, &it_aliases, text, ret, tmatches);
749 GEN_COMPS (flags, CA_ARRAYVAR, &it_arrayvars, text, ret, tmatches);
750 GEN_COMPS (flags, CA_BINDING, &it_bindings, text, ret, tmatches);
751 GEN_COMPS (flags, CA_BUILTIN, &it_builtins, text, ret, tmatches);
752 GEN_COMPS (flags, CA_DISABLED, &it_disabled, text, ret, tmatches);
753 GEN_COMPS (flags, CA_ENABLED, &it_enabled, text, ret, tmatches);
754 GEN_COMPS (flags, CA_EXPORT, &it_exports, text, ret, tmatches);
755 GEN_COMPS (flags, CA_FUNCTION, &it_functions, text, ret, tmatches);
756 GEN_COMPS (flags, CA_HOSTNAME, &it_hostnames, text, ret, tmatches);
757 GEN_COMPS (flags, CA_JOB, &it_jobs, text, ret, tmatches);
758 GEN_COMPS (flags, CA_KEYWORD, &it_keywords, text, ret, tmatches);
759 GEN_COMPS (flags, CA_RUNNING, &it_running, text, ret, tmatches);
760 GEN_COMPS (flags, CA_SETOPT, &it_setopts, text, ret, tmatches);
761 GEN_COMPS (flags, CA_SHOPT, &it_shopts, text, ret, tmatches);
762 GEN_COMPS (flags, CA_SIGNAL, &it_signals, text, ret, tmatches);
763 GEN_COMPS (flags, CA_STOPPED, &it_stopped, text, ret, tmatches);
764 GEN_COMPS (flags, CA_VARIABLE, &it_variables, text, ret, tmatches);
766 GEN_XCOMPS(flags, CA_COMMAND, text, command_word_completion_function, cmatches, ret, tmatches);
767 GEN_XCOMPS(flags, CA_FILE, text, pcomp_filename_completion_function, cmatches, ret, tmatches);
768 GEN_XCOMPS(flags, CA_USER, text, rl_username_completion_function, cmatches, ret, tmatches);
769 GEN_XCOMPS(flags, CA_GROUP, text, bash_groupname_completion_function, cmatches, ret, tmatches);
770 GEN_XCOMPS(flags, CA_SERVICE, text, bash_servicename_completion_function, cmatches, ret, tmatches);
772 /* And lastly, the special case for directories */
773 if (flags & CA_DIRECTORY)
775 rl_completion_mark_symlink_dirs = 1; /* override user preference */
776 cmatches = bash_directory_completion_matches (text);
777 tmatches = completions_to_stringlist (cmatches);
778 ret = strlist_append (ret, tmatches);
779 strvec_dispose (cmatches);
780 strlist_dispose (tmatches);
786 /* Generate a list of matches for CS->globpat. Unresolved: should this use
787 TEXT as a match prefix, or just go without? Currently, the code does not
788 use TEXT, just globs CS->globpat and returns the results. If we do decide
789 to use TEXT, we should call quote_string_for_globbing before the call to
792 gen_globpat_matches (cs, text)
798 sl = strlist_create (0);
799 sl->list = glob_filename (cs->globpat, 0);
800 if (GLOB_FAILED (sl->list))
801 sl->list = (char **)NULL;
803 sl->list_len = sl->list_size = strvec_len (sl->list);
807 /* Perform the shell word expansions on CS->words and return the results.
808 Again, this ignores TEXT. */
810 gen_wordlist_matches (cs, text)
817 char *ntxt; /* dequoted TEXT to use in comparisons */
819 if (cs->words == 0 || cs->words[0] == '\0')
820 return ((STRINGLIST *)NULL);
822 /* This used to be a simple expand_string(cs->words, 0), but that won't
823 do -- there's no way to split a simple list into individual words
824 that way, since the shell semantics say that word splitting is done
825 only on the results of expansion. */
826 l = split_at_delims (cs->words, strlen (cs->words), (char *)NULL, -1, (int *)NULL, (int *)NULL);
828 return ((STRINGLIST *)NULL);
829 /* This will jump back to the top level if the expansion fails... */
830 l2 = expand_words_shellexp (l);
833 nw = list_length (l2);
834 sl = strlist_create (nw + 1);
836 ntxt = bash_dequote_text (text);
837 tlen = STRLEN (ntxt);
839 for (nw = 0, l = l2; l; l = l->next)
841 if (tlen == 0 || STREQN (l->word->word, ntxt, tlen))
842 sl->list[nw++] = STRDUP (l->word->word);
844 sl->list[sl->list_len = nw] = (char *)NULL;
853 bind_comp_words (lwords)
858 v = find_variable ("COMP_WORDS");
860 v = make_new_array_variable ("COMP_WORDS");
862 VUNSETATTR (v, att_readonly);
863 if (array_p (v) == 0)
864 v = convert_var_to_array (v);
865 v = assign_array_var_from_word_list (v, lwords);
868 #endif /* ARRAY_VARS */
871 bind_compfunc_variables (line, ind, lwords, cw, exported)
877 char ibuf[INT_STRLEN_BOUND(int) + 1];
881 /* Set the variables that the function expects while it executes. Maybe
882 these should be in the function environment (temporary_env). */
883 v = bind_variable ("COMP_LINE", line);
885 VSETATTR(v, att_exported);
887 value = inttostr (ind, ibuf, sizeof(ibuf));
888 v = bind_int_variable ("COMP_POINT", value);
890 VSETATTR(v, att_exported);
892 /* Since array variables can't be exported, we don't bother making the
897 v = bind_comp_words (lwords);
898 value = inttostr (cw, ibuf, sizeof(ibuf));
899 bind_int_variable ("COMP_CWORD", value);
903 array_needs_making = 1;
907 unbind_compfunc_variables (exported)
910 unbind_variable ("COMP_LINE");
911 unbind_variable ("COMP_POINT");
913 unbind_variable ("COMP_WORDS");
914 unbind_variable ("COMP_CWORD");
917 array_needs_making = 1;
920 /* Build the list of words to pass to a function or external command
921 as arguments. When the function or command is invoked,
923 $0 == function or command being invoked
925 $2 = word to be completed (possibly null)
928 Functions can access all of the words in the current command line
929 with the COMP_WORDS array. External commands cannot. */
932 build_arg_list (cmd, text, lwords, ind)
938 WORD_LIST *ret, *cl, *l;
942 ret = (WORD_LIST *)NULL;
944 ret = make_word_list (w, (WORD_LIST *)NULL);
946 w = (lwords && lwords->word) ? copy_word (lwords->word) : make_word ("");
947 cl = ret->next = make_word_list (w, (WORD_LIST *)NULL);
949 w = make_word (text);
950 cl->next = make_word_list (w, (WORD_LIST *)NULL);
953 /* Search lwords for current word */
954 for (l = lwords, i = 1; l && i < ind-1; l = l->next, i++)
956 w = (l && l->word) ? copy_word (l->word) : make_word ("");
957 cl->next = make_word_list (w, (WORD_LIST *)NULL);
962 /* Build a command string with
963 $0 == cs->funcname (function to execute for completion list)
964 $1 == command name (command being completed)
965 $2 = word to be completed (possibly null)
967 and run in the current shell. The function should put its completion
968 list into the array variable COMPREPLY. We build a STRINGLIST
969 from the results and return it.
971 Since the shell function should return its list of matches in an array
972 variable, this does nothing if arrays are not compiled into the shell. */
975 gen_shell_function_matches (cs, text, line, ind, lwords, nw, cw)
988 #if defined (ARRAY_VARS)
992 funcname = cs->funcname;
993 f = find_function (funcname);
996 internal_error (_("completion: function `%s' not found"), funcname);
999 return ((STRINGLIST *)NULL);
1002 #if !defined (ARRAY_VARS)
1003 return ((STRINGLIST *)NULL);
1006 /* We pass cw - 1 because command_line_to_word_list returns indices that are
1007 1-based, while bash arrays are 0-based. */
1008 bind_compfunc_variables (line, ind, lwords, cw - 1, 0);
1010 cmdlist = build_arg_list (funcname, text, lwords, cw);
1012 fval = execute_shell_function (f, cmdlist);
1014 /* Now clean up and destroy everything. */
1015 dispose_words (cmdlist);
1016 unbind_compfunc_variables (0);
1018 /* The list of completions is returned in the array variable COMPREPLY. */
1019 v = find_variable ("COMPREPLY");
1021 return ((STRINGLIST *)NULL);
1022 if (array_p (v) == 0)
1023 v = convert_var_to_array (v);
1026 if (a == 0 || array_empty (a))
1027 sl = (STRINGLIST *)NULL;
1030 /* XXX - should we filter the list of completions so only those matching
1031 TEXT are returned? Right now, we do not. */
1032 sl = strlist_create (0);
1033 sl->list = array_to_argv (a);
1034 sl->list_len = sl->list_size = array_num_elements (a);
1037 /* XXX - should we unbind COMPREPLY here? */
1038 unbind_variable ("COMPREPLY");
1044 /* Build a command string with
1045 $0 == cs->command (command to execute for completion list)
1046 $1 == command name (command being completed)
1047 $2 = word to be completed (possibly null)
1049 and run in with command substitution. Parse the results, one word
1050 per line, with backslashes allowed to escape newlines. Build a
1051 STRINGLIST from the results and return it. */
1054 gen_command_matches (cs, text, line, ind, lwords, nw, cw)
1062 char *csbuf, *cscmd, *t;
1063 int cmdlen, cmdsize, n, ws, we;
1064 WORD_LIST *cmdlist, *cl;
1067 bind_compfunc_variables (line, ind, lwords, cw, 1);
1068 cmdlist = build_arg_list (cs->command, text, lwords, cw);
1070 /* Estimate the size needed for the buffer. */
1071 n = strlen (cs->command);
1073 for (cl = cmdlist->next; cl; cl = cl->next)
1074 cmdsize += STRLEN (cl->word->word) + 3;
1077 /* allocate the string for the command and fill it in. */
1078 cscmd = (char *)xmalloc (cmdsize + 1);
1080 strcpy (cscmd, cs->command); /* $0 */
1082 cscmd[cmdlen++] = ' ';
1083 for (cl = cmdlist->next; cl; cl = cl->next) /* $1, $2, $3, ... */
1085 t = sh_single_quote (cl->word->word ? cl->word->word : "");
1087 RESIZE_MALLOCED_BUFFER (cscmd, cmdlen, n + 2, cmdsize, 64);
1088 strcpy (cscmd + cmdlen, t);
1091 cscmd[cmdlen++] = ' ';
1094 cscmd[cmdlen] = '\0';
1096 csbuf = command_substitute (cscmd, 0);
1098 /* Now clean up and destroy everything. */
1099 dispose_words (cmdlist);
1101 unbind_compfunc_variables (1);
1103 if (csbuf == 0 || *csbuf == '\0')
1106 return ((STRINGLIST *)NULL);
1109 /* Now break CSBUF up at newlines, with backslash allowed to escape a
1110 newline, and put the individual words into a STRINGLIST. */
1111 sl = strlist_create (16);
1112 for (ws = 0; csbuf[ws]; )
1115 while (csbuf[we] && csbuf[we] != '\n')
1117 if (csbuf[we] == '\\' && csbuf[we+1] == '\n')
1121 t = substring (csbuf, ws, we);
1122 if (sl->list_len >= sl->list_size - 1)
1123 strlist_resize (sl, sl->list_size + 16);
1124 sl->list[sl->list_len++] = t;
1125 while (csbuf[we] == '\n') we++;
1128 sl->list[sl->list_len] = (char *)NULL;
1135 command_line_to_word_list (line, llen, sentinel, nwp, cwp)
1137 int llen, sentinel, *nwp, *cwp;
1142 delims = "()<>;&| \t\n"; /* shell metacharacters break words */
1143 ret = split_at_delims (line, llen, delims, sentinel, nwp, cwp);
1147 /* Evaluate COMPSPEC *cs and return all matches for WORD. */
1150 gen_compspec_completions (cs, cmd, word, start, end)
1156 STRINGLIST *ret, *tmatches;
1163 debug_printf ("gen_compspec_completions (%s, %s, %d, %d)", cmd, word, start, end);
1164 debug_printf ("gen_compspec_completions: %s -> %p", cmd, cs);
1166 ret = gen_action_completions (cs, word);
1168 if (ret && progcomp_debug)
1170 debug_printf ("gen_action_completions (%p, %s) -->", cs, word);
1171 strlist_print (ret, "\t");
1176 /* Now we start generating completions based on the other members of CS. */
1179 tmatches = gen_globpat_matches (cs, word);
1185 debug_printf ("gen_globpat_matches (%p, %s) -->", cs, word);
1186 strlist_print (tmatches, "\t");
1190 ret = strlist_append (ret, tmatches);
1191 strlist_dispose (tmatches);
1192 rl_filename_completion_desired = 1;
1198 tmatches = gen_wordlist_matches (cs, word);
1204 debug_printf ("gen_wordlist_matches (%p, %s) -->", cs, word);
1205 strlist_print (tmatches, "\t");
1209 ret = strlist_append (ret, tmatches);
1210 strlist_dispose (tmatches);
1214 lwords = (WORD_LIST *)NULL;
1215 line = (char *)NULL;
1216 if (cs->command || cs->funcname)
1218 /* If we have a command or function to execute, we need to first break
1219 the command line into individual words, find the number of words,
1220 and find the word in the list containing the word to be completed. */
1221 line = substring (rl_line_buffer, start, end);
1225 debug_printf ("command_line_to_word_list (%s, %d, %d, %p, %p)",
1226 line, llen, rl_point - start, &nw, &cw);
1228 lwords = command_line_to_word_list (line, llen, rl_point - start, &nw, &cw);
1230 if (lwords == 0 && llen > 0)
1231 debug_printf ("ERROR: command_line_to_word_list returns NULL");
1232 else if (progcomp_debug)
1234 debug_printf ("command_line_to_word_list -->");
1236 print_word_list (lwords, "!");
1246 tmatches = gen_shell_function_matches (cs, word, line, rl_point - start, lwords, nw, cw);
1252 debug_printf ("gen_shell_function_matches (%p, %s, %p, %d, %d) -->", cs, word, lwords, nw, cw);
1253 strlist_print (tmatches, "\t");
1257 ret = strlist_append (ret, tmatches);
1258 strlist_dispose (tmatches);
1264 tmatches = gen_command_matches (cs, word, line, rl_point - start, lwords, nw, cw);
1270 debug_printf ("gen_command_matches (%p, %s, %p, %d, %d) -->", cs, word, lwords, nw, cw);
1271 strlist_print (tmatches, "\t");
1275 ret = strlist_append (ret, tmatches);
1276 strlist_dispose (tmatches);
1280 if (cs->command || cs->funcname)
1283 dispose_words (lwords);
1289 tmatches = filter_stringlist (ret, cs->filterpat, word);
1293 debug_printf ("filter_stringlist (%p, %s, %s) -->", ret, cs->filterpat, word);
1294 strlist_print (tmatches, "\t");
1298 if (ret && ret != tmatches)
1306 if (cs->prefix || cs->suffix)
1307 ret = strlist_prefix_suffix (ret, cs->prefix, cs->suffix);
1309 /* If no matches have been generated and the user has specified that
1310 directory completion should be done as a default, call
1311 gen_action_completions again to generate a list of matching directory
1313 if ((ret == 0 || ret->list_len == 0) && (cs->options & COPT_DIRNAMES))
1315 tcs = compspec_create ();
1316 tcs->actions = CA_DIRECTORY;
1317 ret = gen_action_completions (tcs, word);
1318 compspec_dispose (tcs);
1320 else if (cs->options & COPT_PLUSDIRS)
1322 tcs = compspec_create ();
1323 tcs->actions = CA_DIRECTORY;
1324 tmatches = gen_action_completions (tcs, word);
1325 ret = strlist_append (ret, tmatches);
1326 strlist_dispose (tmatches);
1327 compspec_dispose (tcs);
1333 /* The driver function for the programmable completion code. Returns a list
1334 of matches for WORD, which is an argument to command CMD. START and END
1335 bound the command currently being completed in rl_line_buffer. */
1337 programmable_completions (cmd, word, start, end, foundp)
1340 int start, end, *foundp;
1344 char **rmatches, *t;
1346 /* We look at the basename of CMD if the full command does not have
1347 an associated COMPSPEC. */
1348 cs = progcomp_search (cmd);
1351 t = strrchr (cmd, '/');
1353 cs = progcomp_search (++t);
1359 return ((char **)NULL);
1362 cs = compspec_copy (cs);
1364 /* Signal the caller that we found a COMPSPEC for this command, and pass
1365 back any meta-options associated with the compspec. */
1367 *foundp = 1|cs->options;
1369 ret = gen_compspec_completions (cs, cmd, word, start, end);
1371 compspec_dispose (cs);
1375 rmatches = ret->list;
1379 rmatches = (char **)NULL;
1384 #endif /* PROGRAMMABLE_COMPLETION */