File: alevt-cap.c

package info (click to toggle)
alevt 1%3A1.6.2-5
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 956 kB
  • ctags: 661
  • sloc: ansic: 6,284; makefile: 118; perl: 104; sh: 15
file content (320 lines) | stat: -rw-r--r-- 7,138 bytes parent folder | download | duplicates (3)
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>

#include "vt.h"
#include "misc.h"
#include "fdset.h"
#include "vbi.h"
#include "lang.h"
#include "dllist.h"
#include "export.h"

int debug = 0;
static volatile int timed_out = 0;

struct req
{
    struct dl_node node[1];
    char *name;			// file name
    char *pgno_str;		// the pgno as given on the cmdline
    int pgno, subno;		// decoded pgno
    struct export *export;	// export data
    struct vt_page vtp[1];	// the capture page data
};



static void
usage(FILE *fp, int exitval)
{
    fprintf(fp, "\nUsage: %s [options] ppp[.ss]...\n", prgname);
    fprintf(fp,
	    "\n"
	    "  Valid options:\t\tDefault:\n"
	    "    --help\n"
	    "    --version\n"
	    "    -vbi <vbidev>\t\t/dev/vbi0\n"
	    "    -finetune <-4..4|auto>\t0\n"
	    //"    -oldbttv\t\t\t(for bttv <0.5.20)\n"
	    "    -charset latin-1/2\t\tlatin-1\n"
	    "    -timeout <secs>\t\tnone\n"
	    "    -name <filename>\t\tttext-%%s.%%e\n"
	    "    -format <fmt[,options]>\tascii\n"
	    "    -format help\n"
	    "\n"
	    "  ppp[.ss] stands for a page number and an\n"
	    "  optional subpage number (ie 123.4).  If\n"
	    "  the subpage number is omitted the first\n"
	    "  transmitted subpage is captured.\n"
	    "\n"
	);
    exit(exitval);
}

static void
exp_help(FILE *fp)
{
    struct export_module **ep;
    char **cp, c;

    fprintf(fp,
	    "\nSyntax: -format Name[,Options]\n"
	    "\n"
	    "    Name\tExt.\tOptions\n"
	    "    --------------------------------\n"
	);
    for (ep = modules; *ep; ep++)
    {
	fprintf(fp, "    %-7s\t.%-4s", (*ep)->fmt_name, (*ep)->extension);
	for (c = '\t', cp = (*ep)->options; cp && *cp; cp++, c = ',')
	    fprintf(fp, "%c%s", c, *cp);
	fprintf(fp, "\n");
    }
    fprintf(fp,
	    "\n"
	    "Common options: reveal,hide\n"
	    "\n"
	    "Example: -format ansi,reveal,bg=none\n"
	    "\n"
	);
    exit(0);
}


static void
sig_handler(int sig)
{
    if (debug)
	write(2, "*BREAK*\r\n", 9);
    timed_out = 1;
}


static int
arg_pgno(char *p, int *subno)
{
    char *end;
    int pgno;

    *subno = ANY_SUB;
    if (*p)
    {
	pgno = strtol(p, &end, 16);
	if ((*end == ':' || *end == '/' || *end == '.') && end[1])
	    *subno = strtol(end + 1, &end, 16);
	if (*end == 0)
	    if (pgno >= 0x100 && pgno <= 0x899)
		if (*subno == ANY_SUB || (*subno >= 0x00 && *subno <= 0x3f7f))
		    return pgno;
    }
    fatal("%s: invalid page number", p);
}


static int
option(int argc, char **argv, int *ind, char **arg)
{
    static struct { char *nam, *altnam; int arg; } opts[] = {
	{ "--help", "-h", 0 },
	{ "--version", "-v", 0 },
	{ "-copyright", "-", 0 },
	{ "-debug", "--debug", 0 },
	{ "-vbi", "-dev", 1 },
	{ "-newbttv", "-new", 0 },
	{ "-oldbttv", "-old", 0 },
	{ "-finetune", "-f", 1 },
	{ "-charset", "-latin", 1 },
	{ "-format", "-fmt", 1 },
	{ "-name", "-o", 1 },
	{ "-timeout", "-t", 1 },
    };
    int i;

    if (*ind >= argc)
	return 0;

    *arg = argv[(*ind)++];
    for (i = 0; i < NELEM(opts); ++i)
	if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
	{
	    if (opts[i].arg)
		if (*ind < argc)
		    *arg = argv[(*ind)++];
		else
		    fatal("option %s requires an argument", *arg);
	    return i+1;
	}

    if (**arg == '-')
    {
	fatal("%s: invalid option", *arg);
	usage(stderr, 2);
    }

    return -1;
}



