]>
Commit | Line | Data |
---|---|---|
bb70624e JA |
1 | /* pcomplib.c - library functions for programmable completion. */ |
2 | ||
3185942a | 3 | /* Copyright (C) 1999-2009 Free Software Foundation, Inc. |
bb70624e JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
3185942a JA |
7 | Bash 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. | |
bb70624e | 11 | |
3185942a JA |
12 | Bash 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. | |
bb70624e | 16 | |
3185942a JA |
17 | You should have received a copy of the GNU General Public License |
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
bb70624e JA |
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 | ||
b80f6443 JA |
35 | #include "bashintl.h" |
36 | ||
bb70624e JA |
37 | #include "shell.h" |
38 | #include "pcomplete.h" | |
39 | ||
ac50fbac | 40 | #define COMPLETE_HASH_BUCKETS 128 /* must be power of two */ |
bb70624e JA |
41 | |
42 | #define STRDUP(x) ((x) ? savestring (x) : (char *)NULL) | |
43 | ||
44 | HASH_TABLE *prog_completes = (HASH_TABLE *)NULL; | |
45 | ||
f73dda09 JA |
46 | static void free_progcomp __P((PTR_T)); |
47 | ||
bb70624e | 48 | COMPSPEC * |
7117c2d2 | 49 | compspec_create () |
bb70624e JA |
50 | { |
51 | COMPSPEC *ret; | |
52 | ||
53 | ret = (COMPSPEC *)xmalloc (sizeof (COMPSPEC)); | |
54 | ret->refcount = 0; | |
55 | ||
56 | ret->actions = (unsigned long)0; | |
28ef6c31 | 57 | ret->options = (unsigned long)0; |
bb70624e JA |
58 | |
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; | |
495aee44 | 65 | ret->lcommand = (char *)NULL; |
bb70624e JA |
66 | ret->filterpat = (char *)NULL; |
67 | ||
68 | return ret; | |
69 | } | |
70 | ||
71 | void | |
7117c2d2 | 72 | compspec_dispose (cs) |
bb70624e JA |
73 | COMPSPEC *cs; |
74 | { | |
75 | cs->refcount--; | |
76 | if (cs->refcount == 0) | |
77 | { | |
78 | FREE (cs->globpat); | |
79 | FREE (cs->words); | |
80 | FREE (cs->prefix); | |
81 | FREE (cs->suffix); | |
82 | FREE (cs->funcname); | |
83 | FREE (cs->command); | |
495aee44 | 84 | FREE (cs->lcommand); |
bb70624e JA |
85 | FREE (cs->filterpat); |
86 | ||
87 | free (cs); | |
88 | } | |
89 | } | |
90 | ||
91 | COMPSPEC * | |
7117c2d2 | 92 | compspec_copy (cs) |
bb70624e JA |
93 | COMPSPEC *cs; |
94 | { | |
95 | COMPSPEC *new; | |
96 | ||
97 | new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC)); | |
98 | ||
99 | new->refcount = cs->refcount; | |
100 | new->actions = cs->actions; | |
28ef6c31 | 101 | new->options = cs->options; |
bb70624e JA |
102 | |
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); | |
495aee44 | 109 | new->lcommand = STRDUP (cs->lcommand); |
bb70624e JA |
110 | new->filterpat = STRDUP (cs->filterpat); |
111 | ||
112 | return new; | |
113 | } | |
114 | ||
115 | void | |
7117c2d2 | 116 | progcomp_create () |
bb70624e | 117 | { |
7117c2d2 JA |
118 | if (prog_completes == 0) |
119 | prog_completes = hash_create (COMPLETE_HASH_BUCKETS); | |
bb70624e JA |
120 | } |
121 | ||
122 | int | |
7117c2d2 | 123 | progcomp_size () |
bb70624e | 124 | { |
7117c2d2 | 125 | return (HASH_ENTRIES (prog_completes)); |
bb70624e JA |
126 | } |
127 | ||
128 | static void | |
129 | free_progcomp (data) | |
f73dda09 | 130 | PTR_T data; |
bb70624e JA |
131 | { |
132 | COMPSPEC *cs; | |
133 | ||
134 | cs = (COMPSPEC *)data; | |
7117c2d2 | 135 | compspec_dispose (cs); |
bb70624e JA |
136 | } |
137 | ||
138 | void | |
7117c2d2 JA |
139 | progcomp_flush () |
140 | { | |
141 | if (prog_completes) | |
142 | hash_flush (prog_completes, free_progcomp); | |
143 | } | |
144 | ||
145 | void | |
146 | progcomp_dispose () | |
bb70624e JA |
147 | { |
148 | if (prog_completes) | |
7117c2d2 JA |
149 | hash_dispose (prog_completes); |
150 | prog_completes = (HASH_TABLE *)NULL; | |
bb70624e JA |
151 | } |
152 | ||
153 | int | |
7117c2d2 | 154 | progcomp_remove (cmd) |
bb70624e JA |
155 | char *cmd; |
156 | { | |
157 | register BUCKET_CONTENTS *item; | |
158 | ||
159 | if (prog_completes == 0) | |
160 | return 1; | |
161 | ||
7117c2d2 | 162 | item = hash_remove (cmd, prog_completes, 0); |
bb70624e JA |
163 | if (item) |
164 | { | |
7117c2d2 JA |
165 | if (item->data) |
166 | free_progcomp (item->data); | |
bb70624e JA |
167 | free (item->key); |
168 | free (item); | |
169 | return (1); | |
170 | } | |
171 | return (0); | |
172 | } | |
173 | ||
174 | int | |
7117c2d2 | 175 | progcomp_insert (cmd, cs) |
bb70624e JA |
176 | char *cmd; |
177 | COMPSPEC *cs; | |
178 | { | |
179 | register BUCKET_CONTENTS *item; | |
180 | ||
bb70624e | 181 | if (cs == NULL) |
b80f6443 | 182 | programming_error (_("progcomp_insert: %s: NULL COMPSPEC"), cmd); |
7117c2d2 JA |
183 | |
184 | if (prog_completes == 0) | |
185 | progcomp_create (); | |
bb70624e | 186 | |
b80f6443 | 187 | cs->refcount++; |
7117c2d2 | 188 | item = hash_insert (cmd, prog_completes, 0); |
bb70624e JA |
189 | if (item->data) |
190 | free_progcomp (item->data); | |
191 | else | |
192 | item->key = savestring (cmd); | |
7117c2d2 | 193 | item->data = cs; |
b80f6443 | 194 | |
bb70624e JA |
195 | return 1; |
196 | } | |
197 | ||
198 | COMPSPEC * | |
7117c2d2 | 199 | progcomp_search (cmd) |
28ef6c31 | 200 | const char *cmd; |
bb70624e JA |
201 | { |
202 | register BUCKET_CONTENTS *item; | |
203 | COMPSPEC *cs; | |
204 | ||
205 | if (prog_completes == 0) | |
206 | return ((COMPSPEC *)NULL); | |
207 | ||
7117c2d2 | 208 | item = hash_search (cmd, prog_completes, 0); |
bb70624e JA |
209 | |
210 | if (item == NULL) | |
211 | return ((COMPSPEC *)NULL); | |
212 | ||
213 | cs = (COMPSPEC *)item->data; | |
214 | ||
215 | return (cs); | |
216 | } | |
217 | ||
218 | void | |
7117c2d2 JA |
219 | progcomp_walk (pfunc) |
220 | hash_wfunc *pfunc; | |
bb70624e | 221 | { |
7117c2d2 | 222 | if (prog_completes == 0 || pfunc == 0 || HASH_ENTRIES (prog_completes) == 0) |
bb70624e JA |
223 | return; |
224 | ||
7117c2d2 | 225 | hash_walk (prog_completes, pfunc); |
bb70624e JA |
226 | } |
227 | ||
228 | #endif /* PROGRAMMABLE_COMPLETION */ |