blob: 905741467cc33e7371ceb8dffdcc7736b8eec7e3 [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 "internal.h"
24#include <errno.h>
25#include <fcntl.h>
26#include <stdio.h>
27#include <linux/kd.h>
28#include <linux/keyboard.h>
29#include <sys/ioctl.h>
30
31
Erik Andersen7ab9c7e2000-05-12 19:41:47 +000032static const char loadkmap_usage[] = "loadkmap\n"
33#ifndef BB_FEATURE_TRIVIAL_HELP
34 "Loads a binary keyboard translation table from standard input.\n"
35#endif
36 ;
Eric Andersencc8ed391999-10-05 16:24:54 +000037
38
Erik Andersene49d5ec2000-02-08 19:58:47 +000039int loadkmap_main(int argc, char **argv)
40{
41 struct kbentry ke;
42 u_short *ibuff;
43 int i, j, fd, readsz, pos, ibuffsz = NR_KEYS * sizeof(u_short);
44 char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap", buff[7];
Eric Andersencc8ed391999-10-05 16:24:54 +000045
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000046 if (argc>=2 && *argv[1]=='-') {
47 usage(loadkmap_usage);
48 }
49
Erik Andersene49d5ec2000-02-08 19:58:47 +000050 fd = open("/dev/tty0", O_RDWR);
51 if (fd < 0) {
52 fprintf(stderr, "Error opening /dev/tty0: %s\n", strerror(errno));
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000053 exit(FALSE);
Eric Andersencc8ed391999-10-05 16:24:54 +000054 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000055
56 read(0, buff, 7);
57 if (0 != strncmp(buff, magic, 7)) {
58 fprintf(stderr, "This is not a valid binary keymap.\n");
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000059 exit(FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000060 }
61
62 if (MAX_NR_KEYMAPS != read(0, flags, MAX_NR_KEYMAPS)) {
63 fprintf(stderr, "Error reading keymap flags: %s\n",
64 strerror(errno));
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000065 exit(FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000066 }
67
68 ibuff = (u_short *) malloc(ibuffsz);
69 if (!ibuff) {
70 fprintf(stderr, "Out of memory.\n");
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000071 exit(FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000072 }
73
74 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
75 if (flags[i] == 1) {
76 pos = 0;
77 while (pos < ibuffsz) {
78 if ((readsz = read(0, (char *) ibuff + pos, ibuffsz - pos))
79 < 0) {
80 fprintf(stderr, "Error reading keymap: %s\n",
81 strerror(errno));
Erik Andersen3fe7f9f2000-04-19 03:59:10 +000082 exit(FALSE);
Erik Andersene49d5ec2000-02-08 19:58:47 +000083 }
84 pos += readsz;
85 }
86 for (j = 0; j < NR_KEYS; j++) {
87 ke.kb_index = j;
88 ke.kb_table = i;
89 ke.kb_value = ibuff[j];
90 ioctl(fd, KDSKBENT, &ke);
91 }
92 }
93 }
Erik Andersen298854f2000-03-23 01:09:18 +000094 /* Don't bother to close files. Exit does that
95 * automagically, so we can save a few bytes */
96 /* close(fd); */
Eric Andersenb6106152000-06-19 17:25:40 +000097 return(TRUE);
Eric Andersencc8ed391999-10-05 16:24:54 +000098}