-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModuleCmd_Init.c
529 lines (462 loc) · 15.1 KB
/
ModuleCmd_Init.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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
/*****
** ** Module Header ******************************************************* **
** **
** Modules Revision 3.0 **
** Providing a flexible user environment **
** **
** File: ModuleCmd_Init.c **
** First Edition: 1991/10/23 **
** **
** Authors: John Furlan, [email protected] **
** Jens Hamisch, [email protected] **
** R.K. Owen, <[email protected]> or <[email protected]> **
** **
** Description: Routines that act on a user's "dot" startup files to **
** add, remove, and list modulefiles to/from/in their **
** startup files. **
** **
** Exports: ModuleCmd_Init **
** **
** 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 **/
/** ************************************************************************ **/
/** not applicable **/
/** ************************************************************************ **/
/** STATIC FUNCTIONS **/
/** ************************************************************************ **/
/* Handles the output of a substring where the start & ending positions
* are given - if either is NULL then just do nothing and return -1
* all other cases it returns 0
*/
static int out_substr(FILE *stream, char *start, char *end) {
char save;
if (!start || !end) return -1;
save = *end;
*end = '\0';
fputs(start, stream);
*end = save;
return 0;
}
/*++++
** ** Function-Header ***************************************************** **
** **
** Function: ModuleCmd_Init **
** **
** Description: Execution of the module-command 'init' **
** **
** 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 These are set up accordingly before **
** this function is called in order to **
** control everything **
** **
** ************************************************************************ **
++++*/
int ModuleCmd_Init( Tcl_Interp *interp,
int argc,
char *argv[])
{
char *home_pathname,
*home_pathname2,
**shell_startups; /** A list of all startup files our **/
/** invoking shell will source **/
char **modlist,
*home,
*buffer,
ch,
*startp, *endp,
*Modcmd =
"^([ \t]*module[ \t]+)(load|add)[ \t]+([^#\n]*)([#.\n]*)";
static Tcl_Obj *modcmdObj;
static Tcl_RegExp modcmdPtr;
FILE *fileptr, *newfileptr;
int i, j,
found_module_command = 0,
found_modload_flag = 0,
shell_num = 0,
final_list_num = 0,
nummods, bufsiz = 8192,
new_file,
homelen, home_end, path_end;
/**
** If called with no arguments and the flags don't say that there's some-
** thing to do - exit now!
**/
if (argc < 1 && !(g_flags & (M_DISPLAY | M_CLEAR)))
goto success0;
if (!modcmdObj)
modcmdObj = Tcl_NewStringObj(Modcmd,strlen(Modcmd));
if (!modcmdPtr)
modcmdPtr = Tcl_GetRegExpFromObj(interp,modcmdObj,TCL_REG_ADVANCED);
/**
** Parameter check for the initswitch command
**/
if (g_flags & M_SWITCH) {
argc--;
if (argc != 1)
if (OK != ErrorLogger(ERR_USAGE, LOC,
"initswitch oldmodule newmodule", NULL))
goto unwind0;
}
/**
** Where's my HOME?
**/
if (!(home = (char *) getenv("HOME")))
if (OK != ErrorLogger(ERR_HOME, LOC, NULL))
goto unwind1;
/**
** Put HOME into a buffer and store a slash where the end of HOME is
** for quick concatination of the shell startup files.
**/
homelen = strlen(home) + 40;
if (!(home_pathname = stringer(NULL, homelen, home, psep, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind0;
if (!(home_pathname2 = stringer(NULL, homelen, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind1;
home_end = strlen(home_pathname);
/**
** Allocate a buffer for fgets ...
**/
if (!(buffer = stringer(NULL, bufsiz, NULL)))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind2;
/**
** Scan all startup files related to the current invoking shell
**/
if (!(shell_startups = SetStartupFiles(shell_name)))
goto unwind3;
while (shell_startups[shell_num]) {
new_file = 1;
found_modload_flag = 0;
if (!stringer(home_pathname + home_end, 40,
shell_startups[shell_num], NULL))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
if (!(fileptr = fopen(home_pathname, "r")))
goto unwhile0; /** while( shell_startups) ... **/
/**
** ... when the startup file exists ...
** open a new startupfile with the extension -NEW for output
**/
path_end = strlen(home_pathname);
if (!stringer(home_pathname + path_end,
homelen - path_end, "-NEW", NULL))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
if (!(g_flags & M_DISPLAY) &&
(!(newfileptr = fopen(home_pathname, "w")))) {
(void) ErrorLogger(ERR_OPEN, LOC, home_pathname,
_(em_writing), NULL);
goto unwhile0; /** while( shell_startups) ... **/
}
/**
** Seek for a modules load|add command within the shell startup file
** Copy the shell input file to the new one until the magic cookie
** is found.
**/
while (fgets(buffer, bufsiz, fileptr)) {
if (Tcl_RegExpExec(interp, modcmdPtr, buffer, buffer)) {
found_modload_flag = 1;
/**
** ... module load|add found ...
**/
found_module_command = 1;
/* print out the "module" part */
(void) Tcl_RegExpRange(modcmdPtr, 1,
(CONST84 char **) &startp,
(CONST84 char **) &endp);
if (!(g_flags & M_DISPLAY))
(void) out_substr(newfileptr, startp, endp);
/* print out the "add/load" part */
(void) Tcl_RegExpRange(modcmdPtr, 2,
(CONST84 char **) &startp,
(CONST84 char **) &endp);
if (!(g_flags & M_DISPLAY))
(void) out_substr(newfileptr, startp, endp);
if (!(g_flags & M_CLEAR)) {
/* look at the "module list" part */
(void) Tcl_RegExpRange(modcmdPtr, 3,
(CONST84 char **) &startp,
(CONST84 char **) &endp);
/* save the end character & set to 0 */
if (endp) {
ch = *endp;
*endp = '\0';
}
if (!(modlist
= uvec_vector(SplitIntoList(startp, &nummods," \t"))))
continue; /** while(fgets) **/
/* restore the list end character */
if (endp)
*endp = ch;
if (g_flags & M_DISPLAY) {
if (!modlist[0]) {
fprintf(stderr,
/* TRANSLATORS: don't rearrange the arguments */
_("\nNo modules are loaded in %s's initialization file $HOME/%s\n"),
shell_name,shell_startups[shell_num]);
} else {
if (new_file) {
fprintf(stderr,
/* TRANSLATORS: don't rearrange the arguments */
_("\n%s initialization file $HOME/%s loads modules:\n\t"),
shell_name, shell_startups[shell_num]);
(void) out_substr(stderr, startp, endp);
fputs("\n",stderr);
new_file = 0;
} else {
fputs("\t",stderr);
(void) out_substr(stderr, startp, endp);
fputs("\n",stderr);
}
}
/* FreeList(modlist, nummods); */
continue; /** while(fgets) **/
}
for (i = 0; i < argc; i++) {
/**
** Search through the modlist of modules that are currently
** in the ~/.startup. If one is found, it handles removing
** it, switching it, etc.
**/
for (j = 0; j < nummods; j++) {
if (modlist[j] && !strcmp(modlist[j], argv[i])) {
if (g_flags & (M_LOAD | M_REMOVE)) {
/**
** If removing, adding, prepending it,
** NULL it off the list.
**/
if (g_flags & M_REMOVE)
fprintf(stderr, _("Removed %s\n"),
modlist[j]);
else if ((g_flags & M_LOAD)
&& !(g_flags & M_PREPEND))
fprintf(stderr, _("Moving %s to end\n"),
modlist[j]);
else if (g_flags & M_PREPEND)
fprintf(stderr,
_("Moving %s to beginning\n"),
modlist[j]);
null_free((void *) (modlist + j));
} else if (g_flags & M_SWITCH) {
/**
** If switching it, swap the old string with
** the new string in the list.
**/
/* TRANSLATORS: don't rearrange the arguments */
fprintf(stderr, _("Switching %s to %s\n"),
modlist[j], argv[i + 1]);
null_free((void *) (modlist + j));
modlist[j]=stringer(NULL,0,argv[i+1],NULL);
}
} /** if **/
} /** for(j) **/
} /** for(i) **/
/**
** Ok, if we're removing it, prepending it, or switching it,
** the modlist contains what needs to be put where...
**/
if ((new_file) && (g_flags & M_PREPEND)) {
/**
** PREPENDING
**/
for (i = 0; i < argc; i++) {
fprintf(newfileptr, " %s", argv[i]);
final_list_num++;
}
}
if ((g_flags & (M_LOAD | M_REMOVE | M_SWITCH))) {
/**
** DUMP LIST
**/
for (j = 0; j < nummods; j++) {
if (modlist[j]) {
fprintf(newfileptr, " %s", modlist[j]);
final_list_num++;
}
}
}
if ((new_file) && (g_flags & M_LOAD)
&& !(g_flags & M_PREPEND)) {
/**
** ADDING
**/
for (i = 0; i < argc; i++) {
fprintf(newfileptr, " %s", argv[i]);
final_list_num++;
}
}
/* always place a null if an empty list */
if (!final_list_num)
fprintf(newfileptr, " %s", "null");
/* FreeList(modlist, nummods); */
} else { /** if( M_CLEAR) **/
/**
** Clear out the list, but leave a "null"
**/
fprintf(newfileptr, " %s", "null");
}
/**
** Restore any comments at the end of the line...
**/
(void) Tcl_RegExpRange(modcmdPtr, 4,
(CONST84 char **) &startp,
(CONST84 char **) &endp);
(void) out_substr(newfileptr, startp, endp);
new_file = 0;
} else { /* not module load line */
if (!(g_flags & M_DISPLAY))
fputs(buffer, newfileptr);
}
} /** while (fgets) **/
if (g_flags & M_DISPLAY) {
fputs("\n",stderr);
}
if (!found_modload_flag) {
/**
** If not found...
**/
if (EOF == fclose(fileptr))
if (OK != ErrorLogger(ERR_CLOSE, LOC, home_pathname, NULL))
goto unwind3;
if (!(g_flags & M_DISPLAY)) {
if (EOF == fclose(newfileptr))
if (OK != ErrorLogger(ERR_CLOSE, LOC, home_pathname, NULL))
goto unwind3;
if (0 > unlink(home_pathname))
if (OK != ErrorLogger(ERR_UNLINK, LOC, home_pathname, NULL))
goto unwind3;
}
} else { /* found_modload_flag */
/**
** Don't need these any more
**/
if (EOF == fclose(fileptr))
if (OK != ErrorLogger(ERR_CLOSE, LOC, home_pathname, NULL))
goto unwind3;
if (g_flags & M_DISPLAY)
goto unwhile0; /** while( shell_startups) ... **/
if (EOF == fclose(newfileptr))
if (OK != ErrorLogger(ERR_CLOSE, LOC, home_pathname, NULL))
goto unwind3;
/**
** Truncate -NEW from home_pathname and Create a -OLD name
** Move ~/.startup to ~/.startup-OLD
**/
home_pathname[path_end] = '\0';
if (!stringer(home_pathname2, homelen,
home_pathname, "-OLD", NULL))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
if (0 > rename(home_pathname, home_pathname2))
if (OK !=
ErrorLogger(ERR_RENAME, LOC, home_pathname, home_pathname2,
NULL))
goto unwind3;
/**
** Create a -NEW name
** Move ~/.startup-NEW to ~/.startup
**/
if (!stringer(home_pathname2, homelen,
home_pathname, "-NEW", NULL))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
if (0 > rename(home_pathname2, home_pathname)) {
if (OK !=
ErrorLogger(ERR_RENAME, LOC, home_pathname2, home_pathname,
NULL)) {
/**
** Put the -OLD one back if I can't rename it
**/
if (!stringer(home_pathname2, homelen,
home_pathname, "-OLD", NULL))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
if (0 > rename(home_pathname2, home_pathname))
ErrorLogger(ERR_RENAME, LOC, home_pathname2,
home_pathname, NULL);
goto unwind3;
}
}
/**
** So far we're successful so
** Create a -OLD name
** Unlink ~/.startup-OLD
**/
if (!stringer(home_pathname2, homelen,
home_pathname, "-OLD", NULL))
if (OK != ErrorLogger(ERR_STRING, LOC, NULL))
goto unwind3;
if ((g_flags & (M_CLEAR | M_LOAD | M_REMOVE | M_SWITCH)))
if (0 > unlink(home_pathname2)) {
ErrorLogger(ERR_UNLINK, LOC, home_pathname2, NULL);
goto unwind3;
}
}
unwhile0:
shell_num++;
} /** while( shell_startups) **/
/**
** Free up internal I/O buffers
**/
null_free((void *) &buffer);
if (!found_module_command)
if (OK != ErrorLogger(ERR_INIT_STUP, LOC, shell_name, NULL))
goto unwind2;
/**
** Free up memory
**/
null_free((void *) &home_pathname2);
null_free((void *) &home_pathname);
success0:
return (TCL_OK); /** -------- EXIT (SUCCESS) -------> **/
unwind3:
null_free((void *) &buffer);
unwind2:
null_free((void *) &home_pathname2);
unwind1:
null_free((void *) &home_pathname);
unwind0:
return (TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/
} /** end of 'ModuleCmd_Init' **/