From d6365a70527275f700b9594452020715a0c52510 Mon Sep 17 00:00:00 2001 From: Elliott Hughes Date: Mon, 8 May 2017 18:04:49 -0700 Subject: [PATCH] Make fastboot command-line parsing a bit more like adb. Only show all the help if asked to, and have a few more descriptive syntax errors. Also show the help on stdout rather than stderr. Bug: N/A Test: manually ran "fastboot flash"/"fastboot update"/"fastboot flashall" Change-Id: I59abd60e58a56fe7e44da5116a702087c36e14ce --- fastboot/engine.cpp | 36 ++-- fastboot/fastboot.cpp | 412 ++++++++++++++++++------------------------ fastboot/fastboot.h | 2 +- 3 files changed, 197 insertions(+), 253 deletions(-) diff --git a/fastboot/engine.cpp b/fastboot/engine.cpp index 56ee81637..7e10cc9bf 100644 --- a/fastboot/engine.cpp +++ b/fastboot/engine.cpp @@ -258,6 +258,12 @@ static int cb_reject(Action* a, int status, const char* resp) { return cb_check(a, status, resp, 1); } +static char* xstrdup(const char* s) { + char* result = strdup(s); + if (!result) die("out of memory"); + return result; +} + void fb_queue_require(const char *prod, const char *var, bool invert, size_t nvalues, const char **value) { @@ -276,16 +282,14 @@ static int cb_display(Action* a, int status, const char* resp) { fprintf(stderr, "%s FAILED (%s)\n", a->cmd, resp); return status; } - fprintf(stderr, "%s: %s\n", (char*) a->data, resp); + fprintf(stderr, "%s: %s\n", static_cast(a->data), resp); + free(static_cast(a->data)); return 0; } -void fb_queue_display(const char *var, const char *prettyname) -{ - Action *a; - a = queue_action(OP_QUERY, "getvar:%s", var); - a->data = strdup(prettyname); - if (a->data == nullptr) die("out of memory"); +void fb_queue_display(const char* var, const char* prettyname) { + Action* a = queue_action(OP_QUERY, "getvar:%s", var); + a->data = xstrdup(prettyname); a->func = cb_display; } @@ -298,11 +302,9 @@ static int cb_save(Action* a, int status, const char* resp) { return 0; } -void fb_queue_query_save(const char *var, char *dest, uint32_t dest_size) -{ - Action *a; - a = queue_action(OP_QUERY, "getvar:%s", var); - a->data = (void *)dest; +void fb_queue_query_save(const char* var, char* dest, uint32_t dest_size) { + Action* a = queue_action(OP_QUERY, "getvar:%s", var); + a->data = dest; a->size = dest_size; a->func = cb_save; } @@ -342,15 +344,13 @@ void fb_queue_download_fd(const char *name, int fd, uint32_t sz) a->msg = mkmsg("sending '%s' (%d KB)", name, sz / 1024); } -void fb_queue_upload(char *outfile) -{ - Action *a = queue_action(OP_UPLOAD, ""); - a->data = outfile; +void fb_queue_upload(const char* outfile) { + Action* a = queue_action(OP_UPLOAD, ""); + a->data = xstrdup(outfile); a->msg = mkmsg("uploading '%s'", outfile); } -void fb_queue_notice(const char *notice) -{ +void fb_queue_notice(const char* notice) { Action *a = queue_action(OP_NOTICE, ""); a->data = (void*) notice; } diff --git a/fastboot/fastboot.cpp b/fastboot/fastboot.cpp index 5d5ac9b80..be7bf4a91 100644 --- a/fastboot/fastboot.cpp +++ b/fastboot/fastboot.cpp @@ -135,17 +135,17 @@ static std::string find_item_given_name(const char* img_name) { return android::base::StringPrintf("%s/%s", dir, img_name); } -std::string find_item(const char* item) { +std::string find_item(const std::string& item) { for (size_t i = 0; i < arraysize(images); ++i) { - if (images[i].nickname && !strcmp(images[i].nickname, item)) { + if (images[i].nickname && item == images[i].nickname) { return find_item_given_name(images[i].img_name); } } - if (!strcmp(item, "userdata")) return find_item_given_name("userdata.img"); - if (!strcmp(item, "cache")) return find_item_given_name("cache.img"); + if (item == "userdata") return find_item_given_name("userdata.img"); + if (item == "cache") return find_item_given_name("cache.img"); - fprintf(stderr, "unknown partition '%s'\n", item); + fprintf(stderr, "unknown partition '%s'\n", item.c_str()); return ""; } @@ -305,8 +305,21 @@ static void list_devices() { usb_open(list_devices_callback); } -static void usage() { - fprintf(stderr, +static void syntax_error(const char* fmt, ...) { + fprintf(stderr, "fastboot: usage: "); + + va_list ap; + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + + fprintf(stderr, "\n"); + exit(1); +} + +static int show_help() { + // clang-format off + fprintf(stdout, /* 1234567890123456789012345678901234567890123456789012345678901234567890123456 */ "usage: fastboot [