udhcpc: kill undocumented -W, it was a no-op.
[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 = ARRAY_SIZE(applets) };
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         unsigned MAX_APPLET_NAME_LEN = 1;
51
52         qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
53
54         ofs = 0;
55         for (i = 0; i < NUM_APPLETS; i++) {
56                 offset[i] = ofs;
57                 ofs += strlen(applets[i].name) + 1;
58         }
59         /* We reuse 4 high-order bits of offset array for other purposes,
60          * so if they are indeed needed, refuse to proceed */
61         if (ofs > 0xfff)
62                 return 1;
63         if (!argv[1])
64                 return 1;
65
66         i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
67         if (i < 0)
68                 return 1;
69         dup2(i, 1);
70
71         /* Keep in sync with include/busybox.h! */
72
73         puts("/* This is a generated file, don't edit */\n");
74
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);
79         }
80
81         puts("\nconst char applet_names[] ALIGN1 = \"\"");
82         for (i = 0; i < NUM_APPLETS; i++) {
83                 printf("\"%s\" \"\\0\"\n", applets[i].name);
84                 if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
85                         MAX_APPLET_NAME_LEN = strlen(applets[i].name);
86         }
87         puts(";");
88
89         puts("\nint (*const applet_main[])(int argc, char **argv) = {");
90         for (i = 0; i < NUM_APPLETS; i++) {
91                 printf("%s_main,\n", applets[i].main);
92         }
93         puts("};");
94
95         puts("const uint16_t applet_nameofs[] ALIGN2 = {");
96         for (i = 0; i < NUM_APPLETS; i++) {
97                 printf("0x%04x,\n",
98                         offset[i]
99 #if ENABLE_FEATURE_PREFER_APPLETS
100                         + (applets[i].nofork << 12)
101                         + (applets[i].noexec << 13)
102 #endif
103 #if ENABLE_FEATURE_SUID
104                         + (applets[i].need_suid << 14) /* 2 bits */
105 #endif
106                 );
107         }
108         puts("};");
109
110 #if ENABLE_FEATURE_INSTALLER
111         puts("const uint8_t applet_install_loc[] ALIGN1 = {");
112         i = 0;
113         while (i < NUM_APPLETS) {
114                 int v = applets[i].install_loc; /* 3 bits */
115                 if (++i < NUM_APPLETS)
116                         v |= applets[i].install_loc << 4; /* 3 bits */
117                 printf("0x%02x,\n", v);
118                 i++;
119         }
120         puts("};\n");
121 #endif
122
123         printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
124
125         return 0;
126 }