]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - alias.c
Bash-4.2 patch 45
[thirdparty/bash.git] / alias.c
diff --git a/alias.c b/alias.c
index 707e38b77b278dede0ca71afd1bf11f1bbcd974e..6c953760f1036e2fb4820b4bcfb53bce37407a34 100644 (file)
--- a/alias.c
+++ b/alias.c
@@ -1,40 +1,61 @@
 /* alias.c -- Not a full alias, but just the kind that we use in the
    shell.  Csh style alias is somewhere else (`over there, in a box'). */
 
-/* Copyright (C) 1987,1991 Free Software Foundation, Inc.
+/* Copyright (C) 1987-2009 Free Software Foundation, Inc.
 
    This file is part of GNU Bash, the Bourne Again SHell.
 
-   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 1, or (at your option)
-   any later version.
+   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.
 
-   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.
+   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.
 
    You should have received a copy of the GNU General Public License
-   along with Bash; see the file COPYING.  If not, write to the Free
-   Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include "config.h"
 
 #if defined (ALIAS)
 
 #if defined (HAVE_UNISTD_H)
+#  ifdef _MINIX
+#    include <sys/types.h>
+#  endif
 #  include <unistd.h>
 #endif
 
 #include <stdio.h>
+#include "chartypes.h"
 #include "bashansi.h"
 #include "command.h"
 #include "general.h"
 #include "externs.h"
 #include "alias.h"
 
-static int qsort_alias_compare ();
+#if defined (PROGRAMMABLE_COMPLETION)
+#  include "pcomplete.h"
+#endif
+
+#define ALIAS_HASH_BUCKETS     16      /* must be power of two */
+
+typedef int sh_alias_map_func_t __P((alias_t *));
+
+static void free_alias_data __P((PTR_T));
+static alias_t **map_over_aliases __P((sh_alias_map_func_t *));
+static void sort_aliases __P((alias_t **));
+static int qsort_alias_compare __P((alias_t **, alias_t **));
+
+#if defined (READLINE)
+static int skipquotes __P((char *, int));
+static int skipws __P((char *, int));
+static int rd_token __P((char *, int));
+#endif
 
 /* Non-zero means expand all words on the line.  Otherwise, expand
    after first expansion if the expansion ends in a space. */
@@ -46,8 +67,8 @@ HASH_TABLE *aliases = (HASH_TABLE *)NULL;
 void
 initialize_aliases ()
 {
-  if (!aliases)
-    aliases = make_hash_table (0);
+  if (aliases == 0)
+    aliases = hash_create (ALIAS_HASH_BUCKETS);
 }
 
 /* Scan the list of aliases looking for one with NAME.  Return NULL
@@ -61,7 +82,7 @@ find_alias (name)
   if (aliases == 0)
     return ((alias_t *)NULL);
 
-  al = find_hash_item (name, aliases);
+  al = hash_search (name, aliases, 0);
   return (al ? (alias_t *)al->data : (alias_t *)NULL);
 }
 
@@ -101,6 +122,7 @@ add_alias (name, value)
     {
       free (temp->value);
       temp->value = savestring (value);
+      temp->flags &= ~AL_EXPANDNEXT;
       n = value[strlen (value) - 1];
       if (n == ' ' || n == '\t')
        temp->flags |= AL_EXPANDNEXT;
@@ -116,15 +138,18 @@ add_alias (name, value)
       if (n == ' ' || n == '\t')
        temp->flags |= AL_EXPANDNEXT;
 
-      elt = add_hash_item (savestring (name), aliases);
-      elt->data = (char *)temp;
+      elt = hash_insert (savestring (name), aliases, HASH_NOSRCH);
+      elt->data = temp;
+#if defined (PROGRAMMABLE_COMPLETION)
+      set_itemlist_dirty (&it_aliases);
+#endif
     }
 }
 
 /* Delete a single alias structure. */
 static void
 free_alias_data (data)
-     char *data;
+     PTR_T data;
 {
   register alias_t *a;
 
@@ -146,11 +171,15 @@ remove_alias (name)
   if (aliases == 0)
     return (-1);
 
-  elt = remove_hash_item (name, aliases);
+  elt = hash_remove (name, aliases, 0);
   if (elt)
     {
       free_alias_data (elt->data);
       free (elt->key);         /* alias name */
+      free (elt);              /* XXX */
+#if defined (PROGRAMMABLE_COMPLETION)
+      set_itemlist_dirty (&it_aliases);
+#endif
       return (aliases->nentries);
     }
   return (-1);
@@ -163,41 +192,41 @@ delete_all_aliases ()
   if (aliases == 0)
     return;
 
-  flush_hash_table (aliases, free_alias_data);
-  free (aliases);
+  hash_flush (aliases, free_alias_data);
+  hash_dispose (aliases);
   aliases = (HASH_TABLE *)NULL;
+#if defined (PROGRAMMABLE_COMPLETION)
+  set_itemlist_dirty (&it_aliases);
+#endif
 }
 
 /* Return an array of aliases that satisfy the conditions tested by FUNCTION.
    If FUNCTION is NULL, return all aliases. */
 static alias_t **
 map_over_aliases (function)
