1 This file is type.def, from which is created type.c.
2 It implements the builtin "type" in Bash.
4 Copyright (C) 1987-2002 Free Software Foundation, Inc.
6 This file is part of GNU Bash, the Bourne Again SHell.
8 Bash is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License along
19 with Bash; see the file COPYING. If not, write to the Free Software
20 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.
25 $FUNCTION type_builtin
26 $SHORT_DOC type [-afptP] name [name ...]
27 For each NAME, indicate how it would be interpreted if used as a
30 If the -t option is used, `type' outputs a single word which is one of
31 `alias', `keyword', `function', `builtin', `file' or `', if NAME is an
32 alias, shell reserved word, shell function, shell builtin, disk file,
33 or unfound, respectively.
35 If the -p flag is used, `type' either returns the name of the disk
36 file that would be executed, or nothing if `type -t NAME' would not
39 If the -a flag is used, `type' displays all of the places that contain
40 an executable named `file'. This includes aliases, builtins, and
41 functions, if and only if the -p flag is not also used.
43 The -f flag suppresses shell function lookup.
45 The -P flag forces a PATH search for each NAME, even if it is an alias,
46 builtin, or function, and returns the name of the disk file that would
52 #include "../bashtypes.h"
53 #include "posixstat.h"
55 #if defined (HAVE_UNISTD_H)
60 #include "../bashansi.h"
63 #include "../findcmd.h"
64 #include "../hashcmd.h"
71 #include "bashgetopt.h"
73 extern int find_reserved_word __P((char *));
75 extern char *this_command_name;
77 /* For each word in LIST, find out what the shell is going to do with
78 it as a simple command. i.e., which file would this shell use to
79 execve, or if it is a builtin command, or an alias. Possible flag
81 -t Returns the "type" of the object, one of
82 `alias', `keyword', `function', `builtin',
85 -p Returns the pathname of the file if -type is
88 -a Returns all occurrences of words, whether they
89 be a filename in the path, alias, function,
92 -f Suppress shell function lookup, like `command'.
94 -P Force a path search even in the presence of other
109 int dflags, successful_finds, opt;
113 return (EXECUTION_SUCCESS);
115 dflags = CDESC_SHORTDESC; /* default */
116 successful_finds = 0;
118 /* Handle the obsolescent `-type', `-path', and `-all' by prescanning
119 the arguments and converting those options to the form that
120 internal_getopt recognizes. Converts `--type', `--path', and `--all'
121 also. THIS SHOULD REALLY GO AWAY. */
122 for (this = list; this && this->word->word[0] == '-'; this = this->next)
124 char *flag = &(this->word->word[1]);
126 if (STREQ (flag, "type") || STREQ (flag, "-type"))
128 this->word->word[1] = 't';
129 this->word->word[2] = '\0';
131 else if (STREQ (flag, "path") || STREQ (flag, "-path"))
133 this->word->word[1] = 'p';
134 this->word->word[2] = '\0';
136 else if (STREQ (flag, "all") || STREQ (flag, "-all"))
138 this->word->word[1] = 'a';
139 this->word->word[2] = '\0';
143 reset_internal_getopt ();
144 while ((opt = internal_getopt (list, "afptP")) != -1)
152 dflags |= CDESC_NOFUNCS;
155 dflags |= CDESC_PATH_ONLY;
156 dflags &= ~(CDESC_TYPE|CDESC_SHORTDESC);
159 dflags |= CDESC_TYPE;
160 dflags &= ~(CDESC_PATH_ONLY|CDESC_SHORTDESC);
162 case 'P': /* shorthand for type -ap */
163 dflags |= (CDESC_PATH_ONLY|CDESC_FORCE_PATH);
164 dflags &= ~(CDESC_TYPE|CDESC_SHORTDESC);
177 found = describe_command (list->word->word, dflags);
179 if (!found && (dflags & (CDESC_PATH_ONLY|CDESC_TYPE)) == 0)
180 sh_notfound (list->word->word);
182 successful_finds += found;
188 return ((successful_finds != 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
192 * Describe COMMAND as required by the type and command builtins.
194 * Behavior is controlled by DFLAGS. Flag values are
195 * CDESC_ALL print all descriptions of a command
196 * CDESC_SHORTDESC print the description for type and command -V
197 * CDESC_REUSABLE print in a format that may be reused as input
198 * CDESC_TYPE print the type for type -t
199 * CDESC_PATH_ONLY print the path for type -p
200 * CDESC_FORCE_PATH force a path search for type -P
201 * CDESC_NOFUNCS skip function lookup for type -f
203 * CDESC_ALL says whether or not to look for all occurrences of COMMAND, or
204 * return after finding it once.
207 describe_command (command, dflags)
211 int found, i, found_file, f, all;
218 all = (dflags & CDESC_ALL) != 0;
219 found = found_file = 0;
220 full_path = (char *)NULL;
223 /* Command is an alias? */
224 if (((dflags & CDESC_FORCE_PATH) == 0) && (alias = find_alias (command)))
226 if (dflags & CDESC_TYPE)
228 else if (dflags & CDESC_SHORTDESC)
229 printf ("%s is aliased to `%s'\n", command, alias->value);
230 else if (dflags & CDESC_REUSABLE)
232 x = sh_single_quote (alias->value);
233 printf ("alias %s=%s\n", command, x);
244 /* Command is a shell reserved word? */
245 if (((dflags & CDESC_FORCE_PATH) == 0) && (i = find_reserved_word (command)) >= 0)
247 if (dflags & CDESC_TYPE)
249 else if (dflags & CDESC_SHORTDESC)
250 printf ("%s is a shell keyword\n", command);
251 else if (dflags & CDESC_REUSABLE)
252 printf ("%s\n", command);
260 /* Command is a function? */
261 if (((dflags & (CDESC_FORCE_PATH|CDESC_NOFUNCS)) == 0) && (func = find_function (command)))
263 if (dflags & CDESC_TYPE)
265 else if (dflags & CDESC_SHORTDESC)
267 #define PRETTY_PRINT_FUNC 1
270 printf ("%s is a function\n", command);
272 /* We're blowing away THE_PRINTED_COMMAND here... */
274 result = named_function_string (command,
275 (COMMAND *) function_cell (func),
277 printf ("%s\n", result);
278 #undef PRETTY_PRINT_FUNC
280 else if (dflags & CDESC_REUSABLE)
281 printf ("%s\n", command);
289 /* Command is a builtin? */
290 if (((dflags & CDESC_FORCE_PATH) == 0) && find_shell_builtin (command))
292 if (dflags & CDESC_TYPE)
294 else if (dflags & CDESC_SHORTDESC)
295 printf ("%s is a shell builtin\n", command);
296 else if (dflags & CDESC_REUSABLE)
297 printf ("%s\n", command);
305 /* Command is a disk file? */
306 /* If the command name given is already an absolute command, just
307 check to see if it is executable. */
308 if (absolute_program (command))
310 f = file_status (command);
313 if (dflags & CDESC_TYPE)
315 else if (dflags & CDESC_SHORTDESC)
316 printf ("%s is %s\n", command, command);
317 else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
318 printf ("%s\n", command);
320 /* There's no use looking in the hash table or in $PATH,
321 because they're not consulted when an absolute program
327 /* If the user isn't doing "-a", then we might care about
328 whether the file is present in our hash table. */
329 if (all == 0 || (dflags & CDESC_FORCE_PATH))
331 if (full_path = phash_search (command))
333 if (dflags & CDESC_TYPE)
335 else if (dflags & CDESC_SHORTDESC)
336 printf ("%s is hashed (%s)\n", command, full_path);
337 else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
338 printf ("%s\n", full_path);
345 /* Now search through $PATH. */
349 full_path = find_user_command (command);
352 user_command_matches (command, FS_EXEC_ONLY, found_file);
353 /* XXX - should that be FS_EXEC_PREFERRED? */
358 /* If we found the command as itself by looking through $PATH, it
359 probably doesn't exist. Check whether or not the command is an
360 executable file. If it's not, don't report a match. */
361 if (STREQ (full_path, command))
363 f = file_status (full_path);
364 if ((f & FS_EXECABLE) == 0)
367 full_path = (char *)NULL;
371 else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY|CDESC_SHORTDESC))
372 full_path = sh_makepath ((char *)NULL, full_path, MP_DOCWD);
378 if (dflags & CDESC_TYPE)
380 else if (dflags & CDESC_SHORTDESC)
381 printf ("%s is %s\n", command, full_path);
382 else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
383 printf ("%s\n", full_path);
386 full_path = (char *)NULL;