bash/bash-requires.patch

322 lines
9.9 KiB
Diff
Raw Normal View History

2017-01-06 13:41:12 +00:00
diff --git a/builtins.h b/builtins.h
index dac95fd..5b7e811 100644
2017-01-06 13:41:12 +00:00
--- a/builtins.h
+++ b/builtins.h
@@ -45,6 +45,7 @@
2009-12-27 11:22:38 +00:00
#define ASSIGNMENT_BUILTIN 0x10 /* This builtin takes assignment statements. */
#define POSIX_BUILTIN 0x20 /* This builtins is special in the Posix command search order. */
2017-01-06 13:41:12 +00:00
#define LOCALVAR_BUILTIN 0x40 /* This builtin creates local variables */
+#define REQUIRES_BUILTIN 0x80 /* This builtin requires other files. */
2009-12-27 11:22:38 +00:00
#define BASE_INDENT 4
2017-01-06 13:41:12 +00:00
diff --git a/builtins/mkbuiltins.c b/builtins/mkbuiltins.c
index 4f51201..91c25db 100644
2017-01-06 13:41:12 +00:00
--- a/builtins/mkbuiltins.c
+++ b/builtins/mkbuiltins.c
@@ -69,10 +69,15 @@ extern char *strcpy ();
2009-12-27 11:22:38 +00:00
#define whitespace(c) (((c) == ' ') || ((c) == '\t'))
/* Flag values that builtins can have. */
+/* These flags are for the C code generator,
2009-12-27 11:22:38 +00:00
+ the C which is produced (./builtin.c)
+ includes the flags definitions found
2009-12-27 11:22:38 +00:00
+ in ../builtins.h */
#define BUILTIN_FLAG_SPECIAL 0x01
#define BUILTIN_FLAG_ASSIGNMENT 0x02
2017-01-06 13:41:12 +00:00
#define BUILTIN_FLAG_LOCALVAR 0x04
#define BUILTIN_FLAG_POSIX_BUILTIN 0x08
+#define BUILTIN_FLAG_REQUIRES 0x10
2009-12-27 11:22:38 +00:00
#define BASE_INDENT 4
@@ -173,11 +178,19 @@ char *posix_builtins[] =
2009-12-27 11:22:38 +00:00
(char *)NULL
};
+/* The builtin commands that cause requirements on other files. */
+static char *requires_builtins[] =
+{
+ ".", "command", "exec", "source", "inlib",
+ (char *)NULL
+};
+
2009-12-27 11:22:38 +00:00
/* Forward declarations. */
static int is_special_builtin ();
static int is_assignment_builtin ();
2017-01-06 13:41:12 +00:00
static int is_localvar_builtin ();
2009-12-27 11:22:38 +00:00
static int is_posix_builtin ();
+static int is_requires_builtin ();
#if !defined (HAVE_RENAME)
static int rename ();
@@ -831,6 +844,8 @@ builtin_handler (self, defs, arg)
2017-01-06 13:41:12 +00:00
new->flags |= BUILTIN_FLAG_LOCALVAR;
2009-12-27 11:22:38 +00:00
if (is_posix_builtin (name))
new->flags |= BUILTIN_FLAG_POSIX_BUILTIN;
+ if (is_requires_builtin (name))
+ new->flags |= BUILTIN_FLAG_REQUIRES;
array_add ((char *)new, defs->builtins);
building_builtin = 1;
@@ -1250,12 +1265,13 @@ write_builtins (defs, structfile, externfile)
2009-12-27 11:22:38 +00:00
else
fprintf (structfile, "(sh_builtin_func_t *)0x0, ");
2017-01-06 13:41:12 +00:00
- fprintf (structfile, "%s%s%s%s%s, %s_doc,\n",
+ fprintf (structfile, "%s%s%s%s%s%s, %s_doc,\n",
2009-12-27 11:22:38 +00:00
"BUILTIN_ENABLED | STATIC_BUILTIN",
(builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
(builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
2017-01-06 13:41:12 +00:00
(builtin->flags & BUILTIN_FLAG_LOCALVAR) ? " | LOCALVAR_BUILTIN" : "",
2009-12-27 11:22:38 +00:00
(builtin->flags & BUILTIN_FLAG_POSIX_BUILTIN) ? " | POSIX_BUILTIN" : "",
+ (builtin->flags & BUILTIN_FLAG_REQUIRES) ? " | REQUIRES_BUILTIN" : "",
document_name (builtin));
2017-01-06 13:41:12 +00:00
/* Don't translate short document summaries that are identical
@@ -1645,6 +1661,13 @@ is_posix_builtin (name)
2009-12-27 11:22:38 +00:00
return (_find_in_table (name, posix_builtins));
}
+static int
+is_requires_builtin (name)
+ char *name;
+{
+ return (_find_in_table (name, requires_builtins));
+}
+
#if !defined (HAVE_RENAME)
static int
rename (from, to)
2017-01-06 13:41:12 +00:00
diff --git a/doc/bash.1 b/doc/bash.1
index d91f1fd..111a66d 100644
2017-01-06 13:41:12 +00:00
--- a/doc/bash.1
+++ b/doc/bash.1
@@ -239,6 +239,14 @@ The shell becomes restricted (see
.B "RESTRICTED SHELL"
below).
.TP
+.B \-\-rpm-requires
+Produce the list of files that are required for the
+shell script to run. This implies '-n' and is subject
+to the same limitations as compile time error checking checking;
+Command substitutions, Conditional expressions and
+.BR eval
+builtin are not parsed so some dependencies may be missed.
+.TP
.B \-\-verbose
2017-01-06 13:41:12 +00:00
Equivalent to \fB\-v\fP.
.TP
2017-01-06 13:41:12 +00:00
diff --git a/doc/bashref.texi b/doc/bashref.texi
index d33cd57..6fc4d18 100644
2017-01-06 13:41:12 +00:00
--- a/doc/bashref.texi
+++ b/doc/bashref.texi
@@ -6453,6 +6453,13 @@ standard. @xref{Bash POSIX Mode}, for a description of the Bash
2009-12-27 11:22:38 +00:00
@item --restricted
Make the shell a restricted shell (@pxref{The Restricted Shell}).
+@item --rpm-requires
+Produce the list of files that are required for the
2009-12-27 11:22:38 +00:00
+shell script to run. This implies '-n' and is subject
+to the same limitations as compile time error checking checking;
+Command substitutions, Conditional expressions and @command{eval}
+are not parsed so some dependencies may be missed.
2009-12-27 11:22:38 +00:00
+
@item --verbose
Equivalent to @option{-v}. Print shell input lines as they're read.
2017-01-06 13:41:12 +00:00
diff --git a/eval.c b/eval.c
index f02d6e4..76c1e8d 100644
2017-01-06 13:41:12 +00:00
--- a/eval.c
+++ b/eval.c
@@ -142,7 +142,8 @@ reader_loop ()
2009-12-27 11:22:38 +00:00
if (read_command () == 0)
{
- if (interactive_shell == 0 && read_but_dont_execute)
+
2009-12-27 11:22:38 +00:00
+ if (interactive_shell == 0 && (read_but_dont_execute && !rpm_requires))
{
last_command_exit_value = EXECUTION_SUCCESS;
dispose_command (global_command);
2017-01-06 13:41:12 +00:00
diff --git a/execute_cmd.c b/execute_cmd.c
index 8b3c83a..4eae19c 100644
2017-01-06 13:41:12 +00:00
--- a/execute_cmd.c
+++ b/execute_cmd.c
@@ -538,6 +538,8 @@ async_redirect_stdin ()
2009-12-27 11:22:38 +00:00
#define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
+extern int rpm_requires;
+
2014-02-27 14:09:51 +00:00
/* Execute the command passed in COMMAND, perhaps doing it asynchronously.
2009-12-27 11:22:38 +00:00
COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
ASYNCHROUNOUS, if non-zero, says to do this command in the background.
@@ -569,7 +571,13 @@ execute_command_internal (command, asynchronous, pipe_in, pipe_out,
2014-02-27 14:09:51 +00:00
2009-12-27 11:22:38 +00:00
if (breaking || continuing)
return (last_command_exit_value);
- if (command == 0 || read_but_dont_execute)
+ if (command == 0 || (read_but_dont_execute && !rpm_requires))
2010-03-30 11:46:15 +00:00
+ return (EXECUTION_SUCCESS);
2009-12-27 11:22:38 +00:00
+ if (rpm_requires && command->type == cm_function_def)
+ return last_command_exit_value =
+ execute_intern_function (command->value.Function_def->name,
2015-06-30 07:56:33 +00:00
+ command->value.Function_def);
2010-03-30 11:46:15 +00:00
+ if (read_but_dont_execute)
return (EXECUTION_SUCCESS);
2009-12-27 11:22:38 +00:00
2014-02-27 14:09:51 +00:00
QUIT;
@@ -2813,7 +2821,7 @@ execute_for_command (for_command)
save_line_number = line_number;
if (check_identifier (for_command->name, 1) == 0)
2009-12-27 11:22:38 +00:00
{
- if (posixly_correct && interactive_shell == 0)
+ if (posixly_correct && interactive_shell == 0 && rpm_requires == 0)
{
last_command_exit_value = EX_BADUSAGE;
jump_to_top_level (ERREXIT);
2017-01-06 13:41:12 +00:00
diff --git a/execute_cmd.h b/execute_cmd.h
index dc2f15e..506fff4 100644
2017-01-06 13:41:12 +00:00
--- a/execute_cmd.h
+++ b/execute_cmd.h
@@ -22,6 +22,9 @@
2009-12-27 11:22:38 +00:00
#define _EXECUTE_CMD_H_
#include "stdc.h"
+#include "variables.h"
+#include "command.h"
+
2009-12-27 11:22:38 +00:00
2017-01-06 13:41:12 +00:00
#if defined (ARRAY_VARS)
struct func_array_state
diff --git a/make_cmd.c b/make_cmd.c
index ecbbfd6..3d8bfa4 100644
2017-01-06 13:41:12 +00:00
--- a/make_cmd.c
+++ b/make_cmd.c
@@ -828,6 +828,27 @@ make_coproc_command (name, command)
2009-01-26 11:50:44 +00:00
return (make_command (cm_coproc, (SIMPLE_COM *)temp));
}
+static void
+output_requirement (deptype, filename)
+const char *deptype;
+char *filename;
+{
+ if (strchr(filename, '$') || (filename[0] != '/' && strchr(filename, '/')))
+ return;
+
+ /*
+ if the executable is called via variable substitution we can
+ not dermine what it is at compile time.
+
+ if the executable consists only of characters not in the
+ alphabet we do not consider it a dependency just an artifact
+ of shell parsing (ex "exec < ${infile}").
+ */
+
+ if (strpbrk(filename, alphabet_set))
+ printf ("%s(%s)\n", deptype, filename);
+}
+
/* Reverse the word list and redirection list in the simple command
has just been parsed. It seems simpler to do this here the one
time then by any other method that I can think of. */
@@ -845,6 +866,28 @@ clean_simple_command (command)
REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
}
+ if (rpm_requires && command->value.Simple->words)
+ {
+ char *cmd0;
+ char *cmd1;
+ struct builtin *b;
+
+ cmd0 = command->value.Simple->words->word->word;
+ b = builtin_address_internal (cmd0, 0);
+ cmd1 = 0;
+ if (command->value.Simple->words->next)
+ cmd1 = command->value.Simple->words->next->word->word;
+
+ if (b) {
+ if ( (b->flags & REQUIRES_BUILTIN) && cmd1)
+ output_requirement ("executable", cmd1);
+ } else {
+ if (!assignment(cmd0, 0))
+ output_requirement (find_function(cmd0) ? "function" : "executable", cmd0);
+ }
+ } /*rpm_requires*/
+
+
2009-12-27 11:22:38 +00:00
parser_state &= ~PST_REDIRLIST;
return (command);
}
2017-01-06 13:41:12 +00:00
diff --git a/shell.c b/shell.c
index a2b2a55..c5d99b3 100644
2017-01-06 13:41:12 +00:00
--- a/shell.c
+++ b/shell.c
@@ -193,6 +193,9 @@ int have_devfd = 0;
2009-12-27 11:22:38 +00:00
/* The name of the .(shell)rc file. */
2017-01-06 13:41:12 +00:00
static char *bashrc_file = DEFAULT_BASHRC;
2009-12-27 11:22:38 +00:00
+/* Non-zero if we are finding the scripts requirements. */
+int rpm_requires;
2009-01-26 11:50:44 +00:00
+
2009-12-27 11:22:38 +00:00
/* Non-zero means to act more like the Bourne shell on startup. */
static int act_like_sh;
2008-12-09 14:54:13 +00:00
@@ -259,6 +262,7 @@ static const struct {
2009-12-27 11:22:38 +00:00
{ "protected", Int, &protected_mode, (char **)0x0 },
2014-02-27 14:09:51 +00:00
#endif
2009-12-27 11:22:38 +00:00
{ "rcfile", Charp, (int *)0x0, &bashrc_file },
+ { "rpm-requires", Int, &rpm_requires, (char **)0x0 },
#if defined (RESTRICTED_SHELL)
{ "restricted", Int, &restricted, (char **)0x0 },
#endif
@@ -496,6 +500,12 @@ main (argc, argv, env)
2009-12-27 11:22:38 +00:00
if (dump_translatable_strings)
read_but_dont_execute = 1;
2008-12-09 14:54:13 +00:00
2009-12-27 11:22:38 +00:00
+ if (rpm_requires)
+ {
+ read_but_dont_execute = 1;
+ initialize_shell_builtins ();
+ }
2009-01-26 11:50:44 +00:00
+
2009-12-27 11:22:38 +00:00
if (running_setuid && privileged_mode == 0)
disable_priv_mode ();
diff --git a/shell.h b/shell.h
index 8072605..6c4149d 100644
--- a/shell.h
+++ b/shell.h
@@ -34,12 +34,15 @@
#include "maxpath.h"
#include "unwind_prot.h"
#include "dispose_cmd.h"
+#include "execute_cmd.h"
#include "make_cmd.h"
#include "ocache.h"
#include "subst.h"
#include "sig.h"
#include "pathnames.h"
#include "externs.h"
+#include "builtins.h"
+#include "builtins/common.h"
extern int EOF_Reached;
@@ -99,6 +102,9 @@ extern int interactive, interactive_shell;
extern int startup_state;
extern int reading_shell_script;
extern int shell_initialized;
+extern int rpm_requires;
+static char *alphabet_set = "abcdefghijklmnopqrstuvwxyz"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
extern int bash_argv_initialized;
extern int subshell_environment;
extern int current_command_number;
2017-01-06 13:41:12 +00:00
--
2.17.2
2017-01-06 13:41:12 +00:00