Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 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 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 7 | /* Copyright (C) 1993-2009 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 8 | |
| 9 | This file is part of GNU Bash, the Bourne Again SHell. |
| 10 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 11 | Bash is free software: you can redistribute it and/or modify |
| 12 | it under the terms of the GNU General Public License as published by |
| 13 | the Free Software Foundation, either version 3 of the License, or |
| 14 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 15 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 16 | Bash is distributed in the hope that it will be useful, |
| 17 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 21 | You should have received a copy of the GNU General Public License |
| 22 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
| 23 | */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 24 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 25 | #include "config.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 27 | #if defined (BRACE_EXPANSION) && defined (READLINE) |
| 28 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 31 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 32 | # ifdef _MINIX |
| 33 | # include <sys/types.h> |
| 34 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 35 | # include <unistd.h> |
| 36 | #endif |
| 37 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 38 | #include "bashansi.h" |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 39 | #include "shmbutil.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 40 | |
| 41 | #include "shell.h" |
| 42 | #include <readline/readline.h> |
| 43 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 44 | static int _strcompare __P((char **, char **)); |
| 45 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 46 | /* Find greatest common prefix of two strings. */ |
| 47 | static int |
| 48 | string_gcd (s1, s2) |
| 49 | char *s1, *s2; |
| 50 | { |
| 51 | register int i; |
| 52 | |
| 53 | if (s1 == NULL || s2 == NULL) |
| 54 | return (0); |
| 55 | |
| 56 | for (i = 0; *s1 && *s2; ++s1, ++s2, ++i) |
| 57 | { |
| 58 | if (*s1 != *s2) |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | return (i); |
| 63 | } |
| 64 | |
| 65 | static char * |
| 66 | really_munge_braces (array, real_start, real_end, gcd_zero) |
| 67 | char **array; |
| 68 | int real_start, real_end, gcd_zero; |
| 69 | { |
| 70 | int start, end, gcd; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 71 | char *result, *subterm, *x; |
| 72 | int result_size, flag, tlen; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 73 | |
| 74 | flag = 0; |
| 75 | |
| 76 | if (real_start == real_end) |
| 77 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame^] | 78 | x = array[real_start] ? sh_backslash_quote (array[real_start] + gcd_zero, 0, 0) |
| 79 | : sh_backslash_quote (array[0], 0, 0); |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 80 | return x; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 83 | result = (char *)xmalloc (result_size = 16); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | *result = '\0'; |
| 85 | |
| 86 | for (start = real_start; start < real_end; start = end + 1) |
| 87 | { |
| 88 | gcd = strlen (array[start]); |
| 89 | for (end = start + 1; end < real_end; end++) |
| 90 | { |
| 91 | int temp; |
| 92 | |
| 93 | temp = string_gcd (array[start], array[end]); |
| 94 | |
| 95 | if (temp <= gcd_zero) |
| 96 | break; |
| 97 | |
| 98 | gcd = temp; |
| 99 | } |
| 100 | end--; |
| 101 | |
| 102 | if (gcd_zero == 0 && start == real_start && end != (real_end - 1)) |
| 103 | { |
| 104 | /* In this case, add in a leading '{', because we are at |
| 105 | top level, and there isn't a consistent prefix. */ |
| 106 | result_size += 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 107 | result = (char *)xrealloc (result, result_size); |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 108 | result[0] = '{'; result[1] = '\0'; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 109 | flag++; |
| 110 | } |
| 111 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 112 | /* Make sure we backslash quote every substring we insert into the |
| 113 | resultant brace expression. This is so the default filename |
| 114 | quoting function won't inappropriately quote the braces. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 115 | if (start == end) |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 116 | { |
| 117 | x = savestring (array[start] + gcd_zero); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame^] | 118 | subterm = sh_backslash_quote (x, 0, 0); |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 119 | free (x); |
| 120 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 121 | else |
| 122 | { |
| 123 | /* If there is more than one element in the subarray, |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 124 | insert the (quoted) prefix and an opening brace. */ |
| 125 | tlen = gcd - gcd_zero; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 126 | x = (char *)xmalloc (tlen + 1); |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 127 | strncpy (x, array[start] + gcd_zero, tlen); |
| 128 | x[tlen] = '\0'; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame^] | 129 | subterm = sh_backslash_quote (x, 0, 0); |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 130 | free (x); |
| 131 | result_size += strlen (subterm) + 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 132 | result = (char *)xrealloc (result, result_size); |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 133 | strcat (result, subterm); |
| 134 | free (subterm); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 135 | strcat (result, "{"); |
| 136 | subterm = really_munge_braces (array, start, end + 1, gcd); |
| 137 | subterm[strlen (subterm) - 1] = '}'; |
| 138 | } |
| 139 | |
| 140 | result_size += strlen (subterm) + 1; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 141 | result = (char *)xrealloc (result, result_size); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 142 | strcat (result, subterm); |
| 143 | strcat (result, ","); |
| 144 | free (subterm); |
| 145 | } |
| 146 | |
| 147 | if (gcd_zero == 0) |
| 148 | result[strlen (result) - 1] = flag ? '}' : '\0'; |
| 149 | return (result); |
| 150 | } |
| 151 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 152 | static int |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 153 | _strcompare (s1, s2) |
| 154 | char **s1, **s2; |
| 155 | { |
| 156 | int result; |
| 157 | |
| 158 | result = **s1 - **s2; |
| 159 | if (result == 0) |
| 160 | result = strcmp (*s1, *s2); |
| 161 | |
| 162 | return result; |
| 163 | } |
| 164 | |
| 165 | static int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 166 | hack_braces_completion (names) |
| 167 | char **names; |
| 168 | { |
| 169 | register int i; |
| 170 | char *temp; |
| 171 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 172 | i = strvec_len (names); |
| 173 | if (MB_CUR_MAX > 1 && i > 2) |
| 174 | qsort (names+1, i-1, sizeof (char *), (QSFUNC *)_strcompare); |
| 175 | |
| 176 | temp = really_munge_braces (names, 1, i, 0); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 177 | |
| 178 | for (i = 0; names[i]; ++i) |
| 179 | { |
| 180 | free (names[i]); |
| 181 | names[i] = NULL; |
| 182 | } |
| 183 | names[0] = temp; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 184 | return 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 187 | /* We handle quoting ourselves within hack_braces_completion, so we turn off |
| 188 | rl_filename_quoting_desired and rl_filename_quoting_function. */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 189 | int |
| 190 | bash_brace_completion (count, ignore) |
| 191 | int count, ignore; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 192 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 193 | rl_compignore_func_t *orig_ignore_func; |
| 194 | rl_compentry_func_t *orig_entry_func; |
| 195 | rl_quote_func_t *orig_quoting_func; |
| 196 | rl_completion_func_t *orig_attempt_func; |
| 197 | int orig_quoting_desired, r; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 198 | |
| 199 | orig_ignore_func = rl_ignore_some_completions_function; |
| 200 | orig_attempt_func = rl_attempted_completion_function; |
| 201 | orig_entry_func = rl_completion_entry_function; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 202 | orig_quoting_func = rl_filename_quoting_function; |
| 203 | orig_quoting_desired = rl_filename_quoting_desired; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 204 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 205 | rl_completion_entry_function = rl_filename_completion_function; |
| 206 | rl_attempted_completion_function = (rl_completion_func_t *)NULL; |
| 207 | rl_ignore_some_completions_function = hack_braces_completion; |
| 208 | rl_filename_quoting_function = (rl_quote_func_t *)NULL; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 209 | rl_filename_quoting_desired = 0; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 210 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 211 | r = rl_complete_internal (TAB); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 212 | |
| 213 | rl_ignore_some_completions_function = orig_ignore_func; |
| 214 | rl_attempted_completion_function = orig_attempt_func; |
| 215 | rl_completion_entry_function = orig_entry_func; |
Jari Aalto | e8ce775 | 1997-09-22 20:22:27 +0000 | [diff] [blame] | 216 | rl_filename_quoting_function = orig_quoting_func; |
| 217 | rl_filename_quoting_desired = orig_quoting_desired; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 218 | |
| 219 | return r; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 220 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 221 | #endif /* BRACE_EXPANSION && READLINE */ |