-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.c
2927 lines (2630 loc) · 85.5 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, [email protected] **
** Jens Hamisch, [email protected] **
** R.K. Owen, <[email protected]> or <[email protected]> **
** **
** Description: General routines that are called throughout Modules **
** which are not necessarily specific to any single **
** block of functionality. **
** **
** Exports: SortedDirList **
** SplitIntoList **
** FreeList **
** ModulePathList **
** Global_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 **
** xdup **
** xgetenv **
** countTclHash **
** ReturnValue **
** OutputExit **
** is_ **
** is_interactive **
** **
** Notes: **
** **
** ************************************************************************ **
****/
/** ** Copyright *********************************************************** **
** **
** Copyright 1991-1994 by John L. Furlan. **
** see LICENSE.GPL, which must be provided, for details **
** **
** ************************************************************************ **/
static char Id[] = "@(#)$Id$";
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[] = __FILE__;
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
;
static const int bourne_funcs = /** HAS_BOURNE_FUNCS macro **/
#ifdef HAS_BOURNE_FUNCS
1
#else
0
#endif
;
static const int bourne_alias = /** HAS_BOURNE_FUNCS macro **/
#ifdef HAS_BOURNE_ALIAS
1
#else
0
#endif
;
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
static int Output_Modulefile_Aliases( void);
static int Output_Directory_Change( void);
static int output_set_variable( Tcl_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 EscapeFishString(char* in);
static void EscapeCmakeString(const char* in,
char* out);
static int is_interactive(void) ;
static int alias_is_func(const char *val) ;
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: filename_compare **
** **
** Description: This is a reverse compare function to reverse the **
** filename list. The function is used as compare func- **
** tion for qsort. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: const void *fi1 First filename to compare **
** const void *fi2 Second filename to compare **
** **
** Result: int -1 filename 1 > filename 2 **
** 0 filename 1 == filename 2 **
** 1 filename 1 < filename 2 **
** **
** Attached Globals: **
** **
** ************************************************************************ **
++++*/
static int filename_compare(
const void *fi1,
const void *fi2
) {
return strcmp(*(char **)fi2, *(char **)fi1);
}
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: SortedDirList **
** **
** Description: Checks the given path for the given modulefile. **
** If the path + the module filename is the modulefile, **
** then it is returned as the first element in the list.**
** If the path + the module filename is a directory, the**
** directory is read and sorted as the list. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: char *path Path to start seeking**
** char *modulename Name of the module **
** int *listcnt Buffer to return the **
** size of the created **
** list in elements **
** **
** Result: uvec* NULL Any failure (alloc, param) **
** else Base pointer to the newly **
** created list. **
** *listcnt Number of elements in the **
** list if one was created, un- **
** changed otherwise **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
uvec *SortedDirList(
char *path,
char *modulename,
int *listcnt
) {
struct dirent *file; /** Directory entry **/
DIR *subdirp; /** Subdirectoy handle **/
char *full_path; /** Sugg. full path (path + module) **/
uvec *filelist; /** Temp. uvec list **/
int pathlen; /** String length of 'fullpath' **/
is_Result fstate; /** file stat **/
/**
** Allocate memory for the list to be created. Suggest a list size of
** 20 Elements. This may be changed later on.
**/
if (!(filelist = uvec_ctor(20)))
if (OK != ErrorLogger(ERR_UVEC, LOC, NULL))
goto unwind0;
/**
** Form the suggested module file name out of the passed path and
** the name of the module. Alloc memory in order to do this.
**/
if (!(full_path = stringer(NULL, 0,
path, psep, modulename, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind1;
pathlen = strlen(full_path);
/**
** Check whether this file exists. If it doesn't free up everything
** and return on failure
**/
if (!(fstate = is_("what",full_path)))
goto unwind2;
/**
** If the suggested module file is a regular one, we've found what we've
** seeked for. Put it on the top of the list and return.
**/
if (fstate == IS_FILE) {
if (uvec_add(filelist, modulename))
if (OK != ErrorLogger(ERR_UVEC, LOC, NULL))
goto unwind2;
*listcnt = 1;
return (filelist); /** --- EXIT PROCEDURE (SUCCESS) --> **/
}
/**
** What we've found is a directory
**/
if (fstate == IS_DIR) {
char *tbuf;
/** Buffer for the whole filename for each **/
/** content of the directory **/
char *mpath;
/** Pointer into *tbuf where to write the dir**/
/** entry **/
/**
** Open the directory for reading
**/
if (!(subdirp = opendir(full_path))) {
#if 0
/* if you can't open the directory ... is that really an error? */
if (OK !=
ErrorLogger(ERR_OPENDIR, LOC, full_path, NULL))
#endif
goto unwind2;
}
/**
** Allocate a buffer for constructing complete file names
** and initialize it with the directory part we do already know.
**/
if (!(tbuf = stringer(NULL,MOD_BUFSIZE, full_path, psep, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
mpath = (tbuf + pathlen + 1);
/**
** Now scan all entries of the just opened directory
**/
for (file = readdir(subdirp);
file != NULL;
file = readdir(subdirp)) {
/**
** Now, if we got a real entry which is not '.*' or '..' and
** finally is not a temporary file (which are defined to end
** on '~' ...
**/
if (file->d_name && *file->d_name != '.' &&
strcmp(file->d_name, "..") &&
file->d_name[NLENGTH(file) - 1] != '~') {
/**
** ... build the full pathname and check for the magic
** cookie or for another directory level in order to
** validate this as a modulefile entry we're seeking for.
**/
strcpy(mpath, file->d_name);
if (check_magic(tbuf, MODULES_MAGIC_COOKIE,
MODULES_MAGIC_COOKIE_LENGTH) ||
is_("what",tbuf) ) {
/**
** Yep! Found! Put it on the list
**/
if (uvec_add(filelist, stringer(NULL, 0,
modulename, psep, file->d_name, NULL)))
if (OK !=
ErrorLogger(ERR_UVEC, LOC,
NULL))
goto unwindt;
} /** if( mag. cookie or directory) **/
} /** if( not a dotfile) **/
} /** for **/
/**
** Put a terminator at the lists end and then sort the list
**/
if (uvec_qsort(filelist, filename_compare))
if (OK != ErrorLogger(ERR_UVEC, LOC, NULL))
goto unwindt;
/**
** Free up temporary values ...
**/
if (closedir(subdirp) < 0)
if (OK !=
ErrorLogger(ERR_CLOSEDIR, LOC, full_path, NULL)) {
goto unwindt;
}
null_free((void *)&full_path);
null_free((void *)&tbuf);
/**
** Set up return values and pass the created list to the caller
**/
*listcnt = uvec_number(filelist);
return (filelist); /** --- EXIT PROCEDURE (SUCCESS) --> **/
if (0) {
unwindt:
null_free((void *)&tbuf);
goto unwind3;
}
}
/**
** If it is not a regular file, neither a directory, we don't support
** it at all ...
**/
unwind3:
if (closedir(subdirp) < 0)
ErrorLogger(ERR_CLOSEDIR, LOC, full_path, NULL);
unwind2:
null_free((void *)&full_path);
unwind1:
FreeList(&filelist);
unwind0:
*listcnt = -1;
return (NULL); /** --- EXIT PROCEDURE (FAILURE) --> **/
} /** End of 'SortedDirList' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: SplitIntoList **
** **
** Description: Splits a path-type environment variable into an array**
** of char* list. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: char *pathenv Path to split **
** int *numpaths Buffer to write the **
** number of array ele- **
** ments to. **
** char *delim Path delimiter **
** **
** Result: char** NULL Any failure (alloc, param.) **
** else Base pointer of the created **
** array **
** *numpaths Number of elements if an ar- **
** ray has been created, unchan-**
** ged otherwise. **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
uvec *SplitIntoList(
const char *pathenv,
int *numpaths,
const char *delim
) {
uvec *pathlist = NULL; /** Temporary uvec **/
char *givenpath = NULL, /** modifiable buffer **/
*dirname = NULL, /** Token ptr **/
*tpath = NULL; /** temp xdup string **/
/**
** Parameter check
**/
if (!pathenv)
if (OK != ErrorLogger(ERR_PARAM, LOC, "pathenv", NULL))
goto unwind0;
/**
** Need a copy of pathenv for tokenization
**/
if (!(givenpath = stringer(NULL,0,pathenv,NULL)))
if (OK != ErrorLogger(ERR_PARAM, LOC, "pathenv", NULL))
goto unwind0;
/**
** Split the path, first allocate a new vector
**/
if (!(pathlist = uvec_ctor(uvec_count_tok(delim,pathenv)+1))) {
if( OK != ErrorLogger( ERR_UVEC, LOC, NULL))
goto unwind1;
}
/**
** Split the given path environment variable into its components
** May need to expand internal variables
**/
for (dirname = xstrtok(givenpath, delim);
dirname;
dirname = xstrtok( NULL, delim)) {
tpath = xdup(dirname);
/* chop off any ending \n's */
if (tpath[strlen(tpath)-1] == '\n')
tpath[strlen(tpath) - 1] = '\0';
if(uvec_add(pathlist,tpath)) {
if( OK != ErrorLogger( ERR_UVEC, LOC, NULL))
goto unwind2;
}
null_free((void *) &tpath);
}
/**
** Free up the temporary string buffer
**/
if (givenpath)
null_free((void *) &givenpath);
/**
** Set up the return value (Number of elements allocated) and pass
** the uvec to the caller
**/
*numpaths = uvec_number(pathlist);
return (pathlist);
unwind2:
uvec_dtor(&pathlist);
unwind1:
null_free((void *) &givenpath);
unwind0:
return (NULL); /** -------- EXIT FAILURE -------> **/
} /** End of 'SplitIntoList' **/
#ifndef FreeList
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: FreeList **
** **
** Description: Frees a char* array type list. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: char **list Pointer to the list **
** int numelem Number of elements in the **
** list **
** Result: - **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
void FreeList( uvec **list)
{
uvec_dtor(list);
} /** End of 'FreeList' **/
#endif
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: ModulePathList **
** **
** Description: Splits a MODULEPATH environment variable into **
** of vector list. **
** Memory must be released manually with FreeList(). **
** **
** First Edition: 2009/08/28 **
** **
** Parameters: (none) **
** **
** Result: uvec* NULL If env.var. does not exist **
** or any failure **
** else Base pointer of the created **
** vector **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
uvec *ModulePathList(
void
) {
char *modulepath; /** modulepath env.var. **/
uvec *modulevec; /** modulepath vector **/
int numpaths; /** number of paths **/
/**
** Load the MODULEPATH and split it into a list of paths
**/
if (!(modulepath = xgetenv("MODULEPATH"))) {
/* ErrorLogger(ERR_MODULE_PATH, LOC, NULL); */
return NULL;
}
modulevec = SplitIntoList(modulepath, &numpaths, _colon);
null_free((void *) &modulepath);
return modulevec;
}
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Global_Hash_Tables **
** **
** Description: Process the global hash tables in one fell swoop **
** **
** First Edition: 2009/09/01 **
** **
** Parameters: GlobalHashAction action **
** Clear reinitialize hashes **
** Delete destroy them all **
** Copy return a copy of each **
** MHash ** globalhashtables **
** **
** Result: MHash ** newtables (copy only) **
** **
** Attached Globals: GlobalHashTables (if null input) **
** **
** ************************************************************************ **
++++*/
MHash **Global_Hash_Tables(
GHashAction action,
MHash **globalhashtables
) {
MHash **newtable,
**t_ptr = globalhashtables,
**newt_ptr,
*tmp;
/**
** Loop for all the global hash tables. If there's no value stored
** in a hash table, skip to the next one.
**/
if (!globalhashtables)
t_ptr = GlobalHashTables;
if (action == GHashCopy) {
if (!(newt_ptr = newtable =
(MHash **) module_malloc(5 * sizeof(MHash *))))
if( OK != ErrorLogger( ERR_ALLOC, LOC, NULL))
return (MHash **) NULL;
}
while (t_ptr && *t_ptr) {
switch (action) {
case GHashClear:
tmp = mhash_ctor(mhash_type(*t_ptr));
mhash_dtor(t_ptr);
*t_ptr = tmp;
break;
case GHashDelete:
mhash_dtor(t_ptr);
break;
case GHashCopy:
*newt_ptr++ = mhash_copy(*t_ptr);
break;
default:
return (MHash **) NULL;
}
t_ptr++;
}
if (action == GHashCopy) {
*newt_ptr = (MHash *) NULL;
return newtable;
} else
return globalhashtables;
} /** End of 'Global_Hash_Tables' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: **
** **
** Description: Once 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.**
** MHash **oldTables Hash tables storing **
** the former environm. **
** Result: **
** Attached Globals: **
** **
** ************************************************************************ **
++++*/
int Unwind_Modulefile_Changes(
Tcl_Interp * interp,
MHash ** oldTables
) {
char *val = NULL, /** Stored value (is a pointer!) **/
**keys; /** Tcl hash key **/
int i; /** Loop counter **/
if (oldTables) {
/**
** Entries 0 and 1 which contain all changes to the
** shell variables (setenv and unsetenv)
** Entries 2 and 3 which contain the aliase/unalias setting
**/
for (i = 0; i < 4; i++) {
/**
** The hash entry will contain the appropriate value for the
** specified 'key' because it will have been acquired depending
** upon whether the unset or set table was used.
**/
if (!mhash_exists(oldTables[i]))
continue;
keys = mhash_keys(oldTables[i]);
while (keys && *keys) {
val = mhash_value(oldTables[i], *keys);
if (val)
(void) EMSetEnv(interp, *keys, val);
keys++;
}
} /** for **/
/**
** Delete and reset the hash tables now the current contents have been
** flushed.
**/
Global_Hash_Tables(GHashDelete, NULL);
setenvHashTable = oldTables[0];
unsetenvHashTable = oldTables[1];
aliasSetHashTable = oldTables[2];
aliasUnsetHashTable = oldTables[3];
} else {
Global_Hash_Tables(GHashClear, NULL);
}
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: GlobalHashTable, **
** unsetenvHashTable, **
** aliasSetHashTable, via Output_Modulefile_Aliases**
** aliasUnsetHashTable via Output_Modulefile_Aliases**
** change_dir for the chdir command **
** **
** ************************************************************************ **
++++*/
int Output_Modulefile_Changes(
Tcl_Interp * interp
) {
char *val = NULL, /** Stored value (is a pointer!) **/
**keys; /** hash keys **/
uvec *uvkeys; /** vector of keys **/
int i; /** Loop counter **/
MHash *table[2]; /** setenv & unsetenv hashes **/
/**
** The following hash tables contain all changes to be made on
** shell variables
**/
table[0] = setenvHashTable;
table[1] = unsetenvHashTable;
aliasfile = stdout;
/**
** Scan both tables that are of interest for shell variables
**/
for (i = 0; i < 2; i++) {
if (!mhash_exists(table[i]))
continue;
uvkeys = mhash_keys_uvec(table[i]);
uvec_qsort(uvkeys, keycmp);
/* output key/values */
keys = uvec_vector(uvkeys);
while (keys && *keys) {
if (i == 1) {
output_unset_variable(*keys);
} else {
val = EMGetEnv(interp, *keys);
if (val && *val)
output_set_variable(interp, *keys, val);
null_free((void *) &val);
}
keys++;
}
} /** for **/
if (EOF == fflush(stdout))
if (OK != ErrorLogger(ERR_FLUSH, LOC, _fil_stdout, NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
Output_Modulefile_Aliases();
Output_Directory_Change();
/**
** Delete and reset the hash tables since the current contents have been
** flushed.
**/
Global_Hash_Tables(GHashClear, NULL);
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 <[email protected]> **
** **
** 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,
_(em_appending), 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: void none **
** **
** Result: int TCL_OK Successful operation **
** **
** Attached Globals: aliasSetHashTable, via Output_Modulefile_Aliases**
** aliasUnsetHashTable via Output_Modulefile_Aliases**
** **
** ************************************************************************ **
++++*/
static int Output_Modulefile_Aliases(
void
) {
char *val = NULL, /** Stored value (is a pointer!) **/
**keys = NULL, /** hash keys **/
*sourceCommand; /** Command used to source the alias **/
int i, /** Loop counter **/
openfile = 0; /** whether using a file or not **/
/**
** The following hash tables do contain all changes to be made on
** shell aliases
**/
MHash *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.
**
** We only need to output stuff into a temporary file if we're setting
** stuff. We can unset variables and aliases by just using eval.
**/
if (mhash_exists(aliasSetHashTable))
keys = mhash_keys(aliasSetHashTable);
while (keys && *keys) {
/**
** 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,
_(em_appending), 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 */
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);
} /** openfile **/
keys++;
} /* while */
/**
** Scan the hash tables involved in changing aliases
**/
for (i = 0; i < 2; i++) {
if (!mhash_exists(table[i]))
continue;
keys = mhash_keys(table[i]);
while (keys && *keys) {
/**
** The hashtable list index is used to differ between aliases
** to be set and aliases to be reset
**/
val = mhash_value(table[i], *keys);
if (i == 1) {
output_unset_alias(*keys, val);
} else {
output_set_alias(*keys, val);
}
keys++;
}
} /** for **/
if (openfile) {
if (OK != Open_Aliasfile(0))
if (OK !=
ErrorLogger(ERR_CLOSE, LOC, aliasfilename, NULL))
return (TCL_ERROR); /** -- EXIT (FAILURE) -> **/
null_free((void *)&aliasfilename);
}
return (TCL_OK);
} /** End of 'Output_Modulefile_Aliases' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Output_Directory_Change **
** **
** Description: Changes the current working directory. **
** **
** Parameters: void none **
** **
** Result: int TCL_OK Successful operation **
** TCL_ERROR When not applicable **
** **
** Attached Global: change_dir **
** **
** ************************************************************************ **
++++*/
static int Output_Directory_Change(
void
) {
int retval = TCL_OK;
if (!change_dir)
return retval;
assert(shell_derelict != NULL);
if (!strcmp(shell_derelict, "csh") || !strcmp(shell_derelict, "sh")) {
fprintf(stdout, "cd '%s'%s", change_dir, shell_cmd_separator);
} else if (!strcmp(shell_derelict, "perl")) {
fprintf(stdout, "chdir '%s'%s", change_dir,
shell_cmd_separator);
} else if (!strcmp(shell_derelict, "python")) {
fprintf(stdout, "os.chdir('%s')\n", change_dir);
} else if( !strcmp( shell_derelict, "ruby")) {
fprintf(stdout, "Dir.chdir('%s')\n", change_dir);
} else {
retval = TCL_ERROR;
}
null_free((void *)&change_dir);
return retval;
}
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: output_set_variable **
** **
** Description: Outputs the command required to set a shell variable **
** according to the current shell **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp The attached Tcl interpreter **
** const char *var Name of the variable to be **
** set **
** const char *val Value to be assigned **
** **
** Result: int TCL_OK Finished successful **
** TCL_ERROR Unknown shell type **
** **
** Attached Globals: shell_derelict **
** **
** ************************************************************************ **
++++*/
static int output_set_variable( Tcl_Interp *interp,
const char *var,
const char *val) {
/**
** Differ between the different kinds of shells at first
**
** CSH
**/
chop(val);
chop(var);
assert(shell_derelict != NULL);
if (!strcmp((char *)shell_derelict, "csh")) {