Imported from ../bash-2.05b.tar.gz.
[platform/upstream/bash.git] / builtins / hash.def
1 This file is hash.def, from which is created hash.c.
2 It implements the builtin "hash" in Bash.
3
4 Copyright (C) 1987-2002 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
12
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING.  If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
21
22 $PRODUCES hash.c
23
24 $BUILTIN hash
25 $FUNCTION hash_builtin
26 $SHORT_DOC hash [-lr] [-p pathname] [-dt] [name ...]
27 For each NAME, the full pathname of the command is determined and
28 remembered.  If the -p option is supplied, PATHNAME is used as the
29 full pathname of NAME, and no path search is performed.  The -r
30 option causes the shell to forget all remembered locations.  The -d
31 option causes the shell to forget the remembered location of each NAME.
32 If the -t option is supplied the full pathname to which each NAME
33 corresponds is printed.  If multiple NAME arguments are supplied with
34 -t, the NAME is printed before the hashed full pathname.  The -l option
35 causes output to be displayed in a format that may be reused as input.
36 If no arguments are given, information about remembered commands is displayed.
37 $END
38
39 #include <config.h>
40
41 #include <stdio.h>
42
43 #include "../bashtypes.h"
44
45 #if defined (HAVE_UNISTD_H)
46 #  include <unistd.h>
47 #endif
48
49 #include <errno.h>
50
51 #include "../bashansi.h"
52
53 #include "../shell.h"
54 #include "../builtins.h"
55 #include "../flags.h"
56 #include "../findcmd.h"
57 #include "../hashcmd.h"
58 #include "common.h"
59 #include "bashgetopt.h"
60
61 extern int dot_found_in_search;
62 extern char *this_command_name;
63
64 static int add_hashed_command __P((char *, int));
65 static int print_hash_info __P((BUCKET_CONTENTS *));
66 static int print_portable_hash_info __P((BUCKET_CONTENTS *));
67 static int print_hashed_commands __P((int));
68 static int list_hashed_filename_targets __P((WORD_LIST *, int));
69
70 /* Print statistics on the current state of hashed commands.  If LIST is
71    not empty, then rehash (or hash in the first place) the specified
72    commands. */
73 int
74 hash_builtin (list)
75      WORD_LIST *list;
76 {
77   int expunge_hash_table, list_targets, list_portably, delete, opt;
78   char *w, *pathname;
79
80   if (hashing_enabled == 0)
81     {
82       builtin_error ("hashing disabled");
83       return (EXECUTION_FAILURE);
84     }
85
86   expunge_hash_table = list_targets = list_portably = delete = 0;
87   pathname = (char *)NULL;
88   reset_internal_getopt ();
89   while ((opt = internal_getopt (list, "dlp:rt")) != -1)
90     {
91       switch (opt)
92         {
93         case 'd':
94           delete = 1;
95           break;
96         case 'l':
97           list_portably = 1;
98           break;
99         case 'p':
100           pathname = list_optarg;
101           break;
102         case 'r':
103           expunge_hash_table = 1;
104           break;
105         case 't':
106           list_targets = 1;
107           break;
108         default:
109           builtin_usage ();
110           return (EX_USAGE);
111         }
112     }
113   list = loptend;
114
115   /* hash -t requires at least one argument. */
116   if (list == 0 && list_targets)
117     {
118       sh_needarg ("-t");
119       return (EXECUTION_FAILURE);
120     }
121
122   /* We want hash -r to be silent, but hash -- to print hashing info, so
123      we test expunge_hash_table. */
124   if (list == 0 && expunge_hash_table == 0)
125     {
126       if (print_hashed_commands (list_portably) == 0)
127         printf ("%s: hash table empty\n", this_command_name);
128
129       return (EXECUTION_SUCCESS);
130     }
131
132   if (expunge_hash_table)
133     phash_flush ();
134
135   /* If someone runs `hash -r -t xyz' he will be disappointed. */
136   if (list_targets)
137     return (list_hashed_filename_targets (list, list_portably));
138       
139 #if defined (RESTRICTED_SHELL)
140   if (restricted && pathname && strchr (pathname, '/'))
141     {
142       sh_restricted (pathname);
143       return (EXECUTION_FAILURE);
144     }
145 #endif
146
147   for (opt = EXECUTION_SUCCESS; list; list = list->next)
148     {
149       /* Add, remove or rehash the specified commands. */
150       w = list->word->word;
151       if (pathname)
152         {
153           if (is_directory (pathname))
154             {
155 #ifdef EISDIR
156               builtin_error ("%s: %s", pathname, strerror (EISDIR));
157 #else
158               builtin_error ("%s: is a directory", pathname);
159 #endif
160               opt = EXECUTION_FAILURE;
161             }
162           else
163             phash_insert (w, pathname, 0, 0);
164         }
165       else if (absolute_program (w))
166         continue;
167       else if (delete && phash_remove (w))
168         {
169           sh_notfound (w);
170           opt = EXECUTION_FAILURE;
171         }
172       else if (add_hashed_command (w, 0))
173         opt = EXECUTION_FAILURE;
174     }
175
176   fflush (stdout);
177   return (opt);
178 }
179
180 static int
181 add_hashed_command (w, quiet)
182      char *w;
183      int quiet;
184 {
185   int rv;
186   char *full_path;
187
188   rv = 0;
189   if (find_function (w) == 0 && find_shell_builtin (w) == 0)
190     {
191       full_path = find_user_command (w);
192       if (full_path && executable_file (full_path))
193         phash_insert (w, full_path, dot_found_in_search, 0);
194       else
195         {
196           if (quiet == 0)
197             sh_notfound (w);
198           rv++;
199         }
200       FREE (full_path);
201     }
202   return (rv);
203 }
204
205 /* Print information about current hashed info. */
206 static int
207 print_hash_info (item)
208      BUCKET_CONTENTS *item;
209 {
210   printf ("%4d\t%s\n", item->times_found, pathdata(item)->path);
211   return 0;
212 }
213
214 static int
215 print_portable_hash_info (item)
216      BUCKET_CONTENTS *item;
217 {
218   printf ("builtin hash -p %s %s\n", pathdata(item)->path, item->key);
219   return 0;
220 }
221
222 static int
223 print_hashed_commands (fmt)
224      int fmt;
225 {
226   if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
227     return (0);
228
229   if (fmt == 0)
230     printf ("hits\tcommand\n");
231   hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info);
232   return (1);
233 }
234
235 static int
236 list_hashed_filename_targets (list, fmt)
237      WORD_LIST *list;
238      int fmt;
239 {
240   int all_found, multiple;
241   char *target;
242   WORD_LIST *l;
243
244   all_found = 1;
245   multiple = list->next != 0;
246
247   for (l = list; l; l = l->next)
248     {
249       target = phash_search (l->word->word);
250       if (target == 0)
251         {
252           all_found = 0;
253           sh_notfound (l->word->word);
254           continue;
255         }
256       if (fmt)
257         printf ("builtin hash -p %s %s\n", target, l->word->word);
258       else
259         {
260           if (multiple)
261             printf ("%s\t", l->word->word);
262           printf ("%s\n", target);
263         }
264     }
265
266   return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
267 }