]> git.ipfire.org Git - thirdparty/bash.git/blob - bracecomp.c
Imported from ../bash-2.01.1.tar.gz.
[thirdparty/bash.git] / bracecomp.c
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
3 shell. */
4
5 /* Original version by tromey@cns.caltech.edu, Fri Feb 7 1992. */
6
7 /* Copyright (C) 1993 Free Software Foundation, Inc.
8
9 This file is part of GNU Bash, the Bourne Again SHell.
10
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
14 version.
15
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
19 for more details.
20
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. */
24
25 #include "config.h"
26 #if defined (BRACE_EXPANSION) && defined (READLINE)
27
28 #include <stdio.h>
29
30 #if defined (HAVE_UNISTD_H)
31 # include <unistd.h>
32 #endif
33
34 #include "bashansi.h"
35
36 #include "shell.h"
37 #include <readline/readline.h>
38
39 extern char *backslash_quote ();
40
41 /* Find greatest common prefix of two strings. */
42 static int
43 string_gcd (s1, s2)
44 char *s1, *s2;
45 {
46 register int i;
47
48 if (s1 == NULL || s2 == NULL)
49 return (0);
50
51 for (i = 0; *s1 && *s2; ++s1, ++s2, ++i)
52 {
53 if (*s1 != *s2)
54 break;
55 }
56
57 return (i);
58 }
59
60 static char *
61 really_munge_braces (array, real_start, real_end, gcd_zero)
62 char **array;
63 int real_start, real_end, gcd_zero;
64 {
65 int start, end, gcd;
66 char *result, *subterm, *x;
67 int result_size, flag, tlen;
68
69 flag = 0;
70
71 if (real_start == real_end)
72 {
73 x = array[real_start] ? backslash_quote (array[real_start] + gcd_zero)
74 : backslash_quote (array[0]);
75 return x;
76 }
77
78 result = xmalloc (result_size = 16);
79 *result = '\0';
80
81 for (start = real_start; start < real_end; start = end + 1)
82 {
83 gcd = strlen (array[start]);
84 for (end = start + 1; end < real_end; end++)
85 {
86 int temp;
87
88 temp = string_gcd (array[start], array[end]);
89
90 if (temp <= gcd_zero)
91 break;
92
93 gcd = temp;
94 }
95 end--;
96
97 if (gcd_zero == 0 && start == real_start && end != (real_end - 1))
98 {
99 /* In this case, add in a leading '{', because we are at
100 top level, and there isn't a consistent prefix. */
101 result_size += 1;
102 result = xrealloc (result, result_size);
103 result[0] = '{'; result[1] = '\0';
104 flag++;
105 }
106
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. */
110 if (start == end)
111 {
112 x = savestring (array[start] + gcd_zero);
113 subterm = backslash_quote (x);
114 free (x);
115 }
116 else
117 {
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);
123 x[tlen] = '\0';
124 subterm = backslash_quote (x);
125 free (x);
126 result_size += strlen (subterm) + 1;
127 result = xrealloc (result, result_size);
128 strcat (result, subterm);
129 free (subterm);
130 strcat (result, "{");
131 subterm = really_munge_braces (array, start, end + 1, gcd);
132 subterm[strlen (subterm) - 1] = '}';
133 }
134
135 result_size += strlen (subterm) + 1;
136 result = xrealloc (result, result_size);
137 strcat (result, subterm);
138 strcat (result, ",");
139 free (subterm);
140 }
141
142 if (gcd_zero == 0)
143 result[strlen (result) - 1] = flag ? '}' : '\0';
144 return (result);
145 }
146
147 static void
148 hack_braces_completion (names)
149 char **names;
150 {
151 register int i;
152 char *temp;
153
154 temp = really_munge_braces (names, 1, array_len (names), 0);
155
156 for (i = 0; names[i]; ++i)
157 {
158 free (names[i]);
159 names[i] = NULL;
160 }
161 names[0] = temp;
162 }
163
164 /* We handle quoting ourselves within hack_braces_completion, so we turn off
165 rl_filename_quoting_desired and rl_filename_quoting_function. */
166 void
167 bash_brace_completion ()
168 {
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;
174
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;
180
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;
186
187 rl_complete_internal (TAB);
188
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;
194 }
195 #endif /* BRACE_EXPANSION && READLINE */