rcolebaugh / rpms / bash

Forked from rpms/bash 2 years ago
Clone

Blame SOURCES/bash-requires.patch

9014c4
diff --git a/builtins.h b/builtins.h
9014c4
index 0cfea18..a6ef958 100644
9014c4
--- a/builtins.h
9014c4
+++ b/builtins.h
9014c4
@@ -42,6 +42,7 @@
9014c4
 #define ASSIGNMENT_BUILTIN 0x10	/* This builtin takes assignment statements. */
9014c4
 #define POSIX_BUILTIN	0x20	/* This builtins is special in the Posix command search order. */
9014c4
 #define LOCALVAR_BUILTIN   0x40	/* This builtin creates local variables */
9014c4
+#define REQUIRES_BUILTIN 0x80  /* This builtin requires other files. */
9014c4
 
9014c4
 #define BASE_INDENT	4
9014c4
 
9014c4
diff --git a/builtins/mkbuiltins.c b/builtins/mkbuiltins.c
9014c4
index 4f51201..283bfea 100644
9014c4
--- a/builtins/mkbuiltins.c
9014c4
+++ b/builtins/mkbuiltins.c
9014c4
@@ -69,10 +69,15 @@ extern char *strcpy ();
9014c4
 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
9014c4
 
9014c4
 /* Flag values that builtins can have. */
9014c4
+/*  These flags are for the C code generator, 
9014c4
+    the C which is produced (./builtin.c)
9014c4
+    includes the flags definitions found 
9014c4
+    in ../builtins.h */
9014c4
 #define BUILTIN_FLAG_SPECIAL	0x01
9014c4
 #define BUILTIN_FLAG_ASSIGNMENT 0x02
9014c4
 #define BUILTIN_FLAG_LOCALVAR	0x04
9014c4
 #define BUILTIN_FLAG_POSIX_BUILTIN 0x08
9014c4
+#define BUILTIN_FLAG_REQUIRES  0x10
9014c4
 
9014c4
 #define BASE_INDENT	4
9014c4
 
9014c4
@@ -173,11 +178,20 @@ char *posix_builtins[] =
9014c4
   (char *)NULL
9014c4
 };
9014c4
 
9014c4
+/* The builtin commands that cause requirements on other files. */
9014c4
+static char *requires_builtins[] =
9014c4
+{
9014c4
+  ".", "command", "exec", "source", "inlib",
9014c4
+  (char *)NULL
9014c4
+};
9014c4
+
9014c4
+
9014c4
 /* Forward declarations. */
9014c4
 static int is_special_builtin ();
9014c4
 static int is_assignment_builtin ();
9014c4
 static int is_localvar_builtin ();
9014c4
 static int is_posix_builtin ();
9014c4
+static int is_requires_builtin ();
9014c4
 
9014c4
 #if !defined (HAVE_RENAME)
9014c4
 static int rename ();
9014c4
@@ -831,6 +845,9 @@ builtin_handler (self, defs, arg)
9014c4
     new->flags |= BUILTIN_FLAG_LOCALVAR;
9014c4
   if (is_posix_builtin (name))
9014c4
     new->flags |= BUILTIN_FLAG_POSIX_BUILTIN;
9014c4
+  if (is_requires_builtin (name))
9014c4
+    new->flags |= BUILTIN_FLAG_REQUIRES;
9014c4
+
9014c4
 
9014c4
   array_add ((char *)new, defs->builtins);
9014c4
   building_builtin = 1;
9014c4
@@ -1250,12 +1267,13 @@ write_builtins (defs, structfile, externfile)
9014c4
 		  else
9014c4
 		    fprintf (structfile, "(sh_builtin_func_t *)0x0, ");
9014c4
 
