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