Debian Codegraph @

tcl/main.tcl.in

raw
1 ##########################################################################
2
3 # MAIN.TCL, main procedures and code
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 # exit in a clean manner by flushing and closing interaction with external
24 # components
25 proc flushAndExit {} {
26 # output all shell code generated on stdout
27 renderFlush
28 # send messages to log if any and enabled
29 logFlush
30 # output last messages on the report file descriptor and close it
31 reportFlush
32
33 exit [expr {[getState error_count] > 0}]
34 }
35
36 # runs the global RC files if they exist
37 proc runModulerc {} {
38 setState rc_running 1
39 foreach rc [getGlobalRcFileList] {
40 reportDebug "Executing $rc"
41 cmdModuleSource load $rc
42 lappendState rc_loaded $rc
43 }
44 unsetState rc_running
45
46 # identify alias or symbolic version set in these global RC files to be
47 # able to include them or not in output or resolution processes
48 array set ::g_rcAlias [array get ::g_moduleAlias]
49 array set ::g_rcVersion [array get ::g_moduleVersion]
50 array set ::g_rcVirtual [array get ::g_moduleVirtual]
51 }
52
53 proc aboveCommandName {} {
54 return [lindex [getState commandname] end-1]
55 }
56
57 # get top-level command name, with ml frontend translated onto the
58 # underlying load or unload sub-command it currently processes
59 proc topCommandName {} {
60 set subcmd [topState commandname]
61 if {$subcmd eq {ml}} {
62 set subcmd [topState eval_context]
63 }
64 return $subcmd
65 }
66
67 proc ongoingCommandName {commandName} {
68 return [expr {[lsearch -exact [getState commandname] $commandName] != -1}]
69 }
70
71 # analyze/translate command name passed to module
72 proc parseModuleCommandName {command defaultcmd} {
73 set cmdempty 0
74
75 # resolve command if alias or shortcut name used
76 switch -- $command {
77 add {set command load}
78 try-add {set command try-load}
79 add-any {set command load-any}
80 rm - remove {set command unload}
81 show {set command display}
82 apropos - keyword {set command search}
83 {} {
84 # if empty string supplied translate to default command
85 set command $defaultcmd
86 set cmdempty 1
87 }
88 default {
89 # specific match for shortcut names
90 set cmdlen [string length $command]
91 foreach {match minlen sccmd} {load 2 load unload 4 unload delete 3\
92 unload refresh 3 refresh reload 3 reload switch 2 switch swap 2\
93 switch display 2 display available 2 avail aliases 2 aliases list\
94 2 list whatis 2 whatis purge 2 purge initadd 5 initadd initload 6\
95 initadd initprepend 5 initprepend initswitch 6 initswitch\
96 initswap 6 initswitch initunload 8 initrm initlist 5 initlist\
97 spider 3 spider update 2 reload disable 4 saverm describe 3\
98 saveshow} {
99 if {$cmdlen >= $minlen && [string equal -length $cmdlen $command\
100 $match]} {
101 set command $sccmd
102 break
103 }
104 }
105 }
106 }
107
108 set cmdvalid [expr {$command in [list load unload reload use unuse source\
109 switch display avail aliases path paths list whatis search purge save\
110 restore saverm saveshow savelist initadd initprepend initswitch initrm\
111 initlist initclear autoinit clear config help test prepend-path\
112 append-path remove-path is-loaded is-saved is-used is-avail info-loaded\
113 sh-to-mod edit try-load refresh state load-any lint mod-to-sh reset\
114 stash stashpop stashrm stashshow stashclear stashlist cachebuild\
115 cacheclear spider]}]
116
117 reportDebug "(command=$command, cmdvalid=$cmdvalid, cmdempty=$cmdempty)"
118 return [list $command $cmdvalid $cmdempty]
119 }
120
121 # analyze arg list passed to a module cmd to set options
122 proc parseModuleCommandArgs {topcall cmd ignerr ml_spec args} {
123 set show_oneperline 0
124 set show_mtime 0
125 set show_filter {}
126 set indepth_opt [expr {$cmd eq {spider} ? {spider_indepth} :\
127 {avail_indepth}}]
128 set search_filter [expr {[getConf $indepth_opt] ? {} : {noindepth}}]
129 set search_match [getConf search_match]
130 set dump_state 0
131 set addpath_pos prepend
132 set not_req 0
133 set tag_list {}
134 set otherargs {}
135
136 # parse argument list
137 foreach arg $args {
138 if {[info exists nextargisval]} {
139 ##nagelfar vartype nextargisval varName
140 set $nextargisval $arg
141 unset nextargisval
142 } elseif {[info exists nextargisvaltosplit]} {
143 ##nagelfar vartype nextargisvaltosplit varName
144 set $nextargisvaltosplit [split $arg :]
145 unset nextargisvaltosplit
146 } elseif {[info exists ignore_next_arg]} {
147 unset ignore_next_arg
148 } else {
149 switch -glob -- $arg {
150 -j - --json {
151 # enable json output only on supported command
152 if {$cmd in [list avail savelist stashlist list search\
153 whatis spider]} {
154 setState report_format json
155 set show_oneperline 0
156 set show_mtime 0
157 }
158 }
159 -t - --terse {
160 set show_oneperline 1
161 set show_mtime 0
162 setState report_format terse
163 }
164 -l - --long {
165 set show_mtime 1
166 set show_oneperline 0
167 setState report_format long
168 }
169 -o {
170 # option is only valid for specific sub-commands
171 if {$cmd in [list avail list spider]} {
172 set nextargisval asked_output
173 set output_arg -o
174 } else {
175 if {!$ignerr} {
176 knerror "Unsupported option '$arg' on $cmd sub-command"
177 }
178 set ignore_next_arg 1
179 }
180 }
181 --output=* {
182 # option is only valid for specific sub-commands
183 if {$cmd in [list avail list spider]} {
184 set asked_output [string range $arg 9 end]
185 set output_arg --output
186 } elseif {!$ignerr} {
187 knerror "Unsupported option '--output' on $cmd sub-command"
188 }
189 }
190 --tag=* - --tag {
191 # option is only valid for specific sub-commands
192 # unload allowed not to raise error on unload/load mixed ml cmd
193 if {$cmd in [list load try-load load-any switch unload]} {
194 if {$arg eq {--tag}} {
195 ##nagelfar ignore Found constant
196 set nextargisvaltosplit tag_list
197 } else {
198 set tag_list [split [string range $arg 6 end] :]
199 if {![llength $tag_list]} {
200 knerror "Missing value for '--tag' option"
201 }
202 }
203 } elseif {!$ignerr} {
204 knerror "Unsupported option '--tag' on $cmd sub-command"
205 }
206 }
207 --append - -append {
208 if {$cmd eq {use}} {
209 set addpath_pos append
210 } else {
211 lappend otherargs $arg
212 }
213 }
214 -p - --prepend - -prepend {
215 if {$cmd eq {use}} {
216 set addpath_pos prepend
217 } else {
218 lappend otherargs $arg
219 }
220 }
221 --all - --show_hidden {
222 # include hidden modules only on a limited set of command
223 if {$cmd in [list avail aliases search whatis ml list lint\
224 savelist spider]} {
225 set ::asked_hiding_threshold 2
226 } else {
227 lappend otherargs $arg
228 }
229 }
230 -a {
231 # -a option has a different meaning whether sub-command is use
232 # or one of the search/listing sub-commands
233 if {$cmd eq {use}} {
234 set addpath_pos append
235 } elseif {$cmd in [list avail aliases search whatis ml list\
236 lint savelist spider]} {
237 set ::asked_hiding_threshold 2
238 } else {
239 lappend otherargs $arg
240 }
241 }
242 -d - --default {
243 # in case of *-path command, -d means --delim
244 if {$arg eq {-d} && [string match *-path $cmd]} {
245 lappend otherargs $arg
246 } else {
247 set show_filter onlydefaults
248 }
249 }
250 -L - --latest {
251 set show_filter onlylatest
252 }
253 -C - --contains {
254 set search_match contains
255 }
256 -S - --starts-with {
257 set search_match starts_with
258 }
259 --indepth {
260 # empty value means 'in depth' as it is default behavior
261 set search_filter {}
262 }
263 --no-indepth {
264 set search_filter noindepth
265 }
266 --dump-state {
267 set dump_state 1
268 }
269 --auto - --no-auto - -f - --force {
270 reportWarning "Unsupported option '$arg'"
271 }
272 --not-req {
273 if {!$topcall && $cmd in [list load try-load load-any unload\
274 switch]} {
275 set not_req 1
276 } else {
277 knerror "Unsupported option '$arg' on $cmd sub-command"
278 }
279 }
280 --output {
281 knerror "Missing value for '$arg' option"
282 }
283 default {
284 lappend otherargs $arg
285 }
286 }
287 set prevarg $arg
288 }
289 }
290
291 if {[info exists nextargisval] || [info exists nextargisvaltosplit]} {
292 knerror "Missing value for '$prevarg' option"
293 }
294
295 foreach tag $tag_list {
296 if {$tag in [list loaded auto-loaded forbidden nearly-forbidden\
297 hidden warning]} {
298 knerror "Tag '$tag' cannot be manually set"
299 }
300 }
301
302 if {[info exists asked_output]} {
303 if {[getState report_format] in [list long json]} {
304 knerror "Unsupported option '$output_arg' on [getState\
305 report_format] output mode"
306 } else {
307 # get config name relative to current sub-command and output format
308 set outputconf $cmd
309 if {[getState report_format] ne {regular}} {
310 append outputconf _[getState report_format]
311 }
312 append outputconf _output
313
314 # check option value is coherent with current sub-command (remove
315 # append/subtract operator at the start if any)
316 set asked_output_list [split [expr {[string index $asked_output 0]\
317 in {+ -} ? [string range $asked_output 1 end] : $asked_output}] :]
318 if {[isDiffBetweenList $asked_output_list [lindex\
319 $::g_config_defs($outputconf) 4]]} {
320 knerror "Invalid element in value list for '$output_arg' option\
321 on $cmd sub-command\nAllowed elements are: [lindex\
322 $::g_config_defs($outputconf) 4] (separated by ':')"
323 } else {
324 ##nagelfar ignore Suspicious variable name
325 set ::asked_$outputconf $asked_output
326 }
327 }
328 }
329
330 # parse module version specification
331 if {$cmd in {avail paths whatis load unload switch help test display path\
332 is-avail edit try-load load-any list lint mod-to-sh source spider}} {
333 # some cmds allow to express variant without module name
334 set allow_noname_spec [expr {$cmd in {avail list paths whatis spider}}]
335 # some cmds allow extra specifier in query
336 set allow_xt_spec [expr {$cmd in {avail paths whatis spider}}]
337 set otherargs [parseModuleSpecification $ml_spec $allow_noname_spec\
338 $allow_xt_spec 0 {*}$otherargs]
339 }
340
341 reportDebug "(show_oneperline=$show_oneperline, show_mtime=$show_mtime,\
342 show_filter=$show_filter, search_filter=$search_filter,\
343 search_match=$search_match, dump_state=$dump_state,\
344 addpath_pos=$addpath_pos, not_req=$not_req, tag_list=$tag_list,\
345 otherargs=$otherargs)"
346 return [list $show_oneperline $show_mtime $show_filter $search_filter\
347 $search_match $dump_state $addpath_pos $not_req $tag_list $otherargs]
348 }
349
350 proc checkModuleCommandNbArgs {topcall command nb_args} {
351 if {[switch -- $command {
352 unload - source - display - initadd - initprepend - initrm - test -\
353 is-avail - try-load - load-any {
354 expr {!$nb_args}
355 }
356 refresh - reload - aliases - purge - initlist - initclear - autoinit -\
357 reset - stash - stashclear - stashlist - cacheclear {
358 expr {$nb_args}
359 }
360 switch {
361 expr {!$nb_args || $nb_args > 2}
362 }
363 path - paths - info-loaded - edit {
364 expr {$nb_args != 1}
365 }
366 search - save - restore - saverm - saveshow - clear - state - stashpop\
367 - stashrm - stashshow {
368 expr {$nb_args > 1}
369 }
370 initswitch {
371 expr {$nb_args != 2}
372 }
373 prepend-path - append-path - remove-path - sh-to-mod - mod-to-sh {
374 expr {$nb_args < 2}
375 }
376 config {
377 expr {$nb_args > 2}
378 }
379 default {
380 expr {0 == 1}
381 }
382 }]} {
383 knerrorModule $topcall "Unexpected number of args for '$command'\
384 command"
385 }
386 }
387
388 # define module sub-command procedure to launch and its associated options
389 proc formatModuleCommand {topcall command mode tag_list addpath_pos\
390 show_oneperline show_mtime show_filter search_filter search_match\
391 dump_state parsed_args} {
392 set cmd_proc_name cmdModule[switch -- $command {
393 load - try-load - load-any {
394 return -level 0 IntLoad
395 }
396 unload {
397 return -level 0 IntUnload
398 }
399 switch {
400 return -level 0 IntSwitch
401 }
402 initadd - initprepend - initswitch - initrm - initlist - initclear {
403 return -level 0 Init
404 }
405 sh-to-mod {
406 return -level 0 ShToMod
407 }
408 mod-to-sh {
409 return -level 0 ModToSh
410 }
411 prepend-path - append-path - remove-path - is-loaded - is-saved -\
412 is-used - is-avail {
413 return -level 0 Resurface
414 }
415 info-loaded {
416 return -level 0 Resurface
417 }
418 whatis {
419 return -level 0 Search
420 }
421 default {
422 return -level 0 [string toupper $command 0 0]
423 }}]
424
425 set cmd_opts [switch -- $command {
426 load - try-load - load-any {
427 list $topcall $command $mode $tag_list
428 }
429 unload {
430 list $mode
431 }
432 switch {
433 list $mode $tag_list
434 }
435 use {
436 list $mode $addpath_pos
437 }
438 unuse {
439 list $mode
440 }
441 source {
442 list load
443 }
444 avail {
445 list $show_oneperline $show_mtime $show_filter $search_filter\
446 $search_match [getModulePathList exiterronundef]
447 }
448 spider {
449 list $show_oneperline $show_mtime $show_filter $search_filter\
450 $search_match
451 }
452 list - savelist {
453 list $show_oneperline $show_mtime $search_match
454 }
455 config {
456 list $dump_state
457 }
458 stashlist {
459 list $show_oneperline $show_mtime
460 }
461 whatis {
462 list {}
463 }
464 info-loaded {
465 list module-info loaded
466 }
467 prepend-path - append-path - remove-path - is-loaded - is-saved -\
468 is-used - is-avail {
469 list $command
470 }
471 initadd - initprepend - initswitch - initrm - initlist - initclear {
472 list [string range $command 4 end]
473 }}]
474
475 return [list $cmd_proc_name {*}$cmd_opts {*}$parsed_args]
476 }
477
478 # define the dynamic procedures that compare module specifications and parse
479 # loaded environment if enabled
480 proc initModCmpProcsAndLoadedEnv {parse_loaded_env} {
481 set icase [isIcase]
482 set extdfl [getConf extended_default]
483 defineModStartNbProc $icase
484 defineModEqProc $icase $extdfl
485 defineModVersCmpProc $icase $extdfl
486 defineGetEqArrayKeyProc [getConf implicit_default]
487 defineParseModuleSpecificationProc [getConf advanced_version_spec]
488
489 if {$parse_loaded_env} {
490 cacheCurrentModules
491 }
492 }
493
494 proc moduleTop {command args} {
495 # resolve and check command name
496 lassign [parseModuleCommandName $command help] command cmdvalid cmdempty
497 # clear other args if no command name supplied
498 if {$cmdempty} {
499 set args {}
500 }
501 # raise error if supplied command is not known
502 if {!$cmdvalid} {
503 knerrorModule 1 "Invalid command '$command'"
504 }
505
506 # define states needed by module spec parsing and sub-command processing
507 # define if modfile should always be fully read even for validity check
508 lappendState always_read_full_file [expr {$command ni [list path paths\
509 list avail aliases edit spider]}]
510 lappendState commandname $command
511
512 initModCmpProcsAndLoadedEnv [expr {$command ni {append-path cachebuild\
513 cacheclear clear config initadd initclear initlist initprepend initrm\
514 initswitch is-saved is-used prepend-path remove-path savelist saverm\
515 saveshow sh-to-mod stashlist stashrm stashclear stashshow state use\
516 unuse}}]
517
518 # parse options, do that globally to ignore options not related to a given
519 # module sub-command (exclude them from arg list)
520 lassign [parseModuleCommandArgs 1 $command 0 0 {*}$args] show_oneperline\
521 show_mtime show_filter search_filter search_match dump_state\
522 addpath_pos not_req tag_list parsed_args
523
524 checkModuleCommandNbArgs 1 $command [llength $parsed_args]
525
526 logEvent requested_cmd {command} $command arguments $args
527 # Find and execute any global rc file found
528 runModulerc
529
530 {*}[formatModuleCommand 1 $command load $tag_list $addpath_pos\
531 $show_oneperline $show_mtime $show_filter $search_filter $search_match\
532 $dump_state $parsed_args]
533
534 lpopState commandname
535 lpopState always_read_full_file
536
537 renderSettings
538
539 return {}
540 }
541
542 proc ml {args} {
543 # filter out all known options from argument list to guess command name
544 # without them in the way
545 lassign [parseModuleCommandArgs 1 ml 1 0 {*}$args] show_oneperline\
546 show_mtime show_filter search_filter search_match dump_state\
547 addpath_pos not_req tag_list fargs
548
549 # determine if first argument is a known module sub-command
550 lassign [parseModuleCommandName [lindex $fargs 0] list] command cmdvalid\
551 cmdempty
552
553 if {$cmdempty} {
554 # consider empty string supplied as first argument as module name
555 if {[llength $fargs]} {
556 set cmdvalid 0
557 }
558 set margs $args
559 } else {
560 # first argument was command name
561 set margs [lrange $args 1 end]
562 }
563
564 # directly call module procedure if sub-command spotted as first argument
565 # or no argument supplied
566 if {$cmdvalid} {
567 moduleTop $command {*}$margs
568 } else {
569 # define if modfile should always be fully read even for validity check
570 lappendState always_read_full_file 1
571 lappendState commandname ml
572
573 initModCmpProcsAndLoadedEnv 1
574
575 # main procedure has already raised error for badly written argument
576 # like '-' or '--', but we need here to replay module-specific argument
577 # parsing to raise error if some arg are not allowed on unload/load cmd
578 lassign [parseModuleCommandArgs 1 load 0 1 {*}$args] show_oneperline\
579 show_mtime show_filter search_filter search_match dump_state\
580 addpath_pos not_req tag_list fargs
581
582 # split result to get list of mods to unload and mods to load
583 lassign $fargs modunlist modlolist
584
585 logEvent requested_cmd {command} ml arguments $args
586
587 # Find and execute any global rc file found
588 runModulerc
589
590 set ret 0
591 pushSettings
592
593 # first unload specified modules
594 if {[llength $modunlist]} {
595 set ret [cmdModuleUnload unload match 1 s 0 {*}$modunlist]
596 }
597 # then load other modules unless unload phase failed and abort mode
598 if {(!$ret || ![commandAbortOnError]) && [llength $modlolist]} {
599 set ret [cmdModuleLoad load 1 0 0 $tag_list {} {*}$modlolist]
600 }
601
602 # rollback changes if abort mode and any load or unload failed
603 if {$ret && [commandAbortOnError]} {
604 restoreSettings
605 }
606 popSettings
607
608 lpopState commandname
609 lpopState always_read_full_file
610
611 renderSettings
612 }
613
614 return {}
615 }
616
617 #
618 # Main program
619 #
620
621 # needed on a gentoo system. Shouldn't hurt since it is
622 # supposed to be the default behavior
623 fconfigure stderr -translation auto
624
625 if {[catch {
626 # parse all command-line arguments before doing any action, no output is
627 # made during argument parse to wait for potential paging to be setup
628 set show_help 0
629 set show_version 0
630 set show_name 0
631 setState cmdline "$argv0 $argv"
632
633 # Load extension library if enabled
634 ##nagelfar ignore #6 Strange command
635 ##nagelfar ignore +2 Too long line
636 @libtclenvmodules@if {[file readable [getConf tcl_ext_lib]]} {
637 @libtclenvmodules@ reportDebug "Load Tcl extension library ([getConf tcl_ext_lib])"
638 @libtclenvmodules@ if {[catch {
639 @libtclenvmodules@ load [file normalize [getConf tcl_ext_lib]] Envmodules
640 @libtclenvmodules@ } errMsg]} {
641 @libtclenvmodules@ reportErrorAndExit $errMsg
642 @libtclenvmodules@ }
643 @libtclenvmodules@ setState tcl_ext_lib_loaded 1
644 @libtclenvmodules@}
645 # use fallback procs if extension library is not loaded
646 if {[info commands readFile] eq {}} {
647 rename ::__readFile ::readFile
648 rename ::__getFilesInDirectory ::getFilesInDirectory
649 rename ::__initStateUsergroups ::initStateUsergroups
650 rename ::__initStateUsername ::initStateUsername
651 rename ::__initStateClockSeconds ::initStateClockSeconds
652 rename ::__parseDateTimeArg ::parseDateTimeArg
653 }
654 ##nagelfar syntax readFile x x? x?
655 ##nagelfar syntax getFilesInDirectory x x
656 ##nagelfar syntax initStateUsergroups
657 ##nagelfar syntax initStateUsername
658 ##nagelfar syntax initStateClockSeconds
659 ##nagelfar syntax parseDateTimeArg x x
660
661 # source site configuration script if any
662 sourceSiteConfig
663
664 setState supported_shells {sh bash ksh zsh csh tcsh fish cmd tcl perl\
665 python ruby lisp cmake r pwsh}
666
667 # Parse shell
668 setState shell [lindex $argv 0]
669 if {[getState shell] ni [getState supported_shells]} {
670 reportErrorAndExit "Unknown shell type '([getState shell])'"
671 }
672
673 # extract options and command switches from other args
674 set otherargv {}
675 set extraargv {}
676 set ddelimarg 0
677 # split first arg if multi-word string detected for compat with previous
678 # doc on module usage with scripting language: module('load mod1 mod2')
679 ##nagelfar ignore #2 Badly formed if statement
680 set argtoparse [if {[llength [lindex $argv 1]] > 1} {list {*}[split\
681 [lindex $argv 1]] {*}[lrange $argv 2 end]} {lrange $argv 1 end}]
682 foreach arg $argtoparse {
683 if {[info exists ignore_next_arg]} {
684 unset ignore_next_arg
685 } elseif {[info exists nextargisextraargv]} {
686 lappend extraargv $arg
687 unset nextargisextraargv
688 } elseif {[info exists nextargisval]} {
689 ##nagelfar vartype nextargisval varName
690 set $nextargisval $arg
691 unset nextargisval
692 } else {
693 switch -glob -- $arg {
694 -T - --trace {
695 set asked_verbosity trace
696 }
697 -D - -DD - --debug {
698 set asked_verbosity [expr {$arg eq {-DD} || ([info exists\
699 asked_verbosity] && $asked_verbosity in {debug debug2}) ?\
700 {debug2} : {debug}}]
701 }
702 -s - --silent {
703 set asked_verbosity silent
704 }
705 -v - -vv - --verbose {
706 set asked_verbosity [expr {$arg eq {-vv} || ([info exists\
707 asked_verbosity] && $asked_verbosity in {verbose verbose2})\
708 ? {verbose2} : {verbose}}]
709 }
710 --help - -h {
711 set show_help 1
712 }
713 -V - --version {
714 set show_version 1
715 }
716 --dumpname {
717 set show_name 1
718 }
719 -p {
720 # means paginate if set as an option (before specifying subcmd)
721 if {![llength $otherargv]} {
722 set asked_paginate 1
723 } else {
724 lappend otherargv $arg
725 }
726 }
727 --paginate {
728 set asked_paginate 1
729 }
730 -P - --no-pager {
731 set asked_paginate 0
732 }
733 --redirect {
734 if {[getState shelltype] ni {sh fish pwsh}} {
735 reportWarning "Unsupported option '--redirect' on [getState\
736 shell] shell"
737 } else {
738 set asked_redirect_output 1
739 }
740 }
741 --no-redirect - --no_redirect {
742 set asked_redirect_output 0
743 }
744 --auto {
745 set asked_auto_handling 1
746 }
747 --no-auto {
748 set asked_auto_handling 0
749 }
750 -f - --force {
751 set asked_force 1
752 }
753 --color* {
754 set asked_color [string range $arg 8 end]
755 if {$asked_color eq {}} {
756 set asked_color always
757 } elseif {$asked_color ni [lindex $::g_config_defs(color) 4]} {
758 unset asked_color
759 }
760 }
761 -o {
762 # add with next arg to the command-specific switches
763 lappend extraargv $arg
764 set nextargisextraargv 1
765 }
766 --width* {
767 set asked_term_width [string range $arg 8 end]
768 set term_width_arg --width
769 if {$asked_term_width eq {}} {
770 set asked_term_width 0
771 }
772 }
773 -w {
774 ##nagelfar ignore Found constant
775 set nextargisval asked_term_width
776 set term_width_arg -w
777 }
778 -t - --terse - -l - --long - --default - -L - --latest - -S -\
779 --starts-with - -C - --contains - -j - --json - --output=* {
780 # command-specific switches that can for compatibility be
781 # passed before the command name, so add them to a specific
782 # arg list to ensure command name as first position argument
783 lappend extraargv $arg
784 }
785 -d {
786 # in case of *-path command, -d means --delim
787 if {$ddelimarg} {
788 lappend otherargv $arg
789 } else {
790 lappend extraargv $arg
791 }
792 }
793 -a - --append - -append - --all - --prepend - -prepend - --delim\
794 - -delim - --delim=* - -delim=* - --duplicates - --index -\
795 --notuasked - --indepth - --no-indepth - --dump-state - --reset -\
796 --tag - --tag=* - --glob - --show_hidden {
797 # command-specific switches interpreted later on
798 lappend otherargv $arg
799 }
800 append-path - prepend-path - remove-path {
801 # detect *-path commands to say -d means --delim, not --default
802 set ddelimarg 1
803 lappend otherargv $arg
804 }
805 -i - --icase {
806 set asked_icase always
807 }
808 --ignore-cache - --ignore_cache {
809 set asked_ignore_cache 1
810 }
811 --ignore-user-rc {
812 set asked_ignore_user_rc 1
813 }
814 --no-modulepath-ignore {
815 set asked_modulepath_ignore 0
816 }
817 --timer {
818 setState timer 1
819 set timer_start [clock microseconds]
820 }
821 --human - -c - --create - --userlvl=* {
822 # ignore C-version specific option, no error only warning
823 reportWarning "Unsupported option '$arg'"
824 }
825 -u - --userlvl {
826 reportWarning "Unsupported option '$arg'"
827 # also ignore argument value
828 set ignore_next_arg 1
829 }
830 --output {
831 reportErrorAndExit "Missing value for '$arg' option\nTry\
832 'module --help' for more information."
833 }
834 --initial_load {
835 # ignore option for compatibility
836 }
837 {-} - {--} - {--*} {
838 reportErrorAndExit "Invalid option '$arg'\nTry 'module --help'\
839 for more information."
840 }
841 -* {
842 # verify current command accepts minus arg (-*)
843 if {![info exists accept_minus_arg] && [llength $otherargv]} {
844 set subcmdtest [lindex $otherargv 0]
845 if {$subcmdtest ne {ml}} {
846 lassign [parseModuleCommandName $subcmdtest {}]\
847 subcmdtest
848 }
849 # accepted if command is ml or config or if adv vers spec is
850 # enabled and command can receive boolean variant
851 # specification
852 set accept_minus_arg [expr {$subcmdtest in {ml config} ||\
853 ([getConf advanced_version_spec] && $subcmdtest in\
854 {avail list display help is-avail is-loaded load path\
855 paths switch test unload whatis mod-to-sh source\
856 spider})}]
857 }
858 # spare argument if minus arg is accepted
859 if {[info exists accept_minus_arg] && $accept_minus_arg} {
860 lappend otherargv $arg
861 } else {
862 reportErrorAndExit "Invalid option '$arg'\nTry 'module\
863 --help' for more information."
864 }
865 }
866 default {
867 lappend otherargv $arg
868 }
869 }
870 set prevarg $arg
871 }
872 }
873
874 if {[info exists nextargisextraargv]} {
875 reportErrorAndExit "Missing value for '$prevarg' option\nTry 'module\
876 --help' for more information."
877 }
878 if {[info exists asked_term_width]} {
879 set rangewidth [lindex $::g_config_defs(term_width) 4]
880 if {[string is integer -strict $::asked_term_width] && \
881 $::asked_term_width >= [lindex $rangewidth 0] &&\
882 $::asked_term_width <= [lindex $rangewidth 1]} {
883 set validval 1
884 } else {
885 reportErrorAndExit "Invalid value for option\
886 '$term_width_arg'\nValue should be an integer comprised between\
887 [lindex $rangewidth 0] and [lindex $rangewidth 1]"
888 }
889 }
890
891 setState subcmd [lindex $otherargv 0]
892 set otherargv [list {*}[lreplace $otherargv 0 0] {*}$extraargv]
893 setState subcmd_args $otherargv
894 # call ml frontend if it is asked command
895 if {[getState subcmd] eq {ml}} {
896 set execcmdlist [list ml {*}$otherargv]
897 } else {
898 set execcmdlist [list moduleTop [getState subcmd] {*}$otherargv]
899 }
900
901 # now options are known initialize error report (start pager if enabled)
902 initErrorReport
903
904 # put back quarantine variables in env, if quarantine mechanism supported
905 if {[info exists env(__MODULES_QUARANTINE_SET)] &&\
906 $env(__MODULES_QUARANTINE_SET) eq {1}} {
907 foreach var [getQuarantineVarList 1] {
908 set quarvar __MODULES_QUAR_${var}
909 # put back value
910 if {[info exists env($quarvar)]} {
911 reportDebug "Release '$var' environment variable from\
912 quarantine ($env($quarvar))"
913 set env($var) $env($quarvar)
914 unset env($quarvar)
915 # or unset env var if no value found in quarantine
916 } elseif {[info exists env($var)]} {
917 reportDebug "Unset '$var' environment variable after\
918 quarantine"
919 unset env($var)
920 }
921 }
922 }
923
924 if {$show_help} {
925 if {[getState subcmd] eq {ml}} {
926 reportMlUsage
927 } else {
928 reportUsage
929 }
930 flushAndExit
931 }
932 if {$show_version} {
933 reportVersion
934 flushAndExit
935 }
936 if {$show_name} {
937 reportName
938 flushAndExit
939 }
940
941 # eval needed to pass otherargv as list to module proc
942 {*}$execcmdlist
943 } errMsg ]} {
944 # re-enable error report in case it was previously inhibited
945 setState inhibit_errreport 0
946 # remove any message record id to render next error
947 clearAllMsgRecordId
948 reportFinalError $errMsg
949 # init error report here in case the error raised before the regular init
950 initErrorReport
951 flushAndExit
952 }
953
954 flushAndExit
955
956 # ;;; Local Variables:
957 # ;;; Mode: tcl-mode
958 # ;;; tcl-indent-level: 3
959 # ;;; tcl-continued-indent-level: 3
960 # ;;; indent-tabs-mode: nil
961 # ;;; End:
962 # vim:set tabstop=3 shiftwidth=3 expandtab autoindent: