about summary refs log tree commit diff stats
path: root/subtext.awk
diff options
context:
space:
mode:
Diffstat (limited to 'subtext.awk')
-rw-r--r--subtext.awk29
1 files 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 {
10 ## Tuneables 10 ## Tuneables
11 dryrun = dryrun ? dryrun : 0 11 dryrun = dryrun ? dryrun : 0
12 sopath = sopath ? sopath : ".:" ENVIRON["HOME"] "/.subtext" 12 sopath = sopath ? sopath : ".:" ENVIRON["HOME"] "/.subtext"
13 bodyend = bodyend ? bodyend : "_@end@_" 13 shxend = shxend ? shxend : "%%end"
14 ## Kernel 14 ## Kernel
15 # Ask sed to do these b/c awk has no capture groups ;_; 15 # Ask sed to do these b/c awk has no capture groups ;_;
16 shellfix = "sed -E" \ 16 shellfix = "sed -E" \
17 " -e 's/`/\\\\`/g'" \ 17 " -e 's/`/\\\\`/g'" \
18 " -e 's/(^|[^\\$])\\$([^\\$]|$)/\\1\\\\$\\2/g'" \ 18 " -e 's/(^|[^\\$])\\$([^\\$]|$)/\\1\\\\$\\2/g'" \
19 " -e 's/(^|[^\\$])\\$(\\$+)([^\\$]|$)/\\1\\2\\3/g'" 19 " -e 's/(^|[^\\$])\\$(\\$+)([^\\$]|$)/\\1\\2\\3/g'"
20 shxwrap = " -e 's/^/:/'" 20 shxquote = " -e 's/^/:/'"
21 kernel = "quote()(sed \"s/^/$1/\")\n" \
22 "unquote()(sed \"s/^$1//\")\n" \
23 "shexpand()(eval " \
24 "\"$(echo 'cat<<" bodyend "';cat;echo " bodyend ")\")\n" \
25 "shellfix()(" shellfix ")\n" \
26 "ST_SOPATH=" sopath
27} 21}
28 22
29/\\$/ { # line continuation 23/\\$/ { # line continuation
@@ -47,7 +41,7 @@ BEGIN {
47/^\.\./ { # block 41/^\.\./ { # block
48 body = body (body?"\n":"") \ 42 body = body (body?"\n":"") \
49 "$$(unquote + << .." \ 43 "$$(unquote + << .." \
50 (length>2 ? " | " substr($1,3) " " quote(2) : "") \ 44 (length>2 ? " | " substr($1,3) " " shquote(2) : "") \
51 slurp("..") \ 45 slurp("..") \
52 "..\n)" 46 "..\n)"
53 next 47 next
@@ -55,7 +49,7 @@ BEGIN {
55 49
56/^\./ { # line 50/^\./ { # line
57 body = body (body?"\n":"") \ 51 body = body (body?"\n":"") \
58 "$$(" substr($1, 2) " " quote(2) ")" 52 "$$(" substr($1, 2) " " shquote(2) ")"
59 next 53 next
60} 54}
61 55
@@ -72,20 +66,25 @@ END {
72 66
73 print "#!/bin/sh" 67 print "#!/bin/sh"
74 print "### generated with subtext" 68 print "### generated with subtext"
75 print kernel 69 print "quote()(sed \"s/^/$1/\")"
70 print "unquote()(sed \"s/^$1//\")"
71 printf "shexpand()(eval \"$(echo 'cat<<%s';cat;echo '%s')\")\n", \
72 shxend, shxend
73 printf "shellfix()(%s)\n", shellfix
74 printf "ST_SOPATH=%s\n", sopath
76 print "### head" 75 print "### head"
77 print head 76 print head
78 print "### body" 77 print "### body"
79 print "body(){ shexpand << \\_" 78 print "body(){ unquote : << \\_ | shexpand"
80 pushpar() 79 pushpar()
81 print body | shellfix 80 print body | (shellfix shxquote)
82 close(shellfix) 81 close(shellfix shxquote)
83 print "_" 82 print "_"
84 print "}" 83 print "}"
85 if (!dryrun) print "body" 84 if (!dryrun) print "body"
86} 85}
87 86
88function quote(begin, end, out) { 87function shquote(begin, end, out) {
89 if (!begin) begin = 1 88 if (!begin) begin = 1
90 if (!end) end = NF 89 if (!end) end = NF
91 for (i=begin; i<=NF; i++) { 90 for (i=begin; i<=NF; i++) {