1 /* pcomplib.c - library functions for programmable completion. */
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if defined (PROGRAMMABLE_COMPLETION)
28 #if defined (HAVE_UNISTD_H)
30 # include <sys/types.h>
36 #include "pcomplete.h"
38 #define COMPLETE_HASH_BUCKETS 29 /* for testing */
40 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
42 HASH_TABLE *prog_completes = (HASH_TABLE *)NULL;
44 static int progcomp_initialized = 0;
51 ret = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
54 ret->actions = (unsigned long)0;
55 ret->options = (unsigned long)0;
57 ret->globpat = (char *)NULL;
58 ret->words = (char *)NULL;
59 ret->prefix = (char *)NULL;
60 ret->suffix = (char *)NULL;
61 ret->funcname = (char *)NULL;
62 ret->command = (char *)NULL;
63 ret->filterpat = (char *)NULL;
73 if (cs->refcount == 0)
93 new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
95 new->refcount = cs->refcount;
96 new->actions = cs->actions;
97 new->options = cs->options;
99 new->globpat = STRDUP (cs->globpat);
100 new->words = STRDUP (cs->words);
101 new->prefix = STRDUP (cs->prefix);
102 new->suffix = STRDUP (cs->suffix);
103 new->funcname = STRDUP (cs->funcname);
104 new->command = STRDUP (cs->command);
105 new->filterpat = STRDUP (cs->filterpat);
111 initialize_progcomp ()
113 if (progcomp_initialized == 0)
115 prog_completes = make_hash_table (COMPLETE_HASH_BUCKETS);
116 progcomp_initialized = 1;
123 if (progcomp_initialized == 0 || prog_completes == 0)
125 return (prog_completes->nentries);
134 cs = (COMPSPEC *)data;
142 flush_hash_table (prog_completes, free_progcomp);
146 remove_progcomp (cmd)
149 register BUCKET_CONTENTS *item;
151 if (prog_completes == 0)
154 item = remove_hash_item (cmd, prog_completes);
157 free_progcomp (item->data);
166 add_progcomp (cmd, cs)
170 register BUCKET_CONTENTS *item;
172 if (progcomp_initialized == 0 || prog_completes == 0)
173 initialize_progcomp ();
176 programming_error ("add_progcomp: %s: NULL COMPSPEC", cmd);
178 item = add_hash_item (cmd, prog_completes);
180 free_progcomp (item->data);
182 item->key = savestring (cmd);
183 item->data = (char *)cs;
192 register BUCKET_CONTENTS *item;
195 if (prog_completes == 0)
196 return ((COMPSPEC *)NULL);
198 item = find_hash_item ((char *)cmd, prog_completes); /* XXX fix const later */
201 return ((COMPSPEC *)NULL);
203 cs = (COMPSPEC *)item->data;
209 print_all_compspecs (pfunc)
212 BUCKET_CONTENTS *item_list;
216 if (prog_completes == 0 || pfunc == 0)
219 for (bucket = 0; bucket < prog_completes->nbuckets; bucket++)
221 item_list = get_hash_bucket (bucket, prog_completes);
225 for ( ; item_list; item_list = item_list->next)
227 cs = (COMPSPEC *)item_list->data;
228 (*pfunc) (item_list->key, cs);
233 #endif /* PROGRAMMABLE_COMPLETION */