aboutsummaryrefslogtreecommitdiffstats
path: root/util/alevt/alevt-cap.c
blob: 9e0c3bd1a92ba2ca026ffeec446f31764b48a4aa (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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <locale.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"

static volatile int timed_out = 0;
static char *channel;
char *outfile = "";
u_int16_t sid;


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"
	    "    -cs -charset\t\tlatin-1\n"
	    "    <latin-1/2/koi8-r/iso8859-7>\n"
	    "    -f -format <fmt,options>\tascii\n"
	    "    -f help -format help\n"
	    "    -h -help\n"
	    "    -n -name <filename>\t\tttext-%%s.%%e\n"
	    "    -s -sid <sid>\t\t(none;dvb only)\n"
	    "    -to -timeout <secs>\t\t(none)\n"
	    "    -t -ttpid <ttpid>\t\t(none;dvb only)\n"
	    "    -v -vbi <vbidev>\t\t/dev/vbi\n"
		"                 \t\t/dev/vbi0\n"
		"                 \t\t/dev/video0\n"
		"                 \t\t/dev/dvb/adapter0/demux0\n"
	    "\n"
	    "  ppp.ss stands for a page number and an\n"
	    "  optional subpage number (ie 123.4).\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"
	    "Example: -format ansi,reveal,bg=none\n"
	    "\n"
	);
    exit(0);
}


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[] = {
	{ "-charset", "-cs", 1 },
	{ "-format", "-f", 1 },
	{ "-help", "-h", 0 },
	{ "-name", "-n", 1 },
	{ "-sid", "-s", 1 },
	{ "-timeout", "-to", 1 },
	{ "-ttpid", "-t", 1 },
	{ "-vbi", "-v", 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)
		    {
			*req->vtp = *vtp;
			dl_insert_last(reqs + 1, dl_remove(req->node));
	    }
	}
    }
}


int main(int argc, char **argv)
{
    char *vbi_name = NULL;
    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
    int ttpid = -1;

    setlocale (LC_CTYPE, "");
    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: // charset
		if (streq(arg, "latin-1") || streq(arg, "1"))
		    latin1 = 1;
		else if (streq(arg, "latin-2") || streq(arg, "2"))
		    latin1 = 0;
		else if (streq(arg, "koi8-r") || streq(arg, "koi"))
		    latin1 = KOI8;
		else if (streq(arg, "iso8859-7") || streq(arg, "el"))
		    latin1 = GREEK;
		else
		    fatal("bad charset (not latin-1/2/koi8-r/iso8859-7)");
		break;
	    case 2: // format
		if (streq(arg, "help") || streq(arg, "?") || streq(arg, "list"))
		    exp_help(stdout);
		out_fmt = arg;
		fmt = 0;
		break;
	    case 3: // help
		usage(stdout, 0);
		break;
	    case 4: // name
		fname = arg;
		break;
	    case 5: // timeout
		timeout = strtol(arg, 0, 10);
		if (timeout < 1 || timeout > 999999)
		fatal("bad timeout value", timeout);
		break;
	    case 6: // service id
		sid = strtoul(arg, NULL, 0);
		break;
	    case 7: // teletext pid
		ttpid = strtoul(arg, NULL, 0);
		break;
	    case 8: // vbi
		vbi_name = arg;
		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, channel, outfile, sid, ttpid)))
	fatal("cannot open %s", vbi_name);
    vbi_add_handler(vbi, event, reqs); // register event 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);
}