-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathargument.c
494 lines (390 loc) · 11.9 KB
/
argument.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
#define _XOPEN_SOURCE 500
#define _GNU_SOURCE
#include "./argument.h"
#include "canvas.h"
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <getopt.h>
#include <err.h>
typedef enum Token {
BAD = 0,
END,
ENTRY,
DELIM,
NUMBER
} Token;
static const char *token_name[] = { "BAD", "END", "ENTRY", "DELIM", "NUMBER" };
static const struct option long_options[] = {
{"output_file", required_argument, NULL, 'o'},
{"size", required_argument, NULL, 's'},
{"anchors", required_argument, NULL, 'a'},
{"anchors_from", required_argument, NULL, 'A'},
{"colors", required_argument, NULL, 'c'},
{"colors_from", required_argument, NULL, 'C'},
{"frames", required_argument, NULL, 'f'},
{"keep", no_argument, NULL, 'k'},
{"seed", required_argument, NULL, 'x'},
{"verbose", optional_argument, NULL, 'v'},
{"help", optional_argument, NULL, 'h'},
{0, 0, 0, 0},
};
static const size_t long_options_size = sizeof(long_options) / sizeof(long_options[0]);
static Token parseToken(const char *, const char **, long *);
static long *parseEntries(const char *, size_t, size_t *);
static char *mmapFile(const char *, size_t *);
static long getNumber(const char *);
static point parseSize(const char *);
Token parseToken(const char *fmt, const char **next, long *number) {
char c = 0;
char digit_buff[16];
size_t idx = 0;
do {
if((c = *fmt) == '\0') {
if(next) *next = fmt;
return END;
}
} while(isspace(*fmt++));
switch(c) {
case 0:
if(next) *next = fmt;
return END;
case ';':
case '\n':
if(next) *next = fmt;
return ENTRY;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
fmt--;
while(isdigit(*fmt)) {
digit_buff[idx++] = *fmt++;
};
digit_buff[idx++] = '\0';
*number = strtol(digit_buff, NULL, 10);
if(next) *next = fmt;
return NUMBER;
case ',':
if(next) *next = fmt;
return DELIM;
default:
return BAD;
}
}
long *parseEntries(const char *fmt, size_t members_count, size_t *written) {
if(!fmt || members_count == 0) return NULL;
size_t numbers = 0;
const char *next = NULL;
bool parsed = false;
long number = 0;
size_t num_entries = 0;
bool single_value = false;
const char *start = fmt;
while(!parsed) {
Token t = parseToken(fmt, &next, &number);
fmt = next;
switch(t) {
case NUMBER:
numbers++;
break;
case END:
parsed = true;
case ENTRY:
if(num_entries == 0 && numbers == 0) return NULL;
if(num_entries == 0 && numbers != 1 && numbers != members_count) return NULL;
if(num_entries > 0 && numbers != 0 && numbers != members_count) return NULL;
if(num_entries == 0 && numbers == 1) {
single_value = true;
if(written) *written = 1;
parsed = true;
}
if(numbers > 0) num_entries++;
numbers = 0;
break;
case BAD:
return NULL;
case DELIM:
break;
}
}
if(single_value) {
if(written) *written = 1;
return (long*)number;
}
if(written) *written = num_entries * members_count;
long *entries = NULL;
entries = calloc(num_entries * members_count, sizeof(long));
if(!entries) {
warn("Failed to allocate memory");
return NULL;
}
parsed = false;
next = NULL;
num_entries = 0;
numbers = 0;
while(!parsed) {
Token t = parseToken(start, &next, &number);
start = next;
switch(t) {
case NUMBER:
entries[numbers + num_entries * members_count] = number;
numbers++;
break;
case END:
parsed = true;
case ENTRY:
num_entries++;
numbers = 0;
break;
default:
break;
}
}
return entries;
}
static point parseSize(const char *fmt) {
point ret = {0, 0};
size_t memb_size = 0;
long *memb = parseEntries(fmt, 2, &memb_size);
if(!memb || memb_size == 0)
return ret;
if(memb_size == 1) {
ret.x = (long) memb;
ret.y = ret.x;
} else {
ret.x = memb[0];
ret.y = memb[1];
free(memb);
}
return ret;
}
static anchor *parseAnchors(const char *fmt, long *size) {
size_t memb_size = 0;
long *memb = parseEntries(fmt, 2, &memb_size);
if(!size || !memb || memb_size == 0)
return NULL;
anchor *anc = NULL;
if(memb_size > 1) {
size_t count = memb_size / 2;
anc = calloc(memb_size * memb_size / 2, sizeof(anchor));
for(size_t idx = 0, ent = 0; idx < memb_size; idx += 2, ent++) {
anc[ent].pos.x = memb[idx];
anc[ent].pos.y = memb[idx + 1];
}
*size = count;
} else {
anc = NULL;
*size = (long)memb;
}
return anc;
}
static color *parsePallete(const char *fmt, long *size) {
size_t memb_size = 0;
long *memb = parseEntries(fmt, 3, &memb_size);
if(!size || !memb || memb_size == 0)
return NULL;
color *cols = NULL;
if(memb_size > 1) {
size_t count = memb_size / 3;
cols = calloc(memb_size * count, sizeof(color));
for(size_t idx = 0, ent = 0; idx < memb_size; idx += 3, ent++) {
cols[ent].red = memb[idx];
cols[ent].green = memb[idx + 1];
cols[ent].blue = memb[idx + 2];
}
*size = count;
} else {
*size = (long)memb;
cols = NULL;
}
return cols;
}
static char *mmapFile(const char *filename, size_t *size) {
if(!size) return NULL;
int fd = open(filename, O_RDONLY);
struct stat sb;
if(fstat(fd, &sb) == -1) {
return NULL;
}
void *addr = mmap(NULL, sb.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
if(addr == MAP_FAILED) return NULL;
*size = sb.st_size;
close(fd);
return addr;
}
static long getNumber(const char *fmt) {
char *c;
long num = strtol(fmt, &c, 10);
if(*c != '\0') return -1;
return num;
}
Params parseArguments(int argc, char **argv) {
opterr = 0;
int opt;
Params params = NEW_PARAMS();
bool got_anchors = false;
bool got_colors = false;
int opt_idx = -1;
while((opt = getopt_long(argc, argv, "o:s:a:A:c:C:f:kx:v::h", long_options, &opt_idx)) != -1) {
switch(opt) {
case 'o': {
params.filename = optarg;
break;
}
case 's': {
point p = parseSize(optarg);
if(p.x == 0) {
errx(1, "Invalid size option %s", optarg);
}
params.size = p;
break;
}
case 'a': {
if(got_anchors) {
errx(1, "Duplicate anchors options");
}
got_anchors = true;
anchor *a = NULL;
long a_size = 0;
a = parseAnchors(optarg, &a_size);
if(!a && a_size == 0) {
errx(1, "Invalid anchors option %s", optarg);
}
params.anchors = a;
params.anchors_size = a_size;
break;
}
case 'A': {
if(got_anchors) {
errx(1, "Duplicate anchors options");
}
got_anchors = true;
char *anchor_file = optarg;
size_t cont_size = 0;
char *contents = mmapFile(anchor_file, &cont_size);
if(!contents) {
err(1, "mmap()");
}
long af_size = 0;
anchor *af = parseAnchors(contents, &af_size);
if(!af && af_size == 0) {
errx(1, "Invalid anchors option %s", optarg);
}
params.anchors = af;
params.anchors_size = af_size;
munmap(contents, cont_size);
break;
}
case 'c': {
if(got_colors) {
errx(1, "Duplicate colors options");
}
got_colors = true;
color *p = NULL;
long p_size = 0;
p = parsePallete(optarg, &p_size);
if(!p && p_size == 0) {
errx(1, "Invalid pallete option %s", optarg);
}
params.colors = p;
params.colors_size = p_size;
break;
}
case 'C': {
if(got_colors) {
errx(1, "Duplicate colors options");
}
got_colors = true;
char *colors_file = optarg;
size_t cont_size = 0;
char *contents = mmapFile(colors_file, &cont_size);
if(!contents) {
err(1, "Failed call to mmap()");
}
long cl_size = 0;
color *cl = parsePallete(contents, &cl_size);
if(!cl && cl_size == 0) {
errx(1, "Invalid anchors option %s", optarg);
}
params.colors = cl;
params.colors_size = cl_size;
munmap(contents, cont_size);
break;
}
case 'f': {
long frames = getNumber(optarg);
if(frames == -1) {
errx(1, "Invalid frames option: %s", optarg);
}
params.frames = frames;
break;
}
case 'k': {
params.keep = true;
break;
}
case 'x': {
long seed = getNumber(optarg);
if(seed == -1) {
errx(1, "Invalid frames option: %s", optarg);
}
params.seed = seed;
break;
}
case 'v': {
break;
}
case 'h': {
break;
}
case 0:
case '?': {
errx(1, "Got invaid argument: %s", argv[optind - 1]);
}
}
}
if(params.seed == 0) {
void* addr = malloc(0);
params.seed = (long)*(long*)&addr;
free(addr);
}
srandom(params.seed);
if(!params.filename) {
errx(1, "No input file");
}
if(!params.colors) {
params.colors = calloc(params.colors_size, sizeof(color));
if(!params.colors) {
err(1, "Failed to allocate %zu bytes", params.colors_size * sizeof(color));
}
for(size_t idx = 0; idx < params.colors_size; idx++) {
params.colors[idx] = randomColor();
}
}
if(!params.anchors) {
params.anchors = calloc(params.anchors_size, sizeof(anchor));
if(!params.anchors) {
err(1, "Failed to allocate %zu bytes", params.anchors_size * sizeof(color));
}
for(size_t idx = 0; idx < params.anchors_size; idx++) {
params.anchors[idx].pos = randomPoint(params.size);
}
}
for(size_t idx = 0; idx < params.anchors_size; idx++) {
size_t rand_idx = random() % params.colors_size;
params.anchors[idx].col = params.colors[rand_idx];
}
return params;
}