1 #!/bin/bash 2 3 SRC="$1" 4 5 if [ -z "${SRC}" ]; then 6 echo "Usage: ttml2cgi <file>" >&2 7 exit 1 8 fi 9 DEST=`echo "${SRC}" | sed s/'.ttml$'/'.cgi'/` 10 if [ "${DEST}" = "${SRC}" ]; then 11 DEST="${DEST}.cgi" 12 fi 13 14 if [ -L "${SRC}" ]; then 15 SRC="`ls -l "${SRC}" | awk '{ print $11 }' | sed s/'.ttml$'/'.cgi'/`" 16 echo "Symlinking ${SRC} to ${DEST} ... done." >&2 17 ln -fs ${SRC} ${DEST} 18 exit 0 19 fi 20 21 echo -n "Converting ${SRC} to ${DEST} ... " >&2 22 23 cat <<EOF >"${DEST}" 24 #!/usr/bin/tcl 25 26 if {![info exists ::GLOBALS]} { 27 set VARS(tmp) "" 28 unset VARS(tmp) 29 set ::GLOBALS "[info globals] GLOBALS" 30 } 31 32 proc hputs {text} { 33 global TTML 34 set TTML(exists) 1 35 if {![info exists TTML(header_sent)]} { set need_header 1 } else { set need_header 0 } 36 if {\$text=="" || \$text=="\n"} { 37 set need_header 0 38 } 39 if {\$need_header} { 40 if {![info exists TTML(header_type)]} { set TTML(header_type) "text/html" } 41 set TTML(header_sent) 1 42 puts "Content-type: \$TTML(header_type)\n" 43 } 44 puts -nonewline \$text 45 } 46 47 proc parse {file} { 48 regsub {\.ttml\$} \$file {.cgi} file 49 if {![file exists \$file]} { error "No such file" } 50 51 uplevel #0 "source \$file" 52 } 53 54 proc hsavevars {{method "form"}} { 55 global GLOBALS VARS 56 set joinchars(form) "\n" 57 set joinchars(get) "&" 58 59 if {![info exists joinchars(\$method)]} { return "" } 60 set newGLOBALS [lindex [intersect3 \$GLOBALS [info globals]] 2] 61 62 set ret "" 63 # set vars [union \$newGLOBALS [array names VARS]] 64 set vars [union [array names VARS] ""] 65 foreach var \$vars { 66 if {[string tolower \$var]=="submit"} { continue } 67 if {[string tolower \$var]=="cancel"} { continue } 68 global \$var 69 if {[info exists VARS(\$var)]} { 70 set val \$VARS(\$var) 71 } else { 72 set val [subst \\\$\$var] 73 } 74 75 switch -- \$method { 76 "form" { 77 lappend ret "<input type=\"hidden\" name=\"\$var\" value=\"[string map {\" \\\"} \$val]\">" 78 } 79 "get" { 80 lappend ret "\$var=[hexcode \$val]" 81 } 82 } 83 } 84 return [join \$ret \$joinchars(\$method)] 85 } 86 87 proc hexcode {text} { 88 set ret "" 89 set is [string length \$text] 90 for {set i 0} {\$i<\$is} {incr i} { 91 set char [string index \$text \$i] 92 if {![ctype alnum \$char]} { 93 set char [format "%%%02x" [ctype ord \$char]] 94 } 95 append ret \$char 96 } 97 return \$ret 98 } 99 100 proc dehexcode {val} { 101 set val [string map {+ \ } \$val] 102 foreach pt [split \$val %] { 103 if {![info exists rval]} { set rval \$pt; continue } 104 set char [ctype char 0x[string range \$pt 0 1]] 105 append rval "\$char[string range \$pt 2 end]" 106 } 107 if {![info exists rval]} { set rval "" } 108 return \$rval 109 } 110 111 proc hgetvars {} { 112 global VARS env 113 if {![info exists env(QUERY_STRING)]} { return } 114 foreach {var val} [split \$env(QUERY_STRING) &=] { set VARS(\$var) [dehexcode \$val] } 115 foreach {var val} [split [gets stdin] &=] { set VARS(\$var) [dehexcode \$val] } 116 } 117 118 catch { 119 EOF 120 121 echo -n " hputs {" >> "${DEST}" 122 sed -e s/'<?'/'};'/g -e s/'?>'/';hputs {'/g -e s/'ENVS'/'env'/g "${SRC}" >> "${DEST}" 123 124 echo " }" >> "${DEST}" 125 cat <<EOF >>"${DEST}" 126 } errmsg 127 128 if {\$errmsg!=""} { 129 hputs "[replicate </td></tr></table> 10]" 130 if {[info procs bgerror]!=""} { 131 bgerror \$errmsg 132 } else { 133 hputs "An internal error has occured." 134 } 135 hputs "</body></html>" 136 } 137 EOF 138 139 chmod +x "${DEST}" 140 141 echo "done." >&2 ttml2cgi.sh is a script that converts TTML to CGI. |