Imported from ../bash-3.2.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-2006 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 #include "../bashintl.h"
53
54 #include "../shell.h"
55 #include "../builtins.h"
56 #include "../flags.h"
57 #include "../findcmd.h"
58 #include "../hashcmd.h"
59 #include "common.h"
60 #include "bashgetopt.h"
61
62 extern int posixly_correct;
63 extern int dot_found_in_search;
64 extern char *this_command_name;
65
66 static int add_hashed_command __P((char *, int));
67 static int print_hash_info __P((BUCKET_CONTENTS *));
68 static int print_portable_hash_info __P((BUCKET_CONTENTS *));
69 static int print_hashed_commands __P((int));
70 static int list_hashed_filename_targets __P((WORD_LIST *, int));
71
72 /* Print statistics on the current state of hashed commands.  If LIST is
73    not empty, then rehash (or hash in the first place) the specified
74    commands. */
75 int
76 hash_builtin (list)
77      WORD_LIST *list;
78 {
79   int expunge_hash_table, list_targets, list_portably, delete, opt;
80   char *w, *pathname;
81
82   if (hashing_enabled == 0)
83     {
84       builtin_error (_("hashing disabled"));
85       return (EXECUTION_FAILURE);
86     }
87
88   expunge_hash_table = list_targets = list_portably = delete = 0;
89   pathname = (char *)NULL;
90   reset_internal_getopt ();
91   while ((opt = internal_getopt (list, "dlp:rt")) != -1)
92     {
93       switch (opt)
94         {
95         case 'd':
96           delete = 1;
97           break;
98         case 'l':
99           list_portably = 1;
100           break;
101         case 'p':
102           pathname = list_optarg;
103           break;
104         case 'r':
105           expunge_hash_table = 1;
106           break;
107         case 't':
108           list_targets = 1;
109           break;
110         default:
111           builtin_usage ();
112           return (EX_USAGE);
113         }
114     }
115   list = loptend;
116
117   /* hash -t requires at least one argument. */
118   if (list == 0 && list_targets)
119     {
120       sh_needarg ("-t");
121       return (EXECUTION_FAILURE);
122     }
123
124   /* We want hash -r to be silent, but hash -- to print hashing info, so
125      we test expunge_hash_table. */
126   if (list == 0 && expunge_hash_table == 0)
127     {
128       opt = print_hashed_commands (list_portably);
129       if (opt == 0 && posixly_correct == 0)
130         printf (_("%s: hash table empty\n"), this_command_name);
131
132       return (EXECUTION_SUCCESS);
133     }
134
135   if (expunge_hash_table)
136     phash_flush ();
137
138   /* If someone runs `hash -r -t xyz' he will be disappointed. */
139   if (list_targets)
140     return (list_hashed_filename_targets (list, list_portably));
141       
142 #if defined (RESTRICTED_SHELL)
143   if (restricted && pathname && strchr (pathname, '/'))
144     {
145       sh_restricted (pathname);
146       return (EXECUTION_FAILURE);
147     }
148 #endif
149
150   for (opt = EXECUTION_SUCCESS; list; list = list->next)
151     {
152       /* Add, remove or rehash the specified commands. */
153       w = list->word->word;
154       if (pathname)
155         {
156           if (is_directory (pathname))
157             {
158 #ifdef EISDIR
159               builtin_error ("%s: %s", pathname, strerror (EISDIR));
160 #else
161               builtin_error ("%s: is a directory", pathname);
162 #endif
163               opt = EXECUTION_FAILURE;
164             }
165           else
166             phash_insert (w, pathname, 0, 0);
167         }
168       else if (absolute_program (w))
169         continue;
170       else if (delete)
171         {
172           if (phash_remove (w))
173             {
174               sh_notfound (w);
175               opt = EXECUTION_FAILURE;
176             }
177         }
178       else if (add_hashed_command (w, 0))
179         opt = EXECUTION_FAILURE;
180     }
181
182   fflush (stdout);
183   return (opt);
184 }
185
186 static int
187 add_hashed_command (w, quiet)
188      char *w;
189      int quiet;
190 {
191   int rv;
192   char *full_path;
193
194   rv = 0;
195   if (find_function (w) == 0 && find_shell_builtin (w) == 0)
196     {
197       full_path = find_user_command (w);
198       if (full_path && executable_file (full_path))
199         phash_insert (w, full_path, dot_found_in_search, 0);
200       else
201         {
202           if (quiet == 0)
203             sh_notfound (w);
204           rv++;
205         }
206       FREE (full_path);
207     }
208   return (rv);
209 }
210
211 /* Print information about current hashed info. */
212 static int
213 print_hash_info (item)
214      BUCKET_CONTENTS *item;
215 {
216   printf ("%4d\t%s\n", item->times_found, pathdata(item)->path);
217   return 0;
218 }
219
220 static int
221 print_portable_hash_info (item)
222      BUCKET_CONTENTS *item;
223 {
224   printf ("builtin hash -p %s %s\n", pathdata(item)->path, item->key);
225   return 0;
226 }
227
228 static int
229 print_hashed_commands (fmt)
230      int fmt;
231 {
232   if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
233     return (0);
234
235   if (fmt == 0)
236     printf ("hits\tcommand\n");
237   hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info);
238   return (1);
239 }
240
241 static int
242 list_hashed_filename_targets (list, fmt)
243      WORD_LIST *list;
244      int fmt;
245 {
246   int all_found, multiple;
247   char *target;
248   WORD_LIST *l;
249
250   all_found = 1;
251   multiple = list->next != 0;
252
253   for (l = list; l; l = l->next)
254     {
255       target = phash_search (l->word->word);
256       if (target == 0)
257         {
258           all_found = 0;
259           sh_notfound (l->word->word);
260           continue;
261         }
262       if (fmt)
263         printf ("builtin hash -p %s %s\n", target, l->word->word);
264       else
265         {
266           if (multiple)
267             printf ("%s\t", l->word->word);
268           printf ("%s\n", target);
269         }
270     }
271
272   return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
273 }