blob: 85ef364bc8be2b0cfdeb9f3af0186e04ecff5704 [file] [log] [blame]
Jari Aalto726f6381996-08-26 18:22:31 +00001This file is hash.def, from which is created hash.c.
2It implements the builtin "hash" in Bash.
3
Chet Rameyac50fba2014-02-26 09:36:43 -05004Copyright (C) 1987-2013 Free Software Foundation, Inc.
Jari Aalto726f6381996-08-26 18:22:31 +00005
6This file is part of GNU Bash, the Bourne Again SHell.
7
Jari Aalto31859422009-01-12 13:36:28 +00008Bash is free software: you can redistribute it and/or modify
9it under the terms of the GNU General Public License as published by
10the Free Software Foundation, either version 3 of the License, or
11(at your option) any later version.
Jari Aalto726f6381996-08-26 18:22:31 +000012
Jari Aalto31859422009-01-12 13:36:28 +000013Bash is distributed in the hope that it will be useful,
14but WITHOUT ANY WARRANTY; without even the implied warranty of
15MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16GNU General Public License for more details.
Jari Aalto726f6381996-08-26 18:22:31 +000017
Jari Aalto31859422009-01-12 13:36:28 +000018You should have received a copy of the GNU General Public License
19along with Bash. If not, see <http://www.gnu.org/licenses/>.
Jari Aalto726f6381996-08-26 18:22:31 +000020
21$PRODUCES hash.c
22
23$BUILTIN hash
24$FUNCTION hash_builtin
Jari Aalto7117c2d2002-07-17 14:10:11 +000025$SHORT_DOC hash [-lr] [-p pathname] [-dt] [name ...]
Jari Aalto31859422009-01-12 13:36:28 +000026Remember or display program locations.
27
28Determine and remember the full pathname of each command NAME. If
29no arguments are given, information about remembered commands is displayed.
30
31Options:
32 -d forget the remembered location of each NAME
33 -l display in a format that may be reused as input
Chet Rameyac50fba2014-02-26 09:36:43 -050034 -p pathname use PATHNAME as the full pathname of NAME
Jari Aalto31859422009-01-12 13:36:28 +000035 -r forget all remembered locations
36 -t print the remembered location of each NAME, preceding
37 each location with the corresponding NAME if multiple
38 NAMEs are given
39Arguments:
40 NAME Each NAME is searched for in $PATH and added to the list
41 of remembered commands.
42
43Exit Status:
44Returns success unless NAME is not found or an invalid option is given.
Jari Aalto726f6381996-08-26 18:22:31 +000045$END
46
Jari Aaltoccc6cda1996-12-23 17:02:34 +000047#include <config.h>
48
Jari Aalto726f6381996-08-26 18:22:31 +000049#include <stdio.h>
50
Jari Aaltocce855b1998-04-17 19:52:44 +000051#include "../bashtypes.h"
52
Jari Aaltoccc6cda1996-12-23 17:02:34 +000053#if defined (HAVE_UNISTD_H)
54# include <unistd.h>
55#endif
56
Jari Aalto28ef6c32001-04-06 19:14:31 +000057#include <errno.h>
58
Jari Aaltoccc6cda1996-12-23 17:02:34 +000059#include "../bashansi.h"
Jari Aaltob80f6442004-07-27 13:29:18 +000060#include "../bashintl.h"
Jari Aalto726f6381996-08-26 18:22:31 +000061
62#include "../shell.h"
63#include "../builtins.h"
Jari Aaltod166f041997-06-05 14:59:13 +000064#include "../flags.h"
Jari Aaltocce855b1998-04-17 19:52:44 +000065#include "../findcmd.h"
Jari Aaltod166f041997-06-05 14:59:13 +000066#include "../hashcmd.h"
Jari Aalto726f6381996-08-26 18:22:31 +000067#include "common.h"
Jari Aaltoccc6cda1996-12-23 17:02:34 +000068#include "bashgetopt.h"
Jari Aalto726f6381996-08-26 18:22:31 +000069
Jari Aalto06285672006-10-10 14:15:34 +000070extern int posixly_correct;
Jari Aalto726f6381996-08-26 18:22:31 +000071extern int dot_found_in_search;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000072extern char *this_command_name;
73
Jari Aaltof73dda02001-11-13 17:56:06 +000074static int add_hashed_command __P((char *, int));
Jari Aalto7117c2d2002-07-17 14:10:11 +000075static int print_hash_info __P((BUCKET_CONTENTS *));
76static int print_portable_hash_info __P((BUCKET_CONTENTS *));
77static int print_hashed_commands __P((int));
78static int list_hashed_filename_targets __P((WORD_LIST *, int));
Jari Aaltoccc6cda1996-12-23 17:02:34 +000079
Jari Aalto726f6381996-08-26 18:22:31 +000080/* Print statistics on the current state of hashed commands. If LIST is
81 not empty, then rehash (or hash in the first place) the specified
82 commands. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +000083int
Jari Aalto726f6381996-08-26 18:22:31 +000084hash_builtin (list)
85 WORD_LIST *list;
86{
Jari Aalto7117c2d2002-07-17 14:10:11 +000087 int expunge_hash_table, list_targets, list_portably, delete, opt;
Jari Aaltocce855b1998-04-17 19:52:44 +000088 char *w, *pathname;
Jari Aalto726f6381996-08-26 18:22:31 +000089
Jari Aaltoccc6cda1996-12-23 17:02:34 +000090 if (hashing_enabled == 0)
Jari Aalto726f6381996-08-26 18:22:31 +000091 {
Jari Aaltob80f6442004-07-27 13:29:18 +000092 builtin_error (_("hashing disabled"));
Jari Aalto726f6381996-08-26 18:22:31 +000093 return (EXECUTION_FAILURE);
94 }
95
Jari Aalto7117c2d2002-07-17 14:10:11 +000096 expunge_hash_table = list_targets = list_portably = delete = 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +000097 pathname = (char *)NULL;
98 reset_internal_getopt ();
Jari Aalto7117c2d2002-07-17 14:10:11 +000099 while ((opt = internal_getopt (list, "dlp:rt")) != -1)
Jari Aalto726f6381996-08-26 18:22:31 +0000100 {
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000101 switch (opt)
Jari Aalto726f6381996-08-26 18:22:31 +0000102 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000103 case 'd':
104 delete = 1;
105 break;
106 case 'l':
107 list_portably = 1;
Jari Aalto726f6381996-08-26 18:22:31 +0000108 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000109 case 'p':
110 pathname = list_optarg;
111 break;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000112 case 'r':
113 expunge_hash_table = 1;
114 break;
Jari Aaltof73dda02001-11-13 17:56:06 +0000115 case 't':
116 list_targets = 1;
117 break;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000118 default:
119 builtin_usage ();
Jari Aalto726f6381996-08-26 18:22:31 +0000120 return (EX_USAGE);
121 }
Jari Aalto726f6381996-08-26 18:22:31 +0000122 }
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000123 list = loptend;
Jari Aalto726f6381996-08-26 18:22:31 +0000124
Jari Aaltof73dda02001-11-13 17:56:06 +0000125 /* hash -t requires at least one argument. */
126 if (list == 0 && list_targets)
127 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000128 sh_needarg ("-t");
Jari Aaltof73dda02001-11-13 17:56:06 +0000129 return (EXECUTION_FAILURE);
130 }
131
Jari Aalto7117c2d2002-07-17 14:10:11 +0000132 /* We want hash -r to be silent, but hash -- to print hashing info, so
133 we test expunge_hash_table. */
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000134 if (list == 0 && expunge_hash_table == 0)
Jari Aalto726f6381996-08-26 18:22:31 +0000135 {
Jari Aalto06285672006-10-10 14:15:34 +0000136 opt = print_hashed_commands (list_portably);
137 if (opt == 0 && posixly_correct == 0)
138 printf (_("%s: hash table empty\n"), this_command_name);
Jari Aalto726f6381996-08-26 18:22:31 +0000139
140 return (EXECUTION_SUCCESS);
141 }
142
143 if (expunge_hash_table)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000144 phash_flush ();
Jari Aalto726f6381996-08-26 18:22:31 +0000145
Jari Aaltof73dda02001-11-13 17:56:06 +0000146 /* If someone runs `hash -r -t xyz' he will be disappointed. */
147 if (list_targets)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000148 return (list_hashed_filename_targets (list, list_portably));
Jari Aaltof73dda02001-11-13 17:56:06 +0000149
Jari Aaltobb706242000-03-17 21:46:59 +0000150#if defined (RESTRICTED_SHELL)
151 if (restricted && pathname && strchr (pathname, '/'))
152 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000153 sh_restricted (pathname);
Jari Aaltobb706242000-03-17 21:46:59 +0000154 return (EXECUTION_FAILURE);
155 }
156#endif
157
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000158 for (opt = EXECUTION_SUCCESS; list; list = list->next)
Jari Aalto726f6381996-08-26 18:22:31 +0000159 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000160 /* Add, remove or rehash the specified commands. */
Jari Aaltocce855b1998-04-17 19:52:44 +0000161 w = list->word->word;
Chet Ramey495aee42011-11-22 19:11:26 -0500162 if (absolute_program (w))
163 continue;
164 else if (pathname)
Jari Aalto28ef6c32001-04-06 19:14:31 +0000165 {
166 if (is_directory (pathname))
167 {
168#ifdef EISDIR
169 builtin_error ("%s: %s", pathname, strerror (EISDIR));
170#else
Jari Aalto31859422009-01-12 13:36:28 +0000171 builtin_error (_("%s: is a directory"), pathname);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000172#endif
173 opt = EXECUTION_FAILURE;
174 }
175 else
Jari Aalto7117c2d2002-07-17 14:10:11 +0000176 phash_insert (w, pathname, 0, 0);
Jari Aalto28ef6c32001-04-06 19:14:31 +0000177 }
Jari Aalto95732b42005-12-07 14:08:12 +0000178 else if (delete)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000179 {
Jari Aalto95732b42005-12-07 14:08:12 +0000180 if (phash_remove (w))
181 {
182 sh_notfound (w);
183 opt = EXECUTION_FAILURE;
184 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000185 }
Jari Aaltocce855b1998-04-17 19:52:44 +0000186 else if (add_hashed_command (w, 0))
Jari Aaltod166f041997-06-05 14:59:13 +0000187 opt = EXECUTION_FAILURE;
Jari Aalto726f6381996-08-26 18:22:31 +0000188 }
189
190 fflush (stdout);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000191 return (opt);
Jari Aalto726f6381996-08-26 18:22:31 +0000192}
193
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000194static int
Jari Aaltocce855b1998-04-17 19:52:44 +0000195add_hashed_command (w, quiet)
196 char *w;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000197 int quiet;
198{
199 int rv;
200 char *full_path;
201
202 rv = 0;
Jari Aaltocce855b1998-04-17 19:52:44 +0000203 if (find_function (w) == 0 && find_shell_builtin (w) == 0)
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000204 {
Chet Ramey495aee42011-11-22 19:11:26 -0500205 phash_remove (w);
Jari Aaltocce855b1998-04-17 19:52:44 +0000206 full_path = find_user_command (w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000207 if (full_path && executable_file (full_path))
Jari Aalto7117c2d2002-07-17 14:10:11 +0000208 phash_insert (w, full_path, dot_found_in_search, 0);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000209 else
210 {
211 if (quiet == 0)
Jari Aalto7117c2d2002-07-17 14:10:11 +0000212 sh_notfound (w);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000213 rv++;
214 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000215 FREE (full_path);
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000216 }
217 return (rv);
218}
219
220/* Print information about current hashed info. */
221static int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000222print_hash_info (item)
223 BUCKET_CONTENTS *item;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000224{
Jari Aalto7117c2d2002-07-17 14:10:11 +0000225 printf ("%4d\t%s\n", item->times_found, pathdata(item)->path);
226 return 0;
Jari Aaltoccc6cda1996-12-23 17:02:34 +0000227}
Jari Aaltof73dda02001-11-13 17:56:06 +0000228
229static int
Jari Aalto7117c2d2002-07-17 14:10:11 +0000230print_portable_hash_info (item)
231 BUCKET_CONTENTS *item;
232{
233 printf ("builtin hash -p %s %s\n", pathdata(item)->path, item->key);
234 return 0;
235}
236
237static int
238print_hashed_commands (fmt)
239 int fmt;
240{
241 if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
242 return (0);
243
244 if (fmt == 0)
Jari Aalto31859422009-01-12 13:36:28 +0000245 printf (_("hits\tcommand\n"));
Jari Aalto7117c2d2002-07-17 14:10:11 +0000246 hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info);
247 return (1);
248}
249
250static int
251list_hashed_filename_targets (list, fmt)
Jari Aaltof73dda02001-11-13 17:56:06 +0000252 WORD_LIST *list;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000253 int fmt;
Jari Aaltof73dda02001-11-13 17:56:06 +0000254{
255 int all_found, multiple;
256 char *target;
257 WORD_LIST *l;
258
259 all_found = 1;
260 multiple = list->next != 0;
261
262 for (l = list; l; l = l->next)
263 {
Jari Aalto7117c2d2002-07-17 14:10:11 +0000264 target = phash_search (l->word->word);
Jari Aaltof73dda02001-11-13 17:56:06 +0000265 if (target == 0)
266 {
267 all_found = 0;
Jari Aalto7117c2d2002-07-17 14:10:11 +0000268 sh_notfound (l->word->word);
Jari Aaltof73dda02001-11-13 17:56:06 +0000269 continue;
270 }
Jari Aalto7117c2d2002-07-17 14:10:11 +0000271 if (fmt)
272 printf ("builtin hash -p %s %s\n", target, l->word->word);
273 else
274 {
275 if (multiple)
276 printf ("%s\t", l->word->word);
277 printf ("%s\n", target);
278 }
Chet Rameyac50fba2014-02-26 09:36:43 -0500279 free (target);
Jari Aaltof73dda02001-11-13 17:56:06 +0000280 }
281
282 return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
283}