Skip to content
Browse files

basic esh in 57 loc. win.

  • Loading branch information...
1 parent 98d8d7f commit eccb38b7809588209a825174152e681938f73486 @jneen committed Jan 10, 2011
Showing with 57 additions and 0 deletions.
  1. +57 −0 bin/esh
View
57 bin/esh
@@ -0,0 +1,57 @@
+#!/bin/bash
+
+fname=$1; shift
+
+read_until() {
+ local glob=$1; shift
+ local var=$1; shift
+
+ local out
+
+ while IFS= read -d"$(echo -e '\004')" -n1 ch; do
+ if [ -z "$ch" ]; then
+ ch="$(echo -e "\n")"
+ fi
+ out="${out}${ch}"
+ case "$out" in
+ *$glob)
+ FOUND=1
+ export $var="${out%$glob}"
+ return 0
+ ;;
+ esac
+ done
+
+ # we've hit an EOF
+ if [ -z "$out" ]; then
+ false
+ else
+ FOUND=0
+ export $var="$out"
+ true
+ fi
+}
+
+compile() {
+ if [ -f "$fname" ]; then
+ cat $fname
+ else
+ cat -
+ fi | compile
+
+ local code
+ local chunk
+ while read_until '<%' chunk; do
+ # escape ' with '\''. sorry everyone.
+ chunk="$(
+ echo "$chunk" | sed "s:':'\\\\'':g"
+ )"
+ echo "echo -n '$chunk'"
+
+ if [ "$FOUND" = 1 ]; then
+ read_until '%>' code
+ echo "$code"
+ fi
+
+ done
+}

0 comments on commit eccb38b

Please sign in to comment.
Something went wrong with that request. Please try again.