-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathparse.c
3086 lines (2676 loc) · 76 KB
/
parse.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
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*********************************************************************
* bfs *
* Copyright (C) 2015-2017 Tavian Barnes <[email protected]> *
* *
* This program is free software. It comes without any warranty, to *
* the extent permitted by applicable law. You can redistribute it *
* and/or modify it under the terms of the Do What The Fuck You Want *
* To Public License, Version 2, as published by Sam Hocevar. See *
* the COPYING file or http://www.wtfpl.net/ for more details. *
*********************************************************************/
#include "bfs.h"
#include "dstring.h"
#include "exec.h"
#include "printf.h"
#include "typo.h"
#include "util.h"
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <grp.h>
#include <limits.h>
#include <pwd.h>
#include <regex.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
// Strings printed by -D tree for "fake" expressions
static char *fake_and_arg = "-a";
static char *fake_false_arg = "-false";
static char *fake_or_arg = "-o";
static char *fake_print_arg = "-print";
static char *fake_true_arg = "-true";
/**
* Singleton true expression instance.
*/
static struct expr expr_true = {
.eval = eval_true,
.lhs = NULL,
.rhs = NULL,
.pure = true,
.always_true = true,
.argc = 1,
.argv = &fake_true_arg,
};
/**
* Singleton false expression instance.
*/
static struct expr expr_false = {
.eval = eval_false,
.lhs = NULL,
.rhs = NULL,
.pure = true,
.always_false = true,
.argc = 1,
.argv = &fake_false_arg,
};
/**
* Free an expression.
*/
static void free_expr(struct expr *expr) {
if (expr && expr != &expr_true && expr != &expr_false) {
if (expr->cfile && expr->cfile->close) {
if (cfclose(expr->cfile) != 0) {
perror("cfclose()");
}
}
if (expr->regex) {
regfree(expr->regex);
free(expr->regex);
}
free_bfs_printf(expr->printf);
free_bfs_exec(expr->execbuf);
free_expr(expr->lhs);
free_expr(expr->rhs);
free(expr);
}
}
/**
* Create a new expression.
*/
static struct expr *new_expr(eval_fn *eval, size_t argc, char **argv) {
struct expr *expr = malloc(sizeof(struct expr));
if (!expr) {
perror("malloc()");
return NULL;
}
expr->eval = eval;
expr->lhs = NULL;
expr->rhs = NULL;
expr->pure = false;
expr->always_true = false;
expr->always_false = false;
expr->evaluations = 0;
expr->successes = 0;
expr->elapsed.tv_sec = 0;
expr->elapsed.tv_nsec = 0;
expr->argc = argc;
expr->argv = argv;
expr->cfile = NULL;
expr->regex = NULL;
expr->execbuf = NULL;
expr->printf = NULL;
return expr;
}
/**
* Create a new unary expression.
*/
static struct expr *new_unary_expr(eval_fn *eval, struct expr *rhs, char **argv) {
struct expr *expr = new_expr(eval, 1, argv);
if (!expr) {
free_expr(rhs);
return NULL;
}
expr->rhs = rhs;
return expr;
}
/**
* Create a new binary expression.
*/
static struct expr *new_binary_expr(eval_fn *eval, struct expr *lhs, struct expr *rhs, char **argv) {
struct expr *expr = new_expr(eval, 1, argv);
if (!expr) {
free_expr(rhs);
free_expr(lhs);
return NULL;
}
expr->lhs = lhs;
expr->rhs = rhs;
return expr;
}
/**
* Check if an expression never returns.
*/
bool expr_never_returns(const struct expr *expr) {
// Expressions that never return are vacuously both always true and always false
return expr->always_true && expr->always_false;
}
/**
* Set an expression to never return.
*/
static void expr_set_never_returns(struct expr *expr) {
expr->always_true = expr->always_false = true;
}
/**
* Dump the parsed expression tree, for debugging.
*/
static void dump_expr(CFILE *cfile, const struct expr *expr, bool verbose) {
fputs("(", cfile->file);
if (expr->lhs || expr->rhs) {
cfprintf(cfile, "%{red}%s%{rs}", expr->argv[0]);
} else {
cfprintf(cfile, "%{blu}%s%{rs}", expr->argv[0]);
}
for (size_t i = 1; i < expr->argc; ++i) {
cfprintf(cfile, " %{bld}%s%{rs}", expr->argv[i]);
}
if (verbose) {
double rate = 0.0, time = 0.0;
if (expr->evaluations) {
rate = 100.0*expr->successes/expr->evaluations;
time = (1.0e9*expr->elapsed.tv_sec + expr->elapsed.tv_nsec)/expr->evaluations;
}
fprintf(cfile->file, " [%zu/%zu=%g%%; %gns]", expr->successes, expr->evaluations, rate, time);
}
if (expr->lhs) {
fputs(" ", cfile->file);
dump_expr(cfile, expr->lhs, verbose);
}
if (expr->rhs) {
fputs(" ", cfile->file);
dump_expr(cfile, expr->rhs, verbose);
}
fputs(")", cfile->file);
}
/**
* Free the parsed command line.
*/
void free_cmdline(struct cmdline *cmdline) {
if (cmdline) {
free_expr(cmdline->expr);
free_bfs_mtab(cmdline->mtab);
cfclose(cmdline->cerr);
cfclose(cmdline->cout);
free_colors(cmdline->colors);
struct root *root = cmdline->roots;
while (root) {
struct root *next = root->next;
free(root);
root = next;
}
free(cmdline->argv);
free(cmdline);
}
}
/**
* Color use flags.
*/
enum use_color {
COLOR_NEVER,
COLOR_AUTO,
COLOR_ALWAYS,
};
/**
* Ephemeral state for parsing the command line.
*/
struct parser_state {
/** The command line being constructed. */
struct cmdline *cmdline;
/** The command line arguments being parsed. */
char **argv;
/** The name of this program. */
const char *command;
/** The current tail of the root path list. */
struct root **roots_tail;
/** The current regex flags to use. */
int regex_flags;
/** Whether this session is interactive. */
bool interactive;
/** Whether -color or -nocolor has been passed. */
enum use_color use_color;
/** Whether a -print action is implied. */
bool implicit_print;
/** Whether warnings are enabled (see -warn, -nowarn). */
bool warn;
/** Whether the expression has started. */
bool expr_started;
/** Whether any non-option arguments have been encountered. */
bool non_option_seen;
/** Whether an information option like -help or -version was passed. */
bool just_info;
/** A "-depth"-type argument if any. */
const char *depth_arg;
/** A "-prune"-type argument if any. */
const char *prune_arg;
/** The current time. */
struct timespec now;
};
/**
* Possible token types.
*/
enum token_type {
/** A flag. */
T_FLAG,
/** A root path. */
T_PATH,
/** An option. */
T_OPTION,
/** A test. */
T_TEST,
/** An action. */
T_ACTION,
/** An operator. */
T_OPERATOR,
};
/**
* Log an optimization.
*/
static void debug_opt(const struct parser_state *state, const char *format, ...) {
if (!(state->cmdline->debug & DEBUG_OPT)) {
return;
}
CFILE *cerr = state->cmdline->cerr;
va_list args;
va_start(args, format);
for (const char *i = format; *i != '\0'; ++i) {
if (*i == '%') {
switch (*++i) {
case '%':
fputc('%', stderr);
break;
case 'e':
dump_expr(cerr, va_arg(args, const struct expr *), false);
break;
case 's':
cfprintf(cerr, "%{red}%s%{rs}", va_arg(args, const char *));
break;
}
} else {
fputc(*i, stderr);
}
}
va_end(args);
}
/**
* Invoke stat() on an argument.
*/
static int stat_arg(const struct parser_state *state, struct expr *expr, struct stat *sb) {
const struct cmdline *cmdline = state->cmdline;
bool follow = cmdline->flags & (BFTW_COMFOLLOW | BFTW_LOGICAL);
int flags = follow ? 0 : AT_SYMLINK_NOFOLLOW;
int ret = fstatat(AT_FDCWD, expr->sdata, sb, flags);
if (ret != 0) {
cfprintf(cmdline->cerr, "%{er}error: '%s': %s%{rs}\n", expr->sdata, strerror(errno));
}
return ret;
}
/**
* Parse the expression specified on the command line.
*/
static struct expr *parse_expr(struct parser_state *state);
/**
* Advance by a single token.
*/
static char **parser_advance(struct parser_state *state, enum token_type type, size_t argc) {
if (type != T_FLAG && type != T_PATH) {
state->expr_started = true;
if (type != T_OPTION) {
state->non_option_seen = true;
}
}
char **argv = state->argv;
state->argv += argc;
return argv;
}
/**
* Parse a root path.
*/
static int parse_root(struct parser_state *state, const char *path) {
struct root *root = malloc(sizeof(struct root));
if (!root) {
perror("malloc()");
return -1;
}
root->path = path;
root->next = NULL;
*state->roots_tail = root;
state->roots_tail = &root->next;
return 0;
}
/**
* While parsing an expression, skip any paths and add them to the cmdline.
*/
static int skip_paths(struct parser_state *state) {
while (true) {
const char *arg = state->argv[0];
if (!arg) {
return 0;
}
if (arg[0] == '-') {
if (strcmp(arg, "--") == 0) {
// find uses -- to separate flags from the rest
// of the command line. We allow mixing flags
// and paths/predicates, so we just ignore --.
parser_advance(state, T_FLAG, 1);
continue;
}
if (strcmp(arg, "-") != 0) {
// - by itself is a file name. Anything else
// starting with - is a flag/predicate.
return 0;
}
}
// By POSIX, these are always options
if (strcmp(arg, "(") == 0 || strcmp(arg, "!") == 0) {
return 0;
}
if (state->expr_started) {
// By POSIX, these can be paths. We only treat them as
// such at the beginning of the command line.
if (strcmp(arg, ")") == 0 || strcmp(arg, ",") == 0) {
return 0;
}
}
if (parse_root(state, arg) != 0) {
return -1;
}
parser_advance(state, T_PATH, 1);
}
}
/** Integer parsing flags. */
enum int_flags {
IF_BASE_MASK = 0x03F,
IF_INT = 0x040,
IF_LONG = 0x080,
IF_LONG_LONG = 0x0C0,
IF_SIZE_MASK = 0x0C0,
IF_UNSIGNED = 0x100,
IF_PARTIAL_OK = 0x200,
IF_QUIET = 0x400,
};
/**
* Parse an integer.
*/
static const char *parse_int(const struct parser_state *state, const char *str, void *result, enum int_flags flags) {
char *endptr;
int base = flags & IF_BASE_MASK;
if (base == 0) {
base = 10;
}
errno = 0;
long long value = strtoll(str, &endptr, base);
if (errno != 0) {
goto bad;
}
if (endptr == str) {
goto bad;
}
if (!(flags & IF_PARTIAL_OK) && *endptr != '\0') {
goto bad;
}
if ((flags & IF_UNSIGNED) && value < 0) {
goto bad;
}
switch (flags & IF_SIZE_MASK) {
case IF_INT:
if (value < INT_MIN || value > INT_MAX) {
goto bad;
}
*(int *)result = value;
break;
case IF_LONG:
if (value < LONG_MIN || value > LONG_MAX) {
goto bad;
}
*(long *)result = value;
break;
case IF_LONG_LONG:
*(long long *)result = value;
break;
}
return endptr;
bad:
if (!(flags & IF_QUIET)) {
cfprintf(state->cmdline->cerr, "%{er}error: '%s' is not a valid integer.%{rs}\n", str);
}
return NULL;
}
/**
* Parse an integer and a comparison flag.
*/
static const char *parse_icmp(const struct parser_state *state, const char *str, struct expr *expr, enum int_flags flags) {
switch (str[0]) {
case '-':
expr->cmp_flag = CMP_LESS;
++str;
break;
case '+':
expr->cmp_flag = CMP_GREATER;
++str;
break;
default:
expr->cmp_flag = CMP_EXACT;
break;
}
return parse_int(state, str, &expr->idata, flags | IF_LONG_LONG | IF_UNSIGNED);
}
/**
* Check if a string could be an integer comparison.
*/
static bool looks_like_icmp(const char *str) {
if (str[0] == '-' || str[0] == '+') {
++str;
}
return str[0] >= '0' && str[0] <= '9';
}
/**
* Parse a single flag.
*/
static struct expr *parse_flag(struct parser_state *state, size_t argc) {
parser_advance(state, T_FLAG, argc);
return &expr_true;
}
/**
* Parse a flag that doesn't take a value.
*/
static struct expr *parse_nullary_flag(struct parser_state *state) {
return parse_flag(state, 1);
}
/**
* Parse a flag that takes a single value.
*/
static struct expr *parse_unary_flag(struct parser_state *state) {
return parse_flag(state, 2);
}
/**
* Parse a single option.
*/
static struct expr *parse_option(struct parser_state *state, size_t argc) {
const char *arg = *parser_advance(state, T_OPTION, argc);
if (state->warn && state->non_option_seen) {
cfprintf(state->cmdline->cerr,
"%{wr}warning: The '%s' option applies to the entire command line. For clarity, place\n"
"it before any non-option arguments.%{rs}\n\n",
arg);
}
return &expr_true;
}
/**
* Parse an option that doesn't take a value.
*/
static struct expr *parse_nullary_option(struct parser_state *state) {
return parse_option(state, 1);
}
/**
* Parse an option that takes a value.
*/
static struct expr *parse_unary_option(struct parser_state *state) {
return parse_option(state, 2);
}
/**
* Parse a single positional option.
*/
static struct expr *parse_positional_option(struct parser_state *state, size_t argc) {
parser_advance(state, T_OPTION, argc);
return &expr_true;
}
/**
* Parse a positional option that doesn't take a value.
*/
static struct expr *parse_nullary_positional_option(struct parser_state *state) {
return parse_positional_option(state, 1);
}
/**
* Parse a positional option that takes a single value.
*/
static struct expr *parse_unary_positional_option(struct parser_state *state, const char **value) {
const char *arg = state->argv[0];
*value = state->argv[1];
if (!*value) {
cfprintf(state->cmdline->cerr, "%{er}error: %s needs a value.%{rs}\n", arg);
return NULL;
}
return parse_positional_option(state, 2);
}
/**
* Parse a single test.
*/
static struct expr *parse_test(struct parser_state *state, eval_fn *eval, size_t argc) {
char **argv = parser_advance(state, T_TEST, argc);
struct expr *expr = new_expr(eval, argc, argv);
if (expr) {
expr->pure = true;
}
return expr;
}
/**
* Parse a test that doesn't take a value.
*/
static struct expr *parse_nullary_test(struct parser_state *state, eval_fn *eval) {
return parse_test(state, eval, 1);
}
/**
* Parse a test that takes a value.
*/
static struct expr *parse_unary_test(struct parser_state *state, eval_fn *eval) {
const char *arg = state->argv[0];
const char *value = state->argv[1];
if (!value) {
cfprintf(state->cmdline->cerr, "%{er}error: %s needs a value.%{rs}\n", arg);
return NULL;
}
struct expr *expr = parse_test(state, eval, 2);
if (expr) {
expr->sdata = value;
}
return expr;
}
/**
* Parse a single action.
*/
static struct expr *parse_action(struct parser_state *state, eval_fn *eval, size_t argc) {
if (eval != eval_nohidden && eval != eval_prune && eval != eval_quit) {
state->implicit_print = false;
}
char **argv = parser_advance(state, T_ACTION, argc);
return new_expr(eval, argc, argv);
}
/**
* Parse an action that takes no arguments.
*/
static struct expr *parse_nullary_action(struct parser_state *state, eval_fn *eval) {
return parse_action(state, eval, 1);
}
/**
* Parse an action that takes one argument.
*/
static struct expr *parse_unary_action(struct parser_state *state, eval_fn *eval) {
const char *arg = state->argv[0];
const char *value = state->argv[1];
if (!value) {
cfprintf(state->cmdline->cerr, "%{er}error: %s needs a value.%{rs}\n", arg);
return NULL;
}
struct expr *expr = parse_action(state, eval, 2);
if (expr) {
expr->sdata = value;
}
return expr;
}
/**
* Parse a test expression with integer data and a comparison flag.
*/
static struct expr *parse_test_icmp(struct parser_state *state, eval_fn *eval) {
struct expr *expr = parse_unary_test(state, eval);
if (!expr) {
return NULL;
}
if (!parse_icmp(state, expr->sdata, expr, 0)) {
free_expr(expr);
return NULL;
}
return expr;
}
/**
* Parse -D FLAG.
*/
static struct expr *parse_debug(struct parser_state *state, int arg1, int arg2) {
struct cmdline *cmdline = state->cmdline;
const char *arg = state->argv[0];
const char *flag = state->argv[1];
if (!flag) {
cfprintf(cmdline->cerr, "%{er}error: %s needs a flag.%{rs}\n", arg);
return NULL;
}
if (strcmp(flag, "help") == 0) {
printf("Supported debug flags:\n\n");
printf(" help: This message.\n");
printf(" opt: Print optimization details.\n");
printf(" rates: Print predicate success rates.\n");
printf(" stat: Trace all stat() calls.\n");
printf(" tree: Print the parse tree.\n");
state->just_info = true;
return NULL;
} else if (strcmp(flag, "exec") == 0) {
cmdline->debug |= DEBUG_EXEC;
} else if (strcmp(flag, "opt") == 0) {
cmdline->debug |= DEBUG_OPT;
} else if (strcmp(flag, "rates") == 0) {
cmdline->debug |= DEBUG_RATES;
} else if (strcmp(flag, "stat") == 0) {
cmdline->debug |= DEBUG_STAT;
} else if (strcmp(flag, "tree") == 0) {
cmdline->debug |= DEBUG_TREE;
} else {
cfprintf(cmdline->cerr, "%{wr}warning: Unrecognized debug flag '%s'.%{rs}\n\n", flag);
}
return parse_unary_flag(state);
}
/**
* Parse -On.
*/
static struct expr *parse_optlevel(struct parser_state *state, int arg1, int arg2) {
int *optlevel = &state->cmdline->optlevel;
if (strcmp(state->argv[0], "-Ofast") == 0) {
*optlevel = 4;
} else if (!parse_int(state, state->argv[0] + 2, optlevel, IF_INT)) {
return NULL;
}
if (state->warn && *optlevel > 4) {
cfprintf(state->cmdline->cerr, "%{wr}warning: %s is the same as -O4.%{rs}\n\n", state->argv[0]);
}
return parse_nullary_flag(state);
}
/**
* Parse -[PHL], -(no)?follow.
*/
static struct expr *parse_follow(struct parser_state *state, int flags, int option) {
struct cmdline *cmdline = state->cmdline;
cmdline->flags &= ~(BFTW_COMFOLLOW | BFTW_LOGICAL | BFTW_DETECT_CYCLES);
cmdline->flags |= flags;
if (option) {
return parse_nullary_positional_option(state);
} else {
return parse_nullary_flag(state);
}
}
/**
* Parse -X.
*/
static struct expr *parse_xargs_safe(struct parser_state *state, int arg1, int arg2) {
state->cmdline->xargs_safe = true;
return parse_nullary_flag(state);
}
/**
* Parse -executable, -readable, -writable
*/
static struct expr *parse_access(struct parser_state *state, int flag, int arg2) {
struct expr *expr = parse_nullary_test(state, eval_access);
if (expr) {
expr->idata = flag;
}
return expr;
}
/**
* Parse -[acm]{min,time}.
*/
static struct expr *parse_acmtime(struct parser_state *state, int field, int unit) {
struct expr *expr = parse_test_icmp(state, eval_acmtime);
if (expr) {
expr->reftime = state->now;
expr->time_field = field;
expr->time_unit = unit;
}
return expr;
}
/**
* Parse -[ac]?newer.
*/
static struct expr *parse_acnewer(struct parser_state *state, int field, int arg2) {
struct expr *expr = parse_unary_test(state, eval_acnewer);
if (!expr) {
return NULL;
}
struct stat sb;
if (stat_arg(state, expr, &sb) != 0) {
free_expr(expr);
return NULL;
}
expr->reftime = sb.st_mtim;
expr->time_field = field;
return expr;
}
/**
* Parse -(no)?color.
*/
static struct expr *parse_color(struct parser_state *state, int color, int arg2) {
struct cmdline *cmdline = state->cmdline;
struct colors *colors = cmdline->colors;
if (color) {
state->use_color = COLOR_ALWAYS;
cmdline->cout->colors = colors;
cmdline->cerr->colors = colors;
} else {
state->use_color = COLOR_NEVER;
cmdline->cout->colors = NULL;
cmdline->cerr->colors = NULL;
}
return parse_nullary_option(state);
}
/**
* Parse -{false,true}.
*/
static struct expr *parse_const(struct parser_state *state, int value, int arg2) {
parser_advance(state, T_TEST, 1);
return value ? &expr_true : &expr_false;
}
/**
* Parse -daystart.
*/
static struct expr *parse_daystart(struct parser_state *state, int arg1, int arg2) {
struct tm tm;
if (xlocaltime(&state->now.tv_sec, &tm) != 0) {
perror("xlocaltime()");
return NULL;
}
if (tm.tm_hour || tm.tm_min || tm.tm_sec || state->now.tv_nsec) {
++tm.tm_mday;
}
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
time_t time = mktime(&tm);
if (time == -1) {
perror("mktime()");
return NULL;
}
state->now.tv_sec = time;
state->now.tv_nsec = 0;
return parse_nullary_positional_option(state);
}
/**
* Parse -delete.
*/
static struct expr *parse_delete(struct parser_state *state, int arg1, int arg2) {
state->cmdline->flags |= BFTW_DEPTH;
state->depth_arg = state->argv[0];
return parse_nullary_action(state, eval_delete);
}
/**
* Parse -d, -depth.
*/
static struct expr *parse_depth(struct parser_state *state, int arg1, int arg2) {
state->cmdline->flags |= BFTW_DEPTH;
state->depth_arg = state->argv[0];
return parse_nullary_option(state);
}
/**
* Parse -depth [N].
*/
static struct expr *parse_depth_n(struct parser_state *state, int arg1, int arg2) {
const char *arg = state->argv[1];
if (arg && looks_like_icmp(arg)) {
return parse_test_icmp(state, eval_depth);
} else {
return parse_depth(state, arg1, arg2);
}
}
/**
* Parse -{min,max}depth N.
*/
static struct expr *parse_depth_limit(struct parser_state *state, int is_min, int arg2) {
struct cmdline *cmdline = state->cmdline;
const char *arg = state->argv[0];
const char *value = state->argv[1];
if (!value) {
cfprintf(cmdline->cerr, "%{er}error: %s needs a value.%{rs}\n", arg);
return NULL;
}
int *depth = is_min ? &cmdline->mindepth : &cmdline->maxdepth;
if (!parse_int(state, value, depth, IF_INT | IF_UNSIGNED)) {
return NULL;
}
return parse_unary_option(state);
}
/**
* Parse -empty.
*/
static struct expr *parse_empty(struct parser_state *state, int arg1, int arg2) {
return parse_nullary_test(state, eval_empty);
}
/**
* Parse -exec(dir)?/-ok(dir)?.
*/
static struct expr *parse_exec(struct parser_state *state, int flags, int arg2) {
struct bfs_exec *execbuf = parse_bfs_exec(state->argv, flags, state->cmdline);
if (!execbuf) {
return NULL;
}
if ((execbuf->flags & BFS_EXEC_CHDIR) && (execbuf->flags & BFS_EXEC_MULTI)) {
++state->cmdline->nopen_files;
}
struct expr *expr = parse_action(state, eval_exec, execbuf->tmpl_argc + 2);
if (!expr) {
free_bfs_exec(execbuf);
return NULL;
}
if (execbuf->flags & BFS_EXEC_MULTI) {
expr->always_true = true;
}
expr->execbuf = execbuf;
return expr;
}
/**
* Parse -exit [STATUS].
*/
static struct expr *parse_exit(struct parser_state *state, int arg1, int arg2) {
size_t argc = 1;
const char *value = state->argv[1];
int status = EXIT_SUCCESS;
if (value && parse_int(state, value, &status, IF_INT | IF_UNSIGNED | IF_QUIET)) {
argc = 2;
}
struct expr *expr = parse_action(state, eval_exit, argc);
if (expr) {
expr_set_never_returns(expr);
expr->idata = status;
}
return expr;
}
/**
* Parse -f PATH.
*/
static struct expr *parse_f(struct parser_state *state, int arg1, int arg2) {
parser_advance(state, T_FLAG, 1);