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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
26 #if defined (BRACE_EXPANSION) && defined (READLINE)
30 #if defined (HAVE_UNISTD_H)
32 # include <sys/types.h>
40 #include <readline/readline.h>
42 extern char *sh_backslash_quote ();
44 /* Find greatest common prefix of two strings. */
51 if (s1
== NULL
|| s2
== NULL
)
54 for (i
= 0; *s1
&& *s2
; ++s1
, ++s2
, ++i
)
64 really_munge_braces (array
, real_start
, real_end
, gcd_zero
)
66 int real_start
, real_end
, gcd_zero
;
69 char *result
, *subterm
, *x
;
70 int result_size
, flag
, tlen
;
74 if (real_start
== real_end
)
76 x
= array
[real_start
] ? sh_backslash_quote (array
[real_start
] + gcd_zero
)
77 : sh_backslash_quote (array
[0]);
81 result
= xmalloc (result_size
= 16);
84 for (start
= real_start
; start
< real_end
; start
= end
+ 1)
86 gcd
= strlen (array
[start
]);
87 for (end
= start
+ 1; end
< real_end
; end
++)
91 temp
= string_gcd (array
[start
], array
[end
]);
100 if (gcd_zero
== 0 && start
== real_start
&& end
!= (real_end
- 1))
102 /* In this case, add in a leading '{', because we are at
103 top level, and there isn't a consistent prefix. */
105 result
= xrealloc (result
, result_size
);
106 result
[0] = '{'; result
[1] = '\0';
110 /* Make sure we backslash quote every substring we insert into the
111 resultant brace expression. This is so the default filename
112 quoting function won't inappropriately quote the braces. */
115 x
= savestring (array
[start
] + gcd_zero
);
116 subterm
= sh_backslash_quote (x
);
121 /* If there is more than one element in the subarray,
122 insert the (quoted) prefix and an opening brace. */
123 tlen
= gcd
- gcd_zero
;
124 x
= xmalloc (tlen
+ 1);
125 strncpy (x
, array
[start
] + gcd_zero
, tlen
);
127 subterm
= sh_backslash_quote (x
);
129 result_size
+= strlen (subterm
) + 1;
130 result
= xrealloc (result
, result_size
);
131 strcat (result
, subterm
);
133 strcat (result
, "{");
134 subterm
= really_munge_braces (array
, start
, end
+ 1, gcd
);
135 subterm
[strlen (subterm
) - 1] = '}';
138 result_size
+= strlen (subterm
) + 1;
139 result
= xrealloc (result
, result_size
);
140 strcat (result
, subterm
);
141 strcat (result
, ",");
146 result
[strlen (result
) - 1] = flag
? '}' : '\0';
151 hack_braces_completion (names
)
157 temp
= really_munge_braces (names
, 1, array_len (names
), 0);
159 for (i
= 0; names
[i
]; ++i
)
168 /* We handle quoting ourselves within hack_braces_completion, so we turn off
169 rl_filename_quoting_desired and rl_filename_quoting_function. */
171 bash_brace_completion (count
, ignore
)
174 rl_compignore_func_t
*orig_ignore_func
;
175 rl_compentry_func_t
*orig_entry_func
;
176 rl_quote_func_t
*orig_quoting_func
;
177 rl_completion_func_t
*orig_attempt_func
;
178 int orig_quoting_desired
, r
;
180 orig_ignore_func
= rl_ignore_some_completions_function
;
181 orig_attempt_func
= rl_attempted_completion_function
;
182 orig_entry_func
= rl_completion_entry_function
;
183 orig_quoting_func
= rl_filename_quoting_function
;
184 orig_quoting_desired
= rl_filename_quoting_desired
;
186 rl_completion_entry_function
= rl_filename_completion_function
;
187 rl_attempted_completion_function
= (rl_completion_func_t
*)NULL
;
188 rl_ignore_some_completions_function
= hack_braces_completion
;
189 rl_filename_quoting_function
= (rl_quote_func_t
*)NULL
;
190 rl_filename_quoting_desired
= 0;
192 r
= rl_complete_internal (TAB
);
194 rl_ignore_some_completions_function
= orig_ignore_func
;
195 rl_attempted_completion_function
= orig_attempt_func
;
196 rl_completion_entry_function
= orig_entry_func
;
197 rl_filename_quoting_function
= orig_quoting_func
;
198 rl_filename_quoting_desired
= orig_quoting_desired
;
202 #endif /* BRACE_EXPANSION && READLINE */