Add pancyrillic font
[platform/upstream/kbd.git] / src / dumpkeys.c
1 /*
2  * dumpkeys.c
3  *
4  * derived from version 0.81 - aeb@cwi.nl
5  */
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <ctype.h>
9 #include <fcntl.h>
10 #include <getopt.h>
11 #include <unistd.h>
12 #include <linux/types.h>
13 #include <linux/kd.h>
14 #include <linux/keyboard.h>
15 #include <sys/ioctl.h>
16 #include <string.h>
17 #include <errno.h>
18 #include "ksyms.h"
19 #include "getfd.h"
20 #include "modifiers.h"
21 #include "nls.h"
22 #include "version.h"
23
24 static int fd;
25
26 static void __attribute__ ((noreturn))
27 usage(void) {
28         fprintf(stderr, _("dumpkeys version %s"), PACKAGE_VERSION);
29         fprintf(stderr, _("\
30 \n\
31 usage: dumpkeys [options...]\n\
32 \n\
33 valid options are:\n\
34 \n\
35         -h --help           display this help text\n\
36         -i --short-info     display information about keyboard driver\n\
37         -l --long-info      display above and symbols known to loadkeys\n\
38         -n --numeric        display keytable in hexadecimal notation\n\
39         -f --full-table     don't use short-hand notations, one row per keycode\n\
40         -1 --separate-lines one line per (modifier,keycode) pair\n\
41            --funcs-only     display only the function key strings\n\
42            --keys-only      display only key bindings\n\
43            --compose-only   display only compose key combinations\n\
44         -c --charset="));
45         lk_list_charsets(stderr);
46         fprintf(stderr, _("\
47                             interpret character action codes to be from the\n\
48                             specified character set\n\
49 "));
50         exit(1);
51 }
52
53 int
54 main (int argc, char *argv[]) {
55         const char *short_opts = "hilvsnf1tkdS:c:V";
56         const struct option long_opts[] = {
57                 { "help",       no_argument,            NULL, 'h' },
58                 { "short-info", no_argument,            NULL, 'i' },
59                 { "long-info",  no_argument,            NULL, 'l' },
60                 { "numeric",    no_argument,            NULL, 'n' },
61                 { "full-table", no_argument,            NULL, 'f' },
62                 { "separate-lines",no_argument,         NULL, '1' },
63                 { "shape",      required_argument,      NULL, 'S' },
64                 { "funcs-only", no_argument,            NULL, 't' },
65                 { "keys-only",  no_argument,            NULL, 'k' },
66                 { "compose-only",no_argument,           NULL, 'd' },
67                 { "charset",    required_argument,      NULL, 'c' },
68                 { "verbose",    no_argument,            NULL, 'v' },
69                 { "version",    no_argument,            NULL, 'V' },
70                 { NULL, 0, NULL, 0 }
71         };
72         int c, rc;
73         int kbd_mode;
74
75         char long_info = 0;
76         char short_info = 0;
77         char numeric = 0;
78         lk_table_shape table = LK_SHAPE_DEFAULT;
79         char funcs_only = 0;
80         char keys_only = 0;
81         char diac_only = 0;
82
83         struct lk_ctx *ctx;
84
85         set_progname(argv[0]);
86
87         setlocale(LC_ALL, "");
88         bindtextdomain(PACKAGE_NAME, LOCALEDIR);
89         textdomain(PACKAGE_NAME);
90
91         ctx = lk_init();
92         if (!ctx) {
93                 exit(EXIT_FAILURE);
94         }
95
96         while ((c = getopt_long(argc, argv,
97                 short_opts, long_opts, NULL)) != -1) {
98                 switch (c) {
99                         case 'i':
100                                 short_info = 1;
101                                 break;
102                         case 's':
103                         case 'l':
104                                 long_info = 1;
105                                 break;
106                         case 'n':
107                                 numeric = 1;
108                                 break;
109                         case 'f':
110                                 table = LK_SHAPE_FULL_TABLE;
111                                 break;
112                         case '1':
113                                 table = LK_SHAPE_SEPARATE_LINES;
114                                 break;
115                         case 'S':
116                                 table = atoi(optarg);
117                                 break;
118                         case 't':
119                                 funcs_only = 1;
120                                 break;
121                         case 'k':
122                                 keys_only = 1;
123                                 break;
124                         case 'd':
125                                 diac_only = 1;
126                                 break;
127                         case 'v':
128                                 lk_set_log_priority(ctx, LOG_INFO);
129                                 break;
130                         case 'c':
131                                 if ((lk_set_charset(ctx, optarg)) != 0) {
132                                         fprintf(stderr, _("unknown charset %s - ignoring charset request\n"),
133                                                 optarg);
134                                         usage();
135                                 }
136                                 printf("charset \"%s\"\n", optarg);
137                                 break;
138                         case 'V':
139                                 print_version_and_exit();
140                         case 'h':
141                         case '?':
142                                 usage();
143                 }
144         }
145
146         if (optind < argc)
147                 usage();
148
149         fd = getfd(NULL);
150
151         /* check whether the keyboard is in Unicode mode */
152         if (ioctl(fd, KDGKBMODE, &kbd_mode)) {
153                 fprintf(stderr, _("%s: error reading keyboard mode: %m\n"),
154                         progname);
155                 exit(EXIT_FAILURE);
156         }
157
158         if (kbd_mode == K_UNICODE) {
159                 lk_set_parser_flags(ctx, LK_FLAG_PREFER_UNICODE);
160         }
161
162         if ((rc = lk_kernel_keymap(ctx, fd)) < 0)
163                 goto fail;
164
165         if (short_info || long_info) {
166                 lk_dump_summary(ctx, stdout, fd);
167
168                 if (long_info) {
169                         printf(_("Symbols recognized by %s:\n(numeric value, symbol)\n\n"),
170                                 progname);
171                         lk_dump_symbols(ctx, stdout);
172                 }
173                 exit(EXIT_SUCCESS);
174         }
175
176 #ifdef KDGKBDIACR
177         if (!diac_only) {
178 #endif
179         if (!funcs_only) {
180                 lk_dump_keymap(ctx, stdout, table, numeric);
181         }
182 #ifdef KDGKBDIACR
183         }
184
185         if (!funcs_only && !keys_only)
186                 lk_dump_diacs(ctx, stdout);
187 #endif
188
189  fail:  lk_free(ctx);
190         close(fd);
191
192         if (rc < 0)
193                 exit(EXIT_FAILURE);
194
195         exit(EXIT_SUCCESS);
196 }