blob: f525ee5d11f52abe32d9b8b7596194b741a67ca5 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersenc4996011999-10-20 22:08:37 +00002/*
3 * Mini loadkmap implementation for busybox
4 *
5 * Copyright (C) 1998 Enrique Zanardi <ezanardi@ull.es>
6 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersenc4996011999-10-20 22:08:37 +00008 */
Pere Orga55068c42011-03-27 23:42:28 +02009
10//usage:#define loadkmap_trivial_usage
11//usage: "< keymap"
12//usage:#define loadkmap_full_usage "\n\n"
Denys Vlasenko86031a52015-01-24 19:46:45 +010013//usage: "Load a binary keyboard translation table from stdin"
14////usage: "\n"
15////usage: "\n -C TTY Affect TTY instead of /dev/tty"
Pere Orga55068c42011-03-27 23:42:28 +020016//usage:
17//usage:#define loadkmap_example_usage
18//usage: "$ loadkmap < /etc/i18n/lang-keymap\n"
19
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000021
Matt Kraai9133c982000-10-25 16:48:15 +000022#define BINARY_KEYMAP_MAGIC "bkeymap"
23
Eric Andersenbd22ed82000-07-08 18:55:24 +000024/* From <linux/kd.h> */
25struct kbentry {
26 unsigned char kb_table;
27 unsigned char kb_index;
28 unsigned short kb_value;
29};
Glenn L McGrathacbdc472003-11-21 09:27:02 +000030/* sets one entry in translation table */
31#define KDSKBENT 0x4B47
Eric Andersenbd22ed82000-07-08 18:55:24 +000032
33/* From <linux/keyboard.h> */
Glenn L McGrathacbdc472003-11-21 09:27:02 +000034#define NR_KEYS 128
35#define MAX_NR_KEYMAPS 256
Eric Andersenbd22ed82000-07-08 18:55:24 +000036
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000037int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko8aab0c92010-06-06 18:10:50 +020038int loadkmap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000039{
40 struct kbentry ke;
Glenn L McGrathacbdc472003-11-21 09:27:02 +000041 int i, j, fd;
Denis Vlasenko28703012006-12-19 20:32:02 +000042 uint16_t ibuff[NR_KEYS];
Denis Vlasenkodc700692008-11-08 21:39:06 +000043/* const char *tty_name = CURRENT_TTY; */
Denys Vlasenkoa355da02010-01-04 13:16:08 +010044 RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Denys Vlasenko2fd45c22010-06-06 18:09:57 +020046 /* When user accidentally runs "loadkmap FILE"
47 * instead of "loadkmap <FILE", we end up waiting for input from tty.
48 * Let's prevent it: */
49 if (argv[1])
50 bb_show_usage();
Denys Vlasenkoa355da02010-01-04 13:16:08 +010051/* bb_warn_ignoring_args(argv[1]); */
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020052
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000053 fd = get_console_fd_or_die();
Denis Vlasenkodc700692008-11-08 21:39:06 +000054/* or maybe:
55 opt = getopt32(argv, "C:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010056 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000057*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000058
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000059 xread(STDIN_FILENO, flags, 7);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010060 if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC))
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000061 bb_error_msg_and_die("not a valid binary keymap");
Erik Andersene49d5ec2000-02-08 19:58:47 +000062
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000063 xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
Erik Andersene49d5ec2000-02-08 19:58:47 +000065 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020066 if (flags[i] != 1)
67 continue;
68 xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
69 for (j = 0; j < NR_KEYS; j++) {
70 ke.kb_index = j;
71 ke.kb_table = i;
72 ke.kb_value = ibuff[j];
73 /*
74 * Note: table[idx:0] can contain special value
75 * K_ALLOCATED (marks allocated tables in kernel).
76 * dumpkmap saves the value as-is; but attempts
77 * to load it here fail, since it isn't a valid
78 * key value: it is K(KT_SPEC,126) == 2<<8 + 126,
79 * whereas last valid KT_SPEC is
80 * K_BARENUMLOCK == K(KT_SPEC,19).
81 * So far we just ignore these errors:
82 */
83 ioctl(fd, KDSKBENT, &ke);
Erik Andersene49d5ec2000-02-08 19:58:47 +000084 }
85 }
Glenn L McGrathacbdc472003-11-21 09:27:02 +000086
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000087 if (ENABLE_FEATURE_CLEAN_UP) {
88 close(fd);
89 RELEASE_CONFIG_BUFFER(flags);
90 }
91 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000092}