blob: 62313e77f297eefd9d09efce152ed71bd5355680 [file] [log] [blame]
Eric Andersen61dc0572000-07-11 17:29:36 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Mini dumpkmap implementation for busybox
4 *
5 * Copyright (C) Arne Bernin <arne@matrix.loopback.org>
6 *
"Robert P. J. Day"801ab142006-07-12 07:56:04 +00007 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersen61dc0572000-07-11 17:29:36 +00008 *
9 */
10
Eric Andersen61dc0572000-07-11 17:29:36 +000011#include <errno.h>
12#include <fcntl.h>
13#include <stdio.h>
Eric Andersened3ef502001-01-27 08:24:39 +000014#include <unistd.h>
15#include <string.h>
16#include <stdlib.h>
Eric Andersen61dc0572000-07-11 17:29:36 +000017#include <sys/ioctl.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000018#include "busybox.h"
Eric Andersen61dc0572000-07-11 17:29:36 +000019
20/* From <linux/kd.h> */
21struct kbentry {
22 unsigned char kb_table;
23 unsigned char kb_index;
24 unsigned short kb_value;
25};
Mike Frysinger7e64db22005-07-31 22:09:33 +000026#define KDGKBENT 0x4B46 /* gets one entry in translation table */
Eric Andersen61dc0572000-07-11 17:29:36 +000027
28/* From <linux/keyboard.h> */
Mike Frysinger7e64db22005-07-31 22:09:33 +000029#define NR_KEYS 128
30#define MAX_NR_KEYMAPS 256
Eric Andersen61dc0572000-07-11 17:29:36 +000031
Eric Andersen61dc0572000-07-11 17:29:36 +000032int dumpkmap_main(int argc, char **argv)
33{
34 struct kbentry ke;
35 int i, j, fd;
36 char flags[MAX_NR_KEYMAPS], magic[] = "bkeymap";
37
Mike Frysingerf28c7ec2005-07-30 09:24:49 +000038 if (argc >= 2 && *argv[1] == '-')
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_show_usage();
Eric Andersen61dc0572000-07-11 17:29:36 +000040
Mike Frysingerf28c7ec2005-07-30 09:24:49 +000041 fd = bb_xopen(CURRENT_VC, O_RDWR);
Eric Andersen61dc0572000-07-11 17:29:36 +000042
43 write(1, magic, 7);
44
Mike Frysingere11ff712005-07-31 22:06:38 +000045 /* Here we want to set everything to 0 except for indexes:
46 * [0-2] [4-6] [8-10] [12] */
Mike Frysinger08c20362005-07-31 20:51:58 +000047 memset(flags, 0x00, MAX_NR_KEYMAPS);
Mike Frysingere11ff712005-07-31 22:06:38 +000048 memset(flags, 0x01, 13);
49 flags[3] = flags[7] = flags[11] = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000050
Eric Andersen61dc0572000-07-11 17:29:36 +000051 /* dump flags */
Mike Frysinger08c20362005-07-31 20:51:58 +000052 write(1, flags, MAX_NR_KEYMAPS);
Eric Andersen61dc0572000-07-11 17:29:36 +000053
54 for (i = 0; i < MAX_NR_KEYMAPS; i++) {
55 if (flags[i] == 1) {
56 for (j = 0; j < NR_KEYS; j++) {
57 ke.kb_index = j;
58 ke.kb_table = i;
59 if (ioctl(fd, KDGKBENT, &ke) < 0) {
Mike Frysinger198ea3c2005-07-30 09:29:10 +000060 bb_perror_msg("ioctl failed with %s, %s, %p",
Mike Frysingerf28c7ec2005-07-30 09:24:49 +000061 (char *)&ke.kb_index,
62 (char *)&ke.kb_table,
Mike Frysinger198ea3c2005-07-30 09:29:10 +000063 &ke.kb_value);
Mike Frysingerf28c7ec2005-07-30 09:24:49 +000064 } else {
65 write(1, (void*)&ke.kb_value, 2);
66 }
Eric Andersen61dc0572000-07-11 17:29:36 +000067 }
68 }
69 }
70 close(fd);
Matt Kraai3e856ce2000-12-01 02:55:13 +000071 return EXIT_SUCCESS;
Eric Andersen61dc0572000-07-11 17:29:36 +000072}