~ubuntu-branches/ubuntu/gutsy/alevt/gutsy

« back to all changes in this revision

Viewing changes to alevt-cap.c

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Schoepf
  • Date: 2002-03-17 15:09:36 UTC
  • Revision ID: james.westby@ubuntu.com-20020317150936-yglzziwcc0luz55k
Tags: upstream-1.6.0
Import upstream version 1.6.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <stdio.h>
 
2
#include <stdlib.h>
 
3
#include <signal.h>
 
4
 
 
5
#include "vt.h"
 
6
#include "misc.h"
 
7
#include "fdset.h"
 
8
#include "vbi.h"
 
9
#include "lang.h"
 
10
#include "dllist.h"
 
11
#include "export.h"
 
12
 
 
13
int debug = 0;
 
14
static volatile int timed_out = 0;
 
15
 
 
16
struct req
 
17
{
 
18
    struct dl_node node[1];
 
19
    char *name;                 // file name
 
20
    char *pgno_str;             // the pgno as given on the cmdline
 
21
    int pgno, subno;            // decoded pgno
 
22
    struct export *export;      // export data
 
23
    struct vt_page vtp[1];      // the capture page data
 
24
};
 
25
 
 
26
 
 
27
 
 
28
static void
 
29
usage(FILE *fp, int exitval)
 
30
{
 
31
    fprintf(fp, "\nUsage: %s [options] ppp[.ss]...\n", prgname);
 
32
    fprintf(fp,
 
33
            "\n"
 
34
            "  Valid options:\t\tDefault:\n"
 
35
            "    --help\n"
 
36
            "    --version\n"
 
37
            "    -vbi <vbidev>\t\t/dev/vbi\n"
 
38
            "    -finetune <-4..4|auto>\t0\n"
 
39
            //"    -oldbttv\t\t\t(for bttv <0.5.20)\n"
 
40
            "    -charset latin-1/2\t\tlatin-1\n"
 
41
            "    -timeout <secs>\t\tnone\n"
 
42
            "    -name <filename>\t\tttext-%%s.%%e\n"
 
43
            "    -format <fmt[,options]>\tascii\n"
 
44
            "    -format help\n"
 
45
            "\n"
 
46
            "  ppp[.ss] stands for a page number and an\n"
 
47
            "  optional subpage number (ie 123.4).  If\n"
 
48
            "  the subpage number is omitted the first\n"
 
49
            "  transmitted subpage is captured.\n"
 
50
            "\n"
 
51
        );
 
52
    exit(exitval);
 
53
}
 
54
 
 
55
static void
 
56
exp_help(FILE *fp)
 
57
{
 
58
    struct export_module **ep;
 
59
    char **cp, c;
 
60
 
 
61
    fprintf(fp,
 
62
            "\nSyntax: -format Name[,Options]\n"
 
63
            "\n"
 
64
            "    Name\tExt.\tOptions\n"
 
65
            "    --------------------------------\n"
 
66
        );
 
67
    for (ep = modules; *ep; ep++)
 
68
    {
 
69
        fprintf(fp, "    %-7s\t.%-4s", (*ep)->fmt_name, (*ep)->extension);
 
70
        for (c = '\t', cp = (*ep)->options; cp && *cp; cp++, c = ',')
 
71
            fprintf(fp, "%c%s", c, *cp);
 
72
        fprintf(fp, "\n");
 
73
    }
 
74
    fprintf(fp,
 
75
            "\n"
 
76
            "Common options: reveal,hide\n"
 
77
            "\n"
 
78
            "Example: -format ansi,reveal,bg=none\n"
 
79
            "\n"
 
80
        );
 
81
    exit(0);
 
82
}
 
83
 
 
84
 
 
85
static void
 
86
sig_handler(int sig)
 
87
{
 
88
    if (debug)
 
89
        write(2, "*BREAK*\r\n", 9);
 
90
    timed_out = 1;
 
91
}
 
92
 
 
93
 
 
94
static int
 
95
arg_pgno(char *p, int *subno)
 
