blob: 66ec3b043e8e3fab7e71636c3e5aa167b57b41c3 [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"
13//usage: "Load a binary keyboard translation table from stdin\n"
14/* //usage: "\n -C TTY Affect TTY instead of /dev/tty" */
15//usage:
16//usage:#define loadkmap_example_usage
17//usage: "$ loadkmap < /etc/i18n/lang-keymap\n"
18
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000019#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000020
Matt Kraai9133c982000-10-25 16:48:15 +000021#define BINARY_KEYMAP_MAGIC "bkeymap"
22
Eric Andersenbd22ed82000-07-08 18:55:24 +000023/* From <linux/kd.h> */
24struct kbentry {
25 unsigned char kb_table;
26 unsigned char kb_index;
27 unsigned short kb_value;
28};
Glenn L McGrathacbdc472003-11-21 09:27:02 +000029/* sets one entry in translation table */
30#define KDSKBENT 0x4B47
Eric Andersenbd22ed82000-07-08 18:55:24 +000031
32/* From <linux/keyboard.h> */
Glenn L McGrathacbdc472003-11-21 09:27:02 +000033#define NR_KEYS 128
34#define MAX_NR_KEYMAPS 256
Eric Andersenbd22ed82000-07-08 18:55:24 +000035
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000036int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko8aab0c92010-06-06 18:10:50 +020037int loadkmap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000038{
39 struct kbentry ke;
Glenn L McGrathacbdc472003-11-21 09:27:02 +000040 int i, j, fd;
Denis Vlasenko28703012006-12-19 20:32:02 +000041 uint16_t ibuff[NR_KEYS];
Denis Vlasenkodc700692008-11-08 21:39:06 +000042/* const char *tty_name = CURRENT_TTY; */
Denys Vlasenkoa355da02010-01-04 13:16:08 +010043 RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
Eric Andersencc8ed391999-10-05 16:24:54 +000044
Denys Vlasenko2fd45c22010-06-06 18:09:57 +020045 /* When user accidentally runs "loadkmap FILE"
46 * instead of "loadkmap <FILE", we end up waiting for input from tty.
47 * Let's prevent it: */
48 if (argv[1])
49 bb_show_usage();
Denys Vlasenkoa355da02010-01-04 13:16:08 +010050/* bb_warn_ignoring_args(argv[1]); */
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020051
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000052 fd = get_console_fd_or_die();
Denis Vlasenkodc700692008-11-08 21:39:06 +000053/* or maybe:
54 opt = getopt32(argv, "C:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010055 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000056*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000057
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000058 xread(STDIN_FILENO, flags, 7);
59 if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7))
60 bb_error_msg_and_die("not a valid binary keymap");
Erik Andersene49d5ec2000-02-08 19:58:47 +000061
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000062 xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
Erik Andersene49d5ec2000-02-08 19:58:47 +000063
Erik Andersene49d5ec2000-02-08 19:58:47 +000064 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
Denys Vlasenko054f3eb2013-09-19 17:56:59 +020065 if (flags[i] != 1)
66 continue;
67 xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
68 for (j = 0; j < NR_KEYS; j++) {
69 ke.kb_index = j;
70 ke.kb_table = i;
71 ke.kb_value = ibuff[j];
72 /*
73 * Note: table[idx:0] can contain special value
74 * K_ALLOCATED (marks allocated tables in kernel).
75 * dumpkmap saves the value as-is; but attempts
76 * to load it here fail, since it isn't a valid
77 * key value: it is K(KT_SPEC,126) == 2<<8 + 126,
78 * whereas last valid KT_SPEC is
79 * K_BARENUMLOCK == K(KT_SPEC,19).
80 * So far we just ignore these errors:
81 */
82 ioctl(fd, KDSKBENT, &ke);
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 }
84 }
Glenn L McGrathacbdc472003-11-21 09:27:02 +000085
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000086 if (ENABLE_FEATURE_CLEAN_UP) {
87 close(fd);
88 RELEASE_CONFIG_BUFFER(flags);
89 }
90 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000091}