openclaw
Version:
Multi-channel AI gateway with extensible messaging integrations
120 lines (119 loc) • 3.82 kB
JavaScript
import { t as parseWindowsNativeArguments } from "./windows-command-line-DRjGApuy.mjs";
//#region src/daemon/cmd-set.ts
function assertNoCmdLineBreak(e, t) {
if (/[\r\n]/.test(e)) throw new Error(`${t} cannot contain CR or LF in Windows task scripts.`);
}
function escapeCmdSetAssignmentComponent(e, t) {
return (t ? e.replace(/\^/g, "^^").replace(/!/g, "^!") : e).replace(/%/g, "%%").replace(/"/g, "^\"");
}
function unescapeCmdSetAssignmentComponent(e) {
return e.replace(/\^([\^"!])|%%/g, (e, t) => t ?? "%");
}
function parseCmdSetAssignment(e, t = false) {
const n = t ? e.trimStart() : e.trim();
if (!n) return null;
const r = n.startsWith("\"") && n.endsWith("\"") && n.length >= 2;
const i = r ? n.slice(1, -1) : n;
const a = i.indexOf("=");
if (a <= 0) return null;
const o = i.slice(0, a).trim();
const s = t ? i.slice(a + 1) : i.slice(a + 1).trim();
if (!o) return null;
if (t && (o !== i.slice(0, a) || /[%!^"]/.test(i.replace(/%%/g, "")) || !r && (i.startsWith("/") || /[&|<>()]/.test(i)))) return null;
if (!r && !t) return {
key: o,
value: s
};
return {
key: unescapeCmdSetAssignmentComponent(t ? o.toUpperCase() : o),
value: unescapeCmdSetAssignmentComponent(s)
};
}
function renderCmdSetAssignment(e, t, n = {}) {
assertNoCmdLineBreak(e, "Environment variable name");
assertNoCmdLineBreak(t, "Environment variable value");
return `set "${escapeCmdSetAssignmentComponent(e, n.delayedExpansion !== false)}=${escapeCmdSetAssignmentComponent(t, n.delayedExpansion !== false)}"`;
}
//#endregion
//#region src/daemon/cmd-argv.ts
function quoteCmdScriptArg(e, t = {}) {
assertNoCmdLineBreak(e, "Command argument");
const n = e.replace(/%/g, "%%");
const r = t.delayedExpansion === false ? n : n.replace(/!/g, "^!");
if (e && !/[ \t"&|<>^()%!]/g.test(e)) return r;
return `"${r.replace(/(\\*)"/g, "$1$1\\\"").replace(/\\+$/g, "$&$&")}"`;
}
function parseCmdScriptCommandLine(e) {
return parseWindowsNativeArguments(e).map((e) => e.replace(/\^!/g, "!").replace(/%%/g, "%"));
}
function stripTrailingCmdRedirections(e) {
const t = [];
if (/[\r\n]|(?:^|[^\\])(?:\\\\)*\\"/.test(e)) return null;
for (let n = 0; n < e.length;) {
if (/[ \t]/.test(e.charAt(n))) {
n++;
continue;
}
let r = n;
const i = e[n];
if (i === ">" || i === "<") {
const a = t.at(-1);
if (a && !a.redirect && a.end === n) {
const n = e.slice(a.start, a.end);
if (/\d$/.test(n)) {
if (!/^\d$/.test(n)) return null;
r = a.start;
t.pop();
}
}
n++;
let o = i;
if (i === ">" && e[n] === ">") {
o = ">>";
n++;
}
if (o === ">" && e[n] === "&") {
if (!/[0-9]/.test(e[n + 1] ?? "")) return null;
o = ">&";
n += 2;
}
t.push({
start: r,
end: n,
redirect: o
});
continue;
}
let a = false;
while (n < e.length) {
const t = e.charAt(n);
if (t === "^" && (!a || e[n + 1] === "\"")) return null;
if (t === "\"") a = !a;
else if (!a) {
if ("&|()".includes(t)) return null;
if (/[ \t<>]/.test(t)) break;
}
n++;
}
if (a) return null;
t.push({
start: r,
end: n
});
}
const n = t.findIndex((e) => e.redirect !== void 0);
const r = t[n];
if (!r) return e;
for (let r = n; r < t.length; r++) {
const n = t[r];
if (!n?.redirect) return null;
if (n.redirect === ">&") continue;
const i = t[++r];
if (!i || i.redirect) return null;
const a = e.slice(i.start, i.end);
if (a.includes("\"") && !/^"[^"]+"$/.test(a) || !a.includes("\"") && /[,;=%!]/.test(a) || n.redirect === "<" && !/^(?:NUL|"NUL")$/i.test(a)) return null;
}
return e.slice(0, r.start);
}
//#endregion
export { parseCmdSetAssignment as a, assertNoCmdLineBreak as i, quoteCmdScriptArg as n, renderCmdSetAssignment as o, stripTrailingCmdRedirections as r, parseCmdScriptCommandLine as t };