1 /* pcomplib.c - library functions for programmable completion. */
3 /* Copyright (C) 1999-2002 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>
38 #include "pcomplete.h"
40 #define COMPLETE_HASH_BUCKETS 32 /* must be power of two */
42 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
44 HASH_TABLE
*prog_completes
= (HASH_TABLE
*)NULL
;
46 static void free_progcomp
__P((PTR_T
));
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
);
115 if (prog_completes
== 0)
116 prog_completes
= hash_create (COMPLETE_HASH_BUCKETS
);
122 return (HASH_ENTRIES (prog_completes
));
131 cs
= (COMPSPEC
*)data
;
132 compspec_dispose (cs
);
139 hash_flush (prog_completes
, free_progcomp
);
146 hash_dispose (prog_completes
);
147 prog_completes
= (HASH_TABLE
*)NULL
;
151 progcomp_remove (cmd
)
154 register BUCKET_CONTENTS
*item
;
156 if (prog_completes
== 0)
159 item
= hash_remove (cmd
, prog_completes
, 0);
163 free_progcomp (item
->data
);
172 progcomp_insert (cmd
, cs
)
176 register BUCKET_CONTENTS
*item
;
179 programming_error (_("progcomp_insert: %s: NULL COMPSPEC"), cmd
);
181 if (prog_completes
== 0)
185 item
= hash_insert (cmd
, prog_completes
, 0);
187 free_progcomp (item
->data
);
189 item
->key
= savestring (cmd
);
196 progcomp_search (cmd
)
199 register BUCKET_CONTENTS
*item
;
202 if (prog_completes
== 0)
203 return ((COMPSPEC
*)NULL
);
205 item
= hash_search (cmd
, prog_completes
, 0);
208 return ((COMPSPEC
*)NULL
);
210 cs
= (COMPSPEC
*)item
->data
;
216 progcomp_walk (pfunc
)
219 if (prog_completes
== 0 || pfunc
== 0 || HASH_ENTRIES (prog_completes
) == 0)
222 hash_walk (prog_completes
, pfunc
);
225 #endif /* PROGRAMMABLE_COMPLETION */