]> git.ipfire.org Git - thirdparty/bash.git/blame - bracecomp.c
Imported from ../bash-2.01.tar.gz.
[thirdparty/bash.git] / bracecomp.c
CommitLineData
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
d166f048 34#include "bashansi.h"
726f6388
JA
35
36#include "shell.h"
37#include <readline/readline.h>
38
39/* Find greatest common prefix of two strings. */
40static int
41string_gcd (s1, s2)
42 char *s1, *s2;
43{
44 register int i;
45
46 if (s1 == NULL || s2 == NULL)
47 return (0);
48
49 for (i = 0; *s1 && *s2; ++s1, ++s2, ++i)
50 {
51 if (*s1 != *s2)
52 break;
53 }
54
55 return (i);
56}
57
58static char *
59really_munge_braces (array, real_start, real_end, gcd_zero)
60 char **array;
61 int real_start, real_end, gcd_zero;
62{
63 int start, end, gcd;
64 char *result, *subterm;
65 int result_size, flag;
66
67 flag = 0;
68
69 if (real_start == real_end)
70 {
71 if (array[real_start])
72 return (savestring (array[real_start] + gcd_zero));
73 else
74 return (savestring (array[0]));
75 }
76
ccc6cda3 77 result = xmalloc (result_size = 1);
726f6388
JA
78 *result = '\0';
79
80 for (start = real_start; start < real_end; start = end + 1)
81 {
82 gcd = strlen (array[start]);
83 for (end = start + 1; end < real_end; end++)
84 {
85 int temp;
86
87 temp = string_gcd (array[start], array[end]);
88
89 if (temp <= gcd_zero)
90 break;
91
92 gcd = temp;
93 }
94 end--;
95
96 if (gcd_zero == 0 && start == real_start && end != (real_end - 1))
97 {
98 /* In this case, add in a leading '{', because we are at
99 top level, and there isn't a consistent prefix. */
100 result_size += 1;
ccc6cda3 101 result = xrealloc (result, result_size);
d166f048 102 result[0] = '{'; result[1] = '\0';
726f6388
JA
103 flag++;
104 }
105
106 if (start == end)
107 subterm = savestring (array[start] + gcd_zero);
108 else
109 {
110 /* If there is more than one element in the subarray,
111 insert the prefix and an opening brace. */
112 result_size += gcd - gcd_zero + 1;
ccc6cda3 113 result = xrealloc (result, result_size);
726f6388
JA
114 strncat (result, array[start] + gcd_zero, gcd - gcd_zero);
115 strcat (result, "{");
116 subterm = really_munge_braces (array, start, end + 1, gcd);
117 subterm[strlen (subterm) - 1] = '}';
118 }
119
120 result_size += strlen (subterm) + 1;
ccc6cda3 121 result = xrealloc (result, result_size);
726f6388
JA
122 strcat (result, subterm);
123 strcat (result, ",");
124 free (subterm);
125 }
126
127 if (gcd_zero == 0)
128 result[strlen (result) - 1] = flag ? '}' : '\0';
129 return (result);
130}
131
132static void
133hack_braces_completion (names)
134 char **names;
135{
136 register int i;
137 char *temp;
138
139 temp = really_munge_braces (names, 1, array_len (names), 0);
140
141 for (i = 0; names[i]; ++i)
142 {
143 free (names[i]);
144 names[i] = NULL;
145 }
146 names[0] = temp;
147}
148
149void
150bash_brace_completion ()
151{
152 Function *orig_ignore_func;
153 Function *orig_entry_func;
154 CPPFunction *orig_attempt_func;
155
156 orig_ignore_func = rl_ignore_some_completions_function;
157 orig_attempt_func = rl_attempted_completion_function;
158 orig_entry_func = rl_completion_entry_function;
159
160 rl_completion_entry_function = (Function *) filename_completion_function;
161 rl_attempted_completion_function = NULL;
162 rl_ignore_some_completions_function = (Function *) hack_braces_completion;
163
164 rl_complete_internal (TAB);
165
166 rl_ignore_some_completions_function = orig_ignore_func;
167 rl_attempted_completion_function = orig_attempt_func;
168 rl_completion_entry_function = orig_entry_func;
169}
ccc6cda3 170#endif /* BRACE_EXPANSION && READLINE */