rcolebaugh / rpms / bash

Forked from rpms/bash 2 years ago
Clone
Petr Šabata 33fc6c
diff --git a/builtins.h b/builtins.h
Petr Šabata 33fc6c
index dac95fd..5b7e811 100644
Petr Šabata 33fc6c
--- a/builtins.h
Petr Šabata 33fc6c
+++ b/builtins.h
Petr Šabata 33fc6c
@@ -45,6 +45,7 @@
Petr Šabata 33fc6c
 #define ASSIGNMENT_BUILTIN 0x10	/* This builtin takes assignment statements. */
Petr Šabata 33fc6c
 #define POSIX_BUILTIN	0x20	/* This builtins is special in the Posix command search order. */
Petr Šabata 33fc6c
 #define LOCALVAR_BUILTIN   0x40	/* This builtin creates local variables */
Petr Šabata 33fc6c
+#define REQUIRES_BUILTIN 0x80  /* This builtin requires other files. */
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #define BASE_INDENT	4
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
diff --git a/builtins/mkbuiltins.c b/builtins/mkbuiltins.c
DistroBaker 62c465
index e243021..0a7a0e5 100644
Petr Šabata 33fc6c
--- a/builtins/mkbuiltins.c
Petr Šabata 33fc6c
+++ b/builtins/mkbuiltins.c
Petr Šabata 33fc6c
@@ -69,10 +69,15 @@ extern char *strcpy ();
Petr Šabata 33fc6c
 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 /* Flag values that builtins can have. */
Petr Šabata 33fc6c
+/*  These flags are for the C code generator,
Petr Šabata 33fc6c
+    the C which is produced (./builtin.c)
Petr Šabata 33fc6c
+    includes the flags definitions found
Petr Šabata 33fc6c
+    in ../builtins.h */
Petr Šabata 33fc6c
 #define BUILTIN_FLAG_SPECIAL	0x01
Petr Šabata 33fc6c
 #define BUILTIN_FLAG_ASSIGNMENT 0x02
Petr Šabata 33fc6c
 #define BUILTIN_FLAG_LOCALVAR	0x04
Petr Šabata 33fc6c
 #define BUILTIN_FLAG_POSIX_BUILTIN 0x08
Petr Šabata 33fc6c
+#define BUILTIN_FLAG_REQUIRES  0x10
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #define BASE_INDENT	4
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
@@ -173,11 +178,19 @@ char *posix_builtins[] =
Petr Šabata 33fc6c
   (char *)NULL
Petr Šabata 33fc6c
 };
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+/* The builtin commands that cause requirements on other files. */
Petr Šabata 33fc6c
+static char *requires_builtins[] =
Petr Šabata 33fc6c
+{
Petr Šabata 33fc6c
+  ".", "command", "exec", "source", "inlib",
Petr Šabata 33fc6c
+  (char *)NULL
Petr Šabata 33fc6c
+};
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 /* Forward declarations. */
Petr Šabata 33fc6c
 static int is_special_builtin ();
Petr Šabata 33fc6c
 static int is_assignment_builtin ();
Petr Šabata 33fc6c
 static int is_localvar_builtin ();
Petr Šabata 33fc6c
 static int is_posix_builtin ();
Petr Šabata 33fc6c
+static int is_requires_builtin ();
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #if !defined (HAVE_RENAME)
Petr Šabata 33fc6c
 static int rename ();
Petr Šabata 33fc6c
@@ -831,6 +844,8 @@ builtin_handler (self, defs, arg)
Petr Šabata 33fc6c
     new->flags |= BUILTIN_FLAG_LOCALVAR;
Petr Šabata 33fc6c
   if (is_posix_builtin (name))
Petr Šabata 33fc6c
     new->flags |= BUILTIN_FLAG_POSIX_BUILTIN;
Petr Šabata 33fc6c
+  if (is_requires_builtin (name))
Petr Šabata 33fc6c
+    new->flags |= BUILTIN_FLAG_REQUIRES;
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   array_add ((char *)new, defs->builtins);
Petr Šabata 33fc6c
   building_builtin = 1;
