diff options
| author | Elliott Hughes <enh@google.com> | 2016-04-11 13:25:25 -0700 |
|---|---|---|
| committer | Elliott Hughes <enh@google.com> | 2016-04-11 17:45:49 -0700 |
| commit | ad2c07e4b5820010b1be6231a9cfbb94b207ded7 (patch) | |
| tree | 3ee52a7fa0ca01a5dd382a6454b91021fc0b1f45 | |
| parent | a10b4a0235ed5e3a219a41080988ece185f5dea8 (diff) | |
| download | system_core-ad2c07e4b5820010b1be6231a9cfbb94b207ded7.tar.gz system_core-ad2c07e4b5820010b1be6231a9cfbb94b207ded7.tar.bz2 system_core-ad2c07e4b5820010b1be6231a9cfbb94b207ded7.zip | |
Clean up toolbox runner slightly.
Remove the weird @command syntax, and make toolbox itself list the available
commands, like toybox does. Also stop special-casing toolbox itself.
Change-Id: I77dfaaf0cf42e375d866b77362dd8afa717fb2e1
| -rw-r--r-- | toolbox/Android.mk | 2 | ||||
| -rw-r--r-- | toolbox/toolbox.c | 53 |
2 files changed, 21 insertions, 34 deletions
diff --git a/toolbox/Android.mk b/toolbox/Android.mk index b1cdb607e..8b3241864 100644 --- a/toolbox/Android.mk +++ b/toolbox/Android.mk @@ -72,7 +72,7 @@ include $(BUILD_EXECUTABLE) $(LOCAL_PATH)/toolbox.c: $(intermediates)/tools.h TOOLS_H := $(intermediates)/tools.h -$(TOOLS_H): PRIVATE_TOOLS := $(ALL_TOOLS) +$(TOOLS_H): PRIVATE_TOOLS := toolbox $(ALL_TOOLS) $(TOOLS_H): PRIVATE_CUSTOM_TOOL = echo "/* file generated automatically */" > $@ ; for t in $(PRIVATE_TOOLS) ; do echo "TOOL($$t)" >> $@ ; done $(TOOLS_H): $(LOCAL_PATH)/Android.mk $(TOOLS_H): diff --git a/toolbox/toolbox.c b/toolbox/toolbox.c index 915da440c..6bce5ed41 100644 --- a/toolbox/toolbox.c +++ b/toolbox/toolbox.c @@ -4,29 +4,14 @@ #include <string.h> #include <unistd.h> -int main(int, char **); - -static int toolbox_main(int argc, char **argv) -{ - // "toolbox foo ..." is equivalent to "foo ..." - if (argc > 1) { - return main(argc - 1, argv + 1); - } else { - printf("Toolbox!\n"); - return 0; - } -} - #define TOOL(name) int name##_main(int, char**); #include "tools.h" #undef TOOL -static struct -{ - const char *name; +static struct { + const char* name; int (*func)(int, char**); } tools[] = { - { "toolbox", toolbox_main }, #define TOOL(name) { #name, name##_main }, #include "tools.h" #undef TOOL @@ -40,29 +25,18 @@ static void SIGPIPE_handler(int signal) { _exit(0); } -int main(int argc, char **argv) -{ - int i; - char *name = argv[0]; - +int main(int argc, char** argv) { // Let's assume that none of this code handles broken pipes. At least ls, // ps, and top were broken (though I'd previously added this fix locally // to top). We exit rather than use SIG_IGN because tools like top will // just keep on writing to nowhere forever if we don't stop them. signal(SIGPIPE, SIGPIPE_handler); - if((argc > 1) && (argv[1][0] == '@')) { - name = argv[1] + 1; - argc--; - argv++; - } else { - char *cmd = strrchr(argv[0], '/'); - if (cmd) - name = cmd + 1; - } + char* cmd = strrchr(argv[0], '/'); + char* name = cmd ? (cmd + 1) : argv[0]; - for(i = 0; tools[i].name; i++){ - if(!strcmp(tools[i].name, name)){ + for (size_t i = 0; tools[i].name; i++) { + if (!strcmp(tools[i].name, name)) { return tools[i].func(argc, argv); } } @@ -70,3 +44,16 @@ int main(int argc, char **argv) printf("%s: no such tool\n", argv[0]); return -1; } + +int toolbox_main(int argc, char** argv) { + // "toolbox foo ..." is equivalent to "foo ..." + if (argc > 1) { + return main(argc - 1, argv + 1); + } + + // Plain "toolbox" lists the tools. + for (size_t i = 1; tools[i].name; i++) { + printf("%s%c", tools[i].name, tools[i+1].name ? ' ' : '\n'); + } + return 0; +} |
