]> git.ipfire.org Git - thirdparty/bash.git/blobdiff - pcomplib.c
fix for SIGINT in sourced script
[thirdparty/bash.git] / pcomplib.c
index 133c6f40744e654ca35197a797bd565d70949d94..70c7a17a602194c792fe91a9cf23fb456ff78d98 100644 (file)
@@ -1,22 +1,22 @@
 /* pcomplib.c - library functions for programmable completion. */
 
-/* Copyright (C) 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1999-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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
+   You should have received a copy of the GNU General Public License
+   along with Bash.  If not, see <http://www.gnu.org/licenses/>.
+*/
 
 #include <config.h>
 
 #  include <unistd.h>
 #endif
 
+#include "bashintl.h"
+
 #include "shell.h"
 #include "pcomplete.h"
 
-#define COMPLETE_HASH_BUCKETS  29      /* for testing */
+#define COMPLETE_HASH_BUCKETS  256     /* must be power of two */
 
 #define STRDUP(x)      ((x) ? savestring (x) : (char *)NULL)
 
 HASH_TABLE *prog_completes = (HASH_TABLE *)NULL;
 
-static int progcomp_initialized = 0;
+static void free_progcomp __P((PTR_T));
 
 COMPSPEC *
-alloc_compspec ()
+compspec_create ()
 {
   COMPSPEC *ret;
 
@@ -52,6 +54,7 @@ alloc_compspec ()
   ret->refcount = 0;
 
   ret->actions = (unsigned long)0;
+  ret->options = (unsigned long)0;
 
   ret->globpat = (char *)NULL;
   ret->words = (char *)NULL;
@@ -59,13 +62,14 @@ alloc_compspec ()
   ret->suffix = (char *)NULL;
   ret->funcname = (char *)NULL;
   ret->command = (char *)NULL;
+  ret->lcommand = (char *)NULL;
   ret->filterpat = (char *)NULL;
 
   return ret;
 }
 
 void
-free_compspec (cs)
+compspec_dispose (cs)
      COMPSPEC *cs;
 {
   cs->refcount--;
@@ -77,6 +81,7 @@ free_compspec (cs)
       FREE (cs->suffix);
       FREE (cs->funcname);
       FREE (cs->command);
+      FREE (cs->lcommand);
       FREE (cs->filterpat);
 
       free (cs);
@@ -84,15 +89,16 @@ free_compspec (cs)
 }
 
 COMPSPEC *
-copy_compspec (cs)
+compspec_copy (cs)
      COMPSPEC *cs;
 {
   COMPSPEC *new;
 
   new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
 
-  new->refcount = cs->refcount;
+  new->refcount = 1;   /* was cs->refcount, but this is a fresh copy */
   new->actions = cs->actions;
+  new->options = cs->options;
 
   new->globpat = STRDUP (cs->globpat);
   new->words = STRDUP (cs->words);
@@ -100,48 +106,52 @@ copy_compspec (cs)
   new->suffix = STRDUP (cs->suffix);
   new->funcname = STRDUP (cs->funcname);
   new->command = STRDUP (cs->command);
+  new->lcommand = STRDUP (cs->lcommand);
   new->filterpat = STRDUP (cs->filterpat);
 
   return new;
 }
 
 void
-initialize_progcomp ()
+progcomp_create ()
 {
-  if (progcomp_initialized == 0)
-    {
-      prog_completes = make_hash_table (COMPLETE_HASH_BUCKETS);
-      progcomp_initialized = 1;
-    }
+  if (prog_completes == 0)
+    prog_completes = hash_create (COMPLETE_HASH_BUCKETS);
 }
 
 int
-num_progcomps ()
+progcomp_size ()
 {
-  if (progcomp_initialized == 0 || prog_completes == 0)
-    return (0);
-  return (prog_completes->nentries);
+  return (HASH_ENTRIES (prog_completes));
 }
 
 static void
 free_progcomp (data)
