| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* bracecomp.c -- Complete a filename with the possible completions enclosed
 | 
					
						
							|  |  |  |    in csh-style braces such that the list of completions is available to the | 
					
						
							|  |  |  |    shell. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Original version by tromey@cns.caltech.edu,  Fri Feb  7 1992. */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | /* Copyright (C) 1993-2009 Free Software Foundation, Inc.
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |    This file is part of GNU Bash, the Bourne Again SHell. | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |    it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |    the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |    (at your option) any later version. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    Bash is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |    GNU General Public License for more details. | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |    You should have received a copy of the GNU General Public License | 
					
						
							|  |  |  |    along with Bash.  If not, see <http://www.gnu.org/licenses/>.
 | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #include "config.h"
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #if defined (BRACE_EXPANSION) && defined (READLINE)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | #include <stdio.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #if defined (HAVE_UNISTD_H)
 | 
					
						
							| 
									
										
										
										
											1998-04-17 19:52:44 +00:00
										 |  |  | #  ifdef _MINIX
 | 
					
						
							|  |  |  | #    include <sys/types.h>
 | 
					
						
							|  |  |  | #  endif
 | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #  include <unistd.h>
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | #include "bashansi.h"
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | #include "shmbutil.h"
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include "shell.h"
 | 
					
						
							|  |  |  | #include <readline/readline.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | static int _strcompare __P((char **, char **)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | /* Find greatest common prefix of two strings. */ | 
					
						
							|  |  |  | static int | 
					
						
							|  |  |  | string_gcd (s1, s2) | 
					
						
							|  |  |  |      char *s1, *s2; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (s1 == NULL || s2 == NULL) | 
					
						
							|  |  |  |     return (0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; *s1 && *s2; ++s1, ++s2, ++i) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       if (*s1 != *s2) | 
					
						
							|  |  |  | 	break; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return (i); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static char * | 
					
						
							|  |  |  | really_munge_braces (array, real_start, real_end, gcd_zero) | 
					
						
							|  |  |  |      char **array; | 
					
						
							|  |  |  |      int real_start, real_end, gcd_zero; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int start, end, gcd; | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |   char *result, *subterm, *x; | 
					
						
							|  |  |  |   int result_size, flag, tlen; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   flag = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (real_start == real_end) | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  |       x = array[real_start] ? sh_backslash_quote (array[real_start] + gcd_zero, 0, 0) | 
					
						
							|  |  |  |  			    : sh_backslash_quote (array[0], 0, 0); | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |       return x; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |   result = (char *)xmalloc (result_size = 16); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |   *result = '\0'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   for (start = real_start; start < real_end; start = end + 1) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       gcd = strlen (array[start]); | 
					
						
							|  |  |  |       for (end = start + 1; end < real_end; end++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  int temp; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	  temp = string_gcd (array[start], array[end]); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	  if (temp <= gcd_zero) | 
					
						
							|  |  |  | 	    break; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	  gcd = temp; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  |       end--; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       if (gcd_zero == 0 && start == real_start && end != (real_end - 1)) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  /* In this case, add in a leading '{', because we are at
 | 
					
						
							|  |  |  | 	     top level, and there isn't a consistent prefix. */ | 
					
						
							|  |  |  | 	  result_size += 1; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	  result = (char *)xrealloc (result, result_size); | 
					
						
							| 
									
										
										
										
											1997-06-05 14:59:13 +00:00
										 |  |  | 	  result[0] = '{'; result[1] = '\0'; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	  flag++; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |       /* Make sure we backslash quote every substring we insert into the
 | 
					
						
							|  |  |  | 	 resultant brace expression.  This is so the default filename | 
					
						
							|  |  |  | 	 quoting function won't inappropriately quote the braces. */ | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       if (start == end) | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 	  x = savestring (array[start] + gcd_zero); | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | 	  subterm = sh_backslash_quote (x, 0, 0); | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | 	  free (x); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       else | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 	  /* If there is more than one element in the subarray,
 | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | 	     insert the (quoted) prefix and an opening brace. */ | 
					
						
							|  |  |  | 	  tlen = gcd - gcd_zero; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	  x = (char *)xmalloc (tlen + 1); | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | 	  strncpy (x, array[start] + gcd_zero, tlen); | 
					
						
							|  |  |  | 	  x[tlen] = '\0'; | 
					
						
							| 
									
										
										
										
											2014-02-26 09:36:43 -05:00
										 |  |  | 	  subterm = sh_backslash_quote (x, 0, 0); | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | 	  free (x); | 
					
						
							|  |  |  | 	  result_size += strlen (subterm) + 1; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  | 	  result = (char *)xrealloc (result, result_size); | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | 	  strcat (result, subterm); | 
					
						
							|  |  |  | 	  free (subterm); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 	  strcat (result, "{"); | 
					
						
							|  |  |  | 	  subterm = really_munge_braces (array, start, end + 1, gcd); | 
					
						
							|  |  |  | 	  subterm[strlen (subterm) - 1] = '}'; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       result_size += strlen (subterm) + 1; | 
					
						
							| 
									
										
										
										
											2001-11-13 17:56:06 +00:00
										 |  |  |       result = (char *)xrealloc (result, result_size); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  |       strcat (result, subterm); | 
					
						
							|  |  |  |       strcat (result, ","); | 
					
						
							|  |  |  |       free (subterm); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   if (gcd_zero == 0) | 
					
						
							|  |  |  |     result[strlen (result) - 1] = flag ? '}' : '\0'; | 
					
						
							|  |  |  |   return (result); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  | static int | 
					
						
							|  |  |  | _strcompare (s1, s2) | 
					
						
							|  |  |  |      char **s1, **s2; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   int result; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   result = **s1 - **s2; | 
					
						
							|  |  |  |   if (result == 0) | 
					
						
							|  |  |  |     result = strcmp (*s1, *s2); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | static int | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | hack_braces_completion (names) | 
					
						
							|  |  |  |      char **names; | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |   register int i; | 
					
						
							|  |  |  |   char *temp; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-12 13:36:28 +00:00
										 |  |  |   i = strvec_len (names); | 
					
						
							|  |  |  |   if (MB_CUR_MAX > 1 && i > 2) | 
					
						
							|  |  |  |     qsort (names+1, i-1, sizeof (char *), (QSFUNC *)_strcompare); | 
					
						
							|  |  |  |        | 
					
						
							|  |  |  |   temp = really_munge_braces (names, 1, i, 0); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   for (i = 0; names[i]; ++i) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |       free (names[i]); | 
					
						
							|  |  |  |       names[i] = NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   names[0] = temp; | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   return 0; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  | /* We handle quoting ourselves within hack_braces_completion, so we turn off
 | 
					
						
							|  |  |  |    rl_filename_quoting_desired and rl_filename_quoting_function. */ | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | int | 
					
						
							|  |  |  | bash_brace_completion (count, ignore) | 
					
						
							|  |  |  |      int count, ignore; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   rl_compignore_func_t *orig_ignore_func; | 
					
						
							|  |  |  |   rl_compentry_func_t *orig_entry_func; | 
					
						
							|  |  |  |   rl_quote_func_t *orig_quoting_func; | 
					
						
							|  |  |  |   rl_completion_func_t *orig_attempt_func; | 
					
						
							|  |  |  |   int orig_quoting_desired, r; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   orig_ignore_func = rl_ignore_some_completions_function; | 
					
						
							|  |  |  |   orig_attempt_func = rl_attempted_completion_function; | 
					
						
							|  |  |  |   orig_entry_func = rl_completion_entry_function; | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |   orig_quoting_func = rl_filename_quoting_function; | 
					
						
							|  |  |  |   orig_quoting_desired = rl_filename_quoting_desired; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   rl_completion_entry_function = rl_filename_completion_function; | 
					
						
							|  |  |  |   rl_attempted_completion_function = (rl_completion_func_t *)NULL; | 
					
						
							|  |  |  |   rl_ignore_some_completions_function = hack_braces_completion; | 
					
						
							|  |  |  |   rl_filename_quoting_function = (rl_quote_func_t *)NULL; | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |   rl_filename_quoting_desired = 0; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  |   r = rl_complete_internal (TAB); | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   rl_ignore_some_completions_function = orig_ignore_func; | 
					
						
							|  |  |  |   rl_attempted_completion_function = orig_attempt_func; | 
					
						
							|  |  |  |   rl_completion_entry_function = orig_entry_func; | 
					
						
							| 
									
										
										
										
											1997-09-22 20:22:27 +00:00
										 |  |  |   rl_filename_quoting_function = orig_quoting_func; | 
					
						
							|  |  |  |   rl_filename_quoting_desired = orig_quoting_desired; | 
					
						
							| 
									
										
										
										
											2001-04-06 19:14:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |   return r; | 
					
						
							| 
									
										
										
										
											1996-08-26 18:22:31 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-23 17:02:34 +00:00
										 |  |  | #endif /* BRACE_EXPANSION && READLINE */
 |