blob: a47574737f7ef84f84aab48b70efd6b8dd626a31 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2, see file LICENSE in this source tree.
Denis Vlasenko41f5add2007-11-28 06:49:42 +00009 */
Denys Vlasenko6ec15102011-03-28 01:23:38 +020010#include <sys/types.h>
11#include <sys/stat.h>
12#include <fcntl.h>
Denis Vlasenko41f5add2007-11-28 06:49:42 +000013#include <stdlib.h>
14#include <string.h>
15#include <stdio.h>
Denys Vlasenko6ec15102011-03-28 01:23:38 +020016#include <unistd.h>
17
18#undef ARRAY_SIZE
19#define ARRAY_SIZE(x) ((unsigned)(sizeof(x) / sizeof((x)[0])))
Denis Vlasenko41f5add2007-11-28 06:49:42 +000020
21#include "../include/autoconf.h"
Denys Vlasenko6ec15102011-03-28 01:23:38 +020022#include "../include/applet_metadata.h"
Denis Vlasenko41f5add2007-11-28 06:49:42 +000023
24struct bb_applet {
25 const char *name;
26 const char *main;
27 enum bb_install_loc_t install_loc;
28 enum bb_suid_t need_suid;
29 /* true if instead of fork(); exec("applet"); waitpid();
30 * one can do fork(); exit(applet_main(argc,argv)); waitpid(); */
31 unsigned char noexec;
32 /* Even nicer */
33 /* true if instead of fork(); exec("applet"); waitpid();
34 * one can simply call applet_main(argc,argv); */
35 unsigned char nofork;
36};
37
38/* Define struct bb_applet applets[] */
39#include "../include/applets.h"
40
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +000041enum { NUM_APPLETS = ARRAY_SIZE(applets) };
Denis Vlasenko41f5add2007-11-28 06:49:42 +000042
43static int offset[NUM_APPLETS];
44
45static int cmp_name(const void *a, const void *b)
46{
47 const struct bb_applet *aa = a;
48 const struct bb_applet *bb = b;
49 return strcmp(aa->name, bb->name);
50}
51
52int main(int argc, char **argv)
53{
54 int i;
55 int ofs;
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000056 unsigned MAX_APPLET_NAME_LEN = 1;
Denis Vlasenko41f5add2007-11-28 06:49:42 +000057
58 qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name);
59
Denis Vlasenko41f5add2007-11-28 06:49:42 +000060 ofs = 0;
Denis Vlasenko41f5add2007-11-28 06:49:42 +000061 for (i = 0; i < NUM_APPLETS; i++) {
62 offset[i] = ofs;
63 ofs += strlen(applets[i].name) + 1;
Denis Vlasenko745cd172007-11-29 03:31:20 +000064 }
65 /* We reuse 4 high-order bits of offset array for other purposes,
66 * so if they are indeed needed, refuse to proceed */
67 if (ofs > 0xfff)
68 return 1;
69 if (!argv[1])
70 return 1;
71
72 i = open(argv[1], O_WRONLY | O_TRUNC | O_CREAT, 0666);
73 if (i < 0)
74 return 1;
Denis Vlasenkof7be20e2007-12-24 14:09:19 +000075 dup2(i, 1);
Denis Vlasenko745cd172007-11-29 03:31:20 +000076
77 /* Keep in sync with include/busybox.h! */
78
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +020079 printf("/* This is a generated file, don't edit */\n\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +000080
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000081 printf("#define NUM_APPLETS %u\n", NUM_APPLETS);
Denis Vlasenko468aea22008-04-01 14:47:57 +000082 if (NUM_APPLETS == 1) {
Denys Vlasenko6d2d6522011-09-16 14:28:32 +020083 char *dash_to_underscore, *p;
Denis Vlasenko468aea22008-04-01 14:47:57 +000084 printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name);
Denys Vlasenko6d2d6522011-09-16 14:28:32 +020085 /* Example: "ether-wake" -> "ether_wake" */
86 p = dash_to_underscore = strdup(applets[0].name);
87 p--;
88 while (*++p)
89 if (*p == '-')
90 *p = '_';
91 printf("#define SINGLE_APPLET_MAIN %s_main\n", dash_to_underscore);
Denis Vlasenko468aea22008-04-01 14:47:57 +000092 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +020093 printf("\n");
Denis Vlasenko468aea22008-04-01 14:47:57 +000094
Denys Vlasenko1fcbff22010-06-26 02:40:08 +020095 //printf("#ifndef SKIP_definitions\n");
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +020096 printf("const char applet_names[] ALIGN1 = \"\"\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +000097 for (i = 0; i < NUM_APPLETS; i++) {
Denis Vlasenko41f5add2007-11-28 06:49:42 +000098 printf("\"%s\" \"\\0\"\n", applets[i].name);
Denis Vlasenko79cedcb2008-04-08 21:13:28 +000099 if (MAX_APPLET_NAME_LEN < strlen(applets[i].name))
100 MAX_APPLET_NAME_LEN = strlen(applets[i].name);
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000101 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200102 printf(";\n\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000103
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200104 printf("#ifndef SKIP_applet_main\n");
105 printf("int (*const applet_main[])(int argc, char **argv) = {\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000106 for (i = 0; i < NUM_APPLETS; i++) {
107 printf("%s_main,\n", applets[i].main);
108 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200109 printf("};\n");
110 printf("#endif\n\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000111
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200112 printf("const uint16_t applet_nameofs[] ALIGN2 = {\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000113 for (i = 0; i < NUM_APPLETS; i++) {
Denis Vlasenko745cd172007-11-29 03:31:20 +0000114 printf("0x%04x,\n",
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000115 offset[i]
Denis Vlasenko745cd172007-11-29 03:31:20 +0000116#if ENABLE_FEATURE_PREFER_APPLETS
117 + (applets[i].nofork << 12)
118 + (applets[i].noexec << 13)
119#endif
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000120#if ENABLE_FEATURE_SUID
Denis Vlasenko32b2a9f2008-02-22 22:43:22 +0000121 + (applets[i].need_suid << 14) /* 2 bits */
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000122#endif
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000123 );
124 }
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200125 printf("};\n\n");
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000126
Denis Vlasenko745cd172007-11-29 03:31:20 +0000127#if ENABLE_FEATURE_INSTALLER
Denys Vlasenkoacabf8f2009-10-02 01:10:32 +0200128 printf("const uint8_t applet_install_loc[] ALIGN1 = {\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +0000129 i = 0;
130 while (i < NUM_APPLETS) {
131 int v = applets[i].install_loc; /* 3 bits */
132 if (++i < NUM_APPLETS)
133 v |= applets[i].install_loc << 4; /* 3 bits */
134 printf("0x%02x,\n", v);
135 i++;
136 }
Denys Vlasenko14974842010-03-23 01:08:26 +0100137 printf("};\n");
Denis Vlasenko745cd172007-11-29 03:31:20 +0000138#endif
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200139 //printf("#endif /* SKIP_definitions */\n");
Denys Vlasenko14974842010-03-23 01:08:26 +0100140 printf("\n");
Denis Vlasenko79cedcb2008-04-08 21:13:28 +0000141 printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN);
142
Denys Vlasenko1fcbff22010-06-26 02:40:08 +0200143 if (argv[2]) {
144 char line_old[80];
145 char line_new[80];
146 FILE *fp;
147
148 line_old[0] = 0;
149 fp = fopen(argv[2], "r");
150 if (fp) {
151 fgets(line_old, sizeof(line_old), fp);
152 fclose(fp);
153 }
154 sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS);
155 if (strcmp(line_old, line_new) != 0) {
156 fp = fopen(argv[2], "w");
157 if (!fp)
158 return 1;
159 fputs(line_new, fp);
160 }
161 }
162
Denis Vlasenko41f5add2007-11-28 06:49:42 +0000163 return 0;
164}