blob: 2d1c9e2848178d9e4257c524e57c8355de7f1da2 [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 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +00009#include "libbb.h"
Eric Andersencc8ed391999-10-05 16:24:54 +000010
Matt Kraai9133c982000-10-25 16:48:15 +000011#define BINARY_KEYMAP_MAGIC "bkeymap"
12
Eric Andersenbd22ed82000-07-08 18:55:24 +000013/* From <linux/kd.h> */
14struct kbentry {
15 unsigned char kb_table;
16 unsigned char kb_index;
17 unsigned short kb_value;
18};
Glenn L McGrathacbdc472003-11-21 09:27:02 +000019/* sets one entry in translation table */
20#define KDSKBENT 0x4B47
Eric Andersenbd22ed82000-07-08 18:55:24 +000021
22/* From <linux/keyboard.h> */
Glenn L McGrathacbdc472003-11-21 09:27:02 +000023#define NR_KEYS 128
24#define MAX_NR_KEYMAPS 256
Eric Andersenbd22ed82000-07-08 18:55:24 +000025
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000026int loadkmap_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denys Vlasenko8aab0c92010-06-06 18:10:50 +020027int loadkmap_main(int argc UNUSED_PARAM, char **argv)
Erik Andersene49d5ec2000-02-08 19:58:47 +000028{
29 struct kbentry ke;
Glenn L McGrathacbdc472003-11-21 09:27:02 +000030 int i, j, fd;
Denis Vlasenko28703012006-12-19 20:32:02 +000031 uint16_t ibuff[NR_KEYS];
Denis Vlasenkodc700692008-11-08 21:39:06 +000032/* const char *tty_name = CURRENT_TTY; */
Denys Vlasenkoa355da02010-01-04 13:16:08 +010033 RESERVE_CONFIG_BUFFER(flags, MAX_NR_KEYMAPS);
Eric Andersencc8ed391999-10-05 16:24:54 +000034
Denys Vlasenko2fd45c22010-06-06 18:09:57 +020035 /* When user accidentally runs "loadkmap FILE"
36 * instead of "loadkmap <FILE", we end up waiting for input from tty.
37 * Let's prevent it: */
38 if (argv[1])
39 bb_show_usage();
Denys Vlasenkoa355da02010-01-04 13:16:08 +010040/* bb_warn_ignoring_args(argv[1]); */
Denis Vlasenko2afd5ab2008-08-05 23:32:27 +000041 fd = get_console_fd_or_die();
Denis Vlasenkodc700692008-11-08 21:39:06 +000042/* or maybe:
43 opt = getopt32(argv, "C:", &tty_name);
Bernhard Reutner-Fischera4830872009-10-26 23:27:08 +010044 fd = xopen_nonblocking(tty_name);
Denis Vlasenkodc700692008-11-08 21:39:06 +000045*/
Erik Andersene49d5ec2000-02-08 19:58:47 +000046
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000047 xread(STDIN_FILENO, flags, 7);
48 if (strncmp(flags, BINARY_KEYMAP_MAGIC, 7))
49 bb_error_msg_and_die("not a valid binary keymap");
Erik Andersene49d5ec2000-02-08 19:58:47 +000050
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000051 xread(STDIN_FILENO, flags, MAX_NR_KEYMAPS);
Erik Andersene49d5ec2000-02-08 19:58:47 +000052
Erik Andersene49d5ec2000-02-08 19:58:47 +000053 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
54 if (flags[i] == 1) {
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000055 xread(STDIN_FILENO, ibuff, NR_KEYS * sizeof(uint16_t));
Erik Andersene49d5ec2000-02-08 19:58:47 +000056 for (j = 0; j < NR_KEYS; j++) {
57 ke.kb_index = j;
58 ke.kb_table = i;
59 ke.kb_value = ibuff[j];
60 ioctl(fd, KDSKBENT, &ke);
61 }
62 }
63 }
Glenn L McGrathacbdc472003-11-21 09:27:02 +000064
Bernhard Reutner-Fischerae4342c2008-05-19 08:18:50 +000065 if (ENABLE_FEATURE_CLEAN_UP) {
66 close(fd);
67 RELEASE_CONFIG_BUFFER(flags);
68 }
69 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000070}