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 tarball for details.
15 #include "../include/autoconf.h"
16 #include "../include/busybox.h"
21 enum bb_install_loc_t install_loc;
22 enum bb_suid_t need_suid;
23 /* true if instead of fork(); exec("applet"); waitpid();
24 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
27 /* true if instead of fork(); exec("applet"); waitpid();
28 * one can simply call applet_main(argc,argv); */
32 /* Define struct bb_applet applets[] */
33 #include "../include/applets.h"
35 enum { NUM_APPLETS = ARRAY_SIZE(applets) };
37 static int offset[NUM_APPLETS];
39 static int cmp_name(const void *a, const void *b)
41 const struct bb_applet *aa = a;
42 const struct bb_applet *bb = b;
43 return strcmp(aa->name, bb->name);
46 int main(int argc, char **argv)
50 unsigned MAX_APPLET_NAME_LEN = 1;
52 qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
55 for (i = 0; i < NUM_APPLETS; i++) {
57 ofs += strlen(applets[i].name) + 1;
59 /* We reuse 4 high-order bits of offset array for other purposes,
60 * so if they are indeed needed, refuse to proceed */
66 i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
71 /* Keep in sync with include/busybox.h! */
73 printf("/* This is a generated file, don't edit */\n\n");
75 printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
76 if (NUM_APPLETS == 1) {
77 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
78 printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].name);
82 //printf("#ifndef SKIP_definitions\n");
83 printf("const char applet_names[] ALIGN1 = \"\"\n");
84 for (i = 0; i < NUM_APPLETS; i++) {
85 printf("\"%s\" \"\\0\"\n", applets[i].name);
86 if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
87 MAX_APPLET_NAME_LEN = strlen(applets[i].name);
91 printf("#ifndef SKIP_applet_main\n");
92 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
93 for (i = 0; i < NUM_APPLETS; i++) {
94 printf("%s_main,\n", applets[i].main);
99 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
100 for (i = 0; i < NUM_APPLETS; i++) {
103 #if ENABLE_FEATURE_PREFER_APPLETS
104 + (applets[i].nofork << 12)
105 + (applets[i].noexec << 13)
107 #if ENABLE_FEATURE_SUID
108 + (applets[i].need_suid << 14) /* 2 bits */
114 #if ENABLE_FEATURE_INSTALLER
115 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
117 while (i < NUM_APPLETS) {
118 int v = applets[i].install_loc; /* 3 bits */
119 if (++i < NUM_APPLETS)
120 v |= applets[i].install_loc << 4; /* 3 bits */
121 printf("0x%02x,\n", v);
126 //printf("#endif /* SKIP_definitions */\n");
128 printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
136 fp = fopen(argv[2], "r");
138 fgets(line_old, sizeof(line_old), fp);
141 sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
142 if (strcmp(line_old, line_new) != 0) {
143 fp = fopen(argv[2], "w");