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 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets
[0].name
);
84 printf("#define SINGLE_APPLET_MAIN %s_main\n", applets
[0].main
);
88 //printf("#ifndef SKIP_definitions\n");
89 printf("const char applet_names[] ALIGN1 = \"\"\n");
90 for (i
= 0; i
< NUM_APPLETS
; i
++) {
91 printf("\"%s\" \"\\0\"\n", applets
[i
].name
);
92 if (MAX_APPLET_NAME_LEN
< strlen(applets
[i
].name
))
93 MAX_APPLET_NAME_LEN
= strlen(applets
[i
].name
);
97 printf("#ifndef SKIP_applet_main\n");
98 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
99 for (i
= 0; i
< NUM_APPLETS
; i
++) {
100 printf("%s_main,\n", applets
[i
].main
);
103 printf("#endif\n\n");
105 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
106 for (i
= 0; i
< NUM_APPLETS
; i
++) {
109 #if ENABLE_FEATURE_PREFER_APPLETS
110 + (applets
[i
].nofork
<< 12)
111 + (applets
[i
].noexec
<< 13)
113 #if ENABLE_FEATURE_SUID
114 + (applets
[i
].need_suid
<< 14) /* 2 bits */
120 #if ENABLE_FEATURE_INSTALLER
121 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
123 while (i
< NUM_APPLETS
) {
124 int v
= applets
[i
].install_loc
; /* 3 bits */
125 if (++i
< NUM_APPLETS
)
126 v
|= applets
[i
].install_loc
<< 4; /* 3 bits */
127 printf("0x%02x,\n", v
);
132 //printf("#endif /* SKIP_definitions */\n");
134 printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN
);
142 fp
= fopen(argv
[2], "r");
144 fgets(line_old
, sizeof(line_old
), fp
);
147 sprintf(line_new
, "#define NUM_APPLETS %u\n", NUM_APPLETS
);
148 if (strcmp(line_old
, line_new
) != 0) {
149 fp
= fopen(argv
[2], "w");