xref: /minix3/minix/commands/screendump/screendump.c (revision 433d6423c39e34ec4b79c950597bb2d236f886be)
1*433d6423SLionel Sambuc /*	screendump 1.2 - dump the contents of the console
2*433d6423SLionel Sambuc  *							Author: Kees J. Bot
3*433d6423SLionel Sambuc  *								16 Dec 1994
4*433d6423SLionel Sambuc  */
5*433d6423SLionel Sambuc #define nil 0
6*433d6423SLionel Sambuc #include <sys/types.h>
7*433d6423SLionel Sambuc #include <stdlib.h>
8*433d6423SLionel Sambuc #include <unistd.h>
9*433d6423SLionel Sambuc #include <errno.h>
10*433d6423SLionel Sambuc #include <fcntl.h>
11*433d6423SLionel Sambuc #include <string.h>
12*433d6423SLionel Sambuc #include <termios.h>
13*433d6423SLionel Sambuc #include <sys/ioctl.h>
14*433d6423SLionel Sambuc 
15*433d6423SLionel Sambuc #define BIOS_CRTBASE	0x00463L	/* BIOS parameters: CRT base. */
16*433d6423SLionel Sambuc #define CRTBASE_MONO	0x03B4		/* Value of CRT base for mono mode. */
17*433d6423SLionel Sambuc 
18*433d6423SLionel Sambuc #define MONO_BASE	0xB0000L	/* Screen memory in monochrome mode. */
19*433d6423SLionel Sambuc #define COLOR_BASE	0xB8000L	/* ... colour mode. */
20*433d6423SLionel Sambuc 
21*433d6423SLionel Sambuc #define DEF_COLS	80		/* Default screen geometry. */
22*433d6423SLionel Sambuc #define DEF_ROWS	25
23*433d6423SLionel Sambuc 
24*433d6423SLionel Sambuc #define MAX_COLS	132		/* Maximum screen geometry. */
25*433d6423SLionel Sambuc #define MAX_ROWS	60
26*433d6423SLionel Sambuc 
27*433d6423SLionel Sambuc char MEMORY[] =		"/dev/mem";	/* Memory device to read screen. */
28*433d6423SLionel Sambuc int mfd;				/* Open memory device. */
29*433d6423SLionel Sambuc 
tell(const char * message)30*433d6423SLionel Sambuc void tell(const char *message)
31*433d6423SLionel Sambuc {
32*433d6423SLionel Sambuc 	write(2, message, strlen(message));
33*433d6423SLionel Sambuc }
34*433d6423SLionel Sambuc 
fatal(const char * label)35*433d6423SLionel Sambuc void fatal(const char *label)
36*433d6423SLionel Sambuc {
37*433d6423SLionel Sambuc 	const char *err= strerror(errno);
38*433d6423SLionel Sambuc 
39*433d6423SLionel Sambuc 	tell("screendump: ");
40*433d6423SLionel Sambuc 	tell(label);
41*433d6423SLionel Sambuc 	tell(": ");
42*433d6423SLionel Sambuc 	tell(err);
43*433d6423SLionel Sambuc 	tell("\n");
44*433d6423SLionel Sambuc 	exit(1);
45*433d6423SLionel Sambuc }
46*433d6423SLionel Sambuc 
video_base(void)47*433d6423SLionel Sambuc long video_base(void)
48*433d6423SLionel Sambuc /* Is it monochrome or colour? */
49*433d6423SLionel Sambuc {
50*433d6423SLionel Sambuc 	static unsigned short bios_crtbase;
51*433d6423SLionel Sambuc 
52*433d6423SLionel Sambuc 	if (lseek(mfd, (off_t) BIOS_CRTBASE, SEEK_SET) == -1) fatal(MEMORY);
53*433d6423SLionel Sambuc 	switch (read(mfd, &bios_crtbase, sizeof(bios_crtbase))) {
54*433d6423SLionel Sambuc 	case -1:
55*433d6423SLionel Sambuc 		fatal(MEMORY);
56*433d6423SLionel Sambuc 	default:
57*433d6423SLionel Sambuc 		tell("screendump: can't obtain BIOS parameter: short read\n");
58*433d6423SLionel Sambuc 		exit(1);
59*433d6423SLionel Sambuc 	case sizeof(bios_crtbase):
60*433d6423SLionel Sambuc 		/* Fine */;
61*433d6423SLionel Sambuc 	}
62*433d6423SLionel Sambuc 
63*433d6423SLionel Sambuc 	return bios_crtbase == CRTBASE_MONO ? MONO_BASE : COLOR_BASE;
64*433d6423SLionel Sambuc }
65*433d6423SLionel Sambuc 
main(void)66*433d6423SLionel Sambuc int main(void)
67*433d6423SLionel Sambuc {
68*433d6423SLionel Sambuc 	static unsigned char screen[MAX_COLS * MAX_ROWS * 2];
69*433d6423SLionel Sambuc 	unsigned char *ps;
70*433d6423SLionel Sambuc 	long base;
71*433d6423SLionel Sambuc 	int lfd;
72*433d6423SLionel Sambuc 	int row;
73*433d6423SLionel Sambuc 	int nrows, ncols;
74*433d6423SLionel Sambuc 	struct winsize winsize;
75*433d6423SLionel Sambuc 
76*433d6423SLionel Sambuc 	/* Open the memory device. */
77*433d6423SLionel Sambuc 	if ((mfd= open(MEMORY, O_RDONLY)) < 0) fatal(MEMORY);
78*433d6423SLionel Sambuc 
79*433d6423SLionel Sambuc 	base= video_base();
80*433d6423SLionel Sambuc 
81*433d6423SLionel Sambuc 	/* Read screen memory. */
82*433d6423SLionel Sambuc 	if (lseek(mfd, base, SEEK_SET) == -1) fatal(MEMORY);
83*433d6423SLionel Sambuc 
84*433d6423SLionel Sambuc 	switch (read(mfd, screen, sizeof(screen))) {
85*433d6423SLionel Sambuc 	case -1:
86*433d6423SLionel Sambuc 		fatal(MEMORY);
87*433d6423SLionel Sambuc 	default:
88*433d6423SLionel Sambuc 		tell("screendump: can't obtain screen dump: short read\n");
89*433d6423SLionel Sambuc 		exit(1);
90*433d6423SLionel Sambuc 	case sizeof(screen):
91*433d6423SLionel Sambuc 		/* Fine */;
92*433d6423SLionel Sambuc 	}
93*433d6423SLionel Sambuc 
94*433d6423SLionel Sambuc 	/* Try to obtain the screen geometry from /dev/log. */
95*433d6423SLionel Sambuc 	ncols= DEF_COLS;
96*433d6423SLionel Sambuc 	nrows= DEF_ROWS;
97*433d6423SLionel Sambuc 	if ((lfd= open("/dev/log", O_WRONLY)) != -1
98*433d6423SLionel Sambuc 		&& ioctl(lfd, TIOCGWINSZ, &winsize) == 0
99*433d6423SLionel Sambuc 	) {
100*433d6423SLionel Sambuc 		if (40 <= winsize.ws_col && winsize.ws_col <= MAX_COLS) {
101*433d6423SLionel Sambuc 			ncols= winsize.ws_col;
102*433d6423SLionel Sambuc 		}
103*433d6423SLionel Sambuc 		if (25 <= winsize.ws_row && winsize.ws_row <= MAX_COLS) {
104*433d6423SLionel Sambuc 			nrows= winsize.ws_row;
105*433d6423SLionel Sambuc 		}
106*433d6423SLionel Sambuc 	}
107*433d6423SLionel Sambuc 	if (lfd != -1) close(lfd);
108*433d6423SLionel Sambuc 
109*433d6423SLionel Sambuc 	/* Print the contents of the screen line by line.  Omit trailing
110*433d6423SLionel Sambuc 	 * blanks.  Note that screen memory consists of pairs of characters
111*433d6423SLionel Sambuc 	 * and attribute bytes.
112*433d6423SLionel Sambuc 	 */
113*433d6423SLionel Sambuc 	ps= screen;
114*433d6423SLionel Sambuc 	for (row= 0; row < nrows; row++) {
115*433d6423SLionel Sambuc 		char line[MAX_COLS + 1];
116*433d6423SLionel Sambuc 		char *pl= line;
117*433d6423SLionel Sambuc 		int column;
118*433d6423SLionel Sambuc 		int blanks= 0;
119*433d6423SLionel Sambuc 
120*433d6423SLionel Sambuc 		for (column= 0; column < ncols; column++) {
121*433d6423SLionel Sambuc 			if (*ps <= ' ') {
122*433d6423SLionel Sambuc 				/* Skip trailing junk. */
123*433d6423SLionel Sambuc 				blanks++;
124*433d6423SLionel Sambuc 			} else {
125*433d6423SLionel Sambuc 				/* Reinsert blanks and add a character. */
126*433d6423SLionel Sambuc 				while (blanks > 0) { *pl++= ' '; blanks--; }
127*433d6423SLionel Sambuc 				*pl++= *ps;
128*433d6423SLionel Sambuc 			}
129*433d6423SLionel Sambuc 			/* Skip character and attribute byte. */
130*433d6423SLionel Sambuc 			ps+= 2;
131*433d6423SLionel Sambuc 		}
132*433d6423SLionel Sambuc 		*pl++= '\n';
133*433d6423SLionel Sambuc 		if (write(1, line, pl - line) < 0) fatal("stdout");
134*433d6423SLionel Sambuc 	}
135*433d6423SLionel Sambuc 	exit(0);
136*433d6423SLionel Sambuc }
137