xref: /freebsd/usr.sbin/vidcontrol/vidcontrol.c (revision 148a8da8)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1994-1996 Søren Schmidt
5  * All rights reserved.
6  *
7  * Portions of this software are based in part on the work of
8  * Sascha Wildner <saw@online.de> contributed to The DragonFly Project
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer,
15  *    in this position and unchanged.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. The name of the author may not be used to endorse or promote products
20  *    derived from this software without specific prior written permission
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.10 2005/03/02 06:08:29 joerg Exp $
34  */
35 
36 #ifndef lint
37 static const char rcsid[] =
38   "$FreeBSD$";
39 #endif /* not lint */
40 
41 #include <ctype.h>
42 #include <err.h>
43 #include <limits.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <sys/fbio.h>
49 #include <sys/consio.h>
50 #include <sys/endian.h>
51 #include <sys/errno.h>
52 #include <sys/param.h>
53 #include <sys/types.h>
54 #include <sys/stat.h>
55 #include <sys/sysctl.h>
56 #include "path.h"
57 #include "decode.h"
58 
59 
60 #define	DATASIZE(x)	((x).w * (x).h * 256 / 8)
61 
62 /* Screen dump modes */
63 #define DUMP_FMT_RAW	1
64 #define DUMP_FMT_TXT	2
65 /* Screen dump options */
66 #define DUMP_FBF	0
67 #define DUMP_ALL	1
68 /* Screen dump file format revision */
69 #define DUMP_FMT_REV	1
70 
71 static const char *legal_colors[16] = {
72 	"black", "blue", "green", "cyan",
73 	"red", "magenta", "brown", "white",
74 	"grey", "lightblue", "lightgreen", "lightcyan",
75 	"lightred", "lightmagenta", "yellow", "lightwhite"
76 };
77 
78 static struct {
79 	int			active_vty;
80 	vid_info_t		console_info;
81 	unsigned char		screen_map[256];
82 	int			video_mode_number;
83 	struct video_info	video_mode_info;
84 } cur_info;
85 
86 struct vt4font_header {
87 	uint8_t		magic[8];
88 	uint8_t		width;
89 	uint8_t		height;
90 	uint16_t	pad;
91 	uint32_t	glyph_count;
92 	uint32_t	map_count[4];
93 } __packed;
94 
95 static int	hex = 0;
96 static int	vesa_cols;
97 static int	vesa_rows;
98 static int	font_height;
99 static int	vt4_mode = 0;
100 static int	video_mode_changed;
101 static struct	video_info new_mode_info;
102 
103 
104 /*
105  * Initialize revert data.
106  *
107  * NOTE: the following parameters are not yet saved/restored:
108  *
109  *   screen saver timeout
110  *   cursor type
111  *   mouse character and mouse show/hide state
112  *   vty switching on/off state
113  *   history buffer size
114  *   history contents
115  *   font maps
116  */
117 
118 static void
119 init(void)
120 {
121 	if (ioctl(0, VT_GETACTIVE, &cur_info.active_vty) == -1)
122 		err(1, "getting active vty");
123 
124 	cur_info.console_info.size = sizeof(cur_info.console_info);
125 	if (ioctl(0, CONS_GETINFO, &cur_info.console_info) == -1)
126 		err(1, "getting console information");
127 
128 	/* vt(4) use unicode, so no screen mapping required. */
129 	if (vt4_mode == 0 &&
130 	    ioctl(0, GIO_SCRNMAP, &cur_info.screen_map) == -1)
131 		err(1, "getting screen map");
132 
133 	if (ioctl(0, CONS_GET, &cur_info.video_mode_number) == -1)
134 		err(1, "getting video mode number");
135 
136 	cur_info.video_mode_info.vi_mode = cur_info.video_mode_number;
137 
138 	if (ioctl(0, CONS_MODEINFO, &cur_info.video_mode_info) == -1)
139 		err(1, "getting video mode parameters");
140 }
141 
142 
143 /*
144  * If something goes wrong along the way we call revert() to go back to the
145  * console state we came from (which is assumed to be working).
146  *
147  * NOTE: please also read the comments of init().
148  */
149 
150 static void
151 revert(void)
152 {
153 	int save_errno, size[3];
154 
155 	save_errno = errno;
156 
157 	ioctl(0, VT_ACTIVATE, cur_info.active_vty);
158 
159 	ioctl(0, KDSBORDER, cur_info.console_info.mv_ovscan);
160 	fprintf(stderr, "\033[=%dH", cur_info.console_info.mv_rev.fore);
161 	fprintf(stderr, "\033[=%dI", cur_info.console_info.mv_rev.back);
162 
163 	if (vt4_mode == 0)
164 		ioctl(0, PIO_SCRNMAP, &cur_info.screen_map);
165 
166 	if (video_mode_changed) {
167 		if (cur_info.video_mode_number >= M_VESA_BASE)
168 			ioctl(0,
169 			    _IO('V', cur_info.video_mode_number - M_VESA_BASE),
170 			    NULL);
171 		else
172 			ioctl(0, _IO('S', cur_info.video_mode_number), NULL);
173 		if (cur_info.video_mode_info.vi_flags & V_INFO_GRAPHICS) {
174 			size[0] = cur_info.console_info.mv_csz;
175 			size[1] = cur_info.console_info.mv_rsz;
176 			size[2] = cur_info.console_info.font_size;
177 			ioctl(0, KDRASTER, size);
178 		}
179 	}
180 
181 	/* Restore some colors last since mode setting forgets some. */
182 	fprintf(stderr, "\033[=%dF", cur_info.console_info.mv_norm.fore);
183 	fprintf(stderr, "\033[=%dG", cur_info.console_info.mv_norm.back);
184 
185 	errno = save_errno;
186 }
187 
188 
189 /*
190  * Print a short usage string describing all options, then exit.
191  */
192 
193 static void
194 usage(void)
195 {
196 	if (vt4_mode)
197 		fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
198 "usage: vidcontrol [-CHPpx] [-b color] [-c appearance] [-f [[size] file]]",
199 "                  [-g geometry] [-h size] [-i active | adapter | mode]",
200 "                  [-M char] [-m on | off]",
201 "                  [-r foreground background] [-S on | off] [-s number]",
202 "                  [-T xterm | cons25] [-t N | off] [mode]",
203 "                  [foreground [background]] [show]");
204 	else
205 		fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n",
206 "usage: vidcontrol [-CdHLPpx] [-b color] [-c appearance] [-E emulator]",
207 "                  [-f [[size] file]] [-g geometry] [-h size]",
208 "                  [-i active | adapter | mode] [-l screen_map] [-M char]",
209 "                  [-m on | off] [-r foreground background] [-S on | off]",
210 "                  [-s number] [-T xterm | cons25] [-t N | off] [mode]",
211 "                  [foreground [background]] [show]");
212 	exit(1);
213 }
214 
215 /* Detect presence of vt(4). */
216 static int
217 is_vt4(void)
218 {
219 	char vty_name[4] = "";
220 	size_t len = sizeof(vty_name);
221 
222 	if (sysctlbyname("kern.vty", vty_name, &len, NULL, 0) != 0)
223 		return (0);
224 	return (strcmp(vty_name, "vt") == 0);
225 }
226 
227 /*
228  * Retrieve the next argument from the command line (for options that require
229  * more than one argument).
230  */
231 
232 static char *
233 nextarg(int ac, char **av, int *indp, int oc, int strict)
234 {
235 	if (*indp < ac)
236 		return(av[(*indp)++]);
237 
238 	if (strict != 0) {
239 		revert();
240 		errx(1, "option requires two arguments -- %c", oc);
241 	}
242 
243 	return(NULL);
244 }
245 
246 
247 /*
248  * Guess which file to open. Try to open each combination of a specified set
249  * of file name components.
250  */
251 
252 static FILE *
253 openguess(const char *a[], const char *b[], const char *c[], const char *d[], char **name)
254 {
255 	FILE *f;
256 	int i, j, k, l;
257 
258 	for (i = 0; a[i] != NULL; i++) {
259 		for (j = 0; b[j] != NULL; j++) {
260 			for (k = 0; c[k] != NULL; k++) {
261 				for (l = 0; d[l] != NULL; l++) {
262 					asprintf(name, "%s%s%s%s",
263 						 a[i], b[j], c[k], d[l]);
264 
265 					f = fopen(*name, "r");
266 
267 					if (f != NULL)
268 						return (f);
269 
270 					free(*name);
271 				}
272 			}
273 		}
274 	}
275 	return (NULL);
276 }
277 
278 
279 /*
280  * Load a screenmap from a file and set it.
281  */
282 
283 static void
284 load_scrnmap(const char *filename)
285 {
286 	FILE *fd;
287 	int size;
288 	char *name;
289 	scrmap_t scrnmap;
290 	const char *a[] = {"", SCRNMAP_PATH, NULL};
291 	const char *b[] = {filename, NULL};
292 	const char *c[] = {"", ".scm", NULL};
293 	const char *d[] = {"", NULL};
294 
295 	fd = openguess(a, b, c, d, &name);
296 
297 	if (fd == NULL) {
298 		revert();
299 		errx(1, "screenmap file not found");
300 	}
301 
302 	size = sizeof(scrnmap);
303 
304 	if (decode(fd, (char *)&scrnmap, size) != size) {
305 		rewind(fd);
306 
307 		if (fread(&scrnmap, 1, size, fd) != (size_t)size) {
308 			fclose(fd);
309 			revert();
310 			errx(1, "bad screenmap file");
311 		}
312 	}
313 
314 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
315 		revert();
316 		err(1, "loading screenmap");
317 	}
318 
319 	fclose(fd);
320 }
321 
322 
323 /*
324  * Set the default screenmap.
325  */
326 
327 static void
328 load_default_scrnmap(void)
329 {
330 	scrmap_t scrnmap;
331 	int i;
332 
333 	for (i=0; i<256; i++)
334 		*((char*)&scrnmap + i) = i;
335 
336 	if (ioctl(0, PIO_SCRNMAP, &scrnmap) == -1) {
337 		revert();
338 		err(1, "loading default screenmap");
339 	}
340 }
341 
342 
343 /*
344  * Print the current screenmap to stdout.
345  */
346 
347 static void
348 print_scrnmap(void)
349 {
350 	unsigned char map[256];
351 	size_t i;
352 
353 	if (ioctl(0, GIO_SCRNMAP, &map) == -1) {
354 		revert();
355 		err(1, "getting screenmap");
356 	}
357 	for (i=0; i<sizeof(map); i++) {
358 		if (i != 0 && i % 16 == 0)
359 			fprintf(stdout, "\n");
360 
361 		if (hex != 0)
362 			fprintf(stdout, " %02x", map[i]);
363 		else
364 			fprintf(stdout, " %03d", map[i]);
365 	}
366 	fprintf(stdout, "\n");
367 
368 }
369 
370 
371 /*
372  * Determine a file's size.
373  */
374 
375 static int
376 fsize(FILE *file)
377 {
378 	struct stat sb;
379 
380 	if (fstat(fileno(file), &sb) == 0)
381 		return sb.st_size;
382 	else
383 		return -1;
384 }
385 
386 static vfnt_map_t *
387 load_vt4mappingtable(unsigned int nmappings, FILE *f)
388 {
389 	vfnt_map_t *t;
390 	unsigned int i;
391 
392 	if (nmappings == 0)
393 		return (NULL);
394 
395 	if ((t = calloc(nmappings, sizeof(*t))) == NULL) {
396 		warn("calloc");
397 		return (NULL);
398 	}
399 
400 	if (fread(t, sizeof *t * nmappings, 1, f) != 1) {
401 		warn("read mappings");
402 		free(t);
403 		return (NULL);
404 	}
405 
406 	for (i = 0; i < nmappings; i++) {
407 		t[i].src = be32toh(t[i].src);
408 		t[i].dst = be16toh(t[i].dst);
409 		t[i].len = be16toh(t[i].len);
410 	}
411 
412 	return (t);
413 }
414 
415 /*
416  * Set the default vt font.
417  */
418 
419 static void
420 load_default_vt4font(void)
421 {
422 	if (ioctl(0, PIO_VFONT_DEFAULT) == -1) {
423 		revert();
424 		err(1, "loading default vt font");
425 	}
426 }
427 
428 static void
429 load_vt4font(FILE *f)
430 {
431 	struct vt4font_header fh;
432 	static vfnt_t vfnt;
433 	size_t glyphsize;
434 	unsigned int i;
435 
436 	if (fread(&fh, sizeof fh, 1, f) != 1) {
437 		warn("read file_header");
438 		return;
439 	}
440 
441 	if (memcmp(fh.magic, "VFNT0002", 8) != 0) {
442 		warnx("bad magic in font file\n");
443 		return;
444 	}
445 
446 	for (i = 0; i < VFNT_MAPS; i++)
447 		vfnt.map_count[i] = be32toh(fh.map_count[i]);
448 	vfnt.glyph_count = be32toh(fh.glyph_count);
449 	vfnt.width = fh.width;
450 	vfnt.height = fh.height;
451 
452 	glyphsize = howmany(vfnt.width, 8) * vfnt.height * vfnt.glyph_count;
453 	if ((vfnt.glyphs = malloc(glyphsize)) == NULL) {
454 		warn("malloc");
455 		return;
456 	}
457 
458 	if (fread(vfnt.glyphs, glyphsize, 1, f) != 1) {
459 		warn("read glyphs");
460 		free(vfnt.glyphs);
461 		return;
462 	}
463 
464 	for (i = 0; i < VFNT_MAPS; i++)
465 		vfnt.map[i] = load_vt4mappingtable(vfnt.map_count[i], f);
466 
467 	if (ioctl(STDIN_FILENO, PIO_VFONT, &vfnt) == -1)
468 		warn("PIO_VFONT");
469 
470 	for (i = 0; i < VFNT_MAPS; i++)
471 		free(vfnt.map[i]);
472 	free(vfnt.glyphs);
473 }
474 
475 /*
476  * Load a font from file and set it.
477  */
478 
479 static void
480 load_font(const char *type, const char *filename)
481 {
482 	FILE	*fd;
483 	int	h, i, size, w;
484 	unsigned long io = 0;	/* silence stupid gcc(1) in the Wall mode */
485 	char	*name, *fontmap, size_sufx[6];
486 	const char	*a[] = {"", FONT_PATH, NULL};
487 	const char	*vt4a[] = {"", VT_FONT_PATH, NULL};
488 	const char	*b[] = {filename, NULL};
489 	const char	*c[] = {"", size_sufx, NULL};
490 	const char	*d[] = {"", ".fnt", NULL};
491 	vid_info_t info;
492 
493 	struct sizeinfo {
494 		int w;
495 		int h;
496 		unsigned long io;
497 	} sizes[] = {{8, 16, PIO_FONT8x16},
498 		     {8, 14, PIO_FONT8x14},
499 		     {8,  8,  PIO_FONT8x8},
500 		     {0,  0,            0}};
501 
502 	if (vt4_mode) {
503 		size_sufx[0] = '\0';
504 	} else {
505 		info.size = sizeof(info);
506 		if (ioctl(0, CONS_GETINFO, &info) == -1) {
507 			revert();
508 			err(1, "getting console information");
509 		}
510 
511 		snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
512 	}
513 	fd = openguess((vt4_mode == 0) ? a : vt4a, b, c, d, &name);
514 
515 	if (fd == NULL) {
516 		revert();
517 		errx(1, "%s: can't load font file", filename);
518 	}
519 
520 	if (vt4_mode) {
521 		load_vt4font(fd);
522 		fclose(fd);
523 		return;
524 	}
525 
526 	if (type != NULL) {
527 		size = 0;
528 		if (sscanf(type, "%dx%d", &w, &h) == 2) {
529 			for (i = 0; sizes[i].w != 0; i++) {
530 				if (sizes[i].w == w && sizes[i].h == h) {
531 					size = DATASIZE(sizes[i]);
532 					io = sizes[i].io;
533 					font_height = sizes[i].h;
534 				}
535 			}
536 		}
537 		if (size == 0) {
538 			fclose(fd);
539 			revert();
540 			errx(1, "%s: bad font size specification", type);
541 		}
542 	} else {
543 		/* Apply heuristics */
544 
545 		int j;
546 		int dsize[2];
547 
548 		size = DATASIZE(sizes[0]);
549 		fontmap = (char*) malloc(size);
550 		dsize[0] = decode(fd, fontmap, size);
551 		dsize[1] = fsize(fd);
552 		free(fontmap);
553 
554 		size = 0;
555 		for (j = 0; j < 2; j++) {
556 			for (i = 0; sizes[i].w != 0; i++) {
557 				if (DATASIZE(sizes[i]) == dsize[j]) {
558 					size = dsize[j];
559 					io = sizes[i].io;
560 					font_height = sizes[i].h;
561 					j = 2;	/* XXX */
562 					break;
563 				}
564 			}
565 		}
566 
567 		if (size == 0) {
568 			fclose(fd);
569 			revert();
570 			errx(1, "%s: can't guess font size", filename);
571 		}
572 
573 		rewind(fd);
574 	}
575 
576 	fontmap = (char*) malloc(size);
577 
578 	if (decode(fd, fontmap, size) != size) {
579 		rewind(fd);
580 		if (fsize(fd) != size ||
581 		    fread(fontmap, 1, size, fd) != (size_t)size) {
582 			fclose(fd);
583 			free(fontmap);
584 			revert();
585 			errx(1, "%s: bad font file", filename);
586 		}
587 	}
588 
589 	if (ioctl(0, io, fontmap) == -1) {
590 		revert();
591 		err(1, "loading font");
592 	}
593 
594 	fclose(fd);
595 	free(fontmap);
596 }
597 
598 
599 /*
600  * Set the timeout for the screensaver.
601  */
602 
603 static void
604 set_screensaver_timeout(char *arg)
605 {
606 	int nsec;
607 
608 	if (!strcmp(arg, "off")) {
609 		nsec = 0;
610 	} else {
611 		nsec = atoi(arg);
612 
613 		if ((*arg == '\0') || (nsec < 1)) {
614 			revert();
615 			errx(1, "argument must be a positive number");
616 		}
617 	}
618 
619 	if (ioctl(0, CONS_BLANKTIME, &nsec) == -1) {
620 		revert();
621 		err(1, "setting screensaver period");
622 	}
623 }
624 
625 static void
626 parse_cursor_params(char *param, struct cshape *shape)
627 {
628 	char *dupparam, *word;
629 	int type;
630 
631 	param = dupparam = strdup(param);
632 	type = shape->shape[0];
633 	while ((word = strsep(&param, ",")) != NULL) {
634 		if (strcmp(word, "normal") == 0)
635 			type = 0;
636 		else if (strcmp(word, "destructive") == 0)
637 			type = CONS_BLINK_CURSOR | CONS_CHAR_CURSOR;
638 		else if (strcmp(word, "blink") == 0)
639 			type |= CONS_BLINK_CURSOR;
640 		else if (strcmp(word, "noblink") == 0)
641 			type &= ~CONS_BLINK_CURSOR;
642 		else if (strcmp(word, "block") == 0)
643 			type &= ~CONS_CHAR_CURSOR;
644 		else if (strcmp(word, "noblock") == 0)
645 			type |= CONS_CHAR_CURSOR;
646 		else if (strcmp(word, "hidden") == 0)
647 			type |= CONS_HIDDEN_CURSOR;
648 		else if (strcmp(word, "nohidden") == 0)
649 			type &= ~CONS_HIDDEN_CURSOR;
650 		else if (strncmp(word, "base=", 5) == 0)
651 			shape->shape[1] = strtol(word + 5, NULL, 0);
652 		else if (strncmp(word, "height=", 7) == 0)
653 			shape->shape[2] = strtol(word + 7, NULL, 0);
654 		else if (strcmp(word, "charcolors") == 0)
655 			type |= CONS_CHARCURSOR_COLORS;
656 		else if (strcmp(word, "mousecolors") == 0)
657 			type |= CONS_MOUSECURSOR_COLORS;
658 		else if (strcmp(word, "default") == 0)
659 			type |= CONS_DEFAULT_CURSOR;
660 		else if (strcmp(word, "shapeonly") == 0)
661 			type |= CONS_SHAPEONLY_CURSOR;
662 		else if (strcmp(word, "local") == 0)
663 			type |= CONS_LOCAL_CURSOR;
664 		else if (strcmp(word, "reset") == 0)
665 			type |= CONS_RESET_CURSOR;
666 		else if (strcmp(word, "show") == 0)
667 			printf("flags %#x, base %d, height %d\n",
668 			    type, shape->shape[1], shape->shape[2]);
669 		else {
670 			revert();
671 			errx(1,
672 			    "invalid parameters for -c starting at '%s%s%s'",
673 			    word, param != NULL ? "," : "",
674 			    param != NULL ? param : "");
675 		}
676 	}
677 	free(dupparam);
678 	shape->shape[0] = type;
679 }
680 
681 
682 /*
683  * Set the cursor's shape/type.
684  */
685 
686 static void
687 set_cursor_type(char *param)
688 {
689 	struct cshape shape;
690 
691 	/* Dry run to determine color, default and local flags. */
692 	shape.shape[0] = 0;
693 	shape.shape[1] = -1;
694 	shape.shape[2] = -1;
695 	parse_cursor_params(param, &shape);
696 
697 	/* Get the relevant old setting. */
698 	if (ioctl(0, CONS_GETCURSORSHAPE, &shape) != 0) {
699 		revert();
700 		err(1, "ioctl(CONS_GETCURSORSHAPE)");
701 	}
702 
703 	parse_cursor_params(param, &shape);
704 	if (ioctl(0, CONS_SETCURSORSHAPE, &shape) != 0) {
705 		revert();
706 		err(1, "ioctl(CONS_SETCURSORSHAPE)");
707 	}
708 }
709 
710 
711 /*
712  * Set the video mode.
713  */
714 
715 static void
716 video_mode(int argc, char **argv, int *mode_index)
717 {
718 	static struct {
719 		const char *name;
720 		unsigned long mode;
721 		unsigned long mode_num;
722 	} modes[] = {
723 		{ "80x25",        SW_TEXT_80x25,   M_TEXT_80x25 },
724 		{ "80x30",        SW_TEXT_80x30,   M_TEXT_80x30 },
725 		{ "80x43",        SW_TEXT_80x43,   M_TEXT_80x43 },
726 		{ "80x50",        SW_TEXT_80x50,   M_TEXT_80x50 },
727 		{ "80x60",        SW_TEXT_80x60,   M_TEXT_80x60 },
728 		{ "132x25",       SW_TEXT_132x25,  M_TEXT_132x25 },
729 		{ "132x30",       SW_TEXT_132x30,  M_TEXT_132x30 },
730 		{ "132x43",       SW_TEXT_132x43,  M_TEXT_132x43 },
731 		{ "132x50",       SW_TEXT_132x50,  M_TEXT_132x50 },
732 		{ "132x60",       SW_TEXT_132x60,  M_TEXT_132x60 },
733 		{ "VGA_40x25",    SW_VGA_C40x25,   M_VGA_C40x25 },
734 		{ "VGA_80x25",    SW_VGA_C80x25,   M_VGA_C80x25 },
735 		{ "VGA_80x30",    SW_VGA_C80x30,   M_VGA_C80x30 },
736 		{ "VGA_80x50",    SW_VGA_C80x50,   M_VGA_C80x50 },
737 		{ "VGA_80x60",    SW_VGA_C80x60,   M_VGA_C80x60 },
738 #ifdef SW_VGA_C90x25
739 		{ "VGA_90x25",    SW_VGA_C90x25,   M_VGA_C90x25 },
740 		{ "VGA_90x30",    SW_VGA_C90x30,   M_VGA_C90x30 },
741 		{ "VGA_90x43",    SW_VGA_C90x43,   M_VGA_C90x43 },
742 		{ "VGA_90x50",    SW_VGA_C90x50,   M_VGA_C90x50 },
743 		{ "VGA_90x60",    SW_VGA_C90x60,   M_VGA_C90x60 },
744 #endif
745 		{ "VGA_320x200",	SW_VGA_CG320,	M_CG320 },
746 		{ "EGA_80x25",		SW_ENH_C80x25,	M_ENH_C80x25 },
747 		{ "EGA_80x43",		SW_ENH_C80x43,	M_ENH_C80x43 },
748 		{ "VESA_132x25",	SW_VESA_C132x25,M_VESA_C132x25 },
749 		{ "VESA_132x43",	SW_VESA_C132x43,M_VESA_C132x43 },
750 		{ "VESA_132x50",	SW_VESA_C132x50,M_VESA_C132x50 },
751 		{ "VESA_132x60",	SW_VESA_C132x60,M_VESA_C132x60 },
752 		{ "VESA_800x600",	SW_VESA_800x600,M_VESA_800x600 },
753 		{ NULL, 0, 0 },
754 	};
755 
756 	int new_mode_num = 0;
757 	unsigned long mode = 0;
758 	int cur_mode;
759 	int save_errno;
760 	int size[3];
761 	int i;
762 
763 	if (ioctl(0, CONS_GET, &cur_mode) < 0)
764 		err(1, "cannot get the current video mode");
765 
766 	/*
767 	 * Parse the video mode argument...
768 	 */
769 
770 	if (*mode_index < argc) {
771 		if (!strncmp(argv[*mode_index], "MODE_", 5)) {
772 			if (!isdigit(argv[*mode_index][5]))
773 				errx(1, "invalid video mode number");
774 
775 			new_mode_num = atoi(&argv[*mode_index][5]);
776 		} else {
777 			for (i = 0; modes[i].name != NULL; ++i) {
778 				if (!strcmp(argv[*mode_index], modes[i].name)) {
779 					mode = modes[i].mode;
780 					new_mode_num = modes[i].mode_num;
781 					break;
782 				}
783 			}
784 
785 			if (modes[i].name == NULL)
786 				return;
787 			if (ioctl(0, mode, NULL) < 0) {
788 				revert();
789 				err(1, "cannot set videomode");
790 			}
791 			video_mode_changed = 1;
792 		}
793 
794 		/*
795 		 * Collect enough information about the new video mode...
796 		 */
797 
798 		new_mode_info.vi_mode = new_mode_num;
799 
800 		if (ioctl(0, CONS_MODEINFO, &new_mode_info) == -1) {
801 			revert();
802 			err(1, "obtaining new video mode parameters");
803 		}
804 
805 		if (mode == 0) {
806 			if (new_mode_num >= M_VESA_BASE)
807 				mode = _IO('V', new_mode_num - M_VESA_BASE);
808 			else
809 				mode = _IO('S', new_mode_num);
810 		}
811 
812 		/*
813 		 * Try setting the new mode.
814 		 */
815 
816 		if (ioctl(0, mode, NULL) == -1) {
817 			revert();
818 			err(1, "setting video mode");
819 		}
820 		video_mode_changed = 1;
821 
822 		/*
823 		 * For raster modes it's not enough to just set the mode.
824 		 * We also need to explicitly set the raster mode.
825 		 */
826 
827 		if (new_mode_info.vi_flags & V_INFO_GRAPHICS) {
828 			/* font size */
829 
830 			if (font_height == 0)
831 				font_height = cur_info.console_info.font_size;
832 
833 			size[2] = font_height;
834 
835 			/* adjust columns */
836 
837 			if ((vesa_cols * 8 > new_mode_info.vi_width) ||
838 			    (vesa_cols <= 0)) {
839 				size[0] = new_mode_info.vi_width / 8;
840 			} else {
841 				size[0] = vesa_cols;
842 			}
843 
844 			/* adjust rows */
845 
846 			if ((vesa_rows * font_height > new_mode_info.vi_height) ||
847 			    (vesa_rows <= 0)) {
848 				size[1] = new_mode_info.vi_height /
849 					  font_height;
850 			} else {
851 				size[1] = vesa_rows;
852 			}
853 
854 			/* set raster mode */
855 
856 			if (ioctl(0, KDRASTER, size)) {
857 				save_errno = errno;
858 				if (cur_mode >= M_VESA_BASE)
859 					ioctl(0,
860 					    _IO('V', cur_mode - M_VESA_BASE),
861 					    NULL);
862 				else
863 					ioctl(0, _IO('S', cur_mode), NULL);
864 				revert();
865 				errno = save_errno;
866 				err(1, "cannot activate raster display");
867 			}
868 		}
869 
870 		/* Recover from mode setting forgetting colors. */
871 		fprintf(stderr, "\033[=%dF",
872 		    cur_info.console_info.mv_norm.fore);
873 		fprintf(stderr, "\033[=%dG",
874 		    cur_info.console_info.mv_norm.back);
875 
876 		(*mode_index)++;
877 	}
878 }
879 
880 
881 /*
882  * Return the number for a specified color name.
883  */
884 
885 static int
886 get_color_number(char *color)
887 {
888 	int i;
889 
890 	for (i=0; i<16; i++) {
891 		if (!strcmp(color, legal_colors[i]))
892 			return i;
893 	}
894 	return -1;
895 }
896 
897 
898 /*
899  * Set normal text and background colors.
900  */
901 
902 static void
903 set_normal_colors(int argc, char **argv, int *_index)
904 {
905 	int color;
906 
907 	if (*_index < argc && (color = get_color_number(argv[*_index])) != -1) {
908 		(*_index)++;
909 		fprintf(stderr, "\033[=%dF", color);
910 		if (*_index < argc
911 		    && (color = get_color_number(argv[*_index])) != -1) {
912 			(*_index)++;
913 			fprintf(stderr, "\033[=%dG", color);
914 		}
915 	}
916 }
917 
918 
919 /*
920  * Set reverse text and background colors.
921  */
922 
923 static void
924 set_reverse_colors(int argc, char **argv, int *_index)
925 {
926 	int color;
927 
928 	if ((color = get_color_number(argv[*(_index)-1])) != -1) {
929 		fprintf(stderr, "\033[=%dH", color);
930 		if (*_index < argc
931 		    && (color = get_color_number(argv[*_index])) != -1) {
932 			(*_index)++;
933 			fprintf(stderr, "\033[=%dI", color);
934 		}
935 	}
936 }
937 
938 
939 /*
940  * Switch to virtual terminal #arg.
941  */
942 
943 static void
944 set_console(char *arg)
945 {
946 	int n;
947 
948 	if(!arg || strspn(arg,"0123456789") != strlen(arg)) {
949 		revert();
950 		errx(1, "bad console number");
951 	}
952 
953 	n = atoi(arg);
954 
955 	if (n < 1 || n > 16) {
956 		revert();
957 		errx(1, "console number out of range");
958 	} else if (ioctl(0, VT_ACTIVATE, n) == -1) {
959 		revert();
960 		err(1, "switching vty");
961 	}
962 }
963 
964 
965 /*
966  * Sets the border color.
967  */
968 
969 static void
970 set_border_color(char *arg)
971 {
972 	int color;
973 
974 	color = get_color_number(arg);
975 	if (color == -1) {
976 		revert();
977 		errx(1, "invalid color '%s'", arg);
978 	}
979 	if (ioctl(0, KDSBORDER, color) != 0) {
980 		revert();
981 		err(1, "ioctl(KD_SBORDER)");
982 	}
983 }
984 
985 static void
986 set_mouse_char(char *arg)
987 {
988 	struct mouse_info mouse;
989 	long l;
990 
991 	l = strtol(arg, NULL, 0);
992 
993 	if ((l < 0) || (l > UCHAR_MAX - 3)) {
994 		revert();
995 		warnx("argument to -M must be 0 through %d", UCHAR_MAX - 3);
996 		return;
997 	}
998 
999 	mouse.operation = MOUSE_MOUSECHAR;
1000 	mouse.u.mouse_char = (int)l;
1001 
1002 	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
1003 		revert();
1004 		err(1, "setting mouse character");
1005 	}
1006 }
1007 
1008 
1009 /*
1010  * Show/hide the mouse.
1011  */
1012 
1013 static void
1014 set_mouse(char *arg)
1015 {
1016 	struct mouse_info mouse;
1017 
1018 	if (!strcmp(arg, "on")) {
1019 		mouse.operation = MOUSE_SHOW;
1020 	} else if (!strcmp(arg, "off")) {
1021 		mouse.operation = MOUSE_HIDE;
1022 	} else {
1023 		revert();
1024 		errx(1, "argument to -m must be either on or off");
1025 	}
1026 
1027 	if (ioctl(0, CONS_MOUSECTL, &mouse) == -1) {
1028 		revert();
1029 		err(1, "%sing the mouse",
1030 		     mouse.operation == MOUSE_SHOW ? "show" : "hid");
1031 	}
1032 }
1033 
1034 
1035 static void
1036 set_lockswitch(char *arg)
1037 {
1038 	int data;
1039 
1040 	if (!strcmp(arg, "off")) {
1041 		data = 0x01;
1042 	} else if (!strcmp(arg, "on")) {
1043 		data = 0x02;
1044 	} else {
1045 		revert();
1046 		errx(1, "argument to -S must be either on or off");
1047 	}
1048 
1049 	if (ioctl(0, VT_LOCKSWITCH, &data) == -1) {
1050 		revert();
1051 		err(1, "turning %s vty switching",
1052 		     data == 0x01 ? "off" : "on");
1053 	}
1054 }
1055 
1056 
1057 /*
1058  * Return the adapter name for a specified type.
1059  */
1060 
1061 static const char
1062 *adapter_name(int type)
1063 {
1064     static struct {
1065 	int type;
1066 	const char *name;
1067     } names[] = {
1068 	{ KD_MONO,	"MDA" },
1069 	{ KD_HERCULES,	"Hercules" },
1070 	{ KD_CGA,	"CGA" },
1071 	{ KD_EGA,	"EGA" },
1072 	{ KD_VGA,	"VGA" },
1073 	{ KD_TGA,	"TGA" },
1074 	{ -1,		"Unknown" },
1075     };
1076 
1077     int i;
1078 
1079     for (i = 0; names[i].type != -1; ++i)
1080 	if (names[i].type == type)
1081 	    break;
1082     return names[i].name;
1083 }
1084 
1085 
1086 /*
1087  * Show active VTY, ie current console number.
1088  */
1089 
1090 static void
1091 show_active_info(void)
1092 {
1093 
1094 	printf("%d\n", cur_info.active_vty);
1095 }
1096 
1097 
1098 /*
1099  * Show graphics adapter information.
1100  */
1101 
1102 static void
1103 show_adapter_info(void)
1104 {
1105 	struct video_adapter_info ad;
1106 
1107 	ad.va_index = 0;
1108 
1109 	if (ioctl(0, CONS_ADPINFO, &ad) == -1) {
1110 		revert();
1111 		err(1, "obtaining adapter information");
1112 	}
1113 
1114 	printf("fb%d:\n", ad.va_index);
1115 	printf("    %.*s%d, type:%s%s (%d), flags:0x%x\n",
1116 	       (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
1117 	       (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
1118 	       adapter_name(ad.va_type), ad.va_type, ad.va_flags);
1119 	printf("    initial mode:%d, current mode:%d, BIOS mode:%d\n",
1120 	       ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
1121 	printf("    frame buffer window:0x%zx, buffer size:0x%zx\n",
1122 	       ad.va_window, ad.va_buffer_size);
1123 	printf("    window size:0x%zx, origin:0x%x\n",
1124 	       ad.va_window_size, ad.va_window_orig);
1125 	printf("    display start address (%d, %d), scan line width:%d\n",
1126 	       ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
1127 	printf("    reserved:0x%zx\n", ad.va_unused0);
1128 }
1129 
1130 
1131 /*
1132  * Show video mode information.
1133  */
1134 
1135 static void
1136 show_mode_info(void)
1137 {
1138 	char buf[80];
1139 	struct video_info info;
1140 	int c;
1141 	int mm;
1142 	int mode;
1143 
1144 	printf("    mode#     flags   type    size       "
1145 	       "font      window      linear buffer\n");
1146 	printf("---------------------------------------"
1147 	       "---------------------------------------\n");
1148 
1149 	memset(&info, 0, sizeof(info));
1150 	for (mode = 0; mode <= M_VESA_MODE_MAX; ++mode) {
1151 		info.vi_mode = mode;
1152 		if (ioctl(0, CONS_MODEINFO, &info))
1153 			continue;
1154 		if (info.vi_mode != mode)
1155 			continue;
1156 		if (info.vi_width == 0 && info.vi_height == 0 &&
1157 		    info.vi_cwidth == 0 && info.vi_cheight == 0)
1158 			continue;
1159 
1160 		printf("%3d (0x%03x)", mode, mode);
1161     		printf(" 0x%08x", info.vi_flags);
1162 		if (info.vi_flags & V_INFO_GRAPHICS) {
1163 			c = 'G';
1164 
1165 			if (info.vi_mem_model == V_INFO_MM_PLANAR)
1166 				snprintf(buf, sizeof(buf), "%dx%dx%d %d",
1167 				    info.vi_width, info.vi_height,
1168 				    info.vi_depth, info.vi_planes);
1169 			else {
1170 				switch (info.vi_mem_model) {
1171 				case V_INFO_MM_PACKED:
1172 					mm = 'P';
1173 					break;
1174 				case V_INFO_MM_DIRECT:
1175 					mm = 'D';
1176 					break;
1177 				case V_INFO_MM_CGA:
1178 					mm = 'C';
1179 					break;
1180 				case V_INFO_MM_HGC:
1181 					mm = 'H';
1182 					break;
1183 				case V_INFO_MM_VGAX:
1184 					mm = 'V';
1185 					break;
1186 				default:
1187 					mm = ' ';
1188 					break;
1189 				}
1190 				snprintf(buf, sizeof(buf), "%dx%dx%d %c",
1191 				    info.vi_width, info.vi_height,
1192 				    info.vi_depth, mm);
1193 			}
1194 		} else {
1195 			c = 'T';
1196 
1197 			snprintf(buf, sizeof(buf), "%dx%d",
1198 				 info.vi_width, info.vi_height);
1199 		}
1200 
1201 		printf(" %c %-15s", c, buf);
1202 		snprintf(buf, sizeof(buf), "%dx%d",
1203 			 info.vi_cwidth, info.vi_cheight);
1204 		printf(" %-5s", buf);
1205     		printf(" 0x%05zx %2dk %2dk",
1206 		       info.vi_window, (int)info.vi_window_size/1024,
1207 		       (int)info.vi_window_gran/1024);
1208     		printf(" 0x%08zx %dk\n",
1209 		       info.vi_buffer, (int)info.vi_buffer_size/1024);
1210 	}
1211 }
1212 
1213 
1214 static void
1215 show_info(char *arg)
1216 {
1217 
1218 	if (!strcmp(arg, "active")) {
1219 		show_active_info();
1220 	} else if (!strcmp(arg, "adapter")) {
1221 		show_adapter_info();
1222 	} else if (!strcmp(arg, "mode")) {
1223 		show_mode_info();
1224 	} else {
1225 		revert();
1226 		errx(1, "argument to -i must be active, adapter, or mode");
1227 	}
1228 }
1229 
1230 
1231 static void
1232 test_frame(void)
1233 {
1234 	vid_info_t info;
1235 	const char *bg, *sep;
1236 	int i, fore;
1237 
1238 	info.size = sizeof(info);
1239 	if (ioctl(0, CONS_GETINFO, &info) == -1)
1240 		err(1, "getting console information");
1241 
1242 	fore = 15;
1243 	if (info.mv_csz < 80) {
1244 		bg = "BG";
1245 		sep = " ";
1246 	} else {
1247 		bg = "BACKGROUND";
1248 		sep = "    ";
1249 	}
1250 
1251 	fprintf(stdout, "\033[=0G\n\n");
1252 	for (i=0; i<8; i++) {
1253 		fprintf(stdout,
1254 		    "\033[=%dF\033[=0G%2d \033[=%dF%-7s%s"
1255 		    "\033[=%dF\033[=0G%2d \033[=%dF%-12s%s"
1256 		    "\033[=%dF%2d \033[=%dG%s\033[=0G%s"
1257 		    "\033[=%dF%2d \033[=%dG%s\033[=0G\n",
1258 		    fore, i, i, legal_colors[i], sep,
1259 		    fore, i + 8, i + 8, legal_colors[i + 8], sep,
1260 		    fore, i, i, bg, sep,
1261 		    fore, i + 8, i + 8, bg);
1262 	}
1263 	fprintf(stdout, "\033[=%dF\033[=%dG\033[=%dH\033[=%dI\n",
1264 		info.mv_norm.fore, info.mv_norm.back,
1265 		info.mv_rev.fore, info.mv_rev.back);
1266 }
1267 
1268 
1269 /*
1270  * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
1271  * ioctl, and writes the results to stdout either in the special
1272  * binary format (see manual page for details), or in the plain
1273  * text format.
1274  */
1275 
1276 static void
1277 dump_screen(int mode, int opt)
1278 {
1279 	scrshot_t shot;
1280 	vid_info_t info;
1281 
1282 	info.size = sizeof(info);
1283 	if (ioctl(0, CONS_GETINFO, &info) == -1) {
1284 		revert();
1285 		err(1, "getting console information");
1286 	}
1287 
1288 	shot.x = shot.y = 0;
1289 	shot.xsize = info.mv_csz;
1290 	shot.ysize = info.mv_rsz;
1291 	if (opt == DUMP_ALL)
1292 		shot.ysize += info.mv_hsz;
1293 
1294 	shot.buf = alloca(shot.xsize * shot.ysize * sizeof(u_int16_t));
1295 	if (shot.buf == NULL) {
1296 		revert();
1297 		errx(1, "failed to allocate memory for dump");
1298 	}
1299 
1300 	if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
1301 		revert();
1302 		err(1, "dumping screen");
1303 	}
1304 
1305 	if (mode == DUMP_FMT_RAW) {
1306 		printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
1307 		       shot.xsize, shot.ysize);
1308 
1309 		fflush(stdout);
1310 
1311 		write(STDOUT_FILENO, shot.buf,
1312 		      shot.xsize * shot.ysize * sizeof(u_int16_t));
1313 	} else {
1314 		char *line;
1315 		int x, y;
1316 		u_int16_t ch;
1317 
1318 		line = alloca(shot.xsize + 1);
1319 
1320 		if (line == NULL) {
1321 			revert();
1322 			errx(1, "failed to allocate memory for line buffer");
1323 		}
1324 
1325 		for (y = 0; y < shot.ysize; y++) {
1326 			for (x = 0; x < shot.xsize; x++) {
1327 				ch = shot.buf[x + (y * shot.xsize)];
1328 				ch &= 0xff;
1329 
1330 				if (isprint(ch) == 0)
1331 					ch = ' ';
1332 
1333 				line[x] = (char)ch;
1334 			}
1335 
1336 			/* Trim trailing spaces */
1337 
1338 			do {
1339 				line[x--] = '\0';
1340 			} while (line[x] == ' ' && x != 0);
1341 
1342 			puts(line);
1343 		}
1344 
1345 		fflush(stdout);
1346 	}
1347 }
1348 
1349 
1350 /*
1351  * Set the console history buffer size.
1352  */
1353 
1354 static void
1355 set_history(char *opt)
1356 {
1357 	int size;
1358 
1359 	size = atoi(opt);
1360 
1361 	if ((*opt == '\0') || size < 0) {
1362 		revert();
1363 		errx(1, "argument must not be less than zero");
1364 	}
1365 
1366 	if (ioctl(0, CONS_HISTORY, &size) == -1) {
1367 		revert();
1368 		err(1, "setting history buffer size");
1369 	}
1370 }
1371 
1372 
1373 /*
1374  * Clear the console history buffer.
1375  */
1376 
1377 static void
1378 clear_history(void)
1379 {
1380 	if (ioctl(0, CONS_CLRHIST) == -1) {
1381 		revert();
1382 		err(1, "clearing history buffer");
1383 	}
1384 }
1385 
1386 static int
1387 get_terminal_emulator(int i, struct term_info *tip)
1388 {
1389 	tip->ti_index = i;
1390 	if (ioctl(0, CONS_GETTERM, tip) == 0)
1391 		return (1);
1392 	strlcpy((char *)tip->ti_name, "unknown", sizeof(tip->ti_name));
1393 	strlcpy((char *)tip->ti_desc, "unknown", sizeof(tip->ti_desc));
1394 	return (0);
1395 }
1396 
1397 static void
1398 get_terminal_emulators(void)
1399 {
1400 	struct term_info ti;
1401 	int i;
1402 
1403 	for (i = 0; i < 10; i++) {
1404 		if (get_terminal_emulator(i, &ti) == 0)
1405 			break;
1406 		printf("%d: %s (%s)%s\n", i, ti.ti_name, ti.ti_desc,
1407 		    i == 0 ? " (active)" : "");
1408 	}
1409 }
1410 
1411 static void
1412 set_terminal_emulator(const char *name)
1413 {
1414 	struct term_info old_ti, ti;
1415 
1416 	get_terminal_emulator(0, &old_ti);
1417 	strlcpy((char *)ti.ti_name, name, sizeof(ti.ti_name));
1418 	if (ioctl(0, CONS_SETTERM, &ti) != 0)
1419 		warn("SETTERM '%s'", name);
1420 	get_terminal_emulator(0, &ti);
1421 	printf("%s (%s) -> %s (%s)\n", old_ti.ti_name, old_ti.ti_desc,
1422 	    ti.ti_name, ti.ti_desc);
1423 }
1424 
1425 static void
1426 set_terminal_mode(char *arg)
1427 {
1428 
1429 	if (strcmp(arg, "xterm") == 0)
1430 		fprintf(stderr, "\033[=T");
1431 	else if (strcmp(arg, "cons25") == 0)
1432 		fprintf(stderr, "\033[=1T");
1433 }
1434 
1435 
1436 int
1437 main(int argc, char **argv)
1438 {
1439 	char    *font, *type, *termmode;
1440 	const char *opts;
1441 	int	dumpmod, dumpopt, opt;
1442 
1443 	vt4_mode = is_vt4();
1444 
1445 	init();
1446 
1447 	dumpmod = 0;
1448 	dumpopt = DUMP_FBF;
1449 	termmode = NULL;
1450 	if (vt4_mode)
1451 		opts = "b:Cc:fg:h:Hi:M:m:pPr:S:s:T:t:x";
1452 	else
1453 		opts = "b:Cc:deE:fg:h:Hi:l:LM:m:pPr:S:s:T:t:x";
1454 
1455 	while ((opt = getopt(argc, argv, opts)) != -1)
1456 		switch(opt) {
1457 		case 'b':
1458 			set_border_color(optarg);
1459 			break;
1460 		case 'C':
1461 			clear_history();
1462 			break;
1463 		case 'c':
1464 			set_cursor_type(optarg);
1465 			break;
1466 		case 'd':
1467 			if (vt4_mode)
1468 				break;
1469 			print_scrnmap();
1470 			break;
1471 		case 'E':
1472 			if (vt4_mode)
1473 				break;
1474 			set_terminal_emulator(optarg);
1475 			break;
1476 		case 'e':
1477 			if (vt4_mode)
1478 				break;
1479 			get_terminal_emulators();
1480 			break;
1481 		case 'f':
1482 			optarg = nextarg(argc, argv, &optind, 'f', 0);
1483 			if (optarg != NULL) {
1484 				font = nextarg(argc, argv, &optind, 'f', 0);
1485 
1486 				if (font == NULL) {
1487 					type = NULL;
1488 					font = optarg;
1489 				} else
1490 					type = optarg;
1491 
1492 				load_font(type, font);
1493 			} else {
1494 				if (!vt4_mode)
1495 					usage(); /* Switch syscons to ROM? */
1496 
1497 				load_default_vt4font();
1498 			}
1499 			break;
1500 		case 'g':
1501 			if (sscanf(optarg, "%dx%d",
1502 			    &vesa_cols, &vesa_rows) != 2) {
1503 				revert();
1504 				warnx("incorrect geometry: %s", optarg);
1505 				usage();
1506 			}
1507                 	break;
1508 		case 'h':
1509 			set_history(optarg);
1510 			break;
1511 		case 'H':
1512 			dumpopt = DUMP_ALL;
1513 			break;
1514 		case 'i':
1515 			show_info(optarg);
1516 			break;
1517 		case 'l':
1518 			if (vt4_mode)
1519 				break;
1520 			load_scrnmap(optarg);
1521 			break;
1522 		case 'L':
1523 			if (vt4_mode)
1524 				break;
1525 			load_default_scrnmap();
1526 			break;
1527 		case 'M':
1528 			set_mouse_char(optarg);
1529 			break;
1530 		case 'm':
1531 			set_mouse(optarg);
1532 			break;
1533 		case 'p':
1534 			dumpmod = DUMP_FMT_RAW;
1535 			break;
1536 		case 'P':
1537 			dumpmod = DUMP_FMT_TXT;
1538 			break;
1539 		case 'r':
1540 			set_reverse_colors(argc, argv, &optind);
1541 			break;
1542 		case 'S':
1543 			set_lockswitch(optarg);
1544 			break;
1545 		case 's':
1546 			set_console(optarg);
1547 			break;
1548 		case 'T':
1549 			if (strcmp(optarg, "xterm") != 0 &&
1550 			    strcmp(optarg, "cons25") != 0)
1551 				usage();
1552 			termmode = optarg;
1553 			break;
1554 		case 't':
1555 			set_screensaver_timeout(optarg);
1556 			break;
1557 		case 'x':
1558 			hex = 1;
1559 			break;
1560 		default:
1561 			usage();
1562 		}
1563 
1564 	if (dumpmod != 0)
1565 		dump_screen(dumpmod, dumpopt);
1566 	video_mode(argc, argv, &optind);
1567 	set_normal_colors(argc, argv, &optind);
1568 
1569 	if (optind < argc && !strcmp(argv[optind], "show")) {
1570 		test_frame();
1571 		optind++;
1572 	}
1573 
1574 	if (termmode != NULL)
1575 		set_terminal_mode(termmode);
1576 
1577 	if ((optind != argc) || (argc == 1))
1578 		usage();
1579 	return (0);
1580 }
1581 
1582