setkeycodes: something horrible happened here
[platform/upstream/busybox.git] / console-tools / setkeycodes.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * setkeycodes
4  *
5  * Copyright (C) 1994-1998 Andries E. Brouwer <aeb@cwi.nl>
6  *
7  * Adjusted for BusyBox by Erik Andersen <andersen@codepoet.org>
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  */
11
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <fcntl.h>
15 #include <sys/ioctl.h>
16 #include "busybox.h"
17
18
19 /* From <linux/kd.h> */
20 struct kbkeycode {
21         unsigned int scancode, keycode;
22 };
23 enum {
24         KDSETKEYCODE = 0x4B4D  /* write kernel keycode table entry */
25 };
26
27 extern int
28 setkeycodes_main(int argc, char** argv)
29 {
30         char *ep;
31         int fd, sc;
32         struct kbkeycode a;
33
34         if (argc % 2 != 1 || argc < 2) {
35                 bb_show_usage();
36         }
37
38         fd = get_console_fd();
39
40         while (argc > 2) {
41                 a.keycode = atoi(argv[2]);
42                 a.scancode = sc = strtol(argv[1], &ep, 16);
43                 if (*ep) {
44                         bb_error_msg_and_die("error reading SCANCODE: '%s'", argv[1]);
45                 }
46                 if (a.scancode > 127) {
47                         a.scancode -= 0xe000;
48                         a.scancode += 128;
49                 }
50                 if (a.scancode > 255 || a.keycode > 127) {
51                         bb_error_msg_and_die("SCANCODE or KEYCODE outside bounds");
52                 }
53                 if (ioctl(fd,KDSETKEYCODE,&a)) {
54                         bb_perror_msg_and_die("failed to set SCANCODE %x to KEYCODE %d", sc, a.keycode);
55                 }
56                 argc -= 2;
57                 argv += 2;
58         }
59         return EXIT_SUCCESS;
60 }