Add 'prioritize' command to cgpt tool.

This lets us reorder the priority of all the kernel partitions with a single
command, instead of a bunch of complicated and error-prone shell script
logic.

Change-Id: I21d39763ec5a748488d5319a987bcfe7c34ce4d0

BUG=chromium-os:9167
TEST=manual

In the chroot, do this:

cd ~/trunk/src/platform/vboot_reference
make
make runtests
make clean

Everything should pass.

Review URL: http://codereview.chromium.org/5352005
diff --git a/cgpt/cgpt.c b/cgpt/cgpt.c
index d8604b1..4c9fc32 100644
--- a/cgpt/cgpt.c
+++ b/cgpt/cgpt.c
@@ -27,18 +27,19 @@
   {"repair", cmd_repair, "Repair damaged GPT headers and tables"},
   {"boot", cmd_boot, "Edit the PMBR sector for legacy BIOSes"},
   {"find", cmd_find, "Locate a partition by its GUID"},
+  {"prioritize", cmd_prioritize,
+   "Reorder the priority of all kernel partitions"},
 };
 
-
 void Usage(void) {
   int i;
 
-  printf("Usage: %s COMMAND [OPTIONS] DRIVE\n\n"
+  printf("\nUsage: %s COMMAND [OPTIONS] DRIVE\n\n"
          "Supported COMMANDs:\n\n",
          progname);
 
   for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
-    printf("    %-10s  %s\n", cmds[i].name, cmds[i].comment);
+    printf("    %-15s  %s\n", cmds[i].name, cmds[i].comment);
   }
   printf("\nFor more detailed usage, use %s COMMAND -h\n\n", progname);
 }
@@ -47,6 +48,8 @@
 
 int main(int argc, char *argv[]) {
   int i;
+  int match_count = 0;
+  int match_index = 0;
 
   progname = strrchr(argv[0], '/');
   if (progname)
@@ -64,12 +67,23 @@
 
   // Find the command to invoke.
   for (i = 0; command && i < sizeof(cmds)/sizeof(cmds[0]); ++i) {
+    // exact match?
     if (0 == strcmp(cmds[i].name, command)) {
-      return cmds[i].fp(argc, argv);
+      match_index = i;
+      match_count = 1;
+      break;
+    }
+    // unique match?
+    else if (0 == strncmp(cmds[i].name, command, strlen(command))) {
+      match_index = i;
+      match_count++;
     }
   }
 
-  // Couldn't find the command.
+  if (match_count == 1)
+    return cmds[match_index].fp(argc, argv);
+
+  // Couldn't find a single matching command.
   Usage();
 
   return CGPT_FAILED;