summaryrefslogtreecommitdiff
path: root/builtins/type.def
blob: a60db0aacb589667529df3cc1b599d551a6d5ded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
This file is type.def, from which is created type.c.
It implements the builtin "type" in Bash.

Copyright (C) 1987, 1989, 1991, 1992 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
Software Foundation; either version 2, or (at your option) any later
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
Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA.

$PRODUCES type.c

$BUILTIN type
$FUNCTION type_builtin
$SHORT_DOC type [-apt] name [name ...]
For each NAME, indicate how it would be interpreted if used as a
command name.

If the -t option is used, `type' outputs a single word which is one of
`alias', `keyword', `function', `builtin', `file' or `', if NAME is an
alias, shell reserved word, shell function, shell builtin, disk file,
or unfound, respectively.

If the -p flag is used, `type' either returns the name of the disk
file that would be executed, or nothing if `type -t NAME' would not
return `file'.

If the -a flag is used, `type' displays all of the places that contain
an executable named `file'.  This includes aliases and functions, if
and only if the -p flag is not also used.
$END

#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 "../shell.h"
#include "../findcmd.h"
#include "../hashcmd.h"

#if defined (ALIAS)
#include "../alias.h"
#endif /* ALIAS */

#include "common.h"
#include "bashgetopt.h"

extern int find_reserved_word ();

extern char *this_command_name;

/* For each word in LIST, find out what the shell is going to do with
   it as a simple command. i.e., which file would this shell use to
   execve, or if it is a builtin command, or an alias.  Possible flag
   arguments:
	-t		Returns the "type" of the object, one of
			`alias', `keyword', `function', `builtin',
			or `file'.

	-p		Returns the pathname of the file if -type is
			a file.

	-a		Returns all occurrences of words, whether they
			be a filename in the path, alias, function,
			or builtin.
   Order of evaluation:
	alias
	keyword
	function
	builtin
	file
 */

