-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
908 lines (785 loc) · 27.5 KB
/
init.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
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: init.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, [email protected] **
** Jens Hamisch, [email protected] **
** R.K. Owen, <[email protected]> or <[email protected]> **
** **
** Description: The initialization routines for Tcl Modules. **
** Primarily the setup of the different Tcl module **
** commands and the global hash tables are initialized **
** here. The initial storage of the begining **
** environment is here as well. **
** **
** Exports: EM_CreateInterp **
** EM_DeleteInterp **
** Initialize_Module **
** Module_Tcl_ExitCmd **
** Module_Init **
** Setup_Environment **
** TieStdout **
** UnTieStdout **
** SetStartupFiles **
** **
** 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"
#ifdef HAS_TCLXLIBS
# include "tclExtend.h"
#endif /* HAS_TCLXLIBS */
/** ************************************************************************ **/
/** LOCAL DATATYPES **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** CONSTANTS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** MACROS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** LOCAL DATA **/
/** ************************************************************************ **/
static char module_name[] = __FILE__;
/** These are the recognized startup files that the given shells
** use. If your site uses a different set, make the modifications here.
** Give the names and the order they should be searched. The lists
** must be null terminated.
**/
/** CSH **/
static char *cshStartUps[] = {
".modules", ".cshrc" DOT_EXT, ".csh_variables", ".login" DOT_EXT, NULL
};
/** TCSH **/
static char *tcshStartUps[] = {
".modules", ".tcshrc", ".cshrc" DOT_EXT, ".csh_variables",
".login" DOT_EXT, NULL
};
/** SH and KSH **/
/** KSH uses whatever is pointed to by $ENV, which is usually named .kshenv
** (TODO) have it read $ENV and use the value
**/
static char *shStartUps[] = {
".modules", ".profile" DOT_EXT, ".kshenv" DOT_EXT, NULL
};
/** BASH **/
/** BASH uses whatever is pointed to by $ENV, for non-interactive shells
** and for POSIX compliance
** (TODO) have it read $ENV and use the value
**/
static char *bashStartUps[] = {
".modules", ".bash_profile", ".bash_login",
".profile" DOT_EXT, ".bashrc" DOT_EXT, NULL
};
/** ZSH **/
static char *zshStartUps[] = {
".modules", ".zshrc" DOT_EXT, ".zshenv" DOT_EXT, ".zlogin" DOT_EXT, NULL
};
/** All the remaining "shells" are not supposed to use startup files **/
static char *genericStartUps[] = {
NULL
};
/** The shell properties matrix - global pointers are set to elements of
** this array
**/
static char *shellprops [][4] = {
/* shell derelict init cmd sep */
{"csh", "csh", "csh", ";"},
{"tcsh", "csh", "csh", ";"},
{"sh", "sh", "sh", ";"},
{"ksh", "sh", "ksh", ";"},
{"bash", "sh", "bash", ";"},
{"zsh", "sh", "zsh", ";"},
{"perl", "perl", "perl", ";"},
{"python", "python", "python", "\n"},
{"ruby", "ruby", "ruby", "\n"},
{"scm", "scm", NULL, "\n"},
{"scheme", "scm", NULL, "\n"},
{"guile", "scm", NULL, "\n"},
{"mel", "mel", NULL, ";"},
{"cmake", "cmake", "cmake", "\n"},
{"fish", "fish", "fish", "\n"},
{NULL, NULL, NULL, NULL},
};
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
static char *set_shell_properties( const char *name);
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Module_Tcl_ExitCmd **
** **
** Description: Error (???) exit routine **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: ClientData client_data **
** Tcl_Interp* interp The attached Tcl **
** interpreter **
** int objc Number of arguments **
** Tcl_Obj *objv[] Array of arguments **
** to the module command**
** **
** Result: int TCL_ERROR Exit on error **
** **
** Attached Globals: **
** **
** ************************************************************************ **
++++*/
int Module_Tcl_ExitCmd(
ClientData client_data,
Tcl_Interp * interp,
int objc,
Tcl_Obj * CONST84 objv[]
) {
char *buffer; /** Buffer for sprintf **/
int value; /** Return value from exit command **/
/**
** Check the number of arguments. The exit command may take no or one
** parameter. So the following is legal:
** exit;
** exit value;
**/
if ((objc < 1) || (objc > 2))
if (OK != ErrorLogger(ERR_USAGE, LOC,
Tcl_GetString(objv[0]), "?returnCode?", NULL))
goto unwind0;
/**
** If the exit command comes with an paramter, set up the TCL result.
** Otherwise the result is 0.
**/
if (objc == 1) {
value = 0;
} else if (Tcl_GetIntFromObj(interp, objv[1], &value) != TCL_OK) {
if (OK != ErrorLogger(ERR_PARAM, LOC,
Tcl_GetString(objv[1]), NULL))
goto unwind0;
}
/**
** Allocate memory
**/
if (!(buffer = stringer(NULL, 25, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind0;
sprintf(buffer, "EXIT %d", value);
Tcl_SetResult(interp, buffer, NULL);
/**
** Exit from this module command.
** ??? Why hardcoded on error ???
**/
unwind0:
return (TCL_ERROR);
} /** End of 'Module_Tcl_ExitCmd' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: EM_CreateInterp **
** **
** Description: Create a Tcl interpreter and set some default **
** attributes for each interpreter. **
** **
** First Edition: 2011/09/26 **
** **
** Parameters: - **
** **
** Result: Tcl_Interp **interp New Tcl interpr. **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
Tcl_Interp *EM_CreateInterp(void) {
Tcl_Interp *interp;
#if WITH_DEBUGGING_INIT
ErrorLogger( NO_ERR_START, LOC, _proc_EM_CreateInterp, NULL);
#endif
interp = Tcl_CreateInterp();
/*
* avoid freeing storage when in use
*/
Tcl_Preserve(interp);
return interp;
} /** End of 'EM_CreateInterp' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: EM_DeleteInterp **
** **
** Description: Delete a Tcl interpreter and set some default **
** attributes for each interpreter. **
** **
** First Edition: 2011/09/26 **
** **
** Parameters: Tcl_Interp **interp Tcl interpr to **
** delete **
** **
** Result : - **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
void EM_DeleteInterp(Tcl_Interp *interp) {
#if WITH_DEBUGGING_INIT
ErrorLogger( NO_ERR_START, LOC, _proc_EM_DeleteInterp, NULL);
#endif
/*
* avoid freeing storage when in use, now release
*/
Tcl_Release(interp);
Tcl_DeleteInterp(interp);
} /** End of 'EM_DeleteInterp' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Initialize_Module **
** **
** Description: This procedure is called from 'main' in order to ini-**
** tialize the whole thing. The arguments specified on **
** the invoking command line are passed to here. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp **interp Buffer to store the **
** Tcl interpr. handle **
** int argc Number od args and **
** char *argv[] arg. array from the **
** shell command line **
** char *environ[] Process environment **
** **
** Result: int **
** **
** Attached Globals: *Ptr will be initialized **
** *HashTable will be allocated and initialized **
** *psep path separator determined **
** **
** ************************************************************************ **
++++*/
int Initialize_Module(
Tcl_Interp ** interp,
int argc,
char *argv[],
char *environ[]
) {
int Result = TCL_ERROR;
char *tmp;
/**
** Check the command syntax. Since this is already done
** Less than 3 parameters isn't valid. Invocation should be
** 'modulecmd <shell> <command>'
**/
if (argc < 2)
if (OK != ErrorLogger(ERR_USAGE,LOC,argv[0]," shellname", NULL))
goto unwind0;
/**
** Check the first parameter to modulcmd for a known shell type
** and set the shell properties
**/
if (!set_shell_properties(argv[1]))
if (OK != ErrorLogger(ERR_SHELL, LOC, argv[1], NULL))
goto unwind0;
/**
** List of directories & files to skip
**/
if (!(skipdirs = mhash_ctor(MHashInt)))
if (OK != ErrorLogger(ERR_UVEC, LOC, NULL))
goto unwind0;
(void) mhash_add(skipdirs, "CVS",1);
(void) mhash_add(skipdirs, "RCS",1);
(void) mhash_add(skipdirs, ".git",1);
(void) mhash_add(skipdirs, ".svn",1);
(void) mhash_add(skipdirs, modulerc_file,-1);
(void) mhash_add(skipdirs, version_file,-1);
/**
** Create a Tcl interpreter in order to proceed the command. Initialize
** this interpreter and set up pointers to all Tcl Module commands
** (Module_Init)
**/
Tcl_FindExecutable(argv[0]);
*interp = Tcl_CreateInterp();
if (TCL_OK != (Result = Module_Init(*interp)))
goto unwind0;
/**
** Determine the path separator from the cwd
**/
cwd = Tcl_FSGetCwd(*interp);
psep = Tcl_GetString(Tcl_FSPathSeparator(cwd));
if (!psep)
psep = "/";
/**
** Now set up the hash-tables for shell environment modifications.
** For a description of these tables have a look at main.c, where
** they're defined.
** Exit from the whole program in case allocation fails.
**/
if ( (!(setenvHashTable = mhash_ctor(MHashStrings)))
|| (!(unsetenvHashTable = mhash_ctor(MHashStrings)))
|| (!(aliasSetHashTable = mhash_ctor(MHashStrings)))
|| (!(aliasUnsetHashTable = mhash_ctor(MHashStrings)))
|| (!(markVariableHashTable = mhash_ctor(MHashInt)))
|| (!(markAliasHashTable = mhash_ctor(MHashInt)))) {
if (OK != ErrorLogger(ERR_ALLOC, LOC, NULL))
goto unwind0;
}
GlobalHashTables[0] = setenvHashTable;
GlobalHashTables[1] = unsetenvHashTable;
GlobalHashTables[2] = aliasSetHashTable;
GlobalHashTables[3] = aliasUnsetHashTable;
GlobalHashTables[4] = NULL;
#ifdef BEGINENV
# if BEGINENV == 99
/**
** Check for the existence of the environment variable
** "MODULESBEGINENV". This signals that for this
** configuration that the user wants to record the initial
** environment as seen for the first time by the module
** command into the filename given in the MODULESBEGINENV
** environment variable (which can have one level of
** variable expansion). Whether it's the first time or not
** is moderated by the existence of environment variable
** _MODULESBEGINENV_ or not.
**
** The update command will use this information to reinitialize the
** environment and then reload every modulefile that has been loaded
** since as stored in the LOADEDMODULES environment variable in order.
**/
if ((tmp = xgetenv("MODULESBEGINENV"))) {
/* MODULESBEGINENV is set ... use it */
if (!getenv("_MODULESBEGINENV_")) {
FILE *file;
if ((file = fopen(tmp, "w+"))) {
int i = 0;
while (environ[i]) {
fprintf(file, "%s\n", environ[i++]);
}
moduleSetenv(*interp,"_MODULESBEGINENV_",tmp,1);
fclose(file);
} else
if (OK != ErrorLogger(ERR_OPEN, LOC,
TCL_RESULT(*interp), _(em_appending), NULL))
goto unwind0;
null_free((void *)&tmp);
}
}
# else
/**
** Check for the existence of the
** environment variable "_MODULESBEGINENV_". If it is set, then
** do nothing, otherwise, Store every environment variable into
** ~/.modulesbeginenv. This will be used to store the environment
** variables exactly as it was when Modules saw it for the very first
** time.
**
** The update command will use this information to reinitialize the
** environment and then reload every modulefile that has been loaded
** since as stored in the LOADEDMODULES environment variable in order.
**/
if (!getenv("_MODULESBEGINENV_")) {
/* use .modulesbeginenv */
FILE *file;
char savefile[] = "/.modulesbeginenv";
char *buffer;
tmp = getenv("HOME");
if (!(tmp = getenv("HOME")))
if (OK != ErrorLogger(ERR_HOME, LOC, NULL))
goto unwind0;
if (!(buffer = stringer(NULL, 0, tmp, savefile, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind0;
if ((file = fopen(buffer, "w+"))) {
int i = 0;
while (environ[i]) {
fprintf(file, "%s\n", environ[i++]);
}
moduleSetenv(*interp, "_MODULESBEGINENV_", buffer, 1);
fclose(file);
} else
if (OK != ErrorLogger(ERR_OPEN, LOC,
TCL_RESULT(*interp), _(em_appending), NULL))
goto unwind0;
null_free((void *)&buffer);
}
# endif
#endif
/**
** Exit to the main program
**/
return (TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
unwind0:
return (Result); /** -------- EXIT (FAILURE) -------> **/
} /** End of 'Initialize_Module' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Module_Init **
** **
** Description: Initialization of the passed Tcl interpreter. At **
** first the standard Tcl and (if required) TclX initi- **
** alization is called. Thereafter all module commands **
** callback function are defined. **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp The Tcl Interpreter **
** to be initilized **
** **
** Result: int TCL_OK All done, Success **
** TCL_ERROR Failure anywhere **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
int Module_Init(
Tcl_Interp * interp
) {
/**
** General initialization of the Tcl interpreter
**/
if (Tcl_Init(interp) == TCL_ERROR)
if (OK != ErrorLogger(ERR_INIT_TCL, LOC, NULL))
goto unwind0;
#ifdef HAS_TCLXLIBS
/**
** Extended Tcl initialization if configured so ...
**/
#if (TCL_MAJOR_VERSION > 8 || TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION > 3)
if (Tclx_Init(interp) == TCL_ERROR)
#elif (TCL_MAJOR_VERSION > 7 || TCL_MAJOR_VERSION == 7 && TCL_MINOR_VERSION > 5)
if (Tclxcmd_Init(interp) == TCL_ERROR)
#else
if (TclXCmd_Init(interp) == TCL_ERROR)
#endif
{
if (OK != ErrorLogger(ERR_INIT_TCLX, LOC, NULL))
goto unwind0;
}
#endif /* HAS_TCLXLIBS */
#ifdef AUTOLOADPATH
/**
** Extend autoload path
**/
if (TCL_OK != Tcl_Eval(interp,
"if [info exists auto_path] { "
"set auto_path [linsert $auto_path 0 "
AUTOLOADPATH "]} else {" "set auto_path \""
AUTOLOADPATH "\" }"))
if (OK != ErrorLogger(ERR_INIT_ALPATH, LOC, NULL))
goto unwind0;
#endif /* AUTOLOADPATH */
/**
** Now for each module command a callback routine has to be specified
**/
Tcl_CreateObjCommand(interp, "exit", Module_Tcl_ExitCmd,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "setenv", cmdSetEnv,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "unsetenv", cmdUnsetEnv,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "prepend-path", cmdSetPath,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "append-path", cmdSetPath,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "remove-path", cmdRemovePath,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-info", cmdModuleInfo,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module", cmdModule,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-whatis", cmdModuleWhatis,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-verbosity", cmdModuleVerbose,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-user", cmdModuleUser,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-log", cmdModuleLog,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-alias", cmdModuleAlias,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "module-version", cmdModuleVersion,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "set-alias", cmdSetAlias,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "unset-alias", cmdSetAlias,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "conflict", cmdConflict,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "prereq", cmdPrereq,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "is-loaded", cmdIsLoaded,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "chdir", cmdChDir,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "system", cmdSystem,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "uname", cmdUname,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
Tcl_CreateObjCommand(interp, "x-resource", cmdXResource,
(ClientData) shell_derelict,
(void (*)(ClientData))NULL);
return (TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
unwind0:
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
} /** End of 'Module_Init' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: Setup_Environment **
** **
** Description:Define all variables to be found in the current **
** shell environment as Tcl variables in the passed **
** Tcl interpreter. **
** Assign as value 0 to all of them. ??? Why ??? **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp Attched Tcl interpr. **
** **
** Result: int TCL_ERROR Variable could not be set up **
** 0 Success ??? TCL_OK ??? **
** **
** Attached Globals: environ **
** **
** ************************************************************************ **
++++*/
int Setup_Environment( Tcl_Interp* interp)
{
int i, /** loop counter **/
envsize = 0; /** Total size of the environment **/
char *eq; /** Temp. val. used for location the **/
/** Equal sign. **/
char *loaded; /** The currently loaded modules **/
/**
** Scan the whole environment value by value.
** Count its size
**/
for( i = 0; environ[i]; i++) {
envsize += strlen( environ[i]) + 1;
/**
** Locate the equal sign and terminate the string at its position.
**/
eq = environ[i];
while( *eq++ != '=' && *eq);
*(eq - 1) = '\0';
/**
** Now set up a Tcl variable of the same name and value as the
** environment variable
**/
if( ! Tcl_SetVar( interp, environ[i], eq, 0))
if( OK != ErrorLogger( ERR_SET_VAR, LOC, environ[i], NULL))
goto unwind0;
/**
** Reinstall the changed environment
**/
*(eq - 1) = '=';
} /** for **/
/**
** Reconstruct the _LMFILES_ environment variable
**/
loaded = getLMFILES(interp);
if( loaded)
if( !(EMSetEnv( interp, "_LMFILES_", loaded)))
if( OK != ErrorLogger( ERR_SET_VAR, LOC, environ[i], NULL))
goto unwind0;
return( TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
unwind0:
return( TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
} /** end of 'Setup_Environment' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: TieStdout, UnTieStdout **
** **
** Description: TieStdout closes the 'stdout' handle and reopens it **
** as 'stderr'. The original 'stdout' handle is passed **
** back to the caller. **
** UnTieStdout reverts this by reopening 'stdout' as the**
** handle passed as parameter **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: int saved_stdout Handle to be used for rein- **
** stalling stdout **
** **
** Result: int The (just reinstalled or saved) stdout handle**
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
int TieStdout( void) {
int save;
if( 0 > (save = dup(1)))
if( OK != ErrorLogger( ERR_DUP, LOC, _fil_stdout, NULL))
goto unwind0;
if( 0 > close( 1))
if( OK != ErrorLogger( ERR_CLOSE, LOC, _fil_stdout, NULL))
goto unwind0;
/**
** dup used the very first closed handle for duplication. Since stdout
** has just been closed, this will be reopened as stderr here.
**/
if( 0 > (dup(2)))
if( OK != ErrorLogger( ERR_DUP, LOC, _fil_stderr, NULL))
goto unwind0;
return( save); /** ------- EXIT (RESULT) --------> **/
unwind0:
return( -1); /** ------- EXIT (FAILURE) --------> **/
}
int UnTieStdout( int saved_stdout) {
int retval;
if( 0 > close( 1))
if( OK != ErrorLogger( ERR_CLOSE, LOC, _fil_stdout, NULL))
goto unwind0;
if( 0 > (retval = dup( saved_stdout)))
if( OK != ErrorLogger( ERR_DUP, LOC, _fil_stdout, NULL))
goto unwind0;
return( retval);
unwind0:
return( -1); /** ------- EXIT (FAILURE) --------> **/
}
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: SetStartupFiles **
** **
** Description: Collects all startupfiles used by the various shells **
** in the array 'shell_startups'. This function does not**
** care if the startup file do not exist! **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: shell_name the shell being used **
** Result: shell_startups NULL terminated list of startup files**
** for the shell **
** returns NULL if an error **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
char **SetStartupFiles(char *shell_name)
{
/**
** CSH
**/
if( (strcmp( "csh", shell_name) == 0)) {
return cshStartUps;
/**
** TCSH
**/
} else if((strcmp("tcsh", shell_name) == 0)) {
return tcshStartUps;
/**
** SH and KSH
** ??? What's about .environ ???
**/
} else if((strcmp("sh", shell_name) == 0) ||
(strcmp("ksh", shell_name) == 0)) {
return shStartUps;
/**
** BASH
** ??? doesn't this guy use the SH startups, too ???
**/
} else if((strcmp("bash", shell_name) == 0)) {
return bashStartUps;
/**
** ZSH
**/
} else if((strcmp("zsh", shell_name) == 0)) {
return zshStartUps;
/**
** All of the remainig "shells" are not supposed to used startup
** files
**/
} else {
return genericStartUps;
}
} /** End of 'SetStartupFiles' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: set_shell_properties **
** **
** Description: Normalize the current calling shell to one of the **
** basic shells defining the variable and alias syntax **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: const char *name Invoking shell name **
** **
** Result: char* Shell derelict name **
** **
** Attached Globals: shell_derelict **
** shell_cmd_separator **
** **
** ************************************************************************ **
++++*/
static char *set_shell_properties( const char *name)
{
/**
** Loop through the shell properties matrix until a match is found
**/
int i = 0;
while (shellprops[i][0]) {
if( !strcmp(name,shellprops[i][0])) { /* found match */
shell_name = shellprops[i][0];
shell_derelict = shellprops[i][1];
shell_init = shellprops[i][2];
shell_cmd_separator = shellprops[i][3];
return ((char *) name);
}
i++;
}
shell_name = NULL;
shell_derelict = NULL;
shell_init = NULL;
shell_cmd_separator = NULL;
/**
** Oops! Undefined shell name ...
**/
return( NULL);
} /** End of 'set_shell_properties' **/