futility: Improve help messages

This provides help messages for the futility commands similar to
the way git does. These show the available commands:

  futility
  futility help
  futility --help

While these show help for a specific command:

  futility help COMMAND
  futility --help COMMAND
  futility COMMAND --help

BUG=none
BRANCH=ToT
TEST=manual

make runtests

And manually look at help messages for each command.

Change-Id: I1126471e242784c6ca7a2f11694fa7c505d833e8
Signed-off-by: Bill Richardson <wfrichar@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/219528
Reviewed-by: Randall Spangler <rspangler@chromium.org>
diff --git a/futility/cmd_vbutil_key.c b/futility/cmd_vbutil_key.c
index c8c1c61..ea8dea0 100644
--- a/futility/cmd_vbutil_key.c
+++ b/futility/cmd_vbutil_key.c
@@ -38,40 +38,32 @@
 	{NULL, 0, 0, 0}
 };
 
-/* Print help and return error */
-static int PrintHelp(char *progname)
+static void print_help(const char *progname)
 {
 	int i;
 
-	fprintf(stderr,
-		"This program wraps RSA keys with verified boot headers\n");
-	fprintf(stderr,
-		"\n"
-		"Usage:  %s --pack <outfile> [PARAMETERS]\n"
-		"\n"
-		"  Required parameters:\n"
-		"    --key <infile>              RSA key file (.keyb or .pem)\n"
-		"    --version <number>          Key version number "
-		"(required for .keyb,\n"
-		"                                  ignored for .pem)\n"
-		"    --algorithm <number>        "
-		"Signing algorithm to use with key:\n", progname);
+	printf("\n"
+	       "Usage:  " MYNAME " %s --pack <outfile> [PARAMETERS]\n"
+	       "\n"
+	       "  Required parameters:\n"
+	       "    --key <infile>              RSA key file (.keyb or .pem)\n"
+	       "    --version <number>          Key version number "
+	       "(required for .keyb,\n"
+	       "                                  ignored for .pem)\n"
+	       "    --algorithm <number>        "
+	       "Signing algorithm to use with key:\n", progname);
 
 	for (i = 0; i < kNumAlgorithms; i++) {
-		fprintf(stderr,
-			"                                  %d = (%s)\n",
-			i, algo_strings[i]);
+		printf("                                  %d = (%s)\n",
+		       i, algo_strings[i]);
 	}
 
-	fprintf(stderr,
-		"\nOR\n\n"
-		"Usage:  %s --unpack <infile>\n"
-		"\n"
-		"  Optional parameters:\n"
-		"    --copyto <file>             "
-		"Write a copy of the key to this file.\n" "\n", progname);
-
-	return 1;
+	printf("\nOR\n\n"
+	       "Usage:  " MYNAME " %s --unpack <infile>\n"
+	       "\n"
+	       "  Optional parameters:\n"
+	       "    --copyto <file>             "
+	       "Write a copy of the key to this file.\n\n", progname);
 }
 
 /* Pack a .keyb file into a .vbpubk, or a .pem into a .vbprivk */
@@ -86,7 +78,8 @@
 		return 1;
 	}
 
-	if ((pubkey = PublicKeyReadKeyb(infile, algorithm, version))) {
+	pubkey = PublicKeyReadKeyb(infile, algorithm, version);
+	if (pubkey) {
 		if (0 != PublicKeyWrite(outfile, pubkey)) {
 			fprintf(stderr, "vbutil_key: Error writing key.\n");
 			return 1;
@@ -95,7 +88,8 @@
 		return 0;
 	}
 
-	if ((privkey = PrivateKeyReadPem(infile, algorithm))) {
+	privkey = PrivateKeyReadPem(infile, algorithm);
+	if (privkey) {
 		if (0 != PrivateKeyWrite(outfile, privkey)) {
 			fprintf(stderr, "vbutil_key: Error writing key.\n");
 			return 1;
@@ -119,7 +113,8 @@
 		return 1;
 	}
 
-	if ((pubkey = PublicKeyRead(infile))) {
+	pubkey = PublicKeyRead(infile);
+	if (pubkey) {
 		printf("Public Key file:   %s\n", infile);
 		printf("Algorithm:         %" PRIu64 " %s\n", pubkey->algorithm,
 		       (pubkey->algorithm < kNumAlgorithms ?
@@ -140,7 +135,8 @@
 		return 0;
 	}
 
-	if ((privkey = PrivateKeyRead(infile))) {
+	privkey = PrivateKeyRead(infile);
+	if (privkey) {
 		printf("Private Key file:  %s\n", infile);
 		printf("Algorithm:         %" PRIu64 " %s\n",
 		       privkey->algorithm,
@@ -177,12 +173,6 @@
 	char *e;
 	int i;
 
-	char *progname = strrchr(argv[0], '/');
-	if (progname)
-		progname++;
-	else
-		progname = argv[0];
-
 	while ((i = getopt_long(argc, argv, "", long_opts, NULL)) != -1) {
 		switch (i) {
 		case '?':
@@ -227,8 +217,10 @@
 		}
 	}
 
-	if (parse_error)
-		return PrintHelp(progname);
+	if (parse_error) {
+		print_help(argv[0]);
+		return 1;
+	}
 
 	switch (mode) {
 	case OPT_MODE_PACK:
@@ -237,9 +229,11 @@
 		return Unpack(infile, outfile);
 	default:
 		printf("Must specify a mode.\n");
-		return PrintHelp(progname);
+		print_help(argv[0]);
+		return 1;
 	}
 }
 
 DECLARE_FUTIL_COMMAND(vbutil_key, do_vbutil_key,
-		      "Wraps RSA keys with vboot headers");
+		      "Wraps RSA keys with vboot headers",
+		      print_help);