/* 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 128 /* 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;
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--;
FREE (cs->suffix);
FREE (cs->funcname);
FREE (cs->command);
+ FREE (cs->lcommand);
FREE (cs->filterpat);
free (cs);
}
COMPSPEC *
-copy_compspec (cs)
+compspec_copy (cs)
COMPSPEC *cs;
{
COMPSPEC *new;
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;
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);
}
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)
+progcomp_search (cmd)
const char *cmd;
{
register BUCKET_CONTENTS *item;
if (prog_completes == 0)
return ((COMPSPEC *)NULL);
- item = find_hash_item ((char *)cmd, prog_completes); /* XXX fix const later */
+ item = hash_search (cmd, prog_completes, 0);
if (item == NULL)
return ((COMPSPEC *)NULL);
}
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 */