-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleCmd_Whatis.c
413 lines (351 loc) · 11.7 KB
/
ModuleCmd_Whatis.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
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: ModuleCmd_Whatis.c **
** First Edition: 1995/12/31 **
** **
** Authors: Jens Hamisch, [email protected] **
** R.K. Owen, <[email protected]> or <[email protected]> **
** **
** ModuleCmd_Apropos **
** ModuleCmd_Whatis **
** **
** Notes: **
** **
** ************************************************************************ **
****/
/** ** Copyright *********************************************************** **
** **
** Copyright 1991-1994 by John L. Furlan. **
** see LICENSE.GPL, which must be provided, for details **
** **
** ************************************************************************ **/
#define _GNU_SOURCE 1
static char Id[] = "@(#)$Id$";
static void *UseId[] = { &UseId, Id };
/** ************************************************************************ **/
/** HEADERS **/
/** ************************************************************************ **/
#include "modules_def.h"
/** ************************************************************************ **/
/** LOCAL DATATYPES **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** CONSTANTS **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** MACROS **/
/** ************************************************************************ **/
#define WHATIS_SOME 0
#define WHATIS_ALL 1
/** ************************************************************************ **/
/** LOCAL DATA **/
/** ************************************************************************ **/
static char module_name[] = __FILE__;
/** ************************************************************************ **/
/** PROTOTYPES **/
/** ************************************************************************ **/
static int whatis_dir( char*, int, char**, int);
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: ModuleCmd_Whatis **
** **
** Description: Display the passed modules 'whatis' information **
** **
** First Edition: 1995/12/31 **
** **
** 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 These are set up accordingly before **
** this function is called in order to **
** control everything **
** g_current_module The module which is handled **
** by the current command **
** **
** ************************************************************************ **
++++*/
int ModuleCmd_Whatis(
Tcl_Interp * interp,
int argc,
char *argv[]
) {
Tcl_Interp *whatis_interp;
Tcl_DString cmdbuf; /** dynamic string buffer **/
int i, /** loop index **/
result = TCL_OK; /** result (default OK **/
char modulefile[MOD_BUFSIZE],/** buffer for modulefile **/
modulename[MOD_BUFSIZE],/** buffer for modulename **/
**wptr, /** whatis text line **/
**dirname; /** modulepath dir **/
/**
** Initialize the command buffer and set up the modules flag to
** 'whatisonly'
**/
Tcl_DStringInit(&cmdbuf);
g_flags |= M_WHATIS;
/**
** Handle each passed module file. Create a Tcl interpreter for each
** module file to be handled and initialize it with custom module commands
**/
if (argc) {
/**
** User provided a list of modules for ``whatis'' info
**/
for (i = 0; i < argc && argv[i]; i++) {
whatis_interp = EM_CreateInterp();
if (TCL_OK != (result = Module_Init(whatis_interp))) {
EM_DeleteInterp(whatis_interp);
result = TCL_ERROR;
break;
}
/**
** locate the filename related to the passed module
**/
if (TCL_ERROR ==
Locate_ModuleFile(whatis_interp, argv[i],
modulename, modulefile)) {
EM_DeleteInterp(whatis_interp);
g_retval =1;
if (OK !=
ErrorLogger(ERR_LOCATE, LOC, argv[i], NULL))
break;
else
continue;
}
/**
** Print out everything that would happen if the module file were
** executed ...
**/
g_current_module = modulename;
cmdModuleWhatisInit();
result = CallModuleProcedure(whatis_interp, &cmdbuf,
modulefile,
"ModulesWhatis", 0);
/**
** Print the result ...
**/
if (whatis) {
wptr = whatis;
while (*wptr)
fprintf(stderr, "%-21s: %s\n", argv[i],
*wptr++);
}
/**
** Remove the Tcl interpreter that has been used for printing ...
**/
EM_DeleteInterp(whatis_interp);
cmdModuleWhatisShut();
}
/** for **/
} else {
/**
** User wants all module ``whatis'' info
**/
/**
** Load the MODULEPATH and split it into a list of paths.
** Assume success if no list is to be displayed ...
**/
if (!uvec_number(ModulePathVec))
goto success0;
/**
** Scan all the files
**/
dirname = ModulePath;
while (dirname && *dirname) {
if (check_dir(*dirname))
whatis_dir(*dirname, argc, argv, WHATIS_ALL);
dirname++;
}
}
/**
** Leave the 'whatis only mode', free up what has been used and return
**/
g_flags &= ~M_WHATIS;
fprintf(stderr, "\n");
Tcl_DStringFree(&cmdbuf);
/**
** Return on success
**/
success0:
return (TCL_OK); /** --- EXIT PROCEDURE (result) --> **/
unwind0:
return (TCL_ERROR); /** --- EXIT PROCEDURE (FAILURE) --> **/
} /** End of 'ModuleCmd_Whatis' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: ModuleCmd_Apropos **
** **
** Description: Scan the whatis database in order to find something **
** matching the passed strings **
** **
** First Edition: 1995/12/31 **
** **
** 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_Apropos(
Tcl_Interp * interp,
int argc,
char *argv[]
) {
char **dirname; /** modulepath dir **/
/**
** Load the MODULEPATH and split it into a list of paths.
** Assume success if no list is to be displayed ...
**/
if (!uvec_number(ModulePathVec))
goto success0;
dirname = ModulePath;
while (dirname && *dirname) {
if (!check_dir(*dirname))
continue;
whatis_dir(*dirname++, argc, argv, WHATIS_SOME);
}
success0:
return (TCL_OK);
unwind0:
return (TCL_ERROR); /** ---- EXIT (FAILURE) ---> **/
} /** End of 'ModuleCmd_Apropos' **/
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: whatis_dir **
** **
** Description: Print all files beyond the passed directory **
** **
** First Edition: 1991/10/23 **
** **
** Parameters: char *dir Directory to be scanned **
** int argc Number of tokens **
** char **argv List of tokens to check **
** **
** Result: int TCL_OK Successful operation **
** **
** Attached Globals: g_flags These are set up accordingly before **
** this function is called in order to **
** control everything **
** g_current_module The module which is handled **
** by the current command **
** **
** ************************************************************************ **
++++*/
static int whatis_dir( char *dir, int argc, char **argv,
int whatis_list)
{
fi_ent *dirlst_head = NULL; /** Directory list base pointer **/
int count = 0; /** Number of elements in the top **/
/** level directory list **/
int tcount = 0; /** Total number of files to print **/
char **list; /** flat list of module files **/
int start = 0, i, k;
int result = TCL_OK;
Tcl_Interp *whatis_interp;
Tcl_DString cmdbuf;
char modulefile[ MOD_BUFSIZE];
char **wptr, *c;
char (*str_cmp) (const char *, const char *);
/**
** Ignore case ?
**/
if (sw_icase) str_cmp = &strcasestr;
else str_cmp = &strstr;
/**
** Normal reading of the files
**/
if(!(dirlst_head = get_dir( dir, NULL, &count, &tcount)))
if( OK != ErrorLogger( ERR_READDIR, LOC, dir, NULL))
goto unwind0;
if(!(list = (char**) module_malloc( tcount * sizeof( char**))))
if( OK != ErrorLogger( ERR_ALLOC, LOC, NULL))
goto unwind1;
dirlst_to_list( list, dirlst_head, count, &start, NULL, NULL);
/**
** Initialize the command buffer and set up the modules flag to 'whatislay
** only'
**/
Tcl_DStringInit( &cmdbuf);
g_flags |= M_WHATIS;
/**
** Check all the files in the flat list for the passed tokens
**/
for( i=0; i<tcount; i++) {
whatis_interp = EM_CreateInterp();
if( TCL_OK != (result = Module_Init( whatis_interp))) {
EM_DeleteInterp( whatis_interp);
result = TCL_ERROR;
break; /** for( i) **/
}
/**
** locate the filename related to the passed module
**/
if(!stringer(modulefile,MOD_BUFSIZE, dir,psep,list[i],NULL)) {
result = TCL_ERROR;
break; /** for( i) **/
}
g_current_module = list[ i];
if(is_("dir", modulefile))
continue;
cmdModuleWhatisInit();
result = CallModuleProcedure( whatis_interp, &cmdbuf, modulefile,
"ModulesApropos", 0);
/**
** Check if at least one of the passed tokens is found in the
** retrieved whatis strings. If yes, print the string.
**/
if( whatis) {
wptr = whatis;
while( *wptr) {
c = *wptr;
/**
** Seek for the passed tokens
**/
if( whatis_list)
fprintf( stderr, "%-21s: %s\n", list[i], *wptr);
else
for( k=0; k<argc; k++) {
if( str_cmp( c, argv[ k]))
fprintf( stderr, "%-21s: %s\n", list[i], *wptr);
}
wptr++;
}
}
/**
** Remove the Tcl interpreter that has been used for printing ...
**/
EM_DeleteInterp( whatis_interp);
cmdModuleWhatisShut();
} /** for( i) **/
/**
** Cleanup
**/
g_flags &= ~M_WHATIS;
delete_dirlst( dirlst_head, count);
delete_cache_list( list, start);
return( result); /** ------- EXIT (result) --------> **/
/* unwind2:
delete_cache_list( list, start); */
unwind1:
delete_dirlst( dirlst_head, count);
unwind0:
return( TCL_ERROR); /** ------- EXIT (FAILURE) --------> **/
} /** End of 'whatis_dir' **/