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
|
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "vt.h"
#include "misc.h"
#include "export.h"
extern struct export_module export_txt;
extern struct export_module export_ansi;
extern struct export_module export_html;
extern struct export_module export_png;
extern struct export_module export_ppm;
struct export_module *modules[] =
{
&export_txt,
&export_ansi,
&export_html,
&export_ppm,
#ifdef WITH_PNG
&export_png,
#endif
0
};
static char *glbl_opts[] =
{
"reveal", // show hidden text
"hide", // don't show hidden text (default)
0
};
static char errbuf[64];
void export_error(char *str, ...)
{
va_list args;
va_start(args, str);
vsnprintf(errbuf, sizeof(errbuf)-1, str, args);
}
char * export_errstr(void)
{
return errbuf;
}
static int find_opt(char **opts, char *opt, char *arg)
{
int err = 0;
char buf[256];
char **oo, *o, *a;
if (oo = opts)
while (o = *oo++)
{
if (a = strchr(o, '='))
{
a = buf + (a - o);
o = strcpy(buf, o);
*a++ = 0;
}
if (strcasecmp(o, opt) == 0)
{
if ((a != 0) == (arg != 0))
return oo - opts;
err = -1;
}
}
return err;
}
struct export * export_open(char *fmt)
{
struct export_module **eem, *em;
struct export *e;
char *opt, *optend, *optarg;
int opti;
if (fmt = strdup(fmt))
{
if (opt = strchr(fmt, ','))
*opt++ = 0;
for (eem = modules; em = *eem; eem++)
if (strcasecmp(em->fmt_name, fmt) == 0)
break;
if (em)
{
if (e = malloc(sizeof(*e) + em->local_size))
{
e->mod = em;
e->fmt_str = fmt;
e->reveal = 0;
memset(e + 1, 0, em->local_size);
if (not em->open || em->open(e) == 0)
{
for (; opt; opt = optend)
{
if (optend = strchr(opt, ','))
*optend++ = 0;
if (not *opt)
continue;
if (optarg = strchr(opt, '='))
*optarg++ = 0;
if ((opti = find_opt(glbl_opts, opt, optarg)) > 0)
{
if (opti == 1) // reveal
e->reveal = 1;
else if (opti == 2) // hide
e->reveal = 0;
}
else if (opti == 0 &&
(opti = find_opt(em->options, opt, optarg)) > 0)
{
if (em->option(e, opti, optarg))
break;
}
else
{
if (opti == 0)
export_error("%s: unknown option", opt);
else if (optarg)
export_error("%s: takes no arg", opt);
else
export_error("%s: missing arg", opt);
break;
}
}
if (opt == 0)
return e;
if (em->close)
em->close(e);
}
free(e);
}
else
export_error("out of memory");
}
else
export_error("unknown format: %s", fmt);
free(fmt);
}
else
export_error("out of memory");
return 0;
}
void export_close(struct export *e)
{
if (e->mod->close)
e->mod->close(e);
free(e->fmt_str);
free(e);
}
static char * hexnum(char *buf, unsigned int num)
{
char *p = buf + 5;
num &= 0xffff;
*--p = 0;
do
{
*--p = "0123456789abcdef"[num % 16];
num /= 16;
} while (num);
return p;
}
static char * adjust(char *p, char *str, char fill, int width)
{
int l = width - strlen(str);
while (l-- > 0)
*p++ = fill;
while (*p = *str++)
p++;
return p;
}
char * export_mkname(struct export *e, char *fmt, struct vt_page *vtp, char *usr)
{
char bbuf[1024];
char *p = bbuf;
while (*p = *fmt++)
if (*p++ == '%')
{
char buf[32], buf2[32];
int width = 0;
p--;
while (*fmt >= '0' && *fmt <= '9')
width = width*10 + *fmt++ - '0';
switch (*fmt++)
{
case '%':
p = adjust(p, "%", '%', width);
break;
case 'e': // extension
p = adjust(p, e->mod->extension, '.', width);
break;
case 'p': // pageno[.subno]
if (vtp->subno)
p = adjust(p,strcat(strcat(hexnum(buf, vtp->pgno),
"."), hexnum(buf2, vtp->subno)), ' ', width);
else
p = adjust(p, hexnum(buf, vtp->pgno), ' ', width);
break;
case 'S': // subno
p = adjust(p, hexnum(buf, vtp->subno), '0', width);
break;
case 'P': // pgno
p = adjust(p, hexnum(buf, vtp->pgno), '0', width);
break;
case 's': // user strin
p = adjust(p, usr, ' ', width);
break;
//TODO: add date, channel name, ...
}
}
p = strdup(bbuf);
if (not p)
export_error("out of memory");
return p;
}
static void fmt_page(struct export *e, struct fmt_page *pg, struct vt_page *vtp)
{
char buf[16];
int x, y;
u8 *p = vtp->data[0];
pg->dbl = 0;
sprintf(buf, "\2%x.%02x\7", vtp->pgno, vtp->subno & 0xff);
for (y = 0; y < H; y++)
{
struct fmt_char c;
int last_ch = ' ';
int dbl = 0, hold = 0;
c.fg = 7;
c.bg = 0;
c.attr = 0;
for (x = 0; x < W; ++x)
{
c.ch = *p++;
if (y == 0 && x < 8)
c.ch = buf[x];
switch (c.ch)
{
case 0x00 ... 0x07: /* alpha + fg color */
c.fg = c.ch & 7;
c.attr &= ~(EA_GRAPHIC | EA_CONCEALED);
goto ctrl;
case 0x08: /* flash */
c.attr |= EA_BLINK;
goto ctrl;
case 0x09: /* steady */
c.attr &= ~EA_BLINK;
goto ctrl;
case 0x0a: /* end box */
case 0x0b: /* start box */
goto ctrl;
case 0x0c: /* normal height */
c.attr &= EA_DOUBLE;
goto ctrl;
case 0x0d: /* double height */
if (y < H-2) /* ignored on last 2 lines */
{
c.attr |= EA_DOUBLE;
dbl = 1;
}
goto ctrl;
case 0x10 ... 0x17: /* gfx + fg color */
c.fg = c.ch & 7;
c.attr |= EA_GRAPHIC;
c.attr &= ~EA_CONCEALED;
goto ctrl;
case 0x18: /* conceal */
c.attr |= EA_CONCEALED;
goto ctrl;
case 0x19: /* contiguous gfx */
c.attr &= ~EA_SEPARATED;
goto ctrl;
case 0x1a: /* separate gfx */
c.attr |= EA_SEPARATED;
goto ctrl;
case 0x1c: /* black bg */
c.bg = 0;
goto ctrl;
case 0x1d: /* new bg */
c.bg = c.fg;
goto ctrl;
case 0x1e: /* hold gfx */
hold = 1;
goto ctrl;
case 0x1f: /* release gfx */
hold = 0;
goto ctrl;
case 0x0e: /* SO */
case 0x0f: /* SI */
case 0x1b: /* ESC */
c.ch = ' ';
break;
ctrl:
c.ch = ' ';
if (hold && (c.attr & EA_GRAPHIC))
c.ch = last_ch;
break;
}
if (c.attr & EA_GRAPHIC)
if ((c.ch & 0xa0) == 0x20)
{
last_ch = c.ch;
c.ch += (c.ch & 0x40) ? 32 : -32;
}
if (c.attr & EA_CONCEALED)
if (not e->reveal)
c.ch = ' ';
pg->data[y][x] = c;
}
if (dbl)
{
pg->dbl |= 1 << y;
for (x = 0; x < W; ++x)
{
if (~pg->data[y][x].attr & EA_DOUBLE)
pg->data[y][x].attr |= EA_HDOUBLE;
pg->data[y+1][x] = pg->data[y][x];
pg->data[y+1][x].ch = ' ';
}
y++;
p += W;
}
}
pg->hid = pg->dbl << 1;
}
int export(struct export *e, struct vt_page *vtp, char *name)
{
struct fmt_page pg[1];
fmt_page(e, pg, vtp);
return e->mod->output(e, name, pg);
}
|