1    | /*****
2    |  ** ** Module Header ******************************************************* **
3    |  ** 									     **
4    |  **   Modules Revision 3.0						     **
5    |  **   Providing a flexible user environment				     **
6    |  ** 									     **
7    |  **   File:		ModuleCmd_List.c				     **
8    |  **   First Edition:	1991/10/23					     **
9    |  ** 									     **
10   |  **   Authors:	John Furlan, jlf@behere.com				     **
11   |  **		Jens Hamisch, jens@Strawberry.COM			     **
12   |  ** 									     **
13   |  **   Description:	Lists the currently loaded modulefiles.		     **
14   |  ** 									     **
15   |  **   Exports:		ModuleCmd_List					     **
16   |  ** 									     **
17   |  **   Notes:								     **
18   |  ** 									     **
19   |  ** ************************************************************************ **
20   |  ****/
21   | 
22   | /** ** Copyright *********************************************************** **
23   |  ** 									     **
24   |  ** Copyright 1991-1994 by John L. Furlan.                      	     **
25   |  ** see LICENSE.GPL, which must be provided, for details		     **
26   |  ** 									     ** 
27   |  ** ************************************************************************ **/
28   | 
29   | static char Id[] = "@(#)$Id: ModuleCmd_List.c.src.html,v 1.6 2006/01/18 05:35:11 rkowen Exp $";
30   | static void *UseId[] = { &UseId, Id };
31   | 
32   | /** ************************************************************************ **/
33   | /** 				      HEADERS				     **/
34   | /** ************************************************************************ **/
35   | 
36   | #include "modules_def.h"
37   | 
38   | /** ************************************************************************ **/
39   | /** 				  LOCAL DATATYPES			     **/
40   | /** ************************************************************************ **/
41   | 
42   | /** not applicable **/
43   | 
44   | /** ************************************************************************ **/
45   | /** 				     CONSTANTS				     **/
46   | /** ************************************************************************ **/
47   | 
48   | /** not applicable **/
49   | 
50   | /** ************************************************************************ **/
51   | /**				      MACROS				     **/
52   | /** ************************************************************************ **/
53   | 
54   | /** not applicable **/
55   | 
56   | /** ************************************************************************ **/
57   | /** 				    LOCAL DATA				     **/
58   | /** ************************************************************************ **/
59   | 
60   | static	char	module_name[] = "ModuleCmd_List.c";	/** File name of this module **/
61   | static	char	_proc_ModuleCmd_List[] = "ModuleCmd_List";
62   | 
63   | /** ************************************************************************ **/
64   | /**				    PROTOTYPES				     **/
65   | /** ************************************************************************ **/
66   | 
67   | /** not applicable **/
68   | 
69   | 
70   | /*++++
71   |  ** ** Function-Header ***************************************************** **
72   |  ** 									     **
73   |  **   Function:		ModuleCmd_List					     **
74   |  ** 									     **
75   |  **   Description:	Execution of the module-command 'list'		     **
76   |  **			Lists all modules stored in the environment variable **
77   |  **			'LOADEDMODULES'					     **
78   |  ** 									     **
79   |  **   First Edition:	1991/10/23					     **
80   |  ** 									     **
81   |  **   Parameters:	Tcl_Interp	*interp		Attached Tcl Interp. **
82   |  **			int		 argc		Number of args	     **
83   |  **			char 		*argv[]		Argument list	     **
84   |  ** 									     **
85   |  **   Result:		int	TCL_ERROR	Failure			     **
86   |  **				TCL_OK		Successfull operation	     **
87   |  ** 									     **
88   |  **   Attached Globals:							     **
89   |  ** 									     **
90   |  ** ************************************************************************ **
91   |  ++++*/
92   | 
93   | int	ModuleCmd_List(	Tcl_Interp	*interp,
94   | 			int		 argc,
95   |                    	char		*argv[])
96   | {
97   |     /**
98   |      **  Get the list of loaded modules at first
99   |      **/
100  | 
101  |     char	*loaded, *lmfiles;
102  |     int		 i, count1, count2;
103  |     char	*list[ MOD_BUFSIZE];
104  |     char	*files[ MOD_BUFSIZE];
105  |     char	*tmplist[ MOD_BUFSIZE], *s;
106  |     int	  	 len;
107  | 
108  | #if WITH_DEBUGGING_MODULECMD
109  |     ErrorLogger( NO_ERR_START, LOC, _proc_ModuleCmd_List, NULL);
110  | #endif
111  | 
112  |     lmfiles = getLMFILES( interp);
113  |     loaded = getenv( "LOADEDMODULES");
114  |     
115  |     if( !loaded || !*loaded) {
116  | 	if( sw_format & (SW_TERSE | SW_LONG | SW_HUMAN) )
117  | 	    fprintf(stderr, "No Modulefiles Currently Loaded.\n");
118  |     } else {
119  | 
120  | 	/**
121  | 	 **  Now tokenize it, form a list and print it out.
122  | 	 **/
123  | 
124  | 	if( sw_format & SW_LONG ) {
125  | 	    fprintf( stderr, long_header);
126  | 	}
127  | 	if( sw_format & (SW_TERSE | SW_LONG | SW_HUMAN) )
128  | 	    fprintf( stderr, "Currently Loaded Modulefiles:\n");
129  | 
130  | 	/**
131  | 	 **  LOADEDMODULES and _LMFILES_ should provide a list of loaded
132  | 	 **  modules and assigned files in the SAME ORDER
133  | 	 ** but double check, because if they aren't you will get a crash.
134  | 	 **/
135  | 
136  | 	count1 = 1;
137  |         for( list[ 0] = strtok( loaded, ":");
138  | 	     list[ count1] = strtok( NULL, ":");
139  | 	     count1++ );
140  | 
141  | 	count2 = 1;
142  |         for( files[ 0] = strtok( lmfiles, ":");
143  | 	     files[ count2] = strtok( NULL, ":");
144  | 	     count2++ );
145  | 	if (count1 != count2) {
146  | 	  ErrorLogger( ERR_ENVVAR, LOC, NULL);
147  | 	}
148  | 	  
149  | 
150  | 	/**
151  | 	 **  We have to build a single list of files for each loaded entry
152  | 	 **  in order to be able to figure out the length of the directory
153  | 	 **  part
154  | 	 **/
155  | 
156  | 	for( i=0; i<count1; i++) {
157  | 
158  | 	    len = strlen( files[i]) - strlen( list[i]);
159  | 	    tmplist[i] = files[i];
160  | 
161  | 	    /**
162  | 	     **  We have to source all relevant .modulerc and .version files
163  | 	     **  on the path
164  | 	     **/
165  | 
166  | 	    s = files[i] + len;
167  | 	    while( s) {
168  | 		if( s = strchr( s, '/'))
169  | 		    *s = '\0';
170  | 
171  | 		SourceRC( interp, files[i], modulerc_file);
172  | 		SourceVers( interp, files[i], list[i]);
173  | 
174  | 		if( s)
175  | 		    *s++ = '/';
176  | 	    }
177  | 
178  | 	    /** 
179  | 	     **  Print this guy
180  | 	     **/
181  | 	}
182  | 	print_aligned_files( interp, NULL, NULL, tmplist, count1, 1);
183  |     }
184  | 
185  |     /**
186  |      **  Return on success
187  |      **/
188  | 
189  | #if WITH_DEBUGGING_MODULECMD
190  |     ErrorLogger( NO_ERR_END, LOC, _proc_ModuleCmd_List, NULL);
191  | #endif
192  | 
193  |     return( TCL_OK);
194  | 
195  | } /** End of 'ModuleCmd_List' **/