Add pancyrillic font
[platform/upstream/kbd.git] / src / kbdinfo.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <error.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6 #include <linux/kd.h>
7 #include <getopt.h>
8 #include "getfd.h"
9 #include "nls.h"
10 #include "version.h"
11
12 static const char *action = NULL;
13 static const char *value  = NULL;
14
15 static void __attribute__ ((noreturn))
16 usage(int code) {
17         fprintf(stderr,
18                 _("Usage: %1$s [-C DEVICE] getmode [text|graphics]\n"
19                   "   or: %1$s [-C DEVICE] gkbmode [raw|xlate|mediumraw|unicode]\n"
20                   "   or: %1$s [-C DEVICE] gkbmeta [metabit|escprefix]\n"
21                   "   or: %1$s [-C DEVICE] gkbled  [scrolllock|numlock|capslock]\n"),
22                 progname);
23         exit(code);
24 }
25
26 static int
27 answer(const char *ans) {
28         if (value)
29                 return strcasecmp(value, ans) ? EXIT_FAILURE : EXIT_SUCCESS;
30
31         printf("%s\n", ans);
32         return EXIT_SUCCESS;
33 }
34
35 int
36 main(int argc, char **argv) {
37         int fd, mode, c;
38         int rc = EXIT_FAILURE;
39         char flags;
40         const char *console = NULL;
41
42         set_progname(argv[0]);
43
44         setlocale(LC_ALL, "");
45         bindtextdomain(PACKAGE_NAME, LOCALEDIR);
46         textdomain(PACKAGE_NAME);
47
48         while ((c = getopt(argc, argv, "C:hV")) != EOF) {
49                 switch (c) {
50                         case 'C':
51                                 if (optarg == NULL || optarg[0] == '\0')
52                                         usage(EXIT_FAILURE);
53                                 console = optarg;
54                                 break;
55                         case 'V':
56                                 print_version_and_exit();
57                                 break;
58                         case 'h':
59                                 usage(EXIT_SUCCESS);
60                                 break;
61                 }
62         }
63
64         if (optind == argc) {
65                 fprintf(stderr, _("Error: Not enough arguments.\n"));
66                 exit(EXIT_FAILURE);
67         }
68
69         action = argv[optind++];
70
71         if (optind < argc)
72                 value = argv[optind++];
73
74         fd = getfd(console);
75
76         if (!strcasecmp("GETMODE", action)) {
77                 if (ioctl(fd, KDGETMODE, &mode) == -1)
78                         error(EXIT_FAILURE, errno, "ioctl");
79
80                 switch (mode) {
81                         case KD_TEXT:           rc = answer("text");            break;
82                         case KD_GRAPHICS:       rc = answer("graphics");        break;
83                 }
84
85         } else if (!strcasecmp("GKBMODE", action)) {
86                 if (ioctl(fd, KDGKBMODE, &mode) == -1)
87                         error(EXIT_FAILURE, errno, "ioctl");
88
89                 switch (mode) {
90                         case K_RAW:             rc = answer("raw");             break;
91                         case K_XLATE:           rc = answer("xlate");           break;
92                         case K_MEDIUMRAW:       rc = answer("mediumraw");       break;
93                         case K_UNICODE:         rc = answer("unicode");         break;
94                 }
95
96         } else if (!strcasecmp("GKBMETA", action)) {
97                 if (ioctl(fd, KDGKBMETA, &mode) == -1)
98                         error(EXIT_FAILURE, errno, "ioctl");
99
100                 switch (mode) {
101                         case K_METABIT:         rc = answer("metabit");         break;
102                         case K_ESCPREFIX:       rc = answer("escprefix");       break;
103                 }
104
105         } else if (!strcasecmp("GKBLED", action)) {
106                 if (ioctl(fd, KDGKBLED, &flags) == -1)
107                         error(EXIT_FAILURE, errno, "ioctl");
108
109                 mode = (flags & 0x7);
110
111                 if (value) {
112                         if (((mode & LED_SCR) && !strcasecmp(value, "scrolllock")) ||
113                             ((mode & LED_NUM) && !strcasecmp(value, "numlock"))    ||
114                             ((mode & LED_CAP) && !strcasecmp(value, "capslock")))
115                                 rc = EXIT_SUCCESS;
116                 } else {
117                         printf("scrolllock:%s ", (mode & LED_SCR) ? "on" : "off");
118                         printf("numlock:%s ",    (mode & LED_NUM) ? "on" : "off");
119                         printf("capslock:%s\n",  (mode & LED_CAP) ? "on" : "off");
120                         rc = EXIT_SUCCESS;
121                 }
122
123         } else {
124                 fprintf(stderr, _("Error: Unrecognized action: %s\n"), action);
125         }
126
127         close(fd);
128         return rc;
129 }