]>
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 | |
23 | Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
24 | ||
ccc6cda3 JA |
25 | #include "config.h" |
26 | #if defined (BRACE_EXPANSION) && defined (READLINE) | |
27 | ||
726f6388 JA |
28 | #include <stdio.h> |
29 | ||
ccc6cda3 JA |
30 | #if defined (HAVE_UNISTD_H) |
31 | # include <unistd.h> | |
32 | #endif | |
33 | ||
726f6388 JA |
34 | #if defined (HAVE_STRING_H) |
35 | # include <string.h> | |
36 | #else /* !HAVE_STRING_H */ | |
37 | # include <strings.h> | |
38 | #endif /* !HAVE_STRING_H */ | |
39 | ||
40 | #include "shell.h" | |
41 | #include <readline/readline.h> | |
42 | ||
43 | /* Find greatest common prefix of two strings. */ | |
44 | static int | |
45 | string_gcd (s1, s2) | |
46 | char *s1, *s2; | |
47 | { | |
48 | register int i; | |
49 | ||
50 | if (s1 == NULL || s2 == NULL) | |
51 | return (0); | |
52 | ||
53 | for (i = 0; *s1 && *s2; ++s1, ++s2, ++i) | |
54 | { | |
55 | if (*s1 != *s2) | |
56 | break; | |
57 | } | |
58 | ||
59 | return (i); | |
60 | } | |
61 | ||
62 | static char * | |
63 | really_munge_braces (array, real_start, real_end, gcd_zero) | |
64 | char **array; | |
65 | int real_start, real_end, gcd_zero; | |
66 | { | |
67 | int start, end, gcd; | |
68 | char *result, *subterm; | |
69 | int result_size, flag; | |
70 | ||
71 | flag = 0; | |
72 | ||
73 | if (real_start == real_end) | |
74 | { | |
75 | if (array[real_start]) | |
76 | return (savestring (array[real_start] + gcd_zero)); | |
77 | else | |
78 | return (savestring (array[0])); | |
79 | } | |
80 | ||
ccc6cda3 | 81 | result = xmalloc (result_size = 1); |
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); |
726f6388 JA |
106 | strcpy (result, "{"); |
107 | flag++; | |
108 | } | |
109 | ||
110 | if (start == end) | |
111 | subterm = savestring (array[start] + gcd_zero); | |
112 | else | |
113 | { | |
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; | |
ccc6cda3 | 117 | result = xrealloc (result, result_size); |
726f6388 JA |
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] = '}'; | |
122 | } | |
123 | ||
124 | result_size += strlen (subterm) + 1; | |
ccc6cda3 | 125 | result = xrealloc (result, result_size); |
726f6388 JA |
126 | strcat (result, subterm); |
127 | strcat (result, ","); | |
128 | free (subterm); | |
129 | } | |
130 | ||
131 | if (gcd_zero == 0) | |
132 | result[strlen (result) - 1] = flag ? '}' : '\0'; | |
133 | return (result); | |
134 | } | |
135 | ||
136 | static void | |
137 | hack_braces_completion (names) | |
138 | char **names; | |
139 | { | |
140 | register int i; | |
141 | char *temp; | |
142 | ||
143 | temp = really_munge_braces (names, 1, array_len (names), 0); | |
144 | ||
145 | for (i = 0; names[i]; ++i) | |
146 | { | |
147 | free (names[i]); | |
148 | names[i] = NULL; | |
149 | } | |
150 | names[0] = temp; | |
151 | } | |
152 | ||
153 | void | |
154 | bash_brace_completion () | |
155 | { | |
156 | Function *orig_ignore_func; | |
157 | Function *orig_entry_func; | |
158 | CPPFunction *orig_attempt_func; | |
159 | ||
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; | |
163 | ||
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; | |
167 | ||
168 | rl_complete_internal (TAB); | |
169 | ||
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; | |
173 | } | |
ccc6cda3 | 174 | #endif /* BRACE_EXPANSION && READLINE */ |