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 void free_progcomp __P((PTR_T));
46 static int progcomp_initialized = 0;
53 ret = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
56 ret->actions = (unsigned long)0;
57 ret->options = (unsigned long)0;
59 ret->globpat = (char *)NULL;
60 ret->words = (char *)NULL;
61 ret->prefix = (char *)NULL;
62 ret->suffix = (char *)NULL;
63 ret->funcname = (char *)NULL;
64 ret->command = (char *)NULL;
65 ret->filterpat = (char *)NULL;
75 if (cs->refcount == 0)
95 new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
97 new->refcount = cs->refcount;
98 new->actions = cs->actions;
99 new->options = cs->options;
101 new->globpat = STRDUP (cs->globpat);
102 new->words = STRDUP (cs->words);
103 new->prefix = STRDUP (cs->prefix);
104 new->suffix = STRDUP (cs->suffix);
105 new->funcname = STRDUP (cs->funcname);
106 new->command = STRDUP (cs->command);
107 new->filterpat = STRDUP (cs->filterpat);
113 initialize_progcomp ()
115 if (progcomp_initialized == 0)
117 prog_completes = make_hash_table (COMPLETE_HASH_BUCKETS);
118 progcomp_initialized = 1;
125 if (progcomp_initialized == 0 || prog_completes == 0)
127 return (prog_completes->nentries);
136 cs = (COMPSPEC *)data;
144 flush_hash_table (prog_completes, free_progcomp);
148 remove_progcomp (cmd)
151 register BUCKET_CONTENTS *item;
153 if (prog_completes == 0)
156 item = remove_hash_item (cmd, prog_completes);
159 free_progcomp (item->data);
168 add_progcomp (cmd, cs)
172 register BUCKET_CONTENTS *item;
174 if (progcomp_initialized == 0 || prog_completes == 0)
175 initialize_progcomp ();
178 programming_error ("add_progcomp: %s: NULL COMPSPEC", cmd);
180 item = add_hash_item (cmd, prog_completes);
182 free_progcomp (item->data);
184 item->key = savestring (cmd);
185 item->data = (char *)cs;
194 register BUCKET_CONTENTS *item;
197 if (prog_completes == 0)
198 return ((COMPSPEC *)NULL);
200 item = find_hash_item (cmd, prog_completes);
203 return ((COMPSPEC *)NULL);
205 cs = (COMPSPEC *)item->data;
211 print_all_compspecs (pfunc)
212 sh_csprint_func_t *pfunc;
214 BUCKET_CONTENTS *item_list;
218 if (prog_completes == 0 || pfunc == 0)
221 for (bucket = 0; bucket < prog_completes->nbuckets; bucket++)
223 item_list = get_hash_bucket (bucket, prog_completes);
227 for ( ; item_list; item_list = item_list->next)
229 cs = (COMPSPEC *)item_list->data;
230 (*pfunc) (item_list->key, cs);
235 #endif /* PROGRAMMABLE_COMPLETION */