From a5892287ba6f2b64e883f9f36a8687c3c9f5790c Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Fri, 2 Feb 2024 10:26:52 -0600 Subject: Make everything better --- subtext.awk | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/subtext.awk b/subtext.awk index c45aae4..2eedeeb 100644 --- a/subtext.awk +++ b/subtext.awk @@ -10,20 +10,14 @@ BEGIN { ## Tuneables dryrun = dryrun ? dryrun : 0 sopath = sopath ? sopath : ".:" ENVIRON["HOME"] "/.subtext" - bodyend = bodyend ? bodyend : "_@end@_" + shxend = shxend ? shxend : "%%end" ## Kernel # 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/^/:/'" - kernel = "quote()(sed \"s/^/$1/\")\n" \ - "unquote()(sed \"s/^$1//\")\n" \ - "shexpand()(eval " \ - "\"$(echo 'cat<<" bodyend "';cat;echo " bodyend ")\")\n" \ - "shellfix()(" shellfix ")\n" \ - "ST_SOPATH=" sopath + shxquote = " -e 's/^/:/'" } /\\$/ { # line continuation @@ -47,7 +41,7 @@ BEGIN { /^\.\./ { # block body = body (body?"\n":"") \ "$$(unquote + << .." \ - (length>2 ? " | " substr($1,3) " " quote(2) : "") \ + (length>2 ? " | " substr($1,3) " " shquote(2) : "") \ slurp("..") \ "..\n)" next @@ -55,7 +49,7 @@ BEGIN { /^\./ { # line body = body (body?"\n":"") \ - "$$(" substr($1, 2) " " quote(2) ")" + "$$(" substr($1, 2) " " shquote(2) ")" next } @@ -72,20 +66,25 @@ END { print "#!/bin/sh" print "### generated with subtext" - print kernel + print "quote()(sed \"s/^/$1/\")" + print "unquote()(sed \"s/^$1//\")" + printf "shexpand()(eval \"$(echo 'cat<<%s';cat;echo '%s')\")\n", \ + shxend, shxend + printf "shellfix()(%s)\n", shellfix + printf "ST_SOPATH=%s\n", sopath print "### head" print head print "### body" - print "body(){ shexpand << \\_" + print "body(){ unquote : << \\_ | shexpand" pushpar() - print body | shellfix - close(shellfix) + print body | (shellfix shxquote) + close(shellfix shxquote) print "_" print "}" if (!dryrun) print "body" } -function quote(begin, end, out) { +function shquote(begin, end, out) { if (!begin) begin = 1 if (!end) end = NF for (i=begin; i<=NF; i++) { -- cgit 1.4.1-21-gabe81