133c6f40744e654ca35197a797bd565d70949d94
[platform/upstream/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
56   ret->globpat = (char *)NULL;
57   ret->words = (char *)NULL;
58   ret->prefix = (char *)NULL;
59   ret->suffix = (char *)NULL;
60   ret->funcname = (char *)NULL;
61   ret->command = (char *)NULL;
62   ret->filterpat = (char *)NULL;
63
64   return ret;
65 }
66
67 void
68 free_compspec (cs)
69      COMPSPEC *cs;
70 {
71   cs->refcount--;
72   if (cs->refcount == 0)
73     {
74       FREE (cs->globpat);
75       FREE (cs->words);
76       FREE (cs->prefix);
77       FREE (cs->suffix);
78       FREE (cs->funcname);
79       FREE (cs->command);
80       FREE (cs->filterpat);
81
82       free (cs);
83     }
84 }
85
86 COMPSPEC *
87 copy_compspec (cs)
88      COMPSPEC *cs;
89 {
90   COMPSPEC *new;
91
92   new = (COMPSPEC *)xmalloc (sizeof (COMPSPEC));
93
94   new->refcount = cs->refcount;
95   new->actions = cs->actions;
96
97   new->globpat = STRDUP (cs->globpat);
98   new->words = STRDUP (cs->words);
99   new->prefix = STRDUP (cs->prefix);
100   new->suffix = STRDUP (cs->suffix);
101   new->funcname = STRDUP (cs->funcname);
102   new->command = STRDUP (cs->command);
103   new->filterpat = STRDUP (cs->filterpat);
104
105   return new;
106 }
107
108 void
109 initialize_progcomp ()
110 {
111   if (progcomp_initialized == 0)
112     {
113       prog_completes = make_hash_table (COMPLETE_HASH_BUCKETS);
114       progcomp_initialized = 1;
115     }
116 }
117
118 int
119 num_progcomps ()
120 {
121   if (progcomp_initialized == 0 || prog_completes == 0)
122     return (0);
123   return (prog_completes->nentries);
124 }
125
126 static void
127 free_progcomp (data)
128      char *data;
129 {
130   COMPSPEC *cs;
131
132   cs = (COMPSPEC *)data;
133   free_compspec (cs);
134 }
135   
136 void
137 clear_progcomps ()
138 {
139   if (prog_completes)
140     flush_hash_table (prog_completes, free_progcomp);
141 }
142
143 int
144 remove_progcomp (cmd)
145      char *cmd;
146 {
147   register BUCKET_CONTENTS *item;
148
149   if (prog_completes == 0)
150     return 1;
151
152   item = remove_hash_item (cmd, prog_completes);
153   if (item)
154     {
155       free_progcomp (item->data);
156       free (item->key);
157       free (item);
158       return (1);
159     }
160   return (0);
161 }
162
163 int
164 add_progcomp (cmd, cs)
165       char *cmd;
166       COMPSPEC *cs;
167 {
168   register BUCKET_CONTENTS *item;
169
170   if (progcomp_initialized == 0 || prog_completes == 0)
171     initialize_progcomp ();
172
173   if (cs == NULL)
174     programming_error ("add_progcomp: %s: NULL COMPSPEC", cmd);
175
176   item = add_hash_item (cmd, prog_completes);
177   if (item->data)
178     free_progcomp (item->data);
179   else
180     item->key = savestring (cmd);
181   item->data = (char *)cs;
182   cs->refcount++;
183   return 1;
184 }
185
186 COMPSPEC *
187 find_compspec (cmd)
188      char *cmd;
189 {
190   register BUCKET_CONTENTS *item;
191   COMPSPEC *cs;
192
193   if (prog_completes == 0)
194     return ((COMPSPEC *)NULL);
195
196   item = find_hash_item (cmd, prog_completes);
197
198   if (item == NULL)
199     return ((COMPSPEC *)NULL);
200
201   cs = (COMPSPEC *)item->data;
202
203   return (cs);
204 }
205
206 void
207 print_all_compspecs (pfunc)
208      VFunction *pfunc;
209 {
210   BUCKET_CONTENTS *item_list;
211   int bucket;
212   COMPSPEC *cs;
213
214   if (prog_completes == 0 || pfunc == 0)
215     return;
216
217   for (bucket = 0; bucket < prog_completes->nbuckets; bucket++)
218     {
219       item_list = get_hash_bucket (bucket, prog_completes);
220       if (item_list == 0)
221         continue;
222
223       for ( ; item_list; item_list = item_list->next)
224         {
225           cs = (COMPSPEC *)item_list->data;
226           (*pfunc) (item_list->key, cs);
227         }
228     }
229 }
230
231 #endif /* PROGRAMMABLE_COMPLETION */