blob: 35b099eb423ab1a6b566cfce9b516f41aa0c82fa [file] [log] [blame]
Denis Vlasenko41f5add2007-11-28 06:49:42 +00001/* 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
18struct 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
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +000035enum { NUM_APPLETS = ARRAY_SIZE(applets) };
Denis Vlasenko41f5add2007-11-28 06:49:42 +000036
37static int offset[NUM_APPLETS];
38
39static 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
46int main(int argc, char **argv)
47{
48 int i;
49 int ofs;
50
51 qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
52
Denis Vlasenko41f5add2007-11-28 06:49:42 +000053 ofs = 0;
Denis Vlasenko41f5add2007-11-28 06:49:42 +000054 for (i = 0; i < NUM_APPLETS; i++) {
55 offset[i] = ofs;
56 ofs += strlen(applets[i].name) + 1;
Denis Vlasenko745cd172007-11-29 03:31:20 +000057 }
58 /* We reuse 4 high-order bits of offset array for other purposes,
59 * so if they are indeed needed, refuse to proceed */
60 if (ofs > 0xfff)
61 return 1;
62 if (!argv[1])
63 return 1;
64
65 i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
66 if (i < 0)
67 return 1;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000068 dup2(i, 1);
Denis Vlasenko745cd172007-11-29 03:31:20 +000069
70 /* Keep in sync with include/busybox.h! */
71
Denis Vlasenko468aea22008-04-01 14:47:57 +000072 puts("/* This is a generated file, don't edit */\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +000073
Denis Vlasenko468aea22008-04-01 14:47:57 +000074 if (NUM_APPLETS == 1) {
75 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
76 printf("#define SINGLE_APPLET_MAIN %s_main\n\n", applets[0].name);
77 }
78
79 puts("const char applet_names[] ALIGN1 = \"\"");
Denis Vlasenko745cd172007-11-29 03:31:20 +000080 for (i = 0; i < NUM_APPLETS; i++) {
Denis Vlasenko41f5add2007-11-28 06:49:42 +000081 printf("\"%s\" \"\\0\"\n", applets[i].name);
82 }
83 puts(";");
84
Denis Vlasenko745cd172007-11-29 03:31:20 +000085 puts("int (*const applet_main[])(int argc, char **argv) = {");
Denis Vlasenko41f5add2007-11-28 06:49:42 +000086 for (i = 0; i < NUM_APPLETS; i++) {
87 printf("%s_main,\n", applets[i].main);
88 }
89 puts("};");
90
Denis Vlasenko41f5add2007-11-28 06:49:42 +000091 puts("const uint16_t applet_nameofs[] ALIGN2 = {");
Denis Vlasenko41f5add2007-11-28 06:49:42 +000092 for (i = 0; i < NUM_APPLETS; i++) {
Denis Vlasenko745cd172007-11-29 03:31:20 +000093 printf("0x%04x,\n",
Denis Vlasenko41f5add2007-11-28 06:49:42 +000094 offset[i]
Denis Vlasenko745cd172007-11-29 03:31:20 +000095#if ENABLE_FEATURE_PREFER_APPLETS
96 + (applets[i].nofork << 12)
97 + (applets[i].noexec << 13)
98#endif
Denis Vlasenko41f5add2007-11-28 06:49:42 +000099#if ENABLE_FEATURE_SUID
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +0000100 + (applets[i].need_suid << 14) /* 2 bits */
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000101#endif
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000102 );
103 }
104 puts("};");
105
Denis Vlasenko745cd172007-11-29 03:31:20 +0000106#if ENABLE_FEATURE_INSTALLER
107 puts("const uint8_t applet_install_loc[] ALIGN1 = {");
108 i = 0;
109 while (i < NUM_APPLETS) {
110 int v = applets[i].install_loc; /* 3 bits */
111 if (++i < NUM_APPLETS)
112 v |= applets[i].install_loc << 4; /* 3 bits */
113 printf("0x%02x,\n", v);
114 i++;
115 }
116 puts("};");
117#endif
118
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000119 return 0;
120}