1 /* pcomplete.c - functions to generate lists of matches for programmable completion. */
3 /* Copyright (C) 1999-2009 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bash is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bash. If not, see <http://www.gnu.org/licenses/>.
23 #if defined (PROGRAMMABLE_COMPLETION)
25 #include "bashtypes.h"
26 #include "posixstat.h"
28 #if defined (HAVE_UNISTD_H)
34 #if defined (PREFER_STDARG)
45 #include "pcomplete.h"
48 #include "execute_cmd.h"
51 #if defined (JOB_CONTROL)
60 #include "builtins/common.h"
62 #include <glob/glob.h>
63 #include <glob/strmatch.h>
65 #include <readline/rlconf.h>
66 #include <readline/readline.h>
67 #include <readline/history.h>
69 #define PCOMP_RETRYFAIL 256
74 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
76 typedef SHELL_VAR
**SVFUNC ();
79 extern char *strpbrk
__P((char *, char *));
82 extern int array_needs_making
;
83 extern STRING_INT_ALIST word_token_alist
[];
84 extern char *signal_names
[];
87 #if defined (PREFER_STDARG)
88 static void debug_printf (const char *, ...) __attribute__((__format__ (printf
, 1, 2)));
92 static int it_init_joblist
__P((ITEMLIST
*, int));
94 static int it_init_aliases
__P((ITEMLIST
*));
95 static int it_init_arrayvars
__P((ITEMLIST
*));
96 static int it_init_bindings
__P((ITEMLIST
*));
97 static int it_init_builtins
__P((ITEMLIST
*));
98 static int it_init_disabled
__P((ITEMLIST
*));
99 static int it_init_enabled
__P((ITEMLIST
*));
100 static int it_init_exported
__P((ITEMLIST
*));
101 static int it_init_functions
__P((ITEMLIST
*));
102 static int it_init_hostnames
__P((ITEMLIST
*));
103 static int it_init_jobs
__P((ITEMLIST
*));
104 static int it_init_running
__P((ITEMLIST
*));
105 static int it_init_stopped
__P((ITEMLIST
*));
106 static int it_init_keywords
__P((ITEMLIST
*));
107 static int it_init_signals
__P((ITEMLIST
*));
108 static int it_init_variables
__P((ITEMLIST
*));
109 static int it_init_setopts
__P((ITEMLIST
*));
110 static int it_init_shopts
__P((ITEMLIST
*));
112 static int shouldexp_filterpat
__P((char *));
113 static char *preproc_filterpat
__P((char *, char *));
115 static void init_itemlist_from_varlist
__P((ITEMLIST
*, SVFUNC
*));
117 static STRINGLIST
*gen_matches_from_itemlist
__P((ITEMLIST
*, const char *));
118 static STRINGLIST
*gen_action_completions
__P((COMPSPEC
*, const char *));
119 static STRINGLIST
*gen_globpat_matches
__P((COMPSPEC
*, const char *));
120 static STRINGLIST
*gen_wordlist_matches
__P((COMPSPEC
*, const char *));
121 static STRINGLIST
*gen_shell_function_matches
__P((COMPSPEC
*, const char *,
122 char *, int, WORD_LIST
*,
124 static STRINGLIST
*gen_command_matches
__P((COMPSPEC
*, const char *, char *,
125 int, WORD_LIST
*, int, int));
127 static STRINGLIST
*gen_progcomp_completions
__P((const char *, const char *,
129 int, int, int *, int *,
132 static char *pcomp_filename_completion_function
__P((const char *, int));
134 #if defined (ARRAY_VARS)
135 static SHELL_VAR
*bind_comp_words
__P((WORD_LIST
*));
137 static void bind_compfunc_variables
__P((char *, int, WORD_LIST
*, int, int));
138 static void unbind_compfunc_variables
__P((int));
139 static WORD_LIST
*build_arg_list
__P((char *, const char *, WORD_LIST
*, int));
140 static WORD_LIST
*command_line_to_word_list
__P((char *, int, int, int *, int *));
143 static int progcomp_debug
= 0;
146 int prog_completion_enabled
= 1;
148 /* These are used to manage the arrays of strings for possible completions. */
149 ITEMLIST it_aliases
= { 0, it_init_aliases
, (STRINGLIST
*)0 };
150 ITEMLIST it_arrayvars
= { LIST_DYNAMIC
, it_init_arrayvars
, (STRINGLIST
*)0 };
151 ITEMLIST it_bindings
= { 0, it_init_bindings
, (STRINGLIST
*)0 };
152 ITEMLIST it_builtins
= { 0, it_init_builtins
, (STRINGLIST
*)0 };
153 ITEMLIST it_commands
= { LIST_DYNAMIC
}; /* unused */
154 ITEMLIST it_directories
= { LIST_DYNAMIC
}; /* unused */
155 ITEMLIST it_disabled
= { 0, it_init_disabled
, (STRINGLIST
*)0 };
156 ITEMLIST it_enabled
= { 0, it_init_enabled
, (STRINGLIST
*)0 };
157 ITEMLIST it_exports
= { LIST_DYNAMIC
, it_init_exported
, (STRINGLIST
*)0 };
158 ITEMLIST it_files
= { LIST_DYNAMIC
}; /* unused */
159 ITEMLIST it_functions
= { 0, it_init_functions
, (STRINGLIST
*)0 };
160 ITEMLIST it_hostnames
= { LIST_DYNAMIC
, it_init_hostnames
, (STRINGLIST
*)0 };
161 ITEMLIST it_groups
= { LIST_DYNAMIC
}; /* unused */
162 ITEMLIST it_jobs
= { LIST_DYNAMIC
, it_init_jobs
, (STRINGLIST
*)0 };
163 ITEMLIST it_keywords
= { 0, it_init_keywords
, (STRINGLIST
*)0 };
164 ITEMLIST it_running
= { LIST_DYNAMIC
, it_init_running
, (STRINGLIST
*)0 };
165 ITEMLIST it_services
= { LIST_DYNAMIC
}; /* unused */
166 ITEMLIST it_setopts
= { 0, it_init_setopts
, (STRINGLIST
*)0 };
167 ITEMLIST it_shopts
= { 0, it_init_shopts
, (STRINGLIST
*)0 };
168 ITEMLIST it_signals
= { 0, it_init_signals
, (STRINGLIST
*)0 };
169 ITEMLIST it_stopped
= { LIST_DYNAMIC
, it_init_stopped
, (STRINGLIST
*)0 };
170 ITEMLIST it_users
= { LIST_DYNAMIC
}; /* unused */
171 ITEMLIST it_variables
= { LIST_DYNAMIC
, it_init_variables
, (STRINGLIST
*)0 };
173 COMPSPEC
*pcomp_curcs
;
174 const char *pcomp_curcmd
;
179 #if defined (PREFER_STDARG)
180 debug_printf (const char *format
, ...)
182 debug_printf (format
, va_alist
)
189 if (progcomp_debug
== 0)
192 SH_VA_START (args
, format
);
194 fprintf (stdout
, "DEBUG: ");
195 vfprintf (stdout
, format
, args
);
196 fprintf (stdout
, "\n");
204 /* Functions to manage the item lists */
207 set_itemlist_dirty (it
)
210 it
->flags
|= LIST_DIRTY
;
214 initialize_itemlist (itp
)
217 (*itp
->list_getter
) (itp
);
218 itp
->flags
|= LIST_INITIALIZED
;
219 itp
->flags
&= ~LIST_DIRTY
;
231 if ((itp
->flags
& (LIST_DONTFREEMEMBERS
|LIST_DONTFREE
)) == 0)
232 strvec_flush (sl
->list
);
233 if ((itp
->flags
& LIST_DONTFREE
) == 0)
237 itp
->slist
= (STRINGLIST
*)NULL
;
238 itp
->flags
&= ~(LIST_DONTFREE
|LIST_DONTFREEMEMBERS
|LIST_INITIALIZED
|LIST_DIRTY
);
243 shouldexp_filterpat (s
)
248 for (p
= s
; p
&& *p
; p
++)
258 /* Replace any instance of `&' in PAT with TEXT. Backslash may be used to
259 quote a `&' and inhibit substitution. Returns a new string. This just
260 calls stringlib.c:strcreplace(). */
262 preproc_filterpat (pat
, text
)
268 ret
= strcreplace (pat
, '&', text
, 1);
272 /* Remove any match of FILTERPAT from SL. A `&' in FILTERPAT is replaced by
273 TEXT. A leading `!' in FILTERPAT negates the pattern; in this case
274 any member of SL->list that does *not* match will be removed. This returns
275 a new STRINGLIST with the matching members of SL *copied*. Any
276 non-matching members of SL->list are *freed*. */
278 filter_stringlist (sl
, filterpat
, text
)
280 char *filterpat
, *text
;
286 if (sl
== 0 || sl
->list
== 0 || sl
->list_len
== 0)
289 npat
= shouldexp_filterpat (filterpat
) ? preproc_filterpat (filterpat
, text
) : filterpat
;
291 not = (npat
[0] == '!');
292 t
= not ? npat
+ 1 : npat
;
294 ret
= strlist_create (sl
->list_size
);
295 for (i
= 0; i
< sl
->list_len
; i
++)
297 m
= strmatch (t
, sl
->list
[i
], FNMATCH_EXTFLAG
);
298 if ((not && m
== FNM_NOMATCH
) || (not == 0 && m
!= FNM_NOMATCH
))
301 ret
->list
[ret
->list_len
++] = sl
->list
[i
];
304 ret
->list
[ret
->list_len
] = (char *)NULL
;
305 if (npat
!= filterpat
)
311 /* Turn an array of strings returned by rl_completion_matches into a STRINGLIST.
312 This understands how rl_completion_matches sets matches[0] (the lcd of the
313 strings in the list, unless it's the only match). */
315 completions_to_stringlist (matches
)
321 mlen
= (matches
== 0) ? 0 : strvec_len (matches
);
322 sl
= strlist_create (mlen
+ 1);
324 if (matches
== 0 || matches
[0] == 0)
329 sl
->list
[0] = STRDUP (matches
[0]);
330 sl
->list
[sl
->list_len
= 1] = (char *)NULL
;
334 for (i
= 1, n
= 0; i
< mlen
; i
++, n
++)
335 sl
->list
[n
] = STRDUP (matches
[i
]);
337 sl
->list
[n
] = (char *)NULL
;
342 /* Functions to manage the various ITEMLISTs that we populate internally.
343 The caller is responsible for setting ITP->flags correctly. */
346 it_init_aliases (itp
)
350 alias_t
**alias_list
;
354 alias_list
= all_aliases ();
357 itp
->slist
= (STRINGLIST
*)NULL
;
360 for (n
= 0; alias_list
[n
]; n
++)
362 sl
= strlist_create (n
+1);
363 for (i
= 0; i
< n
; i
++)
364 sl
->list
[i
] = STRDUP (alias_list
[i
]->name
);
365 sl
->list
[n
] = (char *)NULL
;
366 sl
->list_size
= sl
->list_len
= n
;
369 itp
->slist
= (STRINGLIST
*)NULL
;
375 init_itemlist_from_varlist (itp
, svfunc
)
383 vlist
= (*svfunc
) ();
386 itp
->slist
= (STRINGLIST
*)NULL
;
389 for (n
= 0; vlist
[n
]; n
++)
391 sl
= strlist_create (n
+1);
392 for (i
= 0; i
< n
; i
++)
393 sl
->list
[i
] = savestring (vlist
[i
]->name
);
394 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
399 it_init_arrayvars (itp
)
402 #if defined (ARRAY_VARS)
403 init_itemlist_from_varlist (itp
, all_array_variables
);
411 it_init_bindings (itp
)
417 /* rl_funmap_names allocates blist, but not its members */
418 blist
= (char **)rl_funmap_names (); /* XXX fix const later */
419 sl
= strlist_create (0);
422 sl
->list_len
= strvec_len (sl
->list
);
423 itp
->flags
|= LIST_DONTFREEMEMBERS
;
430 it_init_builtins (itp
)
436 sl
= strlist_create (num_shell_builtins
);
437 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
438 if (shell_builtins
[i
].function
)
439 sl
->list
[n
++] = shell_builtins
[i
].name
;
440 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
441 itp
->flags
|= LIST_DONTFREEMEMBERS
;
447 it_init_enabled (itp
)
453 sl
= strlist_create (num_shell_builtins
);
454 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
456 if (shell_builtins
[i
].function
&& (shell_builtins
[i
].flags
& BUILTIN_ENABLED
))
457 sl
->list
[n
++] = shell_builtins
[i
].name
;
459 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
460 itp
->flags
|= LIST_DONTFREEMEMBERS
;
466 it_init_disabled (itp
)
472 sl
= strlist_create (num_shell_builtins
);
473 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
475 if (shell_builtins
[i
].function
&& ((shell_builtins
[i
].flags
& BUILTIN_ENABLED
) == 0))
476 sl
->list
[n
++] = shell_builtins
[i
].name
;
478 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
479 itp
->flags
|= LIST_DONTFREEMEMBERS
;
485 it_init_exported (itp
)
488 init_itemlist_from_varlist (itp
, all_exported_variables
);
493 it_init_functions (itp
)
496 init_itemlist_from_varlist (itp
, all_visible_functions
);
501 it_init_hostnames (itp
)
506 sl
= strlist_create (0);
507 sl
->list
= get_hostname_list ();
508 sl
->list_len
= sl
->list
? strvec_len (sl
->list
) : 0;
509 sl
->list_size
= sl
->list_len
;
511 itp
->flags
|= LIST_DONTFREEMEMBERS
|LIST_DONTFREE
;
516 it_init_joblist (itp
, jstate
)
520 #if defined (JOB_CONTROL)
526 JOB_STATE ws
; /* wanted state */
531 else if (jstate
== 1)
534 sl
= strlist_create (js
.j_jobslots
);
535 for (i
= js
.j_jobslots
- 1; i
>= 0; i
--)
537 j
= get_job_by_jid (i
);
541 if (jstate
== -1 || JOBSTATE(i
) == ws
)
543 s
= savestring (p
->command
);
544 t
= strpbrk (s
, " \t\n");
547 sl
->list
[sl
->list_len
++] = s
;
552 itp
->slist
= (STRINGLIST
*)NULL
;
561 return (it_init_joblist (itp
, -1));
565 it_init_running (itp
)
568 return (it_init_joblist (itp
, 0));
572 it_init_stopped (itp
)
575 return (it_init_joblist (itp
, 1));
579 it_init_keywords (itp
)
585 for (n
= 0; word_token_alist
[n
].word
; n
++)
587 sl
= strlist_create (n
);
588 for (i
= 0; i
< n
; i
++)
589 sl
->list
[i
] = word_token_alist
[i
].word
;
590 sl
->list
[sl
->list_len
= i
] = (char *)NULL
;
591 itp
->flags
|= LIST_DONTFREEMEMBERS
;
597 it_init_signals (itp
)
602 sl
= strlist_create (0);
603 sl
->list
= signal_names
;
604 sl
->list_len
= strvec_len (sl
->list
);
605 itp
->flags
|= LIST_DONTFREE
;
611 it_init_variables (itp
)
614 init_itemlist_from_varlist (itp
, all_visible_variables
);
619 it_init_setopts (itp
)
624 sl
= strlist_create (0);
625 sl
->list
= get_minus_o_opts ();
626 sl
->list_len
= strvec_len (sl
->list
);
628 itp
->flags
|= LIST_DONTFREEMEMBERS
;
638 sl
= strlist_create (0);
639 sl
->list
= get_shopt_options ();
640 sl
->list_len
= strvec_len (sl
->list
);
642 itp
->flags
|= LIST_DONTFREEMEMBERS
;
646 /* Generate a list of all matches for TEXT using the STRINGLIST in itp->slist
647 as the list of possibilities. If the itemlist has been marked dirty or
648 it should be regenerated every time, destroy the old STRINGLIST and make a
649 new one before trying the match. TEXT is dequoted before attempting a
652 gen_matches_from_itemlist (itp
, text
)
656 STRINGLIST
*ret
, *sl
;
660 if ((itp
->flags
& (LIST_DIRTY
|LIST_DYNAMIC
)) ||
661 (itp
->flags
& LIST_INITIALIZED
) == 0)
663 if (itp
->flags
& (LIST_DIRTY
|LIST_DYNAMIC
))
664 clean_itemlist (itp
);
665 if ((itp
->flags
& LIST_INITIALIZED
) == 0)
666 initialize_itemlist (itp
);
669 return ((STRINGLIST
*)NULL
);
670 ret
= strlist_create (itp
->slist
->list_len
+1);
673 ntxt
= bash_dequote_text (text
);
674 tlen
= STRLEN (ntxt
);
676 for (i
= n
= 0; i
< sl
->list_len
; i
++)
678 if (tlen
== 0 || STREQN (sl
->list
[i
], ntxt
, tlen
))
679 ret
->list
[n
++] = STRDUP (sl
->list
[i
]);
681 ret
->list
[ret
->list_len
= n
] = (char *)NULL
;
687 /* A wrapper for rl_filename_completion_function that dequotes the filename
688 before attempting completions. */
690 pcomp_filename_completion_function (text
, state
)
694 static char *dfn
; /* dequoted filename */
700 /* remove backslashes quoting special characters in filenames. */
702 if (RL_ISSTATE (RL_STATE_COMPLETING
) && rl_filename_dequoting_function
)
704 if (rl_filename_dequoting_function
)
707 /* Use rl_completion_quote_character because any single or
708 double quotes have been removed by the time TEXT makes it
709 here, and we don't want to remove backslashes inside
711 dfn
= (*rl_filename_dequoting_function
) ((char *)text
, rl_completion_quote_character
);
714 dfn
= savestring (text
);
717 return (rl_filename_completion_function (dfn
, state
));
720 #define GEN_COMPS(bmap, flag, it, text, glist, tlist) \
724 tlist = gen_matches_from_itemlist (it, text); \
727 glist = strlist_append (glist, tlist); \
728 strlist_dispose (tlist); \
733 #define GEN_XCOMPS(bmap, flag, text, func, cmatches, glist, tlist) \
737 cmatches = rl_completion_matches (text, func); \
738 tlist = completions_to_stringlist (cmatches); \
739 glist = strlist_append (glist, tlist); \
740 strvec_dispose (cmatches); \
741 strlist_dispose (tlist); \
745 /* Functions to generate lists of matches from the actions member of CS. */
748 gen_action_completions (cs
, text
)
752 STRINGLIST
*ret
, *tmatches
;
753 char **cmatches
; /* from rl_completion_matches ... */
756 ret
= tmatches
= (STRINGLIST
*)NULL
;
759 GEN_COMPS (flags
, CA_ALIAS
, &it_aliases
, text
, ret
, tmatches
);
760 GEN_COMPS (flags
, CA_ARRAYVAR
, &it_arrayvars
, text
, ret
, tmatches
);
761 GEN_COMPS (flags
, CA_BINDING
, &it_bindings
, text
, ret
, tmatches
);
762 GEN_COMPS (flags
, CA_BUILTIN
, &it_builtins
, text
, ret
, tmatches
);
763 GEN_COMPS (flags
, CA_DISABLED
, &it_disabled
, text
, ret
, tmatches
);
764 GEN_COMPS (flags
, CA_ENABLED
, &it_enabled
, text
, ret
, tmatches
);
765 GEN_COMPS (flags
, CA_EXPORT
, &it_exports
, text
, ret
, tmatches
);
766 GEN_COMPS (flags
, CA_FUNCTION
, &it_functions
, text
, ret
, tmatches
);
767 GEN_COMPS (flags
, CA_HOSTNAME
, &it_hostnames
, text
, ret
, tmatches
);
768 GEN_COMPS (flags
, CA_JOB
, &it_jobs
, text
, ret
, tmatches
);
769 GEN_COMPS (flags
, CA_KEYWORD
, &it_keywords
, text
, ret
, tmatches
);
770 GEN_COMPS (flags
, CA_RUNNING
, &it_running
, text
, ret
, tmatches
);
771 GEN_COMPS (flags
, CA_SETOPT
, &it_setopts
, text
, ret
, tmatches
);
772 GEN_COMPS (flags
, CA_SHOPT
, &it_shopts
, text
, ret
, tmatches
);
773 GEN_COMPS (flags
, CA_SIGNAL
, &it_signals
, text
, ret
, tmatches
);
774 GEN_COMPS (flags
, CA_STOPPED
, &it_stopped
, text
, ret
, tmatches
);
775 GEN_COMPS (flags
, CA_VARIABLE
, &it_variables
, text
, ret
, tmatches
);
777 GEN_XCOMPS(flags
, CA_COMMAND
, text
, command_word_completion_function
, cmatches
, ret
, tmatches
);
778 GEN_XCOMPS(flags
, CA_FILE
, text
, pcomp_filename_completion_function
, cmatches
, ret
, tmatches
);
779 GEN_XCOMPS(flags
, CA_USER
, text
, rl_username_completion_function
, cmatches
, ret
, tmatches
);
780 GEN_XCOMPS(flags
, CA_GROUP
, text
, bash_groupname_completion_function
, cmatches
, ret
, tmatches
);
781 GEN_XCOMPS(flags
, CA_SERVICE
, text
, bash_servicename_completion_function
, cmatches
, ret
, tmatches
);
783 /* And lastly, the special case for directories */
784 if (flags
& CA_DIRECTORY
)
786 rl_completion_mark_symlink_dirs
= 1; /* override user preference */
787 cmatches
= bash_directory_completion_matches (text
);
788 tmatches
= completions_to_stringlist (cmatches
);
789 ret
= strlist_append (ret
, tmatches
);
790 strvec_dispose (cmatches
);
791 strlist_dispose (tmatches
);
797 /* Generate a list of matches for CS->globpat. Unresolved: should this use
798 TEXT as a match prefix, or just go without? Currently, the code does not
799 use TEXT, just globs CS->globpat and returns the results. If we do decide
800 to use TEXT, we should call quote_string_for_globbing before the call to
803 gen_globpat_matches (cs
, text
)
809 sl
= strlist_create (0);
810 sl
->list
= glob_filename (cs
->globpat
, 0);
811 if (GLOB_FAILED (sl
->list
))
812 sl
->list
= (char **)NULL
;
814 sl
->list_len
= sl
->list_size
= strvec_len (sl
->list
);
818 /* Perform the shell word expansions on CS->words and return the results.
819 Again, this ignores TEXT. */
821 gen_wordlist_matches (cs
, text
)
828 char *ntxt
; /* dequoted TEXT to use in comparisons */
830 if (cs
->words
== 0 || cs
->words
[0] == '\0')
831 return ((STRINGLIST
*)NULL
);
833 /* This used to be a simple expand_string(cs->words, 0), but that won't
834 do -- there's no way to split a simple list into individual words
835 that way, since the shell semantics say that word splitting is done
836 only on the results of expansion. */
837 l
= split_at_delims (cs
->words
, strlen (cs
->words
), (char *)NULL
, -1, 0, (int *)NULL
, (int *)NULL
);
839 return ((STRINGLIST
*)NULL
);
840 /* This will jump back to the top level if the expansion fails... */
841 l2
= expand_words_shellexp (l
);
844 nw
= list_length (l2
);
845 sl
= strlist_create (nw
+ 1);
847 ntxt
= bash_dequote_text (text
);
848 tlen
= STRLEN (ntxt
);
850 for (nw
= 0, l
= l2
; l
; l
= l
->next
)
852 if (tlen
== 0 || STREQN (l
->word
->word
, ntxt
, tlen
))
853 sl
->list
[nw
++] = STRDUP (l
->word
->word
);
855 sl
->list
[sl
->list_len
= nw
] = (char *)NULL
;
865 bind_comp_words (lwords
)
870 v
= find_variable ("COMP_WORDS");
872 v
= make_new_array_variable ("COMP_WORDS");
874 VUNSETATTR (v
, att_readonly
);
875 if (array_p (v
) == 0)
876 v
= convert_var_to_array (v
);
877 v
= assign_array_var_from_word_list (v
, lwords
, 0);
879 VUNSETATTR (v
, att_invisible
);
882 #endif /* ARRAY_VARS */
885 bind_compfunc_variables (line
, ind
, lwords
, cw
, exported
)
891 char ibuf
[INT_STRLEN_BOUND(int) + 1];
895 /* Set the variables that the function expects while it executes. Maybe
896 these should be in the function environment (temporary_env). */
897 v
= bind_variable ("COMP_LINE", line
, 0);
899 VSETATTR(v
, att_exported
);
901 value
= inttostr (ind
, ibuf
, sizeof(ibuf
));
902 v
= bind_int_variable ("COMP_POINT", value
);
904 VSETATTR(v
, att_exported
);
906 value
= inttostr (rl_completion_type
, ibuf
, sizeof (ibuf
));
907 v
= bind_int_variable ("COMP_TYPE", value
);
909 VSETATTR(v
, att_exported
);
911 value
= inttostr (rl_completion_invoking_key
, ibuf
, sizeof (ibuf
));
912 v
= bind_int_variable ("COMP_KEY", value
);
914 VSETATTR(v
, att_exported
);
916 /* Since array variables can't be exported, we don't bother making the
921 v
= bind_comp_words (lwords
);
922 value
= inttostr (cw
, ibuf
, sizeof(ibuf
));
923 bind_int_variable ("COMP_CWORD", value
);
927 array_needs_making
= 1;
931 unbind_compfunc_variables (exported
)
934 unbind_variable ("COMP_LINE");
935 unbind_variable ("COMP_POINT");
936 unbind_variable ("COMP_TYPE");
937 unbind_variable ("COMP_KEY");
939 unbind_variable ("COMP_WORDS");
940 unbind_variable ("COMP_CWORD");
943 array_needs_making
= 1;
946 /* Build the list of words to pass to a function or external command
947 as arguments. When the function or command is invoked,
949 $0 == function or command being invoked
951 $2 == word to be completed (possibly null)
954 Functions can access all of the words in the current command line
955 with the COMP_WORDS array. External commands cannot; they have to
956 make do with the COMP_LINE and COMP_POINT variables. */
959 build_arg_list (cmd
, text
, lwords
, ind
)
965 WORD_LIST
*ret
, *cl
, *l
;
969 ret
= (WORD_LIST
*)NULL
;
971 ret
= make_word_list (w
, (WORD_LIST
*)NULL
);
973 w
= (lwords
&& lwords
->word
) ? copy_word (lwords
->word
) : make_word ("");
974 cl
= ret
->next
= make_word_list (w
, (WORD_LIST
*)NULL
);
976 w
= make_word (text
);
977 cl
->next
= make_word_list (w
, (WORD_LIST
*)NULL
);
980 /* Search lwords for current word */
981 for (l
= lwords
, i
= 1; l
&& i
< ind
-1; l
= l
->next
, i
++)
983 w
= (l
&& l
->word
) ? copy_word (l
->word
) : make_word ("");
984 cl
->next
= make_word_list (w
, (WORD_LIST
*)NULL
);
989 /* Build a command string with
990 $0 == cs->funcname (function to execute for completion list)
991 $1 == command name (command being completed)
992 $2 = word to be completed (possibly null)
994 and run in the current shell. The function should put its completion
995 list into the array variable COMPREPLY. We build a STRINGLIST
996 from the results and return it.
998 Since the shell function should return its list of matches in an array
999 variable, this does nothing if arrays are not compiled into the shell. */
1002 gen_shell_function_matches (cs
, text
, line
, ind
, lwords
, nw
, cw
, foundp
)
1016 sh_parser_state_t ps
;
1017 sh_parser_state_t
* restrict pps
;
1018 #if defined (ARRAY_VARS)
1026 funcname
= cs
->funcname
;
1027 f
= find_function (funcname
);
1030 internal_error (_("completion: function `%s' not found"), funcname
);
1033 return ((STRINGLIST
*)NULL
);
1036 #if !defined (ARRAY_VARS)
1037 return ((STRINGLIST
*)NULL
);
1040 /* We pass cw - 1 because command_line_to_word_list returns indices that are
1041 1-based, while bash arrays are 0-based. */
1042 bind_compfunc_variables (line
, ind
, lwords
, cw
- 1, 0);
1044 cmdlist
= build_arg_list (funcname
, text
, lwords
, cw
);
1047 save_parser_state (pps
);
1048 begin_unwind_frame ("gen-shell-function-matches");
1049 add_unwind_protect (restore_parser_state
, (char *)pps
);
1050 add_unwind_protect (dispose_words
, (char *)cmdlist
);
1051 add_unwind_protect (unbind_compfunc_variables
, (char *)0);
1053 fval
= execute_shell_function (f
, cmdlist
);
1055 discard_unwind_frame ("gen-shell-function-matches");
1056 restore_parser_state (pps
);
1058 found
= fval
!= EX_NOTFOUND
;
1059 if (fval
== EX_RETRYFAIL
)
1060 found
|= PCOMP_RETRYFAIL
;
1064 /* Now clean up and destroy everything. */
1065 dispose_words (cmdlist
);
1066 unbind_compfunc_variables (0);
1068 /* The list of completions is returned in the array variable COMPREPLY. */
1069 v
= find_variable ("COMPREPLY");
1071 return ((STRINGLIST
*)NULL
);
1072 if (array_p (v
) == 0)
1073 v
= convert_var_to_array (v
);
1075 VUNSETATTR (v
, att_invisible
);
1078 if (found
== 0 || (found
& PCOMP_RETRYFAIL
) || a
== 0 || array_empty (a
))
1079 sl
= (STRINGLIST
*)NULL
;
1082 /* XXX - should we filter the list of completions so only those matching
1083 TEXT are returned? Right now, we do not. */
1084 sl
= strlist_create (0);
1085 sl
->list
= array_to_argv (a
);
1086 sl
->list_len
= sl
->list_size
= array_num_elements (a
);
1089 /* XXX - should we unbind COMPREPLY here? */
1090 unbind_variable ("COMPREPLY");
1096 /* Build a command string with
1097 $0 == cs->command (command to execute for completion list)
1098 $1 == command name (command being completed)
1099 $2 = word to be completed (possibly null)
1101 and run in with command substitution. Parse the results, one word
1102 per line, with backslashes allowed to escape newlines. Build a
1103 STRINGLIST from the results and return it. */
1106 gen_command_matches (cs
, text
, line
, ind
, lwords
, nw
, cw
)
1114 char *csbuf
, *cscmd
, *t
;
1115 int cmdlen
, cmdsize
, n
, ws
, we
;
1116 WORD_LIST
*cmdlist
, *cl
;
1120 bind_compfunc_variables (line
, ind
, lwords
, cw
, 1);
1121 cmdlist
= build_arg_list (cs
->command
, text
, lwords
, cw
);
1123 /* Estimate the size needed for the buffer. */
1124 n
= strlen (cs
->command
);
1126 for (cl
= cmdlist
->next
; cl
; cl
= cl
->next
)
1127 cmdsize
+= STRLEN (cl
->word
->word
) + 3;
1130 /* allocate the string for the command and fill it in. */
1131 cscmd
= (char *)xmalloc (cmdsize
+ 1);
1133 strcpy (cscmd
, cs
->command
); /* $0 */
1135 cscmd
[cmdlen
++] = ' ';
1136 for (cl
= cmdlist
->next
; cl
; cl
= cl
->next
) /* $1, $2, $3, ... */
1138 t
= sh_single_quote (cl
->word
->word
? cl
->word
->word
: "");
1140 RESIZE_MALLOCED_BUFFER (cscmd
, cmdlen
, n
+ 2, cmdsize
, 64);
1141 strcpy (cscmd
+ cmdlen
, t
);
1144 cscmd
[cmdlen
++] = ' ';
1147 cscmd
[cmdlen
] = '\0';
1149 tw
= command_substitute (cscmd
, 0);
1150 csbuf
= tw
? tw
->word
: (char *)NULL
;
1151 dispose_word_desc (tw
);
1153 /* Now clean up and destroy everything. */
1154 dispose_words (cmdlist
);
1156 unbind_compfunc_variables (1);
1158 if (csbuf
== 0 || *csbuf
== '\0')
1161 return ((STRINGLIST
*)NULL
);
1164 /* Now break CSBUF up at newlines, with backslash allowed to escape a
1165 newline, and put the individual words into a STRINGLIST. */
1166 sl
= strlist_create (16);
1167 for (ws
= 0; csbuf
[ws
]; )
1170 while (csbuf
[we
] && csbuf
[we
] != '\n')
1172 if (csbuf
[we
] == '\\' && csbuf
[we
+1] == '\n')
1176 t
= substring (csbuf
, ws
, we
);
1177 if (sl
->list_len
>= sl
->list_size
- 1)
1178 strlist_resize (sl
, sl
->list_size
+ 16);
1179 sl
->list
[sl
->list_len
++] = t
;
1180 while (csbuf
[we
] == '\n') we
++;
1183 sl
->list
[sl
->list_len
] = (char *)NULL
;
1190 command_line_to_word_list (line
, llen
, sentinel
, nwp
, cwp
)
1192 int llen
, sentinel
, *nwp
, *cwp
;
1198 delims
= "()<>;&| \t\n"; /* shell metacharacters break words */
1200 delims
= rl_completer_word_break_characters
;
1202 ret
= split_at_delims (line
, llen
, delims
, sentinel
, SD_NOQUOTEDELIM
, nwp
, cwp
);
1206 /* Evaluate COMPSPEC *cs and return all matches for WORD. */
1209 gen_compspec_completions (cs
, cmd
, word
, start
, end
, foundp
)
1216 STRINGLIST
*ret
, *tmatches
;
1218 int llen
, nw
, cw
, found
, foundf
;
1225 debug_printf ("gen_compspec_completions (%s, %s, %d, %d)", cmd
, word
, start
, end
);
1226 debug_printf ("gen_compspec_completions: %s -> %p", cmd
, cs
);
1228 ret
= gen_action_completions (cs
, word
);
1230 if (ret
&& progcomp_debug
)
1232 debug_printf ("gen_action_completions (%p, %s) -->", cs
, word
);
1233 strlist_print (ret
, "\t");
1238 /* Now we start generating completions based on the other members of CS. */
1241 tmatches
= gen_globpat_matches (cs
, word
);
1247 debug_printf ("gen_globpat_matches (%p, %s) -->", cs
, word
);
1248 strlist_print (tmatches
, "\t");
1252 ret
= strlist_append (ret
, tmatches
);
1253 strlist_dispose (tmatches
);
1254 rl_filename_completion_desired
= 1;
1260 tmatches
= gen_wordlist_matches (cs
, word
);
1266 debug_printf ("gen_wordlist_matches (%p, %s) -->", cs
, word
);
1267 strlist_print (tmatches
, "\t");
1271 ret
= strlist_append (ret
, tmatches
);
1272 strlist_dispose (tmatches
);
1276 lwords
= (WORD_LIST
*)NULL
;
1277 line
= (char *)NULL
;
1278 if (cs
->command
|| cs
->funcname
)
1280 /* If we have a command or function to execute, we need to first break
1281 the command line into individual words, find the number of words,
1282 and find the word in the list containing the word to be completed. */
1283 line
= substring (rl_line_buffer
, start
, end
);
1287 debug_printf ("command_line_to_word_list (%s, %d, %d, %p, %p)",
1288 line
, llen
, rl_point
- start
, &nw
, &cw
);
1290 lwords
= command_line_to_word_list (line
, llen
, rl_point
- start
, &nw
, &cw
);
1292 if (lwords
== 0 && llen
> 0)
1293 debug_printf ("ERROR: command_line_to_word_list returns NULL");
1294 else if (progcomp_debug
)
1296 debug_printf ("command_line_to_word_list -->");
1298 print_word_list (lwords
, "!");
1309 tmatches
= gen_shell_function_matches (cs
, word
, line
, rl_point
- start
, lwords
, nw
, cw
, &foundf
);
1317 debug_printf ("gen_shell_function_matches (%p, %s, %p, %d, %d) -->", cs
, word
, lwords
, nw
, cw
);
1318 strlist_print (tmatches
, "\t");
1322 ret
= strlist_append (ret
, tmatches
);
1323 strlist_dispose (tmatches
);
1329 tmatches
= gen_command_matches (cs
, word
, line
, rl_point
- start
, lwords
, nw
, cw
);
1335 debug_printf ("gen_command_matches (%p, %s, %p, %d, %d) -->", cs
, word
, lwords
, nw
, cw
);
1336 strlist_print (tmatches
, "\t");
1340 ret
= strlist_append (ret
, tmatches
);
1341 strlist_dispose (tmatches
);
1345 if (cs
->command
|| cs
->funcname
)
1348 dispose_words (lwords
);
1355 if (found
== 0 || (found
& PCOMP_RETRYFAIL
))
1357 strlist_dispose (ret
);
1363 tmatches
= filter_stringlist (ret
, cs
->filterpat
, word
);
1367 debug_printf ("filter_stringlist (%p, %s, %s) -->", ret
, cs
->filterpat
, word
);
1368 strlist_print (tmatches
, "\t");
1372 if (ret
&& ret
!= tmatches
)
1380 if (cs
->prefix
|| cs
->suffix
)
1381 ret
= strlist_prefix_suffix (ret
, cs
->prefix
, cs
->suffix
);
1383 /* If no matches have been generated and the user has specified that
1384 directory completion should be done as a default, call
1385 gen_action_completions again to generate a list of matching directory
1387 if ((ret
== 0 || ret
->list_len
== 0) && (cs
->options
& COPT_DIRNAMES
))
1389 tcs
= compspec_create ();
1390 tcs
->actions
= CA_DIRECTORY
;
1391 ret
= gen_action_completions (tcs
, word
);
1392 compspec_dispose (tcs
);
1394 else if (cs
->options
& COPT_PLUSDIRS
)
1396 tcs
= compspec_create ();
1397 tcs
->actions
= CA_DIRECTORY
;
1398 tmatches
= gen_action_completions (tcs
, word
);
1399 ret
= strlist_append (ret
, tmatches
);
1400 strlist_dispose (tmatches
);
1401 compspec_dispose (tcs
);
1408 pcomp_set_readline_variables (flags
, nval
)
1411 /* If the user specified that the compspec returns filenames, make
1412 sure that readline knows it. */
1413 if (flags
& COPT_FILENAMES
)
1414 rl_filename_completion_desired
= nval
;
1415 /* If the user doesn't want a space appended, tell readline. */
1416 if (flags
& COPT_NOSPACE
)
1417 rl_completion_suppress_append
= nval
;
1420 /* Set or unset FLAGS in the options word of the current compspec.
1421 SET_OR_UNSET is 1 for setting, 0 for unsetting. */
1423 pcomp_set_compspec_options (cs
, flags
, set_or_unset
)
1425 int flags
, set_or_unset
;
1427 if (cs
== 0 && ((cs
= pcomp_curcs
) == 0))
1430 cs
->options
|= flags
;
1432 cs
->options
&= ~flags
;
1436 gen_progcomp_completions (ocmd
, cmd
, word
, start
, end
, foundp
, retryp
, lastcs
)
1441 int *foundp
, *retryp
;
1444 COMPSPEC
*cs
, *oldcs
;
1448 cs
= progcomp_search (ocmd
);
1450 if (cs
== 0 || cs
== *lastcs
)
1454 compspec_dispose (*lastcs
);
1455 cs
->refcount
++; /* XXX */
1458 cs
= compspec_copy (cs
);
1460 oldcs
= pcomp_curcs
;
1461 oldcmd
= pcomp_curcmd
;
1466 ret
= gen_compspec_completions (cs
, cmd
, word
, start
, end
, foundp
);
1468 pcomp_curcs
= oldcs
;
1469 pcomp_curcmd
= oldcmd
;
1471 /* We need to conditionally handle setting *retryp here */
1473 *retryp
= foundp
&& (*foundp
& PCOMP_RETRYFAIL
);
1477 *foundp
&= ~PCOMP_RETRYFAIL
;
1478 *foundp
|= cs
->options
;
1481 compspec_dispose (cs
);
1485 /* The driver function for the programmable completion code. Returns a list
1486 of matches for WORD, which is an argument to command CMD. START and END
1487 bound the command currently being completed in rl_line_buffer. */
1489 programmable_completions (cmd
, word
, start
, end
, foundp
)
1492 int start
, end
, *foundp
;
1494 COMPSPEC
*cs
, *lastcs
;
1496 char **rmatches
, *t
;
1497 int found
, retry
, count
;
1506 /* We look at the basename of CMD if the full command does not have
1507 an associated COMPSPEC. */
1508 ret
= gen_progcomp_completions (cmd
, cmd
, word
, start
, end
, &found
, &retry
, &lastcs
);
1511 t
= strrchr (cmd
, '/');
1513 ret
= gen_progcomp_completions (t
, cmd
, word
, start
, end
, &found
, &retry
, &lastcs
);
1517 ret
= gen_progcomp_completions (DEFAULTCMD
, cmd
, word
, start
, end
, &found
, &retry
, &lastcs
);
1523 internal_warning ("programmable_completion: %s: possible retry loop", cmd
);
1531 rmatches
= ret
->list
;
1535 rmatches
= (char **)NULL
;
1540 if (lastcs
) /* XXX - should be while? */
1541 compspec_dispose (lastcs
);
1546 #endif /* PROGRAMMABLE_COMPLETION */