From 4ffc433123c6464af635cc543c0d9b1f2abd85ae Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Sat, 31 Aug 2024 14:31:43 +0200 Subject: [PATCH] portable macros.awk --- macros.awk | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) 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[""]; -- 2.39.2