-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuiltin_string.cpp
1406 lines (1228 loc) · 48.3 KB
/
builtin_string.cpp
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
// Implementation of the string builtin.
#include "config.h"
#define PCRE2_CODE_UNIT_WIDTH WCHAR_T_BITS
#ifdef _WIN32
#define PCRE2_STATIC
#endif
#include <errno.h>
#include <limits.h>
#include <stdarg.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <wchar.h>
#include <wctype.h>
#include <algorithm>
#include <cwctype>
#include <iterator>
#include <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
#include "builtin.h"
#include "common.h"
#include "fallback.h" // IWYU pragma: keep
#include "io.h"
#include "parse_util.h"
#include "pcre2.h"
#include "wcstringutil.h"
#include "wgetopt.h"
#include "wildcard.h"
#include "wutil.h" // IWYU pragma: keep
class parser_t;
#define STRING_ERR_MISSING _(L"%ls: Expected argument\n")
// How many bytes we read() at once.
// Bash uses 128 here, so we do too (see READ_CHUNK_SIZE).
// This should be about the size of a line.
#define STRING_CHUNK_SIZE 128
static void string_error(io_streams_t &streams, const wchar_t *fmt, ...) {
streams.err.append(L"string ");
va_list va;
va_start(va, fmt);
streams.err.append_formatv(fmt, va);
va_end(va);
}
static void string_unknown_option(parser_t &parser, io_streams_t &streams, const wchar_t *subcmd,
const wchar_t *opt) {
string_error(streams, BUILTIN_ERR_UNKNOWN, subcmd, opt);
builtin_print_help(parser, streams, L"string", streams.err);
}
// We read from stdin if we are the second or later process in a pipeline.
static bool string_args_from_stdin(const io_streams_t &streams) {
return streams.stdin_is_directly_redirected;
}
static const wchar_t *string_get_arg_argv(int *argidx, const wchar_t *const *argv) {
return argv && argv[*argidx] ? argv[(*argidx)++] : NULL;
}
// A helper type for extracting arguments from either argv or stdin.
namespace {
class arg_iterator_t {
// The list of arguments passed to the string builtin.
const wchar_t *const *argv_;
// If using argv, index of the next argument to return.
int argidx_;
// If not using argv, a string to store bytes that have been read but not yet returned.
std::string buffer_;
// Backing storage for the next() string.
wcstring storage_;
const io_streams_t &streams_;
/// \return the next argument from stdin
const wchar_t *get_arg_stdin() {
assert(string_args_from_stdin(streams_) && "should not be reading from stdin");
// Read in chunks from fd until buffer has a line.
size_t pos;
while ((pos = buffer_.find('\n')) == std::string::npos) {
char buf[STRING_CHUNK_SIZE];
long n = read_blocked(streams_.stdin_fd, buf, STRING_CHUNK_SIZE);
if (n == 0) {
// If we still have buffer contents, flush them,
// in case there was no trailing '\n'.
if (buffer_.empty()) return NULL;
storage_ = str2wcstring(buffer_);
buffer_.clear();
return storage_.c_str();
}
if (n == -1) {
// Some error happened. We can't do anything about it,
// so ignore it.
// (read_blocked already retries for EAGAIN and EINTR)
storage_ = str2wcstring(buffer_);
buffer_.clear();
return NULL;
}
buffer_.append(buf, n);
}
// Split the buffer on the '\n' and return the first part.
storage_ = str2wcstring(buffer_, pos);
buffer_.erase(0, pos + 1);
return storage_.c_str();
}
public:
arg_iterator_t(const wchar_t *const *argv, int argidx, const io_streams_t &streams)
: argv_(argv), argidx_(argidx), streams_(streams) {}
/// \return the next argument, or null if the argument list is exhausted.
const wchar_t *next() {
if (string_args_from_stdin(streams_)) {
return get_arg_stdin();
}
return string_get_arg_argv(&argidx_, argv_);
}
};
}
// This is used by the string subcommands to communicate with the option parser which flags are
// valid and get the result of parsing the command for flags.
typedef struct { //!OCLINT(too many fields)
bool all_valid = false;
bool chars_valid = false;
bool count_valid = false;
bool entire_valid = false;
bool filter_valid = false;
bool ignore_case_valid = false;
bool index_valid = false;
bool invert_valid = false;
bool left_valid = false;
bool length_valid = false;
bool max_valid = false;
bool no_newline_valid = false;
bool no_quoted_valid = false;
bool quiet_valid = false;
bool regex_valid = false;
bool right_valid = false;
bool start_valid = false;
bool style_valid = false;
bool all = false;
bool entire = false;
bool filter = false;
bool ignore_case = false;
bool index = false;
bool invert_match = false;
bool left = false;
bool no_newline = false;
bool no_quoted = false;
bool quiet = false;
bool regex = false;
bool right = false;
long count = 0;
long length = 0;
long max = 0;
long start = 0;
const wchar_t *chars_to_trim = L" \f\n\r\t";
const wchar_t *arg1 = NULL;
const wchar_t *arg2 = NULL;
escape_string_style_t escape_style = STRING_STYLE_SCRIPT;
} options_t;
/// This handles the `--style=xxx` flag.
static int handle_flag_1(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
const wchar_t *cmd = argv[0];
if (opts->style_valid) {
if (wcscmp(w.woptarg, L"script") == 0) {
opts->escape_style = STRING_STYLE_SCRIPT;
} else if (wcscmp(w.woptarg, L"url") == 0) {
opts->escape_style = STRING_STYLE_URL;
} else if (wcscmp(w.woptarg, L"var") == 0) {
opts->escape_style = STRING_STYLE_VAR;
} else {
string_error(streams, _(L"%ls: Invalid escape style '%ls'\n"), cmd, w.woptarg);
return STATUS_INVALID_ARGS;
}
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_N(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->no_newline_valid) {
opts->no_newline = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_a(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->all_valid) {
opts->all = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_c(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->chars_valid) {
opts->chars_to_trim = w.woptarg;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_e(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->entire_valid) {
opts->entire = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_f(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->filter_valid) {
opts->filter = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_i(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->ignore_case_valid) {
opts->ignore_case = true;
return STATUS_CMD_OK;
} else if (opts->index_valid) {
opts->index = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_l(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->length_valid) {
opts->length = fish_wcstol(w.woptarg);
if (opts->length < 0 || opts->length == LONG_MIN || errno == ERANGE) {
string_error(streams, _(L"%ls: Invalid length value '%ls'\n"), argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
} else if (errno) {
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
}
return STATUS_CMD_OK;
} else if (opts->left_valid) {
opts->left = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_m(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->max_valid) {
opts->max = fish_wcstol(w.woptarg);
if (opts->max < 0 || errno == ERANGE) {
string_error(streams, _(L"%ls: Invalid max value '%ls'\n"), argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
} else if (errno) {
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
}
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_n(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->count_valid) {
opts->count = fish_wcstol(w.woptarg);
if (opts->count < 0 || errno == ERANGE) {
string_error(streams, _(L"%ls: Invalid count value '%ls'\n"), argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
} else if (errno) {
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
}
return STATUS_CMD_OK;
} else if (opts->index_valid) {
opts->index = true;
return STATUS_CMD_OK;
} else if (opts->no_quoted_valid) {
opts->no_quoted = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_q(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->quiet_valid) {
opts->quiet = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_r(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->regex_valid) {
opts->regex = true;
return STATUS_CMD_OK;
} else if (opts->right_valid) {
opts->right = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_s(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->start_valid) {
opts->start = fish_wcstol(w.woptarg);
if (opts->start == 0 || opts->start == LONG_MIN || errno == ERANGE) {
string_error(streams, _(L"%ls: Invalid start value '%ls'\n"), argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
} else if (errno) {
string_error(streams, BUILTIN_ERR_NOT_NUMBER, argv[0], w.woptarg);
return STATUS_INVALID_ARGS;
}
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
static int handle_flag_v(wchar_t **argv, parser_t &parser, io_streams_t &streams, wgetopter_t &w,
options_t *opts) {
if (opts->invert_valid) {
opts->invert_match = true;
return STATUS_CMD_OK;
}
string_unknown_option(parser, streams, argv[0], argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
}
/// This constructs the wgetopt() short options string based on which arguments are valid for the
/// subcommand. We have to do this because many short flags have multiple meanings and may or may
/// not require an argument depending on the meaning.
static wcstring construct_short_opts(options_t *opts) { //!OCLINT(high npath complexity)
wcstring short_opts(L":");
if (opts->all_valid) short_opts.append(L"a");
if (opts->chars_valid) short_opts.append(L"c:");
if (opts->count_valid) short_opts.append(L"n:");
if (opts->entire_valid) short_opts.append(L"e");
if (opts->filter_valid) short_opts.append(L"f");
if (opts->ignore_case_valid) short_opts.append(L"i");
if (opts->index_valid) short_opts.append(L"n");
if (opts->invert_valid) short_opts.append(L"v");
if (opts->left_valid) short_opts.append(L"l");
if (opts->length_valid) short_opts.append(L"l:");
if (opts->max_valid) short_opts.append(L"m:");
if (opts->no_newline_valid) short_opts.append(L"N");
if (opts->no_quoted_valid) short_opts.append(L"n");
if (opts->quiet_valid) short_opts.append(L"q");
if (opts->regex_valid) short_opts.append(L"r");
if (opts->right_valid) short_opts.append(L"r");
if (opts->start_valid) short_opts.append(L"s:");
return short_opts;
}
// Note that several long flags share the same short flag. That is okay. The caller is expected
// to indicate that a max of one of the long flags sharing a short flag is valid.
static const struct woption long_options[] = {{L"all", no_argument, NULL, 'a'},
{L"chars", required_argument, NULL, 'c'},
{L"count", required_argument, NULL, 'n'},
{L"entire", no_argument, NULL, 'e'},
{L"filter", no_argument, NULL, 'f'},
{L"ignore-case", no_argument, NULL, 'i'},
{L"index", no_argument, NULL, 'n'},
{L"invert", no_argument, NULL, 'v'},
{L"left", no_argument, NULL, 'l'},
{L"length", required_argument, NULL, 'l'},
{L"max", required_argument, NULL, 'm'},
{L"no-newline", no_argument, NULL, 'N'},
{L"no-quoted", no_argument, NULL, 'n'},
{L"quiet", no_argument, NULL, 'q'},
{L"regex", no_argument, NULL, 'r'},
{L"right", no_argument, NULL, 'r'},
{L"start", required_argument, NULL, 's'},
{L"style", required_argument, NULL, 1},
{NULL, 0, NULL, 0}};
static std::unordered_map<char, decltype(*handle_flag_N)> flag_to_function = {
{'N', handle_flag_N}, {'a', handle_flag_a}, {'c', handle_flag_c}, {'e', handle_flag_e},
{'f', handle_flag_f}, {'i', handle_flag_i}, {'l', handle_flag_l}, {'m', handle_flag_m},
{'n', handle_flag_n}, {'q', handle_flag_q}, {'r', handle_flag_r}, {'s', handle_flag_s},
{'v', handle_flag_v}, {1, handle_flag_1}};
/// Parse the arguments for flags recognized by a specific string subcommand.
static int parse_opts(options_t *opts, int *optind, int n_req_args, int argc, wchar_t **argv,
parser_t &parser, io_streams_t &streams) {
const wchar_t *cmd = argv[0];
wcstring short_opts = construct_short_opts(opts);
const wchar_t *short_options = short_opts.c_str();
int opt;
wgetopter_t w;
while ((opt = w.wgetopt_long(argc, argv, short_options, long_options, NULL)) != -1) {
auto fn = flag_to_function.find(opt);
if (fn != flag_to_function.end()) {
int retval = fn->second(argv, parser, streams, w, opts);
if (retval != STATUS_CMD_OK) return retval;
} else if (opt == ':') {
string_error(streams, STRING_ERR_MISSING, cmd);
return STATUS_INVALID_ARGS;
} else if (opt == '?') {
string_unknown_option(parser, streams, cmd, argv[w.woptind - 1]);
return STATUS_INVALID_ARGS;
} else {
DIE("unexpected retval from wgetopt_long");
}
}
*optind = w.woptind;
// If the caller requires one or two mandatory args deal with that here.
if (n_req_args) {
opts->arg1 = string_get_arg_argv(optind, argv);
if (!opts->arg1) {
string_error(streams, STRING_ERR_MISSING, cmd);
return STATUS_INVALID_ARGS;
}
}
if (n_req_args > 1) {
opts->arg2 = string_get_arg_argv(optind, argv);
if (!opts->arg2) {
string_error(streams, STRING_ERR_MISSING, cmd);
return STATUS_INVALID_ARGS;
}
}
// At this point we should not have optional args and be reading args from stdin.
if (string_args_from_stdin(streams) && argc > *optind) {
string_error(streams, BUILTIN_ERR_TOO_MANY_ARGUMENTS, cmd);
return STATUS_INVALID_ARGS;
}
return STATUS_CMD_OK;
}
/// Escape a string so that it can be used in a fish script without further word splitting.
static int string_escape_script(options_t &opts, int optind, wchar_t **argv,
io_streams_t &streams) {
int nesc = 0;
escape_flags_t flags = ESCAPE_ALL;
if (opts.no_quoted) flags |= ESCAPE_NO_QUOTED;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
streams.out.append(escape_string(arg, flags, STRING_STYLE_SCRIPT));
streams.out.append(L'\n');
nesc++;
}
return nesc > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
/// Escape a string so that it can be used as a URL.
static int string_escape_url(options_t &opts, int optind, wchar_t **argv, io_streams_t &streams) {
UNUSED(opts);
int nesc = 0;
escape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
streams.out.append(escape_string(arg, flags, STRING_STYLE_URL));
streams.out.append(L'\n');
nesc++;
}
return nesc > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
/// Escape a string so that it can be used as a fish var name.
static int string_escape_var(options_t &opts, int optind, wchar_t **argv, io_streams_t &streams) {
UNUSED(opts);
int nesc = 0;
escape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
streams.out.append(escape_string(arg, flags, STRING_STYLE_VAR));
streams.out.append(L'\n');
nesc++;
}
return nesc > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
/// Unescape a string encoded so it can be used in fish script.
static int string_unescape_script(options_t &opts, int optind, wchar_t **argv,
io_streams_t &streams) {
UNUSED(opts);
int nesc = 0;
unescape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
wcstring result;
if (unescape_string(arg, &result, flags, STRING_STYLE_SCRIPT)) {
streams.out.append(result);
streams.out.append(L'\n');
nesc++;
}
}
return nesc > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
/// Unescape an encoded URL.
static int string_unescape_url(options_t &opts, int optind, wchar_t **argv, io_streams_t &streams) {
UNUSED(opts);
int nesc = 0;
unescape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
wcstring result;
if (unescape_string(arg, &result, flags, STRING_STYLE_URL)) {
streams.out.append(result);
streams.out.append(L'\n');
nesc++;
}
}
return nesc > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
/// Unescape an encoded var name.
static int string_unescape_var(options_t &opts, int optind, wchar_t **argv, io_streams_t &streams) {
UNUSED(opts);
int nesc = 0;
unescape_flags_t flags = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
wcstring result;
if (unescape_string(arg, &result, flags, STRING_STYLE_VAR)) {
streams.out.append(result);
streams.out.append(L'\n');
nesc++;
}
}
return nesc > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
static int string_escape(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
options_t opts;
opts.no_quoted_valid = true;
opts.style_valid = true;
int optind;
int retval = parse_opts(&opts, &optind, 0, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval;
switch (opts.escape_style) {
case STRING_STYLE_SCRIPT: {
return string_escape_script(opts, optind, argv, streams);
}
case STRING_STYLE_URL: {
return string_escape_url(opts, optind, argv, streams);
}
case STRING_STYLE_VAR: {
return string_escape_var(opts, optind, argv, streams);
}
}
DIE("should never reach this statement");
}
static int string_unescape(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
options_t opts;
opts.no_quoted_valid = true;
opts.style_valid = true;
int optind;
int retval = parse_opts(&opts, &optind, 0, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval;
switch (opts.escape_style) {
case STRING_STYLE_SCRIPT: {
return string_unescape_script(opts, optind, argv, streams);
}
case STRING_STYLE_URL: {
return string_unescape_url(opts, optind, argv, streams);
}
case STRING_STYLE_VAR: {
return string_unescape_var(opts, optind, argv, streams);
}
}
DIE("should never reach this statement");
}
static int string_join(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
options_t opts;
opts.quiet_valid = true;
int optind;
int retval = parse_opts(&opts, &optind, 1, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval;
const wchar_t *sep = opts.arg1;
int nargs = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
if (!opts.quiet) {
if (nargs > 0) {
streams.out.append(sep);
}
streams.out.append(arg);
}
nargs++;
}
if (nargs > 0 && !opts.quiet) {
streams.out.push_back(L'\n');
}
return nargs > 1 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
static int string_length(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
options_t opts;
opts.quiet_valid = true;
int optind;
int retval = parse_opts(&opts, &optind, 0, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval;
int nnonempty = 0;
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
size_t n = wcslen(arg);
if (n > 0) {
nnonempty++;
}
if (!opts.quiet) {
streams.out.append(to_string(n));
streams.out.append(L'\n');
}
}
return nnonempty > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
class string_matcher_t {
protected:
options_t opts;
io_streams_t &streams;
int total_matched;
public:
string_matcher_t(options_t opts_, io_streams_t &streams_)
: opts(std::move(opts_)), streams(streams_), total_matched(0) {}
virtual ~string_matcher_t() = default;
virtual bool report_matches(const wchar_t *arg) = 0;
int match_count() { return total_matched; }
};
class wildcard_matcher_t : public string_matcher_t {
private:
wcstring wcpattern;
public:
wildcard_matcher_t(const wchar_t * /*argv0*/, const wchar_t *pattern, const options_t &opts,
io_streams_t &streams)
: string_matcher_t(opts, streams), wcpattern(parse_util_unescape_wildcards(pattern)) {
if (opts.ignore_case) {
for (size_t i = 0; i < wcpattern.length(); i++) {
wcpattern[i] = towlower(wcpattern[i]);
}
}
if (opts.entire && !wcpattern.empty()) {
if (wcpattern.front() != ANY_STRING) wcpattern.insert(0, 1, ANY_STRING);
if (wcpattern.back() != ANY_STRING) wcpattern.push_back(ANY_STRING);
}
}
~wildcard_matcher_t() override = default;
bool report_matches(const wchar_t *arg) override {
// Note: --all is a no-op for glob matching since the pattern is always matched
// against the entire argument.
bool match;
if (opts.ignore_case) {
wcstring s = arg;
for (size_t i = 0; i < s.length(); i++) {
s[i] = towlower(s[i]);
}
match = wildcard_match(s, wcpattern, false);
} else {
match = wildcard_match(arg, wcpattern, false);
}
if (match ^ opts.invert_match) {
total_matched++;
if (!opts.quiet) {
if (opts.index) {
streams.out.append_format(L"1 %lu\n", wcslen(arg));
} else {
streams.out.append(arg);
streams.out.append(L'\n');
}
}
}
return true;
}
};
static wcstring pcre2_strerror(int err_code) {
wchar_t buf[128];
pcre2_get_error_message(err_code, (PCRE2_UCHAR *)buf, sizeof(buf) / sizeof(wchar_t));
return buf;
}
struct compiled_regex_t {
pcre2_code *code;
pcre2_match_data *match;
compiled_regex_t(const wchar_t *argv0, const wchar_t *pattern, bool ignore_case,
io_streams_t &streams)
: code(0), match(0) {
// Disable some sequences that can lead to security problems.
uint32_t options = PCRE2_NEVER_UTF;
#if PCRE2_CODE_UNIT_WIDTH < 32
options |= PCRE2_NEVER_BACKSLASH_C;
#endif
int err_code = 0;
PCRE2_SIZE err_offset = 0;
code =
pcre2_compile(PCRE2_SPTR(pattern), PCRE2_ZERO_TERMINATED,
options | (ignore_case ? PCRE2_CASELESS : 0), &err_code, &err_offset, 0);
if (code == 0) {
string_error(streams, _(L"%ls: Regular expression compile error: %ls\n"), argv0,
pcre2_strerror(err_code).c_str());
string_error(streams, L"%ls: %ls\n", argv0, pattern);
string_error(streams, L"%ls: %*ls\n", argv0, err_offset, L"^");
return;
}
match = pcre2_match_data_create_from_pattern(code, 0);
assert(match);
}
~compiled_regex_t() {
if (match != 0) {
pcre2_match_data_free(match);
}
if (code != 0) {
pcre2_code_free(code);
}
}
};
class pcre2_matcher_t : public string_matcher_t {
const wchar_t *argv0;
compiled_regex_t regex;
int report_match(const wchar_t *arg, int pcre2_rc) {
// Return values: -1 = error, 0 = no match, 1 = match.
if (pcre2_rc == PCRE2_ERROR_NOMATCH) {
if (opts.invert_match && !opts.quiet) {
if (opts.index) {
streams.out.append_format(L"1 %lu\n", wcslen(arg));
} else {
streams.out.append(arg);
streams.out.push_back(L'\n');
}
}
return opts.invert_match ? 1 : 0;
} else if (pcre2_rc < 0) {
string_error(streams, _(L"%ls: Regular expression match error: %ls\n"), argv0,
pcre2_strerror(pcre2_rc).c_str());
return -1;
} else if (pcre2_rc == 0) {
// The output vector wasn't big enough. Should not happen.
string_error(streams, _(L"%ls: Regular expression internal error\n"), argv0);
return -1;
} else if (opts.invert_match) {
return 0;
}
if (opts.entire) {
streams.out.append(arg);
streams.out.push_back(L'\n');
}
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(regex.match);
for (int j = (opts.entire ? 1 : 0); j < pcre2_rc; j++) {
PCRE2_SIZE begin = ovector[2 * j];
PCRE2_SIZE end = ovector[2 * j + 1];
if (begin != PCRE2_UNSET && end != PCRE2_UNSET && !opts.quiet) {
if (opts.index) {
streams.out.append_format(L"%lu %lu", (unsigned long)(begin + 1),
(unsigned long)(end - begin));
} else if (end > begin) {
// May have end < begin if \K is used.
streams.out.append(wcstring(&arg[begin], end - begin));
}
streams.out.push_back(L'\n');
}
}
return opts.invert_match ? 0 : 1;
}
public:
pcre2_matcher_t(const wchar_t *argv0_, const wchar_t *pattern, const options_t &opts,
io_streams_t &streams)
: string_matcher_t(opts, streams),
argv0(argv0_),
regex(argv0_, pattern, opts.ignore_case, streams) {}
~pcre2_matcher_t() override = default;
bool report_matches(const wchar_t *arg) override {
// A return value of true means all is well (even if no matches were found), false indicates
// an unrecoverable error.
if (regex.code == 0) {
// pcre2_compile() failed.
return false;
}
int matched = 0;
// See pcre2demo.c for an explanation of this logic.
PCRE2_SIZE arglen = wcslen(arg);
int rc = report_match(
arg, pcre2_match(regex.code, PCRE2_SPTR(arg), arglen, 0, 0, regex.match, 0));
if (rc < 0) { // pcre2 match error.
return false;
} else if (rc == 0) { // no match
return true;
}
matched++;
total_matched++;
if (opts.invert_match) {
return true;
}
// Report any additional matches.
PCRE2_SIZE *ovector = pcre2_get_ovector_pointer(regex.match);
while (opts.all || matched == 0) {
uint32_t options = 0;
PCRE2_SIZE offset = ovector[1]; // start at end of previous match
if (ovector[0] == ovector[1]) {
if (ovector[0] == arglen) {
break;
}
options = PCRE2_NOTEMPTY_ATSTART | PCRE2_ANCHORED;
}
rc = report_match(arg, pcre2_match(regex.code, PCRE2_SPTR(arg), arglen, offset, options,
regex.match, 0));
if (rc < 0) {
return false;
}
if (rc == 0) {
if (options == 0) { // all matches found
break;
}
ovector[1] = offset + 1;
continue;
}
matched++;
total_matched++;
}
return true;
}
};
static int string_match(parser_t &parser, io_streams_t &streams, int argc, wchar_t **argv) {
wchar_t *cmd = argv[0];
options_t opts;
opts.all_valid = true;
opts.entire_valid = true;
opts.ignore_case_valid = true;
opts.invert_valid = true;
opts.quiet_valid = true;
opts.regex_valid = true;
opts.index_valid = true;
int optind;
int retval = parse_opts(&opts, &optind, 1, argc, argv, parser, streams);
if (retval != STATUS_CMD_OK) return retval;
const wchar_t *pattern = opts.arg1;
if (opts.entire && opts.index) {
streams.err.append_format(BUILTIN_ERR_COMBO2, cmd,
_(L"--entire and --index are mutually exclusive"));
return STATUS_INVALID_ARGS;
}
std::unique_ptr<string_matcher_t> matcher;
if (opts.regex) {
matcher = make_unique<pcre2_matcher_t>(cmd, pattern, opts, streams);
} else {
matcher = make_unique<wildcard_matcher_t>(cmd, pattern, opts, streams);
}
arg_iterator_t aiter(argv, optind, streams);
while (const wchar_t *arg = aiter.next()) {
if (!matcher->report_matches(arg)) {
return STATUS_INVALID_ARGS;
}
}
return matcher->match_count() > 0 ? STATUS_CMD_OK : STATUS_CMD_ERROR;
}
class string_replacer_t {
protected:
const wchar_t *argv0;
options_t opts;
int total_replaced;
io_streams_t &streams;
public:
string_replacer_t(const wchar_t *argv0_, options_t opts_, io_streams_t &streams_)
: argv0(argv0_), opts(std::move(opts_)), total_replaced(0), streams(streams_) {}
virtual ~string_replacer_t() = default;
int replace_count() { return total_replaced; }
virtual bool replace_matches(const wchar_t *arg) = 0;
};
class literal_replacer_t : public string_replacer_t {
const wchar_t *pattern;
const wchar_t *replacement;
size_t patlen;
public:
literal_replacer_t(const wchar_t *argv0, const wchar_t *pattern_, const wchar_t *replacement_,
const options_t &opts, io_streams_t &streams)
: string_replacer_t(argv0, opts, streams),
pattern(pattern_),
replacement(replacement_),
patlen(wcslen(pattern)) {}
~literal_replacer_t() override = default;
bool replace_matches(const wchar_t *arg) override;
};
static wcstring interpret_escapes(const wchar_t *orig) {
wcstring result;
while (*orig != L'\0') {
if (*orig == L'\\') {
orig += read_unquoted_escape(orig, &result, true, false);
} else {
result += *orig;
orig++;
}
}
return result;
}
class regex_replacer_t : public string_replacer_t {
compiled_regex_t regex;