grub-mklayout.c 17 KB

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