-function macro(call, LOCAL, line, files, n, exec) {
- "cd " ENVIRON["MD_MACROS"] "; printf '%s/' *" |getline line;
- split(line, files, "/");
- for (n in files) { files[files[n]] = ""; delete files[n]; }
- delete files[""];
+function sh_escape(arg){
+ return "'" gensub(/'/, "'\"'\"'", "g", arg) "'";
+}
+
+function argsplit(line, args, LOCAL, c, n, ctx) {
+ ctx="space"; n=0;
+
+ while ( length(line) > 0 ) {
+ c = substr(line, 1, 1);
+ line = substr(line, 2);
+ if (ctx == "space" )
+ if (c ~ /[ \t]/) ctx = "space";
+ else if (c ~ /\\/) { n++; ctx = "escbare"; }
+ else if (c ~ /"/) { n++; ctx = "dquot"; }
+ else if (c ~ /'/) { n++; ctx = "squot"; }
+ else { n++; args[n] = c; ctx = "bare"; }
+ else if (ctx == "bare")
+ if (c ~ /[ \t]/) ctx = "space";
+ else if (c ~ /\\/) ctx = "escbare";
+ else if (c ~ /"/) ctx = "dquot";
+ else if (c ~ /'/) ctx = "squot";
+ else args[n] = args[n] c;
+ else if (ctx == "dquot")
+ if (c ~ /"/) ctx = "bare";
+ else if (c ~ /\\/) ctx = "escdquot";
+ else args[n] = args[n] c;
+ else if (ctx == "squot")
+ if (c ~ /'/) ctx = "bare";
+ else args[n] = args[n] c;
+ else if (ctx == "escbare") {
+ args[n] = args[n] c;
+ ctx = "bare";
+ }
+ else if (ctx == "escdquot") {
+ args[n] = args[n] c;
+ ctx = "dquot";
+ }
+ }
+}
+
+function macro(call, LOCAL, line, args) {
+ argsplit(call, args);
+ call="";