From: Paul Hänsch Date: Sat, 31 Aug 2024 12:31:43 +0000 (+0200) Subject: portable macros.awk X-Git-Url: https://git.plutz.net/?a=commitdiff_plain;h=4ffc433123c6464af635cc543c0d9b1f2abd85ae;p=shellwiki portable macros.awk --- diff --git a/macros.awk b/macros.awk index b72110b..db4561f 100755 --- a/macros.awk +++ b/macros.awk @@ -1,7 +1,6 @@ #!/bin/awk -f -#!/opt/busybox/awk -f -# Copyright 2022 - 2023 Paul Hänsch +# Copyright 2022 - 2024 Paul Hänsch # # Permission to use, copy, modify, and/or distribute this software for any # purpose with or without fee is hereby granted, provided that the above @@ -15,8 +14,11 @@ # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. +function debug(text) { printf "\n---\n%s\n---\n", text > "/dev/stderr"; } + function sh_escape(arg){ - return "'" gensub(/'/, "'\"'\"'", "g", arg) "'"; + gsub(/'/, "'\"'\"'", arg); + return "'" arg "'"; } function argsplit(line, args, LOCAL, c, n, ctx) { @@ -65,15 +67,15 @@ function HTML ( text ) { return text; } -function macro(call, LOCAL, line, args) { +function macro(call, LOCAL, line, args, cmd) { argsplit(call, args); call=""; for (n = 1; n in args; n++) call = call sh_escape(args[n]) " "; if (args[1] in MACROS) { - printf "%s", file | sh_escape(ENVIRON["MD_MACROS"]) "/" call; - close(sh_escape(ENVIRON["MD_MACROS"]) "/" call); + cmd = sh_escape(ENVIRON["MD_MACROS"]) "/" call; + printf "%s", file | cmd; close( cmd ); } else { printf "%s", HTML("<<" call ">>"); } @@ -104,8 +106,10 @@ function findmacro(line, LOCAL, st, len, pre, post) { BEGIN { if (ENVIRON["MD_MACROS"]) { - AllowMacros = "true"; - "cd " sh_escape(ENVIRON["MD_MACROS"]) "; printf '%s/' *" |getline macro_list; + cmd = "cd " sh_escape(ENVIRON["MD_MACROS"]) "; printf '%s/' *"; + "" cmd |getline macro_list; + # ^- need empty string "" concatenation to work in gowk + # need prebuilt cmd-variable to work properly in mawk split(macro_list, MACROS, "/"); for (n in MACROS) { MACROS[MACROS[n]] = ""; delete MACROS[n]; } delete MACROS[""];