summaryrefslogtreecommitdiff
path: root/usr.bin/mg/log.c
blob: 81a93e9282fb56adb36dc86e0e76f2b10f7dcd3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
/*	$OpenBSD: log.c,v 1.10 2019/06/28 13:35:02 deraadt Exp $	*/

/* 
 * This file is in the public domain.
 *
 * Author: Mark Lumsden <mark@showcomplex.com>
 *
 */

/*
 * Record a history of an mg session for temporal debugging.
 * Sometimes pressing a key will set the scene for a bug only visible 
 * dozens of keystrokes later. gdb has its limitations in this scenario.
 *
 * Note this file is not compiled into mg by default, you will need to
 * amend the 'Makefile' for that to happen. Because of this, the code
 * is subject to bit-rot. However, I know myself and others have 
 * written similar functionally often enough, that recording the below 
 * in a code repository could aid the developement efforts of mg, even
 * if it requires a bit of effort to get working. The current code is
 * written in the spirit of debugging (quickly and perhaps not ideal,
 * but it does what is required well enough). Should debugging become
 * more formalised within mg, then I would expect that to change.
 *
 * If you open a file with long lines to run through this debugging 
 * code, you may run into problems with the 1st fprintf statement in
 * in the mglog_lines() function. mg sometimes segvs at a strlen call
 * in fprintf - possibly something to do with the format string?
 * 	"%s%p b^%p f.%p %d %d\t%c|%s\n"
 * When I get time I will look into it. But since my debugging 
 * generally revolves around a file like:
 * 
 * abc
 * def
 * ghk
 *
 * I don't experience this bug. Just note it for future investigation.
 */

#include <sys/queue.h>
#include <sys/stat.h>
#include <ctype.h>
#include <fcntl.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "def.h"
#include "key.h"
#include "kbd.h"
#include "funmap.h"
#include "chrdef.h"

#include "log.h"

char	*mglogfiles_create(char *);
int	 mglog_lines(PF);
int	 mglog_undo(void);
int	 mglog_window(void);
int	 mglog_key(KEYMAP *map);

char		*mglogdir;
extern char	*mglogpath_lines;
extern char	*mglogpath_undo;
extern char	*mglogpath_window;
extern char	*mglogpath_key;
int		 mgloglevel;

int
mglog(PF funct, KEYMAP *map)
{
	if(!mglog_lines(funct))
		ewprintf("Problem logging lines");
	if(!mglog_undo())
		ewprintf("Problem logging undo");
	if(!mglog_window())
		ewprintf("Problem logging window");
	if(!mglog_key(map))
		ewprintf("Problem logging key");

	return (TRUE);
}


int
mglog_key(KEYMAP *map)
{
	struct stat      sb;
	FILE            *fd;
	PF		*pfp;

	if(stat(mglogpath_key, &sb))
		 return (FALSE);
	fd = fopen(mglogpath_key, "a");

	if (ISWORD(*key.k_chars)) {
		if (fprintf(fd, "k_count:%d k_chars:%hd\tchr:%c\t", key.k_count,
		    *key.k_chars, CHARMASK(*key.k_chars)) == -1) {
			fclose(fd);
			return (FALSE);
		}
	} else {
		if (fprintf(fd, "k_count:%d k_chars:%hd\t\t", key.k_count,
		    *key.k_chars) == -1) {
			fclose(fd);
			return (FALSE);
		}
	}
	if (fprintf(fd, "map:%p %d %d %p %hd %hd\n",
	    map,
	    map->map_num,
	    map->map_max,
	    map->map_default,
	    map->map_element->k_base,
	    map->map_element->k_num
	    ) == -1) {
		fclose(fd);
		return (FALSE);
	}
	for (pfp = map->map_element->k_funcp; *pfp != '\0'; pfp++)
		fprintf(fd, "%s ", function_name(*pfp));

	fprintf(fd, "\n\n");
	fclose(fd);
	return (TRUE);
}

