blob: 5b2f31abcf8af840c85ca8e026359ca05f98af5a [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 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 */
22
Eric Andersencc8ed391999-10-05 16:24:54 +000023#include <errno.h>
24#include <fcntl.h>
25#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <string.h>
27#include <stdlib.h>
28#include <unistd.h>
Eric Andersencc8ed391999-10-05 16:24:54 +000029#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000030#include "busybox.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
Erik Andersene49d5ec2000-02-08 19:58:47 +000047int loadkmap_main(int argc, char **argv)
48{
49 struct kbentry ke;
Glenn L McGrathacbdc472003-11-21 09:27:02 +000050 int i, j, fd;
51 u_short ibuff[NR_KEYS];
52 char flags[MAX_NR_KEYMAPS];
53 char buff[7];
Eric Andersencc8ed391999-10-05 16:24:54 +000054
Matt Kraai9133c982000-10-25 16:48:15 +000055 if (argc != 1)
Manuel Novoa III cad53642003-03-19 09:13:01 +000056 bb_show_usage();
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000057
Glenn L McGrathacbdc472003-11-21 09:27:02 +000058 fd = bb_xopen(CURRENT_VC, O_RDWR);
Erik Andersene49d5ec2000-02-08 19:58:47 +000059
Glenn L McGrathacbdc472003-11-21 09:27:02 +000060 if ((bb_full_read(0, buff, 7) != 7) || (strncmp(buff, BINARY_KEYMAP_MAGIC, 7) != 0))
Manuel Novoa III cad53642003-03-19 09:13:01 +000061 bb_error_msg_and_die("This is not a valid binary keymap.");
Erik Andersene49d5ec2000-02-08 19:58:47 +000062
Glenn L McGrathacbdc472003-11-21 09:27:02 +000063 if (bb_full_read(0, flags, MAX_NR_KEYMAPS) != MAX_NR_KEYMAPS)
Manuel Novoa III cad53642003-03-19 09:13:01 +000064 bb_perror_msg_and_die("Error reading keymap flags");
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
67 if (flags[i] == 1) {
Glenn L McGrathacbdc472003-11-21 09:27:02 +000068 bb_full_read(0, ibuff, NR_KEYS * sizeof(u_short));
Erik Andersene49d5ec2000-02-08 19:58:47 +000069 for (j = 0; j < NR_KEYS; j++) {
70 ke.kb_index = j;
71 ke.kb_table = i;
72 ke.kb_value = ibuff[j];
73 ioctl(fd, KDSKBENT, &ke);
74 }
75 }
76 }
Glenn L McGrathacbdc472003-11-21 09:27:02 +000077
Erik Andersen298854f2000-03-23 01:09:18 +000078 /* Don't bother to close files. Exit does that
79 * automagically, so we can save a few bytes */
80 /* close(fd); */
Matt Kraai9133c982000-10-25 16:48:15 +000081 return EXIT_SUCCESS;
Eric Andersencc8ed391999-10-05 16:24:54 +000082}