Browse Source

[time] Use generic option-parsing library

Total saving: 88 bytes.

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

+ 58
- 24
src/hci/commands/time_cmd.c View File

@@ -24,24 +24,43 @@
24 24
 #include <string.h>
25 25
 #include <unistd.h>
26 26
 #include <ipxe/command.h>
27
+#include <ipxe/parseopt.h>
27 28
 #include <ipxe/nap.h>
28 29
 #include <ipxe/timer.h>
29 30
 
31
+/** @file
32
+ *
33
+ * Time commands
34
+ *
35
+ */
36
+
37
+/** "time" options */
38
+struct time_options {};
39
+
40
+/** "time" option list */
41
+static struct option_descriptor time_opts[] = {};
42
+
43
+/** "time" command descriptor */
44
+static struct command_descriptor time_cmd =
45
+	COMMAND_DESC ( struct time_options, time_opts, 1, MAX_ARGUMENTS,
46
+		       "<command>", "Time a command" );
47
+
48
+/**
49
+ * "time" command
50
+ *
51
+ * @v argc		Argument count
52
+ * @v argv		Argument list
53
+ * @ret rc		Return status code
54
+ */
30 55
 static int time_exec ( int argc, char **argv ) {
56
+	struct time_options opts;
31 57
 	unsigned long start;
32
-	int rc, secs;
33
-
34
-	if ( argc == 1 ||
35
-	     !strcmp ( argv[1], "--help" ) ||
36
-	     !strcmp ( argv[1], "-h" ) )
37
-	{
38
-		printf ( "Usage:\n"
39
-			 "  %s <command>\n"
40
-			 "\n"
41
-			 "Time a command\n",
42
-			 argv[0] );
43
-		return 1;
44
-	}
58
+	int secs;
59
+	int rc;
60
+
61
+	/* Parse options */
62
+	if ( ( rc = parse_options ( argc, argv, &time_cmd, &opts ) ) != 0 )
63
+		return rc;
45 64
 
46 65
 	start = currticks();
47 66
 	rc = execv ( argv[1], argv + 1 );
@@ -52,25 +71,39 @@ static int time_exec ( int argc, char **argv ) {
52 71
 	return rc;
53 72
 }
54 73
 
74
+/** "time" command */
55 75
 struct command time_command __command = {
56 76
 	.name = "time",
57 77
 	.exec = time_exec,
58 78
 };
59 79
 
80
+/** "sleep" options */
81
+struct sleep_options {};
82
+
83
+/** "sleep" option list */
84
+static struct option_descriptor sleep_opts[] = {};
85
+
86
+/** "sleep" command descriptor */
87
+static struct command_descriptor sleep_cmd =
88
+	COMMAND_DESC ( struct sleep_options, sleep_opts, 1, 1,
89
+		       "<seconds>", "Sleep for <seconds> seconds" );
90
+
91
+/**
92
+ * "sleep" command
93
+ *
94
+ * @v argc		Argument count
95
+ * @v argv		Argument list
96
+ * @ret rc		Return status code
97
+ */
60 98
 static int sleep_exec ( int argc, char **argv ) {
99
+	struct sleep_options opts;
61 100
 	unsigned long start, delay;
101
+	int rc;
102
+
103
+	/* Parse options */
104
+	if ( ( rc = parse_options ( argc, argv, &sleep_cmd, &opts ) ) != 0 )
105
+		return rc;
62 106
 
63
-	if ( argc == 1 ||
64
-	     !strcmp ( argv[1], "--help" ) ||
65
-	     !strcmp ( argv[1], "-h" ))
66
-	{
67
-		printf ( "Usage:\n"
68
-			 "  %s <seconds>\n"
69
-			 "\n"
70
-			 "Sleep for <seconds> seconds\n",
71
-			 argv[0] );
72
-		return 1;
73
-	}
74 107
 	start = currticks();
75 108
 	delay = strtoul ( argv[1], NULL, 0 ) * ticks_per_sec();
76 109
 	while ( ( currticks() - start ) <= delay )
@@ -78,6 +111,7 @@ static int sleep_exec ( int argc, char **argv ) {
78 111
 	return 0;
79 112
 }
80 113
 
114
+/** "sleep" command */
81 115
 struct command sleep_command __command = {
82 116
 	.name = "sleep",
83 117
 	.exec = sleep_exec,

Loading…
Cancel
Save