i-bash/builtins/hash.def

268 lines
6.4 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.
2002-07-17 14:10:11 +00:00
Copyright (C) 1987-2002 Free Software Foundation, Inc.
1996-08-26 18:22:31 +00:00
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
2002-07-17 14:10:11 +00:00
$SHORT_DOC hash [-lr] [-p pathname] [-dt] [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
2002-07-17 14:10:11 +00:00
option causes the shell to forget all remembered locations. The -d
option causes the shell to forget the remembered location of each NAME.
2001-11-13 17:56:06 +00:00
If the -t option is supplied the full pathname to which each NAME
corresponds is printed. If multiple NAME arguments are supplied with
2002-07-17 14:10:11 +00:00
-t, the NAME is printed before the hashed full pathname. The -l option
causes output to be displayed in a format that may be reused as input.
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));
2002-07-17 14:10:11 +00:00
static int print_hash_info __P((BUCKET_CONTENTS *));
static int print_portable_hash_info __P((BUCKET_CONTENTS *));
static int print_hashed_commands __P((int));
static int list_hashed_filename_targets __P((WORD_LIST *, int));
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;
{
2002-07-17 14:10:11 +00:00
int expunge_hash_table, list_targets, list_portably, delete, 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);
}
2002-07-17 14:10:11 +00:00
expunge_hash_table = list_targets = list_portably = delete = 0;
1996-12-23 17:02:34 +00:00
pathname = (char *)NULL;
reset_internal_getopt ();
2002-07-17 14:10:11 +00:00
while ((opt = internal_getopt (list, "dlp:rt")) != -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
{
2002-07-17 14:10:11 +00:00
case 'd':
delete = 1;
break;
case 'l':
list_portably = 1;
1996-08-26 18:22:31 +00:00
break;
1996-12-23 17:02:34 +00:00
case 'p':
pathname = list_optarg;
break;
2002-07-17 14:10:11 +00:00
case 'r':
expunge_hash_table = 1;
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)
{
2002-07-17 14:10:11 +00:00
sh_needarg ("-t");
2001-11-13 17:56:06 +00:00
return (EXECUTION_FAILURE);
}
2002-07-17 14:10:11 +00:00
/* We want hash -r to be silent, but hash -- to print hashing info, so
we test expunge_hash_table. */
1996-12-23 17:02:34 +00:00
if (list == 0 && expunge_hash_table == 0)
1996-08-26 18:22:31 +00:00
{
2002-07-17 14:10:11 +00:00
if (print_hashed_commands (list_portably) == 0)
1996-12-23 17:02:34 +00:00
printf ("%s: hash table empty\n", this_command_name);
1996-08-26 18:22:31 +00:00
return (EXECUTION_SUCCESS);
}
if (expunge_hash_table)
2002-07-17 14:10:11 +00:00
phash_flush ();
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)
2002-07-17 14:10:11 +00:00
return (list_hashed_filename_targets (list, list_portably));
2001-11-13 17:56:06 +00:00
2000-03-17 21:46:59 +00:00
#if defined (RESTRICTED_SHELL)
if (restricted && pathname && strchr (pathname, '/'))
{
2002-07-17 14:10:11 +00:00
sh_restricted (pathname);
2000-03-17 21:46:59 +00:00
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
{
2002-07-17 14:10:11 +00:00
/* Add, remove 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
2002-07-17 14:10:11 +00:00
phash_insert (w, pathname, 0, 0);
2001-04-06 19:14:31 +00:00
}
1998-04-17 19:52:44 +00:00
else if (absolute_program (w))
1997-06-05 14:59:13 +00:00
continue;
2002-07-17 14:10:11 +00:00
else if (delete && phash_remove (w))
{
sh_notfound (w);
opt = EXECUTION_FAILURE;
}
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))
2002-07-17 14:10:11 +00:00
phash_insert (w, full_path, dot_found_in_search, 0);
1996-12-23 17:02:34 +00:00
else
{
if (quiet == 0)
2002-07-17 14:10:11 +00:00
sh_notfound (w);
1996-12-23 17:02:34 +00:00
rv++;
}
2002-07-17 14:10:11 +00:00
FREE (full_path);
1996-12-23 17:02:34 +00:00
}
return (rv);
}
/* Print information about current hashed info. */
static int
2002-07-17 14:10:11 +00:00
print_hash_info (item)
BUCKET_CONTENTS *item;
1996-12-23 17:02:34 +00:00
{
2002-07-17 14:10:11 +00:00
printf ("%4d\t%s\n", item->times_found, pathdata(item)->path);
return 0;
}
1996-12-23 17:02:34 +00:00
2002-07-17 14:10:11 +00:00
static int
print_portable_hash_info (item)
BUCKET_CONTENTS *item;
{
printf ("builtin hash -p %s %s\n", pathdata(item)->path, item->key);
return 0;
}
1996-12-23 17:02:34 +00:00
2002-07-17 14:10:11 +00:00
static int
print_hashed_commands (fmt)
int fmt;
{
if (hashed_filenames == 0 || HASH_ENTRIES (hashed_filenames) == 0)
return (0);
1996-12-23 17:02:34 +00:00
2002-07-17 14:10:11 +00:00
if (fmt == 0)
printf ("hits\tcommand\n");
hash_walk (hashed_filenames, fmt ? print_portable_hash_info : print_hash_info);
return (1);
1996-12-23 17:02:34 +00:00
}
2001-11-13 17:56:06 +00:00
static int
2002-07-17 14:10:11 +00:00
list_hashed_filename_targets (list, fmt)
2001-11-13 17:56:06 +00:00
WORD_LIST *list;
2002-07-17 14:10:11 +00:00
int fmt;
2001-11-13 17:56:06 +00:00
{
int all_found, multiple;
char *target;
WORD_LIST *l;
all_found = 1;
multiple = list->next != 0;
for (l = list; l; l = l->next)
{
2002-07-17 14:10:11 +00:00
target = phash_search (l->word->word);
2001-11-13 17:56:06 +00:00
if (target == 0)
{
all_found = 0;
2002-07-17 14:10:11 +00:00
sh_notfound (l->word->word);
2001-11-13 17:56:06 +00:00
continue;
}
2002-07-17 14:10:11 +00:00
if (fmt)
printf ("builtin hash -p %s %s\n", target, l->word->word);
else
{
if (multiple)
printf ("%s\t", l->word->word);
printf ("%s\n", target);
}
2001-11-13 17:56:06 +00:00
}
return (all_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}