static void
event(struct dl_head *reqs, struct vt_event *ev)
{
    struct req *req, *nxt;

    switch (ev->type)
    {
	case EV_PAGE:	// new page
	{
	    struct vt_page *vtp = ev->p1;

	    for (req = PTR reqs->first; nxt = PTR req->node->next; req = nxt)
		if (req->pgno == vtp->pgno)
		    if (req->subno == ANY_SUB || req->subno == vtp->subno)
		    {
			if (debug)
			    printf("captured page %x.%02x\n", vtp->pgno,
								vtp->subno);
			*req->vtp = *vtp;
			dl_insert_last(reqs + 1, dl_remove(req->node));
			// the same page may be there in different formats.
			// so, don't break.
			//break;
		    }
	}
    }
}



int
main(int argc, char **argv)
{
    char *vbi_name = "/dev/vbi0";
    int fine_tune = 1; // auto = 999;
    int newbttv = -1;
    int timeout = 0;
    char *fname = "ttext-%s.%e";
    char *out_fmt = "ascii";
    struct export *fmt = 0;
    int opt, ind;
    char *arg;
    struct vbi *vbi;
    struct req *req;
    struct dl_head reqs[2];	// simple linear lists of requests & captures

    setprgname(argv[0]);

    fdset_init(fds);
    dl_init(reqs);	// the requests
    dl_init(reqs+1);	// the captured pages

    ind = 1;
    while (opt = option(argc, argv, &ind, &arg))
	switch (opt)
	{
	    case 1:	// help
		usage(stdout, 0);
		break;
	    case 2:	// version
		printf("AleVT Version "VERSION"\n");
		exit(0);
	    case 3:	// copyright
		printf("Copyright 2000 by E. Toernig, froese@gmx.de\n");
		exit(0);
	    case 4:	// debug
		debug++;
		break;
	    case 5:	// vbi
		vbi_name = arg;
		break;
	    case 6:	// newbttv
		newbttv = 1;
		break;
	    case 7:	// oldbttv
		newbttv = 0;
		break;
	    case 8:	// finetune
		if (streq(arg, "auto"))
		    fine_tune = 999;
		else
		    fine_tune = strtol(arg, 0, 10);
		break;
            case 9:    // charset
		if (streq(arg, "latin-1") || streq(arg, "1"))
		    latin1 = 1;
		else if (streq(arg, "latin-2") || streq(arg, "2"))
		    latin1 = 0;
		else
		    fatal("bad charset (not latin-1/2)");
		break;
	    case 10:	// format
		if (streq(arg, "help") || streq(arg, "?") || streq(arg, "list"))
		    exp_help(stdout);
		out_fmt = arg;
		fmt = 0;
		break;
	    case 11:	// name
		fname = arg;
		break;
	    case 12:	// timeout
		timeout = strtol(arg, 0, 10);
		if (timeout < 1 || timeout > 999999)
		    fatal("bad timeout value", timeout);
		break;
	    case -1:	// non-option arg
		if (not fmt)
		    fmt = export_open(out_fmt);
		if (not fmt)
		    fatal("%s", export_errstr());
		if (not(req = malloc(sizeof(*req))))
		    out_of_mem(sizeof(*req));
		req->name = fname;
		req->pgno_str = arg;
		req->pgno = arg_pgno(arg, &req->subno);
		req->export = fmt;
		dl_insert_last(reqs, req->node);
		break;
	}

    if (dl_empty(reqs))
	fatal("no pages requested");

    // setup device
    if (not(vbi = vbi_open(vbi_name, 0, fine_tune, newbttv)))
	fatal("cannot open %s", vbi_name);
    vbi_add_handler(vbi, event, reqs); // register event handler

    signal(SIGUSR1, sig_handler);
    signal(SIGALRM, sig_handler);
    if (timeout)
	alarm(timeout);

    // capture pages (moves requests from reqs[0] to reqs[1])
    while (not dl_empty(reqs) && not timed_out)
	if (fdset_select(fds, 30000) == 0)	// 30sec select time out
	{
	    error("no signal.");
	    break;
	}

    alarm(0);
    vbi_del_handler(vbi, event, reqs);
    vbi_close(vbi);
    
    if (not dl_empty(reqs))
	error("capture aborted.  some pages are missing.");

    for (req = PTR reqs[1].first; req->node->next; req = PTR req->node->next)
    {
	fname = export_mkname(req->export, req->name, req->vtp, req->pgno_str);
	if (not fname || export(req->export, req->vtp, fname))
	    error("error saving page %s: %s", req->pgno_str, export_errstr());
	if (fname)
	    free(fname);
    }

    exit(dl_empty(reqs) ? 0 : 1);
}