Imported from ../bash-2.04.tar.gz.
[platform/upstream/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, 59 Temple Place, Suite 330, Boston, MA 02111 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 #  ifdef _MINIX
32 #    include <sys/types.h>
33 #  endif
34 #  include <unistd.h>
35 #endif
36
37 #include "bashansi.h"
38
39 #include "shell.h"
40 #include <readline/readline.h>
41
42 extern char *backslash_quote ();
43
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;
69   char *result, *subterm, *x;
70   int result_size, flag, tlen;
71
72   flag = 0;
73
74   if (real_start == real_end)
75     {
76       x = array[real_start] ? backslash_quote (array[real_start] + gcd_zero)
77                             : backslash_quote (array[0]);
78       return x;
79     }
80
81   result = xmalloc (result_size = 16);
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;
105           result = xrealloc (result, result_size);
106           result[0] = '{'; result[1] = '\0';
107           flag++;
108         }
109
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. */
113       if (start == end)
114         {
115           x = savestring (array[start] + gcd_zero);
116           subterm = backslash_quote (x);
117           free (x);
118         }
119       else
120         {
121           /* If there is more than one element in the subarray,
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';
127           subterm = backslash_quote (x);
128           free (x);
129           result_size += strlen (subterm) + 1;
130           result = xrealloc (result, result_size);
131           strcat (result, subterm);
132           free (subterm);
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;
139       result = xrealloc (result, result_size);
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
150 static void
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;
165 }
166
167 /* We handle quoting ourselves within hack_braces_completion, so we turn off
168    rl_filename_quoting_desired and rl_filename_quoting_function. */
169 void
170 bash_brace_completion ()
171 {
172   Function *orig_ignore_func;
173   Function *orig_entry_func;
174   CPFunction *orig_quoting_func;
175   CPPFunction *orig_attempt_func;
176   int orig_quoting_desired;
177
178   orig_ignore_func = rl_ignore_some_completions_function;
179   orig_attempt_func = rl_attempted_completion_function;
180   orig_entry_func = rl_completion_entry_function;
181   orig_quoting_func = rl_filename_quoting_function;
182   orig_quoting_desired = rl_filename_quoting_desired;
183
184   rl_completion_entry_function = (Function *) filename_completion_function;
185   rl_attempted_completion_function = NULL;
186   rl_ignore_some_completions_function = (Function *) hack_braces_completion;
187   rl_filename_quoting_function = NULL;
188   rl_filename_quoting_desired = 0;
189
190   rl_complete_internal (TAB);
191
192   rl_ignore_some_completions_function = orig_ignore_func;
193   rl_attempted_completion_function = orig_attempt_func;
194   rl_completion_entry_function = orig_entry_func;
195   rl_filename_quoting_function = orig_quoting_func;
196   rl_filename_quoting_desired = orig_quoting_desired;
197 }
198 #endif /* BRACE_EXPANSION && READLINE */