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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
|
/** \file env.c
Functions for setting and getting environment variables.
*/
#include "config.h"
#include <stdlib.h>
#include <wchar.h>
#include <string.h>
#include <stdio.h>
#include <locale.h>
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#if HAVE_NCURSES_H
#include <ncurses.h>
#else
#include <curses.h>
#endif
#if HAVE_TERMIO_H
#include <termio.h>
#endif
#if HAVE_TERM_H
#include <term.h>
#elif HAVE_NCURSES_TERM_H
#include <ncurses/term.h>
#endif
#include <errno.h>
#include "fallback.h"
#include "util.h"
#include "wutil.h"
#include "proc.h"
#include "common.h"
#include "env.h"
#include "sanity.h"
#include "expand.h"
#include "history.h"
#include "reader.h"
#include "parser.h"
#include "env_universal.h"
#include "input_common.h"
#include "event.h"
#include "translate.h"
#include "complete.h"
/**
Command used to start fishd
*/
#define FISHD_CMD L"if which fishd >/dev/null; fishd ^/tmp/fishd.%s.log; end"
/**
Value denoting a null string
*/
#define ENV_NULL L"\x1d"
/**
At init, we read all the environment variables from this array
*/
extern char **environ;
/**
Struct representing one level in the function variable stack
*/
typedef struct env_node
{
/**
Variable table
*/
hash_table_t env;
/**
Does this node imply a new variable scope? If yes, all
non-global variables below this one in the stack are
invisible. If new_scope is set for the global variable node,
the universe will explode.
*/
int new_scope;
/**
Does this node contain any variables which are exported to subshells
*/
int export;
/**
Pointer to next level
*/
struct env_node *next;
}
env_node_t;
/**
A variable entry. Stores the value of a variable and whether it
should be exported. Obviously, it needs to be allocated large
enough to fit the value string.
*/
typedef struct var_entry
{
int export; /**< Whether the variable should be exported */
wchar_t val[0]; /**< The value of the variable */
}
var_entry_t;
/**
Top node on the function stack
*/
static env_node_t *top=0;
/**
Bottom node on the function stack
*/
static env_node_t *global_env = 0;
/**
Table for global variables
*/
static hash_table_t *global;
/**
Table of variables that may not be set using the set command.
*/
static hash_table_t env_read_only;
/**
Table of variables whose value is dynamically calculated, such as umask, status, etc
*/
static hash_table_t env_electric;
/**
Exported variable array used by execv
*/
static char **export_arr=0;
/**
Buffer used for storing string contents for export_arr
*/
static buffer_t export_buffer;
/**
Flag for checking if we need to regenerate the exported variable
array
*/
static int has_changed = 1;
/**
This stringbuffer is used to store the value of dynamically
generated variables, such as history.
*/
static string_buffer_t dyn_var;
/**
Variable used by env_get_names to communicate auxiliary information
to add_key_to_hash
*/
static int get_names_show_exported;
/**
Variable used by env_get_names to communicate auxiliary information
to add_key_to_hash
*/
static int get_names_show_unexported;
/**
List of all locale variable names
*/
static const wchar_t *locale_variable[] =
{
L"LANG",
L"LC_ALL",
L"LC_COLLATE",
L"LC_CTYPE",
L"LC_MESSAGES",
L"LC_MONETARY",
L"LC_NUMERIC",
L"LC_TIME",
(void *)0
}
;
/**
Free hash key and hash value
*/
static void clear_hash_entry( const void *key, const void *data )
{
var_entry_t *entry = (var_entry_t *)data;
if( entry->export )
has_changed = 1;
free( (void *)key );
free( (void *)data );
}
/**
When fishd isn't started, this function is provided to
env_universal as a callback, it tries to start up fishd. It's
implementation is a bit of a hack, since it evaluates a bit of
shellscript, and it might be used at times when that might not be
the best idea.
*/
static void start_fishd()
{
string_buffer_t cmd;
struct passwd *pw;
sb_init( &cmd );
pw = getpwuid(getuid());
debug( 3, L"Spawning new copy of fishd" );
if( !pw )
{
debug( 0, _( L"Could not get user information" ) );
return;
}
sb_printf( &cmd, FISHD_CMD, pw->pw_name );
eval( (wchar_t *)cmd.buff,
0,
TOP );
sb_destroy( &cmd );
}
/**
Return the current umask value.
*/
static mode_t get_umask()
{
mode_t res;
res = umask( 0 );
umask( res );
return res;
}
/**
Checks if the specified variable is a locale variable
*/
static int is_locale( const wchar_t *key )
{
int i;
for( i=0; locale_variable[i]; i++ )
if( wcscmp(locale_variable[i], key ) == 0 )
return 1;
return 0;
}
/**
Properly sets all locale information
*/
static void handle_locale()
{
const wchar_t *lc_all = env_get( L"LC_ALL" );
const wchar_t *lang;
int i;
wchar_t *old = wcsdup(wsetlocale( LC_MESSAGES, (void *)0 ));
/*
Array of locale constants corresponding to the local variable names defined in locale_variable
*/
static const int cat[] =
{
0,
LC_ALL,
LC_COLLATE,
LC_CTYPE,
LC_MESSAGES,
LC_MONETARY,
LC_NUMERIC,
LC_TIME
}
;
if( lc_all )
{
wsetlocale( LC_ALL, lc_all );
}
else
{
lang = env_get( L"LANG" );
if( lang )
{
wsetlocale( LC_ALL, lang );
}
for( i=2; locale_variable[i]; i++ )
{
const wchar_t *val = env_get( locale_variable[i] );
if( val )
wsetlocale( cat[i], val );
}
}
if( wcscmp( wsetlocale( LC_MESSAGES, (void *)0 ), old ) != 0 )
{
/* Try to make change known to gettext. */
#ifdef HAVE__NL_MSG_CAT_CNTR
{
extern int _nl_msg_cat_cntr;
++_nl_msg_cat_cntr;
}
#elif HAVE_DCGETTEXT
dcgettext("fish","Changing language to English",LC_MESSAGES);
#endif
if( is_interactive )
{
complete_destroy();
complete_init();
debug( 0, _(L"Changing language to English") );
}
}
free( old );
}
/**
Universal variable callback function. This function makes sure the
proper events are triggered when an event occurs.
*/
static void universal_callback( int type,
const wchar_t *name,
const wchar_t *val )
{
wchar_t *str=0;
if( is_locale( name ) )
handle_locale();
switch( type )
{
case SET:
case SET_EXPORT:
str=L"SET";
break;
case ERASE:
str=L"ERASE";
break;
}
if( str )
{
event_t ev;
has_changed=1;
ev.type=EVENT_VARIABLE;
ev.param1.variable=name;
ev.function_name=0;
al_init( &ev.arguments );
al_push( &ev.arguments, L"VARIABLE" );
al_push( &ev.arguments, str );
al_push( &ev.arguments, name );
event_fire( &ev );
al_destroy( &ev.arguments );
}
}
/**
Make sure the PATH variable contains the essaential directories
*/
static void setup_path()
{
wchar_t *path;
int i, j;
array_list_t l;
const wchar_t *path_el[] =
{
L"/bin",
L"/usr/bin",
PREFIX L"/bin",
0
}
;
path = env_get( L"PATH" );
if( !path )
{
env_set( L"PATH", 0, ENV_EXPORT | ENV_GLOBAL );
path=0;
}
al_init( &l );
if( path )
expand_variable_array( path, &l );
for( j=0; path_el[j]; j++ )
{
int has_el=0;
for( i=0; i<al_get_count( &l); i++ )
{
wchar_t * el = (wchar_t *)al_get( &l, i );
size_t len = wcslen( el );
while( (len > 0) && (el[len-1]==L'/') )
len--;
if( (wcslen( path_el[j] ) == len) && (wcsncmp( el, path_el[j], len)==0) )
{
has_el = 1;
}
}
if( !has_el )
{
string_buffer_t b;
debug( 3, L"directory %ls was missing", path_el[j] );
sb_init( &b );
if( path )
{
sb_append( &b, path );
}
sb_append2( &b,
ARRAY_SEP_STR,
path_el[j],
(void *)0 );
env_set( L"PATH", (wchar_t *)b.buff, ENV_GLOBAL | ENV_EXPORT );
sb_destroy( &b );
al_foreach( &l, (void (*)(const void *))&free );
path = env_get( L"PATH" );
al_truncate( &l, 0 );
expand_variable_array( path, &l );
}
}
al_foreach( &l, (void (*)(const void *))&free );
al_destroy( &l );
}
void env_init()
{
char **p;
struct passwd *pw;
wchar_t *uname;
sb_init( &dyn_var );
b_init( &export_buffer );
/*
These variables can not be altered directly by the user
*/
hash_init( &env_read_only, &hash_wcs_func, &hash_wcs_cmp );
hash_put( &env_read_only, L"status", L"" );
hash_put( &env_read_only, L"history", L"" );
hash_put( &env_read_only, L"_", L"" );
hash_put( &env_read_only, L"LINES", L"" );
hash_put( &env_read_only, L"COLUMNS", L"" );
hash_put( &env_read_only, L"PWD", L"" );
/*
Names of all dynamically calculated variables
*/
hash_init( &env_electric, &hash_wcs_func, &hash_wcs_cmp );
hash_put( &env_electric, L"history", L"" );
hash_put( &env_electric, L"status", L"" );
hash_put( &env_electric, L"umask", L"" );
/*
HOME and USER should be writeable by root, since this can be a
convenient way to install software.
*/
if( getuid() != 0 )
{
hash_put( &env_read_only, L"HOME", L"" );
hash_put( &env_read_only, L"USER", L"" );
}
top = malloc( sizeof(env_node_t) );
top->next = 0;
top->new_scope = 0;
top->export=0;
hash_init( &top->env, &hash_wcs_func, &hash_wcs_cmp );
global_env = top;
global = &top->env;
/*
Now the environemnt variable handling is set up, the next step
is to insert valid data
*/
/*
Import environment variables
*/
for( p=environ; *p; p++ )
{
wchar_t *key, *val;
wchar_t *pos;
key = str2wcs(*p);
if( !key )
continue;
val = wcschr( key, L'=' );
if( val == 0 )
env_set( key, L"", ENV_EXPORT );
else
{
*val = L'\0';
val++;
pos=val;
//fwprintf( stderr, L"Set $%ls to %ls\n", key, val );
while( *pos )
{
if( *pos == L':' )
*pos = ARRAY_SEP;
pos++;
}
env_set( key, val, ENV_EXPORT | ENV_GLOBAL );
}
free(key);
}
/*
Set up the PATH variable
*/
setup_path();
/*
Set up the USER variable
*/
pw = getpwuid( getuid() );
if( pw )
{
uname = str2wcs( pw->pw_name );
env_set( L"USER", uname, ENV_GLOBAL | ENV_EXPORT );
free( uname );
}
env_universal_init( env_get( L"FISHD_SOCKET_DIR"),
env_get( L"USER" ),
&start_fishd,
&universal_callback );
}
void env_destroy()
{
env_universal_destroy();
sb_destroy( &dyn_var );
b_destroy( &export_buffer );
while( &top->env != global )
env_pop();
hash_destroy( &env_read_only );
hash_destroy( &env_electric );
hash_foreach( global, &clear_hash_entry );
hash_destroy( global );
free( top );
free( export_arr );
}
/**
Find the scope hashtable containing the variable with the specified
key
*/
static env_node_t *env_get_node( const wchar_t *key )
{
var_entry_t* res;
env_node_t *env = top;
while( env != 0 )
{
res = (var_entry_t *) hash_get( &env->env,
key );
if( res != 0 )
{
return env;
}
if( env->new_scope )
env = global_env;
else
env = env->next;
}
return 0;
}
void env_set( const wchar_t *key,
const wchar_t *val,
int var_mode )
{
int free_val = 0;
var_entry_t *entry;
env_node_t *node;
int has_changed_old = has_changed;
int has_changed_new = 0;
var_entry_t *e=0;
int done=0;
event_t ev;
int is_universal = 0;
if( (var_mode & ENV_USER ) &&
hash_get( &env_read_only, key ) )
{
return;
}
if( wcscmp( key, L"umask" ) == 0)
{
wchar_t *end;
int mask;
/*
Set the new umask
*/
if( val && wcslen(val) )
{
errno=0;
mask = wcstol( val, &end, 8 );
if( !errno && (!*end) && (mask <= 0777) && (mask >= 0) )
{
umask( mask );
}
}
/*
Do not actually create a umask variable, on env_get, it will be calculated dynamically
*/
return;
}
/*
Zero element arrays are internaly not coded as null but as this placeholder string
*/
if( !val )
{
val = ENV_NULL;
}
if( var_mode & ENV_UNIVERSAL )
{
int export = 0;
if( !(var_mode & ENV_EXPORT ) &&
!(var_mode & ENV_UNEXPORT ) )
{
env_universal_get_export( key );
}
else
export = (var_mode & ENV_EXPORT );
env_universal_set( key, val, export );
is_universal = 1;
}
else
{
if( val == 0 )
{
wchar_t *prev_val;
free_val = 1;
prev_val = env_get( key );
val = wcsdup( prev_val?prev_val:L"" );
}
node = env_get_node( key );
if( node && &node->env != 0 )
{
e = (var_entry_t *) hash_get( &node->env,
key );
if( e->export )
has_changed_new = 1;
}
if( (var_mode & ENV_LOCAL) ||
(var_mode & ENV_GLOBAL) )
{
node = ( var_mode & ENV_GLOBAL )?global_env:top;
}
else
{
if( node )
{
if( !(var_mode & ENV_EXPORT ) &&
!(var_mode & ENV_UNEXPORT ) )
{
var_mode = e->export?ENV_EXPORT:0;
}
}
else
{
if( !proc_had_barrier)
{
proc_had_barrier=1;
env_universal_barrier();
}
if( env_universal_get( key ) )
{
int export = 0;
if( !(var_mode & ENV_EXPORT ) &&
!(var_mode & ENV_UNEXPORT ) )
{
env_universal_get_export( key );
}
else
export = (var_mode & ENV_EXPORT );
env_universal_set( key, val, export );
is_universal = 1;
done = 1;
}
else
{
/*
New variable with unspecified scope. The default
scope is the innermost scope that is shadowing
*/
node = top;
while( node->next && !node->new_scope )
node = node->next;
}
}
}
if( !done )
{
void *k, *v;
hash_remove( &node->env, key, (const void **)&k, (const void **)&v );
free( k );
free( v );
entry = malloc( sizeof( var_entry_t ) +
sizeof(wchar_t )*(wcslen(val)+1));
if( var_mode & ENV_EXPORT)
{
entry->export = 1;
has_changed_new = 1;
}
else
entry->export = 0;
wcscpy( entry->val, val );
hash_put( &node->env, wcsdup(key), entry );
if( entry->export )
{
node->export=1;
}
if( free_val )
free((void *)val);
has_changed = has_changed_old || has_changed_new;
}
}
if( !is_universal )
{
ev.type=EVENT_VARIABLE;
ev.param1.variable = key;
ev.function_name = 0;
al_init( &ev.arguments );
al_push( &ev.arguments, L"VARIABLE" );
al_push( &ev.arguments, key );
// debug( 1, L"env_set: fire events on variable %ls", key );
event_fire( &ev );
// debug( 1, L"env_set: return from event firing" );
al_destroy( &ev.arguments );
}
if( is_locale( key ) )
{
handle_locale();
}
}
/**
Attempt to remove/free the specified key/value pair from the
specified hash table.
*/
static int try_remove( env_node_t *n,
const wchar_t *key )
{
const void *old_key_void, *old_val_void;
wchar_t *old_key, *old_val;
if( n == 0 )
return 0;
hash_remove( &n->env,
key,
&old_key_void,
&old_val_void );
old_key = (wchar_t *)old_key_void;
old_val = (wchar_t *)old_val_void;
if( old_key != 0 )
{
var_entry_t * v = (var_entry_t *)old_val;
if( v->export )
{
has_changed = 1;
}
free(old_key);
free(old_val);
return 1;
}
if( n->new_scope )
return try_remove( global_env, key );
else
return try_remove( n->next, key );
}
void env_remove( const wchar_t *key, int var_mode )
{
if( (var_mode & ENV_USER ) &&
hash_get( &env_read_only, key ) )
{
return;
}
if( !try_remove( top, key ) )
{
env_universal_remove( key );
}
if( is_locale( key ) )
handle_locale();
}
wchar_t *env_get( const wchar_t *key )
{
var_entry_t *res;
env_node_t *env = top;
wchar_t *item;
if( wcscmp( key, L"history" ) == 0 )
{
wchar_t *current;
int i;
int add_current=0;
sb_clear( &dyn_var );
current = reader_get_buffer();
if( current && wcslen( current ) )
{
add_current=1;
sb_append( &dyn_var, current );
}
for( i=add_current; i<8; i++ )
{
wchar_t *next = history_get( i-add_current );
if( !next )
{
/*
This is not an error - it simply means the user has
a short history
*/
break;
}
if( i!=0)
sb_append( &dyn_var, ARRAY_SEP_STR );
sb_append( &dyn_var, next );
}
return (wchar_t *)dyn_var.buff;
}
else if( wcscmp( key, L"COLUMNS" )==0 )
{
sb_clear( &dyn_var );
sb_printf( &dyn_var, L"%d", common_get_width() );
return (wchar_t *)dyn_var.buff;
}
else if( wcscmp( key, L"LINES" )==0 )
{
sb_clear( &dyn_var );
sb_printf( &dyn_var, L"%d", common_get_height() );
return (wchar_t *)dyn_var.buff;
}
else if( wcscmp( key, L"status" )==0 )
{
sb_clear( &dyn_var );
sb_printf( &dyn_var, L"%d", proc_get_last_status() );
return (wchar_t *)dyn_var.buff;
}
else if( wcscmp( key, L"umask" )==0 )
{
sb_clear( &dyn_var );
sb_printf( &dyn_var, L"0%0.3o", get_umask() );
return (wchar_t *)dyn_var.buff;
}
while( env != 0 )
{
res = (var_entry_t *) hash_get( &env->env,
key );
if( res != 0 )
{
if( wcscmp( res->val, ENV_NULL )==0)
{
return 0;
}
else
return res->val;
}
if( env->new_scope )
env = global_env;
else
env = env->next;
}
if( !proc_had_barrier)
{
proc_had_barrier=1;
env_universal_barrier();
}
item = env_universal_get( key );
if( !item || (wcscmp( item, ENV_NULL )==0))
{
return 0;
}
else
return item;
}
int env_exist( const wchar_t *key )
{
var_entry_t *res;
env_node_t *env = top;
wchar_t *item;
if( hash_get( &env_read_only, key ) || hash_get( &env_electric, key ) )
{
return 1;
}
while( env != 0 )
{
res = (var_entry_t *) hash_get( &env->env,
key );
if( res != 0 )
{
return 1;
}
if( env->new_scope )
env = global_env;
else
env = env->next;
}
if( !proc_had_barrier)
{
proc_had_barrier=1;
env_universal_barrier();
}
item = env_universal_get( key );
return item != 0;
}
/**
Returns true if the specified scope or any non-shadowed non-global subscopes contain an exported variable.
*/
static int local_scope_exports( env_node_t *n )
{
if( n==global_env )
return 0;
if( n->export )
return 1;
if( n->new_scope )
return 0;
return local_scope_exports( n->next );
}
void env_push( int new_scope )
{
env_node_t *node = malloc( sizeof(env_node_t) );
node->next = top;
node->export=0;
hash_init( &node->env, &hash_wcs_func, &hash_wcs_cmp );
node->new_scope=new_scope;
if( new_scope )
{
has_changed |= local_scope_exports(top);
}
top = node;
}
void env_pop()
{
if( &top->env != global )
{
int i;
int locale_changed = 0;
env_node_t *killme = top;
for( i=0; locale_variable[i]; i++ )
{
if( hash_get( &killme->env, locale_variable[i] ) )
{
locale_changed = 1;
break;
}
}
if( killme->new_scope )
{
has_changed |= killme->export || local_scope_exports( killme->next );
}
top = top->next;
hash_foreach( &killme->env, &clear_hash_entry );
hash_destroy( &killme->env );
free( killme );
if( locale_changed )
handle_locale();
}
else
{
debug( 0,
_( L"Tried to pop empty environment stack." ) );
sanity_lose();
}
}
/**
Function used with hash_foreach to insert keys of one table into
another
*/
static void add_key_to_hash( const void *key,
const void *data,
void *aux )
{
var_entry_t *e = (var_entry_t *)data;
if( ( e->export && get_names_show_exported) ||
( !e->export && get_names_show_unexported) )
{
hash_put( (hash_table_t *)aux, key, 0 );
}
}
/**
Add key to hashtable
*/
static void add_to_hash( const void *k, void *aux )
{
hash_put( (hash_table_t *)aux,
k,
0 );
}
/**
Add key to list
*/
static void add_key_to_list( const void * key,
const void * val,
void *aux )
{
al_push( (array_list_t *)aux, key );
}
void env_get_names( array_list_t *l, int flags )
{
int show_local = flags & ENV_LOCAL;
int show_global = flags & ENV_GLOBAL;
int show_universal = flags & ENV_UNIVERSAL;
hash_table_t names;
env_node_t *n=top;
get_names_show_exported =
flags & ENV_EXPORT|| (!(flags & ENV_UNEXPORT));
get_names_show_unexported =
flags & ENV_UNEXPORT|| (!(flags & ENV_EXPORT));
if( !show_local && !show_global && !show_universal )
{
show_local =show_universal = show_global=1;
}
hash_init( &names, &hash_wcs_func, &hash_wcs_cmp );
if( show_local )
{
while( n )
{
if( n == global_env )
break;
hash_foreach2( &n->env,
add_key_to_hash,
&names );
if( n->new_scope )
break;
else
n = n->next;
}
}
if( show_global )
{
hash_foreach2( &global_env->env,
add_key_to_hash,
&names );
if( get_names_show_unexported )
hash_foreach2( &env_electric, &add_key_to_list, l );
if( get_names_show_exported )
{
al_push( l, L"COLUMNS" );
al_push( l, L"LINES" );
}
}
if( show_universal )
{
array_list_t uni_list;
al_init( &uni_list );
env_universal_get_names( &uni_list,
get_names_show_exported,
get_names_show_unexported );
al_foreach2( &uni_list, &add_to_hash, &names );
al_destroy( &uni_list );
}
hash_get_keys( &names, l );
hash_destroy( &names );
}
/**
Function used by env_export_arr to iterate over hashtable of variables
*/
static void export_func1( const void *k, const void *v, void *aux )
{
var_entry_t *val_entry = (var_entry_t *)v;
if( val_entry->export )
{
hash_table_t *h = (hash_table_t *)aux;
if( !hash_get( h, k ) )
hash_put( h, k, val_entry->val );
}
}
/**
Function used by env_export_arr to iterate over hashtable of variables
*/
static void export_func2( const void *k, const void *v, void *aux )
{
wchar_t *key = (wchar_t *)k;
wchar_t *val = (wchar_t *)v;
char *ks = wcs2str( key );
char *vs = wcs2str( val );
char *pos = vs;
buffer_t *out = (buffer_t *)aux;
if( !ks || !vs )
{
die_mem();
}
/*
Make arrays into colon-separated lists
*/
while( *pos )
{
if( *pos == ARRAY_SEP )
*pos = ':';
pos++;
}
int nil = 0;
b_append( out, ks, strlen(ks) );
b_append( out, "=", 1 );
b_append( out, vs, strlen(vs) );
b_append( out, &nil, 1 );
free( ks );
free( vs );
}
char **env_export_arr( int recalc)
{
if( recalc && !proc_had_barrier)
{
proc_had_barrier=1;
env_universal_barrier();
}
if( has_changed )
{
array_list_t uni;
hash_table_t vals;
env_node_t *n=top;
int prev_was_null=1;
int pos=0;
int i;
debug( 4, L"env_export_arr() recalc" );
hash_init( &vals, &hash_wcs_func, &hash_wcs_cmp );
while( n )
{
hash_foreach2( &n->env, &export_func1, &vals );
if( n->new_scope )
n = global_env;
else
n = n->next;
}
al_init( &uni );
env_universal_get_names( &uni, 1, 0 );
for( i=0; i<al_get_count( &uni ); i++ )
{
wchar_t *key = (wchar_t *)al_get( &uni, i );
wchar_t *val = env_universal_get( key );
if( !hash_get( &vals, key ) )
hash_put( &vals, key, val );
}
al_destroy( &uni );
export_buffer.used=0;
hash_foreach2( &vals, &export_func2, &export_buffer );
hash_destroy( &vals );
export_arr = realloc( export_arr,
sizeof(char *)*(hash_get_count( &vals) + 1) );
for( i=0; i<export_buffer.used; i++ )
{
if( prev_was_null )
{
export_arr[pos++]= &export_buffer.buff[i];
debug( 3, L"%s", &export_buffer.buff[i]);
}
prev_was_null = (export_buffer.buff[i]==0);
}
export_arr[pos]=0;
has_changed=0;
}
return export_arr;
}
|