aboutsummaryrefslogtreecommitdiffstats
path: root/builtins/command.def
blob: 6e6e5f60b0f50acb7cec44ed2931ba03750e4c27 (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
This file is command.def, from which is created command.c.
It implements the builtin "command" in Bash.

Copyright (C) 1987, 1989, 1991 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 1, 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, 675 Mass Ave, Cambridge, MA 02139, USA.

$PRODUCES command.c

$BUILTIN command
$FUNCTION command_builtin
$SHORT_DOC command [-pVv] command [arg ...]
Runs COMMAND with ARGS ignoring shell functions.  If you have a shell
function called `ls', and you wish to call the command `ls', you can
say "command ls".  If the -p option is given, a default value is used
for PATH that is guaranteed to find all of the standard utilities.  If
the -V or -v option is given, a string is printed describing COMMAND.
The -V option produces a more verbose description.
$END

#include <config.h>

#if defined (HAVE_UNISTD_H)
#  include <unistd.h>
#endif

#include "../bashansi.h"

#include "../shell.h"
#include "../execute_cmd.h"
#include "../flags.h"
#include "bashgetopt.h"
#include "common.h"

extern int subshell_environment;

static void restore_path ();
static char *get_standard_path ();

/* Run the commands mentioned in LIST without paying attention to shell
   functions. */
int
command_builtin (list)
     WORD_LIST *list;
{
  int result, verbose, use_standard_path, opt;
  char *old_path, *standard_path;
  COMMAND *command;

  verbose = use_standard_path = 0;
  reset_internal_getopt ();
  while ((opt = internal_getopt (list, "pvV")) != -1)
    {
      switch (opt)
	{
	case 'p':
	  use_standard_path = 1;
	  break;
	case 'V':
	  verbose = 2;
	  break;
	case 'v':
	  verbose = 4;
	  break;
	default:
	  builtin_usage ();
	  return (EX_USAGE);
	}
    }
  list = loptend;

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

  if (verbose)
    {
      int found, any_found;

      for (any_found = 0; list; list = list->next)
	{
	  found = describe_command (list->word->word, verbose, 0);

	  if (found == 0)
	    builtin_error ("%s: not found", list->word->word);

	  any_found += found;
	}
      return (any_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
    }

#if defined (RESTRICTED_SHELL)
  if (use_standard_path && restricted)
    {
      builtin_error ("restricted: cannot use -p");
      return (EXECUTION_FAILURE);
    }
#endif

  begin_unwind_frame ("command_builtin");

  /* We don't want this to be reparsed (consider command echo 'foo &'), so
     just make a simple_command structure and call execute_command with it. */
  if (use_standard_path)
    {      
      old_path = get_string_value ("PATH");
      /* If old_path is NULL, $PATH is unset.  If so, we want to make sure
	 it's unset after this command completes. */
      if (old_path)
	old_path = savestring (old_path);
      add_unwind_protect ((Function *)restore_path, old_path);

      standard_path = get_standard_path ();
      bind_variable ("PATH", standard_path ? standard_path : "");
      FREE (standard_path);
    }

  command = make_bare_simple_command ();
  command->value.Simple->words = (WORD_LIST *)copy_word_list (list);
  command->value.Simple->redirects = (REDIRECT *)NULL;
  command->flags |= (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION);
  command->value.Simple->flags |= (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION);
#if 0
  /* This breaks for things like ( cd /tmp ; command z ababa ; echo next )
     or $(command echo a ; command echo b;) or even
     { command echo a; command echo b; } & */
  /* If we're in a subshell, see if we can get away without forking
     again, since we've already forked to run this builtin. */
  if (subshell_environment)
    {
      command->flags |= CMD_NO_FORK;
      command->value.Simple->flags |= CMD_NO_FORK;
    }
#endif
  add_unwind_protect ((char *)dispose_command, command);
  result = execute_command (command);

  run_unwind_frame ("command_builtin");

  return (result);
}

/* Restore the value of the $PATH variable after replacing it when
   executing `command -p'. */
static void
restore_path (var)
     char *var;
{
  if (var)
    {
      bind_variable ("PATH", var);
      free (var);
    }
  else
    unbind_variable ("PATH");
}

/* Return a value for PATH that is guaranteed to find all of the standard
   utilities.  This uses Posix.2 configuration variables, if present.  It
   uses a value defined in config.h as a last resort. */
static char *
get_standard_path ()
{
#if defined (_CS_PATH) && defined (HAVE_CONFSTR)
  char *p;
  size_t len;

  len = (size_t)confstr (_CS_PATH, (char *)NULL, (size_t)0);
  p = xmalloc ((int)len + 2);
  *p = '\0';
  confstr (_CS_PATH, p, len);
  return (p);
#else /* !_CSPATH || !HAVE_CONFSTR  */
#  if defined (CS_PATH)
  return (savestring (CS_PATH));
#  else
  return (savestring (STANDARD_UTILS_PATH));
#  endif /* !CS_PATH */
#endif /* !_CS_PATH || !HAVE_CONFSTR */
}