forgotten part of previous commit
[platform/upstream/busybox.git] / applets / applet_tables.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Applet table generator.
4  * Runs on host and produces include/applet_tables.h
5  *
6  * Copyright (C) 2007 Denys Vlasenko <vda.linux@googlemail.com>
7  *
8  * Licensed under GPLv2, see file License in this tarball for details.
9  */
10
11 #include <stdlib.h>
12 #include <string.h>
13 #include <stdio.h>
14
15 #include "../include/autoconf.h"
16 #include "../include/busybox.h"
17
18 struct bb_applet {
19         const char *name;
20         const char *main;
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(); */
25         unsigned char noexec;
26         /* Even nicer */
27         /* true if instead of fork(); exec("applet"); waitpid();
28          * one can simply call applet_main(argc,argv); */
29         unsigned char nofork;
30 };
31
32 /* Define struct bb_applet applets[] */
33 #include "../include/applets.h"
34
35 enum { NUM_APPLETS = sizeof(applets)/sizeof(applets[0]) };
36
37 static int offset[NUM_APPLETS];
38
39 static int cmp_name(const void *a, const void *b)
40 {
41         const struct bb_applet *aa = a;
42         const struct bb_applet *bb = b;
43         return strcmp(aa->name, bb->name);
44 }
45
46 int main(int argc, char **argv)
47 {
48         int i;
49         int ofs;
50
51         qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
52
53         /* Keep in sync with include/busybox.h! */
54
55         puts("const char applet_names[] ALIGN1 =");
56         ofs = 0;
57         i = 0;
58         for (i = 0; i < NUM_APPLETS; i++) {
59                 offset[i] = ofs;
60                 ofs += strlen(applets[i].name) + 1;
61                 printf("\"%s\" \"\\0\"\n", applets[i].name);
62         }
63         puts(";");
64
65         puts("int (*const applet_mains[])(int argc, char **argv) = {");
66         for (i = 0; i < NUM_APPLETS; i++) {
67                 printf("%s_main,\n", applets[i].main);
68         }
69         puts("};");
70
71 #if ENABLE_FEATURE_INSTALLER || ENABLE_FEATURE_PREFER_APPLETS
72         puts("const uint32_t applet_nameofs[] = {");
73 #else
74         puts("const uint16_t applet_nameofs[] ALIGN2 = {");
75 #endif
76         for (i = 0; i < NUM_APPLETS; i++) {
77                 printf("0x%08x,\n",
78                         offset[i]
79 #if ENABLE_FEATURE_SUID
80                         + (applets[i].need_suid   << 14) /* 2 bits */
81 #endif
82 #if ENABLE_FEATURE_INSTALLER
83                         + (applets[i].install_loc << 16) /* 3 bits */
84 #endif
85 #if ENABLE_FEATURE_PREFER_APPLETS
86                         + (applets[i].nofork << 19)
87                         + (applets[i].noexec << 20)
88 #endif
89                 );
90         }
91         puts("};");
92
93         return 0;
94 }