1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <fcntl.h>
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <linux/kd.h>
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <linux/vt.h>
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void setkey_usage(char *argv[])
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "%s [-t <table>] [-k <index>] [-v value] [-r] [-h]\n"
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -t <table> Select table\n"
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -k <index> Select key\n"
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -v <value> Set entry\n"
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -r         Read current entry\n"
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "  -h         Print help\n", argv[0]);
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define TTYDEV	"/dev/tty0"
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint setkey_main(int argc, char *argv[])
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd;
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct kbentry kbe;
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int did_something = 0;
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    kbe.kb_table = 0;
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    kbe.kb_index = -1;
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    kbe.kb_value = 0;
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = open(TTYDEV, O_RDWR | O_SYNC);
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (fd < 0) {
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "open %s: %s\n", TTYDEV, strerror(errno));
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    do {
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int c, ret;
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        c = getopt(argc, argv, "t:k:v:hr");
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (c == EOF)
42dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        switch (c) {
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 't':
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            kbe.kb_table = strtol(optarg, NULL, 0);
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'k':
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            kbe.kb_index = strtol(optarg, NULL, 0);
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'v':
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            kbe.kb_value = strtol(optarg, NULL, 0);
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ret = ioctl(fd, KDSKBENT, &kbe);
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (ret < 0) {
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                fprintf(stderr, "KDSKBENT %d %d %d failed: %s\n",
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        kbe.kb_table, kbe.kb_index, kbe.kb_value,
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        strerror(errno));
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                return 1;
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            did_something = 1;
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'r':
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ret = ioctl(fd, KDGKBENT, &kbe);
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (ret < 0) {
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                fprintf(stderr, "KDGKBENT %d %d  failed: %s\n",
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        kbe.kb_table, kbe.kb_index, strerror(errno));
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                return 1;
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            printf("0x%x 0x%x 0x%x\n",
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                   kbe.kb_table, kbe.kb_index, kbe.kb_value);
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            did_something = 1;
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case 'h':
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            setkey_usage(argv);
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        case '?':
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "%s: invalid option -%c\n",
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argv[0], optopt);
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } while (1);
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(optind != argc || !did_something) {
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        setkey_usage(argv);
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
87dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
88dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
89dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
90