]> git.proxmox.com Git - grub2.git/blobdiff - util/grub-mkrelpath.c
* grub-core/genmod.sh.in: Strip before converting to ELF as strip
[grub2.git] / util / grub-mkrelpath.c
index 788db38a25e441da85833f21d0d51d053f0ad80b..6e64accd03e12c12672597c0e9c5e05873279595 100644 (file)
 #include <config.h>
 
 #include <stdio.h>
+#include <string.h>
 #include <grub/util/misc.h>
 #include <grub/emu/misc.h>
 #include <grub/i18n.h>
-#include <getopt.h>
+
+#define _GNU_SOURCE    1
+#include <argp.h>
 
 #include "progname.h"
 
-static struct option options[] =
-  {
-    {"help", no_argument, 0, 'h'},
-    {"version", no_argument, 0, 'V'},
-    {0, 0, 0, 0},
-  };
+struct arguments
+{
+  char *pathname;
+};
+
+static struct argp_option options[] = {
+  { 0, 0, 0, 0, 0, 0 }
+};
 
-static void
-usage (int status)
+static 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 [OPTIONS] PATH\n\
-\n\
-Make a system path relative to its root.\n\
-\n\
-Options:\n\
-  -h, --help                display this message and exit\n\
-  -V, --version             print version information and exit\n\
-\n\
-Report bugs to <%s>.\n"), program_name, PACKAGE_BUGREPORT);
+  /* Get the input argument from argp_parse, which we
+     know is a pointer to our arguments structure. */
+  struct arguments *arguments = state->input;
 
-  exit (status);
+  switch (key)
+    {
+    case ARGP_KEY_ARG:
+      if (state->arg_num == 0)
+       arguments->pathname = xstrdup (arg);
+      else
+       {
+         /* Too many arguments. */
+         fprintf (stderr, _("Unknown extra argument `%s'."), arg);
+         fprintf (stderr, "\n");
+         argp_usage (state);
+       }
+      break;
+    case ARGP_KEY_NO_ARGS:
+      fprintf (stderr, "%s", _("No path is specified.\n"));
+      argp_usage (state);
+      exit (1);
+      break;
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+  return 0;
 }
 
+static struct argp argp = {
+  options, argp_parser, N_("PATH"),
+  N_("Transform a system filename into GRUB one."),
+  NULL, NULL, NULL
+};
+
 int
 main (int argc, char *argv[])
 {
-  char *argument, *relpath;
+  char *relpath;
+  struct arguments arguments;
 
-  set_program_name (argv[0]);
+  grub_util_host_init (&argc, &argv);
 
-  grub_util_init_nls ();
+  memset (&arguments, 0, sizeof (struct arguments));
 
   /* Check for options.  */
-  while (1)
+  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
     {
-      int c = getopt_long (argc, argv, "hV", options, 0);
-
-      if (c == -1)
-       break;
-      else
-       switch (c)
-         {
-         case 'h':
-           usage (0);
-           break;
-
-         case 'V':
-           printf ("%s (%s) %s\n", program_name, PACKAGE_NAME, PACKAGE_VERSION);
-           return 0;
-
-         default:
-           usage (1);
-           break;
-         }
-    }
-
-  if (optind >= argc)
-    {
-      fprintf (stderr, _("No path is specified.\n"));
-      usage (1);
+      fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
+      exit(1);
     }
 
-  if (optind + 1 != argc)
-    {
-      fprintf (stderr, _("Unknown extra argument `%s'.\n"), argv[optind + 1]);
-      usage (1);
-    }
-
-  argument = argv[optind];
-
-  relpath = grub_make_system_path_relative_to_its_root (argument);
+  relpath = grub_make_system_path_relative_to_its_root (arguments.pathname);
   printf ("%s\n", relpath);
   free (relpath);