-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtvguide.c
169 lines (144 loc) · 4.43 KB
/
rtvguide.c
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
/*
* Copyright (C) 2002 John Todd Larason <jtl@molehill.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "rtv.h"
#include "guideclient.h"
#ifdef WIN32
#include "gnu_getopt\getopt.h"
#endif
#ifdef __unix__
#include <unistd.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void usage(const char * err)
{
if (err)
fprintf(stderr, "*** ERROR: %s\n\n", err);
fprintf(stderr,
"Usage: rtvguide [-a address] [-s software version] [command <args...>]\n"
"Commands: get [ -f <dstfile> ] [ -a ] [ -t <timestamp> ]\n"
" -a -- auto mode: name file \"snapshot.<address>.<timestamp>\"\n"
" and output timestamp on stdout; if -t is also given, and\n"
" the file hasn't changed, don't modify it.\n"
" -t -- if a timestamp is given, only output if the data has\n"
" changed.\n\n"
"Address defaults to $RTV4K_ADDRESS, if that's set; if not, -a must be used\n"
"Software version defaults to 4.3.0 (230); can be set with $RTV4K_VERSION\n"
"or -s; format should be like '520411140' or '4.1.1.140'\n"
);
exit(err ? 1 : 0);
}
static int guide_get(const char * address, int argc, char ** argv)
{
int ch;
int auto_mode = 0;
char * orig_timestamp = "0";
char * filename = NULL;
unsigned char * result, * timestamp;
unsigned long size;
unsigned long status;
FILE * fp;
while ((ch = getopt(argc, argv, "f:at:")) != EOF) {
switch(ch) {
case 'f':
filename = optarg;
break;
case 'a':
auto_mode = 1;
break;
case 't':
orig_timestamp = optarg;
break;
default:
usage("Invalid argument to guide_get");
}
}
if (optind != argc) {
usage("Invalid argument to guide_get");
}
if (auto_mode && filename) {
usage("Auto mode and explicit filename are incompatible.");
}
status = guide_client_get_snapshot(&result, ×tamp, &size,
address, orig_timestamp);
if (status != 0) {
fprintf(stderr, "Error get_snapshot %ld\n", status);
return -1;
}
if (strcmp(timestamp, "upToDate") == 0)
return 0;
if (auto_mode) {
filename = malloc(64);
sprintf(filename, "snapshot.%s.%s", address, timestamp);
}
if (filename) {
fp = fopen(filename, "w");
if (!fp) {
perror("Open output file");
return -1;
}
} else {
fp = stdout;
}
fwrite(result, size, 1, fp);
if (auto_mode) {
printf("%s\n", timestamp);
}
return 0;
}
static struct
{
const char * command;
int (*fn)(const char *, int, char **);
} commands[] = {
{ "get", guide_get },
};
int main(int argc, char ** argv)
{
char * address, * version;
int num_commands = sizeof(commands)/sizeof(commands[0]);
int ch;
int r = -1;
int i;
int my_optind;
address = getenv("RTV4K_ADDRESS");
version = getenv("RTV4K_VERSION");
while ((ch = getopt(argc, argv, "+a:s:")) != EOF) {
switch(ch) {
case 'a':
address = optarg;
break;
default:
usage("Invalid argument");
}
}
if (optind == argc || address == NULL)
usage(NULL);
my_optind = optind;
optind = 1;
if (version)
if (rtv_set_replaytv_version(version) != 0)
usage("Invalid software version");
for (i = 0; i < num_commands; i++) {
if (strcmp(argv[my_optind], commands[i].command) == 0) {
r = commands[i].fn(address, argc - my_optind, argv + my_optind);
break;
}
}
if (i == num_commands) {
usage("INvalid command");
}
return r;
}