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