#!/bin/sh { excitable="text" "exec" "awk" "-f" "$0" "$@"; } # -*- awk -*- # usage: et | sh BEGIN { # Tuneables postproc = postproc ? postproc : "shexpand" bodyfunc = bodyfunc ? bodyfunc : "body" # Globals wrpfx = ";:"; shxpfx = "!shx!"; def_docend = "---end---" pretext = bodyfunc"(){\n"; posttext = "}\n"bodyfunc # Ask sed to do these b/c awk has no capture groups ;_; shellfix = "sed -E" \ " -e 's/`/\\\\`/g'" \ " -e 's/(^|[^\\$])\\$([^\\$]|$)/\\1\\\\$\\2/g'" \ " -e 's/(^|[^\\$])\\$(\\$+)([^\\$]|$)/\\1\\2\\3/g'" shxwrap = " -e 's/^/"wrpfx"/'" htmlfix = "sed -E" \ " -e 's#([^\\\\]|^)&#\\1\\&#g'" \ " -e 's#([^\\\\]|^)<#\\1\\<#g'" \ " -e 's#([^\\\\]|^)>#\\1\\>#g'" \ " -e 's#\\\\([&<>])#\\1#g'" true = 1 ; false = 0 print "### excitable text ###" print "wrap()(sed \"s/^/$1/\");unwrap()(sed \"s/^$1//\")" print "shexpand()(eval"\ " \"$(:;(echo 'cat<<.';sed 's/^/"shxpfx"/';echo .)"\ "|unwrap "shxpfx")\")" print "shellfix()(" shellfix ")\nhtmlfix()(" htmlfix ")" print "postproc=" postproc } end[endn] && $0 == end[endn] { par = par "\n" end[endn--] "\n" ")" printpar() subdocp = false next } /\\$/ { getline nl sub(/\\$/,"") $0 = $0 " " nl } /^\.\.\./ { printpar() docp = !docp docend = $2 ? $2 : def_docend if (docp) print pretext "unwrap '" wrpfx "'" \ "<<'" docend "'|$postproc" else end_text() next } /^\.\./ && docp { subdocp = true end[++endn] = (match($0,"<<") ? substr($0,RSTART+RLENGTH) : "..") command = substr($0, 3, RSTART ? RSTART - 2 : length) $0 = "$$(" (command ? command : "cat") "<<" end[endn] } /^\./ && docp { specialp = 2 gsub(/"/,"\\\\&") ln = "$$(" substr($1, 2) for (f=2;f<=NF;f++) ln = ln " \"" $f "\"" ln = ln ")" $0 = ln } /^$/ { if (!par) next printpar() } { par = par (par?"\n":"") $0 } { if (--specialp < 0) specialp = 0 } END { if (par) printpar() if (docp) end_text() } function end_text() { print docend "\n" posttext } function printpar() { specialp = specialp || (match(par, /^[ ]*" par "

" shx = shellfix shxwrap print par | shx close(shx) } else print par par = "" }