xref: /netbsd-src/external/bsd/less/dist/lesstest/log.c (revision e4a6e799a67c2028562d75b4e61407b22434aa36)
1*e4a6e799Ssimonb #include <stdio.h>
2*e4a6e799Ssimonb #include <string.h>
3*e4a6e799Ssimonb #include <unistd.h>
4*e4a6e799Ssimonb #include <stdarg.h>
5*e4a6e799Ssimonb #include <time.h>
6*e4a6e799Ssimonb #include <sys/stat.h>
7*e4a6e799Ssimonb #include "lesstest.h"
8*e4a6e799Ssimonb 
9*e4a6e799Ssimonb static FILE* logf = NULL;
10*e4a6e799Ssimonb 
log_open(const char * logfile)11*e4a6e799Ssimonb int log_open(const char* logfile) {
12*e4a6e799Ssimonb 	if (logf != NULL) fclose(logf);
13*e4a6e799Ssimonb 	logf = (strcmp(logfile, "-") == 0) ? stdout : fopen(logfile, "w");
14*e4a6e799Ssimonb 	if (logf == NULL) {
15*e4a6e799Ssimonb 		fprintf(stderr, "cannot create %s\n", logfile);
16*e4a6e799Ssimonb 		return 0;
17*e4a6e799Ssimonb 	}
18*e4a6e799Ssimonb 	return 1;
19*e4a6e799Ssimonb }
20*e4a6e799Ssimonb 
log_close(void)21*e4a6e799Ssimonb void log_close(void) {
22*e4a6e799Ssimonb 	if (logf == NULL) return;
23*e4a6e799Ssimonb 	if (logf == stdout) return;
24*e4a6e799Ssimonb 	fclose(logf);
25*e4a6e799Ssimonb 	logf = NULL;
26*e4a6e799Ssimonb }
27*e4a6e799Ssimonb 
log_file_header(void)28*e4a6e799Ssimonb int log_file_header(void) {
29*e4a6e799Ssimonb 	if (logf == NULL) return 0;
30*e4a6e799Ssimonb 	time_t now = time(NULL);
31*e4a6e799Ssimonb 	struct tm* tm = gmtime(&now);
32*e4a6e799Ssimonb 	fprintf(logf, "!lesstest!\n!version %d\n!created %d-%02d-%02d %02d:%02d:%02d\n",
33*e4a6e799Ssimonb 		LESSTEST_VERSION,
34*e4a6e799Ssimonb 		tm->tm_year+1900, tm->tm_mon+1, tm->tm_mday,
35*e4a6e799Ssimonb 		tm->tm_hour, tm->tm_min, tm->tm_sec);
36*e4a6e799Ssimonb 	return 1;
37*e4a6e799Ssimonb }
38*e4a6e799Ssimonb 
log_env(const char * name,int namelen,const char * value)39*e4a6e799Ssimonb int log_env(const char* name, int namelen, const char* value) {
40*e4a6e799Ssimonb 	if (logf == NULL) return 0;
41*e4a6e799Ssimonb 	fprintf(logf, "E \"%.*s\" \"%s\"\n", namelen, name, value);
42*e4a6e799Ssimonb 	return 1;
43*e4a6e799Ssimonb }
44*e4a6e799Ssimonb 
log_tty_char(wchar ch)45*e4a6e799Ssimonb int log_tty_char(wchar ch) {
46*e4a6e799Ssimonb 	if (logf == NULL) return 0;
47*e4a6e799Ssimonb 	fprintf(logf, "+%lx\n", ch);
48*e4a6e799Ssimonb 	return 1;
49*e4a6e799Ssimonb }
50*e4a6e799Ssimonb 
log_screen(const byte * img,int len)51*e4a6e799Ssimonb int log_screen(const byte* img, int len) {
52*e4a6e799Ssimonb 	if (logf == NULL) return 0;
53*e4a6e799Ssimonb 	fwrite("=", 1, 1, logf);
54*e4a6e799Ssimonb 	fwrite(img, 1, len, logf);
55*e4a6e799Ssimonb 	fwrite("\n", 1, 1, logf);
56*e4a6e799Ssimonb 	return 1;
57*e4a6e799Ssimonb }
58*e4a6e799Ssimonb 
59*e4a6e799Ssimonb #if 0
60*e4a6e799Ssimonb int log_debug(char const* fmt, ...) {
61*e4a6e799Ssimonb 	va_list ap;
62*e4a6e799Ssimonb 	va_start(ap, fmt);
63*e4a6e799Ssimonb 	fprintf(logf, "D ");
64*e4a6e799Ssimonb 	vfprintf(logf, fmt, ap);
65*e4a6e799Ssimonb 	fprintf(logf, "\n");
66*e4a6e799Ssimonb 	va_end(ap);
67*e4a6e799Ssimonb 	fflush(logf);
68*e4a6e799Ssimonb 	return 1;
69*e4a6e799Ssimonb }
70*e4a6e799Ssimonb #endif
71*e4a6e799Ssimonb 
log_command(char * const * argv,int argc,const char * textfile)72*e4a6e799Ssimonb int log_command(char* const* argv, int argc, const char* textfile) {
73*e4a6e799Ssimonb 	if (logf == NULL) return 0;
74*e4a6e799Ssimonb 	fprintf(logf, "A");
75*e4a6e799Ssimonb 	int a;
76*e4a6e799Ssimonb 	for (a = 1; a < argc; ++a)
77*e4a6e799Ssimonb 		fprintf(logf, " \"%s\"", (a < argc-1) ? argv[a] : textfile);
78*e4a6e799Ssimonb 	fprintf(logf, "\n");
79*e4a6e799Ssimonb 	return 1;
80*e4a6e799Ssimonb }
81*e4a6e799Ssimonb 
log_textfile(const char * textfile)82*e4a6e799Ssimonb int log_textfile(const char* textfile) {
83*e4a6e799Ssimonb 	if (logf == NULL) return 0;
84*e4a6e799Ssimonb 	struct stat st;
85*e4a6e799Ssimonb 	if (stat(textfile, &st) < 0) {
86*e4a6e799Ssimonb 		fprintf(stderr, "cannot stat %s\n", textfile);
87*e4a6e799Ssimonb 		return 0;
88*e4a6e799Ssimonb 	}
89*e4a6e799Ssimonb 	FILE* fd = fopen(textfile, "r");
90*e4a6e799Ssimonb 	if (fd == NULL) {
91*e4a6e799Ssimonb 		fprintf(stderr, "cannot open %s\n", textfile);
92*e4a6e799Ssimonb 		return 0;
93*e4a6e799Ssimonb 	}
94*e4a6e799Ssimonb 	fprintf(logf, "F \"%s\" %ld\n", textfile, (long) st.st_size);
95*e4a6e799Ssimonb 	off_t nread = 0;
96*e4a6e799Ssimonb 	while (nread < st.st_size) {
97*e4a6e799Ssimonb 		char buf[4096];
98*e4a6e799Ssimonb 		size_t n = fread(buf, 1, sizeof(buf), fd);
99*e4a6e799Ssimonb 		if (n <= 0) {
100*e4a6e799Ssimonb 			fprintf(stderr, "read only %ld/%ld from %s\n", (long) nread, (long) st.st_size, textfile);
101*e4a6e799Ssimonb 			fclose(fd);
102*e4a6e799Ssimonb 			return 0;
103*e4a6e799Ssimonb 		}
104*e4a6e799Ssimonb 		nread += n;
105*e4a6e799Ssimonb 		fwrite(buf, 1, n, logf);
106*e4a6e799Ssimonb 	}
107*e4a6e799Ssimonb 	fclose(fd);
108*e4a6e799Ssimonb 	return 1;
109*e4a6e799Ssimonb }
110*e4a6e799Ssimonb 
log_test_header(char * const * argv,int argc,const char * textfile)111*e4a6e799Ssimonb int log_test_header(char* const* argv, int argc, const char* textfile) {
112*e4a6e799Ssimonb 	if (logf == NULL) return 0;
113*e4a6e799Ssimonb 	fprintf(logf, "T \"%s\"\n", textfile);
114*e4a6e799Ssimonb 	if (!log_command(argv, argc, textfile))
115*e4a6e799Ssimonb 		return 0;
116*e4a6e799Ssimonb 	if (!log_textfile(textfile))
117*e4a6e799Ssimonb 		return 0;
118*e4a6e799Ssimonb 	fprintf(logf, "R\n");
119*e4a6e799Ssimonb 	return 1;
120*e4a6e799Ssimonb }
121*e4a6e799Ssimonb 
log_test_footer(void)122*e4a6e799Ssimonb int log_test_footer(void) {
123*e4a6e799Ssimonb 	if (logf == NULL) return 0;
124*e4a6e799Ssimonb 	fprintf(logf, "Q\n");
125*e4a6e799Ssimonb 	return 1;
126*e4a6e799Ssimonb }
127