-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconfig.c
367 lines (312 loc) · 7.55 KB
/
config.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
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include "get-comics.h"
#include "my-parser.h"
#include <dirent.h>
static struct tm *today;
static unsigned wday;
static char *gocomics_url;
static char *gocomics_regexp;
static char *index_dir;
static void add_comic(struct connection *new)
{
static struct connection *tail;
if (comics)
tail->next = new;
else
comics = head = new;
tail = new;
++n_comics;
}
static void new_comic(struct connection **conn)
{
static int id;
if (*conn == NULL) {
*conn = must_alloc(sizeof(struct connection));
(*conn)->out = -1;
(*conn)->id = ++id;
(*conn)->days = 0x7f;
}
}
static void sanity_check_comic(struct connection *new)
{
if (!new)
/* Empty entries are allowed */
return;
else if (!new->url) {
printf("ERROR: comic with no url!\n");
exit(1);
} else if (!new->host) {
printf("ERROR: comic with no host!\n");
exit(1);
} else if ((new->days & wday) == 0) {
if (verbose)
printf("Skipping: %s\n", new->url);
++skipped;
free(new);
return;
} else if (!is_http(new->url)) {
if (verbose)
printf("Skipping not http: %s\n", new->url);
++skipped;
free(new);
return;
} else if (!new->outname)
/* User did not supply a filename. Get it from the URL. */
new->outname = create_outname(new->url);
add_comic(new);
}
static void add_url(struct connection **conn, char *url)
{
char outurl[1024], *e;
new_comic(conn);
if (strftime(outurl, sizeof(outurl), url, today) == 0) {
printf("strftime failed for '%s'\n", url);
exit(1);
}
if ((*conn)->url)
printf("Url already set. Ignoring %s\n", url);
else {
(*conn)->url = must_strdup(outurl);
/* isolate the host from original url */
e = is_http(url);
if (e)
e = strchr(e, '/');
else
e = strchr(url + 1, '/');
if (e)
*e = '\0';
(*conn)->host = must_strdup(url);
}
}
static void add_days(struct connection **conn, char *days)
{
unsigned i;
new_comic(conn);
(*conn)->days = 0;
for (i = 0; *days && i < 7; ++days, ++i)
if (*days != 'X' && *days != 'x')
(*conn)->days |= 1 << i;
}
static void add_regexp(struct connection **conn, char *regexp)
{
char out[256];
new_comic(conn);
if (strftime(out, sizeof(out), regexp, today) == 0) {
printf("strftime failed for '%s'\n", regexp);
exit(1);
}
do_add_regexp(*conn, out, index_dir);
}
static void add_regmatch(struct connection **conn, int match)
{
new_comic(conn);
if (match >= MATCH_DEPTH) {
printf("<regmatch> %d too big.\n", match);
exit(1);
} else
(*conn)->regmatch = match;
}
static void add_outname(struct connection **conn, char *outname)
{
new_comic(conn);
(*conn)->outname = must_alloc(strlen(outname) + 4 + 1);
strcpy((*conn)->outname, outname);
}
static void add_base_href(struct connection **conn, char *base_href)
{
new_comic(conn);
(*conn)->base_href = must_strdup(base_href);
}
static void add_referer(struct connection **conn, char *referer)
{
new_comic(conn);
if (strcasecmp((char *)referer, "url") == 0 && (*conn)->url)
(*conn)->referer = must_strdup((*conn)->url);
else
(*conn)->referer = must_strdup(referer);
}
static void add_gocomic(struct connection **conn, char *comic)
{
char url[1024];
if (!gocomics_url) {
puts("ERROR: gocomic entry but gocomics-url not set");
exit(1);
}
if (!gocomics_regexp) {
puts("ERROR: gocomic entry but gocomics-regexp not set");
exit(1);
}
snprintf(url, sizeof(url), gocomics_url, comic);
add_url(conn, url);
add_regexp(conn, gocomics_regexp);
add_outname(conn, comic);
}
static void add_redirect_ok(struct connection **conn, int val)
{
new_comic(conn);
(*conn)->redirect_ok = val;
}
static void add_insecure(struct connection **conn, int val)
{
new_comic(conn);
(*conn)->insecure = val;
}
static void parse_top_str(char *key, char *val)
{
if (verbose > 2)
printf("key '%s' val '%s'\n", key, val);
if (strcmp(key, "directory") == 0) {
/* Do not override the command line option */
if (!comics_dir)
comics_dir = strdup(val);
} else if (strcmp(key, "gocomics-url") == 0)
gocomics_url = must_strdup(val);
else if (strcmp(key, "gocomics-regexp") == 0)
gocomics_regexp = must_strdup(val);
else if (strcmp(key, "debug") == 0)
debug_fp = fopen(val, "w");
else if (strcmp(key, "threads") == 0) {
if (!threads_set)
thread_limit = JSON_int(val);
} else if (strcmp(key, "timeout") == 0)
read_timeout = JSON_int(val);
else
printf("Unexpected element '%s'\n", key);
}
static void parse_comic_str(struct connection **new, char *key, char *val)
{
if (verbose > 2)
printf(" key '%s' val '%s'\n", key, val);
if (strcmp(key, "url") == 0)
add_url(new, val);
else if (strcmp(key, "days") == 0)
add_days(new, val);
else if (strcmp(key, "regexp") == 0)
add_regexp(new, val);
else if (strcmp(key, "output") == 0)
add_outname(new, val);
else if (strcmp(key, "href") == 0)
add_base_href(new, val);
else if (strcmp(key, "referer") == 0)
add_referer(new, val);
else if (strcmp(key, "gocomic") == 0)
add_gocomic(new, val);
else if (strcmp(key, "regmatch") == 0)
add_regmatch(new, JSON_int(val));
else if (strcmp(key, "redirect") == 0)
add_redirect_ok(new, JSON_int(val));
else if (strcmp(key, "insecure") == 0)
add_insecure(new, JSON_int(val));
else
printf("Unexpected entry %s\n", key);
}
static struct parse_ctx {
int in_comics;
struct connection *new;
} parse_ctx;
static int parse(void *ctxin, int type, JSON_value *value)
{
struct parse_ctx *ctx = ctxin;
switch (type) {
case JSON_T_STRING:
if (!value->key) {
printf("Parse error: string with no key\n");
exit(1);
}
if (ctx->in_comics)
parse_comic_str(&ctx->new, value->key, value->str);
else
parse_top_str(value->key, value->str);
break;
case JSON_T_ARRAY_BEGIN:
if (strcmp(value->key, "comics") == 0)
ctx->in_comics = 1;
else {
printf("Invalid array\n");
exit(1);
}
break;
case JSON_T_ARRAY_END:
ctx->in_comics = 0;
break;
case JSON_T_OBJECT_BEGIN:
if (ctx->in_comics) {
ctx->new = NULL;
if (verbose > 2)
printf("Comic:\n");
}
break;
case JSON_T_OBJECT_END:
if (ctx->in_comics)
sanity_check_comic(ctx->new);
break;
default:
printf("Unexpected JSON object %d\n", type);
}
return 1;
}
int read_config(const char *fname)
{
/* Get the time for the urls */
time_t now = time(NULL);
today = localtime(&now);
wday = 1 << today->tm_wday;
memset(&parse_ctx, 0, sizeof(parse_ctx)); /* reset context */
if (JSON_parse_file(fname ? fname : JSON_FILE, parse, &parse_ctx))
exit(1);
free(gocomics_url);
free(gocomics_regexp);
return 0;
}
/* Because we cd to the comics dir this must be absolute. */
void add_index_dir(const char *dir)
{
#ifdef WIN32
index_dir = must_strdup(dir);
#else
if (access(dir, F_OK)) {
char cmd[PATH_MAX + 12];
snprintf(cmd, sizeof(cmd), "mkdir -p %s", dir);
if (system(cmd)) {
my_perror(dir);
exit(1);
}
}
if (!(index_dir = realpath(dir, NULL))) {
my_perror(dir);
exit(1);
}
#endif
}
#if defined(WIN32) || defined(__QNXNTO__)
#ifndef PATH_MAX
#define PATH_MAX MAX_PATH
#endif
static int unlinkat(int unused, const char *name, int flags)
{ /* Fake unlinkat. */
char path[PATH_MAX];
snprintf(path, sizeof(path), "%s/%s", index_dir, name);
return unlink(path);
}
#define dirfd(fd) 0
#endif
void clean_index_dir(void)
{
DIR *dir;
if (!index_dir || access(index_dir, F_OK))
return;
if (!(dir = opendir(index_dir))) {
my_perror(index_dir);
exit(1);
}
struct dirent *ent;
while ((ent = readdir(dir))) {
if (*ent->d_name == '.') continue;
char *p = strrchr(ent->d_name, '.');
if (p && strcmp(p, ".html") == 0) {
if (unlinkat(dirfd(dir), ent->d_name, 0))
my_perror(ent->d_name);
} else
printf("Warning: %s/%s\n", index_dir, ent->d_name);
}
closedir(dir);
}