1 /* pcomplete.c - functions to generate lists of matches for programmable
4 /* Copyright (C) 1999 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)
38 # if defined (PREFER_VARARGS)
47 #include "pcomplete.h"
52 #if defined (JOB_CONTROL)
61 #include "builtins/common.h"
63 #include <glob/glob.h>
64 #include <glob/fnmatch.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 rl_filename_completion_desired;
82 extern int array_needs_making;
83 extern STRING_INT_ALIST word_token_alist[];
84 extern char *signal_names[];
86 static int it_init_aliases ();
87 static int it_init_arrayvars ();
88 static int it_init_bindings ();
89 static int it_init_builtins ();
90 static int it_init_disabled ();
91 static int it_init_enabled ();
92 static int it_init_exported ();
93 static int it_init_functions ();
94 static int it_init_hostnames ();
95 static int it_init_jobs ();
96 static int it_init_running ();
97 static int it_init_stopped ();
98 static int it_init_keywords ();
99 static int it_init_signals ();
100 static int it_init_variables ();
101 static int it_init_setopts ();
102 static int it_init_shopts ();
104 static int progcomp_debug = 0;
106 int prog_completion_enabled = 1;
108 /* These are used to manage the arrays of strings for possible completions. */
109 ITEMLIST it_aliases = { 0, it_init_aliases, (STRINGLIST *)0 };
110 ITEMLIST it_arrayvars = { LIST_DYNAMIC, it_init_arrayvars, (STRINGLIST *)0 };
111 ITEMLIST it_bindings = { 0, it_init_bindings, (STRINGLIST *)0 };
112 ITEMLIST it_builtins = { 0, it_init_builtins, (STRINGLIST *)0 };
113 ITEMLIST it_commands = { LIST_DYNAMIC }; /* unused */
114 ITEMLIST it_directories = { LIST_DYNAMIC }; /* unused */
115 ITEMLIST it_disabled = { 0, it_init_disabled, (STRINGLIST *)0 };
116 ITEMLIST it_enabled = { 0, it_init_enabled, (STRINGLIST *)0 };
117 ITEMLIST it_exports = { LIST_DYNAMIC, it_init_exported, (STRINGLIST *)0 };
118 ITEMLIST it_files = { LIST_DYNAMIC }; /* unused */
119 ITEMLIST it_functions = { 0, it_init_functions, (STRINGLIST *)0 };
120 ITEMLIST it_hostnames = { LIST_DYNAMIC, it_init_hostnames, (STRINGLIST *)0 };
121 ITEMLIST it_jobs = { LIST_DYNAMIC, it_init_jobs, (STRINGLIST *)0 };;
122 ITEMLIST it_keywords = { 0, it_init_keywords, (STRINGLIST *)0 };
123 ITEMLIST it_running = { LIST_DYNAMIC, it_init_running, (STRINGLIST *)0 };
124 ITEMLIST it_setopts = { 0, it_init_setopts, (STRINGLIST *)0 };
125 ITEMLIST it_shopts = { 0, it_init_shopts, (STRINGLIST *)0 };
126 ITEMLIST it_signals = { 0, it_init_signals, (STRINGLIST *)0 };
127 ITEMLIST it_stopped = { LIST_DYNAMIC, it_init_stopped, (STRINGLIST *)0 };
128 ITEMLIST it_users = { LIST_DYNAMIC }; /* unused */
129 ITEMLIST it_variables = { LIST_DYNAMIC, it_init_variables, (STRINGLIST *)0 };
132 #if !defined (USE_VARARGS)
134 debug_printf (format, arg1, arg2, arg3, arg4, arg5)
137 if (progcomp_debug == 0)
140 fprintf (stdout, format, arg1, arg2, arg3, arg4, arg5);
141 fprintf (stdout, "\n");
146 #if defined (PREFER_STDARG)
147 debug_printf (const char *format, ...)
149 debug_printf (format, va_alist)
156 if (progcomp_debug == 0)
159 #if defined (PREFER_STDARG)
160 va_start (args, format);
165 fprintf (stdout, "DEBUG: ");
166 vfprintf (stdout, format, args);
167 fprintf (stdout, "\n");
173 #endif /* USE_VARARGS */
175 /* Functions to manage the item lists */
178 set_itemlist_dirty (it)
181 it->flags |= LIST_DIRTY;
185 initialize_itemlist (itp)
188 (*itp->list_getter) (itp);
189 itp->flags |= LIST_INITIALIZED;
190 itp->flags &= ~LIST_DIRTY;
202 if ((itp->flags & (LIST_DONTFREEMEMBERS|LIST_DONTFREE)) == 0)
203 free_array_members (sl->list);
204 if ((itp->flags & LIST_DONTFREE) == 0)
208 itp->slist = (STRINGLIST *)NULL;
209 itp->flags &= ~(LIST_DONTFREE|LIST_DONTFREEMEMBERS|LIST_INITIALIZED|LIST_DIRTY);
212 /* Functions to manage the string lists -- lists of matches. These should
213 really be moved to a separate file. */
215 /* Allocate a new STRINGLIST, with room for N strings. */
223 ret = (STRINGLIST *)xmalloc (sizeof (STRINGLIST));
226 ret->list = alloc_array (n+1);
228 for (i = 0; i < n; i++)
229 ret->list[i] = (char *)NULL;
233 ret->list = (char **)NULL;
241 realloc_stringlist (sl, n)
247 if (n > sl->list_size)
249 sl->list = (char **)xrealloc (sl->list, (n+1) * sizeof (char *));
250 for (i = sl->list_size; i <= n; i++)
251 sl->list[i] = (char *)NULL;
264 free_array (sl->list);
275 new = alloc_stringlist (sl->list_size);
276 /* I'd like to use copy_array, but that doesn't copy everything. */
279 for (i = 0; i < sl->list_size; i++)
280 new->list[i] = STRDUP (sl->list[i]);
282 new->list_size = sl->list_size;
283 new->list_len = sl->list_len;
284 /* just being careful */
286 new->list[new->list_len] = (char *)NULL;
290 /* Return a new STRINGLIST with everything from M1 and M2. */
293 merge_stringlists (m1, m2)
299 l1 = m1 ? m1->list_len : 0;
300 l2 = m2 ? m2->list_len : 0;
302 sl = alloc_stringlist (l1 + l2 + 1);
303 for (i = n = 0; i < l1; i++, n++)
304 sl->list[n] = STRDUP (m1->list[i]);
305 for (i = 0; i < l2; i++, n++)
306 sl->list[n] = STRDUP (m2->list[i]);
308 sl->list[n] = (char *)NULL;
311 /* Make STRINGLIST M1 contain everything in M1 and M2. */
313 append_stringlist (m1, m2)
316 register int i, n, len1, len2;
320 m1 = copy_stringlist (m2);
325 len2 = m2 ? m2->list_len : 0;
329 m1 = realloc_stringlist (m1, len1 + len2 + 1);
330 for (i = 0, n = len1; i < len2; i++, n++)
331 m1->list[n] = STRDUP (m2->list[i]);
332 m1->list[n] = (char *)NULL;
340 shouldexp_filterpat (s)
345 for (p = s; p && *p; p++)
355 /* Replace any instance of `&' in PAT with TEXT. Backslash may be used to
356 quote a `&' and inhibit substitution. Returns a new string. This just
357 calls stringlib.c:strcreplace(). */
359 preproc_filterpat (pat, text)
365 ret = strcreplace (pat, '&', text, 1);
369 /* Remove any match of FILTERPAT from SL. A `&' in FILTERPAT is replaced by
370 TEXT. A leading `!' in FILTERPAT negates the pattern; in this case
371 any member of SL->list that does *not* match will be removed. This returns
372 a new STRINGLIST with the matching members of SL *copied*. Any
373 non-matching members of SL->list are *freed*. */
375 filter_stringlist (sl, filterpat, text)
377 char *filterpat, *text;
383 if (sl == 0 || sl->list == 0 || sl->list_len == 0)
386 npat = shouldexp_filterpat (filterpat) ? preproc_filterpat (filterpat, text) : filterpat;
388 not = (npat[0] == '!');
389 t = not ? npat + 1 : npat;
391 ret = alloc_stringlist (sl->list_size);
392 for (i = 0; i < sl->list_len; i++)
394 m = fnmatch (t, sl->list[i], FNMATCH_EXTFLAG);
395 if ((not && m == FNM_NOMATCH) || (not == 0 && m != FNM_NOMATCH))
398 ret->list[ret->list_len++] = sl->list[i];
401 ret->list[ret->list_len] = (char *)NULL;
402 if (npat != filterpat)
409 prefix_suffix_stringlist (sl, prefix, suffix)
411 char *prefix, *suffix;
413 int plen, slen, tlen, llen, i;
416 if (sl == 0 || sl->list == 0 || sl->list_len == 0)
419 plen = STRLEN (prefix);
420 slen = STRLEN (suffix);
422 if (plen == 0 && slen == 0)
425 for (i = 0; i < sl->list_len; i++)
427 llen = STRLEN (sl->list[i]);
428 tlen = plen + llen + slen + 1;
429 t = xmalloc (tlen + 1);
432 strcpy (t + plen, sl->list[i]);
434 strcpy (t + plen + llen, suffix);
443 print_stringlist (sl, prefix)
451 for (i = 0; i < sl->list_len; i++)
452 printf ("%s%s\n", prefix ? prefix : "", sl->list[i]);
455 /* Turn an array of strings returned by completion_matches into a STRINGLIST.
456 This understands how completion_matches sets matches[0] (the lcd of the
457 strings in the list, unless it's the only match). */
459 completions_to_stringlist (matches)
465 mlen = (matches == 0) ? 0 : array_len (matches);
466 sl = alloc_stringlist (mlen + 1);
468 if (matches == 0 || matches[0] == 0)
473 sl->list[0] = STRDUP (matches[0]);
474 sl->list[sl->list_len = 1] = (char *)NULL;
478 for (i = 1, n = 0; i < mlen; i++, n++)
479 sl->list[n] = STRDUP (matches[i]);
481 sl->list[n] = (char *)NULL;
486 /* Functions to manage the various ITEMLISTs that we populate internally.
487 The caller is responsible for setting ITP->flags correctly. */
490 it_init_aliases (itp)
498 aliases = all_aliases ();
501 itp->slist = (STRINGLIST *)NULL;
504 for (n = 0; aliases[n]; n++)
506 sl = alloc_stringlist (n+1);
507 for (i = 0; i < n; i++)
508 sl->list[i] = STRDUP (aliases[i]->name);
509 sl->list[n] = (char *)NULL;
510 sl->list_size = sl->list_len = n;
513 itp->slist = (STRINGLIST *)NULL;
519 init_itemlist_from_varlist (itp, svfunc)
527 vlist = (*svfunc) ();
528 for (n = 0; vlist[n]; n++)
530 sl = alloc_stringlist (n+1);
531 for (i = 0; i < n; i++)
532 sl->list[i] = savestring (vlist[i]->name);
533 sl->list[sl->list_len = n] = (char *)NULL;
538 it_init_arrayvars (itp)
541 #if defined (ARRAY_VARS)
542 init_itemlist_from_varlist (itp, all_array_variables);
550 it_init_bindings (itp)
556 /* rl_funmap_names allocates blist, but not its members */
557 blist = rl_funmap_names ();
558 sl = alloc_stringlist (0);
561 sl->list_len = array_len (sl->list);
562 itp->flags |= LIST_DONTFREEMEMBERS;
569 it_init_builtins (itp)
576 sl = alloc_stringlist (num_shell_builtins);
577 for (i = n = 0; i < num_shell_builtins; i++)
578 if (shell_builtins[i].function)
579 sl->list[n++] = shell_builtins[i].name;
580 sl->list[sl->list_len = n] = (char *)NULL;
581 itp->flags |= LIST_DONTFREEMEMBERS;
587 it_init_enabled (itp)
594 sl = alloc_stringlist (num_shell_builtins);
595 for (i = n = 0; i < num_shell_builtins; i++)
597 if (shell_builtins[i].function && (shell_builtins[i].flags & BUILTIN_ENABLED))
598 sl->list[n++] = shell_builtins[i].name;
600 sl->list[sl->list_len = n] = (char *)NULL;
601 itp->flags |= LIST_DONTFREEMEMBERS;
607 it_init_disabled (itp)
614 sl = alloc_stringlist (num_shell_builtins);
615 for (i = n = 0; i < num_shell_builtins; i++)
617 if (shell_builtins[i].function && ((shell_builtins[i].flags & BUILTIN_ENABLED) == 0))
618 sl->list[n++] = shell_builtins[i].name;
620 sl->list[sl->list_len = n] = (char *)NULL;
621 itp->flags |= LIST_DONTFREEMEMBERS;
627 it_init_exported (itp)
630 init_itemlist_from_varlist (itp, all_exported_variables);
635 it_init_functions (itp)
638 init_itemlist_from_varlist (itp, all_visible_functions);
643 it_init_hostnames (itp)
648 sl = alloc_stringlist (0);
649 sl->list = get_hostname_list ();
650 sl->list_len = sl->list ? array_len (sl->list) : 0;
651 sl->list_size = sl->list_len;
653 itp->flags |= LIST_DONTFREEMEMBERS|LIST_DONTFREE;
658 it_init_joblist (itp, jstate)
662 #if defined (JOB_CONTROL)
671 else if (jstate == 1)
674 sl = alloc_stringlist (job_slots);
675 for (i = job_slots - 1; i >= 0; i--)
680 if (jstate == -1 || JOBSTATE(i) == js)
682 s = savestring (p->command);
683 t = strpbrk (s, " \t\n");
686 sl->list[sl->list_len++] = s;
691 itp->slist = (STRINGLIST *)NULL;
700 return (it_init_joblist (itp, -1));
704 it_init_running (itp)
707 return (it_init_joblist (itp, 0));
711 it_init_stopped (itp)
714 return (it_init_joblist (itp, 1));
718 it_init_keywords (itp)
724 for (n = 0; word_token_alist[n].word; n++)
726 sl = alloc_stringlist (n);
727 for (i = 0; i < n; i++)
728 sl->list[i] = word_token_alist[i].word;
729 sl->list[sl->list_len = i] = (char *)NULL;
730 itp->flags |= LIST_DONTFREEMEMBERS;
736 it_init_signals (itp)
741 sl = alloc_stringlist (0);
742 sl->list = signal_names;
743 sl->list_len = array_len (sl->list);
744 itp->flags |= LIST_DONTFREE;
750 it_init_variables (itp)
753 init_itemlist_from_varlist (itp, all_visible_variables);
758 it_init_setopts (itp)
763 sl = alloc_stringlist (0);
764 sl->list = get_minus_o_opts ();
765 sl->list_len = array_len (sl->list);
767 itp->flags |= LIST_DONTFREEMEMBERS;
777 sl = alloc_stringlist (0);
778 sl->list = get_shopt_options ();
779 sl->list_len = array_len (sl->list);
781 itp->flags |= LIST_DONTFREEMEMBERS;
785 /* Generate a list of all matches for TEXT using the STRINGLIST in itp->slist
786 as the list of possibilities. If the itemlist has been marked dirty or
787 it should be regenerated every time, destroy the old STRINGLIST and make a
788 new one before trying the match. */
790 gen_matches_from_itemlist (itp, text)
794 STRINGLIST *ret, *sl;
797 if ((itp->flags & (LIST_DIRTY|LIST_DYNAMIC)) ||
798 (itp->flags & LIST_INITIALIZED) == 0)
800 if (itp->flags & (LIST_DIRTY | LIST_DYNAMIC))
801 clean_itemlist (itp);
802 if ((itp->flags & LIST_INITIALIZED) == 0)
803 initialize_itemlist (itp);
805 ret = alloc_stringlist (itp->slist->list_len+1);
807 tlen = STRLEN (text);
808 for (i = n = 0; i < sl->list_len; i++)
810 if (tlen == 0 || STREQN (sl->list[i], text, tlen))
811 ret->list[n++] = STRDUP (sl->list[i]);
813 ret->list[ret->list_len = n] = (char *)NULL;
817 /* A wrapper for filename_completion_function that dequotes the filename
818 before attempting completions. */
820 pcomp_filename_completion_function (text, state)
824 static char *dfn; /* dequoted filename */
830 /* remove backslashes quoting special characters in filenames. */
831 if (rl_filename_dequoting_function)
833 qc = (text[0] == '"' || text[0] == '\'') ? text[0] : 0;
834 dfn = (*rl_filename_dequoting_function) (text, qc);
837 dfn = savestring (text);
840 return (filename_completion_function (dfn, state));
843 #define GEN_COMPS(bmap, flag, it, text, glist, tlist) \
847 tlist = gen_matches_from_itemlist (it, text); \
848 glist = append_stringlist (glist, tlist); \
849 free_stringlist (tlist); \
853 #define GEN_XCOMPS(bmap, flag, text, func, cmatches, glist, tlist) \
857 cmatches = completion_matches (text, func); \
858 tlist = completions_to_stringlist (cmatches); \
859 glist = append_stringlist (glist, tlist); \
860 free_array (cmatches); \
861 free_stringlist (tlist); \
865 /* Functions to generate lists of matches from the actions member of CS. */
868 gen_action_completions (cs, text)
872 STRINGLIST *ret, *tmatches;
873 char **cmatches; /* from completion_matches ... */
876 ret = tmatches = (STRINGLIST *)NULL;
879 GEN_COMPS (flags, CA_ALIAS, &it_aliases, text, ret, tmatches);
880 GEN_COMPS (flags, CA_ARRAYVAR, &it_arrayvars, text, ret, tmatches);
881 GEN_COMPS (flags, CA_BINDING, &it_bindings, text, ret, tmatches);
882 GEN_COMPS (flags, CA_BUILTIN, &it_builtins, text, ret, tmatches);
883 GEN_COMPS (flags, CA_DISABLED, &it_disabled, text, ret, tmatches);
884 GEN_COMPS (flags, CA_ENABLED, &it_enabled, text, ret, tmatches);
885 GEN_COMPS (flags, CA_EXPORT, &it_exports, text, ret, tmatches);
886 GEN_COMPS (flags, CA_FUNCTION, &it_functions, text, ret, tmatches);
887 GEN_COMPS (flags, CA_HOSTNAME, &it_hostnames, text, ret, tmatches);
888 GEN_COMPS (flags, CA_JOB, &it_jobs, text, ret, tmatches);
889 GEN_COMPS (flags, CA_KEYWORD, &it_keywords, text, ret, tmatches);
890 GEN_COMPS (flags, CA_RUNNING, &it_running, text, ret, tmatches);
891 GEN_COMPS (flags, CA_SETOPT, &it_setopts, text, ret, tmatches);
892 GEN_COMPS (flags, CA_SHOPT, &it_shopts, text, ret, tmatches);
893 GEN_COMPS (flags, CA_SIGNAL, &it_signals, text, ret, tmatches);
894 GEN_COMPS (flags, CA_STOPPED, &it_stopped, text, ret, tmatches);
895 GEN_COMPS (flags, CA_VARIABLE, &it_variables, text, ret, tmatches);
897 GEN_XCOMPS(flags, CA_COMMAND, text, command_word_completion_function, cmatches, ret, tmatches);
898 GEN_XCOMPS(flags, CA_FILE, text, pcomp_filename_completion_function, cmatches, ret, tmatches);
899 GEN_XCOMPS(flags, CA_USER, text, username_completion_function, cmatches, ret, tmatches);
901 /* And lastly, the special case for directories */
902 if (flags & CA_DIRECTORY)
904 cmatches = bash_directory_completion_matches (text);
905 tmatches = completions_to_stringlist (cmatches);
906 ret = append_stringlist (ret, tmatches);
907 free_array (cmatches);
908 free_stringlist (tmatches);
914 /* Generate a list of matches for CS->globpat. Unresolved: should this use
915 TEXT as a match prefix, or just go without? Currently, the code does not
916 use TEXT, just globs CS->globpat and returns the results. If we do decide
917 to use TEXT, we should call quote_string_for_globbing before the call to
920 gen_globpat_matches (cs, text)
927 sl = alloc_stringlist (0);
928 sl->list = glob_filename (cs->globpat);
929 if (GLOB_FAILED (sl->list))
930 sl->list = (char **)NULL;
932 sl->list_len = sl->list_size = array_len (sl->list);
936 /* Perform the shell word expansions on CS->words and return the results.
937 Again, this ignores TEXT. */
939 gen_wordlist_matches (cs, text)
947 if (cs->words == 0 || cs->words[0] == '\0')
948 return ((STRINGLIST *)NULL);
950 /* This used to be a simple expand_string(cs->words, 0), but that won't
951 do -- there's no way to split a simple list into individual words
952 that way, since the shell semantics say that word splitting is done
953 only on the results of expansion. */
954 l = split_at_delims (cs->words, strlen (cs->words), (char *)NULL, -1, (int *)NULL, (int *)NULL);
956 return ((STRINGLIST *)NULL);
957 /* This will jump back to the top level if the expansion fails... */
958 l2 = expand_words_shellexp (l);
961 nw = list_length (l2);
962 sl = alloc_stringlist (nw + 1);
963 tlen = STRLEN (text);
965 for (nw = 0, l = l2; l; l = l->next)
967 if (tlen == 0 || STREQN (l->word->word, text, tlen))
968 sl->list[nw++] = STRDUP (l->word->word);
970 sl->list[sl->list_len = nw] = (char *)NULL;
978 bind_comp_words (lwords)
983 v = find_variable ("COMP_WORDS");
985 v = make_new_array_variable ("COMP_WORDS");
987 VUNSETATTR (v, att_readonly);
988 if (array_p (v) == 0)
989 v = convert_var_to_array (v);
990 v = assign_array_var_from_word_list (v, lwords);
993 #endif /* ARRAY_VARS */
996 bind_compfunc_variables (line, ind, lwords, cw, exported)
1006 /* Set the variables that the function expects while it executes. Maybe
1007 these should be in the function environment (temporary_env). */
1008 v = bind_variable ("COMP_LINE", line);
1010 VSETATTR(v, att_exported);
1012 value = inttostr (ind, ibuf, 32);
1013 v = bind_int_variable ("COMP_POINT", value);
1015 VSETATTR(v, att_exported);
1017 /* Since array variables can't be exported, we don't bother making the
1022 v = bind_comp_words (lwords);
1023 value = inttostr (cw, ibuf, 32);
1024 bind_int_variable ("COMP_CWORD", value);
1028 array_needs_making = 1;
1032 unbind_compfunc_variables (exported)
1035 makunbound ("COMP_LINE", shell_variables);
1036 makunbound ("COMP_POINT", shell_variables);
1038 makunbound ("COMP_WORDS", shell_variables);
1039 makunbound ("COMP_CWORD", shell_variables);
1042 array_needs_making = 1;
1045 /* Build the list of words to pass to a function or external command
1046 as arguments. When the function or command is invoked,
1048 $0 == function or command being invoked
1050 $2 = word to be completed (possibly null)
1053 Functions can access all of the words in the current command line
1054 with the COMP_WORDS array. External commands cannot. */
1057 build_arg_list (cmd, text, lwords, ind)
1063 WORD_LIST *ret, *cl, *l;
1067 ret = (WORD_LIST *)NULL;
1068 w = make_word (cmd);
1069 ret = make_word_list (w, (WORD_LIST *)NULL);
1071 w = (lwords && lwords->word) ? copy_word (lwords->word) : make_word ("");
1072 cl = ret->next = make_word_list (w, (WORD_LIST *)NULL);
1074 w = make_word (text);
1075 cl->next = make_word_list (w, (WORD_LIST *)NULL);
1078 /* Search lwords for current word */
1079 for (l = lwords, i = 1; l && i < ind-1; l = l->next, i++)
1081 w = (l && l->word) ? copy_word (l->word) : make_word ("");
1082 cl->next = make_word_list (w, (WORD_LIST *)NULL);
1087 /* Build a command string with
1088 $0 == cs->funcname (function to execute for completion list)
1089 $1 == command name (command being completed)
1090 $2 = word to be completed (possibly null)
1092 and run in the current shell. The function should put its completion
1093 list into the array variable COMPREPLY. We build a STRINGLIST
1094 from the results and return it.
1096 Since the shell function should return its list of matches in an array
1097 variable, this does nothing if arrays are not compiled into the shell. */
1100 gen_shell_function_matches (cs, text, line, ind, lwords, nw, cw)
1112 #if defined (ARRAY_VARS)
1116 funcname = cs->funcname;
1117 f = find_function (funcname);
1120 fprintf (stderr, "gen_shell_function_matches: function `%s' not found\n", funcname);
1123 return ((STRINGLIST *)NULL);
1126 #if !defined (ARRAY_VARS)
1127 return ((STRINGLIST *)NULL);
1130 /* We pass cw - 1 because command_line_to_word_list returns indices that are
1131 1-based, while bash arrays are 0-based. */
1132 bind_compfunc_variables (line, ind, lwords, cw - 1, 0);
1134 cmdlist = build_arg_list (funcname, text, lwords, cw);
1136 fval = execute_shell_function (f, cmdlist);
1138 /* Now clean up and destroy everything. */
1139 dispose_words (cmdlist);
1140 unbind_compfunc_variables (0);
1142 /* The list of completions is returned in the array variable COMPREPLY. */
1143 v = find_variable ("COMPREPLY");
1145 return ((STRINGLIST *)NULL);
1146 if (array_p (v) == 0)
1147 v = convert_var_to_array (v);
1150 if (a == 0 || array_empty (a))
1151 sl = (STRINGLIST *)NULL;
1154 /* XXX - should we filter the list of completions so only those matching
1155 TEXT are returned? Right now, we do not. */
1156 sl = alloc_stringlist (0);
1157 sl->list = array_to_argv (a);
1158 sl->list_len = sl->list_size = array_num_elements (a);
1161 /* XXX - should we unbind COMPREPLY here? */
1162 makunbound ("COMPREPLY", shell_variables);
1168 /* Build a command string with
1169 $0 == cs->command (command to execute for completion list)
1170 $1 == command name (command being completed)
1171 $2 = word to be completed (possibly null)
1173 and run in with command substitution. Parse the results, one word
1174 per line, with backslashes allowed to escape newlines. Build a
1175 STRINGLIST from the results and return it. */
1178 gen_command_matches (cs, text, line, ind, lwords, nw, cw)
1185 char *csbuf, *cscmd, *t;
1186 int cmdlen, cmdsize, n, ws, we;
1187 WORD_LIST *cmdlist, *cl;
1190 bind_compfunc_variables (line, ind, lwords, cw, 1);
1191 cmdlist = build_arg_list (cs->command, text, lwords, cw);
1193 /* Estimate the size needed for the buffer. */
1194 n = strlen (cs->command);
1196 for (cl = cmdlist->next; cl; cl = cl->next)
1197 cmdsize += STRLEN (cl->word->word) + 3;
1200 /* allocate the string for the command and fill it in. */
1201 cscmd = xmalloc (cmdsize + 1);
1203 strcpy (cscmd, cs->command); /* $0 */
1205 cscmd[cmdlen++] = ' ';
1206 for (cl = cmdlist->next; cl; cl = cl->next) /* $1, $2, $3, ... */
1208 t = single_quote (cl->word->word ? cl->word->word : "");
1210 RESIZE_MALLOCED_BUFFER (cscmd, cmdlen, n + 2, cmdsize, 64);
1211 strcpy (cscmd + cmdlen, t);
1214 cscmd[cmdlen++] = ' ';
1217 cscmd[cmdlen] = '\0';
1219 csbuf = command_substitute (cscmd, 0);
1221 /* Now clean up and destroy everything. */
1222 dispose_words (cmdlist);
1224 unbind_compfunc_variables (1);
1226 if (csbuf == 0 || *csbuf == '\0')
1229 return ((STRINGLIST *)NULL);
1232 /* Now break CSBUF up at newlines, with backslash allowed to escape a
1233 newline, and put the individual words into a STRINGLIST. */
1234 sl = alloc_stringlist (16);
1235 for (ws = 0; csbuf[ws]; )
1238 while (csbuf[we] && csbuf[we] != '\n')
1240 if (csbuf[we] == '\\' && csbuf[we+1] == '\n')
1244 t = substring (csbuf, ws, we);
1245 if (sl->list_len >= sl->list_size - 1)
1246 realloc_stringlist (sl, sl->list_size + 16);
1247 sl->list[sl->list_len++] = t;
1248 while (csbuf[we] == '\n') we++;
1251 sl->list[sl->list_len] = (char *)NULL;
1258 command_line_to_word_list (line, llen, sentinel, nwp, cwp)
1260 int llen, sentinel, *nwp, *cwp;
1265 delims = "()<>;&| \t\n"; /* shell metacharacters break words */
1266 ret = split_at_delims (line, llen, delims, sentinel, nwp, cwp);
1270 /* Evaluate COMPSPEC *cs and return all matches for WORD. */
1273 gen_compspec_completions (cs, cmd, word, start, end)
1279 STRINGLIST *ret, *tmatches;
1284 debug_printf ("programmable_completions (%s, %s, %d, %d)", cmd, word, start, end);
1285 debug_printf ("programmable_completions: %s -> 0x%x", cmd, (int)cs);
1286 ret = gen_action_completions (cs, word);
1287 if (ret && progcomp_debug)
1289 debug_printf ("gen_action_completions (0x%x, %s) -->", (int)cs, word);
1290 print_stringlist (ret, "\t");
1294 /* Now we start generating completions based on the other members of CS. */
1297 tmatches = gen_globpat_matches (cs, word);
1302 debug_printf ("gen_globpat_matches (0x%x, %s) -->", (int)cs, word);
1303 print_stringlist (tmatches, "\t");
1306 ret = append_stringlist (ret, tmatches);
1307 free_stringlist (tmatches);
1308 rl_filename_completion_desired = 1;
1314 tmatches = gen_wordlist_matches (cs, word);
1319 debug_printf ("gen_wordlist_matches (0x%x, %s) -->", (int)cs, word);
1320 print_stringlist (tmatches, "\t");
1323 ret = append_stringlist (ret, tmatches);
1324 free_stringlist (tmatches);
1328 lwords = (WORD_LIST *)NULL;
1329 line = (char *)NULL;
1330 if (cs->command || cs->funcname)
1332 /* If we have a command or function to execute, we need to first break
1333 the command line into individual words, find the number of words,
1334 and find the word in the list containing the word to be completed. */
1335 line = substring (rl_line_buffer, start, end);
1338 debug_printf ("command_line_to_word_list (%s, %d, %d, 0x%x, 0x%x)",
1339 line, llen, rl_point - start, &nw, &cw);
1340 lwords = command_line_to_word_list (line, llen, rl_point - start, &nw, &cw);
1341 if (lwords == 0 && llen > 0)
1342 debug_printf ("ERROR: command_line_to_word_list returns NULL");
1343 else if (progcomp_debug)
1345 debug_printf ("command_line_to_word_list -->");
1347 print_word_list (lwords, "!");
1356 tmatches = gen_shell_function_matches (cs, word, line, rl_point - start, lwords, nw, cw);
1361 debug_printf ("gen_shell_function_matches (0x%x, %s, 0x%x, %d, %d) -->", (int)cs, word, lwords, nw, cw);
1362 print_stringlist (tmatches, "\t");
1365 ret = append_stringlist (ret, tmatches);
1366 free_stringlist (tmatches);
1372 tmatches = gen_command_matches (cs, word, line, rl_point - start, lwords, nw, cw);
1377 debug_printf ("gen_command_matches (0x%x, %s, 0x%x, %d, %d) -->", (int)cs, word, lwords, nw, cw);
1378 print_stringlist (tmatches, "\t");
1381 ret = append_stringlist (ret, tmatches);
1382 free_stringlist (tmatches);
1386 if (cs->command || cs->funcname)
1389 dispose_words (lwords);
1395 tmatches = filter_stringlist (ret, cs->filterpat, word);
1398 debug_printf ("filter_stringlist (0x%x, %s, %s) -->", ret, cs->filterpat, word);
1399 print_stringlist (tmatches, "\t");
1402 if (ret && ret != tmatches)
1410 if (cs->prefix || cs->suffix)
1411 ret = prefix_suffix_stringlist (ret, cs->prefix, cs->suffix);
1416 /* The driver function for the programmable completion code. Returns a list
1417 of matches for WORD, which is an argument to command CMD. START and END
1418 bound the command currently being completed in rl_line_buffer. */
1420 programmable_completions (cmd, word, start, end, foundp)
1422 int start, end, *foundp;
1426 char **rmatches, *t;
1428 /* We look at the basename of CMD if the full command does not have
1429 an associated COMPSPEC. */
1430 cs = find_compspec (cmd);
1433 t = strrchr (cmd, '/');
1435 cs = find_compspec (++t);
1441 return ((char **)NULL);
1444 /* Signal the caller that we found a COMPSPEC for this command. */
1448 ret = gen_compspec_completions (cs, cmd, word, start, end);
1452 rmatches = ret->list;
1456 rmatches = (char **)NULL;
1461 #endif /* PROGRAMMABLE_COMPLETION */