Browse Source

[digest] Use generic option-parsing library

Total saving: 68 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 14 years ago
parent
commit
ee53e69bab
1 changed files with 25 additions and 25 deletions
  1. 25
    25
      src/hci/commands/digest_cmd.c

+ 25
- 25
src/hci/commands/digest_cmd.c View File

19
 #include <stdio.h>
19
 #include <stdio.h>
20
 #include <string.h>
20
 #include <string.h>
21
 #include <unistd.h>
21
 #include <unistd.h>
22
+#include <getopt.h>
22
 #include <ipxe/command.h>
23
 #include <ipxe/command.h>
24
+#include <ipxe/parseopt.h>
23
 #include <ipxe/image.h>
25
 #include <ipxe/image.h>
24
 #include <ipxe/crypto.h>
26
 #include <ipxe/crypto.h>
25
-
26
 #include <ipxe/md5.h>
27
 #include <ipxe/md5.h>
27
 #include <ipxe/sha1.h>
28
 #include <ipxe/sha1.h>
28
 
29
 
29
-/**
30
- * "digest" command syntax message
30
+/** @file
31
+ *
32
+ * Digest commands
31
  *
33
  *
32
- * @v argv		Argument list
33
  */
34
  */
34
-static void digest_syntax ( char **argv ) {
35
-	printf ( "Usage:\n"
36
-		 "  %s <image name>\n"
37
-		 "\n"
38
-		 "Calculate the %s of an image\n",
39
-		 argv[0], argv[0] );
40
-}
35
+
36
+/** "digest" options */
37
+struct digest_options {};
38
+
39
+/** "digest" option list */
40
+static struct option_descriptor digest_opts[] = {};
41
+
42
+/** "digest" command descriptor */
43
+static struct command_descriptor digest_cmd =
44
+	COMMAND_DESC ( struct digest_options, digest_opts, 1, MAX_ARGUMENTS,
45
+		       "<image> [<image>...]",
46
+		       "Calculate the digest of an image" );
41
 
47
 
42
 /**
48
 /**
43
  * The "digest" command
49
  * The "digest" command
45
  * @v argc		Argument count
51
  * @v argc		Argument count
46
  * @v argv		Argument list
52
  * @v argv		Argument list
47
  * @v digest		Digest algorithm
53
  * @v digest		Digest algorithm
48
- * @ret rc		Exit code
54
+ * @ret rc		Return status code
49
  */
55
  */
50
 static int digest_exec ( int argc, char **argv,
56
 static int digest_exec ( int argc, char **argv,
51
 			 struct digest_algorithm *digest ) {
57
 			 struct digest_algorithm *digest ) {
52
-	const char *image_name;
58
+	struct digest_options opts;
53
 	struct image *image;
59
 	struct image *image;
54
 	uint8_t digest_ctx[digest->ctxsize];
60
 	uint8_t digest_ctx[digest->ctxsize];
55
 	uint8_t digest_out[digest->digestsize];
61
 	uint8_t digest_out[digest->digestsize];
59
 	size_t frag_len;
65
 	size_t frag_len;
60
 	int i;
66
 	int i;
61
 	unsigned j;
67
 	unsigned j;
68
+	int rc;
62
 
69
 
63
-	if ( argc < 2 ||
64
-	     !strcmp ( argv[1], "--help" ) ||
65
-	     !strcmp ( argv[1], "-h" ) ) {
66
-		digest_syntax ( argv );
67
-		return 1;
68
-	}
70
+	/* Parse options */
71
+	if ( ( rc = parse_options ( argc, argv, &digest_cmd, &opts ) ) != 0 )
72
+		return rc;
69
 
73
 
70
-	for ( i = 1 ; i < argc ; i++ ) {
71
-		image_name = argv[i];
74
+	for ( i = optind ; i < argc ; i++ ) {
72
 
75
 
73
 		/* find image */
76
 		/* find image */
74
-		image = find_image ( image_name );
75
-		if ( ! image ) {
76
-			printf ( "No such image: %s\n", image_name );
77
+		if ( ( rc = parse_image ( argv[i], &image ) ) != 0 )
77
 			continue;
78
 			continue;
78
-		}
79
 		offset = 0;
79
 		offset = 0;
80
 		len = image->len;
80
 		len = image->len;
81
 
81
 

Loading…
Cancel
Save