File: setmetamode.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 (109 lines) | stat: -rw-r--r-- 2,384 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
/*
 * setmetamode.c - aeb, 940130
 *
 * Call: setmetamode { metabit | escprefix }
 * and report the setting before and after.
 * Without arguments setmetamode will only report.
 */
#include "config.h"

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

static void __attribute__((noreturn))
usage(void)
{
	fprintf(stderr, _(
	                    "Usage:\n"
	                    "	setmetamode [ metabit | meta | bit | escprefix | esc | prefix ]\n"
	                    "Each vt has his own copy of this bit. Use\n"
	                    "	setmetamode [arg] < /dev/ttyn\n"
	                    "to change the settings of another vt.\n"
	                    "The setting before and after the change are reported.\n"));
	exit(EXIT_FAILURE);
}

static void
report(int meta)
{
	char *s;

	switch (meta) {
		case K_METABIT:
			s = _("Meta key sets high order bit\n");
			break;
		case K_ESCPREFIX:
			s = _("Meta key gives Esc prefix\n");
			break;
		default:
			s = _("Strange mode for Meta key?\n");
	}
	printf("%s", s);
}

struct meta {
	char *name;
	int val;
} metas[] = {
	{ "metabit", K_METABIT },
	{ "meta", K_METABIT },
	{ "bit", K_METABIT },
	{ "escprefix", K_ESCPREFIX },
	{ "esc", K_ESCPREFIX },
	{ "prefix", K_ESCPREFIX }
};

#define SIZE(a) (sizeof(a) / sizeof(a[0]))

int main(int argc, char **argv)
{
	char ometa, nmeta;
	struct meta *mp;

	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();

	if (ioctl(0, KDGKBMETA, &ometa)) {
		kbd_error(EXIT_FAILURE, errno, _("Error reading current setting. Maybe stdin is not a VT?: "
		                                 "ioctl KDGKBMETA"));
	}

	if (argc <= 1) {
		report(ometa);
		exit(EXIT_SUCCESS);
	}

	nmeta = 0; /* make gcc happy */
	for (mp = metas; (unsigned)(mp - metas) < SIZE(metas); mp++) {
		if (!strcmp(argv[1], mp->name)) {
			nmeta = mp->val;
			goto fnd;
		}
	}
	fprintf(stderr, _("unrecognized argument: _%s_\n\n"), argv[1]);
	usage();

fnd:
	printf(_("old state:    "));
	report(ometa);
	if (ioctl(0, KDSKBMETA, nmeta)) {
		kbd_error(EXIT_FAILURE, errno, "ioctl KDSKBMETA");
	}
	printf(_("new state:    "));
	report(nmeta);

	return EXIT_SUCCESS;
}