Add pancyrillic font
[platform/upstream/kbd.git] / src / screendump.c
1 /*
2  * screendump.c - aeb 950214
3  *
4  * [Note: similar functionality can be found in setterm]
5  *
6  * Call: "screendump N" when the screen of /dev/ttyN has to be dumped
7  *
8  * On Linux up to 1.1.91 there is an ioctl that will do the dumping.
9  * Because of problems with security this has been scrapped.
10  * From 1.1.92 on, make devices "virtual console screen" and
11  * "virtual console screen with attributes" by (fill in the ellipses):
12  *      cd /dev
13  *      for i in 0 1 2 3 ...; do
14  *              mknod vcs$i c 7 $i
15  *              mknod vcsa$i c 7 `expr 128 + $i`
16  *      done
17  * and give them your favourite owners and permissions.
18  */
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <fcntl.h>
23 #include <errno.h>
24 #include <sys/ioctl.h>
25 #include "xmalloc.h"
26 #include "nls.h"
27 #include "version.h"
28
29 int
30 main(int argc, char **argv) {
31     int cons = 0;
32     char infile[20];
33     unsigned char header[4];
34     unsigned int rows, cols;
35     int fd;
36     unsigned int i, j;
37     char *inbuf, *outbuf, *p, *q;
38
39     set_progname(argv[0]);
40
41     setlocale(LC_ALL, "");
42     bindtextdomain(PACKAGE_NAME, LOCALEDIR);
43     textdomain(PACKAGE_NAME);
44
45     if (argc == 2 && !strcmp(argv[1], "-V"))
46         print_version_and_exit();
47
48     if (argc > 2) {
49         fprintf(stderr, _("usage: screendump [n]\n"));
50         exit(1);
51     }
52
53     cons = (argc == 2) ? atoi(argv[1]) : 0;
54
55     sprintf(infile, "/dev/vcsa%d", cons);
56     fd = open(infile, O_RDONLY);
57     if (fd < 0 && cons == 0 && errno == ENOENT) {
58       sprintf(infile, "/dev/vcsa");
59       fd = open(infile, O_RDONLY);
60     }
61     if (fd < 0 && errno == ENOENT) {
62       sprintf(infile, "/dev/vcs/a%d", cons);
63       fd = open(infile, O_RDONLY);
64     }
65     if (fd < 0 && cons == 0 && errno == ENOENT) {
66       sprintf(infile, "/dev/vcs/a");
67       fd = open(infile, O_RDONLY);
68     }
69     if (fd < 0 || read(fd, header, 4) != 4)
70       goto try_ioctl;
71     rows = header[0];
72     cols = header[1];
73     if (rows * cols == 0)
74       goto try_ioctl;
75     inbuf = xmalloc(rows*cols*2);
76     outbuf = xmalloc(rows*(cols+1));
77
78     if (read(fd, inbuf, rows*cols*2) != (ssize_t) (rows*cols*2)) {
79         fprintf(stderr, _("Error reading %s\n"), infile);
80         exit(1);
81     }
82     p = inbuf;
83     q = outbuf;
84     for(i=0; i<rows; i++) {
85         for(j=0; j<cols; j++) {
86             *q++ = *p;
87             p += 2;
88         }
89         while(j-- > 0 && q[-1] == ' ')
90           q--;
91         *q++ = '\n';
92     }
93     goto done;
94
95 try_ioctl:
96     {
97         struct winsize win;
98         char consnam[20], devfsconsnam[20];
99         unsigned char *screenbuf;
100
101         sprintf(consnam, "/dev/tty%d", cons);
102         fd = open(consnam, O_RDONLY);
103         if (fd < 0 && errno == ENOENT) {
104             sprintf(devfsconsnam, "/dev/vc/%d", cons);
105             fd = open(devfsconsnam, O_RDONLY);
106             if (fd < 0)
107                 errno = ENOENT;
108         }
109         if (fd < 0) {
110             perror(consnam);
111             fd = 0;
112         }
113
114         if (ioctl(fd, TIOCGWINSZ, &win)) {
115             perror("TIOCGWINSZ");
116             exit(1);
117         }
118
119         screenbuf = xmalloc(2 + win.ws_row * win.ws_col);
120         screenbuf[0] = 0;
121         screenbuf[1] = (unsigned char) cons;
122
123         if (ioctl(fd,TIOCLINUX,screenbuf) &&
124             (!fd || ioctl(0,TIOCLINUX,screenbuf))) {
125 #if 0
126             perror("TIOCLINUX");
127             fprintf(stderr,_("couldn't read %s, and cannot ioctl dump\n"),
128                     infile);
129 #else
130             /* we tried this just to be sure, but TIOCLINUX
131                function 0 has been disabled since 1.1.92
132                Do not mention `ioctl dump' in error msg */
133             fprintf(stderr,_("couldn't read %s\n"), infile);
134 #endif
135             exit(1);
136         }
137
138         rows = screenbuf[0];
139         cols = screenbuf[1];
140         if (rows != win.ws_row || cols != win.ws_col) {
141             fprintf(stderr,
142                     _("Strange ... screen is both %dx%d and %dx%d ??\n"),
143                     win.ws_col, win.ws_row, cols, rows);
144             exit(1);
145         }
146
147         outbuf = xmalloc(rows*(cols+1));
148         p = ((char *)screenbuf) + 2;
149         q = outbuf;
150         for (i=0; i<rows; i++) {
151             for (j=0; j<cols; j++)
152               *q++ = *p++;
153             while (j-- > 0 && (q[-1] == ' '))
154               q--;
155             *q++ = '\n';
156         }
157     }
158 done:
159     if (write(1, outbuf, q-outbuf) != q-outbuf) {
160         fprintf(stderr, _("Error writing screendump\n"));
161         exit(1);
162     }
163     exit(0);
164 }