Artifact
ca9d16713fb01cff83d25bd2e36410fe6c398fdc:
Attachment "segfault.tcl" to
ticket [
2978773fff]
added by
sbron on
2010-03-30 02:25:31.
#!/usr/bin/tclsh
proc getstr {fd var} {
upvar 1 $var line
set cnt [gets $fd line]
puts "*** gets ($cnt): '$line'"
return $cnt
}
proc xyzrequest {s} {
set f [chan create read refchan]
fconfigure $f -blocking 0
chan event $f readable [list xyzexec $f $s]
refchan::output $f "Content-Type: text/html\n"
refchan::output $f "Line 1"
refchan::output $f "Line 2"
refchan::output $f "Line 3"
refchan::done $f
}
proc xyzexec {f s} {
puts [info level 0]
if {[eof $f]} {
# Should not happen - error
} elseif {[getstr $f line] == -1} {
# Not a full line available
puts "Not a full line"
return
} elseif {![string length $line]} {
# Empty line indicates the end of header information
puts "Empty line"
return
} elseif {[regexp {^(\S+)\s*:\s*(.+)} $line -> var val]} {
# Found a valid header line
puts "Header: $var=$val"
return
}
puts "Invalid output"
close $f
puts "Connection from webhost($f) closed, $f"
}
namespace eval refchan {
namespace ensemble create -subcommands {
initialize finalize watch read write
}
proc initialize {id mode} {
puts [info level 0]
variable chan
variable buffer
set chan($id) [dict create watch {} eof 0 event ""]
set buffer($id) ""
return [namespace ensemble configure refchan -subcommands]
}
proc watch {id spec} {
puts [info level 0]
variable chan
dict set chan($id) watch $spec
postevent $id
}
proc read {id count} {
puts [info level 0]
variable chan
variable buffer
if {[info exists chan($id)]} {
puts $chan($id)
if {$buffer($id) eq ""} {
if {[dict get $chan($id) eof]} {
puts "[info level 0] => EOF"
return ""
} else {
puts "[info level 0] => EAGAIN"
return -code error EAGAIN
}
}
# Bug in Tcl? Work-around: Only provide one line per request
set x [string first \n $buffer($id)]
# if {$x >= 0} {set count [expr {$x + 1}]}
set rc [string replace $buffer($id) $count end]
set buffer($id) [string range $buffer($id) $count end]
puts "[info level 0] -> '$rc'"
return $rc
} else {
puts "[info level 0] => EOF"
return ""
}
}
proc finalize id {
puts [info level 0]
variable chan
set after [dict get $chan($id) event]
after cancel $after
unset chan($id)
}
proc output {id str} {
puts [info level 0]
variable chan
variable buffer
if {[info exists chan($id)]} {
append buffer($id) $str \n
postevent $id
}
}
proc done {id} {
puts [info level 0]
variable chan
dict set chan($id) eof 1
postevent $id
}
proc postevent {id} {
variable chan
variable buffer
dict with chan($id) {}
after cancel $event
if {"read" in $watch} {
if {$buffer($id) ne "" || $eof} {
dict set chan($id) event [after 10 [list refchan::postevent $id]]
puts [list chan postevent $id read]
chan postevent $id read
}
}
}
}
xyzrequest stdout
if {[catch {vwait forever}]} exit