1 /* bracecomp.c -- Complete a filename with the possible completions enclosed
2 in csh-style braces such that the list of completions is available to the
5 /* Original version by tromey@cns.caltech.edu, Fri Feb 7 1992. */
7 /* Copyright (C) 1993 Free Software Foundation, Inc.
9 This file is part of GNU Bash, the Bourne Again SHell.
11 Bash is free software; you can redistribute it and/or modify it under
12 the terms of the GNU General Public License as published by the Free
13 Software Foundation; either version 2, or (at your option) any later
16 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
17 WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 You should have received a copy of the GNU General Public License along
22 with Bash; see the file COPYING. If not, write to the Free Software
23 Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
26 #if defined (BRACE_EXPANSION) && defined (READLINE)
30 #if defined (HAVE_UNISTD_H)
37 #include <readline/readline.h>
39 /* Find greatest common prefix of two strings. */
46 if (s1 == NULL || s2 == NULL)
49 for (i = 0; *s1 && *s2; ++s1, ++s2, ++i)
59 really_munge_braces (array, real_start, real_end, gcd_zero)
61 int real_start, real_end, gcd_zero;
64 char *result, *subterm;
65 int result_size, flag;
69 if (real_start == real_end)
71 if (array[real_start])
72 return (savestring (array[real_start] + gcd_zero));
74 return (savestring (array[0]));
77 result = xmalloc (result_size = 1);
80 for (start = real_start; start < real_end; start = end + 1)
82 gcd = strlen (array[start]);
83 for (end = start + 1; end < real_end; end++)
87 temp = string_gcd (array[start], array[end]);
96 if (gcd_zero == 0 && start == real_start && end != (real_end - 1))
98 /* In this case, add in a leading '{', because we are at
99 top level, and there isn't a consistent prefix. */
101 result = xrealloc (result, result_size);
102 result[0] = '{'; result[1] = '\0';
107 subterm = savestring (array[start] + gcd_zero);
110 /* If there is more than one element in the subarray,
111 insert the prefix and an opening brace. */
112 result_size += gcd - gcd_zero + 1;
113 result = xrealloc (result, result_size);
114 strncat (result, array[start] + gcd_zero, gcd - gcd_zero);
115 strcat (result, "{");
116 subterm = really_munge_braces (array, start, end + 1, gcd);
117 subterm[strlen (subterm) - 1] = '}';
120 result_size += strlen (subterm) + 1;
121 result = xrealloc (result, result_size);
122 strcat (result, subterm);
123 strcat (result, ",");
128 result[strlen (result) - 1] = flag ? '}' : '\0';
133 hack_braces_completion (names)
139 temp = really_munge_braces (names, 1, array_len (names), 0);
141 for (i = 0; names[i]; ++i)
150 bash_brace_completion ()
152 Function *orig_ignore_func;
153 Function *orig_entry_func;
154 CPPFunction *orig_attempt_func;
156 orig_ignore_func = rl_ignore_some_completions_function;
157 orig_attempt_func = rl_attempted_completion_function;
158 orig_entry_func = rl_completion_entry_function;
160 rl_completion_entry_function = (Function *) filename_completion_function;
161 rl_attempted_completion_function = NULL;
162 rl_ignore_some_completions_function = (Function *) hack_braces_completion;
164 rl_complete_internal (TAB);
166 rl_ignore_some_completions_function = orig_ignore_func;
167 rl_attempted_completion_function = orig_attempt_func;
168 rl_completion_entry_function = orig_entry_func;
170 #endif /* BRACE_EXPANSION && READLINE */