]>
git.ipfire.org Git - thirdparty/bash.git/blob - pcomplib.c
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>
36 #include "pcomplete.h"
38 #define COMPLETE_HASH_BUCKETS 32 /* must be power of two */
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
));
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
);
113 if (prog_completes
== 0)
114 prog_completes
= hash_create (COMPLETE_HASH_BUCKETS
);
120 return (HASH_ENTRIES (prog_completes
));
129 cs
= (COMPSPEC
*)data
;
130 compspec_dispose (cs
);
137 hash_flush (prog_completes
, free_progcomp
);
144 hash_dispose (prog_completes
);
145 prog_completes
= (HASH_TABLE
*)NULL
;
149 progcomp_remove (cmd
)
152 register BUCKET_CONTENTS
*item
;
154 if (prog_completes
== 0)
157 item
= hash_remove (cmd
, prog_completes
, 0);
161 free_progcomp (item
->data
);
170 progcomp_insert (cmd
, cs
)
174 register BUCKET_CONTENTS
*item
;
177 programming_error ("progcomp_insert: %s: NULL COMPSPEC", cmd
);
179 if (prog_completes
== 0)
182 item
= hash_insert (cmd
, prog_completes
, 0);
184 free_progcomp (item
->data
);
186 item
->key
= savestring (cmd
);
193 progcomp_search (cmd
)
196 register BUCKET_CONTENTS
*item
;
199 if (prog_completes
== 0)
200 return ((COMPSPEC
*)NULL
);
202 item
= hash_search (cmd
, prog_completes
, 0);
205 return ((COMPSPEC
*)NULL
);
207 cs
= (COMPSPEC
*)item
->data
;
213 progcomp_walk (pfunc
)
216 if (prog_completes
== 0 || pfunc
== 0 || HASH_ENTRIES (prog_completes
) == 0)
219 hash_walk (prog_completes
, pfunc
);
222 #endif /* PROGRAMMABLE_COMPLETION */