9014c4
-		  fprintf (structfile, "%s%s%s%s%s, %s_doc,\n",
9014c4
+		  fprintf (structfile, "%s%s%s%s%s%s, %s_doc,\n",
9014c4
 		    "BUILTIN_ENABLED | STATIC_BUILTIN",
9014c4
 		    (builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
9014c4
 		    (builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
9014c4
 		    (builtin->flags & BUILTIN_FLAG_LOCALVAR) ? " | LOCALVAR_BUILTIN" : "",
9014c4
 		    (builtin->flags & BUILTIN_FLAG_POSIX_BUILTIN) ? " | POSIX_BUILTIN" : "",
9014c4
+		    (builtin->flags & BUILTIN_FLAG_REQUIRES) ? " | REQUIRES_BUILTIN" : "",
9014c4
 		    document_name (builtin));
9014c4
 
9014c4
 		  /* Don't translate short document summaries that are identical
9014c4
@@ -1645,6 +1663,13 @@ is_posix_builtin (name)
9014c4
   return (_find_in_table (name, posix_builtins));
9014c4
 }
9014c4
 
9014c4
+static int
9014c4
+is_requires_builtin (name)
9014c4
+     char *name;
9014c4
+{
9014c4
+  return (_find_in_table (name, requires_builtins));
9014c4
+}
9014c4
+
9014c4
 #if !defined (HAVE_RENAME)
9014c4
 static int
9014c4
 rename (from, to)
9014c4
diff --git a/doc/bash.1 b/doc/bash.1
9014c4
index c21e877..04ce845 100644
9014c4
--- a/doc/bash.1
9014c4
+++ b/doc/bash.1
9014c4
@@ -238,6 +238,14 @@ The shell becomes restricted (see
9014c4
 .B "RESTRICTED SHELL"
9014c4
 below).
9014c4
 .TP
9014c4
+.B \-\-rpm-requires
9014c4
+Produce the list of files that are required for the 
9014c4
+shell script to run.  This implies '-n' and is subject
9014c4
+to the same limitations as compile time error checking checking;
9014c4
+Command substitutions, Conditional expressions and
9014c4
+.BR eval
9014c4
+builtin are not parsed so some dependencies may be missed.
9014c4
+.TP
9014c4
 .B \-\-verbose
9014c4
 Equivalent to \fB\-v\fP.
9014c4
 .TP
9014c4
diff --git a/doc/bashref.texi b/doc/bashref.texi
9014c4
index 06957b6..e3fe925 100644
9014c4
--- a/doc/bashref.texi
9014c4
+++ b/doc/bashref.texi
9014c4
@@ -6243,6 +6243,13 @@ standard.  @xref{Bash POSIX Mode}, for a description of the Bash
9014c4
 @item --restricted
9014c4
 Make the shell a restricted shell (@pxref{The Restricted Shell}).
9014c4
 
9014c4
+@item --rpm-requires
9014c4
+Produce the list of files that are required for the 
9014c4
+shell script to run.  This implies '-n' and is subject
9014c4
+to the same limitations as compile time error checking checking;
9014c4
+Command substitutions, Conditional expressions and @command{eval}
9014c4
+are not parsed so some dependencies may be missed.
9014c4
+
9014c4
 @item --verbose
9014c4
 Equivalent to @option{-v}.  Print shell input lines as they're read.
9014c4
 
9014c4
diff --git a/eval.c b/eval.c
9014c4
index db863e7..5a5af32 100644
9014c4
--- a/eval.c
9014c4
+++ b/eval.c
9014c4
@@ -56,6 +56,7 @@ extern int need_here_doc;
9014c4
 extern int current_command_number, current_command_line_count, line_number;
9014c4
 extern int expand_aliases;
9014c4
 extern char *ps0_prompt;
9014c4
+extern int rpm_requires;
9014c4
 
9014c4
 #if defined (HAVE_POSIX_SIGNALS)
9014c4
 extern sigset_t top_level_mask;
9014c4
@@ -148,7 +149,7 @@ reader_loop ()
9014c4
 
9014c4
       if (read_command () == 0)
9014c4
 	{
9014c4
-	  if (interactive_shell == 0 && read_but_dont_execute)
9014c4
+	  if (interactive_shell == 0 && (read_but_dont_execute && !rpm_requires))
9014c4
 	    {
9014c4
 	      last_command_exit_value = EXECUTION_SUCCESS;
9014c4
 	      dispose_command (global_command);
9014c4
diff --git a/execute_cmd.c b/execute_cmd.c
9014c4
index b5cd405..88c7a5c 100644
9014c4
--- a/execute_cmd.c
9014c4
+++ b/execute_cmd.c
9014c4
@@ -533,6 +533,8 @@ async_redirect_stdin ()
9014c4
 
9014c4
 #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
9014c4
 
9014c4
+extern int rpm_requires;
9014c4
+
9014c4
 /* Execute the command passed in COMMAND, perhaps doing it asynchronously.
9014c4
    COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
9014c4
    ASYNCHROUNOUS, if non-zero, says to do this command in the background.
9014c4
@@ -565,7 +567,13 @@ execute_command_internal (command, asynchronous, pipe_in, pipe_out,
9014c4
 
9014c4
   if (breaking || continuing)
9014c4
     return (last_command_exit_value);
9014c4
-  if (command == 0 || read_but_dont_execute)
9014c4
+  if (command == 0 || (read_but_dont_execute && !rpm_requires))
9014c4
+    return (EXECUTION_SUCCESS);
9014c4
+  if (rpm_requires && command->type == cm_function_def)
9014c4
+    return last_command_exit_value =
9014c4
+      execute_intern_function (command->value.Function_def->name,
9014c4
+                              command->value.Function_def);
9014c4
+  if (read_but_dont_execute)
9014c4
     return (EXECUTION_SUCCESS);
9014c4
 
9014c4
   QUIT;
9014c4
@@ -5752,7 +5760,7 @@ execute_intern_function (name, funcdef)
9014c4
 
9014c4
   if (check_identifier (name, posixly_correct) == 0)
9014c4
     {
9014c4
-      if (posixly_correct && interactive_shell == 0)
9014c4
+      if (posixly_correct && interactive_shell == 0 && rpm_requires == 0)
9014c4
 	{
9014c4
 	  last_command_exit_value = EX_BADUSAGE;
9014c4
 	  jump_to_top_level (ERREXIT);
9014c4
diff --git a/execute_cmd.h b/execute_cmd.h
9014c4
index 62bec82..d42dc85 100644
9014c4
--- a/execute_cmd.h
9014c4
+++ b/execute_cmd.h
9014c4
@@ -22,6 +22,8 @@
9014c4
 #define _EXECUTE_CMD_H_
9014c4
 
9014c4
 #include "stdc.h"
9014c4
+#include "variables.h"
9014c4
+#include "command.h"
9014c4
 
9014c4
 #if defined (ARRAY_VARS)
9014c4
 struct func_array_state
9014c4
diff --git a/make_cmd.c b/make_cmd.c
9014c4
index b42e9ff..a982fe0 100644
9014c4
--- a/make_cmd.c
9014c4
+++ b/make_cmd.c
9014c4
@@ -42,11 +42,15 @@
9014c4
 #include "flags.h"
9014c4
 #include "make_cmd.h"
9014c4
 #include "dispose_cmd.h"
9014c4
+#include "execute_cmd.h"
9014c4
 #include "variables.h"
9014c4
 #include "subst.h"
9014c4
 #include "input.h"
9014c4
 #include "ocache.h"
9014c4
 #include "externs.h"
9014c4
+#include "builtins.h"
9014c4
+
9014c4
+#include "builtins/common.h"
9014c4
 
9014c4
 #if defined (JOB_CONTROL)
9014c4
 #include "jobs.h"
9014c4
@@ -57,6 +61,10 @@
9014c4
 extern int line_number, current_command_line_count, parser_state;
9014c4
 extern int last_command_exit_value;
9014c4
 extern int shell_initialized;
9014c4
+extern int rpm_requires;
9014c4
+
9014c4
+static char *alphabet_set = "abcdefghijklmnopqrstuvwxyz"
9014c4
+                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
9014c4
 
9014c4
 int here_doc_first_line = 0;
9014c4
 
9014c4
@@ -839,6 +847,27 @@ make_coproc_command (name, command)
9014c4
   return (make_command (cm_coproc, (SIMPLE_COM *)temp));
9014c4
 }
9014c4
 
9014c4
+static void
9014c4
+output_requirement (deptype, filename)
9014c4
+const char *deptype;
9014c4
+char *filename;
9014c4
+{
9014c4
+  if (strchr(filename, '$') || (filename[0] != '/' && strchr(filename, '/')))
9014c4
+    return;
9014c4
+
9014c4
+  /* 
9014c4
+      if the executable is called via variable substitution we can
9014c4
+      not dermine what it is at compile time.  
9014c4
+
9014c4
+      if the executable consists only of characters not in the
9014c4
+      alphabet we do not consider it a dependency just an artifact
9014c4
+      of shell parsing (ex "exec < ${infile}").
9014c4
+  */
9014c4
+
9014c4
+  if (strpbrk(filename, alphabet_set))
9014c4
+    printf ("%s(%s)\n", deptype, filename);
9014c4
+}
9014c4
+
9014c4
 /* Reverse the word list and redirection list in the simple command
9014c4
    has just been parsed.  It seems simpler to do this here the one
9014c4
    time then by any other method that I can think of. */
9014c4
@@ -856,6 +885,27 @@ clean_simple_command (command)
9014c4
 	REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
9014c4
     }
9014c4
 
9014c4
+  if (rpm_requires && command->value.Simple->words)
9014c4
+    {
9014c4
+      char *cmd0;
9014c4
+      char *cmd1;
9014c4
+      struct builtin *b;
9014c4
+
9014c4
+      cmd0 = command->value.Simple->words->word->word;
9014c4
+      b = builtin_address_internal (cmd0, 0);
9014c4
+      cmd1 = 0;
9014c4
+      if (command->value.Simple->words->next)
9014c4
+        cmd1 = command->value.Simple->words->next->word->word;
9014c4
+
9014c4
+      if (b) {
9014c4
+        if ( (b->flags & REQUIRES_BUILTIN) && cmd1)
9014c4
+          output_requirement ("executable", cmd1);
9014c4
+      } else {
9014c4
+        if (!assignment(cmd0, 0))
9014c4
+          output_requirement (find_function(cmd0) ? "function" : "executable", cmd0);
9014c4
+      }
9014c4
+    } /*rpm_requires*/
9014c4
+
9014c4
   parser_state &= ~PST_REDIRLIST;
9014c4
   return (command);
9014c4
 }
