-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubst.c
13035 lines (11510 loc) · 362 KB
/
subst.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
/* subst.c -- 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 "bashtypes.h"
#include <stdio.h>
#include "chartypes.h"
#if defined (HAVE_PWD_H)
# include <pwd.h>
#endif
#include <signal.h>
#include <errno.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#define NEED_FPURGE_DECL
#include "bashansi.h"
#include "posixstat.h"
#include "bashintl.h"
#include "shell.h"
#include "parser.h"
#include "redir.h"
#include "flags.h"
#include "jobs.h"
#include "execute_cmd.h"
#include "filecntl.h"
#include "trap.h"
#include "pathexp.h"
#include "mailcheck.h"
#include "shmbutil.h"
#if defined (HAVE_MBSTR_H) && defined (HAVE_MBSCHR)
# include <mbstr.h> /* mbschr */
#endif
#include "typemax.h"
#include "builtins/getopt.h"
#include "builtins/common.h"
#include "builtins/builtext.h"
#include <tilde/tilde.h>
#include <glob/strmatch.h>
#if !defined (errno)
extern int errno;
#endif /* !errno */
/* The size that strings change by. */
#define DEFAULT_INITIAL_ARRAY_SIZE 112
#define DEFAULT_ARRAY_SIZE 128
/* Variable types. */
#define VT_VARIABLE 0
#define VT_POSPARMS 1
#define VT_ARRAYVAR 2
#define VT_ARRAYMEMBER 3
#define VT_ASSOCVAR 4
#define VT_STARSUB 128 /* $* or ${array[*]} -- used to split */
/* Flags for quoted_strchr */
#define ST_BACKSL 0x01
#define ST_CTLESC 0x02
#define ST_SQUOTE 0x04 /* unused yet */
#define 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 PARAMS((char *, int));
/* Process ID of the last command executed within command substitution. */
pid_t last_command_subst_pid = NO_PID;
pid_t current_command_subst_pid = NO_PID;
/* Variables used to keep track of the characters in IFS. */
SHELL_VAR *ifs_var;
char *ifs_value;
unsigned char ifs_cmap[UCHAR_MAX + 1];
int ifs_is_set, ifs_is_null;
#if defined (HANDLE_MULTIBYTE)
unsigned char ifs_firstc[MB_LEN_MAX];
size_t ifs_firstc_len;
#else
unsigned char ifs_firstc;
#endif
/* If non-zero, command substitution inherits the value of errexit option */
int inherit_errexit = 0;
/* Sentinel to tell when we are performing variable assignments preceding a
command name and putting them into the environment. Used to make sure
we use the temporary environment when looking up variable values. */
int assigning_in_environment;
/* Used to hold a list of variable assignments preceding a command. Global
so the SIGCHLD handler in jobs.c can unwind-protect it when it runs a
SIGCHLD trap and so it can be saved and restored by the trap handlers. */
WORD_LIST *subst_assign_varlist = (WORD_LIST *)NULL;
/* Tell the expansion functions to not longjmp back to top_level on fatal
errors. Enabled when doing completion and prompt string expansion. */
int no_longjmp_on_fatal_error = 0;
/* Non-zero means to allow unmatched globbed filenames to expand to
a null file. */
int allow_null_glob_expansion;
/* Non-zero means to throw an error when globbing fails to match anything. */
int fail_glob_expansion;
/* If non-zero, perform `&' substitution on the replacement string in the
pattern substitution word expansion. */
int patsub_replacement = 1;
/* Extern functions and variables from different files. */
extern struct fd_bitmap *current_fds_to_close;
extern int wordexp_only;
extern int singlequote_translations;
extern int extended_quote;
#if defined (JOB_CONTROL) && defined (PROCESS_SUBSTITUTION)
extern PROCESS *last_procsub_child;
#endif
#if !defined (HAVE_WCSDUP) && defined (HANDLE_MULTIBYTE)
extern wchar_t *wcsdup PARAMS((const wchar_t *));
#endif
#if 0
/* Variables to keep track of which words in an expanded word list (the
output of expand_word_list_internal) are the result of globbing
expansions. GLOB_ARGV_FLAGS is used by execute_cmd.c.
(CURRENTLY UNUSED). */
char *glob_argv_flags;
static int glob_argv_flags_size;
#endif
static WORD_LIST *cached_quoted_dollar_at = 0;
/* Distinguished error values to return from expansion functions */
static WORD_LIST expand_word_error, expand_word_fatal;
static WORD_DESC expand_wdesc_error, expand_wdesc_fatal;
static char expand_param_error, expand_param_fatal, expand_param_unset;
static char extract_string_error, extract_string_fatal;
/* Set by expand_word_unsplit and several of the expand_string_XXX functions;
used to inhibit splitting and re-joining $* on $IFS, primarily when doing
assignment statements. The idea is that if we're in a context where this
is set, we're not going to be performing word splitting, so we use the same
rules to expand $* as we would if it appeared within double quotes. */
static int expand_no_split_dollar_star = 0;
/* A WORD_LIST of words to be expanded by expand_word_list_internal,
without any leading variable assignments. */
static WORD_LIST *garglist = (WORD_LIST *)NULL;
static char *quoted_substring PARAMS((char *, int, int));
static int quoted_strlen PARAMS((char *));
static char *quoted_strchr PARAMS((char *, int, int));
static char *expand_string_if_necessary PARAMS((char *, int, EXPFUNC *));
static inline char *expand_string_to_string_internal PARAMS((char *, int, EXPFUNC *));
static WORD_LIST *call_expand_word_internal PARAMS((WORD_DESC *, int, int, int *, int *));
static WORD_LIST *expand_string_internal PARAMS((char *, int));
static WORD_LIST *expand_string_leave_quoted PARAMS((char *, int));
static WORD_LIST *expand_string_for_rhs PARAMS((char *, int, int, int, int *, int *));
static WORD_LIST *expand_string_for_pat PARAMS((char *, int, int *, int *));
static char *quote_escapes_internal PARAMS((const char *, int));
static WORD_LIST *list_quote_escapes PARAMS((WORD_LIST *));
static WORD_LIST *list_dequote_escapes PARAMS((WORD_LIST *));
static char *make_quoted_char PARAMS((int));
static WORD_LIST *quote_list PARAMS((WORD_LIST *));
static int unquoted_substring PARAMS((char *, char *));
static int unquoted_member PARAMS((int, char *));
#if defined (ARRAY_VARS)
static SHELL_VAR *do_compound_assignment PARAMS((char *, char *, int));
#endif
static int do_assignment_internal PARAMS((const WORD_DESC *, int));
static char *string_extract_verbatim PARAMS((char *, size_t, int *, char *, int));
static char *string_extract PARAMS((char *, int *, char *, int));
static char *string_extract_double_quoted PARAMS((char *, int *, int));
static inline char *string_extract_single_quoted PARAMS((char *, int *, int));
static inline int skip_single_quoted PARAMS((const char *, size_t, int, int));
static int skip_double_quoted PARAMS((char *, size_t, int, int));
static char *extract_delimited_string PARAMS((char *, int *, char *, char *, char *, int));
static char *extract_heredoc_dolbrace_string PARAMS((char *, int *, int, int));
static char *extract_dollar_brace_string PARAMS((char *, int *, int, int));
static int skip_matched_pair PARAMS((const char *, int, int, int, int));
static char *pos_params PARAMS((char *, int, int, int, int));
static unsigned char *mb_getcharlens PARAMS((char *, int));
static char *remove_upattern PARAMS((char *, char *, int));
#if defined (HANDLE_MULTIBYTE)
static wchar_t *remove_wpattern PARAMS((wchar_t *, size_t, wchar_t *, int));
#endif
static char *remove_pattern PARAMS((char *, char *, int));
static int match_upattern PARAMS((char *, char *, int, char **, char **));
#if defined (HANDLE_MULTIBYTE)
static int match_wpattern PARAMS((wchar_t *, char **, size_t, wchar_t *, int, char **, char **));
#endif
static int match_pattern PARAMS((char *, char *, int, char **, char **));
static int getpatspec PARAMS((int, char *));
static char *getpattern PARAMS((char *, int, int));
static char *variable_remove_pattern PARAMS((char *, char *, int, int));
static char *list_remove_pattern PARAMS((WORD_LIST *, char *, int, int, int));
static char *parameter_list_remove_pattern PARAMS((int, char *, int, int));
#ifdef ARRAY_VARS
static char *array_remove_pattern PARAMS((SHELL_VAR *, char *, int, int, int));
#endif
static char *parameter_brace_remove_pattern PARAMS((char *, char *, array_eltstate_t *, char *, int, int, int));
static char *string_var_assignment PARAMS((SHELL_VAR *, char *));
#if defined (ARRAY_VARS)
static char *array_var_assignment PARAMS((SHELL_VAR *, int, int, int));
#endif
static char *pos_params_assignment PARAMS((WORD_LIST *, int, int));
static char *string_transform PARAMS((int, SHELL_VAR *, char *));
static char *list_transform PARAMS((int, SHELL_VAR *, WORD_LIST *, int, int));
static char *parameter_list_transform PARAMS((int, int, int));
#if defined ARRAY_VARS
static char *array_transform PARAMS((int, SHELL_VAR *, int, int));
#endif
static char *parameter_brace_transform PARAMS((char *, char *, array_eltstate_t *, char *, int, int, int, int));
static int valid_parameter_transform PARAMS((char *));
static char *process_substitute PARAMS((char *, int));
static char *optimize_cat_file PARAMS((REDIRECT *, int, int, int *));
static char *read_comsub PARAMS((int, int, int, int *));
#ifdef ARRAY_VARS
static arrayind_t array_length_reference PARAMS((char *));
#endif
static int valid_brace_expansion_word PARAMS((char *, int));
static int chk_atstar PARAMS((char *, int, int, int *, int *));
static int chk_arithsub PARAMS((const char *, int));
static WORD_DESC *parameter_brace_expand_word PARAMS((char *, int, int, int, array_eltstate_t *));
static char *parameter_brace_find_indir PARAMS((char *, int, int, int));
static WORD_DESC *parameter_brace_expand_indir PARAMS((char *, int, int, int, int *, int *));
static WORD_DESC *parameter_brace_expand_rhs PARAMS((char *, char *, int, int, int, int *, int *));
static void parameter_brace_expand_error PARAMS((char *, char *, int));
static int valid_length_expression PARAMS((char *));
static intmax_t parameter_brace_expand_length PARAMS((char *));
static char *skiparith PARAMS((char *, int));
static int verify_substring_values PARAMS((SHELL_VAR *, char *, char *, int, intmax_t *, intmax_t *));
static int get_var_and_type PARAMS((char *, char *, array_eltstate_t *, int, int, SHELL_VAR **, char **));
static char *mb_substring PARAMS((char *, int, int));
static char *parameter_brace_substring PARAMS((char *, char *, array_eltstate_t *, char *, int, int, int));
static int shouldexp_replacement PARAMS((char *));
static char *pos_params_pat_subst PARAMS((char *, char *, char *, int));
static char *expand_string_for_patsub PARAMS((char *, int));
static char *parameter_brace_patsub PARAMS((char *, char *, array_eltstate_t *, char *, int, int, int));
static char *pos_params_casemod PARAMS((char *, char *, int, int));
static char *parameter_brace_casemod PARAMS((char *, char *, array_eltstate_t *, int, char *, int, int, int));
static WORD_DESC *parameter_brace_expand PARAMS((char *, int *, int, int, int *, int *));
static WORD_DESC *param_expand PARAMS((char *, int *, int, int *, int *, int *, int *, int));
static WORD_LIST *expand_word_internal PARAMS((WORD_DESC *, int, int, int *, int *));
static WORD_LIST *word_list_split PARAMS((WORD_LIST *));
static void exp_jump_to_top_level PARAMS((int));
static WORD_LIST *separate_out_assignments PARAMS((WORD_LIST *));
static WORD_LIST *glob_expand_word_list PARAMS((WORD_LIST *, int));
#ifdef BRACE_EXPANSION
static WORD_LIST *brace_expand_word_list PARAMS((WORD_LIST *, int));
#endif
#if defined (ARRAY_VARS)
static int make_internal_declare PARAMS((char *, char *, char *));
static void expand_compound_assignment_word PARAMS((WORD_LIST *, int));
static WORD_LIST *expand_declaration_argument PARAMS((WORD_LIST *, WORD_LIST *));
#endif
static WORD_LIST *shell_expand_word_list PARAMS((WORD_LIST *, int));
static WORD_LIST *expand_word_list_internal PARAMS((WORD_LIST *, int));
static int do_assignment_statements PARAMS((WORD_LIST *, char *, int));
/* **************************************************************** */
/* */
/* Utility Functions */
/* */
/* **************************************************************** */
#if defined (DEBUG)
void
dump_word_flags (flags)
int flags;
{
int f;
f = flags;
fprintf (stderr, "%d -> ", f);
if (f & W_ARRAYIND)
{
f &= ~W_ARRAYIND;
fprintf (stderr, "W_ARRAYIND%s", f ? "|" : "");
}
if (f & W_ASSIGNASSOC)
{
f &= ~W_ASSIGNASSOC;
fprintf (stderr, "W_ASSIGNASSOC%s", f ? "|" : "");
}
if (f & W_ASSIGNARRAY)
{
f &= ~W_ASSIGNARRAY;
fprintf (stderr, "W_ASSIGNARRAY%s", f ? "|" : "");
}
if (f & W_SAWQUOTEDNULL)
{
f &= ~W_SAWQUOTEDNULL;
fprintf (stderr, "W_SAWQUOTEDNULL%s", f ? "|" : "");
}
if (f & W_NOPROCSUB)
{
f &= ~W_NOPROCSUB;
fprintf (stderr, "W_NOPROCSUB%s", f ? "|" : "");
}
if (f & W_DQUOTE)
{
f &= ~W_DQUOTE;
fprintf (stderr, "W_DQUOTE%s", f ? "|" : "");
}
if (f & W_HASQUOTEDNULL)
{
f &= ~W_HASQUOTEDNULL;
fprintf (stderr, "W_HASQUOTEDNULL%s", f ? "|" : "");
}
if (f & W_ASSIGNARG)
{
f &= ~W_ASSIGNARG;
fprintf (stderr, "W_ASSIGNARG%s", f ? "|" : "");
}
if (f & W_ASSNBLTIN)
{
f &= ~W_ASSNBLTIN;
fprintf (stderr, "W_ASSNBLTIN%s", f ? "|" : "");
}
if (f & W_ASSNGLOBAL)
{
f &= ~W_ASSNGLOBAL;
fprintf (stderr, "W_ASSNGLOBAL%s", f ? "|" : "");
}
if (f & W_COMPASSIGN)
{
f &= ~W_COMPASSIGN;
fprintf (stderr, "W_COMPASSIGN%s", f ? "|" : "");
}
if (f & W_EXPANDRHS)
{
f &= ~W_EXPANDRHS;
fprintf (stderr, "W_EXPANDRHS%s", f ? "|" : "");
}
if (f & W_NOTILDE)
{
f &= ~W_NOTILDE;
fprintf (stderr, "W_NOTILDE%s", f ? "|" : "");
}
if (f & W_ASSIGNRHS)
{
f &= ~W_ASSIGNRHS;
fprintf (stderr, "W_ASSIGNRHS%s", f ? "|" : "");
}
if (f & W_NOASSNTILDE)
{
f &= ~W_NOASSNTILDE;
fprintf (stderr, "W_NOASSNTILDE%s", f ? "|" : "");
}
if (f & W_NOCOMSUB)
{
f &= ~W_NOCOMSUB;
fprintf (stderr, "W_NOCOMSUB%s", f ? "|" : "");
}
if (f & W_ARRAYREF)
{
f &= ~W_ARRAYREF;
fprintf (stderr, "W_ARRAYREF%s", f ? "|" : "");
}
if (f & W_DOLLARAT)
{
f &= ~W_DOLLARAT;
fprintf (stderr, "W_DOLLARAT%s", f ? "|" : "");
}
if (f & W_TILDEEXP)
{
f &= ~W_TILDEEXP;
fprintf (stderr, "W_TILDEEXP%s", f ? "|" : "");
}
if (f & W_NOSPLIT2)
{
f &= ~W_NOSPLIT2;
fprintf (stderr, "W_NOSPLIT2%s", f ? "|" : "");
}
if (f & W_NOSPLIT)
{
f &= ~W_NOSPLIT;
fprintf (stderr, "W_NOSPLIT%s", f ? "|" : "");
}
if (f & W_NOBRACE)
{
f &= ~W_NOBRACE;
fprintf (stderr, "W_NOBRACE%s", f ? "|" : "");
}
if (f & W_NOGLOB)
{
f &= ~W_NOGLOB;
fprintf (stderr, "W_NOGLOB%s", f ? "|" : "");
}
if (f & W_SPLITSPACE)
{
f &= ~W_SPLITSPACE;
fprintf (stderr, "W_SPLITSPACE%s", f ? "|" : "");
}
if (f & W_ASSIGNMENT)
{
f &= ~W_ASSIGNMENT;
fprintf (stderr, "W_ASSIGNMENT%s", f ? "|" : "");
}
if (f & W_QUOTED)
{
f &= ~W_QUOTED;
fprintf (stderr, "W_QUOTED%s", f ? "|" : "");
}
if (f & W_HASDOLLAR)
{
f &= ~W_HASDOLLAR;
fprintf (stderr, "W_HASDOLLAR%s", f ? "|" : "");
}
if (f & W_COMPLETE)
{
f &= ~W_COMPLETE;
fprintf (stderr, "W_COMPLETE%s", f ? "|" : "");
}
if (f & W_CHKLOCAL)
{
f &= ~W_CHKLOCAL;
fprintf (stderr, "W_CHKLOCAL%s", f ? "|" : "");
}
if (f & W_FORCELOCAL)
{
f &= ~W_FORCELOCAL;
fprintf (stderr, "W_FORCELOCAL%s", f ? "|" : "");
}
fprintf (stderr, "\n");
fflush (stderr);
}
#endif
#ifdef INCLUDE_UNUSED
static char *
quoted_substring (string, start, end)
char *string;
int start, end;
{
register int len, l;
register char *result, *s, *r;
len = end - start;
/* Move to string[start], skipping quoted characters. */
for (s = string, l = 0; *s && l < start; )
{
if (*s == CTLESC)
{
s++;
continue;
}
l++;
if (*s == 0)
break;
}
r = result = (char *)xmalloc (2*len + 1); /* save room for quotes */
/* Copy LEN characters, including quote characters. */
s = string + l;
for (l = 0; l < len; s++)
{
if (*s == CTLESC)
*r++ = *s++;
*r++ = *s;
l++;
if (*s == 0)
break;
}
*r = '\0';
return result;
}
#endif
#ifdef INCLUDE_UNUSED
/* Return the length of S, skipping over quoted characters */
static int
quoted_strlen (s)
char *s;
{
register char *p;
int i;
i = 0;
for (p = s; *p; p++)
{
if (*p == CTLESC)
{
p++;
if (*p == 0)
return (i + 1);
}
i++;
}
return i;
}
#endif
#ifdef INCLUDE_UNUSED
/* Find the first occurrence of character C in string S, obeying shell
quoting rules. If (FLAGS & ST_BACKSL) is non-zero, backslash-escaped
characters are skipped. If (FLAGS & ST_CTLESC) is non-zero, characters
escaped with CTLESC are skipped. */
static char *
quoted_strchr (s, c, flags)
char *s;
int c, flags;
{
register char *p;
for (p = s; *p; p++)
{
if (((flags & ST_BACKSL) && *p == '\\')
|| ((flags & ST_CTLESC) && *p == CTLESC))
{
p++;
if (*p == '\0')
return ((char *)NULL);
continue;
}
else if (*p == c)
return p;
}
return ((char *)NULL);
}
/* Return 1 if CHARACTER appears in an unquoted portion of
STRING. Return 0 otherwise. CHARACTER must be a single-byte character. */
static int
unquoted_member (character, string)
int character;
char *string;
{
size_t slen;
int sindex, c;
DECLARE_MBSTATE;
slen = strlen (string);
sindex = 0;
while (c = string[sindex])
{
if (c == character)
return (1);
switch (c)
{
default:
ADVANCE_CHAR (string, slen, sindex);
break;
case '\\':
sindex++;
if (string[sindex])
ADVANCE_CHAR (string, slen, sindex);
break;
case '\'':
sindex = skip_single_quoted (string, slen, ++sindex, 0);
break;
case '"':
sindex = skip_double_quoted (string, slen, ++sindex, 0);
break;
}
}
return (0);
}
/* Return 1 if SUBSTR appears in an unquoted portion of STRING. */
static int
unquoted_substring (substr, string)
char *substr, *string;
{
size_t slen;
int sindex, c, sublen;
DECLARE_MBSTATE;
if (substr == 0 || *substr == '\0')
return (0);
slen = strlen (string);
sublen = strlen (substr);
for (sindex = 0; c = string[sindex]; )
{
if (STREQN (string + sindex, substr, sublen))
return (1);
switch (c)
{
case '\\':
sindex++;
if (string[sindex])
ADVANCE_CHAR (string, slen, sindex);
break;
case '\'':
sindex = skip_single_quoted (string, slen, ++sindex, 0);
break;
case '"':
sindex = skip_double_quoted (string, slen, ++sindex, 0);
break;
default:
ADVANCE_CHAR (string, slen, sindex);
break;
}
}
return (0);
}
#endif
/* Most of the substitutions must be done in parallel. In order
to avoid using tons of unclear goto's, I have some functions
for manipulating malloc'ed strings. They all take INDX, a
pointer to an integer which is the offset into the string
where manipulation is taking place. They also take SIZE, a
pointer to an integer which is the current length of the
character array for this string. */
/* Append SOURCE to TARGET at INDEX. SIZE is the current amount
of space allocated to TARGET. SOURCE can be NULL, in which
case nothing happens. Gets rid of SOURCE by freeing it.
Returns TARGET in case the location has changed. */
INLINE char *
sub_append_string (source, target, indx, size)
char *source, *target;
size_t *indx;
size_t *size;
{
if (source)
{
size_t n, srclen;
srclen = STRLEN (source);
if (srclen >= (*size - *indx))
{
n = srclen + *indx;
n = (n + DEFAULT_ARRAY_SIZE) - (n % DEFAULT_ARRAY_SIZE);
target = (char *)xrealloc (target, (*size = n));
}
FASTCOPY (source, target + *indx, srclen);
*indx += srclen;
target[*indx] = '\0';
free (source);
}
return (target);
}
#if 0
/* UNUSED */
/* Append the textual representation of NUMBER to TARGET.
INDX and SIZE are as in SUB_APPEND_STRING. */
char *
sub_append_number (number, target, indx, size)
intmax_t number;
char *target;
size_t *indx;
size_t *size;
{
char *temp;
temp = itos (number);
return (sub_append_string (temp, target, indx, size));
}
#endif
/* 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. */
static char *
string_extract (string, sindex, charlist, flags)
char *string;
int *sindex;
char *charlist;
int flags;
{
register int c, i;
int found;
size_t slen;
char *temp;
DECLARE_MBSTATE;
slen = (MB_CUR_MAX > 1) ? strlen (string + *sindex) + *sindex : 0;
i = *sindex;
found = 0;
while (c = string[i])
{
if (c == '\\')
{
if (string[i + 1])
i++;
else
break;
}
#if defined (ARRAY_VARS)
else if ((flags & SX_VARNAME) && c == LBRACK)
{
int ni;
/* If this is an array subscript, skip over it and continue. */
ni = skipsubscript (string, i, 0);
if (string[ni] == RBRACK)
i = ni;
}
#endif
else if (MEMBER (c, charlist))
{
found = 1;
break;
}
ADVANCE_CHAR (string, slen, i);
}
/* If we had to have a matching delimiter and didn't find one, return an
error and let the caller deal with it. */
if ((flags & SX_REQMATCH) && found == 0)
{
*sindex = i;
return (&extract_string_error);
}
temp = (flags & SX_NOALLOC) ? (char *)NULL : 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. */
static char *
string_extract_double_quoted (string, sindex, flags)
char *string;
int *sindex, flags;
{
size_t slen;
char *send;
int j, i, t;
unsigned char c;
char *temp, *ret; /* The new string we return. */
int pass_next, backquote, si; /* State variables for the machine. */
int dquote;
int stripdq;
DECLARE_MBSTATE;
slen = strlen (string + *sindex) + *sindex;
send = string + slen;
stripdq = (flags & SX_STRIPDQ);
pass_next = backquote = dquote = 0;
temp = (char *)xmalloc (1 + slen - *sindex);
j = 0;
i = *sindex;
while (c = 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 == 0 && c != '"') ||
(stripdq && ((dquote && (sh_syntaxtab[c] & CBSDQUOTE)) || dquote == 0)))
temp[j++] = '\\';
pass_next = 0;
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++;
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 = 0;
temp[j++] = c; /* COPY_CHAR_I? */
i++;
continue;
}
if (c == '`')
{
temp[j++] = c;
backquote++;
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)))
{
int free_ret = 1;
si = i + 2;
if (string[i + 1] == LPAREN)
ret = extract_command_subst (string, &si, (flags & SX_COMPLETE));
else
ret = extract_dollar_brace_string (string, &si, Q_DOUBLE_QUOTES, 0);
temp[j++] = '$';
temp[j++] = string[i + 1];
/* Just paranoia; ret will not be 0 unless no_longjmp_on_fatal_error
is set. */
if (ret == 0 && no_longjmp_on_fatal_error)
{
free_ret = 0;
ret = string + i + 2;
}
/* XXX - CHECK_STRING_OVERRUN here? */
for (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)
free (ret);
continue;
}
/* Add any character but a double quote to the quoted string we're
accumulating. */
if (c != '"')
goto add_one_character;