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