int
type_builtin (list)
     WORD_LIST *list;
{
  int path_only, type_only, all, verbose;
  int successful_finds, opt;
  WORD_LIST *prev, *this;

  if (list == 0)
    return (EXECUTION_SUCCESS);

  path_only = type_only = all = 0;
  successful_finds = 0;

  /* Handle the obsolescent `-type', `-path', and `-all' by prescanning
     the arguments and removing those options from the list before calling
     internal_getopt.  Recognize `--type', `--path', and `--all' also.
     THIS SHOULD REALLY GO AWAY. */
  for (this = list; this && this->word->word[0] == '-'; )
    {
      char *flag = &(this->word->word[1]);

      if (STREQ (flag, "type") || STREQ (flag, "-type"))
	{
	  type_only = 1;
	  path_only = 0;
	}
      else if (STREQ (flag, "path") || STREQ (flag, "-path"))
	{
	  path_only = 1;
	  type_only = 0;
	}
      else if (STREQ (flag, "all") || STREQ (flag, "-all"))
	all = 1;
      else
	{
	  prev = this;
	  this = this->next;
	  continue;
	}

      /* We found a long option; remove it from the argument list.  Don't
	 free it if it's the head of the argument list, though -- the
	 argument list will be freed by the caller. */
      if (this == list)
	this = list = list->next;
      else
	{
	  prev->next = this->next;
	  this->next = (WORD_LIST *)NULL;
	  dispose_words (this);
	  this = prev->next;
	}
    }

  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "apt")) != -1)
    {
      switch (opt)
	{
	case 't':
	  type_only = 1;
	  path_only = 0;
	  break;
	case 'p':
	  path_only = 1;
	  type_only = 0;
	  break;
	case 'a':
	  all = 1;
	  break;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

  if (type_only)
    verbose = 1;
  else if (path_only == 0)
    verbose = 2;
  else if (path_only)
    verbose = 3;
  else
    verbose = 0;

  while (list)
    {
      int found;

      found = describe_command (list->word->word, verbose, all);

      if (!found && !path_only && !type_only)
	builtin_error ("%s: not found", list->word->word);

      successful_finds += found;
      list = list->next;
    }

  fflush (stdout);

  return ((successful_finds != 0) ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}

/*
 * Describe COMMAND as required by the type builtin.
 *
 * If VERBOSE == 0, don't print anything
 * If VERBOSE == 1, print short description as for `type -t'
 * If VERBOSE == 2, print long description as for `type' and `command -V'
 * If VERBOSE == 3, print path name only for disk files
 * If VERBOSE == 4, print string used to invoke COMMAND, for `command -v'
 *
 * ALL says whether or not to look for all occurrences of COMMAND, or
 * return after finding it once.
 */
int
describe_command (command, verbose, all)
     char *command;
     int verbose, all;
{
  int found, i, found_file, f;
  char *full_path, *x, *cwd;
  SHELL_VAR *func;
#if defined (ALIAS)
  alias_t *alias;
#endif

  found = found_file = 0;
  full_path = (char *)NULL;

#if defined (ALIAS)
  /* Command is an alias? */
  alias = find_alias (command);

  if (alias)
    {
      if (verbose == 1)
	puts ("alias");
      else if (verbose == 2)
	printf ("%s is aliased to `%s'\n", command, alias->value);
      else if (verbose == 4)
	{
	  x = sh_single_quote (alias->value);
	  printf ("alias %s=%s\n", command, x);
	  free (x);
	}

      found = 1;

      if (all == 0)
	return (1);
    }
#endif /* ALIAS */

  /* Command is a shell reserved word? */
  i = find_reserved_word (command);
  if (i >= 0)
    {
      if (verbose == 1)
	puts ("keyword");
      else if (verbose == 2)
	printf ("%s is a shell keyword\n", command);
      else if (verbose == 4)
	printf ("%s\n", command);

      found = 1;

      if (all == 0)
	return (1);
    }

  /* Command is a function? */
  func = find_function (command);

  if (func)
    {
      if (verbose == 1)
	puts ("function");
      else if (verbose == 2)
	{
#define PRETTY_PRINT_FUNC 1
	  char *result;

	  printf ("%s is a function\n", command);

	  /* We're blowing away THE_PRINTED_COMMAND here... */

	  result = named_function_string (command,
					  (COMMAND *) function_cell (func),
					  PRETTY_PRINT_FUNC);
	  printf ("%s\n", result);
#undef PRETTY_PRINT_FUNC
	}
      else if (verbose == 4)
	printf ("%s\n", command);

      found = 1;

      if (all == 0)
	return (1);
    }

  /* Command is a builtin? */
  if (find_shell_builtin (command))
    {
      if (verbose == 1)
	puts ("builtin");
      else if (verbose == 2)
	printf ("%s is a shell builtin\n", command);
      else if (verbose == 4)
	printf ("%s\n", command);

      found = 1;

      if (all == 0)
	return (1);
    }

  /* Command is a disk file? */
  /* If the command name given is already an absolute command, just
     check to see if it is executable. */
  if (absolute_program (command))
    {
      f = file_status (command);
      if (f & FS_EXECABLE)
	{
	  if (verbose == 1)
	    puts ("file");
	  else if (verbose == 2)
	    printf ("%s is %s\n", command, command);
	  else if (verbose == 3 || verbose == 4)
	    printf ("%s\n", command);

	  /* There's no use looking in the hash table or in $PATH,
	     because they're not consulted when an absolute program
	     name is supplied. */
	  return (1);
	}
    }

  /* If the user isn't doing "-a", then we might care about
     whether the file is present in our hash table. */
  if (all == 0)
    {
      if ((full_path = find_hashed_filename (command)) != (char *)NULL)
	{
	  if (verbose == 1)
	    puts ("file");
	  else if (verbose == 2)
	    printf ("%s is hashed (%s)\n", command, full_path);
	  else if (verbose == 3 || verbose == 4)
	    printf ("%s\n", full_path);

	  free (full_path);
	  return (1);
	}
    }

  /* Now search through $PATH. */
  while (1)
    {
      if (all == 0)
	full_path = find_user_command (command);
      else
	full_path =
	  user_command_matches (command, FS_EXEC_ONLY, found_file);
	  /* XXX - should that be FS_EXEC_PREFERRED? */

      if (!full_path)
	break;

      /* If we found the command as itself by looking through $PATH, it
	 probably doesn't exist.  Check whether or not the command is an
	 executable file.  If it's not, don't report a match. */
      if (STREQ (full_path, command))
	{
	  f = file_status (full_path);
	  if ((f & FS_EXECABLE) == 0)
	    {
	      free (full_path);
	      full_path = (char *)NULL;
	      if (all == 0)
		break;
	    }
	  else if (verbose >= 2)
	    full_path = sh_makepath ((char *)NULL, full_path, MP_DOCWD);
	}

      found_file++;
      found = 1;

      if (verbose == 1)
	puts ("file");
      else if (verbose == 2)
	printf ("%s is %s\n", command, full_path);
      else if (verbose == 3 || verbose == 4)
	printf ("%s\n", full_path);

      free (full_path);
      full_path = (char *)NULL;

      if (all == 0)
	break;
    }

  return (found);
}