[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    Re: [PATCH 2/2] busybox: add '--show name' option to display script content
From:       Denys Vlasenko <vda.linux () googlemail ! com>
Date:       2018-11-18 18:20:24
Message-ID: CAK1hOcNeW+2zQV3=Yza_gKQUYOQNp35u+CW0wv_6McZUsv7PAQ () mail ! gmail ! com
[Download RAW message or body]

Applied with some edits, thanks!
On Sun, Nov 18, 2018 at 4:03 PM Ron Yorston <rmy@pobox.com> wrote:
>
> Add an option to allow the content of embedded scripts to be
> displayed.  This includes applet scripts, custom scripts and the
> .profile script.
>
> function                                             old     new   delta
> run_applet_and_exit                                  728     820     +92
> .rodata                                           168634  168690     +56
> ------------------------------------------------------------------------------
> (add/remove: 0/0 grow/shrink: 2/0 up/down: 148/0)             Total: 148 bytes
>
> Signed-off-by: Ron Yorston <rmy@pobox.com>
> ---
>  libbb/appletlib.c | 107 +++++++++++++++++++++++++++++-----------------
>  1 file changed, 67 insertions(+), 40 deletions(-)
>
> diff --git a/libbb/appletlib.c b/libbb/appletlib.c
> index 9e17b84ac..34867ada0 100644
> --- a/libbb/appletlib.c
> +++ b/libbb/appletlib.c
> @@ -61,6 +61,12 @@
>  #endif
>
>  #define SEPARATE_CUSTOM_SCRIPTS (NUM_CUSTOM_SCRIPTS > 0 || HAS_PROFILE)
> +#define SHOW_SCRIPT_CONTENT     (NUM_SCRIPTS > 0 || HAS_PROFILE)
> +#if SHOW_SCRIPT_CONTENT
> +# define IF_SHOW_SCRIPT_CONTENT(...) __VA_ARGS__
> +#else
> +# define IF_SHOW_SCRIPT_CONTENT(...)
> +#endif
>
>  #if NUM_SCRIPTS > 0 || HAS_PROFILE
>  # include "bb_archive.h"
> @@ -762,6 +768,46 @@ static void install_links(const char *busybox UNUSED_PARAM,
>
>  static void run_applet_and_exit(const char *name, char **argv) NORETURN;
>
> +# if NUM_SCRIPTS > 0 || HAS_PROFILE
> +static int find_script_by_name(const char *name)
> +{
> +       int i;
> +       int applet = find_applet_by_name(name);
> +
> +       if (applet >= 0) {
> +               for (i = 0; i < NUM_SCRIPTS; ++i)
> +                       if (applet_numbers[i] == applet)
> +                               return i + HAS_PROFILE;
> +       }
> +       return -1;
> +}
> +
> +char* FAST_FUNC
> +get_script_content(unsigned n)
> +{
> +       char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
> +                                       UNPACKED_SCRIPTS_LENGTH);
> +       if (t) {
> +               while (n != 0) {
> +                       while (*t++ != '\0')
> +                               continue;
> +                       n--;
> +               }
> +       }
> +       return t;
> +}
> +# endif /* NUM_SCRIPTS > 0 || HAS_PROFILE */
> +
> +#if NUM_SCRIPTS > 0
> +int scripted_main(int argc UNUSED_PARAM, char **argv)
> +{
> +       int script = find_script_by_name(applet_name);
> +       if (script >= 0)
> +               exit(ash_main(-script - 1, argv));
> +       return 0;
> +}
> +# endif /* NUM_SCRIPTS > 0 */
> +
>  # if ENABLE_BUSYBOX
>  #  if SEPARATE_CUSTOM_SCRIPTS
>  static int is_custom(int applet_no)
> @@ -814,6 +860,9 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
>                         "\n"
>                         "Usage: busybox [function [arguments]...]\n"
>                         "   or: busybox --list"IF_FEATURE_INSTALLER("[-full]")"\n"
> +                       IF_SHOW_SCRIPT_CONTENT(
> +                       "   or: busybox --show name\n"
> +                       )
>                         IF_FEATURE_INSTALLER(
>                         "   or: busybox --install [-s] [DIR]\n"
>                         )
> @@ -906,6 +955,24 @@ int busybox_main(int argc UNUSED_PARAM, char **argv)
>                 return 0;
>         }
>
> +       if (SHOW_SCRIPT_CONTENT && strcmp(argv[1], "--show") == 0) {
> +               if (argv[2] != NULL) {
> +                       int n = (HAS_PROFILE && strcmp(argv[2], ".profile") == 0) ? 0 :
> +                                               find_script_by_name(argv[2]);
> +                       if (n >= 0) {
> +                               char *script = get_script_content(n);
> +                               if (script != NULL) {
> +                                       full_write1_str(script);
> +                               }
> +                       }
> +                       else {
> +                               bb_error_msg_and_die("script '%s' not found", argv[2]);
> +                       }
> +                       return 0;
> +               }
> +               bb_error_msg_and_die(bb_msg_requires_arg, "--show");
> +       }
> +
>         if (ENABLE_FEATURE_INSTALLER && strcmp(argv[1], "--install") == 0) {
>                 int use_symbolic_links;
>                 const char *busybox;
> @@ -989,46 +1056,6 @@ void FAST_FUNC run_applet_no_and_exit(int applet_no, const char *name, char **ar
>  }
>  # endif /* NUM_APPLETS > 0 */
>
> -# if NUM_SCRIPTS > 0
> -static int find_script_by_name(const char *name)
> -{
> -       int i;
> -       int applet = find_applet_by_name(name);
> -
> -       if (applet >= 0) {
> -               for (i = 0; i < NUM_SCRIPTS; ++i)
> -                       if (applet_numbers[i] == applet)
> -                               return i + HAS_PROFILE;
> -       }
> -       return -1;
> -}
> -
> -int scripted_main(int argc UNUSED_PARAM, char **argv)
> -{
> -       int script = find_script_by_name(applet_name);
> -       if (script >= 0)
> -               exit(ash_main(-script - 1, argv));
> -       return 0;
> -}
> -# endif /* NUM_SCRIPTS > 0 */
> -
> -# if NUM_SCRIPTS > 0 || HAS_PROFILE
> -char* FAST_FUNC
> -get_script_content(unsigned n)
> -{
> -       char *t = unpack_bz2_data(packed_scripts, sizeof(packed_scripts),
> -                                       UNPACKED_SCRIPTS_LENGTH);
> -       if (t) {
> -               while (n != 0) {
> -                       while (*t++ != '\0')
> -                               continue;
> -                       n--;
> -               }
> -       }
> -       return t;
> -}
> -# endif /* NUM_SCRIPTS > 0 || HAS_PROFILE */
> -
>  # if ENABLE_BUSYBOX || NUM_APPLETS > 0
>  static NORETURN void run_applet_and_exit(const char *name, char **argv)
>  {
> --
> 2.19.1
>
> _______________________________________________
> busybox mailing list
> busybox@busybox.net
> http://lists.busybox.net/mailman/listinfo/busybox
_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic