~ubuntu-branches/ubuntu/maverick/alevt/maverick

« back to all changes in this revision

Viewing changes to alevt-date.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 <sys/time.h>
 
3
#include <stdlib.h>
 
4
#include <unistd.h>
 
5
#include <signal.h>
 
6
 
 
7
#include "os.h"
 
8
#include "vt.h"
 
9
#include "fdset.h"
 
10
#include "vbi.h"
 
11
#include "lang.h"
 
12
#include "misc.h"
 
13
 
 
14
int debug = 0;
 
15
 
 
16
char *fmt = "%a %b %d %H:%M:%S %Z %Y";
 
17
int max_diff = 2*60*60; // default: 2 hours
 
18
int set_time = 0;
 
19
 
 
20
static void
 
21
chk_time(int t)
 
22
{
 
23
    struct tm *tm;
 
24
    time_t sys_t;
 
25
    int dt;
 
26
    char buf[256];
 
27
 
 
28
    if (t < 0 || t > 235959 || t%100 > 59 || t/100%100 > 59)
 
29
        return;
 
30
 
 
31
    sys_t = time(0);
 
32
    tm = localtime(&sys_t);
 
33
 
 
34
    // Now convert to UTC seconds
 
35
    t = t/100/100 * 60*60 + t/100%100 * 60 + t%100;
 
36
#ifdef BSD
 
37
    t -= tm->tm_gmtoff; // dst already included...
 
38
#else
 
39
    t += timezone;
 
40
    if (tm->tm_isdst)
 
41
        t -= 60*60;
 
42
#endif
 
43
 
 
44
    dt = t - sys_t % (24*60*60);
 
45
    if (dt <= -12*60*60)
 
46
        dt += 24*60*60;
 
47
 
 
48
    if (dt <= -max_diff || dt >= max_diff)
 
49
        fatal("time diff to big (%2d:%02d:%02d)", dt/60/60, abs(dt)/60%60, abs(dt)%60);
 
50
 
 
51
    sys_t += dt;
 
52
 
 
53
    if (set_time)
 
54
    {
 
55
        struct timeval tv[1];
 
56
 
 
57
        tv->tv_sec = sys_t;
 
58
        tv->tv_usec = 500000;
 
59
        if (settimeofday(tv, 0) == -1)
 
60
            ioerror("settimeofday");
 
61
    }
 
62
    if (*fmt)
 
63
    {
 
64
        tm = localtime(&sys_t);
 
65
        if (strftime(buf, sizeof(buf), fmt, tm))
 
66
            puts(buf);
 
67
    }
 
68
    exit(0);
 
69
}
 
70
 
 
71
static void
 
72
event(void *_, struct vt_event *ev)
 
73
{
 
74
    switch (ev->type)
 
75
    {
 
76
        /* vbi may generate EV_PAGE, EV_HEADER, EV_XPACKET */
 
77
        /* for event arguments see vt.h */
 
78
 
 
79
        case EV_HEADER: // a new title line (for running headers)
 
80
        {
 
81
            static int last_t = -1;
 
82
            u8 *s = ev->p1;
 
83
            int i, t = 1;
 
84
 
 
85
            if (ev->i2 & PG_OUTOFSEQ)
 
86
                break;
 
87
 
 
88
            for (i = 32; i < 40; ++i)
 
89
                if (s[i] >= '0' && s[i] <= '9')
 
90
                    t = t * 10+ s[i] - '0';
 
91
            if (t >= 1000000 && t <= 1235959)
 
92
                if (t == last_t || t - last_t == 1)
 
93
                    chk_time(t - 1000000);
 
94
            /*
 
95
            if (last_t != t)
 
96
                printf("%06d\n", t-1000000);
 
97
            */
 
98
            last_t = t;
 
99
            break;
 
100
        }
 
101
    }
 
102
}
 
103
 
 
104
 
 
105
static void
 
106
usage(FILE *fp, int exit_val)
 
107
{
 
108
    fprintf(fp, "usage: %s [options]\n", prgname);
 
109
    fprintf(fp,
 
110
            "\n"
 
111
            "  Valid options:\tDefault:\n"
 
112
            "    --help\n"
 
113
            "    --version\n"
 
114
            "    -vbi <vbidev>\n"
 
115
            //"    -oldbttv\n"
 
116
            "    -set\t\toff\n"
 
117
            "    -delta <max_secs>\t7200 (2 hours)\n"
 
118
            "    -format <fmtstr>\t%%c\n"
 
119
            "    -timeout <seconds>\tnone\n"
 
120
            );
 
121
    exit(exit_val);
 
122
}
 
123
 
 
124
static int
 
125
option(int argc, char **argv, int *ind, char **arg)
 
126
{
 
127
    static struct { char *nam, *altnam; int arg; } opts[] = {
 
128
        { "-set", "-s", 0 },
 
129
        { "-delta", "-d", 1 },
 
130
        { "-format", "-f", 1 },
 
131
        { "-vbi", "-dev", 1 },
 
132
        { "-timeout", "-t", 1 },
 
133
        { "--version", "-v", 0 },
 
134
        { "--help", "-h", 0 },
 
135
        { "-newbttv", "-new", 0 },
 
136
        { "-oldbttv", "-old", 0 },
 
137
    };
 
138
    int i;
 
139
 
 
140
    if (*ind >= argc)
 
141
        return 0;
 
142
 
 
143
    *arg = argv[(*ind)++];
 
144
    for (i = 0; i < NELEM(opts); ++i)
 
145
        if (streq(*arg, opts[i].nam) || streq(*arg, opts[i].altnam))
 
146
        {
 
147
            if (opts[i].arg)
 
148
                if (*ind < argc)
 
149
                    *arg = argv[(*ind)++];
 
150
                else
 
151
                    fatal("option %s requires an argument", *arg);
 
152
            return i+1;
 
153
        }
 
154
 
 
155
    if (**arg == '-')
 
156
    {
 
157
        fatal("%s: invalid option", *arg);
 
158
        usage(stderr, 1);
 
159
    }
 
160
 
 
161
    return -1;
 
162
}
 
163
 
 
164
int
 
165
main(int argc, char **argv)
 
166
{
 
167
    char *vbi_name = "/dev/vbi";
 
168
    int timeout = 0;
 
169
    int big_buf = -1;
 
170
    struct vbi *vbi;
 
171
    int opt, ind;
 
172
    char *arg;
 
173
 
 
174
    setprgname(argv[0]);
 
175
 
 
176
    ind = 1;
 
177
    while (opt = option(argc, argv, &ind, &arg))
 
178
        switch (opt)
 
179
        {
 
180
            case 1:     // -set
 
181
                set_time = 1;
 
182
                break;
 
183
 
 
184
            case 2:     // -delta
 
185
                max_diff = atoi(arg);
 
186
                if (max_diff < 1)
 
187
                    fatal("-delta: illegal value '%s'", arg);
 
188
                if (max_diff > 12*60*60)
 
189
                {
 
190
                    max_diff = 12*60*60;
 
191
                    error("-delta: %d too big.  assuming %d", arg, max_diff);
 
192
                }
 
193
                break;
 
194
            case 3:     // -fmt
 
195
                fmt = arg;
 
196
                break;
 
197
            case 4:     // -vbi
 
198
                vbi_name = arg;
 
199
                break;
 
200
            case 5:     // -timeout
 
201
                timeout = atoi(arg);
 
202
                if (timeout < 1 || timeout > 60*60)
 
203
                    fatal("-timeout: illegal value '%s'", arg);
 
204
                break;
 
205
            case 6:     // -version
 
206
                printf("AleVT-Date Version "VERSION"\n");
 
207
                exit(0);
 
208
            case 7:     // help
 
209
                usage(stdout, 0);
 
210
                break;
 
211
            case 8:     // newbttv
 
212
                big_buf = 1;
 
213
                break;
 
214
            case 9:     // oldbttv
 
215
                big_buf = 0;
 
216
                break;
 
217
            case -1:
 
218
                usage(stderr, 1);
 
219
                break;
 
220
        }
 
221
 
 
222
    fdset_init(fds);
 
223
 
 
224
    if (timeout)
 
225
    {
 
226
        signal(SIGALRM, SIG_DFL);       // kill me
 
227
        alarm(timeout);
 
228
    }
 
229
 
 
230
    vbi = vbi_open(vbi_name, 0, 1, big_buf);    // open device
 
231
    if (not vbi)
 
232
        fatal_ioerror(vbi_name);
 
233
 
 
234
    vbi_add_handler(vbi, event, 0);     // register event handler
 
235
 
 
236
    for (;;)
 
237
        fdset_select(fds, -1);          // call scheduler
 
238
 
 
239
    /* never reached */
 
240
    vbi_del_handler(vbi, event, 0);
 
241
    vbi_close(vbi);
 
242
    exit(0);
 
243
}