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>
20 #define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
22 #include "../include/autoconf.h"
23 #include "../include/applet_metadata.h"
28 enum bb_install_loc_t install_loc
;
29 enum bb_suid_t need_suid
;
30 /* true if instead of fork(); exec("applet"); waitpid();
31 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
34 /* true if instead of fork(); exec("applet"); waitpid();
35 * one can simply call applet_main(argc,argv); */
39 /* Define struct bb_applet applets[] */
40 #include "../include/applets.h"
42 enum { NUM_APPLETS
= ARRAY_SIZE(applets
) };
44 static int offset
[NUM_APPLETS
];
46 static int cmp_name(const void *a
, const void *b
)
48 const struct bb_applet
*aa
= a
;
49 const struct bb_applet
*bb
= b
;
50 return strcmp(aa
->name
, bb
->name
);
53 static int str_isalnum_(const char *s
)
56 if (!isalnum(*s
) && *s
!= '_')
63 int main(int argc
, char **argv
)
67 // unsigned MAX_APPLET_NAME_LEN = 1;
69 qsort(applets
, NUM_APPLETS
, sizeof(applets
[0]), cmp_name
);
72 for (i
= 0; i
< NUM_APPLETS
; i
++) {
74 ofs
+= strlen(applets
[i
].name
) + 1;
76 /* We reuse 4 high-order bits of offset array for other purposes,
77 * so if they are indeed needed, refuse to proceed */
83 i
= open(argv
[1], O_WRONLY
| O_TRUNC
| O_CREAT
, 0666);
88 /* Keep in sync with include/busybox.h! */
90 printf("/* This is a generated file, don't edit */\n\n");
92 printf("#define NUM_APPLETS %u\n", NUM_APPLETS
);
93 if (NUM_APPLETS
== 1) {
94 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets
[0].name
);
95 printf("#define SINGLE_APPLET_MAIN %s_main\n", applets
[0].main
);
99 //printf("#ifndef SKIP_definitions\n");
100 printf("const char applet_names[] ALIGN1 = \"\"\n");
101 for (i
= 0; i
< NUM_APPLETS
; i
++) {
102 printf("\"%s\" \"\\0\"\n", applets
[i
].name
);
103 // if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
104 // MAX_APPLET_NAME_LEN = strlen(applets[i].name);
108 for (i
= 0; i
< NUM_APPLETS
; i
++) {
109 if (str_isalnum_(applets
[i
].name
))
110 printf("#define APPLET_NO_%s %d\n", applets
[i
].name
, i
);
114 printf("#ifndef SKIP_applet_main\n");
115 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
116 for (i
= 0; i
< NUM_APPLETS
; i
++) {
117 printf("%s_main,\n", applets
[i
].main
);
120 printf("#endif\n\n");
122 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
123 for (i
= 0; i
< NUM_APPLETS
; i
++) {
126 #if ENABLE_FEATURE_PREFER_APPLETS
127 + (applets
[i
].nofork
<< 12)
128 + (applets
[i
].noexec
<< 13)
130 #if ENABLE_FEATURE_SUID
131 + (applets
[i
].need_suid
<< 14) /* 2 bits */
137 #if ENABLE_FEATURE_INSTALLER
138 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
140 while (i
< NUM_APPLETS
) {
141 int v
= applets
[i
].install_loc
; /* 3 bits */
142 if (++i
< NUM_APPLETS
)
143 v
|= applets
[i
].install_loc
<< 4; /* 3 bits */
144 printf("0x%02x,\n", v
);
149 //printf("#endif /* SKIP_definitions */\n");
151 // printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
159 fp
= fopen(argv
[2], "r");
161 fgets(line_old
, sizeof(line_old
), fp
);
164 sprintf(line_new
, "#define NUM_APPLETS %u\n", NUM_APPLETS
);
165 if (strcmp(line_old
, line_new
) != 0) {
166 fp
= fopen(argv
[2], "w");