File: showconsolefont.c

package info (click to toggle)
kbd 2.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 14,192 kB
  • ctags: 3,648
  • sloc: sh: 12,831; ansic: 12,269; pascal: 472; lex: 464; makefile: 384; yacc: 355; sed: 16
file content (204 lines) | stat: -rw-r--r-- 4,482 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* showfont.c - aeb, 940207 - updated 2001-02-06 */
/* renamed to showconsolefont.c to avoid clash with the X showfont */

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

/*
 * Showing the font is nontrivial mostly because testing whether
 * we are in utf8 mode cannot be done in an easy and clean way.
 * So, we set up things here in such a way that it does not matter
 * whether we are in utf8 mode.
 */

unsigned short obuf[E_TABSZ], nbuf[E_TABSZ];
struct unimapdesc ounimap, nunimap;
int fd = 0;
int have_obuf = 0;
int have_ounimap = 0;

static void __attribute__ ((noreturn))
leave(int n) {
	if (have_obuf && loaduniscrnmap(fd,obuf)) {
		kbd_warning(0, _("failed to restore original translation table\n"));
		n = EXIT_FAILURE;
	}
	if (have_ounimap && loadunimap(fd,NULL,&ounimap)) {
		kbd_warning(0, _("failed to restore original unimap\n"));
		n = EXIT_FAILURE;
	}
	exit(n);
}

static void
settrivialscreenmap(void) {
	int i;

	if (getuniscrnmap(fd,obuf))
		exit(1);
	have_obuf = 1;

	for(i=0; i<E_TABSZ; i++)
		nbuf[i] = i;

	if (loaduniscrnmap(fd,nbuf)) {
		kbd_error(EXIT_FAILURE, 0, _("cannot change translation table\n"));
	}
}

static void
getoldunicodemap(void) {
	struct unimapdesc descr;

	if (getunimap(fd, &descr))
		leave(EXIT_FAILURE);
	ounimap = descr;
	have_ounimap = 1;
}

#define BASE	041		/* ' '+1 */

static void
setnewunicodemap(int *list, int cnt) {
	int i;

	if (!nunimap.entry_ct) {
		nunimap.entry_ct = 512;
		nunimap.entries = (struct unipair *) xmalloc(nunimap.entry_ct * sizeof(struct unipair));
	}
	for (i=0; i<512; i++) {
		nunimap.entries[i].fontpos = i;
		nunimap.entries[i].unicode = 0;
	}
	for (i=0; i<cnt; i++)
		nunimap.entries[list[i]].unicode = BASE+i;

	if (loadunimap(fd, NULL, &nunimap))
		leave(EXIT_FAILURE);
}

static void __attribute__ ((noreturn))
usage(void) {
	fprintf(stderr,
		_("usage: showconsolefont -V|--version\n"
		  "       showconsolefont [-C tty] [-v] [-i]\n"
		  "(probably after loading a font with `setfont font')\n"
		  "\n"
		  "Valid options are:\n"
		  " -C tty   Device to read the font from. Default: current tty.\n"
		  " -v       Be more verbose.\n"
		  " -i       Don't print out the font table, just show\n"
		  "          ROWSxCOLSxCOUNT and exit.\n"));
	exit(EXIT_FAILURE);
}

int
main (int argc, char **argv) {
	int c, n, cols, rows, nr, i, j, k;
	int mode;
	char *space, *sep, *console = NULL;
	int list[64], lth, info = 0, verbose = 0;

	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, "ivC:")) != EOF) {
		switch (c) {
		case 'i':
			info = 1;
			break;
		case 'v':
			verbose = 1;
			break;
		case 'C':
			console = optarg;
			break;
		default:
			usage();
		}
	}

	if (optind != argc)
		usage();

	fd = getfd(console);

	if (ioctl(fd, KDGKBMODE, &mode)) {
		kbd_warning(errno, "ioctl KDGKBMODE");
		leave(EXIT_FAILURE);
	}
	if (mode == K_UNICODE)
		space = "\xef\x80\xa0";	/* U+F020 (direct-to-font space) */
	else
		space = " ";

        if (info) {
	    nr = rows = cols = 0;
	    n = getfont(fd, NULL, &nr, &rows, &cols);
	    if (n != 0)
	      leave(EXIT_FAILURE);

	    if (verbose) {
	        printf(_("Character count: %d\n"), nr);
		printf(_("Font width     : %d\n"), rows);
		printf(_("Font height    : %d\n"), cols);
	    }
	    else
		printf("%dx%dx%d\n", rows, cols, nr);
	    leave(EXIT_SUCCESS);
	  }

	settrivialscreenmap();
	getoldunicodemap();

	n = getfontsize(fd);
	if (verbose)
		printf(_("Showing %d-char font\n\n"), n);
	cols = ((n > 256) ? 32 : 16);
	nr = 64/cols;
	rows = (n+cols-1)/cols;
	sep = ((cols == 16) ? "%1$s%1$s" : "%1$s");

	for (i=0; i<rows; i++) {
		if (i % nr == 0) {
			lth = 0;
			for (k=i; k<i+nr; k++)
				for (j=0; j < cols; j++)
					list[lth++] = k+j*rows;
			setnewunicodemap(list, lth);
		}
		printf("%1$s%1$s%1$s%1$s", space);
		for(j=0; j < cols && i+j*rows < n; j++) {
			putchar(BASE + (i%nr)*cols+j);
			printf(sep, space);
			if (j%8 == 7)
				printf(sep, space);
		}
		putchar('\n');
		if (i%8 == 7)
			putchar('\n');
		fflush(stdout);
	}

	leave(EXIT_SUCCESS);
	return EXIT_SUCCESS;
}