-
Notifications
You must be signed in to change notification settings - Fork 12
/
main.c
335 lines (304 loc) · 8.37 KB
/
main.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
#include "urlopen.h"
#include "encoding.h"
#include "fec.h"
#include <stdlib.h>
#include <pcre.h>
#include <string.h>
#ifndef _WIN32
#include <unistd.h>
#endif
#define BUFFERSIZE 65536
const char *FLAG_FILING_ID = "--include-filing-id";
const char FLAG_FILING_ID_SHORT = 'i';
const char *FLAG_SILENT = "--silent";
const char FLAG_SILENT_SHORT = 's';
const char *FLAG_WARN = "--warn";
const char FLAG_WARN_SHORT = 'w';
const char *FLAG_DISABLE_STDIN = "--no-stdin";
const char FLAG_DISABLE_STDIN_SHORT = 'x';
void printUsage(char *argv[])
{
fprintf(stderr, "\nUsage:\n %s [flags] <id, file, or url> [output directory=output] [override id]\nor: [some command] | %s [flags] <id> [output directory=output]\n", argv[0], argv[0]);
fprintf(stderr, "\nOptional flags:\n");
fprintf(stderr, " %s, -%c: include a filing_id column at the beginning of\n every output CSV\n", FLAG_FILING_ID, FLAG_FILING_ID_SHORT);
fprintf(stderr, " %s, -%c : suppress all stdout messages\n\n", FLAG_SILENT, FLAG_SILENT_SHORT);
fprintf(stderr, " %s, -%c : show warning messages\n\n", FLAG_WARN, FLAG_WARN_SHORT);
fprintf(stderr, " %s, -%c : disable piped input\n\n", FLAG_DISABLE_STDIN, FLAG_DISABLE_STDIN_SHORT);
}
/* Small main program to retrieve from a url using fgets and fread saving the
* output to two test files (note the fgets method will corrupt binary files if
* they contain 0 chars */
int main(int argc, char *argv[])
{
int piped = !isatty(fileno(stdin));
int flagOffset = 0;
URL_FILE *handle;
const char *url;
if (argc < 2)
{
printUsage(argv);
exit(1);
}
// Regexes and constants for URL handling
const char *error;
int errorOffset;
pcre *filingIdOnly = pcre_compile("^\\s*([0-9]+)\\s*$", 0, &error, &errorOffset, NULL);
if (filingIdOnly == NULL)
{
fprintf(stderr, "PCRE filing ID compilation failed at offset %d: %s\n", errorOffset, error);
exit(1);
}
pcre *extractNumber = pcre_compile("^.*?([0-9]+)(\\.[^\\.]+)?\\s*$", 0, &error, &errorOffset, NULL);
if (extractNumber == NULL)
{
fprintf(stderr, "PCRE number extraction compilation failed at offset %d: %s\n", errorOffset, error);
exit(1);
}
// Try to extract flags
int includeFilingId = 0;
int silent = 0;
int warn = 0;
while (argv[1 + flagOffset][0] == '-')
{
if (strcmp(argv[1 + flagOffset], FLAG_FILING_ID) == 0)
{
includeFilingId = 1;
flagOffset++;
}
else if (strcmp(argv[1 + flagOffset], FLAG_SILENT) == 0)
{
silent = 1;
flagOffset++;
}
else if (strcmp(argv[1 + flagOffset], FLAG_WARN) == 0)
{
warn = 1;
flagOffset++;
}
else if (strcmp(argv[1 + flagOffset], FLAG_DISABLE_STDIN) == 0)
{
piped = 0;
flagOffset++;
}
else
{
// Try to extract flags in short form
int matched = 0;
for (int i = 1; i < strlen(argv[1 + flagOffset]); i++)
{
if (argv[1 + flagOffset][i] == FLAG_FILING_ID_SHORT)
{
includeFilingId = 1;
matched = 1;
}
else if (argv[1 + flagOffset][i] == FLAG_SILENT_SHORT)
{
silent = 1;
matched = 1;
}
else if (argv[1 + flagOffset][i] == FLAG_WARN_SHORT)
{
warn = 1;
matched = 1;
}
else if (argv[1 + flagOffset][i] == FLAG_DISABLE_STDIN_SHORT)
{
piped = 0;
matched = 1;
}
else
{
printUsage(argv);
exit(1);
}
}
if (matched)
{
flagOffset++;
}
else
{
printUsage(argv);
exit(1);
}
}
}
const char *docqueryUrl = "https://docquery.fec.gov/dcdev/posted/";
const char *docqueryUrlAlt = "https://docquery.fec.gov/paper/posted/";
url = piped ? NULL : argv[1 + flagOffset];
// Strings that will carry the decoded values for the filing URL and ID,
// where URL can be a local file or a remote URL
char *fecUrl = NULL;
char *fecBackupUrl = NULL;
char *fecId = NULL;
char *outputDirectory = malloc(8);
strcpy(outputDirectory, "output/");
char *fecExtension = ".fec";
if (argc > 2 + flagOffset)
{
outputDirectory = realloc(outputDirectory, strlen(argv[2 + flagOffset]) + 1);
strcpy(outputDirectory, argv[2 + flagOffset]);
// Ensure output directory ends with a trailing slash
if (outputDirectory[strlen(outputDirectory) - 1] != '/')
{
outputDirectory = realloc(outputDirectory, strlen(outputDirectory) + 2);
strcat(outputDirectory, "/");
}
}
// Pull out ID/override ID parameter, depending on how input is piped
if (piped)
{
if (argc > 1 + flagOffset)
{
fecId = malloc(strlen(argv[1 + flagOffset]) + 1);
strcpy(fecId, argv[1 + flagOffset]);
}
else
{
printUsage(argv);
exit(1);
}
}
else
{
if (argc > 3 + flagOffset)
{
fecId = malloc(strlen(argv[3 + flagOffset]) + 1);
strcpy(fecId, argv[3 + flagOffset]);
}
}
// Check URL format and grab matching ID
int matches[6];
if (!piped && (pcre_exec(filingIdOnly, NULL, url, strlen(url), 0, 0, matches, 3) >= 0))
{
// URL is a purely numeric filing ID
int start = matches[0];
int end = matches[1];
// Initialize FEC id
fecId = malloc(end - start + 1);
strncpy(fecId, url + start, end - start);
fecId[end - start] = '\0';
// Initialize FEC URL (and backup URL)
fecUrl = malloc(strlen(docqueryUrl) + strlen(fecId) + strlen(fecExtension) + 1);
strcpy(fecUrl, docqueryUrl);
strcat(fecUrl, fecId);
strcat(fecUrl, fecExtension);
fecUrl[strlen(fecUrl)] = '\0';
fecBackupUrl = malloc(strlen(docqueryUrlAlt) + strlen(fecId) + strlen(fecExtension) + 1);
strcpy(fecBackupUrl, docqueryUrlAlt);
strcat(fecBackupUrl, fecId);
strcat(fecBackupUrl, fecExtension);
}
else if (!piped)
{
// URL is a local file or a remote URL
fecUrl = malloc(strlen(url) + 1);
strcpy(fecUrl, url);
fecUrl[strlen(fecUrl)] = '\0';
// Try to extract the ID from the URL
if (fecId == NULL && pcre_exec(extractNumber, NULL, fecUrl, strlen(fecUrl), 0, 0, matches, 6) >= 0)
{
int start = matches[2];
int end = matches[3];
// Initialize FEC id
fecId = malloc(end - start + 1);
strncpy(fecId, url + start, end - start);
fecId[end - start] = '\0';
}
}
if (fecId == NULL)
{
fprintf(stderr, "Please specify a filing ID manually\n");
printUsage(argv);
// Clear associated memory
free(outputDirectory);
if (fecUrl != NULL)
{
free(fecUrl);
}
if (fecBackupUrl != NULL)
{
free(fecBackupUrl);
}
pcre_free(filingIdOnly);
pcre_free(extractNumber);
exit(1);
}
if (!silent)
{
printf("About to parse filing ID %s\n", fecId);
}
if (piped)
{
if (!silent)
{
printf("Using stdin\n");
}
}
else
{
if (!silent)
{
printf("Trying URL: %s\n", fecUrl);
}
}
handle = piped ? url_fopen_stdin() : url_fopen(fecUrl, "r", NULL);
if (!handle)
{
if (fecBackupUrl != NULL)
{
if (!silent)
{
printf("Couldn't open URL %s\n", fecUrl);
printf("Trying backup URL: %s\n", fecBackupUrl);
}
handle = url_fopen(fecBackupUrl, "r", NULL);
if (!handle)
{
fprintf(stderr, "Couldn't open URL: %s\n", fecBackupUrl);
return 2;
}
}
else
{
fprintf(stderr, "Couldn't open URL %s\n", fecUrl);
return 2;
}
}
// Initialize persistent memory context
PERSISTENT_MEMORY_CONTEXT *persistentMemory = newPersistentMemoryContext();
// Initialize FEC context
FEC_CONTEXT *fec = newFecContext(persistentMemory, ((BufferRead)(&url_readBuffer)), BUFFERSIZE, NULL, BUFFERSIZE, handle, fecId, outputDirectory, includeFilingId, silent, warn);
// Parse the fec file
int fecParseResult = parseFec(fec);
// Clear up memory
freeFecContext(fec);
freePersistentMemoryContext(persistentMemory);
pcre_free(filingIdOnly);
pcre_free(extractNumber);
free(outputDirectory);
if (fecUrl != NULL)
{
free(fecUrl);
}
if (fecBackupUrl != NULL)
{
free(fecBackupUrl);
}
if (fecId != NULL)
{
free(fecId);
}
// Close file handles
url_fclose(handle);
if (!fecParseResult)
{
fprintf(stderr, "Parsing FEC failed\n");
return 3;
}
if (!silent)
{
printf("Done; parsing successful!\n");
}
return 0; /* all done */
}