forked from bminor/bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
shell.hh
7031 lines (5555 loc) · 189 KB
/
shell.hh
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
/* shell.hh -- The data structures used by the shell */
/* Copyright (C) 1993-2020 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/>.
*/
#if !defined(_SHELL_H_)
#define _SHELL_H_
#include "general.hh"
#if defined(HAVE_PWD_H)
#include <pwd.h>
#endif
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#if defined(HAVE_SYS_FILE_H)
#include <sys/file.h>
#endif
#include <sys/stat.h>
#include <unistd.h>
#include "alias.hh"
#include "array.hh"
#if defined(ARRAY_VARS)
#include "assoc.hh"
#endif
#include "builtins.hh"
#include "command.hh"
#include "externs.hh"
#include "flags.hh"
#include "hashcmd.hh"
#include "jobs.hh"
#include "maxpath.hh"
#include "parser.hh"
#include "pathexp.hh"
#include "pathnames.hh"
#if defined(PROGRAMMABLE_COMPLETION)
#include "pcomplete.hh"
#endif
#include "posixstat.hh"
#include "quit.hh"
#include "shtty.hh"
#include "sig.hh"
#include "subst.hh"
#include "syntax.hh"
#include "trap.hh"
#include "variables.hh"
#include "builtins/common.hh"
#ifdef DEBUG
#define YYDEBUG 1
#else
#define YYDEBUG 0
#endif
// include the generated Bison C++ header after suppressing some warnings
#if defined(__clang__)
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic ignored "-Wsuggest-destructor-override"
#pragma clang diagnostic ignored "-Wswitch-enum"
#endif
#include "parse.hh"
#if defined(__clang__)
#pragma clang diagnostic pop
#endif
#if defined(HISTORY)
#include "history.hh"
#endif
#if defined(READLINE)
#include "readline.hh"
#else
#include "tilde.hh"
#endif
#if defined(HAVE_ICONV)
#include <iconv.h>
#endif
using std::map;
using std::set;
using std::string;
using std::vector;
namespace bash
{
/* Flag values for history_control */
enum hist_ctl_flags
{
HC_IGNSPACE = 0x01,
HC_IGNDUPS = 0x02,
HC_ERASEDUPS = 0x04,
HC_IGNBOTH = (HC_IGNSPACE | HC_IGNDUPS)
};
#if defined(STRICT_POSIX)
#undef HISTEXPAND_DEFAULT
#define HISTEXPAND_DEFAULT 0
#else
#if !defined(HISTEXPAND_DEFAULT)
#define HISTEXPAND_DEFAULT 1
#endif /* !HISTEXPAND_DEFAULT */
#endif
#define HEREDOC_MAX 16
#define NUM_POSIX_VARS 5
constexpr int NO_PIPE = -1;
constexpr int REDIRECT_BOTH = -2;
constexpr int NO_VARIABLE = -1;
/* Values that can be returned by execute_command (). */
constexpr int EXECUTION_FAILURE = 1;
constexpr int EXECUTION_SUCCESS = 0;
/* Usage messages by builtins result in a return status of 2. */
constexpr int EX_BADUSAGE = 2;
constexpr int EX_MISCERROR = 2;
/* Special exit statuses used by the shell, internally and externally. */
constexpr int EX_RETRYFAIL = 124;
constexpr int EX_WEXPCOMSUB = 125;
constexpr int EX_BINARY_FILE = 126;
constexpr int EX_NOEXEC = 126;
constexpr int EX_NOINPUT = 126;
constexpr int EX_NOTFOUND = 127;
constexpr int EX_SHERRBASE = 256; /* all special error values are > this. */
constexpr int EX_BADSYNTAX = 257; /* shell syntax error */
constexpr int EX_USAGE = 258; /* syntax error in usage */
constexpr int EX_REDIRFAIL = 259; /* redirection failed */
constexpr int EX_BADASSIGN = 260; /* variable assignment error */
constexpr int EX_EXPFAIL = 261; /* word expansion failed */
constexpr int EX_DISKFALLBACK
= 262; /* fall back to disk command from builtin */
static const char *str_on = "on";
static const char *str_off = "off";
/* Flag values that control parameter pattern substitution. */
enum match_flags
{
MATCH_ANY = 0x000,
MATCH_BEG = 0x001,
MATCH_END = 0x002,
MATCH_TYPEMASK = 0x003,
MATCH_GLOBREP = 0x010,
MATCH_QUOTED = 0x020,
MATCH_ASSIGNRHS = 0x040,
MATCH_STARSUB = 0x080
};
/* Flags for skip_to_delim */
enum sd_flags
{
SD_NOTHROW = 0x001, // don't throw exception on fatal error.
SD_INVERT = 0x002, // look for chars NOT in passed set
SD_NOQUOTEDELIM
= 0x004, // don't let single or double quotes act as delimiters
SD_NOSKIPCMD = 0x008, // don't skip over $(, <(, or >( command/process
// substitution; parse them as commands
SD_EXTGLOB = 0x010, // skip over extended globbing patterns if appropriate
SD_IGNOREQUOTE = 0x020, // single and double quotes are not special
SD_GLOB = 0x040, // skip over glob patterns like bracket expressions
SD_NOPROCSUB = 0x080, // don't parse process substitutions as commands
SD_COMPLETE = 0x100, // skip_to_delim during completion
SD_HISTEXP = 0x200, // skip_to_delim during history expansion
SD_ARITHEXP = 0x400, // skip_to_delim during arithmetic expansion
SD_NOERROR = 0x800 // don't print error messages
};
static inline sd_flags
operator| (const sd_flags &a, const sd_flags &b)
{
return static_cast<sd_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
/* Information about the current user. */
struct UserInfo
{
UserInfo ()
: uid (static_cast<uid_t> (-1)), euid (static_cast<uid_t> (-1)),
gid (static_cast<gid_t> (-1)), egid (static_cast<gid_t> (-1))
{
}
uid_t uid;
uid_t euid;
gid_t gid;
gid_t egid;
char *user_name;
char *shell; /* shell from the password file */
char *home_dir;
};
/* Definitions moved from expr.cc to simplify definition of Shell class. */
/* The Tokens. Singing "The Lion Sleeps Tonight". */
enum token_t
{
NONE = 0,
EQEQ = 1, /* "==" */
NEQ = 2, /* "!=" */
LEQ = 3, /* "<=" */
GEQ = 4, /* ">=" */
STR = 5, /* string */
NUM = 6, /* number */
LAND = 7, /* "&&" Logical AND */
LOR = 8, /* "||" Logical OR */
LSH = 9, /* "<<" Left SHift */
RSH = 10, /* ">>" Right SHift */
OP_ASSIGN = 11, /* op= expassign as in Posix.2 */
COND = 12, /* exp1 ? exp2 : exp3 */
POWER = 13, /* exp1**exp2 */
PREINC = 14, /* ++var */
PREDEC = 15, /* --var */
POSTINC = 16, /* var++ */
POSTDEC = 17, /* var-- */
EQ = '=',
GT = '>',
LT = '<',
PLUS = '+',
MINUS = '-',
MUL = '*',
DIV = '/',
MOD = '%',
NOT = '!',
LPAR = '(',
RPAR = ')',
BAND = '&', /* Bitwise AND */
BOR = '|', /* Bitwise OR. */
BXOR = '^', /* Bitwise eXclusive OR. */
BNOT = '~', /* Bitwise NOT; Two's complement. */
QUES = '?',
COL = ':',
COMMA = ','
};
struct token_lvalue
{
token_lvalue () : tokval (-1), ind (-1) {}
char *tokstr; /* possibly-rewritten lvalue if not nullptr */
SHELL_VAR *tokvar; /* variable described by array or var reference */
int64_t tokval; /* expression evaluated value */
int64_t ind; /* array index if not -1 */
void
init ()
{
tokstr = nullptr;
tokvar = nullptr;
tokval = ind = -1;
}
};
/* A struct defining a single expression context. */
struct EXPR_CONTEXT
{
char *expression, *tp, *lasttp;
char *tokstr;
int64_t tokval;
token_lvalue lval;
token_t curtok, lasttok;
int noeval;
};
/* Flags which describe the current handling state of a signal. */
enum sigmode_t
{
SIG_INHERITED = 0x0, /* Value inherited from parent. */
SIG_TRAPPED = 0x1, /* Currently trapped. */
SIG_HARD_IGNORE = 0x2, /* Signal was ignored on shell entry. */
SIG_SPECIAL = 0x4, /* Treat this signal specially. */
SIG_NO_TRAP = 0x8, /* Signal cannot be trapped. */
SIG_INPROGRESS = 0x10, /* Signal handler currently executing. */
SIG_CHANGED = 0x20, /* Trap value changed in trap handler. */
SIG_IGNORED = 0x40 /* The signal is currently being ignored. */
};
static inline sigmode_t &
operator|= (sigmode_t &a, const sigmode_t &b)
{
a = static_cast<sigmode_t> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
return a;
}
static inline sigmode_t
operator| (const sigmode_t &a, const sigmode_t &b)
{
return static_cast<sigmode_t> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
static inline sigmode_t &
operator&= (sigmode_t &a, const sigmode_t &b)
{
a = static_cast<sigmode_t> (static_cast<uint32_t> (a)
& static_cast<uint32_t> (b));
return a;
}
static inline sigmode_t
operator~(const sigmode_t &a)
{
return static_cast<sigmode_t> (~static_cast<uint32_t> (a));
}
#define SPECIAL_TRAP(s) \
((s) == EXIT_TRAP || (s) == DEBUG_TRAP || (s) == ERROR_TRAP \
|| (s) == RETURN_TRAP)
#define GETORIGSIG(sig) \
do \
{ \
original_signals[sig] = set_signal_handler (sig, SIG_DFL); \
set_signal_handler (sig, original_signals[sig]); \
if (original_signals[sig] == SIG_IGN) \
sigmodes[sig] |= SIG_HARD_IGNORE; \
} \
while (0)
#define SETORIGSIG(sig, handler) \
do \
{ \
original_signals[sig] = handler; \
if (original_signals[sig] == SIG_IGN) \
sigmodes[sig] |= SIG_HARD_IGNORE; \
} \
while (0)
#define GET_ORIGINAL_SIGNAL(sig) \
if (sig && sig < NSIG && original_signals[sig] == IMPOSSIBLE_TRAP_HANDLER) \
GETORIGSIG (sig)
// The global (constructed in shell.cc) pointer to the single shell object.
extern Shell *the_shell;
// Define two invalid references to indicate an invalid entry.
#define IMPOSSIBLE_TRAP_HANDLER reinterpret_cast<SigHandler> (2)
#define IMPOSSIBLE_TRAP_NAME (reinterpret_cast<char *> (the_shell))
enum print_flags
{
FUNC_NOFLAGS = 0,
FUNC_MULTILINE = 0x01,
FUNC_EXTERNAL = 0x02
};
static inline print_flags
operator| (const print_flags &a, const print_flags &b)
{
return static_cast<print_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
/* enum for sh_makepath function defined in lib/sh/makepath.c */
enum mp_flags
{
MP_NOFLAGS = 0,
MP_DOTILDE = 0x01,
MP_DOCWD = 0x02,
MP_RMDOT = 0x04,
MP_IGNDOT = 0x08
};
static inline mp_flags
operator| (const mp_flags &a, const mp_flags &b)
{
return static_cast<mp_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
/*
* Pushing and popping strings, implemented in parser.cc.
*/
enum push_string_flags
{
PSH_NOFLAGS = 0,
PSH_ALIAS = 0x01,
PSH_DPAREN = 0x02,
PSH_SOURCE = 0x04,
PSH_ARRAY = 0x08
};
class STRING_SAVER : public GENERIC_LIST
{
public:
STRING_SAVER () : GENERIC_LIST () {}
STRING_SAVER *
next ()
{
return static_cast<STRING_SAVER *> (next_);
}
string saved_line;
#if defined(ALIAS)
alias_t *expander; /* alias that caused this line to be pushed. */
#endif
size_t saved_line_index;
int expand_alias; /* Value to set expand_alias to when string is popped. */
int saved_line_terminator;
push_string_flags flags;
};
/* Here is a generic structure for associating character strings
with integers. It is used in the parser for shell tokenization. */
struct STRING_INT_ALIST
{
CONSTEXPR
STRING_INT_ALIST (const char *word_, int token_)
: word (word_), token (token_)
{
}
const char *word;
int token;
};
// Flags used when matching pairs of grouping constructs.
enum pgroup_flags
{
P_NOFLAGS = 0,
P_FIRSTCLOSE = 0x0001,
P_ALLOWESC = 0x0002,
P_DQUOTE = 0x0004,
P_COMMAND = 0x0008, // parsing a command, so look for comments
P_BACKQUOTE = 0x0010, // parsing a backquoted command substitution
P_ARRAYSUB = 0x0020, // parsing a [...] array subscript for assignment
P_DOLBRACE = 0x0040 // parsing a ${...} construct
};
static inline pgroup_flags
operator& (const pgroup_flags &a, const pgroup_flags &b)
{
return static_cast<pgroup_flags> (static_cast<uint32_t> (a)
& static_cast<uint32_t> (b));
}
static inline pgroup_flags
operator| (const pgroup_flags &a, const pgroup_flags &b)
{
return static_cast<pgroup_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
static inline pgroup_flags
operator~(const pgroup_flags &a)
{
return static_cast<pgroup_flags> (~static_cast<uint32_t> (a));
}
/* Lexical state while parsing a grouping construct or $(...). */
enum lexical_state_flags
{
LEX_NOFLAGS = 0,
LEX_WASDOL = 0x0001,
LEX_CKCOMMENT = 0x0002,
LEX_INCOMMENT = 0x0004,
LEX_PASSNEXT = 0x0008,
LEX_RESWDOK = 0x0010,
LEX_CKCASE = 0x0020,
LEX_INCASE = 0x0040,
LEX_INHEREDOC = 0x0080,
LEX_HEREDELIM = 0x0100, // reading here-doc delimiter
LEX_STRIPDOC = 0x0200, // <<- strip tabs from here doc delim
LEX_QUOTEDDOC = 0x0400, // here doc with quoted delim
LEX_INWORD = 0x0800,
LEX_GTLT = 0x1000,
LEX_CKESAC = 0x2000, // check esac after in -- for later
LEX_CASEWD = 0x4000, // word after case
LEX_PATLIST = 0x8000 // case statement pattern list
};
static inline lexical_state_flags &
operator|= (lexical_state_flags &a, const lexical_state_flags &b)
{
a = static_cast<lexical_state_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
return a;
}
static inline lexical_state_flags &
operator&= (lexical_state_flags &a, const lexical_state_flags &b)
{
a = static_cast<lexical_state_flags> (static_cast<uint32_t> (a)
& static_cast<uint32_t> (b));
return a;
}
static inline lexical_state_flags
operator| (const lexical_state_flags &a, const lexical_state_flags &b)
{
return static_cast<lexical_state_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
static inline lexical_state_flags
operator& (const lexical_state_flags &a, const lexical_state_flags &b)
{
return static_cast<lexical_state_flags> (static_cast<uint32_t> (a)
& static_cast<uint32_t> (b));
}
static inline lexical_state_flags
operator~(const lexical_state_flags &a)
{
return static_cast<lexical_state_flags> (~static_cast<uint32_t> (a));
}
#if defined(ALIAS)
enum print_alias_flags
{
PAL_NOFLAGS = 0,
PAL_REUSABLE = 0x01
};
static inline print_alias_flags &
operator|= (print_alias_flags &a, const print_alias_flags &b)
{
a = static_cast<print_alias_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
return a;
}
#endif
/* Flags for _evalfile() */
enum evalfile_flags_t
{
FEVAL_NOFLAGS = 0,
FEVAL_ENOENTOK = 0x001,
FEVAL_BUILTIN = 0x002,
FEVAL_UNWINDPROT = 0x004,
FEVAL_NONINT = 0x008,
FEVAL_THROW = 0x010,
FEVAL_HISTORY = 0x020,
FEVAL_CHECKBINARY = 0x040,
FEVAL_REGFILE = 0x080,
FEVAL_NOPUSHARGS = 0x100
};
static inline evalfile_flags_t &
operator|= (evalfile_flags_t &a, const evalfile_flags_t &b)
{
a = static_cast<evalfile_flags_t> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
return a;
}
static inline evalfile_flags_t
operator| (const evalfile_flags_t &a, const evalfile_flags_t &b)
{
return static_cast<evalfile_flags_t> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
/* Values for flags argument to do_redirections */
enum do_redir_flags
{
RX_ACTIVE = 0x01, /* do it; don't just go through the motions */
RX_UNDOABLE = 0x02, /* make a list to undo these redirections */
RX_CLEXEC = 0x04, /* set close-on-exec for opened fds > 2 */
RX_INTERNAL = 0x08,
RX_USER = 0x10,
RX_SAVCLEXEC = 0x20, /* set close-on-exec off in restored fd even though
saved on has it on */
RX_SAVEFD = 0x40 /* fd used to save another even if < SHELL_FD_BASE */
};
static inline do_redir_flags
operator| (const do_redir_flags &a, const do_redir_flags &b)
{
return static_cast<do_redir_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
/* Values for the flags argument to binary_test */
enum binary_test_flags
{
TEST_NOFLAGS = 0,
TEST_PATMATCH = 0x01,
TEST_ARITHEXP = 0x02,
TEST_LOCALE = 0x04,
TEST_ARRAYEXP = 0x08 // array subscript expansion
};
static inline binary_test_flags
operator| (const binary_test_flags &a, const binary_test_flags &b)
{
return static_cast<binary_test_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
/* Flags for search_for_command */
enum cmd_search_flags
{
CMDSRCH_NOFLAGS = 0,
CMDSRCH_HASH = 0x01,
CMDSRCH_STDPATH = 0x02,
CMDSRCH_TEMPENV = 0x04
};
static inline cmd_search_flags
operator| (const cmd_search_flags &a, const cmd_search_flags &b)
{
return static_cast<cmd_search_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
}
// Data type for token lookup.
typedef map<string_view, parser::token_kind_type> STRING_TOKEN_MAP;
// Parser subclass to implement error printing.
class BashParser : public bash::parser
{
virtual void error (const string &) override;
};
struct sh_getopt_state_t
{
char *gs_optarg;
char *gs_nextchar;
int gs_optind;
int gs_curopt;
int gs_charindex;
int gs_flags;
};
// This char array size is assumed by subst.cc and elsewhere.
#define SYNSIZE 256
/* flags for find_variable_internal */
enum find_var_flags
{
FV_NOFLAGS = 0,
FV_FORCETEMPENV = 0x01,
FV_SKIPINVISIBLE = 0x02,
FV_NODYNAMIC = 0x04
};
static inline find_var_flags &
operator|= (find_var_flags &a, const find_var_flags &b)
{
a = static_cast<find_var_flags> (static_cast<uint32_t> (a)
| static_cast<uint32_t> (b));
return a;
}
/* Simple shell state: variables that can be memcpy'd to subshells. */
class SimpleState
{
public:
// Initialize with default values (defined in shell.cc).
SimpleState ();
#if defined(PREFER_POSIX_SPAWN)
explicit SimpleState (int fd); // TODO: Load initial values from pipe
#endif
// Start of protected variables, ordered by decreasing alignment.
protected:
/* ************************************************************** */
/* Bash Variables (int64_t/uint64_t) */
/* ************************************************************** */
/* The value of $SECONDS. This is the number of seconds since shell
invocation, or, the number of seconds since the last assignment + the
value of the last assignment. */
int64_t seconds_value_assigned;
/* ************************************************************** */
/* Bash Variables (array/struct/ptr types) */
/* ************************************************************** */
/* time in seconds when the shell was started */
time_t shell_start_time;
struct timeval shellstart;
/* ************************************************************** */
/* Bash Variables (size_t: 32/64-bit) */
/* ************************************************************** */
#if defined(HANDLE_MULTIBYTE)
size_t ifs_firstc_len;
#endif
/* The maximum bytes per char for this locale. */
size_t locale_mb_cur_max;
/* ************************************************************** */
/* Bash Variables (32-bit int types) */
/* ************************************************************** */
/* from expr.cc: the OP in OP= */
token_t assigntok;
// from bashhist.cc
int history_lines_this_session;
int history_lines_in_file;
int history_control;
/* The controlling tty for this shell. */
int shell_tty;
/* The shell's process group. */
pid_t shell_pgrp;
/* The terminal's process group. */
pid_t terminal_pgrp;
/* The process group of the shell's parent. */
pid_t original_pgrp;
/* The process group of the pipeline currently being made. */
pid_t pipeline_pgrp;
#if defined(PGRP_PIPE)
/* Pipes which each shell uses to communicate with the process group leader
until all of the processes in a pipeline have been started. Then the
process leader is allowed to continue. */
int pgrp_pipe[2];
#endif
/* Last child made by the shell. */
pid_t last_made_pid;
/* Pid of the last asynchronous child. */
pid_t last_asynchronous_pid;
/* Used to synchronize between wait_for and other functions and the SIGCHLD
signal handler. */
int sigchld;
int queue_sigchld;
subshell_flags subshell_environment;
int shell_compatibility_level;
/* The number of commands executed so far. */
int current_command_number;
/* Non-zero is the recursion depth for commands. */
int indirection_level;
#if defined(BUFFERED_INPUT)
/* The file descriptor from which the shell is reading input. */
int default_buffered_input;
int bash_input_fd_changed;
#endif
/* The current variable context. This is really a count of how deep into
executing functions we are. */
int variable_context;
/* The number of times BASH has been executed. This is set
by initialize_variables (). */
int shell_level;
/* variables from evalfile.c */
int sourcelevel;
/* Variables declared in execute_cmd.cc, used by many other files */
/* Whether or not the last command (corresponding to last_command_exit_value)
was terminated by a signal, and, if so, which one. */
int last_command_exit_signal;
int executing_builtin;
int executing_list; /* list nesting level */
int comsub_ignore_return; /* can be greater than 1 */
int subshell_level;
int funcnest;
int funcnest_max;
int evalnest;
int evalnest_max;
int sourcenest;
int sourcenest_max;
/* $LINENO ($BASH_LINENO) for use by an ERR trap. */
int line_number_for_err_trap;
#if defined(PROCESS_SUBSTITUTION) && defined(HAVE_DEV_FD)
int nfds;
#endif
/* For catching RETURN in a function. */
int return_catch_flag;
/* variables from break.def/continue.def */
int breaking;
int continuing;
int loop_level;
int posparam_count;
/* The value of $$. */
pid_t dollar_dollar_pid;
int subshell_argc;
/* The random number seed. You can change this by setting RANDOM. */
uint32_t rseed;
uint32_t rseed32;
uint32_t last_rand32;
int last_random_value;
int seeded_subshell;
/* Set to errno when a here document cannot be created for some reason.
Used to print a reasonable error message. */
int heredoc_errno;
/* variables from subst.cc */
/* Process ID of the last command executed within command substitution. */
pid_t last_command_subst_pid;
pid_t current_command_subst_pid;
/* variables from lib/sh/shtty.cc */
// additional 32-bit variables
unsigned int zread_lind; // read index in zread_lbuf
unsigned int zread_lused; // bytes used in zread_lbuf
int list_optopt;
int list_opttype;
word_desc_flags list_optflags;
int export_env_index;
int export_env_size;
#if defined(READLINE)
int winsize_assignment; /* currently assigning to LINES or COLUMNS */
#endif
/* The number of lines read from input while creating the current command. */
int current_command_line_count;
/* The number of lines in a command saved while we run parse_and_execute */
int saved_command_line_count;
/* A count of signals received for which we have trap handlers. */
int pending_traps[NSIG];
/* Set to the number of the signal we're running the trap for + 1.
Used in execute_cmd.cc and builtins/common.cc to clean up when
parse_and_execute does not return normally after executing the
trap command (e.g., when `return' is executed in the trap command). */
int running_trap;
/* Set to last_command_exit_value before running a trap. */
int trap_saved_exit_value;
/* The (trapped) signal received while executing in the `wait' builtin */
int wait_signal_received;
int trapped_signal_received;
#if defined(SELECT_COMMAND)
int select_lines;
int select_cols;
#endif
public:
// The following are accessed by the parser.
/* The token that currently denotes the end of parse. */
int shell_eof_token;
/* variables from evalstring.c */
int parse_and_execute_level;
/* The value returned by the last synchronous command (possibly modified
by the parser). */
int last_command_exit_value;
/* The current parser state. */
pstate_flags parser_state;
/* The line number in a script at which an arithmetic for command starts. */
int arith_for_lineno;
/* Do that silly `type "bye" to exit' stuff. You know, "ignoreeof". */
/* The number of times that we have encountered an EOF character without
another character intervening. When this gets above the limit, the
shell terminates. */
int eof_encountered;
/* The limit for eof_encountered. */
int eof_encountered_limit;
/* The line number in a script where the word in a `case WORD', `select WORD'
or `for WORD' begins. This is a nested command maximum, since the array
index is decremented after a case, select, or for command is parsed. */
#define MAX_CASE_NEST 128
int word_lineno[MAX_CASE_NEST + 1];
int word_top;
/* The line number in a script on which a function definition starts. */
int function_dstart;
/* The line number in a script on which a function body starts. */
int function_bstart;
/* The current index into the redir_stack array. */
int need_here_doc;
/* If non-zero, it is the token that we want read_token to return
regardless of what text is (or isn't) present to be read. This
is reset by read_token. If token_to_read == WORD or
ASSIGNMENT_WORD, yylval.word should be set to word_desc_to_read. */
parser::token_kind_type token_to_read;
// Current printing indentation level.
int indentation;
// Indentation amount.
int indentation_amount;
// Increase the printing indentation level.
void
add_indentation ()
{
indentation += indentation_amount;
}
// Decrease the printing indentation level.
void
sub_indentation ()
{
indentation -= indentation_amount;
}
/* The depth of the group commands that we are currently printing. This
includes the group command that is a function body. */
int group_command_nesting;
// When non-zero, skip the next indent.
int skip_this_indent;
/* Non-zero means stuff being printed is inside of a function def. */
int inside_function_def;
/* Non-zero means stuff being printed is inside of a connection. */
int printing_connection;
/* Non-zero means stuff being printed is inside of a command sub. */
int printing_comsub;
/* The globally known line number. */
int line_number;