forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.cc
4765 lines (4113 loc) · 149 KB
/
parser.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
/* parser.cc - Parser code previously in parse.y. */
/* Copyright (C) 1989-2020 Free Software Foundation, Inc.
Copyright 2022, Jake Hamby.
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.hh"
#include "conftypes.hh"
using std::make_pair;
namespace bash
{
#if defined(HANDLE_MULTIBYTE)
#define last_shell_getc_is_singlebyte \
((shell_input_line.size () > 1) \
? shell_input_line_property[shell_input_line.size () - 1] \
: true)
#define MBTEST(x) ((x) && last_shell_getc_is_singlebyte)
#else
#define last_shell_getc_is_singlebyte true
#define MBTEST(x) ((x))
#endif
/* Initial size to reserve for tokens. */
#define TOKEN_DEFAULT_INITIAL_SIZE 496
/* Should we call prompt_again? */
#define SHOULD_PROMPT() \
(interactive \
&& (bash_input.type == st_stdin || bash_input.type == st_stream))
#if defined(ALIAS)
#define expanding_alias() (pushed_string_list && pushed_string_list->expander)
#else
#define expanding_alias() (false)
#endif
#ifdef DEBUG
void
Shell::debug_parser (parser::debug_level_type i)
{
#if YYDEBUG != 0
parser_.set_debug_level (i);
parser_.set_debug_stream (std::cerr);
#endif
}
#endif
/* yy_getc () returns the next available character from input or EOF.
yy_ungetc (c) makes `c' the next character to read.
init_yy_io (get, unget, type, location) makes the function GET the
installed function for getting the next character, makes UNGET the
installed function for un-getting a character, sets the type of stream
(either string or file) from TYPE, and makes LOCATION point to where
the input is coming from. */
/* Unconditionally returns end-of-file. */
int
Shell::return_EOF ()
{
return EOF;
}
/* Set all of the fields in BASH_INPUT to nullptr. Free bash_input.name if it
is non-null, avoiding a memory leak. */
void
Shell::initialize_bash_input ()
{
bash_input.type = st_none;
bash_input.name.clear ();
bash_input.location.file = nullptr;
bash_input.location.string = nullptr;
bash_input.getter = nullptr;
bash_input.ungetter = nullptr;
}
/* Set the contents of the current bash input stream from
GET, UNGET, TYPE, NAME, and LOCATION. */
void
Shell::init_yy_io (sh_cget_func_t get, sh_cunget_func_t unget,
stream_type type, const string &name, INPUT_STREAM location)
{
bash_input.type = type;
bash_input.name = name;
/* XXX */
bash_input.location = location;
bash_input.getter = get;
bash_input.ungetter = unget;
}
/* **************************************************************** */
/* */
/* Let input be read from readline (). */
/* */
/* **************************************************************** */
#if defined(READLINE)
int
Shell::yy_readline_get ()
{
SigHandler old_sigint;
if (current_readline_line.empty ())
{
if (!bash_readline_initialized)
initialize_readline ();
#if defined(JOB_CONTROL)
if (job_control)
give_terminal_to (shell_pgrp, 0);
#endif /* JOB_CONTROL */
old_sigint = IMPOSSIBLE_TRAP_HANDLER;
if (signal_is_ignored (SIGINT) == 0)
{
old_sigint = set_signal_handler (SIGINT, &sigint_sighandler_global);
}
sh_unset_nodelay_mode (fileno (rl_instream)); /* just in case */
current_readline_line = readline (current_readline_prompt);
CHECK_TERMSIG;
if (signal_is_ignored (SIGINT) == 0)
{
if (old_sigint != IMPOSSIBLE_TRAP_HANDLER)
set_signal_handler (SIGINT, old_sigint);
}
#if 0
/* Reset the prompt to the decoded value of prompt_string_pointer. */
reset_readline_prompt ();
#endif
if (current_readline_line.empty ())
return EOF;
current_readline_line_index = 0;
current_readline_line.push_back ('\n');
}
if (current_readline_line.size () == current_readline_line_index)
{
current_readline_line.clear ();
return yy_readline_get ();
}
else
{
return static_cast<unsigned char> (
current_readline_line[current_readline_line_index++]);
}
}
void
Shell::with_input_from_stdin ()
{
INPUT_STREAM location;
if (bash_input.type != st_stdin && !stream_on_stack (st_stdin))
{
location.string = savestring (current_readline_line);
init_yy_io (&Shell::yy_readline_get, &Shell::yy_readline_unget, st_stdin,
"readline stdin", location);
}
}
#else /* !READLINE */
void
Shell::with_input_from_stdin ()
{
with_input_from_stream (stdin, "stdin");
}
#endif /* !READLINE */
/* **************************************************************** */
/* */
/* Let input come from STRING. STRING is zero terminated. */
/* */
/* **************************************************************** */
int
Shell::yy_string_get ()
{
char *string = bash_input.location.string;
/* If the string doesn't exist, or is empty, EOF found. */
if (string && *string)
{
unsigned char c = static_cast<unsigned char> (*string++);
bash_input.location.string = string;
return c;
}
else
return EOF;
}
int
Shell::yy_string_unget (int c)
{
*(--bash_input.location.string) = static_cast<char> (c);
return c;
}
void
Shell::with_input_from_string (char *str, const string &name)
{
INPUT_STREAM location;
location.string = str;
init_yy_io (&Shell::yy_string_get, &Shell::yy_string_unget, st_string, name,
location);
}
/* Count the number of characters we've consumed from
bash_input.location.string and read into shell_input_line, but have not
returned from shell_getc. That is the true input location. Rewind
bash_input.location.string by that number of characters, so it points to the
last character actually consumed by the parser. */
void
Shell::rewind_input_string ()
{
size_t xchars;
/* number of unconsumed characters in the input -- XXX need to take newlines
into account, e.g., $(...\n) */
xchars = shell_input_line.size () - shell_input_line_index;
if (bash_input.location.string[-1] == '\n')
xchars++;
/* XXX - how to reflect bash_input.location.string back to string passed to
parse_and_execute or xparse_dolparen? xparse_dolparen needs to know how
far into the string we parsed. parse_and_execute knows where bash_input.
location.string is, and how far from orig_string that is -- that's the
number of characters the command consumed. */
/* bash_input.location.string - xchars should be where we parsed to */
/* need to do more validation on xchars value for sanity -- test cases. */
bash_input.location.string -= xchars;
}
/* **************************************************************** */
/* */
/* Let input come from STREAM. */
/* */
/* **************************************************************** */
/* These two functions used to test the value of the HAVE_RESTARTABLE_SYSCALLS
define, and just use getc/ungetc if it was defined, but since bash
installs most of its signal handlers without the SA_RESTART flag, some
signals received during a read(2) will not cause the read to be restarted.
We will need to restart it ourselves. */
int
Shell::yy_stream_get ()
{
int result;
result = EOF;
if (bash_input.location.file)
{
/* XXX - don't need terminate_immediately; getc_with_restart checks
for terminating signals itself if read returns < 0 */
result = getc_with_restart (bash_input.location.file);
}
return result;
}
int
Shell::yy_stream_unget (int c)
{
return ungetc_with_restart (c, bash_input.location.file);
}
void
Shell::with_input_from_stream (FILE *stream, const string &name)
{
INPUT_STREAM location;
location.file = stream;
init_yy_io (&Shell::yy_stream_get, &Shell::yy_stream_unget, st_stream, name,
location);
}
void
Shell::push_stream (int reset_lineno)
{
STREAM_SAVER *saver = new STREAM_SAVER ();
saver->bash_input = bash_input;
#if defined(BUFFERED_INPUT)
saver->bstream = nullptr;
/* If we have a buffered stream, clear out buffers[fd]. */
if (bash_input.type == st_bstream && bash_input.location.buffered_fd >= 0)
saver->bstream
= set_buffered_stream (bash_input.location.buffered_fd, nullptr);
#endif /* BUFFERED_INPUT */
saver->line = line_number;
bash_input.name.clear ();
saver->set_next (stream_list);
stream_list = saver;
EOF_Reached = false;
if (reset_lineno)
line_number = 0;
}
void
Shell::pop_stream ()
{
if (!stream_list)
EOF_Reached = true;
else
{
STREAM_SAVER *saver = stream_list;
EOF_Reached = false;
stream_list = stream_list->next ();
init_yy_io (saver->bash_input.getter, saver->bash_input.ungetter,
saver->bash_input.type, saver->bash_input.name,
saver->bash_input.location);
#if defined(BUFFERED_INPUT)
/* If we have a buffered stream, restore buffers[fd]. */
/* If the input file descriptor was changed while this was on the
save stack, update the buffered fd to the new file descriptor and
re-establish the buffer <-> bash_input fd correspondence. */
if (bash_input.type == st_bstream
&& bash_input.location.buffered_fd >= 0)
{
if (bash_input_fd_changed)
{
bash_input_fd_changed = 0;
if (default_buffered_input >= 0)
{
bash_input.location.buffered_fd = default_buffered_input;
saver->bstream->b_fd = default_buffered_input;
SET_CLOSE_ON_EXEC (default_buffered_input);
}
}
/* XXX could free buffered stream returned as result here. */
set_buffered_stream (bash_input.location.buffered_fd,
saver->bstream);
}
#endif /* BUFFERED_INPUT */
line_number = saver->line;
delete saver;
}
}
/*
* Pseudo-global variables used in implementing token-wise alias expansion.
*/
/*
* Push the current shell_input_line onto a stack of such lines and make S
* the current input. Used when expanding aliases. EXPAND is used to set
* the value of expand_next_token when the string is popped, so that the
* word after the alias in the original line is handled correctly when the
* alias expands to multiple words. TOKEN is the token that was expanded
* into S; it is saved and used to prevent infinite recursive expansion.
*/
void
Shell::push_string (char *s, int expand, alias_t *ap)
{
STRING_SAVER *temp = new STRING_SAVER ();
temp->expand_alias = expand;
temp->saved_line = shell_input_line;
temp->saved_line_index = shell_input_line_index;
temp->saved_line_terminator = shell_input_line_terminator;
#if defined(ALIAS)
temp->expander = ap;
if (ap)
temp->flags = PSH_ALIAS;
#endif
temp->set_next (pushed_string_list);
pushed_string_list = temp;
#if defined(ALIAS)
if (ap)
ap->flags |= AL_BEINGEXPANDED;
#endif
shell_input_line = s;
shell_input_line_index = 0;
shell_input_line_terminator = '\0';
#if 0
parser_state &= ~PST_ALEXPNEXT; /* XXX */
#endif
set_line_mbstate ();
}
/*
* Make the top of the pushed_string stack be the current shell input.
* Only called when there is something on the stack. Called from shell_getc
* when it thinks it has consumed the string generated by an alias expansion
* and needs to return to the original input line.
*/
void
Shell::pop_string ()
{
STRING_SAVER *t;
shell_input_line = pushed_string_list->saved_line;
shell_input_line_index = pushed_string_list->saved_line_index;
shell_input_line_terminator = pushed_string_list->saved_line_terminator;
#if defined(ALIAS)
if (pushed_string_list->expand_alias)
parser_state |= PST_ALEXPNEXT;
else
parser_state &= ~PST_ALEXPNEXT;
#endif
t = pushed_string_list;
pushed_string_list = pushed_string_list->next ();
#if defined(ALIAS)
if (t->expander)
t->expander->flags &= ~AL_BEINGEXPANDED;
#endif
delete t;
set_line_mbstate ();
}
void
Shell::free_string_list ()
{
STRING_SAVER *t, *t1;
for (t = pushed_string_list; t;)
{
t1 = t->next ();
#if defined(ALIAS)
if (t->expander)
t->expander->flags &= ~AL_BEINGEXPANDED;
#endif
delete t;
t = t1;
}
pushed_string_list = nullptr;
}
#if defined(ALIAS)
/* Before freeing AP, make sure that there aren't any cases of pointer
aliasing that could cause us to reference freed memory later on. */
void
Shell::clear_string_list_expander (alias_t *ap)
{
STRING_SAVER *t;
for (t = pushed_string_list; t; t = t->next ())
{
if (t->expander && t->expander == ap)
t->expander = nullptr;
}
}
#endif
/* Return a line of text, taken from wherever yylex () reads input.
If there is no more input, then we return nullptr. If REMOVE_QUOTED_NEWLINE
is non-zero, we remove unquoted \<newline> pairs. This is used by
read_secondary_line to read here documents. */
const string *
Shell::read_a_line (bool remove_quoted_newline)
{
int c, peekc;
#if defined(READLINE)
if (no_line_editing && SHOULD_PROMPT ())
#else
if (SHOULD_PROMPT ())
#endif
print_prompt ();
read_a_line_buffer.clear ();
bool pass_next = false;
while (1)
{
/* Allow immediate exit if interrupted during input. */
QUIT;
c = yy_getc ();
/* Ignore null bytes in input. */
if (c == 0)
continue;
/* If there is no more input, then we return nullptr. */
if (c == EOF)
{
if (interactive && bash_input.type == st_stream)
clearerr (stdin);
if (read_a_line_buffer.empty ())
return nullptr;
c = '\n';
}
/* IF REMOVE_QUOTED_NEWLINES is non-zero, we are reading a
here document with an unquoted delimiter. In this case,
the line will be expanded as if it were in double quotes.
We allow a backslash to escape the next character, but we
need to treat the backslash specially only if a backslash
quoting a backslash-newline pair appears in the line. */
if (pass_next)
{
read_a_line_buffer.push_back (static_cast<char> (c));
pass_next = false;
}
else if (c == '\\' && remove_quoted_newline)
{
QUIT;
peekc = yy_getc ();
if (peekc == '\n')
{
line_number++;
continue; /* Make the unquoted \<newline> pair disappear. */
}
else
{
yy_ungetc (peekc);
pass_next = true;
/* Preserve the backslash. */
read_a_line_buffer.push_back (static_cast<char> (c));
}
}
else
{
/* remove_quoted_newline is non-zero if the here-document delimiter
is unquoted. In this case, we will be expanding the lines and
need to make sure CTLESC and CTLNUL in the input are quoted. */
if (remove_quoted_newline && (c == CTLESC || c == CTLNUL))
read_a_line_buffer.push_back (CTLESC);
read_a_line_buffer.push_back (static_cast<char> (c));
}
if (c == '\n')
return &read_a_line_buffer;
}
}
/* Return a line as in read_a_line (), but insure that the prompt is
the secondary prompt. This is used to read the lines of a here
document. REMOVE_QUOTED_NEWLINE is non-zero if we should remove
newlines quoted with backslashes while reading the line. It is
non-zero unless the delimiter of the here document was quoted. */
const string *
Shell::read_secondary_line (bool remove_quoted_newline)
{
prompt_string_pointer = &ps2_prompt;
if (SHOULD_PROMPT ())
prompt_again ();
const string *ret = read_a_line (remove_quoted_newline);
#if defined(HISTORY)
if (ret && !ret->empty () && remember_on_history
&& (parser_state & PST_HEREDOC))
{
/* To make adding the here-document body right, we need to rely on
history_delimiting_chars() returning \n for the first line of the
here-document body and the null string for the second and subsequent
lines, so we avoid double newlines.
current_command_line_count == 2 for the first line of the body. */
current_command_line_count++;
maybe_add_history (*ret);
}
#endif /* HISTORY */
return ret;
}
/* **************************************************************** */
/* */
/* YYLEX () */
/* */
/* **************************************************************** */
#if __cplusplus < 201103L
#define emplace(x) insert (x)
#endif
void
Shell::init_token_lists ()
{
// Reserved words. Only recognized as the first word of a command.
word_token_map.emplace (make_pair ("if", parser::token::IF));
word_token_map.emplace (make_pair ("then", parser::token::THEN));
word_token_map.emplace (make_pair ("else", parser::token::ELSE));
word_token_map.emplace (make_pair ("elif", parser::token::ELIF));
word_token_map.emplace (make_pair ("fi", parser::token::FI));
word_token_map.emplace (make_pair ("case", parser::token::CASE));
word_token_map.emplace (make_pair ("esac", parser::token::ESAC));
word_token_map.emplace (make_pair ("for", parser::token::FOR));
#if defined(SELECT_COMMAND)
word_token_map.emplace (make_pair ("select", parser::token::SELECT));
#endif
word_token_map.emplace (make_pair ("while", parser::token::WHILE));
word_token_map.emplace (make_pair ("until", parser::token::UNTIL));
word_token_map.emplace (make_pair ("do", parser::token::DO));
word_token_map.emplace (make_pair ("done", parser::token::DONE));
word_token_map.emplace (make_pair ("in", parser::token::IN));
word_token_map.emplace (make_pair ("function", parser::token::FUNCTION));
#if defined(COMMAND_TIMING)
word_token_map.emplace (make_pair ("time", parser::token::TIME));
#endif
word_token_map.emplace (make_pair ("{", parser::token_kind_type ('{')));
word_token_map.emplace (make_pair ("}", parser::token_kind_type ('}')));
word_token_map.emplace (make_pair ("!", parser::token::BANG));
#if defined(COND_COMMAND)
word_token_map.emplace (make_pair ("[[", parser::token::COND_START));
word_token_map.emplace (make_pair ("]]", parser::token::COND_END));
#endif
#if defined(COPROCESS_SUPPORT)
word_token_map.emplace (make_pair ("coproc", parser::token::COPROC));
#endif
// other tokens that can be returned by read_token ()
/* Multiple-character tokens with special values */
other_token_map.emplace (make_pair ("--", parser::token::TIMEIGN));
other_token_map.emplace (make_pair ("-p", parser::token::TIMEOPT));
other_token_map.emplace (make_pair ("&&", parser::token::AND_AND));
other_token_map.emplace (make_pair ("||", parser::token::OR_OR));
other_token_map.emplace (make_pair (">>", parser::token::GREATER_GREATER));
other_token_map.emplace (make_pair ("<<", parser::token::LESS_LESS));
other_token_map.emplace (make_pair ("<&", parser::token::LESS_AND));
other_token_map.emplace (make_pair (">&", parser::token::GREATER_AND));
other_token_map.emplace (make_pair (";;", parser::token::SEMI_SEMI));
other_token_map.emplace (make_pair (";&", parser::token::SEMI_AND));
other_token_map.emplace (make_pair (";;&", parser::token::SEMI_SEMI_AND));
other_token_map.emplace (make_pair ("<<-", parser::token::LESS_LESS_MINUS));
other_token_map.emplace (make_pair ("<<<", parser::token::LESS_LESS_LESS));
other_token_map.emplace (make_pair ("&>", parser::token::AND_GREATER));
other_token_map.emplace (
make_pair ("&>>", parser::token::AND_GREATER_GREATER));
other_token_map.emplace (make_pair ("<>", parser::token::LESS_GREATER));
other_token_map.emplace (make_pair (">|", parser::token::GREATER_BAR));
other_token_map.emplace (make_pair ("|&", parser::token::BAR_AND));
other_token_map.emplace (make_pair ("EOF", parser::token::yacc_EOF));
/* Tokens whose value is the character itself */
other_token_map.emplace (make_pair (">", parser::token_kind_type ('>')));
other_token_map.emplace (make_pair ("<", parser::token_kind_type ('<')));
other_token_map.emplace (make_pair ("-", parser::token_kind_type ('-')));
other_token_map.emplace (make_pair ("{", parser::token_kind_type ('{')));
other_token_map.emplace (make_pair ("}", parser::token_kind_type ('}')));
other_token_map.emplace (make_pair (";", parser::token_kind_type (';')));
other_token_map.emplace (make_pair ("(", parser::token_kind_type ('(')));
other_token_map.emplace (make_pair (")", parser::token_kind_type (')')));
other_token_map.emplace (make_pair ("|", parser::token_kind_type ('|')));
other_token_map.emplace (make_pair ("&", parser::token_kind_type ('&')));
other_token_map.emplace (
make_pair ("newline", parser::token_kind_type ('\n')));
#if defined(HISTORY)
no_semi_successors.emplace (static_cast<parser::token_kind_type> ('\n'));
no_semi_successors.emplace (static_cast<parser::token_kind_type> ('{'));
no_semi_successors.emplace (static_cast<parser::token_kind_type> ('('));
no_semi_successors.emplace (static_cast<parser::token_kind_type> (')'));
no_semi_successors.emplace (static_cast<parser::token_kind_type> (';'));
no_semi_successors.emplace (static_cast<parser::token_kind_type> ('&'));
no_semi_successors.emplace (static_cast<parser::token_kind_type> ('|'));
no_semi_successors.emplace (parser::token::CASE);
no_semi_successors.emplace (parser::token::DO);
no_semi_successors.emplace (parser::token::ELSE);
no_semi_successors.emplace (parser::token::IF);
no_semi_successors.emplace (parser::token::SEMI_SEMI);
no_semi_successors.emplace (parser::token::SEMI_AND);
no_semi_successors.emplace (parser::token::SEMI_SEMI_AND);
no_semi_successors.emplace (parser::token::THEN);
no_semi_successors.emplace (parser::token::UNTIL);
no_semi_successors.emplace (parser::token::WHILE);
no_semi_successors.emplace (parser::token::AND_AND);
no_semi_successors.emplace (parser::token::OR_OR);
no_semi_successors.emplace (parser::token::IN);
#endif
}
#if __cplusplus < 201103L
#undef emplace
#endif
/* others not listed here (values contained in parser::symbol_type):
WORD
ASSIGNMENT_WORD
NUMBER
ARITH_CMD
ARITH_FOR_EXPRS
COND_CMD
*/
/* Return the next shell input character. This always reads characters
from shell_input_line; when that line is exhausted, it is time to
read the next line. This is called by read_token when the shell is
processing normal command input. */
int
Shell::shell_getc (bool remove_quoted_newline)
{
int c;
unsigned char uc;
QUIT;
bool last_was_backslash = false;
if (sigwinch_received)
{
sigwinch_received = 0;
get_new_window_size (nullptr, nullptr);
}
if (eol_ungetc_lookahead)
{
c = eol_ungetc_lookahead;
eol_ungetc_lookahead = 0;
return c;
}
#if defined(ALIAS) || defined(DPAREN_ARITHMETIC)
/* If shell_input_line[shell_input_line_index] == 0, but there is
something on the pushed list of strings, then we don't want to go
off and get another line. We let the code down below handle it. */
if (shell_input_line.empty ()
|| ((!shell_input_line[shell_input_line_index])
&& (pushed_string_list == nullptr)))
#else /* !ALIAS && !DPAREN_ARITHMETIC */
if (shell_input_line.empty () || !shell_input_line[shell_input_line_index])
#endif /* !ALIAS && !DPAREN_ARITHMETIC */
{
line_number++;
/* Let's not let one really really long line blow up memory allocation */
if (shell_input_line.capacity () >= 32768)
shell_input_line.reserve (0);
restart_read:
/* Allow immediate exit if interrupted during input. */
QUIT;
int i = 0, truncating = 0;
shell_input_line_terminator = 0;
/* If the shell is interactive, but not currently printing a prompt
(interactive_shell && interactive == 0), we don't want to print
notifies or cleanup the jobs -- we want to defer it until we do
print the next prompt. */
if (!interactive_shell || SHOULD_PROMPT ())
{
#if defined(JOB_CONTROL)
/* This can cause a problem when reading a command as the result
of a trap, when the trap is called from flush_child. This call
had better not cause jobs to disappear from the job table in
that case, or we will have big trouble. */
notify_and_cleanup ();
#else /* !JOB_CONTROL */
cleanup_dead_jobs ();
#endif /* !JOB_CONTROL */
}
#if defined(READLINE)
if (no_line_editing && SHOULD_PROMPT ())
#else
if (SHOULD_PROMPT ())
#endif
print_prompt ();
if (bash_input.type == st_stream)
clearerr (stdin);
while (1)
{
c = yy_getc ();
/* Allow immediate exit if interrupted during input. */
QUIT;
if (c == '\0')
{
/* If we get EOS while parsing a string, treat it as EOF so we
don't just keep looping. Happens very rarely */
if (bash_input.type == st_string)
{
if (i == 0)
shell_input_line_terminator = EOF;
c = EOF;
break;
}
continue;
}
if (c == EOF)
{
if (bash_input.type == st_stream)
clearerr (stdin);
if (i == 0)
shell_input_line_terminator = EOF;
break;
}
if (truncating == 0 || c == '\n')
shell_input_line.push_back (static_cast<char> (c));
if (c == '\n')
{
current_command_line_count++;
break;
}
last_was_backslash = (!last_was_backslash && c == '\\');
}
shell_input_line_index = 0;
set_line_mbstate ();
#if defined(HISTORY)
if (remember_on_history && !shell_input_line.empty ())
{
char *expansions;
#if defined(BANG_HISTORY)
/* If the current delimiter is a single quote, we should not be
performing history expansion, even if we're on a different
line from the original single quote. */
if (dstack.back () == '\'')
history_quoting_state = '\'';
else if (dstack.back () == '"')
history_quoting_state = '"';
else
history_quoting_state = 0;
#endif
/* Calling with a third argument of 1 allows remember_on_history to
determine whether or not the line is saved to the history list */
expansions = pre_process_line (shell_input_line, true, true);
#if defined(BANG_HISTORY)
history_quoting_state = 0;
#endif
if (expansions != shell_input_line.c_str ())
{
shell_input_line = expansions;
delete[] expansions;
if (shell_input_line.empty ())
current_command_line_count--;
set_line_mbstate ();
}
}
/* Try to do something intelligent with blank lines encountered while
entering multi-line commands. XXX - this is grotesque */
else if (remember_on_history && !shell_input_line.empty ()
&& current_command_line_count > 1)
{
if (!dstack.empty ())
/* We know shell_input_line[0] == 0 and we're reading some sort of
quoted string. This means we've got a line consisting of only
a newline in a quoted string. We want to make sure this line
gets added to the history. */
maybe_add_history (shell_input_line);
else
{
string hdcs;
hdcs = history_delimiting_chars (shell_input_line);
if (!hdcs.empty () && hdcs[0] == ';')
maybe_add_history (shell_input_line);
}
}
#endif /* HISTORY */
if (!shell_input_line.empty ())
{
/* Lines that signify the end of the shell's input should not be
echoed. We should not echo lines while parsing command
substitutions with recursive calls into the parsing engine; those
should only be echoed once when we read the word. That is the
reason for the test against shell_eof_token, which is set to a
right paren when parsing the contents of command substitutions. */
if (echo_input_at_read
&& (shell_input_line[0] || shell_input_line_terminator != EOF)
&& shell_eof_token == 0)
fprintf (stderr, "%s\n", shell_input_line.c_str ());
}
else
{
prompt_string_pointer = ¤t_prompt_string;
if (SHOULD_PROMPT ())
prompt_again ();
goto restart_read;
}
/* Add the newline to the end of this string, iff the string does
not already end in an EOF character. */
if (shell_input_line_terminator != EOF)
{
/* Don't add a newline to a string that ends with a backslash if
we're going to be removing quoted newlines, since that will eat
the backslash. Add another backslash instead (will be removed by
word expansion). */
if (bash_input.type == st_string && !parser_expanding_alias ()
&& last_was_backslash && c == EOF && remove_quoted_newline)
shell_input_line.push_back ('\\');
else
shell_input_line.push_back ('\n');
#if defined(HANDLE_MULTIBYTE)
/* This is kind of an abstraction violation, but there's no need to
go through the entire shell_input_line again with a call to
set_line_mbstate(). */
shell_input_line_property.reserve (shell_input_line.size () + 1);
shell_input_line_property[shell_input_line.size ()] = 1;
#endif
}
}
next_alias_char:
if (shell_input_line_index == 0)
unquoted_backslash = false;
uc = static_cast<unsigned char> (shell_input_line[shell_input_line_index]);
if (uc)
{
unquoted_backslash = (!unquoted_backslash && uc == '\\');
shell_input_line_index++;
}
#if defined(ALIAS) || defined(DPAREN_ARITHMETIC)
/* If UC is nullptr, we have reached the end of the current input string. If
pushed_string_list is non-empty, it's time to pop to the previous string
because we have fully consumed the result of the last alias expansion.
Do it transparently; just return the next character of the string popped
to. */
/* If pushed_string_list != 0 but pushed_string_list->expander == 0 (not
currently tested) and the flags value is not PSH_SOURCE, we are not
parsing an alias, we have just saved one (push_string, when called by
the parse_dparen code) In this case, just go on as well. The PSH_SOURCE
case is handled below. */
/* If we're at the end of an alias expansion add a space to make sure that
the alias remains marked as being in use while we expand its last word.
This makes sure that pop_string doesn't mark the alias as not in use
before the string resulting from the alias expansion is tokenized and
checked for alias expansion, preventing recursion. At this point, the
last character in shell_input_line is the last character of the alias
expansion. We test that last character to determine whether or not to
return the space that will delimit the token and postpone the
pop_string. This set of conditions duplicates what used to be in
mk_alexpansion () below, with the addition that we don't add a space if
we're currently reading a quoted string or in a shell comment. */
if (uc == 0 && pushed_string_list && pushed_string_list->flags != PSH_SOURCE
&& pushed_string_list->flags != PSH_DPAREN
&& (parser_state & PST_COMMENT) == 0
&& (parser_state & PST_ENDALIAS) == 0 && /* only once */
shell_input_line_index > 0
&& shellblank (shell_input_line[shell_input_line_index - 1]) == 0