4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
24 * Copyright (c) 1988 by Sun Microsystems, Inc.
27 #include <sys/types.h>
36 SM_INVALID
, /* this shift mask is invalid for this keyboard */
37 SM_NORMAL
, /* "normal", valid shift mask */
38 SM_NUMLOCK
, /* "Num Lock" shift mask */
39 SM_UP
/* "Up" shift mask */
49 smentry_t shiftmasks
[] = {
50 { "base", 0, SM_NORMAL
},
51 { "shift", SHIFTMASK
, SM_NORMAL
},
52 { "caps", CAPSMASK
, SM_NORMAL
},
53 { "ctrl", CTRLMASK
, SM_NORMAL
},
54 { "altg", ALTGRAPHMASK
, SM_NORMAL
},
55 { "numl", NUMLOCKMASK
, SM_NUMLOCK
},
56 { "up", UPMASK
, SM_UP
},
59 #define NSHIFTS (sizeof (shiftmasks) / sizeof (shiftmasks[0]))
61 static void printentry(struct kiockeymap
*kio
);
62 static void printchar(int character
, int delim
);
66 main(int argc
, char **argv
)
69 register int keystation
;
72 struct kiockeymap keyentry
[NSHIFTS
];
75 if ((kbdfd
= open("/dev/kbd", O_WRONLY
)) < 0) {
76 perror("dumpkeys: /dev/kbd");
79 if (ioctl(kbdfd
, KIOCTYPE
, &ktype
) < 0) {
80 perror("dumpkeys: ioctl(KIOCTYPE)");
83 /* if no keyboard detected, or ascii terminal, exit silently */
84 if (ktype
== KB_ASCII
|| ktype
< 0)
88 * See which shift masks are valid for this keyboard.
89 * We do that by trying to get the entry for keystation 0 and that
90 * shift mask; if the "ioctl" fails, we assume it's because the shift
93 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
94 keyentry
[shift
].kio_tablemask
=
95 shiftmasks
[shift
].sm_mask
;
96 keyentry
[shift
].kio_station
= 0;
97 if (ioctl(kbdfd
, KIOCGKEY
, &keyentry
[shift
]) < 0)
98 shiftmasks
[shift
].sm_type
= SM_INVALID
;
102 * Loop until we get an EINVAL, so we don't have to know
103 * how big the table might be.
105 for (keystation
= 0; ; keystation
++) {
106 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
107 if (shiftmasks
[shift
].sm_type
!= SM_INVALID
) {
108 keyentry
[shift
].kio_tablemask
=
109 shiftmasks
[shift
].sm_mask
;
110 keyentry
[shift
].kio_station
= keystation
;
111 if (ioctl(kbdfd
, KIOCGKEY
,
112 &keyentry
[shift
]) < 0) {
115 perror("dumpkeys: KIOCGKEY");
121 (void) printf("key %d\t", keystation
);
124 * See if all the "normal" entries (all but the Num Lock and Up
125 * entries) are the same.
128 for (shift
= 1; shift
< NSHIFTS
; shift
++) {
129 if (shiftmasks
[shift
].sm_type
== SM_NORMAL
) {
130 if (keyentry
[0].kio_entry
131 != keyentry
[shift
].kio_entry
) {
140 * All of the "normal" entries are the same; just print
143 (void) printf(" all ");
144 printentry(&keyentry
[0]);
147 * The normal entries aren't all the same; print them
150 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
151 if (shiftmasks
[shift
].sm_type
== SM_NORMAL
) {
152 (void) printf(" %s ",
153 shiftmasks
[shift
].sm_name
);
154 printentry(&keyentry
[shift
]);
158 if (allsame
&& keyentry
[0].kio_entry
== HOLE
) {
160 * This key is a "hole"; if either the Num Lock or Up
161 * entry isn't a "hole", print it.
163 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
164 switch (shiftmasks
[shift
].sm_type
) {
168 if (keyentry
[shift
].kio_entry
170 (void) printf(" %s ",
171 shiftmasks
[shift
].sm_name
);
172 printentry(&keyentry
[shift
]);
179 * This entry isn't a "hole"; if the Num Lock entry
180 * isn't NONL (i.e, if Num Lock actually does
181 * something) print it, and if the Up entry isn't NOP
182 * (i.e., if up transitions on this key actually do
183 * something) print it.
185 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
186 switch (shiftmasks
[shift
].sm_type
) {
189 if (keyentry
[shift
].kio_entry
191 (void) printf(" %s ",
192 shiftmasks
[shift
].sm_name
);
193 printentry(&keyentry
[shift
]);
198 if (keyentry
[shift
].kio_entry
200 (void) printf(" %s ",
201 shiftmasks
[shift
].sm_name
);
202 printentry(&keyentry
[shift
]);
212 static char *shiftkeys
[] = {
219 "meta", /* not used */
220 "top", /* not used */
221 "cmd", /* reserved */
227 #define NSHIFTKEYS (sizeof (shiftkeys) / sizeof (shiftkeys[0]))
229 static char *buckybits
[] = {
234 #define NBUCKYBITS (sizeof (buckybits) / sizeof (buckybits[0]))
236 static char *funnies
[] = {
250 #define NFUNNIES (sizeof (funnies) / sizeof (funnies[0]))
252 static char *fa_class
[] = {
270 #define NFA_CLASS (sizeof (fa_class) / sizeof (fa_class[0]))
277 builtin_string_t builtin_strings
[] = {
278 { "\033[H", "homearrow" },
279 { "\033[A", "uparrow" },
280 { "\033[B", "downarrow" },
281 { "\033[D", "leftarrow" },
282 { "\033[C", "rightarrow" },
285 #define NBUILTIN_STRINGS (sizeof (builtin_strings) / \
286 sizeof (builtin_strings[0]))
288 static char *fkeysets
[] = {
295 #define NFKEYSETS (sizeof (fkeysets) / sizeof (fkeysets[0]))
297 static char *padkeys
[] = {
318 #define NPADKEYS (sizeof (padkeys) / sizeof (padkeys[0]))
321 printentry(struct kiockeymap
*kio
)
323 int entry
= (kio
->kio_entry
& 0x1F);
328 switch (KEYFLAGS(kio
->kio_entry
)) {
331 if (kio
->kio_entry
== '"')
332 (void) printf("'\"'"); /* special case */
333 else if (kio
->kio_entry
== ' ')
334 (void) printf("' '"); /* special case */
336 printchar((int)kio
->kio_entry
, '\'');
340 if (entry
< NSHIFTKEYS
)
341 (void) printf("shiftkeys+%s", shiftkeys
[entry
]);
343 (void) printf("%#4x", kio
->kio_entry
);
347 if (entry
< NBUCKYBITS
)
348 (void) printf("buckybits+%s", buckybits
[entry
]);
350 (void) printf("%#4x", kio
->kio_entry
);
354 if (entry
< NFUNNIES
)
355 (void) printf("%s", funnies
[entry
]);
357 (void) printf("%#4x", kio
->kio_entry
);
361 if (entry
< NFA_CLASS
)
362 (void) printf("%s", fa_class
[entry
]);
364 (void) printf("%#4x", kio
->kio_entry
);
368 if (entry
< NBUILTIN_STRINGS
&& strncmp(kio
->kio_string
,
369 builtin_strings
[entry
].string
, KTAB_STRLEN
) == 0)
370 (void) printf("string+%s", builtin_strings
[entry
].name
);
374 i
< KTAB_STRLEN
&& (c
= kio
->kio_string
[i
]) != '\0';
382 fkeyset
= (int)(kio
->kio_entry
& 0xF0) >> 4;
383 if (fkeyset
< NFKEYSETS
)
384 (void) printf("%s(%d)", fkeysets
[fkeyset
],
387 (void) printf("%#4x", kio
->kio_entry
);
391 if (entry
< NPADKEYS
)
392 (void) printf("%s", padkeys
[entry
]);
394 (void) printf("%#4x", kio
->kio_entry
);
398 (void) printf("%#4x", kio
->kio_entry
);
404 printchar(int character
, int delim
)
409 (void) printf("'\\n'");
413 (void) printf("'\\t'");
417 (void) printf("'\\b'");
421 (void) printf("'\\r'");
425 (void) printf("'\\v'");
429 (void) printf("'\\\\'");
433 if (isprint(character
)) {
434 if (character
== delim
)
435 (void) printf("'\\'");
436 (void) printf("%c", character
);
439 (void) printf("^%c", character
+ 0100);
440 else if (character
<= 0xff)
441 (void) printf("'\\%.3o'", character
);
443 (void) printf("%#4x", character
);