]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[fc] Use generic option-parsing library
authorMichael Brown <mcb30@ipxe.org>
Sun, 21 Nov 2010 19:06:06 +0000 (19:06 +0000)
committerMichael Brown <mcb30@ipxe.org>
Mon, 22 Nov 2010 00:34:40 +0000 (00:34 +0000)
Total saving: 111 bytes.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
src/hci/commands/fcmgmt_cmd.c
src/include/ipxe/errfile.h

index 98647c1349e0ca4ce3b4f321c1e5550e7ad1df54..c76554f68c14f4800948e77b89b92f40b4541185 100644 (file)
 FILE_LICENCE ( GPL2_OR_LATER );
 
 #include <stdio.h>
+#include <errno.h>
 #include <getopt.h>
 #include <strings.h>
 #include <ipxe/fc.h>
 #include <ipxe/fcels.h>
 #include <ipxe/command.h>
+#include <ipxe/parseopt.h>
 #include <ipxe/tables.h>
 #include <usr/fcmgmt.h>
 
@@ -33,36 +35,96 @@ FILE_LICENCE ( GPL2_OR_LATER );
  *
  */
 
-static void fcstat_syntax ( char **argv ) {
-       printf ( "Usage:\n  %s\n", argv[0] );
+/**
+ * Parse Fibre Channel port name
+ *
+ * @v text             Text
+ * @ret port           Fibre Channel port
+ * @ret rc             Return status code
+ */
+static int parse_fc_port ( const char *text, struct fc_port **port ) {
+
+       /* Sanity check */
+       assert ( text != NULL );
+
+       /* Find Fibre Channel port */
+       *port = fc_port_find ( text );
+       if ( ! *port ) {
+               printf ( "\"%s\": no such port\n", text );
+               return -ENODEV;
+       }
+
+       return 0;
 }
 
+/**
+ * Parse Fibre Channel port ID
+ *
+ * @v text             Text
+ * @ret port_id                Fibre Channel port ID
+ * @ret rc             Return status code
+ */
+static int parse_fc_port_id ( const char *text, struct fc_port_id *port_id ) {
+       int rc;
+
+       /* Sanity check */
+       assert ( text != NULL );
+
+       /* Parse port ID */
+       if ( ( rc = fc_id_aton ( text, port_id ) ) != 0 ) {
+               printf ( "\"%s\": invalid port ID\n", text );
+               return -EINVAL;
+       }
+
+       return 0;
+}
+
+/**
+ * Parse Fibre Channel ELS handler name
+ *
+ * @v text             Text
+ * @ret handler                Fibre Channel ELS handler
+ * @ret rc             Return status code
+ */
+static int parse_fc_els_handler ( const char *text,
+                                 struct fc_els_handler **handler ) {
+
+       for_each_table_entry ( (*handler), FC_ELS_HANDLERS ) {
+               if ( strcasecmp ( (*handler)->name, text ) == 0 )
+                       return 0;
+       }
+
+       printf ( "\"%s\": unrecognised ELS\n", text );
+       return -ENOENT;
+}
+
+/** "fcstat" options */
+struct fcstat_options {};
+
+/** "fcstat" option list */
+static struct option_descriptor fcstat_opts[] = {};
+
+/** "fcstat" command descriptor */
+static struct command_descriptor fcstat_cmd =
+       COMMAND_DESC ( struct fcstat_options, fcstat_opts, 0, 0,
+                      "", "" );
+
+/**
+ * The "fcstat" command
+ *
+ * @v argc             Argument count
+ * @v argv             Argument list
+ * @ret rc             Return status code
+ */
 static int fcstat_exec ( int argc, char **argv ) {
-       static struct option fcstat_opts[] = {
-               { "help", 0, NULL, 'h' },
-               { NULL, 0, NULL, 0 },
-       };
+       struct fcstat_options opts;
        struct fc_port *port;
        struct fc_peer *peer;
-       int c;
+       int rc;
 
        /* Parse options */
-       while ( ( c = getopt_long ( argc, argv, "h", fcstat_opts,
-                                   NULL ) ) >= 0 ) {
-               switch ( c ) {
-               case 'h':
-                       /* Display help text */
-               default:
-                       /* Unrecognised/invalid option */
-                       fcstat_syntax ( argv );
-                       return 1;
-               }
-       }
-
-       if ( optind != argc ) {
-               fcstat_syntax ( argv );
-               return 1;
-       }
+       if ( ( rc = parse_options ( argc, argv, &fcstat_cmd, &opts ) ) != 0 )
+               return rc;
 
        list_for_each_entry ( port, &fc_ports, list )
                fcportstat ( port );
@@ -72,105 +134,72 @@ static int fcstat_exec ( int argc, char **argv ) {
        return 0;
 }
 
-static void fcels_syntax ( char **argv ) {
-       printf ( "Usage:\n  %s [--port <port>] [--id <peer port id>]"
-                " <command>\n", argv[0] );
-}
+/** "fcels" options */
+struct fcels_options {
+       /** Fibre Channel port */
+       struct fc_port *port;
+       /** Fibre Channel peer port ID */
+       struct fc_port_id peer_port_id;
+};
 
-static struct fc_els_handler * fcels_find_handler ( const char *name ) {
-       struct fc_els_handler *handler;
+/** "fcels" option list */
+static struct option_descriptor fcels_opts[] = {
+       OPTION_DESC ( "port", 'p', required_argument,
+                     struct fcels_options, port, parse_fc_port ),
+       OPTION_DESC ( "id", 'i', required_argument,
+                     struct fcels_options, peer_port_id, parse_fc_port_id ),
+};
 
-       for_each_table_entry ( handler, FC_ELS_HANDLERS ) {
-               if ( strcasecmp ( handler->name, name ) == 0 )
-                       return handler;
-       }
-       return NULL;
-}
+/** "fcels" command descriptor */
+static struct command_descriptor fcels_cmd =
+       COMMAND_DESC ( struct fcels_options, fcels_opts, 1, 1,
+                      "[--port <port>] [--id <peer_port_id>] <command>", "" );
 
+/**
+ * The "fcels" command
+ *
+ * @v argc             Argument count
+ * @v argv             Argument list
+ * @ret rc             Return status code
+ */
 static int fcels_exec ( int argc, char **argv ) {
-       static struct option fcels_opts[] = {
-               { "help", 0, NULL, 'h' },
-               { "port", required_argument, NULL, 'p' },
-               { "id", required_argument, NULL, 'i' },
-               { NULL, 0, NULL, 0 },
-       };
-       const char *handler_text;
-       const char *port_text = NULL;
-       const char *id_text = NULL;
+       struct fcels_options opts;
        struct fc_els_handler *handler;
-       struct fc_port *port;
-       struct fc_port_id id_buf;
        struct fc_port_id *id;
-       int c;
+       int rc;
 
        /* Parse options */
-       while ( ( c = getopt_long ( argc, argv, "hp:i:", fcels_opts,
-                                   NULL ) ) >= 0 ) {
-               switch ( c ) {
-               case 'p':
-                       port_text = optarg;
-                       break;
-               case 'i':
-                       id_text = optarg;
-                       break;
-               case 'h':
-                       /* Display help text */
-               default:
-                       /* Unrecognised/invalid option */
-                       fcels_syntax ( argv );
-                       return 1;
-               }
-       }
-
-       /* Identify ELS */
-       if ( optind != ( argc - 1 ) ) {
-               fcels_syntax ( argv );
-               return 1;
-       }
-       handler_text = argv[optind];
-       handler = fcels_find_handler ( handler_text );
-       if ( ! handler ) {
-               printf ( "%s: unrecognised ELS\n", handler_text );
-               return 1;
-       }
-
-       /* Identify port */
-       if ( port_text ) {
-               /* Use specified port */
-               port = fc_port_find ( port_text );
-               if ( ! port ) {
-                       printf ( "%s: no such port\n", port_text );
-                       return 1;
-               }
-       } else {
-               /* Use first port */
-               port = list_first_entry ( &fc_ports, struct fc_port, list );
-               if ( ! port ) {
+       if ( ( rc = parse_options ( argc, argv, &fcels_cmd, &opts ) ) != 0 )
+               return rc;
+
+       /* Parse ELS handler */
+       if ( ( rc = parse_fc_els_handler ( argv[optind], &handler ) ) != 0 )
+               return rc;
+
+       /* Use first port if no port specified */
+       if ( ! opts.port ) {
+               opts.port = list_first_entry ( &fc_ports, struct fc_port,
+                                              list );
+               if ( ! opts.port ) {
                        printf ( "No ports\n" );
-                       return 1;
+                       return -ENODEV;
                }
        }
-       assert ( port != NULL );
 
-       /* Identify port ID */
-       if ( id_text ) {
-               if ( fc_id_aton ( id_text, &id_buf ) != 0 ) {
-                       printf ( "%s: invalid port ID\n", id_text );
-                       return 1;
-               }
-               id = &id_buf;
-       } else {
-               if ( fc_link_ok ( &port->link ) &&
-                    ! ( port->flags & FC_PORT_HAS_FABRIC ) ) {
-                       id = &port->ptp_link_port_id;
+       /* Use link peer port ID if no peer port ID specified */
+       id = &opts.peer_port_id;
+       if ( memcmp ( id, &fc_empty_port_id, sizeof ( *id ) ) == 0 ) {
+               if ( fc_link_ok ( &opts.port->link ) &&
+                    ! ( opts.port->flags & FC_PORT_HAS_FABRIC ) ) {
+                       id = &opts.port->ptp_link_port_id;
                } else {
                        id = &fc_f_port_id;
                }
        }
-       assert ( id != NULL );
 
-       if ( fcels ( port, id, handler ) != 0 )
-               return 1;
+       /** Issue ELS */
+       if ( ( rc = fcels ( opts.port, id, handler ) ) != 0 )
+               return rc;
 
        return 0;
 }
index e475eca5f743ecf3eaa409aa19a817c80d611551..d505bb6e797b4d216f2dca0572313e35250c4428 100644 (file)
@@ -230,6 +230,7 @@ FILE_LICENCE ( GPL2_OR_LATER );
 #define ERRFILE_lotest               ( ERRFILE_OTHER | 0x001b0000 )
 #define ERRFILE_config_cmd           ( ERRFILE_OTHER | 0x001c0000 )
 #define ERRFILE_ifmgmt_cmd           ( ERRFILE_OTHER | 0x001d0000 )
+#define ERRFILE_fcmgmt_cmd           ( ERRFILE_OTHER | 0x001e0000 )
 
 /** @} */