47dcf5fc27 2019-05-01 1: #! /usr/bin/env tclsh
47dcf5fc27 2019-05-01 2:
47dcf5fc27 2019-05-01 3: namespace eval ::minirivet {}
47dcf5fc27 2019-05-01 4:
47dcf5fc27 2019-05-01 5: proc ::minirivet::parseString {string} {
47dcf5fc27 2019-05-01 6:
47dcf5fc27 2019-05-01 7: set fixMap [list]
47dcf5fc27 2019-05-01 8: foreach char [list "\{" "\}" "\\"] {
47dcf5fc27 2019-05-01 9: lappend fixMap $char "\}; puts -nonewline \"\\$char\"; puts -nonewline \{"
47dcf5fc27 2019-05-01 10: }
47dcf5fc27 2019-05-01 11:
47dcf5fc27 2019-05-01 12: set code ""
47dcf5fc27 2019-05-01 13: while {$string ne ""} {
47dcf5fc27 2019-05-01 14: set endIndex [string first "<?" $string]
47dcf5fc27 2019-05-01 15: if {$endIndex == -1} {
47dcf5fc27 2019-05-01 16: set endIndex [expr {[string length $string] + 1}]
47dcf5fc27 2019-05-01 17: }
47dcf5fc27 2019-05-01 18:
47dcf5fc27 2019-05-01 19:
47dcf5fc27 2019-05-01 20: append code "puts -nonewline \{" [string map $fixMap [string range $string 0 $endIndex-1]] "\}; "
47dcf5fc27 2019-05-01 21: set string [string range $string $endIndex end]
47dcf5fc27 2019-05-01 22: set endIndex [string first "?>" $string]
47dcf5fc27 2019-05-01 23: if {$endIndex == -1} {
47dcf5fc27 2019-05-01 24: set endIndex [expr {[string length $string] + 1}]
47dcf5fc27 2019-05-01 25: }
47dcf5fc27 2019-05-01 26:
47dcf5fc27 2019-05-01 27: set work [string range $string 0 2]
47dcf5fc27 2019-05-01 28: if {$work eq "<?="} {
47dcf5fc27 2019-05-01 29: set startIndex 3
47dcf5fc27 2019-05-01 30: append code "puts -nonewline [string trim [string range $string 3 $endIndex-1]]; "
47dcf5fc27 2019-05-01 31: } else {
47dcf5fc27 2019-05-01 32: append code [string range $string 2 $endIndex-1] "\n"
47dcf5fc27 2019-05-01 33: }
47dcf5fc27 2019-05-01 34:
47dcf5fc27 2019-05-01 35: set string [string range $string $endIndex+2 end]
47dcf5fc27 2019-05-01 36:
47dcf5fc27 2019-05-01 37:
47dcf5fc27 2019-05-01 38: }
47dcf5fc27 2019-05-01 39:
47dcf5fc27 2019-05-01 40: tailcall namespace eval ::request $code
47dcf5fc27 2019-05-01 41: }
47dcf5fc27 2019-05-01 42:
47dcf5fc27 2019-05-01 43: proc ::minirivet::parse {file} {
47dcf5fc27 2019-05-01 44: set fd [open $file]
47dcf5fc27 2019-05-01 45: set data [read $fd]
47dcf5fc27 2019-05-01 46: close $fd
47dcf5fc27 2019-05-01 47: tailcall parseString $data
47dcf5fc27 2019-05-01 48: }
47dcf5fc27 2019-05-01 49:
47dcf5fc27 2019-05-01 50: package provide minirivet 1