1 /* pcomplete.c - functions to generate lists of matches for programmable completion. */
3 /* Copyright (C) 1999-2012 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)
47 #include "pcomplete.h"
50 #include "execute_cmd.h"
53 #if defined (JOB_CONTROL)
64 #include "builtins/common.h"
65 #include "builtins/builtext.h"
67 #include <glob/glob.h>
68 #include <glob/strmatch.h>
70 #include <readline/rlconf.h>
71 #include <readline/readline.h>
72 #include <readline/history.h>
74 #define PCOMP_RETRYFAIL 256
79 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
81 typedef SHELL_VAR
**SVFUNC ();
84 extern char *strpbrk
__P((char *, char *));
87 extern int array_needs_making
;
88 extern STRING_INT_ALIST word_token_alist
[];
89 extern char *signal_names
[];
90 extern sh_builtin_func_t
*last_shell_builtin
, *this_shell_builtin
;
93 #if defined (PREFER_STDARG)
94 static void debug_printf (const char *, ...) __attribute__((__format__ (printf
, 1, 2)));
98 static int it_init_joblist
__P((ITEMLIST
*, int));
100 static int it_init_aliases
__P((ITEMLIST
*));
101 static int it_init_arrayvars
__P((ITEMLIST
*));
102 static int it_init_bindings
__P((ITEMLIST
*));
103 static int it_init_builtins
__P((ITEMLIST
*));
104 static int it_init_disabled
__P((ITEMLIST
*));
105 static int it_init_enabled
__P((ITEMLIST
*));
106 static int it_init_exported
__P((ITEMLIST
*));
107 static int it_init_functions
__P((ITEMLIST
*));
108 static int it_init_helptopics
__P((ITEMLIST
*));
109 static int it_init_hostnames
__P((ITEMLIST
*));
110 static int it_init_jobs
__P((ITEMLIST
*));
111 static int it_init_running
__P((ITEMLIST
*));
112 static int it_init_stopped
__P((ITEMLIST
*));
113 static int it_init_keywords
__P((ITEMLIST
*));
114 static int it_init_signals
__P((ITEMLIST
*));
115 static int it_init_variables
__P((ITEMLIST
*));
116 static int it_init_setopts
__P((ITEMLIST
*));
117 static int it_init_shopts
__P((ITEMLIST
*));
119 static int shouldexp_filterpat
__P((char *));
120 static char *preproc_filterpat
__P((char *, char *));
122 static void init_itemlist_from_varlist
__P((ITEMLIST
*, SVFUNC
*));
124 static STRINGLIST
*gen_matches_from_itemlist
__P((ITEMLIST
*, const char *));
125 static STRINGLIST
*gen_action_completions
__P((COMPSPEC
*, const char *));
126 static STRINGLIST
*gen_globpat_matches
__P((COMPSPEC
*, const char *));
127 static STRINGLIST
*gen_wordlist_matches
__P((COMPSPEC
*, const char *));
128 static STRINGLIST
*gen_shell_function_matches
__P((COMPSPEC
*, const char *,
130 char *, int, WORD_LIST
*,
132 static STRINGLIST
*gen_command_matches
__P((COMPSPEC
*, const char *,
134 char *, int, WORD_LIST
*,
137 static STRINGLIST
*gen_progcomp_completions
__P((const char *, const char *,
139 int, int, int *, int *,
142 static char *pcomp_filename_completion_function
__P((const char *, int));
144 #if defined (ARRAY_VARS)
145 static SHELL_VAR
*bind_comp_words
__P((WORD_LIST
*));
147 static void bind_compfunc_variables
__P((char *, int, WORD_LIST
*, int, int));
148 static void unbind_compfunc_variables
__P((int));
149 static WORD_LIST
*build_arg_list
__P((char *, const char *, const char *, WORD_LIST
*, int));
150 static WORD_LIST
*command_line_to_word_list
__P((char *, int, int, int *, int *));
153 static int progcomp_debug
= 0;
156 int prog_completion_enabled
= 1;
158 /* These are used to manage the arrays of strings for possible completions. */
159 ITEMLIST it_aliases
= { 0, it_init_aliases
, (STRINGLIST
*)0 };
160 ITEMLIST it_arrayvars
= { LIST_DYNAMIC
, it_init_arrayvars
, (STRINGLIST
*)0 };
161 ITEMLIST it_bindings
= { 0, it_init_bindings
, (STRINGLIST
*)0 };
162 ITEMLIST it_builtins
= { 0, it_init_builtins
, (STRINGLIST
*)0 };
163 ITEMLIST it_commands
= { LIST_DYNAMIC
}; /* unused */
164 ITEMLIST it_directories
= { LIST_DYNAMIC
}; /* unused */
165 ITEMLIST it_disabled
= { 0, it_init_disabled
, (STRINGLIST
*)0 };
166 ITEMLIST it_enabled
= { 0, it_init_enabled
, (STRINGLIST
*)0 };
167 ITEMLIST it_exports
= { LIST_DYNAMIC
, it_init_exported
, (STRINGLIST
*)0 };
168 ITEMLIST it_files
= { LIST_DYNAMIC
}; /* unused */
169 ITEMLIST it_functions
= { 0, it_init_functions
, (STRINGLIST
*)0 };
170 ITEMLIST it_helptopics
= { 0, it_init_helptopics
, (STRINGLIST
*)0 };
171 ITEMLIST it_hostnames
= { LIST_DYNAMIC
, it_init_hostnames
, (STRINGLIST
*)0 };
172 ITEMLIST it_groups
= { LIST_DYNAMIC
}; /* unused */
173 ITEMLIST it_jobs
= { LIST_DYNAMIC
, it_init_jobs
, (STRINGLIST
*)0 };
174 ITEMLIST it_keywords
= { 0, it_init_keywords
, (STRINGLIST
*)0 };
175 ITEMLIST it_running
= { LIST_DYNAMIC
, it_init_running
, (STRINGLIST
*)0 };
176 ITEMLIST it_services
= { LIST_DYNAMIC
}; /* unused */
177 ITEMLIST it_setopts
= { 0, it_init_setopts
, (STRINGLIST
*)0 };
178 ITEMLIST it_shopts
= { 0, it_init_shopts
, (STRINGLIST
*)0 };
179 ITEMLIST it_signals
= { 0, it_init_signals
, (STRINGLIST
*)0 };
180 ITEMLIST it_stopped
= { LIST_DYNAMIC
, it_init_stopped
, (STRINGLIST
*)0 };
181 ITEMLIST it_users
= { LIST_DYNAMIC
}; /* unused */
182 ITEMLIST it_variables
= { LIST_DYNAMIC
, it_init_variables
, (STRINGLIST
*)0 };
184 COMPSPEC
*pcomp_curcs
;
185 const char *pcomp_curcmd
;
190 #if defined (PREFER_STDARG)
191 debug_printf (const char *format
, ...)
193 debug_printf (format
, va_alist
)
200 if (progcomp_debug
== 0)
203 SH_VA_START (args
, format
);
205 fprintf (stdout
, "DEBUG: ");
206 vfprintf (stdout
, format
, args
);
207 fprintf (stdout
, "\n");
215 /* Functions to manage the item lists */
218 set_itemlist_dirty (it
)
221 it
->flags
|= LIST_DIRTY
;
225 initialize_itemlist (itp
)
228 (*itp
->list_getter
) (itp
);
229 itp
->flags
|= LIST_INITIALIZED
;
230 itp
->flags
&= ~LIST_DIRTY
;
242 if ((itp
->flags
& (LIST_DONTFREEMEMBERS
|LIST_DONTFREE
)) == 0)
243 strvec_flush (sl
->list
);
244 if ((itp
->flags
& LIST_DONTFREE
) == 0)
248 itp
->slist
= (STRINGLIST
*)NULL
;
249 itp
->flags
&= ~(LIST_DONTFREE
|LIST_DONTFREEMEMBERS
|LIST_INITIALIZED
|LIST_DIRTY
);
254 shouldexp_filterpat (s
)
259 for (p
= s
; p
&& *p
; p
++)
269 /* Replace any instance of `&' in PAT with TEXT. Backslash may be used to
270 quote a `&' and inhibit substitution. Returns a new string. This just
271 calls stringlib.c:strcreplace(). */
273 preproc_filterpat (pat
, text
)
279 ret
= strcreplace (pat
, '&', text
, 1);
283 /* Remove any match of FILTERPAT from SL. A `&' in FILTERPAT is replaced by
284 TEXT. A leading `!' in FILTERPAT negates the pattern; in this case
285 any member of SL->list that does *not* match will be removed. This returns
286 a new STRINGLIST with the matching members of SL *copied*. Any
287 non-matching members of SL->list are *freed*. */
289 filter_stringlist (sl
, filterpat
, text
)
291 char *filterpat
, *text
;
297 if (sl
== 0 || sl
->list
== 0 || sl
->list_len
== 0)
300 npat
= shouldexp_filterpat (filterpat
) ? preproc_filterpat (filterpat
, text
) : filterpat
;
302 not = (npat
[0] == '!');
303 t
= not ? npat
+ 1 : npat
;
305 ret
= strlist_create (sl
->list_size
);
306 for (i
= 0; i
< sl
->list_len
; i
++)
308 m
= strmatch (t
, sl
->list
[i
], FNMATCH_EXTFLAG
);
309 if ((not && m
== FNM_NOMATCH
) || (not == 0 && m
!= FNM_NOMATCH
))
312 ret
->list
[ret
->list_len
++] = sl
->list
[i
];
315 ret
->list
[ret
->list_len
] = (char *)NULL
;
316 if (npat
!= filterpat
)
322 /* Turn an array of strings returned by rl_completion_matches into a STRINGLIST.
323 This understands how rl_completion_matches sets matches[0] (the lcd of the
324 strings in the list, unless it's the only match). */
326 completions_to_stringlist (matches
)
332 mlen
= (matches
== 0) ? 0 : strvec_len (matches
);
333 sl
= strlist_create (mlen
+ 1);
335 if (matches
== 0 || matches
[0] == 0)
340 sl
->list
[0] = STRDUP (matches
[0]);
341 sl
->list
[sl
->list_len
= 1] = (char *)NULL
;
345 for (i
= 1, n
= 0; i
< mlen
; i
++, n
++)
346 sl
->list
[n
] = STRDUP (matches
[i
]);
348 sl
->list
[n
] = (char *)NULL
;
353 /* Functions to manage the various ITEMLISTs that we populate internally.
354 The caller is responsible for setting ITP->flags correctly. */
357 it_init_aliases (itp
)
361 alias_t
**alias_list
;
365 alias_list
= all_aliases ();
368 itp
->slist
= (STRINGLIST
*)NULL
;
371 for (n
= 0; alias_list
[n
]; n
++)
373 sl
= strlist_create (n
+1);
374 for (i
= 0; i
< n
; i
++)
375 sl
->list
[i
] = STRDUP (alias_list
[i
]->name
);
376 sl
->list
[n
] = (char *)NULL
;
377 sl
->list_size
= sl
->list_len
= n
;
380 itp
->slist
= (STRINGLIST
*)NULL
;
387 init_itemlist_from_varlist (itp
, svfunc
)
395 vlist
= (*svfunc
) ();
398 itp
->slist
= (STRINGLIST
*)NULL
;
401 for (n
= 0; vlist
[n
]; n
++)
403 sl
= strlist_create (n
+1);
404 for (i
= 0; i
< n
; i
++)
405 sl
->list
[i
] = savestring (vlist
[i
]->name
);
406 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
411 it_init_arrayvars (itp
)
414 #if defined (ARRAY_VARS)
415 init_itemlist_from_varlist (itp
, all_array_variables
);
423 it_init_bindings (itp
)
429 /* rl_funmap_names allocates blist, but not its members */
430 blist
= (char **)rl_funmap_names (); /* XXX fix const later */
431 sl
= strlist_create (0);
434 sl
->list_len
= strvec_len (sl
->list
);
435 itp
->flags
|= LIST_DONTFREEMEMBERS
;
442 it_init_builtins (itp
)
448 sl
= strlist_create (num_shell_builtins
);
449 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
450 if (shell_builtins
[i
].function
)
451 sl
->list
[n
++] = shell_builtins
[i
].name
;
452 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
453 itp
->flags
|= LIST_DONTFREEMEMBERS
;
459 it_init_enabled (itp
)
465 sl
= strlist_create (num_shell_builtins
);
466 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
468 if (shell_builtins
[i
].function
&& (shell_builtins
[i
].flags
& BUILTIN_ENABLED
))
469 sl
->list
[n
++] = shell_builtins
[i
].name
;
471 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
472 itp
->flags
|= LIST_DONTFREEMEMBERS
;
478 it_init_disabled (itp
)
484 sl
= strlist_create (num_shell_builtins
);
485 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
487 if (shell_builtins
[i
].function
&& ((shell_builtins
[i
].flags
& BUILTIN_ENABLED
) == 0))
488 sl
->list
[n
++] = shell_builtins
[i
].name
;
490 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
491 itp
->flags
|= LIST_DONTFREEMEMBERS
;
497 it_init_exported (itp
)
500 init_itemlist_from_varlist (itp
, all_exported_variables
);
505 it_init_functions (itp
)
508 init_itemlist_from_varlist (itp
, all_visible_functions
);
512 /* Like it_init_builtins, but includes everything the help builtin looks at,
513 not just builtins with an active implementing function. */
515 it_init_helptopics (itp
)
521 sl
= strlist_create (num_shell_builtins
);
522 for (i
= n
= 0; i
< num_shell_builtins
; i
++)
523 sl
->list
[n
++] = shell_builtins
[i
].name
;
524 sl
->list
[sl
->list_len
= n
] = (char *)NULL
;
525 itp
->flags
|= LIST_DONTFREEMEMBERS
;
531 it_init_hostnames (itp
)
536 sl
= strlist_create (0);
537 sl
->list
= get_hostname_list ();
538 sl
->list_len
= sl
->list
? strvec_len (sl
->list
) : 0;
539 sl
->list_size
= sl
->list_len
;
541 itp
->flags
|= LIST_DONTFREEMEMBERS
|LIST_DONTFREE
;
546 it_init_joblist (itp
, jstate
)
550 #if defined (JOB_CONTROL)
556 JOB_STATE ws
; /* wanted state */
561 else if (jstate
== 1)
564 sl
= strlist_create (js
.j_jobslots
);
565 for (i
= js
.j_jobslots
- 1; i
>= 0; i
--)
567 j
= get_job_by_jid (i
);
571 if (jstate
== -1 || JOBSTATE(i
) == ws
)
573 s
= savestring (p
->command
);
574 t
= strpbrk (s
, " \t\n");
577 sl
->list
[sl
->list_len
++] = s
;
582 itp
->slist
= (STRINGLIST
*)NULL
;
591 return (it_init_joblist (itp
, -1));
595 it_init_running (itp
)
598 return (it_init_joblist (itp
, 0));
602 it_init_stopped (itp
)
605 return (it_init_joblist (itp
, 1));
609 it_init_keywords (itp
)
615 for (n
= 0; word_token_alist
[n
].word
; n
++)
617 sl
= strlist_create (n
);
618 for (i
= 0; i
< n
; i
++)
619 sl
->list
[i
] = word_token_alist
[i
].word
;
620 sl
->list
[sl
->list_len
= i
] = (char *)NULL
;
621 itp
->flags
|= LIST_DONTFREEMEMBERS
;
627 it_init_signals (itp
)
632 sl
= strlist_create (0);
633 sl
->list
= signal_names
;
634 sl
->list_len
= strvec_len (sl
->list
);
635 itp
->flags
|= LIST_DONTFREE
;
641 it_init_variables (itp
)
644 init_itemlist_from_varlist (itp
, all_visible_variables
);
649 it_init_setopts (itp
)
654 sl
= strlist_create (0);
655 sl
->list
= get_minus_o_opts ();
656 sl
->list_len
= strvec_len (sl
->list
);
658 itp
->flags
|= LIST_DONTFREEMEMBERS
;
668 sl
= strlist_create (0);
669 sl
->list
= get_shopt_options ();
670 sl
->list_len
= strvec_len (sl
->list
);
672 itp
->flags
|= LIST_DONTFREEMEMBERS
;
676 /* Generate a list of all matches for TEXT using the STRINGLIST in itp->slist
677 as the list of possibilities. If the itemlist has been marked dirty or
678 it should be regenerated every time, destroy the old STRINGLIST and make a
679 new one before trying the match. TEXT is dequoted before attempting a
682 gen_matches_from_itemlist (itp
, text
)
686 STRINGLIST
*ret
, *sl
;
690 if ((itp
->flags
& (LIST_DIRTY
|LIST_DYNAMIC
)) ||
691 (itp
->flags
& LIST_INITIALIZED
) == 0)
693 if (itp
->flags
& (LIST_DIRTY
|LIST_DYNAMIC
))
694 clean_itemlist (itp
);
695 if ((itp
->flags
& LIST_INITIALIZED
) == 0)
696 initialize_itemlist (itp
);
699 return ((STRINGLIST
*)NULL
);
700 ret
= strlist_create (itp
->slist
->list_len
+1);
703 ntxt
= bash_dequote_text (text
);
704 tlen
= STRLEN (ntxt
);
706 for (i
= n
= 0; i
< sl
->list_len
; i
++)
708 if (tlen
== 0 || STREQN (sl
->list
[i
], ntxt
, tlen
))
709 ret
->list
[n
++] = STRDUP (sl
->list
[i
]);
711 ret
->list
[ret
->list_len
= n
] = (char *)NULL
;
717 /* A wrapper for rl_filename_completion_function that dequotes the filename
718 before attempting completions. */
720 pcomp_filename_completion_function (text
, state
)
724 static char *dfn
; /* dequoted filename */
726 int iscompgen
, iscompleting
;
731 /* remove backslashes quoting special characters in filenames. */
732 /* There are roughly three paths we can follow to get here:
734 2. compgen -f "$word" from a completion function
735 3. compgen -f "$word" from the command line
736 They all need to be handled.
738 In the first two cases, readline will run the filename dequoting
739 function in rl_filename_completion_function if it found a filename
740 quoting character in the word to be completed
741 (rl_completion_found_quote). We run the dequoting function here
742 if we're running compgen, we're not completing, and the
743 rl_filename_completion_function won't dequote the filename
744 (rl_completion_found_quote == 0). */
745 iscompgen
= this_shell_builtin
== compgen_builtin
;
746 iscompleting
= RL_ISSTATE (RL_STATE_COMPLETING
);
747 if (iscompgen
&& iscompleting
== 0 && rl_completion_found_quote
== 0
748 && rl_filename_dequoting_function
)
750 /* Use rl_completion_quote_character because any single or
751 double quotes have been removed by the time TEXT makes it
752 here, and we don't want to remove backslashes inside
754 dfn
= (*rl_filename_dequoting_function
) ((char *)text
, rl_completion_quote_character
);
757 dfn
= savestring (text
);
760 return (rl_filename_completion_function (dfn
, state
));
763 #define GEN_COMPS(bmap, flag, it, text, glist, tlist) \
767 tlist = gen_matches_from_itemlist (it, text); \
770 glist = strlist_append (glist, tlist); \
771 strlist_dispose (tlist); \
776 #define GEN_XCOMPS(bmap, flag, text, func, cmatches, glist, tlist) \
780 cmatches = rl_completion_matches (text, func); \
781 tlist = completions_to_stringlist (cmatches); \
782 glist = strlist_append (glist, tlist); \
783 strvec_dispose (cmatches); \
784 strlist_dispose (tlist); \
788 /* Functions to generate lists of matches from the actions member of CS. */
791 gen_action_completions (cs
, text
)
795 STRINGLIST
*ret
, *tmatches
;
796 char **cmatches
; /* from rl_completion_matches ... */
800 ret
= tmatches
= (STRINGLIST
*)NULL
;
803 GEN_COMPS (flags
, CA_ALIAS
, &it_aliases
, text
, ret
, tmatches
);
804 GEN_COMPS (flags
, CA_ARRAYVAR
, &it_arrayvars
, text
, ret
, tmatches
);
805 GEN_COMPS (flags
, CA_BINDING
, &it_bindings
, text
, ret
, tmatches
);
806 GEN_COMPS (flags
, CA_BUILTIN
, &it_builtins
, text
, ret
, tmatches
);
807 GEN_COMPS (flags
, CA_DISABLED
, &it_disabled
, text
, ret
, tmatches
);
808 GEN_COMPS (flags
, CA_ENABLED
, &it_enabled
, text
, ret
, tmatches
);
809 GEN_COMPS (flags
, CA_EXPORT
, &it_exports
, text
, ret
, tmatches
);
810 GEN_COMPS (flags
, CA_FUNCTION
, &it_functions
, text
, ret
, tmatches
);
811 GEN_COMPS (flags
, CA_HELPTOPIC
, &it_helptopics
, text
, ret
, tmatches
);
812 GEN_COMPS (flags
, CA_HOSTNAME
, &it_hostnames
, text
, ret
, tmatches
);
813 GEN_COMPS (flags
, CA_JOB
, &it_jobs
, text
, ret
, tmatches
);
814 GEN_COMPS (flags
, CA_KEYWORD
, &it_keywords
, text
, ret
, tmatches
);
815 GEN_COMPS (flags
, CA_RUNNING
, &it_running
, text
, ret
, tmatches
);
816 GEN_COMPS (flags
, CA_SETOPT
, &it_setopts
, text
, ret
, tmatches
);
817 GEN_COMPS (flags
, CA_SHOPT
, &it_shopts
, text
, ret
, tmatches
);
818 GEN_COMPS (flags
, CA_SIGNAL
, &it_signals
, text
, ret
, tmatches
);
819 GEN_COMPS (flags
, CA_STOPPED
, &it_stopped
, text
, ret
, tmatches
);
820 GEN_COMPS (flags
, CA_VARIABLE
, &it_variables
, text
, ret
, tmatches
);
822 GEN_XCOMPS(flags
, CA_COMMAND
, text
, command_word_completion_function
, cmatches
, ret
, tmatches
);
823 GEN_XCOMPS(flags
, CA_FILE
, text
, pcomp_filename_completion_function
, cmatches
, ret
, tmatches
);
824 GEN_XCOMPS(flags
, CA_USER
, text
, rl_username_completion_function
, cmatches
, ret
, tmatches
);
825 GEN_XCOMPS(flags
, CA_GROUP
, text
, bash_groupname_completion_function
, cmatches
, ret
, tmatches
);
826 GEN_XCOMPS(flags
, CA_SERVICE
, text
, bash_servicename_completion_function
, cmatches
, ret
, tmatches
);
828 /* And lastly, the special case for directories */
829 if (flags
& CA_DIRECTORY
)
831 t
= rl_filename_completion_desired
;
832 rl_completion_mark_symlink_dirs
= 1; /* override user preference */
833 cmatches
= bash_directory_completion_matches (text
);
834 /* If we did not want filename completion before this, and there are
835 no matches, turn off rl_filename_completion_desired so whatever
836 matches we get are not treated as filenames (it gets turned on by
837 rl_filename_completion_function unconditionally). */
838 if (t
== 0 && cmatches
== 0 && rl_filename_completion_desired
== 1)
839 rl_filename_completion_desired
= 0;
840 tmatches
= completions_to_stringlist (cmatches
);
841 ret
= strlist_append (ret
, tmatches
);
842 strvec_dispose (cmatches
);
843 strlist_dispose (tmatches
);
849 /* Generate a list of matches for CS->globpat. Unresolved: should this use
850 TEXT as a match prefix, or just go without? Currently, the code does not
851 use TEXT, just globs CS->globpat and returns the results. If we do decide
852 to use TEXT, we should call quote_string_for_globbing before the call to
855 gen_globpat_matches (cs
, text
)
861 sl
= strlist_create (0);
862 sl
->list
= glob_filename (cs
->globpat
, 0);
863 if (GLOB_FAILED (sl
->list
))
864 sl
->list
= (char **)NULL
;
866 sl
->list_len
= sl
->list_size
= strvec_len (sl
->list
);
870 /* Perform the shell word expansions on CS->words and return the results.
871 Again, this ignores TEXT. */
873 gen_wordlist_matches (cs
, text
)
880 char *ntxt
; /* dequoted TEXT to use in comparisons */
882 if (cs
->words
== 0 || cs
->words
[0] == '\0')
883 return ((STRINGLIST
*)NULL
);
885 /* This used to be a simple expand_string(cs->words, 0), but that won't
886 do -- there's no way to split a simple list into individual words
887 that way, since the shell semantics say that word splitting is done
888 only on the results of expansion. split_at_delims also handles embedded
889 quoted strings and preserves the quotes for the expand_words_shellexp
890 function call that follows. */
891 /* XXX - this is where this function spends most of its time */
892 l
= split_at_delims (cs
->words
, strlen (cs
->words
), (char *)NULL
, -1, 0, (int *)NULL
, (int *)NULL
);
894 return ((STRINGLIST
*)NULL
);
895 /* This will jump back to the top level if the expansion fails... */
896 l2
= expand_words_shellexp (l
);
899 nw
= list_length (l2
);
900 sl
= strlist_create (nw
+ 1);
902 ntxt
= bash_dequote_text (text
);
903 tlen
= STRLEN (ntxt
);
905 for (nw
= 0, l
= l2
; l
; l
= l
->next
)
907 if (tlen
== 0 || STREQN (l
->word
->word
, ntxt
, tlen
))
908 sl
->list
[nw
++] = STRDUP (l
->word
->word
);
910 sl
->list
[sl
->list_len
= nw
] = (char *)NULL
;
920 bind_comp_words (lwords
)
925 v
= find_variable ("COMP_WORDS");
927 v
= make_new_array_variable ("COMP_WORDS");
929 VUNSETATTR (v
, att_readonly
);
930 if (array_p (v
) == 0)
931 v
= convert_var_to_array (v
);
932 v
= assign_array_var_from_word_list (v
, lwords
, 0);
934 VUNSETATTR (v
, att_invisible
);
937 #endif /* ARRAY_VARS */
940 bind_compfunc_variables (line
, ind
, lwords
, cw
, exported
)
946 char ibuf
[INT_STRLEN_BOUND(int) + 1];
952 /* Set the variables that the function expects while it executes. Maybe
953 these should be in the function environment (temporary_env). */
954 v
= bind_variable ("COMP_LINE", line
, 0);
956 VSETATTR(v
, att_exported
);
958 /* Post bash-4.2: COMP_POINT is characters instead of bytes. */
961 llen
= MB_STRLEN (line
);
963 value
= inttostr (llen
, ibuf
, sizeof(ibuf
));
964 v
= bind_int_variable ("COMP_POINT", value
);
966 VSETATTR(v
, att_exported
);
968 value
= inttostr (rl_completion_type
, ibuf
, sizeof (ibuf
));
969 v
= bind_int_variable ("COMP_TYPE", value
);
971 VSETATTR(v
, att_exported
);
973 value
= inttostr (rl_completion_invoking_key
, ibuf
, sizeof (ibuf
));
974 v
= bind_int_variable ("COMP_KEY", value
);
976 VSETATTR(v
, att_exported
);
978 /* Since array variables can't be exported, we don't bother making the
983 v
= bind_comp_words (lwords
);
984 value
= inttostr (cw
, ibuf
, sizeof(ibuf
));
985 bind_int_variable ("COMP_CWORD", value
);
989 array_needs_making
= 1;
993 unbind_compfunc_variables (exported
)
996 unbind_variable ("COMP_LINE");
997 unbind_variable ("COMP_POINT");
998 unbind_variable ("COMP_TYPE");
999 unbind_variable ("COMP_KEY");
1001 unbind_variable ("COMP_WORDS");
1002 unbind_variable ("COMP_CWORD");
1005 array_needs_making
= 1;
1008 /* Build the list of words to pass to a function or external command
1009 as arguments. When the function or command is invoked,
1011 $0 == function or command being invoked
1013 $2 == word to be completed (possibly null)
1016 Functions can access all of the words in the current command line
1017 with the COMP_WORDS array. External commands cannot; they have to
1018 make do with the COMP_LINE and COMP_POINT variables. */
1021 build_arg_list (cmd
, cname
, text
, lwords
, ind
)
1028 WORD_LIST
*ret
, *cl
, *l
;
1032 ret
= (WORD_LIST
*)NULL
;
1033 w
= make_word (cmd
);
1034 ret
= make_word_list (w
, (WORD_LIST
*)NULL
); /* $0 */
1036 w
= make_word (cname
); /* $1 */
1037 cl
= ret
->next
= make_word_list (w
, (WORD_LIST
*)NULL
);
1039 w
= make_word (text
);
1040 cl
->next
= make_word_list (w
, (WORD_LIST
*)NULL
); /* $2 */
1043 /* Search lwords for current word */
1044 for (l
= lwords
, i
= 1; l
&& i
< ind
-1; l
= l
->next
, i
++)
1046 w
= (l
&& l
->word
) ? copy_word (l
->word
) : make_word ("");
1047 cl
->next
= make_word_list (w
, (WORD_LIST
*)NULL
);
1052 /* Build a command string with
1053 $0 == cs->funcname (function to execute for completion list)
1054 $1 == command name (command being completed)
1055 $2 = word to be completed (possibly null)
1057 and run in the current shell. The function should put its completion
1058 list into the array variable COMPREPLY. We build a STRINGLIST
1059 from the results and return it.
1061 Since the shell function should return its list of matches in an array
1062 variable, this does nothing if arrays are not compiled into the shell. */
1065 gen_shell_function_matches (cs
, cmd
, text
, line
, ind
, lwords
, nw
, cw
, foundp
)
1080 sh_parser_state_t ps
;
1081 sh_parser_state_t
* restrict pps
;
1082 #if defined (ARRAY_VARS)
1090 funcname
= cs
->funcname
;
1091 f
= find_function (funcname
);
1094 internal_error (_("completion: function `%s' not found"), funcname
);
1097 return ((STRINGLIST
*)NULL
);
1100 #if !defined (ARRAY_VARS)
1101 return ((STRINGLIST
*)NULL
);
1104 /* We pass cw - 1 because command_line_to_word_list returns indices that are
1105 1-based, while bash arrays are 0-based. */
1106 bind_compfunc_variables (line
, ind
, lwords
, cw
- 1, 0);
1108 cmdlist
= build_arg_list (funcname
, cmd
, text
, lwords
, cw
);
1111 save_parser_state (pps
);
1112 begin_unwind_frame ("gen-shell-function-matches");
1113 add_unwind_protect (restore_parser_state
, (char *)pps
);
1114 add_unwind_protect (dispose_words
, (char *)cmdlist
);
1115 add_unwind_protect (unbind_compfunc_variables
, (char *)0);
1117 fval
= execute_shell_function (f
, cmdlist
);
1119 discard_unwind_frame ("gen-shell-function-matches");
1120 restore_parser_state (pps
);
1122 found
= fval
!= EX_NOTFOUND
;
1123 if (fval
== EX_RETRYFAIL
)
1124 found
|= PCOMP_RETRYFAIL
;
1128 /* Now clean up and destroy everything. */
1129 dispose_words (cmdlist
);
1130 unbind_compfunc_variables (0);
1132 /* The list of completions is returned in the array variable COMPREPLY. */
1133 v
= find_variable ("COMPREPLY");
1135 return ((STRINGLIST
*)NULL
);
1136 if (array_p (v
) == 0)
1137 v
= convert_var_to_array (v
);
1139 VUNSETATTR (v
, att_invisible
);
1142 if (found
== 0 || (found
& PCOMP_RETRYFAIL
) || a
== 0 || array_empty (a
))
1143 sl
= (STRINGLIST
*)NULL
;
1146 /* XXX - should we filter the list of completions so only those matching
1147 TEXT are returned? Right now, we do not. */
1148 sl
= strlist_create (0);
1149 sl
->list
= array_to_argv (a
);
1150 sl
->list_len
= sl
->list_size
= array_num_elements (a
);
1153 /* XXX - should we unbind COMPREPLY here? */
1154 unbind_variable ("COMPREPLY");
1160 /* Build a command string with
1161 $0 == cs->command (command to execute for completion list)
1162 $1 == command name (command being completed)
1163 $2 = word to be completed (possibly null)
1165 and run in with command substitution. Parse the results, one word
1166 per line, with backslashes allowed to escape newlines. Build a
1167 STRINGLIST from the results and return it. */
1170 gen_command_matches (cs
, cmd
, text
, line
, ind
, lwords
, nw
, cw
)
1179 char *csbuf
, *cscmd
, *t
;
1180 int cmdlen
, cmdsize
, n
, ws
, we
;
1181 WORD_LIST
*cmdlist
, *cl
;
1185 bind_compfunc_variables (line
, ind
, lwords
, cw
, 1);
1186 cmdlist
= build_arg_list (cs
->command
, cmd
, text
, lwords
, cw
);
1188 /* Estimate the size needed for the buffer. */
1189 n
= strlen (cs
->command
);
1191 for (cl
= cmdlist
->next
; cl
; cl
= cl
->next
)
1192 cmdsize
+= STRLEN (cl
->word
->word
) + 3;
1195 /* allocate the string for the command and fill it in. */
1196 cscmd
= (char *)xmalloc (cmdsize
+ 1);
1198 strcpy (cscmd
, cs
->command
); /* $0 */
1200 cscmd
[cmdlen
++] = ' ';
1201 for (cl
= cmdlist
->next
; cl
; cl
= cl
->next
) /* $1, $2, $3, ... */
1203 t
= sh_single_quote (cl
->word
->word
? cl
->word
->word
: "");
1205 RESIZE_MALLOCED_BUFFER (cscmd
, cmdlen
, n
+ 2, cmdsize
, 64);
1206 strcpy (cscmd
+ cmdlen
, t
);
1209 cscmd
[cmdlen
++] = ' ';
1212 cscmd
[cmdlen
] = '\0';
1214 tw
= command_substitute (cscmd
, 0);
1215 csbuf
= tw
? tw
->word
: (char *)NULL
;
1217 dispose_word_desc (tw
);
1219 /* Now clean up and destroy everything. */
1220 dispose_words (cmdlist
);
1222 unbind_compfunc_variables (1);
1224 if (csbuf
== 0 || *csbuf
== '\0')
1227 return ((STRINGLIST
*)NULL
);
1230 /* Now break CSBUF up at newlines, with backslash allowed to escape a
1231 newline, and put the individual words into a STRINGLIST. */
1232 sl
= strlist_create (16);
1233 for (ws
= 0; csbuf
[ws
]; )
1236 while (csbuf
[we
] && csbuf
[we
] != '\n')
1238 if (csbuf
[we
] == '\\' && csbuf
[we
+1] == '\n')
1242 t
= substring (csbuf
, ws
, we
);
1243 if (sl
->list_len
>= sl
->list_size
- 1)
1244 strlist_resize (sl
, sl
->list_size
+ 16);
1245 sl
->list
[sl
->list_len
++] = t
;
1246 while (csbuf
[we
] == '\n') we
++;
1249 sl
->list
[sl
->list_len
] = (char *)NULL
;
1256 command_line_to_word_list (line
, llen
, sentinel
, nwp
, cwp
)
1258 int llen
, sentinel
, *nwp
, *cwp
;
1264 delims
= "()<>;&| \t\n"; /* shell metacharacters break words */
1266 delims
= rl_completer_word_break_characters
;
1268 ret
= split_at_delims (line
, llen
, delims
, sentinel
, SD_NOQUOTEDELIM
, nwp
, cwp
);
1272 /* Evaluate COMPSPEC *cs and return all matches for WORD. */
1275 gen_compspec_completions (cs
, cmd
, word
, start
, end
, foundp
)
1282 STRINGLIST
*ret
, *tmatches
;
1284 int llen
, nw
, cw
, found
, foundf
;
1292 debug_printf ("gen_compspec_completions (%s, %s, %d, %d)", cmd
, word
, start
, end
);
1293 debug_printf ("gen_compspec_completions: %s -> %p", cmd
, cs
);
1295 ret
= gen_action_completions (cs
, word
);
1297 if (ret
&& progcomp_debug
)
1299 debug_printf ("gen_action_completions (%p, %s) -->", cs
, word
);
1300 strlist_print (ret
, "\t");
1305 /* Now we start generating completions based on the other members of CS. */
1308 tmatches
= gen_globpat_matches (cs
, word
);
1314 debug_printf ("gen_globpat_matches (%p, %s) -->", cs
, word
);
1315 strlist_print (tmatches
, "\t");
1319 ret
= strlist_append (ret
, tmatches
);
1320 strlist_dispose (tmatches
);
1321 rl_filename_completion_desired
= 1;
1327 tmatches
= gen_wordlist_matches (cs
, word
);
1333 debug_printf ("gen_wordlist_matches (%p, %s) -->", cs
, word
);
1334 strlist_print (tmatches
, "\t");
1338 ret
= strlist_append (ret
, tmatches
);
1339 strlist_dispose (tmatches
);
1343 lwords
= (WORD_LIST
*)NULL
;
1344 line
= (char *)NULL
;
1345 if (cs
->command
|| cs
->funcname
)
1347 /* If we have a command or function to execute, we need to first break
1348 the command line into individual words, find the number of words,
1349 and find the word in the list containing the word to be completed. */
1350 line
= substring (rl_line_buffer
, start
, end
);
1354 debug_printf ("command_line_to_word_list (%s, %d, %d, %p, %p)",
1355 line
, llen
, rl_point
- start
, &nw
, &cw
);
1357 lwords
= command_line_to_word_list (line
, llen
, rl_point
- start
, &nw
, &cw
);
1358 /* If we skipped a NULL word at the beginning of the line, add it back */
1359 if (lwords
&& lwords
->word
&& cmd
[0] == 0 && lwords
->word
->word
[0] != 0)
1361 lw
= make_bare_word (cmd
);
1362 lwords
= make_word_list (lw
, lwords
);
1367 if (lwords
== 0 && llen
> 0)
1368 debug_printf ("ERROR: command_line_to_word_list returns NULL");
1369 else if (progcomp_debug
)
1371 debug_printf ("command_line_to_word_list -->");
1373 print_word_list (lwords
, "!");
1384 tmatches
= gen_shell_function_matches (cs
, cmd
, word
, line
, rl_point
- start
, lwords
, nw
, cw
, &foundf
);
1392 debug_printf ("gen_shell_function_matches (%p, %s, %s, %p, %d, %d) -->", cs
, cmd
, word
, lwords
, nw
, cw
);
1393 strlist_print (tmatches
, "\t");
1397 ret
= strlist_append (ret
, tmatches
);
1398 strlist_dispose (tmatches
);
1404 tmatches
= gen_command_matches (cs
, cmd
, word
, line
, rl_point
- start
, lwords
, nw
, cw
);
1410 debug_printf ("gen_command_matches (%p, %s, %s, %p, %d, %d) -->", cs
, cmd
, word
, lwords
, nw
, cw
);
1411 strlist_print (tmatches
, "\t");
1415 ret
= strlist_append (ret
, tmatches
);
1416 strlist_dispose (tmatches
);
1420 if (cs
->command
|| cs
->funcname
)
1423 dispose_words (lwords
);
1430 if (found
== 0 || (found
& PCOMP_RETRYFAIL
))
1432 strlist_dispose (ret
);
1438 tmatches
= filter_stringlist (ret
, cs
->filterpat
, word
);
1442 debug_printf ("filter_stringlist (%p, %s, %s) -->", ret
, cs
->filterpat
, word
);
1443 strlist_print (tmatches
, "\t");
1447 if (ret
&& ret
!= tmatches
)
1455 if (cs
->prefix
|| cs
->suffix
)
1456 ret
= strlist_prefix_suffix (ret
, cs
->prefix
, cs
->suffix
);
1458 /* If no matches have been generated and the user has specified that
1459 directory completion should be done as a default, call
1460 gen_action_completions again to generate a list of matching directory
1462 if ((ret
== 0 || ret
->list_len
== 0) && (cs
->options
& COPT_DIRNAMES
))
1464 tcs
= compspec_create ();
1465 tcs
->actions
= CA_DIRECTORY
;
1467 ret
= gen_action_completions (tcs
, word
);
1468 compspec_dispose (tcs
);
1470 else if (cs
->options
& COPT_PLUSDIRS
)
1472 tcs
= compspec_create ();
1473 tcs
->actions
= CA_DIRECTORY
;
1474 tmatches
= gen_action_completions (tcs
, word
);
1475 ret
= strlist_append (ret
, tmatches
);
1476 strlist_dispose (tmatches
);
1477 compspec_dispose (tcs
);
1484 pcomp_set_readline_variables (flags
, nval
)
1487 /* If the user specified that the compspec returns filenames, make
1488 sure that readline knows it. */
1489 if (flags
& COPT_FILENAMES
)
1490 rl_filename_completion_desired
= nval
;
1491 /* If the user doesn't want a space appended, tell readline. */
1492 if (flags
& COPT_NOSPACE
)
1493 rl_completion_suppress_append
= nval
;
1494 /* The value here is inverted, since the default is on and the `noquote'
1495 option is supposed to turn it off */
1496 if (flags
& COPT_NOQUOTE
)
1497 rl_filename_quoting_desired
= 1 - nval
;
1500 /* Set or unset FLAGS in the options word of the current compspec.
1501 SET_OR_UNSET is 1 for setting, 0 for unsetting. */
1503 pcomp_set_compspec_options (cs
, flags
, set_or_unset
)
1505 int flags
, set_or_unset
;
1507 if (cs
== 0 && ((cs
= pcomp_curcs
) == 0))
1510 cs
->options
|= flags
;
1512 cs
->options
&= ~flags
;
1516 gen_progcomp_completions (ocmd
, cmd
, word
, start
, end
, foundp
, retryp
, lastcs
)
1521 int *foundp
, *retryp
;
1524 COMPSPEC
*cs
, *oldcs
;
1528 cs
= progcomp_search (ocmd
);
1530 if (cs
== 0 || cs
== *lastcs
)
1540 compspec_dispose (*lastcs
);
1541 cs
->refcount
++; /* XXX */
1544 cs
= compspec_copy (cs
);
1546 oldcs
= pcomp_curcs
;
1547 oldcmd
= pcomp_curcmd
;
1552 ret
= gen_compspec_completions (cs
, cmd
, word
, start
, end
, foundp
);
1554 pcomp_curcs
= oldcs
;
1555 pcomp_curcmd
= oldcmd
;
1557 /* We need to conditionally handle setting *retryp here */
1559 *retryp
= foundp
&& (*foundp
& PCOMP_RETRYFAIL
);
1563 *foundp
&= ~PCOMP_RETRYFAIL
;
1564 *foundp
|= cs
->options
;
1567 compspec_dispose (cs
);
1571 /* The driver function for the programmable completion code. Returns a list
1572 of matches for WORD, which is an argument to command CMD. START and END
1573 bound the command currently being completed in rl_line_buffer. */
1575 programmable_completions (cmd
, word
, start
, end
, foundp
)
1578 int start
, end
, *foundp
;
1580 COMPSPEC
*cs
, *lastcs
;
1582 char **rmatches
, *t
;
1583 int found
, retry
, count
;
1592 /* We look at the basename of CMD if the full command does not have
1593 an associated COMPSPEC. */
1594 ret
= gen_progcomp_completions (cmd
, cmd
, word
, start
, end
, &found
, &retry
, &lastcs
);
1597 t
= strrchr (cmd
, '/');
1599 ret
= gen_progcomp_completions (t
, cmd
, word
, start
, end
, &found
, &retry
, &lastcs
);
1603 ret
= gen_progcomp_completions (DEFAULTCMD
, cmd
, word
, start
, end
, &found
, &retry
, &lastcs
);
1609 internal_warning ("programmable_completion: %s: possible retry loop", cmd
);
1617 rmatches
= ret
->list
;
1621 rmatches
= (char **)NULL
;
1626 if (lastcs
) /* XXX - should be while? */
1627 compspec_dispose (lastcs
);
1632 #endif /* PROGRAMMABLE_COMPLETION */