i-bash/builtins/hash.def

249 lines
5.9 KiB
Modula-2
Raw Normal View History

1996-08-26 18:22:31 +00:00
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
2000-03-17 21:46:59 +00:00
Software Foundation; either version 2, or (at your option) any later
1996-08-26 18:22:31 +00:00
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
2000-03-17 21:46:59 +00:00
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
1996-08-26 18:22:31 +00:00
$PRODUCES hash.c
$BUILTIN hash
$FUNCTION hash_builtin
2001-11-13 17:56:06 +00:00
$SHORT_DOC hash [-r] [-p pathname] [-t] [name ...]
1996-08-26 18:22:31 +00:00
For each NAME, the full pathname of the command is determined and
1996-12-23 17:02:34 +00:00
remembered. If the -p option is supplied, PATHNAME is used as the
full pathname of NAME, and no path search is performed. The -r
2001-11-13 17:56:06 +00:00
option causes the shell to forget all remembered locations.
If the -t option is supplied the full pathname to which each NAME
corresponds is printed. If multiple NAME arguments are supplied with
-t, the NAME is printed before the hashed full pathname. If no arguments
are given, information about remembered commands is displayed.
1996-08-26 18:22:31 +00:00
$END
1996-12-23 17:02:34 +00:00
#include <config.h>
1996-08-26 18:22:31 +00:00
#include <stdio.h>
1998-04-17 19:52:44 +00:00
#include "../bashtypes.h"
1996-12-23 17:02:34 +00:00
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
2001-04-06 19:14:31 +00:00
#include <errno.h>
1996-12-23 17:02:34 +00:00
#include "../bashansi.h"
1996-08-26 18:22:31 +00:00
#include "../shell.h"
#include "../builtins.h"
1997-06-05 14:59:13 +00:00
#include "../flags.h"
1998-04-17 19:52:44 +00:00
#include "../findcmd.h"
1997-06-05 14:59:13 +00:00
#include "../hashcmd.h"
1996-08-26 18:22:31 +00:00
#include "common.h"
1996-12-23 17:02:34 +00:00
#include "bashgetopt.h"
1996-08-26 18:22:31 +00:00
extern int dot_found_in_search;
1996-12-23 17:02:34 +00:00
extern char *this_command_name;
2001-11-13 17:56:06 +00:00
static int add_hashed_command __P((char *, int));
static int print_hashed_commands __P((void));
static int list_hashed_filename_targets __P((WORD_LIST *));
1996-12-23 17:02:34 +00:00
1996-08-26 18:22:31 +00:00
/* 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. */
1996-12-23 17:02:34 +00:00
int
1996-08-26 18:22:31 +00:00
hash_builtin (list)
WORD_LIST *list;
{
2001-11-13 17:56:06 +00:00
int expunge_hash_table, list_targets, opt;
1998-04-17 19:52:44 +00:00
char *w, *pathname;
1996-08-26 18:22:31 +00:00
1996-12-23 17:02:34 +00:00
if (hashing_enabled == 0)
1996-08-26 18:22:31 +00:00
{
builtin_error ("hashing disabled");
return (EXECUTION_FAILURE);
}
2001-11-13 17:56:06 +00:00
expunge_hash_table = list_targets = 0;
1996-12-23 17:02:34 +00:00
pathname = (char *)NULL;
reset_internal_getopt ();
2001-11-13 17:56:06 +00:00
while ((opt = internal_getopt (list, "rp:t")) != -1)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
switch (opt)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
case 'r':
1996-08-26 18:22:31 +00:00
expunge_hash_table = 1;
break;
1996-12-23 17:02:34 +00:00
case 'p':
pathname = list_optarg;
break;
2001-11-13 17:56:06 +00:00
case 't':
list_targets = 1;
break;
1996-12-23 17:02:34 +00:00
default:
builtin_usage ();
1996-08-26 18:22:31 +00:00
return (EX_USAGE);
}
}
1996-12-23 17:02:34 +00:00
list = loptend;
1996-08-26 18:22:31 +00:00
2001-11-13 17:56:06 +00:00
/* hash -t requires at least one argument. */
if (list == 0 && list_targets)
{
builtin_error("-t: argument required");
return (EXECUTION_FAILURE);
}
1996-08-26 18:22:31 +00:00
/* We want hash -r to be silent, but hash -- to print hashing info. That
1996-12-23 17:02:34 +00:00
is the reason for the test of expunge_hash_table. */
if (list == 0 && expunge_hash_table == 0)
1996-08-26 18:22:31 +00:00
{
1996-12-23 17:02:34 +00:00
if (print_hashed_commands () == 0)
printf ("%s: hash table empty\n", this_command_name);
1996-08-26 18:22:31 +00:00
return (EXECUTION_SUCCESS);
}
if (expunge_hash_table)
1996-12-23 17:02:34 +00:00
flush_hashed_filenames ();
1996-08-26 18:22:31 +00:00
2001-11-13 17:56:06 +00:00
/* If someone runs `hash -r -t xyz' he will be disappointed. */
if (list_targets)
{
return (list_hashed_filename_targets (list));
}
2000-03-17 21:46:59 +00:00
#if defined (RESTRICTED_SHELL)
if (restricted && pathname && strchr (pathname, '/'))
{
builtin_error ("%s: restricted", pathname);
return (EXECUTION_FAILURE);
}
#endif
1996-12-23 17:02:34 +00:00
for (opt = EXECUTION_SUCCESS; list; list = list->next)
1996-08-26 18:22:31 +00:00
{
/* Add or rehash the specified commands. */
1998-04-17 19:52:44 +00:00
w = list->word->word;
1996-12-23 17:02:34 +00:00
if (pathname)
2001-04-06 19:14:31 +00:00
{
if (is_directory (pathname))
{
#ifdef EISDIR
builtin_error ("%s: %s", pathname, strerror (EISDIR));
#else
builtin_error ("%s: is a directory", pathname);
#endif
opt = EXECUTION_FAILURE;
}
else
remember_filename (w, pathname, 0, 0);
}
1998-04-17 19:52:44 +00:00
else if (absolute_program (w))
1997-06-05 14:59:13 +00:00
continue;
1998-04-17 19:52:44 +00:00
else if (add_hashed_command (w, 0))
1997-06-05 14:59:13 +00:00
opt = EXECUTION_FAILURE;
1996-08-26 18:22:31 +00:00
}
fflush (stdout);
1996-12-23 17:02:34 +00:00
return (opt);
1996-08-26 18:22:31 +00:00
}
1996-12-23 17:02:34 +00:00
static int
1998-04-17 19:52:44 +00:00
add_hashed_command (w, quiet)
char *w;
1996-12-23 17:02:34 +00:00
int quiet;
{
int rv;
char *full_path;
rv = 0;
1998-04-17 19:52:44 +00:00
if (find_function (w) == 0 && find_shell_builtin (w) == 0)
1996-12-23 17:02:34 +00:00
{
1998-04-17 19:52:44 +00:00
full_path = find_user_command (w);
1996-12-23 17:02:34 +00:00
if (full_path && executable_file (full_path))
1998-04-17 19:52:44 +00:00
remember_filename (w, full_path, dot_found_in_search, 0);
1996-12-23 17:02:34 +00:00
else
{
if (quiet == 0)
1998-04-17 19:52:44 +00:00
builtin_error ("%s: not found", w);
1996-12-23 17:02:34 +00:00
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;
1997-06-05 14:59:13 +00:00
if (hashed_filenames == 0)
return (0);
1996-12-23 17:02:34 +00:00
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);
}
2001-11-13 17:56:06 +00:00
static int
list_hashed_filename_targets (list)
WORD_LIST *list;
{
int all_found, multiple;
char *target;
WORD_LIST *l;
all_found = 1;
multiple = list->next != 0;
for (l = list; l; l = l->next)
{
target = find_hashed_filename (l->word->word);
if (target == 0)
{
all_found = 0;
builtin_error ("%s: not found", l->word->word);
continue;
}
if (multiple)
printf ("%s\t", l->word->word);
printf ("%s\n", target);
}
return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}