int
mglog_window(void)
{
	struct mgwin	*wp;
	struct stat	 sb;
	FILE		*fd;
	int		 i;

	if(stat(mglogpath_window, &sb))
		return (FALSE);
	fd = fopen(mglogpath_window, "a");

	for (wp = wheadp, i = 0; wp != NULL; wp = wp->w_wndp, ++i) {
		if (fprintf(fd,
		    "%d wh%p wlst%p wbfp%p wlp%p wdtp%p wmkp%p wdto%d wmko%d" \
		    " wtpr%d wntr%d wfrm%d wrfl%c wflg%c wwrl%p wdtl%d" \
		    " wmkl%d\n",
		    i,
		    wp,
		    &wp->w_list,
		    wp->w_bufp,
		    wp->w_linep,
		    wp->w_dotp,
		    wp->w_markp,
		    wp->w_doto,
		    wp->w_marko,
		    wp->w_toprow,
		    wp->w_ntrows,
		    wp->w_frame,
		    wp->w_rflag,
		    wp->w_flag,
		    wp->w_wrapline,
		    wp->w_dotline,
		    wp->w_markline) == -1) {
			fclose(fd);
			return (FALSE);
		}
	}
	fclose(fd);
	return (TRUE);
}

int
mglog_undo(void)
{
	struct undo_rec	*rec;
	struct stat	 sb;
	FILE		*fd;
	char		 buf[4096], tmp[1024];
	int      	 num;
	char		*jptr;

	jptr = "^J"; /* :) */

	if(stat(mglogpath_undo, &sb))
		return (FALSE);
	fd = fopen(mglogpath_undo, "a");
	
	/*
	 * From undo_dump()
	 */
	num = 0;
	TAILQ_FOREACH(rec, &curbp->b_undo, next) {
		num++;
		if (fprintf(fd, "%d:\t %s at %d ", num,
		    (rec->type == DELETE) ? "DELETE":
		    (rec->type == DELREG) ? "DELREGION":
		    (rec->type == INSERT) ? "INSERT":
		    (rec->type == BOUNDARY) ? "----" :
		    (rec->type == MODIFIED) ? "MODIFIED": "UNKNOWN",
		    rec->pos) == -1) {
			fclose(fd);
			return (FALSE);
		}
		if (rec->content) {
			(void)strlcat(buf, "\"", sizeof(buf));
			snprintf(tmp, sizeof(tmp), "%.*s",
			    *rec->content == '\n' ? 2 : rec->region.r_size,
			    *rec->content == '\n' ? jptr : rec->content);
			(void)strlcat(buf, tmp, sizeof(buf));
			(void)strlcat(buf, "\"", sizeof(buf));
		}
		snprintf(tmp, sizeof(tmp), " [%d]", rec->region.r_size);
		if (strlcat(buf, tmp, sizeof(buf)) >= sizeof(buf)) {
			dobeep();
			ewprintf("Undo record too large. Aborted.");
			return (FALSE);
		}
		if (fprintf(fd, "%s\n", buf) == -1) {
			fclose(fd);
			return (FALSE);
		}
		tmp[0] = buf[0] = '\0';
	}
	if (fprintf(fd, "\t [end-of-undo]\n\n") == -1) {
		fclose(fd);
		return (FALSE);
	}
	fclose(fd);

	return (TRUE);
}

