digimaus

Untitled

Aug 27th, 2026
23
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.59 KB | None | 0 0
  1. # ================================================================
  2. # doorunit.tcl
  3. # Turbo Pascal DOORUNIT.PAS → Tcl port
  4. # By Sean Dennis (Cheepware), ported faithfully to Tcl
  5. # ================================================================
  6.  
  7. namespace eval doorunit {
  8.  
  9. # ------------------------------------------------------------
  10. # Global variables
  11. # ------------------------------------------------------------
  12. variable ForceANSI 0
  13. variable Loc 0
  14. variable PausePrompt 1
  15. variable ScreenClear 1
  16. variable Doorname ""
  17. variable Path ""
  18. variable ErrorLog ""
  19. variable CYear "2025"
  20.  
  21. variable Days {
  22. Sunday Monday Tuesday Wednesday Thursday Friday Saturday
  23. }
  24. variable Months {
  25. January February March April May June July August September October November December
  26. }
  27.  
  28. # ------------------------------------------------------------
  29. # Basic helpers
  30. # ------------------------------------------------------------
  31. proc AllCaps {s} { return [string toupper $s] }
  32. proc BooleanToStr {b} { if {$b} {return "True"} else {return "False"} }
  33. proc IntToStr {n} { return $n }
  34. proc StrToInt {s} { if {[string is integer -strict $s]} {return $s} else {return 0} }
  35.  
  36. proc Dup {ch times} { return [string repeat $ch $times] }
  37.  
  38. proc NL {} {
  39. puts ""
  40. flush stdout
  41. }
  42.  
  43. # ------------------------------------------------------------
  44. # Logging
  45. # ------------------------------------------------------------
  46. proc Log {em} {
  47. variable ErrorLog
  48.  
  49. if {$ErrorLog eq ""} {
  50. puts stderr "[clock format [clock seconds]] :: $em"
  51. return
  52. }
  53.  
  54. set ts "[clock format [clock seconds] -format {%Y-%m-%d %H:%M:%S}]"
  55. set f [open $ErrorLog a]
  56. puts $f "$ts: $em"
  57. close $f
  58. }
  59.  
  60. proc OpenLog {} {
  61. variable ErrorLog
  62. variable Doorname
  63.  
  64. if {$ErrorLog eq ""} {
  65. set ErrorLog "${Doorname}_error.log"
  66. }
  67.  
  68. if {[file exists $ErrorLog]} {
  69. set f [open $ErrorLog a]
  70. puts $f ""
  71. close $f
  72. } else {
  73. set f [open $ErrorLog w]
  74. puts $f "$Doorname Error Log"
  75. puts $f [Dup "=" 79]
  76. close $f
  77. }
  78. }
  79.  
  80. proc CloseLog {} { return }
  81.  
  82. proc CloseDoor {} {
  83. CloseLog
  84. puts -nonewline "\033
  85.  
  86. \[0m"
  87. flush stdout
  88. }
  89.  
  90. # ------------------------------------------------------------
  91. # Strip helpers
  92. # ------------------------------------------------------------
  93. proc StripLead {st ch} { return [string trimleft $st $ch] }
  94. proc StripTrail {st ch} { return [string trimright $st $ch] }
  95. proc StripBoth {st ch} { return [string trim $st $ch] }
  96.  
  97. # ------------------------------------------------------------
  98. # Path helpers
  99. # ------------------------------------------------------------
  100. proc GetFileName {instring} {
  101. set instring [StripBoth $instring " "]
  102. return [file tail $instring]
  103. }
  104.  
  105. proc GetFilePath {instring} {
  106. set instring [StripBoth $instring " "]
  107. set dir [file dirname $instring]
  108. if {$dir eq "."} { return "" }
  109. return "$dir/"
  110. }
  111.  
  112. proc CurrentPath {filename} {
  113. return "[GetFilePath $filename][GetFileName $filename]"
  114. }
  115.  
  116. # ------------------------------------------------------------
  117. # Cursor control
  118. # ------------------------------------------------------------
  119. proc CursorHide {} {
  120. puts -nonewline "\033
  121.  
  122. \[?25l"
  123. flush stdout
  124. }
  125.  
  126. proc CursorShow {} {
  127. puts -nonewline "\033
  128.  
  129. \[?25h"
  130. flush stdout
  131. }
  132.  
  133. # ------------------------------------------------------------
  134. # SmartDelay / DKeyPressed
  135. # ------------------------------------------------------------
  136. proc SmartDelay {ms} { after $ms }
  137.  
  138. proc DKeyPressed {} {
  139. while {![eof stdin]} {
  140. if {[gets stdin line] >= 0} { break }
  141. SmartDelay 200
  142. SmartDelay 200
  143. SmartDelay 200
  144. SmartDelay 200
  145. }
  146. }
  147.  
  148. # ------------------------------------------------------------
  149. # DOY (Day of Year)
  150. # ------------------------------------------------------------
  151. proc DOY {} {
  152. set now [clock seconds]
  153. return [StrToInt [clock format $now -format "%j"]]
  154. }
  155.  
  156. # ------------------------------------------------------------
  157. # File existence
  158. # ------------------------------------------------------------
  159. proc FExist {fn} {
  160. return [file exists $fn]
  161. }
  162.  
  163. # ------------------------------------------------------------
  164. # GoLocal
  165. # ------------------------------------------------------------
  166. proc GoLocal {} {
  167. variable Loc
  168. variable Doorname
  169.  
  170. doorunit::clear
  171. IceText "* $Doorname Local Login" 1 0
  172. IceText "* Enter your name or press <ENTER> for 'SysOp': " 1 0
  173.  
  174. flush stdout
  175. gets stdin name
  176. if {$name eq ""} { set name "SysOp" }
  177.  
  178. set Loc 1
  179. Log "Local login as $name"
  180. }
  181.  
  182. # ------------------------------------------------------------
  183. # IceText — character-by-character color output
  184. # ------------------------------------------------------------
  185. proc IceText {tempStr lf centered} {
  186. set tlen [string length $tempStr]
  187.  
  188. if {$centered} {
  189. set col [expr {40 - ($tlen / 2)}]
  190. puts -nonewline "\033
  191.  
  192. \[${col}G"
  193. }
  194.  
  195. for {set i 0} {$i < $tlen} {incr i} {
  196. set ch [string index $tempStr $i]
  197. set ord [scan $ch %c]
  198.  
  199. if {$ord >= 65 && $ord <= 90} {
  200. puts -nonewline "\033
  201.  
  202. \[97m$ch"
  203. } elseif {$ord >= 97 && $ord <= 122} {
  204. puts -nonewline "\033
  205.  
  206. \[96m$ch"
  207. } elseif {$ord > 127 || $ord < 32} {
  208. puts -nonewline "\033
  209.  
  210. \[36m$ch"
  211. } else {
  212. puts -nonewline "\033
  213.  
  214. \[36m$ch"
  215. }
  216. }
  217.  
  218. if {$lf} { NL }
  219. puts -nonewline "\033
  220.  
  221. \[0m"
  222. flush stdout
  223. }
  224.  
  225. # ------------------------------------------------------------
  226. # SRFColor — random colors
  227. # ------------------------------------------------------------
  228. proc SRFColor {s centered} {
  229. set len [string length $s]
  230.  
  231. if {$centered} {
  232. set col [expr {40 - ($len / 2)}]
  233. puts -nonewline "\033
  234.  
  235. \[${col}G"
  236. }
  237.  
  238. for {set i 0} {$i < $len} {incr i} {
  239. set x [expr {1 + int(rand() * 15)}]
  240. puts -nonewline "\033
  241.  
  242. \[[expr {30 + $x}]m[string index $s $i]"
  243. }
  244.  
  245. puts ""
  246. puts -nonewline "\033
  247.  
  248. \[0m"
  249. flush stdout
  250. }
  251.  
  252. # ------------------------------------------------------------
  253. # PipeStringLength — visible length ignoring pipe codes
  254. # ------------------------------------------------------------
  255. proc PipeStringLength {st} {
  256. if {[string length $st] < 1} { return 0 }
  257.  
  258. set out ""
  259. set i 0
  260. set len [string length $st]
  261.  
  262. while {$i < $len} {
  263. set ch [string index $st $i]
  264.  
  265. if {$ch eq "`"} {
  266. if {$i + 2 < $len} {
  267. set c1 [string index $st [expr {$i+1}]]
  268. set c2 [string index $st [expr {$i+2}]]
  269.  
  270. if {[string match {[0-7A-F]} $c1] &&
  271. [string match {[0-9A-F]} $c2]} {
  272. incr i 3
  273. continue
  274. }
  275. }
  276. }
  277.  
  278. append out $ch
  279. incr i
  280. }
  281.  
  282. return [string length $out]
  283. }
  284.  
  285. # ------------------------------------------------------------
  286. # pWrite — full pipe/hex color parser
  287. # ------------------------------------------------------------
  288. proc pWrite {inp nl centered} {
  289. variable Loc
  290.  
  291. if {$centered} {
  292. set vislen [PipeStringLength $inp]
  293. set col [expr {40 - ($vislen / 2)}]
  294. puts -nonewline "\033
  295.  
  296. \[${col}G"
  297. }
  298.  
  299. set i 0
  300. set len [string length $inp]
  301.  
  302. while {$i < $len} {
  303. set ch [string index $inp $i]
  304.  
  305. # Pipe color `XY or |XY
  306. if {$ch eq "`" || $ch eq "|"} {
  307. if {$i + 2 < $len} {
  308. set c1 [string index $inp [expr {$i+1}]]
  309. set c2 [string index $inp [expr {$i+2}]]
  310.  
  311. if {[string match {[0-9A-F]} $c1] &&
  312. [string match {[0-9A-F]} $c2]} {
  313.  
  314. if {[string is digit -strict $c1]} {
  315. set bg [expr {$c1}]
  316. } else {
  317. set bg [expr {[scan $c1 %c] - 55 - 8}]
  318. }
  319.  
  320. if {[string is digit -strict $c2]} {
  321. set fg [expr {$c2}]
  322. } else {
  323. set fg [expr {[scan $c2 %c] - 55}]
  324. }
  325.  
  326. puts -nonewline "\033
  327.  
  328. \[[expr {40 + $bg}]m"
  329. puts -nonewline "\033
  330.  
  331. \[[expr {30 + $fg}]m"
  332.  
  333. incr i 3
  334. continue
  335. }
  336. }
  337. }
  338.  
  339. # Hex color #RRGGBB
  340. if {$ch eq "#"} {
  341. if {$i + 6 < $len} {
  342. set hex [string range $inp [expr {$i+1}] [expr {$i+6}]]
  343.  
  344. if {[regexp {^[0-9A-Fa-f]{6}$} $hex]} {
  345. set r [scan [string range $hex 0 1] %x]
  346. set g [scan [string range $hex 2 3] %x]
  347. set b [scan [string range $hex 4 5] %x]
  348.  
  349. puts -nonewline "\033
  350.  
  351. \[38;2;${r};${g};${b}m"
  352. incr i 7
  353. continue
  354. }
  355. }
  356. }
  357.  
  358. puts -nonewline $ch
  359. incr i
  360. }
  361.  
  362. if {$nl} { puts "" }
  363. flush stdout
  364. puts -nonewline "\033
  365.  
  366. \[0m"
  367. }
  368.  
  369. # ------------------------------------------------------------
  370. # ReadIn — simple input field
  371. # ------------------------------------------------------------
  372. proc ReadIn {x len up fg bg} {
  373. if {$x > 0} {
  374. puts -nonewline "\033
  375.  
  376. \[${x}G"
  377. }
  378.  
  379. puts -nonewline [string repeat " " $len]
  380. puts -nonewline "\033
  381.  
  382. \[${x}G"
  383.  
  384. flush stdout
  385. gets stdin line
  386.  
  387. if {$up} { set line [string toupper $line] }
  388.  
  389. return $line
  390. }
  391.  
  392. # ------------------------------------------------------------
  393. # ReturnDate / ReturnTime
  394. # ------------------------------------------------------------
  395. proc ReturnDate {style} {
  396. variable Days
  397. variable Months
  398.  
  399. set now [clock seconds]
  400. set y [clock format $now -format %Y]
  401. set m [clock format $now -format %m]
  402. set d [clock format $now -format %d]
  403. set dow [clock format $now -format %w]
  404. set doy [DOY]
  405.  
  406. set m0 [expr {$m - 1}]
  407. set dayname [lindex $Days $dow]
  408. set monthname [lindex $Months $m0]
  409.  
  410. switch -- [string toupper $style] {
  411. "N" { return "$m/$d/$y" }
  412. "D" { return "$dayname $m/$d/$y" }
  413. "F" { return "$dayname $d $monthname $y" }
  414. "O" { return "$dayname $d $monthname $y (day $doy)" }
  415. default { return "$m/$d/$y" }
  416. }
  417. }
  418.  
  419. proc ReturnTime {} {
  420. set now [clock seconds]
  421. set h [clock format $now -format %H]
  422. set m [clock format $now -format %M]
  423. set s [clock format $now -format %S]
  424. return "$h:$m:$s"
  425. }
  426.  
  427. # ------------------------------------------------------------
  428. # TrimFile — keep last N lines
  429. # ------------------------------------------------------------
  430. proc TrimFile {fname numlines} {
  431. if {![file exists $fname]} { return }
  432.  
  433. set lines [split [read [open $fname r]] "\n"]
  434. set count [llength $lines]
  435.  
  436. if {$count <= $numlines} { return }
  437.  
  438. set trimmed [lrange $lines [expr {$count - $numlines}] end]
  439. set f [open $fname w]
  440. foreach line $trimmed { puts $f $line }
  441. close $f
  442. }
  443.  
  444. # ------------------------------------------------------------
  445. # UpKey / UpCaseStr / YesNo
  446. # ------------------------------------------------------------
  447. proc UpKey {} {
  448. while {1} {
  449. flush stdout
  450. gets stdin ch
  451. if {$ch ne ""} { return [string toupper $ch] }
  452. }
  453. }
  454.  
  455. proc UpCaseStr {st} { return [string toupper $st] }
  456.  
  457. proc YesNo {} {
  458. while {1} {
  459. flush stdout
  460. gets stdin ch
  461. if {$ch eq ""} { continue }
  462. set ch [string toupper $ch]
  463. if {$ch eq "Y"} { return 1 }
  464. if {$ch eq "N"} { return 0 }
  465. }
  466. }
  467.  
  468. # ------------------------------------------------------------
  469. # CommandLineHelp
  470. # ------------------------------------------------------------
  471. proc CommandLineHelp {} {
  472. variable Doorname
  473. variable CYear
  474.  
  475. puts "\n"
  476. IceText "$Doorname door" 1 0
  477. IceText "(C) $CYear Sean Dennis" 1 0
  478. IceText "Another Cheepware production!" 1 0
  479. IceText "========================================================================" 1 0
  480. pWrite "`03Example: `0A$Doorname.tcl --dropfile=/path/to/DOOR.SYS" 1 0
  481. pWrite " `0A$Doorname.tcl --node=%1 --dropfile=/path/to/DORINFO1.DEF" 1 0
  482. NL
  483. pWrite "`0E--node `0BNode number" 1 0
  484. pWrite "`0E--dropfile `0BFull path and filename of dropfile" 1 0
  485. pWrite "`0E--force-ansi `0BForce ANSI emulation" 1 0
  486. pWrite "`0E--local `0BRun in local mode" 1 0
  487. pWrite "`0E--help `0BView this help screen" 1 0
  488. NL
  489. exit 0
  490. }
  491.  
  492. # ------------------------------------------------------------
  493. # StartDoor — Tcl version of Pascal argument parser
  494. # ------------------------------------------------------------
  495. proc StartDoor {dname argv} {
  496. variable Loc
  497. variable ForceANSI
  498. variable ScreenClear
  499. variable PausePrompt
  500. variable ErrorLog
  501. variable Doorname
  502.  
  503. set Loc 0
  504. set ForceANSI 0
  505. set ScreenClear 1
  506. set PausePrompt 1
  507. set ErrorLog "error.log"
  508. set Doorname $dname
  509.  
  510. foreach arg $argv {
  511. set argU [string toupper $arg]
  512.  
  513. if {[string match "-C*" $argU]} {
  514. if {$dname eq "QOTD"} { set ScreenClear 0 }
  515. } elseif {[string match "-D*" $argU]} {
  516. set temp [string range $argU 2 end]
  517. set dropfile [GetFileName $temp]
  518. set path [GetFilePath $temp]
  519. } elseif {[string match "-F*" $argU]} {
  520. set ForceANSI 1
  521. } elseif {[string match "-L*" $argU]} {
  522. set Loc 1
  523. GoLocal
  524. } elseif {[string match "-N*" $argU]} {
  525. continue
  526. } elseif {[string match "-P*" $argU]} {
  527. if {$dname eq "QOTD"} { set PausePrompt 0 }
  528. } elseif {[string match "-?*" $argU]} {
  529. CommandLineHelp
  530. } else {
  531. CommandLineHelp
  532. }
  533. }
  534.  
  535. OpenLog
  536. }
  537. }
  538.  
Advertisement
Add Comment
Please, Sign In to add comment