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)
34 #if defined (HAVE_STRING_H)
36 #else /* !HAVE_STRING_H */
38 #endif /* !HAVE_STRING_H */
41 #include <readline/readline.h>
43 /* Find greatest common prefix of two strings. */
50 if (s1
== NULL
|| s2
== NULL
)
53 for (i
= 0; *s1
&& *s2
; ++s1
, ++s2
, ++i
)
63 really_munge_braces (array
, real_start
, real_end
, gcd_zero
)
65 int real_start
, real_end
, gcd_zero
;
68 char *result
, *subterm
;
69 int result_size
, flag
;
73 if (real_start
== real_end
)
75 if (array
[real_start
])
76 return (savestring (array
[real_start
] + gcd_zero
));
78 return (savestring (array
[0]));
81 result
= xmalloc (result_size
= 1);
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 strcpy (result
, "{");
111 subterm
= savestring (array
[start
] + gcd_zero
);
114 /* If there is more than one element in the subarray,
115 insert the prefix and an opening brace. */
116 result_size
+= gcd
- gcd_zero
+ 1;
117 result
= xrealloc (result
, result_size
);
118 strncat (result
, array
[start
] + gcd_zero
, gcd
- gcd_zero
);
119 strcat (result
, "{");
120 subterm
= really_munge_braces (array
, start
, end
+ 1, gcd
);
121 subterm
[strlen (subterm
) - 1] = '}';
124 result_size
+= strlen (subterm
) + 1;
125 result
= xrealloc (result
, result_size
);
126 strcat (result
, subterm
);
127 strcat (result
, ",");
132 result
[strlen (result
) - 1] = flag
? '}' : '\0';
137 hack_braces_completion (names
)
143 temp
= really_munge_braces (names
, 1, array_len (names
), 0);
145 for (i
= 0; names
[i
]; ++i
)
154 bash_brace_completion ()
156 Function
*orig_ignore_func
;
157 Function
*orig_entry_func
;
158 CPPFunction
*orig_attempt_func
;
160 orig_ignore_func
= rl_ignore_some_completions_function
;
161 orig_attempt_func
= rl_attempted_completion_function
;
162 orig_entry_func
= rl_completion_entry_function
;
164 rl_completion_entry_function
= (Function
*) filename_completion_function
;
165 rl_attempted_completion_function
= NULL
;
166 rl_ignore_some_completions_function
= (Function
*) hack_braces_completion
;
168 rl_complete_internal (TAB
);
170 rl_ignore_some_completions_function
= orig_ignore_func
;
171 rl_attempted_completion_function
= orig_attempt_func
;
172 rl_completion_entry_function
= orig_entry_func
;
174 #endif /* BRACE_EXPANSION && READLINE */