svashisht / rpms / bash

Forked from rpms/bash 4 years ago
Clone

Blame SOURCES/bash-requires.patch

ff19ae
diff -up bash-4.1/builtins.h.requires bash-4.1/builtins.h
ff19ae
--- bash-4.1/builtins.h.requires	2009-01-04 20:32:23.000000000 +0100
ff19ae
+++ bash-4.1/builtins.h	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -41,6 +41,8 @@
ff19ae
 #define SPECIAL_BUILTIN 0x08	/* This is a Posix `special' builtin. */
ff19ae
 #define ASSIGNMENT_BUILTIN 0x10	/* This builtin takes assignment statements. */
ff19ae
 #define POSIX_BUILTIN	0x20	/* This builtins is special in the Posix command search order. */
ff19ae
+#define REQUIRES_BUILTIN 0x40	/* This builtin requires other files. */
ff19ae
+
ff19ae
 
ff19ae
 #define BASE_INDENT	4
ff19ae
 
ff19ae
diff -up bash-4.1/builtins/mkbuiltins.c.requires bash-4.1/builtins/mkbuiltins.c
ff19ae
--- bash-4.1/builtins/mkbuiltins.c.requires	2009-01-04 20:32:23.000000000 +0100
ff19ae
+++ bash-4.1/builtins/mkbuiltins.c	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -69,9 +69,15 @@ extern char *strcpy ();
ff19ae
 #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
ff19ae
 
ff19ae
 /* Flag values that builtins can have. */
ff19ae
+/*  These flags are for the C code generator, 
ff19ae
+    the C which is produced (./builtin.c)
ff19ae
+    includes the flags definitions found 
ff19ae
+    in ../builtins.h */
ff19ae
 #define BUILTIN_FLAG_SPECIAL	0x01
ff19ae
 #define BUILTIN_FLAG_ASSIGNMENT 0x02
ff19ae
 #define BUILTIN_FLAG_POSIX_BUILTIN 0x04
ff19ae
+#define BUILTIN_FLAG_REQUIRES	0x08
ff19ae
+
ff19ae
 
ff19ae
 #define BASE_INDENT	4
ff19ae
 
ff19ae
@@ -163,10 +169,18 @@ char *posix_builtins[] =
ff19ae
   (char *)NULL
ff19ae
 };
ff19ae
 
ff19ae
+/* The builtin commands that cause requirements on other files. */
ff19ae
+static char *requires_builtins[] =
ff19ae
+{
ff19ae
+  ".", "command", "exec", "source", "inlib",
ff19ae
+  (char *)NULL
ff19ae
+};
ff19ae
+
ff19ae
 /* Forward declarations. */
ff19ae
 static int is_special_builtin ();
ff19ae
 static int is_assignment_builtin ();
ff19ae
 static int is_posix_builtin ();
ff19ae
+static int is_requires_builtin ();
ff19ae
 
ff19ae
 #if !defined (HAVE_RENAME)
ff19ae
 static int rename ();
ff19ae
@@ -812,6 +826,9 @@ builtin_handler (self, defs, arg)
ff19ae
     new->flags |= BUILTIN_FLAG_ASSIGNMENT;
ff19ae
   if (is_posix_builtin (name))
ff19ae
     new->flags |= BUILTIN_FLAG_POSIX_BUILTIN;
ff19ae
+  if (is_requires_builtin (name))
ff19ae
+    new->flags |= BUILTIN_FLAG_REQUIRES;
ff19ae
+
ff19ae
 
ff19ae
   array_add ((char *)new, defs->builtins);
ff19ae
   building_builtin = 1;