Petr Šabata 33fc6c
@@ -1250,12 +1265,13 @@ write_builtins (defs, structfile, externfile)
Petr Šabata 33fc6c
 		  else
Petr Šabata 33fc6c
 		    fprintf (structfile, "(sh_builtin_func_t *)0x0, ");
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
-		  fprintf (structfile, "%s%s%s%s%s, %s_doc,\n",
Petr Šabata 33fc6c
+		  fprintf (structfile, "%s%s%s%s%s%s, %s_doc,\n",
Petr Šabata 33fc6c
 		    "BUILTIN_ENABLED | STATIC_BUILTIN",
Petr Šabata 33fc6c
 		    (builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
Petr Šabata 33fc6c
 		    (builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
Petr Šabata 33fc6c
 		    (builtin->flags & BUILTIN_FLAG_LOCALVAR) ? " | LOCALVAR_BUILTIN" : "",
Petr Šabata 33fc6c
 		    (builtin->flags & BUILTIN_FLAG_POSIX_BUILTIN) ? " | POSIX_BUILTIN" : "",
Petr Šabata 33fc6c
+		    (builtin->flags & BUILTIN_FLAG_REQUIRES) ? " | REQUIRES_BUILTIN" : "",
Petr Šabata 33fc6c
 		    document_name (builtin));
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 		  /* Don't translate short document summaries that are identical
Petr Šabata 33fc6c
@@ -1645,6 +1661,13 @@ is_posix_builtin (name)
Petr Šabata 33fc6c
   return (_find_in_table (name, posix_builtins));
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+static int
Petr Šabata 33fc6c
+is_requires_builtin (name)
Petr Šabata 33fc6c
+     char *name;
Petr Šabata 33fc6c
+{
Petr Šabata 33fc6c
+  return (_find_in_table (name, requires_builtins));
Petr Šabata 33fc6c
+}
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 #if !defined (HAVE_RENAME)
Petr Šabata 33fc6c
 static int
Petr Šabata 33fc6c
 rename (from, to)
Petr Šabata 33fc6c
diff --git a/doc/bash.1 b/doc/bash.1
DistroBaker 62c465
index 5af7d42..7539368 100644
Petr Šabata 33fc6c
--- a/doc/bash.1
Petr Šabata 33fc6c
+++ b/doc/bash.1
Petr Šabata 33fc6c
@@ -239,6 +239,14 @@ The shell becomes restricted (see
Petr Šabata 33fc6c
 .B "RESTRICTED SHELL"
Petr Šabata 33fc6c
 below).
Petr Šabata 33fc6c
 .TP
Petr Šabata 33fc6c
+.B \-\-rpm-requires
Petr Šabata 33fc6c
+Produce the list of files that are required for the
Petr Šabata 33fc6c
+shell script to run.  This implies '-n' and is subject
Petr Šabata 33fc6c
+to the same limitations as compile time error checking checking;
Petr Šabata 33fc6c
+Command substitutions, Conditional expressions and
Petr Šabata 33fc6c
+.BR eval
Petr Šabata 33fc6c
+builtin are not parsed so some dependencies may be missed.
Petr Šabata 33fc6c
+.TP
Petr Šabata 33fc6c
 .B \-\-verbose
Petr Šabata 33fc6c
 Equivalent to \fB\-v\fP.
Petr Šabata 33fc6c
 .TP
Petr Šabata 33fc6c
diff --git a/doc/bashref.texi b/doc/bashref.texi
DistroBaker 62c465
index 9e23f58..d02151e 100644
Petr Šabata 33fc6c
--- a/doc/bashref.texi
Petr Šabata 33fc6c
+++ b/doc/bashref.texi
DistroBaker 62c465
@@ -6554,6 +6554,13 @@ standard.  @xref{Bash POSIX Mode}, for a description of the Bash
Petr Šabata 33fc6c
 @item --restricted
Petr Šabata 33fc6c
 Make the shell a restricted shell (@pxref{The Restricted Shell}).
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+@item --rpm-requires
Petr Šabata 33fc6c
+Produce the list of files that are required for the
Petr Šabata 33fc6c
+shell script to run.  This implies '-n' and is subject
Petr Šabata 33fc6c
+to the same limitations as compile time error checking checking;
Petr Šabata 33fc6c
+Command substitutions, Conditional expressions and @command{eval}
Petr Šabata 33fc6c
+are not parsed so some dependencies may be missed.
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 @item --verbose
Petr Šabata 33fc6c
 Equivalent to @option{-v}.  Print shell input lines as they're read.
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
diff --git a/eval.c b/eval.c
DistroBaker 62c465
index 1d967da..f197033 100644
Petr Šabata 33fc6c
--- a/eval.c
Petr Šabata 33fc6c
+++ b/eval.c
DistroBaker 62c465
@@ -137,7 +137,8 @@ reader_loop ()
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
       if (read_command () == 0)
Petr Šabata 33fc6c
 	{
Petr Šabata 33fc6c
-	  if (interactive_shell == 0 && read_but_dont_execute)
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+	  if (interactive_shell == 0 && (read_but_dont_execute && !rpm_requires))
Petr Šabata 33fc6c
 	    {
DistroBaker 62c465
 	      set_exit_status (EXECUTION_SUCCESS);
Petr Šabata 33fc6c
 	      dispose_command (global_command);
Petr Šabata 33fc6c
diff --git a/execute_cmd.c b/execute_cmd.c
DistroBaker 62c465
index d2555ad..397e283 100644
Petr Šabata 33fc6c
--- a/execute_cmd.c
Petr Šabata 33fc6c
+++ b/execute_cmd.c
DistroBaker 62c465
@@ -543,6 +543,8 @@ async_redirect_stdin ()
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+extern int rpm_requires;
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 /* Execute the command passed in COMMAND, perhaps doing it asynchronously.
Petr Šabata 33fc6c
    COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
DistroBaker 62c465
    ASYNCHRONOUS, if non-zero, says to do this command in the background.
DistroBaker 62c465
@@ -574,7 +576,13 @@ execute_command_internal (command, asynchronous, pipe_in, pipe_out,
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   if (breaking || continuing)
Petr Šabata 33fc6c
     return (last_command_exit_value);
Petr Šabata 33fc6c
-  if (command == 0 || read_but_dont_execute)
Petr Šabata 33fc6c
+  if (command == 0 || (read_but_dont_execute && !rpm_requires))
Petr Šabata 33fc6c
+    return (EXECUTION_SUCCESS);
Petr Šabata 33fc6c
+  if (rpm_requires && command->type == cm_function_def)
Petr Šabata 33fc6c
+    return last_command_exit_value =
Petr Šabata 33fc6c
+      execute_intern_function (command->value.Function_def->name,
Petr Šabata 33fc6c
+                              command->value.Function_def);
Petr Šabata 33fc6c
+  if (read_but_dont_execute)
Petr Šabata 33fc6c
     return (EXECUTION_SUCCESS);
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
   QUIT;
DistroBaker 62c465
@@ -2836,7 +2844,7 @@ execute_for_command (for_command)
Petr Šabata 33fc6c
   save_line_number = line_number;
Petr Šabata 33fc6c
   if (check_identifier (for_command->name, 1) == 0)
Petr Šabata 33fc6c
     {
Petr Šabata 33fc6c
-      if (posixly_correct && interactive_shell == 0)
Petr Šabata 33fc6c
+      if (posixly_correct && interactive_shell == 0 && rpm_requires == 0)
Petr Šabata 33fc6c
 	{
Petr Šabata 33fc6c
 	  last_command_exit_value = EX_BADUSAGE;
Petr Šabata 33fc6c
 	  jump_to_top_level (ERREXIT);
Petr Šabata 33fc6c
diff --git a/execute_cmd.h b/execute_cmd.h
DistroBaker 62c465
index 465030a..9c7fd1c 100644
Petr Šabata 33fc6c
--- a/execute_cmd.h
Petr Šabata 33fc6c
+++ b/execute_cmd.h
Petr Šabata 33fc6c
@@ -22,6 +22,9 @@
Petr Šabata 33fc6c
 #define _EXECUTE_CMD_H_
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #include "stdc.h"
Petr Šabata 33fc6c
+#include "variables.h"
Petr Šabata 33fc6c
+#include "command.h"
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #if defined (ARRAY_VARS)
Petr Šabata 33fc6c
 struct func_array_state
Petr Šabata 33fc6c
diff --git a/make_cmd.c b/make_cmd.c
DistroBaker 62c465
index 2d7ac96..ac53526 100644
Petr Šabata 33fc6c
--- a/make_cmd.c
Petr Šabata 33fc6c
+++ b/make_cmd.c
Petr Šabata 33fc6c
@@ -35,6 +35,8 @@
Petr Šabata 33fc6c
 #include "bashintl.h"
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
 #include "shell.h"
Petr Šabata 33fc6c
+#include "builtins.h"
Petr Šabata 33fc6c
+#include "builtins/common.h"
Petr Šabata 33fc6c
 #include "execute_cmd.h"
Petr Šabata 33fc6c
 #include "parser.h"
Petr Šabata 33fc6c
 #include "flags.h"
Petr Šabata 33fc6c
@@ -828,6 +830,30 @@ make_coproc_command (name, command)
Petr Šabata 33fc6c
   return (make_command (cm_coproc, (SIMPLE_COM *)temp));
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+static void
Petr Šabata 33fc6c
+output_requirement (deptype, filename)
Petr Šabata 33fc6c
+const char *deptype;
Petr Šabata 33fc6c
+char *filename;
Petr Šabata 33fc6c
+{
Petr Šabata 33fc6c
+  static char *alphabet_set = "abcdefghijklmnopqrstuvwxyz"
Petr Šabata 33fc6c
+                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+  if (strchr(filename, '$') || (filename[0] != '/' && strchr(filename, '/')))
Petr Šabata 33fc6c
+    return;
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+  /*
Petr Šabata 33fc6c
+      if the executable is called via variable substitution we can
Petr Šabata 33fc6c
+      not dermine what it is at compile time.
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+      if the executable consists only of characters not in the
Petr Šabata 33fc6c
+      alphabet we do not consider it a dependency just an artifact
Petr Šabata 33fc6c
+      of shell parsing (ex "exec < ${infile}").
Petr Šabata 33fc6c
+  */
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+  if (strpbrk(filename, alphabet_set))
Petr Šabata 33fc6c
+    printf ("%s(%s)\n", deptype, filename);
Petr Šabata 33fc6c
+}
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 /* Reverse the word list and redirection list in the simple command
Petr Šabata 33fc6c
    has just been parsed.  It seems simpler to do this here the one
Petr Šabata 33fc6c
    time then by any other method that I can think of. */
Petr Šabata 33fc6c
@@ -845,6 +871,28 @@ clean_simple_command (command)
Petr Šabata 33fc6c
 	REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
Petr Šabata 33fc6c
     }
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+  if (rpm_requires && command->value.Simple->words)
Petr Šabata 33fc6c
+    {
Petr Šabata 33fc6c
+      char *cmd0;
Petr Šabata 33fc6c
+      char *cmd1;
Petr Šabata 33fc6c
+      struct builtin *b;
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+      cmd0 = command->value.Simple->words->word->word;
Petr Šabata 33fc6c
+      b = builtin_address_internal (cmd0, 0);
Petr Šabata 33fc6c
+      cmd1 = 0;
Petr Šabata 33fc6c
+      if (command->value.Simple->words->next)
Petr Šabata 33fc6c
+        cmd1 = command->value.Simple->words->next->word->word;
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+      if (b) {
Petr Šabata 33fc6c
+        if ( (b->flags & REQUIRES_BUILTIN) && cmd1)
Petr Šabata 33fc6c
+          output_requirement ("executable", cmd1);
Petr Šabata 33fc6c
+      } else {
Petr Šabata 33fc6c
+        if (!assignment(cmd0, 0))
Petr Šabata 33fc6c
+          output_requirement (find_function(cmd0) ? "function" : "executable", cmd0);
Petr Šabata 33fc6c
+      }
Petr Šabata 33fc6c
+    } /*rpm_requires*/
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
   parser_state &= ~PST_REDIRLIST;
Petr Šabata 33fc6c
   return (command);
Petr Šabata 33fc6c
 }
