aboutsummaryrefslogtreecommitdiff
path: root/bin/embed
diff options
context:
space:
mode:
Diffstat (limited to 'bin/embed')
-rwxr-xr-xbin/embed25
1 files changed, 17 insertions, 8 deletions
diff --git a/bin/embed b/bin/embed
index 5efe6d8..40dc853 100755
--- a/bin/embed
+++ b/bin/embed
@@ -1,6 +1,6 @@
-#!/bin/sed -f
+#!/bin/sh -euf
#
-# Convert input to 32bit hex encoded utf8.
+# Escape shell script for embedding in C string literal.
#
# Copyright (c) 2026 Dylan Araps
#
@@ -23,10 +23,19 @@
# SOFTWARE.
#
-/^[[:space:]]*#/d
-s/\\/\\\\/g
-s/"/\\"/g
-s/$/\\n/
-s/^/"/
-s/$/"/
+s() {
+ x=$l
+ shift 1
+ while :; do case $x-$# in
+ *"$1"*) x=${x%"$1"*}$2${x##*"$1"} ;;
+ *-2) break ;;
+ *) shift 2
+ esac done
+}
+
+while IFS= read -r l; do case $l in [!#]*)
+ s "$l" \" "__EMBED_$$_QUOTE__" \\ "__EMBED_$$_SLASH__" \
+ "__EMBED_$$_QUOTE__" \\\" "__EMBED_$$_SLASH__" \\\\
+ echo "\"$x\\n\""
+esac done < "$1"