96
{
 
97
    char *end;
 
98
    int pgno;
 
99
 
 
100
    *subno = ANY_SUB;
 
101
    if (*p)
 
102
    {
 
103
        pgno = strtol(p, &end, 16);
 
104
        if ((*end == ':' || *end == '/' || *end == '.') && end[1])
 
105
            *subno = strtol(end + 1, &end, 16);
 
106
        if (*end == 0)
 
107
            if (pgno >= 0x100 && pgno <= 0x899)
 
108
                if (*subno == ANY_SUB || (*subno >= 0x00 && *subno <= 0x3f7f))
 
109
                    return pgno;
 
110
    }
 
111
    fatal("%s: invalid page number", p);
 
112
}
 
113
 
 
114
 
 
115
static int
 
116
option(int argc, char **argv, int *ind, char **arg)
 
117
{
 
118
    static struct { char *nam, *altnam; int arg; } opts[] = {
 
119
        { "--help", "-h", 0 },
 
120
        { "--version", "-v", 0 },
 
121
        { "-copyright", "-�", 0 },
 
122
        { "-debug", "--debug", 0 },
 
123
        { "-vbi", "-dev", 1 },
 
124
        { "-newbttv", "-new", 0 },
 
125
        { "-oldbttv", "-old", 0 },
 
126
        { "-finetune", "-f", 1 },
 
127
        { "-charset", "-latin", 1 },
 
128
        { "-format", "-fmt", 1 },
 
129
        { "-name", "-o", 1 },
 
130
        { "-timeout", "-t", 1 },
 
131
    };
 
132
    int i;
 
133
 
 
134
    if (*ind >= argc)
 
135
        return 0;
 
136
 
 
137
    *arg = argv[(*ind)++];
 
138
    for (i = 0; i < NELEM(opts); ++i)
 
139
        if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
 
140
        {
 
141
            if (opts[i].arg)
 
142
                if (*ind < argc)
 
143
                    *arg = argv[(*ind)++];
 
144
                else
 
145
                    fatal("option %s requires an argument", *arg);
 
146
            return i+1;
 
147
        }
 
148
 
 
149
    if (**arg == '-')
 
150
    {
 
151
        fatal("%s: invalid option", *arg);
 
152
        usage(stderr, 2);
 
153
    }
 
154
 
 
155
    return -1;
 
156
}
 
157
 
 
158
 
 
159
 
 
160
static void
 
161
event(struct dl_head *reqs, struct vt_event *ev)
 
162
{
 
163
    struct req *req, *nxt;
 
164
 
 
165
    switch (ev->type)
 
166
    {
 
167
        case EV_PAGE:   // new page
 
168
        {
 
169
            struct vt_page *vtp = ev->p1;
 
170
 
 
171
            for (req = $ reqs->first; nxt = $ req->node->next; req = nxt)
 
172
                if (req->pgno == vtp->pgno)
 
173
                    if (req->subno == ANY_SUB || req->subno == vtp->subno)
 
174
                    {
 
175
                        if (debug)
 
176
                            printf("captured page %x.%02x\n", vtp->pgno,
 
177
                                                                vtp->subno);
 
178
                        *req->vtp = *vtp;
 
179
                        dl_insert_last(reqs + 1, dl_remove(req->node));
 
180
                        // the same page may be there in different formats.
 
181
                        // so, don't break.
 
182
                        //break;
 
183
                    }
 
184
        }
 
185
    }
 
186
}
 
187
 
 
188
 
 
189
 
 
190
int
 
191
main(int argc, char **argv)
 
