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 | |
Denis Vlasenko | 32b2a9f | 2008-02-22 22:43:22 +0000 | [diff] [blame] | 35 | enum { NUM_APPLETS = ARRAY_SIZE(applets) }; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 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; |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 50 | unsigned MAX_APPLET_NAME_LEN = 1; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 51 | |
| 52 | qsort(applets, NUM_APPLETS, sizeof(applets[0]), cmp_name); |
| 53 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 54 | ofs = 0; |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 55 | for (i = 0; i < NUM_APPLETS; i++) { |
| 56 | offset[i] = ofs; |
| 57 | ofs += strlen(applets[i].name) + 1; |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 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; |
Denis Vlasenko | f7be20e | 2007-12-24 14:09:19 +0000 | [diff] [blame] | 69 | dup2(i, 1); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 70 | |
| 71 | /* Keep in sync with include/busybox.h! */ |
| 72 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 73 | printf("/* This is a generated file, don't edit */\n\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 74 | |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 75 | printf("#define NUM_APPLETS %u\n", NUM_APPLETS); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 76 | if (NUM_APPLETS == 1) { |
| 77 | printf("#define SINGLE_APPLET_STR \"%s\"\n", applets[0].name); |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 78 | printf("#define SINGLE_APPLET_MAIN %s_main\n", applets[0].name); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 79 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 80 | printf("\n"); |
Denis Vlasenko | 468aea2 | 2008-04-01 14:47:57 +0000 | [diff] [blame] | 81 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame^] | 82 | //printf("#ifndef SKIP_definitions\n"); |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 83 | printf("const char applet_names[] ALIGN1 = \"\"\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 84 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 85 | printf("\"%s\" \"\\0\"\n", applets[i].name); |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 86 | if (MAX_APPLET_NAME_LEN < strlen(applets[i].name)) |
| 87 | MAX_APPLET_NAME_LEN = strlen(applets[i].name); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 88 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 89 | printf(";\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 91 | printf("#ifndef SKIP_applet_main\n"); |
| 92 | printf("int (*const applet_main[])(int argc, char **argv) = {\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 93 | for (i = 0; i < NUM_APPLETS; i++) { |
| 94 | printf("%s_main,\n", applets[i].main); |
| 95 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 96 | printf("};\n"); |
| 97 | printf("#endif\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 98 | |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 99 | printf("const uint16_t applet_nameofs[] ALIGN2 = {\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 100 | for (i = 0; i < NUM_APPLETS; i++) { |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 101 | printf("0x%04x,\n", |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 102 | offset[i] |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 103 | #if ENABLE_FEATURE_PREFER_APPLETS |
| 104 | + (applets[i].nofork << 12) |
| 105 | + (applets[i].noexec << 13) |
| 106 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 107 | #if ENABLE_FEATURE_SUID |
Denis Vlasenko | 32b2a9f | 2008-02-22 22:43:22 +0000 | [diff] [blame] | 108 | + (applets[i].need_suid << 14) /* 2 bits */ |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 109 | #endif |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 110 | ); |
| 111 | } |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 112 | printf("};\n\n"); |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 113 | |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 114 | #if ENABLE_FEATURE_INSTALLER |
Denys Vlasenko | acabf8f | 2009-10-02 01:10:32 +0200 | [diff] [blame] | 115 | printf("const uint8_t applet_install_loc[] ALIGN1 = {\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 116 | i = 0; |
| 117 | while (i < NUM_APPLETS) { |
| 118 | int v = applets[i].install_loc; /* 3 bits */ |
| 119 | if (++i < NUM_APPLETS) |
| 120 | v |= applets[i].install_loc << 4; /* 3 bits */ |
| 121 | printf("0x%02x,\n", v); |
| 122 | i++; |
| 123 | } |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 124 | printf("};\n"); |
Denis Vlasenko | 745cd17 | 2007-11-29 03:31:20 +0000 | [diff] [blame] | 125 | #endif |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame^] | 126 | //printf("#endif /* SKIP_definitions */\n"); |
Denys Vlasenko | 1497484 | 2010-03-23 01:08:26 +0100 | [diff] [blame] | 127 | printf("\n"); |
Denis Vlasenko | 79cedcb | 2008-04-08 21:13:28 +0000 | [diff] [blame] | 128 | printf("#define MAX_APPLET_NAME_LEN %u\n", MAX_APPLET_NAME_LEN); |
| 129 | |
Denys Vlasenko | 1fcbff2 | 2010-06-26 02:40:08 +0200 | [diff] [blame^] | 130 | if (argv[2]) { |
| 131 | char line_old[80]; |
| 132 | char line_new[80]; |
| 133 | FILE *fp; |
| 134 | |
| 135 | line_old[0] = 0; |
| 136 | fp = fopen(argv[2], "r"); |
| 137 | if (fp) { |
| 138 | fgets(line_old, sizeof(line_old), fp); |
| 139 | fclose(fp); |
| 140 | } |
| 141 | sprintf(line_new, "#define NUM_APPLETS %u\n", NUM_APPLETS); |
| 142 | if (strcmp(line_old, line_new) != 0) { |
| 143 | fp = fopen(argv[2], "w"); |
| 144 | if (!fp) |
| 145 | return 1; |
| 146 | fputs(line_new, fp); |
| 147 | } |
| 148 | } |
| 149 | |
Denis Vlasenko | 41f5add | 2007-11-28 06:49:42 +0000 | [diff] [blame] | 150 | return 0; |
| 151 | } |