#!/usr/bin/tclsh if { $argc != 1 && $argc != 2 } { puts "Usage: $argv0 \[\]" exit 1 } set procname [lindex $argv 0] set subcmd [lindex $argv 1] set thisdir [file dirname [file normalize $argv0]] set libdir $thisdir/lib set filelist [glob -directory $libdir *] set excludelist { pkgIndex.tcl helgaPkg.tcl } foreach fl $filelist { set flbase [lindex [file split $fl] end] if { [lsearch $excludelist $flbase] == -1 } { source $fl } } set nshead [namespace qualifiers $procname] set ns [namespace tail $nshead] set procbase [namespace tail $procname] set filename $libdir/${ns}.tcl if { $ns == "global" } { if { $subcmd == "" } { set grepString "proc ${procbase}args" set stopString "#\t$procbase" } else { set grepString "proc ${procbase}${subcmd}args" set stopString "#\t$procbase $subcmd" } } else { set stopString "#\t$procbase" set grepString "proc $procbase " } # if our grep errors, it's probably because it didn't find the line it # was looking for. that probably means we're working with one of # hadd, hget, hset, or hremove. use the proc name to get that file. if { [catch "exec grep -n \"$grepString\" $filename" procline] } { set filename $libdir/${procbase}.tcl set procline [exec grep -n $grepString $filename] } set linenum [expr [lindex [split $procline ":"] 0] - 1] set fp [open $filename] set data [read $fp] close $fp set lines [split $data \n] set collecting false set done false set desclines {} for { set i [expr $linenum - 1] } { $i > 0 && !$done } { incr i -1 } { set currentline [lindex $lines $i] set strippedline [string trim $currentline] if { $strippedline == "#" } { if { $collecting } { lappend desclines "\n\n" } else { set collecting true } } elseif { $strippedline == $stopString || $strippedline == "" } { if { $desclines == {} } { set desclines [list "$procbase"] } else { set desclines [lrange $desclines 0 end-1] } set done true } elseif { $collecting } { lappend desclines [string trim [string range $strippedline 1 end]] } } set desc "" set lastbreak false for { set d [expr [llength $desclines] - 1] } { $d > -1 } { incr d -1 } { set line [lindex $desclines $d] if { $line == "\n\n" } { append desc "\n\n" set lastbreak true } else { if { $desc != "" && !$lastbreak } { append desc " " } append desc $line set lastbreak false } } regsub -all < $desc "\\<" desc regsub -all > $desc "\\>" desc regsub -all \n $desc "
" desc puts $desc if { $ns == "global" } { if { $subcmd == "" } { set argproc "${procname}args" } else { set argproc "${procname}${subcmd}args" } puts "proc $argproc \{\} \{" set body [info body $argproc] set bodylines [split $body \n] foreach line $bodylines { set linetrimmed [string trim $line] set linesplit [split $linetrimmed " "] if { [lindex $linesplit 0] == "return" && [lindex $linesplit 1] == "\[list" } { puts -nonewline "    return \[list" set linesplit [lrange $linesplit 2 end] set numChunks [llength $linesplit] for { set i 0 } { $i < [expr $numChunks - 1] } { incr i } { set chunk [lindex $linesplit $i] if { $chunk == "\[list" } { puts "" puts -nonewline "        " } else { puts -nonewline " " } regsub -all < $chunk "\\<" chunk regsub -all > $chunk "\\>" chunk regsub "\t" $chunk "" chunk regsub -all "\t" $chunk "\\ \\ \\ \\ " chunk puts -nonewline $chunk } puts "" puts "    \]" } else { regsub -all < $line "\\<" line regsub -all > $line "\\>" line regsub "\t" $line "" line regsub -all "\t" $line "\\ \\ \\ \\ " line puts $line } } puts "\}" } set args [info args $procname] set argstring "" foreach arg $args { if {[info default $procname $arg value]} { lappend argstring [list $arg $value] } else { lappend argstring $arg } } puts -nonewline "proc $procname " if { $argstring == "args" } { puts -nonewline args } else { puts -nonewline "\{ $argstring \}" } puts " \{" set body [info body $procname] set bodylines [split $body \n] if { $subcmd == "" } { foreach line $bodylines { regsub -all < $line "\\<" line regsub -all > $line "\\>" line regsub "\t" $line "" line regsub -all "\t" $line "\\ \\ \\ \\ " line puts $line } } else { puts "    ..." set collecting false foreach line $bodylines { if { $collecting } { set linetrimmed [string trim $line] set linesplit [split $linetrimmed " "] if { [llength $linesplit] == 2 && [lindex $linesplit 1] == "\{" } { break } regsub -all < $line "\\<" line regsub -all > $line "\\>" line regsub "\t" $line "" line regsub -all "\t" $line "\\ \\ \\ \\ " line puts $line } elseif { [string trim $line] == "$subcmd \{" } { set collecting true regsub "\t" $line "" line regsub -all "\t" $line "\\ \\ \\ \\ " line puts $line } } puts "    ..." } puts "\}"