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 2, 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, 59 Temple Place, Suite 330, Boston, MA 02111 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 <stdio.h>

#include "../bashtypes.h"

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

#include "../bashansi.h"

#include "../shell.h"
#include "../builtins.h"
#include "../flags.h"
#include "../findcmd.h"
#include "../hashcmd.h"
#include "common.h"
#include "bashgetopt.h"

#if defined (__MSDOS__)
#include "dosutil.h"
#endif

extern int dot_found_in_search;
extern char *this_command_name;

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

/* 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 *w, *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 ();

#if defined (RESTRICTED_SHELL)
  if (restricted && pathname && strchr (pathname, '/'))
    {
      builtin_error ("%s: restricted", pathname);
      return (EXECUTION_FAILURE);
    }
#endif

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

  fflush (stdout);
  return (opt);
}

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

  rv = 0;
  if (find_function (w) == 0 && find_shell_builtin (w) == 0)
    {
      full_path = find_user_command (w);
      if (full_path && executable_file (full_path))
	remember_filename (w, full_path, dot_found_in_search, 0);
      else
	{
	  if (quiet == 0)
	    builtin_error ("%s: not found", w);
	  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;

  if (hashed_filenames == 0)
    return (0);

  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)
#if defined (__MSDOS__)
	{
	  char *fixp;

	  fixp = dosutil_make_import_path (pathdata(item_list)->path);
	  printf ("%4d\t%s\n", item_list->times_found, fixp);
	  free (fixp);
	}
#else
	printf ("%4d\t%s\n", item_list->times_found, pathdata(item_list)->path);
#endif
    }
  return (any_printed);
}
