1 /* vi: set sw=4 ts=4: */
3 * Support for main() which needs to end up in libbusybox, not busybox,
4 * if one builds libbusybox.
6 * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
8 * Licensed under GPLv2, see file License in this tarball for details.
15 /* Declare <applet>_main() */
20 #if ENABLE_SHOW_USAGE && !ENABLE_FEATURE_COMPRESS_USAGE
21 /* Define usage_messages[] */
22 static const char usage_messages[] ALIGN1 = ""
29 #define usage_messages 0
30 #endif /* SHOW_USAGE */
32 /* Define struct bb_applet applets[] */
35 #if ENABLE_FEATURE_SH_STANDALONE
36 /* -1 because last entry is NULL */
37 const unsigned short NUM_APPLETS = ARRAY_SIZE(applets) - 1;
41 #if ENABLE_FEATURE_COMPRESS_USAGE
43 #include "usage_compressed.h"
44 #include "unarchive.h"
46 static const char *unpack_usage_messages(void)
54 /* inbuf: */ packed_usage,
55 /* len: */ sizeof(packed_usage));
56 /* read_bunzip can longjmp to start_bunzip, and ultimately
57 * end up here with i != 0 on read data errors! Not trivial */
59 /* Cannot use xmalloc: will leak bd in NOFORK case! */
60 outbuf = malloc_or_warn(SIZEOF_usage_messages);
62 read_bunzip(bd, outbuf, SIZEOF_usage_messages);
67 #define dealloc_usage_messages(s) free(s)
71 #define unpack_usage_messages() usage_messages
72 #define dealloc_usage_messages(s) ((void)(s))
74 #endif /* FEATURE_COMPRESS_USAGE */
77 void bb_show_usage(void)
79 if (ENABLE_SHOW_USAGE) {
80 const char *format_string;
82 const char *usage_string = p = unpack_usage_messages();
83 const struct bb_applet *ap = find_applet_by_name(applet_name);
86 if (!ap) /* never happens, paranoia */
91 while (*p++) continue;
95 fprintf(stderr, "%s multi-call binary\n", bb_banner);
96 format_string = "\nUsage: %s %s\n\n";
98 format_string = "\nNo help available.\n\n";
99 fprintf(stderr, format_string, applet_name, p);
100 dealloc_usage_messages((char*)usage_string);
106 static int applet_name_compare(const void *name, const void *vapplet)
108 const struct bb_applet *applet = vapplet;
110 return strcmp(name, applet->name);
113 const struct bb_applet *find_applet_by_name(const char *name)
115 /* Do a binary search to find the applet entry given the name. */
116 return bsearch(name, applets, ARRAY_SIZE(applets)-1, sizeof(applets[0]),
117 applet_name_compare);
122 /* Make it reside in R/W memory: */
123 int *const bb_errno __attribute__ ((section (".data")));
126 void bbox_prepare_main(char **argv)
129 (*(int **)&bb_errno) = __errno_location();
132 /* Set locale for everybody except 'init' */
133 if (ENABLE_LOCALE_SUPPORT && getpid() != 1)
134 setlocale(LC_ALL, "");
136 /* Redundant for busybox, but needed for individual applets */
137 if (argv[1] && strcmp(argv[1], "--help") == 0)