1 /* pcomplib.c - library functions for programmable completion. */
3 /* Copyright (C) 1999-2020 Free Software Foundation, Inc.
5 This file is part of GNU Bush, the Bourne Again SHell.
7 Bush is free software: you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation, either version 3 of the License, or
10 (at your option) any later version.
12 Bush is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with Bush. If not, see <http://www.gnu.org/licenses/>.
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 256 /* 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
PARAMS((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
->lcommand
= (char *)NULL
;
66 ret
->filterpat
= (char *)NULL
;
76 if (cs
->refcount
== 0)
97 new = (COMPSPEC
*)xmalloc (sizeof (COMPSPEC
));
99 new->refcount
= 1; /* was cs->refcount, but this is a fresh copy */
100 new->actions
= cs
->actions
;
101 new->options
= cs
->options
;
103 new->globpat
= STRDUP (cs
->globpat
);
104 new->words
= STRDUP (cs
->words
);
105 new->prefix
= STRDUP (cs
->prefix
);
106 new->suffix
= STRDUP (cs
->suffix
);
107 new->funcname
= STRDUP (cs
->funcname
);
108 new->command
= STRDUP (cs
->command
);
109 new->lcommand
= STRDUP (cs
->lcommand
);
110 new->filterpat
= STRDUP (cs
->filterpat
);
118 if (prog_completes
== 0)
119 prog_completes
= hash_create (COMPLETE_HASH_BUCKETS
);
125 return (HASH_ENTRIES (prog_completes
));
134 cs
= (COMPSPEC
*)data
;
135 compspec_dispose (cs
);
142 hash_flush (prog_completes
, free_progcomp
);
149 hash_dispose (prog_completes
);
150 prog_completes
= (HASH_TABLE
*)NULL
;
154 progcomp_remove (cmd
)
157 register BUCKET_CONTENTS
*item
;
159 if (prog_completes
== 0)
162 item
= hash_remove (cmd
, prog_completes
, 0);
166 free_progcomp (item
->data
);
175 progcomp_insert (cmd
, cs
)
179 register BUCKET_CONTENTS
*item
;
182 programming_error (_("progcomp_insert: %s: NULL COMPSPEC"), cmd
);
184 if (prog_completes
== 0)
188 item
= hash_insert (cmd
, prog_completes
, 0);
190 free_progcomp (item
->data
);
192 item
->key
= savestring (cmd
);
199 progcomp_search (cmd
)
202 register BUCKET_CONTENTS
*item
;
205 if (prog_completes
== 0)
206 return ((COMPSPEC
*)NULL
);
208 item
= hash_search (cmd
, prog_completes
, 0);
211 return ((COMPSPEC
*)NULL
);
213 cs
= (COMPSPEC
*)item
->data
;
219 progcomp_walk (pfunc
)
222 if (prog_completes
== 0 || pfunc
== 0 || HASH_ENTRIES (prog_completes
) == 0)
225 hash_walk (prog_completes
, pfunc
);
228 #endif /* PROGRAMMABLE_COMPLETION */