Imported from ../bash-3.0.tar.gz.
[platform/upstream/bash.git] / builtins / type.def
1 This file is type.def, from which is created type.c.
2 It implements the builtin "type" in Bash.
3
4 Copyright (C) 1987-2002 Free Software Foundation, Inc.
5
6 This file is part of GNU Bash, the Bourne Again SHell.
7
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
11 version.
12
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
16 for more details.
17
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.
21
22 $PRODUCES type.c
23
24 $BUILTIN type
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
28 command name.
29
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.
34
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
37 return `file'.
38
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.
42
43 The -f flag suppresses shell function lookup.
44
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
47 be executed.
48 $END
49
50 #include <config.h>
51
52 #include "../bashtypes.h"
53 #include "posixstat.h"
54
55 #if defined (HAVE_UNISTD_H)
56 #  include <unistd.h>
57 #endif
58
59 #include <stdio.h>
60 #include "../bashansi.h"
61 #include "../bashintl.h"
62
63 #include "../shell.h"
64 #include "../findcmd.h"
65 #include "../hashcmd.h"
66
67 #if defined (ALIAS)
68 #include "../alias.h"
69 #endif /* ALIAS */
70
71 #include "common.h"
72 #include "bashgetopt.h"
73
74 extern int find_reserved_word __P((char *));
75
76 extern char *this_command_name;
77 extern int expand_aliases;
78
79 /* For each word in LIST, find out what the shell is going to do with
80    it as a simple command. i.e., which file would this shell use to
81    execve, or if it is a builtin command, or an alias.  Possible flag
82    arguments:
83         -t              Returns the "type" of the object, one of
84                         `alias', `keyword', `function', `builtin',
85                         or `file'.
86
87         -p              Returns the pathname of the file if -type is
88                         a file.
89
90         -a              Returns all occurrences of words, whether they
91                         be a filename in the path, alias, function,
92                         or builtin.
93
94         -f              Suppress shell function lookup, like `command'.
95
96         -P              Force a path search even in the presence of other
97                         definitions.
98
99    Order of evaluation:
100         alias
101         keyword
102         function
103         builtin
104         file
105  */
106
107 int
108 type_builtin (list)
109      WORD_LIST *list;
110 {
111   int dflags, successful_finds, opt;
112   WORD_LIST *this;
113
114   if (list == 0)
115     return (EXECUTION_SUCCESS);
116
117   dflags = CDESC_SHORTDESC;     /* default */
118   successful_finds = 0;
119
120   /* Handle the obsolescent `-type', `-path', and `-all' by prescanning
121      the arguments and converting those options to the form that
122      internal_getopt recognizes. Converts `--type', `--path', and `--all'
123      also. THIS SHOULD REALLY GO AWAY. */
124   for (this = list; this && this->word->word[0] == '-'; this = this->next)
125     {
126       char *flag = &(this->word->word[1]);
127
128       if (STREQ (flag, "type") || STREQ (flag, "-type"))
129         {
130           this->word->word[1] = 't';
131           this->word->word[2] = '\0';
132         }
133       else if (STREQ (flag, "path") || STREQ (flag, "-path"))
134         {
135           this->word->word[1] = 'p';
136           this->word->word[2] = '\0';
137         }
138       else if (STREQ (flag, "all") || STREQ (flag, "-all"))
139         {
140           this->word->word[1] = 'a';
141           this->word->word[2] = '\0';
142         }
143     }
144
145   reset_internal_getopt ();
146   while ((opt = internal_getopt (list, "afptP")) != -1)
147     {
148       switch (opt)
149         {
150         case 'a':
151           dflags |= CDESC_ALL;
152           break;
153         case 'f':
154           dflags |= CDESC_NOFUNCS;
155           break;
156         case 'p':
157           dflags |= CDESC_PATH_ONLY;
158           dflags &= ~(CDESC_TYPE|CDESC_SHORTDESC);
159           break;
160         case 't':
161           dflags |= CDESC_TYPE;
162           dflags &= ~(CDESC_PATH_ONLY|CDESC_SHORTDESC);
163           break;
164         case 'P':       /* shorthand for type -ap */
165           dflags |= (CDESC_PATH_ONLY|CDESC_FORCE_PATH);
166           dflags &= ~(CDESC_TYPE|CDESC_SHORTDESC);
167           break;
168         default:
169           builtin_usage ();
170           return (EX_USAGE);
171         }
172     }
173   list = loptend;
174
175   while (list)
176     {
177       int found;
178
179       found = describe_command (list->word->word, dflags);
180
181       if (!found && (dflags & (CDESC_PATH_ONLY|CDESC_TYPE)) == 0)
182         sh_notfound (list->word->word);
183
184       successful_finds += found;
185       list = list->next;
186     }
187
188   fflush (stdout);
189
190   return ((successful_finds != 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
191 }
192
193 /*
194  * Describe COMMAND as required by the type and command builtins.
195  *
196  * Behavior is controlled by DFLAGS.  Flag values are
197  *      CDESC_ALL       print all descriptions of a command
198  *      CDESC_SHORTDESC print the description for type and command -V
199  *      CDESC_REUSABLE  print in a format that may be reused as input
200  *      CDESC_TYPE      print the type for type -t
201  *      CDESC_PATH_ONLY print the path for type -p
202  *      CDESC_FORCE_PATH        force a path search for type -P
203  *      CDESC_NOFUNCS   skip function lookup for type -f
204  *
205  * CDESC_ALL says whether or not to look for all occurrences of COMMAND, or
206  * return after finding it once.
207  */
208 int
209 describe_command (command, dflags)
210      char *command;
211      int dflags;
212 {
213   int found, i, found_file, f, all;
214   char *full_path, *x;
215   SHELL_VAR *func;
216 #if defined (ALIAS)
217   alias_t *alias;
218 #endif
219
220   all = (dflags & CDESC_ALL) != 0;
221   found = found_file = 0;
222   full_path = (char *)NULL;
223
224 #if defined (ALIAS)
225   /* Command is an alias? */
226   if (((dflags & CDESC_FORCE_PATH) == 0) && expand_aliases && (alias = find_alias (command)))
227     {
228       if (dflags & CDESC_TYPE)
229         puts ("alias");
230       else if (dflags & CDESC_SHORTDESC)
231         printf (_("%s is aliased to `%s'\n"), command, alias->value);
232       else if (dflags & CDESC_REUSABLE)
233         {
234           x = sh_single_quote (alias->value);
235           printf ("alias %s=%s\n", command, x);
236           free (x);
237         }
238
239       found = 1;
240
241       if (all == 0)
242         return (1);
243     }
244 #endif /* ALIAS */
245
246   /* Command is a shell reserved word? */
247   if (((dflags & CDESC_FORCE_PATH) == 0) && (i = find_reserved_word (command)) >= 0)
248     {
249       if (dflags & CDESC_TYPE)
250         puts ("keyword");
251       else if (dflags & CDESC_SHORTDESC)
252         printf (_("%s is a shell keyword\n"), command);
253       else if (dflags & CDESC_REUSABLE)
254         printf ("%s\n", command);
255
256       found = 1;
257
258       if (all == 0)
259         return (1);
260     }
261
262   /* Command is a function? */
263   if (((dflags & (CDESC_FORCE_PATH|CDESC_NOFUNCS)) == 0) && (func = find_function (command)))
264     {
265       if (dflags & CDESC_TYPE)
266         puts ("function");
267       else if (dflags & CDESC_SHORTDESC)
268         {
269 #define PRETTY_PRINT_FUNC 1
270           char *result;
271
272           printf (_("%s is a function\n"), command);
273
274           /* We're blowing away THE_PRINTED_COMMAND here... */
275
276           result = named_function_string (command,
277                                           (COMMAND *) function_cell (func),
278                                           PRETTY_PRINT_FUNC);
279           printf ("%s\n", result);
280 #undef PRETTY_PRINT_FUNC
281         }
282       else if (dflags & CDESC_REUSABLE)
283         printf ("%s\n", command);
284
285       found = 1;
286
287       if (all == 0)
288         return (1);
289     }
290
291   /* Command is a builtin? */
292   if (((dflags & CDESC_FORCE_PATH) == 0) && find_shell_builtin (command))
293     {
294       if (dflags & CDESC_TYPE)
295         puts ("builtin");
296       else if (dflags & CDESC_SHORTDESC)
297         printf (_("%s is a shell builtin\n"), command);
298       else if (dflags & CDESC_REUSABLE)
299         printf ("%s\n", command);
300
301       found = 1;
302
303       if (all == 0)
304         return (1);
305     }
306
307   /* Command is a disk file? */
308   /* If the command name given is already an absolute command, just
309      check to see if it is executable. */
310   if (absolute_program (command))
311     {
312       f = file_status (command);
313       if (f & FS_EXECABLE)
314         {
315           if (dflags & CDESC_TYPE)
316             puts ("file");
317           else if (dflags & CDESC_SHORTDESC)
318             printf (_("%s is %s\n"), command, command);
319           else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
320             printf ("%s\n", command);
321
322           /* There's no use looking in the hash table or in $PATH,
323              because they're not consulted when an absolute program
324              name is supplied. */
325           return (1);
326         }
327     }
328
329   /* If the user isn't doing "-a", then we might care about
330      whether the file is present in our hash table. */
331   if (all == 0 || (dflags & CDESC_FORCE_PATH))
332     {
333       if (full_path = phash_search (command))
334         {
335           if (dflags & CDESC_TYPE)
336             puts ("file");
337           else if (dflags & CDESC_SHORTDESC)
338             printf (_("%s is hashed (%s)\n"), command, full_path);
339           else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
340             printf ("%s\n", full_path);
341
342           free (full_path);
343           return (1);
344         }
345     }
346
347   /* Now search through $PATH. */
348   while (1)
349     {
350       if (all == 0)
351         full_path = find_user_command (command);
352       else
353         full_path =
354           user_command_matches (command, FS_EXEC_ONLY, found_file);
355           /* XXX - should that be FS_EXEC_PREFERRED? */
356
357       if (!full_path)
358         break;
359
360       /* If we found the command as itself by looking through $PATH, it
361          probably doesn't exist.  Check whether or not the command is an
362          executable file.  If it's not, don't report a match. */
363       if (STREQ (full_path, command))
364         {
365           f = file_status (full_path);
366           if ((f & FS_EXECABLE) == 0)
367             {
368               free (full_path);
369               full_path = (char *)NULL;
370               if (all == 0)
371                 break;
372             }
373           else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY|CDESC_SHORTDESC))
374             full_path = sh_makepath ((char *)NULL, full_path, MP_DOCWD);
375         }
376
377       found_file++;
378       found = 1;
379
380       if (dflags & CDESC_TYPE)
381         puts ("file");
382       else if (dflags & CDESC_SHORTDESC)
383         printf ("%s is %s\n", command, full_path);
384       else if (dflags & (CDESC_REUSABLE|CDESC_PATH_ONLY))
385         printf ("%s\n", full_path);
386
387       free (full_path);
388       full_path = (char *)NULL;
389
390       if (all == 0)
391         break;
392     }
393
394   return (found);
395 }