Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is hash.def, from which is created hash.c. |
| 2 | It implements the builtin "hash" in Bash. |
| 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2013 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES hash.c |
| 22 | |
| 23 | $BUILTIN hash |
| 24 | $FUNCTION hash_builtin |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 25 | $SHORT_DOC hash [-lr] [-p pathname] [-dt] [name ...] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 26 | Remember or display program locations. |
| 27 | |
| 28 | Determine and remember the full pathname of each command NAME. If |
| 29 | no arguments are given, information about remembered commands is displayed. |
| 30 | |
| 31 | Options: |
| 32 | -d forget the remembered location of each NAME |
| 33 | -l display in a format that may be reused as input |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 34 | -p pathname use PATHNAME as the full pathname of NAME |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 35 | -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 |
| 39 | Arguments: |
| 40 | NAME Each NAME is searched for in $PATH and added to the list |
| 41 | of remembered commands. |
| 42 | |
| 43 | Exit Status: |
| 44 | Returns success unless NAME is not found or an invalid option is given. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 45 | $END |
| 46 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 47 | #include <config.h> |
| 48 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 49 | #include <stdio.h> |
| 50 | |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 51 | #include "../bashtypes.h" |
| 52 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 53 | #if defined (HAVE_UNISTD_H) |
| 54 | # include <unistd.h> |
| 55 | #endif |
| 56 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 57 | #include <errno.h> |
| 58 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 59 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 60 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 61 | |
| 62 | #include "../shell.h" |
| 63 | #include "../builtins.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 64 | #include "../flags.h" |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 65 | #include "../findcmd.h" |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 66 | #include "../hashcmd.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 67 | #include "common.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 68 | #include "bashgetopt.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 69 | |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 70 | extern int posixly_correct; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 71 | extern int dot_found_in_search; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 72 | extern char *this_command_name; |
| 73 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 74 | static int add_hashed_command __P((char *, int)); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 75 | static int print_hash_info __P((BUCKET_CONTENTS *)); |
| 76 | static int print_portable_hash_info __P((BUCKET_CONTENTS *)); |
| 77 | static int print_hashed_commands __P((int)); |
| 78 | static int list_hashed_filename_targets __P((WORD_LIST *, int)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 79 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 80 | /* 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 Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 83 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 84 | hash_builtin (list) |
| 85 | WORD_LIST *list; |
| 86 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 87 | int expunge_hash_table, list_targets, list_portably, delete, opt; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 88 | char *w, *pathname; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 89 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 90 | if (hashing_enabled == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 91 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 92 | builtin_error (_("hashing disabled")); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 93 | return (EXECUTION_FAILURE); |
| 94 | } |
| 95 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 96 | expunge_hash_table = list_targets = list_portably = delete = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 97 | pathname = (char *)NULL; |
| 98 | reset_internal_getopt (); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 99 | while ((opt = internal_getopt (list, "dlp:rt")) != -1) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 100 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 101 | switch (opt) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 102 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 103 | case 'd': |
| 104 | delete = 1; |
| 105 | break; |
| 106 | case 'l': |
| 107 | list_portably = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 108 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 109 | case 'p': |
| 110 | pathname = list_optarg; |
| 111 | break; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 112 | case 'r': |
| 113 | expunge_hash_table = 1; |
| 114 | break; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 115 | case 't': |
| 116 | list_targets = 1; |
| 117 | break; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 118 | default: |
| 119 | builtin_usage (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 120 | return (EX_USAGE); |
| 121 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 122 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 123 | list = loptend; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 124 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 125 | /* hash -t requires at least one argument. */ |
| 126 | if (list == 0 && list_targets) |
| 127 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 128 | sh_needarg ("-t"); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 129 | return (EXECUTION_FAILURE); |
| 130 | } |
| 131 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 132 | /* We want hash -r to be silent, but hash -- to print hashing info, so |
| 133 | we test expunge_hash_table. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 134 | if (list == 0 && expunge_hash_table == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 135 | { |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 136 | opt = print_hashed_commands (list_portably); |
| 137 | if (opt == 0 && posixly_correct == 0) |
| 138 | printf (_("%s: hash table empty\n"), this_command_name); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 139 | |
| 140 | return (EXECUTION_SUCCESS); |
| 141 | } |
| 142 | |
| 143 | if (expunge_hash_table) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 144 | phash_flush (); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 145 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 146 | /* If someone runs `hash -r -t xyz' he will be disappointed. */ |
| 147 | if (list_targets) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 148 | return (list_hashed_filename_targets (list, list_portably)); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 149 | |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 150 | #if defined (RESTRICTED_SHELL) |
| 151 | if (restricted && pathname && strchr (pathname, '/')) |
| 152 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 153 | sh_restricted (pathname); |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 154 | return (EXECUTION_FAILURE); |
| 155 | } |
| 156 | #endif |
| 157 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 158 | for (opt = EXECUTION_SUCCESS; list; list = list->next) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 159 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 160 | /* Add, remove or rehash the specified commands. */ |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 161 | w = list->word->word; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 162 | if (absolute_program (w)) |
| 163 | continue; |
| 164 | else if (pathname) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 165 | { |
| 166 | if (is_directory (pathname)) |
| 167 | { |
| 168 | #ifdef EISDIR |
| 169 | builtin_error ("%s: %s", pathname, strerror (EISDIR)); |
| 170 | #else |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 171 | builtin_error (_("%s: is a directory"), pathname); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 172 | #endif |
| 173 | opt = EXECUTION_FAILURE; |
| 174 | } |
| 175 | else |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 176 | phash_insert (w, pathname, 0, 0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 177 | } |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 178 | else if (delete) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 179 | { |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 180 | if (phash_remove (w)) |
| 181 | { |
| 182 | sh_notfound (w); |
| 183 | opt = EXECUTION_FAILURE; |
| 184 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 185 | } |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 186 | else if (add_hashed_command (w, 0)) |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 187 | opt = EXECUTION_FAILURE; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | fflush (stdout); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 191 | return (opt); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 194 | static int |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 195 | add_hashed_command (w, quiet) |
| 196 | char *w; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 197 | int quiet; |
| 198 | { |
| 199 | int rv; |
| 200 | char *full_path; |
| 201 | |
| 202 | rv = 0; |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 203 | if (find_function (w) == 0 && find_shell_builtin (w) == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 204 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 205 | phash_remove (w); |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 206 | full_path = find_user_command (w); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 207 | if (full_path && executable_file (full_path)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 208 | phash_insert (w, full_path, dot_found_in_search, 0); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 209 | else |
| 210 | { |
| 211 | if (quiet == 0) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 212 | sh_notfound (w); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 213 | rv++; |
| 214 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 215 | FREE (full_path); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 216 | } |
| 217 | return (rv); |
| 218 | } |
| 219 | |
| 220 | /* Print information about current hashed info. */ |
| 221 | static int |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 222 | print_hash_info (item) |
| 223 | BUCKET_CONTENTS *item; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 224 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 225 | printf ("%4d\t%s\n", item->times_found, pathdata(item)->path); |
| 226 | return 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 227 | } |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 228 | |
| 229 | static int |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 230 | print_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 | |
| 237 | static int |
| 238 | print_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 Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 245 | printf (_("hits\tcommand\n")); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 246 | hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info); |
| 247 | return (1); |
| 248 | } |
| 249 | |
| 250 | static int |
| 251 | list_hashed_filename_targets (list, fmt) |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 252 | WORD_LIST *list; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 253 | int fmt; |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 254 | { |
| 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 Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 264 | target = phash_search (l->word->word); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 265 | if (target == 0) |
| 266 | { |
| 267 | all_found = 0; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 268 | sh_notfound (l->word->word); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 269 | continue; |
| 270 | } |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 271 | 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 Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 279 | free (target); |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE); |
| 283 | } |