#line 23 "type.def"
#line 49 "type.def"
#include <config.h>
#include "../bashtypes.h"
#include "posixstat.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <stdio.h>
#include "../bashansi.h"
#include "../bashintl.h"
#include "../shell.h"
#include "../findcmd.h"
#include "../hashcmd.h"
#if defined (ALIAS)
#include "../alias.h"
#endif
#include "common.h"
#include "bashgetopt.h"
extern int find_reserved_word __P((char *));
extern char *this_command_name;
extern int expand_aliases, posixly_correct;
int
type_builtin (list)
WORD_LIST *list;
{
int dflags, successful_finds, opt;
WORD_LIST *this;
if (list == 0)
return (EXECUTION_SUCCESS);
dflags = CDESC_SHORTDESC;
successful_finds = 0;
for (this = list; this && this->word->word[0] == '-'; this = this->next)
{
char *flag = &(this->word->word[1]);
if (STREQ (flag, "type") || STREQ (flag, "-type"))
{
this->word->word[1] = 't';
this->word->word[2] = '\0';
}
else if (STREQ (flag, "path") || STREQ (flag, "-path"))
{
this->word->word[1] = 'p';
this->word->word[2] = '\0';
}
else if (STREQ (flag, "all") || STREQ (flag, "-all"))
{
this->word->word[1] = 'a';
this->word->word[2] = '\0';
}
}
reset_internal_getopt ();
while ((opt = internal_getopt (list, "afptP")) != -1)
{
switch (opt)
{
case 'a':
dflags |= CDESC_ALL;
break;
case 'f':
dflags |= CDESC_NOFUNCS;
break;
case 'p':
dflags |= CDESC_PATH_ONLY;
dflags &= ~(CDESC_TYPE|CDESC_SHORTDESC);
break;
case 't':
dflags |= CDESC_TYPE;
dflags &= ~(CDESC_PATH_ONLY|CDESC_SHORTDESC);
break;
case 'P':
dflags |= (CDESC_PATH_ONLY|CDESC_FORCE_PATH);
dflags &= ~(CDESC_TYPE|CDESC_SHORTDESC);
break;
default:
builtin_usage ();
return (EX_USAGE);
}
}
list = loptend;
while (list)
{
int found;
found = describe_command (list->word->word, dflags);
if (!found && (dflags & (CDESC_PATH_ONLY|CDESC_TYPE)) == 0)
sh_notfound (list->word->word);
successful_finds += found;
list = list->next;
}
fflush (stdout);
return ((successful_finds != 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}
int
describe_command (command, dflags)
char *command;
int dflags;
{
int found, i, found_file, f, all;
char *full_path, *x;
SHELL_VAR *func;
#if defined (ALIAS)
alias_t *alias;
#endif
all = (dflags & CDESC_ALL) != 0;
found = found_file = 0;
full_path = (char *)NULL;
#if defined (ALIAS)
if (((dflags & CDESC_FORCE_PATH) == 0) && expand_aliases && (alias = find_alias (command)))
{
if (dflags & CDESC_TYPE)
puts ("alias");
else if (dflags & CDESC_SHORTDESC)
printf (_("%s is aliased to `%s'\n"), command, alias->value);
else if (dflags & CDESC_REUSABLE)
{
x = sh_single_quote (alias->value);
printf ("alias %s=%s\n", command, x);
free (x);
}
found = 1;
if (all == 0)
return (1);
}
#endif
if (((dflags & CDESC_FORCE_PATH) == 0) && (i = find_reserved_word (command)) >= 0)
{
if (dflags & CDESC_TYPE)
puts ("keyword");
else if (dflags & CDESC_SHORTDESC)
printf (_("%s is a shell keyword\n"), command);
else if (dflags & CDESC_REUSABLE)
printf ("%s\n", command);
found = 1;
if (all == 0)
return (1);
}
if (((dflags & (CDESC_FORCE_PATH|CDESC_NOFUNCS)) == 0) && (func = find_function (command)))
{
if (dflags & CDESC_TYPE)
puts ("function");
else if (dflags & CDESC_SHORTDESC)
{
#define PRETTY_PRINT_FUNC 1
char *result;
printf (_("%s is a function\n"), command);
result = named_function_string (command,
(COMMAND *) function_cell (func),
PRETTY_PRINT_FUNC);
printf ("%s\n", result);
#undef PRETTY_PRINT_FUNC
}
else if (dflags & CDESC_REUSABLE)
printf ("%s\n", command);
found = 1;
if (all == 0)
return (1);
}
if (((dflags & CDESC_FORCE_PATH) == 0) && find_shell_builtin (command))
{
if (dflags & CDESC_TYPE)
puts ("builtin");
else if (dflags & CDESC_SHORTDESC)
printf (_("%s is a shell builtin\n"), command);
else if (dflags & CDESC_REUSABLE)
printf ("%s\n", command);
found = 1;
if (all == 0)
return (1);
}
if (absolute_program (command))
{
f = file_status (command);
if (f & FS_EXECABLE)
{
if (dflags & CDESC_TYPE)
puts ("file");
else if (dflags & CDESC_SHORTDESC)
printf (_("%s is %s\n"), command, command);
else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
printf ("%s\n", command);
return (1);
}
}
if (all == 0 || (dflags & CDESC_FORCE_PATH))
{
if (full_path = phash_search (command))
{
if (dflags & CDESC_TYPE)
puts ("file");
else if (dflags & CDESC_SHORTDESC)
printf (_("%s is hashed (%s)\n"), command, full_path);
else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
printf ("%s\n", full_path);
free (full_path);
return (1);
}
}
while (1)
{
if (all == 0)
full_path = find_user_command (command);
else
full_path =
user_command_matches (command, FS_EXEC_ONLY, found_file);
if (!full_path)
break;
if (STREQ (full_path, command) || posixly_correct)
{
f = file_status (full_path);
if ((f & FS_EXECABLE) == 0)
{
free (full_path);
full_path = (char *)NULL;
if (all == 0)
break;
}
else if (ABSPATH (full_path))
;
else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY|CDESC_SHORTDESC))
{
f = MP_DOCWD | ((dflags & CDESC_ABSPATH) ? MP_RMDOT : 0);
full_path = sh_makepath ((char *)NULL, full_path, f);
}
}
else if ((dflags & CDESC_ABSPATH) && ABSPATH (full_path) == 0)
full_path = sh_makepath ((char *)NULL, full_path, MP_DOCWD|MP_RMDOT);
found_file++;
found = 1;
if (dflags & CDESC_TYPE)
puts ("file");
else if (dflags & CDESC_SHORTDESC)
printf ("%s is %s\n", command, full_path);
else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
printf ("%s\n", full_path);
free (full_path);
full_path = (char *)NULL;
if (all == 0)
break;
}
return (found);
}