]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[digest] Use generic option-parsing library
authorMichael Brown <mcb30@ipxe.org>
Sun, 21 Nov 2010 18:37:54 +0000 (18:37 +0000)
committerMichael Brown <mcb30@ipxe.org>
Sun, 21 Nov 2010 23:37:56 +0000 (23:37 +0000)
Total saving: 68 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/hci/commands/digest_cmd.c

index e28e6c3bb31485f6585cdd4c0e785c01ff433e4a..99c196a14405f311467574f9cbd9aee45bcd6bab 100644 (file)
 #include <stdio.h>
 #include <string.h>
 #include <unistd.h>
+#include <getopt.h>
 #include <ipxe/command.h>
+#include <ipxe/parseopt.h>
 #include <ipxe/image.h>
 #include <ipxe/crypto.h>
-
 #include <ipxe/md5.h>
 #include <ipxe/sha1.h>
 
-/**
- * "digest" command syntax message
+/** @file
+ *
+ * Digest commands
  *
- * @v argv             Argument list
  */
-static void digest_syntax ( char **argv ) {
-       printf ( "Usage:\n"
-                "  %s <image name>\n"
-                "\n"
-                "Calculate the %s of an image\n",
-                argv[0], argv[0] );
-}
+
+/** "digest" options */
+struct digest_options {};
+
+/** "digest" option list */
+static struct option_descriptor digest_opts[] = {};
+
+/** "digest" command descriptor */
+static struct command_descriptor digest_cmd =
+       COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
+                      "<image> [<image>...]",
+                      "Calculate the digest of an image" );
 
 /**
  * The "digest" command
@@ -45,11 +51,11 @@ static void digest_syntax ( char **argv ) {
  * @v argc             Argument count
  * @v argv             Argument list
  * @v digest           Digest algorithm
- * @ret rc             Exit code
+ * @ret rc             Return status code
  */
 static int digest_exec ( int argc, char **argv,
                         struct digest_algorithm *digest ) {
-       const char *image_name;
+       struct digest_options opts;
        struct image *image;
        uint8_t digest_ctx[digest->ctxsize];
        uint8_t digest_out[digest->digestsize];
@@ -59,23 +65,17 @@ static int digest_exec ( int argc, char **argv,
        size_t frag_len;
        int i;
        unsigned j;
+       int rc;
 
-       if ( argc < 2 ||
-            !strcmp ( argv[1], "--help" ) ||
-            !strcmp ( argv[1], "-h" ) ) {
-               digest_syntax ( argv );
-               return 1;
-       }
+       /* Parse options */
+       if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
+               return rc;
 
-       for ( i = 1 ; i < argc ; i++ ) {
-               image_name = argv[i];
+       for ( i = optind ; i < argc ; i++ ) {
 
                /* find image */
-               image = find_image ( image_name );
-               if ( ! image ) {
-                       printf ( "No such image: %s\n", image_name );
+               if ( ( rc = parse_image ( argv[i], &image ) ) != 0 )
                        continue;
-               }
                offset = 0;
                len = image->len;