Petr Šabata 33fc6c
diff --git a/shell.c b/shell.c
DistroBaker 62c465
index ce8087f..7dcd000 100644
Petr Šabata 33fc6c
--- a/shell.c
Petr Šabata 33fc6c
+++ b/shell.c
DistroBaker 62c465
@@ -194,6 +194,9 @@ int have_devfd = 0;
Petr Šabata 33fc6c
 /* The name of the .(shell)rc file. */
Petr Šabata 33fc6c
 static char *bashrc_file = DEFAULT_BASHRC;
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+/* Non-zero if we are finding the scripts requirements. */
Petr Šabata 33fc6c
+int rpm_requires;
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
 /* Non-zero means to act more like the Bourne shell on startup. */
Petr Šabata 33fc6c
 static int act_like_sh;
Petr Šabata 33fc6c
 
DistroBaker 62c465
@@ -260,6 +263,7 @@ static const struct {
Petr Šabata 33fc6c
   { "protected", Int, &protected_mode, (char **)0x0 },
Petr Šabata 33fc6c
 #endif
Petr Šabata 33fc6c
   { "rcfile", Charp, (int *)0x0, &bashrc_file },
Petr Šabata 33fc6c
+  { "rpm-requires", Int, &rpm_requires, (char **)0x0 },
Petr Šabata 33fc6c
 #if defined (RESTRICTED_SHELL)
Petr Šabata 33fc6c
   { "restricted", Int, &restricted, (char **)0x0 },
Petr Šabata 33fc6c
 #endif
DistroBaker 62c465
@@ -502,6 +506,12 @@ main (argc, argv, env)
Petr Šabata 33fc6c
   if (dump_translatable_strings)
Petr Šabata 33fc6c
     read_but_dont_execute = 1;
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
+  if (rpm_requires)
Petr Šabata 33fc6c
+    {
Petr Šabata 33fc6c
+      read_but_dont_execute = 1;
Petr Šabata 33fc6c
+      initialize_shell_builtins ();
Petr Šabata 33fc6c
+    }
Petr Šabata 33fc6c
+
Petr Šabata 33fc6c
   if (running_setuid && privileged_mode == 0)
Petr Šabata 33fc6c
     disable_priv_mode ();
Petr Šabata 33fc6c
 
Petr Šabata 33fc6c
diff --git a/shell.h b/shell.h
DistroBaker 62c465
index 8b41792..29b0efb 100644
Petr Šabata 33fc6c
--- a/shell.h
Petr Šabata 33fc6c
+++ b/shell.h
Petr Šabata 33fc6c
@@ -99,6 +99,7 @@ extern int interactive, interactive_shell;
Petr Šabata 33fc6c
 extern int startup_state;
Petr Šabata 33fc6c
 extern int reading_shell_script;
Petr Šabata 33fc6c
 extern int shell_initialized;
Petr Šabata 33fc6c
+extern int rpm_requires;
Petr Šabata 33fc6c
 extern int bash_argv_initialized;
Petr Šabata 33fc6c
 extern int subshell_environment;
Petr Šabata 33fc6c
 extern int current_command_number;