]>
Commit | Line | Data |
---|---|---|
726f6388 JA |
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 | |
bb70624e | 23 | Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */ |
726f6388 | 24 | |
ccc6cda3 JA |
25 | #include "config.h" |
26 | #if defined (BRACE_EXPANSION) && defined (READLINE) | |
27 | ||
726f6388 JA |
28 | #include <stdio.h> |
29 | ||
ccc6cda3 | 30 | #if defined (HAVE_UNISTD_H) |
cce855bc JA |
31 | # ifdef _MINIX |
32 | # include <sys/types.h> | |
33 | # endif | |
ccc6cda3 JA |
34 | # include <unistd.h> |
35 | #endif | |
36 | ||
d166f048 | 37 | #include "bashansi.h" |
726f6388 JA |
38 | |
39 | #include "shell.h" | |
40 | #include <readline/readline.h> | |
41 | ||
28ef6c31 | 42 | extern char *sh_backslash_quote (); |
e8ce775d | 43 | |
726f6388 JA |
44 | /* Find greatest common prefix of two strings. */ |
45 | static int | |
46 | string_gcd (s1, s2) | |
47 | char *s1, *s2; | |
48 | { | |
49 | register int i; | |
50 | ||
51 | if (s1 == NULL || s2 == NULL) | |
52 | return (0); | |
53 | ||
54 | for (i = 0; *s1 && *s2; ++s1, ++s2, ++i) | |
55 | { | |
56 | if (*s1 != *s2) | |
57 | break; | |
58 | } | |
59 | ||
60 | return (i); | |
61 | } | |
62 | ||
63 | static char * | |
64 | really_munge_braces (array, real_start, real_end, gcd_zero) | |
65 | char **array; | |
66 | int real_start, real_end, gcd_zero; | |
67 | { | |
68 | int start, end, gcd; | |
e8ce775d JA |
69 | char *result, *subterm, *x; |
70 | int result_size, flag, tlen; | |
726f6388 JA |
71 | |
72 | flag = 0; | |
73 | ||
74 | if (real_start == real_end) | |
75 | { | |
28ef6c31 JA |
76 | x = array[real_start] ? sh_backslash_quote (array[real_start] + gcd_zero) |
77 | : sh_backslash_quote (array[0]); | |
e8ce775d | 78 | return x; |
726f6388 JA |
79 | } |
80 | ||
e8ce775d | 81 | result = xmalloc (result_size = 16); |
726f6388 JA |
82 | *result = '\0'; |
83 | ||
84 | for (start = real_start; start < real_end; start = end + 1) | |
85 | { | |
86 | gcd = strlen (array[start]); | |
87 | for (end = start + 1; end < real_end; end++) | |
88 | { | |
89 | int temp; | |
90 | ||
91 | temp = string_gcd (array[start], array[end]); | |
92 | ||
93 | if (temp <= gcd_zero) | |
94 | break; | |
95 | ||
96 | gcd = temp; | |
97 | } | |
98 | end--; | |
99 | ||
100 | if (gcd_zero == 0 && start == real_start && end != (real_end - 1)) | |
101 | { | |
102 | /* In this case, add in a leading '{', because we are at | |
103 | top level, and there isn't a consistent prefix. */ | |
104 | result_size += 1; | |
ccc6cda3 | 105 | result = xrealloc (result, result_size); |
d166f048 | 106 | result[0] = '{'; result[1] = '\0'; |
726f6388 JA |
107 | flag++; |
108 | } | |
109 | ||
e8ce775d JA |
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. */ | |
726f6388 | 113 | if (start == end) |
e8ce775d JA |
114 | { |
115 | x = savestring (array[start] + gcd_zero); | |
28ef6c31 | 116 | subterm = sh_backslash_quote (x); |
e8ce775d JA |
117 | free (x); |
118 | } | |
726f6388 JA |
119 | else |
120 | { | |
121 | /* If there is more than one element in the subarray, | |
e8ce775d JA |
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); | |
126 | x[tlen] = '\0'; | |
28ef6c31 | 127 | subterm = sh_backslash_quote (x); |
e8ce775d JA |
128 | free (x); |
129 | result_size += strlen (subterm) + 1; | |
ccc6cda3 | 130 | result = xrealloc (result, result_size); |
e8ce775d JA |
131 | strcat (result, subterm); |
132 | free (subterm); | |
726f6388 JA |
133 | strcat (result, "{"); |
134 | subterm = really_munge_braces (array, start, end + 1, gcd); | |
135 | subterm[strlen (subterm) - 1] = '}'; | |
136 | } | |
137 | ||
138 | result_size += strlen (subterm) + 1; | |
ccc6cda3 | 139 | result = xrealloc (result, result_size); |
726f6388 JA |
140 | strcat (result, subterm); |
141 | strcat (result, ","); | |
142 | free (subterm); | |
143 | } | |
144 | ||
145 | if (gcd_zero == 0) | |
146 | result[strlen (result) - 1] = flag ? '}' : '\0'; | |
147 | return (result); | |
148 | } | |
149 | ||
28ef6c31 | 150 | static int |
726f6388 JA |
151 | hack_braces_completion (names) |
152 | char **names; | |
153 | { | |
154 | register int i; | |
155 | char *temp; | |
156 | ||
157 | temp = really_munge_braces (names, 1, array_len (names), 0); | |
158 | ||
159 | for (i = 0; names[i]; ++i) | |
160 | { | |
161 | free (names[i]); | |
162 | names[i] = NULL; | |
163 | } | |
164 | names[0] = temp; | |
28ef6c31 | 165 | return 0; |
726f6388 JA |
166 | } |
167 | ||
e8ce775d JA |
168 | /* We handle quoting ourselves within hack_braces_completion, so we turn off |
169 | rl_filename_quoting_desired and rl_filename_quoting_function. */ | |
28ef6c31 JA |
170 | int |
171 | bash_brace_completion (count, ignore) | |
172 | int count, ignore; | |
726f6388 | 173 | { |
28ef6c31 JA |
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; | |
726f6388 JA |
179 | |
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; | |
e8ce775d JA |
183 | orig_quoting_func = rl_filename_quoting_function; |
184 | orig_quoting_desired = rl_filename_quoting_desired; | |
726f6388 | 185 | |
28ef6c31 JA |
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; | |
e8ce775d | 190 | rl_filename_quoting_desired = 0; |
726f6388 | 191 | |
28ef6c31 | 192 | r = rl_complete_internal (TAB); |
726f6388 JA |
193 | |
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; | |
e8ce775d JA |
197 | rl_filename_quoting_function = orig_quoting_func; |
198 | rl_filename_quoting_desired = orig_quoting_desired; | |
28ef6c31 JA |
199 | |
200 | return r; | |
726f6388 | 201 | } |
ccc6cda3 | 202 | #endif /* BRACE_EXPANSION && READLINE */ |