9014c4
diff --git a/shell.c b/shell.c
9014c4
index 7f63969..a0fb7ce 100644
9014c4
--- a/shell.c
9014c4
+++ b/shell.c
9014c4
@@ -201,6 +201,9 @@ int have_devfd = 0;
9014c4
 /* The name of the .(shell)rc file. */
9014c4
 static char *bashrc_file = DEFAULT_BASHRC;
9014c4
 
9014c4
+/* Non-zero if we are finding the scripts requirements. */
9014c4
+int rpm_requires;
9014c4
+
9014c4
 /* Non-zero means to act more like the Bourne shell on startup. */
9014c4
 static int act_like_sh;
9014c4
 
9014c4
@@ -264,6 +267,7 @@ static const struct {
9014c4
   { "protected", Int, &protected_mode, (char **)0x0 },
9014c4
 #endif
9014c4
   { "rcfile", Charp, (int *)0x0, &bashrc_file },
9014c4
+  { "rpm-requires", Int, &rpm_requires, (char **)0x0 },
9014c4
 #if defined (RESTRICTED_SHELL)
9014c4
   { "restricted", Int, &restricted, (char **)0x0 },
9014c4
 #endif
9014c4
@@ -500,6 +504,12 @@ main (argc, argv, env)
9014c4
   if (dump_translatable_strings)
9014c4
     read_but_dont_execute = 1;
9014c4
 
9014c4
+  if (rpm_requires)
9014c4
+    {
9014c4
+      read_but_dont_execute = 1;
9014c4
+      initialize_shell_builtins ();
9014c4
+    }
9014c4
+
9014c4
   if (running_setuid && privileged_mode == 0)
9014c4
     disable_priv_mode ();
9014c4
 
9014c4
-- 
9014c4
2.9.3
9014c4