Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 53 | ofs = 0; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 54 | for (i = 0; i < NUM_APPLETS; i++) { |
| 55 | offset[i] = ofs; |
| 56 | ofs += strlen(applets[i].name) + 1; |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 57 | } |
| 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; |
| 68 | dup2(i, 1); |
| 69 | |
| 70 | /* Keep in sync with include/busybox.h! */ |
| 71 | |
| 72 | puts("/* This is a generated file, don't edit */"); |
| 73 | |
| 74 | puts("const char applet_names[] ALIGN1 ="); |
| 75 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 76 | printf("\"%s\" \"\\0\"\n", applets[i].name); |
| 77 | } |
| 78 | puts(";"); |
| 79 | |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 80 | puts("int (*const applet_main[])(int argc, char **argv) = {"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 81 | for (i = 0; i < NUM_APPLETS; i++) { |
| 82 | printf("%s_main,\n", applets[i].main); |
| 83 | } |
| 84 | puts("};"); |
| 85 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 86 | puts("const uint16_t applet_nameofs[] ALIGN2 = {"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 87 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 88 | printf("0x%04x,\n", |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 89 | offset[i] |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 90 | #if ENABLE_FEATURE_PREFER_APPLETS |
| 91 | + (applets[i].nofork << 12) |
| 92 | + (applets[i].noexec << 13) |
| 93 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 94 | #if ENABLE_FEATURE_SUID |
| 95 | + (applets[i].need_suid << 14) /* 2 bits */ |
| 96 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 97 | ); |
| 98 | } |
| 99 | puts("};"); |
| 100 | |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 101 | #if ENABLE_FEATURE_INSTALLER |
| 102 | puts("const uint8_t applet_install_loc[] ALIGN1 = {"); |
| 103 | i = 0; |
| 104 | while (i < NUM_APPLETS) { |
| 105 | int v = applets[i].install_loc; /* 3 bits */ |
| 106 | if (++i < NUM_APPLETS) |
| 107 | v |= applets[i].install_loc << 4; /* 3 bits */ |
| 108 | printf("0x%02x,\n", v); |
| 109 | i++; |
| 110 | } |
| 111 | puts("};"); |
| 112 | #endif |
| 113 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 114 | return 0; |
| 115 | } |