int
mglog_lines(PF funct)
{
	struct line     *lp;
	struct stat      sb;
	char		*curline, *tmp, o;
	FILE            *fd;
	int		 i;

	i = 0;

	if(stat(mglogpath_lines, &sb))
		return (FALSE);

	fd = fopen(mglogpath_lines, "a");
	if (fprintf(fd, "%s\n", function_name(funct)) == -1) {
		fclose(fd);
		return (FALSE);
	}
	lp = bfirstlp(curbp);

	for(;;) {
		i++;
		curline = " ";
		o = ' ';
		if (i == curwp->w_dotline) {
			curline = ">";
			if (lp->l_used > 0 && curwp->w_doto < lp->l_used)
				o = lp->l_text[curwp->w_doto];
			else
				o = '-';
		}
		if (lp->l_size == 0)
			tmp = " ";
		else
			tmp = lp->l_text;

		/* segv on fprintf below with long lines */
		if (fprintf(fd, "%s%p b^%p f.%p %d %d\t%c|%s\n", curline,
		    lp, lp->l_bp, lp->l_fp,
		    lp->l_size, lp->l_used, o, tmp) == -1) {
			fclose(fd);
			return (FALSE);
		}
		lp = lforw(lp);
		if (lp == curbp->b_headp) {
			if (fprintf(fd, " %p b^%p f.%p [bhead]\n(EOB)\n",
			    lp, lp->l_bp, lp->l_fp) == -1) {
				fclose(fd);
        	                return (FALSE);
			}
			if (fprintf(fd, "lines:raw:%d buf:%d wdot:%d\n\n",
			    i, curbp->b_lines, curwp->w_dotline) == -1) {
				fclose(fd);
        	                return (FALSE);
			}
			break;
		}
	}
	fclose(fd);

	return (TRUE);
}


/*
 * Make sure logging to log files can happen.
 */
int
mgloginit(void)
{
	struct stat	 sb;
	mode_t           dir_mode, f_mode, oumask;
	char		*mglogfile_lines, *mglogfile_undo, *mglogfile_window;
	char		*mglogfile_key;

	mglogdir = "./log/";
	mglogfile_lines = "line.log";
	mglogfile_undo = "undo.log";
	mglogfile_window = "window.log";
	mglogfile_key = "key.log";

	/* 
	 * Change mgloglevel for desired level of logging.
	 * log.h has relevant level info.
	 */
	mgloglevel = 1;

	oumask = umask(0);
	f_mode = 0777& ~oumask;
	dir_mode = f_mode | S_IWUSR | S_IXUSR;

	if(stat(mglogdir, &sb)) {
		if (mkdir(mglogdir, dir_mode) != 0)
			return (FALSE);
		if (chmod(mglogdir, f_mode) == -1)
			return (FALSE);
	}
	mglogpath_lines = mglogfiles_create(mglogfile_lines);
	if (mglogpath_lines == NULL)
		return (FALSE);
	mglogpath_undo = mglogfiles_create(mglogfile_undo);
	if (mglogpath_undo == NULL)
		return (FALSE);
	mglogpath_window = mglogfiles_create(mglogfile_window);
	if (mglogpath_window == NULL)
		return (FALSE);
	mglogpath_key = mglogfiles_create(mglogfile_key);
	if (mglogpath_key == NULL)
		return (FALSE);

	return (TRUE);
}	


char *
mglogfiles_create(char *mglogfile)
{
	struct stat	 sb;
	char		 tmp[20], *tmp2;
	int     	 fd;

	if (strlcpy(tmp, mglogdir, sizeof(tmp)) >
	    sizeof(tmp))
		return (NULL);
	if (strlcat(tmp, mglogfile, sizeof(tmp)) >
	    sizeof(tmp))
		return (NULL);
	if ((tmp2 = strndup(tmp, 20)) == NULL)
		return (NULL);

	if(stat(tmp2, &sb))
		fd = open(tmp2, O_RDWR | O_CREAT | O_TRUNC, 0644);
	else
		fd = open(tmp2, O_RDWR | O_TRUNC, 0644);

	if (fd == -1)
		return (NULL);

	close(fd);	

	return (tmp2);
}

/*
 * Template log function.
 */
/*
int
mglog_?(void)
{
	struct stat      sb;
	FILE            *fd;

	if(stat(mglogpath_?, &sb))
	fd = fopen(mglogpath_?, "a");

	if (fprintf(fd, "%?", ??) == -1) {
		fclose(fd);
		return (FALSE);
	}
	fclose(fd);
	return (TRUE);
}
*/