-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvariables.c
6604 lines (5679 loc) · 168 KB
/
variables.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
/* variables.c -- Functions for hacking shell variables. */
/* 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 "posixstat.h"
#include "posixtime.h"
#if defined (__QNX__)
# if defined (__QNXNTO__)
# include <sys/netmgr.h>
# else
# include <sys/vc.h>
# endif /* !__QNXNTO__ */
#endif /* __QNX__ */
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include <stdio.h>
#include "chartypes.h"
#if defined (HAVE_PWD_H)
# include <pwd.h>
#endif
#include "bashansi.h"
#include "bashintl.h"
#include "filecntl.h"
#define NEED_XTRACE_SET_DECL
#include "shell.h"
#include "parser.h"
#include "flags.h"
#include "execute_cmd.h"
#include "findcmd.h"
#include "mailcheck.h"
#include "input.h"
#include "hashcmd.h"
#include "pathexp.h"
#include "alias.h"
#include "jobs.h"
#include "version.h"
#include "builtins/getopt.h"
#include "builtins/common.h"
#include "builtins/builtext.h"
#if defined (READLINE)
# include "bashline.h"
# include <readline/readline.h>
#else
# include <tilde/tilde.h>
#endif
#if defined (HISTORY)
# include "bashhist.h"
# include <readline/history.h>
#endif /* HISTORY */
#if defined (PROGRAMMABLE_COMPLETION)
# include "pcomplete.h"
#endif
#define VARIABLES_HASH_BUCKETS 1024 /* must be power of two */
#define FUNCTIONS_HASH_BUCKETS 512
#define TEMPENV_HASH_BUCKETS 4 /* must be power of two */
#define BASHFUNC_PREFIX "BASH_FUNC_"
#define BASHFUNC_PREFLEN 10 /* == strlen(BASHFUNC_PREFIX */
#define BASHFUNC_SUFFIX "%%"
#define BASHFUNC_SUFFLEN 2 /* == strlen(BASHFUNC_SUFFIX) */
#if ARRAY_EXPORT
#define BASHARRAY_PREFIX "BASH_ARRAY_"
#define BASHARRAY_PREFLEN 11
#define BASHARRAY_SUFFIX "%%"
#define BASHARRAY_SUFFLEN 2
#define BASHASSOC_PREFIX "BASH_ASSOC_"
#define BASHASSOC_PREFLEN 11
#define BASHASSOC_SUFFIX "%%" /* needs to be the same as BASHARRAY_SUFFIX */
#define BASHASSOC_SUFFLEN 2
#endif
/* flags for find_variable_internal */
#define FV_FORCETEMPENV 0x01
#define FV_SKIPINVISIBLE 0x02
#define FV_NODYNAMIC 0x04
extern char **environ;
/* Variables used here and defined in other files. */
extern time_t shell_start_time;
extern struct timeval shellstart;
/* The list of shell variables that the user has created at the global
scope, or that came from the environment. */
VAR_CONTEXT *global_variables = (VAR_CONTEXT *)NULL;
/* The current list of shell variables, including function scopes */
VAR_CONTEXT *shell_variables = (VAR_CONTEXT *)NULL;
/* The list of shell functions that the user has created, or that came from
the environment. */
HASH_TABLE *shell_functions = (HASH_TABLE *)NULL;
HASH_TABLE *invalid_env = (HASH_TABLE *)NULL;
#if defined (DEBUGGER)
/* The table of shell function definitions that the user defined or that
came from the environment. */
HASH_TABLE *shell_function_defs = (HASH_TABLE *)NULL;
#endif
/* The current variable context. This is really a count of how deep into
executing functions we are. */
int variable_context = 0;
/* If non-zero, local variables inherit values and attributes from a variable
with the same name at a previous scope. */
int localvar_inherit = 0;
/* If non-zero, calling `unset' on local variables in previous scopes marks
them as invisible so lookups find them unset. This is the same behavior
as local variables in the current local scope. */
int localvar_unset = 0;
/* The set of shell assignments which are made only in the environment
for a single command. */
HASH_TABLE *temporary_env = (HASH_TABLE *)NULL;
/* Set to non-zero if an assignment error occurs while putting variables
into the temporary environment. */
int tempenv_assign_error;
/* Some funky variables which are known about specially. Here is where
"$*", "$1", and all the cruft is kept. */
char *dollar_vars[10];
WORD_LIST *rest_of_args = (WORD_LIST *)NULL;
int posparam_count = 0;
/* The value of $$. */
pid_t dollar_dollar_pid;
/* Non-zero means that we have to remake EXPORT_ENV. */
int array_needs_making = 1;
/* The number of times BASH has been executed. This is set
by initialize_variables (). */
int shell_level = 0;
/* An array which is passed to commands as their environment. It is
manufactured from the union of the initial environment and the
shell variables that are marked for export. */
char **export_env = (char **)NULL;
static int export_env_index;
static int export_env_size;
#if defined (READLINE)
static int winsize_assignment; /* currently assigning to LINES or COLUMNS */
#endif
SHELL_VAR nameref_invalid_value;
static SHELL_VAR nameref_maxloop_value;
static HASH_TABLE *last_table_searched; /* hash_lookup sets this */
static VAR_CONTEXT *last_context_searched;
/* Some forward declarations. */
static void create_variable_tables PARAMS((void));
static void set_machine_vars PARAMS((void));
static void set_home_var PARAMS((void));
static void set_shell_var PARAMS((void));
static char *get_bash_name PARAMS((void));
static void initialize_shell_level PARAMS((void));
static void uidset PARAMS((void));
#if defined (ARRAY_VARS)
static void make_vers_array PARAMS((void));
#endif
static SHELL_VAR *null_assign PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
#if defined (ARRAY_VARS)
static SHELL_VAR *null_array_assign PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
#endif
static SHELL_VAR *get_self PARAMS((SHELL_VAR *));
#if defined (ARRAY_VARS)
static SHELL_VAR *init_dynamic_array_var PARAMS((char *, sh_var_value_func_t *, sh_var_assign_func_t *, int));
static SHELL_VAR *init_dynamic_assoc_var PARAMS((char *, sh_var_value_func_t *, sh_var_assign_func_t *, int));
#endif
static inline SHELL_VAR *set_int_value (SHELL_VAR *, intmax_t, int);
static inline SHELL_VAR *set_string_value (SHELL_VAR *, const char *, int);
static SHELL_VAR *assign_seconds PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
static SHELL_VAR *get_seconds PARAMS((SHELL_VAR *));
static SHELL_VAR *init_seconds_var PARAMS((void));
static SHELL_VAR *assign_random PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
static SHELL_VAR *get_random PARAMS((SHELL_VAR *));
static SHELL_VAR *get_urandom PARAMS((SHELL_VAR *));
static SHELL_VAR *assign_lineno PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
static SHELL_VAR *get_lineno PARAMS((SHELL_VAR *));
static SHELL_VAR *assign_subshell PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
static SHELL_VAR *get_subshell PARAMS((SHELL_VAR *));
static SHELL_VAR *get_epochseconds PARAMS((SHELL_VAR *));
static SHELL_VAR *get_epochrealtime PARAMS((SHELL_VAR *));
static SHELL_VAR *get_bashpid PARAMS((SHELL_VAR *));
static SHELL_VAR *get_bash_argv0 PARAMS((SHELL_VAR *));
static SHELL_VAR *assign_bash_argv0 PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
static void set_argv0 PARAMS((void));
#if defined (HISTORY)
static SHELL_VAR *get_histcmd PARAMS((SHELL_VAR *));
#endif
#if defined (READLINE)
static SHELL_VAR *get_comp_wordbreaks PARAMS((SHELL_VAR *));
static SHELL_VAR *assign_comp_wordbreaks PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
#endif
#if defined (PUSHD_AND_POPD) && defined (ARRAY_VARS)
static SHELL_VAR *assign_dirstack PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
static SHELL_VAR *get_dirstack PARAMS((SHELL_VAR *));
#endif
#if defined (ARRAY_VARS)
static SHELL_VAR *get_groupset PARAMS((SHELL_VAR *));
# if defined (DEBUGGER)
static SHELL_VAR *get_bashargcv PARAMS((SHELL_VAR *));
# endif
static SHELL_VAR *build_hashcmd PARAMS((SHELL_VAR *));
static SHELL_VAR *get_hashcmd PARAMS((SHELL_VAR *));
static SHELL_VAR *assign_hashcmd PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
# if defined (ALIAS)
static SHELL_VAR *build_aliasvar PARAMS((SHELL_VAR *));
static SHELL_VAR *get_aliasvar PARAMS((SHELL_VAR *));
static SHELL_VAR *assign_aliasvar PARAMS((SHELL_VAR *, char *, arrayind_t, char *));
# endif
#endif
static SHELL_VAR *get_funcname PARAMS((SHELL_VAR *));
static SHELL_VAR *init_funcname_var PARAMS((void));
static void initialize_dynamic_variables PARAMS((void));
static SHELL_VAR *bind_invalid_envvar PARAMS((const char *, char *, int));
static int var_sametype PARAMS((SHELL_VAR *, SHELL_VAR *));
static SHELL_VAR *hash_lookup PARAMS((const char *, HASH_TABLE *));
static SHELL_VAR *new_shell_variable PARAMS((const char *));
static SHELL_VAR *make_new_variable PARAMS((const char *, HASH_TABLE *));
static SHELL_VAR *bind_variable_internal PARAMS((const char *, char *, HASH_TABLE *, int, int));
static void dispose_variable_value PARAMS((SHELL_VAR *));
static void free_variable_hash_data PARAMS((PTR_T));
static VARLIST *vlist_alloc PARAMS((int));
static VARLIST *vlist_realloc PARAMS((VARLIST *, int));
static void vlist_add PARAMS((VARLIST *, SHELL_VAR *, int));
static void flatten PARAMS((HASH_TABLE *, sh_var_map_func_t *, VARLIST *, int));
static int qsort_var_comp PARAMS((SHELL_VAR **, SHELL_VAR **));
static SHELL_VAR **vapply PARAMS((sh_var_map_func_t *));
static SHELL_VAR **fapply PARAMS((sh_var_map_func_t *));
static int visible_var PARAMS((SHELL_VAR *));
static int visible_and_exported PARAMS((SHELL_VAR *));
static int export_environment_candidate PARAMS((SHELL_VAR *));
static int local_and_exported PARAMS((SHELL_VAR *));
static int visible_variable_in_context PARAMS((SHELL_VAR *));
static int variable_in_context PARAMS((SHELL_VAR *));
#if defined (ARRAY_VARS)
static int visible_array_vars PARAMS((SHELL_VAR *));
#endif
static SHELL_VAR *find_variable_internal PARAMS((const char *, int));
static SHELL_VAR *find_nameref_at_context PARAMS((SHELL_VAR *, VAR_CONTEXT *));
static SHELL_VAR *find_variable_nameref_context PARAMS((SHELL_VAR *, VAR_CONTEXT *, VAR_CONTEXT **));
static SHELL_VAR *find_variable_last_nameref_context PARAMS((SHELL_VAR *, VAR_CONTEXT *, VAR_CONTEXT **));
static SHELL_VAR *bind_tempenv_variable PARAMS((const char *, char *));
static void push_posix_temp_var PARAMS((PTR_T));
static void push_temp_var PARAMS((PTR_T));
static void propagate_temp_var PARAMS((PTR_T));
static void dispose_temporary_env PARAMS((sh_free_func_t *));
static inline char *mk_env_string PARAMS((const char *, const char *, int));
static char **make_env_array_from_var_list PARAMS((SHELL_VAR **));
static char **make_var_export_array PARAMS((VAR_CONTEXT *));
static char **make_func_export_array PARAMS((void));
static void add_temp_array_to_env PARAMS((char **, int, int));
static int n_shell_variables PARAMS((void));
static int set_context PARAMS((SHELL_VAR *));
static void push_func_var PARAMS((PTR_T));
static void push_builtin_var PARAMS((PTR_T));
static void push_exported_var PARAMS((PTR_T));
static void delete_local_contexts PARAMS((VAR_CONTEXT *));
/* This needs to be looked at again. */
static inline void push_posix_tempvar_internal PARAMS((SHELL_VAR *, int));
static inline int find_special_var PARAMS((const char *));
static void
create_variable_tables ()
{
if (shell_variables == 0)
{
shell_variables = global_variables = new_var_context ((char *)NULL, 0);
shell_variables->scope = 0;
shell_variables->table = hash_create (VARIABLES_HASH_BUCKETS);
}
if (shell_functions == 0)
shell_functions = hash_create (FUNCTIONS_HASH_BUCKETS);
#if defined (DEBUGGER)
if (shell_function_defs == 0)
shell_function_defs = hash_create (FUNCTIONS_HASH_BUCKETS);
#endif
}
/* Initialize the shell variables from the current environment.
If PRIVMODE is nonzero, don't import functions from ENV or
parse $SHELLOPTS. */
void
initialize_shell_variables (env, privmode)
char **env;
int privmode;
{
char *name, *string, *temp_string;
int c, char_index, string_index, string_length, ro;
SHELL_VAR *temp_var;
create_variable_tables ();
for (string_index = 0; env && (string = env[string_index++]); )
{
char_index = 0;
name = string;
while ((c = *string++) && c != '=')
;
if (string[-1] == '=')
char_index = string - name - 1;
/* If there are weird things in the environment, like `=xxx' or a
string without an `=', just skip them. */
if (char_index == 0)
continue;
/* ASSERT(name[char_index] == '=') */
name[char_index] = '\0';
/* Now, name = env variable name, string = env variable value, and
char_index == strlen (name) */
temp_var = (SHELL_VAR *)NULL;
#if defined (FUNCTION_IMPORT)
/* If exported function, define it now. Don't import functions from
the environment in privileged mode. */
if (privmode == 0 && read_but_dont_execute == 0 &&
STREQN (BASHFUNC_PREFIX, name, BASHFUNC_PREFLEN) &&
STREQ (BASHFUNC_SUFFIX, name + char_index - BASHFUNC_SUFFLEN) &&
STREQN ("() {", string, 4))
{
size_t namelen;
char *tname; /* desired imported function name */
namelen = char_index - BASHFUNC_PREFLEN - BASHFUNC_SUFFLEN;
tname = name + BASHFUNC_PREFLEN; /* start of func name */
tname[namelen] = '\0'; /* now tname == func name */
string_length = strlen (string);
temp_string = (char *)xmalloc (namelen + string_length + 2);
memcpy (temp_string, tname, namelen);
temp_string[namelen] = ' ';
memcpy (temp_string + namelen + 1, string, string_length + 1);
/* Don't import function names that are invalid identifiers from the
environment in posix mode, though we still allow them to be defined as
shell variables. */
if (absolute_program (tname) == 0 && (posixly_correct == 0 || legal_identifier (tname)))
parse_and_execute (temp_string, tname, SEVAL_NONINT|SEVAL_NOHIST|SEVAL_FUNCDEF|SEVAL_ONECMD);
else
free (temp_string); /* parse_and_execute does this */
if (temp_var = find_function (tname))
{
VSETATTR (temp_var, (att_exported|att_imported));
array_needs_making = 1;
}
else
{
if (temp_var = bind_invalid_envvar (name, string, 0))
{
VSETATTR (temp_var, (att_exported | att_imported | att_invisible));
array_needs_making = 1;
}
last_command_exit_value = EXECUTION_FAILURE;
report_error (_("error importing function definition for `%s'"), tname);
}
/* Restore original suffix */
tname[namelen] = BASHFUNC_SUFFIX[0];
}
else
#endif /* FUNCTION_IMPORT */
#if defined (ARRAY_VARS)
# if ARRAY_EXPORT
/* Array variables may not yet be exported. */
if (STREQN (BASHARRAY_PREFIX, name, BASHARRAY_PREFLEN) &&
STREQN (BASHARRAY_SUFFIX, name + char_index - BASHARRAY_SUFFLEN, BASHARRAY_SUFFLEN) &&
*string == '(' && string[1] == '[' && string[strlen (string) - 1] == ')')
{
size_t namelen;
char *tname; /* desired imported array variable name */
namelen = char_index - BASHARRAY_PREFLEN - BASHARRAY_SUFFLEN;
tname = name + BASHARRAY_PREFLEN; /* start of variable name */
tname[namelen] = '\0'; /* now tname == varname */
string_length = 1;
temp_string = extract_array_assignment_list (string, &string_length);
temp_var = assign_array_from_string (tname, temp_string, 0);
FREE (temp_string);
if (temp_var)
{
VSETATTR (temp_var, (att_exported | att_imported));
array_needs_making = 1;
}
}
else if (STREQN (BASHASSOC_PREFIX, name, BASHASSOC_PREFLEN) &&
STREQN (BASHASSOC_SUFFIX, name + char_index - BASHASSOC_SUFFLEN, BASHASSOC_SUFFLEN) &&
*string == '(' && string[1] == '[' && string[strlen (string) - 1] == ')')
{
size_t namelen;
char *tname; /* desired imported assoc variable name */
namelen = char_index - BASHASSOC_PREFLEN - BASHASSOC_SUFFLEN;
tname = name + BASHASSOC_PREFLEN; /* start of variable name */
tname[namelen] = '\0'; /* now tname == varname */
/* need to make sure it exists as an associative array first */
temp_var = find_or_make_array_variable (tname, 2);
if (temp_var)
{
string_length = 1;
temp_string = extract_array_assignment_list (string, &string_length);
temp_var = assign_array_var_from_string (temp_var, temp_string, 0);
}
FREE (temp_string);
if (temp_var)
{
VSETATTR (temp_var, (att_exported | att_imported));
array_needs_making = 1;
}
}
else
# endif /* ARRAY_EXPORT */
#endif
{
ro = 0;
/* If we processed a command-line option that caused SHELLOPTS to be
set, it may already be set (and read-only) by the time we process
the shell's environment. */
if (/* posixly_correct &&*/ STREQ (name, "SHELLOPTS"))
{
temp_var = find_variable ("SHELLOPTS");
ro = temp_var && readonly_p (temp_var);
if (temp_var)
VUNSETATTR (temp_var, att_readonly);
}
if (legal_identifier (name))
{
temp_var = bind_variable (name, string, 0);
if (temp_var)
{
VSETATTR (temp_var, (att_exported | att_imported));
if (ro)
VSETATTR (temp_var, att_readonly);
}
}
else
{
temp_var = bind_invalid_envvar (name, string, 0);
if (temp_var)
VSETATTR (temp_var, (att_exported | att_imported | att_invisible));
}
if (temp_var)
array_needs_making = 1;
}
name[char_index] = '=';
/* temp_var can be NULL if it was an exported function with a syntax
error (a different bug, but it still shouldn't dump core). */
if (temp_var && function_p (temp_var) == 0) /* XXX not yet */
{
CACHE_IMPORTSTR (temp_var, name);
}
}
set_pwd ();
/* Set up initial value of $_ */
temp_var = set_if_not ("_", dollar_vars[0]);
/* Remember this pid. */
dollar_dollar_pid = getpid ();
/* Now make our own defaults in case the vars that we think are
important are missing. */
temp_var = set_if_not ("PATH", DEFAULT_PATH_VALUE);
temp_var = set_if_not ("TERM", "dumb");
#if defined (__QNX__)
/* set node id -- don't import it from the environment */
{
char node_name[22];
# if defined (__QNXNTO__)
netmgr_ndtostr(ND2S_LOCAL_STR, ND_LOCAL_NODE, node_name, sizeof(node_name));
# else
qnx_nidtostr (getnid (), node_name, sizeof (node_name));
# endif
temp_var = bind_variable ("NODE", node_name, 0);
if (temp_var)
set_auto_export (temp_var);
}
#endif
/* set up the prompts. */
if (interactive_shell)
{
#if defined (PROMPT_STRING_DECODE)
set_if_not ("PS1", primary_prompt);
#else
if (current_user.uid == -1)
get_current_user_info ();
set_if_not ("PS1", current_user.euid == 0 ? "# " : primary_prompt);
#endif
set_if_not ("PS2", secondary_prompt);
}
if (current_user.euid == 0)
bind_variable ("PS4", "+ ", 0);
else
set_if_not ("PS4", "+ ");
/* Don't allow IFS to be imported from the environment. */
temp_var = bind_variable ("IFS", " \t\n", 0);
setifs (temp_var);
/* Magic machine types. Pretty convenient. */
set_machine_vars ();
/* Default MAILCHECK for interactive shells. Defer the creation of a
default MAILPATH until the startup files are read, because MAIL
names a mail file if MAILPATH is not set, and we should provide a
default only if neither is set. */
if (interactive_shell)
{
temp_var = set_if_not ("MAILCHECK", posixly_correct ? "600" : "60");
VSETATTR (temp_var, att_integer);
}
/* Do some things with shell level. */
initialize_shell_level ();
set_ppid ();
set_argv0 ();
/* Initialize the `getopts' stuff. */
temp_var = bind_variable ("OPTIND", "1", 0);
VSETATTR (temp_var, att_integer);
getopts_reset (0);
bind_variable ("OPTERR", "1", 0);
sh_opterr = 1;
if (login_shell == 1 && posixly_correct == 0)
set_home_var ();
/* Get the full pathname to THIS shell, and set the BASH variable
to it. */
name = get_bash_name ();
temp_var = bind_variable ("BASH", name, 0);
free (name);
/* Make the exported environment variable SHELL be the user's login
shell. Note that the `tset' command looks at this variable
to determine what style of commands to output; if it ends in "csh",
then C-shell commands are output, else Bourne shell commands. */
set_shell_var ();
/* Make a variable called BASH_VERSION which contains the version info. */
bind_variable ("BASH_VERSION", shell_version_string (), 0);
#if defined (ARRAY_VARS)
make_vers_array ();
#endif
if (command_execution_string)
bind_variable ("BASH_EXECUTION_STRING", command_execution_string, 0);
/* Find out if we're supposed to be in Posix.2 mode via an
environment variable. */
temp_var = find_variable ("POSIXLY_CORRECT");
if (!temp_var)
temp_var = find_variable ("POSIX_PEDANTIC");
if (temp_var && imported_p (temp_var))
sv_strict_posix (temp_var->name);
#if defined (HISTORY)
/* Set history variables to defaults, and then do whatever we would
do if the variable had just been set. Do this only in the case
that we are remembering commands on the history list. */
if (remember_on_history)
{
name = bash_tilde_expand (posixly_correct ? "~/.sh_history" : "~/.bash_history", 0);
set_if_not ("HISTFILE", name);
free (name);
}
#endif /* HISTORY */
/* Seed the random number generators. */
seedrand ();
seedrand32 ();
/* Handle some "special" variables that we may have inherited from a
parent shell. */
if (interactive_shell)
{
temp_var = find_variable ("IGNOREEOF");
if (!temp_var)
temp_var = find_variable ("ignoreeof");
if (temp_var && imported_p (temp_var))
sv_ignoreeof (temp_var->name);
}
#if defined (HISTORY)
if (interactive_shell && remember_on_history)
{
sv_history_control ("HISTCONTROL");
sv_histignore ("HISTIGNORE");
sv_histtimefmt ("HISTTIMEFORMAT");
}
#endif /* HISTORY */
#if defined (READLINE) && defined (STRICT_POSIX)
/* POSIXLY_CORRECT will be 1 here if the shell was compiled
-DSTRICT_POSIX or if POSIXLY_CORRECT was supplied in the shell's
environment */
if (interactive_shell && posixly_correct && no_line_editing == 0)
rl_prefer_env_winsize = 1;
#endif /* READLINE && STRICT_POSIX */
/* Get the user's real and effective user ids. */
uidset ();
temp_var = set_if_not ("BASH_LOADABLES_PATH", DEFAULT_LOADABLE_BUILTINS_PATH);
temp_var = find_variable ("BASH_XTRACEFD");
if (temp_var && imported_p (temp_var))
sv_xtracefd (temp_var->name);
sv_shcompat ("BASH_COMPAT");
/* Allow FUNCNEST to be inherited from the environment. */
sv_funcnest ("FUNCNEST");
/* Initialize the dynamic variables, and seed their values. */
initialize_dynamic_variables ();
}
/* **************************************************************** */
/* */
/* Setting values for special shell variables */
/* */
/* **************************************************************** */
static void
set_machine_vars ()
{
SHELL_VAR *temp_var;
temp_var = set_if_not ("HOSTTYPE", HOSTTYPE);
temp_var = set_if_not ("OSTYPE", OSTYPE);
temp_var = set_if_not ("MACHTYPE", MACHTYPE);
temp_var = set_if_not ("HOSTNAME", current_host_name);
}
/* Set $HOME to the information in the password file if we didn't get
it from the environment. */
/* This function is not static so the tilde and readline libraries can
use it. */
char *
sh_get_home_dir ()
{
if (current_user.home_dir == 0)
get_current_user_info ();
return current_user.home_dir;
}
static void
set_home_var ()
{
SHELL_VAR *temp_var;
temp_var = find_variable ("HOME");
if (temp_var == 0)
temp_var = bind_variable ("HOME", sh_get_home_dir (), 0);
#if 0
VSETATTR (temp_var, att_exported);
#endif
}
/* Set $SHELL to the user's login shell if it is not already set. Call
get_current_user_info if we haven't already fetched the shell. */
static void
set_shell_var ()
{
SHELL_VAR *temp_var;
temp_var = find_variable ("SHELL");
if (temp_var == 0)
{
if (current_user.shell == 0)
get_current_user_info ();
temp_var = bind_variable ("SHELL", current_user.shell, 0);
}
#if 0
VSETATTR (temp_var, att_exported);
#endif
}
static char *
get_bash_name ()
{
char *name;
if ((login_shell == 1) && RELPATH(shell_name))
{
if (current_user.shell == 0)
get_current_user_info ();
name = savestring (current_user.shell);
}
else if (ABSPATH(shell_name))
name = savestring (shell_name);
else if (shell_name[0] == '.' && shell_name[1] == '/')
{
/* Fast path for common case. */
char *cdir;
int len;
cdir = get_string_value ("PWD");
if (cdir)
{
len = strlen (cdir);
name = (char *)xmalloc (len + strlen (shell_name) + 1);
strcpy (name, cdir);
strcpy (name + len, shell_name + 1);
}
else
name = savestring (shell_name);
}
else
{
char *tname;
int s;
tname = find_user_command (shell_name);
if (tname == 0)
{
/* Try the current directory. If there is not an executable
there, just punt and use the login shell. */
s = file_status (shell_name);
if (s & FS_EXECABLE)
{
tname = make_absolute (shell_name, get_string_value ("PWD"));
if (*shell_name == '.')
{
name = sh_canonpath (tname, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
if (name == 0)
name = tname;
else
free (tname);
}
else
name = tname;
}
else
{
if (current_user.shell == 0)
get_current_user_info ();
name = savestring (current_user.shell);
}
}
else
{
name = full_pathname (tname);
free (tname);
}
}
return (name);
}
void
adjust_shell_level (change)
int change;
{
char new_level[5], *old_SHLVL;
intmax_t old_level;
SHELL_VAR *temp_var;
old_SHLVL = get_string_value ("SHLVL");
if (old_SHLVL == 0 || *old_SHLVL == '\0' || legal_number (old_SHLVL, &old_level) == 0)
old_level = 0;
shell_level = old_level + change;
if (shell_level < 0)
shell_level = 0;
else if (shell_level >= 1000)
{
internal_warning (_("shell level (%d) too high, resetting to 1"), shell_level);
shell_level = 1;
}
/* We don't need the full generality of itos here. */
if (shell_level < 10)
{
new_level[0] = shell_level + '0';
new_level[1] = '\0';
}
else if (shell_level < 100)
{
new_level[0] = (shell_level / 10) + '0';
new_level[1] = (shell_level % 10) + '0';
new_level[2] = '\0';
}
else if (shell_level < 1000)
{
new_level[0] = (shell_level / 100) + '0';
old_level = shell_level % 100;
new_level[1] = (old_level / 10) + '0';
new_level[2] = (old_level % 10) + '0';
new_level[3] = '\0';
}
temp_var = bind_variable ("SHLVL", new_level, 0);
set_auto_export (temp_var);
}
static void
initialize_shell_level ()
{
adjust_shell_level (1);
}
/* If we got PWD from the environment, update our idea of the current
working directory. In any case, make sure that PWD exists before
checking it. It is possible for getcwd () to fail on shell startup,
and in that case, PWD would be undefined. If this is an interactive
login shell, see if $HOME is the current working directory, and if
that's not the same string as $PWD, set PWD=$HOME. */
void
set_pwd ()
{
SHELL_VAR *temp_var, *home_var;
char *temp_string, *home_string, *current_dir;
home_var = find_variable ("HOME");
home_string = home_var ? value_cell (home_var) : (char *)NULL;
temp_var = find_variable ("PWD");
/* Follow posix rules for importing PWD */
if (temp_var && imported_p (temp_var) &&
(temp_string = value_cell (temp_var)) &&
temp_string[0] == '/' &&
same_file (temp_string, ".", (struct stat *)NULL, (struct stat *)NULL))
{
current_dir = sh_canonpath (temp_string, PATH_CHECKDOTDOT|PATH_CHECKEXISTS);
if (current_dir == 0)
current_dir = get_working_directory ("shell_init");
else
set_working_directory (current_dir);
if (posixly_correct && current_dir)
{
temp_var = bind_variable ("PWD", current_dir, 0);
set_auto_export (temp_var);
}
free (current_dir);
}
else if (home_string && interactive_shell && login_shell &&
same_file (home_string, ".", (struct stat *)NULL, (struct stat *)NULL))
{
set_working_directory (home_string);
temp_var = bind_variable ("PWD", home_string, 0);
set_auto_export (temp_var);
}
else
{
temp_string = get_working_directory ("shell-init");
if (temp_string)
{
temp_var = bind_variable ("PWD", temp_string, 0);
set_auto_export (temp_var);
free (temp_string);
}
}
/* According to the Single Unix Specification, v2, $OLDPWD is an
`environment variable' and therefore should be auto-exported. If we
don't find OLDPWD in the environment, or it doesn't name a directory,
make a dummy invisible variable for OLDPWD, and mark it as exported. */
temp_var = find_variable ("OLDPWD");
#if defined (OLDPWD_CHECK_DIRECTORY)
if (temp_var == 0 || value_cell (temp_var) == 0 || file_isdir (value_cell (temp_var)) == 0)
#else
if (temp_var == 0 || value_cell (temp_var) == 0)
#endif
{
temp_var = bind_variable ("OLDPWD", (char *)NULL, 0);
VSETATTR (temp_var, (att_exported | att_invisible));
}
}
/* Make a variable $PPID, which holds the pid of the shell's parent. */
void
set_ppid ()
{
char namebuf[INT_STRLEN_BOUND(pid_t) + 1], *name;
SHELL_VAR *temp_var;
name = inttostr (getppid (), namebuf, sizeof(namebuf));
temp_var = find_variable ("PPID");
if (temp_var)
VUNSETATTR (temp_var, (att_readonly | att_exported));
temp_var = bind_variable ("PPID", name, 0);
VSETATTR (temp_var, (att_readonly | att_integer));
}
static void
uidset ()
{
char buff[INT_STRLEN_BOUND(uid_t) + 1], *b;
register SHELL_VAR *v;
b = inttostr (current_user.uid, buff, sizeof (buff));
v = find_variable ("UID");
if (v == 0)
{
v = bind_variable ("UID", b, 0);
VSETATTR (v, (att_readonly | att_integer));
}
if (current_user.euid != current_user.uid)