-
Notifications
You must be signed in to change notification settings - Fork 0
/
utility.c
3406 lines (2968 loc) · 100 KB
/
utility.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
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: utility.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, jlf@behere.com **
** Jens Hamisch, jens@Strawberry.COM **
** **
** Description: General routines that are called throughout Modules **
** which are not necessarily specific to any single **
** block of functionality. **
** **
** Exports: store_hash_value **
** clear_hash_value **
** Delete_Global_Hash_Tables **
** Delete_Hash_Tables **
** Copy_Hash_Tables **
** Unwind_Modulefile_Changes **
** Output_Modulefile_Changes **
** IsLoaded_ExactMatch **
** IsLoaded **
** chk_marked_entry **
** set_marked_entry **
** Update_LoadedList **
** check_magic **
** regex_quote **
** xstrtok **
** xstrtok_r **
** chk4spch **
** module_malloc **
** xdup **
** xgetenv **
** stringer **
** null_free **
** countTclHash **
** **
** strdup if not defined by the system libs. **
** **
** Notes: **
** **
** ************************************************************************ **
****/
/** ** Copyright *********************************************************** **
** **
** Copyright 1991-1994 by John L. Furlan. **
** see LICENSE.GPL, which must be provided, for details **
** **
** ************************************************************************ **/
static char Id[] = "@(#)$Id: 71346743bc4fa2cec67de59999a645f374273ef4 $";
static void *UseId[] = { &UseId, Id };
/** ************************************************************************ **/
/** HEADERS **/
/** ************************************************************************ **/
#include "modules_def.h"
/** ************************************************************************ **/
/** LOCAL DATATYPES **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** CONSTANTS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** MACROS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** LOCAL DATA **/
/** ************************************************************************ **/
static char module_name[] = "utility.c"; /** File name of this module **/
#if WITH_DEBUGGING_UTIL_2
static char _proc_store_hash_value[] = "store_hash_value";
static char _proc_clear_hash_value[] = "clear_hash_value";
static char _proc_Clear_Global_Hash_Tables[] = "Clear_Global_Hash_Tables";
static char _proc_Delete_Global_Hash_Tables[] = "Delete_Global_Hash_Tables";
static char _proc_Delete_Hash_Tables[] = "Delete_Hash_Tables";
static char _proc_Copy_Hash_Tables[] = "Copy_Hash_Tables";
static char _proc_Unwind_Modulefile_Changes[] = "Unwind_Modulefile_Changes";
static char _proc_Output_Modulefile_Changes[] = "Output_Modulefile_Changes";
static char _proc_Output_Modulefile_Aliases[] = "Output_Modulefile_Aliases";
static char _proc_Output_Directory_Change[] = "Output_Directory_Change";
static char _proc_output_set_variable[] = "output_set_variable";
static char _proc_output_unset_variable[] = "output_unset_variable";
static char _proc_output_function[] = "output_function";
static char _proc_output_set_alias[] = "output_set_alias";
static char _proc_output_unset_alias[] = "output_unset_alias";
static char _proc_getLMFILES[] = "getLMFILES";
static char _proc___IsLoaded[] = "__IsLoaded";
static char _proc_chk_marked_entry[] = "chk_marked_entry";
static char _proc_set_marked_entry[] = "set_marked_entry";
static char _proc_get_module_basename[] = "get_module_basename";
static char _proc_Update_LoadedList[] = "Update_LoadedList";
static char _proc_check_magic[] = "check_magic";
static char _proc_cleanse_path[] = "cleanse_path";
static char _proc_chop[] = "chop";
static char _proc_stringer[] = "stringer";
#endif /* WITH_DEBUGGING_UTIL_2 */
static FILE *aliasfile; /** Temporary file to write aliases **/
static char *aliasfilename; /** Temporary file name **/
static char alias_separator = ';'; /** Alias command separator **/
static const int eval_alias = /** EVAL_ALIAS macro **/
#ifdef EVAL_ALIAS
1
#else
0
#endif /* EVAL_ALIAS */
;
static const int bourne_funcs = /** HAS_BOURNE_FUNCS macro **/
#ifdef HAS_BOURNE_FUNCS
1
#else
0
#endif /* HAS_BOURNE_FUNCS */
;
static const int bourne_alias = /** HAS_BOURNE_FUNCS macro **/
#ifdef HAS_BOURNE_ALIAS
1
#else
0
#endif /* HAS_BOURNE_ALIAS */
;
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
static void Clear_Global_Hash_Tables(void);
static int Output_Modulefile_Aliases(Tcl_Interp *interp);
static int Output_Directory_Change(Tcl_Interp *interp);
static int output_set_variable(Tcl_Interp *interp, const char*, const char*);
static int output_unset_variable(const char* var);
static void output_function(const char*, const char*);
static int output_set_alias(const char*, const char*);
static int output_unset_alias(const char*, const char*);
static int __IsLoaded(Tcl_Interp*, char*, char**, char*, int);
static char *get_module_basename(char*);
static char *chop(const char*);
static void EscapeCshString(const char* in, char* out);
static void EscapeShString(const char* in, char* out);
static void EscapePerlString(const char* in, char* out);
static void EscapeCmakeString(const char* in, char* out);
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: store_hash_value **
** **
** Description: Keeps the old value of the variable around if it is **
** touched in the modulefile to enable undoing a **
** modulefile by resetting the evironment to it started.**
** **
** This is the same for unset_shell_variable() **
** **
** First Edition: 1992/10/14 **
** **
** Parameters: Tcl_HashTable *htable Hash table to be used**
** const char *key Attached key **
** const char *value Alias value **
** **
** Result: int TCL_OK Successful completion **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
int store_hash_value(Tcl_HashTable* htable, const char* key, const char* value)
{
int new; /** Return from Tcl_CreateHashEntry **/
/** which indicates creation or ref- **/
/** ference to an existing entry **/
char *tmp; /** Temp pointer used for disalloc. **/
Tcl_HashEntry *hentry; /** Hash entry reference **/
#if WITH_DEBUGGING_UTIL_2
ErrorLogger(NO_ERR_START, LOC, _proc_store_hash_value, NULL);
#endif /* WITH_DEBUGGING_UTIL_2 */
/**
** Create a hash entry for the key to be stored. If there exists one
** so far, its value has to be unlinked.
** All values in this hash are pointers to allocated memory areas.
**/
hentry = Tcl_CreateHashEntry(htable, (char*) key, &new);
if (!new) {
tmp = (char *)Tcl_GetHashValue(hentry);
if (tmp) {
null_free((void *)&tmp);
}
}
/**
** Set up the new value. strdup allocates!
**/
if (value) {
Tcl_SetHashValue(hentry, (char*)stringer(NULL, 0, (char *)value, NULL));
} else {
Tcl_SetHashValue(hentry, (char*)NULL);
}
return (TCL_OK);
} /** End of 'store_hash_value' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: clear_hash_value **
** **
** Description: Remove the specified shell variable from the passed **
** hash table **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_HashTable *htable Hash table to be used**
** const char *key Attached key **
** **
** Result: int TCL_OK Successful completion **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
int clear_hash_value( Tcl_HashTable *htable,
const char *key)
{
char *tmp; /** Temp pointer used for dealloc. **/
Tcl_HashEntry *hentry; /** Hash entry reference **/
#if WITH_DEBUGGING_UTIL_2
ErrorLogger( NO_ERR_START, LOC, _proc_clear_hash_value, NULL);
#endif
/**
** If I haven't already created an entry for keeping this environment
** variable's value, then just leave.
** Otherwise, remove this entry from the hash table.
**/
if( hentry = Tcl_FindHashEntry( htable, (char*) key) ) {
tmp = (char*) Tcl_GetHashValue( hentry);
if( tmp)
null_free((void *) &tmp);
Tcl_DeleteHashEntry( hentry);
}
return( TCL_OK);
} /** End of 'clear_hash_value' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Clear_Global_Hash_Tables **
** **
** Description: Deletes and reinitializes our env. hash tables. **
** **
** First Edition: 1992/10/14 **
** **
** Parameters: - **
** Result: - **
** **
** Attached Globals: setenvHashTable, **
** unsetenvHashTable, **
** aliasSetHashTable, **
** aliasUnsetHashTable **
** **
** ************************************************************************ **
++++*/
static void Clear_Global_Hash_Tables( void)
{
Tcl_HashSearch searchPtr; /** Tcl hash search handle **/
Tcl_HashEntry *hashEntry; /** Result from Tcl hash search **/
char *val = NULL; /** Stored value (is a pointer!) **/
/**
** The following hash tables are to be initialized
**/
Tcl_HashTable *table[5],
**table_ptr = table;
table[0] = setenvHashTable;
table[1] = unsetenvHashTable;
table[2] = aliasSetHashTable;
table[3] = aliasUnsetHashTable;
table[4] = NULL;
#if WITH_DEBUGGING_UTIL_2
ErrorLogger( NO_ERR_START, LOC, _proc_Clear_Global_Hash_Tables, NULL);
#endif
/**
** Loop for all the hash tables named above. If there's no value stored
** in a hash table, skip to the next one.
**/
for( ; *table_ptr; table_ptr++) {
if( ( hashEntry = Tcl_FirstHashEntry( *table_ptr, &searchPtr)) == NULL)
continue;
/**
** Otherwise remove all values stored in the table
**/
do {
val = (char*) Tcl_GetHashValue( hashEntry);
if( val)
null_free((void *) &val);
} while( hashEntry = Tcl_NextHashEntry( &searchPtr));
/**
** Reinitialize the hash table by unlocking it from memory and
** thereafter initializing it again.
**/
Tcl_DeleteHashTable( *table_ptr);
Tcl_InitHashTable( *table_ptr, TCL_STRING_KEYS);
} /** for **/
} /** End of 'Clear_Global_Hash_Tables' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Delete_Global_Hash_Tables **
** Delete_Hash_Tables **
** **
** Description: Deletes our environment hash tables. **
** **
** First Edition: 1992/10/14 **
** **
** Parameters: Tcl_HashTable **table_ptr NULL-Terminated list **
** of hash tables to be **
** deleted **
** Result: - **
** **
** Attached Globals: setenvHashTable, **
** unsetenvHashTable, **
** aliasSetHashTable, **
** aliasUnsetHashTable **
** **
** ************************************************************************ **
++++*/
void Delete_Global_Hash_Tables( void) {
/**
** The following hash tables are to be initialized
**/
Tcl_HashTable *table[5];
table[0] = setenvHashTable;
table[1] = unsetenvHashTable;
table[2] = aliasSetHashTable;
table[3] = aliasUnsetHashTable;
table[4] = NULL;
#if WITH_DEBUGGING_UTIL_2
ErrorLogger( NO_ERR_START, LOC, _proc_Delete_Global_Hash_Tables, NULL);
#endif
Delete_Hash_Tables( table);
} /** End of 'Delete_Global_Hash_Tables' **/
void Delete_Hash_Tables( Tcl_HashTable **table_ptr)
{
Tcl_HashSearch searchPtr; /** Tcl hash search handle **/
Tcl_HashEntry *hashEntry; /** Result from Tcl hash search **/
char *val = NULL; /** Stored value (is a pointer!) **/
#if WITH_DEBUGGING_UTIL_2
ErrorLogger( NO_ERR_START, LOC, _proc_Delete_Hash_Tables, NULL);
#endif
/**
** Loop for all the hash tables named above. Remove all values stored in
** the table and then free up the whole table
**/
for( ; *table_ptr; table_ptr++) {
if( ( hashEntry = Tcl_FirstHashEntry( *table_ptr, &searchPtr))) {
/**
** Remove all values stored in the table
**/
do {
val = (char*) Tcl_GetHashValue( hashEntry);
if( val)
null_free((void *) &val);
} while( hashEntry = Tcl_NextHashEntry( &searchPtr));
/**
** Remove internal hash control structures
**/
Tcl_DeleteHashTable( *table_ptr);
}
null_free((void *) table_ptr);
} /** for **/
#if WITH_DEBUGGING_UTIL_2
ErrorLogger( NO_ERR_END, LOC, _proc_Delete_Hash_Tables, NULL);
#endif
} /** End of 'Delete_Hash_Tables' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Copy_Hash_Tables **
** **
** Description: Allocate new hash tables for the global environment, **
** initialize them and copy the contents of the current **
** tables into them. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: - **
** Result: Tcl_HashTable** Pointer to the new list of **
** hash tables **
** Attached Globals: setenvHashTable, **
** unsetenvHashTable, **
** aliasSetHashTable, **
** aliasUnsetHashTable **
** **
** ************************************************************************ **
++++*/
Tcl_HashTable **Copy_Hash_Tables(void)
{
Tcl_HashSearch searchPtr; /** Tcl hash search handle **/
Tcl_HashEntry *oldHashEntry, /** Hash entries to be copied **/
*newHashEntry;
char *val = NULL, /** Stored value (is a pointer!) **/
*key = NULL; /** Hash key **/
int new; /** Tcl inidicator, if the new hash entry has been
** created or ref. **/
Tcl_HashTable *oldTable[5],
**o_ptr, **n_ptr,
**newTable; /** Destination hash table **/
oldTable[0] = setenvHashTable;
oldTable[1] = unsetenvHashTable;
oldTable[2] = aliasSetHashTable;
oldTable[3] = aliasUnsetHashTable;
oldTable[4] = NULL;
#if WITH_DEBUGGING_UTIL_2
ErrorLogger(NO_ERR_START, LOC, _proc_Copy_Hash_Tables, NULL);
#endif /* WITH_DEBUGGING_UTIL_2 */
/**
** Allocate storage for the new list of hash tables
**/
if (!(newTable = (Tcl_HashTable**)module_malloc(sizeof(oldTable)))) {
if (OK != ErrorLogger(ERR_ALLOC, LOC, NULL)) {
goto unwind0;
}
}
/**
** Now copy each hashtable out of the list
**/
for ((o_ptr = oldTable), (n_ptr = newTable); *o_ptr; o_ptr++, n_ptr++) {
/**
** Allocate memory for a single hash table:
**/
if (!(*n_ptr = (Tcl_HashTable*)module_malloc(sizeof(Tcl_HashTable)))) {
if (OK != ErrorLogger(ERR_ALLOC, LOC, NULL)) {
goto unwind1;
}
}
/**
** Initialize that guy and copy it from the old table:
**/
Tcl_InitHashTable(*n_ptr, TCL_STRING_KEYS);
/* assuming that the first '=' is correct, so that oldHashEntry will
* be initialized, and that the second '=' was supposed to be an '==',
* so that the value assigned to 'oldHashEntry' here will be read from
* it: */
if ((oldHashEntry = Tcl_FirstHashEntry(*o_ptr, &searchPtr))) {
/**
** Copy all entries if there are any:
**/
do {
key = (char*)Tcl_GetHashKey(*o_ptr, oldHashEntry);
val = (char*)Tcl_GetHashValue(oldHashEntry);
if (*n_ptr != NULL) {
#ifdef USE_TCLDEFS
newHashEntry = Tcl_CreateHashEntry(*n_ptr, key, &new);
#else
newHashEntry = (*((*n_ptr)->createProc))(*n_ptr, key, &new);
#endif /* USE_TCLDEFS */
} else if (*o_ptr != NULL) {
/* try old one as a backup: */
#ifdef USE_TCLDEFS
newHashEntry = Tcl_CreateHashEntry(*o_ptr, key, &new);
#else
newHashEntry = (*((*o_ptr)->createProc))(*o_ptr, key, &new);
#endif /* USE_TCLDEFS */
} else {
/* *n_ptr and *o_ptr were both null, not sure what to do: */
if (oldHashEntry != NULL) {
newHashEntry = oldHashEntry;
} else {
/* really not sure what to do now: */
;
}
}
if (val) {
Tcl_SetHashValue(newHashEntry,
stringer(NULL, 0, val, NULL));
} else {
Tcl_SetHashValue(newHashEntry, (char *)NULL);
}
} while (oldHashEntry == Tcl_NextHashEntry(&searchPtr));
} /** end if **/
} /** end for-loop **/
/**
** Put a terminator at the end of the new table
**/
*n_ptr = NULL;
#if WITH_DEBUGGING_UTIL_2
ErrorLogger(NO_ERR_END, LOC, _proc_Copy_Hash_Tables, NULL);
#endif /* WITH_DEBUGGING_UTIL_2 */
return (newTable);
unwind1:
null_free((void *)&newTable);
unwind0:
return (NULL); /** -------- EXIT (FAILURE) -------> **/
} /** End of 'Copy_Hash_Tables' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: **
** **
** Description: Once a the loading or unloading of a modulefile **
** fails, any changes it has made to the environment **
** must be undone and reset to its previous state. This **
** function is responsible for unwinding any changes a **
** modulefile has made. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp According TCL interp.**
** Tcl_HashTable **oldTables Hash tables storing **
** the former environm. **
** Result: **
** Attached Globals: **
** **
** ************************************************************************ **
++++*/
int Unwind_Modulefile_Changes( Tcl_Interp *interp,
Tcl_HashTable **oldTables )
{
Tcl_HashSearch searchPtr; /** Tcl hash search handle **/
Tcl_HashEntry *hashEntry; /** Result from Tcl hash search **/
char *val = NULL, /** Stored value (is a pointer!) **/
*key; /** Tcl hash key **/
int i; /** Loop counter **/
#if WITH_DEBUGGING_UTIL_2
ErrorLogger( NO_ERR_START, LOC, _proc_Unwind_Modulefile_Changes, NULL);
#endif
if( oldTables) {
/**
** Use only entries 0 and 1 which do contain all changes to the
** shell varibles (setenv and unsetenv)
**/
/** ??? What about the aliases (table 2 and 3) ??? **/
for( i = 0; i < 2; i++) {
if( hashEntry = Tcl_FirstHashEntry( oldTables[i], &searchPtr)) {
do {
key = (char*) Tcl_GetHashKey( oldTables[i], hashEntry);
/**
** The hashEntry will contain the appropriate value for the
** specified 'key' because it will have been aquired depending
** upon whether the unset or set table was used.
**/
val = (char*) Tcl_GetHashValue( hashEntry);
if( val)
EMSetEnv( interp, key, val);
} while( hashEntry = Tcl_NextHashEntry( &searchPtr) );
} /** if **/
} /** for **/
/**
** Delete and reset the hash tables now that the current contents have been
** flushed.
**/
Delete_Global_Hash_Tables();
setenvHashTable = oldTables[0];
unsetenvHashTable = oldTables[1];
aliasSetHashTable = oldTables[2];
aliasUnsetHashTable = oldTables[3];
} else {
Clear_Global_Hash_Tables();
}
return( TCL_OK);
} /** End of 'Unwind_Modulefile_Changes' **/
static int keycmp(const void *a, const void *b) {
return strcmp(*(const char **) a, *(const char **) b);
}
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Output_Modulefile_Changes **
** **
** Description: Is used to flush out the changes of the current **
** modulefile in a manner depending upon whether the **
** modulefile was successful or unsuccessful. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp The attached Tcl in- **
** terpreter **
** **
** Result: int TCL_OK Successful operation **
** **
** Attached Globals: setenvHashTable, **
** unsetenvHashTable, **
** aliasSetHashTable, via Output_Modulefile_Aliases**
** aliasUnsetHashTable via Output_Modulefile_Aliases**
** change_dir for the chdir command **
** **
** ************************************************************************ **
++++*/
int Output_Modulefile_Changes(Tcl_Interp *interp)
{
Tcl_HashSearch searchPtr; /** Tcl hash search handle **/
Tcl_HashEntry *hashEntry; /** Result from Tcl hash search **/
char *val = NULL, /** Stored value (is a pointer!) **/
*key, /** Tcl hash key **/
**list; /** list of keys **/
int i, k; /** Loop counter **/
size_t hcnt; /** count of hash entries **/
/**
** The following hash tables do contain all changes to be made on
** shell variables
**/
Tcl_HashTable *table[2];
table[0] = setenvHashTable;
table[1] = unsetenvHashTable;
/* initialize 'hashEntry': */
hashEntry = Tcl_FirstHashEntry(table[0], &searchPtr);
#if WITH_DEBUGGING_UTIL_2
ErrorLogger(NO_ERR_START, LOC, _proc_Output_Modulefile_Changes, NULL);
#endif /* WITH_DEBUGGING_UTIL_2 */
aliasfile = stdout;
/**
** Scan both tables that are of interest for shell variables
**/
for ((i = 0); (i < 2); i++) {
/* count hash: */
hcnt = countTclHash(table[i]);
/* always make sure we have at least one: */
if (hcnt == 0) {
hcnt = 1;
}
/* allocate array for keys: */
if (!(list = (char **)module_malloc(hcnt * sizeof(char *)))) {
if (OK != ErrorLogger(ERR_ALLOC, LOC, NULL)) {
return (TCL_ERROR); /** ------- EXIT (FAILURE) ------> **/
}
}
/* collect keys: */
k = 0;
/* assuming that the '='s were supposed to be '=='s, so that the value
* stored to 'hashEntry' will get read its next time around the loop: */
if (hashEntry == Tcl_FirstHashEntry(table[i], &searchPtr)) {
do {
char *strung_key;
key = (char*)Tcl_GetHashKey(table[i], hashEntry);
strung_key = stringer(NULL, 0, key, NULL);
/* try to avoid null pointer dereferences: */
if (list != NULL) {
if (strung_key != NULL) {
list[k++] = strung_key;
} else if (key != NULL) {
list[k++] = key;
} else if (val != NULL) {
list[k++] = val;
} else {
/* cannot think of anything else to try assigning
* to it: */
list[k++] = "";
}
} else {
/* 'list' is null, not sure what to do in this case
* besides increment k... */
k++;
}
} while (hashEntry == Tcl_NextHashEntry(&searchPtr));
}
/* sort hash: */
if (hcnt > 1) {
if (list != NULL) {
qsort((void *)list, hcnt, sizeof(char *), keycmp);
} else {
/* 'list' is null, not sure what to do in this case... */
;
}
}
/* output key/values */
for ((k = 0); (k < (int)hcnt); ++k) {
if (list != NULL) {
key = list[k];
} else {
/* 'list' is null, not sure what to make 'key' in that case: */
key = "";
}
hashEntry = Tcl_FindHashEntry(table[i], key);
/**
** The table list indicator is used in order to differ
** between the setenv and unsetenv operation
**/
if (i == 1) {
output_unset_variable((char*)key);
} else {
val = EMGetEnv(interp, key);
if (val && *val) {
output_set_variable(interp, (char*)key, val);
}
null_free((void *)&val);
}
} /** end for-loop **/
/* dealloc list: */
for ((k = 0); (k < (int)hcnt); ++k) {
free(list[k]);
}
if (list != NULL) {
free(list);
}
} /** end for-loop **/
if (EOF == fflush(stdout)) {
if (OK != ErrorLogger(ERR_FLUSH, LOC, _fil_stdout, NULL)) {
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
}
Output_Modulefile_Aliases(interp);
Output_Directory_Change(interp);
/**
** Delete and reset the hash tables since the current contents have been
** flushed.
**/
Clear_Global_Hash_Tables();
return (TCL_OK);
} /* End of 'Output_Modulefile_Changes' */
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Open_Aliasfile **
** **
** Description: Creates/opens or closes temporary file for sourcing **
** or aliases. **
** Passes back the filehandle and filename in global **
** variables. **
** **
** First Edition: 2005/09/26 R.K.Owen <rk@owen.sj.ca.us> **
** **
** Parameters: int action if != 0 to open else close **
** **
** Result: int TCL_OK Successful operation **
** **
** Attached Globals: aliasfile **
** aliasfilename **
** **
** ************************************************************************ **
++++*/
static int Open_Aliasfile(int action)
{
if (action) {
/**
** Open the file ...
**/
if (tmpfile_mod(&aliasfilename,&aliasfile)) {
if (OK != ErrorLogger(ERR_OPEN, LOC, aliasfilename,
"append", NULL)) {
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
}
} else {
if (EOF == fclose(aliasfile)) {
if (OK != ErrorLogger(ERR_CLOSE, LOC, aliasfilename, NULL)) {
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
}
}
return (TCL_OK);
} /** End of 'Open_Aliasfile' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Output_Modulefile_Aliases **
** **
** Description: Is used to flush out the changes to the aliases of **
** the current modulefile. But, some shells don't work **
** well with having their alias information set via the **
** 'eval' command. So, what we'll do now is output the **
** aliases into a /tmp dotfile, have the shell source **
** the /tmp dotfile and then have the shell remove the **
** /tmp dotfile. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp The attached Tcl in- **
** terpreter **
** **
** Result: int TCL_OK Successful operation **
** **
** Attached Globals: aliasSetHashTable, via Output_Modulefile_Aliases**
** aliasUnsetHashTable via Output_Modulefile_Aliases**
** **
** ************************************************************************ **
++++*/
static int Output_Modulefile_Aliases(Tcl_Interp *interp)
{
Tcl_HashSearch searchPtr; /** Tcl hash search handle **/
Tcl_HashEntry *hashEntry; /** Result from Tcl hash search **/
char *val = NULL, /** Stored value (is a pointer!) **/
*key; /** Tcl hash key **/
int i, /** Loop counter **/
openfile = 0; /** whether using a file or not **/
char *sourceCommand; /** Command used to source the alias **/
/**
** The following hash tables do contain all changes to be made on
** shell aliases
**/
Tcl_HashTable *table[2];
table[0] = aliasSetHashTable;
table[1] = aliasUnsetHashTable;
/**
** If configured so, all changes to aliases are written into a temporary
** file which is sourced by the invoking shell ...
** In this case a temporary filename has to be assigned for the alias
** source file. The file has to be opened as 'aliasfile'.
** The default for aliasfile, if no shell sourcing is used, is stdout.
**/
#if WITH_DEBUGGING_UTIL_2
ErrorLogger(NO_ERR_START, LOC, _proc_Output_Modulefile_Aliases, NULL);
#endif /* WITH_DEBUGGING_UTIL_2 */
/**
** We only need to output stuff into a temporary file if we are setting
** stuff. We can unset variables and aliases by just using eval.
**/
/* assuming that the '=' is correct to keep 'hashEntry' from being a
* garbage value (will read new value assigned to it later): */
if ((hashEntry = Tcl_FirstHashEntry(aliasSetHashTable, &searchPtr))) {
/**
** We must use an aliasfile if EVAL_ALIAS is not defined
** or the sh shell does not do aliases (HAS_BOURNE_ALIAS)
** and that the sh shell does do functions (HAS_BOURNE_FUNCS)
**/
if (!eval_alias
|| (!strcmp(shell_name, "sh") && !bourne_alias && bourne_funcs)) {
if (OK != Open_Aliasfile(1)) {
if (OK != ErrorLogger(ERR_OPEN, LOC, aliasfilename,
"append", NULL)) {
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
}
openfile = 1;
}
/**
** We only support sh and csh variants for aliases. If not either
** sh or csh print warning message and return
**/
assert(shell_derelict != NULL);
if (!strcmp(shell_derelict, "csh")) {
sourceCommand = "source %s%s";
} else if (!strcmp(shell_derelict, "sh")) {
sourceCommand = ". %s%s";
} else {
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
if (openfile) {
/**
** Only the source command has to be flushed to stdout. After
** sourcing the alias definition (temporary) file, the source
** file is to be removed.
**/
alias_separator = '\n';
fprintf(stdout, sourceCommand, aliasfilename, shell_cmd_separator);
fprintf(stdout, "/bin/rm -f %s%s",
aliasfilename, shell_cmd_separator);
} /** end "if (openfile)" **/
} /** end "if (alias to set)" **/
/**
** Scan the hash tables involved in changing aliases
**/
for ((i = 0); (i < 2); i++) {
/* assuming that the '='s in here were supposed to be '=='s, so that
* the previous value assigned to 'hashEntry' will be used: */
if ((hashEntry == Tcl_FirstHashEntry(table[i], &searchPtr)) &&
(hashEntry != NULL) && (table[i] != NULL)) {
do {
key = (char*)Tcl_GetHashKey(table[i], hashEntry);
val = (char*)Tcl_GetHashValue(hashEntry);
/**
** The hashtable list index is used to differ between aliases
** to be set and aliases to be reset
**/
if (i == 1) {
output_unset_alias(key, val);
} else {
output_set_alias(key, val);
}
} while ((hashEntry == Tcl_NextHashEntry(&searchPtr)) &&