192
{
 
193
    char *vbi_name = "/dev/vbi";
 
194
    int fine_tune = 1; // auto = 999;
 
195
    int newbttv = -1;
 
196
    int timeout = 0;
 
197
    char *fname = "ttext-%s.%e";
 
198
    char *out_fmt = "ascii";
 
199
    struct export *fmt = 0;
 
200
    int pgno, subno;
 
201
    int opt, ind;
 
202
    char *arg;
 
203
    struct vbi *vbi;
 
204
    struct req *req;
 
205
    struct dl_head reqs[2];     // simple linear lists of requests & captures
 
206
 
 
207
    setprgname(argv[0]);
 
208
 
 
209
    fdset_init(fds);
 
210
    dl_init(reqs);      // the requests
 
211
    dl_init(reqs+1);    // the captured pages
 
212
 
 
213
    ind = 1;
 
214
    while (opt = option(argc, argv, &ind, &arg))
 
215
        switch (opt)
 
216
        {
 
217
            case 1:     // help
 
218
                usage(stdout, 0);
 
219
                break;
 
220
            case 2:     // version
 
221
                printf("AleVT Version "VERSION"\n");
 
222
                exit(0);
 
223
            case 3:     // copyright
 
224
                printf("Copyright 2000 by E. Toernig, froese@gmx.de\n");
 
225
                exit(0);
 
226
            case 4:     // debug
 
227
                debug++;
 
228
                break;
 
229
            case 5:     // vbi
 
230
                vbi_name = arg;
 
231
                break;
 
232
            case 6:     // newbttv
 
233
                newbttv = 1;
 
234
                break;
 
235
            case 7:     // oldbttv
 
236
                newbttv = 0;
 
237
                break;
 
238
            case 8:     // finetune
 
239
                if (streq(arg, "auto"))
 
240
                    fine_tune = 999;
 
241
                else
 
242
                    fine_tune = strtol(arg, 0, 10);
 
243
                break;
 
244
            case 9:    // charset
 
245
                if (streq(arg, "latin-1") || streq(arg, "1"))
 
246
                    latin1 = 1;
 
247
                else if (streq(arg, "latin-2") || streq(arg, "2"))
 
248
                    latin1 = 0;
 
249
                else
 
250
                    fatal("bad charset (not latin-1/2)");
 
251
                break;
 
252
            case 10:    // format
 
253
                if (streq(arg, "help") || streq(arg, "?") || streq(arg, "list"))
 
254
                    exp_help(stdout);
 
255
                out_fmt = arg;
 
256
                fmt = 0;
 
257
                break;
 
258
            case 11:    // name
 
259
                fname = arg;
 
260
                break;
 
261
            case 12:    // timeout
 
262
                timeout = strtol(arg, 0, 10);
 
263
                if (timeout < 1 || timeout > 999999)
 
264
                    fatal("bad timeout value", timeout);
 
265
                break;
 
266
            case -1:    // non-option arg
 
267
                if (not fmt)
 
268
                    fmt = export_open(out_fmt);
 
269
                if (not fmt)
 
270
                    fatal("%s", export_errstr());
 
271
                if (not(req = malloc(sizeof(*req))))
 
272
                    out_of_mem(sizeof(*req));
 
273
                req->name = fname;
 
274
                req->pgno_str = arg;
 
275
                req->pgno = arg_pgno(arg, &req->subno);
 
276
                req->export = fmt;
 
277
                dl_insert_last(reqs, req->node);
 
278
                break;
 
279
        }
 
280
 
 
281
    if (dl_empty(reqs))
 
282
        fatal("no pages requested");
 
283
 
 
284
    // setup device
 
285
    if (not(vbi = vbi_open(vbi_name, 0, fine_tune, newbttv)))
 
286
        fatal("cannot open %s", vbi_name);
 
287
    vbi_add_handler(vbi, event, reqs); // register event handler
 
288
 
 
289
    signal(SIGUSR1, sig_handler);
 
290
    signal(SIGALRM, sig_handler);
 
291
    if (timeout)
 
292
        alarm(timeout);
 
293
 
 
294
    // capture pages (moves requests from reqs[0] to reqs[1])
 
295
    while (not dl_empty(reqs) && not timed_out)
 
296
        if (fdset_select(fds, 30000) == 0)      // 30sec select time out
 
297
        {
 
298
            error("no signal.");
 
299
            break;
 
300
        }
 
301
 
 
302
    alarm(0);
 
303
    vbi_del_handler(vbi, event, reqs);
 
304
    vbi_close(vbi);
 
305
    
 
306
    if (not dl_empty(reqs))
 
307
        error("capture aborted.  some pages are missing.");
 
308
 
 
309
    for (req = $ reqs[1].first; req->node->next; req = $ req->node->next)
 
310
    {
 
311
        fname = export_mkname(req->export, req->name, req->vtp, req->pgno_str);
 
312
        if (not fname || export(req->export, req->vtp, fname))
 
313
            error("error saving page %s: %s", req->pgno_str, export_errstr());
 
314
        if (fname)
 
315
            free(fname);
 
316
    }
 
317
 
 
318
    exit(dl_empty(reqs) ? 0 : 1);
 
319
}