source: vendor/bash/3.1/builtins/hash.def

Last change on this file was 3228, checked in by bird, 18 years ago

bash 3.1

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