-     Function *function;
+     sh_alias_map_func_t *function;
 {
   register int i;
   register BUCKET_CONTENTS *tlist;
   alias_t *alias, **list;
-  int list_index, list_size;
+  int list_index;
 
-  list = (alias_t **)NULL;
-  for (i = list_index = list_size = 0; i < aliases->nbuckets; i++)
-    {
-      tlist = get_hash_bucket (i, aliases);
+  i = HASH_ENTRIES (aliases);
+  if (i == 0)
+    return ((alias_t **)NULL);
 
-      while (tlist)
+  list = (alias_t **)xmalloc ((i + 1) * sizeof (alias_t *));
+  for (i = list_index = 0; i < aliases->nbuckets; i++)
+    {
+      for (tlist = hash_items (i, aliases); tlist; tlist = tlist->next)
        {
          alias = (alias_t *)tlist->data;
 
          if (!function || (*function) (alias))
            {
-             if (list_index + 1 >= list_size)
-               list = (alias_t **)
-                 xrealloc ((char *)list, (list_size += 20) * sizeof (alias_t *));
-
              list[list_index++] = alias;
              list[list_index] = (alias_t *)NULL;
            }
-         tlist = tlist->next;
        }
     }
   return (list);
@@ -207,7 +236,7 @@ static void
 sort_aliases (array)
      alias_t **array;
 {
-  qsort (array, array_len ((char **)array), sizeof (alias_t *), qsort_alias_compare);
+  qsort (array, strvec_len ((char **)array), sizeof (alias_t *), (QSFUNC *)qsort_alias_compare);
 }
 
 static int
@@ -228,10 +257,10 @@ all_aliases ()
 {
   alias_t **list;
 
-  if (!aliases)
+  if (aliases == 0 || HASH_ENTRIES (aliases) == 0)
     return ((alias_t **)NULL);
 
-  list = map_over_aliases ((Function *)NULL);
+  list = map_over_aliases ((sh_alias_map_func_t *)NULL);
   if (list)
     sort_aliases (list);
   return (list);
@@ -247,6 +276,10 @@ alias_expand_word (s)
   return (r ? savestring (r->value) : (char *)NULL);
 }
 
+/* Readline support functions -- expand all aliases in a line. */
+
+#if defined (READLINE)
+
 /* Return non-zero if CHARACTER is a member of the class of characters
    that are self-delimiting in the shell (this really means that these
    characters delimit tokens). */
@@ -286,6 +319,8 @@ skipquotes (string, start)
       if (string[i] == '\\')
        {
          i++;          /* skip backslash-quoted quote characters, too */
+         if (string[i] == 0)
+           break;
          continue;
        }
 
@@ -303,12 +338,13 @@ skipws (string, start)
      char *string;
      int start;
 {
-  register int i = 0;
-  int pass_next, backslash_quoted_word, peekc;
+  register int i;
+  int pass_next, backslash_quoted_word;
+  unsigned char peekc;
 
   /* skip quoted strings, in ' or ", and words in which a character is quoted
      with a `\'. */
-  backslash_quoted_word = pass_next = 0;
+  i = backslash_quoted_word = pass_next = 0;
 
   /* Skip leading whitespace (or separator characters), and quoted words.
      But save it in the output.  */
@@ -330,7 +366,9 @@ skipws (string, start)
       if (string[i] == '\\')
        {
          peekc = string[i+1];
-         if (isletter (peekc))
+         if (peekc == 0)
+           break;
+         if (ISLETTER (peekc))
            backslash_quoted_word++;    /* this is a backslash-quoted word */
          else
            pass_next++;
@@ -351,7 +389,7 @@ skipws (string, start)
            break;
 
          peekc = string[i + 1];
-         if (isletter (peekc))
+         if (ISLETTER (peekc))
            backslash_quoted_word++;
          continue;
        }
@@ -395,6 +433,8 @@ rd_token (string, start)
       if (string[i] == '\\')
        {
          i++;  /* skip backslash-escaped character */
+         if (string[i] == 0)
+           break;
          continue;
        }
 
@@ -406,6 +446,12 @@ rd_token (string, start)
       if (quote_char (string[i]))
        {
          i = skipquotes (string, i);
+         /* This could be a line that contains a single quote character,
+            in which case skipquotes () terminates with string[i] == '\0'
+            (the end of the string).  Check for that here. */
+         if (string[i] == '\0')
+           break;
+
          /* Now string[i] is the matching quote character, and the
             quoted portion of the token has been scanned. */
          continue;
@@ -419,21 +465,22 @@ char *
 alias_expand (string)
      char *string;
 {
-  int line_len = 1 + strlen (string);
-  char *line = (char *)xmalloc (line_len);
   register int i, j, start;
-  char *token = xmalloc (line_len);
-  int tl, real_start, expand_next, expand_this_token;
+  char *line, *token;
+  int line_len, tl, real_start, expand_next, expand_this_token;
   alias_t *alias;
 
+  line_len = strlen (string) + 1;
+  line = (char *)xmalloc (line_len);
+  token = (char *)xmalloc (line_len);
+
   line[0] = i = 0;
   expand_next = 0;
   command_word = 1; /* initialized to expand the first word on the line */
 
   /* Each time through the loop we find the next word in line.  If it
-     has an alias, substitute
-     the alias value.  If the value ends in ` ', then try again
-     with the next word.  Else, if there is no value, or if
+     has an alias, substitute the alias value.  If the value ends in ` ',
+     then try again with the next word.  Else, if there is no value, or if
      the value does not end in space, we are done. */
 
   for (;;)
@@ -485,7 +532,7 @@ alias_expand (string)
       /* If there is a backslash-escaped character quoted in TOKEN,
         then we don't do alias expansion.  This should check for all
         other quoting characters, too. */
-      if (strchr (token, '\\'))
+      if (mbschr (token, '\\'))
        expand_this_token = 0;
 
       /* If we should be expanding here, if we are expanding all words, or if
@@ -529,4 +576,5 @@ alias_expand (string)
       command_word = 0;
     }
 }
+#endif /* READLINE */
 #endif /* ALIAS */