Debian Codegraph @

tcl/interp.tcl.in

raw
1 ##########################################################################
2
3 # INTERP.TCL, sub-interpreter management procedures
4 # Copyright (C) 2002-2004 Mark Lakata
5 # Copyright (C) 2004-2017 Kent Mein
6 # Copyright (C) 2016-2026 Xavier Delaruelle
7 #
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 2 of the License, or
11 # (at your option) any later version.
12 #
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20
21 ##########################################################################
22
23 # dummy proc to disable modulefile commands on some evaluation modes
24 proc nop {args} {}
25
26 # dummy proc for commands available on other Modules flavor but not here
27 proc nimp {cmd args} {
28 reportWarning "'$cmd' command not implemented"
29 }
30
31 proc createInterp {itrp} {
32 # set quarantine variables to their runtime value or unset them during
33 # interp creation to protect this initialization step from their current
34 # definition (e.g., a TCL_LIBRARY variable pointing at a broken Tcl
35 # installation)
36 set quar_var_list [getQuarantineVarList]
37 foreach var $quar_var_list {
38 if {[info exists ::env($var)]} {
39 set quar_orig_val($var) $::env($var)
40 }
41 if {[info exists ::env(MODULES_RUNENV_$var)]} {
42 reportDebug "Set '$var' environment variable in quarantine\
43 ($::env(MODULES_RUNENV_$var))"
44 set ::env($var) $::env(MODULES_RUNENV_$var)
45 } else {
46 reportDebug "Unset '$var' environment variable in quarantine"
47 unset -nocomplain ::env($var)
48 }
49 }
50
51 reportDebug "creating interp $itrp"
52 interp create $itrp
53
54 # restore environment variables put in quarantine once interp is created
55 foreach var $quar_var_list {
56 if {[info exists quar_orig_val($var)]} {
57 set ::env($var) $quar_orig_val($var)
58 } else {
59 unset -nocomplain ::env($var)
60 # also unset variable in created interp if it was set during
61 # creation: this env entry is not removed by the unset above and a
62 # read attempt on it would raise a "no such variable" error
63 ##nagelfar ignore Found constant
64 interp eval $itrp [list unset -nocomplain ::env($var)]
65 }
66 }
67 }
68
69 # Get identifier name of current Tcl modulefile interpreter. An interp is
70 # dedicated to each mode/auto_handling option value/depth level of modulefile
71 # interpretation
72 proc getCurrentModfileInterpName {} {
73 return __modfile_[currentState mode]_[getConf auto_handling]_[depthState\
74 modulename]
75 }
76
77 # Initialize list of interp alias commands to define for given evaluation mode
78 # and auto_handling enablement
79 proc initModfileModeAliases {mode auto aliasesVN aliasesPassArgVN\
80 tracesVN} {
81 global g_modfilePerModeAliases
82 upvar #0 $aliasesVN aliases
83 upvar #0 $aliasesPassArgVN aliasesPassArg
84 upvar #0 $tracesVN traces
85
86 if {![info exists g_modfilePerModeAliases]} {
87 set ::g_modfileBaseAliases [list versioncmp versioncmp getenv getenv\
88 getvariant getvariant is-loaded is-loaded is-saved is-saved is-used\
89 is-used is-avail is-avail uname uname module-info module-info\
90 modulepath-label modulepath-label exit exitModfileCmd reportCmdTrace\
91 reportCmdTrace reportWarning reportWarning reportError reportError\
92 incrErrorCount incrErrorCount report report isWin initStateIsWin\
93 puts putsModfileCmd getModuleContent getModuleContent lsb-release\
94 lsb-release getModuleHelpLines getModuleHelpLines append\
95 appendModfileCmd]
96
97 if {[getConf source_cache]} {
98 lappend ::g_modfileBaseAliases source sourceModfileCmd
99 }
100 if {[llength [getState linked_envvars]]} {
101 lappend ::g_modfileBaseAliases mirrorEnvVarChange mirrorEnvVarChange
102 }
103
104 # list of alias commands whose target procedure is adapted according to
105 # the evaluation mode
106 set ::g_modfileEvalModes {load unload display help test whatis refresh\
107 scan}
108 ##nagelfar ignore #50 Too long line
109 array set g_modfilePerModeAliases {
110 add-property {add-property nop reportCmd nop nop nop nop nop}
111 always-load {always-load nop reportCmd nop nop nop nop always-load-sc}
112 append-path {append-path append-path-un append-path append-path append-path edit-path-wh nop edit-path-sc}
113 chdir {chdir nop reportCmd nop nop nop nop chdir-sc }
114 complete {complete complete-un reportCmd nop nop nop complete complete-sc }
115 conflict {conflict nop reportCmd nop nop nop nop conflict-sc }
116 depends-on {prereqAllModfileCmd nop reportCmd nop nop nop nop prereq-all-sc}
117 depends-on-any {prereqAnyModfileCmd nop reportCmd nop nop nop nop prereq-sc }
118 extensions {extensions nop reportCmd nop nop nop nop extensions-sc}
119 family {family family-un reportCmd nop nop nop nop family-sc }
120 haveDynamicMPATH {nop nop nop nop nop nop nop nop }
121 hide-modulefile {hide-modulefile hide-modulefile hide-modulefile hide-modulefile hide-modulefile hide-modulefile nop nop }
122 hide-version {hide-modulefile hide-modulefile hide-modulefile hide-modulefile hide-modulefile hide-modulefile nop nop }
123 module {module module reportCmd nop nop nop nop module-sc }
124 module-alias {module-alias module-alias module-alias module-alias module-alias module-alias nop nop }
125 module-log {nimp nimp reportCmd nop nop nop nop nop }
126 module-trace {nimp nimp reportCmd nop nop nop nop nop }
127 module-user {nimp nimp reportCmd nop nop nop nop nop }
128 module-verbosity {nimp nimp reportCmd nop nop nop nop nop }
129 module-version {module-version module-version module-version module-version module-version module-version nop nop }
130 module-virtual {module-virtual module-virtual module-virtual module-virtual module-virtual module-virtual nop nop }
131 module-forbid {module-forbid module-forbid module-forbid module-forbid module-forbid module-forbid nop nop }
132 module-help {nop nop reportCmd module-help nop nop nop nop }
133 module-hide {module-hide module-hide module-hide module-hide module-hide module-hide nop nop }
134 module-tag {module-tag module-tag module-tag module-tag module-tag module-tag nop nop }
135 module-warn {module-warn module-warn module-warn module-warn module-warn module-warn nop nop }
136 module-whatis {nop nop reportCmd nop nop module-whatis nop nop }
137 modulepath-ignore {modulepath-ignore modulepath-ignore modulepath-ignore modulepath-ignore modulepath-ignore modulepath-ignore nop nop}
138 prepend-path {prepend-path prepend-path-un prepend-path prepend-path prepend-path edit-path-wh nop edit-path-sc}
139 prereq-all {prereqAllModfileCmd nop reportCmd nop nop nop nop prereq-all-sc}
140 prereq-any {prereqAnyModfileCmd nop reportCmd nop nop nop nop prereq-sc }
141 prereq {prereqAnyModfileCmd nop reportCmd nop nop nop nop prereq-sc }
142 provide {provide nop reportCmd nop nop nop nop provide-sc }
143 pushenv {pushenv pushenv-un pushenv pushenv pushenv pushenv-wh nop pushenv-sc }
144 remove-path {remove-path remove-path-un remove-path remove-path remove-path edit-path-wh nop edit-path-sc}
145 remove-property {nop nop nop nop nop nop nop nop }
146 require-fullname {require-fullname nop reportCmd nop nop nop nop nop }
147 set-alias {set-alias set-alias-un reportCmd nop nop nop set-alias set-alias-sc}
148 set-function {set-function set-function-un reportCmd nop nop nop set-function set-function-sc}
149 setenv {setenv setenv-un setenv setenv setenv setenv-wh nop setenv-sc }
150 source-sh {source-sh source-sh-un source-sh-di nop nop nop source-sh source-sh }
151 system {system system reportCmd nop nop nop nop nop }
152 unique-name-conflict {unique-name-conflict nop nop nop nop nop nop nop }
153 uncomplete {uncomplete nop reportCmd nop nop nop nop uncomplete-sc}
154 unset-alias {unset-alias nop reportCmd nop nop nop nop unset-alias-sc}
155 unset-function {unset-function nop reportCmd nop nop nop nop unset-function-sc}
156 unsetenv {unsetenv unsetenv-un unsetenv unsetenv unsetenv unsetenv-wh nop unsetenv-sc }
157 variant {variant variant variant variant variant variant-wh variant variant-sc }
158 x-resource {x-resource x-resource reportCmd nop nop nop nop nop }
159 }
160 }
161
162 # alias commands where interpreter ref should be passed as argument
163 array set aliasesPassArg [list getvariant [list __itrp__] puts [list\
164 __itrp__] variant [list __itrp__] source [list __itrp__] append [list\
165 __itrp__]]
166
167 # initialize list with all commands not dependent of the evaluation mode
168 array set aliases $::g_modfileBaseAliases
169
170 # add site-specific command aliases for modulefile interp
171 if {[info exists ::modulefile_extra_cmds]} {
172 if {[catch {array set aliases $::modulefile_extra_cmds} errorMsg]} {
173 knerror "Invalid value '$::modulefile_extra_cmds' ($errorMsg)\nfor\
174 siteconfig variable 'modulefile_extra_cmds'"
175 }
176 }
177
178 # add alias commands whose target command vary depending on the eval mode
179 set modeidx [lsearch -exact $::g_modfileEvalModes $mode]
180 foreach alias [array names g_modfilePerModeAliases] {
181 set aliastarget [set aliases($alias) [lindex\
182 $g_modfilePerModeAliases($alias) $modeidx]]
183 # some target procedures need command name as first arg
184 if {$aliastarget in {reportCmd nimp edit-path-wh edit-path-sc}} {
185 set aliasesPassArg($alias) [list $alias]
186 # prereq commands need auto_handling state as first arg
187 } elseif {$mode eq {load} && $alias in {prereq prereq-any prereq-all\
188 depends-on depends-on-any}} {
189 set aliasesPassArg($alias) [list 0 $auto]
190 # associate a trace command if per-mode alias command is not reportCmd
191 # in display mode (except for source-sh and unique-name-conflict)
192 } elseif {$mode eq {display} && $alias ni {source-sh\
193 unique-name-conflict}} {
194 lappend traces($alias) reportCmdTrace
195 }
196 # mirror environment variable changes if some links are defined
197 if {[llength [getState linked_envvars]] && $alias in {append-path\
198 prepend-path pushenv remove-path setenv unsetenv}} {
199 lappend traces($alias) mirrorEnvVarChange
200 }
201 }
202 }
203
204 proc execute-modulefile {modfile modname modnamevrvar modspec requested\
205 {up_namevr 1} {fetch_tags 1} {modpath {}}} {
206 # link to modnamevr variable name from calling ctx if content update asked
207 if {$up_namevr} {
208 upvar $modnamevrvar modnamevr
209 } else {
210 set modnamevr $modnamevrvar
211 }
212
213 lappendState modulefile $modfile
214 lappendState modulename $modname
215 lappendState modulenamevr $modnamevr
216 lappendState specifiedname $modspec
217 lappendState modulepath $modpath
218 set mode [currentState mode]
219 lappendState debug_msg_prefix\
220 "\[#[depthState modulename]:$mode:$modname\] "
221
222 # skip modulefile if interpretation has been inhibited
223 if {[getState inhibit_interp]} {
224 reportDebug "skipping $modfile"
225 return 1
226 }
227
228 reportTrace "'$modfile' as '$modname'" {Evaluate modulefile}
229
230 # reset modulefile-specific information
231 set ::g_help_lines {}
232
233 # gather all tags of evaluated modulefile
234 if {$fetch_tags} {
235 collectModuleTags $modnamevr
236 }
237
238 if {![info exists ::g_modfileUntrackVars]} {
239 # list variable that should not be tracked for saving
240 array set ::g_modfileUntrackVars [list ModulesCurrentModulefile 1 env 1]
241
242 # commands that should be renamed before aliases setup
243 array set ::g_modfileRenameCmds [list puts _puts append _append]
244 }
245 # dedicate an interpreter per mode and per level of interpretation to have
246 # a dedicated interpreter in case of cascaded multi-mode interpretations
247 set itrp [getCurrentModfileInterpName]
248
249 # evaluation mode-specific configuration
250 set autosuf [expr {[getConf auto_handling] ? {AH} : {}}]
251 set dumpCommandsVN g_modfile${mode}${autosuf}Commands
252 set aliasesVN g_modfile${mode}${autosuf}Aliases
253 set aliasesPassArgVN g_modfile${mode}${autosuf}AliasesPassArg
254 set tracesVN g_modfile${mode}${autosuf}Traces
255 ##nagelfar ignore Suspicious variable name
256 if {![info exists ::$aliasesVN]} {
257 ##nagelfar vartype aliasesVN varName
258 ##nagelfar vartype aliasesPassArgVN varName
259 ##nagelfar vartype tracesVN varName
260 initModfileModeAliases $mode [getConf auto_handling] $aliasesVN\
261 $aliasesPassArgVN $tracesVN
262 }
263
264 # variable to define in modulefile interp
265 if {![info exists ::g_modfileBaseVars]} {
266 # record module tool properties
267 set ::g_modfileBaseVars [list ModuleTool Modules ModuleToolVersion\
268 [getState modules_release]]
269
270 if {[info exists ::modulefile_extra_vars]} {
271 if {([llength $::modulefile_extra_vars] % 2) != 0} {
272 knerror "Invalid value '$::modulefile_extra_vars' (list must have\
273 an even number of elements)\nfor siteconfig variable\
274 'modulefile_extra_vars'"
275 }
276 foreach {var val} $::modulefile_extra_vars {
277 if {[string first { } $var] != -1} {
278 knerror "Invalid variable name '$var'\ndefined in siteconfig\
279 variable 'modulefile_extra_vars'"
280 }
281 }
282 lappend ::g_modfileBaseVars {*}$::modulefile_extra_vars
283 }
284 }
285
286 # create modulefile interpreter at first interpretation
287 if {![interp exists $itrp]} {
288 createInterp $itrp
289
290 # initialize global static variables for modulefile interp
291 foreach {var val} $::g_modfileBaseVars {
292 interp eval $itrp set ::$var "{$val}"
293 }
294
295 # dump initial interpreter state to restore it before each modulefile
296 # interpretation. use same dump state for all modes/levels
297 if {![info exists ::g_modfileVars]} {
298 dumpInterpState $itrp g_modfileVars g_modfileArrayVars\
299 g_modfileUntrackVars g_modfileProcs
300 }
301
302 # interp has just been created
303 set fresh 1
304 } else {
305 set fresh 0
306 }
307
308 # reset interp state command before each interpretation
309 resetInterpState $itrp $fresh g_modfileVars g_modfileArrayVars\
310 g_modfileUntrackVars g_modfileProcs $aliasesVN $aliasesPassArgVN\
311 $tracesVN g_modfileRenameCmds $dumpCommandsVN
312
313 runHooks before-modulefile-eval $modfile $modname $modnamevr $modspec\
314 $mode $requested
315
316 set vr_spec_list [getVariantListFromVersSpec $modnamevr]
317 set failed_eval [catch {evaluateModulefile $itrp $modfile $vr_spec_list}\
318 errorMsg]
319
320 set eval_return_code [renderModulefileEvalError $itrp $mode $modfile\
321 $failed_eval $errorMsg]
322
323 runHooks after-modulefile-eval $modfile $modname $modnamevr $modspec\
324 $mode $requested $eval_return_code
325
326 if {$mode eq {load} && ![isStateDefined rc_running]} {
327 if {[catch {checkModuleConflict $modname $modnamevr} errorMsg]} {
328 reportError $errorMsg
329 set eval_return_code 1
330 }
331 }
332
333 # check if mod name version and variant has changed (default variant set)
334 # update modnamevr if so and collect tags applying to new name
335 if {$up_namevr} {
336 set newmodnamevr "{$modname}"
337 if {[set vr [getVariantList $modname 1]] ne {}} {
338 append newmodnamevr " $vr"
339 }
340 if {$modnamevr ne $newmodnamevr} {
341 set modnamevr_tag_list [getTagList $modnamevr $modfile]
342 set modnamevr_extratag_list [getExtraTagList $modnamevr]
343 lassign [parseModuleSpecification 0 0 0 0 {*}$newmodnamevr] modnamevr
344 # $up_namevr is only enabled when $fetch_tags is also enabled
345 collectModuleTags $modnamevr
346 # set tags applying to previous name (without default variant set)
347 # not to forget extra defined tags
348 setModuleTag $modnamevr {*}$modnamevr_tag_list
349 setModuleExtraTag $modnamevr {*}$modnamevr_extratag_list
350 }
351 }
352
353 # check if special tags now applies and require to raise an error
354 if {$mode ne {unload}} {
355 if {[isModuleTagged $modnamevr forbidden 1 $modfile]} {
356 set eval_return_code 1
357 reportError [getForbiddenMsg $modnamevr $modfile]
358 }
359 }
360 if {$mode ni {unload refresh scan whatis}} {
361 if {[isModuleTagged $modnamevr nearly-forbidden 1 $modfile]} {
362 reportWarning [getNearlyForbiddenMsg $modnamevr $modfile]
363 }
364 if {[isModuleTagged $modnamevr warning 1 $modfile]} {
365 reportWarning [getWarningMsg $modnamevr $modfile]
366 }
367 }
368
369 # record all module evaluated in scan structure for negation pattern search
370 if {$mode eq {scan}} {
371 recordScanModuleElt modulename all
372 }
373
374 # skip evaluation log for internal evaluation mode
375 if {$mode ni {refresh scan whatis}} {
376 set log_info_list [list {mode} $mode module $modnamevr specified\
377 $modspec modulefile $modfile {requested} $requested]
378 set log_event [expr {$requested ? {requested_eval} : {auto_eval}}]
379 logEvent $log_event {*}$log_info_list
380 }
381
382 reportDebug "exiting $modfile"
383 lpopState debug_msg_prefix
384 lpopState modulepath
385 lpopState specifiedname
386 lpopState modulename
387 lpopState modulenamevr
388 lpopState modulefile
389
390 return $eval_return_code
391 }
392
393 proc evaluateModulefile {itrp mod_file vr_spec_list} {
394 # reset modulefile-specific variable before each interpretation
395 interp eval $itrp set ::ModulesCurrentModulefile "{$mod_file}"
396 interp eval $itrp set vrspeclist "{$vr_spec_list}"
397
398 # eval then call for specific proc depending mode under same catch
399 ##nagelfar ignore +3 Suspicious # char
400 interp eval $itrp {
401 info script $::ModulesCurrentModulefile
402 # raise conflict error if one name of currently loading module is shared
403 # by an already loaded module
404 unique-name-conflict
405 eval [getModuleContent $::ModulesCurrentModulefile]
406 # raise error if a variant specified is not defined in modulefile
407 set vrerrlist {}
408 foreach vrspec $vrspeclist {
409 set vrname [lindex $vrspec 0]
410 if {![info exists ::ModuleVariant($vrname)]} {
411 lappend vrerrlist "Unknown variant '$vrname' specified"
412 }
413 }
414 # report all unknown variants specified, raise error on last report
415 # take caution with vrerrlist variable as we are in mod_file eval ctx
416 if {[info exists vrerrlist] && [llength $vrerrlist]} {
417 for {set i 0} {$i < ([llength $vrerrlist] - 1)} {incr i} {
418 reportError [lindex $vrerrlist $i]
419 }
420 error [lindex $vrerrlist $i] {} MODULES_ERR_GLOBAL
421 }
422
423 switch -- [module-info mode] {
424 help {
425 foreach help_line [getModuleHelpLines] {
426 report $help_line
427 }
428 if {[info procs ModulesHelp] eq {ModulesHelp}} {
429 ModulesHelp
430 } elseif {![llength [getModuleHelpLines]]} {
431 reportWarning "Unable to find ModulesHelp in\
432 $::ModulesCurrentModulefile."
433 }
434 }
435 display {
436 if {[info procs ModulesDisplay] eq {ModulesDisplay}} {
437 ModulesDisplay
438 }
439 }
440 test {
441 if {[info procs ModulesTest] eq {ModulesTest}} {
442 if {[string is true -strict [ModulesTest]]} {
443 report {Test result: PASS}
444 } else {
445 report {Test result: FAIL}
446 incrErrorCount
447 }
448 } else {
449 reportWarning "Unable to find ModulesTest in\
450 $::ModulesCurrentModulefile."
451 }
452 }
453 }
454 }
455 }
456
457 proc renderModulefileEvalError {itrp mode mod_file failed_eval error_msg} {
458 if {!$failed_eval} {
459 return 0
460 }
461
462 set eval_return_code 1
463 # no error in case of "continue" command
464 # catch continue even if called outside of a loop
465 if {$error_msg eq {invoked "continue" outside of a loop} || $failed_eval\
466 == 4} {
467 set eval_return_code 0
468 unset error_msg
469 # catch break even if called outside of a loop
470 # on Darwin, error is different: no errorCode & return code set to 3
471 } elseif {$error_msg eq {invoked "break" outside of a loop} || ($error_msg\
472 eq {} && [getInterpVar $itrp ::errorInfo] eq {}) ||\
473 (![isInterpVarDefined $itrp ::errorCode] && $failed_eval == 3)} {
474 # report load/unload/refresh evaluation break if verbosity level
475 # >= normal, no error count raise during scan evaluation
476 if {$mode in {load unload refresh} && [isVerbosityLevel normal]} {
477 set error_msg {Module evaluation aborted}
478 } else {
479 unset error_msg
480 if {$mode ne {scan}} {
481 set raise_error_count 1
482 }
483 }
484 } else {
485 switch -- [getInterpVar $itrp errorCode] {
486 MODULES_ERR_READ {}
487 MODULES_ERR_VALIDITY {
488 set error_msg [formatMessageInModule $error_msg $mod_file]
489 set internal_bug 1
490 }
491 MODULES_ERR_SUBFAILED {
492 # error counter and message already handled, just return err code
493 unset error_msg
494 }
495 MODULES_ERR_GLOBAL {}
496 default {
497 set error_msg [formatInterpErrStackTrace $itrp $mod_file]
498 set internal_bug 1
499 }
500 }
501 }
502
503 # split force mode management code as non-unload modes still have a
504 # specific behavior (message returned as error and error exit code set)
505 if {$mode eq {unload}} {
506 if {[getState force]} {
507 set eval_return_code 0
508 }
509 if {[info exists error_msg]} {
510 if {[info exists internal_bug]} {
511 reportInternalBugOrWarningIfForced $error_msg
512 } else {
513 reportErrorOrWarningIfForced $error_msg
514 }
515 }
516 } else {
517 if {[info exists error_msg]} {
518 if {[info exists internal_bug]} {
519 reportInternalBug $error_msg
520 } else {
521 reportError $error_msg
522 }
523 } elseif {[info exists raise_error_count]} {
524 incrErrorCount
525 }
526 }
527
528 return $eval_return_code
529 }
530
531 # Raise an error if a conflict violation is detected. This is done after
532 # modulefile eval to give it a chance to solve its conflicts during eval
533 proc checkModuleConflict {mod_name mod_name_vr} {
534 set mod_con_list [getModuleLoadedConflict $mod_name]
535 if {[llength $mod_con_list]} {
536 set is_con_mod_conun [isModuleEvaluated conun $mod_name_vr {}\
537 {*}$mod_con_list]
538 set is_con_mod_loading [is-loading {*}$mod_con_list]
539 if {!$is_con_mod_conun || $is_con_mod_loading} {
540 set con_msg [getPresentConflictErrorMsg $mod_name_vr $mod_con_list\
541 $is_con_mod_loading]
542 } else {
543 set con_msg {}
544 }
545
546 set msgrecid [currentState msgrecordid]
547 if {![isConflictErrorAlreadyReported $msgrecid $mod_con_list]} {
548 knerrorOrWarningIfForced $con_msg
549 }
550 }
551 }
552
553 # Smaller subset than main module load... This function runs modulerc and
554 # .version files
555 proc execute-modulerc {modfile modname modspec} {
556 lappendState modulefile $modfile
557 # push name to be found by module-alias and version
558 lappendState modulename $modname
559 lappendState specifiedname $modspec
560 set ::ModulesVersion {}
561 lappendState debug_msg_prefix "\[#[depthState modulename]:$modname\] "
562
563 if {![info exists ::g_modrcUntrackVars]} {
564 # list variable that should not be tracked for saving
565 array set ::g_modrcUntrackVars [list ModulesCurrentModulefile 1\
566 ModulesVersion 1 env 1]
567
568 # commands that should be renamed before aliases setup
569 array set ::g_modrcRenameCmds [list]
570
571 # list interpreter alias commands to define
572 array set ::g_modrcAliases [list uname uname system system versioncmp\
573 versioncmp getenv getenv hide-modulefile hide-modulefile\
574 hide-version hide-modulefile is-loaded is-loaded is-used is-used\
575 module-version module-version module-alias module-alias\
576 module-virtual module-virtual module-forbid module-forbid\
577 module-hide module-hide module-tag module-tag module-info\
578 module-info modulepath-ignore modulepath-ignore modulepath-label\
579 modulepath-label setModulesVersion setModulesVersion\
580 getModuleContent getModuleContent lsb-release lsb-release\
581 module-warn module-warn]
582
583 if {[getConf source_cache]} {
584 set ::g_modrcAliases(source) sourceModfileCmd
585 }
586
587 # add site-specific command aliases for modulerc interp
588 if {[info exists ::modulerc_extra_cmds]} {
589 if {[catch {array set ::g_modrcAliases $::modulerc_extra_cmds}\
590 errorMsg]} {
591 knerror "Invalid value '$::modulerc_extra_cmds' ($errorMsg)\nfor\
592 siteconfig variable 'modulerc_extra_cmds'"
593 }
594 }
595
596 # alias commands where an argument should be passed
597 array set ::g_modrcAliasesPassArg [list source [list __itrp__]]
598
599 # trace commands that should be associated to aliases
600 array set ::g_modrcAliasesTraces [list]
601
602 # variable to define in modulerc interp
603 set ::g_modrcBaseVars [list ModuleTool Modules ModuleToolVersion\
604 [getState modules_release]]
605
606 if {[info exists ::modulerc_extra_vars]} {
607 if {([llength $::modulerc_extra_vars] % 2) != 0} {
608 knerror "Invalid value '$::modulerc_extra_vars' (list must have\
609 an even number of elements)\nfor siteconfig variable\
610 'modulerc_extra_vars'"
611 }
612 foreach {var val} $::modulerc_extra_vars {
613 if {[string first { } $var] != -1} {
614 knerror "Invalid variable name '$var'\ndefined in siteconfig\
615 variable 'modulerc_extra_vars'"
616 }
617 }
618 lappend ::g_modrcBaseVars {*}$::modulerc_extra_vars
619 }
620 }
621
622 # dedicate an interpreter per level of interpretation to have in case of
623 # cascaded interpretations a specific interpreter per level
624 set itrp __modrc_[depthState modulename]
625
626 reportTrace '$modfile' {Evaluate modulerc}
627 # create modulerc interpreter at first interpretation
628 if {![interp exists $itrp]} {
629 createInterp $itrp
630
631 # initialize global static variables for modulerc interp
632 foreach {var val} $::g_modrcBaseVars {
633 interp eval $itrp set ::$var "{$val}"
634 }
635
636 # dump initial interpreter state to restore it before each modulerc
637 # interpretation. use same dump state for all levels
638 if {![info exists ::g_modrcVars]} {
639 dumpInterpState $itrp g_modrcVars g_modrcArrayVars\
640 g_modrcUntrackVars g_modrcProcs
641 }
642
643 # interp has just been created
644 set fresh 1
645 } else {
646 set fresh 0
647 }
648
649 # reset interp state command before each interpretation
650 resetInterpState $itrp $fresh g_modrcVars g_modrcArrayVars\
651 g_modrcUntrackVars g_modrcProcs g_modrcAliases g_modrcAliasesPassArg\
652 g_modrcAliasesTraces g_modrcRenameCmds g_modrcCommands
653
654 runHooks before-modulerc-eval $modfile $modname
655
656 set failed_eval [catch {evaluateModulerc $itrp $modfile} errorMsg]
657
658 set eval_return_code [renderModulercEvalError $itrp $modfile $failed_eval\
659 $errorMsg]
660
661 runHooks after-modulerc-eval $modfile $modname $eval_return_code
662
663 # default version set via ModulesVersion variable in .version file
664 # override previously defined default version for modname
665 lassign [getModuleNameVersion] mod modname modversion
666 if {$modversion eq {.version} && $::ModulesVersion ne {}} {
667 # ModulesVersion should target an element in current directory
668 if {[string first / $::ModulesVersion] == -1} {
669 setModuleResolution $modname/default $modname/$::ModulesVersion\
670 default
671 } else {
672 reportError "Invalid ModulesVersion '$::ModulesVersion' defined"
673 }
674 }
675
676 lpopState debug_msg_prefix
677 lpopState specifiedname
678 lpopState modulename
679 lpopState modulefile
680
681 return $::ModulesVersion
682 }
683
684 proc evaluateModulerc {itrp mod_file} {
685 interp eval $itrp set ::ModulesCurrentModulefile "{$mod_file}"
686 interp eval $itrp {set ::ModulesVersion {}}
687 # create an alias ModuleVersion on ModulesVersion
688 interp eval $itrp {upvar 0 ::ModulesVersion ::ModuleVersion}
689
690 ##nagelfar ignore +4 Suspicious # char
691 interp eval $itrp {
692 info script $::ModulesCurrentModulefile
693 eval [getModuleContent $::ModulesCurrentModulefile]
694 # pass ModulesVersion value to main interp
695 if {[info exists ::ModulesVersion]} {
696 setModulesVersion $::ModulesVersion
697 }
698 }
699 }
700
701 proc renderModulercEvalError {itrp mod_file failed_eval error_msg} {
702 if {!$failed_eval} {
703 return 0
704 }
705 # no error if rc file cannot be read
706 switch -- [getInterpVar $itrp errorCode] {
707 MODULES_ERR_READ {}
708 MODULES_ERR_VALIDITY {reportInternalBug $error_msg $mod_file}
709 default {reportInternalBug [formatInterpErrStackTrace $itrp $mod_file]}
710 }
711 return 1
712 }
713
714 proc isInterpVarDefined {itrp var_name} {
715 return [interp eval $itrp info exists $var_name]
716 }
717
718 proc getInterpVar {itrp var_name {val_if_unset {}}} {
719 if {[isInterpVarDefined $itrp $var_name]} {
720 return [interp eval $itrp set $var_name]
721 } else {
722 return $val_if_unset
723 }
724 }
725
726 # format error stack trace to report modulefile information only
727 proc formatInterpErrStackTrace {itrp modfile} {
728 return [formatErrStackTrace [getInterpVar $itrp ::errorInfo] $modfile\
729 [concat [interp eval $itrp info procs] [interp eval $itrp info\
730 commands]]]
731 }
732
733 # Save list of the defined procedure and the global variables with their
734 # associated values set in sub interpreter passed as argument. Global
735 # structures are used to save these information and the name of these
736 # structures are provided as argument.
737 proc dumpInterpState {itrp dumpVarsVN dumpArrayVarsVN untrackVarsVN\
738 dumpProcsVN} {
739 upvar #0 $dumpVarsVN dumpVars
740 upvar #0 $dumpArrayVarsVN dumpArrayVars
741 upvar #0 $untrackVarsVN untrackVars
742 upvar #0 $dumpProcsVN dumpProcs
743
744 regexp {^__[a-z]+} $itrp itrpkind
745 # save name and value for any other global variables
746 foreach var [$itrp eval {info globals}] {
747 if {![info exists untrackVars($var)]} {
748 reportDebug "saving for $itrpkind var $var"
749 if {[$itrp eval array exists ::$var]} {
750 set dumpVars($var) [$itrp eval array get ::$var]
751 set dumpArrayVars($var) 1
752 } else {
753 set dumpVars($var) [$itrp eval set ::$var]
754 }
755 }
756 }
757
758 # save name of every defined procedures
759 foreach var [$itrp eval {info procs}] {
760 set dumpProcs($var) 1
761 }
762 reportDebug "saving for $itrpkind proc list [array names dumpProcs]"
763 }
764
765 # Define commands to be known by sub interpreter.
766 proc initInterpCommands {itrp fresh aliasesVN aliasesPassArgVN tracesVN\
767 renameCmdsVN} {
768 upvar #0 $aliasesVN aliases
769 upvar #0 $aliasesPassArgVN aliasesPassArg
770 upvar #0 $tracesVN traces
771 upvar #0 $renameCmdsVN renameCmds
772
773 # rename some commands on freshly created interp before aliases defined
774 # below overwrite them
775 if {$fresh} {
776 foreach cmd [array names renameCmds] {
777 $itrp eval rename $cmd $renameCmds($cmd)
778 }
779 }
780
781 # set interpreter alias commands each time to guaranty them being
782 # defined and not overridden by modulefile or modulerc content
783 foreach alias [array names aliases] {
784 if {[info exists aliasesPassArg($alias)]} {
785 set aliasargs $aliasesPassArg($alias)
786 # pass current itrp reference on special keyword
787 if {[lindex $aliasargs 0] eq {__itrp__}} {
788 lset aliasargs 0 $itrp
789 }
790 interp alias $itrp $alias {} $aliases($alias) {*}$aliasargs
791 } else {
792 interp alias $itrp $alias {} $aliases($alias)
793 }
794 }
795
796 if {$fresh} {
797 # trace each modulefile command call if verbosity is set to debug (when
798 # higher verbosity level is set all cmds are already traced) and timer
799 # mode is disabled
800 if {[getConf verbosity] eq {debug} && ![getState timer]} {
801 interp alias $itrp reportTraceExecEnter {} reportTraceExecEnter
802 foreach alias [array names aliases] {
803 # exclude internal commands expoxed to modulerc/file interpreter
804 # exclude cachefile commands
805 if {$alias ni {report reportDebug reportError reportWarning\
806 reportCmdTrace incrErrorCount reportInternalBug\
807 formatErrStackTrace isVerbosityLevel modulefile-content\
808 modulerc-content modulefile-invalid limited-access-file\
809 limited-access-directory}} {
810 interp eval $itrp [list trace add execution $alias enter\
811 reportTraceExecEnter]
812 }
813 }
814 }
815 }
816
817 foreach alias [array names traces] {
818 foreach trace_proc $traces($alias) {
819 interp eval $itrp [list trace add execution $alias leave $trace_proc]
820 }
821 }
822 }
823
824 # Restore initial setup of sub interpreter passed as argument based on
825 # global structure previously filled with initial list of defined procedure
826 # and values of global variable.
827 proc resetInterpState {itrp fresh dumpVarsVN dumpArrayVarsVN untrackVarsVN\
828 dumpProcsVN aliasesVN aliasesPassArgVN tracesVN renameCmdsVN\
829 dumpCommandsVN} {
830 upvar #0 $dumpVarsVN dumpVars
831 upvar #0 $dumpArrayVarsVN dumpArrayVars
832 upvar #0 $untrackVarsVN untrackVars
833 upvar #0 $dumpProcsVN dumpProcs
834 upvar #0 $dumpCommandsVN dumpCommands
835
836 # look at list of defined procedures and delete those not part of the
837 # initial state list. do not check if they have been altered as no vital
838 # procedures lied there. note that if a Tcl command has been overridden
839 # by a proc, it will be removed here and command will also disappear
840 foreach var [$itrp eval {info procs}] {
841 if {![info exists dumpProcs($var)]} {
842 reportDebug "removing on $itrp proc $var"
843 $itrp eval [list rename $var {}]
844 }
845 }
846
847 ##nagelfar vartype aliasesVN varName
848 ##nagelfar vartype aliasesPassArgVN varName
849 ##nagelfar vartype tracesVN varName
850 ##nagelfar vartype renameCmdsVN varName
851 # rename some commands and set aliases on interpreter
852 initInterpCommands $itrp $fresh $aliasesVN $aliasesPassArgVN $tracesVN\
853 $renameCmdsVN
854
855 # dump interpreter command list here on first time as aliases should be
856 # set prior to be found on this list for correct match
857 if {![info exists dumpCommands]} {
858 set dumpCommands [$itrp eval {info commands}]
859 reportDebug "saving for $itrp command list $dumpCommands"
860 # if current interpreter command list does not match initial list it
861 # means that at least one command has been altered so we need to recreate
862 # interpreter to guaranty proper functioning
863 } elseif {$dumpCommands ne [$itrp eval {info commands}]} {
864 reportDebug "missing command(s), recreating interp $itrp"
865 interp delete $itrp
866 createInterp $itrp
867 initInterpCommands $itrp 1 $aliasesVN $aliasesPassArgVN $tracesVN\
868 $renameCmdsVN
869 }
870
871 # check every global variables currently set and correct them to restore
872 # initial interpreter state. work on variables at the very end to ensure
873 # procedures and commands are correctly defined
874 foreach var [$itrp eval {info globals}] {
875 if {![info exists untrackVars($var)]} {
876 if {![info exists dumpVars($var)]} {
877 reportDebug "removing on $itrp var $var"
878 $itrp eval unset -nocomplain ::$var
879 } elseif {![info exists dumpArrayVars($var)]} {
880 if {$dumpVars($var) ne [$itrp eval set ::$var]} {
881 reportDebug "restoring on $itrp var $var"
882 if {[string is list $dumpVars($var)] && [llength\
883 $dumpVars($var)] > 1} {
884 # restore value as list
885 $itrp eval set ::$var [list $dumpVars($var)]
886 } else {
887 # brace value to be able to restore empty string
888 $itrp eval set ::$var "{$dumpVars($var)}"
889 }
890 }
891 } else {
892 if {$dumpVars($var) ne [$itrp eval array get ::$var]} {
893 reportDebug "restoring on $itrp var $var"
894 $itrp eval array set ::$var [list $dumpVars($var)]
895 }
896 }
897 }
898 }
899 }
900
901 # ;;; Local Variables:
902 # ;;; Mode: tcl-mode
903 # ;;; tcl-indent-level: 3
904 # ;;; tcl-continued-indent-level: 3
905 # ;;; indent-tabs-mode: nil
906 # ;;; End:
907 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: