~ubuntu-branches/ubuntu/utopic/modules/utopic-proposed

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
/*****
 ** ** Module Header ******************************************************* **
 ** 									     **
 **   Modules Revision 3.0						     **
 **   Providing a flexible user environment				     **
 ** 									     **
 **   File:		ModuleCmd_Use.c					     **
 **   First Edition:	1991/10/23					     **
 ** 									     **
 **   Authors:	John Furlan, jlf@behere.com				     **
 **		Jens Hamisch, jens@Strawberry.COM			     **
 ** 									     **
 **   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: af24837bb2adc1beba4276c79f510b5bf7e82331 $";
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[] = "ModuleCmd_Use.c";	/** File name of this module **/
#if WITH_DEBUGGING_UTIL_1
static	char	_proc_append_to_modulesbeginenv[] = "append_to_modulesbeginenv";
#endif
#if WITH_DEBUGGING_MODULECMD
static	char	_proc_ModuleCmd_Use[] = "ModuleCmd_Use";
static	char	_proc_ModuleCmd_UnUse[] = "ModuleCmd_UnUse";
#endif

/** ************************************************************************ **/
/**				    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 WITH_DEBUGGING_UTIL_1
    ErrorLogger( NO_ERR_START, LOC, _proc_append_to_modulesbeginenv, NULL);
#endif

    if(	var
#if BEGINENV == 99
	&& Tcl_GetVar2( interp,"env","MODULESBEGINENV", TCL_GLOBAL_ONLY)
#endif
	) {

#if WITH_DEBUGGING_UTIL_1
    ErrorLogger( NO_ERR_DEBUG, LOC, "Adding '", var, "'");
#endif

	/**
	 **  Get filename and the value of the passed variable
	 **/

	filename = EMGetEnv( interp, "_MODULESBEGINENV_");
	if(filename && *filename) {
	    val = 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, "appending", NULL);
	    }

	} /** if( get filename) **/
	null_free((void *)&filename);
    } /** if( var passed) **/

} /** 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[])
{
    struct stat	 stats;			/** Buffer for the stat() systemcall **/
    char	*pathargv[4];		/** Argument buffer for Tcl calls    **/
    int		 i;
  
    /**
     **  Parameter check. Usage is 'module use <path> [ <path> ... ]'
     **/

#if WITH_DEBUGGING_MODULECMD
    ErrorLogger( NO_ERR_START, LOC, _proc_ModuleCmd_Use, NULL);
#endif

    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";
		continue;

	} else if( stat( argv[i], &stats) < 0) {
	/**
	 **  Check for existing, readable directories
	 **/
	    if( OK != ErrorLogger( ERR_DIRNOTFOUND, LOC, argv[i], NULL))
		return( TCL_ERROR);	/** -------- EXIT (FAILURE) -------> **/
	} else if( !S_ISDIR( stats.st_mode))  {
	    if( OK != ErrorLogger( ERR_NODIR, LOC, argv[i], NULL))
		return( TCL_ERROR);	/** -------- EXIT (FAILURE) -------> **/
	}

	/**
	 **  Used the 'cmdSetPath' callback function to modify the MODULEPATH
	 **/

	pathargv[2] = argv[i];

	if( cmdSetPath((ClientData) 0, interp, 3, (CONST84 char **) pathargv)
	== 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");

#if WITH_DEBUGGING_MODULECMD
    ErrorLogger( NO_ERR_END, LOC, _proc_ModuleCmd_Use, NULL);
#endif

    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];
    int		 i = 0;
  
#if WITH_DEBUGGING_MODULECMD
    ErrorLogger( NO_ERR_START, LOC, _proc_ModuleCmd_UnUse, NULL);
#endif

    /**
     **  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
     **/

    pathargv[0] = "remove-path";
    pathargv[1] = "MODULEPATH";
    pathargv[3] = NULL;

    for(i = 0; i < argc; i++) {
	pathargv[2] = argv[i];
	if(cmdRemovePath((ClientData) 0, interp, 3, (CONST84 char **) pathargv)
	== 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");

#if WITH_DEBUGGING_MODULECMD
    ErrorLogger( NO_ERR_END, LOC, _proc_ModuleCmd_UnUse, NULL);
#endif

    return( TCL_OK);
  
} /** End of 'ModuleCmd_UnUse' **/