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;
56 ret->globpat = (char *)NULL;
57 ret->words = (char *)NULL;
58 ret->prefix = (char *)NULL;
59 ret->suffix = (char *)NULL;
60 ret->funcname = (char *)NULL;
61 ret->command = (char *)NULL;
62 ret->filterpat = (char *)NULL;
72 if (cs->refcount == 0)
92 new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
94 new->refcount = cs->refcount;
95 new->actions = cs->actions;
97 new->globpat = STRDUP (cs->globpat);
98 new->words = STRDUP (cs->words);
99 new->prefix = STRDUP (cs->prefix);
100 new->suffix = STRDUP (cs->suffix);
101 new->funcname = STRDUP (cs->funcname);
102 new->command = STRDUP (cs->command);
103 new->filterpat = STRDUP (cs->filterpat);
109 initialize_progcomp ()
111 if (progcomp_initialized == 0)
113 prog_completes = make_hash_table (COMPLETE_HASH_BUCKETS);
114 progcomp_initialized = 1;
121 if (progcomp_initialized == 0 || prog_completes == 0)
123 return (prog_completes->nentries);
132 cs = (COMPSPEC *)data;
140 flush_hash_table (prog_completes, free_progcomp);
144 remove_progcomp (cmd)
147 register BUCKET_CONTENTS *item;
149 if (prog_completes == 0)
152 item = remove_hash_item (cmd, prog_completes);
155 free_progcomp (item->data);
164 add_progcomp (cmd, cs)
168 register BUCKET_CONTENTS *item;
170 if (progcomp_initialized == 0 || prog_completes == 0)
171 initialize_progcomp ();
174 programming_error ("add_progcomp: %s: NULL COMPSPEC", cmd);
176 item = add_hash_item (cmd, prog_completes);
178 free_progcomp (item->data);
180 item->key = savestring (cmd);
181 item->data = (char *)cs;
190 register BUCKET_CONTENTS *item;
193 if (prog_completes == 0)
194 return ((COMPSPEC *)NULL);
196 item = find_hash_item (cmd, prog_completes);
199 return ((COMPSPEC *)NULL);
201 cs = (COMPSPEC *)item->data;
207 print_all_compspecs (pfunc)
210 BUCKET_CONTENTS *item_list;
214 if (prog_completes == 0 || pfunc == 0)
217 for (bucket = 0; bucket < prog_completes->nbuckets; bucket++)
219 item_list = get_hash_bucket (bucket, prog_completes);
223 for ( ; item_list; item_list = item_list->next)
225 cs = (COMPSPEC *)item_list->data;
226 (*pfunc) (item_list->key, cs);
231 #endif /* PROGRAMMABLE_COMPLETION */