-     char *data;
+     PTR_T data;
 {
   COMPSPEC *cs;
 
   cs = (COMPSPEC *)data;
-  free_compspec (cs);
+  compspec_dispose (cs);
 }
   
 void
-clear_progcomps ()
+progcomp_flush ()
+{
+  if (prog_completes)
+    hash_flush (prog_completes, free_progcomp);
+}
+
+void
+progcomp_dispose ()
 {
   if (prog_completes)
-    flush_hash_table (prog_completes, free_progcomp);
+    hash_dispose (prog_completes);
+  prog_completes = (HASH_TABLE *)NULL;
 }
 
 int
-remove_progcomp (cmd)
+progcomp_remove (cmd)
      char *cmd;
 {
   register BUCKET_CONTENTS *item;
@@ -149,10 +159,11 @@ remove_progcomp (cmd)
   if (prog_completes == 0)
     return 1;
 
-  item = remove_hash_item (cmd, prog_completes);
+  item = hash_remove (cmd, prog_completes, 0);
   if (item)
     {
-      free_progcomp (item->data);
+      if (item->data)
+       free_progcomp (item->data);
       free (item->key);
       free (item);
       return (1);
@@ -161,31 +172,32 @@ remove_progcomp (cmd)
 }
 
 int
-add_progcomp (cmd, cs)
+progcomp_insert (cmd, cs)
       char *cmd;
       COMPSPEC *cs;
 {
   register BUCKET_CONTENTS *item;
 
-  if (progcomp_initialized == 0 || prog_completes == 0)
-    initialize_progcomp ();
-
   if (cs == NULL)
-    programming_error ("add_progcomp: %s: NULL COMPSPEC", cmd);
+    programming_error (_("progcomp_insert: %s: NULL COMPSPEC"), cmd);
 
-  item = add_hash_item (cmd, prog_completes);
+  if (prog_completes == 0)
+    progcomp_create ();
+
+  cs->refcount++;
+  item = hash_insert (cmd, prog_completes, 0);
   if (item->data)
     free_progcomp (item->data);
   else
     item->key = savestring (cmd);
-  item->data = (char *)cs;
-  cs->refcount++;
+  item->data = cs;
+
   return 1;
 }
 
 COMPSPEC *
-find_compspec (cmd)
-     char *cmd;
+progcomp_search (cmd)
+     const char *cmd;
 {
   register BUCKET_CONTENTS *item;
   COMPSPEC *cs;
@@ -193,7 +205,7 @@ find_compspec (cmd)
   if (prog_completes == 0)
     return ((COMPSPEC *)NULL);
 
-  item = find_hash_item (cmd, prog_completes);
+  item = hash_search (cmd, prog_completes, 0);
 
   if (item == NULL)
     return ((COMPSPEC *)NULL);
@@ -204,28 +216,13 @@ find_compspec (cmd)
 }
 
 void
-print_all_compspecs (pfunc)
-     VFunction *pfunc;
+progcomp_walk (pfunc)
+     hash_wfunc *pfunc;
 {
-  BUCKET_CONTENTS *item_list;
-  int bucket;
-  COMPSPEC *cs;
-
-  if (prog_completes == 0 || pfunc == 0)
+  if (prog_completes == 0 || pfunc == 0 || HASH_ENTRIES (prog_completes) == 0)
     return;
 
-  for (bucket = 0; bucket < prog_completes->nbuckets; bucket++)
-    {
-      item_list = get_hash_bucket (bucket, prog_completes);
-      if (item_list == 0)
-       continue;
-
-      for ( ; item_list; item_list = item_list->next)
-       {
-         cs = (COMPSPEC *)item_list->data;
-         (*pfunc) (item_list->key, cs);
-       }
-    }
+  hash_walk (prog_completes, pfunc);
 }
 
 #endif /* PROGRAMMABLE_COMPLETION */