File: kbd_mode.c

package info (click to toggle)
kbd 2.0.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster, sid
  • size: 14,136 kB
  • sloc: sh: 12,838; ansic: 12,531; pascal: 472; lex: 466; makefile: 394; yacc: 356; perl: 126; sed: 16
file content (110 lines) | stat: -rw-r--r-- 2,236 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*
 * kbd_mode: report and set keyboard mode - aeb, 940406
 * 
 * If you make \215A\201 an alias for "kbd_mode -a", and you are
 * in raw mode, then hitting F7 = (two keys) will return you to sanity.
 */
#include "config.h"

#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include "getfd.h"
#include "nls.h"
#include "version.h"
#include "kbd_error.h"

static void __attribute__((noreturn))
usage(void)
{
	fprintf(stderr, _("usage: kbd_mode [-a|-u|-k|-s] [-C device]\n"));
	exit(EXIT_FAILURE);
}

int main(int argc, char *argv[])
{
	int fd, mode, c, n = 0;
	char *console = NULL;

	set_progname(argv[0]);

	setlocale(LC_ALL, "");
	bindtextdomain(PACKAGE_NAME, LOCALEDIR);
	textdomain(PACKAGE_NAME);

	if (argc == 2 && !strcmp(argv[1], "-V"))
		print_version_and_exit();

	while ((c = getopt(argc, argv, "auskC:")) != EOF) {
		switch (c) {
			case 'a':
				if (n > 0)
					usage();
				mode = K_XLATE;
				n++;
				break;
			case 'u':
				if (n > 0)
					usage();
				mode = K_UNICODE;
				n++;
				break;
			case 's':
				if (n > 0)
					usage();
				mode = K_RAW;
				n++;
				break;
			case 'k':
				if (n > 0)
					usage();
				mode = K_MEDIUMRAW;
				n++;
				break;
			case 'C':
				if (!optarg || !optarg[0])
					usage();
				console = optarg;
				break;
			default:
				usage();
		}
	}

	if ((fd = getfd(console)) < 0)
		kbd_error(EXIT_FAILURE, 0, _("Couldn't get a file descriptor referring to the console"));

	if (n == 0) {
		/* report mode */
		if (ioctl(fd, KDGKBMODE, &mode)) {
			kbd_error(EXIT_FAILURE, errno, "ioctl KDGKBMODE");
		}
		switch (mode) {
			case K_RAW:
				printf(_("The keyboard is in raw (scancode) mode\n"));
				break;
			case K_MEDIUMRAW:
				printf(_("The keyboard is in mediumraw (keycode) mode\n"));
				break;
			case K_XLATE:
				printf(_("The keyboard is in xlate (8-bit) mode\n"));
				break;
			case K_UNICODE:
				printf(_("The keyboard is in Unicode (UTF-8) mode\n"));
				break;
			default:
				printf(_("The keyboard is in some unknown mode\n"));
		}
		return EXIT_SUCCESS;
	}

	if (ioctl(fd, KDSKBMODE, mode)) {
		kbd_error(EXIT_FAILURE, errno, "ioctl KDSKBMODE");
	}

	return EXIT_SUCCESS;
}