Hello, all. I've just imported argp module. I attach the patch to make grub-fstest argp-based. Can someone familiar with argp have a look at it?
-- Regards Vladimir 'φ-coder/phcoder' Serbinenko
=== modified file 'util/grub-fstest.c' --- util/grub-fstest.c 2010-01-27 01:49:11 +0000 +++ util/grub-fstest.c 2010-04-02 22:47:13 +0000 @@ -39,9 +39,9 @@ #include <unistd.h> #include <string.h> #include <stdlib.h> -#include <getopt.h> #include "progname.h" +#include "argp.h" void grub_putchar (int c) @@ -275,8 +275,17 @@ cmd_crc (char *pathname) printf ("%08x\n", crc); } +static char *root = NULL; +static int args_count = 0; +static int nparm = 0; +static int num_disks = 1; +static char **images = NULL; +static int cmd = 0; +static char *debug_str = NULL; +static char **args = NULL; + static void -fstest (char **images, int num_disks, int cmd, int n, char **args) +fstest (int n, char **args) { char *host_file; char *loop_name; @@ -350,202 +359,166 @@ fstest (char **images, int num_disks, in } } -static struct option options[] = { - {"root", required_argument, 0, 'r'}, - {"skip", required_argument, 0, 's'}, - {"length", required_argument, 0, 'n'}, - {"diskcount", required_argument, 0, 'c'}, - {"debug", required_argument, 0, 'd'}, - {"help", no_argument, 0, 'h'}, - {"version", no_argument, 0, 'V'}, - {"verbose", no_argument, 0, 'v'}, - {0, 0, 0, 0} +static struct argp_option options[] = { + {0, 0, 0 , OPTION_DOC, "Commands:", 1}, + {"ls PATH", 0, 0 , OPTION_DOC, "List files in PATH.", 1}, + {"cp FILE LOCAL", 0, 0, OPTION_DOC, "Copy FILE to local file LOCAL.", 1}, + {"cmp FILE LOCAL", 0, 0, OPTION_DOC, "Compare FILE with local file LOCAL.", 1}, + {"hex FILE", 0, 0 , OPTION_DOC, "Hex dump FILE.", 1}, + {"crc FILE", 0, 0 , OPTION_DOC, "Get crc32 checksum of FILE.", 1}, + {"blocklist FILE", 0, 0, OPTION_DOC, "Display blocklist of FILE.", 1}, + + {"root", 'r', "DEVICE_NAME", 0, "Set root device.", 2}, + {"skip", 's', "N", 0, "Skip N bytes from output file.", 2}, + {"length", 'n', "N", 0, "Handle N bytes in output file.", 2}, + {"diskcount", 'c', "N", 0, "N input files.", 2}, + {"debug", 'd', "S", 0, "Set debug environment variable.", 2}, + {"verbose", 'v', NULL, OPTION_ARG_OPTIONAL, "Print verbose messages.", 2}, + {0, 0, 0, 0, 0, 0} }; -static void -usage (int status) +error_t +argp_parser (int key, char *arg, struct argp_state *state) { - if (status) - fprintf (stderr, "Try `%s --help' for more information.\n", program_name); - else - printf ("\ -Usage: %s [OPTION]... IMAGE_PATH COMMANDS\n\ -\n\ -Debug tool for filesystem driver.\n\ -\nCommands:\n\ - ls PATH list files in PATH\n\ - cp FILE LOCAL copy FILE to local file LOCAL\n\ - cmp FILE LOCAL compare FILE with local file LOCAL\n\ - hex FILE Hex dump FILE\n\ - crc FILE Get crc32 checksum of FILE\n\ - blocklist FILE display blocklist of FILE\n\ -\nOptions:\n\ - -r, --root=DEVICE_NAME set root device\n\ - -s, --skip=N skip N bytes from output file\n\ - -n, --length=N handle N bytes in output file\n\ - -c, --diskcount=N N input files\n\ - -d, --debug=S Set debug environment variable\n\ - -h, --help display this message and exit\n\ - -V, --version print version information and exit\n\ - -v, --verbose print verbose messages\n\ -\n\ -Report bugs to <%s>.\n", program_name, PACKAGE_BUGREPORT); - - exit (status); -} + char *p; -int -main (int argc, char *argv[]) -{ - char *debug_str = NULL, *root = NULL, *default_root, *alloc_root; - int i, cmd, num_opts, image_index, num_disks = 1; - - set_program_name (argv[0]); - - grub_util_init_nls (); - - /* Find the first non option entry. */ - for (num_opts = 1; num_opts < argc; num_opts++) - if (argv[num_opts][0] == '-') - { - if ((argv[num_opts][2] == 0) && (num_opts < argc - 1) && - ((argv[num_opts][1] == 'r') || - (argv[num_opts][1] == 's') || - (argv[num_opts][1] == 'n') || - (argv[num_opts][1] == 'c') || - (argv[num_opts][1] == 'd'))) - num_opts++; - } - else - break; - - /* Check for options. */ - while (1) + switch (key) { - int c = getopt_long (num_opts, argv, "r:s:n:c:d:hVv", options, 0); - char *p; - - if (c == -1) - break; - else - switch (c) - { - case 'r': - root = optarg; - break; - - case 's': - skip = grub_strtoul (optarg, &p, 0); - if (*p == 's') - skip <<= GRUB_DISK_SECTOR_BITS; - break; - - case 'n': - leng = grub_strtoul (optarg, &p, 0); - if (*p == 's') - leng <<= GRUB_DISK_SECTOR_BITS; - break; - - case 'c': - num_disks = grub_strtoul (optarg, NULL, 0); - if (num_disks < 1) - { - fprintf (stderr, "Invalid disk count.\n"); - usage (1); - } - break; - - case 'd': - debug_str = optarg; - break; + case 'r': + root = arg; + return 0; + + case 's': + skip = grub_strtoul (arg, &p, 0); + if (*p == 's') + skip <<= GRUB_DISK_SECTOR_BITS; + return 0; + + case 'n': + leng = grub_strtoul (arg, &p, 0); + if (*p == 's') + leng <<= GRUB_DISK_SECTOR_BITS; + return 0; + + case 'c': + num_disks = grub_strtoul (arg, NULL, 0); + if (num_disks < 1) + { + fprintf (stderr, "Invalid disk count.\n"); + argp_usage (state); + } + if (args_count != 0) + { + fprintf (stderr, "Disk count must precede disks list.\n"); + argp_usage (state); + } + return 0; - case 'h': - usage (0); - break; + case 'd': + debug_str = arg; + return 0; + + case 'v': + verbosity++; + return 0; - case 'V': - printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION); - return 0; + case ARGP_KEY_END: + if (args_count < num_disks) + { + fprintf (stderr, "No command is specified.\n"); + argp_usage (state); + } + if (args_count - 1 - num_disks < nparm) + { + fprintf (stderr, "Not enough parameters to command.\n"); + argp_usage (state); + } + return 0; - case 'v': - verbosity++; - break; + case ARGP_KEY_ARG: + break; - default: - usage (1); - break; - } + default: + return ARGP_ERR_UNKNOWN; } - /* Obtain PATH. */ - if (optind + num_disks - 1 >= argc) + if (args_count < num_disks) { - fprintf (stderr, "Not enough pathname.\n"); - usage (1); + if (arg[0] != '/') + { + fprintf (stderr, "Must use absolute path.\n"); + argp_usage (state); + } + if (args_count == 0) + images = xmalloc (num_disks * sizeof (images[0])); + images[args_count] = xstrdup (arg); + args_count++; + return 0; } - image_index = optind; - for (i = 0; i < num_disks; i++, optind++) - if (argv[optind][0] != '/') - { - fprintf (stderr, "Must use absolute path.\n"); - usage (1); - } - - cmd = 0; - if (optind < argc) + if (args_count == num_disks) { - int nparm = 0; - - if (!grub_strcmp (argv[optind], "ls")) + if (!grub_strcmp (arg, "ls")) { cmd = CMD_LS; } - else if (!grub_strcmp (argv[optind], "cp")) + else if (!grub_strcmp (arg, "cp")) { cmd = CMD_CP; nparm = 2; } - else if (!grub_strcmp (argv[optind], "cmp")) + else if (!grub_strcmp (arg, "cmp")) { cmd = CMD_CMP; nparm = 2; } - else if (!grub_strcmp (argv[optind], "hex")) + else if (!grub_strcmp (arg, "hex")) { cmd = CMD_HEX; nparm = 1; } - else if (!grub_strcmp (argv[optind], "crc")) + else if (!grub_strcmp (arg, "crc")) { cmd = CMD_CRC; nparm = 1; } - else if (!grub_strcmp (argv[optind], "blocklist")) + else if (!grub_strcmp (arg, "blocklist")) { cmd = CMD_BLOCKLIST; nparm = 1; } else { - fprintf (stderr, "Invalid command %s.\n", argv[optind]); - usage (1); + fprintf (stderr, "Invalid command %s.\n", arg); + argp_usage (state); } + args_count++; + return 0; + } - if (optind + 1 + nparm > argc) - { - fprintf (stderr, "Invalid parameter for command %s.\n", - argv[optind]); - usage (1); - } + args[args_count - 1 - num_disks] = xstrdup (arg); + args_count++; + return 0; +} - optind++; - } - else - { - fprintf (stderr, "No command is specified.\n"); - usage (1); - } +struct argp argp = { + options, argp_parser, "IMAGE_PATH COMMANDS", + "Debug tool for filesystem driver.", + NULL, NULL, NULL +}; + +int +main (int argc, char *argv[]) +{ + char *default_root, *alloc_root; + + set_program_name (argv[0]); + + grub_util_init_nls (); + + args = xmalloc (argc * sizeof (args[0])); + + argp_parse (&argp, argc, argv, 0, 0, 0); /* Initialize all modules. */ grub_init_all (); @@ -574,7 +547,7 @@ main (int argc, char *argv[]) free (alloc_root); /* Do it. */ - fstest (argv + image_index, num_disks, cmd, argc - optind, argv + optind); + fstest (args_count - 1 - num_disks, args); /* Free resources. */ grub_fini_all ();
signature.asc
Description: OpenPGP digital signature
_______________________________________________ Grub-devel mailing list Grub-devel@gnu.org http://lists.gnu.org/mailman/listinfo/grub-devel