-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
806 lines (733 loc) · 21.9 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
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
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
#include <dirent.h>
#include <errno.h>
#include <fts.h>
#include <memory.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define SINGLE_BLANK_LINE_BETWEEN_FORMS
#define ASIZE(a) (sizeof(a) / sizeof(a[0]))
#define MAX_PATH_SIZE 2048
const char *body_indent[] = {
"fn", "bound-fn", "case", "cond->", "cond->>",
"as->", "condp", "while", "future", "thread",
"comment", "doto", "locking", "fdef", "extend",
"catch", "let", "letfn", "binding", "loop",
"for", "go-loop", "doseq", "dotimes", "struct-map",
"testing", "are", "context", "use-fixtures", "POST",
"GET", "PUT", "DELETE", "handler-case", "handle",
"dotrace", "match"};
const char *do_indent[] = {"do", "try", "finally", "go",
"alt!", "alt!!", "cond"};
const int TRIE_CHILDREN_NUM = '~' - '!' + 1;
typedef struct trie_node {
struct trie_node *children[TRIE_CHILDREN_NUM];
const char *rest;
bool is_terminal;
} trie_node;
static trie_node *make_trie_node(void) {
trie_node *res = malloc(sizeof(trie_node));
memset(res, 0, sizeof(trie_node));
return res;
}
static bool is_in_trie(trie_node *root, const char *start, const char *end) {
trie_node *node = root;
for (const char *p = start; p != end; p++) {
int index = p[0] - '!';
if (node->rest && !memcmp(node->rest, p, end - p) &&
!node->rest[end - p])
return true;
if (!node->children[index])
return false;
node = node->children[index];
}
return node->is_terminal || (node->rest && !node->rest[0]);
}
static void add_to_trie(trie_node *node, const char *data) {
int index = data[0] - '!';
if (!node->children[index]) {
node->children[index] = make_trie_node();
node->children[index]->rest = data + 1;
const char *rest = node->rest;
if (rest) {
node->rest = NULL;
if (!rest[0]) {
node->is_terminal = true;
} else {
add_to_trie(node, rest);
}
}
return;
}
if (!data[1]) {
node->children[index]->is_terminal = true;
} else {
add_to_trie(node->children[index], data + 1);
}
}
typedef struct {
char *input;
char *output;
} options;
typedef enum {
V_CHAR,
V_STRING,
V_REGEX,
V_NUMBER,
V_SYMBOL,
V_KEYWORD,
V_NIL,
V_BOOL,
V_COMMENT,
V_LIST,
V_VECTOR,
V_MAP,
V_SET,
V_FN,
V_MAX
} value_type;
char *value_type_names[V_MAX] = {
"char", "string", "regex", "number", "symbol", "keyword", "nil",
"bool", "comment", "list", "vector", "map", "set", "fn"};
typedef struct {
value_type type;
char *start;
char *end;
char *name_start;
int new_lines;
} value;
typedef struct {
value val;
value **vals;
int count;
int capacity;
} collection;
typedef struct {
value *last_read_val;
size_t size;
char *bytes;
char *sp;
const char *filename;
int indent;
int offset;
bool is_defrecord;
} context;
static value *read_value(void);
static bool is_separator(char c);
static void format_value(value *val, FILE *f);
context *ctx;
trie_node *body_indent_trie;
trie_node *do_indent_trie;
static inline size_t length(value *val) { return val->end - val->start; }
static void pump_tries(void) {
body_indent_trie = make_trie_node();
for (int i = 0; i < ASIZE(body_indent); i++) {
add_to_trie(body_indent_trie, body_indent[i]);
}
do_indent_trie = make_trie_node();
for (int i = 0; i < ASIZE(do_indent); i++) {
add_to_trie(do_indent_trie, do_indent[i]);
}
}
static context *make_context(void) {
context *ctx = malloc(sizeof(context));
memset(ctx, 0, sizeof(context));
return ctx;
}
static value *make_value(value_type type, char *start, char *end) {
value *res = malloc(sizeof(value));
res->type = type;
res->start = start;
res->name_start = start;
res->end = end;
res->new_lines = 0;
return res;
}
static collection *make_collection(value_type type, char *start) {
collection *res = malloc(sizeof(collection));
memset(res, 0, (sizeof(collection)));
res->val.type = type;
res->val.start = start;
return res;
}
static void add_to_coll(collection *coll, value *v) {
if (coll->count == coll->capacity) {
coll->capacity = coll->capacity ? coll->capacity * 2 : 4;
coll->vals = realloc(coll->vals, coll->capacity * sizeof(value *));
}
coll->vals[coll->count++] = v;
}
static void print_usage(void) {
fprintf(stderr,
"Usage: cljf [<input file or directory>] [-o <output file>]\n"
"Examples:\n"
"cljf - read source code from stdin and write "
"formatted "
"code to stdout\n"
"cljf foo.clj - format file foo.clj (override its "
"content with formatted code)\n"
"cljf foo.clj -o bar.clj - read source code from file "
"foo.clj and write formatted code to file bar.clj\n"
"cljf src - format all Clojure files (files with "
"extensions "
"*.clj, *.cljs, *.cljc, *.joke) in src directory\n");
exit(1);
}
static value *read_char(void) {
char *start = ctx->sp;
if (!ctx->sp[1]) {
fprintf(stderr, "Unexpected end of file while reading a char\n");
fprintf(stderr, "File: %s\n", ctx->filename);
exit(4);
}
for (ctx->sp += 2; !is_separator(*ctx->sp); ctx->sp++)
;
return make_value(V_CHAR, start, ctx->sp);
}
static value *read_string(value_type type, char *start) {
bool slash = false;
for (ctx->sp++; *ctx->sp != '"' || slash; ctx->sp++) {
switch (*ctx->sp) {
case '\0':
fprintf(stderr, "Unexpected end of file while reading a string\n");
fprintf(stderr, "File: %s\n", ctx->filename);
exit(4);
break;
case '\\':
slash = !slash;
break;
default:
slash = false;
break;
}
}
ctx->sp++;
return make_value(type, start, ctx->sp);
}
static bool is_whitespace(char c) {
// Note: doesn't handle unicode whitespaces (yet?).
switch (c) {
case ' ':
case '\t':
case '\n':
case '\f':
case '\r':
case '\v':
case '\x1C':
case '\x1D':
case '\x1E':
case '\x1F':
return true;
default:
return false;
}
}
static bool is_separator(char c) {
if (is_whitespace(c))
return true;
switch (c) {
case '\0':
case '(':
case ')':
case '[':
case ']':
case '{':
case '}':
case ',':
case '"':
case ';':
case '\\':
return true;
default:
return false;
}
}
static value *read_number(void) {
char *start = ctx->sp;
for (ctx->sp++; !is_separator(*ctx->sp); ctx->sp++)
;
return make_value(V_NUMBER, start, ctx->sp);
}
static value *read_comment(void) {
char *start = ctx->sp;
for (ctx->sp++; *ctx->sp != '\n' && *ctx->sp; ctx->sp++)
;
return make_value(V_COMMENT, start, ctx->sp);
}
static value *read_symbol(void) {
char *start = ctx->sp;
char *name_start = ctx->sp;
for (ctx->sp++; !is_separator(*ctx->sp); ctx->sp++) {
if ((*ctx->sp) == '/') {
name_start = ctx->sp + 1;
}
}
value_type t = V_SYMBOL;
size_t len = ctx->sp - start;
if (len == 3 && start[0] == 'n' && start[1] == 'i' && start[2] == 'l') {
t = V_NIL;
} else if (len == 4 && start[0] == 't' && start[1] == 'r' &&
start[2] == 'u' && start[3] == 'e') {
t = V_BOOL;
} else if (len == 5 && start[0] == 'f' && start[1] == 'a' &&
start[2] == 'l' && start[3] == 's' && start[4] == 'e') {
t = V_BOOL;
} else if (len == 5 && start[0] == '#' && start[1] == '#' &&
start[2] == 'N' && start[3] == 'a' && start[4] == 'N') {
t = V_NUMBER;
} else if (len == 5 && start[0] == '#' && start[1] == '#' &&
start[2] == 'I' && start[3] == 'n' && start[4] == 'f') {
t = V_NUMBER;
} else if (len == 6 && start[0] == '#' && start[1] == '#' &&
start[2] == '-' && start[3] == 'I' && start[4] == 'n' &&
start[5] == 'f') {
t = V_NUMBER;
}
value *res = make_value(t, start, ctx->sp);
res->name_start = name_start;
return res;
}
static value *read_keyword(void) {
char *start = ctx->sp;
for (ctx->sp++; !is_separator(*ctx->sp); ctx->sp++)
;
return make_value(V_KEYWORD, start, ctx->sp);
}
static void skip_whitespace(void) {
while (is_whitespace(*ctx->sp)) {
if (ctx->last_read_val && *ctx->sp == '\n') {
ctx->last_read_val->new_lines++;
}
ctx->sp++;
}
}
static bool is_digit(char c) { return c >= '0' && c <= '9'; }
static value *read_collection(value_type type, char *start, char end) {
ctx->sp++;
collection *coll = make_collection(type, start);
skip_whitespace();
while (*ctx->sp != end) {
ctx->last_read_val = read_value();
if (!ctx->last_read_val) {
fprintf(stderr,
"Unexpected end of file while reading a collection\n");
fprintf(stderr, "File: %s\n", ctx->filename);
exit(4);
}
add_to_coll(coll, ctx->last_read_val);
skip_whitespace();
}
ctx->sp++;
coll->val.end = ctx->sp;
return (value *)coll;
}
static value *read_value(void) {
skip_whitespace();
if (!*ctx->sp)
return NULL;
switch (*ctx->sp) {
case '(':
return read_collection(V_LIST, ctx->sp, ')');
case '[':
return read_collection(V_VECTOR, ctx->sp, ']');
case '{':
return read_collection(V_MAP, ctx->sp, '}');
case '"':
return read_string(V_STRING, ctx->sp);
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
return read_number();
case '.':
if (is_digit(ctx->sp[1]))
return read_number();
return read_symbol();
case ':':
return read_keyword();
case '\\':
return read_char();
case ';':
return read_comment();
case '#':
switch (ctx->sp[1]) {
case '{':
ctx->sp++;
return read_collection(V_SET, ctx->sp - 1, '}');
case '(':
ctx->sp++;
return read_collection(V_FN, ctx->sp - 1, ')');
case '"':
ctx->sp++;
return read_string(V_REGEX, ctx->sp - 1);
case '_':
ctx->sp += 2;
return make_value(V_SYMBOL, ctx->sp - 2, ctx->sp);
}
// fallthrough
default:
return read_symbol();
}
}
static inline bool is_body_indent(value *val) {
return is_in_trie(body_indent_trie, val->name_start, val->end) ||
(val->end - val->name_start > 5 &&
!memcmp(val->name_start, "with-", 5));
}
static inline bool is_do_indent(value *val) {
return is_in_trie(do_indent_trie, val->name_start, val->end);
}
static inline bool is_def_indent(value *val) {
size_t len = val->end - val->name_start;
return !memcmp(val->name_start, "def", 3) ||
(len == 2 && (!memcmp(val->name_start, "if", 2) ||
!memcmp(val->name_start, "ns", 2))) ||
(len > 3 && !memcmp(val->name_start, "if-", 3)) ||
(len == 4 && !memcmp(val->name_start, "when", 4)) ||
(len == 5 && (!memcmp(val->name_start, "reify", 5) ||
!memcmp(val->name_start, "proxy", 5))) ||
(len == 11 && !memcmp(val->name_start, "extend-type", 11)) ||
(len > 5 && !memcmp(val->name_start, "when-", 5)) ||
(len == 15 && !memcmp(val->name_start, "extend-protocol", 15));
}
static inline bool is_require_indent(value *val) {
size_t len = val->end - val->start;
return (len == 8 && !memcmp(val->start, ":require", 8)) ||
(len == 7 && !memcmp(val->start, ":import", 7));
}
static inline bool is_comma(value *val) {
return val->type == V_SYMBOL && val->start[0] == ',' &&
val->start + 1 == val->end;
}
static inline bool is_prefix(value *val) {
if (val->type != V_SYMBOL)
return false;
switch (length(val)) {
case 1:
switch (val->start[0]) {
case '~':
case '`':
case '^':
case '\'':
case '@':
return true;
default:
return false;
}
case 2:
return (val->start[0] == '#' &&
(val->start[1] == '?' || val->start[1] == '_')) ||
(val->start[0] == '~' && val->start[1] == '@') ||
(val->start[0] == '\'' && val->start[1] == '#');
case 3:
return val->start[0] == '#' &&
((val->start[1] == '?' && val->start[2] == '@') ||
val->start[1] == ':');
default:
return val->start[0] == '#' && val->start[1] == ':';
}
}
static bool require_less(value *v1, value *v2) {
if (v1->type > V_COMMENT && ((collection *)v1)->count) {
v1 = ((collection *)v1)->vals[0];
}
if (v2->type > V_COMMENT && ((collection *)v2)->count) {
v2 = ((collection *)v2)->vals[0];
}
size_t len1 = length(v1);
size_t len2 = length(v2);
int r = memcmp(v1->start, v2->start, len1 < len2 ? len1 : len2);
if (!r) {
return len1 < len2;
}
return r < 0;
}
static void sort_require(collection *coll) {
for (int i = 1; i < coll->count; i++) {
switch (coll->vals[i]->type) {
case V_VECTOR:
case V_LIST:
break;
case V_SYMBOL:
if (is_prefix(coll->vals[i]))
return;
break;
default:
return;
}
coll->vals[i]->new_lines = 1;
}
for (int i = 1; i < coll->count - 1; i++) {
for (int j = i + 1; j < coll->count; j++) {
if (require_less(coll->vals[j], coll->vals[i])) {
value *t = coll->vals[i];
coll->vals[i] = coll->vals[j];
coll->vals[j] = t;
}
}
}
}
static void format_collection(collection *coll, char start, char end, FILE *f) {
int old_indent = ctx->indent;
if (coll->val.type == V_FN || coll->val.type == V_SET) {
fputc('#', f);
ctx->offset++;
}
fputc(start, f);
ctx->offset++;
ctx->indent = ctx->offset;
bool is_defrecord = false;
bool old_is_defrecord = ctx->is_defrecord;
if (start == '(' && coll->count > 1) {
value *v = coll->vals[0];
switch (v->type) {
case V_SYMBOL:
if (ctx->is_defrecord || is_body_indent(v) ||
(is_do_indent(v) && v->new_lines)) {
ctx->indent++;
} else if (is_def_indent(v)) {
ctx->indent++;
v->new_lines = 0;
size_t len = v->end - v->name_start;
if ((len == 5 && (!memcmp(v->name_start, "reify", 5) ||
!memcmp(v->name_start, "proxy", 5))) ||
(len == 9 && !memcmp(v->name_start, "defrecord", 9)) ||
(len == 11 &&
(!memcmp(v->name_start, "defprotocol", 11) ||
!memcmp(v->name_start, "extend-type", 11))) ||
(len == 15 &&
!memcmp(v->name_start, "extend-protocol", 15))) {
is_defrecord = true;
}
} else if (!v->new_lines && v->start[0] != '^') {
ctx->indent += (v->end - v->start + 1);
}
break;
case V_KEYWORD:
if (is_require_indent(v)) {
sort_require(coll);
ctx->indent += (v->end - v->start + 1);
v->new_lines = 0;
} else if (!v->new_lines && coll->count == 3) {
// Looks like a (:keyword map default) form,
// format as a function call.
ctx->indent += (v->end - v->start + 1);
}
break;
default:
break;
}
}
ctx->is_defrecord = is_defrecord;
for (int i = 0; i < coll->count - 1; i++) {
value *val = coll->vals[i];
format_value(val, f);
if (is_prefix(val) || is_comma(coll->vals[i + 1]))
continue;
for (int j = 0; j < val->new_lines; j++) {
fputc('\n', f);
}
if (val->new_lines) {
for (int k = 0; k < ctx->indent; k++) {
fputc(' ', f);
}
ctx->offset = ctx->indent;
} else {
fputc(' ', f);
ctx->offset++;
}
}
if (coll->count) {
format_value(coll->vals[coll->count - 1], f);
if (coll->vals[coll->count - 1]->type == V_COMMENT) {
fputc('\n', f);
for (int k = 0; k < ctx->indent; k++) {
fputc(' ', f);
}
}
}
fputc(end, f);
ctx->offset++;
ctx->indent = old_indent;
ctx->is_defrecord = old_is_defrecord;
}
static void format_value(value *val, FILE *f) {
switch (val->type) {
case V_LIST:
format_collection((collection *)val, '(', ')', f);
break;
case V_VECTOR:
format_collection((collection *)val, '[', ']', f);
break;
case V_MAP:
format_collection((collection *)val, '{', '}', f);
break;
case V_SET:
format_collection((collection *)val, '{', '}', f);
break;
case V_FN:
format_collection((collection *)val, '(', ')', f);
break;
default: {
size_t len = length(val);
fwrite(val->start, 1, len, f);
ctx->offset += len;
break;
}
}
}
#define CHUNK_SIZE 4096
static void read_stdin(context *ctx) {
ctx->size = CHUNK_SIZE;
ctx->bytes = malloc(ctx->size + 1);
char *p = ctx->bytes;
size_t cnt;
while ((cnt = fread(p, 1, CHUNK_SIZE, stdin)) == CHUNK_SIZE) {
ctx->size += CHUNK_SIZE;
ctx->bytes = realloc(ctx->bytes, ctx->size + 1);
p = ctx->bytes + ctx->size - CHUNK_SIZE;
}
ctx->size += cnt;
ctx->bytes[ctx->size] = '\0';
ctx->sp = ctx->bytes;
ctx->filename = "stdin";
}
static void read_file(const char *filename, context *ctx) {
FILE *f = fopen(filename, "rb");
if (!f) {
fprintf(stderr, "Cannot open file: %s\n", filename);
exit(2);
}
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, 0, SEEK_SET);
ctx->size = size;
ctx->bytes = malloc(size + 1);
ctx->sp = ctx->bytes;
if (1 != fread(ctx->bytes, size, 1, f)) {
fprintf(stderr, "Error reading file: %s.", filename);
exit(3);
}
fclose(f);
ctx->bytes[size] = '\0';
ctx->filename = filename;
}
static void parse_args(int argc, char **argv, options *opts) {
if (argc == 1) {
opts->input = NULL;
opts->output = NULL;
return;
} else if (argc == 2) {
opts->input = argv[1];
opts->output = argv[1];
return;
} else if (argc == 4) {
if (!strcmp(argv[1], "-o")) {
opts->input = argv[3];
opts->output = argv[2];
return;
}
if (!strcmp(argv[2], "-o")) {
opts->input = argv[1];
opts->output = argv[3];
return;
}
}
print_usage();
}
bool is_clj(const char *filename) {
size_t len = strlen(filename);
if (len < 5)
return false;
return (!strcmp(filename + (len - 4), ".clj") ||
!strcmp(filename + (len - 5), ".cljs") ||
!strcmp(filename + (len - 5), ".cljc") ||
!strcmp(filename + (len - 4), ".edn") ||
!strcmp(filename + (len - 5), ".cljd") ||
!strcmp(filename + (len - 5), ".joke"));
}
void format_file(const char *input, const char *output) {
ctx = make_context();
if (input) {
read_file(input, ctx);
} else {
read_stdin(ctx);
}
collection *forms = make_collection(V_VECTOR, NULL);
while ((ctx->last_read_val = read_value())) {
add_to_coll(forms, ctx->last_read_val);
}
FILE *out = stdout;
if (output) {
out = fopen(output, "wb");
if (!out) {
fprintf(stderr, "Cannot open file for writing: %s\n", output);
exit(2);
}
}
for (int i = 0; i < forms->count; i++) {
value *val = forms->vals[i];
format_value(val, out);
int new_lines = val->new_lines;
#ifdef SINGLE_BLANK_LINE_BETWEEN_FORMS
if (new_lines > 2) {
new_lines = 2;
}
#endif
for (int j = 0; j < new_lines; j++) {
fputc('\n', out);
ctx->offset = 0;
}
if (!val->new_lines && i < forms->count - 1 && !is_prefix(val)) {
fputc(' ', out);
ctx->offset++;
}
ctx->indent = 0;
}
fclose(out);
}
void format_dir(char *path) {
char *paths[] = {path, NULL};
FTS *ftsp = fts_open(paths, FTS_LOGICAL | FTS_NOSTAT, NULL);
if (!ftsp) {
fprintf(stderr, "Cannot open directory: %s\n", path);
exit(2);
}
FTSENT *ent;
while ((ent = fts_read(ftsp))) {
switch (ent->fts_info) {
case FTS_D:
if (ent->fts_name[0] == '.') {
fts_set(ftsp, ent, FTS_SKIP);
}
break;
case FTS_NSOK: // Means regular file since FTS_NOSTAT was passed to
// fts_open.
if (is_clj(ent->fts_name)) {
format_file(ent->fts_accpath, ent->fts_accpath);
}
break;
default:
break;
}
}
fts_close(ftsp);
}
int main(int argc, char **argv) {
options opts;
parse_args(argc, argv, &opts);
pump_tries();
DIR *d;
if (opts.input && (d = opendir(opts.input))) {
closedir(d);
format_dir(opts.input);
} else {
format_file(opts.input, opts.output);
}
}