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