| Elliott Hughes | 5976018 | 2015-04-25 11:49:04 -0700 | [diff] [blame] | 1 | #include <signal.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 2 | #include <stdio.h> | 
|  | 3 | #include <stdlib.h> | 
|  | 4 | #include <string.h> | 
| Elliott Hughes | 5976018 | 2015-04-25 11:49:04 -0700 | [diff] [blame] | 5 | #include <unistd.h> | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 6 |  | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 7 | #define TOOL(name) int name##_main(int, char**); | 
|  | 8 | #include "tools.h" | 
|  | 9 | #undef TOOL | 
|  | 10 |  | 
| Elliott Hughes | ad2c07e | 2016-04-11 13:25:25 -0700 | [diff] [blame] | 11 | static struct { | 
|  | 12 | const char* name; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 13 | int (*func)(int, char**); | 
|  | 14 | } tools[] = { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 15 | #define TOOL(name) { #name, name##_main }, | 
|  | 16 | #include "tools.h" | 
|  | 17 | #undef TOOL | 
|  | 18 | { 0, 0 }, | 
|  | 19 | }; | 
|  | 20 |  | 
| Elliott Hughes | 5976018 | 2015-04-25 11:49:04 -0700 | [diff] [blame] | 21 | static void SIGPIPE_handler(int signal) { | 
|  | 22 | // Those desktop Linux tools that catch SIGPIPE seem to agree that it's | 
|  | 23 | // a successful way to exit, not a failure. (Which makes sense --- we were | 
|  | 24 | // told to stop by a reader, rather than failing to continue ourselves.) | 
|  | 25 | _exit(0); | 
|  | 26 | } | 
|  | 27 |  | 
| Elliott Hughes | ad2c07e | 2016-04-11 13:25:25 -0700 | [diff] [blame] | 28 | int main(int argc, char** argv) { | 
| Elliott Hughes | 5976018 | 2015-04-25 11:49:04 -0700 | [diff] [blame] | 29 | // Let's assume that none of this code handles broken pipes. At least ls, | 
|  | 30 | // ps, and top were broken (though I'd previously added this fix locally | 
|  | 31 | // to top). We exit rather than use SIG_IGN because tools like top will | 
|  | 32 | // just keep on writing to nowhere forever if we don't stop them. | 
|  | 33 | signal(SIGPIPE, SIGPIPE_handler); | 
|  | 34 |  | 
| Elliott Hughes | ad2c07e | 2016-04-11 13:25:25 -0700 | [diff] [blame] | 35 | char* cmd = strrchr(argv[0], '/'); | 
|  | 36 | char* name = cmd ? (cmd + 1) : argv[0]; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 37 |  | 
| Elliott Hughes | ad2c07e | 2016-04-11 13:25:25 -0700 | [diff] [blame] | 38 | for (size_t i = 0; tools[i].name; i++) { | 
|  | 39 | if (!strcmp(tools[i].name, name)) { | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 40 | return tools[i].func(argc, argv); | 
|  | 41 | } | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | printf("%s: no such tool\n", argv[0]); | 
| Elliott Hughes | 22b3567 | 2016-04-12 08:40:43 -0700 | [diff] [blame] | 45 | return 127; | 
| The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 46 | } | 
| Elliott Hughes | ad2c07e | 2016-04-11 13:25:25 -0700 | [diff] [blame] | 47 |  | 
|  | 48 | int toolbox_main(int argc, char** argv) { | 
|  | 49 | // "toolbox foo ..." is equivalent to "foo ..." | 
|  | 50 | if (argc > 1) { | 
|  | 51 | return main(argc - 1, argv + 1); | 
|  | 52 | } | 
|  | 53 |  | 
|  | 54 | // Plain "toolbox" lists the tools. | 
|  | 55 | for (size_t i = 1; tools[i].name; i++) { | 
|  | 56 | printf("%s%c", tools[i].name, tools[i+1].name ? ' ' : '\n'); | 
|  | 57 | } | 
|  | 58 | return 0; | 
|  | 59 | } |