blob: 839dc20833417c91c36c3b634bf348e3caf33363 [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 */
Denys Vlasenko6d932992016-11-23 10:39:27 +01009//config:config LOADKMAP
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020010//config: bool "loadkmap (1.5 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010011//config: default y
12//config: select PLATFORM_LINUX
13//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020014//config: This program loads a keyboard translation table from
15//config: standard input.
Denys Vlasenko6d932992016-11-23 10:39:27 +010016
17//applet:IF_LOADKMAP(APPLET(loadkmap, BB_DIR_SBIN, BB_SUID_DROP))
18
19//kbuild:lib-$(CONFIG_LOADKMAP) += loadkmap.o
Pere Orga55068c42011-03-27 23:42:28 +020020
21//usage:#define loadkmap_trivial_usage
22//usage: "< keymap"
23//usage:#define loadkmap_full_usage "\n\n"
Denys Vlasenko86031a52015-01-24 19:46:45 +010024//usage: "Load a binary keyboard translation table from stdin"
25////usage: "\n"
26////usage: "\n -C TTY Affect TTY instead of /dev/tty"
Pere Orga55068c42011-03-27 23:42:28 +020027//usage:
28//usage:#define loadkmap_example_usage
29//usage: "$ loadkmap < /etc/i18n/lang-keymap\n"
30
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000031#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000032
Matt Kraai9133c982000-10-25 16:48:15 +000033#define BINARY_KEYMAP_MAGIC "bkeymap"
34
Eric Andersenbd22ed82000-07-08 18:55:24 +000035/* From <linux/kd.h> */
36struct kbentry {
37 unsigned char kb_table;
38 unsigned char kb_index;
39 unsigned short kb_value;
40};
Glenn L McGrathacbdc472003-11-21 09:27:02 +000041/* sets one entry in translation table */
42#define KDSKBENT 0x4B47
Eric Andersenbd22ed82000-07-08 18:55:24 +000043
44/* From <linux/keyboard.h> */
Glenn L McGrathacbdc472003-11-21 09:27:02 +000045#define NR_KEYS 128
46#define MAX_NR_KEYMAPS 256
Eric Andersenbd22ed82000-07-08 18:55:24 +000047
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000048int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko8aab0c92010-06-06 18:10:50 +020049int loadkmap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000050{
51 struct kbentry ke;
Glenn L McGrathacbdc472003-11-21 09:27:02 +000052 int i, j, fd;
Denis Vlasenko28703012006-12-19 20:32:02 +000053 uint16_t ibuff[NR_KEYS];
Denis Vlasenkodc700692008-11-08 21:39:06 +000054/* const char *tty_name = CURRENT_TTY; */
Denys Vlasenkoa355da02010-01-04 13:16:08 +010055 RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
Eric Andersencc8ed391999-10-05 16:24:54 +000056
Denys Vlasenko2fd45c22010-06-06 18:09:57 +020057 /* When user accidentally runs "loadkmap FILE"
58 * instead of "loadkmap <FILE", we end up waiting for input from tty.
59 * Let's prevent it: */
60 if (argv[1])
61 bb_show_usage();
Denys Vlasenkoa355da02010-01-04 13:16:08 +010062/* bb_warn_ignoring_args(argv[1]); */
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020063
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000064 fd = get_console_fd_or_die();
Denis Vlasenkodc700692008-11-08 21:39:06 +000065/* or maybe:
66 opt = getopt32(argv, "C:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010067 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000068*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000069
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000070 xread(STDIN_FILENO, flags, 7);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010071 if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC))
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000072 bb_error_msg_and_die("not a valid binary keymap");
Erik Andersene49d5ec2000-02-08 19:58:47 +000073
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000074 xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
Erik Andersene49d5ec2000-02-08 19:58:47 +000075
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020077 if (flags[i] != 1)
78 continue;
79 xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
80 for (j = 0; j < NR_KEYS; j++) {
81 ke.kb_index = j;
82 ke.kb_table = i;
83 ke.kb_value = ibuff[j];
84 /*
85 * Note: table[idx:0] can contain special value
86 * K_ALLOCATED (marks allocated tables in kernel).
87 * dumpkmap saves the value as-is; but attempts
88 * to load it here fail, since it isn't a valid
89 * key value: it is K(KT_SPEC,126) == 2<<8 + 126,
90 * whereas last valid KT_SPEC is
91 * K_BARENUMLOCK == K(KT_SPEC,19).
92 * So far we just ignore these errors:
93 */
94 ioctl(fd, KDSKBENT, &ke);
Erik Andersene49d5ec2000-02-08 19:58:47 +000095 }
96 }
Glenn L McGrathacbdc472003-11-21 09:27:02 +000097
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000098 if (ENABLE_FEATURE_CLEAN_UP) {
99 close(fd);
100 RELEASE_CONFIG_BUFFER(flags);
101 }
102 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000103}