]> git.proxmox.com Git - grub2.git/blobdiff - util/grub-mklayout.c
* grub-core/genmod.sh.in: Strip before converting to ELF as strip
[grub2.git] / util / grub-mklayout.c
index ac59981c7859fa0fad9bdfa75f4af4b1d9cc6471..b90546e12a90088a7e6e7a0e7ef22b8ae34ca91d 100644 (file)
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+#include <config.h>
+
 #include <grub/util/misc.h>
 #include <grub/i18n.h>
 #include <grub/term.h>
 #include <grub/keyboard_layouts.h>
 
+#define _GNU_SOURCE    1
+
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <getopt.h>
+#include <argp.h>
 #include <unistd.h>
 #include <errno.h>
 
 #include "progname.h"
 
-#define CKBCOMP "ckbcomp"
+struct arguments
+{
+  char *input;
+  char *output;
+  int verbosity;
+};
 
-static struct option options[] = {
-  {"output", required_argument, 0, 'o'},
-  {"help", no_argument, 0, 'h'},
-  {"version", no_argument, 0, 'V'},
-  {"verbose", no_argument, 0, 'v'},
-  {0, 0, 0, 0}
+static struct argp_option options[] = {
+  {"input",  'i', N_("FILE"), 0,
+   N_("set input filename. Default is STDIN"), 0},
+  {"output",  'o', N_("FILE"), 0,
+   N_("set output filename. Default is STDOUT"), 0},
+  {"verbose",     'v', 0,      0, N_("print verbose messages."), 0},
+  { 0, 0, 0, 0, 0, 0 }
 };
 
 struct console_grub_equivalence
 {
-  char *layout;
+  const char *layout;
   grub_uint32_t grub;
 };
 
@@ -58,6 +68,8 @@ static struct console_grub_equivalence console_grub_equivalences_shift[] = {
   {"KP_8", '8'},
   {"KP_9", '9'},
   {"KP_Period", '.'},
+
+  {NULL, '\0'}
 };
 
 static struct console_grub_equivalence console_grub_equivalences_unshift[] = {
@@ -72,6 +84,8 @@ static struct console_grub_equivalence console_grub_equivalences_unshift[] = {
   {"KP_8", GRUB_TERM_KEY_UP},
   {"KP_9", GRUB_TERM_KEY_PPAGE},
   {"KP_Period", GRUB_TERM_KEY_DC},
+
+  {NULL, '\0'}
 };
 
 static struct console_grub_equivalence console_grub_equivalences_common[] = {
@@ -236,7 +250,7 @@ static grub_uint8_t linux_to_usb_map[128] = {
   /* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT,      GRUB_KEYBOARD_KEY_NUMDOT, 
   /* 0x54 */ 0,                             0, 
   /* 0x56 */ GRUB_KEYBOARD_KEY_102ND,       GRUB_KEYBOARD_KEY_F11, 
-  /* 0x58 */ GRUB_KEYBOARD_KEY_F12,         0,
+  /* 0x58 */ GRUB_KEYBOARD_KEY_F12,         GRUB_KEYBOARD_KEY_JP_RO,
   /* 0x5a */ 0,                             0,
   /* 0x5c */ 0,                             0,
   /* 0x5e */ 0,                             0,
@@ -247,27 +261,16 @@ static grub_uint8_t linux_to_usb_map[128] = {
   /* 0x68 */ GRUB_KEYBOARD_KEY_PPAGE,       GRUB_KEYBOARD_KEY_LEFT,
   /* 0x6a */ GRUB_KEYBOARD_KEY_RIGHT,       GRUB_KEYBOARD_KEY_END,
   /* 0x6c */ GRUB_KEYBOARD_KEY_DOWN,        GRUB_KEYBOARD_KEY_NPAGE, 
-  /* 0x6e */ GRUB_KEYBOARD_KEY_INSERT,      GRUB_KEYBOARD_KEY_DELETE
+  /* 0x6e */ GRUB_KEYBOARD_KEY_INSERT,      GRUB_KEYBOARD_KEY_DELETE,
+  /* 0x70 */ 0,                             0,
+  /* 0x72 */ 0,                             GRUB_KEYBOARD_KEY_JP_RO,
+  /* 0x74 */ 0,                             0,
+  /* 0x76 */ 0,                             0,
+  /* 0x78 */ 0,                             GRUB_KEYBOARD_KEY_KPCOMMA,
+  /* 0x7a */ 0,                             0,
+  /* 0x7c */ GRUB_KEYBOARD_KEY_JP_YEN,
 }; 
 
-static void
-usage (int status)
-{
-  if (status)
-    fprintf (stderr, "Try `%s --help' for more information.\n", program_name);
-  else
-    printf ("\
-Usage: %s [OPTIONS] LAYOUT\n\
-  -o, --output         set output base name file. Default is LAYOUT.gkb\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);
-}
-
 static void
 add_special_keys (struct grub_keyboard_layout *layout)
 {
@@ -293,7 +296,8 @@ lookup (char *code, int shift)
     if (strcmp (code, console_grub_equivalences_common[i].layout) == 0)
       return console_grub_equivalences_common[i].grub;
 
-  fprintf (stderr, "Unknown key %s\n", code);
+  /* TRANSLATORS: scan identifier is keyboard key symbolic name.  */
+  fprintf (stderr, _("Unknown keyboard scan identifier %s\n"), code);
 
   return '\0';
 }
@@ -313,7 +317,7 @@ get_grub_code (char *layout_code, int shift)
 }
 
 static void
-write_file (FILE *out, struct grub_keyboard_layout *layout)
+write_file (FILE *out, const char *fname, struct grub_keyboard_layout *layout)
 {
   grub_uint32_t version;
   unsigned i;
@@ -335,14 +339,21 @@ write_file (FILE *out, struct grub_keyboard_layout *layout)
     layout->keyboard_map_shift_l3[i]
       = grub_cpu_to_le32(layout->keyboard_map_shift_l3[i]);
 
-  fwrite (GRUB_KEYBOARD_LAYOUTS_FILEMAGIC, 1,
-         GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE, out);
-  fwrite (&version, sizeof (version), 1, out);
-  fwrite (layout, 1, sizeof (*layout), out);
+  if (fwrite (GRUB_KEYBOARD_LAYOUTS_FILEMAGIC, 1,
+             GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE, out)
+      != GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
+      || fwrite (&version, sizeof (version), 1, out) != 1
+      || fwrite (layout, 1, sizeof (*layout), out) != sizeof (*layout))
+    {
+      if (fname)
+       grub_util_error ("cannot write to `%s': %s", fname, strerror (errno));
+      else
+       grub_util_error ("cannot write to the stdout: %s", strerror (errno));
+    }
 }
 
 static void
-write_keymaps (FILE *in, FILE *out)
+write_keymaps (FILE *in, FILE *out, const char *out_filename)
 {
   struct grub_keyboard_layout layout;
   char line[2048];
@@ -389,7 +400,8 @@ write_keymaps (FILE *in, FILE *out)
          if (keycode_usb == 0
              || keycode_usb >= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)
            {
-             fprintf (stderr, "Unknown keycode 0x%02x\n", keycode_linux);
+             /* TRANSLATORS: scan code is keyboard key numeric identifier.  */
+             fprintf (stderr, _("Unknown keyboard scan code 0x%02x\n"), keycode_linux);
              continue;
            }
          if (keycode_usb < GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)
@@ -407,75 +419,79 @@ write_keymaps (FILE *in, FILE *out)
 
   if (ok == 0)
     {
-      fprintf (stderr, "ERROR: no keycodes found. Check output of %s.\n",
-              CKBCOMP);
+      /* TRANSLATORS: this error is triggered when input doesn't contain any
+        key descriptions.  */
+      fprintf (stderr, "%s", _("ERROR: no valid keyboard layout found. Check the input.\n"));
       exit (1);
     }
 
   add_special_keys (&layout);
 
-  write_file (out, &layout);
+  write_file (out, out_filename, &layout);
 }
 
+static error_t
+argp_parser (int key, char *arg, struct argp_state *state)
+{
+  /* Get the input argument from argp_parse, which we
+     know is a pointer to our arguments structure. */
+  struct arguments *arguments = state->input;
+
+  switch (key)
+    {
+    case 'i':
+      arguments->input = xstrdup (arg);
+      break;
+
+    case 'o':
+      arguments->output = xstrdup (arg);
+      break;
+
+    case 'v':
+      arguments->verbosity++;
+      break;
+
+    default:
+      return ARGP_ERR_UNKNOWN;
+    }
+
+  return 0;
+}
+
+static struct argp argp = {
+  options, argp_parser, N_("[OPTIONS]"),
+  /* TRANSLATORS: "one" is a shortcut for "keyboard layout".  */
+  N_("Generate GRUB keyboard layout from Linux console one."),
+  NULL, NULL, NULL
+};
+
 int
 main (int argc, char *argv[])
 {
-  int verbosity;
-  char *infile_name = NULL;
-  char *outfile_name = NULL;
   FILE *in, *out;
+  struct arguments arguments;
 
-  set_program_name (argv[0]);
-
-  verbosity = 0;
+  grub_util_host_init (&argc, &argv);
 
   /* Check for options.  */
-  while (1)
+  memset (&arguments, 0, sizeof (struct arguments));
+  if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
     {
-      int c = getopt_long (argc, argv, "o:i:hVv", options, 0);
-
-      if (c == -1)
-       break;
-      else
-       switch (c)
-         {
-         case 'h':
-           usage (0);
-           break;
-
-         case 'i':
-           infile_name = optarg;
-           break;
-
-         case 'o':
-           outfile_name = optarg;
-           break;
-
-         case 'V':
-           printf ("%s (%s) %s\n", program_name, PACKAGE_NAME,
-                   PACKAGE_VERSION);
-           return 0;
-
-         case 'v':
-           verbosity++;
-           break;
-
-         default:
-           usage (1);
-           break;
-         }
+      fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
+      exit(1);
     }
 
-  if (infile_name)
-    in = fopen (infile_name, "r");
+  if (arguments.input)
+    in = grub_util_fopen (arguments.input, "r");
   else
     in = stdin;
 
   if (!in)
-    grub_util_error ("Couldn't open input file: %s\n", strerror (errno));
+    grub_util_error (_("cannot open `%s': %s"), arguments.input ? : "stdin",
+                    strerror (errno));
 
-  if (outfile_name)
-    out = fopen (outfile_name, "wb");
+  if (arguments.output)
+    out = grub_util_fopen (arguments.output, "wb");
   else
     out = stdout;
 
@@ -483,10 +499,11 @@ main (int argc, char *argv[])
     {
       if (in != stdin)
        fclose (in);
-      grub_util_error ("Couldn't open output file: %s\n", strerror (errno));
+      grub_util_error (_("cannot open `%s': %s"), arguments.output ? : "stdout",
+                      strerror (errno));
     }
 
-  write_keymaps (in, out);
+  write_keymaps (in, out, arguments.output);
 
   if (in != stdin)
     fclose (in);