bash-requires.patch 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. diff -up bash-4.1/builtins.h.requires bash-4.1/builtins.h
  2. --- bash-4.1/builtins.h.requires 2009-01-04 20:32:23.000000000 +0100
  3. +++ bash-4.1/builtins.h 2010-08-02 17:42:41.000000000 +0200
  4. @@ -41,6 +41,8 @@
  5. #define SPECIAL_BUILTIN 0x08 /* This is a Posix `special' builtin. */
  6. #define ASSIGNMENT_BUILTIN 0x10 /* This builtin takes assignment statements. */
  7. #define POSIX_BUILTIN 0x20 /* This builtins is special in the Posix command search order. */
  8. +#define REQUIRES_BUILTIN 0x40 /* This builtin requires other files. */
  9. +
  10. #define BASE_INDENT 4
  11. diff -up bash-4.1/builtins/mkbuiltins.c.requires bash-4.1/builtins/mkbuiltins.c
  12. --- bash-4.1/builtins/mkbuiltins.c.requires 2009-01-04 20:32:23.000000000 +0100
  13. +++ bash-4.1/builtins/mkbuiltins.c 2010-08-02 17:42:41.000000000 +0200
  14. @@ -69,9 +69,15 @@ extern char *strcpy ();
  15. #define whitespace(c) (((c) == ' ') || ((c) == '\t'))
  16. /* Flag values that builtins can have. */
  17. +/* These flags are for the C code generator,
  18. + the C which is produced (./builtin.c)
  19. + includes the flags definitions found
  20. + in ../builtins.h */
  21. #define BUILTIN_FLAG_SPECIAL 0x01
  22. #define BUILTIN_FLAG_ASSIGNMENT 0x02
  23. #define BUILTIN_FLAG_POSIX_BUILTIN 0x04
  24. +#define BUILTIN_FLAG_REQUIRES 0x08
  25. +
  26. #define BASE_INDENT 4
  27. @@ -163,10 +169,18 @@ char *posix_builtins[] =
  28. (char *)NULL
  29. };
  30. +/* The builtin commands that cause requirements on other files. */
  31. +static char *requires_builtins[] =
  32. +{
  33. + ".", "command", "exec", "source", "inlib",
  34. + (char *)NULL
  35. +};
  36. +
  37. /* Forward declarations. */
  38. static int is_special_builtin ();
  39. static int is_assignment_builtin ();
  40. static int is_posix_builtin ();
  41. +static int is_requires_builtin ();
  42. #if !defined (HAVE_RENAME)
  43. static int rename ();
  44. @@ -812,6 +826,9 @@ builtin_handler (self, defs, arg)
  45. new->flags |= BUILTIN_FLAG_ASSIGNMENT;
  46. if (is_posix_builtin (name))
  47. new->flags |= BUILTIN_FLAG_POSIX_BUILTIN;
  48. + if (is_requires_builtin (name))
  49. + new->flags |= BUILTIN_FLAG_REQUIRES;
  50. +
  51. array_add ((char *)new, defs->builtins);
  52. building_builtin = 1;
  53. @@ -1229,11 +1246,12 @@ write_builtins (defs, structfile, extern
  54. else
  55. fprintf (structfile, "(sh_builtin_func_t *)0x0, ");
  56. - fprintf (structfile, "%s%s%s%s, %s_doc,\n",
  57. + fprintf (structfile, "%s%s%s%s%s, %s_doc,\n",
  58. "BUILTIN_ENABLED | STATIC_BUILTIN",
  59. (builtin->flags & BUILTIN_FLAG_SPECIAL) ? " | SPECIAL_BUILTIN" : "",
  60. (builtin->flags & BUILTIN_FLAG_ASSIGNMENT) ? " | ASSIGNMENT_BUILTIN" : "",
  61. (builtin->flags & BUILTIN_FLAG_POSIX_BUILTIN) ? " | POSIX_BUILTIN" : "",
  62. + (builtin->flags & BUILTIN_FLAG_REQUIRES) ? " | REQUIRES_BUILTIN" : "",
  63. document_name (builtin));
  64. if (inhibit_functions)
  65. @@ -1581,6 +1599,13 @@ is_posix_builtin (name)
  66. return (_find_in_table (name, posix_builtins));
  67. }
  68. +static int
  69. +is_requires_builtin (name)
  70. + char *name;
  71. +{
  72. + return (_find_in_table (name, requires_builtins));
  73. +}
  74. +
  75. #if !defined (HAVE_RENAME)
  76. static int
  77. rename (from, to)
  78. diff -up bash-4.1/doc/bash.1.requires bash-4.1/doc/bash.1
  79. --- bash-4.1/doc/bash.1.requires 2010-08-02 17:42:41.000000000 +0200
  80. +++ bash-4.1/doc/bash.1 2010-08-02 18:09:27.000000000 +0200
  81. @@ -231,6 +231,14 @@ The shell becomes restricted (see
  82. .B "RESTRICTED SHELL"
  83. below).
  84. .TP
  85. +.B \-\-rpm-requires
  86. +Produce the list of files that are required for the
  87. +shell script to run. This implies '-n' and is subject
  88. +to the same limitations as compile time error checking checking;
  89. +Command substitutions, Conditional expressions and
  90. +.BR eval
  91. +builtin are not parsed so some dependencies may be missed.
  92. +.TP
  93. .B \-\-verbose
  94. Equivalent to \fB\-v\fP.
  95. .TP
  96. diff -up bash-4.1/doc/bashref.texi.requires bash-4.1/doc/bashref.texi
  97. --- bash-4.1/doc/bashref.texi.requires 2010-08-02 17:42:41.000000000 +0200
  98. +++ bash-4.1/doc/bashref.texi 2010-08-02 18:11:58.000000000 +0200
  99. @@ -5343,6 +5343,13 @@ standard. @xref{Bash POSIX Mode}, for a
  100. @item --restricted
  101. Make the shell a restricted shell (@pxref{The Restricted Shell}).
  102. +@item --rpm-requires
  103. +Produce the list of files that are required for the
  104. +shell script to run. This implies '-n' and is subject
  105. +to the same limitations as compile time error checking checking;
  106. +Command substitutions, Conditional expressions and @command{eval}
  107. +are not parsed so some dependencies may be missed.
  108. +
  109. @item --verbose
  110. Equivalent to @option{-v}. Print shell input lines as they're read.
  111. diff -up bash-4.1/eval.c.requires bash-4.1/eval.c
  112. --- bash-4.1/eval.c.requires 2009-01-04 20:32:26.000000000 +0100
  113. +++ bash-4.1/eval.c 2010-08-02 17:42:41.000000000 +0200
  114. @@ -53,6 +53,7 @@ extern int last_command_exit_value, stdi
  115. extern int need_here_doc;
  116. extern int current_command_number, current_command_line_count, line_number;
  117. extern int expand_aliases;
  118. +extern int rpm_requires;
  119. #if defined (HAVE_POSIX_SIGNALS)
  120. extern sigset_t top_level_mask;
  121. @@ -136,7 +137,7 @@ reader_loop ()
  122. if (read_command () == 0)
  123. {
  124. - if (interactive_shell == 0 && read_but_dont_execute)
  125. + if (interactive_shell == 0 && (read_but_dont_execute && !rpm_requires))
  126. {
  127. last_command_exit_value = EXECUTION_SUCCESS;
  128. dispose_command (global_command);
  129. diff -up bash-4.1/execute_cmd.c.requires bash-4.1/execute_cmd.c
  130. --- bash-4.1/execute_cmd.c.requires 2010-08-02 17:42:41.000000000 +0200
  131. +++ bash-4.1/execute_cmd.c 2010-08-02 17:42:41.000000000 +0200
  132. @@ -503,6 +503,8 @@ async_redirect_stdin ()
  133. #define DESCRIBE_PID(pid) do { if (interactive) describe_pid (pid); } while (0)
  134. +extern int rpm_requires;
  135. +
  136. /* Execute the command passed in COMMAND, perhaps doing it asynchronously.
  137. COMMAND is exactly what read_command () places into GLOBAL_COMMAND.
  138. ASYNCHROUNOUS, if non-zero, says to do this command in the background.
  139. @@ -534,7 +536,13 @@ execute_command_internal (command, async
  140. if (breaking || continuing)
  141. return (last_command_exit_value);
  142. - if (command == 0 || read_but_dont_execute)
  143. + if (command == 0 || (read_but_dont_execute && !rpm_requires))
  144. + return (EXECUTION_SUCCESS);
  145. + if (rpm_requires && command->type == cm_function_def)
  146. + return last_command_exit_value =
  147. + execute_intern_function (command->value.Function_def->name,
  148. + command->value.Function_def->command);
  149. + if (read_but_dont_execute)
  150. return (EXECUTION_SUCCESS);
  151. QUIT;
  152. @@ -5066,7 +5074,7 @@ execute_intern_function (name, function)
  153. if (check_identifier (name, posixly_correct) == 0)
  154. {
  155. - if (posixly_correct && interactive_shell == 0)
  156. + if (posixly_correct && interactive_shell == 0 && rpm_requires == 0)
  157. {
  158. last_command_exit_value = EX_BADUSAGE;
  159. jump_to_top_level (ERREXIT);
  160. diff -up bash-4.1/execute_cmd.h.requires bash-4.1/execute_cmd.h
  161. --- bash-4.1/execute_cmd.h.requires 2009-01-16 22:20:15.000000000 +0100
  162. +++ bash-4.1/execute_cmd.h 2010-08-02 17:42:41.000000000 +0200
  163. @@ -22,6 +22,8 @@
  164. #define _EXECUTE_CMD_H_
  165. #include "stdc.h"
  166. +#include "variables.h"
  167. +#include "command.h"
  168. extern struct fd_bitmap *new_fd_bitmap __P((int));
  169. extern void dispose_fd_bitmap __P((struct fd_bitmap *));
  170. diff -up bash-4.1/make_cmd.c.requires bash-4.1/make_cmd.c
  171. --- bash-4.1/make_cmd.c.requires 2009-09-11 23:26:12.000000000 +0200
  172. +++ bash-4.1/make_cmd.c 2010-08-02 17:42:41.000000000 +0200
  173. @@ -42,11 +42,15 @@
  174. #include "flags.h"
  175. #include "make_cmd.h"
  176. #include "dispose_cmd.h"
  177. +#include "execute_cmd.h"
  178. #include "variables.h"
  179. #include "subst.h"
  180. #include "input.h"
  181. #include "ocache.h"
  182. #include "externs.h"
  183. +#include "builtins.h"
  184. +
  185. +#include "builtins/common.h"
  186. #if defined (JOB_CONTROL)
  187. #include "jobs.h"
  188. @@ -56,6 +60,10 @@
  189. extern int line_number, current_command_line_count, parser_state;
  190. extern int last_command_exit_value;
  191. +extern int rpm_requires;
  192. +
  193. +static char *alphabet_set = "abcdefghijklmnopqrstuvwxyz"
  194. + "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  195. /* Object caching */
  196. sh_obj_cache_t wdcache = {0, 0, 0};
  197. @@ -820,6 +828,27 @@ make_coproc_command (name, command)
  198. return (make_command (cm_coproc, (SIMPLE_COM *)temp));
  199. }
  200. +static void
  201. +output_requirement (deptype, filename)
  202. +const char *deptype;
  203. +char *filename;
  204. +{
  205. + if (strchr(filename, '$') || (filename[0] != '/' && strchr(filename, '/')))
  206. + return;
  207. +
  208. + /*
  209. + if the executable is called via variable substitution we can
  210. + not dermine what it is at compile time.
  211. +
  212. + if the executable consists only of characters not in the
  213. + alphabet we do not consider it a dependency just an artifact
  214. + of shell parsing (ex "exec < ${infile}").
  215. + */
  216. +
  217. + if (strpbrk(filename, alphabet_set))
  218. + printf ("%s(%s)\n", deptype, filename);
  219. +}
  220. +
  221. /* Reverse the word list and redirection list in the simple command
  222. has just been parsed. It seems simpler to do this here the one
  223. time then by any other method that I can think of. */
  224. @@ -837,6 +866,27 @@ clean_simple_command (command)
  225. REVERSE_LIST (command->value.Simple->redirects, REDIRECT *);
  226. }
  227. + if (rpm_requires && command->value.Simple->words)
  228. + {
  229. + char *cmd0;
  230. + char *cmd1;
  231. + struct builtin *b;
  232. +
  233. + cmd0 = command->value.Simple->words->word->word;
  234. + b = builtin_address_internal (cmd0, 0);
  235. + cmd1 = 0;
  236. + if (command->value.Simple->words->next)
  237. + cmd1 = command->value.Simple->words->next->word->word;
  238. +
  239. + if (b) {
  240. + if ( (b->flags & REQUIRES_BUILTIN) && cmd1)
  241. + output_requirement ("executable", cmd1);
  242. + } else {
  243. + if (!assignment(cmd0, 0))
  244. + output_requirement (find_function(cmd0) ? "function" : "executable", cmd0);
  245. + }
  246. + } /*rpm_requires*/
  247. +
  248. parser_state &= ~PST_REDIRLIST;
  249. return (command);
  250. }
  251. diff -up bash-4.1/shell.c.requires bash-4.1/shell.c
  252. --- bash-4.1/shell.c.requires 2010-08-02 17:42:41.000000000 +0200
  253. +++ bash-4.1/shell.c 2010-08-02 17:42:41.000000000 +0200
  254. @@ -193,6 +193,9 @@ int have_devfd = 0;
  255. /* The name of the .(shell)rc file. */
  256. static char *bashrc_file = "~/.bashrc";
  257. +/* Non-zero if we are finding the scripts requirements. */
  258. +int rpm_requires;
  259. +
  260. /* Non-zero means to act more like the Bourne shell on startup. */
  261. static int act_like_sh;
  262. @@ -251,6 +254,7 @@ static const struct {
  263. { "protected", Int, &protected_mode, (char **)0x0 },
  264. #endif
  265. { "rcfile", Charp, (int *)0x0, &bashrc_file },
  266. + { "rpm-requires", Int, &rpm_requires, (char **)0x0 },
  267. #if defined (RESTRICTED_SHELL)
  268. { "restricted", Int, &restricted, (char **)0x0 },
  269. #endif
  270. @@ -485,6 +489,12 @@ main (argc, argv, env)
  271. if (dump_translatable_strings)
  272. read_but_dont_execute = 1;
  273. + if (rpm_requires)
  274. + {
  275. + read_but_dont_execute = 1;
  276. + initialize_shell_builtins ();
  277. + }
  278. +
  279. if (running_setuid && privileged_mode == 0)
  280. disable_priv_mode ();