commit bash-20140905 snapshot

This commit is contained in:
Chet Ramey
2014-10-02 10:20:50 -04:00
parent aee3e5611a
commit e198129d21
48 changed files with 18120 additions and 21 deletions
+73
View File
@@ -6615,3 +6615,76 @@ variables.c
recursively) instead of bind_variable_internal, so invalid variable
names (like arr[0]) don't get created. Fixes bug reported by
<lolilolicon@gmail.com>
9/3
---
execute_cmd.c
- evalnest_max,sourcenest_max: initialize from EVALNEST_MAX and
SOURCENEST_MAX, respectively. Feature suggested by
<bogun.dmitriy@gmail.com>
config-top.h
- define EVALNEST_MAX and SOURCENEST_MAX to 0
9/6
---
bashline.c
- find_cmd_start: fix to (crudely) deal with >| token; even though
skip_to_delim finds `|' as a delimiter, we call it again and use
what the second call finds. Fixes bug reported by Dan Jacobson
<jidanni@jidanni.org>
findcmd.c
- find_in_path_element: if in posix mode, do not expand a literal
tilde in a $PATH element
doc/bashref.texi
- add change to tilde expansion in $PATH elements to posix mode
description
builtins/common.h
- ISHELP: new define for builtins that do their own option parsing
and don't use internal_getopt(); checks whether argument is --help
- CHECK_HELPOPT: convenience define to help builtins that do their
own option parsing to check for --help with one line of code
- CASE_HELPOPT: convenience define to help builtins that use
internal_getopt() check for --help with one line of code
builtins/help.def
- builtin_help: new function, prints out --help output for current
builtin
builtins/{kill,let,pushd}.def
- add CHECK_HELPOPT to builtins that use ISOPTION; call builtin_help
and return EX_USAGE (kill/let/pushd/popd/dirs)
builtins/{caller,fg_bg}.def
- use CHECK_HELPOPT to recognize --help, since these builtins perform
checks that can cause them to return before calling no_options
(caller/fg/bg)
builtins/{exit,return}.def
- use CHECK_HELPOPT to recognize --help before calling get_exitstat()
(return/exit/logout)
builtins/{break,shift}.def
- use CHECK_HELPOPT to recognize --help before any other checks
(break/continue/shift)
builtins/bashgetopt.h
- GETOPT_EOF: convenience define
- GETOPT_HELP: new define, to indicate internal_getopt saw --help
builtins/bashgetopt.c
- internal_getopt: return GETOPT_HELP for --help
builtins/common.c
- no_options: recognize --help, call builtin_help and return 2
(builtin/eval/source/./times)
builtins/command.def
- use CASE_HELPOPT() to handle --help after calling internal_getopt()
builtins/{colon,echo,test}.def
- do not recognize --help (:/true/false/echo/test
)
+81
View File
@@ -6606,3 +6606,84 @@ execute_command.c:
trigger an error and jump back to the top level
- {initialize_subshell,execute_subshell_builtin_or_function}: reset
sourcenest to 0 in a subshell
9/2
---
variables.c
- bind_variable: if a nameref expands to an array reference, make
sure that assign_array_element gets called (maybe even
recursively) instead of bind_variable_internal, so invalid variable
names (like arr[0]) don't get created. Fixes bug reported by
<lolilolicon@gmail.com>
9/3
---
execute_cmd.c
- evalnest_max,sourcenest_max: initialize from EVALNEST_MAX and
SOURCENEST_MAX, respectively. Feature suggested by
<bogun.dmitriy@gmail.com>
config-top.h
- define EVALNEST_MAX and SOURCENEST_MAX to 0
9/6
---
bashline.c
- find_cmd_start: fix to (crudely) deal with >| token; even though
skip_to_delim finds `|' as a delimiter, we call it again and use
what the second call finds. Fixes bug reported by Dan Jacobson
<jidanni@jidanni.org>
findcmd.c
- find_in_path_element: if in posix mode, do not expand a literal
tilde in a $PATH element
doc/bashref.texi
- add change to tilde expansion in $PATH elements to posix mode
description
builtins/common.h
- ISHELP: new define for builtins that do their own option parsing
and don't use internal_getopt(); checks whether argument is --help
- CHECK_HELPOPT: convenience define to help builtins that do their
own option parsing to check for --help with one line of code
- CASE_HELPOPT: convenience define to help builtins that use
internal_getopt() check for --help with one line of code
builtins/help.def
- builtin_help: new function, prints out --help output for current
builtin
builtins/{kill,let,pushd}.def
- add CHECK_HELPOPT to builtins that use ISOPTION; call builtin_help
and return EX_USAGE (kill/let/pushd/popd/dirs)
builtins/{caller,fg_bg}.def
- use CHECK_HELPOPT to recognize --help, since these builtins perform
checks that can cause them to return before calling no_options
(caller/fg/bg)
builtins/{exit,return}.def
- use CHECK_HELPOPT to recognize --help before calling get_exitstat()
(return/exit/logout)
builtins/{break,shift}.def
- use CHECK_HELPOPT to recognize --help before any other checks
(break/continue/shift)
builtins/bashgetopt.h
- GETOPT_EOF: convenience define
- GETOPT_HELP: new define, to indicate internal_getopt saw --help
builtins/bashgetopt.c
- internal_getopt: return GETOPT_HELP for --help
builtins/common.c
- no_options: recognize --help, call builtin_help and return 2
(builtin/eval/source/./times)
builtins/command.def
- use CASE_HELPOPT() to handle --help after calling internal_getopt()
builtins/{colon,echo,test}.def
- do not recognize --help (:/true/false/echo/test)
+13 -2
View File
@@ -1305,7 +1305,7 @@ static int
find_cmd_start (start)
int start;
{
register int s, os;
register int s, os, ns;
os = 0;
/* Flags == SD_NOJMP only because we want to skip over command substitutions
@@ -1313,7 +1313,18 @@ find_cmd_start (start)
command substitutions as individual words. */
while (((s = skip_to_delim (rl_line_buffer, os, COMMAND_SEPARATORS, SD_NOJMP/*|SD_NOSKIPCMD*/)) <= start) &&
rl_line_buffer[s])
os = s+1;
{
/* Handle >| token crudely; treat as > not | */
if (rl_line_buffer[s] == '|' && rl_line_buffer[s-1] == '>')
{
ns = skip_to_delim (rl_line_buffer, s+1, COMMAND_SEPARATORS, SD_NOJMP/*|SD_NOSKIPCMD*/);
if (ns > start || rl_line_buffer[ns] == 0)
return os;
os = ns+1;
continue;
}
os = s+1;
}
return os;
}
+4271
View File
File diff suppressed because it is too large Load Diff
+6
View File
@@ -31,6 +31,8 @@
#include "../shell.h"
#include "common.h"
#include "bashgetopt.h"
#define ISOPT(s) (((*(s) == '-') || (plus && *(s) == '+')) && (s)[1])
#define NOTOPT(s) (((*(s) != '-') && (!plus || *(s) != '+')) || (s)[1] == '\0')
@@ -76,6 +78,10 @@ char *opts;
lhead = (WORD_LIST *)NULL;
loptend = lcurrent;
return(-1);
} else if (ISHELP (lcurrent->word->word)) {
lhead = (WORD_LIST *)NULL;
loptend = lcurrent;
return (GETOPT_HELP);
} else if (lcurrent->word->word[0] == '-' &&
lcurrent->word->word[1] == '-' &&
lcurrent->word->word[2] == 0) {
+179
View File
@@ -0,0 +1,179 @@
/* bashgetopt.c -- `getopt' for use by the builtins. */
/* Copyright (C) 1992-2002 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "../bashansi.h"
#include <chartypes.h>
#include <errno.h>
#include "../shell.h"
#include "common.h"
#define ISOPT(s) (((*(s) == '-') || (plus && *(s) == '+')) && (s)[1])
#define NOTOPT(s) (((*(s) != '-') && (!plus || *(s) != '+')) || (s)[1] == '\0')
static int sp;
char *list_optarg;
int list_optopt;
int list_opttype;
static WORD_LIST *lhead = (WORD_LIST *)NULL;
WORD_LIST *lcurrent = (WORD_LIST *)NULL;
WORD_LIST *loptend; /* Points to the first non-option argument in the list */
int
internal_getopt(list, opts)
WORD_LIST *list;
char *opts;
{
register int c;
register char *cp;
int plus; /* nonzero means to handle +option */
static char errstr[3] = { '-', '\0', '\0' };
plus = *opts == '+';
if (plus)
opts++;
if (list == 0) {
list_optarg = (char *)NULL;
loptend = (WORD_LIST *)NULL; /* No non-option arguments */
return -1;
}
if (list != lhead || lhead == 0) {
/* Hmmm.... called with a different word list. Reset. */
sp = 1;
lcurrent = lhead = list;
loptend = (WORD_LIST *)NULL;
}
if (sp == 1) {
if (lcurrent == 0 || NOTOPT(lcurrent->word->word)) {
lhead = (WORD_LIST *)NULL;
loptend = lcurrent;
return(-1);
} else if (ISHELP (lcurrent->word->word)) {
lhead = (WORD_LIST *)NULL;
loptend = lcurrent;
return (GETOPT_HELP);
} else if (lcurrent->word->word[0] == '-' &&
lcurrent->word->word[1] == '-' &&
lcurrent->word->word[2] == 0) {
lhead = (WORD_LIST *)NULL;
loptend = lcurrent->next;
return(-1);
}
errstr[0] = list_opttype = lcurrent->word->word[0];
}
list_optopt = c = lcurrent->word->word[sp];
if (c == ':' || (cp = strchr(opts, c)) == NULL) {
errstr[1] = c;
sh_invalidopt (errstr);
if (lcurrent->word->word[++sp] == '\0') {
lcurrent = lcurrent->next;
sp = 1;
}
list_optarg = NULL;
if (lcurrent)
loptend = lcurrent->next;
return('?');
}
if (*++cp == ':' || *cp == ';') {
/* `:': Option requires an argument. */
/* `;': option argument may be missing */
/* We allow -l2 as equivalent to -l 2 */
if (lcurrent->word->word[sp+1]) {
list_optarg = lcurrent->word->word + sp + 1;
lcurrent = lcurrent->next;
/* If the specifier is `;', don't set optarg if the next
argument looks like another option. */
#if 0
} else if (lcurrent->next && (*cp == ':' || lcurrent->next->word->word[0] != '-')) {
#else
} else if (lcurrent->next && (*cp == ':' || NOTOPT(lcurrent->next->word->word))) {
#endif
lcurrent = lcurrent->next;
list_optarg = lcurrent->word->word;
lcurrent = lcurrent->next;
} else if (*cp == ';') {
list_optarg = (char *)NULL;
lcurrent = lcurrent->next;
} else { /* lcurrent->next == NULL */
errstr[1] = c;
sh_needarg (errstr);
sp = 1;
list_optarg = (char *)NULL;
return('?');
}
sp = 1;
} else if (*cp == '#') {
/* option requires a numeric argument */
if (lcurrent->word->word[sp+1]) {
if (DIGIT(lcurrent->word->word[sp+1])) {
list_optarg = lcurrent->word->word + sp + 1;
lcurrent = lcurrent->next;
} else
list_optarg = (char *)NULL;
} else {
if (lcurrent->next && legal_number(lcurrent->next->word->word, (intmax_t *)0)) {
lcurrent = lcurrent->next;
list_optarg = lcurrent->word->word;
lcurrent = lcurrent->next;
} else {
errstr[1] = c;
sh_neednumarg (errstr);
sp = 1;
list_optarg = (char *)NULL;
return ('?');
}
}
} else {
/* No argument, just return the option. */
if (lcurrent->word->word[++sp] == '\0') {
sp = 1;
lcurrent = lcurrent->next;
}
list_optarg = (char *)NULL;
}
return(c);
}
/*
* reset_internal_getopt -- force the in[ft]ernal getopt to reset
*/
void
reset_internal_getopt ()
{
lhead = lcurrent = loptend = (WORD_LIST *)NULL;
sp = 1;
}
+3
View File
@@ -25,6 +25,9 @@
#include <stdc.h>
#define GETOPT_EOF -1
#define GETOPT_HELP -99
extern char *list_optarg;
extern int list_optopt;
+39
View File
@@ -0,0 +1,39 @@
/* bashgetopt.h -- extern declarations for stuff defined in bashgetopt.c. */
/* Copyright (C) 1993 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/
/* See getopt.h for the explanation of these variables. */
#if !defined (__BASH_GETOPT_H)
# define __BASH_GETOPT_H
#include <stdc.h>
extern char *list_optarg;
extern int list_optopt;
extern int list_opttype;
extern WORD_LIST *lcurrent;
extern WORD_LIST *loptend;
extern int internal_getopt __P((WORD_LIST *, char *));
extern void reset_internal_getopt __P((void));
#endif /* !__BASH_GETOPT_H */
+2 -2
View File
@@ -1,7 +1,7 @@
This file is bind.def, from which is created bind.c.
It implements the builtin "bind" in Bash.
Copyright (C) 1987-2009 Free Software Foundation, Inc.
Copyright (C) 1987-2014 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
@@ -141,7 +141,7 @@ bind_builtin (list)
rl_outstream = stdout;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "lvpVPsSXf:q:u:m:r:x:")) != EOF)
while ((opt = internal_getopt (list, "lvpVPsSXf:q:u:m:r:x:")) != -1)
{
switch (opt)
{
+4
View File
@@ -67,6 +67,8 @@ break_builtin (list)
{
intmax_t newbreak;
CHECK_HELPOPT (list);
if (check_loop_level () == 0)
return (EXECUTION_SUCCESS);
@@ -107,6 +109,8 @@ continue_builtin (list)
{
intmax_t newcont;
CHECK_HELPOPT (list);
if (check_loop_level () == 0)
return (EXECUTION_SUCCESS);
+141
View File
@@ -0,0 +1,141 @@
This file is break.def, from which is created break.c.
It implements the builtins "break" and "continue" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES break.c
$BUILTIN break
$FUNCTION break_builtin
$SHORT_DOC break [n]
Exit for, while, or until loops.
Exit a FOR, WHILE or UNTIL loop. If N is specified, break N enclosing
loops.
Exit Status:
The exit status is 0 unless N is not greater than or equal to 1.
$END
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
extern char *this_command_name;
extern int posixly_correct;
static int check_loop_level __P((void));
/* The depth of while's and until's. */
int loop_level = 0;
/* Non-zero when a "break" instruction is encountered. */
int breaking = 0;
/* Non-zero when we have encountered a continue instruction. */
int continuing = 0;
/* Set up to break x levels, where x defaults to 1, but can be specified
as the first argument. */
int
break_builtin (list)
WORD_LIST *list;
{
intmax_t newbreak;
if (check_loop_level () == 0)
return (EXECUTION_SUCCESS);
(void)get_numeric_arg (list, 1, &newbreak);
if (newbreak <= 0)
{
sh_erange (list->word->word, _("loop count"));
breaking = loop_level;
return (EXECUTION_FAILURE);
}
if (newbreak > loop_level)
newbreak = loop_level;
breaking = newbreak;
return (EXECUTION_SUCCESS);
}
$BUILTIN continue
$FUNCTION continue_builtin
$SHORT_DOC continue [n]
Resume for, while, or until loops.
Resumes the next iteration of the enclosing FOR, WHILE or UNTIL loop.
If N is specified, resumes the Nth enclosing loop.
Exit Status:
The exit status is 0 unless N is not greater than or equal to 1.
$END
/* Set up to continue x levels, where x defaults to 1, but can be specified
as the first argument. */
int
continue_builtin (list)
WORD_LIST *list;
{
intmax_t newcont;
if (check_loop_level () == 0)
return (EXECUTION_SUCCESS);
(void)get_numeric_arg (list, 1, &newcont);
if (newcont <= 0)
{
sh_erange (list->word->word, _("loop count"));
breaking = loop_level;
return (EXECUTION_FAILURE);
}
if (newcont > loop_level)
newcont = loop_level;
continuing = newcont;
return (EXECUTION_SUCCESS);
}
/* Return non-zero if a break or continue command would be okay.
Print an error message if break or continue is meaningless here. */
static int
check_loop_level ()
{
#if defined (BREAK_COMPLAINS)
if (loop_level == 0 && posixly_correct == 0)
builtin_error (_("only meaningful in a `for', `while', or `until' loop"));
#endif /* BREAK_COMPLAINS */
return (loop_level);
}
+2
View File
@@ -81,6 +81,8 @@ caller_builtin (list)
char *funcname_s, *source_s, *lineno_s;
intmax_t num;
CHECK_HELPOPT (list);
GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
+160
View File
@@ -0,0 +1,160 @@
This file is caller.def, from which is created caller.c. It implements the
builtin "caller" in Bash.
Copyright (C) 2002-2008 Rocky Bernstein for Free Software Foundation, Inc.
Copyright (C) 2008-2013 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES caller.c
$BUILTIN caller
$FUNCTION caller_builtin
$DEPENDS_ON DEBUGGER
$SHORT_DOC caller [expr]
Return the context of the current subroutine call.
Without EXPR, returns "$line $filename". With EXPR, returns
"$line $subroutine $filename"; this extra information can be used to
provide a stack trace.
The value of EXPR indicates how many call frames to go back before the
current one; the top frame is frame 0.
Exit Status:
Returns 0 unless the shell is not executing a shell function or EXPR
is invalid.
$END
#include <config.h>
#include <stdio.h>
#include "chartypes.h"
#include "bashtypes.h"
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include <errno.h>
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
#include "builtext.h"
#include "bashgetopt.h"
#ifdef LOADABLE_BUILTIN
# include "builtins.h"
#endif
#if !defined (errno)
extern int errno;
#endif /* !errno */
int
caller_builtin (list)
WORD_LIST *list;
{
#if !defined (ARRAY_VARS)
printf ("1 NULL\n");
return (EXECUTION_FAILURE);
#else
SHELL_VAR *funcname_v, *bash_source_v, *bash_lineno_v;
ARRAY *funcname_a, *bash_source_a, *bash_lineno_a;
char *funcname_s, *source_s, *lineno_s;
intmax_t num;
if (list && list->word && ISHELP (list->word->word))
{
builtin_help ();
return (EX_USAGE);
}
GET_ARRAY_FROM_VAR ("FUNCNAME", funcname_v, funcname_a);
GET_ARRAY_FROM_VAR ("BASH_SOURCE", bash_source_v, bash_source_a);
GET_ARRAY_FROM_VAR ("BASH_LINENO", bash_lineno_v, bash_lineno_a);
if (bash_lineno_a == 0 || array_empty (bash_lineno_a))
return (EXECUTION_FAILURE);
if (bash_source_a == 0 || array_empty (bash_source_a))
return (EXECUTION_FAILURE);
if (no_options (list))
return (EX_USAGE);
list = loptend; /* skip over possible `--' */
/* If there is no argument list, then give short form: line filename. */
if (list == 0)
{
lineno_s = array_reference (bash_lineno_a, 0);
source_s = array_reference (bash_source_a, 1);
printf("%s %s\n", lineno_s ? lineno_s : "NULL", source_s ? source_s : "NULL");
return (EXECUTION_SUCCESS);
}
if (funcname_a == 0 || array_empty (funcname_a))
return (EXECUTION_FAILURE);
if (legal_number (list->word->word, &num))
{
lineno_s = array_reference (bash_lineno_a, num);
source_s = array_reference (bash_source_a, num+1);
funcname_s = array_reference (funcname_a, num+1);
if (lineno_s == NULL|| source_s == NULL || funcname_s == NULL)
return (EXECUTION_FAILURE);
printf("%s %s %s\n", lineno_s, funcname_s, source_s);
}
else
{
sh_invalidnum (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
return (EXECUTION_SUCCESS);
#endif
}
#ifdef LOADABLE_BUILTIN
static char *caller_doc[] = {
N_("Returns the context of the current subroutine call.\n\
\n\
Without EXPR, returns "$line $filename". With EXPR, returns\n\
"$line $subroutine $filename"; this extra information can be used to\n\
provide a stack trace.\n\
\n\
The value of EXPR indicates how many call frames to go back before the\n\
current one; the top frame is frame 0."),
(char *)NULL
};
struct builtin caller_struct = {
"caller",
caller_builtin,
BUILTIN_ENABLED,
caller_doc,
"caller [EXPR]",
0
};
#endif /* LOADABLE_BUILTIN */
+1
View File
@@ -90,6 +90,7 @@ command_builtin (list)
case 'v':
verbose = CDESC_REUSABLE; /* ditto */
break;
CASE_HELPOPT();
default:
builtin_usage ();
return (EX_USAGE);
+219
View File
@@ -0,0 +1,219 @@
This file is command.def, from which is created command.c.
It implements the builtin "command" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES command.c
$BUILTIN command
$FUNCTION command_builtin
$SHORT_DOC command [-pVv] command [arg ...]
Execute a simple command or display information about commands.
Runs COMMAND with ARGS suppressing shell function lookup, or display
information about the specified COMMANDs. Can be used to invoke commands
on disk when a function with the same name exists.
Options:
-p use a default value for PATH that is guaranteed to find all of
the standard utilities
-v print a description of COMMAND similar to the `type' builtin
-V print a more verbose description of each COMMAND
Exit Status:
Returns exit status of COMMAND, or failure if COMMAND is not found.
$END
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashansi.h"
#include "../shell.h"
#include "../execute_cmd.h"
#include "../flags.h"
#include "bashgetopt.h"
#include "common.h"
#if defined (_CS_PATH) && defined (HAVE_CONFSTR) && !HAVE_DECL_CONFSTR
extern size_t confstr __P((int, char *, size_t));
#endif
extern int subshell_environment;
static void restore_path __P((char *));
static char *get_standard_path __P((void));
/* 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 = CDESC_SHORTDESC|CDESC_ABSPATH; /* look in common.h for constants */
break;
case 'v':
verbose = CDESC_REUSABLE; /* ditto */
break;
default:
builtin_usage ();
return (EX_USAGE);
}
}
list = loptend;
if (list == 0)
return (EXECUTION_SUCCESS);
#if defined (RESTRICTED_SHELL)
if (use_standard_path && restricted)
{
sh_restricted ("-p");
return (EXECUTION_FAILURE);
}
#endif
begin_unwind_frame ("command_builtin");
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 : "", 0);
stupidly_hack_special_variables ("PATH");
FREE (standard_path);
}
if (verbose)
{
int found, any_found;
for (any_found = 0; list; list = list->next)
{
found = describe_command (list->word->word, verbose);
if (found == 0 && verbose != CDESC_REUSABLE)
sh_notfound (list->word->word);
any_found += found;
}
run_unwind_frame ("command_builtin");
return (any_found ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}
#define COMMAND_BUILTIN_FLAGS (CMD_NO_FUNCTIONS | CMD_INHIBIT_EXPANSION | CMD_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. */
command = make_bare_simple_command ();
command->value.Simple->words = (WORD_LIST *)copy_word_list (list);
command->value.Simple->redirects = (REDIRECT *)NULL;
command->flags |= COMMAND_BUILTIN_FLAGS;
command->value.Simple->flags |= COMMAND_BUILTIN_FLAGS;
#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, 0);
free (var);
}
else
unbind_variable ("PATH");
stupidly_hack_special_variables ("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);
if (len > 0)
{
p = (char *)xmalloc (len + 2);
*p = '\0';
confstr (_CS_PATH, p, len);
return (p);
}
else
return (savestring (STANDARD_UTILS_PATH));
#else /* !_CS_PATH || !HAVE_CONFSTR */
# if defined (CS_PATH)
return (savestring (CS_PATH));
# else
return (savestring (STANDARD_UTILS_PATH));
# endif /* !CS_PATH */
#endif /* !_CS_PATH || !HAVE_CONFSTR */
}
+8 -1
View File
@@ -176,9 +176,16 @@ int
no_options (list)
WORD_LIST *list;
{
int opt;
reset_internal_getopt ();
if (internal_getopt (list, "") != -1)
if ((opt = internal_getopt (list, "")) != -1)
{
if (opt == GETOPT_HELP)
{
builtin_help ();
return (2);
}
builtin_usage ();
return (1);
}
+901
View File
@@ -0,0 +1,901 @@
/* common.c - utility functions for all builtins */
/* Copyright (C) 1987-2010 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include <stdio.h>
#include <chartypes.h>
#include "../bashtypes.h"
#include "posixstat.h"
#include <signal.h>
#include <errno.h>
#if defined (PREFER_STDARG)
# include <stdarg.h>
#else
# include <varargs.h>
#endif
#include "../bashansi.h"
#include "../bashintl.h"
#define NEED_FPURGE_DECL
#include "../shell.h"
#include "maxpath.h"
#include "../flags.h"
#include "../jobs.h"
#include "../builtins.h"
#include "../input.h"
#include "../execute_cmd.h"
#include "../trap.h"
#include "bashgetopt.h"
#include "common.h"
#include "builtext.h"
#include <tilde/tilde.h>
#if defined (HISTORY)
# include "../bashhist.h"
#endif
#if !defined (errno)
extern int errno;
#endif /* !errno */
extern int indirection_level, subshell_environment;
extern int line_number;
extern int last_command_exit_value;
extern int trap_saved_exit_value;
extern int running_trap;
extern int posixly_correct;
extern char *this_command_name, *shell_name;
extern const char * const bash_getcwd_errstr;
/* Used by some builtins and the mainline code. */
sh_builtin_func_t *last_shell_builtin = (sh_builtin_func_t *)NULL;
sh_builtin_func_t *this_shell_builtin = (sh_builtin_func_t *)NULL;
/* **************************************************************** */
/* */
/* Error reporting, usage, and option processing */
/* */
/* **************************************************************** */
/* This is a lot like report_error (), but it is for shell builtins
instead of shell control structures, and it won't ever exit the
shell. */
static void
builtin_error_prolog ()
{
char *name;
name = get_name_for_error ();
fprintf (stderr, "%s: ", name);
if (interactive_shell == 0)
fprintf (stderr, _("line %d: "), executing_line_number ());
if (this_command_name && *this_command_name)
fprintf (stderr, "%s: ", this_command_name);
}
void
#if defined (PREFER_STDARG)
builtin_error (const char *format, ...)
#else
builtin_error (format, va_alist)
const char *format;
va_dcl
#endif
{
va_list args;
builtin_error_prolog ();
SH_VA_START (args, format);
vfprintf (stderr, format, args);
va_end (args);
fprintf (stderr, "\n");
}
void
#if defined (PREFER_STDARG)
builtin_warning (const char *format, ...)
#else
builtin_warning (format, va_alist)
const char *format;
va_dcl
#endif
{
va_list args;
builtin_error_prolog ();
fprintf (stderr, _("warning: "));
SH_VA_START (args, format);
vfprintf (stderr, format, args);
va_end (args);
fprintf (stderr, "\n");
}
/* Print a usage summary for the currently-executing builtin command. */
void
builtin_usage ()
{
if (this_command_name && *this_command_name)
fprintf (stderr, _("%s: usage: "), this_command_name);
fprintf (stderr, "%s\n", _(current_builtin->short_doc));
fflush (stderr);
}
/* Return if LIST is NULL else barf and jump to top_level. Used by some
builtins that do not accept arguments. */
void
no_args (list)
WORD_LIST *list;
{
if (list)
{
builtin_error (_("too many arguments"));
top_level_cleanup ();
jump_to_top_level (DISCARD);
}
}
/* Check that no options were given to the currently-executing builtin,
and return 0 if there were options. */
int
no_options (list)
WORD_LIST *list;
{
reset_internal_getopt ();
if (internal_getopt (list, "") != -1)
{
builtin_usage ();
return (1);
}
return (0);
}
void
sh_needarg (s)
char *s;
{
builtin_error (_("%s: option requires an argument"), s);
}
void
sh_neednumarg (s)
char *s;
{
builtin_error (_("%s: numeric argument required"), s);
}
void
sh_notfound (s)
char *s;
{
builtin_error (_("%s: not found"), s);
}
/* Function called when one of the builtin commands detects an invalid
option. */
void
sh_invalidopt (s)
char *s;
{
builtin_error (_("%s: invalid option"), s);
}
void
sh_invalidoptname (s)
char *s;
{
builtin_error (_("%s: invalid option name"), s);
}
void
sh_invalidid (s)
char *s;
{
builtin_error (_("`%s': not a valid identifier"), s);
}
void
sh_invalidnum (s)
char *s;
{
char *msg;
if (*s == '0' && isdigit (s[1]))
msg = _("invalid octal number");
else if (*s == '0' && s[1] == 'x')
msg = _("invalid hex number");
else
msg = _("invalid number");
builtin_error ("%s: %s", s, msg);
}
void
sh_invalidsig (s)
char *s;
{
builtin_error (_("%s: invalid signal specification"), s);
}
void
sh_badpid (s)
char *s;
{
builtin_error (_("`%s': not a pid or valid job spec"), s);
}
void
sh_readonly (s)
const char *s;
{
builtin_error (_("%s: readonly variable"), s);
}
void
sh_erange (s, desc)
char *s, *desc;
{
if (s)
builtin_error (_("%s: %s out of range"), s, desc ? desc : _("argument"));
else
builtin_error (_("%s out of range"), desc ? desc : _("argument"));
}
#if defined (JOB_CONTROL)
void
sh_badjob (s)
char *s;
{
builtin_error (_("%s: no such job"), s);
}
void
sh_nojobs (s)
char *s;
{
if (s)
builtin_error (_("%s: no job control"), s);
else
builtin_error (_("no job control"));
}
#endif
#if defined (RESTRICTED_SHELL)
void
sh_restricted (s)
char *s;
{
if (s)
builtin_error (_("%s: restricted"), s);
else
builtin_error (_("restricted"));
}
#endif
void
sh_notbuiltin (s)
char *s;
{
builtin_error (_("%s: not a shell builtin"), s);
}
void
sh_wrerror ()
{
#if defined (DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS) && defined (EPIPE)
if (errno != EPIPE)
#endif /* DONT_REPORT_BROKEN_PIPE_WRITE_ERRORS && EPIPE */
builtin_error (_("write error: %s"), strerror (errno));
}
void
sh_ttyerror (set)
int set;
{
if (set)
builtin_error (_("error setting terminal attributes: %s"), strerror (errno));
else
builtin_error (_("error getting terminal attributes: %s"), strerror (errno));
}
int
sh_chkwrite (s)
int s;
{
fflush (stdout);
if (ferror (stdout))
{
sh_wrerror ();
fpurge (stdout);
clearerr (stdout);
return (EXECUTION_FAILURE);
}
return (s);
}
/* **************************************************************** */
/* */
/* Shell positional parameter manipulation */
/* */
/* **************************************************************** */
/* Convert a WORD_LIST into a C-style argv. Return the number of elements
in the list in *IP, if IP is non-null. A convenience function for
loadable builtins; also used by `test'. */
char **
make_builtin_argv (list, ip)
WORD_LIST *list;
int *ip;
{
char **argv;
argv = strvec_from_word_list (list, 0, 1, ip);
argv[0] = this_command_name;
return argv;
}
/* Remember LIST in $1 ... $9, and REST_OF_ARGS. If DESTRUCTIVE is
non-zero, then discard whatever the existing arguments are, else
only discard the ones that are to be replaced. */
void
remember_args (list, destructive)
WORD_LIST *list;
int destructive;
{
register int i;
for (i = 1; i < 10; i++)
{
if ((destructive || list) && dollar_vars[i])
{
free (dollar_vars[i]);
dollar_vars[i] = (char *)NULL;
}
if (list)
{
dollar_vars[i] = savestring (list->word->word);
list = list->next;
}
}
/* If arguments remain, assign them to REST_OF_ARGS.
Note that copy_word_list (NULL) returns NULL, and
that dispose_words (NULL) does nothing. */
if (destructive || list)
{
dispose_words (rest_of_args);
rest_of_args = copy_word_list (list);
}
if (destructive)
set_dollar_vars_changed ();
}
static int changed_dollar_vars;
/* Have the dollar variables been reset to new values since we last
checked? */
int
dollar_vars_changed ()
{
return (changed_dollar_vars);
}
void
set_dollar_vars_unchanged ()
{
changed_dollar_vars = 0;
}
void
set_dollar_vars_changed ()
{
if (variable_context)
changed_dollar_vars |= ARGS_FUNC;
else if (this_shell_builtin == set_builtin)
changed_dollar_vars |= ARGS_SETBLTIN;
else
changed_dollar_vars |= ARGS_INVOC;
}
/* **************************************************************** */
/* */
/* Validating numeric input and arguments */
/* */
/* **************************************************************** */
/* Read a numeric arg for this_command_name, the name of the shell builtin
that wants it. LIST is the word list that the arg is to come from.
Accept only the numeric argument; report an error if other arguments
follow. If FATAL is 1, call throw_to_top_level, which exits the
shell; if it's 2, call jump_to_top_level (DISCARD), which aborts the
current command; if FATAL is 0, return an indication of an invalid
number by setting *NUMOK == 0 and return -1. */
int
get_numeric_arg (list, fatal, count)
WORD_LIST *list;
int fatal;
intmax_t *count;
{
char *arg;
if (count)
*count = 1;
if (list && list->word && ISOPTION (list->word->word, '-'))
list = list->next;
if (list)
{
arg = list->word->word;
if (arg == 0 || (legal_number (arg, count) == 0))
{
sh_neednumarg (list->word->word ? list->word->word : "`'");
if (fatal == 0)
return 0;
else if (fatal == 1) /* fatal == 1; abort */
throw_to_top_level ();
else /* fatal == 2; discard current command */
{
top_level_cleanup ();
jump_to_top_level (DISCARD);
}
}
no_args (list->next);
}
return (1);
}
/* Get an eight-bit status value from LIST */
int
get_exitstat (list)
WORD_LIST *list;
{
int status;
intmax_t sval;
char *arg;
if (list && list->word && ISOPTION (list->word->word, '-'))
list = list->next;
if (list == 0)
{
/* If we're not running the DEBUG trap, the return builtin, when not
given any arguments, uses the value of $? before the trap ran. If
given an argument, return uses it. This means that the trap can't
change $?. The DEBUG trap gets to change $?, though, since that is
part of its reason for existing, and because the extended debug mode
does things with the return value. */
if (this_shell_builtin == return_builtin && running_trap > 0 && running_trap != DEBUG_TRAP+1)
return (trap_saved_exit_value);
return (last_command_exit_value);
}
arg = list->word->word;
if (arg == 0 || legal_number (arg, &sval) == 0)
{
sh_neednumarg (list->word->word ? list->word->word : "`'");
return EX_BADUSAGE;
}
no_args (list->next);
status = sval & 255;
return status;
}
/* Return the octal number parsed from STRING, or -1 to indicate
that the string contained a bad number. */
int
read_octal (string)
char *string;
{
int result, digits;
result = digits = 0;
while (*string && ISOCTAL (*string))
{
digits++;
result = (result * 8) + (*string++ - '0');
if (result > 0777)
return -1;
}
if (digits == 0 || *string)
result = -1;
return (result);
}
/* **************************************************************** */
/* */
/* Manipulating the current working directory */
/* */
/* **************************************************************** */
/* Return a consed string which is the current working directory.
FOR_WHOM is the name of the caller for error printing. */
char *the_current_working_directory = (char *)NULL;
char *
get_working_directory (for_whom)
char *for_whom;
{
if (no_symbolic_links)
{
FREE (the_current_working_directory);
the_current_working_directory = (char *)NULL;
}
if (the_current_working_directory == 0)
{
#if defined (GETCWD_BROKEN)
the_current_working_directory = getcwd (0, PATH_MAX);
#else
the_current_working_directory = getcwd (0, 0);
#endif
if (the_current_working_directory == 0)
{
fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
(for_whom && *for_whom) ? for_whom : get_name_for_error (),
_(bash_getcwd_errstr), strerror (errno));
return (char *)NULL;
}
}
return (savestring (the_current_working_directory));
}
/* Make NAME our internal idea of the current working directory. */
void
set_working_directory (name)
char *name;
{
FREE (the_current_working_directory);
the_current_working_directory = savestring (name);
}
/* **************************************************************** */
/* */
/* Job control support functions */
/* */
/* **************************************************************** */
#if defined (JOB_CONTROL)
int
get_job_by_name (name, flags)
const char *name;
int flags;
{
register int i, wl, cl, match, job;
register PROCESS *p;
register JOB *j;
job = NO_JOB;
wl = strlen (name);
for (i = js.j_jobslots - 1; i >= 0; i--)
{
j = get_job_by_jid (i);
if (j == 0 || ((flags & JM_STOPPED) && J_JOBSTATE(j) != JSTOPPED))
continue;
p = j->pipe;
do
{
if (flags & JM_EXACT)
{
cl = strlen (p->command);
match = STREQN (p->command, name, cl);
}
else if (flags & JM_SUBSTRING)
match = strcasestr (p->command, name) != (char *)0;
else
match = STREQN (p->command, name, wl);
if (match == 0)
{
p = p->next;
continue;
}
else if (flags & JM_FIRSTMATCH)
return i; /* return first match */
else if (job != NO_JOB)
{
if (this_shell_builtin)
builtin_error (_("%s: ambiguous job spec"), name);
else
internal_error (_("%s: ambiguous job spec"), name);
return (DUP_JOB);
}
else
job = i;
}
while (p != j->pipe);
}
return (job);
}
/* Return the job spec found in LIST. */
int
get_job_spec (list)
WORD_LIST *list;
{
register char *word;
int job, jflags;
if (list == 0)
return (js.j_current);
word = list->word->word;
if (*word == '\0')
return (NO_JOB);
if (*word == '%')
word++;
if (DIGIT (*word) && all_digits (word))
{
job = atoi (word);
return (job > js.j_jobslots ? NO_JOB : job - 1);
}
jflags = 0;
switch (*word)
{
case 0:
case '%':
case '+':
return (js.j_current);
case '-':
return (js.j_previous);
case '?': /* Substring search requested. */
jflags |= JM_SUBSTRING;
word++;
/* FALLTHROUGH */
default:
return get_job_by_name (word, jflags);
}
}
#endif /* JOB_CONTROL */
/*
* NOTE: `kill' calls this function with forcecols == 0
*/
int
display_signal_list (list, forcecols)
WORD_LIST *list;
int forcecols;
{
register int i, column;
char *name;
int result, signum, dflags;
intmax_t lsignum;
result = EXECUTION_SUCCESS;
if (!list)
{
for (i = 1, column = 0; i < NSIG; i++)
{
name = signal_name (i);
if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
continue;
if (posixly_correct && !forcecols)
{
/* This is for the kill builtin. POSIX.2 says the signal names
are displayed without the `SIG' prefix. */
if (STREQN (name, "SIG", 3))
name += 3;
printf ("%s%s", name, (i == NSIG - 1) ? "" : " ");
}
else
{
printf ("%2d) %s", i, name);
if (++column < 5)
printf ("\t");
else
{
printf ("\n");
column = 0;
}
}
}
if ((posixly_correct && !forcecols) || column != 0)
printf ("\n");
return result;
}
/* List individual signal names or numbers. */
while (list)
{
if (legal_number (list->word->word, &lsignum))
{
/* This is specified by Posix.2 so that exit statuses can be
mapped into signal numbers. */
if (lsignum > 128)
lsignum -= 128;
if (lsignum < 0 || lsignum >= NSIG)
{
sh_invalidsig (list->word->word);
result = EXECUTION_FAILURE;
list = list->next;
continue;
}
signum = lsignum;
name = signal_name (signum);
if (STREQN (name, "SIGJUNK", 7) || STREQN (name, "Unknown", 7))
{
list = list->next;
continue;
}
#if defined (JOB_CONTROL)
/* POSIX.2 says that `kill -l signum' prints the signal name without
the `SIG' prefix. */
printf ("%s\n", (this_shell_builtin == kill_builtin) ? name + 3 : name);
#else
printf ("%s\n", name);
#endif
}
else
{
dflags = DSIG_NOCASE;
if (posixly_correct == 0 || this_shell_builtin != kill_builtin)
dflags |= DSIG_SIGPREFIX;
signum = decode_signal (list->word->word, dflags);
if (signum == NO_SIG)
{
sh_invalidsig (list->word->word);
result = EXECUTION_FAILURE;
list = list->next;
continue;
}
printf ("%d\n", signum);
}
list = list->next;
}
return (result);
}
/* **************************************************************** */
/* */
/* Finding builtin commands and their functions */
/* */
/* **************************************************************** */
/* Perform a binary search and return the address of the builtin function
whose name is NAME. If the function couldn't be found, or the builtin
is disabled or has no function associated with it, return NULL.
Return the address of the builtin.
DISABLED_OKAY means find it even if the builtin is disabled. */
struct builtin *
builtin_address_internal (name, disabled_okay)
char *name;
int disabled_okay;
{
int hi, lo, mid, j;
hi = num_shell_builtins - 1;
lo = 0;
while (lo <= hi)
{
mid = (lo + hi) / 2;
j = shell_builtins[mid].name[0] - name[0];
if (j == 0)
j = strcmp (shell_builtins[mid].name, name);
if (j == 0)
{
/* It must have a function pointer. It must be enabled, or we
must have explicitly allowed disabled functions to be found,
and it must not have been deleted. */
if (shell_builtins[mid].function &&
((shell_builtins[mid].flags & BUILTIN_DELETED) == 0) &&
((shell_builtins[mid].flags & BUILTIN_ENABLED) || disabled_okay))
return (&shell_builtins[mid]);
else
return ((struct builtin *)NULL);
}
if (j > 0)
hi = mid - 1;
else
lo = mid + 1;
}
return ((struct builtin *)NULL);
}
/* Return the pointer to the function implementing builtin command NAME. */
sh_builtin_func_t *
find_shell_builtin (name)
char *name;
{
current_builtin = builtin_address_internal (name, 0);
return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
}
/* Return the address of builtin with NAME, whether it is enabled or not. */
sh_builtin_func_t *
builtin_address (name)
char *name;
{
current_builtin = builtin_address_internal (name, 1);
return (current_builtin ? current_builtin->function : (sh_builtin_func_t *)NULL);
}
/* Return the function implementing the builtin NAME, but only if it is a
POSIX.2 special builtin. */
sh_builtin_func_t *
find_special_builtin (name)
char *name;
{
current_builtin = builtin_address_internal (name, 0);
return ((current_builtin && (current_builtin->flags & SPECIAL_BUILTIN)) ?
current_builtin->function :
(sh_builtin_func_t *)NULL);
}
static int
shell_builtin_compare (sbp1, sbp2)
struct builtin *sbp1, *sbp2;
{
int result;
if ((result = sbp1->name[0] - sbp2->name[0]) == 0)
result = strcmp (sbp1->name, sbp2->name);
return (result);
}
/* Sort the table of shell builtins so that the binary search will work
in find_shell_builtin. */
void
initialize_shell_builtins ()
{
qsort (shell_builtins, num_shell_builtins, sizeof (struct builtin),
(QSFUNC *)shell_builtin_compare);
}
+18
View File
@@ -24,6 +24,21 @@
#include "stdc.h"
#define ISOPTION(s, c) (s[0] == '-' && !s[2] && s[1] == c)
#define ISHELP(s) (STREQ ((s), "--help"))
#define CHECK_HELPOPT(l) \
do { \
if ((l) && (l)->word && ISHELP((l)->word->word)) \
{ \
builtin_help (); \
return (EX_USAGE); \
} \
} while (0)
#define CASE_HELPOPT() \
case GETOPT_HELP: \
builtin_help (); \
return (EX_USAGE)
/* Flag values for parse_and_execute () */
#define SEVAL_NONINT 0x001
@@ -120,6 +135,9 @@ extern void bash_logout __P((void));
/* Functions from getopts.def */
extern void getopts_reset __P((int));
/* Functions from help.def */
extern void builtin_help __P((void));
/* Functions from set.def */
extern int minus_o_option_value __P((char *));
extern void list_minus_o_opts __P((int, int));
+191
View File
@@ -0,0 +1,191 @@
/* common.h -- extern declarations for functions defined in common.c. */
/* Copyright (C) 1993-2010 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/
#if !defined (__COMMON_H)
# define __COMMON_H
#include "stdc.h"
#define ISOPTION(s, c) (s[0] == '-' && !s[2] && s[1] == c)
#define ISHELP(s) (STREQ ((s), "--help"))
#define CHECK_HELPOPT(l) \
do { \
if ((l) && (l)->word && ISHELP((l)->word->word)) \
{ \
builtin_help (); \
return (EX_USAGE); \
} \
} while (0)
/* Flag values for parse_and_execute () */
#define SEVAL_NONINT 0x001
#define SEVAL_INTERACT 0x002
#define SEVAL_NOHIST 0x004
#define SEVAL_NOFREE 0x008
#define SEVAL_RESETLINE 0x010
#define SEVAL_PARSEONLY 0x020
#define SEVAL_NOLONGJMP 0x040
/* Flags for describe_command, shared between type.def and command.def */
#define CDESC_ALL 0x001 /* type -a */
#define CDESC_SHORTDESC 0x002 /* command -V */
#define CDESC_REUSABLE 0x004 /* command -v */
#define CDESC_TYPE 0x008 /* type -t */
#define CDESC_PATH_ONLY 0x010 /* type -p */
#define CDESC_FORCE_PATH 0x020 /* type -ap or type -P */
#define CDESC_NOFUNCS 0x040 /* type -f */
#define CDESC_ABSPATH 0x080 /* convert to absolute path, no ./ */
/* Flags for get_job_by_name */
#define JM_PREFIX 0x01 /* prefix of job name */
#define JM_SUBSTRING 0x02 /* substring of job name */
#define JM_EXACT 0x04 /* match job name exactly */
#define JM_STOPPED 0x08 /* match stopped jobs only */
#define JM_FIRSTMATCH 0x10 /* return first matching job */
/* Flags for remember_args and value of changed_dollar_vars */
#define ARGS_NONE 0x0
#define ARGS_INVOC 0x01
#define ARGS_FUNC 0x02
#define ARGS_SETBLTIN 0x04
/* Functions from common.c */
extern void builtin_error __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
extern void builtin_warning __P((const char *, ...)) __attribute__((__format__ (printf, 1, 2)));
extern void builtin_usage __P((void));
extern void no_args __P((WORD_LIST *));
extern int no_options __P((WORD_LIST *));
/* common error message functions */
extern void sh_needarg __P((char *));
extern void sh_neednumarg __P((char *));
extern void sh_notfound __P((char *));
extern void sh_invalidopt __P((char *));
extern void sh_invalidoptname __P((char *));
extern void sh_invalidid __P((char *));
extern void sh_invalidnum __P((char *));
extern void sh_invalidsig __P((char *));
extern void sh_erange __P((char *, char *));
extern void sh_badpid __P((char *));
extern void sh_badjob __P((char *));
extern void sh_readonly __P((const char *));
extern void sh_nojobs __P((char *));
extern void sh_restricted __P((char *));
extern void sh_notbuiltin __P((char *));
extern void sh_wrerror __P((void));
extern void sh_ttyerror __P((int));
extern int sh_chkwrite __P((int));
extern char **make_builtin_argv __P((WORD_LIST *, int *));
extern void remember_args __P((WORD_LIST *, int));
extern int dollar_vars_changed __P((void));
extern void set_dollar_vars_unchanged __P((void));
extern void set_dollar_vars_changed __P((void));
extern int get_numeric_arg __P((WORD_LIST *, int, intmax_t *));
extern int get_exitstat __P((WORD_LIST *));
extern int read_octal __P((char *));
/* Keeps track of the current working directory. */
extern char *the_current_working_directory;
extern char *get_working_directory __P((char *));
extern void set_working_directory __P((char *));
#if defined (JOB_CONTROL)
extern int get_job_by_name __P((const char *, int));
extern int get_job_spec __P((WORD_LIST *));
#endif
extern int display_signal_list __P((WORD_LIST *, int));
/* It's OK to declare a function as returning a Function * without
providing a definition of what a `Function' is. */
extern struct builtin *builtin_address_internal __P((char *, int));
extern sh_builtin_func_t *find_shell_builtin __P((char *));
extern sh_builtin_func_t *builtin_address __P((char *));
extern sh_builtin_func_t *find_special_builtin __P((char *));
extern void initialize_shell_builtins __P((void));
/* Functions from exit.def */
extern void bash_logout __P((void));
/* Functions from getopts.def */
extern void getopts_reset __P((int));
/* Functions from help.def */
extern void builtin_help __P((void));
/* Functions from set.def */
extern int minus_o_option_value __P((char *));
extern void list_minus_o_opts __P((int, int));
extern char **get_minus_o_opts __P((void));
extern int set_minus_o_option __P((int, char *));
extern void set_shellopts __P((void));
extern void parse_shellopts __P((char *));
extern void initialize_shell_options __P((int));
extern void reset_shell_options __P((void));
/* Functions from shopt.def */
extern void reset_shopt_options __P((void));
extern char **get_shopt_options __P((void));
extern int shopt_setopt __P((char *, int));
extern int shopt_listopt __P((char *, int));
extern int set_login_shell __P((char *, int));
extern void set_bashopts __P((void));
extern void parse_bashopts __P((char *));
extern void initialize_bashopts __P((int));
extern void set_compatibility_opts __P((void));
/* Functions from type.def */
extern int describe_command __P((char *, int));
/* Functions from setattr.def */
extern int set_or_show_attributes __P((WORD_LIST *, int, int));
extern int show_all_var_attributes __P((int, int));
extern int show_var_attributes __P((SHELL_VAR *, int, int));
extern int show_name_attributes __P((char *, int));
extern int show_func_attributes __P((char *, int));
extern void set_var_attribute __P((char *, int, int));
/* Functions from pushd.def */
extern char *get_dirstack_from_string __P((char *));
extern char *get_dirstack_element __P((intmax_t, int));
extern void set_dirstack_element __P((intmax_t, int, char *));
extern WORD_LIST *get_directory_stack __P((int));
/* Functions from evalstring.c */
extern int parse_and_execute __P((char *, const char *, int));
extern int evalstring __P((char *, const char *, int));
extern void parse_and_execute_cleanup __P((void));
extern int parse_string __P((char *, const char *, int, char **));
/* Functions from evalfile.c */
extern int maybe_execute_file __P((const char *, int));
extern int source_file __P((const char *, int));
extern int fc_execute_file __P((const char *));
#endif /* !__COMMON_H */
+2 -2
View File
@@ -1,7 +1,7 @@
This file is complete.def, from which is created complete.c.
It implements the builtins "complete", "compgen", and "compopt" in Bash.
Copyright (C) 1999-2011 Free Software Foundation, Inc.
Copyright (C) 1999-2014 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
@@ -790,7 +790,7 @@ compopt_builtin (list)
ret = EXECUTION_SUCCESS;
reset_internal_getopt ();
while ((opt = internal_getopt (list, "+o:DE")) != EOF)
while ((opt = internal_getopt (list, "+o:DE")) != -1)
{
opts = (list_opttype == '-') ? &opts_on : &opts_off;
+2 -2
View File
@@ -1,7 +1,7 @@
This file is declare.def, from which is created declare.c.
It implements the builtins "declare" and "local" in Bash.
Copyright (C) 1987-2012 Free Software Foundation, Inc.
Copyright (C) 1987-2014 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
@@ -150,7 +150,7 @@ declare_internal (list, local_var)
flags_on = flags_off = any_failed = assign_error = pflag = nodefs = mkglobal = 0;
refvar = (SHELL_VAR *)NULL;
reset_internal_getopt ();
while ((opt = internal_getopt (list, DECLARE_OPTS)) != EOF)
while ((opt = internal_getopt (list, DECLARE_OPTS)) != -1)
{
flags = list_opttype == '+' ? &flags_off : &flags_on;
+4
View File
@@ -60,6 +60,8 @@ int
exit_builtin (list)
WORD_LIST *list;
{
CHECK_HELPOPT (list);
if (interactive)
{
fprintf (stderr, login_shell ? _("logout\n") : "exit\n");
@@ -83,6 +85,8 @@ int
logout_builtin (list)
WORD_LIST *list;
{
CHECK_HELPOPT (list);
if (login_shell == 0 /* && interactive */)
{
builtin_error (_("not login shell: use `exit'"));
+168
View File
@@ -0,0 +1,168 @@
This file is exit.def, from which is created exit.c.
It implements the builtins "exit", and "logout" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES exit.c
$BUILTIN exit
$FUNCTION exit_builtin
$SHORT_DOC exit [n]
Exit the shell.
Exits the shell with a status of N. If N is omitted, the exit status
is that of the last command executed.
$END
#include <config.h>
#include "../bashtypes.h"
#include <stdio.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "../bashintl.h"
#include "../shell.h"
#include "../jobs.h"
#include "common.h"
#include "builtext.h" /* for jobs_builtin */
extern int check_jobs_at_exit;
extern int last_command_exit_value;
extern int running_trap, trap_saved_exit_value;
extern int subshell_environment;
extern sh_builtin_func_t *this_shell_builtin;
extern sh_builtin_func_t *last_shell_builtin;
static int exit_or_logout __P((WORD_LIST *));
static int sourced_logout;
int
exit_builtin (list)
WORD_LIST *list;
{
if (interactive)
{
fprintf (stderr, login_shell ? _("logout\n") : "exit\n");
fflush (stderr);
}
return (exit_or_logout (list));
}
$BUILTIN logout
$FUNCTION logout_builtin
$SHORT_DOC logout [n]
Exit a login shell.
Exits a login shell with exit status N. Returns an error if not executed
in a login shell.
$END
/* How to logout. */
int
logout_builtin (list)
WORD_LIST *list;
{
if (login_shell == 0 /* && interactive */)
{
builtin_error (_("not login shell: use `exit'"));
return (EXECUTION_FAILURE);
}
else
return (exit_or_logout (list));
}
static int
exit_or_logout (list)
WORD_LIST *list;
{
int exit_value;
#if defined (JOB_CONTROL)
int exit_immediate_okay, stopmsg;
exit_immediate_okay = (interactive == 0 ||
last_shell_builtin == exit_builtin ||
last_shell_builtin == logout_builtin ||
last_shell_builtin == jobs_builtin);
/* Check for stopped jobs if the user wants to. */
if (exit_immediate_okay == 0)
{
register int i;
for (i = stopmsg = 0; i < js.j_jobslots; i++)
if (jobs[i] && STOPPED (i))
stopmsg = JSTOPPED;
else if (check_jobs_at_exit && stopmsg == 0 && jobs[i] && RUNNING (i))
stopmsg = JRUNNING;
if (stopmsg == JSTOPPED)
fprintf (stderr, _("There are stopped jobs.\n"));
else if (stopmsg == JRUNNING)
fprintf (stderr, _("There are running jobs.\n"));
if (stopmsg && check_jobs_at_exit)
list_all_jobs (JLIST_STANDARD);
if (stopmsg)
{
/* This is NOT superfluous because EOF can get here without
going through the command parser. Set both last and this
so that either `exit', `logout', or ^D will work to exit
immediately if nothing intervenes. */
this_shell_builtin = last_shell_builtin = exit_builtin;
return (EXECUTION_FAILURE);
}
}
#endif /* JOB_CONTROL */
/* Get return value if present. This means that you can type
`logout 5' to a shell, and it returns 5. */
/* If we're running the exit trap (running_trap == 1, since running_trap
gets set to SIG+1), and we don't have a argument given to `exit'
(list == 0), use the exit status we saved before running the trap
commands (trap_saved_exit_value). */
exit_value = (running_trap == 1 && list == 0) ? trap_saved_exit_value : get_exitstat (list);
bash_logout ();
last_command_exit_value = exit_value;
/* Exit the program. */
jump_to_top_level (EXITPROG);
/*NOTREACHED*/
}
void
bash_logout ()
{
/* Run our `~/.bash_logout' file if it exists, and this is a login shell. */
if (login_shell && sourced_logout++ == 0 && subshell_environment == 0)
{
maybe_execute_file ("~/.bash_logout", 1);
#ifdef SYS_BASH_LOGOUT
maybe_execute_file (SYS_BASH_LOGOUT, 1);
#endif
}
}
+4
View File
@@ -63,6 +63,8 @@ fg_builtin (list)
int fg_bit;
register WORD_LIST *t;
CHECK_HELPOPT (list);
if (job_control == 0)
{
sh_nojobs ((char *)NULL);
@@ -105,6 +107,8 @@ bg_builtin (list)
{
int r;
CHECK_HELPOPT (list);
if (job_control == 0)
{
sh_nojobs ((char *)NULL);
+194
View File
@@ -0,0 +1,194 @@
This file is fg_bg.def, from which is created fg_bg.c.
It implements the builtins "bg" and "fg" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES fg_bg.c
$BUILTIN fg
$FUNCTION fg_builtin
$DEPENDS_ON JOB_CONTROL
$SHORT_DOC fg [job_spec]
Move job to the foreground.
Place the job identified by JOB_SPEC in the foreground, making it the
current job. If JOB_SPEC is not present, the shell's notion of the
current job is used.
Exit Status:
Status of command placed in foreground, or failure if an error occurs.
$END
#include <config.h>
#include "../bashtypes.h"
#include <signal.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "../bashintl.h"
#include "../shell.h"
#include "../jobs.h"
#include "common.h"
#include "bashgetopt.h"
#if defined (JOB_CONTROL)
extern char *this_command_name;
static int fg_bg __P((WORD_LIST *, int));
/* How to bring a job into the foreground. */
int
fg_builtin (list)
WORD_LIST *list;
{
int fg_bit;
register WORD_LIST *t;
CHECK_HELPOPT (list);
if (job_control == 0)
{
sh_nojobs ((char *)NULL);
return (EXECUTION_FAILURE);
}
if (no_options (list))
return (EX_USAGE);
list = loptend;
/* If the last arg on the line is '&', then start this job in the
background. Else, fg the job. */
for (t = list; t && t->next; t = t->next)
;
fg_bit = (t && t->word->word[0] == '&' && t->word->word[1] == '\0') == 0;
return (fg_bg (list, fg_bit));
}
#endif /* JOB_CONTROL */
$BUILTIN bg
$FUNCTION bg_builtin
$DEPENDS_ON JOB_CONTROL
$SHORT_DOC bg [job_spec ...]
Move jobs to the background.
Place the jobs identified by each JOB_SPEC in the background, as if they
had been started with `&'. If JOB_SPEC is not present, the shell's notion
of the current job is used.
Exit Status:
Returns success unless job control is not enabled or an error occurs.
$END
#if defined (JOB_CONTROL)
/* How to put a job into the background. */
int
bg_builtin (list)
WORD_LIST *list;
{
int r;
if (list && list->word && ISHELP (list->word->word))
{
builtin_help ();
return (EX_USAGE);
}
if (job_control == 0)
{
sh_nojobs ((char *)NULL);
return (EXECUTION_FAILURE);
}
if (no_options (list))
return (EX_USAGE);
list = loptend;
/* This relies on the fact that fg_bg() takes a WORD_LIST *, but only acts
on the first member (if any) of that list. */
r = EXECUTION_SUCCESS;
do
{
if (fg_bg (list, 0) == EXECUTION_FAILURE)
r = EXECUTION_FAILURE;
if (list)
list = list->next;
}
while (list);
return r;
}
/* How to put a job into the foreground/background. */
static int
fg_bg (list, foreground)
WORD_LIST *list;
int foreground;
{
sigset_t set, oset;
int job, status, old_async_pid;
JOB *j;
BLOCK_CHILD (set, oset);
job = get_job_spec (list);
if (INVALID_JOB (job))
{
if (job != DUP_JOB)
sh_badjob (list ? list->word->word : _("current"));
goto failure;
}
j = get_job_by_jid (job);
/* Or if j->pgrp == shell_pgrp. */
if (IS_JOBCONTROL (job) == 0)
{
builtin_error (_("job %d started without job control"), job + 1);
goto failure;
}
if (foreground == 0)
{
old_async_pid = last_asynchronous_pid;
last_asynchronous_pid = j->pgrp; /* As per Posix.2 5.4.2 */
}
status = start_job (job, foreground);
if (status >= 0)
{
/* win: */
UNBLOCK_CHILD (oset);
return (foreground ? status : EXECUTION_SUCCESS);
}
else
{
if (foreground == 0)
last_asynchronous_pid = old_async_pid;
failure:
UNBLOCK_CHILD (oset);
return (EXECUTION_FAILURE);
}
}
#endif /* JOB_CONTROL */
+68
View File
@@ -0,0 +1,68 @@
/* getopt.h - declarations for getopt. */
/* Copyright (C) 1989, 1990, 1991, 1992, 1993, 2008,2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/
/* XXX THIS HAS BEEN MODIFIED FOR INCORPORATION INTO BASH XXX */
#ifndef _SH_GETOPT_H
#define _SH_GETOPT_H 1
#include "stdc.h"
#define GETOPT_EOF -1
#define GETOPT_HELP -99
/* For communication from `getopt' to the caller.
When `getopt' finds an option that takes an argument,
the argument value is returned here.
Also, when `ordering' is RETURN_IN_ORDER,
each non-option ARGV-element is returned here. */
extern char *sh_optarg;
/* Index in ARGV of the next element to be scanned.
This is used for communication to and from the caller
and for communication between successive calls to `getopt'.
On entry to `getopt', zero means this is the first call; initialize.
When `getopt' returns EOF, this is the index of the first of the
non-option elements that the caller should itself scan.
Otherwise, `sh_optind' communicates from one call to the next
how much of ARGV has been scanned so far. */
extern int sh_optind;
/* Callers store zero here to inhibit the error message `getopt' prints
for unrecognized options. */
extern int sh_opterr;
/* Set to an option character which was unrecognized. */
extern int sh_optopt;
/* Set to 1 when an unrecognized option is encountered. */
extern int sh_badopt;
extern int sh_getopt __P((int, char *const *, const char *));
extern void sh_getopt_restore_state __P((char **));
#endif /* _SH_GETOPT_H */
+26
View File
@@ -58,6 +58,7 @@ $END
#include <errno.h>
#include <filecntl.h>
#include <stddef.h>
#include "../bashintl.h"
@@ -77,6 +78,9 @@ extern int errno;
extern const char * const bash_copyright;
extern const char * const bash_license;
extern char *this_command_name;
extern struct builtin *current_builtin;
static void show_builtin_command_help __P((void));
static int open_helpfile __P((char *));
static void show_desc __P((char *, int));
@@ -187,6 +191,28 @@ help_builtin (list)
return (EXECUTION_SUCCESS);
}
void
builtin_help ()
{
int ind;
ptrdiff_t d;
current_builtin = builtin_address_internal (this_command_name, 0);
if (current_builtin == 0)
return;
d = current_builtin - shell_builtins;
#if defined (__STDC__)
ind = (int)d;
#else
ind = (int)d / sizeof (struct builtin);
#endif
printf ("%s: %s\n", this_command_name, _(shell_builtins[ind].short_doc));
show_longdoc (ind);
}
static int
open_helpfile (name)
char *name;
+544
View File
@@ -0,0 +1,544 @@
This file is help.def, from which is created help.c.
It implements the builtin "help" in Bash.
Copyright (C) 1987-2013 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES help.c
$BUILTIN help
$FUNCTION help_builtin
$DEPENDS_ON HELP_BUILTIN
$SHORT_DOC help [-dms] [pattern ...]
Display information about builtin commands.
Displays brief summaries of builtin commands. If PATTERN is
specified, gives detailed help on all commands matching PATTERN,
otherwise the list of help topics is printed.
Options:
-d output short description for each topic
-m display usage in pseudo-manpage format
-s output only a short usage synopsis for each topic matching
PATTERN
Arguments:
PATTERN Pattern specifiying a help topic
Exit Status:
Returns success unless PATTERN is not found or an invalid option is given.
$END
#include <config.h>
#if defined (HELP_BUILTIN)
#include <stdio.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include <errno.h>
#include <filecntl.h>
#include <stddef.h>
#include "../bashintl.h"
#include "../shell.h"
#include "../builtins.h"
#include "../pathexp.h"
#include "common.h"
#include "bashgetopt.h"
#include <glob/strmatch.h>
#include <glob/glob.h>
#ifndef errno
extern int errno;
#endif
extern const char * const bash_copyright;
extern const char * const bash_license;
extern char *this_command_name;
extern struct builtin *current_builtin;
static void show_builtin_command_help __P((void));
static int open_helpfile __P((char *));
static void show_desc __P((char *, int));
static void show_manpage __P((char *, int));
static void show_longdoc __P((int));
/* Print out a list of the known functions in the shell, and what they do.
If LIST is supplied, print out the list which matches for each pattern
specified. */
int
help_builtin (list)
WORD_LIST *list;
{
register int i;
char *pattern, *name;
int plen, match_found, sflag, dflag, mflag, m, pass, this_found;
dflag = sflag = mflag = 0;
reset_internal_getopt ();
while ((i = internal_getopt (list, "dms")) != -1)
{
switch (i)
{
case 'd':
dflag = 1;
break;
case 'm':
mflag = 1;
break;
case 's':
sflag = 1;
break;
default:
builtin_usage ();
return (EX_USAGE);
}
}
list = loptend;
if (list == 0)
{
show_shell_version (0);
show_builtin_command_help ();
return (EXECUTION_SUCCESS);
}
/* We should consider making `help bash' do something. */
if (glob_pattern_p (list->word->word))
{
printf (ngettext ("Shell commands matching keyword `", "Shell commands matching keywords `", (list->next ? 2 : 1)));
print_word_list (list, ", ");
printf ("'\n\n");
}
for (match_found = 0, pattern = ""; list; list = list->next)
{
pattern = list->word->word;
plen = strlen (pattern);
for (pass = 1, this_found = 0; pass < 3; pass++)
{
for (i = 0; name = shell_builtins[i].name; i++)
{
QUIT;
/* First pass: look for exact string or pattern matches.
Second pass: look for prefix matches like bash-4.2 */
if (pass == 1)
m = (strcmp (pattern, name) == 0) ||
(strmatch (pattern, name, FNMATCH_EXTFLAG) != FNM_NOMATCH);
else
m = strncmp (pattern, name, plen) == 0;
if (m)
{
this_found = 1;
match_found++;
if (dflag)
{
show_desc (name, i);
continue;
}
else if (mflag)
{
show_manpage (name, i);
continue;
}
printf ("%s: %s\n", name, _(shell_builtins[i].short_doc));
if (sflag == 0)
show_longdoc (i);
}
}
if (pass == 1 && this_found == 1)
break;
}
}
if (match_found == 0)
{
builtin_error (_("no help topics match `%s'. Try `help help' or `man -k %s' or `info %s'."), pattern, pattern, pattern);
return (EXECUTION_FAILURE);
}
fflush (stdout);
return (EXECUTION_SUCCESS);
}
void
builtin_help ()
{
int ind;
ptrdiff_t d;
itrace("builtin_help: this_command_name = %s", this_command_name);
current_builtin = builtin_address_internal (this_command_name, 0);
if (current_builtin == 0)
return;
d = current_builtin - shell_builtins;
itrace("builtin_help: current_builtin = %p diff = %d", current_builtin, (int)d);
ind = (int)d;
printf ("%s: %s\n", this_command_name, _(shell_builtins[ind].short_doc));
show_longdoc (ind);
}
static int
open_helpfile (name)
char *name;
{
int fd;
fd = open (name, O_RDONLY);
if (fd == -1)
{
builtin_error (_("%s: cannot open: %s"), name, strerror (errno));
return -1;
}
return fd;
}
/* By convention, enforced by mkbuiltins.c, if separate help files are being
used, the long_doc array contains one string -- the full pathname of the
help file for this builtin. */
static void
show_longdoc (i)
int i;
{
register int j;
char * const *doc;
int fd;
doc = shell_builtins[i].long_doc;
if (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL)
{
fd = open_helpfile (doc[0]);
if (fd < 0)
return;
zcatfd (fd, 1, doc[0]);
close (fd);
}
else if (doc)
for (j = 0; doc[j]; j++)
printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j]));
}
static void
show_desc (name, i)
char *name;
int i;
{
register int j;
char **doc, *line;
int fd, usefile;
doc = (char **)shell_builtins[i].long_doc;
usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL);
if (usefile)
{
fd = open_helpfile (doc[0]);
if (fd < 0)
return;
zmapfd (fd, &line, doc[0]);
close (fd);
}
else
line = doc ? doc[0] : (char *)NULL;
printf ("%s - ", name);
for (j = 0; line && line[j]; j++)
{
putchar (line[j]);
if (line[j] == '\n')
break;
}
fflush (stdout);
if (usefile)
free (line);
}
/* Print builtin help in pseudo-manpage format. */
static void
show_manpage (name, i)
char *name;
int i;
{
register int j;
char **doc, *line;
int fd, usefile;
doc = (char **)shell_builtins[i].long_doc;
usefile = (doc && doc[0] && *doc[0] == '/' && doc[1] == (char *)NULL);
if (usefile)
{
fd = open_helpfile (doc[0]);
if (fd < 0)
return;
zmapfd (fd, &line, doc[0]);
close (fd);
}
else
line = doc ? _(doc[0]) : (char *)NULL;
/* NAME */
printf ("NAME\n");
printf ("%*s%s - ", BASE_INDENT, " ", name);
for (j = 0; line && line[j]; j++)
{
putchar (line[j]);
if (line[j] == '\n')
break;
}
printf ("\n");
/* SYNOPSIS */
printf ("SYNOPSIS\n");
printf ("%*s%s\n\n", BASE_INDENT, " ", _(shell_builtins[i].short_doc));
/* DESCRIPTION */
printf ("DESCRIPTION\n");
if (usefile == 0)
{
for (j = 0; doc[j]; j++)
printf ("%*s%s\n", BASE_INDENT, " ", _(doc[j]));
}
else
{
for (j = 0; line && line[j]; j++)
{
putchar (line[j]);
if (line[j] == '\n')
printf ("%*s", BASE_INDENT, " ");
}
}
putchar ('\n');
/* SEE ALSO */
printf ("SEE ALSO\n");
printf ("%*sbash(1)\n\n", BASE_INDENT, " ");
/* IMPLEMENTATION */
printf ("IMPLEMENTATION\n");
printf ("%*s", BASE_INDENT, " ");
show_shell_version (0);
printf ("%*s", BASE_INDENT, " ");
printf ("%s\n", _(bash_copyright));
printf ("%*s", BASE_INDENT, " ");
printf ("%s\n", _(bash_license));
fflush (stdout);
if (usefile)
free (line);
}
static void
dispcolumn (i, buf, bufsize, width, height)
int i;
char *buf;
size_t bufsize;
int width, height;
{
int j;
int displen;
char *helpdoc;
/* first column */
helpdoc = _(shell_builtins[i].short_doc);
buf[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? ' ' : '*';
strncpy (buf + 1, helpdoc, width - 2);
buf[width - 2] = '>'; /* indicate truncation */
buf[width - 1] = '\0';
printf ("%s", buf);
if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins))
{
printf ("\n");
return;
}
displen = strlen (buf);
/* two spaces */
for (j = displen; j < width; j++)
putc (' ', stdout);
/* second column */
helpdoc = _(shell_builtins[i+height].short_doc);
buf[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*';
strncpy (buf + 1, helpdoc, width - 3);
buf[width - 3] = '>'; /* indicate truncation */
buf[width - 2] = '\0';
printf ("%s\n", buf);
}
#if defined (HANDLE_MULTIBYTE)
static void
wdispcolumn (i, buf, bufsize, width, height)
int i;
char *buf;
size_t bufsize;
int width, height;
{
int j;
int displen;
char *helpdoc;
wchar_t *wcstr;
size_t slen, n;
int wclen;
/* first column */
helpdoc = _(shell_builtins[i].short_doc);
wcstr = 0;
slen = mbstowcs ((wchar_t *)0, helpdoc, 0);
if (slen == -1)
{
dispcolumn (i, buf, bufsize, width, height);
return;
}
/* No bigger than the passed max width */
if (slen >= width)
slen = width - 2;
wcstr = (wchar_t *)xmalloc (sizeof (wchar_t) * (width + 2));
n = mbstowcs (wcstr+1, helpdoc, slen + 1);
wcstr[n+1] = L'\0';
/* Turn tabs and newlines into spaces for column display, since wcwidth
returns -1 for them */
for (j = 1; j < n; j++)
if (wcstr[j] == L'\n' || wcstr[j] == L'\t')
wcstr[j] = L' ';
displen = wcsnwidth (wcstr+1, slen, width - 2) + 1; /* +1 for ' ' or '*' */
wcstr[0] = (shell_builtins[i].flags & BUILTIN_ENABLED) ? L' ' : L'*';
/* This assumes each wide char takes up one column position when displayed */
wcstr[width - 2] = L'>'; /* indicate truncation */
wcstr[width - 1] = L'\0';
printf ("%ls", wcstr);
if (((i << 1) >= num_shell_builtins) || (i+height >= num_shell_builtins))
{
printf ("\n");
free (wcstr);
return;
}
/* at least one space */
for (j = displen; j < width; j++)
putc (' ', stdout);
/* second column */
helpdoc = _(shell_builtins[i+height].short_doc);
slen = mbstowcs ((wchar_t *)0, helpdoc, 0);
if (slen == -1)
{
/* for now */
printf ("%c%s\n", (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? ' ' : '*', helpdoc);
free (wcstr);
return;
}
/* Reuse wcstr since it is already width wide chars long */
if (slen >= width)
slen = width - 2;
n = mbstowcs (wcstr+1, helpdoc, slen + 1);
wcstr[n+1] = L'\0'; /* make sure null-terminated */
/* Turn tabs and newlines into spaces for column display */
for (j = 1; j < n; j++)
if (wcstr[j] == L'\n' || wcstr[j] == L'\t')
wcstr[j] = L' ';
displen = wcsnwidth (wcstr+1, slen, width - 2);
wcstr[0] = (shell_builtins[i+height].flags & BUILTIN_ENABLED) ? L' ' : L'*';
/* This assumes each wide char takes up one column position when displayed */
wcstr[width - 3] = L'>'; /* indicate truncation */
wcstr[width - 2] = L'\0';
printf ("%ls\n", wcstr);
free (wcstr);
}
#endif /* HANDLE_MULTIBYTE */
static void
show_builtin_command_help ()
{
int i, j;
int height, width;
char *t, blurb[128];
printf (
_("These shell commands are defined internally. Type `help' to see this list.\n\
Type `help name' to find out more about the function `name'.\n\
Use `info bash' to find out more about the shell in general.\n\
Use `man -k' or `info' to find out more about commands not in this list.\n\
\n\
A star (*) next to a name means that the command is disabled.\n\
\n"));
t = get_string_value ("COLUMNS");
width = (t && *t) ? atoi (t) : 80;
if (width <= 0)
width = 80;
width /= 2;
if (width > sizeof (blurb))
width = sizeof (blurb);
if (width <= 3)
width = 40;
height = (num_shell_builtins + 1) / 2; /* number of rows */
for (i = 0; i < height; i++)
{
QUIT;
#if defined (HANDLE_MULTIBYTE)
if (MB_CUR_MAX > 1)
wdispcolumn (i, blurb, sizeof (blurb), width, height);
else
#endif
dispcolumn (i, blurb, sizeof (blurb), width, height);
}
}
#endif /* HELP_BUILTIN */
+1
View File
@@ -96,6 +96,7 @@ kill_builtin (list)
builtin_usage ();
return (EX_USAGE);
}
CHECK_HELPOPT (list);
any_succeeded = listing = saw_signal = 0;
sig = SIGTERM;
+270
View File
@@ -0,0 +1,270 @@
This file is kill.def, from which is created kill.c.
It implements the builtin "kill" in Bash.
Copyright (C) 1987-2010 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES kill.c
$BUILTIN kill
$FUNCTION kill_builtin
$SHORT_DOC kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]
Send a signal to a job.
Send the processes identified by PID or JOBSPEC the signal named by
SIGSPEC or SIGNUM. If neither SIGSPEC nor SIGNUM is present, then
SIGTERM is assumed.
Options:
-s sig SIG is a signal name
-n sig SIG is a signal number
-l list the signal names; if arguments follow `-l' they are
assumed to be signal numbers for which names should be listed
Kill is a shell builtin for two reasons: it allows job IDs to be used
instead of process IDs, and allows processes to be killed if the limit
on processes that you can create is reached.
Exit Status:
Returns success unless an invalid option is given or an error occurs.
$END
#include <config.h>
#include <stdio.h>
#include <errno.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashansi.h"
#include "../bashintl.h"
#include <signal.h>
#include "../shell.h"
#include "../trap.h"
#include "../jobs.h"
#include "common.h"
/* Not all systems declare ERRNO in errno.h... and some systems #define it! */
#if !defined (errno)
extern int errno;
#endif /* !errno */
extern int posixly_correct;
static void kill_error __P((pid_t, int));
#if !defined (CONTINUE_AFTER_KILL_ERROR)
# define CONTINUE_OR_FAIL return (EXECUTION_FAILURE)
#else
# define CONTINUE_OR_FAIL goto continue_killing
#endif /* CONTINUE_AFTER_KILL_ERROR */
/* Here is the kill builtin. We only have it so that people can type
kill -KILL %1? No, if you fill up the process table this way you
can still kill some. */
int
kill_builtin (list)
WORD_LIST *list;
{
int sig, any_succeeded, listing, saw_signal, dflags;
char *sigspec, *word;
pid_t pid;
intmax_t pid_value;
if (list == 0)
{
builtin_usage ();
return (EX_USAGE);
}
any_succeeded = listing = saw_signal = 0;
sig = SIGTERM;
sigspec = "TERM";
dflags = DSIG_NOCASE | ((posixly_correct == 0) ? DSIG_SIGPREFIX : 0);
/* Process options. */
while (list)
{
word = list->word->word;
if (ISOPTION (word, 'l'))
{
listing++;
list = list->next;
}
else if (ISOPTION (word, 's') || ISOPTION (word, 'n'))
{
list = list->next;
if (list)
{
sigspec = list->word->word;
if (sigspec[0] == '0' && sigspec[1] == '\0')
sig = 0;
else
sig = decode_signal (sigspec, dflags);
list = list->next;
saw_signal++;
}
else
{
sh_needarg (word);
return (EXECUTION_FAILURE);
}
}
else if (ISOPTION (word, '-'))
{
list = list->next;
break;
}
else if (ISHELP (word))
{
builtin_help ();
return (EX_USAGE);
}
else if (ISOPTION (word, '?'))
{
builtin_usage ();
return (EX_USAGE);
}
/* If this is a signal specification then process it. We only process
the first one seen; other arguments may signify process groups (e.g,
-num == process group num). */
else if (*word == '-' && saw_signal == 0)
{
sigspec = word + 1;
sig = decode_signal (sigspec, dflags);
saw_signal++;
list = list->next;
}
else
break;
}
if (listing)
return (display_signal_list (list, 0));
/* OK, we are killing processes. */
if (sig == NO_SIG)
{
sh_invalidsig (sigspec);
return (EXECUTION_FAILURE);
}
if (list == 0)
{
builtin_usage ();
return (EX_USAGE);
}
while (list)
{
word = list->word->word;
if (*word == '-')
word++;
/* Use the entire argument in case of minus sign presence. */
if (*word && legal_number (list->word->word, &pid_value) && (pid_value == (pid_t)pid_value))
{
pid = (pid_t) pid_value;
if (kill_pid (pid, sig, pid < -1) < 0)
{
if (errno == EINVAL)
sh_invalidsig (sigspec);
else
kill_error (pid, errno);
CONTINUE_OR_FAIL;
}
else
any_succeeded++;
}
#if defined (JOB_CONTROL)
else if (*list->word->word && *list->word->word != '%')
{
builtin_error (_("%s: arguments must be process or job IDs"), list->word->word);
CONTINUE_OR_FAIL;
}
else if (*word)
/* Posix.2 says you can kill without job control active (4.32.4) */
{ /* Must be a job spec. Check it out. */
int job;
sigset_t set, oset;
JOB *j;
BLOCK_CHILD (set, oset);
job = get_job_spec (list);
if (INVALID_JOB (job))
{
if (job != DUP_JOB)
sh_badjob (list->word->word);
UNBLOCK_CHILD (oset);
CONTINUE_OR_FAIL;
}
j = get_job_by_jid (job);
/* Job spec used. Kill the process group. If the job was started
without job control, then its pgrp == shell_pgrp, so we have
to be careful. We take the pid of the first job in the pipeline
in that case. */
pid = IS_JOBCONTROL (job) ? j->pgrp : j->pipe->pid;
UNBLOCK_CHILD (oset);
if (kill_pid (pid, sig, 1) < 0)
{
if (errno == EINVAL)
sh_invalidsig (sigspec);
else
kill_error (pid, errno);
CONTINUE_OR_FAIL;
}
else
any_succeeded++;
}
#endif /* !JOB_CONTROL */
else
{
sh_badpid (list->word->word);
CONTINUE_OR_FAIL;
}
continue_killing:
list = list->next;
}
return (any_succeeded ? EXECUTION_SUCCESS : EXECUTION_FAILURE);
}
static void
kill_error (pid, e)
pid_t pid;
int e;
{
char *x;
x = strerror (e);
if (x == 0)
x = _("Unknown error");
builtin_error ("(%ld) - %s", (long)pid, x);
}
+2
View File
@@ -86,6 +86,8 @@ let_builtin (list)
intmax_t ret;
int expok;
CHECK_HELPOPT (list);
/* Skip over leading `--' argument. */
if (list && list->word && ISOPTION (list->word->word, '-'))
list = list->next;
+135
View File
@@ -0,0 +1,135 @@
This file is let.def, from which is created let.c.
It implements the builtin "let" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$BUILTIN let
$FUNCTION let_builtin
$PRODUCES let.c
$SHORT_DOC let arg [arg ...]
Evaluate arithmetic expressions.
Evaluate each ARG as an arithmetic expression. Evaluation is done in
fixed-width integers with no check for overflow, though division by 0
is trapped and flagged as an error. The following list of operators is
grouped into levels of equal-precedence operators. The levels are listed
in order of decreasing precedence.
id++, id-- variable post-increment, post-decrement
++id, --id variable pre-increment, pre-decrement
-, + unary minus, plus
!, ~ logical and bitwise negation
** exponentiation
*, /, % multiplication, division, remainder
+, - addition, subtraction
<<, >> left and right bitwise shifts
<=, >=, <, > comparison
==, != equality, inequality
& bitwise AND
^ bitwise XOR
| bitwise OR
&& logical AND
|| logical OR
expr ? expr : expr
conditional operator
=, *=, /=, %=,
+=, -=, <<=, >>=,
&=, ^=, |= assignment
Shell variables are allowed as operands. The name of the variable
is replaced by its value (coerced to a fixed-width integer) within
an expression. The variable need not have its integer attribute
turned on to be used in an expression.
Operators are evaluated in order of precedence. Sub-expressions in
parentheses are evaluated first and may override the precedence
rules above.
Exit Status:
If the last ARG evaluates to 0, let returns 1; let returns 0 otherwise.
$END
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
/* Arithmetic LET function. */
int
let_builtin (list)
WORD_LIST *list;
{
intmax_t ret;
int expok;
if (list && list->word && ISHELP (list->word->word))
{
builtin_help ();
return (EX_USAGE);
}
/* Skip over leading `--' argument. */
if (list && list->word && ISOPTION (list->word->word, '-'))
list = list->next;
if (list == 0)
{
builtin_error (_("expression expected"));
return (EXECUTION_FAILURE);
}
for (; list; list = list->next)
{
ret = evalexp (list->word->word, &expok);
if (expok == 0)
return (EXECUTION_FAILURE);
}
return ((ret == 0) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}
#ifdef INCLUDE_UNUSED
int
exp_builtin (list)
WORD_LIST *list;
{
char *exp;
intmax_t ret;
int expok;
if (list == 0)
{
builtin_error (_("expression expected"));
return (EXECUTION_FAILURE);
}
exp = string_list (list);
ret = evalexp (exp, &expok);
(void)free (exp);
return (((ret == 0) || (expok == 0)) ? EXECUTION_FAILURE : EXECUTION_SUCCESS);
}
#endif
+5
View File
@@ -178,6 +178,8 @@ pushd_builtin (list)
char direction;
orig_list = list;
CHECK_HELPOPT (list);
if (list && list->word && ISOPTION (list->word->word, '-'))
{
list = list->next;
@@ -321,6 +323,8 @@ popd_builtin (list)
char direction;
char *which_word;
CHECK_HELPOPT (list);
which_word = (char *)NULL;
for (flags = 0, which = 0, direction = '+'; list; list = list->next)
{
@@ -402,6 +406,7 @@ dirs_builtin (list)
intmax_t i;
char *temp, *w;
CHECK_HELPOPT (list);
for (flags = vflag = index_flag = 0, desired_index = -1, w = ""; list; list = list->next)
{
if (ISOPTION (list->word->word, 'l'))
+793
View File
@@ -0,0 +1,793 @@
This file is pushd.def, from which is created pushd.c. It implements the
builtins "pushd", "popd", and "dirs" in Bash.
Copyright (C) 1987-2013 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES pushd.c
$BUILTIN pushd
$FUNCTION pushd_builtin
$DEPENDS_ON PUSHD_AND_POPD
$SHORT_DOC pushd [-n] [+N | -N | dir]
Add directories to stack.
Adds a directory to the top of the directory stack, or rotates
the stack, making the new top of the stack the current working
directory. With no arguments, exchanges the top two directories.
Options:
-n Suppresses the normal change of directory when adding
directories to the stack, so only the stack is manipulated.
Arguments:
+N Rotates the stack so that the Nth directory (counting
from the left of the list shown by `dirs', starting with
zero) is at the top.
-N Rotates the stack so that the Nth directory (counting
from the right of the list shown by `dirs', starting with
zero) is at the top.
dir Adds DIR to the directory stack at the top, making it the
new current working directory.
The `dirs' builtin displays the directory stack.
Exit Status:
Returns success unless an invalid argument is supplied or the directory
change fails.
$END
$BUILTIN popd
$FUNCTION popd_builtin
$DEPENDS_ON PUSHD_AND_POPD
$SHORT_DOC popd [-n] [+N | -N]
Remove directories from stack.
Removes entries from the directory stack. With no arguments, removes
the top directory from the stack, and changes to the new top directory.
Options:
-n Suppresses the normal change of directory when removing
directories from the stack, so only the stack is manipulated.
Arguments:
+N Removes the Nth entry counting from the left of the list
shown by `dirs', starting with zero. For example: `popd +0'
removes the first directory, `popd +1' the second.
-N Removes the Nth entry counting from the right of the list
shown by `dirs', starting with zero. For example: `popd -0'
removes the last directory, `popd -1' the next to last.
The `dirs' builtin displays the directory stack.
Exit Status:
Returns success unless an invalid argument is supplied or the directory
change fails.
$END
$BUILTIN dirs
$FUNCTION dirs_builtin
$DEPENDS_ON PUSHD_AND_POPD
$SHORT_DOC dirs [-clpv] [+N] [-N]
Display directory stack.
Display the list of currently remembered directories. Directories
find their way onto the list with the `pushd' command; you can get
back up through the list with the `popd' command.
Options:
-c clear the directory stack by deleting all of the elements
-l do not print tilde-prefixed versions of directories relative
to your home directory
-p print the directory stack with one entry per line
-v print the directory stack with one entry per line prefixed
with its position in the stack
Arguments:
+N Displays the Nth entry counting from the left of the list shown by
dirs when invoked without options, starting with zero.
-N Displays the Nth entry counting from the right of the list shown by
dirs when invoked without options, starting with zero.
Exit Status:
Returns success unless an invalid option is supplied or an error occurs.
$END
#include <config.h>
#if defined (PUSHD_AND_POPD)
#include <stdio.h>
#if defined (HAVE_SYS_PARAM_H)
# include <sys/param.h>
#endif
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashansi.h"
#include "../bashintl.h"
#include <errno.h>
#include <tilde/tilde.h>
#include "../shell.h"
#include "maxpath.h"
#include "common.h"
#include "builtext.h"
#ifdef LOADABLE_BUILTIN
# include "builtins.h"
#endif
#if !defined (errno)
extern int errno;
#endif /* !errno */
/* The list of remembered directories. */
static char **pushd_directory_list = (char **)NULL;
/* Number of existing slots in this list. */
static int directory_list_size;
/* Offset to the end of the list. */
static int directory_list_offset;
static void pushd_error __P((int, char *));
static void clear_directory_stack __P((void));
static int cd_to_string __P((char *));
static int change_to_temp __P((char *));
static void add_dirstack_element __P((char *));
static int get_dirstack_index __P((intmax_t, int, int *));
#define NOCD 0x01
#define ROTATE 0x02
#define LONGFORM 0x04
#define CLEARSTAK 0x08
int
pushd_builtin (list)
WORD_LIST *list;
{
WORD_LIST *orig_list;
char *temp, *current_directory, *top;
int j, flags, skipopt;
intmax_t num;
char direction;
orig_list = list;
CHECK_HELPOPT (list);
if (list && list->word && ISOPTION (list->word->word, '-'))
{
list = list->next;
skipopt = 1;
}
else
skipopt = 0;
/* If there is no argument list then switch current and
top of list. */
if (list == 0)
{
if (directory_list_offset == 0)
{
builtin_error (_("no other directory"));
return (EXECUTION_FAILURE);
}
current_directory = get_working_directory ("pushd");
if (current_directory == 0)
return (EXECUTION_FAILURE);
j = directory_list_offset - 1;
temp = pushd_directory_list[j];
pushd_directory_list[j] = current_directory;
j = change_to_temp (temp);
free (temp);
return j;
}
for (flags = 0; skipopt == 0 && list; list = list->next)
{
if (ISOPTION (list->word->word, 'n'))
{
flags |= NOCD;
}
else if (ISOPTION (list->word->word, '-'))
{
list = list->next;
break;
}
else if (list->word->word[0] == '-' && list->word->word[1] == '\0')
/* Let `pushd -' work like it used to. */
break;
else if (((direction = list->word->word[0]) == '+') || direction == '-')
{
if (legal_number (list->word->word + 1, &num) == 0)
{
sh_invalidnum (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
if (direction == '-')
num = directory_list_offset - num;
if (num > directory_list_offset || num < 0)
{
pushd_error (directory_list_offset, list->word->word);
return (EXECUTION_FAILURE);
}
flags |= ROTATE;
}
else if (*list->word->word == '-')
{
sh_invalidopt (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
else
break;
}
if (flags & ROTATE)
{
/* Rotate the stack num times. Remember, the current
directory acts like it is part of the stack. */
temp = get_working_directory ("pushd");
if (num == 0)
{
j = ((flags & NOCD) == 0) ? change_to_temp (temp) : EXECUTION_SUCCESS;
free (temp);
return j;
}
do
{
top = pushd_directory_list[directory_list_offset - 1];
for (j = directory_list_offset - 2; j > -1; j--)
pushd_directory_list[j + 1] = pushd_directory_list[j];
pushd_directory_list[j + 1] = temp;
temp = top;
num--;
}
while (num);
j = ((flags & NOCD) == 0) ? change_to_temp (temp) : EXECUTION_SUCCESS;
free (temp);
return j;
}
if (list == 0)
return (EXECUTION_SUCCESS);
/* Change to the directory in list->word->word. Save the current
directory on the top of the stack. */
current_directory = get_working_directory ("pushd");
if (current_directory == 0)
return (EXECUTION_FAILURE);
j = ((flags & NOCD) == 0) ? cd_builtin (skipopt ? orig_list : list) : EXECUTION_SUCCESS;
if (j == EXECUTION_SUCCESS)
{
add_dirstack_element ((flags & NOCD) ? savestring (list->word->word) : current_directory);
dirs_builtin ((WORD_LIST *)NULL);
if (flags & NOCD)
free (current_directory);
return (EXECUTION_SUCCESS);
}
else
{
free (current_directory);
return (EXECUTION_FAILURE);
}
}
/* Pop the directory stack, and then change to the new top of the stack.
If LIST is non-null it should consist of a word +N or -N, which says
what element to delete from the stack. The default is the top one. */
int
popd_builtin (list)
WORD_LIST *list;
{
register int i;
intmax_t which;
int flags;
char direction;
char *which_word;
CHECK_HELPOPT (list);
which_word = (char *)NULL;
for (flags = 0, which = 0, direction = '+'; list; list = list->next)
{
if (ISOPTION (list->word->word, 'n'))
{
flags |= NOCD;
}
else if (ISOPTION (list->word->word, '-'))
{
list = list->next;
break;
}
else if (((direction = list->word->word[0]) == '+') || direction == '-')
{
if (legal_number (list->word->word + 1, &which) == 0)
{
sh_invalidnum (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
which_word = list->word->word;
}
else if (*list->word->word == '-')
{
sh_invalidopt (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
else if (*list->word->word)
{
builtin_error (_("%s: invalid argument"), list->word->word);
builtin_usage ();
return (EX_USAGE);
}
else
break;
}
if (which > directory_list_offset || (directory_list_offset == 0 && which == 0))
{
pushd_error (directory_list_offset, which_word ? which_word : "");
return (EXECUTION_FAILURE);
}
/* Handle case of no specification, or top of stack specification. */
if ((direction == '+' && which == 0) ||
(direction == '-' && which == directory_list_offset))
{
i = ((flags & NOCD) == 0) ? cd_to_string (pushd_directory_list[directory_list_offset - 1])
: EXECUTION_SUCCESS;
if (i != EXECUTION_SUCCESS)
return (i);
free (pushd_directory_list[--directory_list_offset]);
}
else
{
/* Since an offset other than the top directory was specified,
remove that directory from the list and shift the remainder
of the list into place. */
i = (direction == '+') ? directory_list_offset - which : which;
free (pushd_directory_list[i]);
directory_list_offset--;
/* Shift the remainder of the list into place. */
for (; i < directory_list_offset; i++)
pushd_directory_list[i] = pushd_directory_list[i + 1];
}
dirs_builtin ((WORD_LIST *)NULL);
return (EXECUTION_SUCCESS);
}
/* Print the current list of directories on the directory stack. */
int
dirs_builtin (list)
WORD_LIST *list;
{
int flags, desired_index, index_flag, vflag;
intmax_t i;
char *temp, *w;
if (list && list->word && ISHELP (list->word->word))
{
builtin_help ();
return (EX_USAGE);
}
for (flags = vflag = index_flag = 0, desired_index = -1, w = ""; list; list = list->next)
{
if (ISOPTION (list->word->word, 'l'))
{
flags |= LONGFORM;
}
else if (ISOPTION (list->word->word, 'c'))
{
flags |= CLEARSTAK;
}
else if (ISOPTION (list->word->word, 'v'))
{
vflag |= 2;
}
else if (ISOPTION (list->word->word, 'p'))
{
vflag |= 1;
}
else if (ISOPTION (list->word->word, '-'))
{
list = list->next;
break;
}
else if (*list->word->word == '+' || *list->word->word == '-')
{
int sign;
if (legal_number (w = list->word->word + 1, &i) == 0)
{
sh_invalidnum (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
sign = (*list->word->word == '+') ? 1 : -1;
desired_index = get_dirstack_index (i, sign, &index_flag);
}
else
{
sh_invalidopt (list->word->word);
builtin_usage ();
return (EX_USAGE);
}
}
if (flags & CLEARSTAK)
{
clear_directory_stack ();
return (EXECUTION_SUCCESS);
}
if (index_flag && (desired_index < 0 || desired_index > directory_list_offset))
{
pushd_error (directory_list_offset, w);
return (EXECUTION_FAILURE);
}
#define DIRSTACK_FORMAT(temp) \
(flags & LONGFORM) ? temp : polite_directory_format (temp)
/* The first directory printed is always the current working directory. */
if (index_flag == 0 || (index_flag == 1 && desired_index == 0))
{
temp = get_working_directory ("dirs");
if (temp == 0)
temp = savestring (_("<no current directory>"));
if (vflag & 2)
printf ("%2d %s", 0, DIRSTACK_FORMAT (temp));
else
printf ("%s", DIRSTACK_FORMAT (temp));
free (temp);
if (index_flag)
{
putchar ('\n');
return (sh_chkwrite (EXECUTION_SUCCESS));
}
}
#define DIRSTACK_ENTRY(i) \
(flags & LONGFORM) ? pushd_directory_list[i] \
: polite_directory_format (pushd_directory_list[i])
/* Now print the requested directory stack entries. */
if (index_flag)
{
if (vflag & 2)
printf ("%2d %s", directory_list_offset - desired_index,
DIRSTACK_ENTRY (desired_index));
else
printf ("%s", DIRSTACK_ENTRY (desired_index));
}
else
for (i = directory_list_offset - 1; i >= 0; i--)
if (vflag >= 2)
printf ("\n%2d %s", directory_list_offset - (int)i, DIRSTACK_ENTRY (i));
else
printf ("%s%s", (vflag & 1) ? "\n" : " ", DIRSTACK_ENTRY (i));
putchar ('\n');
return (sh_chkwrite (EXECUTION_SUCCESS));
}
static void
pushd_error (offset, arg)
int offset;
char *arg;
{
if (offset == 0)
builtin_error (_("directory stack empty"));
else
sh_erange (arg, _("directory stack index"));
}
static void
clear_directory_stack ()
{
register int i;
for (i = 0; i < directory_list_offset; i++)
free (pushd_directory_list[i]);
directory_list_offset = 0;
}
/* Switch to the directory in NAME. This uses the cd_builtin to do the work,
so if the result is EXECUTION_FAILURE then an error message has already
been printed. */
static int
cd_to_string (name)
char *name;
{
WORD_LIST *tlist;
WORD_LIST *dir;
int result;
dir = make_word_list (make_word (name), NULL);
tlist = make_word_list (make_word ("--"), dir);
result = cd_builtin (tlist);
dispose_words (tlist);
return (result);
}
static int
change_to_temp (temp)
char *temp;
{
int tt;
tt = temp ? cd_to_string (temp) : EXECUTION_FAILURE;
if (tt == EXECUTION_SUCCESS)
dirs_builtin ((WORD_LIST *)NULL);
return (tt);
}
static void
add_dirstack_element (dir)
char *dir;
{
if (directory_list_offset == directory_list_size)
pushd_directory_list = strvec_resize (pushd_directory_list, directory_list_size += 10);
pushd_directory_list[directory_list_offset++] = dir;
}
static int
get_dirstack_index (ind, sign, indexp)
intmax_t ind;
int sign, *indexp;
{
if (indexp)
*indexp = sign > 0 ? 1 : 2;
/* dirs +0 prints the current working directory. */
/* dirs -0 prints last element in directory stack */
if (ind == 0 && sign > 0)
return 0;
else if (ind == directory_list_offset)
{
if (indexp)
*indexp = sign > 0 ? 2 : 1;
return 0;
}
else if (ind >= 0 && ind <= directory_list_offset)
return (sign > 0 ? directory_list_offset - ind : ind);
else
return -1;
}
/* Used by the tilde expansion code. */
char *
get_dirstack_from_string (string)
char *string;
{
int ind, sign, index_flag;
intmax_t i;
sign = 1;
if (*string == '-' || *string == '+')
{
sign = (*string == '-') ? -1 : 1;
string++;
}
if (legal_number (string, &i) == 0)
return ((char *)NULL);
index_flag = 0;
ind = get_dirstack_index (i, sign, &index_flag);
if (index_flag && (ind < 0 || ind > directory_list_offset))
return ((char *)NULL);
if (index_flag == 0 || (index_flag == 1 && ind == 0))
return (get_string_value ("PWD"));
else
return (pushd_directory_list[ind]);
}
#ifdef INCLUDE_UNUSED
char *
get_dirstack_element (ind, sign)
intmax_t ind;
int sign;
{
int i;
i = get_dirstack_index (ind, sign, (int *)NULL);
return (i < 0 || i > directory_list_offset) ? (char *)NULL
: pushd_directory_list[i];
}
#endif
void
set_dirstack_element (ind, sign, value)
intmax_t ind;
int sign;
char *value;
{
int i;
i = get_dirstack_index (ind, sign, (int *)NULL);
if (ind == 0 || i < 0 || i > directory_list_offset)
return;
free (pushd_directory_list[i]);
pushd_directory_list[i] = savestring (value);
}
WORD_LIST *
get_directory_stack (flags)
int flags;
{
register int i;
WORD_LIST *ret;
char *d, *t;
for (ret = (WORD_LIST *)NULL, i = 0; i < directory_list_offset; i++)
{
d = (flags&1) ? polite_directory_format (pushd_directory_list[i])
: pushd_directory_list[i];
ret = make_word_list (make_word (d), ret);
}
/* Now the current directory. */
d = get_working_directory ("dirstack");
i = 0; /* sentinel to decide whether or not to free d */
if (d == 0)
d = ".";
else
{
t = polite_directory_format (d);
/* polite_directory_format sometimes returns its argument unchanged.
If it does not, we can free d right away. If it does, we need to
mark d to be deleted later. */
if (t != d)
{
free (d);
d = t;
}
else /* t == d, so d is what we want */
i = 1;
}
ret = make_word_list (make_word (d), ret);
if (i)
free (d);
return ret; /* was (REVERSE_LIST (ret, (WORD_LIST *)); */
}
#ifdef LOADABLE_BUILTIN
char * const dirs_doc[] = {
N_("Display the list of currently remembered directories. Directories\n\
find their way onto the list with the `pushd' command; you can get\n\
back up through the list with the `popd' command.\n\
\n\
Options:\n\
-c clear the directory stack by deleting all of the elements\n\
-l do not print tilde-prefixed versions of directories relative\n\
to your home directory\n\
-p print the directory stack with one entry per line\n\
-v print the directory stack with one entry per line prefixed\n\
with its position in the stack\n\
\n\
Arguments:\n\
+N Displays the Nth entry counting from the left of the list shown by\n\
dirs when invoked without options, starting with zero.\n\
\n\
-N Displays the Nth entry counting from the right of the list shown by\n\
dirs when invoked without options, starting with zero."),
(char *)NULL
};
char * const pushd_doc[] = {
N_("Adds a directory to the top of the directory stack, or rotates\n\
the stack, making the new top of the stack the current working\n\
directory. With no arguments, exchanges the top two directories.\n\
\n\
Options:\n\
-n Suppresses the normal change of directory when adding\n\
directories to the stack, so only the stack is manipulated.\n\
\n\
Arguments:\n\
+N Rotates the stack so that the Nth directory (counting\n\
from the left of the list shown by `dirs', starting with\n\
zero) is at the top.\n\
\n\
-N Rotates the stack so that the Nth directory (counting\n\
from the right of the list shown by `dirs', starting with\n\
zero) is at the top.\n\
\n\
dir Adds DIR to the directory stack at the top, making it the\n\
new current working directory.\n\
\n\
The `dirs' builtin displays the directory stack."),
(char *)NULL
};
char * const popd_doc[] = {
N_("Removes entries from the directory stack. With no arguments, removes\n\
the top directory from the stack, and changes to the new top directory.\n\
\n\
Options:\n\
-n Suppresses the normal change of directory when removing\n\
directories from the stack, so only the stack is manipulated.\n\
\n\
Arguments:\n\
+N Removes the Nth entry counting from the left of the list\n\
shown by `dirs', starting with zero. For example: `popd +0'\n\
removes the first directory, `popd +1' the second.\n\
\n\
-N Removes the Nth entry counting from the right of the list\n\
shown by `dirs', starting with zero. For example: `popd -0'\n\
removes the last directory, `popd -1' the next to last.\n\
\n\
The `dirs' builtin displays the directory stack."),
(char *)NULL
};
struct builtin pushd_struct = {
"pushd",
pushd_builtin,
BUILTIN_ENABLED,
pushd_doc,
"pushd [+N | -N] [-n] [dir]",
0
};
struct builtin popd_struct = {
"popd",
popd_builtin,
BUILTIN_ENABLED,
popd_doc,
"popd [+N | -N] [-n]",
0
};
struct builtin dirs_struct = {
"dirs",
dirs_builtin,
BUILTIN_ENABLED,
dirs_doc,
"dirs [-clpv] [+N] [-N]",
0
};
#endif /* LOADABLE_BUILTIN */
#endif /* PUSHD_AND_POPD */
+1 -5
View File
@@ -60,11 +60,7 @@ int
return_builtin (list)
WORD_LIST *list;
{
#if 0
if (no_options (list))
return (EX_USAGE);
list = loptend; /* skip over possible `--' */
#endif
CHECK_HELPOPT (list);
return_catch_value = get_exitstat (list);
+72
View File
@@ -0,0 +1,72 @@
This file is return.def, from which is created return.c.
It implements the builtin "return" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES return.c
$BUILTIN return
$FUNCTION return_builtin
$SHORT_DOC return [n]
Return from a shell function.
Causes a function or sourced script to exit with the return value
specified by N. If N is omitted, the return status is that of the
last command executed within the function or script.
Exit Status:
Returns N, or failure if the shell is not executing a function or script.
$END
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
#include "bashgetopt.h"
extern int last_command_exit_value;
extern int subshell_environment;
extern int return_catch_flag, return_catch_value;
/* If we are executing a user-defined function then exit with the value
specified as an argument. if no argument is given, then the last
exit status is used. */
int
return_builtin (list)
WORD_LIST *list;
{
return_catch_value = get_exitstat (list);
if (return_catch_flag)
longjmp (return_catch, 1);
else
{
builtin_error (_("can only `return' from a function or sourced script"));
return (EXECUTION_FAILURE);
}
}
+2
View File
@@ -61,6 +61,8 @@ shift_builtin (list)
register int count;
WORD_LIST *temp;
CHECK_HELPOPT (list);
if (get_numeric_arg (list, 0, &times) == 0)
return (EXECUTION_FAILURE);
+101
View File
@@ -0,0 +1,101 @@
This file is shift.def, from which is created shift.c.
It implements the builtin "shift" in Bash.
Copyright (C) 1987-2009 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
$PRODUCES shift.c
#include <config.h>
#if defined (HAVE_UNISTD_H)
# ifdef _MINIX
# include <sys/types.h>
# endif
# include <unistd.h>
#endif
#include "../bashansi.h"
#include "../bashintl.h"
#include "../shell.h"
#include "common.h"
$BUILTIN shift
$FUNCTION shift_builtin
$SHORT_DOC shift [n]
Shift positional parameters.
Rename the positional parameters $N+1,$N+2 ... to $1,$2 ... If N is
not given, it is assumed to be 1.
Exit Status:
Returns success unless N is negative or greater than $#.
$END
int print_shift_error;
/* Shift the arguments ``left''. Shift DOLLAR_VARS down then take one
off of REST_OF_ARGS and place it into DOLLAR_VARS[9]. If LIST has
anything in it, it is a number which says where to start the
shifting. Return > 0 if `times' > $#, otherwise 0. */
int
shift_builtin (list)
WORD_LIST *list;
{
intmax_t times;
register int count;
WORD_LIST *temp;
if (get_numeric_arg (list, 0, &times) == 0)
return (EXECUTION_FAILURE);
if (times == 0)
return (EXECUTION_SUCCESS);
else if (times < 0)
{
sh_erange (list ? list->word->word : NULL, _("shift count"));
return (EXECUTION_FAILURE);
}
else if (times > number_of_args ())
{
if (print_shift_error)
sh_erange (list ? list->word->word : NULL, _("shift count"));
return (EXECUTION_FAILURE);
}
while (times-- > 0)
{
if (dollar_vars[1])
free (dollar_vars[1]);
for (count = 1; count < 9; count++)
dollar_vars[count] = dollar_vars[count + 1];
if (rest_of_args)
{
temp = rest_of_args;
dollar_vars[9] = savestring (temp->word->word);
rest_of_args = rest_of_args->next;
temp->next = (WORD_LIST *)NULL;
dispose_words (temp);
}
else
dollar_vars[9] = (char *)NULL;
}
return (EXECUTION_SUCCESS);
}
+8
View File
@@ -143,3 +143,11 @@
/* Define to 1 if you want the shell to re-check $PATH if a hashed filename
no longer exists. This behavior is the default in Posix mode. */
#define CHECKHASH_DEFAULT 0
/* Define to the maximum level of recursion you want for the eval builtin.
0 means the limit is not active. */
#define EVALNEST_MAX 0
/* Define to the maximum level of recursion you want for the source/. builtin.
0 means the limit is not active. */
#define SOURCENEST_MAX 0
+5
View File
@@ -7245,6 +7245,11 @@ builtins.
@sc{posix} special builtins are found before shell functions
during command lookup.
@item
Literal tildes that appear as the first character in elements of
the @env{PATH} variable are not expanded as described above
under @ref{Tilde Expansion}.
@item
The @code{time} reserved word may be used by itself as a command. When
used in this way, it displays timing statistics for the shell and its
+8762
View File
File diff suppressed because it is too large Load Diff
+2 -2
View File
@@ -7,5 +7,5 @@ Copyright (C) 1988-2014 Free Software Foundation, Inc.
@set EDITION 4.3
@set VERSION 4.3
@set UPDATED 27 August 2014
@set UPDATED-MONTH August 2014
@set UPDATED 6 September 2014
@set UPDATED-MONTH September 2014
+11
View File
@@ -0,0 +1,11 @@
@ignore
Copyright (C) 1988-2014 Free Software Foundation, Inc.
@end ignore
@set LASTCHANGE Wed Aug 27 08:43:08 EDT 2014
@set EDITION 4.3
@set VERSION 4.3
@set UPDATED 27 August 2014
@set UPDATED-MONTH August 2014
+2 -2
View File
@@ -287,10 +287,10 @@ int funcnest = 0;
int funcnest_max = 0;
int evalnest = 0; /* bash-4.4/bash-5.0 */
int evalnest_max = 0;
int evalnest_max = EVALNEST_MAX;
int sourcenest = 0;
int sourcenest_max = 0;
int sourcenest_max = SOURCENEST_MAX;
volatile int from_return_trap = 0;
+1 -1
View File
@@ -478,7 +478,7 @@ find_in_path_element (name, path, flags, name_len, dotinfop)
int status;
char *full_path, *xpath;
xpath = (*path == '~') ? bash_tilde_expand (path, 0) : path;
xpath = (posixly_correct == 0 && *path == '~') ? bash_tilde_expand (path, 0) : path;
/* Remember the location of "." in the path, in all its forms
(as long as they begin with a `.', e.g. `./.') */
+623
View File
@@ -0,0 +1,623 @@
/* findcmd.c -- Functions to search for commands by name. */
/* Copyright (C) 1997-2012 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 3 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include <stdio.h>
#include "chartypes.h"
#include "bashtypes.h"
#if !defined (_MINIX) && defined (HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#include "filecntl.h"
#include "posixstat.h"
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <errno.h>
#include "bashansi.h"
#include "memalloc.h"
#include "shell.h"
#include "flags.h"
#include "hashlib.h"
#include "pathexp.h"
#include "hashcmd.h"
#include "findcmd.h" /* matching prototypes and declarations */
#if !defined (errno)
extern int errno;
#endif
extern int posixly_correct;
extern int last_command_exit_value;
/* Static functions defined and used in this file. */
static char *_find_user_command_internal __P((const char *, int));
static char *find_user_command_internal __P((const char *, int));
static char *find_user_command_in_path __P((const char *, char *, int));
static char *find_in_path_element __P((const char *, char *, int, int, struct stat *));
static char *find_absolute_program __P((const char *, int));
static char *get_next_path_element __P((char *, int *));
/* The file name which we would try to execute, except that it isn't
possible to execute it. This is the first file that matches the
name that we are looking for while we are searching $PATH for a
suitable one to execute. If we cannot find a suitable executable
file, then we use this one. */
static char *file_to_lose_on;
/* Non-zero if we should stat every command found in the hash table to
make sure it still exists. */
int check_hashed_filenames = CHECKHASH_DEFAULT;
/* DOT_FOUND_IN_SEARCH becomes non-zero when find_user_command ()
encounters a `.' as the directory pathname while scanning the
list of possible pathnames; i.e., if `.' comes before the directory
containing the file of interest. */
int dot_found_in_search = 0;
/* Return some flags based on information about this file.
The EXISTS bit is non-zero if the file is found.
The EXECABLE bit is non-zero the file is executble.
Zero is returned if the file is not found. */
int
file_status (name)
const char *name;
{
struct stat finfo;
int r;
/* Determine whether this file exists or not. */
if (stat (name, &finfo) < 0)
return (0);
/* If the file is a directory, then it is not "executable" in the
sense of the shell. */
if (S_ISDIR (finfo.st_mode))
return (FS_EXISTS|FS_DIRECTORY);
r = FS_EXISTS;
#if defined (HAVE_EACCESS)
/* Use eaccess(2) if we have it to take things like ACLs and other
file access mechanisms into account. eaccess uses the effective
user and group IDs, not the real ones. We could use sh_eaccess,
but we don't want any special treatment for /dev/fd. */
if (eaccess (name, X_OK) == 0)
r |= FS_EXECABLE;
if (eaccess (name, R_OK) == 0)
r |= FS_READABLE;
return r;
#elif defined (AFS)
/* We have to use access(2) to determine access because AFS does not
support Unix file system semantics. This may produce wrong
answers for non-AFS files when ruid != euid. I hate AFS. */
if (access (name, X_OK) == 0)
r |= FS_EXECABLE;
if (access (name, R_OK) == 0)
r |= FS_READABLE;
return r;
#else /* !HAVE_EACCESS && !AFS */
/* Find out if the file is actually executable. By definition, the
only other criteria is that the file has an execute bit set that
we can use. The same with whether or not a file is readable. */
/* Root only requires execute permission for any of owner, group or
others to be able to exec a file, and can read any file. */
if (current_user.euid == (uid_t)0)
{
r |= FS_READABLE;
if (finfo.st_mode & S_IXUGO)
r |= FS_EXECABLE;
return r;
}
/* If we are the owner of the file, the owner bits apply. */
if (current_user.euid == finfo.st_uid)
{
if (finfo.st_mode & S_IXUSR)
r |= FS_EXECABLE;
if (finfo.st_mode & S_IRUSR)
r |= FS_READABLE;
}
/* If we are in the owning group, the group permissions apply. */
else if (group_member (finfo.st_gid))
{
if (finfo.st_mode & S_IXGRP)
r |= FS_EXECABLE;
if (finfo.st_mode & S_IRGRP)
r |= FS_READABLE;
}
/* Else we check whether `others' have permission to execute the file */
else
{
if (finfo.st_mode & S_IXOTH)
r |= FS_EXECABLE;
if (finfo.st_mode & S_IROTH)
r |= FS_READABLE;
}
return r;
#endif /* !AFS */
}
/* Return non-zero if FILE exists and is executable.
Note that this function is the definition of what an
executable file is; do not change this unless YOU know
what an executable file is. */
int
executable_file (file)
const char *file;
{
int s;
s = file_status (file);
#if defined EISDIR
if (s & FS_DIRECTORY)
errno = EISDIR; /* let's see if we can improve error messages */
#endif
return ((s & FS_EXECABLE) && ((s & FS_DIRECTORY) == 0));
}
int
is_directory (file)
const char *file;
{
return (file_status (file) & FS_DIRECTORY);
}
int
executable_or_directory (file)
const char *file;
{
int s;
s = file_status (file);
return ((s & FS_EXECABLE) || (s & FS_DIRECTORY));
}
/* Locate the executable file referenced by NAME, searching along
the contents of the shell PATH variable. Return a new string
which is the full pathname to the file, or NULL if the file
couldn't be found. If a file is found that isn't executable,
and that is the only match, then return that. */
char *
find_user_command (name)
const char *name;
{
return (find_user_command_internal (name, FS_EXEC_PREFERRED|FS_NODIRS));
}
/* Locate the file referenced by NAME, searching along the contents
of the shell PATH variable. Return a new string which is the full
pathname to the file, or NULL if the file couldn't be found. This
returns the first readable file found; designed to be used to look
for shell scripts or files to source. */
char *
find_path_file (name)
const char *name;
{
return (find_user_command_internal (name, FS_READABLE));
}
static char *
_find_user_command_internal (name, flags)
const char *name;
int flags;
{
char *path_list, *cmd;
SHELL_VAR *var;
/* Search for the value of PATH in both the temporary environments and
in the regular list of variables. */
if (var = find_variable_tempenv ("PATH")) /* XXX could be array? */
path_list = value_cell (var);
else
path_list = (char *)NULL;
if (path_list == 0 || *path_list == '\0')
return (savestring (name));
cmd = find_user_command_in_path (name, path_list, flags);
return (cmd);
}
static char *
find_user_command_internal (name, flags)
const char *name;
int flags;
{
#ifdef __WIN32__
char *res, *dotexe;
dotexe = (char *)xmalloc (strlen (name) + 5);
strcpy (dotexe, name);
strcat (dotexe, ".exe");
res = _find_user_command_internal (dotexe, flags);
free (dotexe);
if (res == 0)
res = _find_user_command_internal (name, flags);
return res;
#else
return (_find_user_command_internal (name, flags));
#endif
}
/* Return the next element from PATH_LIST, a colon separated list of
paths. PATH_INDEX_POINTER is the address of an index into PATH_LIST;
the index is modified by this function.
Return the next element of PATH_LIST or NULL if there are no more. */
static char *
get_next_path_element (path_list, path_index_pointer)
char *path_list;
int *path_index_pointer;
{
char *path;
path = extract_colon_unit (path_list, path_index_pointer);
if (path == 0)
return (path);
if (*path == '\0')
{
free (path);
path = savestring (".");
}
return (path);
}
/* Look for PATHNAME in $PATH. Returns either the hashed command
corresponding to PATHNAME or the first instance of PATHNAME found
in $PATH. If (FLAGS&1) is non-zero, insert the instance of PATHNAME
found in $PATH into the command hash table. Returns a newly-allocated
string. */
char *
search_for_command (pathname, flags)
const char *pathname;
int flags;
{
char *hashed_file, *command;
int temp_path, st;
SHELL_VAR *path;
hashed_file = command = (char *)NULL;
/* If PATH is in the temporary environment for this command, don't use the
hash table to search for the full pathname. */
path = find_variable_tempenv ("PATH");
temp_path = path && tempvar_p (path);
if (temp_path == 0 && path)
path = (SHELL_VAR *)NULL;
/* Don't waste time trying to find hashed data for a pathname
that is already completely specified or if we're using a command-
specific value for PATH. */
if (path == 0 && absolute_program (pathname) == 0)
hashed_file = phash_search (pathname);
/* If a command found in the hash table no longer exists, we need to
look for it in $PATH. Thank you Posix.2. This forces us to stat
every command found in the hash table. */
if (hashed_file && (posixly_correct || check_hashed_filenames))
{
st = file_status (hashed_file);
if ((st & (FS_EXISTS|FS_EXECABLE)) != (FS_EXISTS|FS_EXECABLE))
{
phash_remove (pathname);
free (hashed_file);
hashed_file = (char *)NULL;
}
}
if (hashed_file)
command = hashed_file;
else if (absolute_program (pathname))
/* A command containing a slash is not looked up in PATH or saved in
the hash table. */
command = savestring (pathname);
else
{
/* If $PATH is in the temporary environment, we've already retrieved
it, so don't bother trying again. */
if (temp_path)
{
command = find_user_command_in_path (pathname, value_cell (path),
FS_EXEC_PREFERRED|FS_NODIRS);
}
else
command = find_user_command (pathname);
if (command && hashing_enabled && temp_path == 0 && (flags & 1))
phash_insert ((char *)pathname, command, dot_found_in_search, 1); /* XXX fix const later */
}
return (command);
}
char *
user_command_matches (name, flags, state)
const char *name;
int flags, state;
{
register int i;
int path_index, name_len;
char *path_list, *path_element, *match;
struct stat dotinfo;
static char **match_list = NULL;
static int match_list_size = 0;
static int match_index = 0;
if (state == 0)
{
/* Create the list of matches. */
if (match_list == 0)
{
match_list_size = 5;
match_list = strvec_create (match_list_size);
}
/* Clear out the old match list. */
for (i = 0; i < match_list_size; i++)
match_list[i] = 0;
/* We haven't found any files yet. */
match_index = 0;
if (absolute_program (name))
{
match_list[0] = find_absolute_program (name, flags);
match_list[1] = (char *)NULL;
path_list = (char *)NULL;
}
else
{
name_len = strlen (name);
file_to_lose_on = (char *)NULL;
dot_found_in_search = 0;
if (stat (".", &dotinfo) < 0)
dotinfo.st_dev = dotinfo.st_ino = 0; /* so same_file won't match */
path_list = get_string_value ("PATH");
path_index = 0;
}
while (path_list && path_list[path_index])
{
path_element = get_next_path_element (path_list, &path_index);
if (path_element == 0)
break;
match = find_in_path_element (name, path_element, flags, name_len, &dotinfo);
free (path_element);
if (match == 0)
continue;
if (match_index + 1 == match_list_size)
{
match_list_size += 10;
match_list = strvec_resize (match_list, (match_list_size + 1));
}
match_list[match_index++] = match;
match_list[match_index] = (char *)NULL;
FREE (file_to_lose_on);
file_to_lose_on = (char *)NULL;
}
/* We haven't returned any strings yet. */
match_index = 0;
}
match = match_list[match_index];
if (match)
match_index++;
return (match);
}
static char *
find_absolute_program (name, flags)
const char *name;
int flags;
{
int st;
st = file_status (name);
/* If the file doesn't exist, quit now. */
if ((st & FS_EXISTS) == 0)
return ((char *)NULL);
/* If we only care about whether the file exists or not, return
this filename. Otherwise, maybe we care about whether this
file is executable. If it is, and that is what we want, return it. */
if ((flags & FS_EXISTS) || ((flags & FS_EXEC_ONLY) && (st & FS_EXECABLE)))
return (savestring (name));
return (NULL);
}
static char *
find_in_path_element (name, path, flags, name_len, dotinfop)
const char *name;
char *path;
int flags, name_len;
struct stat *dotinfop;
{
int status;
char *full_path, *xpath;
xpath = (*path == '~') ? bash_tilde_expand (path, 0) : path;
/* Remember the location of "." in the path, in all its forms
(as long as they begin with a `.', e.g. `./.') */
if (dot_found_in_search == 0 && *xpath == '.')
dot_found_in_search = same_file (".", xpath, dotinfop, (struct stat *)NULL);
full_path = sh_makepath (xpath, name, 0);
status = file_status (full_path);
if (xpath != path)
free (xpath);
if ((status & FS_EXISTS) == 0)
{
free (full_path);
return ((char *)NULL);
}
/* The file exists. If the caller simply wants the first file, here it is. */
if (flags & FS_EXISTS)
return (full_path);
/* If we have a readable file, and the caller wants a readable file, this
is it. */
if ((flags & FS_READABLE) && (status & FS_READABLE))
return (full_path);
/* If the file is executable, then it satisfies the cases of
EXEC_ONLY and EXEC_PREFERRED. Return this file unconditionally. */
if ((status & FS_EXECABLE) && (flags & (FS_EXEC_ONLY|FS_EXEC_PREFERRED)) &&
(((flags & FS_NODIRS) == 0) || ((status & FS_DIRECTORY) == 0)))
{
FREE (file_to_lose_on);
file_to_lose_on = (char *)NULL;
return (full_path);
}
/* The file is not executable, but it does exist. If we prefer
an executable, then remember this one if it is the first one
we have found. */
if ((flags & FS_EXEC_PREFERRED) && file_to_lose_on == 0)
file_to_lose_on = savestring (full_path);
/* If we want only executable files, or we don't want directories and
this file is a directory, or we want a readable file and this file
isn't readable, fail. */
if ((flags & (FS_EXEC_ONLY|FS_EXEC_PREFERRED)) ||
((flags & FS_NODIRS) && (status & FS_DIRECTORY)) ||
((flags & FS_READABLE) && (status & FS_READABLE) == 0))
{
free (full_path);
return ((char *)NULL);
}
else
return (full_path);
}
/* This does the dirty work for find_user_command_internal () and
user_command_matches ().
NAME is the name of the file to search for.
PATH_LIST is a colon separated list of directories to search.
FLAGS contains bit fields which control the files which are eligible.
Some values are:
FS_EXEC_ONLY: The file must be an executable to be found.
FS_EXEC_PREFERRED: If we can't find an executable, then the
the first file matching NAME will do.
FS_EXISTS: The first file found will do.
FS_NODIRS: Don't find any directories.
*/
static char *
find_user_command_in_path (name, path_list, flags)
const char *name;
char *path_list;
int flags;
{
char *full_path, *path;
int path_index, name_len;
struct stat dotinfo;
/* We haven't started looking, so we certainly haven't seen
a `.' as the directory path yet. */
dot_found_in_search = 0;
if (absolute_program (name))
{
full_path = find_absolute_program (name, flags);
return (full_path);
}
if (path_list == 0 || *path_list == '\0')
return (savestring (name)); /* XXX */
file_to_lose_on = (char *)NULL;
name_len = strlen (name);
if (stat (".", &dotinfo) < 0)
dotinfo.st_dev = dotinfo.st_ino = 0;
path_index = 0;
while (path_list[path_index])
{
/* Allow the user to interrupt out of a lengthy path search. */
QUIT;
path = get_next_path_element (path_list, &path_index);
if (path == 0)
break;
/* Side effects: sets dot_found_in_search, possibly sets
file_to_lose_on. */
full_path = find_in_path_element (name, path, flags, name_len, &dotinfo);
free (path);
/* This should really be in find_in_path_element, but there isn't the
right combination of flags. */
if (full_path && is_directory (full_path))
{
free (full_path);
continue;
}
if (full_path)
{
FREE (file_to_lose_on);
return (full_path);
}
}
/* We didn't find exactly what the user was looking for. Return
the contents of FILE_TO_LOSE_ON which is NULL when the search
required an executable, or non-NULL if a file was found and the
search would accept a non-executable as a last resort. If the
caller specified FS_NODIRS, and file_to_lose_on is a directory,
return NULL. */
if (file_to_lose_on && (flags & FS_NODIRS) && is_directory (file_to_lose_on))
{
free (file_to_lose_on);
file_to_lose_on = (char *)NULL;
}
return (file_to_lose_on);
}
-1
View File
@@ -47,7 +47,6 @@
#define ASS_MKASSOC 0x0004
#define ASS_MKGLOBAL 0x0008 /* force global assignment */
#define ASS_NAMEREF 0x0010 /* assigning to nameref variable */
#define ASS_FROMREF 0x0020 /* assigning from value of nameref variable */
/* Flags for the string extraction functions. */
#define SX_NOALLOC 0x0001 /* just skip; don't return substring */
-1
View File
@@ -2687,7 +2687,6 @@ bind_variable (name, value, flags)
SHELL_VAR *v, *nv;
VAR_CONTEXT *vc, *nvc;
int level;
char *newname;
if (shell_variables == 0)
create_variable_tables ();