blob: c038e2d225855186bafd038ce8461f9541cb673e [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 Vlasenkob097a842018-12-28 03:20:17 +010010//config: bool "loadkmap (1.8 kb)"
Denys Vlasenko6d932992016-11-23 10:39:27 +010011//config: default y
Denys Vlasenko6d932992016-11-23 10:39:27 +010012//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020013//config: This program loads a keyboard translation table from
14//config: standard input.
Denys Vlasenko6d932992016-11-23 10:39:27 +010015
Denys Vlasenkoff53bee2017-08-05 02:02:31 +020016//applet:IF_LOADKMAP(APPLET_NOEXEC(loadkmap, loadkmap, BB_DIR_SBIN, BB_SUID_DROP, loadkmap))
Denys Vlasenko6d932992016-11-23 10:39:27 +010017
18//kbuild:lib-$(CONFIG_LOADKMAP) += loadkmap.o
Pere Orga55068c42011-03-27 23:42:28 +020019
20//usage:#define loadkmap_trivial_usage
21//usage: "< keymap"
22//usage:#define loadkmap_full_usage "\n\n"
Denys Vlasenko86031a52015-01-24 19:46:45 +010023//usage: "Load a binary keyboard translation table from stdin"
24////usage: "\n"
25////usage: "\n -C TTY Affect TTY instead of /dev/tty"
Pere Orga55068c42011-03-27 23:42:28 +020026//usage:
27//usage:#define loadkmap_example_usage
28//usage: "$ loadkmap < /etc/i18n/lang-keymap\n"
29
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000030#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000031
Matt Kraai9133c982000-10-25 16:48:15 +000032#define BINARY_KEYMAP_MAGIC "bkeymap"
33
Eric Andersenbd22ed82000-07-08 18:55:24 +000034/* From <linux/kd.h> */
35struct kbentry {
36 unsigned char kb_table;
37 unsigned char kb_index;
38 unsigned short kb_value;
39};
Glenn L McGrathacbdc472003-11-21 09:27:02 +000040/* sets one entry in translation table */
41#define KDSKBENT 0x4B47
Eric Andersenbd22ed82000-07-08 18:55:24 +000042
43/* From <linux/keyboard.h> */
Glenn L McGrathacbdc472003-11-21 09:27:02 +000044#define NR_KEYS 128
45#define MAX_NR_KEYMAPS 256
Eric Andersenbd22ed82000-07-08 18:55:24 +000046
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000047int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko8aab0c92010-06-06 18:10:50 +020048int loadkmap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000049{
50 struct kbentry ke;
Glenn L McGrathacbdc472003-11-21 09:27:02 +000051 int i, j, fd;
Denis Vlasenko28703012006-12-19 20:32:02 +000052 uint16_t ibuff[NR_KEYS];
Denis Vlasenkodc700692008-11-08 21:39:06 +000053/* const char *tty_name = CURRENT_TTY; */
Denys Vlasenkoa355da02010-01-04 13:16:08 +010054 RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
Eric Andersencc8ed391999-10-05 16:24:54 +000055
Denys Vlasenko2fd45c22010-06-06 18:09:57 +020056 /* When user accidentally runs "loadkmap FILE"
57 * instead of "loadkmap <FILE", we end up waiting for input from tty.
58 * Let's prevent it: */
59 if (argv[1])
60 bb_show_usage();
Denys Vlasenkoa355da02010-01-04 13:16:08 +010061/* bb_warn_ignoring_args(argv[1]); */
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020062
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000063 fd = get_console_fd_or_die();
Denis Vlasenkodc700692008-11-08 21:39:06 +000064/* or maybe:
65 opt = getopt32(argv, "C:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010066 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000067*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000068
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000069 xread(STDIN_FILENO, flags, 7);
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010070 if (!is_prefixed_with(flags, BINARY_KEYMAP_MAGIC))
James Byrne69374872019-07-02 11:35:03 +020071 bb_simple_error_msg_and_die("not a valid binary keymap");
Erik Andersene49d5ec2000-02-08 19:58:47 +000072
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000073 xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
Erik Andersene49d5ec2000-02-08 19:58:47 +000074
Erik Andersene49d5ec2000-02-08 19:58:47 +000075 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020076 if (flags[i] != 1)
77 continue;
78 xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
79 for (j = 0; j < NR_KEYS; j++) {
80 ke.kb_index = j;
81 ke.kb_table = i;
82 ke.kb_value = ibuff[j];
83 /*
84 * Note: table[idx:0] can contain special value
85 * K_ALLOCATED (marks allocated tables in kernel).
86 * dumpkmap saves the value as-is; but attempts
87 * to load it here fail, since it isn't a valid
88 * key value: it is K(KT_SPEC,126) == 2<<8 + 126,
89 * whereas last valid KT_SPEC is
90 * K_BARENUMLOCK == K(KT_SPEC,19).
91 * So far we just ignore these errors:
92 */
93 ioctl(fd, KDSKBENT, &ke);
Erik Andersene49d5ec2000-02-08 19:58:47 +000094 }
95 }
Glenn L McGrathacbdc472003-11-21 09:27:02 +000096
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000097 if (ENABLE_FEATURE_CLEAN_UP) {
98 close(fd);
99 RELEASE_CONFIG_BUFFER(flags);
100 }
101 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +0000102}