1 /* vi: set sw=4 ts=4: */
3 * Applet table generator.
4 * Runs on host and produces include/applet_tables.h
6 * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
8 * Licensed under GPLv2, see file LICENSE in this source tree.
10 #include <sys/types.h>
19 #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
21 #include "../include/autoconf.h"
22 #include "../include/applet_metadata.h"
27 enum bb_install_loc_t install_loc;
28 enum bb_suid_t need_suid;
29 /* true if instead of fork(); exec("applet"); waitpid();
30 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
33 /* true if instead of fork(); exec("applet"); waitpid();
34 * one can simply call applet_main(argc,argv); */
38 /* Define struct bb_applet applets[] */
39 #include "../include/applets.h"
41 enum { NUM_APPLETS = ARRAY_SIZE(applets) };
43 static int offset[NUM_APPLETS];
45 static int cmp_name(const void *a, const void *b)
47 const struct bb_applet *aa = a;
48 const struct bb_applet *bb = b;
49 return strcmp(aa->name, bb->name);
52 int main(int argc, char **argv)
56 unsigned MAX_APPLET_NAME_LEN = 1;
58 qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
61 for (i = 0; i < NUM_APPLETS; i++) {
63 ofs += strlen(applets[i].name) + 1;
65 /* We reuse 4 high-order bits of offset array for other purposes,
66 * so if they are indeed needed, refuse to proceed */
72 i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
77 /* Keep in sync with include/busybox.h! */
79 printf("/* This is a generated file, don't edit */\n\n");
81 printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
82 if (NUM_APPLETS == 1) {
83 char *dash_to_underscore, *p;
84 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
85 /* Example: "ether-wake" -> "ether_wake" */
86 p = dash_to_underscore = strdup(applets[0].name);
91 printf("#define SINGLE_APPLET_MAIN %s_main\n", dash_to_underscore);
95 //printf("#ifndef SKIP_definitions\n");
96 printf("const char applet_names[] ALIGN1 = \"\"\n");
97 for (i = 0; i < NUM_APPLETS; i++) {
98 printf("\"%s\" \"\\0\"\n", applets[i].name);
99 if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
100 MAX_APPLET_NAME_LEN = strlen(applets[i].name);
104 printf("#ifndef SKIP_applet_main\n");
105 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
106 for (i = 0; i < NUM_APPLETS; i++) {
107 printf("%s_main,\n", applets[i].main);
110 printf("#endif\n\n");
112 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
113 for (i = 0; i < NUM_APPLETS; i++) {
116 #if ENABLE_FEATURE_PREFER_APPLETS
117 + (applets[i].nofork << 12)
118 + (applets[i].noexec << 13)
120 #if ENABLE_FEATURE_SUID
121 + (applets[i].need_suid << 14) /* 2 bits */
127 #if ENABLE_FEATURE_INSTALLER
128 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
130 while (i < NUM_APPLETS) {
131 int v = applets[i].install_loc; /* 3 bits */
132 if (++i < NUM_APPLETS)
133 v |= applets[i].install_loc << 4; /* 3 bits */
134 printf("0x%02x,\n", v);
139 //printf("#endif /* SKIP_definitions */\n");
141 printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
149 fp = fopen(argv[2], "r");
151 fgets(line_old, sizeof(line_old), fp);
154 sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
155 if (strcmp(line_old, line_new) != 0) {
156 fp = fopen(argv[2], "w");