Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit8002c5d3f376138ab0fe7a1abf92638c70bb3b26 (patch)
Author
 Syndamia <kamen@syndamia.com>
2024-04-30 09:21:41 +0300
Committer
 Syndamia <kamen@syndamia.com>
2024-04-30 09:21:41 +0300
Files273a38df8b8f47015adc32df89a07750c759eaa9
csg-8002c5d3f376138ab0fe7a1abf92638c70bb3b26.tar
csg-8002c5d3f376138ab0fe7a1abf92638c70bb3b26.tar.gz
csg-8002c5d3f376138ab0fe7a1abf92638c70bb3b26.zip
Parentf8266b610f9787689a4f121c57c039952ca9cec2 (diff)
(csg) Added an explanation for field splitting shenanigans
1 files changed, 38 insertions, 0 deletions
-rwxr-xr-xcsg38
diff --git a/csg b/csg
index de8b6bd..5929e1e 100755
--- a/csg
+++ b/csg
@@ -345,3 +345,41 @@ $(find "$DESTINATION" -type f -name "*.html")
EOF
endTimer "[Stage 2] Done"
+
+### Appendix A
+#
+# prev_all and prev_md leverage the awk "-v" arguments to pass on their variables to other scripts.
+# However, field splitting (aka word splitting) in shells wrecks havoc on our plans, since we can
+# output string variables; what would happen if one of those scripts emits '-v var="A B"'?
+# (The whole output is denoted in single quotes, while the string argument is in double quotes,
+# the exact choice of quotes doesn't matter)
+#
+# That value will be stored in either $prev_all or $prev_md, and we can give it as parameters to awk
+# by using (unquoted) parameter expansion, which will in turn force field splitting, so we don't pass
+# the whole (single-quoted) string as one parameter, but pass '-v' as the first and 'var="A B"' as the
+# second.
+#
+# Unfortunately, field splitting is done on spaces without regard for inner quotes, so in practice
+# that (single-quoted) string is split into three parts: '-v', 'var="A', 'B"'.
+# POSIX especially doesn't provide any mechanisms for the shell to properly parse those quotes or
+# for us to pass variables to awk in another efficient way (it can always be done with an intermediary
+# file, but I fear it is always noticeably slower than directly leveraging the shell). So we do the
+# next best thing: we change on which character(s) the splitting is performed, so spaces inside passed
+# strings would be left as-is.
+#
+# An unfortunate sideeffect is that we can't force field splitting rules only for certain
+# variables, it can only be changed for all variables after a given point.
+# So, we'll also need to use our special splitting character in $CSG_AWK, since it calls an awk program
+# with parameters and in $lib_script, $on_md, $on_html.
+# The ksh/bash pattern substitution isn't in POSIX and spawning a new string editing process to convert
+# between splitting characters is going to be slow (especially since we would need to be properly parsing
+# quotes in our script), so I go for the alternative where possible: incredibly ugly shell/awk scripts,
+# which require no post-processing.
+#
+# Some good references:
+# - bash cannot solve substring splitting
+# https://superuser.com/a/1529316
+# - a detailed explanation with examples on field splitting and how to change it
+# https://mywiki.wooledge.org/WordSplitting
+# - another good explanation on field splitting
+# https://unix.stackexchange.com/a/26672