Merge from vendor branch LIBSTDC++:
[dragonfly.git] / usr.sbin / vidcontrol / vidcontrol.c
1 /*-
2  * Copyright (c) 1994-1996 Søren Schmidt
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    in this position and unchanged.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software withough specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.32.2.7 2002/09/15 22:31:50 dd Exp $
29  * $DragonFly: src/usr.sbin/vidcontrol/vidcontrol.c,v 1.2 2003/06/17 04:30:03 dillon Exp $
30  */
31
32 #include <ctype.h>
33 #include <err.h>
34 #include <limits.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
39 #include <machine/console.h>
40 #include <sys/consio.h>
41 #include <sys/errno.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include "path.h"
45 #include "decode.h"
46
47 #define _VESA_800x600_DFL_COLS 80
48 #define _VESA_800x600_DFL_ROWS 25
49 #define _VESA_800x600_DFL_FNSZ 16
50
51 #define DUMP_RAW        0
52 #define DUMP_TXT        1
53
54 #define DUMP_FMT_REV    1
55
56 char    legal_colors[16][16] = {
57         "black", "blue", "green", "cyan",
58         "red", "magenta", "brown", "white",
59         "grey", "lightblue", "lightgreen", "lightcyan",
60         "lightred", "lightmagenta", "yellow", "lightwhite"
61 };
62 int     hex = 0;
63 int     number;
64 int     vesa_cols = _VESA_800x600_DFL_COLS;
65 int     vesa_rows = _VESA_800x600_DFL_ROWS;
66 char    letter;
67 struct  vid_info info;
68
69
70 static void
71 usage()
72 {
73         fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
74 "usage: vidcontrol [-CdLPpx] [-b color] [-c appearance] [-f [size] file]",
75 "                  [-g geometry] [-h size] [-i adapter | mode] [-l screen_map]",
76 "                  [-M char] [-m on | off] [-r foreground background]",
77 "                  [-S on | off] [-s number] [-t N | off] [mode]",
78 "                  [foreground [background]] [show]");
79         exit(1);
80 }
81
82 char *
83 nextarg(int ac, char **av, int *indp, int oc, int strict)
84 {
85         if (*indp < ac)
86                 return(av[(*indp)++]);
87         if (strict != 0)
88                 errx(1, "option requires two arguments -- %c", oc);
89         return(NULL);
90 }
91
92 FILE *
93 openguess(char *a[], char *b[], char *c[], char *d[], char **name)
94 {
95         FILE *f;
96         int i, j, k, l;
97
98         for (i = 0; a[i] != NULL; i++) {
99                 for (j = 0; b[j] != NULL; j++) {
100                         for (k = 0; c[k] != NULL; k++) {
101                                 for (l = 0; d[l] != NULL; l++) {
102                                         asprintf(name, "%s%s%s%s", a[i], b[j],
103                                             c[k], d[l]);
104                                         f = fopen(*name, "r");
105                                         if (f != NULL)
106                                                 return (f);
107                                         free(*name);
108                                 }
109                         }
110                 }
111         }
112         return (NULL);
113 }
114
115 void
116 load_scrnmap(char *filename)
117 {
118         FILE *fd;
119         int size;
120         char *name;
121         scrmap_t scrnmap;
122         char *a[] = {"", SCRNMAP_PATH, NULL};
123         char *b[] = {filename, NULL};
124         char *c[] = {"", ".scm", NULL};
125         char *d[] = {"", NULL};
126
127         fd = openguess(a, b, c, d, &name);
128         if (fd == NULL) {
129                 warn("screenmap file not found");
130                 return;
131         }
132         size = sizeof(scrnmap);
133         if (decode(fd, (char *)&scrnmap, size) != size) {
134                 rewind(fd);
135                 if (fread(&scrnmap, 1, size, fd) != size) {
136                         warnx("bad screenmap file");
137                         fclose(fd);
138                         return;
139                 }
140         }
141         if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
142                 warn("can't load screenmap");
143         fclose(fd);
144 }
145
146 void
147 load_default_scrnmap()
148 {
149         scrmap_t scrnmap;
150         int i;
151
152         for (i=0; i<256; i++)
153                 *((char*)&scrnmap + i) = i;
154         if (ioctl(0, PIO_SCRNMAP, &scrnmap) < 0)
155                 warn("can't load default screenmap");
156 }
157
158 void
159 print_scrnmap()
160 {
161         unsigned char map[256];
162         int i;
163
164         if (ioctl(0, GIO_SCRNMAP, &map) < 0) {
165                 warn("getting screenmap");
166                 return;
167         }
168         for (i=0; i<sizeof(map); i++) {
169                 if (i > 0 && i % 16 == 0)
170                         fprintf(stdout, "\n");
171                 if (hex)
172                         fprintf(stdout, " %02x", map[i]);
173                 else
174                         fprintf(stdout, " %03d", map[i]);
175         }
176         fprintf(stdout, "\n");
177
178 }
179
180 int
181 fsize(FILE *file)
182 {
183         struct stat sb;
184
185         if (fstat(fileno(file), &sb) == 0)
186                 return sb.st_size;
187         else
188                 return -1;
189 }
190
191 #define DATASIZE(x) ((x).w * (x).h * 256 / 8)
192
193 void
194 load_font(char *type, char *filename)
195 {
196         FILE    *fd;
197         int     h, i, size, w;
198         unsigned long io = 0;   /* silence stupid gcc(1) in the Wall mode */
199         char    *name, *fontmap, size_sufx[6];
200         char    *a[] = {"", FONT_PATH, NULL};
201         char    *b[] = {filename, NULL};
202         char    *c[] = {"", size_sufx, NULL};
203         char    *d[] = {"", ".fnt", NULL};
204         vid_info_t info;
205
206         struct sizeinfo {
207                 int w;
208                 int h;
209                 unsigned long io;
210         } sizes[] = {{8, 16, PIO_FONT8x16},
211                      {8, 14, PIO_FONT8x14},
212                      {8,  8,  PIO_FONT8x8},
213                      {0,  0,            0}};
214
215         info.size = sizeof(info);
216         if (ioctl(0, CONS_GETINFO, &info) == -1) {
217                 warn("failed to obtain current video mode parameters");
218                 return;
219         }
220         snprintf(size_sufx, sizeof(size_sufx), "-8x%d", info.font_size);
221         fd = openguess(a, b, c, d, &name);
222         if (fd == NULL) {
223                 warn("%s: can't load font file", filename);
224                 return;
225         }
226         if (type != NULL) {
227                 size = 0;
228                 if (sscanf(type, "%dx%d", &w, &h) == 2)
229                         for (i = 0; sizes[i].w != 0; i++)
230                                 if (sizes[i].w == w && sizes[i].h == h) {
231                                         size = DATASIZE(sizes[i]);
232                                         io = sizes[i].io;
233                                 }
234
235                 if (size == 0) {
236                         warnx("%s: bad font size specification", type);
237                         fclose(fd);
238                         return;
239                 }
240         } else {
241                 /* Apply heuristics */
242                 int j;
243                 int dsize[2];
244
245                 size = DATASIZE(sizes[0]);
246                 fontmap = (char*) malloc(size);
247                 dsize[0] = decode(fd, fontmap, size);
248                 dsize[1] = fsize(fd);
249                 free(fontmap);
250
251                 size = 0;
252                 for (j = 0; j < 2; j++)
253                         for (i = 0; sizes[i].w != 0; i++)
254                                 if (DATASIZE(sizes[i]) == dsize[j]) {
255                                         size = dsize[j];
256                                         io = sizes[i].io;
257                                         j = 2;  /* XXX */
258                                         break;
259                                 }
260
261                 if (size == 0) {
262                         warnx("%s: can't guess font size", filename);
263                         fclose(fd);
264                         return;
265                 }
266                 rewind(fd);
267         }
268
269         fontmap = (char*) malloc(size);
270         if (decode(fd, fontmap, size) != size) {
271                 rewind(fd);
272                 if (fsize(fd) != size || fread(fontmap, 1, size, fd) != size) {
273                         warnx("%s: bad font file", filename);
274                         fclose(fd);
275                         free(fontmap);
276                         return;
277                 }
278         }
279         if (ioctl(0, io, fontmap) < 0)
280                 warn("can't load font");
281         fclose(fd);
282         free(fontmap);
283 }
284
285 void
286 set_screensaver_timeout(char *arg)
287 {
288         int nsec;
289
290         if (!strcmp(arg, "off"))
291                 nsec = 0;
292         else {
293                 nsec = atoi(arg);
294                 if ((*arg == '\0') || (nsec < 1)) {
295                         warnx("argument must be a positive number");
296                         return;
297                 }
298         }
299         if (ioctl(0, CONS_BLANKTIME, &nsec) == -1)
300                 warn("setting screensaver period");
301 }
302
303 void
304 set_cursor_type(char *appearence)
305 {
306         int type;
307
308         if (!strcmp(appearence, "normal"))
309                 type = 0;
310         else if (!strcmp(appearence, "blink"))
311                 type = 1;
312         else if (!strcmp(appearence, "destructive"))
313                 type = 3;
314         else {
315                 warnx("argument to -c must be normal, blink or destructive");
316                 return;
317         }
318         ioctl(0, CONS_CURSORTYPE, &type);
319 }
320
321 void
322 video_mode(int argc, char **argv, int *index)
323 {
324         static struct {
325                 char *name;
326                 unsigned long mode;
327         } modes[] = {
328                 { "80x25",              SW_TEXT_80x25 },
329                 { "80x30",              SW_TEXT_80x30 },
330                 { "80x43",              SW_TEXT_80x43 },
331                 { "80x50",              SW_TEXT_80x50 },
332                 { "80x60",              SW_TEXT_80x60 },
333                 { "132x25",             SW_TEXT_132x25 },
334                 { "132x30",             SW_TEXT_132x30 },
335                 { "132x43",             SW_TEXT_132x43 },
336                 { "132x50",             SW_TEXT_132x50 },
337                 { "132x60",             SW_TEXT_132x60 },
338                 { "VGA_40x25",          SW_VGA_C40x25 },
339                 { "VGA_80x25",          SW_VGA_C80x25 },
340                 { "VGA_80x30",          SW_VGA_C80x30 },
341                 { "VGA_80x50",          SW_VGA_C80x50 },
342                 { "VGA_80x60",          SW_VGA_C80x60 },
343 #ifdef SW_VGA_C90x25
344                 { "VGA_90x25",          SW_VGA_C90x25 },
345                 { "VGA_90x30",          SW_VGA_C90x30 },
346                 { "VGA_90x43",          SW_VGA_C90x43 },
347                 { "VGA_90x50",          SW_VGA_C90x50 },
348                 { "VGA_90x60",          SW_VGA_C90x60 },
349 #endif
350                 { "VGA_320x200",        SW_VGA_CG320 },
351                 { "EGA_80x25",          SW_ENH_C80x25 },
352                 { "EGA_80x43",          SW_ENH_C80x43 },
353                 { "VESA_132x25",        SW_VESA_C132x25 },
354                 { "VESA_132x43",        SW_VESA_C132x43 },
355                 { "VESA_132x50",        SW_VESA_C132x50 },
356                 { "VESA_132x60",        SW_VESA_C132x60 },
357                 { "VESA_800x600",       SW_VESA_800x600 },
358                 { NULL },
359         };
360         unsigned long mode = 0;
361         int cur_mode; 
362         int ioerr;
363         int size[3];
364         int i;
365
366         if (ioctl(0, CONS_GET, &cur_mode) < 0)
367                 err(1, "cannot get the current video mode");
368         if (*index < argc) {
369                 for (i = 0; modes[i].name != NULL; ++i) {
370                         if (!strcmp(argv[*index], modes[i].name)) {
371                                 mode = modes[i].mode;
372                                 break;
373                         }
374                 }
375                 if (modes[i].name == NULL)
376                         return;
377                 if (ioctl(0, mode, NULL) < 0)
378                         warn("cannot set videomode");
379                 if (mode == SW_VESA_800x600) {
380                         /* columns */
381                         if ((vesa_cols * 8 > 800) || (vesa_cols <= 0)) {
382                                 warnx("incorrect number of columns: %d",
383                                       vesa_cols);
384                                 size[0] = _VESA_800x600_DFL_COLS;
385                         } else {
386                                 size[0] = vesa_cols;
387                         }
388                         /* rows */
389                         if ((vesa_rows * _VESA_800x600_DFL_FNSZ > 600) ||
390                             (vesa_rows <=0)) {
391                                 warnx("incorrect number of rows: %d",
392                                       vesa_rows);
393                                 size[1] = _VESA_800x600_DFL_ROWS;
394                         } else {
395                                 size[1] = vesa_rows;
396                         }
397                         /* font size */
398                         size[2] = _VESA_800x600_DFL_FNSZ;
399                         if (ioctl(0, KDRASTER, size)) {
400                                 ioerr = errno;
401                                 if (cur_mode >= M_VESA_BASE)
402                                         ioctl(0,
403                                             _IO('V', cur_mode - M_VESA_BASE),
404                                             NULL);
405                                 else
406                                         ioctl(0, _IO('S', cur_mode), NULL);
407                                 warnc(ioerr, "cannot activate raster display");
408                         }
409                 }
410                 (*index)++;
411         }
412         return;
413 }
414
415 int
416 get_color_number(char *color)
417 {
418         int i;
419
420         for (i=0; i<16; i++)
421                 if (!strcmp(color, legal_colors[i]))
422                         return i;
423         return -1;
424 }
425
426 void
427 set_normal_colors(int argc, char **argv, int *index)
428 {
429         int color;
430
431         if (*index < argc && (color = get_color_number(argv[*index])) != -1) {
432                 (*index)++;
433                 fprintf(stderr, "\e[=%dF", color);
434                 if (*index < argc
435                     && (color = get_color_number(argv[*index])) != -1
436                     && color < 8) {
437                         (*index)++;
438                         fprintf(stderr, "\e[=%dG", color);
439                 }
440         }
441 }
442
443 void
444 set_reverse_colors(int argc, char **argv, int *index)
445 {
446         int color;
447
448         if ((color = get_color_number(argv[*(index)-1])) != -1) {
449                 fprintf(stderr, "\e[=%dH", color);
450                 if (*index < argc
451                     && (color = get_color_number(argv[*index])) != -1
452                     && color < 8) {
453                         (*index)++;
454                         fprintf(stderr, "\e[=%dI", color);
455                 }
456         }
457 }
458
459 void
460 set_console(char *arg)
461 {
462         int n;
463
464         if( !arg || strspn(arg,"0123456789") != strlen(arg)) {
465                 warnx("bad console number");
466                 return;
467         }
468
469         n = atoi(arg);
470         if (n < 1 || n > 16) {
471                 warnx("console number out of range");
472         } else if (ioctl(0, VT_ACTIVATE, (caddr_t) (long) n) == -1)
473                 warn("ioctl(VT_ACTIVATE)");
474 }
475
476 void
477 set_border_color(char *arg)
478 {
479         int color;
480
481         if ((color = get_color_number(arg)) != -1) {
482                 fprintf(stderr, "\e[=%dA", color);
483         }
484         else
485                 usage();
486 }
487
488 void
489 set_mouse_char(char *arg)
490 {
491         struct mouse_info mouse;
492         long l;
493
494         l = strtol(arg, NULL, 0);
495         if ((l < 0) || (l > UCHAR_MAX)) {
496                 warnx("argument to -M must be 0 through %d", UCHAR_MAX);
497                 return;
498         }
499         mouse.operation = MOUSE_MOUSECHAR;
500         mouse.u.mouse_char = (int)l;
501         ioctl(0, CONS_MOUSECTL, &mouse);
502 }
503
504 void
505 set_mouse(char *arg)
506 {
507         struct mouse_info mouse;
508
509         if (!strcmp(arg, "on"))
510                 mouse.operation = MOUSE_SHOW;
511         else if (!strcmp(arg, "off"))
512                 mouse.operation = MOUSE_HIDE;
513         else {
514                 warnx("argument to -m must be either on or off");
515                 return;
516         }
517         ioctl(0, CONS_MOUSECTL, &mouse);
518 }
519
520 void
521 set_lockswitch(char *arg)
522 {
523         int data;
524
525         if (!strcmp(arg, "off"))
526                 data = 0x01;
527         else if (!strcmp(arg, "on"))
528                 data = 0x02;
529         else {
530                 warnx("argument to -S must be either on or off");
531                 return;
532         }
533         if (ioctl(0, VT_LOCKSWITCH, &data) == -1)
534                 warn("ioctl(VT_LOCKSWITCH)");
535 }
536
537 static char
538 *adapter_name(int type)
539 {
540     static struct {
541         int type;
542         char *name;
543     } names[] = {
544         { KD_MONO,      "MDA" },
545         { KD_HERCULES,  "Hercules" },
546         { KD_CGA,       "CGA" },
547         { KD_EGA,       "EGA" },
548         { KD_VGA,       "VGA" },
549         { KD_PC98,      "PC-98xx" },
550         { KD_TGA,       "TGA" },
551         { -1,           "Unknown" },
552     };
553     int i;
554
555     for (i = 0; names[i].type != -1; ++i)
556         if (names[i].type == type)
557             break;
558     return names[i].name;
559 }
560
561 void
562 show_adapter_info(void)
563 {
564         struct video_adapter_info ad;
565
566         ad.va_index = 0;
567         if (ioctl(0, CONS_ADPINFO, &ad)) {
568                 warn("failed to obtain adapter information");
569                 return;
570         }
571
572         printf("fb%d:\n", ad.va_index);
573         printf("    %.*s%d, type:%s%s (%d), flags:0x%x\n",
574                (int)sizeof(ad.va_name), ad.va_name, ad.va_unit,
575                (ad.va_flags & V_ADP_VESA) ? "VESA " : "",
576                adapter_name(ad.va_type), ad.va_type, ad.va_flags);
577         printf("    initial mode:%d, current mode:%d, BIOS mode:%d\n",
578                ad.va_initial_mode, ad.va_mode, ad.va_initial_bios_mode);
579         printf("    frame buffer window:0x%x, buffer size:0x%x\n",
580                ad.va_window, ad.va_buffer_size);
581         printf("    window size:0x%x, origin:0x%x\n",
582                ad.va_window_size, ad.va_window_orig);
583         printf("    display start address (%d, %d), scan line width:%d\n",
584                ad.va_disp_start.x, ad.va_disp_start.y, ad.va_line_width);
585         printf("    reserved:0x%x\n", ad.va_unused0);
586 }
587
588 void
589 show_mode_info(void)
590 {
591         struct video_info info;
592         char buf[80];
593         int mode;
594         int c;
595
596         printf("    mode#     flags   type    size       "
597                "font      window      linear buffer\n");
598         printf("---------------------------------------"
599                "---------------------------------------\n");
600         for (mode = 0; mode < M_VESA_MODE_MAX; ++mode) {
601                 info.vi_mode = mode;
602                 if (ioctl(0, CONS_MODEINFO, &info))
603                         continue;
604                 if (info.vi_mode != mode)
605                         continue;
606
607                 printf("%3d (0x%03x)", mode, mode);
608                 printf(" 0x%08x", info.vi_flags);
609                 if (info.vi_flags & V_INFO_GRAPHICS) {
610                         c = 'G';
611                         snprintf(buf, sizeof(buf), "%dx%dx%d %d",
612                                  info.vi_width, info.vi_height, 
613                                  info.vi_depth, info.vi_planes);
614                 } else {
615                         c = 'T';
616                         snprintf(buf, sizeof(buf), "%dx%d",
617                                  info.vi_width, info.vi_height);
618                 }
619                 printf(" %c %-15s", c, buf);
620                 snprintf(buf, sizeof(buf), "%dx%d", 
621                          info.vi_cwidth, info.vi_cheight); 
622                 printf(" %-5s", buf);
623                 printf(" 0x%05x %2dk %2dk", 
624                        info.vi_window, (int)info.vi_window_size/1024, 
625                        (int)info.vi_window_gran/1024);
626                 printf(" 0x%08x %dk\n",
627                        info.vi_buffer, (int)info.vi_buffer_size/1024);
628         }
629 }
630
631 void
632 show_info(char *arg)
633 {
634         if (!strcmp(arg, "adapter"))
635                 show_adapter_info();
636         else if (!strcmp(arg, "mode"))
637                 show_mode_info();
638         else {
639                 warnx("argument to -i must be either adapter or mode");
640                 return;
641         }
642 }
643
644 void
645 test_frame()
646 {
647         int i;
648
649         fprintf(stdout, "\e[=0G\n\n");
650         for (i=0; i<8; i++) {
651                 fprintf(stdout, "\e[=15F\e[=0G        %2d \e[=%dF%-16s"
652                                 "\e[=15F\e[=0G        %2d \e[=%dF%-16s        "
653                                 "\e[=15F %2d \e[=%dGBACKGROUND\e[=0G\n",
654                         i, i, legal_colors[i], i+8, i+8,
655                         legal_colors[i+8], i, i);
656         }
657         fprintf(stdout, "\e[=%dF\e[=%dG\e[=%dH\e[=%dI\n",
658                 info.mv_norm.fore, info.mv_norm.back,
659                 info.mv_rev.fore, info.mv_rev.back);
660 }
661
662 /*
663  * Snapshot the video memory of that terminal, using the CONS_SCRSHOT
664  * ioctl, and writes the results to stdout either in the special
665  * binary format (see manual page for details), or in the plain
666  * text format.
667  */
668 void
669 dump_screen(int mode)
670 {
671         scrshot_t shot;
672         vid_info_t info;
673
674         info.size = sizeof(info);
675         if (ioctl(0, CONS_GETINFO, &info) == -1) {
676                 warn("failed to obtain current video mode parameters");
677                 return;
678         }
679
680         shot.buf = alloca(info.mv_csz * info.mv_rsz * sizeof(u_int16_t));
681         if (shot.buf == NULL) {
682                 warn("failed to allocate memory for dump");
683                 return;
684         }
685
686         shot.xsize = info.mv_csz;
687         shot.ysize = info.mv_rsz;
688         if (ioctl(0, CONS_SCRSHOT, &shot) == -1) {
689                 warn("failed to get dump of the screen");
690                 return;
691         }
692
693         if (mode == DUMP_RAW) {
694                 printf("SCRSHOT_%c%c%c%c", DUMP_FMT_REV, 2,
695                        shot.xsize, shot.ysize);
696                 fflush(stdout);
697
698                 (void)write(STDOUT_FILENO, shot.buf,
699                             shot.xsize * shot.ysize * sizeof(u_int16_t));
700         } else {
701                 char *line;
702                 int x, y;
703                 u_int16_t ch;
704
705                 line = alloca(shot.xsize + 1);
706                 if (line == NULL) {
707                         warn("failed to allocate memory for line buffer");
708                         return;
709                 }
710
711                 for (y = 0; y < shot.ysize; y++) {
712                         for (x = 0; x < shot.xsize; x++) {
713                                 ch = shot.buf[x + (y * shot.xsize)];
714                                 ch &= 0xff;
715                                 if (isprint(ch) == 0)
716                                         ch = ' ';
717                                 line[x] = (char)ch;
718                         }
719
720                         /* Trim trailing spaces */
721                         do {
722                                 line[x--] = '\0';
723                         } while (line[x] == ' ' && x != 0);
724
725                         puts(line);
726                 }
727                 fflush(stdout);
728         }
729
730         return;
731 }
732
733 void
734 set_history(char *opt)
735 {
736         int size;
737
738         size = atoi(opt);
739         if ((*opt == '\0') || size < 0) {
740                 warnx("argument must be a positive number");
741                 return;
742         }
743         if (ioctl(0, CONS_HISTORY, &size) == -1)
744                 warn("setting history buffer size");
745 }
746
747 void
748 clear_history()
749 {
750
751         if (ioctl(0, CONS_CLRHIST) == -1)
752                 warn("clear history buffer");
753 }
754
755 int
756 main(int argc, char **argv)
757 {
758         char    *font, *type;
759         int     opt;
760
761
762         info.size = sizeof(info);
763         if (argc == 1)
764                 usage();
765                 /* Not reached */
766         if (ioctl(0, CONS_GETINFO, &info) < 0)
767                 err(1, "must be on a virtual console");
768         while((opt = getopt(argc, argv, "b:Cc:df:g:h:i:l:LM:m:pPr:S:s:t:x")) != -1)
769                 switch(opt) {
770                 case 'b':
771                         set_border_color(optarg);
772                         break;
773                 case 'C':
774                         clear_history();
775                         break;
776                 case 'c':
777                         set_cursor_type(optarg);
778                         break;
779                 case 'd':
780                         print_scrnmap();
781                         break;
782                 case 'f':
783                         type = optarg;
784                         font = nextarg(argc, argv, &optind, 'f', 0);
785                         if (font == NULL) {
786                                 type = NULL;
787                                 font = optarg;
788                         }
789                         load_font(type, font);
790                         break;
791                 case 'g':
792                         if (sscanf(optarg, "%dx%d", &vesa_cols,
793                             &vesa_rows) != 2) {
794                                 warnx("incorrect geometry: %s", optarg);
795                                 usage();
796                         }
797                         break;
798                 case 'h':
799                         set_history(optarg);
800                         break;
801                 case 'i':
802                         show_info(optarg);
803                         break;
804                 case 'l':
805                         load_scrnmap(optarg);
806                         break;
807                 case 'L':
808                         load_default_scrnmap();
809                         break;
810                 case 'M':
811                         set_mouse_char(optarg);
812                         break;
813                 case 'm':
814                         set_mouse(optarg);
815                         break;
816                 case 'p':
817                         dump_screen(DUMP_RAW);
818                         break;
819                 case 'P':
820                         dump_screen(DUMP_TXT);
821                         break;
822                 case 'r':
823                         set_reverse_colors(argc, argv, &optind);
824                         break;
825                 case 'S':
826                         set_lockswitch(optarg);
827                         break;
828                 case 's':
829                         set_console(optarg);
830                         break;
831                 case 't':
832                         set_screensaver_timeout(optarg);
833                         break;
834                 case 'x':
835                         hex = 1;
836                         break;
837                 default:
838                         usage();
839                 }
840         video_mode(argc, argv, &optind);
841         set_normal_colors(argc, argv, &optind);
842         if (optind < argc && !strcmp(argv[optind], "show")) {
843                 test_frame();
844                 optind++;
845         }
846         if ((optind != argc) || (argc == 1))
847                 usage();
848         return 0;
849 }
850