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 extern char *backslash_quote ();
41 /* Find greatest common prefix of two strings. */
48 if (s1
== NULL
|| s2
== NULL
)
51 for (i
= 0; *s1
&& *s2
; ++s1
, ++s2
, ++i
)
61 really_munge_braces (array
, real_start
, real_end
, gcd_zero
)
63 int real_start
, real_end
, gcd_zero
;
66 char *result
, *subterm
, *x
;
67 int result_size
, flag
, tlen
;
71 if (real_start
== real_end
)
73 x
= array
[real_start
] ? backslash_quote (array
[real_start
] + gcd_zero
)
74 : backslash_quote (array
[0]);
78 result
= xmalloc (result_size
= 16);
81 for (start
= real_start
; start
< real_end
; start
= end
+ 1)
83 gcd
= strlen (array
[start
]);
84 for (end
= start
+ 1; end
< real_end
; end
++)
88 temp
= string_gcd (array
[start
], array
[end
]);
97 if (gcd_zero
== 0 && start
== real_start
&& end
!= (real_end
- 1))
99 /* In this case, add in a leading '{', because we are at
100 top level, and there isn't a consistent prefix. */
102 result
= xrealloc (result
, result_size
);
103 result
[0] = '{'; result
[1] = '\0';
107 /* Make sure we backslash quote every substring we insert into the
108 resultant brace expression. This is so the default filename
109 quoting function won't inappropriately quote the braces. */
112 x
= savestring (array
[start
] + gcd_zero
);
113 subterm
= backslash_quote (x
);
118 /* If there is more than one element in the subarray,
119 insert the (quoted) prefix and an opening brace. */
120 tlen
= gcd
- gcd_zero
;
121 x
= xmalloc (tlen
+ 1);
122 strncpy (x
, array
[start
] + gcd_zero
, tlen
);
124 subterm
= backslash_quote (x
);
126 result_size
+= strlen (subterm
) + 1;
127 result
= xrealloc (result
, result_size
);
128 strcat (result
, subterm
);
130 strcat (result
, "{");
131 subterm
= really_munge_braces (array
, start
, end
+ 1, gcd
);
132 subterm
[strlen (subterm
) - 1] = '}';
135 result_size
+= strlen (subterm
) + 1;
136 result
= xrealloc (result
, result_size
);
137 strcat (result
, subterm
);
138 strcat (result
, ",");
143 result
[strlen (result
) - 1] = flag
? '}' : '\0';
148 hack_braces_completion (names
)
154 temp
= really_munge_braces (names
, 1, array_len (names
), 0);
156 for (i
= 0; names
[i
]; ++i
)
164 /* We handle quoting ourselves within hack_braces_completion, so we turn off
165 rl_filename_quoting_desired and rl_filename_quoting_function. */
167 bash_brace_completion ()
169 Function
*orig_ignore_func
;
170 Function
*orig_entry_func
;
171 CPFunction
*orig_quoting_func
;
172 CPPFunction
*orig_attempt_func
;
173 int orig_quoting_desired
;
175 orig_ignore_func
= rl_ignore_some_completions_function
;
176 orig_attempt_func
= rl_attempted_completion_function
;
177 orig_entry_func
= rl_completion_entry_function
;
178 orig_quoting_func
= rl_filename_quoting_function
;
179 orig_quoting_desired
= rl_filename_quoting_desired
;
181 rl_completion_entry_function
= (Function
*) filename_completion_function
;
182 rl_attempted_completion_function
= NULL
;
183 rl_ignore_some_completions_function
= (Function
*) hack_braces_completion
;
184 rl_filename_quoting_function
= NULL
;
185 rl_filename_quoting_desired
= 0;
187 rl_complete_internal (TAB
);
189 rl_ignore_some_completions_function
= orig_ignore_func
;
190 rl_attempted_completion_function
= orig_attempt_func
;
191 rl_completion_entry_function
= orig_entry_func
;
192 rl_filename_quoting_function
= orig_quoting_func
;
193 rl_filename_quoting_desired
= orig_quoting_desired
;
195 #endif /* BRACE_EXPANSION && READLINE */