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]
22 #ident "%Z%%M% %I% %E% SMI"
25 * Copyright (c) 1988 by Sun Microsystems, Inc.
28 #include <sys/types.h>
37 SM_INVALID
, /* this shift mask is invalid for this keyboard */
38 SM_NORMAL
, /* "normal", valid shift mask */
39 SM_NUMLOCK
, /* "Num Lock" shift mask */
40 SM_UP
/* "Up" shift mask */
50 smentry_t shiftmasks
[] = {
51 { "base", 0, SM_NORMAL
},
52 { "shift", SHIFTMASK
, SM_NORMAL
},
53 { "caps", CAPSMASK
, SM_NORMAL
},
54 { "ctrl", CTRLMASK
, SM_NORMAL
},
55 { "altg", ALTGRAPHMASK
, SM_NORMAL
},
56 { "numl", NUMLOCKMASK
, SM_NUMLOCK
},
57 { "up", UPMASK
, SM_UP
},
60 #define NSHIFTS (sizeof (shiftmasks) / sizeof (shiftmasks[0]))
62 static void printentry(struct kiockeymap
*kio
);
63 static void printchar(int character
, int delim
);
72 register int keystation
;
75 struct kiockeymap keyentry
[NSHIFTS
];
78 if ((kbdfd
= open("/dev/kbd", O_WRONLY
)) < 0) {
79 perror("dumpkeys: /dev/kbd");
82 if (ioctl(kbdfd
, KIOCTYPE
, &ktype
) < 0) {
83 perror("dumpkeys: ioctl(KIOCTYPE)");
86 /* if no keyboard detected, or ascii terminal, exit silently */
87 if (ktype
== KB_ASCII
|| ktype
< 0)
91 * See which shift masks are valid for this keyboard.
92 * We do that by trying to get the entry for keystation 0 and that
93 * shift mask; if the "ioctl" fails, we assume it's because the shift
96 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
97 keyentry
[shift
].kio_tablemask
=
98 shiftmasks
[shift
].sm_mask
;
99 keyentry
[shift
].kio_station
= 0;
100 if (ioctl(kbdfd
, KIOCGKEY
, &keyentry
[shift
]) < 0)
101 shiftmasks
[shift
].sm_type
= SM_INVALID
;
105 * Loop until we get an EINVAL, so we don't have to know
106 * how big the table might be.
108 for (keystation
= 0; ; keystation
++) {
109 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
110 if (shiftmasks
[shift
].sm_type
!= SM_INVALID
) {
111 keyentry
[shift
].kio_tablemask
=
112 shiftmasks
[shift
].sm_mask
;
113 keyentry
[shift
].kio_station
= keystation
;
114 if (ioctl(kbdfd
, KIOCGKEY
,
115 &keyentry
[shift
]) < 0) {
118 perror("dumpkeys: KIOCGKEY");
124 (void) printf("key %d\t", keystation
);
127 * See if all the "normal" entries (all but the Num Lock and Up
128 * entries) are the same.
131 for (shift
= 1; shift
< NSHIFTS
; shift
++) {
132 if (shiftmasks
[shift
].sm_type
== SM_NORMAL
) {
133 if (keyentry
[0].kio_entry
134 != keyentry
[shift
].kio_entry
) {
143 * All of the "normal" entries are the same; just print
146 (void) printf(" all ");
147 printentry(&keyentry
[0]);
150 * The normal entries aren't all the same; print them
153 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
154 if (shiftmasks
[shift
].sm_type
== SM_NORMAL
) {
155 (void) printf(" %s ",
156 shiftmasks
[shift
].sm_name
);
157 printentry(&keyentry
[shift
]);
161 if (allsame
&& keyentry
[0].kio_entry
== HOLE
) {
163 * This key is a "hole"; if either the Num Lock or Up
164 * entry isn't a "hole", print it.
166 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
167 switch (shiftmasks
[shift
].sm_type
) {
171 if (keyentry
[shift
].kio_entry
173 (void) printf(" %s ",
174 shiftmasks
[shift
].sm_name
);
175 printentry(&keyentry
[shift
]);
182 * This entry isn't a "hole"; if the Num Lock entry
183 * isn't NONL (i.e, if Num Lock actually does
184 * something) print it, and if the Up entry isn't NOP
185 * (i.e., if up transitions on this key actually do
186 * something) print it.
188 for (shift
= 0; shift
< NSHIFTS
; shift
++) {
189 switch (shiftmasks
[shift
].sm_type
) {
192 if (keyentry
[shift
].kio_entry
194 (void) printf(" %s ",
195 shiftmasks
[shift
].sm_name
);
196 printentry(&keyentry
[shift
]);
201 if (keyentry
[shift
].kio_entry
203 (void) printf(" %s ",
204 shiftmasks
[shift
].sm_name
);
205 printentry(&keyentry
[shift
]);
215 static char *shiftkeys
[] = {
222 "meta", /* not used */
223 "top", /* not used */
224 "cmd", /* reserved */
230 #define NSHIFTKEYS (sizeof (shiftkeys) / sizeof (shiftkeys[0]))
232 static char *buckybits
[] = {
237 #define NBUCKYBITS (sizeof (buckybits) / sizeof (buckybits[0]))
239 static char *funnies
[] = {
250 #define NFUNNIES (sizeof (funnies) / sizeof (funnies[0]))
252 static char *fa_class
[] = {
261 #define NFA_CLASS (sizeof (fa_class) / sizeof (fa_class[0]))
268 builtin_string_t builtin_strings
[] = {
269 { "\033[H", "homearrow" },
270 { "\033[A", "uparrow" },
271 { "\033[B", "downarrow" },
272 { "\033[D", "leftarrow" },
273 { "\033[C", "rightarrow" },
276 #define NBUILTIN_STRINGS (sizeof (builtin_strings) / \
277 sizeof (builtin_strings[0]))
279 static char *fkeysets
[] = {
286 #define NFKEYSETS (sizeof (fkeysets) / sizeof (fkeysets[0]))
288 static char *padkeys
[] = {
309 #define NPADKEYS (sizeof (padkeys) / sizeof (padkeys[0]))
313 register struct kiockeymap
*kio
;
315 register int entry
= (kio
->kio_entry
& 0x1F);
316 register int fkeyset
;
320 switch (kio
->kio_entry
>> 8) {
323 if (kio
->kio_entry
== '"')
324 (void) printf("'\"'"); /* special case */
325 else if (kio
->kio_entry
== ' ')
326 (void) printf("' '"); /* special case */
328 printchar((int)kio
->kio_entry
, '\'');
332 if (entry
< NSHIFTKEYS
)
333 (void) printf("shiftkeys+%s", shiftkeys
[entry
]);
335 (void) printf("%#4x", kio
->kio_entry
);
339 if (entry
< NBUCKYBITS
)
340 (void) printf("buckybits+%s", buckybits
[entry
]);
342 (void) printf("%#4x", kio
->kio_entry
);
346 if (entry
< NFUNNIES
)
347 (void) printf("%s", funnies
[entry
]);
349 (void) printf("%#4x", kio
->kio_entry
);
353 if (entry
< NFA_CLASS
)
354 (void) printf("%s", fa_class
[entry
]);
356 (void) printf("%#4x", kio
->kio_entry
);
360 if (entry
< NBUILTIN_STRINGS
&& strncmp(kio
->kio_string
,
361 builtin_strings
[entry
].string
, KTAB_STRLEN
) == 0)
362 (void) printf("string+%s", builtin_strings
[entry
].name
);
366 i
< KTAB_STRLEN
&& (c
= kio
->kio_string
[i
]) != '\0';
374 fkeyset
= (int)(kio
->kio_entry
& 0xF0) >> 4;
375 if (fkeyset
< NFKEYSETS
)
376 (void) printf("%s(%d)", fkeysets
[fkeyset
],
379 (void) printf("%#4x", kio
->kio_entry
);
383 if (entry
< NPADKEYS
)
384 (void) printf("%s", padkeys
[entry
]);
386 (void) printf("%#4x", kio
->kio_entry
);
390 (void) printf("%#4x", kio
->kio_entry
);
396 printchar(character
, delim
)
403 (void) printf("'\\n'");
407 (void) printf("'\\t'");
411 (void) printf("'\\b'");
415 (void) printf("'\\r'");
419 (void) printf("'\\v'");
423 (void) printf("'\\\\'");
427 if (isprint(character
)) {
428 if (character
== delim
)
429 (void) printf("'\\'");
430 (void) printf("%c", character
);
433 (void) printf("^%c", character
+ 0100);
435 (void) printf("'\\%.3o'", character
);