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
This commit is contained in:
Elliott Hughes 2016-04-11 13:25:25 -07:00
parent a10b4a0235
commit ad2c07e4b5
2 changed files with 21 additions and 34 deletions

View File

@ -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):

View File

@ -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;
}