46ec3fd283cd4c1a7a727fd18b7ff0baa196f75b
[platform/upstream/busybox.git] / console-tools / kbd_mode.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini loadkmap implementation for busybox
4  *
5  * Copyright (C) 2007 Loïc Grenié <loic.grenie@gmail.com>
6  *   written using Andries Brouwer <aeb@cwi.nl>'s kbd_mode from
7  *   console-utils v0.2.3, licensed under GNU GPLv2
8  *
9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10  *
11  */
12
13 #include "libbb.h"
14 #include <linux/kd.h>
15
16 int kbd_mode_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
17 int kbd_mode_main(int argc, char **argv)
18 {
19         static const char opts[] = "saku";
20
21         const char *opt = argv[1];
22         const char *p;
23         int fd;
24
25         fd = get_console_fd();
26         if (fd < 0) /* get_console_fd() already complained */
27                 return EXIT_FAILURE;
28
29         if (opt == NULL) {
30                 /* No arg */
31                 const char *msg = "unknown";
32                 int mode;
33
34                 ioctl(fd, KDGKBMODE, &mode);
35                 switch(mode) {
36                 case K_RAW:
37                         msg = "raw (scancode)";
38                         break;
39                 case K_XLATE:
40                         msg = "default (ASCII)";
41                         break;
42                 case K_MEDIUMRAW:
43                         msg = "mediumraw (keycode)";
44                         break;
45                 case K_UNICODE:
46                         msg = "Unicode (UTF-8)";
47                         break;
48                 }
49                 printf("The keyboard is in %s mode\n", msg);
50         }
51         else if (argc > 2 /* more than 1 arg */
52          || *opt != '-' /* not an option */
53          || (p = strchr(opts, opt[1])) == NULL /* not an option we expect */
54          || opt[2] != '\0' /* more than one option char */
55         ) {
56                 bb_show_usage();
57                 /* return EXIT_FAILURE; - not reached */
58         }
59         else {
60 #if K_RAW != 0 || K_XLATE != 1 || K_MEDIUMRAW != 2 || K_UNICODE != 3
61 #error kbd_mode must be changed
62 #endif
63                 /* The options are in the order of the various K_xxx */
64                 ioctl(fd, KDSKBMODE, p - opts);
65         }
66
67         if (ENABLE_FEATURE_CLEAN_UP)
68                 close(fd);
69         return EXIT_SUCCESS;
70 }