global V me N form input
set N(FORM) 0
set N(INPUT) 0
set form 0
set input 0
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# The FORM tag has several attributes:
# ACTION="href" CGI script to process form.
# METHOD=GET|POST args are in URL or STDIN.
# ENCTYPE=encoding says how to encode the data.
#
proc tagFORM {w tag att} {
global V me in N form Form Input
if {$V>2} {puts "$me/tagFORM: w={$w} att={$att}"}
incr N(FORM)
set f [incr form]
set Input(I$f) {}
set Form(A$f) {}
set Form(M$f) {}
set Form(E$f) {application/x-www-form-urlencoded}
if {$V>2} {puts "$me/tagFORM: form $form N(FORM) $N(FORM)"}
if [regexp -nocase {ACTION="*([^ "]+)"*} $att {} x] {
if {$V>2} {puts "$me/tagFORM: ACTION=\"$x\""}
set Form(A$f) $x
} else {
if {$V>2} {puts "$me/tagFORM: ACTION not found for form $f."}
}
if [regexp -nocase {METHOD="*([^ "]+)"*} $att {} x] {
if {$V>2} {puts "$me/tagFORM: METHOD=\"$x\""}
set Form(M$f) $x
} else {
if {$V>2} {puts "$me/tagFORM: METHOD not found for form $f, default to GET."}
set Form(M$f) GET
}
if [regexp -nocase {ENCTYPE="*([^ "]+)"*} $att {} x] {
if {$V>2} {puts "$me/tagFORM: ENCTYPE=\"$x\""}
set Form(E$f) $x
}
if {$V>2} {puts "$me/tagFORM: ACTION \"$Form(A$f)\""}
if {$V>2} {puts "$me/tagFORM: METHOD \"$Form(M$f)\""}
if {$V>2} {puts "$me/tagFORM: ENCTYPE \"$Form(E$f)\""}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Submitting a form gets intricate. For GET forms, it's not too bad: The #
# params are encoded and appended to the URL, with "?" as the first separator #
# and "&" as the rest of the separators. For POST forms, much of the cruft #
# has been encapsulated in the w3post program. We just need to send the data #
# to w3post; it handles the HTTP header, converts newlines into "&", counts #
# the chars, and all that. #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
proc SubmitForm {f i} {
global V me in docFil form Form Input input Check Button
set id "$me/SubmitForm"
if {$V>2} {puts "$id: FORM $f because input $i was pushed."}
if {$V>2} {puts "$id: ACTION \"$Form(A$f)\""}
if {$V>2} {puts "$id: METHOD \"$Form(M$f)\""}
if {$V>2} {puts "$id: ENCTYPE \"$Form(E$f)\""}
switch [string toupper $Form(M$f)] {
GET {
if {$V>2} {puts "$id: Build URL for GET ..."}
set U "$Form(A$f)"
if {$V>2} {puts "$id: Input(I$f)={$Input(I$f)}"}
set sep ?
foreach n $Input(I$f) {
if {$V>2} {puts "$id: URL=\"$U\""}
if {$V>2} {puts "$id: INPUT $i type $Input(T$n) ..."}
switch $Input(T$n) {
SUBMIT {
if {$n == $i} { # Ignore if not the one pushed.
set v [eval $Input(E$n)]
} else {
set v {}
}
if {$V>2} {puts "$id: SUBMIT i=$i n=$n v=\"$v\""}
}
RADIO {
if {$V>2} {puts "$id: RADIO Input(N$n)={$Input(N$n)} "}
if {$V>2} {puts "$id: RADIO Input(V$n)={$Input(V$n)} "}
if {$V>2} {puts "$id: RADIO Button($Input(N$n))={$Button($Input(N$n))}"}
if {[info exists Button($Input(N$n))]
&& $Button($Input(N$n)) == $Input(V$n)} { # Ignore if not the one pushed.
if {$V>2} {puts "$id: RADIO button $Input(N$n)=$Input(V$n) pushed. "}
set v $Input(V$n)
} else {
set v {}
}
if {$V>2} {puts "$id: SUBMIT i=$i n=$n v=\"$v\""}
}
RESET {
if {$V>1} {puts "$id RESET button pressed."}
foreach x [array names Input] {
if {$V>1} {puts "$id RESET Input($x) = \"$Input($x)\""}
set Input($x) {}
}
if {$V>1} {puts "$id RESET button returns to current form."}
return
}
default {
if {[set v $Input(V$n)] != {}} {
if {$V>2} {puts "$id: Input(V$n) is \"$v\""}
} elseif [info exists Input(E$n)] {
set v [eval $Input(E$n)]
if {$V>2} {puts "$id: Input(E$n) gave \"$v\""}
} else {
set v {}
if {$V>2} {puts "$id: Input(E$n) has no value."}
}
}
}
if {$v != {}} {
set VV [URL $v]
if {$V>2} {puts "$id: Input($n) \"$v\" => \"$VV\""}
append U "$sep$Input(N$n)=$VV"
set sep &
}
}
if {$V>2} {puts "$id: URL \"$U\""}
LoadFile "" "" "$U" GET
}
POST {
if {$V>2} {puts "$id: Build URL for POST ..."}
set U "$Form(A$f)"
if {$V>2} {puts "$id: Input(I$f)={$Input(I$f)}"}
set data {}
LoadFile "" "" "$U" POST
if {$docFil == {}} {
if {$V>0} {Msg "$id: LoadFile didn't produce docFil"}
return
}
if {$V>2} {puts "$id: Send form data to $docFil ..."}
foreach n $Input(I$f) {
set nm $Input(N$n)
if {$V>2} {puts "$id: Input(N$n) is \"$nm\""}
if {[set v $Input(V$n)] != {}} {
if {$V>2} {puts "$id: Input(V$n) is \"$v\""}
} elseif {[info exists Input(E$n)] && $Input(E$n) != {}} {
set v [eval $Input(E$n)]
if {$V>2} {puts "$id: Input(E$n) gave \"$v\""}
} else {
set v {}
if {$V>2} {puts "$id: Input(E$n) has no value."}
}
if {$v != {}} {
set V [URL $v]
if {$V>2} {puts "$id: Send {$nm=$V}"}
puts $docFil "$nm=$V"
}
}
if {$V>2} {puts "$id: Send blank line."}
puts $docFil ""
flush $docFil
}
default {
if {$V>2} {puts "$id: Unknown METHOD \"$Form(M$f)\" for form $f"}
}
}
if {$V>2} {puts "$id: Done."}
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - #
# Process an tag. For each such tag within a form, we leave behind a
# number of globals for use by SubmitForm:
# input the index of this tag item.
# Input(I$f) list of input items for form f.
# Input(T$i) item's type.
# Input(V$i) item's value, if known.
# Input(E$i) expression to get the item's value.
# Input(S$i) display size (width in chars) of item.
# Input(M$i) max size (width in chars) of item's value.
# Input(N$i) name of input item.
# In addition to tags, input items are generated by several other
# tags, such as