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
|
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: cmdSetenv.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, jlf@behere.com **
** Jens Hamisch, jens@Strawberry.COM **
** **
** Description: The routines for setting and unsetting environment **
** variables from within modulefiles. **
** **
** Exports: cmdSetEnv **
** cmdUnsetEnv **
** moduleSetenv **
** moduleUnsetenv **
** **
** Notes: **
** **
** ************************************************************************ **
****/
/** ** Copyright *********************************************************** **
** **
** Copyright 1991-1994 by John L. Furlan. **
** see LICENSE.GPL, which must be provided, for details **
** **
** ************************************************************************ **/
static char Id[] = "@(#)$Id: c7add002bfd4245181fa551c05d0426cd79abc76 $";
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[] = "cmdSetenv.c"; /** File name of this module **/
#if WITH_DEBUGGING_CALLBACK
static char _proc_cmdSetEnv[] = "cmdSetEnv";
static char _proc_cmdUnsetEnv[] = "cmdUnsetEnv";
#endif
#if WITH_DEBUGGING_UTIL_1
static char _proc_moduleSetenv[] = "moduleSetenv";
static char _proc_moduleUnsetenv[] = "moduleUnsetenv";
#endif
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
/** not applicable **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: cmdSetEnv **
** **
** Description: Callback function for the 'setenv' command **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: ClientData client_data **
** Tcl_Interp *interp According Tcl interp.**
** int argc Number of arguments **
** char *argv[] Argument array **
** **
** Result: int TCL_OK Successful completion **
** TCL_ERROR Any error **
** **
** Attached Globals: g_flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int cmdSetEnv( ClientData client_data,
Tcl_Interp *interp,
int argc,
CONST84 char *argv[])
{
int force; /** Force removale of variables **/
char *var; /** Varibales name **/
char *val; /** Varibales value **/
#if WITH_DEBUGGING_CALLBACK
ErrorLogger( NO_ERR_START, LOC, _proc_cmdSetEnv, NULL);
#endif
/**
** Check parameters. Usage is: [-force] variable value
**/
if( argc < 3 || argc > 4) {
if( OK != ErrorLogger( ERR_USAGE, LOC, argv[0], "[-force] variable value",
NULL))
return( TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
/**
** Get variables name and value from the argument array
**/
if( *argv[1] == '-') {
if( !strncmp( argv[1], "-force", 6)) {
force = 1;
var = (char *) argv[2];
val = (char *) argv[3];
} else {
if( OK != ErrorLogger( ERR_USAGE, LOC, argv[0], "[-force] variable value",
NULL))
return( TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
} else {
force = 0;
var = (char *) argv[1];
val = (char *) argv[2];
}
moduleSetenv( interp, var, val, force);
/**
** This has to be done after everything has been set because the
** variables may be needed later in the modulefile.
**/
if( g_flags & M_DISPLAY) {
fprintf( stderr, "%s\t\t ", argv[ 0]);
while( --argc)
fprintf( stderr, "%s ", *++argv);
fprintf( stderr, "\n");
}
#if WITH_DEBUGGING_CALLBACK
ErrorLogger( NO_ERR_END, LOC, _proc_cmdSetEnv, NULL);
#endif
return( TCL_OK);
} /** End of 'cmdSetEnv' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: moduleSetenv **
** **
** Description: Set or unset environment variables **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp According Tcl interp.**
** char *variable Name of the variable **
** char *value Value to be set **
** int force Force removal **
** **
** Result: int TCL_OK Successful completion **
** TCL_ERROR Any error **
** **
** Attached Globals: g_flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int moduleSetenv( Tcl_Interp *interp,
char *variable,
char *value,
int force)
{
char *oldval; /** Old value of 'variable' **/
#if WITH_DEBUGGING_UTIL_1
ErrorLogger( NO_ERR_START, LOC, _proc_moduleSetenv, NULL);
#endif
oldval = EMGetEnv( interp, variable);
if (!oldval || !*oldval)
null_free((void *)&oldval);
/**
** Check to see if variable is already set correctly...
**/
if( !(g_flags & (M_REMOVE|M_DISPLAY|M_SWITCH|M_NONPERSIST)) && oldval) {
if( !strcmp( value, oldval)) {
return( TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
}
}
/**
** If I'm in SWSTATE1, I'm removing stuff from the old modulefile, so
** I'll just mark the variables that were used with the SWSTATE1 flag and
** return.
**
** When I come back through in SWSTATE2, I'm setting the variables that
** are in the new modulefile. So, I'll keep track of these by marking
** them as touched by SWSTATE2 and then actually setting their values in
** the environment down below.
**
** Finally, in SWSTATE3, I'll check to see if the variables in the old
** modulefiles that have been marked are still marked as SWSTATE1. If
** they are still the same, then I'll just unset them and return.
**
** And, if I'm not doing any switching, then just unset the variable if
** I'm in remove mode.
**/
if( g_flags & M_SWSTATE1) {
set_marked_entry( markVariableHashTable, variable, M_SWSTATE1);
return( TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
} else if( g_flags & M_SWSTATE2) {
set_marked_entry( markVariableHashTable, variable, M_SWSTATE2);
} else if( g_flags & M_SWSTATE3) {
intptr_t marked_val;
marked_val = chk_marked_entry( markVariableHashTable, variable);
if( marked_val) {
if( marked_val == M_SWSTATE1)
return( moduleUnsetenv(interp, variable)); /** -------> **/
else
return( TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
}
} else if( (g_flags & M_REMOVE) && !force) {
return( moduleUnsetenv( interp, variable)); /** -------> **/
}
/**
** Keep track of our changes just in case we have to bail out and restore
** the environment.
**/
if( !(g_flags & (M_NONPERSIST | M_DISPLAY | M_WHATIS | M_HELP))) {
store_hash_value( setenvHashTable, variable, value);
clear_hash_value( unsetenvHashTable, variable);
}
/**
** Store the value into the environment
**/
EMSetEnv( interp, variable, value);
#if WITH_DEBUGGING_UTIL_1
ErrorLogger( NO_ERR_END, LOC, _proc_moduleSetenv, NULL);
#endif
return( TCL_OK);
} /** End of 'moduleSetenv' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: cmdUnsetEnv **
** **
** Description: Callback function for the 'unset' command **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: ClientData client_data **
** Tcl_Interp *interp According Tcl interp.**
** int argc Number of arguments **
** char *argv[] Argument array **
** **
** Result: int TCL_OK Successful completion **
** TCL_ERROR Any error **
** **
** Attached Globals: g_flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int cmdUnsetEnv( ClientData client_data,
Tcl_Interp *interp,
int argc,
CONST84 char *argv[])
{
/**
** Parameter check. The name of the variable has to be specified
**/
#if WITH_DEBUGGING_CALLBACK
ErrorLogger( NO_ERR_START, LOC, _proc_cmdUnsetEnv, NULL);
#endif
if( argc < 2 || argc > 3) {
if( OK != ErrorLogger( ERR_USAGE, LOC, argv[0], "variable [value]",
NULL))
return( TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
}
/**
** Non-persist mode?
**/
if (g_flags & M_NONPERSIST) {
return (TCL_OK);
}
/**
** Unset the variable or just display what to do ...
**/
if( g_flags & M_DISPLAY) {
fprintf( stderr, "%s\t ", argv[ 0]);
while( --argc)
fprintf( stderr, "%s ", *++argv);
fprintf( stderr, "\n");
} else if( g_flags & M_REMOVE && argc == 3) {
int save_flags = g_flags;
/** allow an optional 3rd argument to set the env.var. to on removal **/
g_flags = (g_flags & ~M_REMOVE) | M_LOAD;
moduleSetenv( interp, (char *) argv[1], (char *) argv[2], 0);
g_flags = save_flags;
} else {
moduleUnsetenv( interp, (char *) argv[1]);
}
/**
** Return on success
**/
#if WITH_DEBUGGING_CALLBACK
ErrorLogger( NO_ERR_END, LOC, _proc_cmdUnsetEnv, NULL);
#endif
return( TCL_OK);
} /** End of 'cmdUnsetEnv' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: moduleUnsetenv **
** **
** Description: Unset environment variables **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp According Tcl interp.**
** char *variable Name of the variable **
** **
** Result: int TCL_OK Successful completion **
** TCL_ERROR Any error **
** **
** Attached Globals: g_flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int moduleUnsetenv( Tcl_Interp *interp,
char *variable)
{
#if WITH_DEBUGGING_UTIL_1
ErrorLogger( NO_ERR_START, LOC, _proc_moduleUnsetenv, NULL);
#endif
/**
** Don't unset the variable in Tcl Space.
** If module writer *REALLY* wants it gone, use $env
**/
if( !(g_flags & (M_NONPERSIST | M_DISPLAY | M_WHATIS | M_HELP))) {
store_hash_value( unsetenvHashTable, variable, NULL);
clear_hash_value( setenvHashTable, variable);
}
#if WITH_DEBUGGING_UTIL_1
ErrorLogger( NO_ERR_END, LOC, _proc_moduleUnsetenv, NULL);
#endif
return( TCL_OK);
} /** end of 'moduleUnsetenv' **/
|