File: getunimap.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 (108 lines) | stat: -rw-r--r-- 2,485 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
#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <linux/kd.h>
#include "getfd.h"
#include "xmalloc.h"
#include "kdmapop.h"
#include "nls.h"
#include "version.h"

#ifndef USE_LIBC
/* There is such function in libc5 but it doesn't work for me [libc 5.4.13] */
#include "wctomb.c"
#define wctomb our_wctomb
#endif

static int
ud_compar(const void *u1, const void *u2)
{
	unsigned short fp1 = ((struct unipair *)u1)->fontpos;
	unsigned short fp2 = ((struct unipair *)u2)->fontpos;
	return (int)fp1 - (int)fp2;
}

static void __attribute__((noreturn))
usage(void)
{
	fprintf(stderr, _("Usage:\n\t%s [-s] [-C console]\n"), progname);
	exit(EXIT_FAILURE);
}

int main(int argc, char **argv)
{
	int sortflag = 0;
	char mb[]    = { 0, 0, 0, 0, 0, 0, 0, 0 };
	unsigned mb_length;
	int fd, c, i;
	char *console = NULL;
	struct unimapdesc ud;

	set_progname(argv[0]);

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

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

	while ((c = getopt(argc, argv, "sC:")) != EOF) {
		switch (c) {
			case 's':
				sortflag = 1;
				break;
			case 'C':
				console = optarg;
				break;
			default:
				usage();
		}
	}

	if (optind < argc)
		usage();

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

	if (getunimap(fd, &ud))
		return EXIT_FAILURE;

	if (sortflag) {
		printf("# sorted kernel unimap - count=%d\n", ud.entry_ct);
		/* sort and merge entries */
		qsort(ud.entries, ud.entry_ct, sizeof(ud.entries[0]),
		      ud_compar);
		for (i = 0; i < ud.entry_ct; i++) {
			int fp = ud.entries[i].fontpos;
			printf("0x%03x\tU+%04x", fp, ud.entries[i].unicode);
			while (i + 1 < ud.entry_ct &&
			       ud.entries[i + 1].fontpos == fp)
				printf(" U+%04x", ud.entries[++i].unicode);
			printf("\n");
		}
	} else {
		printf("# kernel unimap - count=%d\n", ud.entry_ct);
		for (i = 0; i < ud.entry_ct; i++) {
			mb_length                           = wctomb(mb, ud.entries[i].unicode);
			mb[(mb_length > 6) ? 0 : mb_length] = 0;
			if (mb_length == 1 && !isprint(mb[0])) {
				mb[2] = 0;
				mb[1] = mb[0] + 0100;
				mb[0] = '^';
			}
			printf("0x%03x\tU+%04x\t# %s \n",
			       ud.entries[i].fontpos,
			       ud.entries[i].unicode, mb);
		}
	}

	return EXIT_SUCCESS;
}