aboutsummaryrefslogtreecommitdiffstats
path: root/builtins/hash.def
blob: 2f69f6581c374393b626fa3f755f39dbcea7b9be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
This file is hash.def, from which is created hash.c.
It implements the builtin "hash" in Bash.

Copyright (C) 1987, 1989, 1991 Free Software Foundation, Inc.

This file is part of GNU Bash, the Bourne Again SHell.

Bash is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 1, or (at your option) any later
version.

Bash is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with Bash; see the file COPYING.  If not, write to the Free Software
Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

$PRODUCES hash.c

$BUILTIN hash
$FUNCTION hash_builtin
$SHORT_DOC hash [-r] [-p pathname] [name ...]
For each NAME, the full pathname of the command is determined and
remembered.  If the -p option is supplied, PATHNAME is used as the
full pathname of NAME, and no path search is performed.  The -r
option causes the shell to forget all remembered locations.  If no
arguments are given, information about remembered commands is displayed.
$END

#include <config.h>

#include <sys/types.h>
#include "../posixstat.h"

#include <stdio.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "../bashansi.h"

#include "../shell.h"
#include "../builtins.h"
#include "../flags.h"
#include "../execute_cmd.h"
#include "hashcom.h"
#include "common.h"
#include "bashgetopt.h"

extern int dot_found_in_search;
extern char *this_command_name;

static int add_hashed_command ();
static int print_hashed_commands ();

static int hashing_initialized = 0;

HASH_TABLE *hashed_filenames;

void
initialize_filename_hashing ()
{
  if (hashing_initialized == 0)
    {
      hashed_filenames = make_hash_table (FILENAME_HASH_BUCKETS);
      hashing_initialized = 1;
    }
}

static void
free_filename_data (data)
     char *data;
{
  free (((PATH_DATA *)data)->path);
  free (data);
}

void
flush_hashed_filenames ()
{
  flush_hash_table (hashed_filenames, free_filename_data);
}

/* Remove FILENAME from the table of hashed commands. */
void
remove_hashed_filename (filename)
     char *filename;
{
  register BUCKET_CONTENTS *item;

  if (hashing_enabled == 0)
    return;

  item = remove_hash_item (filename, hashed_filenames);
  if (item)
    {
      if (item->data)
	free_filename_data (item->data);
      free (item->key);
      free (item);
    }
}

/* Print statistics on the current state of hashed commands.  If LIST is
   not empty, then rehash (or hash in the first place) the specified
   commands. */
int
hash_builtin (list)
     WORD_LIST *list;
{
  int expunge_hash_table, opt;
  char *word, *pathname;

  if (hashing_enabled == 0)
    {
      builtin_error ("hashing disabled");
      return (EXECUTION_FAILURE);
    }

  expunge_hash_table = 0;
  pathname = (char *)NULL;
  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "rp:")) != -1)
    {
      switch (opt)
	{
	case 'r':
	  expunge_hash_table = 1;
	  break;
	case 'p':
	  pathname = list_optarg;
	  break;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  /* We want hash -r to be silent, but hash -- to print hashing info.  That
     is the reason for the test of expunge_hash_table. */
  if (list == 0 && expunge_hash_table == 0)
    {
      if (print_hashed_commands () == 0)
	printf ("%s: hash table empty\n", this_command_name);

      return (EXECUTION_SUCCESS);
    }

  if (expunge_hash_table)
    flush_hashed_filenames ();

  for (opt = EXECUTION_SUCCESS; list; list = list->next)
    {
      /* Add or rehash the specified commands. */
      word = list->word->word;
      if (pathname)
	remember_filename (word, pathname, 0, 0);
      else
	{
	  if (absolute_program (word))
	    {
	      list = list->next;
	      continue;
	    }

	  if (add_hashed_command (word))
	    opt = EXECUTION_FAILURE;
	}
    }

  fflush (stdout);

  return (opt);
}

/* Place FILENAME (key) and FULL_PATHNAME (data->path) into the
   hash table.  CHECK_DOT if non-null is for future calls to
   find_hashed_filename (); it means that this file was found
   in a directory in $PATH that is not an absolute pathname.
   FOUND is the initial value for times_found. */
void
remember_filename (filename, full_pathname, check_dot, found)
     char *filename, *full_pathname;
     int check_dot, found;
{
  register BUCKET_CONTENTS *item;

  if (hashing_enabled == 0)
    return;

  item = add_hash_item (filename, hashed_filenames);
  if (item->data)
    free (pathdata(item)->path);
  else
    {
      item->key = savestring (filename);
      item->data = xmalloc (sizeof (PATH_DATA));
    }
  pathdata(item)->path = savestring (full_pathname);
  pathdata(item)->flags = 0;
  if (check_dot)
    pathdata(item)->flags |= HASH_CHKDOT;
  if (*full_pathname != '/')
    pathdata(item)->flags |= HASH_RELPATH;
  item->times_found = found;
}

static int
add_hashed_command (word, quiet)
     char *word;
     int quiet;
{
  int rv;
  char *full_path;

  rv = 0;
  if (find_function (word) == 0 && find_shell_builtin (word) == 0)
    {
      full_path = find_user_command (word);
      if (full_path && executable_file (full_path))
	remember_filename (word, full_path, dot_found_in_search, 0);
      else
	{
	  if (quiet == 0)
	    builtin_error ("%s: not found", word);
	  rv++;
	}
      if (full_path)
	free (full_path);
    }
  return (rv);
}

/* Print information about current hashed info. */
static int
print_hashed_commands ()
{
  BUCKET_CONTENTS *item_list;
  int bucket, any_printed;

  for (bucket = any_printed = 0; bucket < hashed_filenames->nbuckets; bucket++)
    {
      item_list = get_hash_bucket (bucket, hashed_filenames);
      if (item_list == 0)
	continue;

      if (any_printed == 0)
	{
	  printf ("hits\tcommand\n");
	  any_printed++;
	}

      for ( ; item_list; item_list = item_list->next)
	printf ("%4d\t%s\n", item_list->times_found, pathdata(item_list)->path);

    }
  return (any_printed);
}