forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
subst.cc
11020 lines (9696 loc) · 337 KB
/
subst.cc
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
/* subst.cc -- The part of the shell that does parameter, command, arithmetic,
and globbing substitutions. */
/* ``Have a little faith, there's magic in the night. You ain't a
beauty, but, hey, you're alright.'' */
/* Copyright (C) 1987-2022 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
/* Include shell.h first for common defs. */
#include "shell.hh"
#include "shmbutil.hh"
#if defined(HAVE_MBSTR_H) && defined(HAVE_MBSCHR)
#include <mbstr.h> /* mbschr */
#endif
#include "typemax.hh"
#include "builtins/common.hh"
#include "builtext.hh"
#include <fnmatch.h>
namespace bash
{
/* Variable types. */
enum var_type
{
VT_VARIABLE = 0,
VT_POSPARMS = 1,
VT_ARRAYVAR = 2,
VT_ARRAYMEMBER = 3,
VT_ASSOCVAR = 4
};
static constexpr int VT_STARSUB = 128; /* $* or ${array[*]} -- used to split */
/* Flags for quoted_strchr */
enum strchr_flags
{
ST_NOFLAGS = 0,
ST_BACKSL = 0x01,
ST_CTLESC = 0x02,
ST_SQUOTE = 0x04, /* unused yet */
ST_DQUOTE = 0x08 /* unused yet */
};
/* These defs make it easier to use the editor. */
#define LBRACE '{'
#define RBRACE '}'
#define LPAREN '('
#define RPAREN ')'
#define LBRACK '['
#define RBRACK ']'
#if defined(HANDLE_MULTIBYTE)
#define WLPAREN L'('
#define WRPAREN L')'
#endif
#define DOLLAR_AT_STAR(c) ((c) == '@' || (c) == '*')
#define STR_DOLLAR_AT_STAR(s) (DOLLAR_AT_STAR ((s)[0]) && (s)[1] == '\0')
/* Evaluates to 1 if C is one of the shell's special parameters whose length
can be taken, but is also one of the special expansion characters. */
#define VALID_SPECIAL_LENGTH_PARAM(c) \
((c) == '-' || (c) == '?' || (c) == '#' || (c) == '@')
/* Evaluates to 1 if C is one of the shell's special parameters for which an
indirect variable reference may be made. */
#define VALID_INDIR_PARAM(c) \
((posixly_correct == 0 && (c) == '#') \
|| (posixly_correct == 0 && (c) == '?') || (c) == '@' || (c) == '*')
/* Evaluates to 1 if C is one of the OP characters that follows the parameter
in ${parameter[:]OPword}. */
#define VALID_PARAM_EXPAND_CHAR(c) (sh_syntaxtab[(unsigned char)c] & CSUBSTOP)
/* Evaluates to 1 if this is one of the shell's special variables. */
#define SPECIAL_VAR(name, wi) \
(*name \
&& ((DIGIT (*name) && all_digits (name)) \
|| (name[1] == '\0' \
&& (sh_syntaxtab[(unsigned char)*name] & CSPECVAR)) \
|| (wi && name[2] == '\0' && VALID_INDIR_PARAM (name[1]))))
/* This can be used by all of the *_extract_* functions that have a similar
structure. It can't just be wrapped in a do...while(0) loop because of
the embedded `break'. The dangling else accommodates a trailing semicolon;
we could also put in a do ; while (0) */
#define CHECK_STRING_OVERRUN(oind, ind, len, ch) \
if (ind >= len) \
{ \
oind = len; \
ch = 0; \
break; \
} \
else
/* An expansion function that takes a string and a quoted flag and returns
a WORD_LIST *. Used as the type of the third argument to
expand_string_if_necessary(). */
typedef WORD_LIST *EXPFUNC (const char *, int);
static char *extract_array_assignment_list (const char *, size_t *);
/* **************************************************************** */
/* */
/* Utility Functions */
/* */
/* **************************************************************** */
/* Extract a substring from STRING, starting at SINDEX and ending with
one of the characters in CHARLIST. Don't make the ending character
part of the string. Leave SINDEX pointing at the ending character.
Understand about backslashes in the string. If (flags & SX_VARNAME)
is non-zero, and array variables have been compiled into the shell,
everything between a `[' and a corresponding `]' is skipped over.
If (flags & SX_NOALLOC) is non-zero, don't return the substring, just
update SINDEX. If (flags & SX_REQMATCH) is non-zero, the string must
contain a closing character from CHARLIST. */
char *
Shell::string_extract (const char *string, size_t *sindex,
const char *charlist, sx_flags flags)
{
char c;
size_t slen;
DECLARE_MBSTATE;
slen = (MB_CUR_MAX > 1) ? (strlen (string + *sindex) + *sindex) : 0;
size_t i = *sindex;
bool found = false;
while ((c = string[i]))
{
if (c == '\\')
{
if (string[i + 1])
i++;
else
break;
}
#if defined(ARRAY_VARS)
else if ((flags & SX_VARNAME) && c == LBRACK)
{
size_t ni;
/* If this is an array subscript, skip over it and continue. */
ni = skipsubscript (string, i, VA_NOFLAGS);
if (string[ni] == RBRACK)
i = ni;
}
#endif
else if (member (c, charlist))
{
found = true;
break;
}
ADVANCE_CHAR (string, slen, i);
}
/* If we had to have a matching delimiter and didn't find one, throw an
exception and let the caller deal with it. */
if ((flags & SX_REQMATCH) && !found)
{
*sindex = i;
throw extract_string_error ();
}
char *temp = (flags & SX_NOALLOC) ? nullptr : substring (string, *sindex, i);
*sindex = i;
return temp;
}
/* Extract the contents of STRING as if it is enclosed in double quotes.
SINDEX, when passed in, is the offset of the character immediately
following the opening double quote; on exit, SINDEX is left pointing after
the closing double quote. If STRIPDQ is non-zero, unquoted double
quotes are stripped and the string is terminated by a null byte.
Backslashes between the embedded double quotes are processed. If STRIPDQ
is zero, an unquoted `"' terminates the string. */
char *
Shell::string_extract_double_quoted (const char *string, size_t *sindex,
sx_flags flags)
{
size_t slen;
const char *send;
unsigned char c;
DECLARE_MBSTATE;
slen = strlen (string + *sindex) + *sindex;
send = string + slen;
size_t j = 0;
size_t i = *sindex;
const bool stripdq = (flags & SX_STRIPDQ);
bool pass_next = false, backquote = false, dquote = false;
char *temp = new char[1 + slen - *sindex];
while ((c = static_cast<unsigned char> (string[i])))
{
/* Process a character that was quoted by a backslash. */
if (pass_next)
{
/* XXX - take another look at this in light of Interp 221 */
/* Posix.2 sez:
``The backslash shall retain its special meaning as an escape
character only when followed by one of the characters:
$ ` " \ <newline>''.
If STRIPDQ is zero, we handle the double quotes here and let
expand_word_internal handle the rest. If STRIPDQ is non-zero,
we have already been through one round of backslash stripping,
and want to strip these backslashes only if DQUOTE is non-zero,
indicating that we are inside an embedded double-quoted string. */
/* If we are in an embedded quoted string, then don't strip
backslashes before characters for which the backslash
retains its special meaning, but remove backslashes in
front of other characters. If we are not in an
embedded quoted string, don't strip backslashes at all.
This mess is necessary because the string was already
surrounded by double quotes (and sh has some really weird
quoting rules).
The returned string will be run through expansion as if
it were double-quoted. */
if ((!(stripdq) && c != '"')
|| (stripdq
&& ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || !dquote)))
temp[j++] = '\\';
pass_next = false;
add_one_character:
COPY_CHAR_I (temp, j, string, send, i);
continue;
}
/* A backslash protects the next character. The code just above
handles preserving the backslash in front of any character but
a double quote. */
if (c == '\\')
{
pass_next = true;
i++;
continue;
}
/* Inside backquotes, ``the portion of the quoted string from the
initial backquote and the characters up to the next backquote
that is not preceded by a backslash, having escape characters
removed, defines that command''. */
if (backquote)
{
if (c == '`')
backquote = false;
temp[j++] = static_cast<char> (c); /* COPY_CHAR_I? */
i++;
continue;
}
if (c == '`')
{
temp[j++] = static_cast<char> (c);
backquote = true;
i++;
continue;
}
/* Pass everything between `$(' and the matching `)' or a quoted
${ ... } pair through according to the Posix.2 specification. */
if (c == '$' && ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
{
bool free_ret = true;
size_t si = i + 2;
char *ret = (string[i + 1] == LPAREN)
? extract_command_subst (string, &si,
(flags & SX_COMPLETE))
: extract_dollar_brace_string (
string, &si, Q_DOUBLE_QUOTES, SX_NOFLAGS);
temp[j++] = '$';
temp[j++] = string[i + 1];
/* Just paranoia; ret will not be 0 unless no_throw_on_fatal_error
is set. */
if (ret == 0 && no_throw_on_fatal_error)
{
free_ret = false;
ret = const_cast<char *> (string + i + 2);
}
/* XXX - CHECK_STRING_OVERRUN here? */
for (int t = 0; ret[t]; t++, j++)
temp[j] = ret[t];
temp[j] = string[si];
if (si < i + 2) /* we went back? */
i += 2;
else if (string[si])
{
j++;
i = si + 1;
}
else
i = si;
if (free_ret)
delete[] ret;
continue;
}
/* Add any character but a double quote to the quoted string we're
accumulating. */
if (c != '"')
goto add_one_character;
/* c == '"' */
if (stripdq)
{
dquote = !dquote;
i++;
continue;
}
break;
}
temp[j] = '\0';
/* Point to after the closing quote. */
if (c)
i++;
*sindex = i;
return temp;
}
/* This should really be another option to string_extract_double_quoted. */
size_t
Shell::skip_double_quoted (const char *string, size_t slen, size_t sind,
sx_flags flags)
{
DECLARE_MBSTATE;
bool pass_next = false, backquote = false;
size_t i = sind;
char c;
while ((c = string[i]))
{
if (pass_next)
{
pass_next = false;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '\\')
{
pass_next = true;
i++;
continue;
}
else if (backquote)
{
if (c == '`')
backquote = false;
ADVANCE_CHAR (string, slen, i);
continue;
}
else if (c == '`')
{
backquote = true;
i++;
continue;
}
else if (c == '$'
&& ((string[i + 1] == LPAREN) || (string[i + 1] == LBRACE)))
{
size_t si = i + 2;
if (string[i + 1] == LPAREN)
(void)extract_command_subst (const_cast<char *> (string), &si,
(SX_NOALLOC | (flags & SX_COMPLETE)));
else
(void)extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES,
SX_NOALLOC);
/* These can consume the entire string if they are unterminated */
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
else if (c != '"')
{
ADVANCE_CHAR (string, slen, i);
continue;
}
else
break;
}
if (c)
i++;
return i;
}
/* Extract the contents of STRING as if it is enclosed in single quotes.
SINDEX, when passed in, is the offset of the character immediately
following the opening single quote; on exit, SINDEX is left pointing after
the closing single quote. */
char *
Shell::string_extract_single_quoted (const char *string, size_t *sindex)
{
size_t i;
size_t slen;
char *t;
DECLARE_MBSTATE;
/* Don't need slen for ADVANCE_CHAR unless multibyte chars possible. */
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
i = *sindex;
while (string[i] && string[i] != '\'')
ADVANCE_CHAR (string, slen, i);
t = substring (string, *sindex, i);
if (string[i])
i++;
*sindex = i;
return t;
}
/* Skip over a single-quoted string. We overload the SX_COMPLETE flag to mean
that we are splitting out words for completion and have encountered a $'...'
string, which allows backslash-escaped single quotes. */
size_t
Shell::skip_single_quoted (const char *string, size_t slen, size_t sind,
sx_flags flags)
{
size_t c;
DECLARE_MBSTATE;
c = sind;
while (string[c] && string[c] != '\'')
{
if ((flags & SX_COMPLETE) && string[c] == '\\' && string[c + 1] == '\''
&& string[c + 2])
ADVANCE_CHAR (string, slen, c);
ADVANCE_CHAR (string, slen, c);
}
if (string[c])
c++;
return c;
}
/* Just like string_extract, but doesn't hack backslashes or any of
that other stuff. Obeys CTLESC quoting. Used to do splitting on $IFS. */
char *
Shell::string_extract_verbatim (const char *string, size_t slen,
size_t *sindex, const char *charlist,
sx_flags flags)
{
DECLARE_MBSTATE;
if ((flags & SX_NOCTLESC) && charlist[0] == '\'' && charlist[1] == '\0')
{
char *temp = string_extract_single_quoted (string, sindex);
--*sindex; /* leave *sindex at separator character */
return temp;
}
/* This can never be called with charlist == NULL. If *charlist == NULL,
we can skip the loop and just return a copy of the string, updating
*sindex */
if (*charlist == 0)
{
const char *temp = string + *sindex;
size_t len = (*sindex == 0) ? slen : strlen (temp);
char *t2 = savestring (temp);
*sindex += len;
return t2;
}
size_t i = *sindex;
#if defined(HANDLE_MULTIBYTE)
wchar_t *wcharlist = 0;
#endif
char c;
while ((c = string[i]))
{
#if defined(HANDLE_MULTIBYTE)
int mblength;
#endif
if ((flags & SX_NOCTLESC) == 0 && c == CTLESC)
{
i += 2;
CHECK_STRING_OVERRUN (i, i, slen, c);
continue;
}
/* Even if flags contains SX_NOCTLESC, we let CTLESC quoting CTLNUL
through, to protect the CTLNULs from later calls to
remove_quoted_nulls. */
else if ((flags & SX_NOESCCTLNUL) == 0 && c == CTLESC
&& string[i + 1] == CTLNUL)
{
i += 2;
CHECK_STRING_OVERRUN (i, i, slen, c);
continue;
}
#if defined(HANDLE_MULTIBYTE)
if (locale_utf8locale && slen > i && UTF8_SINGLEBYTE (string[i]))
mblength = (string[i] != 0) ? 1 : 0;
else
mblength = mblen (string + i, slen - i);
if (mblength > 1)
{
wchar_t wc;
mblength = mbtowc (&wc, string + i, slen - i);
if (MB_INVALIDCH (mblength))
{
if (member (c, charlist))
break;
}
else
{
if (wcharlist == 0)
{
size_t len;
len = mbstowcs (wcharlist, charlist, 0);
if (len == static_cast<size_t> (-1))
len = 0;
wcharlist = new wchar_t[len + 1];
mbstowcs (wcharlist, charlist, len + 1);
}
if (wcschr (wcharlist, wc))
break;
}
}
else
#endif
if (member (c, charlist))
break;
ADVANCE_CHAR (string, slen, i);
}
#if defined(HANDLE_MULTIBYTE)
delete[] wcharlist;
#endif
char *temp = substring (string, *sindex, i);
*sindex = i;
return temp;
}
/* Extract the $( construct in STRING, and return a new string.
Start extracting at (SINDEX) as if we had just seen "$(".
Make (SINDEX) get the position of the matching ")". )
XFLAGS is additional flags to pass to other extraction functions. */
char *
Shell::extract_command_subst (const char *string, size_t *sindex,
sx_flags xflags)
{
char *ret;
if (string[*sindex] == LPAREN || (xflags & SX_COMPLETE))
return (extract_delimited_string (string, sindex, "$(", "(", ")",
(xflags | SX_COMMAND)));
else
{
xflags |= (no_throw_on_fatal_error ? SX_NOTHROW : SX_NOFLAGS);
ret = xparse_dolparen (string, const_cast<char *> (string + *sindex),
sindex, xflags);
return ret;
}
}
#if defined(ARRAY_VARS)
/* This can be fooled by unquoted right parens in the passed string. If
each caller verifies that the last character in STRING is a right paren,
we don't even need to call extract_delimited_string. */
char *
extract_array_assignment_list (const char *string, size_t *sindex)
{
size_t slen = strlen (string);
if (string[slen - 1] == RPAREN)
{
char *ret = substring (string, *sindex, slen - 1);
*sindex = slen - 1;
return ret;
}
return 0;
}
#endif
/* Extract and create a new string from the contents of STRING, a
character string delimited with OPENER and CLOSER. SINDEX is
the address of an int describing the current offset in STRING;
it should point to just after the first OPENER found. On exit,
SINDEX gets the position of the last character of the matching CLOSER.
If OPENER is more than a single character, ALT_OPENER, if non-null,
contains a character string that can also match CLOSER and thus
needs to be skipped. */
char *
Shell::extract_delimited_string (const char *string, size_t *sindex,
const char *opener, const char *alt_opener,
const char *closer, sx_flags flags)
{
DECLARE_MBSTATE;
size_t slen = strlen (string + *sindex) + *sindex;
size_t len_opener = strlen (opener);
size_t len_alt_opener = strlen (alt_opener);
size_t len_closer = strlen (closer);
bool pass_character = false, in_comment = false;
int nesting_level = 1;
size_t i = *sindex;
char c = 0;
while (nesting_level)
{
c = string[i];
/* If a recursive call or a call to ADVANCE_CHAR leaves the index beyond
the end of the string, catch it and cut the loop. */
if (i > slen)
{
i = slen;
c = string[i];
break;
}
if (c == 0)
break;
if (in_comment)
{
if (c == '\n')
in_comment = false;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (pass_character) /* previous char was backslash */
{
pass_character = false;
ADVANCE_CHAR (string, slen, i);
continue;
}
/* Not exactly right yet; should handle shell metacharacters and
multibyte characters, too. See COMMENT_BEGIN define in parse.y */
if ((flags & SX_COMMAND) && c == '#'
&& (i == 0 || string[i - 1] == '\n' || shellblank (string[i - 1])))
{
in_comment = true;
ADVANCE_CHAR (string, slen, i);
continue;
}
if (c == CTLESC || c == '\\')
{
pass_character = true;
i++;
continue;
}
/* Process a nested command substitution, but only if we're parsing an
arithmetic substitution. */
if ((flags & SX_COMMAND) && string[i] == '$' && string[i + 1] == LPAREN)
{
size_t si = i + 2;
(void)extract_command_subst (const_cast<char *> (string), &si,
(flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Process a nested OPENER. */
if (STREQN (string + i, opener, len_opener))
{
size_t si = i + len_opener;
(void)extract_delimited_string (string, &si, opener, alt_opener,
closer, (flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Process a nested ALT_OPENER */
if (len_alt_opener && STREQN (string + i, alt_opener, len_alt_opener))
{
size_t si = i + len_alt_opener;
(void)extract_delimited_string (string, &si, alt_opener, alt_opener,
closer, (flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* If the current substring terminates the delimited string, decrement
the nesting level. */
if (STREQN (string + i, closer, len_closer))
{
i += len_closer - 1; /* move to last byte of the closer */
nesting_level--;
if (nesting_level == 0)
break;
}
/* Pass old-style command substitution through verbatim. */
if (c == '`')
{
size_t si = i + 1;
(void)string_extract (string, &si, "`", (flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Pass single-quoted and double-quoted strings through verbatim. */
if (c == '\'' || c == '"')
{
size_t si = i + 1;
i = (c == '\'') ? skip_single_quoted (string, slen, si, SX_NOFLAGS)
: skip_double_quoted (string, slen, si, SX_NOFLAGS);
continue;
}
/* move past this character, which was not special. */
ADVANCE_CHAR (string, slen, i);
}
if (c == 0 && nesting_level)
{
if (!no_throw_on_fatal_error)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_ ("bad substitution: no closing `%s' in %s"), closer,
string);
exp_throw_to_top_level (discard_exception ());
}
else
{
*sindex = i;
return nullptr;
}
}
size_t si = i - *sindex - len_closer + 1;
char *result;
if (flags & SX_NOALLOC)
result = nullptr;
else
{
result = new char[1 + si];
strncpy (result, string + *sindex, si);
result[si] = '\0';
}
*sindex = i;
return result;
}
/* Extract a parameter expansion expression within ${ and } from STRING.
Obey the Posix.2 rules for finding the ending `}': count braces while
skipping over enclosed quoted strings and command substitutions.
SINDEX is the address of an int describing the current offset in STRING;
it should point to just after the first `{' found. On exit, SINDEX
gets the position of the matching `}'. QUOTED is non-zero if this
occurs inside double quotes. */
/* XXX -- this is very similar to extract_delimited_string -- XXX */
char *
Shell::extract_dollar_brace_string (const char *string, size_t *sindex,
quoted_flags quoted, sx_flags flags)
{
DECLARE_MBSTATE;
bool pass_character = false;
int nesting_level = 1;
size_t slen = strlen (string + *sindex) + *sindex;
/* The handling of dolbrace_state needs to agree with the code in parse.y:
parse_matched_pair(). The different initial value is to handle the
case where this function is called to parse the word in
${param op word} (SX_WORD). */
dolbrace_state_t dolbrace_state
= (flags & SX_WORD) ? DOLBRACE_WORD : DOLBRACE_PARAM;
if ((quoted & (Q_HERE_DOCUMENT | Q_DOUBLE_QUOTES)) && (flags & SX_POSIXEXP))
dolbrace_state = DOLBRACE_QUOTE;
size_t i = *sindex;
char c;
while ((c = string[i]))
{
if (pass_character)
{
pass_character = false;
ADVANCE_CHAR (string, slen, i);
continue;
}
/* CTLESCs and backslashes quote the next character. */
if (c == CTLESC || c == '\\')
{
pass_character = true;
i++;
continue;
}
if (string[i] == '$' && string[i + 1] == LBRACE)
{
nesting_level++;
i += 2;
continue;
}
if (c == RBRACE)
{
nesting_level--;
if (nesting_level == 0)
break;
i++;
continue;
}
/* Pass the contents of old-style command substitutions through
verbatim. */
if (c == '`')
{
size_t si = i + 1;
(void)string_extract (string, &si, "`", (flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
/* Pass the contents of new-style command substitutions and
arithmetic substitutions through verbatim. */
if (string[i] == '$' && string[i + 1] == LPAREN)
{
size_t si = i + 2;
(void)extract_command_subst (const_cast<char *> (string), &si,
(flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
#if defined(PROCESS_SUBSTITUTION)
/* Technically this should only work at the start of a word */
if ((string[i] == '<' || string[i] == '>') && string[i + 1] == LPAREN)
{
size_t si = i + 2;
(void)extract_process_subst (string, &si, (flags | SX_NOALLOC));
CHECK_STRING_OVERRUN (i, si, slen, c);
i = si + 1;
continue;
}
#endif
/* Pass the contents of double-quoted strings through verbatim. */
if (c == '"')
{
size_t si = i + 1;
i = skip_double_quoted (string, slen, si, SX_NOFLAGS);
/* skip_XXX_quoted leaves index one past close quote */
continue;
}
if (c == '\'')
{
/*itrace("extract_dollar_brace_string: c == single quote flags = %d
* quoted = %d dolbrace_state = %d", flags, quoted,
* dolbrace_state);*/
if (posixly_correct && shell_compatibility_level > 42
&& dolbrace_state != DOLBRACE_QUOTE
&& (quoted & (Q_HERE_DOCUMENT | Q_DOUBLE_QUOTES)))
ADVANCE_CHAR (string, slen, i);
else
{
size_t si = i + 1;
i = skip_single_quoted (string, slen, si, SX_NOFLAGS);
}
continue;
}
#if defined(ARRAY_VARS)
if (c == LBRACK && dolbrace_state == DOLBRACE_PARAM)
{
size_t si = skipsubscript (string, i, 0);
CHECK_STRING_OVERRUN (i, si, slen, c);
if (string[si] == RBRACK)
c = string[i = si];
}
#endif
/* move past this character, which was not special. */
ADVANCE_CHAR (string, slen, i);
/* This logic must agree with parse.y:parse_matched_pair, since they
share the same defines. */
if (dolbrace_state == DOLBRACE_PARAM && c == '%' && (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
else if (dolbrace_state == DOLBRACE_PARAM && c == '#'
&& (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
else if (dolbrace_state == DOLBRACE_PARAM && c == '/'
&& (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE2; /* XXX */
else if (dolbrace_state == DOLBRACE_PARAM && c == '^'
&& (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
else if (dolbrace_state == DOLBRACE_PARAM && c == ','
&& (i - *sindex) > 1)
dolbrace_state = DOLBRACE_QUOTE;
/* This is intended to handle all of the [:]op expansions and the
substring/ length/pattern removal/pattern substitution expansions. */
else if (dolbrace_state == DOLBRACE_PARAM
&& strchr ("#%^,~:-=?+/", c) != 0)
dolbrace_state = DOLBRACE_OP;
else if (dolbrace_state == DOLBRACE_OP && strchr ("#%^,~:-=?+/", c) == 0)
dolbrace_state = DOLBRACE_WORD;
}
if (c == 0 && nesting_level)
{
if (!no_throw_on_fatal_error)
{
last_command_exit_value = EXECUTION_FAILURE;
report_error (_ ("bad substitution: no closing `%s' in %s"), "}",
string);
exp_throw_to_top_level (discard_exception ());
}
else
{
*sindex = i;
return nullptr;
}
}
char *result
= (flags & SX_NOALLOC) ? nullptr : substring (string, *sindex, i);
*sindex = i;
return result;
}
// Remove backslashes which are quoting backquotes from STRING (in place).
void
Shell::de_backslash (char *string)
{
DECLARE_MBSTATE;
size_t slen = strlen (string);
size_t i = 0, j = 0;