Indentation fix, cleanup.
[AROS.git] / arch / all-pc / boot / grub2-aros / util / grub-mklayout.c
blob1a4f1b4c8553f9e4e1c1eee80a1ff1805f7f5e83
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2010 Free Software Foundation, Inc.
5 * GRUB is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <config.h>
21 #include <grub/util/misc.h>
22 #include <grub/i18n.h>
23 #include <grub/term.h>
24 #include <grub/keyboard_layouts.h>
26 #define _GNU_SOURCE 1
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
35 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
36 #pragma GCC diagnostic ignored "-Wmissing-declarations"
38 #include <argp.h>
39 #pragma GCC diagnostic error "-Wmissing-prototypes"
40 #pragma GCC diagnostic error "-Wmissing-declarations"
42 #include "progname.h"
44 struct arguments
46 char *input;
47 char *output;
48 int verbosity;
51 static struct argp_option options[] = {
52 {"input", 'i', N_("FILE"), 0,
53 N_("set input filename. Default is STDIN"), 0},
54 {"output", 'o', N_("FILE"), 0,
55 N_("set output filename. Default is STDOUT"), 0},
56 {"verbose", 'v', 0, 0, N_("print verbose messages."), 0},
57 { 0, 0, 0, 0, 0, 0 }
60 struct console_grub_equivalence
62 const char *layout;
63 grub_uint32_t grub;
66 static struct console_grub_equivalence console_grub_equivalences_shift[] = {
67 {"KP_0", '0'},
68 {"KP_1", '1'},
69 {"KP_2", '2'},
70 {"KP_3", '3'},
71 {"KP_4", '4'},
72 {"KP_5", '5'},
73 {"KP_6", '6'},
74 {"KP_7", '7'},
75 {"KP_8", '8'},
76 {"KP_9", '9'},
77 {"KP_Period", '.'},
79 {NULL, '\0'}
82 static struct console_grub_equivalence console_grub_equivalences_unshift[] = {
83 {"KP_0", GRUB_TERM_KEY_INSERT},
84 {"KP_1", GRUB_TERM_KEY_END},
85 {"KP_2", GRUB_TERM_KEY_DOWN},
86 {"KP_3", GRUB_TERM_KEY_NPAGE},
87 {"KP_4", GRUB_TERM_KEY_LEFT},
88 {"KP_5", GRUB_TERM_KEY_CENTER},
89 {"KP_6", GRUB_TERM_KEY_RIGHT},
90 {"KP_7", GRUB_TERM_KEY_HOME},
91 {"KP_8", GRUB_TERM_KEY_UP},
92 {"KP_9", GRUB_TERM_KEY_PPAGE},
93 {"KP_Period", GRUB_TERM_KEY_DC},
95 {NULL, '\0'}
98 static struct console_grub_equivalence console_grub_equivalences_common[] = {
99 {"Escape", GRUB_TERM_ESC},
100 {"Tab", GRUB_TERM_TAB},
101 {"Delete", GRUB_TERM_BACKSPACE},
103 {"KP_Enter", '\n'},
104 {"Return", '\n'},
106 {"KP_Multiply", '*'},
107 {"KP_Subtract", '-'},
108 {"KP_Add", '+'},
109 {"KP_Divide", '/'},
111 {"F1", GRUB_TERM_KEY_F1},
112 {"F2", GRUB_TERM_KEY_F2},
113 {"F3", GRUB_TERM_KEY_F3},
114 {"F4", GRUB_TERM_KEY_F4},
115 {"F5", GRUB_TERM_KEY_F5},
116 {"F6", GRUB_TERM_KEY_F6},
117 {"F7", GRUB_TERM_KEY_F7},
118 {"F8", GRUB_TERM_KEY_F8},
119 {"F9", GRUB_TERM_KEY_F9},
120 {"F10", GRUB_TERM_KEY_F10},
121 {"F11", GRUB_TERM_KEY_F11},
122 {"F12", GRUB_TERM_KEY_F12},
123 {"F13", GRUB_TERM_KEY_F1 | GRUB_TERM_SHIFT},
124 {"F14", GRUB_TERM_KEY_F2 | GRUB_TERM_SHIFT},
125 {"F15", GRUB_TERM_KEY_F3 | GRUB_TERM_SHIFT},
126 {"F16", GRUB_TERM_KEY_F4 | GRUB_TERM_SHIFT},
127 {"F17", GRUB_TERM_KEY_F5 | GRUB_TERM_SHIFT},
128 {"F18", GRUB_TERM_KEY_F6 | GRUB_TERM_SHIFT},
129 {"F19", GRUB_TERM_KEY_F7 | GRUB_TERM_SHIFT},
130 {"F20", GRUB_TERM_KEY_F8 | GRUB_TERM_SHIFT},
131 {"F21", GRUB_TERM_KEY_F9 | GRUB_TERM_SHIFT},
132 {"F22", GRUB_TERM_KEY_F10 | GRUB_TERM_SHIFT},
133 {"F23", GRUB_TERM_KEY_F11 | GRUB_TERM_SHIFT},
134 {"F24", GRUB_TERM_KEY_F12 | GRUB_TERM_SHIFT},
135 {"Console_13", GRUB_TERM_KEY_F1 | GRUB_TERM_ALT},
136 {"Console_14", GRUB_TERM_KEY_F2 | GRUB_TERM_ALT},
137 {"Console_15", GRUB_TERM_KEY_F3 | GRUB_TERM_ALT},
138 {"Console_16", GRUB_TERM_KEY_F4 | GRUB_TERM_ALT},
139 {"Console_17", GRUB_TERM_KEY_F5 | GRUB_TERM_ALT},
140 {"Console_18", GRUB_TERM_KEY_F6 | GRUB_TERM_ALT},
141 {"Console_19", GRUB_TERM_KEY_F7 | GRUB_TERM_ALT},
142 {"Console_20", GRUB_TERM_KEY_F8 | GRUB_TERM_ALT},
143 {"Console_21", GRUB_TERM_KEY_F9 | GRUB_TERM_ALT},
144 {"Console_22", GRUB_TERM_KEY_F10 | GRUB_TERM_ALT},
145 {"Console_23", GRUB_TERM_KEY_F11 | GRUB_TERM_ALT},
146 {"Console_24", GRUB_TERM_KEY_F12 | GRUB_TERM_ALT},
147 {"Console_25", GRUB_TERM_KEY_F1 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
148 {"Console_26", GRUB_TERM_KEY_F2 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
149 {"Console_27", GRUB_TERM_KEY_F3 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
150 {"Console_28", GRUB_TERM_KEY_F4 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
151 {"Console_29", GRUB_TERM_KEY_F5 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
152 {"Console_30", GRUB_TERM_KEY_F6 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
153 {"Console_31", GRUB_TERM_KEY_F7 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
154 {"Console_32", GRUB_TERM_KEY_F8 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
155 {"Console_33", GRUB_TERM_KEY_F9 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
156 {"Console_34", GRUB_TERM_KEY_F10 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
157 {"Console_35", GRUB_TERM_KEY_F11 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
158 {"Console_36", GRUB_TERM_KEY_F12 | GRUB_TERM_SHIFT | GRUB_TERM_ALT},
160 {"Insert", GRUB_TERM_KEY_INSERT},
161 {"Down", GRUB_TERM_KEY_DOWN},
162 {"Up", GRUB_TERM_KEY_UP},
163 {"Home", GRUB_TERM_KEY_HOME},
164 {"End", GRUB_TERM_KEY_END},
165 {"Right", GRUB_TERM_KEY_RIGHT},
166 {"Left", GRUB_TERM_KEY_LEFT},
167 {"Next", GRUB_TERM_KEY_NPAGE},
168 {"Prior", GRUB_TERM_KEY_PPAGE},
169 {"Remove", GRUB_TERM_KEY_DC},
170 {"VoidSymbol", 0},
172 /* "Undead" keys since no dead key support in GRUB. */
173 {"dead_acute", '\''},
174 {"dead_circumflex", '^'},
175 {"dead_grave", '`'},
176 {"dead_tilde", '~'},
177 {"dead_diaeresis", '"'},
179 /* Following ones don't provide any useful symbols for shell. */
180 {"dead_cedilla", 0},
181 {"dead_ogonek", 0},
182 {"dead_caron", 0},
183 {"dead_breve", 0},
184 {"dead_doubleacute", 0},
186 /* Unused in GRUB. */
187 {"Pause", 0},
188 {"Scroll_Forward", 0},
189 {"Scroll_Backward", 0},
190 {"Hex_0", 0},
191 {"Hex_1", 0},
192 {"Hex_2", 0},
193 {"Hex_3", 0},
194 {"Hex_4", 0},
195 {"Hex_5", 0},
196 {"Hex_6", 0},
197 {"Hex_7", 0},
198 {"Hex_8", 0},
199 {"Hex_9", 0},
200 {"Hex_A", 0},
201 {"Hex_B", 0},
202 {"Hex_C", 0},
203 {"Hex_D", 0},
204 {"Hex_E", 0},
205 {"Hex_F", 0},
206 {"Scroll_Lock", 0},
207 {"Show_Memory", 0},
208 {"Show_Registers", 0},
209 {"Control_backslash", 0},
210 {"Compose", 0},
212 {NULL, '\0'}
215 static grub_uint8_t linux_to_usb_map[128] = {
216 /* 0x00 */ 0 /* Unused */, GRUB_KEYBOARD_KEY_ESCAPE,
217 /* 0x02 */ GRUB_KEYBOARD_KEY_1, GRUB_KEYBOARD_KEY_2,
218 /* 0x04 */ GRUB_KEYBOARD_KEY_3, GRUB_KEYBOARD_KEY_4,
219 /* 0x06 */ GRUB_KEYBOARD_KEY_5, GRUB_KEYBOARD_KEY_6,
220 /* 0x08 */ GRUB_KEYBOARD_KEY_7, GRUB_KEYBOARD_KEY_8,
221 /* 0x0a */ GRUB_KEYBOARD_KEY_9, GRUB_KEYBOARD_KEY_0,
222 /* 0x0c */ GRUB_KEYBOARD_KEY_DASH, GRUB_KEYBOARD_KEY_EQUAL,
223 /* 0x0e */ GRUB_KEYBOARD_KEY_BACKSPACE, GRUB_KEYBOARD_KEY_TAB,
224 /* 0x10 */ GRUB_KEYBOARD_KEY_Q, GRUB_KEYBOARD_KEY_W,
225 /* 0x12 */ GRUB_KEYBOARD_KEY_E, GRUB_KEYBOARD_KEY_R,
226 /* 0x14 */ GRUB_KEYBOARD_KEY_T, GRUB_KEYBOARD_KEY_Y,
227 /* 0x16 */ GRUB_KEYBOARD_KEY_U, GRUB_KEYBOARD_KEY_I,
228 /* 0x18 */ GRUB_KEYBOARD_KEY_O, GRUB_KEYBOARD_KEY_P,
229 /* 0x1a */ GRUB_KEYBOARD_KEY_LBRACKET, GRUB_KEYBOARD_KEY_RBRACKET,
230 /* 0x1c */ GRUB_KEYBOARD_KEY_ENTER, GRUB_KEYBOARD_KEY_LEFT_CTRL,
231 /* 0x1e */ GRUB_KEYBOARD_KEY_A, GRUB_KEYBOARD_KEY_S,
232 /* 0x20 */ GRUB_KEYBOARD_KEY_D, GRUB_KEYBOARD_KEY_F,
233 /* 0x22 */ GRUB_KEYBOARD_KEY_G, GRUB_KEYBOARD_KEY_H,
234 /* 0x24 */ GRUB_KEYBOARD_KEY_J, GRUB_KEYBOARD_KEY_K,
235 /* 0x26 */ GRUB_KEYBOARD_KEY_L, GRUB_KEYBOARD_KEY_SEMICOLON,
236 /* 0x28 */ GRUB_KEYBOARD_KEY_DQUOTE, GRUB_KEYBOARD_KEY_RQUOTE,
237 /* 0x2a */ GRUB_KEYBOARD_KEY_LEFT_SHIFT, GRUB_KEYBOARD_KEY_BACKSLASH,
238 /* 0x2c */ GRUB_KEYBOARD_KEY_Z, GRUB_KEYBOARD_KEY_X,
239 /* 0x2e */ GRUB_KEYBOARD_KEY_C, GRUB_KEYBOARD_KEY_V,
240 /* 0x30 */ GRUB_KEYBOARD_KEY_B, GRUB_KEYBOARD_KEY_N,
241 /* 0x32 */ GRUB_KEYBOARD_KEY_M, GRUB_KEYBOARD_KEY_COMMA,
242 /* 0x34 */ GRUB_KEYBOARD_KEY_DOT, GRUB_KEYBOARD_KEY_SLASH,
243 /* 0x36 */ GRUB_KEYBOARD_KEY_RIGHT_SHIFT, GRUB_KEYBOARD_KEY_NUMMUL,
244 /* 0x38 */ GRUB_KEYBOARD_KEY_LEFT_ALT, GRUB_KEYBOARD_KEY_SPACE,
245 /* 0x3a */ GRUB_KEYBOARD_KEY_CAPS_LOCK, GRUB_KEYBOARD_KEY_F1,
246 /* 0x3c */ GRUB_KEYBOARD_KEY_F2, GRUB_KEYBOARD_KEY_F3,
247 /* 0x3e */ GRUB_KEYBOARD_KEY_F4, GRUB_KEYBOARD_KEY_F5,
248 /* 0x40 */ GRUB_KEYBOARD_KEY_F6, GRUB_KEYBOARD_KEY_F7,
249 /* 0x42 */ GRUB_KEYBOARD_KEY_F8, GRUB_KEYBOARD_KEY_F9,
250 /* 0x44 */ GRUB_KEYBOARD_KEY_F10, GRUB_KEYBOARD_KEY_NUM_LOCK,
251 /* 0x46 */ GRUB_KEYBOARD_KEY_SCROLL_LOCK, GRUB_KEYBOARD_KEY_NUM7,
252 /* 0x48 */ GRUB_KEYBOARD_KEY_NUM8, GRUB_KEYBOARD_KEY_NUM9,
253 /* 0x4a */ GRUB_KEYBOARD_KEY_NUMMINUS, GRUB_KEYBOARD_KEY_NUM4,
254 /* 0x4c */ GRUB_KEYBOARD_KEY_NUM5, GRUB_KEYBOARD_KEY_NUM6,
255 /* 0x4e */ GRUB_KEYBOARD_KEY_NUMPLUS, GRUB_KEYBOARD_KEY_NUM1,
256 /* 0x50 */ GRUB_KEYBOARD_KEY_NUM2, GRUB_KEYBOARD_KEY_NUM3,
257 /* 0x52 */ GRUB_KEYBOARD_KEY_NUMDOT, GRUB_KEYBOARD_KEY_NUMDOT,
258 /* 0x54 */ 0, 0,
259 /* 0x56 */ GRUB_KEYBOARD_KEY_102ND, GRUB_KEYBOARD_KEY_F11,
260 /* 0x58 */ GRUB_KEYBOARD_KEY_F12, GRUB_KEYBOARD_KEY_JP_RO,
261 /* 0x5a */ 0, 0,
262 /* 0x5c */ 0, 0,
263 /* 0x5e */ 0, 0,
264 /* 0x60 */ GRUB_KEYBOARD_KEY_NUMENTER, GRUB_KEYBOARD_KEY_RIGHT_CTRL,
265 /* 0x62 */ GRUB_KEYBOARD_KEY_NUMSLASH, 0,
266 /* 0x64 */ GRUB_KEYBOARD_KEY_RIGHT_ALT, 0,
267 /* 0x66 */ GRUB_KEYBOARD_KEY_HOME, GRUB_KEYBOARD_KEY_UP,
268 /* 0x68 */ GRUB_KEYBOARD_KEY_PPAGE, GRUB_KEYBOARD_KEY_LEFT,
269 /* 0x6a */ GRUB_KEYBOARD_KEY_RIGHT, GRUB_KEYBOARD_KEY_END,
270 /* 0x6c */ GRUB_KEYBOARD_KEY_DOWN, GRUB_KEYBOARD_KEY_NPAGE,
271 /* 0x6e */ GRUB_KEYBOARD_KEY_INSERT, GRUB_KEYBOARD_KEY_DELETE,
272 /* 0x70 */ 0, 0,
273 /* 0x72 */ 0, GRUB_KEYBOARD_KEY_JP_RO,
274 /* 0x74 */ 0, 0,
275 /* 0x76 */ 0, 0,
276 /* 0x78 */ 0, GRUB_KEYBOARD_KEY_KPCOMMA,
277 /* 0x7a */ 0, 0,
278 /* 0x7c */ GRUB_KEYBOARD_KEY_JP_YEN,
281 static void
282 add_special_keys (struct grub_keyboard_layout *layout)
284 (void) layout;
287 static unsigned
288 lookup (char *code, int shift)
290 int i;
291 struct console_grub_equivalence *pr;
293 if (shift)
294 pr = console_grub_equivalences_shift;
295 else
296 pr = console_grub_equivalences_unshift;
298 for (i = 0; pr[i].layout != NULL; i++)
299 if (strcmp (code, pr[i].layout) == 0)
300 return pr[i].grub;
302 for (i = 0; console_grub_equivalences_common[i].layout != NULL; i++)
303 if (strcmp (code, console_grub_equivalences_common[i].layout) == 0)
304 return console_grub_equivalences_common[i].grub;
306 /* TRANSLATORS: scan identifier is keyboard key symbolic name. */
307 fprintf (stderr, _("Unknown keyboard scan identifier %s\n"), code);
309 return '\0';
312 static unsigned int
313 get_grub_code (char *layout_code, int shift)
315 unsigned int code;
317 if (strncmp (layout_code, "U+", sizeof ("U+") - 1) == 0)
318 sscanf (layout_code, "U+%x", &code);
319 else if (strncmp (layout_code, "+U+", sizeof ("+U+") - 1) == 0)
320 sscanf (layout_code, "+U+%x", &code);
321 else
322 code = lookup (layout_code, shift);
323 return code;
326 static void
327 write_file (FILE *out, const char *fname, struct grub_keyboard_layout *layout)
329 grub_uint32_t version;
330 unsigned i;
332 version = grub_cpu_to_le32_compile_time (GRUB_KEYBOARD_LAYOUTS_VERSION);
334 for (i = 0; i < ARRAY_SIZE (layout->keyboard_map); i++)
335 layout->keyboard_map[i] = grub_cpu_to_le32(layout->keyboard_map[i]);
337 for (i = 0; i < ARRAY_SIZE (layout->keyboard_map_shift); i++)
338 layout->keyboard_map_shift[i]
339 = grub_cpu_to_le32(layout->keyboard_map_shift[i]);
341 for (i = 0; i < ARRAY_SIZE (layout->keyboard_map_l3); i++)
342 layout->keyboard_map_l3[i]
343 = grub_cpu_to_le32(layout->keyboard_map_l3[i]);
345 for (i = 0; i < ARRAY_SIZE (layout->keyboard_map_shift_l3); i++)
346 layout->keyboard_map_shift_l3[i]
347 = grub_cpu_to_le32(layout->keyboard_map_shift_l3[i]);
349 if (fwrite (GRUB_KEYBOARD_LAYOUTS_FILEMAGIC, 1,
350 GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE, out)
351 != GRUB_KEYBOARD_LAYOUTS_FILEMAGIC_SIZE
352 || fwrite (&version, sizeof (version), 1, out) != 1
353 || fwrite (layout, 1, sizeof (*layout), out) != sizeof (*layout))
355 if (fname)
356 grub_util_error ("cannot write to `%s': %s", fname, strerror (errno));
357 else
358 grub_util_error ("cannot write to the stdout: %s", strerror (errno));
362 static void
363 write_keymaps (FILE *in, FILE *out, const char *out_filename)
365 struct grub_keyboard_layout layout;
366 char line[2048];
367 int ok;
369 memset (&layout, 0, sizeof (layout));
371 /* Process the ckbcomp output and prepare the layouts. */
372 ok = 0;
373 while (fgets (line, sizeof (line), in))
375 if (strncmp (line, "keycode", sizeof ("keycode") - 1) == 0)
377 unsigned keycode_linux;
378 unsigned keycode_usb;
379 char normal[64];
380 char shift[64];
381 char normalalt[64];
382 char shiftalt[64];
384 sscanf (line, "keycode %u = %60s %60s %60s %60s", &keycode_linux,
385 normal, shift, normalalt, shiftalt);
387 /* Not used. */
388 if (keycode_linux == 0x77 /* Pause */
389 /* Some obscure keys */
390 || keycode_linux == 0x63 || keycode_linux == 0x7d
391 || keycode_linux == 0x7e)
392 continue;
394 /* Not remappable. */
395 if (keycode_linux == 0x1d /* Left CTRL */
396 || keycode_linux == 0x61 /* Right CTRL */
397 || keycode_linux == 0x2a /* Left Shift. */
398 || keycode_linux == 0x36 /* Right Shift. */
399 || keycode_linux == 0x38 /* Left ALT. */
400 || keycode_linux == 0x64 /* Right ALT. */
401 || keycode_linux == 0x3a /* CapsLock. */
402 || keycode_linux == 0x45 /* NumLock. */
403 || keycode_linux == 0x46 /* ScrollLock. */)
404 continue;
406 keycode_usb = linux_to_usb_map[keycode_linux];
407 if (keycode_usb == 0
408 || keycode_usb >= GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)
410 /* TRANSLATORS: scan code is keyboard key numeric identifier. */
411 fprintf (stderr, _("Unknown keyboard scan code 0x%02x\n"), keycode_linux);
412 continue;
414 if (keycode_usb < GRUB_KEYBOARD_LAYOUTS_ARRAY_SIZE)
416 layout.keyboard_map[keycode_usb] = get_grub_code (normal, 0);
417 layout.keyboard_map_shift[keycode_usb] = get_grub_code (shift, 1);
418 layout.keyboard_map_l3[keycode_usb]
419 = get_grub_code (normalalt, 0);
420 layout.keyboard_map_shift_l3[keycode_usb]
421 = get_grub_code (shiftalt, 1);
422 ok = 1;
427 if (ok == 0)
429 /* TRANSLATORS: this error is triggered when input doesn't contain any
430 key descriptions. */
431 fprintf (stderr, "%s", _("ERROR: no valid keyboard layout found. Check the input.\n"));
432 exit (1);
435 add_special_keys (&layout);
437 write_file (out, out_filename, &layout);
440 static error_t
441 argp_parser (int key, char *arg, struct argp_state *state)
443 /* Get the input argument from argp_parse, which we
444 know is a pointer to our arguments structure. */
445 struct arguments *arguments = state->input;
447 switch (key)
449 case 'i':
450 arguments->input = xstrdup (arg);
451 break;
453 case 'o':
454 arguments->output = xstrdup (arg);
455 break;
457 case 'v':
458 arguments->verbosity++;
459 break;
461 default:
462 return ARGP_ERR_UNKNOWN;
465 return 0;
468 static struct argp argp = {
469 options, argp_parser, N_("[OPTIONS]"),
470 /* TRANSLATORS: "one" is a shortcut for "keyboard layout". */
471 N_("Generate GRUB keyboard layout from Linux console one."),
472 NULL, NULL, NULL
476 main (int argc, char *argv[])
478 FILE *in, *out;
479 struct arguments arguments;
481 grub_util_host_init (&argc, &argv);
483 /* Check for options. */
484 memset (&arguments, 0, sizeof (struct arguments));
485 if (argp_parse (&argp, argc, argv, 0, 0, &arguments) != 0)
487 fprintf (stderr, "%s", _("Error in parsing command line arguments\n"));
488 exit(1);
491 if (arguments.input)
492 in = grub_util_fopen (arguments.input, "r");
493 else
494 in = stdin;
496 if (!in)
497 grub_util_error (_("cannot open `%s': %s"), arguments.input ? : "stdin",
498 strerror (errno));
500 if (arguments.output)
501 out = grub_util_fopen (arguments.output, "wb");
502 else
503 out = stdout;
505 if (!out)
507 if (in != stdin)
508 fclose (in);
509 grub_util_error (_("cannot open `%s': %s"), arguments.output ? : "stdout",
510 strerror (errno));
513 write_keymaps (in, out, arguments.output);
515 if (in != stdin)
516 fclose (in);
518 if (out != stdout)
519 fclose (out);
521 return 0;