]> git.ipfire.org Git - thirdparty/bash.git/blob - pcomplib.c
Imported from ../bash-2.05.tar.gz.
[thirdparty/bash.git] / pcomplib.c
1 /* pcomplib.c - library functions for programmable completion. */
2
3 /* Copyright (C) 1999 Free Software Foundation, Inc.
4
5 This file is part of GNU Bash, the Bourne Again SHell.
6
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
10 version.
11
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
15 for more details.
16
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. */
20
21 #include <config.h>
22
23 #if defined (PROGRAMMABLE_COMPLETION)
24
25 #include "bashansi.h"
26 #include <stdio.h>
27
28 #if defined (HAVE_UNISTD_H)
29 # ifdef _MINIX
30 # include <sys/types.h>
31 # endif
32 # include <unistd.h>
33 #endif
34
35 #include "shell.h"
36 #include "pcomplete.h"
37
38 #define COMPLETE_HASH_BUCKETS 29 /* for testing */
39
40 #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL)
41
42 HASH_TABLE *prog_completes = (HASH_TABLE *)NULL;
43
44 static int progcomp_initialized = 0;
45
46 COMPSPEC *
47 alloc_compspec ()
48 {
49 COMPSPEC *ret;
50
51 ret = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
52 ret->refcount = 0;
53
54 ret->actions = (unsigned long)0;
55 ret->options = (unsigned long)0;
56
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;
64
65 return ret;
66 }
67
68 void
69 free_compspec (cs)
70 COMPSPEC *cs;
71 {
72 cs->refcount--;
73 if (cs->refcount == 0)
74 {
75 FREE (cs->globpat);
76 FREE (cs->words);
77 FREE (cs->prefix);
78 FREE (cs->suffix);
79 FREE (cs->funcname);
80 FREE (cs->command);
81 FREE (cs->filterpat);
82
83 free (cs);
84 }
85 }
86
87 COMPSPEC *
88 copy_compspec (cs)
89 COMPSPEC *cs;
90 {
91 COMPSPEC *new;
92
93 new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
94
95 new->refcount = cs->refcount;
96 new->actions = cs->actions;
97 new->options = cs->options;
98
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);
106
107 return new;
108 }
109
110 void
111 initialize_progcomp ()
112 {
113 if (progcomp_initialized == 0)
114 {
115 prog_completes = make_hash_table (COMPLETE_HASH_BUCKETS);
116 progcomp_initialized = 1;
117 }
118 }
119
120 int
121 num_progcomps ()
122 {
123 if (progcomp_initialized == 0 || prog_completes == 0)
124 return (0);
125 return (prog_completes->nentries);
126 }
127
128 static void
129 free_progcomp (data)
130 char *data;
131 {
132 COMPSPEC *cs;
133
134 cs = (COMPSPEC *)data;
135 free_compspec (cs);
136 }
137
138 void
139 clear_progcomps ()
140 {
141 if (prog_completes)
142 flush_hash_table (prog_completes, free_progcomp);
143 }
144
145 int
146 remove_progcomp (cmd)
147 char *cmd;
148 {
149 register BUCKET_CONTENTS *item;
150
151 if (prog_completes == 0)
152 return 1;
153
154 item = remove_hash_item (cmd, prog_completes);
155 if (item)
156 {
157 free_progcomp (item->data);
158 free (item->key);
159 free (item);
160 return (1);
161 }
162 return (0);
163 }
164
165 int
166 add_progcomp (cmd, cs)
167 char *cmd;
168 COMPSPEC *cs;
169 {
170 register BUCKET_CONTENTS *item;
171
172 if (progcomp_initialized == 0 || prog_completes == 0)
173 initialize_progcomp ();
174
175 if (cs == NULL)
176 programming_error ("add_progcomp: %s: NULL COMPSPEC", cmd);
177
178 item = add_hash_item (cmd, prog_completes);
179 if (item->data)
180 free_progcomp (item->data);
181 else
182 item->key = savestring (cmd);
183 item->data = (char *)cs;
184 cs->refcount++;
185 return 1;
186 }
187
188 COMPSPEC *
189 find_compspec (cmd)
190 const char *cmd;
191 {
192 register BUCKET_CONTENTS *item;
193 COMPSPEC *cs;
194
195 if (prog_completes == 0)
196 return ((COMPSPEC *)NULL);
197
198 item = find_hash_item ((char *)cmd, prog_completes); /* XXX fix const later */
199
200 if (item == NULL)
201 return ((COMPSPEC *)NULL);
202
203 cs = (COMPSPEC *)item->data;
204
205 return (cs);
206 }
207
208 void
209 print_all_compspecs (pfunc)
210 VFunction *pfunc;
211 {
212 BUCKET_CONTENTS *item_list;
213 int bucket;
214 COMPSPEC *cs;
215
216 if (prog_completes == 0 || pfunc == 0)
217 return;
218
219 for (bucket = 0; bucket < prog_completes->nbuckets; bucket++)
220 {
221 item_list = get_hash_bucket (bucket, prog_completes);
222 if (item_list == 0)
223 continue;
224
225 for ( ; item_list; item_list = item_list->next)
226 {
227 cs = (COMPSPEC *)item_list->data;
228 (*pfunc) (item_list->key, cs);
229 }
230 }
231 }
232
233 #endif /* PROGRAMMABLE_COMPLETION */