ff19ae
@@ -1229,11 +1246,12 @@ write_builtins (defs, structfile, extern
ff19ae
 		  else
ff19ae
 		    fprintf (structfile, "(sh_builtin_func_t *)0x0, ");
ff19ae
 
ff19ae
-		  fprintf (structfile, "%s%s%s%s, %s_doc,\n",
ff19ae
+		  fprintf (structfile, "%s%s%s%s%s, %s_doc,\n",
ff19ae
 		    "BUILTIN_ENABLED | STATIC_BUILTIN",
ff19ae
 		    (builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
ff19ae
 		    (builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
ff19ae
 		    (builtin->flags & BUILTIN_FLAG_POSIX_BUILTIN) ? " | POSIX_BUILTIN" : "",
ff19ae
+		    (builtin->flags & BUILTIN_FLAG_REQUIRES) ? " | REQUIRES_BUILTIN" : "",
ff19ae
 		    document_name (builtin));
ff19ae
 
ff19ae
 		  fprintf
ff19ae
@@ -1581,6 +1599,13 @@ is_posix_builtin (name)
ff19ae
   return (_find_in_table (name, posix_builtins));
ff19ae
 }
ff19ae
 
ff19ae
+static int
ff19ae
+is_requires_builtin (name)
ff19ae
+     char *name;
ff19ae
+{
ff19ae
+  return (_find_in_table (name, requires_builtins));
ff19ae
+}
ff19ae
+
ff19ae
 #if !defined (HAVE_RENAME)
ff19ae
 static int
ff19ae
 rename (from, to)
ff19ae
diff -up bash-4.1/doc/bash.1.requires bash-4.1/doc/bash.1
ff19ae
--- bash-4.1/doc/bash.1.requires	2010-08-02 17:42:41.000000000 +0200
ff19ae
+++ bash-4.1/doc/bash.1	2010-08-02 18:09:27.000000000 +0200
ff19ae
@@ -231,6 +231,14 @@ The shell becomes restricted (see
ff19ae
 .B "RESTRICTED SHELL"
ff19ae
 below).
ff19ae
 .TP
ff19ae
+.B \-\-rpm-requires
ff19ae
+Produce the list of files that are required for the 
ff19ae
+shell script to run.  This implies '-n' and is subject
ff19ae
+to the same limitations as compile time error checking checking;
ff19ae
+Command substitutions, Conditional expressions and
ff19ae
+.BR eval
ff19ae
+builtin are not parsed so some dependencies may be missed.
ff19ae
+.TP
ff19ae
 .B \-\-verbose
ff19ae
 Equivalent to  \fB\-v\fP.
ff19ae
 .TP
ff19ae
diff -up bash-4.1/doc/bashref.texi.requires bash-4.1/doc/bashref.texi
ff19ae
--- bash-4.1/doc/bashref.texi.requires	2010-08-02 17:42:41.000000000 +0200
ff19ae
+++ bash-4.1/doc/bashref.texi	2010-08-02 18:11:58.000000000 +0200
ff19ae
@@ -5343,6 +5343,13 @@ standard.  @xref{Bash POSIX Mode}, for a
ff19ae
 @item --restricted
ff19ae
 Make the shell a restricted shell (@pxref{The Restricted Shell}).
ff19ae
 
ff19ae
+@item --rpm-requires
ff19ae
+Produce the list of files that are required for the 
ff19ae
+shell script to run.  This implies '-n' and is subject
ff19ae
+to the same limitations as compile time error checking checking;
ff19ae
+Command substitutions, Conditional expressions and @command{eval}
ff19ae
+are not parsed so some dependencies may be missed.
ff19ae
+
ff19ae
 @item --verbose
ff19ae
 Equivalent to @option{-v}.  Print shell input lines as they're read.
ff19ae
 
ff19ae
diff -up bash-4.1/eval.c.requires bash-4.1/eval.c
ff19ae
--- bash-4.1/eval.c.requires	2009-01-04 20:32:26.000000000 +0100
ff19ae
+++ bash-4.1/eval.c	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -53,6 +53,7 @@ extern int last_command_exit_value, stdi
ff19ae
 extern int need_here_doc;
ff19ae
 extern int current_command_number, current_command_line_count, line_number;
ff19ae
 extern int expand_aliases;
ff19ae
+extern int rpm_requires;
ff19ae
 
ff19ae
 static void send_pwd_to_eterm __P((void));
ff19ae
 static sighandler alrm_catcher __P((int));
ff19ae
@@ -136,7 +137,7 @@ reader_loop ()
ff19ae
 
ff19ae
       if (read_command () == 0)
ff19ae
 	{
ff19ae
-	  if (interactive_shell == 0 && read_but_dont_execute)
ff19ae
+	  if (interactive_shell == 0 && (read_but_dont_execute && !rpm_requires))
ff19ae
 	    {
ff19ae
 	      last_command_exit_value = EXECUTION_SUCCESS;
ff19ae
 	      dispose_command (global_command);
ff19ae
diff -up bash-4.1/execute_cmd.c.requires bash-4.1/execute_cmd.c
ff19ae
--- bash-4.1/execute_cmd.c.requires	2010-08-02 17:42:41.000000000 +0200
ff19ae
+++ bash-4.1/execute_cmd.c	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -503,6 +503,8 @@ async_redirect_stdin ()
ff19ae
 
ff19ae
 #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
ff19ae
 
ff19ae
+extern int rpm_requires;
ff19ae
+
ff19ae
 /* Execute the command passed in COMMAND, perhaps doing it asynchrounously.
ff19ae
    COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
ff19ae
    ASYNCHROUNOUS, if non-zero, says to do this command in the background.
ff19ae
@@ -534,7 +536,13 @@ execute_command_internal (command, async
ff19ae
 #else
ff19ae
   if (breaking || continuing)
ff19ae
     return (last_command_exit_value);
ff19ae
-  if (command == 0 || read_but_dont_execute)
ff19ae
+  if (command == 0 || (read_but_dont_execute && !rpm_requires))
ff19ae
+    return (EXECUTION_SUCCESS);
ff19ae
+  if (rpm_requires && command->type == cm_function_def)
ff19ae
+    return last_command_exit_value =
ff19ae
+      execute_intern_function (command->value.Function_def->name,
ff19ae
+                              command->value.Function_def->command);
ff19ae
+  if (read_but_dont_execute)
ff19ae
     return (EXECUTION_SUCCESS);
ff19ae
 #endif
ff19ae
 
ff19ae
@@ -5066,7 +5074,7 @@ execute_intern_function (name, function)
ff19ae
 
ff19ae
   if (check_identifier (name, posixly_correct) == 0)
ff19ae
     {
ff19ae
-      if (posixly_correct && interactive_shell == 0)
ff19ae
+      if (posixly_correct && interactive_shell == 0 && rpm_requires == 0)
ff19ae
 	{
ff19ae
 	  last_command_exit_value = EX_BADUSAGE;
ff19ae
 	  jump_to_top_level (ERREXIT);
ff19ae
diff -up bash-4.1/execute_cmd.h.requires bash-4.1/execute_cmd.h
ff19ae
--- bash-4.1/execute_cmd.h.requires	2009-01-16 22:20:15.000000000 +0100
ff19ae
+++ bash-4.1/execute_cmd.h	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -22,6 +22,8 @@
ff19ae
 #define _EXECUTE_CMD_H_
ff19ae
 
ff19ae
 #include "stdc.h"
ff19ae
+#include "variables.h"
ff19ae
+#include "command.h"
ff19ae
 
ff19ae
 extern struct fd_bitmap *new_fd_bitmap __P((int));
ff19ae
 extern void dispose_fd_bitmap __P((struct fd_bitmap *));
ff19ae
diff -up bash-4.1/make_cmd.c.requires bash-4.1/make_cmd.c
ff19ae
--- bash-4.1/make_cmd.c.requires	2009-09-11 23:26:12.000000000 +0200
ff19ae
+++ bash-4.1/make_cmd.c	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -42,11 +42,15 @@
ff19ae
 #include "flags.h"
ff19ae
 #include "make_cmd.h"
ff19ae
 #include "dispose_cmd.h"
ff19ae
+#include "execute_cmd.h"
ff19ae
 #include "variables.h"
ff19ae
 #include "subst.h"
ff19ae
 #include "input.h"
ff19ae
 #include "ocache.h"
ff19ae
 #include "externs.h"
ff19ae
+#include "builtins.h"
ff19ae
+
ff19ae
+#include "builtins/common.h"
ff19ae
 
ff19ae
 #if defined (JOB_CONTROL)
ff19ae
 #include "jobs.h"
ff19ae
@@ -56,6 +60,10 @@
ff19ae
 
ff19ae
 extern int line_number, current_command_line_count, parser_state;
ff19ae
 extern int last_command_exit_value;
ff19ae
+extern int rpm_requires;
ff19ae
+
ff19ae
+static char *alphabet_set = "abcdefghijklmnopqrstuvwxyz"
ff19ae
+                     "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
ff19ae
 
ff19ae
 /* Object caching */
ff19ae
 sh_obj_cache_t wdcache = {0, 0, 0};
ff19ae
@@ -820,6 +828,27 @@ make_coproc_command (name, command)
ff19ae
   return (make_command (cm_coproc, (SIMPLE_COM *)temp));
ff19ae
 }
ff19ae
 
ff19ae
+static void
ff19ae
+output_requirement (deptype, filename)
ff19ae
+const char *deptype;
ff19ae
+char *filename;
ff19ae
+{
ff19ae
+  if (strchr(filename, '$') || (filename[0] != '/' && strchr(filename, '/')))
ff19ae
+    return;
ff19ae
+
ff19ae
+  /* 
ff19ae
+      if the executable is called via variable substitution we can
ff19ae
+      not dermine what it is at compile time.  
ff19ae
+
ff19ae
+      if the executable consists only of characters not in the
ff19ae
+      alphabet we do not consider it a dependency just an artifact
ff19ae
+      of shell parsing (ex "exec < ${infile}").
ff19ae
+  */
ff19ae
+
ff19ae
+  if (strpbrk(filename, alphabet_set))
ff19ae
+    printf ("%s(%s)\n", deptype, filename);
ff19ae
+}
ff19ae
+
ff19ae
 /* Reverse the word list and redirection list in the simple command
ff19ae
    has just been parsed.  It seems simpler to do this here the one
ff19ae
    time then by any other method that I can think of. */
ff19ae
@@ -837,6 +866,27 @@ clean_simple_command (command)
ff19ae
 	REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
ff19ae
     }
ff19ae
 
ff19ae
+  if (rpm_requires && command->value.Simple->words)
ff19ae
+    {
ff19ae
+      char *cmd0;
ff19ae
+      char *cmd1;
ff19ae
+      struct builtin *b;
ff19ae
+
ff19ae
+      cmd0 = command->value.Simple->words->word->word;
ff19ae
+      b = builtin_address_internal (cmd0, 0);
ff19ae
+      cmd1 = 0;
ff19ae
+      if (command->value.Simple->words->next)
ff19ae
+        cmd1 = command->value.Simple->words->next->word->word;
ff19ae
+
ff19ae
+      if (b) {
ff19ae
+        if ( (b->flags & REQUIRES_BUILTIN) && cmd1)
ff19ae
+          output_requirement ("executable", cmd1);
ff19ae
+      } else {
ff19ae
+        if (!assignment(cmd0, 0))
ff19ae
+          output_requirement (find_function(cmd0) ? "function" : "executable", cmd0);
ff19ae
+      }
ff19ae
+    } /*rpm_requires*/
ff19ae
+
ff19ae
   parser_state &= ~PST_REDIRLIST;
ff19ae
   return (command);
ff19ae
 }
ff19ae
diff -up bash-4.1/shell.c.requires bash-4.1/shell.c
ff19ae
--- bash-4.1/shell.c.requires	2010-08-02 17:42:41.000000000 +0200
ff19ae
+++ bash-4.1/shell.c	2010-08-02 17:42:41.000000000 +0200
ff19ae
@@ -193,6 +193,9 @@ int have_devfd = 0;
ff19ae
 /* The name of the .(shell)rc file. */
ff19ae
 static char *bashrc_file = "~/.bashrc";
ff19ae
 
ff19ae
+/* Non-zero if we are finding the scripts requirements. */
ff19ae
+int rpm_requires;
ff19ae
+
ff19ae
 /* Non-zero means to act more like the Bourne shell on startup. */
ff19ae
 static int act_like_sh;
ff19ae
 
ff19ae
@@ -251,6 +254,7 @@ static const struct {
ff19ae
   { "posix", Int, &posixly_correct, (char **)0x0 },
ff19ae
   { "protected", Int, &protected_mode, (char **)0x0 },
ff19ae
   { "rcfile", Charp, (int *)0x0, &bashrc_file },
ff19ae
+  { "rpm-requires", Int, &rpm_requires, (char **)0x0 },
ff19ae
 #if defined (RESTRICTED_SHELL)
ff19ae
   { "restricted", Int, &restricted, (char **)0x0 },
ff19ae
 #endif
ff19ae
@@ -485,6 +489,12 @@ main (argc, argv, env)
ff19ae
   if (dump_translatable_strings)
ff19ae
     read_but_dont_execute = 1;
ff19ae
 
ff19ae
+  if (rpm_requires)
ff19ae
+    {
ff19ae
+      read_but_dont_execute = 1;
ff19ae
+      initialize_shell_builtins ();
ff19ae
+    }
ff19ae
+
ff19ae
   if (running_setuid && privileged_mode == 0)
ff19ae
     disable_priv_mode ();
ff19ae