aboutsummaryrefslogtreecommitdiffstats
path: root/log.c
blob: b831b6fbe3d2317875bf8cc3b1aa726a732a2e12 (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
/*
 *  log.c --  code for #movie / #capture backbuffering
 *            and code for reprint-on-prompt
 *
 *  Copyright (C) 1998 by Massimiliano Ghilardi
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "defines.h"
#include "main.h"
#include "log.h"
#include "tty.h"
#include "list.h"
#include "utils.h"

vtime movie_last;		     /* time movie_file was last written */
FILE *capturefile = (FILE *)NULL;    /* capture file or NULL */
FILE *moviefile = (FILE *)NULL;	     /* movie file or NULL */
FILE *recordfile = (FILE *)NULL;     /* record file or NULL */


static char *datalist;		/* circular string list */
static int datastart = 0;	/* index to first string start */
static int dataend   = 0;	/* index one past last string end */
static int datasize  = 0;	/* size of circular string list */

#define DATALEFT (datastart > dataend ? datastart - dataend - 2 : datastart ? \
		MAX2(datastart - 1, datasize - dataend - 1) : datasize - dataend - 1)

typedef struct logentry {
    enum linetype kind;
    long msecs;			/* millisecs to sleep if kind == SLEEP */
    char *line;			/* pointer to string in "datalist" circular buffer */
} logentry;

logentry *loglist;		/* circular (pointer to string) list */

static int logstart = 0;	/* index to first loglist used */
static int logend   = 0;	/* index one past last loglist used */
static int logsize  = 0;	/* size of circular (pointer to string) list */

#define LOGFULL (logend == (logstart ? logstart - 1 : logsize - 1))

static char *names[] = { NULL, "line", "prompt", "sleep" };

/*
 * flush a single buffer line
 */
static void log_flushline(int i)
{
    if (capturefile)
	fprintf(capturefile, "%s%s",
		loglist[i].line, loglist[i].kind == LINE ? "\n" : "");
    if (moviefile) {
	if (loglist[i].msecs)
	    fprintf(moviefile, "%s %ld\n",
		    names[SLEEP], loglist[i].msecs);
	fprintf(moviefile, "%s %s\n",
		names[loglist[i].kind], loglist[i].line);
    }
}

/*
 * remove the oldest (first) line from the buffer
 */
static void log_clearline(void)
{
    int next;

    if (logstart == logend)
	return;
    log_flushline(logstart);

    next = (logstart + 1) % logsize;
    if (next == logend)
	datastart = dataend = logstart = logend = 0;
    else
	datastart = loglist[next].line - datalist, logstart = next;
}

/*
 * remove an initial SLEEP from the buffer
 */
void log_clearsleep(void)
{
    if (logstart != logend)
	loglist[logstart].msecs = 0;
}

/*
 * flush the buffer
 */
void log_flush(void)
{
    int i = logstart;
    while (i != logend) {
	log_flushline(i);
	if (++i == logsize)
	    i = 0;
    }
    datastart = dataend = logstart = logend = 0;
}

int log_getsize(void)
{
    return datasize;
}

static void log_reset(void)
{
    if (datasize) {
	if (datalist) free(datalist);
	if (loglist)  free(loglist);
	loglist = NULL;
	datalist = NULL;
	logsize = datasize = 0;
    }
}


void log_resize(int newsize)
{
    if (newsize && newsize < 1000) {
	PRINTF("#buffer size must be 0 (zero) or >= 1000\n");
	return;
    }

    if (newsize == datasize)
	return;

    log_flush();
    log_reset();
    if (newsize) {
	datalist = (char *)malloc(newsize);
	if (!datalist) { log_reset(); errmsg("malloc"); return; }

	loglist = (logentry *)malloc(newsize/16*sizeof(logentry));
	if (!loglist) { log_reset(); errmsg("malloc"); return; }

	datasize = newsize;
	logsize = newsize / 16;
    }
    if (opt_info) {
	PRINTF("#buffer resized to %d bytes%s\n", newsize, newsize ? "" : " (disabled)");
    }
}

/*
 * add a single line to the buffer
 */
