Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ================================================================
- # doorunit.tcl
- # Turbo Pascal DOORUNIT.PAS → Tcl port
- # By Sean Dennis (Cheepware), ported faithfully to Tcl
- # ================================================================
- namespace eval doorunit {
- # ------------------------------------------------------------
- # Global variables
- # ------------------------------------------------------------
- variable ForceANSI 0
- variable Loc 0
- variable PausePrompt 1
- variable ScreenClear 1
- variable Doorname ""
- variable Path ""
- variable ErrorLog ""
- variable CYear "2025"
- variable Days {
- Sunday Monday Tuesday Wednesday Thursday Friday Saturday
- }
- variable Months {
- January February March April May June July August September October November December
- }
- # ------------------------------------------------------------
- # Basic helpers
- # ------------------------------------------------------------
- proc AllCaps {s} { return [string toupper $s] }
- proc BooleanToStr {b} { if {$b} {return "True"} else {return "False"} }
- proc IntToStr {n} { return $n }
- proc StrToInt {s} { if {[string is integer -strict $s]} {return $s} else {return 0} }
- proc Dup {ch times} { return [string repeat $ch $times] }
- proc NL {} {
- puts ""
- flush stdout
- }
- # ------------------------------------------------------------
- # Logging
- # ------------------------------------------------------------
- proc Log {em} {
- variable ErrorLog
- if {$ErrorLog eq ""} {
- puts stderr "[clock format [clock seconds]] :: $em"
- return
- }
- set ts "[clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S}]"
- set f [open $ErrorLog a]
- puts $f "$ts: $em"
- close $f
- }
- proc OpenLog {} {
- variable ErrorLog
- variable Doorname
- if {$ErrorLog eq ""} {
- set ErrorLog "${Doorname}_error.log"
- }
- if {[file exists $ErrorLog]} {
- set f [open $ErrorLog a]
- puts $f ""
- close $f
- } else {
- set f [open $ErrorLog w]
- puts $f "$Doorname Error Log"
- puts $f [Dup "=" 79]
- close $f
- }
- }
- proc CloseLog {} { return }
- proc CloseDoor {} {
- CloseLog
- puts -nonewline "\033
- \[0m"
- flush stdout
- }
- # ------------------------------------------------------------
- # Strip helpers
- # ------------------------------------------------------------
- proc StripLead {st ch} { return [string trimleft $st $ch] }
- proc StripTrail {st ch} { return [string trimright $st $ch] }
- proc StripBoth {st ch} { return [string trim $st $ch] }
- # ------------------------------------------------------------
- # Path helpers
- # ------------------------------------------------------------
- proc GetFileName {instring} {
- set instring [StripBoth $instring " "]
- return [file tail $instring]
- }
- proc GetFilePath {instring} {
- set instring [StripBoth $instring " "]
- set dir [file dirname $instring]
- if {$dir eq "."} { return "" }
- return "$dir/"
- }
- proc CurrentPath {filename} {
- return "[GetFilePath $filename][GetFileName $filename]"
- }
- # ------------------------------------------------------------
- # Cursor control
- # ------------------------------------------------------------
- proc CursorHide {} {
- puts -nonewline "\033
- \[?25l"
- flush stdout
- }
- proc CursorShow {} {
- puts -nonewline "\033
- \[?25h"
- flush stdout
- }
- # ------------------------------------------------------------
- # SmartDelay / DKeyPressed
- # ------------------------------------------------------------
- proc SmartDelay {ms} { after $ms }
- proc DKeyPressed {} {
- while {![eof stdin]} {
- if {[gets stdin line] >= 0} { break }
- SmartDelay 200
- SmartDelay 200
- SmartDelay 200
- SmartDelay 200
- }
- }
- # ------------------------------------------------------------
- # DOY (Day of Year)
- # ------------------------------------------------------------
- proc DOY {} {
- set now [clock seconds]
- return [StrToInt [clock format $now -format "%j"]]
- }
- # ------------------------------------------------------------
- # File existence
- # ------------------------------------------------------------
- proc FExist {fn} {
- return [file exists $fn]
- }
- # ------------------------------------------------------------
- # GoLocal
- # ------------------------------------------------------------
- proc GoLocal {} {
- variable Loc
- variable Doorname
- doorunit::clear
- IceText "* $Doorname Local Login" 1 0
- IceText "* Enter your name or press <ENTER> for 'SysOp': " 1 0
- flush stdout
- gets stdin name
- if {$name eq ""} { set name "SysOp" }
- set Loc 1
- Log "Local login as $name"
- }
- # ------------------------------------------------------------
- # IceText — character-by-character color output
- # ------------------------------------------------------------
- proc IceText {tempStr lf centered} {
- set tlen [string length $tempStr]
- if {$centered} {
- set col [expr {40 - ($tlen / 2)}]
- puts -nonewline "\033
- \[${col}G"
- }
- for {set i 0} {$i < $tlen} {incr i} {
- set ch [string index $tempStr $i]
- set ord [scan $ch %c]
- if {$ord >= 65 && $ord <= 90} {
- puts -nonewline "\033
- \[97m$ch"
- } elseif {$ord >= 97 && $ord <= 122} {
- puts -nonewline "\033
- \[96m$ch"
- } elseif {$ord > 127 || $ord < 32} {
- puts -nonewline "\033
- \[36m$ch"
- } else {
- puts -nonewline "\033
- \[36m$ch"
- }
- }
- if {$lf} { NL }
- puts -nonewline "\033
- \[0m"
- flush stdout
- }
- # ------------------------------------------------------------
- # SRFColor — random colors
- # ------------------------------------------------------------
- proc SRFColor {s centered} {
- set len [string length $s]
- if {$centered} {
- set col [expr {40 - ($len / 2)}]
- puts -nonewline "\033
- \[${col}G"
- }
- for {set i 0} {$i < $len} {incr i} {
- set x [expr {1 + int(rand() * 15)}]
- puts -nonewline "\033
- \[[expr {30 + $x}]m[string index $s $i]"
- }
- puts ""
- puts -nonewline "\033
- \[0m"
- flush stdout
- }
- # ------------------------------------------------------------
- # PipeStringLength — visible length ignoring pipe codes
- # ------------------------------------------------------------
- proc PipeStringLength {st} {
- if {[string length $st] < 1} { return 0 }
- set out ""
- set i 0
- set len [string length $st]
- while {$i < $len} {
- set ch [string index $st $i]
- if {$ch eq "`"} {
- if {$i + 2 < $len} {
- set c1 [string index $st [expr {$i+1}]]
- set c2 [string index $st [expr {$i+2}]]
- if {[string match {[0-7A-F]} $c1] &&
- [string match {[0-9A-F]} $c2]} {
- incr i 3
- continue
- }
- }
- }
- append out $ch
- incr i
- }
- return [string length $out]
- }
- # ------------------------------------------------------------
- # pWrite — full pipe/hex color parser
- # ------------------------------------------------------------
- proc pWrite {inp nl centered} {
- variable Loc
- if {$centered} {
- set vislen [PipeStringLength $inp]
- set col [expr {40 - ($vislen / 2)}]
- puts -nonewline "\033
- \[${col}G"
- }
- set i 0
- set len [string length $inp]
- while {$i < $len} {
- set ch [string index $inp $i]
- # Pipe color `XY or |XY
- if {$ch eq "`" || $ch eq "|"} {
- if {$i + 2 < $len} {
- set c1 [string index $inp [expr {$i+1}]]
- set c2 [string index $inp [expr {$i+2}]]
- if {[string match {[0-9A-F]} $c1] &&
- [string match {[0-9A-F]} $c2]} {
- if {[string is digit -strict $c1]} {
- set bg [expr {$c1}]
- } else {
- set bg [expr {[scan $c1 %c] - 55 - 8}]
- }
- if {[string is digit -strict $c2]} {
- set fg [expr {$c2}]
- } else {
- set fg [expr {[scan $c2 %c] - 55}]
- }
- puts -nonewline "\033
- \[[expr {40 + $bg}]m"
- puts -nonewline "\033
- \[[expr {30 + $fg}]m"
- incr i 3
- continue
- }
- }
- }
- # Hex color #RRGGBB
- if {$ch eq "#"} {
- if {$i + 6 < $len} {
- set hex [string range $inp [expr {$i+1}] [expr {$i+6}]]
- if {[regexp {^[0-9A-Fa-f]{6}$} $hex]} {
- set r [scan [string range $hex 0 1] %x]
- set g [scan [string range $hex 2 3] %x]
- set b [scan [string range $hex 4 5] %x]
- puts -nonewline "\033
- \[38;2;${r};${g};${b}m"
- incr i 7
- continue
- }
- }
- }
- puts -nonewline $ch
- incr i
- }
- if {$nl} { puts "" }
- flush stdout
- puts -nonewline "\033
- \[0m"
- }
- # ------------------------------------------------------------
- # ReadIn — simple input field
- # ------------------------------------------------------------
- proc ReadIn {x len up fg bg} {
- if {$x > 0} {
- puts -nonewline "\033
- \[${x}G"
- }
- puts -nonewline [string repeat " " $len]
- puts -nonewline "\033
- \[${x}G"
- flush stdout
- gets stdin line
- if {$up} { set line [string toupper $line] }
- return $line
- }
- # ------------------------------------------------------------
- # ReturnDate / ReturnTime
- # ------------------------------------------------------------
- proc ReturnDate {style} {
- variable Days
- variable Months
- set now [clock seconds]
- set y [clock format $now -format %Y]
- set m [clock format $now -format %m]
- set d [clock format $now -format %d]
- set dow [clock format $now -format %w]
- set doy [DOY]
- set m0 [expr {$m - 1}]
- set dayname [lindex $Days $dow]
- set monthname [lindex $Months $m0]
- switch -- [string toupper $style] {
- "N" { return "$m/$d/$y" }
- "D" { return "$dayname $m/$d/$y" }
- "F" { return "$dayname $d $monthname $y" }
- "O" { return "$dayname $d $monthname $y (day $doy)" }
- default { return "$m/$d/$y" }
- }
- }
- proc ReturnTime {} {
- set now [clock seconds]
- set h [clock format $now -format %H]
- set m [clock format $now -format %M]
- set s [clock format $now -format %S]
- return "$h:$m:$s"
- }
- # ------------------------------------------------------------
- # TrimFile — keep last N lines
- # ------------------------------------------------------------
- proc TrimFile {fname numlines} {
- if {![file exists $fname]} { return }
- set lines [split [read [open $fname r]] "\n"]
- set count [llength $lines]
- if {$count <= $numlines} { return }
- set trimmed [lrange $lines [expr {$count - $numlines}] end]
- set f [open $fname w]
- foreach line $trimmed { puts $f $line }
- close $f
- }
- # ------------------------------------------------------------
- # UpKey / UpCaseStr / YesNo
- # ------------------------------------------------------------
- proc UpKey {} {
- while {1} {
- flush stdout
- gets stdin ch
- if {$ch ne ""} { return [string toupper $ch] }
- }
- }
- proc UpCaseStr {st} { return [string toupper $st] }
- proc YesNo {} {
- while {1} {
- flush stdout
- gets stdin ch
- if {$ch eq ""} { continue }
- set ch [string toupper $ch]
- if {$ch eq "Y"} { return 1 }
- if {$ch eq "N"} { return 0 }
- }
- }
- # ------------------------------------------------------------
- # CommandLineHelp
- # ------------------------------------------------------------
- proc CommandLineHelp {} {
- variable Doorname
- variable CYear
- puts "\n"
- IceText "$Doorname door" 1 0
- IceText "(C) $CYear Sean Dennis" 1 0
- IceText "Another Cheepware production!" 1 0
- IceText "========================================================================" 1 0
- pWrite "`03Example: `0A$Doorname.tcl --dropfile=/path/to/DOOR.SYS" 1 0
- pWrite " `0A$Doorname.tcl --node=%1 --dropfile=/path/to/DORINFO1.DEF" 1 0
- NL
- pWrite "`0E--node `0BNode number" 1 0
- pWrite "`0E--dropfile `0BFull path and filename of dropfile" 1 0
- pWrite "`0E--force-ansi `0BForce ANSI emulation" 1 0
- pWrite "`0E--local `0BRun in local mode" 1 0
- pWrite "`0E--help `0BView this help screen" 1 0
- NL
- exit 0
- }
- # ------------------------------------------------------------
- # StartDoor — Tcl version of Pascal argument parser
- # ------------------------------------------------------------
- proc StartDoor {dname argv} {
- variable Loc
- variable ForceANSI
- variable ScreenClear
- variable PausePrompt
- variable ErrorLog
- variable Doorname
- set Loc 0
- set ForceANSI 0
- set ScreenClear 1
- set PausePrompt 1
- set ErrorLog "error.log"
- set Doorname $dname
- foreach arg $argv {
- set argU [string toupper $arg]
- if {[string match "-C*" $argU]} {
- if {$dname eq "QOTD"} { set ScreenClear 0 }
- } elseif {[string match "-D*" $argU]} {
- set temp [string range $argU 2 end]
- set dropfile [GetFileName $temp]
- set path [GetFilePath $temp]
- } elseif {[string match "-F*" $argU]} {
- set ForceANSI 1
- } elseif {[string match "-L*" $argU]} {
- set Loc 1
- GoLocal
- } elseif {[string match "-N*" $argU]} {
- continue
- } elseif {[string match "-P*" $argU]} {
- if {$dname eq "QOTD"} { set PausePrompt 0 }
- } elseif {[string match "-?*" $argU]} {
- CommandLineHelp
- } else {
- CommandLineHelp
- }
- }
- OpenLog
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment