-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModuleCmd_Use.c
360 lines (306 loc) · 10.7 KB
/
ModuleCmd_Use.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
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: ModuleCmd_Use.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, [email protected] **
** Jens Hamisch, [email protected] **
** R.K. Owen, <[email protected]> or <[email protected]> **
** **
** Description: Prepends (and appends) directories to the MODULEPATH **
** environment variable to enable access to more **
** modulefiles. **
** **
** Exports: ModuleCmd_Use **
** ModuleCmd_UnUse **
** **
** 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__;
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
#ifdef BEGINENV
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: append_to_modulesbeginenv **
** **
** Description: Append the passed variable (with value) to the begin-**
** ning environment **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp Attached Tcl Interp. **
** char *var Name of the variable **
** **
** Result: - **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
static void append_to_modulesbeginenv( Tcl_Interp *interp,
char *var)
{
char *filename, /** The filename, where the begin- **/
/** ning environment resides **/
*val; /** Value of the passed variable **/
FILE *file; /** File read handle **/
#if BEGINENV == 99
char *mbegenv = EMGetEnv( interp,"MODULESBEGINENV");
#endif
if( var
#if BEGINENV == 99
&& (mbegenv && *mbegenv)
#endif
) {
/**
** Get filename and the value of the passed variable
**/
filename = (char *) EMGetEnv( interp, "_MODULESBEGINENV_");
if(filename && *filename) {
val = (char *) EMGetEnv( interp, var);
/**
** Append the string <var>=<value>
**/
if( NULL != (file = fopen( filename, "a+"))) {
fprintf( file, "%s=%s\n", var, val);
if( EOF == fclose( file))
ErrorLogger( ERR_CLOSE, LOC, filename, NULL);
} else {
ErrorLogger( ERR_OPEN, LOC, filename, _(em_appending), NULL);
}
null_free((void *)&val);
} /** if( get filename) **/
null_free((void *)&filename);
} /** if( var passed) **/
#if BEGINENV == 99
null_free((void *)&mbegenv);
#endif
} /** End of 'append_to_modulesbeginenv' **/
#else
# define append_to_modulesbeginenv( a, b) {}
#endif
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: ModuleCmd_Use **
** **
** Description: Execution of the module-command 'use' **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp Attached Tcl Interp. **
** int argc Number of arguments **
** char *argv[] Argument list **
** **
** Result: int TCL_ERROR Failure **
** TCL_OK Successful operation **
** **
** Attached Globals: g_flags Controllig the callback functions **
** **
** ************************************************************************ **
++++*/
int ModuleCmd_Use(
Tcl_Interp * interp,
int argc,
char *argv[]
) {
char *pathargv[4]; /** Argument buffer for Tcl calls **/
Tcl_Obj **objv; /** Tcl Object vector **/
int objc, /** Tcl Object vector count **/
i;
/**
** Parameter check. Usage is 'module use <path> [ <path> ... ]'
**/
if (argc < 1) {
if (OK !=
ErrorLogger(ERR_USAGE, LOC,
"use [-a|--append] dir [dir ...]", NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
}
/**
** Remove is done by another subroutine
**/
if (g_flags & M_REMOVE)
return (ModuleCmd_UnUse(interp, argc, argv));
/**
** Append or prepend the new module path
**/
if (append_flag) {
pathargv[0] = "append-path";
} else {
pathargv[0] = "prepend-path";
}
/**
** Append (prepend) all passed paths to MODULEPATH in case they do exist,
** and are readable directories
**/
pathargv[1] = "MODULEPATH";
pathargv[3] = NULL;
for (i = 0; i < argc; i++) {
/**
** Check for -a|--append flag (if in modulefile - it's not parsed
** by getoptlong) (keep -append for backward compatibility)
**/
if ((!strcmp("-a", argv[i])) || (!strcmp("--append", argv[i]))
|| (!strcmp("-append", argv[i]))) {
pathargv[0] = "append-path";
append_flag = 1;
continue;
} else if (!is_("dir", argv[i])) {
/**
** Check for existing, readable directories
**/
if (OK !=
ErrorLogger(ERR_DIRNOTFOUND, LOC, argv[i], NULL))
return (TCL_ERROR); /** -- EXIT (FAILURE) -> **/
}
/**
** check if given dir is not already present in MODULEPATH
**/
if (ModulePathVec && (uvec_find(ModulePathVec, argv[i], UVEC_ASCEND) >= 0)) {
continue;
}
/**
** indirect check if MODULEPATH exist
** it may makes sense to have module use abble to deal
** with initial empty MODULEPATH
** value
**/
if (!ModulePathVec) {
int n;
ModulePathVec = SplitIntoList("", &n, _colon);
}
/**
** Used the 'cmdSetPath' callback function to modify the MODULEPATH
** and add path to internal ModulePath vector for subsequent searches
**/
if (append_flag)
uvec_unshift(ModulePathVec,argv[i]);
else
uvec_push(ModulePathVec,argv[i]);
/**
** set ModulePath accordingly to the modification
** it is used by Locate_Module
**/
ModulePath = uvec_vector(ModulePathVec);
pathargv[2] = argv[i];
/* convert from argv to objv */
Tcl_ArgvToObjv(&objc, &objv, -1, (char **) pathargv);
if (cmdSetPath((ClientData) 0, interp, objc, objv) == TCL_ERROR)
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
} /** for **/
/**
** Add the new value of MODULESPATH to the end
** of the beginenvcache so that update will be able to find its
** modulefiles.
**/
append_to_modulesbeginenv(interp, "MODULEPATH");
return (TCL_OK);
} /** End of 'ModuleCmd_Use' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: ModuleCmd_UnUse **
** **
** Description: Execution of the module-command 'unuse' **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: Tcl_Interp *interp Attached Tcl Interp. **
** int argc Number of arguments **
** char *argv[] Argument list **
** **
** Result: int TCL_ERROR Failure **
** TCL_OK Successful operation **
** **
** Attached Globals: - **
** **
** ************************************************************************ **
++++*/
int ModuleCmd_UnUse(
Tcl_Interp * interp,
int argc,
char *argv[]
) {
char *pathargv[4]; /** Argument buffer for Tcl calls **/
Tcl_Obj **objv; /** Tcl Object vector **/
int objc, /** Tcl Object vector count **/
i = 0;
/**
** Parameter check. Usage is 'module use <path> [ <path> ... ]'
**/
if (argc < 1) {
if (OK != ErrorLogger(ERR_USAGE,LOC,"unuse dir [dir ...]",NULL))
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
}
/**
** Remove all passed paths from MODULEPATH
** Use the 'cmdSetPath' callback function to modify the MODULEPATH
** Don't bother with ModulePath vector for delayed modulefile removals
**/
pathargv[0] = "remove-path";
pathargv[1] = "MODULEPATH";
pathargv[3] = NULL;
for (i = 0; i < argc; i++) {
/**
** check for existance of the given path in MODULEPATH
**/
if (ModulePathVec && (uvec_find(ModulePathVec, argv[i], UVEC_ASCEND) == -1)) {
g_retval +=1;
continue;
}
if (NULL == argv[i]) continue;
pathargv[2] = argv[i];
/* convert from argv to objv */
Tcl_ArgvToObjv(&objc, &objv, -1, (char **) pathargv);
if (cmdRemovePath((ClientData) 0,interp,objc,objv) == TCL_ERROR)
return (TCL_ERROR); /** ------ EXIT (FAILURE) -----> **/
} /** for **/
/**
** Add the new value of MODULESPATH to the end
** of the beginenvcache so that update will be able to find its
** modulefiles.
**/
append_to_modulesbeginenv(interp, "MODULEPATH");
/**
** update ModulePath accordingly to the modification
**/
ModulePath = uvec_vector(ModulePathVec);
return (TCL_OK);
} /** End of 'ModuleCmd_UnUse' **/