static void log_writeline(char *line, int len, int kind, long msecs)
{
    int dst;

    if (++len >= datasize) {
	PRINTF("#line too long, discarded from movie/capture buffer\n");
	return;
    }
    while (LOGFULL || DATALEFT < len)
	log_clearline();
    /* ok, now we know there IS enough space */

    if (datastart >= dataend /* is == iff loglist is empty */
	|| datasize - dataend > len)
	dst = dataend;
    else
	dst = 0;

    memcpy(loglist[logend].line = datalist + dst, line, len - 1);
    datalist[dst + len - 1] = '\0';

    loglist[logend].kind = kind;
    loglist[logend].msecs = msecs;

    if ((dataend = dst + len) == datasize)
	dataend = 0;

    if (++logend == logsize)
	logend = 0;
}

/*
 * write to #capture / #movie buffer
 */
void log_write(char *str, int len, int newline)
{
    char *next;
    long diff;
    int i, last = 0;

    if (!datasize && !moviefile && !capturefile)
	return;

    update_now();
    diff = diff_vtime(&now, &movie_last);
    movie_last = now;

    do {
	if ((next = memchr(str, '\n', len))) {
	    i = next - str;
            newline = 1;
        } else {
	    i = len;
            last = 1;
	}

	if (datasize)
	    log_writeline(str, i, last && !newline ? PROMPT : LINE, diff);
	else {
	    if (moviefile) {
		if (diff)
		    fprintf(moviefile, "%s %ld\n",
			names[SLEEP], diff);
		fprintf(moviefile, "%s %.*s\n",
			names[last && !newline ? PROMPT : LINE], i, str);
	    }
	    if (capturefile) {
                fwrite(str, 1, i, capturefile);
                if (newline) {
                    const char nl[1] = "\n";
                    fwrite(nl, 1, 1, capturefile);
                }
            }
	}
	diff = 0;
	if (next) {
	    len -= next + 1 - str;
	    str = next + 1;
	}
    } while (next && len > 0);
}

static char reprintlist[BUFSIZE];	/* circular string list */
static int  reprintstart = 0;		/* index to first string start */
static int  reprintend   = 0;		/* index one past last string end */
static int  reprintsize  = BUFSIZE;	/* size of circular string list */

#define REPRINTLEFT (reprintstart > reprintend ? reprintstart - reprintend - 1 : reprintstart ? \
			MAX2(reprintstart - 1, reprintsize - reprintend - 1) : reprintsize - reprintend - 1)

static char *replist[BUFSIZE/8];	/* circular (pointer to string) list */

static int repstart = 0;		/* index to first replist used */
static int repend   = 0;		/* index one past last replist used */
static int repsize  = BUFSIZE/8;	/* size of circular (pointer to string) list */

#define REPFULL (repend == (repstart ? repstart - 1 : repsize - 1))

/*
 * remove the oldest (first) line from reprintlist
 */
static void reprint_clearline(void)
{
    int next;

    if (repstart == repend)
	return;

    next = (repstart + 1) % repsize;
    if (next == repend)
	reprintstart = reprintend = repstart = repend = 0;
    else
	reprintstart = replist[next] - reprintlist, repstart = next;
}

void reprint_clear(void)
{
    reprintstart = reprintend = repstart = repend = 0;
}

/*
 * add a single line to the buffer
 */
void reprint_writeline(char *line)
{
    int len = strlen(line) + 1;
    int dst;

    if (!opt_reprint || (promptlen && prompt_status != -1))
	/*
	 * if prompt is valid, we'll never have to reprint, as we
	 * _already_ printed the command at the right moment
	 */
	return;

    if (len >= reprintsize) {
	PRINTF("#line too long, discarded from prompt reprint buffer\n");
	return;
    }
    while (REPFULL || REPRINTLEFT < len)
	reprint_clearline();
    /* ok, now we know there IS enough space */

    if (reprintstart >= reprintend /* is == iff replist is empty */
	|| reprintsize - reprintend > len)
	dst = reprintend;
    else
	dst = 0;

    memcpy(replist[repend] = reprintlist + dst, line, len - 1);
    reprintlist[dst + len - 1] = '\0';

    if ((reprintend = dst + len) == reprintsize)
	reprintend = 0;

    if (++repend == repsize)
	repend = 0;
}

char *reprint_getline(void)
{
    char *line = NULL;
    if (opt_reprint && repend != repstart)
	line = replist[repstart];
    reprint_clearline();
    return line;
}