just-bash 1.0.1 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,14 +1,14 @@
1
1
  #!/usr/bin/env node
2
- import{a as z,b as Q,c as U,d as J,e as X,f as R,g as W,h as fe,i as B,j as Oe}from"./chunks/chunk-LV662IGP.js";import{a as K}from"./chunks/chunk-4VDEBYW7.js";import"./chunks/chunk-5KNEBKYN.js";import*as $n from"node:fs";import*as vn from"node:readline";var ir=[{name:"echo",load:async()=>(await import("./chunks/echo-DEUIS5JO.js")).echoCommand},{name:"cat",load:async()=>(await import("./chunks/cat-YZXBF5YF.js")).catCommand},{name:"printf",load:async()=>(await import("./chunks/printf-BOFQVMMK.js")).printfCommand},{name:"ls",load:async()=>(await import("./chunks/ls-C4MPAF3H.js")).lsCommand},{name:"mkdir",load:async()=>(await import("./chunks/mkdir-LWULWDHP.js")).mkdirCommand},{name:"touch",load:async()=>(await import("./chunks/touch-XCIAYF5I.js")).touchCommand},{name:"rm",load:async()=>(await import("./chunks/rm-W6CCBEMG.js")).rmCommand},{name:"cp",load:async()=>(await import("./chunks/cp-77UY7PGN.js")).cpCommand},{name:"mv",load:async()=>(await import("./chunks/mv-TDYCNSIQ.js")).mvCommand},{name:"ln",load:async()=>(await import("./chunks/ln-DB7J2W5X.js")).lnCommand},{name:"chmod",load:async()=>(await import("./chunks/chmod-3G4LK462.js")).chmodCommand},{name:"pwd",load:async()=>(await import("./chunks/pwd-X4MWD4JP.js")).pwdCommand},{name:"readlink",load:async()=>(await import("./chunks/readlink-DPNOQY67.js")).readlinkCommand},{name:"head",load:async()=>(await import("./chunks/head-VGXR3WWL.js")).headCommand},{name:"tail",load:async()=>(await import("./chunks/tail-YSVKBQ77.js")).tailCommand},{name:"wc",load:async()=>(await import("./chunks/wc-CNJ3QDRA.js")).wcCommand},{name:"stat",load:async()=>(await import("./chunks/stat-3PHITCPO.js")).statCommand},{name:"grep",load:async()=>(await import("./chunks/grep-OVWU4WCZ.js")).grepCommand},{name:"fgrep",load:async()=>(await import("./chunks/grep-OVWU4WCZ.js")).fgrepCommand},{name:"egrep",load:async()=>(await import("./chunks/grep-OVWU4WCZ.js")).egrepCommand},{name:"sed",load:async()=>(await import("./chunks/sed-7IS44XON.js")).sedCommand},{name:"awk",load:async()=>(await import("./chunks/awk-S63F6IYK.js")).awkCommand},{name:"sort",load:async()=>(await import("./chunks/sort-UJP353TM.js")).sortCommand},{name:"uniq",load:async()=>(await import("./chunks/uniq-W4HF6YSB.js")).uniqCommand},{name:"cut",load:async()=>(await import("./chunks/cut-UYV3FM7R.js")).cutCommand},{name:"paste",load:async()=>(await import("./chunks/paste-R36J3G4K.js")).pasteCommand},{name:"tr",load:async()=>(await import("./chunks/tr-2EEKHHO6.js")).trCommand},{name:"tee",load:async()=>(await import("./chunks/tee-UU2VS3OM.js")).teeCommand},{name:"find",load:async()=>(await import("./chunks/find-4B67H4RP.js")).findCommand},{name:"basename",load:async()=>(await import("./chunks/basename-Y7JDBYHN.js")).basenameCommand},{name:"dirname",load:async()=>(await import("./chunks/dirname-VLHP44TU.js")).dirnameCommand},{name:"tree",load:async()=>(await import("./chunks/tree-4247W67O.js")).treeCommand},{name:"du",load:async()=>(await import("./chunks/du-4FZ7WF2P.js")).duCommand},{name:"env",load:async()=>(await import("./chunks/env-FVITWNHG.js")).envCommand},{name:"printenv",load:async()=>(await import("./chunks/env-FVITWNHG.js")).printenvCommand},{name:"alias",load:async()=>(await import("./chunks/alias-74DFXE2E.js")).aliasCommand},{name:"unalias",load:async()=>(await import("./chunks/alias-74DFXE2E.js")).unaliasCommand},{name:"history",load:async()=>(await import("./chunks/history-PRQ4B6N2.js")).historyCommand},{name:"xargs",load:async()=>(await import("./chunks/xargs-YNLVLPOF.js")).xargsCommand},{name:"true",load:async()=>(await import("./chunks/true-JCX733LK.js")).trueCommand},{name:"false",load:async()=>(await import("./chunks/true-JCX733LK.js")).falseCommand},{name:"clear",load:async()=>(await import("./chunks/clear-GTCFHSB2.js")).clearCommand},{name:"bash",load:async()=>(await import("./chunks/bash-TQGTNXAE.js")).bashCommand},{name:"sh",load:async()=>(await import("./chunks/bash-TQGTNXAE.js")).shCommand},{name:"jq",load:async()=>(await import("./chunks/jq-EYSXBSCP.js")).jqCommand},{name:"base64",load:async()=>(await import("./chunks/base64-NC7HTKLZ.js")).base64Command},{name:"diff",load:async()=>(await import("./chunks/diff-TG2NXCX2.js")).diffCommand},{name:"date",load:async()=>(await import("./chunks/date-7NBRXV2Z.js")).dateCommand},{name:"sleep",load:async()=>(await import("./chunks/sleep-E4DIYGTT.js")).sleepCommand},{name:"timeout",load:async()=>(await import("./chunks/timeout-OZVAGW2H.js")).timeoutCommand},{name:"seq",load:async()=>(await import("./chunks/seq-DGJILX2Q.js")).seqCommand},{name:"expr",load:async()=>(await import("./chunks/expr-HA2ZNL6S.js")).exprCommand},{name:"html-to-markdown",load:async()=>(await import("./chunks/html-to-markdown-L4UWMK4S.js")).htmlToMarkdownCommand},{name:"help",load:async()=>(await import("./chunks/help-5V3MPCYQ.js")).helpCommand},{name:"which",load:async()=>(await import("./chunks/which-HX2NMOP3.js")).whichCommand}],In=[{name:"curl",load:async()=>(await import("./chunks/curl-NE7XEWMN.js")).curlCommand}],or=new Map;function ar(t){return{name:t.name,async execute(e,n){let r=or.get(t.name);return r||(r=await t.load(),or.set(t.name,r)),r.execute(e,n)}}}function cr(t){return(t?ir.filter(n=>t.includes(n.name)):ir).map(ar)}function lr(){return In.map(ar)}var be=new TextEncoder,Pn=new TextDecoder;function ur(t,e){if(t instanceof Uint8Array)return t;switch(e){case"base64":return Uint8Array.from(atob(t),n=>n.charCodeAt(0));case"hex":{let n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=parseInt(t.slice(r,r+2),16);return n}case"binary":case"latin1":return Uint8Array.from(t,n=>n.charCodeAt(0));default:return be.encode(t)}}function Rn(t,e){switch(e){case"base64":return btoa(String.fromCharCode(...t));case"hex":return Array.from(t).map(n=>n.toString(16).padStart(2,"0")).join("");case"binary":case"latin1":return String.fromCharCode(...t);default:return Pn.decode(t)}}function lt(t){if(t!=null)return typeof t=="string"?t:t.encoding??void 0}function kn(t){return typeof t=="object"&&t!==null&&!(t instanceof Uint8Array)&&"content"in t}var de=class{data=new Map;constructor(e){if(this.data.set("/",{type:"directory",mode:493,mtime:new Date}),e)for(let[n,r]of Object.entries(e))kn(r)?this.writeFileSync(n,r.content,void 0,{mode:r.mode,mtime:r.mtime}):this.writeFileSync(n,r)}normalizePath(e){if(!e||e==="/")return"/";let n=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;n.startsWith("/")||(n=`/${n}`);let r=n.split("/").filter(o=>o&&o!=="."),s=[];for(let o of r)o===".."?s.pop():s.push(o);return`/${s.join("/")}`||"/"}dirname(e){let n=this.normalizePath(e);if(n==="/")return"/";let r=n.lastIndexOf("/");return r===0?"/":n.slice(0,r)}ensureParentDirs(e){let n=this.dirname(e);n!=="/"&&(this.data.has(n)||(this.ensureParentDirs(n),this.data.set(n,{type:"directory",mode:493,mtime:new Date})))}writeFileSync(e,n,r,s){let o=this.normalizePath(e);this.ensureParentDirs(o);let i=lt(r),a=ur(n,i);this.data.set(o,{type:"file",content:a,mode:s?.mode??420,mtime:s?.mtime??new Date})}async readFile(e,n){let r=await this.readFileBuffer(e),s=lt(n);return Rn(r,s)}async readFileBuffer(e){let n=this.normalizePath(e),r=this.data.get(n),s=n;if(!r)throw new Error(`ENOENT: no such file or directory, open '${e}'`);let o=new Set;for(;r&&r.type==="symlink";){if(o.has(s))throw new Error(`ELOOP: too many levels of symbolic links, open '${e}'`);o.add(s),s=this.resolveSymlink(s,r.target),r=this.data.get(s)}if(!r)throw new Error(`ENOENT: no such file or directory, open '${e}'`);if(r.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${e}'`);return r.content instanceof Uint8Array?r.content:be.encode(r.content)}async writeFile(e,n,r){this.writeFileSync(e,n,r)}async appendFile(e,n,r){let s=this.normalizePath(e),o=this.data.get(s);if(o&&o.type==="directory")throw new Error(`EISDIR: illegal operation on a directory, write '${e}'`);let i=lt(r),a=ur(n,i);if(o?.type==="file"){let l=o.content instanceof Uint8Array?o.content:be.encode(o.content),c=new Uint8Array(l.length+a.length);c.set(l),c.set(a,l.length),this.data.set(s,{type:"file",content:c,mode:o.mode,mtime:new Date})}else this.writeFileSync(e,n,r)}async exists(e){return this.data.has(this.normalizePath(e))}async stat(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, stat '${e}'`);if(r.type==="symlink"){let o=this.resolveSymlink(n,r.target),i=this.data.get(o);if(!i)throw new Error(`ENOENT: no such file or directory, stat '${e}'`);r=i}let s=0;return r.type==="file"&&r.content&&(r.content instanceof Uint8Array?s=r.content.length:s=be.encode(r.content).length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:s,mtime:r.mtime||new Date}}async lstat(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, lstat '${e}'`);if(r.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:r.mode,size:r.target.length,mtime:r.mtime||new Date};let s=0;return r.type==="file"&&r.content&&(r.content instanceof Uint8Array?s=r.content.length:s=be.encode(r.content).length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:s,mtime:r.mtime||new Date}}resolveSymlink(e,n){if(n.startsWith("/"))return this.normalizePath(n);let r=this.dirname(e);return this.normalizePath(r==="/"?`/${n}`:`${r}/${n}`)}async mkdir(e,n){this.mkdirSync(e,n)}mkdirSync(e,n){let r=this.normalizePath(e);if(this.data.has(r)){if(this.data.get(r)?.type==="file")throw new Error(`EEXIST: file already exists, mkdir '${e}'`);if(!n?.recursive)throw new Error(`EEXIST: directory already exists, mkdir '${e}'`);return}let s=this.dirname(r);if(s!=="/"&&!this.data.has(s))if(n?.recursive)this.mkdirSync(s,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${e}'`);this.data.set(r,{type:"directory",mode:493,mtime:new Date})}async readdir(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, scandir '${e}'`);if(r.type!=="directory")throw new Error(`ENOTDIR: not a directory, scandir '${e}'`);let s=n==="/"?"/":`${n}/`,o=[];for(let i of this.data.keys())if(i!==n&&i.startsWith(s)){let l=i.slice(s.length).split("/")[0];l&&!o.includes(l)&&o.push(l)}return o.sort()}async rm(e,n){let r=this.normalizePath(e),s=this.data.get(r);if(!s){if(n?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${e}'`)}if(s.type==="directory"){let o=await this.readdir(r);if(o.length>0){if(!n?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${e}'`);for(let i of o){let a=r==="/"?`/${i}`:`${r}/${i}`;await this.rm(a,n)}}}this.data.delete(r)}async cp(e,n,r){let s=this.normalizePath(e),o=this.normalizePath(n),i=this.data.get(s);if(!i)throw new Error(`ENOENT: no such file or directory, cp '${e}'`);if(i.type==="file")this.ensureParentDirs(o),this.data.set(o,{...i});else if(i.type==="directory"){if(!r?.recursive)throw new Error(`EISDIR: is a directory, cp '${e}'`);await this.mkdir(o,{recursive:!0});let a=await this.readdir(s);for(let l of a){let c=s==="/"?`/${l}`:`${s}/${l}`,u=o==="/"?`/${l}`:`${o}/${l}`;await this.cp(c,u,r)}}}async mv(e,n){await this.cp(e,n,{recursive:!0}),await this.rm(e,{recursive:!0})}getAllPaths(){return Array.from(this.data.keys())}resolvePath(e,n){if(n.startsWith("/"))return this.normalizePath(n);let r=e==="/"?`/${n}`:`${e}/${n}`;return this.normalizePath(r)}async chmod(e,n){let r=this.normalizePath(e),s=this.data.get(r);if(!s)throw new Error(`ENOENT: no such file or directory, chmod '${e}'`);s.mode=n}async symlink(e,n){let r=this.normalizePath(n);if(this.data.has(r))throw new Error(`EEXIST: file already exists, symlink '${n}'`);this.ensureParentDirs(r),this.data.set(r,{type:"symlink",target:e,mode:511,mtime:new Date})}async link(e,n){let r=this.normalizePath(e),s=this.normalizePath(n),o=this.data.get(r);if(!o)throw new Error(`ENOENT: no such file or directory, link '${e}'`);if(o.type!=="file")throw new Error(`EPERM: operation not permitted, link '${e}'`);if(this.data.has(s))throw new Error(`EEXIST: file already exists, link '${n}'`);this.ensureParentDirs(s),this.data.set(s,{type:"file",content:o.content,mode:o.mode,mtime:o.mtime})}async readlink(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, readlink '${e}'`);if(r.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${e}'`);return r.target}};function L(t,e){return{type:"ArithmeticExpression",expression:ee(t,e,0).expr}}function ee(t,e,n){return Dn(t,e,n)}function Dn(t,e,n){let{expr:r,pos:s}=Te(t,e,n);for(s=D(e,s);e[s]===",";){s++;let{expr:o,pos:i}=Te(t,e,s);r={type:"ArithBinary",operator:",",left:r,right:o},s=D(e,i)}return{expr:r,pos:s}}function Te(t,e,n){let{expr:r,pos:s}=_n(t,e,n);if(s=D(e,s),e[s]==="?"){s++;let{expr:o,pos:i}=ee(t,e,s);if(s=D(e,i),e[s]===":"){s++;let{expr:a,pos:l}=ee(t,e,s);return{expr:{type:"ArithTernary",condition:r,consequent:o,alternate:a},pos:l}}}return{expr:r,pos:s}}function _n(t,e,n){let{expr:r,pos:s}=fr(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="||";){s+=2;let{expr:o,pos:i}=fr(t,e,s);r={type:"ArithBinary",operator:"||",left:r,right:o},s=i}return{expr:r,pos:s}}function fr(t,e,n){let{expr:r,pos:s}=dr(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="&&";){s+=2;let{expr:o,pos:i}=dr(t,e,s);r={type:"ArithBinary",operator:"&&",left:r,right:o},s=i}return{expr:r,pos:s}}function dr(t,e,n){let{expr:r,pos:s}=hr(t,e,n);for(;s=D(e,s),e[s]==="|"&&e[s+1]!=="|";){s++;let{expr:o,pos:i}=hr(t,e,s);r={type:"ArithBinary",operator:"|",left:r,right:o},s=i}return{expr:r,pos:s}}function hr(t,e,n){let{expr:r,pos:s}=pr(t,e,n);for(;s=D(e,s),e[s]==="^";){s++;let{expr:o,pos:i}=pr(t,e,s);r={type:"ArithBinary",operator:"^",left:r,right:o},s=i}return{expr:r,pos:s}}function pr(t,e,n){let{expr:r,pos:s}=mr(t,e,n);for(;s=D(e,s),e[s]==="&"&&e[s+1]!=="&";){s++;let{expr:o,pos:i}=mr(t,e,s);r={type:"ArithBinary",operator:"&",left:r,right:o},s=i}return{expr:r,pos:s}}function mr(t,e,n){let{expr:r,pos:s}=yr(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="=="||e.slice(s,s+2)==="!=";){let o=e.slice(s,s+2);s+=2;let{expr:i,pos:a}=yr(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}return{expr:r,pos:s}}function yr(t,e,n){let{expr:r,pos:s}=ut(t,e,n);for(;;)if(s=D(e,s),e.slice(s,s+2)==="<="||e.slice(s,s+2)===">="){let o=e.slice(s,s+2);s+=2;let{expr:i,pos:a}=ut(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}else if(e[s]==="<"||e[s]===">"){let o=e[s];s++;let{expr:i,pos:a}=ut(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}else break;return{expr:r,pos:s}}function ut(t,e,n){let{expr:r,pos:s}=Er(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="<<"||e.slice(s,s+2)===">>";){let o=e.slice(s,s+2);s+=2;let{expr:i,pos:a}=Er(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}return{expr:r,pos:s}}function Er(t,e,n){let{expr:r,pos:s}=wr(t,e,n);for(;s=D(e,s),(e[s]==="+"||e[s]==="-")&&e[s+1]!==e[s];){let o=e[s];s++;let{expr:i,pos:a}=wr(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}return{expr:r,pos:s}}function wr(t,e,n){let{expr:r,pos:s}=Le(t,e,n);for(;;)if(s=D(e,s),e[s]==="*"&&e[s+1]!=="*"){s++;let{expr:o,pos:i}=Le(t,e,s);r={type:"ArithBinary",operator:"*",left:r,right:o},s=i}else if(e[s]==="/"||e[s]==="%"){let o=e[s];s++;let{expr:i,pos:a}=Le(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}else break;return{expr:r,pos:s}}function Le(t,e,n){let{expr:r,pos:s}=ft(t,e,n),o=D(e,s);if(e.slice(o,o+2)==="**"){o+=2;let{expr:i,pos:a}=Le(t,e,o);return{expr:{type:"ArithBinary",operator:"**",left:r,right:i},pos:a}}return{expr:r,pos:s}}function ft(t,e,n){let r=D(e,n);if(e.slice(r,r+2)==="++"||e.slice(r,r+2)==="--"){let s=e.slice(r,r+2);r+=2;let{expr:o,pos:i}=ft(t,e,r);return{expr:{type:"ArithUnary",operator:s,operand:o,prefix:!0},pos:i}}if(e[r]==="+"||e[r]==="-"||e[r]==="!"||e[r]==="~"){let s=e[r];r++;let{expr:o,pos:i}=ft(t,e,r);return{expr:{type:"ArithUnary",operator:s,operand:o,prefix:!0},pos:i}}return Ln(t,e,r)}function On(t,e){let n=t[e];return n==="$"||n==="`"}function Ln(t,e,n){let{expr:r,pos:s}=Ar(t,e,n),o=[r];for(;On(e,s);){let{expr:i,pos:a}=Ar(t,e,s);o.push(i),s=a}if(o.length>1&&(r={type:"ArithConcat",parts:o}),s=D(e,s),e.slice(s,s+2)==="++"||e.slice(s,s+2)==="--"){let i=e.slice(s,s+2);return s+=2,{expr:{type:"ArithUnary",operator:i,operand:r,prefix:!1},pos:s}}return{expr:r,pos:s}}function Ar(t,e,n){let r=D(e,n);if(e.slice(r,r+3)==="$(("){r+=3;let s=1,o=r;for(;r<e.length-1&&s>0;)e[r]==="("&&e[r+1]==="("?(s++,r+=2):e[r]===")"&&e[r+1]===")"?(s--,s>0&&(r+=2)):r++;let i=e.slice(o,r),{expr:a}=ee(t,i,0);return r+=2,{expr:{type:"ArithNested",expression:a},pos:r}}if(e.slice(r,r+2)==="$("&&e[r+2]!=="("){r+=2;let s=1,o=r;for(;r<e.length&&s>0;)e[r]==="("?s++:e[r]===")"&&s--,s>0&&r++;let i=e.slice(o,r);return r++,{expr:{type:"ArithCommandSubst",command:i},pos:r}}if(e[r]==="`"){r++;let s=r;for(;r<e.length&&e[r]!=="`";)r++;let o=e.slice(s,r);return e[r]==="`"&&r++,{expr:{type:"ArithCommandSubst",command:o},pos:r}}if(e[r]==="("){r++;let{expr:s,pos:o}=ee(t,e,r);return r=D(e,o),e[r]===")"&&r++,{expr:{type:"ArithGroup",expression:s},pos:r}}if(/[0-9]/.test(e[r])){let s="",o=!1;for(;r<e.length;){let a=e[r];if(o)if(/[0-9a-zA-Z@_]/.test(a))s+=a,r++;else break;else if(a==="#")o=!0,s+=a,r++;else if(/[0-9a-fA-FxX]/.test(a))s+=a,r++;else break}if(e[r]==="."&&/[0-9]/.test(e[r+1]))throw new W(`${s}.${e[r+1]}...: syntax error: invalid arithmetic operator`);if(e[r]==="["){let a=e.slice(r).trim();return{expr:{type:"ArithNumberSubscript",number:s,errorToken:a},pos:e.length}}return{expr:{type:"ArithNumber",value:Ee(s)},pos:r}}if(e[r]==="$"&&e[r+1]==="{"){let s=r+2,o=1,i=s;for(;i<e.length&&o>0;)e[i]==="{"?o++:e[i]==="}"&&o--,o>0&&i++;let a=e.slice(s,i),l=i+1;if(e[l]==="#"){let c=l+1;for(;c<e.length&&/[0-9a-zA-Z@_]/.test(e[c]);)c++;let u=e.slice(l+1,c);return{expr:{type:"ArithDynamicBase",baseExpr:a,value:u},pos:c}}if(/[0-9]/.test(e[l])||e[l]==="x"||e[l]==="X"){let c=l;if(e[l]==="x"||e[l]==="X")for(c++;c<e.length&&/[0-9a-fA-F]/.test(e[c]);)c++;else for(;c<e.length&&/[0-9]/.test(e[c]);)c++;let u=e.slice(l,c);return{expr:{type:"ArithDynamicNumber",prefix:a,suffix:u},pos:c}}return r=l,{expr:{type:"ArithBracedExpansion",content:a},pos:r}}if(e[r]==="$"&&r+1<e.length&&/[0-9]/.test(e[r+1])){r++;let s="";for(;r<e.length&&/[0-9]/.test(e[r]);)s+=e[r],r++;return{expr:{type:"ArithVariable",name:s},pos:r}}if(e[r]==="$"&&r+1<e.length&&/[a-zA-Z_]/.test(e[r+1])&&r++,/[a-zA-Z_]/.test(e[r])){let s="";for(;r<e.length&&/[a-zA-Z0-9_]/.test(e[r]);)s+=e[r],r++;if(e[r]==="["){r++;let i;if(e[r]==="'"||e[r]==='"'){let c=e[r];for(r++,i="";r<e.length&&e[r]!==c;)i+=e[r],r++;e[r]===c&&r++,r=D(e,r),e[r]==="]"&&r++}let a;if(i===void 0){let{expr:c,pos:u}=ee(t,e,r);a=c,r=u,e[r]==="]"&&r++}if(r=D(e,r),e[r]==="["&&a)return{expr:{type:"ArithDoubleSubscript",array:s,index:a},pos:r};let l=["=","+=","-=","*=","/=","%=","<<=",">>=","&=","|=","^="];for(let c of l)if(e.slice(r,r+c.length)===c&&e.slice(r,r+c.length+1)!=="=="){r+=c.length;let{expr:u,pos:d}=Te(t,e,r);return{expr:{type:"ArithAssignment",operator:c,variable:s,subscript:a,stringKey:i,value:u},pos:d}}return{expr:{type:"ArithArrayElement",array:s,index:a,stringKey:i},pos:r}}r=D(e,r);let o=["=","+=","-=","*=","/=","%=","<<=",">>=","&=","|=","^="];for(let i of o)if(e.slice(r,r+i.length)===i&&e.slice(r,r+i.length+1)!=="=="){r+=i.length;let{expr:a,pos:l}=Te(t,e,r);return{expr:{type:"ArithAssignment",operator:i,variable:s,value:a},pos:l}}return{expr:{type:"ArithVariable",name:s},pos:r}}return{expr:{type:"ArithNumber",value:0},pos:r}}function Ee(t){if(t.includes("#")){let[e,n]=t.split("#"),r=Number.parseInt(e,10);if(r<2||r>64)return Number.NaN;if(r<=36)return Number.parseInt(n,r);let s=0;for(let o of n){let i;if(o>="0"&&o<="9")i=o.charCodeAt(0)-48;else if(o>="a"&&o<="z")i=o.charCodeAt(0)-97+10;else if(o>="A"&&o<="Z")i=o.charCodeAt(0)-65+36;else if(o==="@")i=62;else if(o==="_")i=63;else return Number.NaN;if(i>=r)return Number.NaN;s=s*r+i}return s}return t.startsWith("0x")||t.startsWith("0X")?Number.parseInt(t.slice(2),16):t.startsWith("0")&&t.length>1&&/^[0-9]+$/.test(t)?/[89]/.test(t)?Number.NaN:Number.parseInt(t,8):Number.parseInt(t,10)}function D(t,e){for(;e<t.length;){if(t[e]==="\\"&&t[e+1]===`
3
- `){e+=2;continue}if(/\s/.test(t[e])){e++;continue}break}return e}var g={script(t){return{type:"Script",statements:t}},statement(t,e=[],n=!1){return{type:"Statement",pipelines:t,operators:e,background:n}},pipeline(t,e=!1){return{type:"Pipeline",commands:t,negated:e}},simpleCommand(t,e=[],n=[],r=[]){return{type:"SimpleCommand",name:t,args:e,assignments:n,redirections:r}},word(t){return{type:"Word",parts:t}},literal(t){return{type:"Literal",value:t}},singleQuoted(t){return{type:"SingleQuoted",value:t}},doubleQuoted(t){return{type:"DoubleQuoted",parts:t}},escaped(t){return{type:"Escaped",value:t}},parameterExpansion(t,e=null){return{type:"ParameterExpansion",parameter:t,operation:e}},commandSubstitution(t,e=!1){return{type:"CommandSubstitution",body:t,legacy:e}},arithmeticExpansion(t){return{type:"ArithmeticExpansion",expression:t}},assignment(t,e,n=!1,r=null){return{type:"Assignment",name:t,value:e,append:n,array:r}},redirection(t,e,n=null){return{type:"Redirection",fd:n,operator:t,target:e}},hereDoc(t,e,n=!1,r=!1){return{type:"HereDoc",delimiter:t,content:e,stripTabs:n,quoted:r}},ifNode(t,e=null,n=[]){return{type:"If",clauses:t,elseBody:e,redirections:n}},forNode(t,e,n,r=[]){return{type:"For",variable:t,words:e,body:n,redirections:r}},whileNode(t,e,n=[]){return{type:"While",condition:t,body:e,redirections:n}},untilNode(t,e,n=[]){return{type:"Until",condition:t,body:e,redirections:n}},caseNode(t,e,n=[]){return{type:"Case",word:t,items:e,redirections:n}},caseItem(t,e,n=";;"){return{type:"CaseItem",patterns:t,body:e,terminator:n}},subshell(t,e=[]){return{type:"Subshell",body:t,redirections:e}},group(t,e=[]){return{type:"Group",body:t,redirections:e}},functionDef(t,e,n=[]){return{type:"FunctionDef",name:t,body:e,redirections:n}},conditionalCommand(t,e=[]){return{type:"ConditionalCommand",expression:t,redirections:e}},arithmeticCommand(t,e=[]){return{type:"ArithmeticCommand",expression:t,redirections:e}}};var f;(function(t){t.EOF="EOF",t.NEWLINE="NEWLINE",t.SEMICOLON="SEMICOLON",t.AMP="AMP",t.PIPE="PIPE",t.PIPE_AMP="PIPE_AMP",t.AND_AND="AND_AND",t.OR_OR="OR_OR",t.BANG="BANG",t.LESS="LESS",t.GREAT="GREAT",t.DLESS="DLESS",t.DGREAT="DGREAT",t.LESSAND="LESSAND",t.GREATAND="GREATAND",t.LESSGREAT="LESSGREAT",t.DLESSDASH="DLESSDASH",t.CLOBBER="CLOBBER",t.TLESS="TLESS",t.AND_GREAT="AND_GREAT",t.AND_DGREAT="AND_DGREAT",t.LPAREN="LPAREN",t.RPAREN="RPAREN",t.LBRACE="LBRACE",t.RBRACE="RBRACE",t.DSEMI="DSEMI",t.SEMI_AND="SEMI_AND",t.SEMI_SEMI_AND="SEMI_SEMI_AND",t.DBRACK_START="DBRACK_START",t.DBRACK_END="DBRACK_END",t.DPAREN_START="DPAREN_START",t.DPAREN_END="DPAREN_END",t.IF="IF",t.THEN="THEN",t.ELSE="ELSE",t.ELIF="ELIF",t.FI="FI",t.FOR="FOR",t.WHILE="WHILE",t.UNTIL="UNTIL",t.DO="DO",t.DONE="DONE",t.CASE="CASE",t.ESAC="ESAC",t.IN="IN",t.FUNCTION="FUNCTION",t.SELECT="SELECT",t.TIME="TIME",t.COPROC="COPROC",t.WORD="WORD",t.NAME="NAME",t.NUMBER="NUMBER",t.ASSIGNMENT_WORD="ASSIGNMENT_WORD",t.COMMENT="COMMENT",t.HEREDOC_CONTENT="HEREDOC_CONTENT"})(f||(f={}));var We={if:f.IF,then:f.THEN,else:f.ELSE,elif:f.ELIF,fi:f.FI,for:f.FOR,while:f.WHILE,until:f.UNTIL,do:f.DO,done:f.DONE,case:f.CASE,esac:f.ESAC,in:f.IN,function:f.FUNCTION,select:f.SELECT,time:f.TIME,coproc:f.COPROC};function xr(t){let e=t.match(/^[a-zA-Z_][a-zA-Z0-9_]*/);if(!e)return!1;let n=t.slice(e[0].length);if(n===""||n==="+")return!0;if(n[0]==="["){let r=0,s=0;for(;s<n.length;s++)if(n[s]==="[")r++;else if(n[s]==="]"&&(r--,r===0))break;if(r!==0||s>=n.length)return!1;let o=n.slice(s+1);return o===""||o==="+"}return!1}var Tn=[[";",";","&",f.SEMI_SEMI_AND],["<","<","<",f.TLESS],["&",">",">",f.AND_DGREAT]],Wn=[["[","[",f.DBRACK_START],["]","]",f.DBRACK_END],["(","(",f.DPAREN_START],[")",")",f.DPAREN_END],["&","&",f.AND_AND],["|","|",f.OR_OR],[";",";",f.DSEMI],[";","&",f.SEMI_AND],["|","&",f.PIPE_AMP],[">",">",f.DGREAT],["<","&",f.LESSAND],[">","&",f.GREATAND],["<",">",f.LESSGREAT],[">","|",f.CLOBBER],["&",">",f.AND_GREAT]],Mn={"|":f.PIPE,"&":f.AMP,";":f.SEMICOLON,"(":f.LPAREN,")":f.RPAREN,"<":f.LESS,">":f.GREAT};function Fn(t){return/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t)}var Me=class{input;pos=0;line=1;column=1;tokens=[];pendingHeredocs=[];constructor(e){this.input=e}tokenize(){let n=this.input.length,r=this.tokens,s=this.pendingHeredocs;for(;this.pos<n&&(this.skipWhitespace(),!(this.pos>=n));){if(s.length>0&&r.length>0&&r[r.length-1].type===f.NEWLINE){this.readHeredocContent();continue}let o=this.nextToken();o&&r.push(o)}return r.push({type:f.EOF,value:"",start:this.pos,end:this.pos,line:this.line,column:this.column}),r}skipWhitespace(){let e=this.input,n=e.length,r=this.pos,s=this.column,o=this.line;for(;r<n;){let i=e[r];if(i===" "||i===" ")r++,s++;else if(i==="\\"&&e[r+1]===`
2
+ import{a as z,b as Q,c as U,d as J,e as X,f as R,g as W,h as fe,i as B,j as _e}from"./chunks/chunk-LV662IGP.js";import{a as K}from"./chunks/chunk-4VDEBYW7.js";import"./chunks/chunk-5KNEBKYN.js";import*as Pn from"node:fs";import*as Rn from"node:readline";var or=[{name:"echo",load:async()=>(await import("./chunks/echo-DEUIS5JO.js")).echoCommand},{name:"cat",load:async()=>(await import("./chunks/cat-YZXBF5YF.js")).catCommand},{name:"printf",load:async()=>(await import("./chunks/printf-BOFQVMMK.js")).printfCommand},{name:"ls",load:async()=>(await import("./chunks/ls-C4MPAF3H.js")).lsCommand},{name:"mkdir",load:async()=>(await import("./chunks/mkdir-LWULWDHP.js")).mkdirCommand},{name:"touch",load:async()=>(await import("./chunks/touch-XCIAYF5I.js")).touchCommand},{name:"rm",load:async()=>(await import("./chunks/rm-W6CCBEMG.js")).rmCommand},{name:"cp",load:async()=>(await import("./chunks/cp-77UY7PGN.js")).cpCommand},{name:"mv",load:async()=>(await import("./chunks/mv-TDYCNSIQ.js")).mvCommand},{name:"ln",load:async()=>(await import("./chunks/ln-DB7J2W5X.js")).lnCommand},{name:"chmod",load:async()=>(await import("./chunks/chmod-3G4LK462.js")).chmodCommand},{name:"pwd",load:async()=>(await import("./chunks/pwd-X4MWD4JP.js")).pwdCommand},{name:"readlink",load:async()=>(await import("./chunks/readlink-DPNOQY67.js")).readlinkCommand},{name:"head",load:async()=>(await import("./chunks/head-VGXR3WWL.js")).headCommand},{name:"tail",load:async()=>(await import("./chunks/tail-YSVKBQ77.js")).tailCommand},{name:"wc",load:async()=>(await import("./chunks/wc-CNJ3QDRA.js")).wcCommand},{name:"stat",load:async()=>(await import("./chunks/stat-3PHITCPO.js")).statCommand},{name:"grep",load:async()=>(await import("./chunks/grep-OVWU4WCZ.js")).grepCommand},{name:"fgrep",load:async()=>(await import("./chunks/grep-OVWU4WCZ.js")).fgrepCommand},{name:"egrep",load:async()=>(await import("./chunks/grep-OVWU4WCZ.js")).egrepCommand},{name:"sed",load:async()=>(await import("./chunks/sed-7IS44XON.js")).sedCommand},{name:"awk",load:async()=>(await import("./chunks/awk-S63F6IYK.js")).awkCommand},{name:"sort",load:async()=>(await import("./chunks/sort-UJP353TM.js")).sortCommand},{name:"uniq",load:async()=>(await import("./chunks/uniq-W4HF6YSB.js")).uniqCommand},{name:"cut",load:async()=>(await import("./chunks/cut-UYV3FM7R.js")).cutCommand},{name:"paste",load:async()=>(await import("./chunks/paste-R36J3G4K.js")).pasteCommand},{name:"tr",load:async()=>(await import("./chunks/tr-2EEKHHO6.js")).trCommand},{name:"tee",load:async()=>(await import("./chunks/tee-UU2VS3OM.js")).teeCommand},{name:"find",load:async()=>(await import("./chunks/find-4B67H4RP.js")).findCommand},{name:"basename",load:async()=>(await import("./chunks/basename-Y7JDBYHN.js")).basenameCommand},{name:"dirname",load:async()=>(await import("./chunks/dirname-VLHP44TU.js")).dirnameCommand},{name:"tree",load:async()=>(await import("./chunks/tree-4247W67O.js")).treeCommand},{name:"du",load:async()=>(await import("./chunks/du-4FZ7WF2P.js")).duCommand},{name:"env",load:async()=>(await import("./chunks/env-FVITWNHG.js")).envCommand},{name:"printenv",load:async()=>(await import("./chunks/env-FVITWNHG.js")).printenvCommand},{name:"alias",load:async()=>(await import("./chunks/alias-74DFXE2E.js")).aliasCommand},{name:"unalias",load:async()=>(await import("./chunks/alias-74DFXE2E.js")).unaliasCommand},{name:"history",load:async()=>(await import("./chunks/history-PRQ4B6N2.js")).historyCommand},{name:"xargs",load:async()=>(await import("./chunks/xargs-YNLVLPOF.js")).xargsCommand},{name:"true",load:async()=>(await import("./chunks/true-JCX733LK.js")).trueCommand},{name:"false",load:async()=>(await import("./chunks/true-JCX733LK.js")).falseCommand},{name:"clear",load:async()=>(await import("./chunks/clear-GTCFHSB2.js")).clearCommand},{name:"bash",load:async()=>(await import("./chunks/bash-TQGTNXAE.js")).bashCommand},{name:"sh",load:async()=>(await import("./chunks/bash-TQGTNXAE.js")).shCommand},{name:"jq",load:async()=>(await import("./chunks/jq-EYSXBSCP.js")).jqCommand},{name:"base64",load:async()=>(await import("./chunks/base64-NC7HTKLZ.js")).base64Command},{name:"diff",load:async()=>(await import("./chunks/diff-TG2NXCX2.js")).diffCommand},{name:"date",load:async()=>(await import("./chunks/date-7NBRXV2Z.js")).dateCommand},{name:"sleep",load:async()=>(await import("./chunks/sleep-E4DIYGTT.js")).sleepCommand},{name:"timeout",load:async()=>(await import("./chunks/timeout-OZVAGW2H.js")).timeoutCommand},{name:"seq",load:async()=>(await import("./chunks/seq-DGJILX2Q.js")).seqCommand},{name:"expr",load:async()=>(await import("./chunks/expr-HA2ZNL6S.js")).exprCommand},{name:"html-to-markdown",load:async()=>(await import("./chunks/html-to-markdown-L4UWMK4S.js")).htmlToMarkdownCommand},{name:"help",load:async()=>(await import("./chunks/help-5V3MPCYQ.js")).helpCommand},{name:"which",load:async()=>(await import("./chunks/which-HX2NMOP3.js")).whichCommand}],kn=[{name:"curl",load:async()=>(await import("./chunks/curl-NE7XEWMN.js")).curlCommand}],ar=new Map;function cr(t){return{name:t.name,async execute(e,n){let r=ar.get(t.name);return r||(r=await t.load(),ar.set(t.name,r)),r.execute(e,n)}}}function lr(t){return(t?or.filter(n=>t.includes(n.name)):or).map(cr)}function ur(){return kn.map(cr)}function fr(t){return"load"in t&&typeof t.load=="function"}function dr(t){let e=null;return{name:t.name,async execute(n,r){return e||(e=await t.load()),e.execute(n,r)}}}var Dn=new TextEncoder,_n=new TextDecoder;function ut(t,e){if(t instanceof Uint8Array)return t;switch(e){case"base64":return Uint8Array.from(atob(t),n=>n.charCodeAt(0));case"hex":{let n=new Uint8Array(t.length/2);for(let r=0;r<t.length;r+=2)n[r/2]=parseInt(t.slice(r,r+2),16);return n}case"binary":case"latin1":return Uint8Array.from(t,n=>n.charCodeAt(0));default:return Dn.encode(t)}}function hr(t,e){switch(e){case"base64":return btoa(String.fromCharCode(...t));case"hex":return Array.from(t).map(n=>n.toString(16).padStart(2,"0")).join("");case"binary":case"latin1":return String.fromCharCode(...t);default:return _n.decode(t)}}function Oe(t){if(t!=null)return typeof t=="string"?t:t.encoding??void 0}var Le=new TextEncoder;function On(t){return typeof t=="object"&&t!==null&&!(t instanceof Uint8Array)&&"content"in t}var de=class{data=new Map;constructor(e){if(this.data.set("/",{type:"directory",mode:493,mtime:new Date}),e)for(let[n,r]of Object.entries(e))On(r)?this.writeFileSync(n,r.content,void 0,{mode:r.mode,mtime:r.mtime}):this.writeFileSync(n,r)}normalizePath(e){if(!e||e==="/")return"/";let n=e.endsWith("/")&&e!=="/"?e.slice(0,-1):e;n.startsWith("/")||(n=`/${n}`);let r=n.split("/").filter(o=>o&&o!=="."),s=[];for(let o of r)o===".."?s.pop():s.push(o);return`/${s.join("/")}`||"/"}dirname(e){let n=this.normalizePath(e);if(n==="/")return"/";let r=n.lastIndexOf("/");return r===0?"/":n.slice(0,r)}ensureParentDirs(e){let n=this.dirname(e);n!=="/"&&(this.data.has(n)||(this.ensureParentDirs(n),this.data.set(n,{type:"directory",mode:493,mtime:new Date})))}writeFileSync(e,n,r,s){let o=this.normalizePath(e);this.ensureParentDirs(o);let i=Oe(r),a=ut(n,i);this.data.set(o,{type:"file",content:a,mode:s?.mode??420,mtime:s?.mtime??new Date})}async readFile(e,n){let r=await this.readFileBuffer(e),s=Oe(n);return hr(r,s)}async readFileBuffer(e){let n=this.normalizePath(e),r=this.data.get(n),s=n;if(!r)throw new Error(`ENOENT: no such file or directory, open '${e}'`);let o=new Set;for(;r&&r.type==="symlink";){if(o.has(s))throw new Error(`ELOOP: too many levels of symbolic links, open '${e}'`);o.add(s),s=this.resolveSymlink(s,r.target),r=this.data.get(s)}if(!r)throw new Error(`ENOENT: no such file or directory, open '${e}'`);if(r.type!=="file")throw new Error(`EISDIR: illegal operation on a directory, read '${e}'`);return r.content instanceof Uint8Array?r.content:Le.encode(r.content)}async writeFile(e,n,r){this.writeFileSync(e,n,r)}async appendFile(e,n,r){let s=this.normalizePath(e),o=this.data.get(s);if(o&&o.type==="directory")throw new Error(`EISDIR: illegal operation on a directory, write '${e}'`);let i=Oe(r),a=ut(n,i);if(o?.type==="file"){let l=o.content instanceof Uint8Array?o.content:Le.encode(o.content),c=new Uint8Array(l.length+a.length);c.set(l),c.set(a,l.length),this.data.set(s,{type:"file",content:c,mode:o.mode,mtime:new Date})}else this.writeFileSync(e,n,r)}async exists(e){return this.data.has(this.normalizePath(e))}async stat(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, stat '${e}'`);if(r.type==="symlink"){let o=this.resolveSymlink(n,r.target),i=this.data.get(o);if(!i)throw new Error(`ENOENT: no such file or directory, stat '${e}'`);r=i}let s=0;return r.type==="file"&&r.content&&(r.content instanceof Uint8Array?s=r.content.length:s=Le.encode(r.content).length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:s,mtime:r.mtime||new Date}}async lstat(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, lstat '${e}'`);if(r.type==="symlink")return{isFile:!1,isDirectory:!1,isSymbolicLink:!0,mode:r.mode,size:r.target.length,mtime:r.mtime||new Date};let s=0;return r.type==="file"&&r.content&&(r.content instanceof Uint8Array?s=r.content.length:s=Le.encode(r.content).length),{isFile:r.type==="file",isDirectory:r.type==="directory",isSymbolicLink:!1,mode:r.mode,size:s,mtime:r.mtime||new Date}}resolveSymlink(e,n){if(n.startsWith("/"))return this.normalizePath(n);let r=this.dirname(e);return this.normalizePath(r==="/"?`/${n}`:`${r}/${n}`)}async mkdir(e,n){this.mkdirSync(e,n)}mkdirSync(e,n){let r=this.normalizePath(e);if(this.data.has(r)){if(this.data.get(r)?.type==="file")throw new Error(`EEXIST: file already exists, mkdir '${e}'`);if(!n?.recursive)throw new Error(`EEXIST: directory already exists, mkdir '${e}'`);return}let s=this.dirname(r);if(s!=="/"&&!this.data.has(s))if(n?.recursive)this.mkdirSync(s,{recursive:!0});else throw new Error(`ENOENT: no such file or directory, mkdir '${e}'`);this.data.set(r,{type:"directory",mode:493,mtime:new Date})}async readdir(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, scandir '${e}'`);if(r.type!=="directory")throw new Error(`ENOTDIR: not a directory, scandir '${e}'`);let s=n==="/"?"/":`${n}/`,o=[];for(let i of this.data.keys())if(i!==n&&i.startsWith(s)){let l=i.slice(s.length).split("/")[0];l&&!o.includes(l)&&o.push(l)}return o.sort()}async rm(e,n){let r=this.normalizePath(e),s=this.data.get(r);if(!s){if(n?.force)return;throw new Error(`ENOENT: no such file or directory, rm '${e}'`)}if(s.type==="directory"){let o=await this.readdir(r);if(o.length>0){if(!n?.recursive)throw new Error(`ENOTEMPTY: directory not empty, rm '${e}'`);for(let i of o){let a=r==="/"?`/${i}`:`${r}/${i}`;await this.rm(a,n)}}}this.data.delete(r)}async cp(e,n,r){let s=this.normalizePath(e),o=this.normalizePath(n),i=this.data.get(s);if(!i)throw new Error(`ENOENT: no such file or directory, cp '${e}'`);if(i.type==="file")this.ensureParentDirs(o),this.data.set(o,{...i});else if(i.type==="directory"){if(!r?.recursive)throw new Error(`EISDIR: is a directory, cp '${e}'`);await this.mkdir(o,{recursive:!0});let a=await this.readdir(s);for(let l of a){let c=s==="/"?`/${l}`:`${s}/${l}`,u=o==="/"?`/${l}`:`${o}/${l}`;await this.cp(c,u,r)}}}async mv(e,n){await this.cp(e,n,{recursive:!0}),await this.rm(e,{recursive:!0})}getAllPaths(){return Array.from(this.data.keys())}resolvePath(e,n){if(n.startsWith("/"))return this.normalizePath(n);let r=e==="/"?`/${n}`:`${e}/${n}`;return this.normalizePath(r)}async chmod(e,n){let r=this.normalizePath(e),s=this.data.get(r);if(!s)throw new Error(`ENOENT: no such file or directory, chmod '${e}'`);s.mode=n}async symlink(e,n){let r=this.normalizePath(n);if(this.data.has(r))throw new Error(`EEXIST: file already exists, symlink '${n}'`);this.ensureParentDirs(r),this.data.set(r,{type:"symlink",target:e,mode:511,mtime:new Date})}async link(e,n){let r=this.normalizePath(e),s=this.normalizePath(n),o=this.data.get(r);if(!o)throw new Error(`ENOENT: no such file or directory, link '${e}'`);if(o.type!=="file")throw new Error(`EPERM: operation not permitted, link '${e}'`);if(this.data.has(s))throw new Error(`EEXIST: file already exists, link '${n}'`);this.ensureParentDirs(s),this.data.set(s,{type:"file",content:o.content,mode:o.mode,mtime:o.mtime})}async readlink(e){let n=this.normalizePath(e),r=this.data.get(n);if(!r)throw new Error(`ENOENT: no such file or directory, readlink '${e}'`);if(r.type!=="symlink")throw new Error(`EINVAL: invalid argument, readlink '${e}'`);return r.target}};function L(t,e){return{type:"ArithmeticExpression",expression:ee(t,e,0).expr}}function ee(t,e,n){return Ln(t,e,n)}function Ln(t,e,n){let{expr:r,pos:s}=We(t,e,n);for(s=D(e,s);e[s]===",";){s++;let{expr:o,pos:i}=We(t,e,s);r={type:"ArithBinary",operator:",",left:r,right:o},s=D(e,i)}return{expr:r,pos:s}}function We(t,e,n){let{expr:r,pos:s}=Tn(t,e,n);if(s=D(e,s),e[s]==="?"){s++;let{expr:o,pos:i}=ee(t,e,s);if(s=D(e,i),e[s]===":"){s++;let{expr:a,pos:l}=ee(t,e,s);return{expr:{type:"ArithTernary",condition:r,consequent:o,alternate:a},pos:l}}}return{expr:r,pos:s}}function Tn(t,e,n){let{expr:r,pos:s}=pr(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="||";){s+=2;let{expr:o,pos:i}=pr(t,e,s);r={type:"ArithBinary",operator:"||",left:r,right:o},s=i}return{expr:r,pos:s}}function pr(t,e,n){let{expr:r,pos:s}=mr(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="&&";){s+=2;let{expr:o,pos:i}=mr(t,e,s);r={type:"ArithBinary",operator:"&&",left:r,right:o},s=i}return{expr:r,pos:s}}function mr(t,e,n){let{expr:r,pos:s}=yr(t,e,n);for(;s=D(e,s),e[s]==="|"&&e[s+1]!=="|";){s++;let{expr:o,pos:i}=yr(t,e,s);r={type:"ArithBinary",operator:"|",left:r,right:o},s=i}return{expr:r,pos:s}}function yr(t,e,n){let{expr:r,pos:s}=Er(t,e,n);for(;s=D(e,s),e[s]==="^";){s++;let{expr:o,pos:i}=Er(t,e,s);r={type:"ArithBinary",operator:"^",left:r,right:o},s=i}return{expr:r,pos:s}}function Er(t,e,n){let{expr:r,pos:s}=wr(t,e,n);for(;s=D(e,s),e[s]==="&"&&e[s+1]!=="&";){s++;let{expr:o,pos:i}=wr(t,e,s);r={type:"ArithBinary",operator:"&",left:r,right:o},s=i}return{expr:r,pos:s}}function wr(t,e,n){let{expr:r,pos:s}=gr(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="=="||e.slice(s,s+2)==="!=";){let o=e.slice(s,s+2);s+=2;let{expr:i,pos:a}=gr(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}return{expr:r,pos:s}}function gr(t,e,n){let{expr:r,pos:s}=ft(t,e,n);for(;;)if(s=D(e,s),e.slice(s,s+2)==="<="||e.slice(s,s+2)===">="){let o=e.slice(s,s+2);s+=2;let{expr:i,pos:a}=ft(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}else if(e[s]==="<"||e[s]===">"){let o=e[s];s++;let{expr:i,pos:a}=ft(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}else break;return{expr:r,pos:s}}function ft(t,e,n){let{expr:r,pos:s}=Ar(t,e,n);for(;s=D(e,s),e.slice(s,s+2)==="<<"||e.slice(s,s+2)===">>";){let o=e.slice(s,s+2);s+=2;let{expr:i,pos:a}=Ar(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}return{expr:r,pos:s}}function Ar(t,e,n){let{expr:r,pos:s}=xr(t,e,n);for(;s=D(e,s),(e[s]==="+"||e[s]==="-")&&e[s+1]!==e[s];){let o=e[s];s++;let{expr:i,pos:a}=xr(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}return{expr:r,pos:s}}function xr(t,e,n){let{expr:r,pos:s}=Te(t,e,n);for(;;)if(s=D(e,s),e[s]==="*"&&e[s+1]!=="*"){s++;let{expr:o,pos:i}=Te(t,e,s);r={type:"ArithBinary",operator:"*",left:r,right:o},s=i}else if(e[s]==="/"||e[s]==="%"){let o=e[s];s++;let{expr:i,pos:a}=Te(t,e,s);r={type:"ArithBinary",operator:o,left:r,right:i},s=a}else break;return{expr:r,pos:s}}function Te(t,e,n){let{expr:r,pos:s}=dt(t,e,n),o=D(e,s);if(e.slice(o,o+2)==="**"){o+=2;let{expr:i,pos:a}=Te(t,e,o);return{expr:{type:"ArithBinary",operator:"**",left:r,right:i},pos:a}}return{expr:r,pos:s}}function dt(t,e,n){let r=D(e,n);if(e.slice(r,r+2)==="++"||e.slice(r,r+2)==="--"){let s=e.slice(r,r+2);r+=2;let{expr:o,pos:i}=dt(t,e,r);return{expr:{type:"ArithUnary",operator:s,operand:o,prefix:!0},pos:i}}if(e[r]==="+"||e[r]==="-"||e[r]==="!"||e[r]==="~"){let s=e[r];r++;let{expr:o,pos:i}=dt(t,e,r);return{expr:{type:"ArithUnary",operator:s,operand:o,prefix:!0},pos:i}}return Mn(t,e,r)}function Wn(t,e){let n=t[e];return n==="$"||n==="`"}function Mn(t,e,n){let{expr:r,pos:s}=Sr(t,e,n),o=[r];for(;Wn(e,s);){let{expr:i,pos:a}=Sr(t,e,s);o.push(i),s=a}if(o.length>1&&(r={type:"ArithConcat",parts:o}),s=D(e,s),e.slice(s,s+2)==="++"||e.slice(s,s+2)==="--"){let i=e.slice(s,s+2);return s+=2,{expr:{type:"ArithUnary",operator:i,operand:r,prefix:!1},pos:s}}return{expr:r,pos:s}}function Sr(t,e,n){let r=D(e,n);if(e.slice(r,r+3)==="$(("){r+=3;let s=1,o=r;for(;r<e.length-1&&s>0;)e[r]==="("&&e[r+1]==="("?(s++,r+=2):e[r]===")"&&e[r+1]===")"?(s--,s>0&&(r+=2)):r++;let i=e.slice(o,r),{expr:a}=ee(t,i,0);return r+=2,{expr:{type:"ArithNested",expression:a},pos:r}}if(e.slice(r,r+2)==="$("&&e[r+2]!=="("){r+=2;let s=1,o=r;for(;r<e.length&&s>0;)e[r]==="("?s++:e[r]===")"&&s--,s>0&&r++;let i=e.slice(o,r);return r++,{expr:{type:"ArithCommandSubst",command:i},pos:r}}if(e[r]==="`"){r++;let s=r;for(;r<e.length&&e[r]!=="`";)r++;let o=e.slice(s,r);return e[r]==="`"&&r++,{expr:{type:"ArithCommandSubst",command:o},pos:r}}if(e[r]==="("){r++;let{expr:s,pos:o}=ee(t,e,r);return r=D(e,o),e[r]===")"&&r++,{expr:{type:"ArithGroup",expression:s},pos:r}}if(/[0-9]/.test(e[r])){let s="",o=!1;for(;r<e.length;){let a=e[r];if(o)if(/[0-9a-zA-Z@_]/.test(a))s+=a,r++;else break;else if(a==="#")o=!0,s+=a,r++;else if(/[0-9a-fA-FxX]/.test(a))s+=a,r++;else break}if(e[r]==="."&&/[0-9]/.test(e[r+1]))throw new W(`${s}.${e[r+1]}...: syntax error: invalid arithmetic operator`);if(e[r]==="["){let a=e.slice(r).trim();return{expr:{type:"ArithNumberSubscript",number:s,errorToken:a},pos:e.length}}return{expr:{type:"ArithNumber",value:Ee(s)},pos:r}}if(e[r]==="$"&&e[r+1]==="{"){let s=r+2,o=1,i=s;for(;i<e.length&&o>0;)e[i]==="{"?o++:e[i]==="}"&&o--,o>0&&i++;let a=e.slice(s,i),l=i+1;if(e[l]==="#"){let c=l+1;for(;c<e.length&&/[0-9a-zA-Z@_]/.test(e[c]);)c++;let u=e.slice(l+1,c);return{expr:{type:"ArithDynamicBase",baseExpr:a,value:u},pos:c}}if(/[0-9]/.test(e[l])||e[l]==="x"||e[l]==="X"){let c=l;if(e[l]==="x"||e[l]==="X")for(c++;c<e.length&&/[0-9a-fA-F]/.test(e[c]);)c++;else for(;c<e.length&&/[0-9]/.test(e[c]);)c++;let u=e.slice(l,c);return{expr:{type:"ArithDynamicNumber",prefix:a,suffix:u},pos:c}}return r=l,{expr:{type:"ArithBracedExpansion",content:a},pos:r}}if(e[r]==="$"&&r+1<e.length&&/[0-9]/.test(e[r+1])){r++;let s="";for(;r<e.length&&/[0-9]/.test(e[r]);)s+=e[r],r++;return{expr:{type:"ArithVariable",name:s},pos:r}}if(e[r]==="$"&&r+1<e.length&&/[a-zA-Z_]/.test(e[r+1])&&r++,/[a-zA-Z_]/.test(e[r])){let s="";for(;r<e.length&&/[a-zA-Z0-9_]/.test(e[r]);)s+=e[r],r++;if(e[r]==="["){r++;let i;if(e[r]==="'"||e[r]==='"'){let c=e[r];for(r++,i="";r<e.length&&e[r]!==c;)i+=e[r],r++;e[r]===c&&r++,r=D(e,r),e[r]==="]"&&r++}let a;if(i===void 0){let{expr:c,pos:u}=ee(t,e,r);a=c,r=u,e[r]==="]"&&r++}if(r=D(e,r),e[r]==="["&&a)return{expr:{type:"ArithDoubleSubscript",array:s,index:a},pos:r};let l=["=","+=","-=","*=","/=","%=","<<=",">>=","&=","|=","^="];for(let c of l)if(e.slice(r,r+c.length)===c&&e.slice(r,r+c.length+1)!=="=="){r+=c.length;let{expr:u,pos:d}=We(t,e,r);return{expr:{type:"ArithAssignment",operator:c,variable:s,subscript:a,stringKey:i,value:u},pos:d}}return{expr:{type:"ArithArrayElement",array:s,index:a,stringKey:i},pos:r}}r=D(e,r);let o=["=","+=","-=","*=","/=","%=","<<=",">>=","&=","|=","^="];for(let i of o)if(e.slice(r,r+i.length)===i&&e.slice(r,r+i.length+1)!=="=="){r+=i.length;let{expr:a,pos:l}=We(t,e,r);return{expr:{type:"ArithAssignment",operator:i,variable:s,value:a},pos:l}}return{expr:{type:"ArithVariable",name:s},pos:r}}return{expr:{type:"ArithNumber",value:0},pos:r}}function Ee(t){if(t.includes("#")){let[e,n]=t.split("#"),r=Number.parseInt(e,10);if(r<2||r>64)return Number.NaN;if(r<=36)return Number.parseInt(n,r);let s=0;for(let o of n){let i;if(o>="0"&&o<="9")i=o.charCodeAt(0)-48;else if(o>="a"&&o<="z")i=o.charCodeAt(0)-97+10;else if(o>="A"&&o<="Z")i=o.charCodeAt(0)-65+36;else if(o==="@")i=62;else if(o==="_")i=63;else return Number.NaN;if(i>=r)return Number.NaN;s=s*r+i}return s}return t.startsWith("0x")||t.startsWith("0X")?Number.parseInt(t.slice(2),16):t.startsWith("0")&&t.length>1&&/^[0-9]+$/.test(t)?/[89]/.test(t)?Number.NaN:Number.parseInt(t,8):Number.parseInt(t,10)}function D(t,e){for(;e<t.length;){if(t[e]==="\\"&&t[e+1]===`
3
+ `){e+=2;continue}if(/\s/.test(t[e])){e++;continue}break}return e}var A={script(t){return{type:"Script",statements:t}},statement(t,e=[],n=!1){return{type:"Statement",pipelines:t,operators:e,background:n}},pipeline(t,e=!1){return{type:"Pipeline",commands:t,negated:e}},simpleCommand(t,e=[],n=[],r=[]){return{type:"SimpleCommand",name:t,args:e,assignments:n,redirections:r}},word(t){return{type:"Word",parts:t}},literal(t){return{type:"Literal",value:t}},singleQuoted(t){return{type:"SingleQuoted",value:t}},doubleQuoted(t){return{type:"DoubleQuoted",parts:t}},escaped(t){return{type:"Escaped",value:t}},parameterExpansion(t,e=null){return{type:"ParameterExpansion",parameter:t,operation:e}},commandSubstitution(t,e=!1){return{type:"CommandSubstitution",body:t,legacy:e}},arithmeticExpansion(t){return{type:"ArithmeticExpansion",expression:t}},assignment(t,e,n=!1,r=null){return{type:"Assignment",name:t,value:e,append:n,array:r}},redirection(t,e,n=null){return{type:"Redirection",fd:n,operator:t,target:e}},hereDoc(t,e,n=!1,r=!1){return{type:"HereDoc",delimiter:t,content:e,stripTabs:n,quoted:r}},ifNode(t,e=null,n=[]){return{type:"If",clauses:t,elseBody:e,redirections:n}},forNode(t,e,n,r=[]){return{type:"For",variable:t,words:e,body:n,redirections:r}},whileNode(t,e,n=[]){return{type:"While",condition:t,body:e,redirections:n}},untilNode(t,e,n=[]){return{type:"Until",condition:t,body:e,redirections:n}},caseNode(t,e,n=[]){return{type:"Case",word:t,items:e,redirections:n}},caseItem(t,e,n=";;"){return{type:"CaseItem",patterns:t,body:e,terminator:n}},subshell(t,e=[]){return{type:"Subshell",body:t,redirections:e}},group(t,e=[]){return{type:"Group",body:t,redirections:e}},functionDef(t,e,n=[]){return{type:"FunctionDef",name:t,body:e,redirections:n}},conditionalCommand(t,e=[]){return{type:"ConditionalCommand",expression:t,redirections:e}},arithmeticCommand(t,e=[]){return{type:"ArithmeticCommand",expression:t,redirections:e}}};var f;(function(t){t.EOF="EOF",t.NEWLINE="NEWLINE",t.SEMICOLON="SEMICOLON",t.AMP="AMP",t.PIPE="PIPE",t.PIPE_AMP="PIPE_AMP",t.AND_AND="AND_AND",t.OR_OR="OR_OR",t.BANG="BANG",t.LESS="LESS",t.GREAT="GREAT",t.DLESS="DLESS",t.DGREAT="DGREAT",t.LESSAND="LESSAND",t.GREATAND="GREATAND",t.LESSGREAT="LESSGREAT",t.DLESSDASH="DLESSDASH",t.CLOBBER="CLOBBER",t.TLESS="TLESS",t.AND_GREAT="AND_GREAT",t.AND_DGREAT="AND_DGREAT",t.LPAREN="LPAREN",t.RPAREN="RPAREN",t.LBRACE="LBRACE",t.RBRACE="RBRACE",t.DSEMI="DSEMI",t.SEMI_AND="SEMI_AND",t.SEMI_SEMI_AND="SEMI_SEMI_AND",t.DBRACK_START="DBRACK_START",t.DBRACK_END="DBRACK_END",t.DPAREN_START="DPAREN_START",t.DPAREN_END="DPAREN_END",t.IF="IF",t.THEN="THEN",t.ELSE="ELSE",t.ELIF="ELIF",t.FI="FI",t.FOR="FOR",t.WHILE="WHILE",t.UNTIL="UNTIL",t.DO="DO",t.DONE="DONE",t.CASE="CASE",t.ESAC="ESAC",t.IN="IN",t.FUNCTION="FUNCTION",t.SELECT="SELECT",t.TIME="TIME",t.COPROC="COPROC",t.WORD="WORD",t.NAME="NAME",t.NUMBER="NUMBER",t.ASSIGNMENT_WORD="ASSIGNMENT_WORD",t.COMMENT="COMMENT",t.HEREDOC_CONTENT="HEREDOC_CONTENT"})(f||(f={}));var Me={if:f.IF,then:f.THEN,else:f.ELSE,elif:f.ELIF,fi:f.FI,for:f.FOR,while:f.WHILE,until:f.UNTIL,do:f.DO,done:f.DONE,case:f.CASE,esac:f.ESAC,in:f.IN,function:f.FUNCTION,select:f.SELECT,time:f.TIME,coproc:f.COPROC};function Cr(t){let e=t.match(/^[a-zA-Z_][a-zA-Z0-9_]*/);if(!e)return!1;let n=t.slice(e[0].length);if(n===""||n==="+")return!0;if(n[0]==="["){let r=0,s=0;for(;s<n.length;s++)if(n[s]==="[")r++;else if(n[s]==="]"&&(r--,r===0))break;if(r!==0||s>=n.length)return!1;let o=n.slice(s+1);return o===""||o==="+"}return!1}var Fn=[[";",";","&",f.SEMI_SEMI_AND],["<","<","<",f.TLESS],["&",">",">",f.AND_DGREAT]],Bn=[["[","[",f.DBRACK_START],["]","]",f.DBRACK_END],["(","(",f.DPAREN_START],[")",")",f.DPAREN_END],["&","&",f.AND_AND],["|","|",f.OR_OR],[";",";",f.DSEMI],[";","&",f.SEMI_AND],["|","&",f.PIPE_AMP],[">",">",f.DGREAT],["<","&",f.LESSAND],[">","&",f.GREATAND],["<",">",f.LESSGREAT],[">","|",f.CLOBBER],["&",">",f.AND_GREAT]],zn={"|":f.PIPE,"&":f.AMP,";":f.SEMICOLON,"(":f.LPAREN,")":f.RPAREN,"<":f.LESS,">":f.GREAT};function Un(t){return/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(t)}var Fe=class{input;pos=0;line=1;column=1;tokens=[];pendingHeredocs=[];constructor(e){this.input=e}tokenize(){let n=this.input.length,r=this.tokens,s=this.pendingHeredocs;for(;this.pos<n&&(this.skipWhitespace(),!(this.pos>=n));){if(s.length>0&&r.length>0&&r[r.length-1].type===f.NEWLINE){this.readHeredocContent();continue}let o=this.nextToken();o&&r.push(o)}return r.push({type:f.EOF,value:"",start:this.pos,end:this.pos,line:this.line,column:this.column}),r}skipWhitespace(){let e=this.input,n=e.length,r=this.pos,s=this.column,o=this.line;for(;r<n;){let i=e[r];if(i===" "||i===" ")r++,s++;else if(i==="\\"&&e[r+1]===`
4
4
  `)r+=2,o++,s=1;else break}this.pos=r,this.column=s,this.line=o}nextToken(){let e=this.input,n=this.pos,r=this.line,s=this.column,o=e[n],i=e[n+1],a=e[n+2];if(o==="#")return this.readComment(n,r,s);if(o===`
5
5
  `)return this.pos=n+1,this.line++,this.column=1,{type:f.NEWLINE,value:`
6
- `,start:n,end:n+1,line:r,column:s};if(o==="<"&&i==="<"&&a==="-")return this.pos=n+3,this.column=s+3,this.registerHeredocFromLookahead(!0),this.makeToken(f.DLESSDASH,"<<-",n,r,s);for(let[c,u,d,h]of Tn)if(o===c&&i===u&&a===d)return this.pos=n+3,this.column=s+3,this.makeToken(h,c+u+d,n,r,s);if(o==="<"&&i==="<")return this.pos=n+2,this.column=s+2,this.registerHeredocFromLookahead(!1),this.makeToken(f.DLESS,"<<",n,r,s);for(let[c,u,d]of Wn)if(o===c&&i===u)return this.pos=n+2,this.column=s+2,this.makeToken(d,c+u,n,r,s);let l=Mn[o];return l?(this.pos=n+1,this.column=s+1,this.makeToken(l,o,n,r,s)):o==="{"?i==="}"?(this.pos=n+2,this.column=s+2,{type:f.WORD,value:"{}",start:n,end:n+2,line:r,column:s,quoted:!1,singleQuoted:!1}):this.scanBraceExpansion(n)!==null?this.readWordWithBraceExpansion(n,r,s):this.scanLiteralBraceWord(n)!==null?this.readWordWithBraceExpansion(n,r,s):i!==void 0&&i!==" "&&i!==" "&&i!==`
6
+ `,start:n,end:n+1,line:r,column:s};if(o==="<"&&i==="<"&&a==="-")return this.pos=n+3,this.column=s+3,this.registerHeredocFromLookahead(!0),this.makeToken(f.DLESSDASH,"<<-",n,r,s);for(let[c,u,d,h]of Fn)if(o===c&&i===u&&a===d)return this.pos=n+3,this.column=s+3,this.makeToken(h,c+u+d,n,r,s);if(o==="<"&&i==="<")return this.pos=n+2,this.column=s+2,this.registerHeredocFromLookahead(!1),this.makeToken(f.DLESS,"<<",n,r,s);for(let[c,u,d]of Bn)if(o===c&&i===u)return this.pos=n+2,this.column=s+2,this.makeToken(d,c+u,n,r,s);let l=zn[o];return l?(this.pos=n+1,this.column=s+1,this.makeToken(l,o,n,r,s)):o==="{"?i==="}"?(this.pos=n+2,this.column=s+2,{type:f.WORD,value:"{}",start:n,end:n+2,line:r,column:s,quoted:!1,singleQuoted:!1}):this.scanBraceExpansion(n)!==null?this.readWordWithBraceExpansion(n,r,s):this.scanLiteralBraceWord(n)!==null?this.readWordWithBraceExpansion(n,r,s):i!==void 0&&i!==" "&&i!==" "&&i!==`
7
7
  `?this.readWord(n,r,s):(this.pos=n+1,this.column=s+1,this.makeToken(f.LBRACE,"{",n,r,s)):o==="}"?this.isWordCharFollowing(n+1)?this.readWord(n,r,s):(this.pos=n+1,this.column=s+1,this.makeToken(f.RBRACE,"}",n,r,s)):o==="!"?i==="="?(this.pos=n+2,this.column=s+2,this.makeToken(f.WORD,"!=",n,r,s)):(this.pos=n+1,this.column=s+1,this.makeToken(f.BANG,"!",n,r,s)):this.readWord(n,r,s)}makeToken(e,n,r,s,o){return{type:e,value:n,start:r,end:this.pos,line:s,column:o}}readComment(e,n,r){let s=this.input,o=s.length,i=this.pos;for(;i<o&&s[i]!==`
8
8
  `;)i++;let a=s.slice(e,i);return this.pos=i,this.column=r+(i-e),{type:f.COMMENT,value:a,start:e,end:i,line:n,column:r}}readWord(e,n,r){let s=this.input,o=s.length,i=this.pos,a=i;for(;i<o;){let m=s[i];if(m===" "||m===" "||m===`
9
9
  `||m===";"||m==="&"||m==="|"||m==="("||m===")"||m==="<"||m===">"||m==="'"||m==='"'||m==="\\"||m==="$"||m==="`"||m==="{"||m==="}"||m==="~"||m==="*"||m==="?"||m==="[")break;i++}if(i>a){let m=s[i];if(i>=o||m===" "||m===" "||m===`
10
- `||m===";"||m==="&"||m==="|"||m==="("||m===")"||m==="<"||m===">"){let E=s.slice(a,i);if(this.pos=i,this.column=r+(i-a),We[E])return{type:We[E],value:E,start:e,end:i,line:n,column:r};let w=E.indexOf("=");return w>0&&xr(E.slice(0,w))?{type:f.ASSIGNMENT_WORD,value:E,start:e,end:i,line:n,column:r}:/^[0-9]+$/.test(E)?{type:f.NUMBER,value:E,start:e,end:i,line:n,column:r}:/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(E)?{type:f.NAME,value:E,start:e,end:i,line:n,column:r,quoted:!1,singleQuoted:!1}:{type:f.WORD,value:E,start:e,end:i,line:n,column:r,quoted:!1,singleQuoted:!1}}}i=this.pos;let l=this.column,c=this.line,u="",d=!1,h=!1,p=!1,y=!1,A=s[i]==='"'||s[i]==="'";for(;i<o;){let m=s[i];if(!p&&!y&&(m===" "||m===" "||m===`
11
- `||m===";"||m==="&"||m==="|"||m==="("||m===")"||m==="<"||m===">"))break;if(m==="$"&&i+1<o&&s[i+1]==="'"&&!p&&!y){for(u+="$'",i+=2,l+=2;i<o&&s[i]!=="'";)s[i]==="\\"&&i+1<o?(u+=s[i]+s[i+1],i+=2,l+=2):(u+=s[i],i++,l++);i<o&&(u+="'",i++,l++);continue}if(m==="$"&&i+1<o&&s[i+1]==='"'&&!p&&!y){i++,l++,y=!0,d=!0,u===""&&(A=!0),i++,l++;continue}if(m==="'"&&!y){p?(p=!1,A||(u+=m)):(p=!0,A?(h=!0,d=!0):u+=m),i++,l++;continue}if(m==='"'&&!p){y?(y=!1,A||(u+=m)):(y=!0,A?d=!0:u+=m),i++,l++;continue}if(m==="\\"&&!p&&i+1<o){let E=s[i+1];if(E===`
10
+ `||m===";"||m==="&"||m==="|"||m==="("||m===")"||m==="<"||m===">"){let E=s.slice(a,i);if(this.pos=i,this.column=r+(i-a),Me[E])return{type:Me[E],value:E,start:e,end:i,line:n,column:r};let w=E.indexOf("=");return w>0&&Cr(E.slice(0,w))?{type:f.ASSIGNMENT_WORD,value:E,start:e,end:i,line:n,column:r}:/^[0-9]+$/.test(E)?{type:f.NUMBER,value:E,start:e,end:i,line:n,column:r}:/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(E)?{type:f.NAME,value:E,start:e,end:i,line:n,column:r,quoted:!1,singleQuoted:!1}:{type:f.WORD,value:E,start:e,end:i,line:n,column:r,quoted:!1,singleQuoted:!1}}}i=this.pos;let l=this.column,c=this.line,u="",d=!1,h=!1,p=!1,y=!1,g=s[i]==='"'||s[i]==="'";for(;i<o;){let m=s[i];if(!p&&!y&&(m===" "||m===" "||m===`
11
+ `||m===";"||m==="&"||m==="|"||m==="("||m===")"||m==="<"||m===">"))break;if(m==="$"&&i+1<o&&s[i+1]==="'"&&!p&&!y){for(u+="$'",i+=2,l+=2;i<o&&s[i]!=="'";)s[i]==="\\"&&i+1<o?(u+=s[i]+s[i+1],i+=2,l+=2):(u+=s[i],i++,l++);i<o&&(u+="'",i++,l++);continue}if(m==="$"&&i+1<o&&s[i+1]==='"'&&!p&&!y){i++,l++,y=!0,d=!0,u===""&&(g=!0),i++,l++;continue}if(m==="'"&&!y){p?(p=!1,g||(u+=m)):(p=!0,g?(h=!0,d=!0):u+=m),i++,l++;continue}if(m==='"'&&!p){y?(y=!1,g||(u+=m)):(y=!0,g?d=!0:u+=m),i++,l++;continue}if(m==="\\"&&!p&&i+1<o){let E=s[i+1];if(E===`
12
12
  `){i+=2,c++,l=1;continue}if(y){if(E==='"'||E==="\\"||E==="$"||E==="`"||E===`
13
13
  `){E==="$"||E==="`"?u+=m+E:u+=E,i+=2,l+=2;continue}}else{E==='"'||E==="'"?u+=m+E:u+=E,i+=2,l+=2;continue}}if(m==="$"&&i+1<o&&s[i+1]==="("){u+=m,i++,l++,u+=s[i],i++,l++;let E=1,w=!1,b=!1,I=0,P=!1,k="",ue=s[i]==="(";for(;E>0&&i<o;){let O=s[i];if(u+=O,w)O==="'"&&(w=!1);else if(b)O==="\\"&&i+1<o?(u+=s[i+1],i++,l++):O==='"'&&(b=!1);else if(O==="'")w=!0,k="";else if(O==='"')b=!0,k="";else if(O==="\\"&&i+1<o)u+=s[i+1],i++,l++,k="";else if(O==="#"&&!ue&&(k===""||/\s/.test(s[i-1]||""))){for(;i+1<o&&s[i+1]!==`
14
14
  `;)i++,l++,u+=s[i];k=""}else/[a-zA-Z_]/.test(O)?k+=O:(k==="case"?(I++,P=!1):k==="in"&&I>0?P=!0:k==="esac"&&I>0&&(I--,P=!1),k="",O==="("?i>0&&s[i-1]==="$"?E++:P||E++:O===")"?P?P=!1:E--:O===";"&&I>0&&i+1<o&&s[i+1]===";"&&(P=!0));O===`
@@ -16,7 +16,7 @@ import{a as z,b as Q,c as U,d as J,e as X,f as R,g as W,h as fe,i as B,j as Oe}f
16
16
  `&&(c++,l=0),i++,l++}continue}if(m==="$"&&i+1<o&&s[i+1]==="{"){u+=m,i++,l++,u+=s[i],i++,l++;let E=1;for(;E>0&&i<o;){let w=s[i];u+=w,w==="{"?E++:w==="}"?E--:w===`
17
17
  `&&(c++,l=0),i++,l++}continue}if(m==="$"&&i+1<o){let E=s[i+1];if(E==="#"||E==="?"||E==="$"||E==="!"||E==="@"||E==="*"||E==="-"||E>="0"&&E<="9"){u+=m+E,i+=2,l+=2;continue}}if(m==="`"){for(u+=m,i++,l++;i<o&&s[i]!=="`";){let E=s[i];u+=E,E==="\\"&&i+1<o&&(u+=s[i+1],i++,l++),E===`
18
18
  `&&(c++,l=0),i++,l++}i<o&&(u+=s[i],i++,l++);continue}u+=m,i++,m===`
19
- `?(c++,l=1):l++}if(this.pos=i,this.column=l,this.line=c,u==="")return{type:f.WORD,value:"",start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h};if(!d&&We[u])return{type:We[u],value:u,start:e,end:i,line:n,column:r};if(!A){let m=u.indexOf("=");if(m>0&&xr(u.slice(0,m)))return{type:f.ASSIGNMENT_WORD,value:u,start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h}}return/^[0-9]+$/.test(u)?{type:f.NUMBER,value:u,start:e,end:i,line:n,column:r}:Fn(u)?{type:f.NAME,value:u,start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h}:{type:f.WORD,value:u,start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h}}readHeredocContent(){for(;this.pendingHeredocs.length>0;){let e=this.pendingHeredocs.shift();if(!e)break;let n=this.pos,r=this.line,s=this.column,o="";for(;this.pos<this.input.length;){let i=this.pos,a="";for(;this.pos<this.input.length&&this.input[this.pos]!==`
19
+ `?(c++,l=1):l++}if(this.pos=i,this.column=l,this.line=c,u==="")return{type:f.WORD,value:"",start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h};if(!d&&Me[u])return{type:Me[u],value:u,start:e,end:i,line:n,column:r};if(!g){let m=u.indexOf("=");if(m>0&&Cr(u.slice(0,m)))return{type:f.ASSIGNMENT_WORD,value:u,start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h}}return/^[0-9]+$/.test(u)?{type:f.NUMBER,value:u,start:e,end:i,line:n,column:r}:Un(u)?{type:f.NAME,value:u,start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h}:{type:f.WORD,value:u,start:e,end:i,line:n,column:r,quoted:d,singleQuoted:h}}readHeredocContent(){for(;this.pendingHeredocs.length>0;){let e=this.pendingHeredocs.shift();if(!e)break;let n=this.pos,r=this.line,s=this.column,o="";for(;this.pos<this.input.length;){let i=this.pos,a="";for(;this.pos<this.input.length&&this.input[this.pos]!==`
20
20
  `;)a+=this.input[this.pos],this.pos++,this.column++;if((e.stripTabs?a.replace(/^\t+/,""):a)===e.delimiter){this.pos<this.input.length&&this.input[this.pos]===`
21
21
  `&&(this.pos++,this.line++,this.column=1);break}o+=a,this.pos<this.input.length&&this.input[this.pos]===`
22
22
  `&&(o+=`
@@ -24,46 +24,46 @@ import{a as z,b as Q,c as U,d as J,e as X,f as R,g as W,h as fe,i as B,j as Oe}f
24
24
  `||n===";"||n==="&"||n==="|"||n==="("||n===")"||n==="<"||n===">")}readWordWithBraceExpansion(e,n,r){let s=this.input,o=s.length,i=e,a=r;for(;i<o;){let c=s[i];if(c===" "||c===" "||c===`
25
25
  `||c===";"||c==="&"||c==="|"||c==="("||c===")"||c==="<"||c===">")break;if(c==="{"){if(this.scanBraceExpansion(i)!==null){let d=1;for(i++,a++;i<o&&d>0;)s[i]==="{"?d++:s[i]==="}"&&d--,i++,a++;continue}i++,a++;continue}if(c==="}"){i++,a++;continue}if(c==="$"&&i+1<o&&s[i+1]==="("){i++,a++,i++,a++;let u=1;for(;u>0&&i<o;)s[i]==="("?u++:s[i]===")"&&u--,i++,a++;continue}if(c==="$"&&i+1<o&&s[i+1]==="{"){i++,a++,i++,a++;let u=1;for(;u>0&&i<o;)s[i]==="{"?u++:s[i]==="}"&&u--,i++,a++;continue}if(c==="`"){for(i++,a++;i<o&&s[i]!=="`";)s[i]==="\\"&&i+1<o?(i+=2,a+=2):(i++,a++);i<o&&(i++,a++);continue}i++,a++}let l=s.slice(e,i);return this.pos=i,this.column=a,{type:f.WORD,value:l,start:e,end:i,line:n,column:r,quoted:!1,singleQuoted:!1}}scanBraceExpansion(e){let n=this.input,r=n.length,s=e+1,o=1,i=!1,a=!1;for(;s<r&&o>0;){let l=n[s];if(l==="{")o++,s++;else if(l==="}")o--,s++;else if(l===","&&o===1)i=!0,s++;else if(l==="."&&s+1<r&&n[s+1]===".")a=!0,s+=2;else{if(l===" "||l===" "||l===`
26
26
  `||l===";"||l==="&"||l==="|")return null;s++}}return o===0&&(i||a)?n.slice(e,s):null}scanLiteralBraceWord(e){let n=this.input,r=n.length,s=e+1,o=1;for(;s<r&&o>0;){let i=n[s];if(i==="{")o++,s++;else if(i==="}"){if(o--,o===0)return n.slice(e,s+1);s++}else{if(i===" "||i===" "||i===`
27
- `||i===";"||i==="&"||i==="|")return null;s++}}return null}};var dt=1e6,ht=1e5,Sr=1e6,br=new Set([f.LESS,f.GREAT,f.DLESS,f.DGREAT,f.LESSAND,f.GREATAND,f.LESSGREAT,f.DLESSDASH,f.CLOBBER,f.TLESS,f.AND_GREAT,f.AND_DGREAT]),Cr=new Set([f.LESS,f.GREAT,f.DLESS,f.DGREAT,f.LESSAND,f.GREATAND,f.LESSGREAT,f.DLESSDASH,f.CLOBBER,f.TLESS]),te=class extends Error{line;column;token;constructor(e,n,r,s=void 0){super(`Parse error at ${n}:${r}: ${e}`),this.line=n,this.column=r,this.token=s,this.name="ParseException"}};function $r(t,e,n){let r=n+1;for(;r<e.length&&/[a-zA-Z0-9_-]/.test(e[r]);)r++;return r}function pt(t,e,n,r,s){let o=1,i=n+1;for(;i<e.length&&o>0;)e[i]===r?o++:e[i]===s&&o--,o>0&&i++;return o===0?i:-1}function he(t,e,n){let r=n,s=1;for(;r<e.length&&s>0;){let o=e[r];if(o==="\\"&&r+1<e.length){r+=2;continue}if(o==="'"){let i=e.indexOf("'",r+1);if(i!==-1){r=i+1;continue}}if(o==='"'){for(r++;r<e.length&&e[r]!=='"';)e[r]==="\\"&&r+1<e.length?r+=2:r++;r<e.length&&r++;continue}o==="{"?s++:o==="}"&&s--,s>0&&r++}return r}function vr(t,e,n){let r=n,s=!1;for(;r<e.length;){let o=e[r];if(o==="/"&&s||o==="}")break;if(o==="'"){let i=e.indexOf("'",r+1);if(i!==-1){r=i+1,s=!0;continue}}if(o==='"'){for(r++;r<e.length&&e[r]!=='"';)e[r]==="\\"&&r+1<e.length?r+=2:r++;r<e.length&&r++,s=!0;continue}o==="\\"?(r+=2,s=!0):(r++,s=!0)}return r}function Ir(t,e,n){let r=n,s="";for(;r<e.length;){let o=e[r];if(o==="*"||o==="?")s+=o,r++;else if(o==="["){let i=Bn(e,r);i===-1?(s+=o,r++):(s+=e.slice(r,i+1),r=i+1)}else break}return{pattern:s,endIndex:r}}function Bn(t,e){let n=e+1;for(n<t.length&&t[n]==="^"&&n++,n<t.length&&t[n]==="]"&&n++;n<t.length;){let r=t[n];if(r==="\\"&&n+1<t.length){n+=2;continue}if(r==="]")return n;if(r==='"'||r==="$"||r==="`")return-1;if(r==="'"){let s=t.indexOf("'",n+1);if(s!==-1){n=s+1;continue}}if(r==="["&&n+1<t.length&&t[n+1]===":"){let s=t.indexOf(":]",n+2);if(s!==-1){n=s+2;continue}}if(r==="["&&n+1<t.length&&(t[n+1]==="."||t[n+1]==="=")){let o=`${t[n+1]}]`,i=t.indexOf(o,n+2);if(i!==-1){n=i+2;continue}}n++}return-1}function Pr(t,e,n){let r="",s=n;for(;s<e.length&&e[s]!=="'";){let o=e[s];if(o==="\\"&&s+1<e.length)switch(e[s+1]){case"n":r+=`
28
- `,s+=2;break;case"t":r+=" ",s+=2;break;case"r":r+="\r",s+=2;break;case"\\":r+="\\",s+=2;break;case"'":r+="'",s+=2;break;case'"':r+='"',s+=2;break;case"a":r+="\x07",s+=2;break;case"b":r+="\b",s+=2;break;case"e":case"E":r+="\x1B",s+=2;break;case"f":r+="\f",s+=2;break;case"v":r+="\v",s+=2;break;case"x":{let a=e.slice(s+2,s+4),l=parseInt(a,16);Number.isNaN(l)?(r+="\\x",s+=2):(r+=String.fromCharCode(l),s+=4);break}case"u":{let a=e.slice(s+2,s+6),l=parseInt(a,16);Number.isNaN(l)?(r+="\\u",s+=2):(r+=String.fromCharCode(l),s+=6);break}case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":{let a="",l=s+1;for(;l<e.length&&l<s+4&&/[0-7]/.test(e[l]);)a+=e[l],l++;let c=parseInt(a,8);r+=String.fromCharCode(c),s=l;break}default:r+=o,s++}else r+=o,s++}return s<e.length&&e[s]==="'"&&s++,{part:g.literal(r),endIndex:s}}function mt(t,e){let n=e.trim();return n===""?{type:"ArithmeticExpression",expression:{type:"ArithNumber",value:0}}:L(t,n)}function Nr(t){let e=[],n="",r=0;for(let s=0;s<t.length;s++){let o=t[s];o==="{"?(r++,n+=o):o==="}"?(r--,n+=o):o===","&&r===0?(e.push(n),n=""):n+=o}return e.push(n),e}function Rr(t,e,n,r){let s=pt(t,e,n,"{","}");if(s===-1)return null;let o=e.slice(n+1,s),i=o.match(/^(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?$/);if(i)return{part:{type:"BraceExpansion",items:[{type:"Range",start:Number.parseInt(i[1],10),end:Number.parseInt(i[2],10),step:i[3]?Number.parseInt(i[3],10):void 0,startStr:i[1],endStr:i[2]}]},endIndex:s+1};let a=o.match(/^([a-zA-Z])\.\.([a-zA-Z])(?:\.\.(-?\d+))?$/);return a?{part:{type:"BraceExpansion",items:[{type:"Range",start:a[1],end:a[2],step:a[3]?Number.parseInt(a[3],10):void 0}]},endIndex:s+1}:o.includes(",")&&r?{part:{type:"BraceExpansion",items:Nr(o).map(u=>({type:"Word",word:g.word(r(t,u,!1,!1,!1))}))},endIndex:s+1}:o.includes(",")?{part:{type:"BraceExpansion",items:Nr(o).map(u=>({type:"Word",word:g.word([g.literal(u)])}))},endIndex:s+1}:null}function kr(t,e){let n="";for(let r of e.parts)switch(r.type){case"Literal":case"SingleQuoted":case"Escaped":n+=r.value;break;case"DoubleQuoted":n+='"';for(let s of r.parts)s.type==="Literal"||s.type==="Escaped"?n+=s.value:s.type==="ParameterExpansion"&&(n+=`\${${s.parameter}}`);n+='"';break;case"ParameterExpansion":n+=`\${${r.parameter}}`;break;case"Glob":n+=r.pattern;break;default:n+=r.type}return n}function Dr(t,e){return{[f.LESS]:"<",[f.GREAT]:">",[f.DGREAT]:">>",[f.LESSAND]:"<&",[f.GREATAND]:">&",[f.LESSGREAT]:"<>",[f.CLOBBER]:">|",[f.TLESS]:"<<<",[f.AND_GREAT]:"&>",[f.AND_DGREAT]:"&>>",[f.DLESS]:"<",[f.DLESSDASH]:"<"}[e]||">"}function Fe(t){let e=t.current(),n=e.type;if(n===f.NUMBER){let r=t.peek(1);return e.end!==r.start?!1:Cr.has(r.type)}return br.has(n)}function Be(t){let e=null;t.check(f.NUMBER)&&(e=Number.parseInt(t.advance().value,10));let n=t.advance(),r=Dr(t,n.type);if(n.type===f.DLESS||n.type===f.DLESSDASH)return zn(t,r,e,n.type===f.DLESSDASH);t.isWord()||t.error("Expected redirection target");let s=t.parseWord();return g.redirection(r,s,e)}function zn(t,e,n,r){t.isWord()||t.error("Expected here-document delimiter");let s=t.advance(),o=s.value,i=s.quoted||!1;(o.startsWith("'")&&o.endsWith("'")||o.startsWith('"')&&o.endsWith('"'))&&(o=o.slice(1,-1));let a=g.redirection(r?"<<-":"<<",g.hereDoc(o,g.word([]),r,i),n);return t.addPendingHeredoc(a,o,r,i),a}function Or(t){let e=[],n=null,r=[],s=[];for(;t.check(f.ASSIGNMENT_WORD);)t.checkIterationLimit(),e.push(Un(t));for(;Fe(t);)t.checkIterationLimit(),s.push(Be(t));for(t.isWord()&&(n=t.parseWord());(!t.isStatementEnd()||t.check(f.RBRACE))&&!t.check(f.PIPE,f.PIPE_AMP);)if(t.checkIterationLimit(),Fe(t))s.push(Be(t));else if(t.check(f.RBRACE)){let o=t.advance();r.push(t.parseWordFromString(o.value,!1,!1))}else if(t.isWord())r.push(t.parseWord());else if(t.check(f.ASSIGNMENT_WORD)){let o=t.advance(),i=o.value,a=i.endsWith("="),l=i.endsWith("=(");if((a||l)&&(l||t.check(f.LPAREN))){let c=l?i.slice(0,-2):i.slice(0,-1);l||t.expect(f.LPAREN);let u=yt(t);t.expect(f.RPAREN);let d=u.map(p=>kr(t,p)),h=`${c}=(${d.join(" ")})`;r.push(t.parseWordFromString(h,!1,!1))}else r.push(t.parseWordFromString(i,o.quoted,o.singleQuoted))}else if(t.check(f.LPAREN))t.error("syntax error near unexpected token `('");else break;return g.simpleCommand(n,r,e,s)}function Un(t){let e=t.expect(f.ASSIGNMENT_WORD),n=e.value,r=n.match(/^[a-zA-Z_][a-zA-Z0-9_]*/);r||t.error(`Invalid assignment: ${n}`);let s=r[0],o,i=s.length;if(n[i]==="["){let d=0,h=i+1;for(;i<n.length;i++)if(n[i]==="[")d++;else if(n[i]==="]"&&(d--,d===0))break;d!==0&&t.error(`Invalid assignment: ${n}`),o=n.slice(h,i),i++}let a=n[i]==="+";a&&i++,n[i]!=="="&&t.error(`Invalid assignment: ${n}`),i++;let l=n.slice(i);if(l==="("){let d=yt(t);t.expect(f.RPAREN);let h=o!==void 0?`${s}[${o}]`:s;return g.assignment(h,null,a,d)}if(l===""&&t.check(f.LPAREN)){let d=t.current();if(e.end===d.start){t.advance();let h=yt(t);t.expect(f.RPAREN);let p=o!==void 0?`${s}[${o}]`:s;return g.assignment(p,null,a,h)}}let c=l?t.parseWordFromString(l,e.quoted,e.singleQuoted,!0):null,u=o!==void 0?`${s}[${o}]`:s;return g.assignment(u,c,a,null)}function yt(t){let e=[];for(t.skipNewlines();!t.check(f.RPAREN,f.EOF);)t.checkIterationLimit(),t.isWord()?e.push(t.parseWord()):t.advance(),t.skipNewlines();return e}function Et(t){t.expect(f.IF);let e=[],n=t.parseCompoundList();t.expect(f.THEN);let r=t.parseCompoundList();if(r.length===0){let i=t.check(f.FI)?"fi":t.check(f.ELSE)?"else":t.check(f.ELIF)?"elif":"fi";t.error(`syntax error near unexpected token \`${i}'`)}for(e.push({condition:n,body:r});t.check(f.ELIF);){t.advance();let i=t.parseCompoundList();t.expect(f.THEN);let a=t.parseCompoundList();if(a.length===0){let l=t.check(f.FI)?"fi":t.check(f.ELSE)?"else":t.check(f.ELIF)?"elif":"fi";t.error(`syntax error near unexpected token \`${l}'`)}e.push({condition:i,body:a})}let s=null;t.check(f.ELSE)&&(t.advance(),s=t.parseCompoundList(),s.length===0&&t.error("syntax error near unexpected token `fi'")),t.expect(f.FI);let o=t.parseOptionalRedirections();return g.ifNode(e,s,o)}function wt(t){if(t.expect(f.FOR),t.check(f.DPAREN_START))return Hn(t);t.isWord()||t.error("Expected variable name in for loop");let n=t.advance().value,r=null;if(t.skipNewlines(),t.check(f.IN))for(t.advance(),r=[];!t.check(f.SEMICOLON,f.NEWLINE,f.DO,f.EOF)&&t.isWord();)r.push(t.parseWord());t.check(f.SEMICOLON)&&t.advance(),t.skipNewlines(),t.expect(f.DO);let s=t.parseCompoundList();t.expect(f.DONE);let o=t.parseOptionalRedirections();return g.forNode(n,r,s,o)}function Hn(t){t.expect(f.DPAREN_START);let e=null,n=null,r=null,s=["","",""],o=0,i=0;for(;!t.check(f.DPAREN_END,f.EOF);){let c=t.advance();if(c.type===f.SEMICOLON&&i===0){if(o++,o>2)break}else c.value==="("&&i++,c.value===")"&&i--,s[o]+=c.value}t.expect(f.DPAREN_END),s[0].trim()&&(e=L(t,s[0].trim())),s[1].trim()&&(n=L(t,s[1].trim())),s[2].trim()&&(r=L(t,s[2].trim())),t.skipNewlines(),t.check(f.SEMICOLON)&&t.advance(),t.skipNewlines(),t.expect(f.DO);let a=t.parseCompoundList();t.expect(f.DONE);let l=t.parseOptionalRedirections();return{type:"CStyleFor",init:e,condition:n,update:r,body:a,redirections:l}}function At(t){t.expect(f.WHILE);let e=t.parseCompoundList();t.expect(f.DO);let n=t.parseCompoundList();n.length===0&&t.error("syntax error near unexpected token `done'"),t.expect(f.DONE);let r=t.parseOptionalRedirections();return g.whileNode(e,n,r)}function gt(t){t.expect(f.UNTIL);let e=t.parseCompoundList();t.expect(f.DO);let n=t.parseCompoundList();n.length===0&&t.error("syntax error near unexpected token `done'"),t.expect(f.DONE);let r=t.parseOptionalRedirections();return g.untilNode(e,n,r)}function xt(t){t.expect(f.CASE),t.isWord()||t.error("Expected word after 'case'");let e=t.parseWord();t.skipNewlines(),t.expect(f.IN),t.skipNewlines();let n=[];for(;!t.check(f.ESAC,f.EOF);){t.checkIterationLimit();let s=t.getPos(),o=Vn(t);if(o&&n.push(o),t.skipNewlines(),t.getPos()===s&&!o)break}t.expect(f.ESAC);let r=t.parseOptionalRedirections();return g.caseNode(e,n,r)}function Vn(t){t.check(f.LPAREN)&&t.advance();let e=[];for(;t.isWord()&&(e.push(t.parseWord()),t.check(f.PIPE));)t.advance();if(e.length===0)return null;t.expect(f.RPAREN),t.skipNewlines();let n=[];for(;!t.check(f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND,f.ESAC,f.EOF);){t.checkIterationLimit(),t.isWord()&&t.peek(1).type===f.RPAREN&&t.error("syntax error near unexpected token `)'"),t.check(f.LPAREN)&&t.peek(1).type===f.WORD&&t.error(`syntax error near unexpected token \`${t.peek(1).value}'`);let s=t.getPos(),o=t.parseStatement();if(o&&n.push(o),t.skipSeparators(!1),t.getPos()===s&&!o)break}let r=";;";return t.check(f.DSEMI)?(t.advance(),r=";;"):t.check(f.SEMI_AND)?(t.advance(),r=";&"):t.check(f.SEMI_SEMI_AND)&&(t.advance(),r=";;&"),g.caseItem(e,n,r)}function St(t){t.peek(1).type,f.LPAREN,t.expect(f.LPAREN),t.check(f.LPAREN)&&t.advance();let e=t.parseCompoundList();t.expect(f.RPAREN);let n=t.parseOptionalRedirections();return g.subshell(e,n)}function bt(t){t.expect(f.LBRACE);let e=t.parseCompoundList();t.expect(f.RBRACE);let n=t.parseOptionalRedirections();return g.group(e,n)}var jn=["-a","-b","-c","-d","-e","-f","-g","-h","-k","-p","-r","-s","-t","-u","-w","-x","-G","-L","-N","-O","-S","-z","-n","-o","-v","-R"],qn=["==","!=","=~","<",">","-eq","-ne","-lt","-le","-gt","-ge","-nt","-ot","-ef"];function Nt(t){return t.skipNewlines(),Qn(t)}function Qn(t){let e=Lr(t);for(t.skipNewlines();t.check(f.OR_OR);){t.advance(),t.skipNewlines();let n=Lr(t);e={type:"CondOr",left:e,right:n},t.skipNewlines()}return e}function Lr(t){let e=Ct(t);for(t.skipNewlines();t.check(f.AND_AND);){t.advance(),t.skipNewlines();let n=Ct(t);e={type:"CondAnd",left:e,right:n},t.skipNewlines()}return e}function Ct(t){return t.skipNewlines(),t.check(f.BANG)?(t.advance(),t.skipNewlines(),{type:"CondNot",operand:Ct(t)}):Kn(t)}function Kn(t){if(t.check(f.LPAREN)){t.advance();let e=Nt(t);return t.expect(f.RPAREN),{type:"CondGroup",expression:e}}if(t.isWord()){let e=t.current(),n=e.value;if(jn.includes(n)&&!e.quoted&&(t.advance(),t.check(f.DBRACK_END)&&t.error(`Expected operand after ${n}`),t.isWord())){let s=t.parseWord();return{type:"CondUnary",operator:n,operand:s}}let r=t.parseWord();if(t.isWord()&&qn.includes(t.current().value)){let s=t.advance().value,o=t.parseWord();return{type:"CondBinary",operator:s,left:r,right:o}}if(t.check(f.LESS)){t.advance();let s=t.parseWord();return{type:"CondBinary",operator:"<",left:r,right:s}}if(t.check(f.GREAT)){t.advance();let s=t.parseWord();return{type:"CondBinary",operator:">",left:r,right:s}}if(t.isWord()&&t.current().value==="="){t.advance();let s=t.parseWord();return{type:"CondBinary",operator:"==",left:r,right:s}}return{type:"CondWord",word:r}}t.error("Expected conditional expression")}function Yn(t,e,n){let r=n+1,s=e[r];if("@*#?$!-0123456789".includes(s))return{part:g.parameterExpansion(s),endIndex:r+1};let o="";for(;r<e.length&&/[a-zA-Z0-9_]/.test(e[r]);)o+=e[r],r++;return{part:g.parameterExpansion(o),endIndex:r}}function Jn(t,e,n,r=!1){let s=n+2,o=!1;e[s]==="!"&&(o=!0,s++);let i=!1;e[s]==="#"&&!/[}:#%/^,]/.test(e[s+1]||"}")&&(i=!0,s++);let a="",l=e[s];if(/[@*#?$!-]/.test(l)&&!/[a-zA-Z0-9_]/.test(e[s+1]||""))a=l,s++;else for(;s<e.length&&/[a-zA-Z0-9_]/.test(e[s]);)a+=e[s],s++;if(e[s]==="["){let u=pt(t,e,s,"[","]");a+=e.slice(s,u+1),s=u+1}a===""&&!o&&!i&&e[s]!=="}"&&t.error(`\${${e[s]}}: bad substitution`);let c=null;if(o){let u=a.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[([@*])\]$/);if(u)c={type:"ArrayKeys",array:u[1],star:u[2]==="*"},a="";else if(e[s]==="*"||e[s]==="@"){let d=e[s];s++,c={type:"VarNamePrefix",prefix:a,star:d==="*"},a=""}else c={type:"Indirection"}}else if(i)if(e[s]===":")for(c={type:"LengthSliceError"};s<e.length&&e[s]!=="}";)s++;else e[s]!=="}"&&/[-+=?]/.test(e[s])?t.error(`\${#${a}${e.slice(s,e.indexOf("}",s))}}: bad substitution`):c={type:"Length"};if(!c&&s<e.length&&e[s]!=="}"){let u=es(t,e,s,a,r);c=u.operation,s=u.endIndex}if(s<e.length&&e[s]!=="}"){let u=e[s];if(!/[:\-+=?#%/^,@[]/.test(u)){let d=s;for(;d<e.length&&e[d]!=="}";)d++;let h=e.slice(n,d+1);t.error(`\${${h.slice(2,-1)}}: bad substitution`)}}for(;s<e.length&&e[s]!=="}";)s++;return{part:g.parameterExpansion(a,c),endIndex:s+1}}function es(t,e,n,r,s=!1){let o=n,i=e[o],a=e[o+1]||"";if(i===":"){let l=a;if("-=?+".includes(l)){o+=2;let E=he(t,e,o),w=e.slice(o,E),b=ie(t,w,!1,!1,!0,!1,s),I=g.word(b.length>0?b:[g.literal("")]);if(l==="-")return{operation:{type:"DefaultValue",word:I,checkEmpty:!0},endIndex:E};if(l==="=")return{operation:{type:"AssignDefault",word:I,checkEmpty:!0},endIndex:E};if(l==="?")return{operation:{type:"ErrorIfUnset",word:I,checkEmpty:!0},endIndex:E};if(l==="+")return{operation:{type:"UseAlternative",word:I,checkEmpty:!0},endIndex:E}}o++;let c=he(t,e,o),u=e.slice(o,c),d=-1,h=0,p=0;for(let m=0;m<u.length;m++){let E=u[m];if(E==="("||E==="[")h++;else if(E===")"||E==="]")h--;else if(E==="?"&&h===0)p++;else if(E===":"&&h===0)if(p>0)p--;else{d=m;break}}let y=d>=0?u.slice(0,d):u,A=d>=0?u.slice(d+1):null;return{operation:{type:"Substring",offset:mt(t,y),length:A?mt(t,A):null},endIndex:c}}if("-=?+".includes(i)){o++;let l=he(t,e,o),c=e.slice(o,l),u=ie(t,c,!1,!1,!0,!1,s),d=g.word(u.length>0?u:[g.literal("")]);if(i==="-")return{operation:{type:"DefaultValue",word:d,checkEmpty:!1},endIndex:l};if(i==="=")return{operation:{type:"AssignDefault",word:d,checkEmpty:!1},endIndex:l};if(i==="?")return{operation:{type:"ErrorIfUnset",word:c?d:null,checkEmpty:!1},endIndex:l};if(i==="+")return{operation:{type:"UseAlternative",word:d,checkEmpty:!1},endIndex:l}}if(i==="#"||i==="%"){let l=a===i,c=i==="#"?"prefix":"suffix";o+=l?2:1;let u=he(t,e,o),d=e.slice(o,u),h=ie(t,d,!1,!1,!1);return{operation:{type:"PatternRemoval",pattern:g.word(h.length>0?h:[g.literal("")]),side:c,greedy:l},endIndex:u}}if(i==="/"){let l=a==="/";o+=l?2:1;let c=null;e[o]==="#"?(c="start",o++):e[o]==="%"&&(c="end",o++);let u=vr(t,e,o),d=e.slice(o,u),h=ie(t,d,!1,!1,!1),p=g.word(h.length>0?h:[g.literal("")]),y=null,A=u;if(e[u]==="/"){let m=u+1,E=he(t,e,m),w=e.slice(m,E),b=ie(t,w,!1,!1,!1);y=g.word(b.length>0?b:[g.literal("")]),A=E}return{operation:{type:"PatternReplacement",pattern:p,replacement:y,all:l,anchor:c},endIndex:A}}if(i==="^"||i===","){let l=a===i,c=i==="^"?"upper":"lower";o+=l?2:1;let u=he(t,e,o),d=e.slice(o,u),h=d?g.word([g.literal(d)]):null;return{operation:{type:"CaseModification",direction:c,all:l,pattern:h},endIndex:u}}return i==="@"&&/[QPaAEK]/.test(a)?{operation:{type:"Transform",operator:a},endIndex:o+2}:{operation:null,endIndex:o}}function $t(t,e,n,r=!1){let s=n+1;if(s>=e.length)return{part:g.literal("$"),endIndex:s};let o=e[s];if(o==="("&&e[s+1]==="(")return t.parseArithmeticExpansion(e,n);if(o==="["){let i=1,a=s+1;for(;a<e.length&&i>0;)e[a]==="["?i++:e[a]==="]"&&i--,i>0&&a++;if(i===0){let l=e.slice(s+1,a),c=L(t,l);return{part:g.arithmeticExpansion(c),endIndex:a+1}}}return o==="("?t.parseCommandSubstitution(e,n):o==="{"?Jn(t,e,n,r):/[a-zA-Z_0-9@*#?$!-]/.test(o)?Yn(t,e,n):{part:g.literal("$"),endIndex:s}}function ts(t,e){let n=[],r=0,s="",o=()=>{s&&(n.push(g.literal(s)),s="")};for(;r<e.length;){let i=e[r];if(i==="\\"&&r+1<e.length){let a=e[r+1];if(a==="$"||a==="`"){s+=a,r+=2;continue}s+=i,r++;continue}if(i==="$"){o();let{part:a,endIndex:l}=$t(t,e,r,!0);a&&n.push(a),r=l;continue}if(i==="`"){o();let{part:a,endIndex:l}=t.parseBacktickSubstitution(e,r,!0);n.push(a),r=l;continue}s+=i,r++}return o(),n}function rs(t,e,n){let r=[],s=n,o="",i=()=>{o&&(r.push(g.literal(o)),o="")};for(;s<e.length&&e[s]!=='"';){let a=e[s];if(a==="\\"&&s+1<e.length){let l=e[s+1];if('"\\$`\n'.includes(l)){o+=l,s+=2;continue}o+=a,s++;continue}if(a==="$"){i();let{part:l,endIndex:c}=$t(t,e,s,!0);l&&r.push(l),s=c;continue}if(a==="`"){i();let{part:l,endIndex:c}=t.parseBacktickSubstitution(e,s,!0);r.push(l),s=c;continue}o+=a,s++}return i(),{part:g.doubleQuoted(r),endIndex:s}}function ie(t,e,n=!1,r=!1,s=!1,o=!1,i=!1){if(r)return[g.singleQuoted(e)];if(n){let d=ts(t,e);return[g.doubleQuoted(d)]}let a=[],l=0,c="",u=()=>{c&&(a.push(g.literal(c)),c="")};for(;l<e.length;){let d=e[l];if(d==="\\"&&l+1<e.length){let h=e[l+1];(o?h==="$"||h==="`"||h==="\\"||h===`
27
+ `||i===";"||i==="&"||i==="|")return null;s++}}return null}};var ht=1e6,pt=1e5,Nr=1e6,$r=new Set([f.LESS,f.GREAT,f.DLESS,f.DGREAT,f.LESSAND,f.GREATAND,f.LESSGREAT,f.DLESSDASH,f.CLOBBER,f.TLESS,f.AND_GREAT,f.AND_DGREAT]),vr=new Set([f.LESS,f.GREAT,f.DLESS,f.DGREAT,f.LESSAND,f.GREATAND,f.LESSGREAT,f.DLESSDASH,f.CLOBBER,f.TLESS]),te=class extends Error{line;column;token;constructor(e,n,r,s=void 0){super(`Parse error at ${n}:${r}: ${e}`),this.line=n,this.column=r,this.token=s,this.name="ParseException"}};function Pr(t,e,n){let r=n+1;for(;r<e.length&&/[a-zA-Z0-9_-]/.test(e[r]);)r++;return r}function mt(t,e,n,r,s){let o=1,i=n+1;for(;i<e.length&&o>0;)e[i]===r?o++:e[i]===s&&o--,o>0&&i++;return o===0?i:-1}function he(t,e,n){let r=n,s=1;for(;r<e.length&&s>0;){let o=e[r];if(o==="\\"&&r+1<e.length){r+=2;continue}if(o==="'"){let i=e.indexOf("'",r+1);if(i!==-1){r=i+1;continue}}if(o==='"'){for(r++;r<e.length&&e[r]!=='"';)e[r]==="\\"&&r+1<e.length?r+=2:r++;r<e.length&&r++;continue}o==="{"?s++:o==="}"&&s--,s>0&&r++}return r}function Rr(t,e,n){let r=n,s=!1;for(;r<e.length;){let o=e[r];if(o==="/"&&s||o==="}")break;if(o==="'"){let i=e.indexOf("'",r+1);if(i!==-1){r=i+1,s=!0;continue}}if(o==='"'){for(r++;r<e.length&&e[r]!=='"';)e[r]==="\\"&&r+1<e.length?r+=2:r++;r<e.length&&r++,s=!0;continue}o==="\\"?(r+=2,s=!0):(r++,s=!0)}return r}function kr(t,e,n){let r=n,s="";for(;r<e.length;){let o=e[r];if(o==="*"||o==="?")s+=o,r++;else if(o==="["){let i=Gn(e,r);i===-1?(s+=o,r++):(s+=e.slice(r,i+1),r=i+1)}else break}return{pattern:s,endIndex:r}}function Gn(t,e){let n=e+1;for(n<t.length&&t[n]==="^"&&n++,n<t.length&&t[n]==="]"&&n++;n<t.length;){let r=t[n];if(r==="\\"&&n+1<t.length){n+=2;continue}if(r==="]")return n;if(r==='"'||r==="$"||r==="`")return-1;if(r==="'"){let s=t.indexOf("'",n+1);if(s!==-1){n=s+1;continue}}if(r==="["&&n+1<t.length&&t[n+1]===":"){let s=t.indexOf(":]",n+2);if(s!==-1){n=s+2;continue}}if(r==="["&&n+1<t.length&&(t[n+1]==="."||t[n+1]==="=")){let o=`${t[n+1]}]`,i=t.indexOf(o,n+2);if(i!==-1){n=i+2;continue}}n++}return-1}function Dr(t,e,n){let r="",s=n;for(;s<e.length&&e[s]!=="'";){let o=e[s];if(o==="\\"&&s+1<e.length)switch(e[s+1]){case"n":r+=`
28
+ `,s+=2;break;case"t":r+=" ",s+=2;break;case"r":r+="\r",s+=2;break;case"\\":r+="\\",s+=2;break;case"'":r+="'",s+=2;break;case'"':r+='"',s+=2;break;case"a":r+="\x07",s+=2;break;case"b":r+="\b",s+=2;break;case"e":case"E":r+="\x1B",s+=2;break;case"f":r+="\f",s+=2;break;case"v":r+="\v",s+=2;break;case"x":{let a=e.slice(s+2,s+4),l=parseInt(a,16);Number.isNaN(l)?(r+="\\x",s+=2):(r+=String.fromCharCode(l),s+=4);break}case"u":{let a=e.slice(s+2,s+6),l=parseInt(a,16);Number.isNaN(l)?(r+="\\u",s+=2):(r+=String.fromCharCode(l),s+=6);break}case"0":case"1":case"2":case"3":case"4":case"5":case"6":case"7":{let a="",l=s+1;for(;l<e.length&&l<s+4&&/[0-7]/.test(e[l]);)a+=e[l],l++;let c=parseInt(a,8);r+=String.fromCharCode(c),s=l;break}default:r+=o,s++}else r+=o,s++}return s<e.length&&e[s]==="'"&&s++,{part:A.literal(r),endIndex:s}}function yt(t,e){let n=e.trim();return n===""?{type:"ArithmeticExpression",expression:{type:"ArithNumber",value:0}}:L(t,n)}function Ir(t){let e=[],n="",r=0;for(let s=0;s<t.length;s++){let o=t[s];o==="{"?(r++,n+=o):o==="}"?(r--,n+=o):o===","&&r===0?(e.push(n),n=""):n+=o}return e.push(n),e}function _r(t,e,n,r){let s=mt(t,e,n,"{","}");if(s===-1)return null;let o=e.slice(n+1,s),i=o.match(/^(-?\d+)\.\.(-?\d+)(?:\.\.(-?\d+))?$/);if(i)return{part:{type:"BraceExpansion",items:[{type:"Range",start:Number.parseInt(i[1],10),end:Number.parseInt(i[2],10),step:i[3]?Number.parseInt(i[3],10):void 0,startStr:i[1],endStr:i[2]}]},endIndex:s+1};let a=o.match(/^([a-zA-Z])\.\.([a-zA-Z])(?:\.\.(-?\d+))?$/);return a?{part:{type:"BraceExpansion",items:[{type:"Range",start:a[1],end:a[2],step:a[3]?Number.parseInt(a[3],10):void 0}]},endIndex:s+1}:o.includes(",")&&r?{part:{type:"BraceExpansion",items:Ir(o).map(u=>({type:"Word",word:A.word(r(t,u,!1,!1,!1))}))},endIndex:s+1}:o.includes(",")?{part:{type:"BraceExpansion",items:Ir(o).map(u=>({type:"Word",word:A.word([A.literal(u)])}))},endIndex:s+1}:null}function Or(t,e){let n="";for(let r of e.parts)switch(r.type){case"Literal":case"SingleQuoted":case"Escaped":n+=r.value;break;case"DoubleQuoted":n+='"';for(let s of r.parts)s.type==="Literal"||s.type==="Escaped"?n+=s.value:s.type==="ParameterExpansion"&&(n+=`\${${s.parameter}}`);n+='"';break;case"ParameterExpansion":n+=`\${${r.parameter}}`;break;case"Glob":n+=r.pattern;break;default:n+=r.type}return n}function Lr(t,e){return{[f.LESS]:"<",[f.GREAT]:">",[f.DGREAT]:">>",[f.LESSAND]:"<&",[f.GREATAND]:">&",[f.LESSGREAT]:"<>",[f.CLOBBER]:">|",[f.TLESS]:"<<<",[f.AND_GREAT]:"&>",[f.AND_DGREAT]:"&>>",[f.DLESS]:"<",[f.DLESSDASH]:"<"}[e]||">"}function Be(t){let e=t.current(),n=e.type;if(n===f.NUMBER){let r=t.peek(1);return e.end!==r.start?!1:vr.has(r.type)}return $r.has(n)}function ze(t){let e=null;t.check(f.NUMBER)&&(e=Number.parseInt(t.advance().value,10));let n=t.advance(),r=Lr(t,n.type);if(n.type===f.DLESS||n.type===f.DLESSDASH)return Hn(t,r,e,n.type===f.DLESSDASH);t.isWord()||t.error("Expected redirection target");let s=t.parseWord();return A.redirection(r,s,e)}function Hn(t,e,n,r){t.isWord()||t.error("Expected here-document delimiter");let s=t.advance(),o=s.value,i=s.quoted||!1;(o.startsWith("'")&&o.endsWith("'")||o.startsWith('"')&&o.endsWith('"'))&&(o=o.slice(1,-1));let a=A.redirection(r?"<<-":"<<",A.hereDoc(o,A.word([]),r,i),n);return t.addPendingHeredoc(a,o,r,i),a}function Wr(t){let e=[],n=null,r=[],s=[];for(;t.check(f.ASSIGNMENT_WORD);)t.checkIterationLimit(),e.push(Vn(t));for(;Be(t);)t.checkIterationLimit(),s.push(ze(t));for(t.isWord()&&(n=t.parseWord());(!t.isStatementEnd()||t.check(f.RBRACE))&&!t.check(f.PIPE,f.PIPE_AMP);)if(t.checkIterationLimit(),Be(t))s.push(ze(t));else if(t.check(f.RBRACE)){let o=t.advance();r.push(t.parseWordFromString(o.value,!1,!1))}else if(t.isWord())r.push(t.parseWord());else if(t.check(f.ASSIGNMENT_WORD)){let o=t.advance(),i=o.value,a=i.endsWith("="),l=i.endsWith("=(");if((a||l)&&(l||t.check(f.LPAREN))){let c=l?i.slice(0,-2):i.slice(0,-1);l||t.expect(f.LPAREN);let u=Et(t);t.expect(f.RPAREN);let d=u.map(p=>Or(t,p)),h=`${c}=(${d.join(" ")})`;r.push(t.parseWordFromString(h,!1,!1))}else r.push(t.parseWordFromString(i,o.quoted,o.singleQuoted))}else if(t.check(f.LPAREN))t.error("syntax error near unexpected token `('");else break;return A.simpleCommand(n,r,e,s)}function Vn(t){let e=t.expect(f.ASSIGNMENT_WORD),n=e.value,r=n.match(/^[a-zA-Z_][a-zA-Z0-9_]*/);r||t.error(`Invalid assignment: ${n}`);let s=r[0],o,i=s.length;if(n[i]==="["){let d=0,h=i+1;for(;i<n.length;i++)if(n[i]==="[")d++;else if(n[i]==="]"&&(d--,d===0))break;d!==0&&t.error(`Invalid assignment: ${n}`),o=n.slice(h,i),i++}let a=n[i]==="+";a&&i++,n[i]!=="="&&t.error(`Invalid assignment: ${n}`),i++;let l=n.slice(i);if(l==="("){let d=Et(t);t.expect(f.RPAREN);let h=o!==void 0?`${s}[${o}]`:s;return A.assignment(h,null,a,d)}if(l===""&&t.check(f.LPAREN)){let d=t.current();if(e.end===d.start){t.advance();let h=Et(t);t.expect(f.RPAREN);let p=o!==void 0?`${s}[${o}]`:s;return A.assignment(p,null,a,h)}}let c=l?t.parseWordFromString(l,e.quoted,e.singleQuoted,!0):null,u=o!==void 0?`${s}[${o}]`:s;return A.assignment(u,c,a,null)}function Et(t){let e=[];for(t.skipNewlines();!t.check(f.RPAREN,f.EOF);)t.checkIterationLimit(),t.isWord()?e.push(t.parseWord()):t.advance(),t.skipNewlines();return e}function wt(t){t.expect(f.IF);let e=[],n=t.parseCompoundList();t.expect(f.THEN);let r=t.parseCompoundList();if(r.length===0){let i=t.check(f.FI)?"fi":t.check(f.ELSE)?"else":t.check(f.ELIF)?"elif":"fi";t.error(`syntax error near unexpected token \`${i}'`)}for(e.push({condition:n,body:r});t.check(f.ELIF);){t.advance();let i=t.parseCompoundList();t.expect(f.THEN);let a=t.parseCompoundList();if(a.length===0){let l=t.check(f.FI)?"fi":t.check(f.ELSE)?"else":t.check(f.ELIF)?"elif":"fi";t.error(`syntax error near unexpected token \`${l}'`)}e.push({condition:i,body:a})}let s=null;t.check(f.ELSE)&&(t.advance(),s=t.parseCompoundList(),s.length===0&&t.error("syntax error near unexpected token `fi'")),t.expect(f.FI);let o=t.parseOptionalRedirections();return A.ifNode(e,s,o)}function gt(t){if(t.expect(f.FOR),t.check(f.DPAREN_START))return jn(t);t.isWord()||t.error("Expected variable name in for loop");let n=t.advance().value,r=null;if(t.skipNewlines(),t.check(f.IN))for(t.advance(),r=[];!t.check(f.SEMICOLON,f.NEWLINE,f.DO,f.EOF)&&t.isWord();)r.push(t.parseWord());t.check(f.SEMICOLON)&&t.advance(),t.skipNewlines(),t.expect(f.DO);let s=t.parseCompoundList();t.expect(f.DONE);let o=t.parseOptionalRedirections();return A.forNode(n,r,s,o)}function jn(t){t.expect(f.DPAREN_START);let e=null,n=null,r=null,s=["","",""],o=0,i=0;for(;!t.check(f.DPAREN_END,f.EOF);){let c=t.advance();if(c.type===f.SEMICOLON&&i===0){if(o++,o>2)break}else c.value==="("&&i++,c.value===")"&&i--,s[o]+=c.value}t.expect(f.DPAREN_END),s[0].trim()&&(e=L(t,s[0].trim())),s[1].trim()&&(n=L(t,s[1].trim())),s[2].trim()&&(r=L(t,s[2].trim())),t.skipNewlines(),t.check(f.SEMICOLON)&&t.advance(),t.skipNewlines(),t.expect(f.DO);let a=t.parseCompoundList();t.expect(f.DONE);let l=t.parseOptionalRedirections();return{type:"CStyleFor",init:e,condition:n,update:r,body:a,redirections:l}}function At(t){t.expect(f.WHILE);let e=t.parseCompoundList();t.expect(f.DO);let n=t.parseCompoundList();n.length===0&&t.error("syntax error near unexpected token `done'"),t.expect(f.DONE);let r=t.parseOptionalRedirections();return A.whileNode(e,n,r)}function xt(t){t.expect(f.UNTIL);let e=t.parseCompoundList();t.expect(f.DO);let n=t.parseCompoundList();n.length===0&&t.error("syntax error near unexpected token `done'"),t.expect(f.DONE);let r=t.parseOptionalRedirections();return A.untilNode(e,n,r)}function St(t){t.expect(f.CASE),t.isWord()||t.error("Expected word after 'case'");let e=t.parseWord();t.skipNewlines(),t.expect(f.IN),t.skipNewlines();let n=[];for(;!t.check(f.ESAC,f.EOF);){t.checkIterationLimit();let s=t.getPos(),o=qn(t);if(o&&n.push(o),t.skipNewlines(),t.getPos()===s&&!o)break}t.expect(f.ESAC);let r=t.parseOptionalRedirections();return A.caseNode(e,n,r)}function qn(t){t.check(f.LPAREN)&&t.advance();let e=[];for(;t.isWord()&&(e.push(t.parseWord()),t.check(f.PIPE));)t.advance();if(e.length===0)return null;t.expect(f.RPAREN),t.skipNewlines();let n=[];for(;!t.check(f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND,f.ESAC,f.EOF);){t.checkIterationLimit(),t.isWord()&&t.peek(1).type===f.RPAREN&&t.error("syntax error near unexpected token `)'"),t.check(f.LPAREN)&&t.peek(1).type===f.WORD&&t.error(`syntax error near unexpected token \`${t.peek(1).value}'`);let s=t.getPos(),o=t.parseStatement();if(o&&n.push(o),t.skipSeparators(!1),t.getPos()===s&&!o)break}let r=";;";return t.check(f.DSEMI)?(t.advance(),r=";;"):t.check(f.SEMI_AND)?(t.advance(),r=";&"):t.check(f.SEMI_SEMI_AND)&&(t.advance(),r=";;&"),A.caseItem(e,n,r)}function bt(t){t.peek(1).type,f.LPAREN,t.expect(f.LPAREN),t.check(f.LPAREN)&&t.advance();let e=t.parseCompoundList();t.expect(f.RPAREN);let n=t.parseOptionalRedirections();return A.subshell(e,n)}function Ct(t){t.expect(f.LBRACE);let e=t.parseCompoundList();t.expect(f.RBRACE);let n=t.parseOptionalRedirections();return A.group(e,n)}var Kn=["-a","-b","-c","-d","-e","-f","-g","-h","-k","-p","-r","-s","-t","-u","-w","-x","-G","-L","-N","-O","-S","-z","-n","-o","-v","-R"],Xn=["==","!=","=~","<",">","-eq","-ne","-lt","-le","-gt","-ge","-nt","-ot","-ef"];function $t(t){return t.skipNewlines(),Yn(t)}function Yn(t){let e=Mr(t);for(t.skipNewlines();t.check(f.OR_OR);){t.advance(),t.skipNewlines();let n=Mr(t);e={type:"CondOr",left:e,right:n},t.skipNewlines()}return e}function Mr(t){let e=Nt(t);for(t.skipNewlines();t.check(f.AND_AND);){t.advance(),t.skipNewlines();let n=Nt(t);e={type:"CondAnd",left:e,right:n},t.skipNewlines()}return e}function Nt(t){return t.skipNewlines(),t.check(f.BANG)?(t.advance(),t.skipNewlines(),{type:"CondNot",operand:Nt(t)}):Jn(t)}function Jn(t){if(t.check(f.LPAREN)){t.advance();let e=$t(t);return t.expect(f.RPAREN),{type:"CondGroup",expression:e}}if(t.isWord()){let e=t.current(),n=e.value;if(Kn.includes(n)&&!e.quoted&&(t.advance(),t.check(f.DBRACK_END)&&t.error(`Expected operand after ${n}`),t.isWord())){let s=t.parseWord();return{type:"CondUnary",operator:n,operand:s}}let r=t.parseWord();if(t.isWord()&&Xn.includes(t.current().value)){let s=t.advance().value,o=t.parseWord();return{type:"CondBinary",operator:s,left:r,right:o}}if(t.check(f.LESS)){t.advance();let s=t.parseWord();return{type:"CondBinary",operator:"<",left:r,right:s}}if(t.check(f.GREAT)){t.advance();let s=t.parseWord();return{type:"CondBinary",operator:">",left:r,right:s}}if(t.isWord()&&t.current().value==="="){t.advance();let s=t.parseWord();return{type:"CondBinary",operator:"==",left:r,right:s}}return{type:"CondWord",word:r}}t.error("Expected conditional expression")}function ts(t,e,n){let r=n+1,s=e[r];if("@*#?$!-0123456789".includes(s))return{part:A.parameterExpansion(s),endIndex:r+1};let o="";for(;r<e.length&&/[a-zA-Z0-9_]/.test(e[r]);)o+=e[r],r++;return{part:A.parameterExpansion(o),endIndex:r}}function rs(t,e,n,r=!1){let s=n+2,o=!1;e[s]==="!"&&(o=!0,s++);let i=!1;e[s]==="#"&&!/[}:#%/^,]/.test(e[s+1]||"}")&&(i=!0,s++);let a="",l=e[s];if(/[@*#?$!-]/.test(l)&&!/[a-zA-Z0-9_]/.test(e[s+1]||""))a=l,s++;else for(;s<e.length&&/[a-zA-Z0-9_]/.test(e[s]);)a+=e[s],s++;if(e[s]==="["){let u=mt(t,e,s,"[","]");a+=e.slice(s,u+1),s=u+1}a===""&&!o&&!i&&e[s]!=="}"&&t.error(`\${${e[s]}}: bad substitution`);let c=null;if(o){let u=a.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[([@*])\]$/);if(u)c={type:"ArrayKeys",array:u[1],star:u[2]==="*"},a="";else if(e[s]==="*"||e[s]==="@"){let d=e[s];s++,c={type:"VarNamePrefix",prefix:a,star:d==="*"},a=""}else c={type:"Indirection"}}else if(i)if(e[s]===":")for(c={type:"LengthSliceError"};s<e.length&&e[s]!=="}";)s++;else e[s]!=="}"&&/[-+=?]/.test(e[s])?t.error(`\${#${a}${e.slice(s,e.indexOf("}",s))}}: bad substitution`):c={type:"Length"};if(!c&&s<e.length&&e[s]!=="}"){let u=ns(t,e,s,a,r);c=u.operation,s=u.endIndex}if(s<e.length&&e[s]!=="}"){let u=e[s];if(!/[:\-+=?#%/^,@[]/.test(u)){let d=s;for(;d<e.length&&e[d]!=="}";)d++;let h=e.slice(n,d+1);t.error(`\${${h.slice(2,-1)}}: bad substitution`)}}for(;s<e.length&&e[s]!=="}";)s++;return{part:A.parameterExpansion(a,c),endIndex:s+1}}function ns(t,e,n,r,s=!1){let o=n,i=e[o],a=e[o+1]||"";if(i===":"){let l=a;if("-=?+".includes(l)){o+=2;let E=he(t,e,o),w=e.slice(o,E),b=ie(t,w,!1,!1,!0,!1,s),I=A.word(b.length>0?b:[A.literal("")]);if(l==="-")return{operation:{type:"DefaultValue",word:I,checkEmpty:!0},endIndex:E};if(l==="=")return{operation:{type:"AssignDefault",word:I,checkEmpty:!0},endIndex:E};if(l==="?")return{operation:{type:"ErrorIfUnset",word:I,checkEmpty:!0},endIndex:E};if(l==="+")return{operation:{type:"UseAlternative",word:I,checkEmpty:!0},endIndex:E}}o++;let c=he(t,e,o),u=e.slice(o,c),d=-1,h=0,p=0;for(let m=0;m<u.length;m++){let E=u[m];if(E==="("||E==="[")h++;else if(E===")"||E==="]")h--;else if(E==="?"&&h===0)p++;else if(E===":"&&h===0)if(p>0)p--;else{d=m;break}}let y=d>=0?u.slice(0,d):u,g=d>=0?u.slice(d+1):null;return{operation:{type:"Substring",offset:yt(t,y),length:g?yt(t,g):null},endIndex:c}}if("-=?+".includes(i)){o++;let l=he(t,e,o),c=e.slice(o,l),u=ie(t,c,!1,!1,!0,!1,s),d=A.word(u.length>0?u:[A.literal("")]);if(i==="-")return{operation:{type:"DefaultValue",word:d,checkEmpty:!1},endIndex:l};if(i==="=")return{operation:{type:"AssignDefault",word:d,checkEmpty:!1},endIndex:l};if(i==="?")return{operation:{type:"ErrorIfUnset",word:c?d:null,checkEmpty:!1},endIndex:l};if(i==="+")return{operation:{type:"UseAlternative",word:d,checkEmpty:!1},endIndex:l}}if(i==="#"||i==="%"){let l=a===i,c=i==="#"?"prefix":"suffix";o+=l?2:1;let u=he(t,e,o),d=e.slice(o,u),h=ie(t,d,!1,!1,!1);return{operation:{type:"PatternRemoval",pattern:A.word(h.length>0?h:[A.literal("")]),side:c,greedy:l},endIndex:u}}if(i==="/"){let l=a==="/";o+=l?2:1;let c=null;e[o]==="#"?(c="start",o++):e[o]==="%"&&(c="end",o++);let u=Rr(t,e,o),d=e.slice(o,u),h=ie(t,d,!1,!1,!1),p=A.word(h.length>0?h:[A.literal("")]),y=null,g=u;if(e[u]==="/"){let m=u+1,E=he(t,e,m),w=e.slice(m,E),b=ie(t,w,!1,!1,!1);y=A.word(b.length>0?b:[A.literal("")]),g=E}return{operation:{type:"PatternReplacement",pattern:p,replacement:y,all:l,anchor:c},endIndex:g}}if(i==="^"||i===","){let l=a===i,c=i==="^"?"upper":"lower";o+=l?2:1;let u=he(t,e,o),d=e.slice(o,u),h=d?A.word([A.literal(d)]):null;return{operation:{type:"CaseModification",direction:c,all:l,pattern:h},endIndex:u}}return i==="@"&&/[QPaAEK]/.test(a)?{operation:{type:"Transform",operator:a},endIndex:o+2}:{operation:null,endIndex:o}}function vt(t,e,n,r=!1){let s=n+1;if(s>=e.length)return{part:A.literal("$"),endIndex:s};let o=e[s];if(o==="("&&e[s+1]==="(")return t.parseArithmeticExpansion(e,n);if(o==="["){let i=1,a=s+1;for(;a<e.length&&i>0;)e[a]==="["?i++:e[a]==="]"&&i--,i>0&&a++;if(i===0){let l=e.slice(s+1,a),c=L(t,l);return{part:A.arithmeticExpansion(c),endIndex:a+1}}}return o==="("?t.parseCommandSubstitution(e,n):o==="{"?rs(t,e,n,r):/[a-zA-Z_0-9@*#?$!-]/.test(o)?ts(t,e,n):{part:A.literal("$"),endIndex:s}}function ss(t,e){let n=[],r=0,s="",o=()=>{s&&(n.push(A.literal(s)),s="")};for(;r<e.length;){let i=e[r];if(i==="\\"&&r+1<e.length){let a=e[r+1];if(a==="$"||a==="`"){s+=a,r+=2;continue}s+=i,r++;continue}if(i==="$"){o();let{part:a,endIndex:l}=vt(t,e,r,!0);a&&n.push(a),r=l;continue}if(i==="`"){o();let{part:a,endIndex:l}=t.parseBacktickSubstitution(e,r,!0);n.push(a),r=l;continue}s+=i,r++}return o(),n}function is(t,e,n){let r=[],s=n,o="",i=()=>{o&&(r.push(A.literal(o)),o="")};for(;s<e.length&&e[s]!=='"';){let a=e[s];if(a==="\\"&&s+1<e.length){let l=e[s+1];if('"\\$`\n'.includes(l)){o+=l,s+=2;continue}o+=a,s++;continue}if(a==="$"){i();let{part:l,endIndex:c}=vt(t,e,s,!0);l&&r.push(l),s=c;continue}if(a==="`"){i();let{part:l,endIndex:c}=t.parseBacktickSubstitution(e,s,!0);r.push(l),s=c;continue}o+=a,s++}return i(),{part:A.doubleQuoted(r),endIndex:s}}function ie(t,e,n=!1,r=!1,s=!1,o=!1,i=!1){if(r)return[A.singleQuoted(e)];if(n){let d=ss(t,e);return[A.doubleQuoted(d)]}let a=[],l=0,c="",u=()=>{c&&(a.push(A.literal(c)),c="")};for(;l<e.length;){let d=e[l];if(d==="\\"&&l+1<e.length){let h=e[l+1];(o?h==="$"||h==="`"||h==="\\"||h===`
29
29
  `:h==="$"||h==="`"||h==="\\"||h==='"'||h===`
30
- `)?c+=h:c+=`\\${h}`,l+=2;continue}if(d==="'"&&!i){u();let h=e.indexOf("'",l+1);if(h===-1){c+=e.slice(l);break}a.push(g.singleQuoted(e.slice(l+1,h))),l=h+1;continue}if(d==='"'){u();let{part:h,endIndex:p}=rs(t,e,l+1);a.push(h),l=p+1;continue}if(d==="$"&&e[l+1]==="'"){u();let{part:h,endIndex:p}=Pr(t,e,l+2);a.push(h),l=p;continue}if(d==="$"){u();let{part:h,endIndex:p}=$t(t,e,l);h&&a.push(h),l=p;continue}if(d==="`"){u();let{part:h,endIndex:p}=t.parseBacktickSubstitution(e,l);a.push(h),l=p;continue}if(d==="~"){let h=l>0?e[l-1]:"";if(l===0||h==="="||s&&h===":"){let y=$r(t,e,l),A=e[y];if(A===void 0||A==="/"||A===":"){u();let m=e.slice(l+1,y)||null;a.push({type:"TildeExpansion",user:m}),l=y;continue}}}if(d==="*"||d==="?"||d==="["){u();let{pattern:h,endIndex:p}=Ir(t,e,l);a.push({type:"Glob",pattern:h}),l=p;continue}if(d==="{"&&!s){let h=Rr(t,e,l,ie);if(h){u(),a.push(h.part),l=h.endIndex;continue}}c+=d,l++}return u(),a}var M=class t{tokens=[];pos=0;pendingHeredocs=[];parseIterations=0;checkIterationLimit(){if(this.parseIterations++,this.parseIterations>Sr)throw new te("Maximum parse iterations exceeded (possible infinite loop)",this.current().line,this.current().column)}parse(e){if(e.length>dt)throw new te(`Input too large: ${e.length} bytes exceeds limit of ${dt}`,1,1);let n=new Me(e);if(this.tokens=n.tokenize(),this.tokens.length>ht)throw new te(`Too many tokens: ${this.tokens.length} exceeds limit of ${ht}`,1,1);return this.pos=0,this.pendingHeredocs=[],this.parseIterations=0,this.parseScript()}parseTokens(e){return this.tokens=e,this.pos=0,this.pendingHeredocs=[],this.parseScript()}current(){return this.tokens[this.pos]||this.tokens[this.tokens.length-1]}peek(e=0){return this.tokens[this.pos+e]||this.tokens[this.tokens.length-1]}advance(){let e=this.current();return this.pos<this.tokens.length-1&&this.pos++,e}getPos(){return this.pos}check(e,n,r,s,...o){let i=this.tokens[this.pos]?.type;return i===e||n!==void 0&&i===n||r!==void 0&&i===r||s!==void 0&&i===s?!0:o.length>0?o.includes(i):!1}expect(e,n){if(this.check(e))return this.advance();let r=this.current();throw new te(n||`Expected ${e}, got ${r.type}`,r.line,r.column,r)}error(e){let n=this.current();throw new te(e,n.line,n.column,n)}skipNewlines(){for(;this.check(f.NEWLINE,f.COMMENT);)this.check(f.NEWLINE)?(this.advance(),this.processHeredocs()):this.advance()}skipSeparators(e=!0){for(;;){if(this.check(f.NEWLINE)){this.advance(),this.processHeredocs();continue}if(this.check(f.SEMICOLON,f.COMMENT)){this.advance();continue}if(e&&this.check(f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)){this.advance();continue}break}}addPendingHeredoc(e,n,r,s){this.pendingHeredocs.push({redirect:e,delimiter:n,stripTabs:r,quoted:s})}processHeredocs(){for(let e of this.pendingHeredocs)if(this.check(f.HEREDOC_CONTENT)){let n=this.advance(),r;e.quoted?r=g.word([g.literal(n.value)]):r=this.parseWordFromString(n.value,!1,!1,!1,!0),e.redirect.target=g.hereDoc(e.delimiter,r,e.stripTabs,e.quoted)}this.pendingHeredocs=[]}isStatementEnd(){return this.check(f.EOF,f.NEWLINE,f.SEMICOLON,f.AMP,f.AND_AND,f.OR_OR,f.RPAREN,f.RBRACE,f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)}isCommandStart(){let e=this.current().type;return e===f.WORD||e===f.NAME||e===f.NUMBER||e===f.ASSIGNMENT_WORD||e===f.IF||e===f.FOR||e===f.WHILE||e===f.UNTIL||e===f.CASE||e===f.LPAREN||e===f.LBRACE||e===f.DPAREN_START||e===f.DBRACK_START||e===f.FUNCTION||e===f.BANG||e===f.IN}parseScript(){let e=[],r=0;for(this.skipNewlines();!this.check(f.EOF);){r++,r>1e4&&this.error("Parser stuck: too many iterations (>10000)"),this.checkUnexpectedToken();let s=this.pos,o=this.parseStatement();o&&e.push(o),this.skipSeparators(!1),this.check(f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)&&this.error(`syntax error near unexpected token \`${this.current().value}'`),this.pos===s&&!this.check(f.EOF)&&this.advance()}return g.script(e)}checkUnexpectedToken(){let e=this.current().type,n=this.current().value;(e===f.DO||e===f.DONE||e===f.THEN||e===f.ELSE||e===f.ELIF||e===f.FI||e===f.ESAC)&&this.error(`syntax error near unexpected token \`${n}'`),(e===f.RBRACE||e===f.RPAREN)&&this.error(`syntax error near unexpected token \`${n}'`),(e===f.DSEMI||e===f.SEMI_AND||e===f.SEMI_SEMI_AND)&&this.error(`syntax error near unexpected token \`${n}'`),e===f.SEMICOLON&&this.error(`syntax error near unexpected token \`${n}'`)}parseStatement(){if(this.skipNewlines(),!this.isCommandStart())return null;let e=[],n=[],r=!1,s=this.parsePipeline();for(e.push(s);this.check(f.AND_AND,f.OR_OR);){let o=this.advance();n.push(o.type===f.AND_AND?"&&":"||"),this.skipNewlines();let i=this.parsePipeline();e.push(i)}return this.check(f.AMP)&&(this.advance(),r=!0),g.statement(e,n,r)}parsePipeline(){let e=0;for(;this.check(f.BANG);)this.advance(),e++;let n=e%2===1,r=[],s=this.parseCommand();for(r.push(s);this.check(f.PIPE,f.PIPE_AMP);){let o=this.advance();this.skipNewlines();let i=this.parseCommand();o.type===f.PIPE_AMP&&i.type==="SimpleCommand"&&i.redirections.unshift(g.redirection(">&",g.word([g.literal("1")]),2)),r.push(i)}return g.pipeline(r,n)}parseCommand(){return this.check(f.IF)?Et(this):this.check(f.FOR)?wt(this):this.check(f.WHILE)?At(this):this.check(f.UNTIL)?gt(this):this.check(f.CASE)?xt(this):this.check(f.LPAREN)?St(this):this.check(f.LBRACE)?bt(this):this.check(f.DPAREN_START)?this.parseArithmeticCommand():this.check(f.DBRACK_START)?this.parseConditionalCommand():this.check(f.FUNCTION)?this.parseFunctionDef():this.check(f.NAME,f.WORD)&&this.peek(1).type===f.LPAREN&&this.peek(2).type===f.RPAREN?this.parseFunctionDef():Or(this)}isWord(){let e=this.current().type;return e===f.WORD||e===f.NAME||e===f.NUMBER||e===f.IF||e===f.FOR||e===f.WHILE||e===f.UNTIL||e===f.CASE||e===f.FUNCTION||e===f.ELSE||e===f.ELIF||e===f.FI||e===f.THEN||e===f.DO||e===f.DONE||e===f.ESAC||e===f.IN||e===f.BANG}parseWord(){let e=this.advance();return this.parseWordFromString(e.value,e.quoted,e.singleQuoted)}parseWordFromString(e,n=!1,r=!1,s=!1,o=!1){let i=ie(this,e,n,r,s,o);return g.word(i)}parseCommandSubstitution(e,n){let r=n+2,s=1,o=r,i=!1,a=!1,l=0,c=!1,u="";for(;o<e.length&&s>0;){let y=e[o];i?y==="'"&&(i=!1):a?y==="\\"&&o+1<e.length?o++:y==='"'&&(a=!1):y==="'"?(i=!0,u=""):y==='"'?(a=!0,u=""):y==="\\"&&o+1<e.length?(o++,u=""):/[a-zA-Z_]/.test(y)?u+=y:(u==="case"?(l++,c=!1):u==="in"&&l>0?c=!0:u==="esac"&&l>0&&(l--,c=!1),u="",y==="("?o>0&&e[o-1]==="$"?s++:c||s++:y===")"?c?c=!1:s--:y===";"&&l>0&&o+1<e.length&&e[o+1]===";"&&(c=!0)),s>0&&o++}s>0&&this.error("unexpected EOF while looking for matching `)'");let d=e.slice(r,o),p=new t().parse(d);return{part:g.commandSubstitution(p,!1),endIndex:o+1}}parseBacktickSubstitution(e,n,r=!1){let o=n+1,i="";for(;o<e.length&&e[o]!=="`";)if(e[o]==="\\"){let c=e[o+1];c==="$"||c==="`"||c==="\\"||c===`
30
+ `)?c+=h:c+=`\\${h}`,l+=2;continue}if(d==="'"&&!i){u();let h=e.indexOf("'",l+1);if(h===-1){c+=e.slice(l);break}a.push(A.singleQuoted(e.slice(l+1,h))),l=h+1;continue}if(d==='"'){u();let{part:h,endIndex:p}=is(t,e,l+1);a.push(h),l=p+1;continue}if(d==="$"&&e[l+1]==="'"){u();let{part:h,endIndex:p}=Dr(t,e,l+2);a.push(h),l=p;continue}if(d==="$"){u();let{part:h,endIndex:p}=vt(t,e,l);h&&a.push(h),l=p;continue}if(d==="`"){u();let{part:h,endIndex:p}=t.parseBacktickSubstitution(e,l);a.push(h),l=p;continue}if(d==="~"){let h=l>0?e[l-1]:"";if(l===0||h==="="||s&&h===":"){let y=Pr(t,e,l),g=e[y];if(g===void 0||g==="/"||g===":"){u();let m=e.slice(l+1,y)||null;a.push({type:"TildeExpansion",user:m}),l=y;continue}}}if(d==="*"||d==="?"||d==="["){u();let{pattern:h,endIndex:p}=kr(t,e,l);a.push({type:"Glob",pattern:h}),l=p;continue}if(d==="{"&&!s){let h=_r(t,e,l,ie);if(h){u(),a.push(h.part),l=h.endIndex;continue}}c+=d,l++}return u(),a}var M=class t{tokens=[];pos=0;pendingHeredocs=[];parseIterations=0;checkIterationLimit(){if(this.parseIterations++,this.parseIterations>Nr)throw new te("Maximum parse iterations exceeded (possible infinite loop)",this.current().line,this.current().column)}parse(e){if(e.length>ht)throw new te(`Input too large: ${e.length} bytes exceeds limit of ${ht}`,1,1);let n=new Fe(e);if(this.tokens=n.tokenize(),this.tokens.length>pt)throw new te(`Too many tokens: ${this.tokens.length} exceeds limit of ${pt}`,1,1);return this.pos=0,this.pendingHeredocs=[],this.parseIterations=0,this.parseScript()}parseTokens(e){return this.tokens=e,this.pos=0,this.pendingHeredocs=[],this.parseScript()}current(){return this.tokens[this.pos]||this.tokens[this.tokens.length-1]}peek(e=0){return this.tokens[this.pos+e]||this.tokens[this.tokens.length-1]}advance(){let e=this.current();return this.pos<this.tokens.length-1&&this.pos++,e}getPos(){return this.pos}check(e,n,r,s,...o){let i=this.tokens[this.pos]?.type;return i===e||n!==void 0&&i===n||r!==void 0&&i===r||s!==void 0&&i===s?!0:o.length>0?o.includes(i):!1}expect(e,n){if(this.check(e))return this.advance();let r=this.current();throw new te(n||`Expected ${e}, got ${r.type}`,r.line,r.column,r)}error(e){let n=this.current();throw new te(e,n.line,n.column,n)}skipNewlines(){for(;this.check(f.NEWLINE,f.COMMENT);)this.check(f.NEWLINE)?(this.advance(),this.processHeredocs()):this.advance()}skipSeparators(e=!0){for(;;){if(this.check(f.NEWLINE)){this.advance(),this.processHeredocs();continue}if(this.check(f.SEMICOLON,f.COMMENT)){this.advance();continue}if(e&&this.check(f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)){this.advance();continue}break}}addPendingHeredoc(e,n,r,s){this.pendingHeredocs.push({redirect:e,delimiter:n,stripTabs:r,quoted:s})}processHeredocs(){for(let e of this.pendingHeredocs)if(this.check(f.HEREDOC_CONTENT)){let n=this.advance(),r;e.quoted?r=A.word([A.literal(n.value)]):r=this.parseWordFromString(n.value,!1,!1,!1,!0),e.redirect.target=A.hereDoc(e.delimiter,r,e.stripTabs,e.quoted)}this.pendingHeredocs=[]}isStatementEnd(){return this.check(f.EOF,f.NEWLINE,f.SEMICOLON,f.AMP,f.AND_AND,f.OR_OR,f.RPAREN,f.RBRACE,f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)}isCommandStart(){let e=this.current().type;return e===f.WORD||e===f.NAME||e===f.NUMBER||e===f.ASSIGNMENT_WORD||e===f.IF||e===f.FOR||e===f.WHILE||e===f.UNTIL||e===f.CASE||e===f.LPAREN||e===f.LBRACE||e===f.DPAREN_START||e===f.DBRACK_START||e===f.FUNCTION||e===f.BANG||e===f.IN}parseScript(){let e=[],r=0;for(this.skipNewlines();!this.check(f.EOF);){r++,r>1e4&&this.error("Parser stuck: too many iterations (>10000)"),this.checkUnexpectedToken();let s=this.pos,o=this.parseStatement();o&&e.push(o),this.skipSeparators(!1),this.check(f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)&&this.error(`syntax error near unexpected token \`${this.current().value}'`),this.pos===s&&!this.check(f.EOF)&&this.advance()}return A.script(e)}checkUnexpectedToken(){let e=this.current().type,n=this.current().value;(e===f.DO||e===f.DONE||e===f.THEN||e===f.ELSE||e===f.ELIF||e===f.FI||e===f.ESAC)&&this.error(`syntax error near unexpected token \`${n}'`),(e===f.RBRACE||e===f.RPAREN)&&this.error(`syntax error near unexpected token \`${n}'`),(e===f.DSEMI||e===f.SEMI_AND||e===f.SEMI_SEMI_AND)&&this.error(`syntax error near unexpected token \`${n}'`),e===f.SEMICOLON&&this.error(`syntax error near unexpected token \`${n}'`)}parseStatement(){if(this.skipNewlines(),!this.isCommandStart())return null;let e=[],n=[],r=!1,s=this.parsePipeline();for(e.push(s);this.check(f.AND_AND,f.OR_OR);){let o=this.advance();n.push(o.type===f.AND_AND?"&&":"||"),this.skipNewlines();let i=this.parsePipeline();e.push(i)}return this.check(f.AMP)&&(this.advance(),r=!0),A.statement(e,n,r)}parsePipeline(){let e=0;for(;this.check(f.BANG);)this.advance(),e++;let n=e%2===1,r=[],s=this.parseCommand();for(r.push(s);this.check(f.PIPE,f.PIPE_AMP);){let o=this.advance();this.skipNewlines();let i=this.parseCommand();o.type===f.PIPE_AMP&&i.type==="SimpleCommand"&&i.redirections.unshift(A.redirection(">&",A.word([A.literal("1")]),2)),r.push(i)}return A.pipeline(r,n)}parseCommand(){return this.check(f.IF)?wt(this):this.check(f.FOR)?gt(this):this.check(f.WHILE)?At(this):this.check(f.UNTIL)?xt(this):this.check(f.CASE)?St(this):this.check(f.LPAREN)?bt(this):this.check(f.LBRACE)?Ct(this):this.check(f.DPAREN_START)?this.parseArithmeticCommand():this.check(f.DBRACK_START)?this.parseConditionalCommand():this.check(f.FUNCTION)?this.parseFunctionDef():this.check(f.NAME,f.WORD)&&this.peek(1).type===f.LPAREN&&this.peek(2).type===f.RPAREN?this.parseFunctionDef():Wr(this)}isWord(){let e=this.current().type;return e===f.WORD||e===f.NAME||e===f.NUMBER||e===f.IF||e===f.FOR||e===f.WHILE||e===f.UNTIL||e===f.CASE||e===f.FUNCTION||e===f.ELSE||e===f.ELIF||e===f.FI||e===f.THEN||e===f.DO||e===f.DONE||e===f.ESAC||e===f.IN||e===f.BANG}parseWord(){let e=this.advance();return this.parseWordFromString(e.value,e.quoted,e.singleQuoted)}parseWordFromString(e,n=!1,r=!1,s=!1,o=!1){let i=ie(this,e,n,r,s,o);return A.word(i)}parseCommandSubstitution(e,n){let r=n+2,s=1,o=r,i=!1,a=!1,l=0,c=!1,u="";for(;o<e.length&&s>0;){let y=e[o];i?y==="'"&&(i=!1):a?y==="\\"&&o+1<e.length?o++:y==='"'&&(a=!1):y==="'"?(i=!0,u=""):y==='"'?(a=!0,u=""):y==="\\"&&o+1<e.length?(o++,u=""):/[a-zA-Z_]/.test(y)?u+=y:(u==="case"?(l++,c=!1):u==="in"&&l>0?c=!0:u==="esac"&&l>0&&(l--,c=!1),u="",y==="("?o>0&&e[o-1]==="$"?s++:c||s++:y===")"?c?c=!1:s--:y===";"&&l>0&&o+1<e.length&&e[o+1]===";"&&(c=!0)),s>0&&o++}s>0&&this.error("unexpected EOF while looking for matching `)'");let d=e.slice(r,o),p=new t().parse(d);return{part:A.commandSubstitution(p,!1),endIndex:o+1}}parseBacktickSubstitution(e,n,r=!1){let o=n+1,i="";for(;o<e.length&&e[o]!=="`";)if(e[o]==="\\"){let c=e[o+1];c==="$"||c==="`"||c==="\\"||c===`
31
31
  `||r&&c==='"'?(c!==`
32
- `&&(i+=c),o+=2):(i+=e[o],o++)}else i+=e[o],o++;o>=e.length&&this.error("unexpected EOF while looking for matching ``'");let l=new t().parse(i);return{part:g.commandSubstitution(l,!0),endIndex:o+1}}parseArithmeticExpansion(e,n){let r=n+3,s=1,o=0,i=r;for(;i<e.length-1&&s>0;)e[i]==="$"&&e[i+1]==="("?e[i+2]==="("?(s++,i+=3):(o++,i+=2):e[i]==="("&&e[i+1]==="("?(s++,i+=2):e[i]===")"&&e[i+1]===")"?o>0?(o--,i++):(s--,s>0&&(i+=2)):e[i]==="("?(o++,i++):(e[i]===")"&&o>0&&o--,i++);let a=e.slice(r,i),l=this.parseArithmeticExpression(a);return{part:g.arithmeticExpansion(l),endIndex:i+2}}parseArithmeticCommand(){this.expect(f.DPAREN_START);let e="",n=1,r=0,s=!1,o=!1;for(;n>0&&!this.check(f.EOF);){if(s){if(s=!1,r>0){r--,e+=")";continue}if(this.check(f.RPAREN)){n--,o=!0,this.advance();continue}if(this.check(f.DPAREN_END)){n--,o=!0;continue}e+=")";continue}this.check(f.DPAREN_START)?(n++,e+="((",this.advance()):this.check(f.DPAREN_END)?r>=2?(r-=2,e+="))",this.advance()):r===1?(r--,e+=")",s=!0,this.advance()):(n--,o=!0,n>0&&(e+="))"),this.advance()):this.check(f.LPAREN)?(r++,e+="(",this.advance()):this.check(f.RPAREN)?(r>0&&r--,e+=")",this.advance()):(e+=this.current().value,this.advance())}o||this.expect(f.DPAREN_END);let i=this.parseArithmeticExpression(e.trim()),a=this.parseOptionalRedirections();return g.arithmeticCommand(i,a)}parseConditionalCommand(){this.expect(f.DBRACK_START);let e=Nt(this);this.expect(f.DBRACK_END);let n=this.parseOptionalRedirections();return g.conditionalCommand(e,n)}parseFunctionDef(){let e;this.check(f.FUNCTION)?(this.advance(),e=this.expect(f.NAME,"Expected function name").value,this.check(f.LPAREN)&&(this.advance(),this.expect(f.RPAREN))):(e=this.advance().value,this.expect(f.LPAREN),this.expect(f.RPAREN)),this.skipNewlines();let n=this.parseCompoundCommandBody(),r=this.parseOptionalRedirections();return g.functionDef(e,n,r)}parseCompoundCommandBody(){if(this.check(f.LBRACE))return bt(this);if(this.check(f.LPAREN))return St(this);if(this.check(f.IF))return Et(this);if(this.check(f.FOR))return wt(this);if(this.check(f.WHILE))return At(this);if(this.check(f.UNTIL))return gt(this);if(this.check(f.CASE))return xt(this);this.error("Expected compound command for function body")}parseCompoundList(){let e=[];for(this.skipNewlines();!this.check(f.EOF,f.FI,f.ELSE,f.ELIF,f.THEN,f.DO,f.DONE,f.ESAC,f.RPAREN,f.RBRACE,f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)&&this.isCommandStart();){this.checkIterationLimit();let n=this.pos,r=this.parseStatement();if(r&&e.push(r),this.skipSeparators(),this.pos===n&&!r)break}return e}parseOptionalRedirections(){let e=[];for(;Fe(this);){this.checkIterationLimit();let n=this.pos;if(e.push(Be(this)),this.pos===n)break}return e}parseArithmeticExpression(e){return L(this,e)}};function oe(t){return new M().parse(t)}var we=class{fs;cwd;constructor(e,n){this.fs=e,this.cwd=n}isGlobPattern(e){return e.includes("*")||e.includes("?")||/\[.*\]/.test(e)}async expandArgs(e,n){let r=[];for(let s=0;s<e.length;s++){let o=e[s];if((n?.[s]??!1)||!this.isGlobPattern(o))r.push(o);else{let a=await this.expand(o);a.length>0?r.push(...a):r.push(o)}}return r}async expand(e){return e.includes("**")?this.expandRecursive(e):this.expandSimple(e)}async expandSimple(e){let n=[],r=e.lastIndexOf("/"),s,o;r===-1?(s=this.cwd,o=e):(s=e.slice(0,r)||"/",o=e.slice(r+1));let i=this.fs.resolvePath(this.cwd,s);try{let a=await this.fs.readdir(i);for(let l of a)if(this.matchPattern(l,o)){let c=r===-1?l:`${s}/${l}`;n.push(c)}}catch{}return n.sort()}async expandRecursive(e){let n=[],r=e.indexOf("**"),s=e.slice(0,r).replace(/\/$/,"")||".",i=e.slice(r+2).replace(/^\//,"");return await this.walkDirectory(s,i,n),n.sort()}async walkDirectory(e,n,r){let s=this.fs.resolvePath(this.cwd,e);try{let o=await this.fs.readdir(s);for(let i of o){let a=e==="."?i:`${e}/${i}`,l=this.fs.resolvePath(this.cwd,a);try{(await this.fs.stat(l)).isDirectory?await this.walkDirectory(a,n,r):n&&this.matchPattern(i,n)&&r.push(a)}catch{}}}catch{}}matchPattern(e,n){return this.patternToRegex(n).test(e)}patternToRegex(e){let n="^";for(let r=0;r<e.length;r++){let s=e[r];if(s==="*")n+=".*";else if(s==="?")n+=".";else if(s==="["){let o=r+1,i="[";for(o<e.length&&(e[o]==="^"||e[o]==="!")&&(i+="^",o++),o<e.length&&e[o]==="]"&&(i+="\\]",o++);o<e.length&&e[o]!=="]";){if(e[o]==="["&&o+1<e.length&&e[o+1]===":"){let a=e.indexOf(":]",o+2);if(a!==-1){let l=e.slice(o+2,a),c=this.posixClassToRegex(l);i+=c,o=a+2;continue}}if(e[o]==="\\"&&o+1<e.length){i+=`\\${e[o+1]}`,o+=2;continue}e[o]==="-"?i+="\\-":i+=e[o],o++}i+="]",n+=i,r=o}else if(s==="\\"&&r+1<e.length){let o=e[r+1];/[.+^${}()|\\*?[\]]/.test(o)?n+=`\\${o}`:n+=o,r++}else/[.+^${}()|]/.test(s)?n+=`\\${s}`:n+=s}return n+="$",new RegExp(n)}posixClassToRegex(e){return{alnum:"a-zA-Z0-9",alpha:"a-zA-Z",ascii:"\\x00-\\x7F",blank:" \\t",cntrl:"\\x00-\\x1F\\x7F",digit:"0-9",graph:"!-~",lower:"a-z",print:" -~",punct:"!-/:-@\\[-`{-~",space:" \\t\\n\\r\\f\\v",upper:"A-Z",word:"a-zA-Z0-9_",xdigit:"0-9a-fA-F"}[e]||""}};function Z(t){switch(t.type){case"ArithCommandSubst":return!0;case"ArithNested":return Z(t.expression);case"ArithBinary":return Z(t.left)||Z(t.right);case"ArithUnary":return Z(t.operand);case"ArithTernary":return Z(t.condition)||Z(t.consequent)||Z(t.alternate);case"ArithAssignment":return Z(t.value);case"ArithGroup":return Z(t.expression);case"ArithArrayElement":return t.index?Z(t.index):!1;case"ArithConcat":return t.parts.some(Z);default:return!1}}function vt(t){let e=t.operation;return e?!!("word"in e&&e.word&&re(e.word)||e.type==="PatternReplacement"&&(e.pattern&&re(e.pattern)||e.replacement&&re(e.replacement))||e.type==="PatternRemoval"&&e.pattern&&re(e.pattern)):!1}function ze(t){switch(t.type){case"CommandSubstitution":return!0;case"ArithmeticExpansion":return Z(t.expression.expression);case"DoubleQuoted":return t.parts.some(ze);case"BraceExpansion":return t.items.some(e=>e.type==="Word"&&re(e.word));case"ParameterExpansion":return vt(t);default:return!1}}function re(t){return t.parts.some(ze)}function It(t){if(!t.operation)return!1;let e=t.operation,n;if((e.type==="DefaultValue"||e.type==="AssignDefault"||e.type==="UseAlternative"||e.type==="ErrorIfUnset")&&(n=e.word?.parts),!n)return!1;for(let r of n)if(r.type==="DoubleQuoted"||r.type==="SingleQuoted")return!0;return!1}function Tr(t){let e=!1,n=!1,r=!1,s=!1,o=!1;for(let i of t){if((i.type==="SingleQuoted"||i.type==="DoubleQuoted")&&(e=!0,i.type==="DoubleQuoted"))for(let a of i.parts)a.type==="ParameterExpansion"&&a.parameter.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/)&&!a.operation&&(s=!0);i.type==="CommandSubstitution"&&(n=!0),i.type==="ParameterExpansion"&&(o=!0,(i.parameter==="@"||i.parameter==="*")&&(r=!0),It(i)&&(e=!0))}return{hasQuoted:e,hasCommandSub:n,hasArrayVar:r,hasArrayAtExpansion:s,hasParamExpansion:o}}function ss(t,e,n,r,s){let o=n??1;o===0&&(o=1);let i=Math.abs(o),a=[],l=0;r?.match(/^-?0\d/)&&(l=Math.max(l,r.replace(/^-/,"").length)),s?.match(/^-?0\d/)&&(l=Math.max(l,s.replace(/^-/,"").length));let c=u=>{if(l>0){let d=u<0,h=String(Math.abs(u)).padStart(l,"0");return d?`-${h}`:h}return String(u)};if(t<=e)for(let u=t,d=0;u<=e&&d<1e4;u+=i,d++)a.push(c(u));else for(let u=t,d=0;u>=e&&d<1e4;u-=i,d++)a.push(c(u));return a}function is(t,e,n){let r=n??1;r===0&&(r=1);let s=t.charCodeAt(0),o=e.charCodeAt(0),i=Math.abs(r),a=t>="A"&&t<="Z",l=t>="a"&&t<="z",c=e>="A"&&e<="Z",u=e>="a"&&e<="z";if(a&&u||l&&c)return null;let d=[];if(s<=o)for(let h=s,p=0;h<=o&&p<1e4;h+=i,p++)d.push(String.fromCharCode(h));else for(let h=s,p=0;h>=o&&p<1e4;h-=i,p++)d.push(String.fromCharCode(h));return d}function Ce(t,e,n,r,s){let o=n!==void 0?`..${n}`:"";return typeof t=="number"&&typeof e=="number"?{expanded:ss(t,e,n,r,s),literal:`{${t}..${e}${o}}`}:typeof t=="string"&&typeof e=="string"?{expanded:is(t,e,n),literal:`{${t}..${e}${o}}`}:{expanded:null,literal:`{${t}..${e}${o}}`}}function G(t,e){let n="",r=0;for(;r<t.length;){let s=t[r];if(s==="\\")if(r+1<t.length){let o=t[r+1];/[\\^$.|+(){}[\]*?]/.test(o)?n+=`\\${o}`:n+=o,r+=2}else n+="\\\\",r++;else if(s==="*")n+=e?".*":".*?",r++;else if(s==="?")n+=".",r++;else if(s==="["){let o=os(t,r);if(o===-1)n+="\\[",r++;else{let i=t.slice(r+1,o);n+=as(i),r=o+1}}else/[\^$.|+(){}]/.test(s)?(n+=`\\${s}`,r++):(n+=s,r++)}return n}function os(t,e){let n=e+1;for(n<t.length&&t[n]==="^"&&n++,n<t.length&&t[n]==="]"&&n++;n<t.length;){if(t[n]==="\\"&&n+1<t.length){n+=2;continue}if(t[n]==="]")return n;if(t[n]==="'"){let r=t.indexOf("'",n+1);if(r!==-1){n=r+1;continue}}if(t[n]==="["&&n+1<t.length&&t[n+1]===":"){let r=t.indexOf(":]",n+2);if(r!==-1){n=r+2;continue}}n++}return-1}function as(t){let e="[",n=0;for((t[0]==="^"||t[0]==="!")&&(e+="^",n++);n<t.length;){if(t[n]==="'"){let s=t.indexOf("'",n+1);if(s!==-1){let o=t.slice(n+1,s);for(let i of o)i==="\\"?e+="\\\\":i==="]"?e+="\\]":i==="^"&&e==="["?e+="\\^":e+=i;n=s+1;continue}}if(t[n]==="["&&n+1<t.length&&t[n+1]===":"){let s=t.indexOf(":]",n+2);if(s!==-1){let o=t.slice(n+2,s);e+=ls(o),n=s+2;continue}}let r=t[n];r==="\\"?n+1<t.length?(e+=`\\${t[n+1]}`,n+=2):(e+="\\\\",n++):r==="-"&&n>0&&n<t.length-1?(e+="-",n++):r==="^"&&n===0?(e+="^",n++):(r==="]"&&n===0?e+="\\]":e+=r,n++)}return e+="]",e}var cs={alnum:"a-zA-Z0-9",alpha:"a-zA-Z",ascii:"\\x00-\\x7F",blank:" \\t",cntrl:"\\x00-\\x1F\\x7F",digit:"0-9",graph:"!-~",lower:"a-z",print:" -~",punct:"!-/:-@\\[-`{-~",space:" \\t\\n\\r\\f\\v",upper:"A-Z",word:"a-zA-Z0-9_",xdigit:"0-9A-Fa-f"};function ls(t){return cs[t]??""}function Ne(t,e){let n=`${e}_`,r=[];for(let s of Object.keys(t.state.env))if(s.startsWith(n)){let o=s.slice(n.length),i=Number.parseInt(o,10);!Number.isNaN(i)&&String(i)===o&&r.push(i)}return r.sort((s,o)=>s-o)}function Pt(t,e){let n=`${e}_`;for(let r of Object.keys(t.state.env))r.startsWith(n)&&delete t.state.env[r]}function Rt(t,e){let n=`${e}_`,r=[];for(let s of Object.keys(t.state.env))if(s.startsWith(n)&&!s.includes("__")){let o=s.slice(n.length);r.push(o)}return r.sort()}function Wr(t){return t.startsWith("'")&&t.endsWith("'")||t.startsWith('"')&&t.endsWith('"')?t.slice(1,-1):t}function Ue(t){return t.IFS??`
33
- `}function Mr(t){return t.IFS===""}function $e(t){return t.split("").map(e=>/[\\^$.*+?()[\]{}|-]/.test(e)?`\\${e}`:e===" "?"\\t":e===`
34
- `?"\\n":e).join("")}function us(t,e){let n=$e(t);return new RegExp(`[${n}]+`,e)}function fs(t){let e=$e(t);return new RegExp(`^[${e}]+`)}function ds(t){let e=$e(t);return new RegExp(`[${e}]+$`)}function ve(t){let e=t.IFS;return e===void 0?" ":e[0]||""}function Fr(t,e){if(e==="")return{words:[t],wordStarts:[0]};let n=[],r=[],s=us(e,"g"),o=0,i=t.match(fs(e));i&&(o=i[0].length),s.lastIndex=o;let a=s.exec(t);for(;a!==null;)a.index>o&&(r.push(o),n.push(t.substring(o,a.index))),o=s.lastIndex,a=s.exec(t);return o<t.length&&(r.push(o),n.push(t.substring(o))),{words:n,wordStarts:r}}function Br(t,e){return e===""?t:t.replace(ds(e),"")}function T(t,e){return t.state.associativeArrays?.has(e)?Rt(t,e).map(o=>[o,t.state.env[`${e}_${o}`]]):Ne(t,e).map(s=>[s,t.state.env[`${e}_${s}`]])}function zr(t,e){return t.state.associativeArrays?.has(e)?Rt(t,e).length>0:Ne(t,e).length>0}function j(t,e,n=!0,r=!1){switch(e){case"?":return String(t.state.lastExitCode);case"$":return String(process.pid);case"#":return t.state.env["#"]||"0";case"@":return t.state.env["@"]||"";case"_":return t.state.lastArg;case"-":{let i="";return t.state.options.errexit&&(i+="e"),t.state.options.nounset&&(i+="u"),t.state.options.verbose&&(i+="v"),t.state.options.xtrace&&(i+="x"),t.state.options.pipefail&&(i+="p"),i}case"*":{let i=Number.parseInt(t.state.env["#"]||"0",10);if(i===0)return"";let a=[];for(let l=1;l<=i;l++)a.push(t.state.env[String(l)]||"");return a.join(ve(t.state.env))}case"0":return t.state.env[0]||"bash";case"PWD":return t.state.env.PWD!==void 0?t.state.env.PWD:"";case"OLDPWD":return t.state.env.OLDPWD!==void 0?t.state.env.OLDPWD:""}if(/^[a-zA-Z_][a-zA-Z0-9_]*\[\]$/.test(e))throw new fe(`\${${e}}`);let s=e.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(s){let i=s[1],a=s[2];if(a==="@"||a==="*"){let d=T(t,i);if(d.length>0)return d.map(([,p])=>p).join(" ");let h=t.state.env[i];return h!==void 0?h:""}if(t.state.associativeArrays?.has(i)){let d=Wr(a),h=t.state.env[`${i}_${d}`];if(h===void 0&&n&&t.state.options.nounset)throw new X(`${i}[${a}]`);return h||""}let c;if(/^-?\d+$/.test(a))c=Number.parseInt(a,10);else try{let d=new M,h=L(d,a);c=N(t,h.expression)}catch{let d=t.state.env[a];c=d?Number.parseInt(d,10):0,Number.isNaN(c)&&(c=0)}if(c<0){let d=T(t,i);if(d.length===0)return t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${i}: bad array subscript
35
- `,"";let p=Math.max(...d.map(([A])=>typeof A=="number"?A:0))+1+c;return p<0?(t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${i}: bad array subscript
36
- `,""):t.state.env[`${i}_${p}`]||""}let u=t.state.env[`${i}_${c}`];if(u===void 0&&n&&t.state.options.nounset)throw new X(`${i}[${c}]`);return u||""}if(/^[1-9][0-9]*$/.test(e)){let i=t.state.env[e];if(i===void 0&&n&&t.state.options.nounset)throw new X(e);return i||""}let o=t.state.env[e];if(o===void 0&&n&&t.state.options.nounset)throw new X(e);return o||""}async function Ur(t,e,n,r,s){let o=[];for(let u of e){let d=u.type==="ParameterExpansion"||u.type==="CommandSubstitution"||u.type==="ArithmeticExpansion";if(u.type==="ParameterExpansion"&&It(u)){let h=await s(t,u);o.push({value:h,splittable:!1})}else{let h=await s(t,u);o.push({value:h,splittable:d})}}if(!o.some(u=>u.splittable&&new RegExp(`[${r}]`).test(u.value))){let u=o.map(d=>d.value).join("");return u?[u]:[]}let a=new RegExp(`[${r}]+`),l=[],c="";for(let u=0;u<o.length;u++){let d=o[u];if(!d.splittable)c+=d.value;else{let h=d.value.split(a);for(let p=0;p<h.length;p++)p===0?c+=h[p]:(c!==""&&l.push(c),c=h[p])}}return c!==""&&l.push(c),l}function H(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Gr(t){switch(t.type){case"Literal":return t.value;case"SingleQuoted":return t.value;case"Escaped":return t.value;default:return null}}function Hr(t){switch(t.type){case"SingleQuoted":case"Escaped":case"DoubleQuoted":return!0;case"Literal":return t.value==="";default:return!1}}function kt(t){if(t==="")return"''";if(!/['\\\n\r\t\x00-\x1f\x7f]/.test(t))return`'${t}'`;let e="$'";for(let n of t)switch(n){case"'":e+="\\'";break;case"\\":e+="\\\\";break;case`
37
- `:e+="\\n";break;case"\r":e+="\\r";break;case" ":e+="\\t";break;default:{let r=n.charCodeAt(0);r<32||r===127?e+=`\\x${r.toString(16).padStart(2,"0")}`:e+=n}}return`${e}'`}function pe(t,e,n=!1){return e.map(r=>ne(t,r,n)).join("")}async function me(t,e,n=!1){let r=[];for(let s of e)r.push(await Y(t,s));return r.join("")}function hs(t){return Hr(t)}function Vr(t){if(t.parts.length===0)return!0;for(let e of t.parts)if(!hs(e))return!1;return!0}function Zr(t){return t.replace(/([*?[\]\\])/g,"\\$1")}function jr(t,e,n=!1){let r=Gr(e);if(r!==null)return r;switch(e.type){case"ParameterExpansion":return Yr(t,e,n);case"TildeExpansion":return n?e.user===null?"~":`~${e.user}`:e.user===null?t.state.env.HOME||"/home/user":e.user==="root"?"/root":`~${e.user}`;case"Glob":return e.pattern;default:return null}}function ne(t,e,n=!1){let r=jr(t,e,n);if(r!==null)return r;switch(e.type){case"DoubleQuoted":{let s=[];for(let o of e.parts)s.push(ne(t,o,!0));return s.join("")}case"ArithmeticExpansion":return String(N(t,e.expression.expression));case"BraceExpansion":{let s=[];for(let o of e.items)if(o.type==="Range"){let i=Ce(o.start,o.end,o.step,o.startStr,o.endStr);if(i.expanded)s.push(...i.expanded);else return i.literal}else s.push(Ge(t,o.word));return s.join(" ")}default:return""}}function Ge(t,e){let n=e.parts,r=n.length;if(r===1)return ne(t,n[0]);let s=[];for(let o=0;o<r;o++)s.push(ne(t,n[o]));return s.join("")}async function v(t,e){return re(e)?Xr(t,e):Ge(t,e)}function He(t){for(let e of t)if(e.type==="BraceExpansion"||e.type==="DoubleQuoted"&&He(e.parts))return!0;return!1}function ps(t){for(let e of t){if(e.type==="BraceExpansion"){for(let n of e.items)if(n.type==="Word"&&re(n.word))return!0}if(ze(e))return!0}return!1}var qr=1e4,Ae=1e5;function Qr(t,e,n={count:0}){if(n.count>Ae)return[[]];let r=[[]];for(let s of e)if(s.type==="BraceExpansion"){let o=[],i=!1,a="";for(let u of s.items)if(u.type==="Range"){let d=Ce(u.start,u.end,u.step,u.startStr,u.endStr);if(d.expanded)for(let h of d.expanded)n.count++,o.push(h);else{i=!0,a=d.literal;break}}else{let d=Qr(t,u.word.parts,n);for(let h of d)n.count++,o.push(h.join(""))}if(i){for(let u of r)n.count++,u.push(a);continue}if(r.length*o.length>qr||n.count>Ae)return r;let c=[];for(let u of r)for(let d of o){if(n.count++,n.count>Ae)return c.length>0?c:r;c.push([...u,d])}r=c}else{let o=ne(t,s);for(let i of r)n.count++,i.push(o)}return r}function ms(t,e){let n=e.parts;return He(n)?Qr(t,n).map(s=>s.join("")):[Ge(t,e)]}async function Kr(t,e,n={count:0}){if(n.count>Ae)return[[]];let r=[[]];for(let s of e)if(s.type==="BraceExpansion"){let o=[],i=!1,a="";for(let u of s.items)if(u.type==="Range"){let d=Ce(u.start,u.end,u.step,u.startStr,u.endStr);if(d.expanded)for(let h of d.expanded)n.count++,o.push(h);else{i=!0,a=d.literal;break}}else{let d=await Kr(t,u.word.parts,n);for(let h of d)n.count++,o.push(h.join(""))}if(i){for(let u of r)n.count++,u.push(a);continue}if(r.length*o.length>qr||n.count>Ae)return r;let c=[];for(let u of r)for(let d of o){if(n.count++,n.count>Ae)return c.length>0?c:r;c.push([...u,d])}r=c}else{let o=await Y(t,s);for(let i of r)n.count++,i.push(o)}return r}async function ys(t,e){let n=e.parts;return He(n)?(await Kr(t,n)).map(s=>s.join("")):[await v(t,e)]}async function Ie(t,e){let n=e.parts,{hasQuoted:r,hasCommandSub:s,hasArrayVar:o,hasArrayAtExpansion:i,hasParamExpansion:a}=Tr(n),c=He(n)?ps(n)?await ys(t,e):ms(t,e):null;if(c&&c.length>1){let h=[];for(let p of c)if(!r&&/[*?[]/.test(p)){let A=await new we(t.fs,t.state.cwd).expand(p);A.length>0?h.push(...A):h.push(p)}else h.push(p);return{values:h,quoted:!1}}if(i&&n.length===1&&n[0].type==="DoubleQuoted"){let h=n[0];if(h.parts.length===1&&h.parts[0].type==="ParameterExpansion"){let y=h.parts[0].parameter.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@]\]$/);if(y){let A=y[1],m=T(t,A);if(m.length>0)return{values:m.map(([,w])=>w),quoted:!0};let E=t.state.env[A];return E!==void 0?{values:[E],quoted:!0}:{values:[],quoted:!0}}}}if(!r&&o&&n.length===1&&n[0].type==="ParameterExpansion"){let h=n[0].parameter;if(h==="@"||h==="*"){let p=Number.parseInt(t.state.env["#"]||"0",10);if(p===0)return{values:[],quoted:!1};let y=[];for(let A=1;A<=p;A++)y.push(t.state.env[String(A)]||"");return{values:y,quoted:!1}}}if(!r&&(s||o||a)&&!Mr(t.state.env)){let h=Ue(t.state.env),p=$e(h),y=await Ur(t,n,h,p,Y),A=[],m=new we(t.fs,t.state.cwd);for(let E of y)if(/[*?[]/.test(E)){let w=await m.expand(E);w.length>0?A.push(...w):A.push(E)}else A.push(E);return{values:A,quoted:!1}}let d=re(e)?await Xr(t,e):Ge(t,e);if(!r&&/[*?[]/.test(d)){let p=await new we(t.fs,t.state.cwd).expand(d);if(p.length>0)return{values:p,quoted:!1}}return d===""&&!r?{values:[],quoted:!1}:{values:[d],quoted:r}}async function Xr(t,e){let n=e.parts,r=n.length;if(r===1)return Y(t,n[0]);let s=[];for(let o=0;o<r;o++)s.push(await Y(t,n[o]));return s.join("")}async function Y(t,e){if(e.type==="ParameterExpansion"&&vt(e))return Es(t,e);let n=jr(t,e);if(n!==null)return n;switch(e.type){case"DoubleQuoted":{let r=[];for(let s of e.parts)r.push(await Y(t,s));return r.join("")}case"CommandSubstitution":try{let r=await t.executeScript(e.body);return t.state.lastExitCode=r.exitCode,t.state.env["?"]=String(r.exitCode),r.stdout.replace(/\n+$/,"")}catch(r){if(r instanceof B)throw r;if(r instanceof R)return t.state.lastExitCode=r.exitCode,t.state.env["?"]=String(r.exitCode),r.stdout.replace(/\n+$/,"");throw r}case"ArithmeticExpansion":return String(await $(t,e.expression.expression));case"BraceExpansion":{let r=[];for(let s of e.items)if(s.type==="Range"){let o=Ce(s.start,s.end,s.step,s.startStr,s.endStr);if(o.expanded)r.push(...o.expanded);else return o.literal}else r.push(await v(t,s.word));return r.join(" ")}default:return""}}function Yr(t,e,n=!1){let{parameter:r,operation:s}=e,o=s&&(s.type==="DefaultValue"||s.type==="AssignDefault"||s.type==="UseAlternative"||s.type==="ErrorIfUnset"),i=j(t,r,!o);if(!s)return i;let a=!(r in t.state.env),l=i==="";switch(s.type){case"DefaultValue":return(a||s.checkEmpty&&l)&&s.word?pe(t,s.word.parts,n):i;case"AssignDefault":{if((a||s.checkEmpty&&l)&&s.word){let u=pe(t,s.word.parts,n),d=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(d){let[,h,p]=d,y;if(/^\d+$/.test(p))y=Number.parseInt(p,10);else{try{let m=new M,E=L(m,p);y=N(t,E.expression)}catch{let m=t.state.env[p];y=m?Number.parseInt(m,10):0}Number.isNaN(y)&&(y=0)}t.state.env[`${h}_${y}`]=u;let A=Number.parseInt(t.state.env[`${h}__length`]||"0",10);y>=A&&(t.state.env[`${h}__length`]=String(y+1))}else t.state.env[r]=u;return u}return i}case"ErrorIfUnset":{if(a||s.checkEmpty&&l){let u=s.word?pe(t,s.word.parts,n):`${r}: parameter null or not set`;throw new R(1,"",`bash: ${u}
38
- `)}return i}case"UseAlternative":return!(a||s.checkEmpty&&l)&&s.word?pe(t,s.word.parts,n):"";case"Length":{let c=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/);if(c){let u=T(t,c[1]);return String(u.length)}if(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(r)&&zr(t,r)){let u=t.state.env[`${r}_0`]||"";return String(u.length)}return String(i.length)}case"LengthSliceError":throw new fe(r);case"Substring":{let c=s.offset?N(t,s.offset.expression):0,u=s.length?N(t,s.length.expression):void 0;if(r==="@"||r==="*"){let y=(t.state.env["@"]||"").split(" ").filter(w=>w),A=t.state.env[0]||"bash",m=c===0?[A,...y]:y,E=c===0?0:c-1;if(E<0||E>=m.length)return"";if(u!==void 0){let w=u<0?m.length+u:E+u;return m.slice(E,Math.max(E,w)).join(" ")}return m.slice(E).join(" ")}let d=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/);if(d){let A=T(t,d[1]).map(([,E])=>E),m=c;if(m<0&&(m=A.length+m,m<0))return"";if(u!==void 0){if(u<0){let E=A.length+u;return A.slice(m,Math.max(m,E)).join(" ")}return A.slice(m,m+u).join(" ")}return A.slice(m).join(" ")}let h=[...i],p=c;if(p<0&&(p=Math.max(0,h.length+p)),u!==void 0){if(u<0){let y=h.length+u;return h.slice(p,Math.max(p,y)).join("")}return h.slice(p,p+u).join("")}return h.slice(p).join("")}case"PatternRemoval":{let c="";if(s.pattern)for(let d of s.pattern.parts)if(d.type==="Glob")c+=G(d.pattern,s.greedy);else if(d.type==="Literal")c+=G(d.value,s.greedy);else if(d.type==="SingleQuoted"||d.type==="Escaped")c+=H(d.value);else if(d.type==="DoubleQuoted"){let h=pe(t,d.parts);c+=H(h)}else if(d.type==="ParameterExpansion"){let h=ne(t,d);c+=G(h,s.greedy)}else{let h=ne(t,d);c+=H(h)}if(s.side==="prefix")return i.replace(new RegExp(`^${c}`),"");let u=new RegExp(`${c}$`);if(s.greedy)return i.replace(u,"");for(let d=i.length;d>=0;d--){let h=i.slice(d);if(u.test(h))return i.slice(0,d)}return i}case"PatternReplacement":{let c="";if(s.pattern)for(let h of s.pattern.parts)if(h.type==="Glob")c+=G(h.pattern,!0);else if(h.type==="Literal")c+=G(h.value,!0);else if(h.type==="SingleQuoted"||h.type==="Escaped")c+=H(h.value);else if(h.type==="DoubleQuoted"){let p=pe(t,h.parts);c+=H(p)}else if(h.type==="ParameterExpansion"){let p=ne(t,h);c+=G(p,!0)}else{let p=ne(t,h);c+=H(p)}let u=s.replacement?pe(t,s.replacement.parts):"";if(c==="")return i;s.anchor==="start"?c=`^${c}`:s.anchor==="end"&&(c=`${c}$`);let d=s.all?"g":"";try{let h=new RegExp(c,d);if(s.all){let p="",y=0,A=h.exec(i);for(;A!==null&&!(A[0].length===0&&A.index===i.length);)p+=i.slice(y,A.index)+u,y=A.index+A[0].length,A[0].length===0&&y++,A=h.exec(i);return p+=i.slice(y),p}return i.replace(h,u)}catch{return i}}case"CaseModification":return s.direction==="upper"?s.all?i.toUpperCase():i.charAt(0).toUpperCase()+i.slice(1):s.all?i.toLowerCase():i.charAt(0).toLowerCase()+i.slice(1);case"Transform":{let c=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/);if(c&&s.operator==="Q")return T(t,c[1]).map(([,h])=>kt(h)).join(" ");switch(s.operator){case"Q":return kt(i);case"P":return i;case"a":return"";case"A":return`${r}=${kt(i)}`;case"E":return i.replace(/\\([\\abefnrtv'"?])/g,(u,d)=>{switch(d){case"\\":return"\\";case"a":return"\x07";case"b":return"\b";case"e":return"\x1B";case"f":return"\f";case"n":return`
39
- `;case"r":return"\r";case"t":return" ";case"v":return"\v";case"'":return"'";case'"':return'"';case"?":return"?";default:return d}});case"K":return"";default:return i}}case"Indirection":return j(t,i);case"ArrayKeys":{let u=T(t,s.array).map(([d])=>String(d));return s.star?u.join(ve(t.state.env)):u.join(" ")}case"VarNamePrefix":{let c=Object.keys(t.state.env).filter(u=>u.startsWith(s.prefix)&&!u.includes("__")).sort();return s.star?c.join(ve(t.state.env)):c.join(" ")}default:return i}}async function Es(t,e,n=!1){let{parameter:r,operation:s}=e,o=s&&(s.type==="DefaultValue"||s.type==="AssignDefault"||s.type==="UseAlternative"||s.type==="ErrorIfUnset"),i=j(t,r,!o);if(!s)return i;let a=!(r in t.state.env),l=i==="";switch(s.type){case"DefaultValue":return(a||s.checkEmpty&&l)&&s.word?me(t,s.word.parts,n):i;case"AssignDefault":{if((a||s.checkEmpty&&l)&&s.word){let u=await me(t,s.word.parts,n),d=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(d){let[,h,p]=d,y;if(/^\d+$/.test(p))y=Number.parseInt(p,10);else{try{let m=new M,E=L(m,p);y=await $(t,E.expression)}catch{let m=t.state.env[p];y=m?Number.parseInt(m,10):0}Number.isNaN(y)&&(y=0)}t.state.env[`${h}_${y}`]=u;let A=Number.parseInt(t.state.env[`${h}__length`]||"0",10);y>=A&&(t.state.env[`${h}__length`]=String(y+1))}else t.state.env[r]=u;return u}return i}case"ErrorIfUnset":{if(a||s.checkEmpty&&l){let u=s.word?await me(t,s.word.parts,n):`${r}: parameter null or not set`;throw new R(1,"",`bash: ${u}
40
- `)}return i}case"UseAlternative":return!(a||s.checkEmpty&&l)&&s.word?me(t,s.word.parts,n):"";case"PatternRemoval":{let c="";if(s.pattern)for(let d of s.pattern.parts)if(d.type==="Glob")c+=G(d.pattern,s.greedy);else if(d.type==="Literal")c+=G(d.value,s.greedy);else if(d.type==="SingleQuoted"||d.type==="Escaped")c+=H(d.value);else if(d.type==="DoubleQuoted"){let h=await me(t,d.parts);c+=H(h)}else if(d.type==="ParameterExpansion"){let h=await Y(t,d);c+=G(h,s.greedy)}else{let h=await Y(t,d);c+=H(h)}if(s.side==="prefix")return i.replace(new RegExp(`^${c}`),"");let u=new RegExp(`${c}$`);if(s.greedy)return i.replace(u,"");for(let d=i.length;d>=0;d--){let h=i.slice(d);if(u.test(h))return i.slice(0,d)}return i}case"PatternReplacement":{let c="";if(s.pattern)for(let h of s.pattern.parts)if(h.type==="Glob")c+=G(h.pattern,!0);else if(h.type==="Literal")c+=G(h.value,!0);else if(h.type==="SingleQuoted"||h.type==="Escaped")c+=H(h.value);else if(h.type==="DoubleQuoted"){let p=await me(t,h.parts);c+=H(p)}else if(h.type==="ParameterExpansion"){let p=await Y(t,h);c+=G(p,!0)}else{let p=await Y(t,h);c+=H(p)}let u=s.replacement?await me(t,s.replacement.parts):"";if(c==="")return i;s.anchor==="start"?c=`^${c}`:s.anchor==="end"&&(c=`${c}$`);let d=s.all?"g":"";try{let h=new RegExp(c,d);if(s.all){let p="",y=0,A=h.exec(i);for(;A!==null&&!(A[0].length===0&&A.index===i.length);)p+=i.slice(y,A.index)+u,y=A.index+A[0].length,A[0].length===0&&y++,A=h.exec(i);return p+=i.slice(y),p}return i.replace(h,u)}catch{return i}}default:return Yr(t,e,n)}}function Dt(t,e,n){switch(n){case"+":return t+e;case"-":return t-e;case"*":return t*e;case"/":return e!==0?Math.trunc(t/e):0;case"%":return e!==0?t%e:0;case"**":if(e<0)throw new W("exponent less than 0");return t**e;case"<<":return t<<e;case">>":return t>>e;case"<":return t<e?1:0;case"<=":return t<=e?1:0;case">":return t>e?1:0;case">=":return t>=e?1:0;case"==":return t===e?1:0;case"!=":return t!==e?1:0;case"&":return t&e;case"|":return t|e;case"^":return t^e;case",":return e;default:return 0}}function Jr(t,e,n){switch(n){case"=":return e;case"+=":return t+e;case"-=":return t-e;case"*=":return t*e;case"/=":return e!==0?Math.trunc(t/e):0;case"%=":return e!==0?t%e:0;case"<<=":return t<<e;case">>=":return t>>e;case"&=":return t&e;case"|=":return t|e;case"^=":return t^e;default:return e}}function _t(t,e){switch(e){case"-":return-t;case"+":return+t;case"!":return t===0?1:0;case"~":return~t;default:return t}}function ws(t,e){let n=t.state.env[e];if(n!==void 0)return n;let r=t.state.env[`${e}_0`];return r!==void 0?r:j(t,e)}function ge(t){if(!t)return 0;let e=Number.parseInt(t,10);if(!Number.isNaN(e)&&/^-?\d+$/.test(t.trim()))return e;let n=t.trim();if(!n)return 0;try{let r=new M,{expr:s,pos:o}=ee(r,n,0);if(o<n.length){let i=n.slice(o).trim().split(/\s+/)[0];throw new W(`${n}: syntax error in expression (error token is "${i}")`)}return s.type==="ArithNumber"?s.value:e||0}catch(r){if(r instanceof W)throw r;let s=n.split(/\s+/).slice(1)[0]||n;throw new W(`${n}: syntax error in expression (error token is "${s}")`)}}function Ve(t,e,n=new Set){if(n.has(e))return 0;n.add(e);let r=ws(t,e);if(!r)return 0;let s=Number.parseInt(r,10);if(!Number.isNaN(s)&&/^-?\d+$/.test(r.trim()))return s;let o=r.trim();if(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(o))return Ve(t,o,n);try{let i=new M,{expr:a}=ee(i,o,0);return q(t,a,n)}catch{return 0}}function q(t,e,n){switch(e.type){case"ArithNumber":if(Number.isNaN(e.value))throw new W("value too great for base");return e.value;case"ArithVariable":return Ve(t,e.name,n);case"ArithBinary":{if(e.operator==="||")return q(t,e.left,n)||q(t,e.right,n)?1:0;if(e.operator==="&&")return q(t,e.left,n)&&q(t,e.right,n)?1:0;let r=q(t,e.left,n),s=q(t,e.right,n);return Dt(r,s,e.operator)}case"ArithUnary":{let r=q(t,e.operand,n);return _t(r,e.operator)}case"ArithTernary":return q(t,e.condition,n)?q(t,e.consequent,n):q(t,e.alternate,n);case"ArithGroup":return q(t,e.expression,n);default:return N(t,e)}}function ae(t,e){if(e.startsWith("#")){let d=e.slice(1),h=t.state.env[d]||"";return String(h.length)}if(e.startsWith("!")){let d=e.slice(1),h=t.state.env[d]||"";return t.state.env[h]||""}let n=[":-",":=",":?",":+","-","=","?","+"],r=-1,s="";for(let d of n){let h=e.indexOf(d);h>0&&(r===-1||h<r)&&(r=h,s=d)}if(r===-1)return j(t,e);let o=e.slice(0,r),i=e.slice(r+s.length),a=t.state.env[o],l=a===void 0,c=a==="",u=s.startsWith(":");switch(s){case":-":case"-":return l||u&&c?i:a||"";case":=":case"=":return l||u&&c?(t.state.env[o]=i,i):a||"";case":+":case"+":return!(l||u&&c)?i:"";case":?":case"?":{if(l||u&&c)throw new Error(i||`${o}: parameter null or not set`);return a||""}default:return a||""}}function N(t,e){switch(e.type){case"ArithNumber":if(Number.isNaN(e.value))throw new W("value too great for base");return e.value;case"ArithVariable":return Ve(t,e.name);case"ArithNested":return N(t,e.expression);case"ArithCommandSubst":return 0;case"ArithBracedExpansion":{let n=ae(t,e.content);return Number.parseInt(n,10)||0}case"ArithDynamicBase":{let n=ae(t,e.baseExpr),r=Number.parseInt(n,10);if(r<2||r>64)return 0;let s=`${r}#${e.value}`;return Ee(s)}case"ArithDynamicNumber":{let r=ae(t,e.prefix)+e.suffix;return Ee(r)}case"ArithArrayElement":{let n=t.state.associativeArrays?.has(e.array),r;if(e.stringKey!==void 0)r=`${e.array}_${e.stringKey}`;else if(n&&e.index?.type==="ArithVariable")r=`${e.array}_${e.index.name}`;else if(e.index){let o=N(t,e.index);if(o<0){let a=T(t,e.array);if(a.length===0)return t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${e.array}: bad array subscript
32
+ `&&(i+=c),o+=2):(i+=e[o],o++)}else i+=e[o],o++;o>=e.length&&this.error("unexpected EOF while looking for matching ``'");let l=new t().parse(i);return{part:A.commandSubstitution(l,!0),endIndex:o+1}}parseArithmeticExpansion(e,n){let r=n+3,s=1,o=0,i=r;for(;i<e.length-1&&s>0;)e[i]==="$"&&e[i+1]==="("?e[i+2]==="("?(s++,i+=3):(o++,i+=2):e[i]==="("&&e[i+1]==="("?(s++,i+=2):e[i]===")"&&e[i+1]===")"?o>0?(o--,i++):(s--,s>0&&(i+=2)):e[i]==="("?(o++,i++):(e[i]===")"&&o>0&&o--,i++);let a=e.slice(r,i),l=this.parseArithmeticExpression(a);return{part:A.arithmeticExpansion(l),endIndex:i+2}}parseArithmeticCommand(){this.expect(f.DPAREN_START);let e="",n=1,r=0,s=!1,o=!1;for(;n>0&&!this.check(f.EOF);){if(s){if(s=!1,r>0){r--,e+=")";continue}if(this.check(f.RPAREN)){n--,o=!0,this.advance();continue}if(this.check(f.DPAREN_END)){n--,o=!0;continue}e+=")";continue}this.check(f.DPAREN_START)?(n++,e+="((",this.advance()):this.check(f.DPAREN_END)?r>=2?(r-=2,e+="))",this.advance()):r===1?(r--,e+=")",s=!0,this.advance()):(n--,o=!0,n>0&&(e+="))"),this.advance()):this.check(f.LPAREN)?(r++,e+="(",this.advance()):this.check(f.RPAREN)?(r>0&&r--,e+=")",this.advance()):(e+=this.current().value,this.advance())}o||this.expect(f.DPAREN_END);let i=this.parseArithmeticExpression(e.trim()),a=this.parseOptionalRedirections();return A.arithmeticCommand(i,a)}parseConditionalCommand(){this.expect(f.DBRACK_START);let e=$t(this);this.expect(f.DBRACK_END);let n=this.parseOptionalRedirections();return A.conditionalCommand(e,n)}parseFunctionDef(){let e;this.check(f.FUNCTION)?(this.advance(),e=this.expect(f.NAME,"Expected function name").value,this.check(f.LPAREN)&&(this.advance(),this.expect(f.RPAREN))):(e=this.advance().value,this.expect(f.LPAREN),this.expect(f.RPAREN)),this.skipNewlines();let n=this.parseCompoundCommandBody(),r=this.parseOptionalRedirections();return A.functionDef(e,n,r)}parseCompoundCommandBody(){if(this.check(f.LBRACE))return Ct(this);if(this.check(f.LPAREN))return bt(this);if(this.check(f.IF))return wt(this);if(this.check(f.FOR))return gt(this);if(this.check(f.WHILE))return At(this);if(this.check(f.UNTIL))return xt(this);if(this.check(f.CASE))return St(this);this.error("Expected compound command for function body")}parseCompoundList(){let e=[];for(this.skipNewlines();!this.check(f.EOF,f.FI,f.ELSE,f.ELIF,f.THEN,f.DO,f.DONE,f.ESAC,f.RPAREN,f.RBRACE,f.DSEMI,f.SEMI_AND,f.SEMI_SEMI_AND)&&this.isCommandStart();){this.checkIterationLimit();let n=this.pos,r=this.parseStatement();if(r&&e.push(r),this.skipSeparators(),this.pos===n&&!r)break}return e}parseOptionalRedirections(){let e=[];for(;Be(this);){this.checkIterationLimit();let n=this.pos;if(e.push(ze(this)),this.pos===n)break}return e}parseArithmeticExpression(e){return L(this,e)}};function oe(t){return new M().parse(t)}var we=class{fs;cwd;constructor(e,n){this.fs=e,this.cwd=n}isGlobPattern(e){return e.includes("*")||e.includes("?")||/\[.*\]/.test(e)}async expandArgs(e,n){let r=[];for(let s=0;s<e.length;s++){let o=e[s];if((n?.[s]??!1)||!this.isGlobPattern(o))r.push(o);else{let a=await this.expand(o);a.length>0?r.push(...a):r.push(o)}}return r}async expand(e){return e.includes("**")?this.expandRecursive(e):this.expandSimple(e)}async expandSimple(e){let n=[],r=e.lastIndexOf("/"),s,o;r===-1?(s=this.cwd,o=e):(s=e.slice(0,r)||"/",o=e.slice(r+1));let i=this.fs.resolvePath(this.cwd,s);try{let a=await this.fs.readdir(i);for(let l of a)if(this.matchPattern(l,o)){let c=r===-1?l:`${s}/${l}`;n.push(c)}}catch{}return n.sort()}async expandRecursive(e){let n=[],r=e.indexOf("**"),s=e.slice(0,r).replace(/\/$/,"")||".",i=e.slice(r+2).replace(/^\//,"");return await this.walkDirectory(s,i,n),n.sort()}async walkDirectory(e,n,r){let s=this.fs.resolvePath(this.cwd,e);try{let o=await this.fs.readdir(s);for(let i of o){let a=e==="."?i:`${e}/${i}`,l=this.fs.resolvePath(this.cwd,a);try{(await this.fs.stat(l)).isDirectory?await this.walkDirectory(a,n,r):n&&this.matchPattern(i,n)&&r.push(a)}catch{}}}catch{}}matchPattern(e,n){return this.patternToRegex(n).test(e)}patternToRegex(e){let n="^";for(let r=0;r<e.length;r++){let s=e[r];if(s==="*")n+=".*";else if(s==="?")n+=".";else if(s==="["){let o=r+1,i="[";for(o<e.length&&(e[o]==="^"||e[o]==="!")&&(i+="^",o++),o<e.length&&e[o]==="]"&&(i+="\\]",o++);o<e.length&&e[o]!=="]";){if(e[o]==="["&&o+1<e.length&&e[o+1]===":"){let a=e.indexOf(":]",o+2);if(a!==-1){let l=e.slice(o+2,a),c=this.posixClassToRegex(l);i+=c,o=a+2;continue}}if(e[o]==="\\"&&o+1<e.length){i+=`\\${e[o+1]}`,o+=2;continue}e[o]==="-"?i+="\\-":i+=e[o],o++}i+="]",n+=i,r=o}else if(s==="\\"&&r+1<e.length){let o=e[r+1];/[.+^${}()|\\*?[\]]/.test(o)?n+=`\\${o}`:n+=o,r++}else/[.+^${}()|]/.test(s)?n+=`\\${s}`:n+=s}return n+="$",new RegExp(n)}posixClassToRegex(e){return{alnum:"a-zA-Z0-9",alpha:"a-zA-Z",ascii:"\\x00-\\x7F",blank:" \\t",cntrl:"\\x00-\\x1F\\x7F",digit:"0-9",graph:"!-~",lower:"a-z",print:" -~",punct:"!-/:-@\\[-`{-~",space:" \\t\\n\\r\\f\\v",upper:"A-Z",word:"a-zA-Z0-9_",xdigit:"0-9a-fA-F"}[e]||""}};function Z(t){switch(t.type){case"ArithCommandSubst":return!0;case"ArithNested":return Z(t.expression);case"ArithBinary":return Z(t.left)||Z(t.right);case"ArithUnary":return Z(t.operand);case"ArithTernary":return Z(t.condition)||Z(t.consequent)||Z(t.alternate);case"ArithAssignment":return Z(t.value);case"ArithGroup":return Z(t.expression);case"ArithArrayElement":return t.index?Z(t.index):!1;case"ArithConcat":return t.parts.some(Z);default:return!1}}function It(t){let e=t.operation;return e?!!("word"in e&&e.word&&re(e.word)||e.type==="PatternReplacement"&&(e.pattern&&re(e.pattern)||e.replacement&&re(e.replacement))||e.type==="PatternRemoval"&&e.pattern&&re(e.pattern)):!1}function Ue(t){switch(t.type){case"CommandSubstitution":return!0;case"ArithmeticExpansion":return Z(t.expression.expression);case"DoubleQuoted":return t.parts.some(Ue);case"BraceExpansion":return t.items.some(e=>e.type==="Word"&&re(e.word));case"ParameterExpansion":return It(t);default:return!1}}function re(t){return t.parts.some(Ue)}function Pt(t){if(!t.operation)return!1;let e=t.operation,n;if((e.type==="DefaultValue"||e.type==="AssignDefault"||e.type==="UseAlternative"||e.type==="ErrorIfUnset")&&(n=e.word?.parts),!n)return!1;for(let r of n)if(r.type==="DoubleQuoted"||r.type==="SingleQuoted")return!0;return!1}function Fr(t){let e=!1,n=!1,r=!1,s=!1,o=!1;for(let i of t){if((i.type==="SingleQuoted"||i.type==="DoubleQuoted")&&(e=!0,i.type==="DoubleQuoted"))for(let a of i.parts)a.type==="ParameterExpansion"&&a.parameter.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/)&&!a.operation&&(s=!0);i.type==="CommandSubstitution"&&(n=!0),i.type==="ParameterExpansion"&&(o=!0,(i.parameter==="@"||i.parameter==="*")&&(r=!0),Pt(i)&&(e=!0))}return{hasQuoted:e,hasCommandSub:n,hasArrayVar:r,hasArrayAtExpansion:s,hasParamExpansion:o}}function as(t,e,n,r,s){let o=n??1;o===0&&(o=1);let i=Math.abs(o),a=[],l=0;r?.match(/^-?0\d/)&&(l=Math.max(l,r.replace(/^-/,"").length)),s?.match(/^-?0\d/)&&(l=Math.max(l,s.replace(/^-/,"").length));let c=u=>{if(l>0){let d=u<0,h=String(Math.abs(u)).padStart(l,"0");return d?`-${h}`:h}return String(u)};if(t<=e)for(let u=t,d=0;u<=e&&d<1e4;u+=i,d++)a.push(c(u));else for(let u=t,d=0;u>=e&&d<1e4;u-=i,d++)a.push(c(u));return a}function cs(t,e,n){let r=n??1;r===0&&(r=1);let s=t.charCodeAt(0),o=e.charCodeAt(0),i=Math.abs(r),a=t>="A"&&t<="Z",l=t>="a"&&t<="z",c=e>="A"&&e<="Z",u=e>="a"&&e<="z";if(a&&u||l&&c)return null;let d=[];if(s<=o)for(let h=s,p=0;h<=o&&p<1e4;h+=i,p++)d.push(String.fromCharCode(h));else for(let h=s,p=0;h>=o&&p<1e4;h-=i,p++)d.push(String.fromCharCode(h));return d}function be(t,e,n,r,s){let o=n!==void 0?`..${n}`:"";return typeof t=="number"&&typeof e=="number"?{expanded:as(t,e,n,r,s),literal:`{${t}..${e}${o}}`}:typeof t=="string"&&typeof e=="string"?{expanded:cs(t,e,n),literal:`{${t}..${e}${o}}`}:{expanded:null,literal:`{${t}..${e}${o}}`}}function G(t,e){let n="",r=0;for(;r<t.length;){let s=t[r];if(s==="\\")if(r+1<t.length){let o=t[r+1];/[\\^$.|+(){}[\]*?]/.test(o)?n+=`\\${o}`:n+=o,r+=2}else n+="\\\\",r++;else if(s==="*")n+=e?".*":".*?",r++;else if(s==="?")n+=".",r++;else if(s==="["){let o=ls(t,r);if(o===-1)n+="\\[",r++;else{let i=t.slice(r+1,o);n+=us(i),r=o+1}}else/[\^$.|+(){}]/.test(s)?(n+=`\\${s}`,r++):(n+=s,r++)}return n}function ls(t,e){let n=e+1;for(n<t.length&&t[n]==="^"&&n++,n<t.length&&t[n]==="]"&&n++;n<t.length;){if(t[n]==="\\"&&n+1<t.length){n+=2;continue}if(t[n]==="]")return n;if(t[n]==="'"){let r=t.indexOf("'",n+1);if(r!==-1){n=r+1;continue}}if(t[n]==="["&&n+1<t.length&&t[n+1]===":"){let r=t.indexOf(":]",n+2);if(r!==-1){n=r+2;continue}}n++}return-1}function us(t){let e="[",n=0;for((t[0]==="^"||t[0]==="!")&&(e+="^",n++);n<t.length;){if(t[n]==="'"){let s=t.indexOf("'",n+1);if(s!==-1){let o=t.slice(n+1,s);for(let i of o)i==="\\"?e+="\\\\":i==="]"?e+="\\]":i==="^"&&e==="["?e+="\\^":e+=i;n=s+1;continue}}if(t[n]==="["&&n+1<t.length&&t[n+1]===":"){let s=t.indexOf(":]",n+2);if(s!==-1){let o=t.slice(n+2,s);e+=ds(o),n=s+2;continue}}let r=t[n];r==="\\"?n+1<t.length?(e+=`\\${t[n+1]}`,n+=2):(e+="\\\\",n++):r==="-"&&n>0&&n<t.length-1?(e+="-",n++):r==="^"&&n===0?(e+="^",n++):(r==="]"&&n===0?e+="\\]":e+=r,n++)}return e+="]",e}var fs={alnum:"a-zA-Z0-9",alpha:"a-zA-Z",ascii:"\\x00-\\x7F",blank:" \\t",cntrl:"\\x00-\\x1F\\x7F",digit:"0-9",graph:"!-~",lower:"a-z",print:" -~",punct:"!-/:-@\\[-`{-~",space:" \\t\\n\\r\\f\\v",upper:"A-Z",word:"a-zA-Z0-9_",xdigit:"0-9A-Fa-f"};function ds(t){return fs[t]??""}function Ce(t,e){let n=`${e}_`,r=[];for(let s of Object.keys(t.state.env))if(s.startsWith(n)){let o=s.slice(n.length),i=Number.parseInt(o,10);!Number.isNaN(i)&&String(i)===o&&r.push(i)}return r.sort((s,o)=>s-o)}function Rt(t,e){let n=`${e}_`;for(let r of Object.keys(t.state.env))r.startsWith(n)&&delete t.state.env[r]}function kt(t,e){let n=`${e}_`,r=[];for(let s of Object.keys(t.state.env))if(s.startsWith(n)&&!s.includes("__")){let o=s.slice(n.length);r.push(o)}return r.sort()}function Br(t){return t.startsWith("'")&&t.endsWith("'")||t.startsWith('"')&&t.endsWith('"')?t.slice(1,-1):t}function Ge(t){return t.IFS??`
33
+ `}function zr(t){return t.IFS===""}function Ne(t){return t.split("").map(e=>/[\\^$.*+?()[\]{}|-]/.test(e)?`\\${e}`:e===" "?"\\t":e===`
34
+ `?"\\n":e).join("")}function hs(t,e){let n=Ne(t);return new RegExp(`[${n}]+`,e)}function ps(t){let e=Ne(t);return new RegExp(`^[${e}]+`)}function ms(t){let e=Ne(t);return new RegExp(`[${e}]+$`)}function $e(t){let e=t.IFS;return e===void 0?" ":e[0]||""}function Ur(t,e){if(e==="")return{words:[t],wordStarts:[0]};let n=[],r=[],s=hs(e,"g"),o=0,i=t.match(ps(e));i&&(o=i[0].length),s.lastIndex=o;let a=s.exec(t);for(;a!==null;)a.index>o&&(r.push(o),n.push(t.substring(o,a.index))),o=s.lastIndex,a=s.exec(t);return o<t.length&&(r.push(o),n.push(t.substring(o))),{words:n,wordStarts:r}}function Gr(t,e){return e===""?t:t.replace(ms(e),"")}function T(t,e){return t.state.associativeArrays?.has(e)?kt(t,e).map(o=>[o,t.state.env[`${e}_${o}`]]):Ce(t,e).map(s=>[s,t.state.env[`${e}_${s}`]])}function Hr(t,e){return t.state.associativeArrays?.has(e)?kt(t,e).length>0:Ce(t,e).length>0}function j(t,e,n=!0,r=!1){switch(e){case"?":return String(t.state.lastExitCode);case"$":return String(process.pid);case"#":return t.state.env["#"]||"0";case"@":return t.state.env["@"]||"";case"_":return t.state.lastArg;case"-":{let i="";return t.state.options.errexit&&(i+="e"),t.state.options.nounset&&(i+="u"),t.state.options.verbose&&(i+="v"),t.state.options.xtrace&&(i+="x"),t.state.options.pipefail&&(i+="p"),i}case"*":{let i=Number.parseInt(t.state.env["#"]||"0",10);if(i===0)return"";let a=[];for(let l=1;l<=i;l++)a.push(t.state.env[String(l)]||"");return a.join($e(t.state.env))}case"0":return t.state.env[0]||"bash";case"PWD":return t.state.env.PWD!==void 0?t.state.env.PWD:"";case"OLDPWD":return t.state.env.OLDPWD!==void 0?t.state.env.OLDPWD:""}if(/^[a-zA-Z_][a-zA-Z0-9_]*\[\]$/.test(e))throw new fe(`\${${e}}`);let s=e.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(s){let i=s[1],a=s[2];if(a==="@"||a==="*"){let d=T(t,i);if(d.length>0)return d.map(([,p])=>p).join(" ");let h=t.state.env[i];return h!==void 0?h:""}if(t.state.associativeArrays?.has(i)){let d=Br(a),h=t.state.env[`${i}_${d}`];if(h===void 0&&n&&t.state.options.nounset)throw new X(`${i}[${a}]`);return h||""}let c;if(/^-?\d+$/.test(a))c=Number.parseInt(a,10);else try{let d=new M,h=L(d,a);c=N(t,h.expression)}catch{let d=t.state.env[a];c=d?Number.parseInt(d,10):0,Number.isNaN(c)&&(c=0)}if(c<0){let d=T(t,i);if(d.length===0)return t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${i}: bad array subscript
35
+ `,"";let p=Math.max(...d.map(([g])=>typeof g=="number"?g:0))+1+c;return p<0?(t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${i}: bad array subscript
36
+ `,""):t.state.env[`${i}_${p}`]||""}let u=t.state.env[`${i}_${c}`];if(u===void 0&&n&&t.state.options.nounset)throw new X(`${i}[${c}]`);return u||""}if(/^[1-9][0-9]*$/.test(e)){let i=t.state.env[e];if(i===void 0&&n&&t.state.options.nounset)throw new X(e);return i||""}let o=t.state.env[e];if(o===void 0&&n&&t.state.options.nounset)throw new X(e);return o||""}async function Vr(t,e,n,r,s){let o=[];for(let u of e){let d=u.type==="ParameterExpansion"||u.type==="CommandSubstitution"||u.type==="ArithmeticExpansion";if(u.type==="ParameterExpansion"&&Pt(u)){let h=await s(t,u);o.push({value:h,splittable:!1})}else{let h=await s(t,u);o.push({value:h,splittable:d})}}if(!o.some(u=>u.splittable&&new RegExp(`[${r}]`).test(u.value))){let u=o.map(d=>d.value).join("");return u?[u]:[]}let a=new RegExp(`[${r}]+`),l=[],c="";for(let u=0;u<o.length;u++){let d=o[u];if(!d.splittable)c+=d.value;else{let h=d.value.split(a);for(let p=0;p<h.length;p++)p===0?c+=h[p]:(c!==""&&l.push(c),c=h[p])}}return c!==""&&l.push(c),l}function H(t){return t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")}function Zr(t){switch(t.type){case"Literal":return t.value;case"SingleQuoted":return t.value;case"Escaped":return t.value;default:return null}}function jr(t){switch(t.type){case"SingleQuoted":case"Escaped":case"DoubleQuoted":return!0;case"Literal":return t.value==="";default:return!1}}function Dt(t){if(t==="")return"''";if(!/['\\\n\r\t\x00-\x1f\x7f]/.test(t))return`'${t}'`;let e="$'";for(let n of t)switch(n){case"'":e+="\\'";break;case"\\":e+="\\\\";break;case`
37
+ `:e+="\\n";break;case"\r":e+="\\r";break;case" ":e+="\\t";break;default:{let r=n.charCodeAt(0);r<32||r===127?e+=`\\x${r.toString(16).padStart(2,"0")}`:e+=n}}return`${e}'`}function pe(t,e,n=!1){return e.map(r=>ne(t,r,n)).join("")}async function me(t,e,n=!1){let r=[];for(let s of e)r.push(await Y(t,s));return r.join("")}function ys(t){return jr(t)}function qr(t){if(t.parts.length===0)return!0;for(let e of t.parts)if(!ys(e))return!1;return!0}function Qr(t){return t.replace(/([*?[\]\\])/g,"\\$1")}function Kr(t,e,n=!1){let r=Zr(e);if(r!==null)return r;switch(e.type){case"ParameterExpansion":return tn(t,e,n);case"TildeExpansion":return n?e.user===null?"~":`~${e.user}`:e.user===null?t.state.env.HOME||"/home/user":e.user==="root"?"/root":`~${e.user}`;case"Glob":return e.pattern;default:return null}}function ne(t,e,n=!1){let r=Kr(t,e,n);if(r!==null)return r;switch(e.type){case"DoubleQuoted":{let s=[];for(let o of e.parts)s.push(ne(t,o,!0));return s.join("")}case"ArithmeticExpansion":return String(N(t,e.expression.expression));case"BraceExpansion":{let s=[];for(let o of e.items)if(o.type==="Range"){let i=be(o.start,o.end,o.step,o.startStr,o.endStr);if(i.expanded)s.push(...i.expanded);else return i.literal}else s.push(He(t,o.word));return s.join(" ")}default:return""}}function He(t,e){let n=e.parts,r=n.length;if(r===1)return ne(t,n[0]);let s=[];for(let o=0;o<r;o++)s.push(ne(t,n[o]));return s.join("")}async function v(t,e){return re(e)?en(t,e):He(t,e)}function Ve(t){for(let e of t)if(e.type==="BraceExpansion"||e.type==="DoubleQuoted"&&Ve(e.parts))return!0;return!1}function Es(t){for(let e of t){if(e.type==="BraceExpansion"){for(let n of e.items)if(n.type==="Word"&&re(n.word))return!0}if(Ue(e))return!0}return!1}var Xr=1e4,ge=1e5;function Yr(t,e,n={count:0}){if(n.count>ge)return[[]];let r=[[]];for(let s of e)if(s.type==="BraceExpansion"){let o=[],i=!1,a="";for(let u of s.items)if(u.type==="Range"){let d=be(u.start,u.end,u.step,u.startStr,u.endStr);if(d.expanded)for(let h of d.expanded)n.count++,o.push(h);else{i=!0,a=d.literal;break}}else{let d=Yr(t,u.word.parts,n);for(let h of d)n.count++,o.push(h.join(""))}if(i){for(let u of r)n.count++,u.push(a);continue}if(r.length*o.length>Xr||n.count>ge)return r;let c=[];for(let u of r)for(let d of o){if(n.count++,n.count>ge)return c.length>0?c:r;c.push([...u,d])}r=c}else{let o=ne(t,s);for(let i of r)n.count++,i.push(o)}return r}function ws(t,e){let n=e.parts;return Ve(n)?Yr(t,n).map(s=>s.join("")):[He(t,e)]}async function Jr(t,e,n={count:0}){if(n.count>ge)return[[]];let r=[[]];for(let s of e)if(s.type==="BraceExpansion"){let o=[],i=!1,a="";for(let u of s.items)if(u.type==="Range"){let d=be(u.start,u.end,u.step,u.startStr,u.endStr);if(d.expanded)for(let h of d.expanded)n.count++,o.push(h);else{i=!0,a=d.literal;break}}else{let d=await Jr(t,u.word.parts,n);for(let h of d)n.count++,o.push(h.join(""))}if(i){for(let u of r)n.count++,u.push(a);continue}if(r.length*o.length>Xr||n.count>ge)return r;let c=[];for(let u of r)for(let d of o){if(n.count++,n.count>ge)return c.length>0?c:r;c.push([...u,d])}r=c}else{let o=await Y(t,s);for(let i of r)n.count++,i.push(o)}return r}async function gs(t,e){let n=e.parts;return Ve(n)?(await Jr(t,n)).map(s=>s.join("")):[await v(t,e)]}async function ve(t,e){let n=e.parts,{hasQuoted:r,hasCommandSub:s,hasArrayVar:o,hasArrayAtExpansion:i,hasParamExpansion:a}=Fr(n),c=Ve(n)?Es(n)?await gs(t,e):ws(t,e):null;if(c&&c.length>1){let h=[];for(let p of c)if(!r&&/[*?[]/.test(p)){let g=await new we(t.fs,t.state.cwd).expand(p);g.length>0?h.push(...g):h.push(p)}else h.push(p);return{values:h,quoted:!1}}if(i&&n.length===1&&n[0].type==="DoubleQuoted"){let h=n[0];if(h.parts.length===1&&h.parts[0].type==="ParameterExpansion"){let y=h.parts[0].parameter.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@]\]$/);if(y){let g=y[1],m=T(t,g);if(m.length>0)return{values:m.map(([,w])=>w),quoted:!0};let E=t.state.env[g];return E!==void 0?{values:[E],quoted:!0}:{values:[],quoted:!0}}}}if(!r&&o&&n.length===1&&n[0].type==="ParameterExpansion"){let h=n[0].parameter;if(h==="@"||h==="*"){let p=Number.parseInt(t.state.env["#"]||"0",10);if(p===0)return{values:[],quoted:!1};let y=[];for(let g=1;g<=p;g++)y.push(t.state.env[String(g)]||"");return{values:y,quoted:!1}}}if(!r&&(s||o||a)&&!zr(t.state.env)){let h=Ge(t.state.env),p=Ne(h),y=await Vr(t,n,h,p,Y),g=[],m=new we(t.fs,t.state.cwd);for(let E of y)if(/[*?[]/.test(E)){let w=await m.expand(E);w.length>0?g.push(...w):g.push(E)}else g.push(E);return{values:g,quoted:!1}}let d=re(e)?await en(t,e):He(t,e);if(!r&&/[*?[]/.test(d)){let p=await new we(t.fs,t.state.cwd).expand(d);if(p.length>0)return{values:p,quoted:!1}}return d===""&&!r?{values:[],quoted:!1}:{values:[d],quoted:r}}async function en(t,e){let n=e.parts,r=n.length;if(r===1)return Y(t,n[0]);let s=[];for(let o=0;o<r;o++)s.push(await Y(t,n[o]));return s.join("")}async function Y(t,e){if(e.type==="ParameterExpansion"&&It(e))return As(t,e);let n=Kr(t,e);if(n!==null)return n;switch(e.type){case"DoubleQuoted":{let r=[];for(let s of e.parts)r.push(await Y(t,s));return r.join("")}case"CommandSubstitution":try{let r=await t.executeScript(e.body);return t.state.lastExitCode=r.exitCode,t.state.env["?"]=String(r.exitCode),r.stdout.replace(/\n+$/,"")}catch(r){if(r instanceof B)throw r;if(r instanceof R)return t.state.lastExitCode=r.exitCode,t.state.env["?"]=String(r.exitCode),r.stdout.replace(/\n+$/,"");throw r}case"ArithmeticExpansion":return String(await $(t,e.expression.expression));case"BraceExpansion":{let r=[];for(let s of e.items)if(s.type==="Range"){let o=be(s.start,s.end,s.step,s.startStr,s.endStr);if(o.expanded)r.push(...o.expanded);else return o.literal}else r.push(await v(t,s.word));return r.join(" ")}default:return""}}function tn(t,e,n=!1){let{parameter:r,operation:s}=e,o=s&&(s.type==="DefaultValue"||s.type==="AssignDefault"||s.type==="UseAlternative"||s.type==="ErrorIfUnset"),i=j(t,r,!o);if(!s)return i;let a=!(r in t.state.env),l=i==="";switch(s.type){case"DefaultValue":return(a||s.checkEmpty&&l)&&s.word?pe(t,s.word.parts,n):i;case"AssignDefault":{if((a||s.checkEmpty&&l)&&s.word){let u=pe(t,s.word.parts,n),d=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(d){let[,h,p]=d,y;if(/^\d+$/.test(p))y=Number.parseInt(p,10);else{try{let m=new M,E=L(m,p);y=N(t,E.expression)}catch{let m=t.state.env[p];y=m?Number.parseInt(m,10):0}Number.isNaN(y)&&(y=0)}t.state.env[`${h}_${y}`]=u;let g=Number.parseInt(t.state.env[`${h}__length`]||"0",10);y>=g&&(t.state.env[`${h}__length`]=String(y+1))}else t.state.env[r]=u;return u}return i}case"ErrorIfUnset":{if(a||s.checkEmpty&&l){let u=s.word?pe(t,s.word.parts,n):`${r}: parameter null or not set`;throw new R(1,"",`bash: ${u}
38
+ `)}return i}case"UseAlternative":return!(a||s.checkEmpty&&l)&&s.word?pe(t,s.word.parts,n):"";case"Length":{let c=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/);if(c){let u=T(t,c[1]);return String(u.length)}if(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(r)&&Hr(t,r)){let u=t.state.env[`${r}_0`]||"";return String(u.length)}return String(i.length)}case"LengthSliceError":throw new fe(r);case"Substring":{let c=s.offset?N(t,s.offset.expression):0,u=s.length?N(t,s.length.expression):void 0;if(r==="@"||r==="*"){let y=(t.state.env["@"]||"").split(" ").filter(w=>w),g=t.state.env[0]||"bash",m=c===0?[g,...y]:y,E=c===0?0:c-1;if(E<0||E>=m.length)return"";if(u!==void 0){let w=u<0?m.length+u:E+u;return m.slice(E,Math.max(E,w)).join(" ")}return m.slice(E).join(" ")}let d=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/);if(d){let g=T(t,d[1]).map(([,E])=>E),m=c;if(m<0&&(m=g.length+m,m<0))return"";if(u!==void 0){if(u<0){let E=g.length+u;return g.slice(m,Math.max(m,E)).join(" ")}return g.slice(m,m+u).join(" ")}return g.slice(m).join(" ")}let h=[...i],p=c;if(p<0&&(p=Math.max(0,h.length+p)),u!==void 0){if(u<0){let y=h.length+u;return h.slice(p,Math.max(p,y)).join("")}return h.slice(p,p+u).join("")}return h.slice(p).join("")}case"PatternRemoval":{let c="";if(s.pattern)for(let d of s.pattern.parts)if(d.type==="Glob")c+=G(d.pattern,s.greedy);else if(d.type==="Literal")c+=G(d.value,s.greedy);else if(d.type==="SingleQuoted"||d.type==="Escaped")c+=H(d.value);else if(d.type==="DoubleQuoted"){let h=pe(t,d.parts);c+=H(h)}else if(d.type==="ParameterExpansion"){let h=ne(t,d);c+=G(h,s.greedy)}else{let h=ne(t,d);c+=H(h)}if(s.side==="prefix")return i.replace(new RegExp(`^${c}`),"");let u=new RegExp(`${c}$`);if(s.greedy)return i.replace(u,"");for(let d=i.length;d>=0;d--){let h=i.slice(d);if(u.test(h))return i.slice(0,d)}return i}case"PatternReplacement":{let c="";if(s.pattern)for(let h of s.pattern.parts)if(h.type==="Glob")c+=G(h.pattern,!0);else if(h.type==="Literal")c+=G(h.value,!0);else if(h.type==="SingleQuoted"||h.type==="Escaped")c+=H(h.value);else if(h.type==="DoubleQuoted"){let p=pe(t,h.parts);c+=H(p)}else if(h.type==="ParameterExpansion"){let p=ne(t,h);c+=G(p,!0)}else{let p=ne(t,h);c+=H(p)}let u=s.replacement?pe(t,s.replacement.parts):"";if(c==="")return i;s.anchor==="start"?c=`^${c}`:s.anchor==="end"&&(c=`${c}$`);let d=s.all?"g":"";try{let h=new RegExp(c,d);if(s.all){let p="",y=0,g=h.exec(i);for(;g!==null&&!(g[0].length===0&&g.index===i.length);)p+=i.slice(y,g.index)+u,y=g.index+g[0].length,g[0].length===0&&y++,g=h.exec(i);return p+=i.slice(y),p}return i.replace(h,u)}catch{return i}}case"CaseModification":return s.direction==="upper"?s.all?i.toUpperCase():i.charAt(0).toUpperCase()+i.slice(1):s.all?i.toLowerCase():i.charAt(0).toLowerCase()+i.slice(1);case"Transform":{let c=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[[@*]\]$/);if(c&&s.operator==="Q")return T(t,c[1]).map(([,h])=>Dt(h)).join(" ");switch(s.operator){case"Q":return Dt(i);case"P":return i;case"a":return"";case"A":return`${r}=${Dt(i)}`;case"E":return i.replace(/\\([\\abefnrtv'"?])/g,(u,d)=>{switch(d){case"\\":return"\\";case"a":return"\x07";case"b":return"\b";case"e":return"\x1B";case"f":return"\f";case"n":return`
39
+ `;case"r":return"\r";case"t":return" ";case"v":return"\v";case"'":return"'";case'"':return'"';case"?":return"?";default:return d}});case"K":return"";default:return i}}case"Indirection":return j(t,i);case"ArrayKeys":{let u=T(t,s.array).map(([d])=>String(d));return s.star?u.join($e(t.state.env)):u.join(" ")}case"VarNamePrefix":{let c=Object.keys(t.state.env).filter(u=>u.startsWith(s.prefix)&&!u.includes("__")).sort();return s.star?c.join($e(t.state.env)):c.join(" ")}default:return i}}async function As(t,e,n=!1){let{parameter:r,operation:s}=e,o=s&&(s.type==="DefaultValue"||s.type==="AssignDefault"||s.type==="UseAlternative"||s.type==="ErrorIfUnset"),i=j(t,r,!o);if(!s)return i;let a=!(r in t.state.env),l=i==="";switch(s.type){case"DefaultValue":return(a||s.checkEmpty&&l)&&s.word?me(t,s.word.parts,n):i;case"AssignDefault":{if((a||s.checkEmpty&&l)&&s.word){let u=await me(t,s.word.parts,n),d=r.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(d){let[,h,p]=d,y;if(/^\d+$/.test(p))y=Number.parseInt(p,10);else{try{let m=new M,E=L(m,p);y=await $(t,E.expression)}catch{let m=t.state.env[p];y=m?Number.parseInt(m,10):0}Number.isNaN(y)&&(y=0)}t.state.env[`${h}_${y}`]=u;let g=Number.parseInt(t.state.env[`${h}__length`]||"0",10);y>=g&&(t.state.env[`${h}__length`]=String(y+1))}else t.state.env[r]=u;return u}return i}case"ErrorIfUnset":{if(a||s.checkEmpty&&l){let u=s.word?await me(t,s.word.parts,n):`${r}: parameter null or not set`;throw new R(1,"",`bash: ${u}
40
+ `)}return i}case"UseAlternative":return!(a||s.checkEmpty&&l)&&s.word?me(t,s.word.parts,n):"";case"PatternRemoval":{let c="";if(s.pattern)for(let d of s.pattern.parts)if(d.type==="Glob")c+=G(d.pattern,s.greedy);else if(d.type==="Literal")c+=G(d.value,s.greedy);else if(d.type==="SingleQuoted"||d.type==="Escaped")c+=H(d.value);else if(d.type==="DoubleQuoted"){let h=await me(t,d.parts);c+=H(h)}else if(d.type==="ParameterExpansion"){let h=await Y(t,d);c+=G(h,s.greedy)}else{let h=await Y(t,d);c+=H(h)}if(s.side==="prefix")return i.replace(new RegExp(`^${c}`),"");let u=new RegExp(`${c}$`);if(s.greedy)return i.replace(u,"");for(let d=i.length;d>=0;d--){let h=i.slice(d);if(u.test(h))return i.slice(0,d)}return i}case"PatternReplacement":{let c="";if(s.pattern)for(let h of s.pattern.parts)if(h.type==="Glob")c+=G(h.pattern,!0);else if(h.type==="Literal")c+=G(h.value,!0);else if(h.type==="SingleQuoted"||h.type==="Escaped")c+=H(h.value);else if(h.type==="DoubleQuoted"){let p=await me(t,h.parts);c+=H(p)}else if(h.type==="ParameterExpansion"){let p=await Y(t,h);c+=G(p,!0)}else{let p=await Y(t,h);c+=H(p)}let u=s.replacement?await me(t,s.replacement.parts):"";if(c==="")return i;s.anchor==="start"?c=`^${c}`:s.anchor==="end"&&(c=`${c}$`);let d=s.all?"g":"";try{let h=new RegExp(c,d);if(s.all){let p="",y=0,g=h.exec(i);for(;g!==null&&!(g[0].length===0&&g.index===i.length);)p+=i.slice(y,g.index)+u,y=g.index+g[0].length,g[0].length===0&&y++,g=h.exec(i);return p+=i.slice(y),p}return i.replace(h,u)}catch{return i}}default:return tn(t,e,n)}}function _t(t,e,n){switch(n){case"+":return t+e;case"-":return t-e;case"*":return t*e;case"/":return e!==0?Math.trunc(t/e):0;case"%":return e!==0?t%e:0;case"**":if(e<0)throw new W("exponent less than 0");return t**e;case"<<":return t<<e;case">>":return t>>e;case"<":return t<e?1:0;case"<=":return t<=e?1:0;case">":return t>e?1:0;case">=":return t>=e?1:0;case"==":return t===e?1:0;case"!=":return t!==e?1:0;case"&":return t&e;case"|":return t|e;case"^":return t^e;case",":return e;default:return 0}}function rn(t,e,n){switch(n){case"=":return e;case"+=":return t+e;case"-=":return t-e;case"*=":return t*e;case"/=":return e!==0?Math.trunc(t/e):0;case"%=":return e!==0?t%e:0;case"<<=":return t<<e;case">>=":return t>>e;case"&=":return t&e;case"|=":return t|e;case"^=":return t^e;default:return e}}function Ot(t,e){switch(e){case"-":return-t;case"+":return+t;case"!":return t===0?1:0;case"~":return~t;default:return t}}function xs(t,e){let n=t.state.env[e];if(n!==void 0)return n;let r=t.state.env[`${e}_0`];return r!==void 0?r:j(t,e)}function Ae(t){if(!t)return 0;let e=Number.parseInt(t,10);if(!Number.isNaN(e)&&/^-?\d+$/.test(t.trim()))return e;let n=t.trim();if(!n)return 0;try{let r=new M,{expr:s,pos:o}=ee(r,n,0);if(o<n.length){let i=n.slice(o).trim().split(/\s+/)[0];throw new W(`${n}: syntax error in expression (error token is "${i}")`)}return s.type==="ArithNumber"?s.value:e||0}catch(r){if(r instanceof W)throw r;let s=n.split(/\s+/).slice(1)[0]||n;throw new W(`${n}: syntax error in expression (error token is "${s}")`)}}function Ze(t,e,n=new Set){if(n.has(e))return 0;n.add(e);let r=xs(t,e);if(!r)return 0;let s=Number.parseInt(r,10);if(!Number.isNaN(s)&&/^-?\d+$/.test(r.trim()))return s;let o=r.trim();if(/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(o))return Ze(t,o,n);try{let i=new M,{expr:a}=ee(i,o,0);return q(t,a,n)}catch{return 0}}function q(t,e,n){switch(e.type){case"ArithNumber":if(Number.isNaN(e.value))throw new W("value too great for base");return e.value;case"ArithVariable":return Ze(t,e.name,n);case"ArithBinary":{if(e.operator==="||")return q(t,e.left,n)||q(t,e.right,n)?1:0;if(e.operator==="&&")return q(t,e.left,n)&&q(t,e.right,n)?1:0;let r=q(t,e.left,n),s=q(t,e.right,n);return _t(r,s,e.operator)}case"ArithUnary":{let r=q(t,e.operand,n);return Ot(r,e.operator)}case"ArithTernary":return q(t,e.condition,n)?q(t,e.consequent,n):q(t,e.alternate,n);case"ArithGroup":return q(t,e.expression,n);default:return N(t,e)}}function ae(t,e){if(e.startsWith("#")){let d=e.slice(1),h=t.state.env[d]||"";return String(h.length)}if(e.startsWith("!")){let d=e.slice(1),h=t.state.env[d]||"";return t.state.env[h]||""}let n=[":-",":=",":?",":+","-","=","?","+"],r=-1,s="";for(let d of n){let h=e.indexOf(d);h>0&&(r===-1||h<r)&&(r=h,s=d)}if(r===-1)return j(t,e);let o=e.slice(0,r),i=e.slice(r+s.length),a=t.state.env[o],l=a===void 0,c=a==="",u=s.startsWith(":");switch(s){case":-":case"-":return l||u&&c?i:a||"";case":=":case"=":return l||u&&c?(t.state.env[o]=i,i):a||"";case":+":case"+":return!(l||u&&c)?i:"";case":?":case"?":{if(l||u&&c)throw new Error(i||`${o}: parameter null or not set`);return a||""}default:return a||""}}function N(t,e){switch(e.type){case"ArithNumber":if(Number.isNaN(e.value))throw new W("value too great for base");return e.value;case"ArithVariable":return Ze(t,e.name);case"ArithNested":return N(t,e.expression);case"ArithCommandSubst":return 0;case"ArithBracedExpansion":{let n=ae(t,e.content);return Number.parseInt(n,10)||0}case"ArithDynamicBase":{let n=ae(t,e.baseExpr),r=Number.parseInt(n,10);if(r<2||r>64)return 0;let s=`${r}#${e.value}`;return Ee(s)}case"ArithDynamicNumber":{let r=ae(t,e.prefix)+e.suffix;return Ee(r)}case"ArithArrayElement":{let n=t.state.associativeArrays?.has(e.array),r;if(e.stringKey!==void 0)r=`${e.array}_${e.stringKey}`;else if(n&&e.index?.type==="ArithVariable")r=`${e.array}_${e.index.name}`;else if(e.index){let o=N(t,e.index);if(o<0){let a=T(t,e.array);if(a.length===0)return t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${e.array}: bad array subscript
41
41
  `,0;let c=Math.max(...a.map(([u])=>typeof u=="number"?u:0))+1+o;if(c<0)return t.state.expansionStderr=(t.state.expansionStderr||"")+`bash: ${e.array}: bad array subscript
42
- `,0;o=c}r=`${e.array}_${o}`;let i=t.state.env[r];if(i!==void 0)return ge(i);if(o===0){let a=t.state.env[e.array];if(a!==void 0)return ge(a)}if(t.state.options.nounset&&!Object.keys(t.state.env).some(l=>l===e.array||l.startsWith(`${e.array}_`)))throw new X(`${e.array}[${o}]`);return 0}else return 0;let s=t.state.env[r];return s!==void 0?ge(s):0}case"ArithDoubleSubscript":throw new W("double subscript","","");case"ArithNumberSubscript":throw new W(`${e.number}${e.errorToken}: syntax error: invalid arithmetic operator (error token is "${e.errorToken}")`);case"ArithBinary":{if(e.operator==="||")return N(t,e.left)||N(t,e.right)?1:0;if(e.operator==="&&")return N(t,e.left)&&N(t,e.right)?1:0;let n=N(t,e.left),r=N(t,e.right);return Dt(n,r,e.operator)}case"ArithUnary":{let n=N(t,e.operand);if(e.operator==="++"||e.operator==="--"){if(e.operand.type==="ArithVariable"){let r=e.operand.name,s=Number.parseInt(j(t,r),10)||0,o=e.operator==="++"?s+1:s-1;return t.state.env[r]=String(o),e.prefix?o:s}if(e.operand.type==="ArithArrayElement"){let r=e.operand.array,s=t.state.associativeArrays?.has(r),o;if(e.operand.stringKey!==void 0)o=`${r}_${e.operand.stringKey}`;else if(s&&e.operand.index?.type==="ArithVariable")o=`${r}_${e.operand.index.name}`;else if(e.operand.index){let l=N(t,e.operand.index);o=`${r}_${l}`}else return n;let i=Number.parseInt(t.state.env[o]||"0",10)||0,a=e.operator==="++"?i+1:i-1;return t.state.env[o]=String(a),e.prefix?a:i}return n}return _t(n,e.operator)}case"ArithTernary":return N(t,e.condition)?N(t,e.consequent):N(t,e.alternate);case"ArithAssignment":{let n=e.variable,r=n;if(e.stringKey!==void 0)r=`${n}_${e.stringKey}`;else if(e.subscript){let a=t.state.associativeArrays?.has(n);if(a&&e.subscript.type==="ArithVariable")r=`${n}_${e.subscript.name}`;else if(a){let l=N(t,e.subscript);r=`${n}_${l}`}else{let l=N(t,e.subscript);if(l<0){let c=T(t,n);c.length>0&&(l=Math.max(...c.map(([d])=>typeof d=="number"?d:0))+1+l)}r=`${n}_${l}`}}let s=Number.parseInt(t.state.env[r]||"0",10)||0,o=N(t,e.value),i=Jr(s,o,e.operator);return t.state.env[r]=String(i),i}case"ArithGroup":return N(t,e.expression);case"ArithConcat":{let n="";for(let r of e.parts)n+=en(t,r);return Number.parseInt(n,10)||0}default:return 0}}function en(t,e){switch(e.type){case"ArithNumber":return String(e.value);case"ArithVariable":return j(t,e.name);case"ArithBracedExpansion":return ae(t,e.content);case"ArithCommandSubst":return"0";case"ArithConcat":{let n="";for(let r of e.parts)n+=en(t,r);return n}default:return String(N(t,e))}}async function $(t,e){switch(e.type){case"ArithNumber":if(Number.isNaN(e.value))throw new W("value too great for base");return e.value;case"ArithVariable":return Ve(t,e.name);case"ArithNested":return $(t,e.expression);case"ArithCommandSubst":{if(t.execFn){let r=(await t.execFn(e.command)).stdout.trim();return Number.parseInt(r,10)||0}return 0}case"ArithBracedExpansion":{let n=ae(t,e.content);return Number.parseInt(n,10)||0}case"ArithDynamicBase":{let n=ae(t,e.baseExpr),r=Number.parseInt(n,10);if(r<2||r>64)return 0;let s=`${r}#${e.value}`;return Ee(s)}case"ArithDynamicNumber":{let r=ae(t,e.prefix)+e.suffix;return Ee(r)}case"ArithArrayElement":{let n=t.state.associativeArrays?.has(e.array),r;if(e.stringKey!==void 0)r=`${e.array}_${e.stringKey}`;else if(n&&e.index?.type==="ArithVariable")r=`${e.array}_${e.index.name}`;else if(e.index){let o=await $(t,e.index);r=`${e.array}_${o}`;let i=t.state.env[r];if(i!==void 0)return ge(i);if(o===0){let a=t.state.env[e.array];if(a!==void 0)return ge(a)}if(t.state.options.nounset&&!Object.keys(t.state.env).some(l=>l===e.array||l.startsWith(`${e.array}_`)))throw new X(`${e.array}[${o}]`);return 0}else return 0;let s=t.state.env[r];return s!==void 0?ge(s):0}case"ArithDoubleSubscript":throw new W("double subscript","","");case"ArithNumberSubscript":throw new W(`${e.number}${e.errorToken}: syntax error: invalid arithmetic operator (error token is "${e.errorToken}")`);case"ArithBinary":{if(e.operator==="||")return await $(t,e.left)||await $(t,e.right)?1:0;if(e.operator==="&&")return await $(t,e.left)&&await $(t,e.right)?1:0;let n=await $(t,e.left),r=await $(t,e.right);return Dt(n,r,e.operator)}case"ArithUnary":{let n=await $(t,e.operand);if(e.operator==="++"||e.operator==="--"){if(e.operand.type==="ArithVariable"){let r=e.operand.name,s=Number.parseInt(j(t,r),10)||0,o=e.operator==="++"?s+1:s-1;return t.state.env[r]=String(o),e.prefix?o:s}if(e.operand.type==="ArithArrayElement"){let r=e.operand.array,s=t.state.associativeArrays?.has(r),o;if(e.operand.stringKey!==void 0)o=`${r}_${e.operand.stringKey}`;else if(s&&e.operand.index?.type==="ArithVariable")o=`${r}_${e.operand.index.name}`;else if(e.operand.index){let l=await $(t,e.operand.index);o=`${r}_${l}`}else return n;let i=Number.parseInt(t.state.env[o]||"0",10)||0,a=e.operator==="++"?i+1:i-1;return t.state.env[o]=String(a),e.prefix?a:i}return n}return _t(n,e.operator)}case"ArithTernary":return await $(t,e.condition)?await $(t,e.consequent):await $(t,e.alternate);case"ArithAssignment":{let n=e.variable,r=n;if(e.stringKey!==void 0)r=`${n}_${e.stringKey}`;else if(e.subscript){let a=t.state.associativeArrays?.has(n);if(a&&e.subscript.type==="ArithVariable")r=`${n}_${e.subscript.name}`;else if(a){let l=await $(t,e.subscript);r=`${n}_${l}`}else{let l=await $(t,e.subscript);if(l<0){let c=T(t,n);c.length>0&&(l=Math.max(...c.map(([d])=>typeof d=="number"?d:0))+1+l)}r=`${n}_${l}`}}let s=Number.parseInt(t.state.env[r]||"0",10)||0,o=await $(t,e.value),i=Jr(s,o,e.operator);return t.state.env[r]=String(i),i}case"ArithGroup":return await $(t,e.expression);case"ArithConcat":{let n="";for(let r of e.parts)n+=await tn(t,r);return Number.parseInt(n,10)||0}default:return 0}}async function tn(t,e){switch(e.type){case"ArithNumber":return String(e.value);case"ArithVariable":return j(t,e.name);case"ArithBracedExpansion":return ae(t,e.content);case"ArithCommandSubst":return t.execFn?(await t.execFn(e.command)).stdout.trim():"0";case"ArithConcat":{let n="";for(let r of e.parts)n+=await tn(t,r);return n}default:return String(await $(t,e))}}var C=Object.freeze({stdout:"",stderr:"",exitCode:0});function V(t=""){return{stdout:t,stderr:"",exitCode:0}}function S(t,e=1){return{stdout:"",stderr:t,exitCode:e}}function x(t,e,n){return{stdout:t,stderr:e,exitCode:n}}function _(t){return{stdout:"",stderr:"",exitCode:t?0:1}}function se(t,e,n="",r=""){throw new B(t,e,n,r)}function Ot(t,e){if(t.state.loopDepth===0)return C;let n=1;if(e.length>0){let r=Number.parseInt(e[0],10);if(Number.isNaN(r)||r<1)throw new R(128,"",`bash: break: ${e[0]}: numeric argument required
43
- `);n=r}throw new z(n)}async function Lt(t,e){let n,r=!1,s=!1,o=0;for(;o<e.length;)if(e[o]==="--"){o++;break}else if(e[o]==="-L")s=!1,o++;else if(e[o]==="-P")s=!0,o++;else if(e[o].startsWith("-")&&e[o]!=="-")o++;else break;let i=e.slice(o);if(i.length===0||i[0]==="~"?n=t.state.env.HOME||"/":i[0]==="-"?(n=t.state.previousDir,r=!0):n=i[0],!n.startsWith("/")&&!n.startsWith("./")&&!n.startsWith("../")&&n!=="."&&n!==".."){let d=t.state.env.CDPATH;if(d){let h=d.split(":").filter(p=>p);for(let p of h){let y=p.startsWith("/")?`${p}/${n}`:`${t.state.cwd}/${p}/${n}`;try{if((await t.fs.stat(y)).isDirectory){n=y,r=!0;break}}catch{}}}}let l=(n.startsWith("/")?n:`${t.state.cwd}/${n}`).split("/").filter(d=>d&&d!=="."),c="";for(let d of l)if(d==="..")c=c.split("/").slice(0,-1).join("/")||"/";else{c=c?`${c}/${d}`:`/${d}`;try{if(!(await t.fs.stat(c)).isDirectory)return S(`bash: cd: ${n}: Not a directory
42
+ `,0;o=c}r=`${e.array}_${o}`;let i=t.state.env[r];if(i!==void 0)return Ae(i);if(o===0){let a=t.state.env[e.array];if(a!==void 0)return Ae(a)}if(t.state.options.nounset&&!Object.keys(t.state.env).some(l=>l===e.array||l.startsWith(`${e.array}_`)))throw new X(`${e.array}[${o}]`);return 0}else return 0;let s=t.state.env[r];return s!==void 0?Ae(s):0}case"ArithDoubleSubscript":throw new W("double subscript","","");case"ArithNumberSubscript":throw new W(`${e.number}${e.errorToken}: syntax error: invalid arithmetic operator (error token is "${e.errorToken}")`);case"ArithBinary":{if(e.operator==="||")return N(t,e.left)||N(t,e.right)?1:0;if(e.operator==="&&")return N(t,e.left)&&N(t,e.right)?1:0;let n=N(t,e.left),r=N(t,e.right);return _t(n,r,e.operator)}case"ArithUnary":{let n=N(t,e.operand);if(e.operator==="++"||e.operator==="--"){if(e.operand.type==="ArithVariable"){let r=e.operand.name,s=Number.parseInt(j(t,r),10)||0,o=e.operator==="++"?s+1:s-1;return t.state.env[r]=String(o),e.prefix?o:s}if(e.operand.type==="ArithArrayElement"){let r=e.operand.array,s=t.state.associativeArrays?.has(r),o;if(e.operand.stringKey!==void 0)o=`${r}_${e.operand.stringKey}`;else if(s&&e.operand.index?.type==="ArithVariable")o=`${r}_${e.operand.index.name}`;else if(e.operand.index){let l=N(t,e.operand.index);o=`${r}_${l}`}else return n;let i=Number.parseInt(t.state.env[o]||"0",10)||0,a=e.operator==="++"?i+1:i-1;return t.state.env[o]=String(a),e.prefix?a:i}return n}return Ot(n,e.operator)}case"ArithTernary":return N(t,e.condition)?N(t,e.consequent):N(t,e.alternate);case"ArithAssignment":{let n=e.variable,r=n;if(e.stringKey!==void 0)r=`${n}_${e.stringKey}`;else if(e.subscript){let a=t.state.associativeArrays?.has(n);if(a&&e.subscript.type==="ArithVariable")r=`${n}_${e.subscript.name}`;else if(a){let l=N(t,e.subscript);r=`${n}_${l}`}else{let l=N(t,e.subscript);if(l<0){let c=T(t,n);c.length>0&&(l=Math.max(...c.map(([d])=>typeof d=="number"?d:0))+1+l)}r=`${n}_${l}`}}let s=Number.parseInt(t.state.env[r]||"0",10)||0,o=N(t,e.value),i=rn(s,o,e.operator);return t.state.env[r]=String(i),i}case"ArithGroup":return N(t,e.expression);case"ArithConcat":{let n="";for(let r of e.parts)n+=nn(t,r);return Number.parseInt(n,10)||0}default:return 0}}function nn(t,e){switch(e.type){case"ArithNumber":return String(e.value);case"ArithVariable":return j(t,e.name);case"ArithBracedExpansion":return ae(t,e.content);case"ArithCommandSubst":return"0";case"ArithConcat":{let n="";for(let r of e.parts)n+=nn(t,r);return n}default:return String(N(t,e))}}async function $(t,e){switch(e.type){case"ArithNumber":if(Number.isNaN(e.value))throw new W("value too great for base");return e.value;case"ArithVariable":return Ze(t,e.name);case"ArithNested":return $(t,e.expression);case"ArithCommandSubst":{if(t.execFn){let r=(await t.execFn(e.command)).stdout.trim();return Number.parseInt(r,10)||0}return 0}case"ArithBracedExpansion":{let n=ae(t,e.content);return Number.parseInt(n,10)||0}case"ArithDynamicBase":{let n=ae(t,e.baseExpr),r=Number.parseInt(n,10);if(r<2||r>64)return 0;let s=`${r}#${e.value}`;return Ee(s)}case"ArithDynamicNumber":{let r=ae(t,e.prefix)+e.suffix;return Ee(r)}case"ArithArrayElement":{let n=t.state.associativeArrays?.has(e.array),r;if(e.stringKey!==void 0)r=`${e.array}_${e.stringKey}`;else if(n&&e.index?.type==="ArithVariable")r=`${e.array}_${e.index.name}`;else if(e.index){let o=await $(t,e.index);r=`${e.array}_${o}`;let i=t.state.env[r];if(i!==void 0)return Ae(i);if(o===0){let a=t.state.env[e.array];if(a!==void 0)return Ae(a)}if(t.state.options.nounset&&!Object.keys(t.state.env).some(l=>l===e.array||l.startsWith(`${e.array}_`)))throw new X(`${e.array}[${o}]`);return 0}else return 0;let s=t.state.env[r];return s!==void 0?Ae(s):0}case"ArithDoubleSubscript":throw new W("double subscript","","");case"ArithNumberSubscript":throw new W(`${e.number}${e.errorToken}: syntax error: invalid arithmetic operator (error token is "${e.errorToken}")`);case"ArithBinary":{if(e.operator==="||")return await $(t,e.left)||await $(t,e.right)?1:0;if(e.operator==="&&")return await $(t,e.left)&&await $(t,e.right)?1:0;let n=await $(t,e.left),r=await $(t,e.right);return _t(n,r,e.operator)}case"ArithUnary":{let n=await $(t,e.operand);if(e.operator==="++"||e.operator==="--"){if(e.operand.type==="ArithVariable"){let r=e.operand.name,s=Number.parseInt(j(t,r),10)||0,o=e.operator==="++"?s+1:s-1;return t.state.env[r]=String(o),e.prefix?o:s}if(e.operand.type==="ArithArrayElement"){let r=e.operand.array,s=t.state.associativeArrays?.has(r),o;if(e.operand.stringKey!==void 0)o=`${r}_${e.operand.stringKey}`;else if(s&&e.operand.index?.type==="ArithVariable")o=`${r}_${e.operand.index.name}`;else if(e.operand.index){let l=await $(t,e.operand.index);o=`${r}_${l}`}else return n;let i=Number.parseInt(t.state.env[o]||"0",10)||0,a=e.operator==="++"?i+1:i-1;return t.state.env[o]=String(a),e.prefix?a:i}return n}return Ot(n,e.operator)}case"ArithTernary":return await $(t,e.condition)?await $(t,e.consequent):await $(t,e.alternate);case"ArithAssignment":{let n=e.variable,r=n;if(e.stringKey!==void 0)r=`${n}_${e.stringKey}`;else if(e.subscript){let a=t.state.associativeArrays?.has(n);if(a&&e.subscript.type==="ArithVariable")r=`${n}_${e.subscript.name}`;else if(a){let l=await $(t,e.subscript);r=`${n}_${l}`}else{let l=await $(t,e.subscript);if(l<0){let c=T(t,n);c.length>0&&(l=Math.max(...c.map(([d])=>typeof d=="number"?d:0))+1+l)}r=`${n}_${l}`}}let s=Number.parseInt(t.state.env[r]||"0",10)||0,o=await $(t,e.value),i=rn(s,o,e.operator);return t.state.env[r]=String(i),i}case"ArithGroup":return await $(t,e.expression);case"ArithConcat":{let n="";for(let r of e.parts)n+=await sn(t,r);return Number.parseInt(n,10)||0}default:return 0}}async function sn(t,e){switch(e.type){case"ArithNumber":return String(e.value);case"ArithVariable":return j(t,e.name);case"ArithBracedExpansion":return ae(t,e.content);case"ArithCommandSubst":return t.execFn?(await t.execFn(e.command)).stdout.trim():"0";case"ArithConcat":{let n="";for(let r of e.parts)n+=await sn(t,r);return n}default:return String(await $(t,e))}}var C=Object.freeze({stdout:"",stderr:"",exitCode:0});function V(t=""){return{stdout:t,stderr:"",exitCode:0}}function S(t,e=1){return{stdout:"",stderr:t,exitCode:e}}function x(t,e,n){return{stdout:t,stderr:e,exitCode:n}}function _(t){return{stdout:"",stderr:"",exitCode:t?0:1}}function se(t,e,n="",r=""){throw new B(t,e,n,r)}function Lt(t,e){if(t.state.loopDepth===0)return C;let n=1;if(e.length>0){let r=Number.parseInt(e[0],10);if(Number.isNaN(r)||r<1)throw new R(128,"",`bash: break: ${e[0]}: numeric argument required
43
+ `);n=r}throw new z(n)}async function Tt(t,e){let n,r=!1,s=!1,o=0;for(;o<e.length;)if(e[o]==="--"){o++;break}else if(e[o]==="-L")s=!1,o++;else if(e[o]==="-P")s=!0,o++;else if(e[o].startsWith("-")&&e[o]!=="-")o++;else break;let i=e.slice(o);if(i.length===0||i[0]==="~"?n=t.state.env.HOME||"/":i[0]==="-"?(n=t.state.previousDir,r=!0):n=i[0],!n.startsWith("/")&&!n.startsWith("./")&&!n.startsWith("../")&&n!=="."&&n!==".."){let d=t.state.env.CDPATH;if(d){let h=d.split(":").filter(p=>p);for(let p of h){let y=p.startsWith("/")?`${p}/${n}`:`${t.state.cwd}/${p}/${n}`;try{if((await t.fs.stat(y)).isDirectory){n=y,r=!0;break}}catch{}}}}let l=(n.startsWith("/")?n:`${t.state.cwd}/${n}`).split("/").filter(d=>d&&d!=="."),c="";for(let d of l)if(d==="..")c=c.split("/").slice(0,-1).join("/")||"/";else{c=c?`${c}/${d}`:`/${d}`;try{if(!(await t.fs.stat(c)).isDirectory)return S(`bash: cd: ${n}: Not a directory
44
44
  `)}catch{return S(`bash: cd: ${n}: No such file or directory
45
45
  `)}}let u=c||"/";return t.state.previousDir=t.state.cwd,t.state.cwd=u,t.state.env.PWD=t.state.cwd,t.state.env.OLDPWD=t.state.previousDir,V(r?`${u}
46
- `:"")}function Tt(t,e){if(t.state.loopDepth===0)return C;if(e.length>1)throw new z(1);let n=1;if(e.length>0){let r=Number.parseInt(e[0],10);if(Number.isNaN(r)||r<1)return S(`bash: continue: ${e[0]}: numeric argument required
47
- `);n=r}throw new Q(n)}function ye(t,e){t.state.readonlyVars=t.state.readonlyVars||new Set,t.state.readonlyVars.add(e)}function As(t,e){return t.state.readonlyVars?.has(e)??!1}function ce(t,e,n="bash"){return As(t,e)?S(`${n}: ${e}: readonly variable
48
- `):null}function rn(t){let e=t.match(/^([a-zA-Z_][a-zA-Z0-9_]*)=\((.*)\)$/s);if(e)return{name:e[1],isArray:!0,arrayElements:Wt(e[2])};if(t.includes("=")){let n=t.indexOf("=");return{name:t.slice(0,n),isArray:!1,value:t.slice(n+1)}}return{name:t,isArray:!1}}function nn(t,e,n={}){let{name:r,isArray:s,arrayElements:o,value:i}=e,{makeReadonly:a=!1,checkReadonly:l=!0}=n;if(l){let c=ce(t,r);if(c)return c}if(s&&o){for(let c=0;c<o.length;c++)t.state.env[`${r}_${c}`]=o[c];t.state.env[`${r}__length`]=String(o.length)}else i!==void 0&&(t.state.env[r]=i);return a&&ye(t,r),null}function Mt(t,e){let n=!1,r=!1,s=!1,o=!1,i=!1,a=[];for(let l=0;l<e.length;l++){let c=e[l];if(c==="-a")n=!0;else if(c==="-A")r=!0;else if(c==="-r")s=!0;else if(c==="-x")o=!0;else if(c==="-p")i=!0;else if(c==="--"){a.push(...e.slice(l+1));break}else if(c.startsWith("-"))for(let u of c.slice(1))u==="a"?n=!0:u==="A"?r=!0:u==="r"?s=!0:u==="x"?o=!0:u==="p"&&(i=!0);else a.push(c)}if(i&&a.length>0){let l="";for(let c of a){let u=t.state.env[c];if(u!==void 0){let d=u.replace(/\\/g,"\\\\").replace(/"/g,'\\"');l+=`declare -- ${c}="${d}"
46
+ `:"")}function Wt(t,e){if(t.state.loopDepth===0)return C;if(e.length>1)throw new z(1);let n=1;if(e.length>0){let r=Number.parseInt(e[0],10);if(Number.isNaN(r)||r<1)return S(`bash: continue: ${e[0]}: numeric argument required
47
+ `);n=r}throw new Q(n)}function ye(t,e){t.state.readonlyVars=t.state.readonlyVars||new Set,t.state.readonlyVars.add(e)}function Ss(t,e){return t.state.readonlyVars?.has(e)??!1}function ce(t,e,n="bash"){return Ss(t,e)?S(`${n}: ${e}: readonly variable
48
+ `):null}function on(t){let e=t.match(/^([a-zA-Z_][a-zA-Z0-9_]*)=\((.*)\)$/s);if(e)return{name:e[1],isArray:!0,arrayElements:Mt(e[2])};if(t.includes("=")){let n=t.indexOf("=");return{name:t.slice(0,n),isArray:!1,value:t.slice(n+1)}}return{name:t,isArray:!1}}function an(t,e,n={}){let{name:r,isArray:s,arrayElements:o,value:i}=e,{makeReadonly:a=!1,checkReadonly:l=!0}=n;if(l){let c=ce(t,r);if(c)return c}if(s&&o){for(let c=0;c<o.length;c++)t.state.env[`${r}_${c}`]=o[c];t.state.env[`${r}__length`]=String(o.length)}else i!==void 0&&(t.state.env[r]=i);return a&&ye(t,r),null}function Ft(t,e){let n=!1,r=!1,s=!1,o=!1,i=!1,a=[];for(let l=0;l<e.length;l++){let c=e[l];if(c==="-a")n=!0;else if(c==="-A")r=!0;else if(c==="-r")s=!0;else if(c==="-x")o=!0;else if(c==="-p")i=!0;else if(c==="--"){a.push(...e.slice(l+1));break}else if(c.startsWith("-"))for(let u of c.slice(1))u==="a"?n=!0:u==="A"?r=!0:u==="r"?s=!0:u==="x"?o=!0:u==="p"&&(i=!0);else a.push(c)}if(i&&a.length>0){let l="";for(let c of a){let u=t.state.env[c];if(u!==void 0){let d=u.replace(/\\/g,"\\\\").replace(/"/g,'\\"');l+=`declare -- ${c}="${d}"
49
49
  `}}return V(l)}if(a.length===0&&!i){let l="",c=Object.entries(t.state.env).filter(([u])=>!u.startsWith("BASH_")).sort(([u],[d])=>u.localeCompare(d));for(let[u,d]of c){let h=d.replace(/'/g,"'\\''");l+=`declare -- ${u}='${h}'
50
- `}return V(l)}for(let l of a){let c=l.match(/^([a-zA-Z_][a-zA-Z0-9_]*)=\((.*)\)$/s);if(c){let u=c[1],d=c[2];if(r&&(t.state.associativeArrays??=new Set,t.state.associativeArrays.add(u)),r&&d.includes("[")){let h=gs(d);for(let[p,y]of h)t.state.env[`${u}_${p}`]=y}else{let h=Wt(d);for(let p=0;p<h.length;p++)t.state.env[`${u}_${p}`]=h[p];t.state.env[`${u}__length`]=String(h.length)}s&&ye(t,u);continue}if(l.includes("=")){let u=l.indexOf("="),d=l.slice(0,u),h=l.slice(u+1),p=ce(t,d);if(p)return p;t.state.env[d]=h,s&&ye(t,d)}else{let u=l;r&&(t.state.associativeArrays??=new Set,t.state.associativeArrays.add(u));let d=Object.keys(t.state.env).some(h=>h.startsWith(`${u}_`)&&!h.startsWith(`${u}__length`));!(u in t.state.env)&&!d&&(n||r?t.state.env[`${u}__length`]="0":t.state.env[u]=""),s&&ye(t,u)}}return C}function Wt(t){let e=[],n="",r=!1,s=!1,o=!1;for(let i of t){if(o){n+=i,o=!1;continue}if(i==="\\"){o=!0;continue}if(i==="'"&&!s){r=!r;continue}if(i==='"'&&!r){s=!s;continue}if((i===" "||i===" "||i===`
51
- `)&&!r&&!s){n&&(e.push(n),n="");continue}n+=i}return n&&e.push(n),e}function gs(t){let e=[],n=0;for(;n<t.length;){for(;n<t.length&&/\s/.test(t[n]);)n++;if(n>=t.length)break;if(t[n]!=="["){n++;continue}n++;let r="";if(t[n]==="'"||t[n]==='"'){let o=t[n];for(n++;n<t.length&&t[n]!==o;)r+=t[n],n++;t[n]===o&&n++}else for(;n<t.length&&t[n]!=="]"&&t[n]!=="=";)r+=t[n],n++;for(;n<t.length&&t[n]!=="]";)n++;if(t[n]==="]"&&n++,t[n]!=="=")continue;n++;let s="";if(t[n]==="'"||t[n]==='"'){let o=t[n];for(n++;n<t.length&&t[n]!==o;)t[n]==="\\"&&n+1<t.length&&n++,s+=t[n],n++;t[n]===o&&n++}else for(;n<t.length&&!/\s/.test(t[n]);)s+=t[n],n++;e.push([r,s])}return e}function Ft(t,e){let n=!1,r=!1,s=[];for(let o=0;o<e.length;o++){let i=e[o];if(i==="-a")n=!0;else if(i==="-A")r=!0;else if(i==="-p"){if(e.length===1){let a="";for(let l of t.state.readonlyVars||[]){let c=t.state.env[l];c!==void 0&&(a+=`declare -r ${l}="${c}"
52
- `)}return V(a)}}else if(i==="--"){s.push(...e.slice(o+1));break}else i.startsWith("-")||s.push(i)}for(let o of s){let i=rn(o);if(i.value===void 0&&!i.isArray){ye(t,i.name);continue}let a=nn(t,i,{makeReadonly:!0});if(a)return a}return C}async function Bt(t,e){let n=e;if(n.length>0){let s=n[0];if(s==="--")n=n.slice(1);else if(s.startsWith("-")&&s!=="-"&&s.length>1)return S(`bash: eval: ${s}: invalid option
50
+ `}return V(l)}for(let l of a){let c=l.match(/^([a-zA-Z_][a-zA-Z0-9_]*)=\((.*)\)$/s);if(c){let u=c[1],d=c[2];if(r&&(t.state.associativeArrays??=new Set,t.state.associativeArrays.add(u)),r&&d.includes("[")){let h=bs(d);for(let[p,y]of h)t.state.env[`${u}_${p}`]=y}else{let h=Mt(d);for(let p=0;p<h.length;p++)t.state.env[`${u}_${p}`]=h[p];t.state.env[`${u}__length`]=String(h.length)}s&&ye(t,u);continue}if(l.includes("=")){let u=l.indexOf("="),d=l.slice(0,u),h=l.slice(u+1),p=ce(t,d);if(p)return p;t.state.env[d]=h,s&&ye(t,d)}else{let u=l;r&&(t.state.associativeArrays??=new Set,t.state.associativeArrays.add(u));let d=Object.keys(t.state.env).some(h=>h.startsWith(`${u}_`)&&!h.startsWith(`${u}__length`));!(u in t.state.env)&&!d&&(n||r?t.state.env[`${u}__length`]="0":t.state.env[u]=""),s&&ye(t,u)}}return C}function Mt(t){let e=[],n="",r=!1,s=!1,o=!1;for(let i of t){if(o){n+=i,o=!1;continue}if(i==="\\"){o=!0;continue}if(i==="'"&&!s){r=!r;continue}if(i==='"'&&!r){s=!s;continue}if((i===" "||i===" "||i===`
51
+ `)&&!r&&!s){n&&(e.push(n),n="");continue}n+=i}return n&&e.push(n),e}function bs(t){let e=[],n=0;for(;n<t.length;){for(;n<t.length&&/\s/.test(t[n]);)n++;if(n>=t.length)break;if(t[n]!=="["){n++;continue}n++;let r="";if(t[n]==="'"||t[n]==='"'){let o=t[n];for(n++;n<t.length&&t[n]!==o;)r+=t[n],n++;t[n]===o&&n++}else for(;n<t.length&&t[n]!=="]"&&t[n]!=="=";)r+=t[n],n++;for(;n<t.length&&t[n]!=="]";)n++;if(t[n]==="]"&&n++,t[n]!=="=")continue;n++;let s="";if(t[n]==="'"||t[n]==='"'){let o=t[n];for(n++;n<t.length&&t[n]!==o;)t[n]==="\\"&&n+1<t.length&&n++,s+=t[n],n++;t[n]===o&&n++}else for(;n<t.length&&!/\s/.test(t[n]);)s+=t[n],n++;e.push([r,s])}return e}function Bt(t,e){let n=!1,r=!1,s=[];for(let o=0;o<e.length;o++){let i=e[o];if(i==="-a")n=!0;else if(i==="-A")r=!0;else if(i==="-p"){if(e.length===1){let a="";for(let l of t.state.readonlyVars||[]){let c=t.state.env[l];c!==void 0&&(a+=`declare -r ${l}="${c}"
52
+ `)}return V(a)}}else if(i==="--"){s.push(...e.slice(o+1));break}else i.startsWith("-")||s.push(i)}for(let o of s){let i=on(o);if(i.value===void 0&&!i.isArray){ye(t,i.name);continue}let a=an(t,i,{makeReadonly:!0});if(a)return a}return C}async function zt(t,e){let n=e;if(n.length>0){let s=n[0];if(s==="--")n=n.slice(1);else if(s.startsWith("-")&&s!=="-"&&s.length>1)return S(`bash: eval: ${s}: invalid option
53
53
  eval: usage: eval [arg ...]
54
54
  `,2)}if(n.length===0)return C;let r=n.join(" ");if(r.trim()==="")return C;try{let s=oe(r);return t.executeScript(s)}catch(s){if(s instanceof z||s instanceof Q||s instanceof U||s instanceof R)throw s;if(s.name==="ParseException")return S(`bash: eval: ${s.message}
55
- `);throw s}}function zt(t,e){let n,r="";if(e.length===0)n=t.state.lastExitCode;else{let s=e[0],o=Number.parseInt(s,10);s===""||Number.isNaN(o)||!/^-?\d+$/.test(s)?(r=`bash: exit: ${s}: numeric argument required
56
- `,n=2):n=(o%256+256)%256}throw new R(n,"",r)}function Ut(t,e){let n=!1,r=[];for(let i of e)i==="-n"?n=!0:i==="-p"||i==="--"||r.push(i);if(r.length===0&&!n){let i="",a=Object.entries(t.state.env).filter(([l])=>!l.startsWith("BASH_ALIAS_")).sort(([l],[c])=>l.localeCompare(c));for(let[l,c]of a){let u=c.replace(/'/g,"'\\''");i+=`declare -x ${l}='${u}'
55
+ `);throw s}}function Ut(t,e){let n,r="";if(e.length===0)n=t.state.lastExitCode;else{let s=e[0],o=Number.parseInt(s,10);s===""||Number.isNaN(o)||!/^-?\d+$/.test(s)?(r=`bash: exit: ${s}: numeric argument required
56
+ `,n=2):n=(o%256+256)%256}throw new R(n,"",r)}function Gt(t,e){let n=!1,r=[];for(let i of e)i==="-n"?n=!0:i==="-p"||i==="--"||r.push(i);if(r.length===0&&!n){let i="",a=Object.entries(t.state.env).filter(([l])=>!l.startsWith("BASH_ALIAS_")).sort(([l],[c])=>l.localeCompare(c));for(let[l,c]of a){let u=c.replace(/'/g,"'\\''");i+=`declare -x ${l}='${u}'
57
57
  `}return V(i)}if(n){for(let i of r){let a=i.split("=")[0];delete t.state.env[a]}return C}let s="",o=0;for(let i of r){let a,l;if(i.includes("=")){let c=i.indexOf("=");a=i.slice(0,c),l=i.slice(c+1)}else a=i;if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(a)){s+=`bash: export: \`${i}': not a valid identifier
58
- `,o=1;continue}l!==void 0?t.state.env[a]=l:a in t.state.env||(t.state.env[a]="")}return x("",s,o)}function xs(t){let e=[],n="",r=0;for(let s of t){for(let o of s)o==="("?r++:o===")"&&r--;n?n+=` ${s}`:n=s,r===0&&(e.push(n),n="")}return n&&e.push(n),e}async function Gt(t,e){if(e.length===0)return S(`bash: let: expression expected
59
- `);let n=xs(e),r=0;for(let s of n)try{let i=oe(`(( ${s} ))`).statements[0];if(i&&i.pipelines.length>0&&i.pipelines[0].commands.length>0){let a=i.pipelines[0].commands[0];a.type==="ArithmeticCommand"&&(r=await $(t,a.expression.expression))}}catch(o){return S(`bash: let: ${s}: ${o.message}
60
- `)}return x("","",r===0?1:0)}function Ss(t,e){let n=t.state.env.HOME||"/home/user";return e.split(":").map(o=>o==="~"?n:o==="~root"?"/root":o.startsWith("~/")?n+o.slice(1):o.startsWith("~root/")?`/root${o.slice(5)}`:o).join(":")}function Ht(t,e){if(t.state.localScopes.length===0)return S(`bash: local: can only be used in a function
61
- `);let n=t.state.localScopes[t.state.localScopes.length-1],r="",s=0;for(let o of e){let i,a;if(o.includes("=")){let l=o.indexOf("=");i=o.slice(0,l),a=Ss(t,o.slice(l+1))}else i=o;if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(i)){r+=`bash: local: \`${o}': not a valid identifier
62
- `,s=1;continue}n.has(i)||n.set(i,t.state.env[i]),a!==void 0&&(t.state.env[i]=a)}return x("",r,s)}function Vt(t,e,n){let r=!1,s=`
63
- `,o="",i=-1,a=null,l=[],c=0,u=!1;for(;c<e.length;){let w=e[c];w==="-r"?r=!0:w==="-d"&&c+1<e.length?(s=e[c+1],c++):w==="-p"&&c+1<e.length?(o=e[c+1],c++):w==="-n"&&c+1<e.length?(i=Number.parseInt(e[c+1],10),(Number.isNaN(i)||i<0)&&(u=!0,i=0),c++):w==="-a"&&c+1<e.length?(a=e[c+1],c++):w==="-t"?c+1<e.length&&!e[c+1].startsWith("-")&&c++:w==="-s"||w.startsWith("-")||l.push(w),c++}if(u)return x("","",1);l.length===0&&a===null&&l.push("REPLY");let d=n;!d&&t.state.groupStdin!==void 0&&(d=t.state.groupStdin);let h="",p=0,y=!0;if(i>=0){for(let w=0;w<d.length&&w<i;w++){let b=d[w];if(b===s){p=w+1;break}h+=b,p=w+1}t.state.groupStdin!==void 0&&!n&&(t.state.groupStdin=d.substring(p))}else{let w=d;for(p=0;;){let b=w.indexOf(s);if(b!==-1){let I=w.substring(0,b);if(p+=b+s.length,w=w.substring(b+s.length),!r&&I.endsWith("\\")){h+=I.slice(0,-1);continue}h+=I,y=!0;break}else if(w.length>0){h+=w,p+=w.length,y=!1,w="";break}else{if(h.length===0){for(let I of l)t.state.env[I]="";return a&&Pt(t,a),x("","",1)}y=!1;break}}t.state.groupStdin!==void 0&&!n&&(t.state.groupStdin=d.substring(p))}if(s===`
58
+ `,o=1;continue}l!==void 0?t.state.env[a]=l:a in t.state.env||(t.state.env[a]="")}return x("",s,o)}function Cs(t){let e=[],n="",r=0;for(let s of t){for(let o of s)o==="("?r++:o===")"&&r--;n?n+=` ${s}`:n=s,r===0&&(e.push(n),n="")}return n&&e.push(n),e}async function Ht(t,e){if(e.length===0)return S(`bash: let: expression expected
59
+ `);let n=Cs(e),r=0;for(let s of n)try{let i=oe(`(( ${s} ))`).statements[0];if(i&&i.pipelines.length>0&&i.pipelines[0].commands.length>0){let a=i.pipelines[0].commands[0];a.type==="ArithmeticCommand"&&(r=await $(t,a.expression.expression))}}catch(o){return S(`bash: let: ${s}: ${o.message}
60
+ `)}return x("","",r===0?1:0)}function Ns(t,e){let n=t.state.env.HOME||"/home/user";return e.split(":").map(o=>o==="~"?n:o==="~root"?"/root":o.startsWith("~/")?n+o.slice(1):o.startsWith("~root/")?`/root${o.slice(5)}`:o).join(":")}function Vt(t,e){if(t.state.localScopes.length===0)return S(`bash: local: can only be used in a function
61
+ `);let n=t.state.localScopes[t.state.localScopes.length-1],r="",s=0;for(let o of e){let i,a;if(o.includes("=")){let l=o.indexOf("=");i=o.slice(0,l),a=Ns(t,o.slice(l+1))}else i=o;if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(i)){r+=`bash: local: \`${o}': not a valid identifier
62
+ `,s=1;continue}n.has(i)||n.set(i,t.state.env[i]),a!==void 0&&(t.state.env[i]=a)}return x("",r,s)}function Zt(t,e,n){let r=!1,s=`
63
+ `,o="",i=-1,a=null,l=[],c=0,u=!1;for(;c<e.length;){let w=e[c];w==="-r"?r=!0:w==="-d"&&c+1<e.length?(s=e[c+1],c++):w==="-p"&&c+1<e.length?(o=e[c+1],c++):w==="-n"&&c+1<e.length?(i=Number.parseInt(e[c+1],10),(Number.isNaN(i)||i<0)&&(u=!0,i=0),c++):w==="-a"&&c+1<e.length?(a=e[c+1],c++):w==="-t"?c+1<e.length&&!e[c+1].startsWith("-")&&c++:w==="-s"||w.startsWith("-")||l.push(w),c++}if(u)return x("","",1);l.length===0&&a===null&&l.push("REPLY");let d=n;!d&&t.state.groupStdin!==void 0&&(d=t.state.groupStdin);let h="",p=0,y=!0;if(i>=0){for(let w=0;w<d.length&&w<i;w++){let b=d[w];if(b===s){p=w+1;break}h+=b,p=w+1}t.state.groupStdin!==void 0&&!n&&(t.state.groupStdin=d.substring(p))}else{let w=d;for(p=0;;){let b=w.indexOf(s);if(b!==-1){let I=w.substring(0,b);if(p+=b+s.length,w=w.substring(b+s.length),!r&&I.endsWith("\\")){h+=I.slice(0,-1);continue}h+=I,y=!0;break}else if(w.length>0){h+=w,p+=w.length,y=!1,w="";break}else{if(h.length===0){for(let I of l)t.state.env[I]="";return a&&Rt(t,a),x("","",1)}y=!1;break}}t.state.groupStdin!==void 0&&!n&&(t.state.groupStdin=d.substring(p))}if(s===`
64
64
  `&&h.endsWith(`
65
- `)&&(h=h.slice(0,-1)),r||(h=h.replace(/\\(.)/g,"$1")),l.length===1&&l[0]==="REPLY")return t.state.env.REPLY=h,x("","",y?0:1);let A=Ue(t.state.env),{words:m,wordStarts:E}=Fr(h,A);if(a){Pt(t,a);for(let w=0;w<m.length;w++)t.state.env[`${a}_${w}`]=m[w];return x("","",y?0:1)}for(let w=0;w<l.length;w++){let b=l[w];if(w<l.length-1)t.state.env[b]=m[w]??"";else if(w<E.length){let I=Br(h.substring(E[w]),A);t.state.env[b]=I}else t.state.env[b]=""}return x("","",y?0:1)}function Zt(t,e){if(t.state.callDepth===0&&t.state.sourceDepth===0)return S("bash: return: can only `return' from a function or sourced script\n");let n=t.state.lastExitCode;if(e.length>0){let r=e[0],s=Number.parseInt(r,10);if(r===""||Number.isNaN(s)||!/^-?\d+$/.test(r))return S(`bash: return: ${r}: numeric argument required
66
- `,2);n=(s%256+256)%256}throw new U(n)}function bs(t){return/^[a-zA-Z0-9_/.:-]*$/.test(t)?t:`'${t.replace(/'/g,"'\\''")}'`}var Ze=`set: usage: set [-eux] [+eux] [-o option] [+o option]
65
+ `)&&(h=h.slice(0,-1)),r||(h=h.replace(/\\(.)/g,"$1")),l.length===1&&l[0]==="REPLY")return t.state.env.REPLY=h,x("","",y?0:1);let g=Ge(t.state.env),{words:m,wordStarts:E}=Ur(h,g);if(a){Rt(t,a);for(let w=0;w<m.length;w++)t.state.env[`${a}_${w}`]=m[w];return x("","",y?0:1)}for(let w=0;w<l.length;w++){let b=l[w];if(w<l.length-1)t.state.env[b]=m[w]??"";else if(w<E.length){let I=Gr(h.substring(E[w]),g);t.state.env[b]=I}else t.state.env[b]=""}return x("","",y?0:1)}function jt(t,e){if(t.state.callDepth===0&&t.state.sourceDepth===0)return S("bash: return: can only `return' from a function or sourced script\n");let n=t.state.lastExitCode;if(e.length>0){let r=e[0],s=Number.parseInt(r,10);if(r===""||Number.isNaN(s)||!/^-?\d+$/.test(r))return S(`bash: return: ${r}: numeric argument required
66
+ `,2);n=(s%256+256)%256}throw new U(n)}function $s(t){return/^[a-zA-Z0-9_/.:-]*$/.test(t)?t:`'${t.replace(/'/g,"'\\''")}'`}var je=`set: usage: set [-eux] [+eux] [-o option] [+o option]
67
67
  Options:
68
68
  -e Exit immediately if a command exits with non-zero status
69
69
  +e Disable -e
@@ -79,48 +79,48 @@ Options:
79
79
  +o pipefail Disable pipefail
80
80
  -o xtrace Same as -x
81
81
  +o xtrace Disable xtrace
82
- `,sn={e:"errexit",u:"nounset",x:"xtrace",v:"verbose",f:null,h:null,C:null,n:null,a:null,b:null,m:null,B:null,H:null,P:null,T:null,E:null,p:null},on={errexit:"errexit",pipefail:"pipefail",nounset:"nounset",xtrace:"xtrace",verbose:"verbose",noclobber:null,noglob:null,noexec:null,allexport:null,notify:null,monitor:null,braceexpand:null,histexpand:null,physical:null,functrace:null,errtrace:null,privileged:null,hashall:null,posix:null,vi:null,emacs:null,ignoreeof:null,"interactive-comments":null,keyword:null,onecmd:null},an=["errexit","nounset","pipefail","verbose","xtrace"],cn=["allexport","braceexpand","emacs","errtrace","functrace","hashall","histexpand","history","ignoreeof","interactive-comments","keyword","monitor","noclobber","noexec","noglob","nolog","notify","onecmd","physical","posix","privileged","vi"];function ln(t,e,n){e!==null&&(t.state.options[e]=n)}function Cs(t,e){return e+1<t.length&&!t[e+1].startsWith("-")&&!t[e+1].startsWith("+")}function qt(t,e){if(e.includes("--help"))return V(Ze);if(e.length===0){let r=Object.entries(t.state.env).filter(([s])=>/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(s)).sort(([s],[o])=>s.localeCompare(o)).map(([s,o])=>`${s}=${bs(o)}`).join(`
82
+ `,cn={e:"errexit",u:"nounset",x:"xtrace",v:"verbose",f:null,h:null,C:null,n:null,a:null,b:null,m:null,B:null,H:null,P:null,T:null,E:null,p:null},ln={errexit:"errexit",pipefail:"pipefail",nounset:"nounset",xtrace:"xtrace",verbose:"verbose",noclobber:null,noglob:null,noexec:null,allexport:null,notify:null,monitor:null,braceexpand:null,histexpand:null,physical:null,functrace:null,errtrace:null,privileged:null,hashall:null,posix:null,vi:null,emacs:null,ignoreeof:null,"interactive-comments":null,keyword:null,onecmd:null},un=["errexit","nounset","pipefail","verbose","xtrace"],fn=["allexport","braceexpand","emacs","errtrace","functrace","hashall","histexpand","history","ignoreeof","interactive-comments","keyword","monitor","noclobber","noexec","noglob","nolog","notify","onecmd","physical","posix","privileged","vi"];function dn(t,e,n){e!==null&&(t.state.options[e]=n)}function vs(t,e){return e+1<t.length&&!t[e+1].startsWith("-")&&!t[e+1].startsWith("+")}function Qt(t,e){if(e.includes("--help"))return V(je);if(e.length===0){let r=Object.entries(t.state.env).filter(([s])=>/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(s)).sort(([s],[o])=>s.localeCompare(o)).map(([s,o])=>`${s}=${$s(o)}`).join(`
83
83
  `);return V(r?`${r}
84
- `:"")}let n=0;for(;n<e.length;){let r=e[n];if((r==="-o"||r==="+o")&&Cs(e,n)){let s=e[n+1];if(!(s in on))return S(`bash: set: ${s}: invalid option name
85
- ${Ze}`);ln(t,on[s],r==="-o"),n+=2;continue}if(r==="-o"){let s=an.map(a=>`${a.padEnd(16)}${t.state.options[a]?"on":"off"}`),o=cn.map(a=>`${a.padEnd(16)}off`),i=[...s,...o].sort();return V(`${i.join(`
84
+ `:"")}let n=0;for(;n<e.length;){let r=e[n];if((r==="-o"||r==="+o")&&vs(e,n)){let s=e[n+1];if(!(s in ln))return S(`bash: set: ${s}: invalid option name
85
+ ${je}`);dn(t,ln[s],r==="-o"),n+=2;continue}if(r==="-o"){let s=un.map(a=>`${a.padEnd(16)}${t.state.options[a]?"on":"off"}`),o=fn.map(a=>`${a.padEnd(16)}off`),i=[...s,...o].sort();return V(`${i.join(`
86
86
  `)}
87
- `)}if(r==="+o"){let s=an.map(a=>`set ${t.state.options[a]?"-o":"+o"} ${a}`),o=cn.map(a=>`set +o ${a}`),i=[...s,...o].sort();return V(`${i.join(`
87
+ `)}if(r==="+o"){let s=un.map(a=>`set ${t.state.options[a]?"-o":"+o"} ${a}`),o=fn.map(a=>`set +o ${a}`),i=[...s,...o].sort();return V(`${i.join(`
88
88
  `)}
89
- `)}if(r.length>1&&(r[0]==="-"||r[0]==="+")&&r[1]!=="-"){let s=r[0]==="-";for(let o=1;o<r.length;o++){let i=r[o];if(!(i in sn))return S(`bash: set: ${r[0]}${i}: invalid option
90
- ${Ze}`);ln(t,sn[i],s)}n++;continue}if(r==="--")return jt(t,e.slice(n+1)),C;if(r==="-"){if(t.state.options.xtrace=!1,t.state.options.verbose=!1,n+1<e.length)return jt(t,e.slice(n+1)),C;n++;continue}if(r==="+"){n++;continue}return r.startsWith("-")||r.startsWith("+")?S(`bash: set: ${r}: invalid option
91
- ${Ze}`):(jt(t,e.slice(n)),C)}return C}function jt(t,e){let n=1;for(;t.state.env[String(n)]!==void 0;)delete t.state.env[String(n)],n++;for(let r=0;r<e.length;r++)t.state.env[String(r+1)]=e[r];t.state.env["#"]=String(e.length),t.state.env["@"]=e.join(" "),t.state.env["*"]=e.join(" ")}function Qt(t,e){let n=1;if(e.length>0){let i=Number.parseInt(e[0],10);if(Number.isNaN(i)||i<0)return S(`bash: shift: ${e[0]}: numeric argument required
89
+ `)}if(r.length>1&&(r[0]==="-"||r[0]==="+")&&r[1]!=="-"){let s=r[0]==="-";for(let o=1;o<r.length;o++){let i=r[o];if(!(i in cn))return S(`bash: set: ${r[0]}${i}: invalid option
90
+ ${je}`);dn(t,cn[i],s)}n++;continue}if(r==="--")return qt(t,e.slice(n+1)),C;if(r==="-"){if(t.state.options.xtrace=!1,t.state.options.verbose=!1,n+1<e.length)return qt(t,e.slice(n+1)),C;n++;continue}if(r==="+"){n++;continue}return r.startsWith("-")||r.startsWith("+")?S(`bash: set: ${r}: invalid option
91
+ ${je}`):(qt(t,e.slice(n)),C)}return C}function qt(t,e){let n=1;for(;t.state.env[String(n)]!==void 0;)delete t.state.env[String(n)],n++;for(let r=0;r<e.length;r++)t.state.env[String(r+1)]=e[r];t.state.env["#"]=String(e.length),t.state.env["@"]=e.join(" "),t.state.env["*"]=e.join(" ")}function Kt(t,e){let n=1;if(e.length>0){let i=Number.parseInt(e[0],10);if(Number.isNaN(i)||i<0)return S(`bash: shift: ${e[0]}: numeric argument required
92
92
  `);n=i}let r=Number.parseInt(t.state.env["#"]||"0",10);if(n>r)return S(`bash: shift: shift count out of range
93
- `);if(n===0)return C;let s=[];for(let i=1;i<=r;i++)s.push(t.state.env[String(i)]||"");let o=s.slice(n);for(let i=1;i<=r;i++)delete t.state.env[String(i)];for(let i=0;i<o.length;i++)t.state.env[String(i+1)]=o[i];return t.state.env["#"]=String(o.length),t.state.env["@"]=o.join(" "),C}async function Kt(t,e){let n=e;if(n.length>0&&n[0]==="--"&&(n=n.slice(1)),n.length===0)return x("",`bash: source: filename argument required
93
+ `);if(n===0)return C;let s=[];for(let i=1;i<=r;i++)s.push(t.state.env[String(i)]||"");let o=s.slice(n);for(let i=1;i<=r;i++)delete t.state.env[String(i)];for(let i=0;i<o.length;i++)t.state.env[String(i+1)]=o[i];return t.state.env["#"]=String(o.length),t.state.env["@"]=o.join(" "),C}async function Xt(t,e){let n=e;if(n.length>0&&n[0]==="--"&&(n=n.slice(1)),n.length===0)return x("",`bash: source: filename argument required
94
94
  `,2);let r=n[0],s=null,o=null;if(r.includes("/")){let l=t.fs.resolvePath(t.state.cwd,r);try{o=await t.fs.readFile(l),s=l}catch{}}else{let c=(t.state.env.PATH||"").split(":").filter(u=>u);for(let u of c){let d=t.fs.resolvePath(t.state.cwd,`${u}/${r}`);try{if((await t.fs.stat(d)).isDirectory)continue;o=await t.fs.readFile(d),s=d;break}catch{}}if(o===null){let u=t.fs.resolvePath(t.state.cwd,r);try{o=await t.fs.readFile(u),s=u}catch{}}}if(o===null)return S(`bash: ${r}: No such file or directory
95
95
  `);let i={};if(n.length>1){for(let c=1;c<=9;c++)i[String(c)]=t.state.env[String(c)];i["#"]=t.state.env["#"],i["@"]=t.state.env["@"];let l=n.slice(1);t.state.env["#"]=String(l.length),t.state.env["@"]=l.join(" ");for(let c=0;c<l.length&&c<9;c++)t.state.env[String(c+1)]=l[c];for(let c=l.length+1;c<=9;c++)delete t.state.env[String(c)]}let a=()=>{if(t.state.sourceDepth--,n.length>1)for(let[l,c]of Object.entries(i))c===void 0?delete t.state.env[l]:t.state.env[l]=c};t.state.sourceDepth++;try{let l=oe(o),c=await t.executeScript(l);return a(),c}catch(l){if(a(),l instanceof R)throw l;if(l instanceof U)return x(l.stdout,l.stderr,l.exitCode);if(l.name==="ParseException")return S(`bash: ${r}: ${l.message}
96
- `);throw l}}function Xt(t,e){let n="variable",r="",s=0;for(let o of e){if(o==="-v"){n="variable";continue}if(o==="-f"){n="function";continue}if(n==="function"){t.state.functions.delete(o);continue}let i=o.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(i){let a=i[1],l=i[2];if(l==="@"||l==="*"){let u=T(t,a);for(let[d]of u)delete t.state.env[`${a}_${d}`];delete t.state.env[a];continue}let c;if(/^-?\d+$/.test(l))c=Number.parseInt(l,10);else{let u=t.state.env[l];c=u?Number.parseInt(u,10):0,Number.isNaN(c)&&(c=0)}if(c<0){let u=T(t,a),d=u.length;if(d===0){r+=`bash: unset: [${c}]: bad array subscript
96
+ `);throw l}}function Yt(t,e){let n="variable",r="",s=0;for(let o of e){if(o==="-v"){n="variable";continue}if(o==="-f"){n="function";continue}if(n==="function"){t.state.functions.delete(o);continue}let i=o.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(i){let a=i[1],l=i[2];if(l==="@"||l==="*"){let u=T(t,a);for(let[d]of u)delete t.state.env[`${a}_${d}`];delete t.state.env[a];continue}let c;if(/^-?\d+$/.test(l))c=Number.parseInt(l,10);else{let u=t.state.env[l];c=u?Number.parseInt(u,10):0,Number.isNaN(c)&&(c=0)}if(c<0){let u=T(t,a),d=u.length;if(d===0){r+=`bash: unset: [${c}]: bad array subscript
97
97
  `,s=1;continue}let h=d+c;if(h<0){r+=`bash: unset: [${c}]: bad array subscript
98
- `,s=1;continue}let p=u[h][0];delete t.state.env[`${a}_${p}`];continue}delete t.state.env[`${a}_${c}`];continue}delete t.state.env[o],t.state.functions.delete(o)}return x("",r,s)}function Yt(t,e){return t.fs.resolvePath(t.state.cwd,e)}var Ns=["-e","-a","-f","-d","-r","-w","-x","-s","-L","-h","-k","-g","-u","-G","-O","-b","-c","-p","-S","-t","-N"];function je(t){return Ns.includes(t)}async function qe(t,e,n){let r=Yt(t,n);switch(e){case"-e":case"-a":return t.fs.exists(r);case"-f":return await t.fs.exists(r)?(await t.fs.stat(r)).isFile:!1;case"-d":return await t.fs.exists(r)?(await t.fs.stat(r)).isDirectory:!1;case"-r":case"-w":case"-x":return t.fs.exists(r);case"-s":return await t.fs.exists(r)?(await t.fs.stat(r)).size>0:!1;case"-L":case"-h":try{return(await t.fs.lstat(r)).isSymbolicLink}catch{return!1}case"-k":return await t.fs.exists(r)?((await t.fs.stat(r)).mode&512)!==0:!1;case"-g":return await t.fs.exists(r)?((await t.fs.stat(r)).mode&1024)!==0:!1;case"-u":return await t.fs.exists(r)?((await t.fs.stat(r)).mode&2048)!==0:!1;case"-G":case"-O":return t.fs.exists(r);case"-b":return!1;case"-c":return["/dev/null","/dev/zero","/dev/random","/dev/urandom","/dev/tty","/dev/stdin","/dev/stdout","/dev/stderr"].some(o=>r===o||r.endsWith(o));case"-p":return!1;case"-S":return!1;case"-t":return!1;case"-N":return t.fs.exists(r);default:return!1}}var $s=["-nt","-ot","-ef"];function Qe(t){return $s.includes(t)}async function Ke(t,e,n,r){let s=Yt(t,n),o=Yt(t,r);switch(e){case"-nt":try{let i=await t.fs.stat(s),a=await t.fs.stat(o);return i.mtime>a.mtime}catch{return!1}case"-ot":try{let i=await t.fs.stat(s),a=await t.fs.stat(o);return i.mtime<a.mtime}catch{return!1}case"-ef":try{if(!await t.fs.exists(s)||!await t.fs.exists(o))return!1;let i=t.fs.resolvePath(t.state.cwd,s),a=t.fs.resolvePath(t.state.cwd,o);return i===a}catch{return!1}default:return!1}}var vs=new Set(["-eq","-ne","-lt","-le","-gt","-ge"]);function Xe(t){return vs.has(t)}function Ye(t,e,n){switch(t){case"-eq":return e===n;case"-ne":return e!==n;case"-lt":return e<n;case"-le":return e<=n;case"-gt":return e>n;case"-ge":return e>=n}}function Je(t){return t==="="||t==="=="||t==="!="}function et(t,e,n,r=!1){let s=r?tt(e,n):e===n;return t==="!="?!s:s}var Is=new Set(["-z","-n"]);function rt(t){return Is.has(t)}function nt(t,e){switch(t){case"-z":return e==="";case"-n":return e!==""}}function st(t,e){let n=e.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(n){let r=n[1],s=n[2];s=s.replace(/\$([a-zA-Z_][a-zA-Z0-9_]*)/g,(i,a)=>t.state.env[a]||"");let o;if(/^-?\d+$/.test(s))o=Number.parseInt(s,10);else try{let i=Function(`"use strict"; return (${s})`)();o=typeof i=="number"?Math.floor(i):0}catch{let i=t.state.env[s];o=i?Number.parseInt(i,10):0}if(o<0){let i=Ne(t,r);if(i.length===0||(o=Math.max(...i)+1+o,o<0))return!1}return`${r}_${o}`in t.state.env}return e in t.state.env}async function le(t,e){switch(e.type){case"CondBinary":{let n=await v(t,e.left),r=await v(t,e.right),s=e.right.parts.length>0&&e.right.parts.every(o=>o.type==="SingleQuoted"||o.type==="DoubleQuoted"||o.type==="Escaped");if(Je(e.operator))return et(e.operator,n,r,!s);if(Xe(e.operator))return Ye(e.operator,fn(t,n),fn(t,r));if(Qe(e.operator))return Ke(t,e.operator,n,r);switch(e.operator){case"=~":try{let o=new RegExp(r),i=n.match(o);if(i){t.state.env.BASH_REMATCH=i[0];for(let a=1;a<i.length;a++)t.state.env[`BASH_REMATCH_${a}`]=i[a]||""}return i!==null}catch{throw new Error("syntax error in regular expression")}case"<":return n<r;case">":return n>r;default:return!1}}case"CondUnary":{let n=await v(t,e.operand);return je(e.operator)?qe(t,e.operator,n):rt(e.operator)?nt(e.operator,n):e.operator==="-v"?st(t,n):e.operator==="-o"?er(t,n):!1}case"CondNot":return!await le(t,e.operand);case"CondAnd":return await le(t,e.left)?await le(t,e.right):!1;case"CondOr":return await le(t,e.left)?!0:await le(t,e.right);case"CondGroup":return await le(t,e.expression);case"CondWord":return await v(t,e.word)!=="";default:return!1}}async function xe(t,e){if(e.length===0)return x("","",1);if(e.length===1)return _(!!e[0]);if(e.length===2){let r=e[0],s=e[1];return r==="("?S(`test: '(' without matching ')'
99
- `,2):je(r)?_(await qe(t,r,s)):rt(r)?_(nt(r,s)):r==="!"?_(!s):r==="-v"?_(st(t,s)):r==="-o"?_(er(t,s)):r==="="||r==="=="||r==="!="||r==="<"||r===">"||r==="-eq"||r==="-ne"||r==="-lt"||r==="-le"||r==="-gt"||r==="-ge"||r==="-nt"||r==="-ot"||r==="-ef"?S(`test: ${r}: unary operator expected
100
- `,2):x("","",1)}if(e.length===3){let r=e[0],s=e[1],o=e[2];if(Je(s))return _(et(s,r,o));if(Xe(s)){let i=it(r),a=it(o);return!i.valid||!a.valid?x("","",2):_(Ye(s,i.value,a.value))}if(Qe(s))return _(await Ke(t,s,r,o));switch(s){case"-a":return _(r!==""&&o!=="");case"-o":return _(r!==""||o!=="");case">":return _(r>o);case"<":return _(r<o)}if(r==="!"){let i=await xe(t,[s,o]);return x("",i.stderr,i.exitCode===0?1:i.exitCode===1?0:i.exitCode)}if(r==="("&&o===")")return _(s!=="")}if(e.length===4){if(e[0]==="!"){let r=await xe(t,e.slice(1));return x("",r.stderr,r.exitCode===0?1:r.exitCode===1?0:r.exitCode)}if(e[0]==="("&&e[3]===")")return xe(t,[e[1],e[2]])}let n=await hn(t,e,0);return n.pos<e.length?S(`test: too many arguments
101
- `,2):_(n.value)}async function hn(t,e,n){return Ps(t,e,n)}async function Ps(t,e,n){let{value:r,pos:s}=await un(t,e,n);for(;e[s]==="-o";){let o=await un(t,e,s+1);r=r||o.value,s=o.pos}return{value:r,pos:s}}async function un(t,e,n){let{value:r,pos:s}=await Jt(t,e,n);for(;e[s]==="-a";){let o=await Jt(t,e,s+1);r=r&&o.value,s=o.pos}return{value:r,pos:s}}async function Jt(t,e,n){if(e[n]==="!"){let{value:r,pos:s}=await Jt(t,e,n+1);return{value:!r,pos:s}}return Rs(t,e,n)}async function Rs(t,e,n){let r=e[n];if(r==="("){let{value:o,pos:i}=await hn(t,e,n+1);return{value:o,pos:e[i]===")"?i+1:i}}if(je(r)){let o=e[n+1]??"";return{value:await qe(t,r,o),pos:n+2}}if(rt(r)){let o=e[n+1]??"";return{value:nt(r,o),pos:n+2}}if(r==="-v"){let o=e[n+1]??"";return{value:st(t,o),pos:n+2}}if(r==="-o"){let o=e[n+1]??"";return{value:er(t,o),pos:n+2}}let s=e[n+1];if(Je(s)){let o=r,i=e[n+2]??"";return{value:et(s,o,i),pos:n+3}}if(Xe(s)){let o=it(r),i=it(e[n+2]??"0");return!o.valid||!i.valid?{value:!1,pos:n+3}:{value:Ye(s,o.value,i.value),pos:n+3}}if(Qe(s)){let o=r,i=e[n+2]??"";return{value:await Ke(t,s,o,i),pos:n+3}}return{value:r!==void 0&&r!=="",pos:n+1}}function tt(t,e){let n="^";for(let r=0;r<e.length;r++){let s=e[r];if(s==="\\")if(r+1<e.length){let o=e[r+1];/[\\^$.|+(){}[\]*?]/.test(o)?n+=`\\${o}`:n+=o,r++}else n+="\\\\";else if(s==="*")n+=".*";else if(s==="?")n+=".";else if(s==="["){let o=e.indexOf("]",r+1);o!==-1?(n+=e.slice(r,o+1),r=o):n+="\\["}else/[\\^$.|+(){}]/.test(s)?n+=`\\${s}`:n+=s}return n+="$",new RegExp(n).test(t)}function er(t,e){let r={errexit:()=>t.state.options.errexit===!0,nounset:()=>t.state.options.nounset===!0,pipefail:()=>t.state.options.pipefail===!0,xtrace:()=>t.state.options.xtrace===!0,e:()=>t.state.options.errexit===!0,u:()=>t.state.options.nounset===!0,x:()=>t.state.options.xtrace===!0}[e];return r?r():!1}function fn(t,e){if(e=e.trim(),e==="")return 0;if(/^[+-]?(\d+#[a-zA-Z0-9@_]+|0[xX][0-9a-fA-F]+|0[0-7]+|\d+)$/.test(e))return dn(e);try{let n=new M,r=L(n,e);return N(t,r.expression)}catch{return dn(e)}}function ks(t,e){let n=0;for(let r of t){let s;if(r>="0"&&r<="9")s=r.charCodeAt(0)-48;else if(r>="a"&&r<="z")s=r.charCodeAt(0)-97+10;else if(r>="A"&&r<="Z")s=r.charCodeAt(0)-65+36;else if(r==="@")s=62;else if(r==="_")s=63;else return Number.NaN;if(s>=e)return Number.NaN;n=n*e+s}return n}function dn(t){if(t=t.trim(),t==="")return 0;let e=!1;t.startsWith("-")?(e=!0,t=t.slice(1)):t.startsWith("+")&&(t=t.slice(1));let n,r=t.match(/^(\d+)#([a-zA-Z0-9@_]+)$/);if(r){let s=Number.parseInt(r[1],10);s>=2&&s<=64?n=ks(r[2],s):n=0}else/^0[xX][0-9a-fA-F]+$/.test(t)?n=Number.parseInt(t,16):/^0[0-7]+$/.test(t)?n=Number.parseInt(t,8):n=Number.parseInt(t,10);return Number.isNaN(n)&&(n=0),e?-n:n}function it(t){if(t=t.trim(),t==="")return{value:0,valid:!0};let e=!1;if(t.startsWith("-")?(e=!0,t=t.slice(1)):t.startsWith("+")&&(t=t.slice(1)),!/^\d+$/.test(t))return{value:0,valid:!1};let n=Number.parseInt(t,10);return Number.isNaN(n)?{value:0,valid:!1}:{value:e?-n:n,valid:!0}}async function tr(t,e){let n=t.state.inCondition;t.state.inCondition=!0;let r="",s="",o=0;try{for(let i of e){let a=await t.executeStatement(i);r+=a.stdout,s+=a.stderr,o=a.exitCode}}finally{t.state.inCondition=n}return{stdout:r,stderr:s,exitCode:o}}function Pe(t,e,n,r){if(t instanceof z)return e+=t.stdout,n+=t.stderr,t.levels>1&&r>1?(t.levels--,t.stdout=e,t.stderr=n,{action:"rethrow",stdout:e,stderr:n,error:t}):{action:"break",stdout:e,stderr:n};if(t instanceof Q)return e+=t.stdout,n+=t.stderr,t.levels>1&&r>1?(t.levels--,t.stdout=e,t.stderr=n,{action:"rethrow",stdout:e,stderr:n,error:t}):{action:"continue",stdout:e,stderr:n};if(t instanceof U||t instanceof J||t instanceof R||t instanceof B)return t.prependOutput(e,n),{action:"rethrow",stdout:e,stderr:n,error:t};let s=K(t);return{action:"error",stdout:e,stderr:`${n}${s}
102
- `,exitCode:1}}async function ot(t,e,n="",r=""){let s=n,o=r,i=0;try{for(let a of e){let l=await t.executeStatement(a);s+=l.stdout,o+=l.stderr,i=l.exitCode}}catch(a){if(Oe(a)||a instanceof J||a instanceof R||a instanceof B)throw a.prependOutput(s,o),a;return{stdout:s,stderr:`${o}${K(a)}
103
- `,exitCode:1}}return{stdout:s,stderr:o,exitCode:i}}async function pn(t,e){let n="",r="";for(let s of e.clauses){let o=await tr(t,s.condition);if(n+=o.stdout,r+=o.stderr,o.exitCode===0)return ot(t,s.body,n,r)}return e.elseBody?ot(t,e.elseBody,n,r):x(n,r,0)}async function mn(t,e){let n="",r="",s=0,o=0;if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(e.variable))return S(`bash: \`${e.variable}': not a valid identifier
104
- `);let i=[];if(e.words===null)i=(t.state.env["@"]||"").split(" ").filter(Boolean);else if(e.words.length===0)i=[];else for(let a of e.words){let l=await Ie(t,a);i.push(...l.values)}t.state.loopDepth++;try{for(let a of i){o++,o>t.limits.maxLoopIterations&&se(`for loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",n,r),t.state.env[e.variable]=a;try{for(let l of e.body){let c=await t.executeStatement(l);n+=c.stdout,r+=c.stderr,s=c.exitCode}}catch(l){let c=Pe(l,n,r,t.state.loopDepth);if(n=c.stdout,r=c.stderr,c.action==="break")break;if(c.action==="continue")continue;if(c.action==="error")return x(n,r,c.exitCode??1);throw c.error}}}finally{t.state.loopDepth--}return x(n,r,s)}async function yn(t,e){let n="",r="",s=0,o=0;e.init&&await $(t,e.init.expression),t.state.loopDepth++;try{for(;o++,o>t.limits.maxLoopIterations&&se(`for loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",n,r),!(e.condition&&await $(t,e.condition.expression)===0);){try{for(let i of e.body){let a=await t.executeStatement(i);n+=a.stdout,r+=a.stderr,s=a.exitCode}}catch(i){let a=Pe(i,n,r,t.state.loopDepth);if(n=a.stdout,r=a.stderr,a.action==="break")break;if(a.action==="continue"){e.update&&await $(t,e.update.expression);continue}if(a.action==="error")return x(n,r,a.exitCode??1);throw a.error}e.update&&await $(t,e.update.expression)}}finally{t.state.loopDepth--}return x(n,r,s)}async function En(t,e,n=""){let r="",s="",o=0,i=0,a=n;for(let c of e.redirections)if((c.operator==="<<"||c.operator==="<<-")&&c.target.type==="HereDoc"){let u=c.target,d=await v(t,u.content);u.stripTabs&&(d=d.split(`
98
+ `,s=1;continue}let p=u[h][0];delete t.state.env[`${a}_${p}`];continue}delete t.state.env[`${a}_${c}`];continue}delete t.state.env[o],t.state.functions.delete(o)}return x("",r,s)}function Jt(t,e){return t.fs.resolvePath(t.state.cwd,e)}var Is=["-e","-a","-f","-d","-r","-w","-x","-s","-L","-h","-k","-g","-u","-G","-O","-b","-c","-p","-S","-t","-N"];function qe(t){return Is.includes(t)}async function Qe(t,e,n){let r=Jt(t,n);switch(e){case"-e":case"-a":return t.fs.exists(r);case"-f":return await t.fs.exists(r)?(await t.fs.stat(r)).isFile:!1;case"-d":return await t.fs.exists(r)?(await t.fs.stat(r)).isDirectory:!1;case"-r":case"-w":case"-x":return t.fs.exists(r);case"-s":return await t.fs.exists(r)?(await t.fs.stat(r)).size>0:!1;case"-L":case"-h":try{return(await t.fs.lstat(r)).isSymbolicLink}catch{return!1}case"-k":return await t.fs.exists(r)?((await t.fs.stat(r)).mode&512)!==0:!1;case"-g":return await t.fs.exists(r)?((await t.fs.stat(r)).mode&1024)!==0:!1;case"-u":return await t.fs.exists(r)?((await t.fs.stat(r)).mode&2048)!==0:!1;case"-G":case"-O":return t.fs.exists(r);case"-b":return!1;case"-c":return["/dev/null","/dev/zero","/dev/random","/dev/urandom","/dev/tty","/dev/stdin","/dev/stdout","/dev/stderr"].some(o=>r===o||r.endsWith(o));case"-p":return!1;case"-S":return!1;case"-t":return!1;case"-N":return t.fs.exists(r);default:return!1}}var Ps=["-nt","-ot","-ef"];function Ke(t){return Ps.includes(t)}async function Xe(t,e,n,r){let s=Jt(t,n),o=Jt(t,r);switch(e){case"-nt":try{let i=await t.fs.stat(s),a=await t.fs.stat(o);return i.mtime>a.mtime}catch{return!1}case"-ot":try{let i=await t.fs.stat(s),a=await t.fs.stat(o);return i.mtime<a.mtime}catch{return!1}case"-ef":try{if(!await t.fs.exists(s)||!await t.fs.exists(o))return!1;let i=t.fs.resolvePath(t.state.cwd,s),a=t.fs.resolvePath(t.state.cwd,o);return i===a}catch{return!1}default:return!1}}var Rs=new Set(["-eq","-ne","-lt","-le","-gt","-ge"]);function Ye(t){return Rs.has(t)}function Je(t,e,n){switch(t){case"-eq":return e===n;case"-ne":return e!==n;case"-lt":return e<n;case"-le":return e<=n;case"-gt":return e>n;case"-ge":return e>=n}}function et(t){return t==="="||t==="=="||t==="!="}function tt(t,e,n,r=!1){let s=r?rt(e,n):e===n;return t==="!="?!s:s}var ks=new Set(["-z","-n"]);function nt(t){return ks.has(t)}function st(t,e){switch(t){case"-z":return e==="";case"-n":return e!==""}}function it(t,e){let n=e.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(n){let r=n[1],s=n[2];s=s.replace(/\$([a-zA-Z_][a-zA-Z0-9_]*)/g,(i,a)=>t.state.env[a]||"");let o;if(/^-?\d+$/.test(s))o=Number.parseInt(s,10);else try{let i=Function(`"use strict"; return (${s})`)();o=typeof i=="number"?Math.floor(i):0}catch{let i=t.state.env[s];o=i?Number.parseInt(i,10):0}if(o<0){let i=Ce(t,r);if(i.length===0||(o=Math.max(...i)+1+o,o<0))return!1}return`${r}_${o}`in t.state.env}return e in t.state.env}async function le(t,e){switch(e.type){case"CondBinary":{let n=await v(t,e.left),r=await v(t,e.right),s=e.right.parts.length>0&&e.right.parts.every(o=>o.type==="SingleQuoted"||o.type==="DoubleQuoted"||o.type==="Escaped");if(et(e.operator))return tt(e.operator,n,r,!s);if(Ye(e.operator))return Je(e.operator,pn(t,n),pn(t,r));if(Ke(e.operator))return Xe(t,e.operator,n,r);switch(e.operator){case"=~":try{let o=new RegExp(r),i=n.match(o);if(i){t.state.env.BASH_REMATCH=i[0];for(let a=1;a<i.length;a++)t.state.env[`BASH_REMATCH_${a}`]=i[a]||""}return i!==null}catch{throw new Error("syntax error in regular expression")}case"<":return n<r;case">":return n>r;default:return!1}}case"CondUnary":{let n=await v(t,e.operand);return qe(e.operator)?Qe(t,e.operator,n):nt(e.operator)?st(e.operator,n):e.operator==="-v"?it(t,n):e.operator==="-o"?tr(t,n):!1}case"CondNot":return!await le(t,e.operand);case"CondAnd":return await le(t,e.left)?await le(t,e.right):!1;case"CondOr":return await le(t,e.left)?!0:await le(t,e.right);case"CondGroup":return await le(t,e.expression);case"CondWord":return await v(t,e.word)!=="";default:return!1}}async function xe(t,e){if(e.length===0)return x("","",1);if(e.length===1)return _(!!e[0]);if(e.length===2){let r=e[0],s=e[1];return r==="("?S(`test: '(' without matching ')'
99
+ `,2):qe(r)?_(await Qe(t,r,s)):nt(r)?_(st(r,s)):r==="!"?_(!s):r==="-v"?_(it(t,s)):r==="-o"?_(tr(t,s)):r==="="||r==="=="||r==="!="||r==="<"||r===">"||r==="-eq"||r==="-ne"||r==="-lt"||r==="-le"||r==="-gt"||r==="-ge"||r==="-nt"||r==="-ot"||r==="-ef"?S(`test: ${r}: unary operator expected
100
+ `,2):x("","",1)}if(e.length===3){let r=e[0],s=e[1],o=e[2];if(et(s))return _(tt(s,r,o));if(Ye(s)){let i=ot(r),a=ot(o);return!i.valid||!a.valid?x("","",2):_(Je(s,i.value,a.value))}if(Ke(s))return _(await Xe(t,s,r,o));switch(s){case"-a":return _(r!==""&&o!=="");case"-o":return _(r!==""||o!=="");case">":return _(r>o);case"<":return _(r<o)}if(r==="!"){let i=await xe(t,[s,o]);return x("",i.stderr,i.exitCode===0?1:i.exitCode===1?0:i.exitCode)}if(r==="("&&o===")")return _(s!=="")}if(e.length===4){if(e[0]==="!"){let r=await xe(t,e.slice(1));return x("",r.stderr,r.exitCode===0?1:r.exitCode===1?0:r.exitCode)}if(e[0]==="("&&e[3]===")")return xe(t,[e[1],e[2]])}let n=await yn(t,e,0);return n.pos<e.length?S(`test: too many arguments
101
+ `,2):_(n.value)}async function yn(t,e,n){return Ds(t,e,n)}async function Ds(t,e,n){let{value:r,pos:s}=await hn(t,e,n);for(;e[s]==="-o";){let o=await hn(t,e,s+1);r=r||o.value,s=o.pos}return{value:r,pos:s}}async function hn(t,e,n){let{value:r,pos:s}=await er(t,e,n);for(;e[s]==="-a";){let o=await er(t,e,s+1);r=r&&o.value,s=o.pos}return{value:r,pos:s}}async function er(t,e,n){if(e[n]==="!"){let{value:r,pos:s}=await er(t,e,n+1);return{value:!r,pos:s}}return _s(t,e,n)}async function _s(t,e,n){let r=e[n];if(r==="("){let{value:o,pos:i}=await yn(t,e,n+1);return{value:o,pos:e[i]===")"?i+1:i}}if(qe(r)){let o=e[n+1]??"";return{value:await Qe(t,r,o),pos:n+2}}if(nt(r)){let o=e[n+1]??"";return{value:st(r,o),pos:n+2}}if(r==="-v"){let o=e[n+1]??"";return{value:it(t,o),pos:n+2}}if(r==="-o"){let o=e[n+1]??"";return{value:tr(t,o),pos:n+2}}let s=e[n+1];if(et(s)){let o=r,i=e[n+2]??"";return{value:tt(s,o,i),pos:n+3}}if(Ye(s)){let o=ot(r),i=ot(e[n+2]??"0");return!o.valid||!i.valid?{value:!1,pos:n+3}:{value:Je(s,o.value,i.value),pos:n+3}}if(Ke(s)){let o=r,i=e[n+2]??"";return{value:await Xe(t,s,o,i),pos:n+3}}return{value:r!==void 0&&r!=="",pos:n+1}}function rt(t,e){let n="^";for(let r=0;r<e.length;r++){let s=e[r];if(s==="\\")if(r+1<e.length){let o=e[r+1];/[\\^$.|+(){}[\]*?]/.test(o)?n+=`\\${o}`:n+=o,r++}else n+="\\\\";else if(s==="*")n+=".*";else if(s==="?")n+=".";else if(s==="["){let o=e.indexOf("]",r+1);o!==-1?(n+=e.slice(r,o+1),r=o):n+="\\["}else/[\\^$.|+(){}]/.test(s)?n+=`\\${s}`:n+=s}return n+="$",new RegExp(n).test(t)}function tr(t,e){let r={errexit:()=>t.state.options.errexit===!0,nounset:()=>t.state.options.nounset===!0,pipefail:()=>t.state.options.pipefail===!0,xtrace:()=>t.state.options.xtrace===!0,e:()=>t.state.options.errexit===!0,u:()=>t.state.options.nounset===!0,x:()=>t.state.options.xtrace===!0}[e];return r?r():!1}function pn(t,e){if(e=e.trim(),e==="")return 0;if(/^[+-]?(\d+#[a-zA-Z0-9@_]+|0[xX][0-9a-fA-F]+|0[0-7]+|\d+)$/.test(e))return mn(e);try{let n=new M,r=L(n,e);return N(t,r.expression)}catch{return mn(e)}}function Os(t,e){let n=0;for(let r of t){let s;if(r>="0"&&r<="9")s=r.charCodeAt(0)-48;else if(r>="a"&&r<="z")s=r.charCodeAt(0)-97+10;else if(r>="A"&&r<="Z")s=r.charCodeAt(0)-65+36;else if(r==="@")s=62;else if(r==="_")s=63;else return Number.NaN;if(s>=e)return Number.NaN;n=n*e+s}return n}function mn(t){if(t=t.trim(),t==="")return 0;let e=!1;t.startsWith("-")?(e=!0,t=t.slice(1)):t.startsWith("+")&&(t=t.slice(1));let n,r=t.match(/^(\d+)#([a-zA-Z0-9@_]+)$/);if(r){let s=Number.parseInt(r[1],10);s>=2&&s<=64?n=Os(r[2],s):n=0}else/^0[xX][0-9a-fA-F]+$/.test(t)?n=Number.parseInt(t,16):/^0[0-7]+$/.test(t)?n=Number.parseInt(t,8):n=Number.parseInt(t,10);return Number.isNaN(n)&&(n=0),e?-n:n}function ot(t){if(t=t.trim(),t==="")return{value:0,valid:!0};let e=!1;if(t.startsWith("-")?(e=!0,t=t.slice(1)):t.startsWith("+")&&(t=t.slice(1)),!/^\d+$/.test(t))return{value:0,valid:!1};let n=Number.parseInt(t,10);return Number.isNaN(n)?{value:0,valid:!1}:{value:e?-n:n,valid:!0}}async function rr(t,e){let n=t.state.inCondition;t.state.inCondition=!0;let r="",s="",o=0;try{for(let i of e){let a=await t.executeStatement(i);r+=a.stdout,s+=a.stderr,o=a.exitCode}}finally{t.state.inCondition=n}return{stdout:r,stderr:s,exitCode:o}}function Ie(t,e,n,r){if(t instanceof z)return e+=t.stdout,n+=t.stderr,t.levels>1&&r>1?(t.levels--,t.stdout=e,t.stderr=n,{action:"rethrow",stdout:e,stderr:n,error:t}):{action:"break",stdout:e,stderr:n};if(t instanceof Q)return e+=t.stdout,n+=t.stderr,t.levels>1&&r>1?(t.levels--,t.stdout=e,t.stderr=n,{action:"rethrow",stdout:e,stderr:n,error:t}):{action:"continue",stdout:e,stderr:n};if(t instanceof U||t instanceof J||t instanceof R||t instanceof B)return t.prependOutput(e,n),{action:"rethrow",stdout:e,stderr:n,error:t};let s=K(t);return{action:"error",stdout:e,stderr:`${n}${s}
102
+ `,exitCode:1}}async function at(t,e,n="",r=""){let s=n,o=r,i=0;try{for(let a of e){let l=await t.executeStatement(a);s+=l.stdout,o+=l.stderr,i=l.exitCode}}catch(a){if(_e(a)||a instanceof J||a instanceof R||a instanceof B)throw a.prependOutput(s,o),a;return{stdout:s,stderr:`${o}${K(a)}
103
+ `,exitCode:1}}return{stdout:s,stderr:o,exitCode:i}}async function En(t,e){let n="",r="";for(let s of e.clauses){let o=await rr(t,s.condition);if(n+=o.stdout,r+=o.stderr,o.exitCode===0)return at(t,s.body,n,r)}return e.elseBody?at(t,e.elseBody,n,r):x(n,r,0)}async function wn(t,e){let n="",r="",s=0,o=0;if(!/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(e.variable))return S(`bash: \`${e.variable}': not a valid identifier
104
+ `);let i=[];if(e.words===null)i=(t.state.env["@"]||"").split(" ").filter(Boolean);else if(e.words.length===0)i=[];else for(let a of e.words){let l=await ve(t,a);i.push(...l.values)}t.state.loopDepth++;try{for(let a of i){o++,o>t.limits.maxLoopIterations&&se(`for loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",n,r),t.state.env[e.variable]=a;try{for(let l of e.body){let c=await t.executeStatement(l);n+=c.stdout,r+=c.stderr,s=c.exitCode}}catch(l){let c=Ie(l,n,r,t.state.loopDepth);if(n=c.stdout,r=c.stderr,c.action==="break")break;if(c.action==="continue")continue;if(c.action==="error")return x(n,r,c.exitCode??1);throw c.error}}}finally{t.state.loopDepth--}return x(n,r,s)}async function gn(t,e){let n="",r="",s=0,o=0;e.init&&await $(t,e.init.expression),t.state.loopDepth++;try{for(;o++,o>t.limits.maxLoopIterations&&se(`for loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",n,r),!(e.condition&&await $(t,e.condition.expression)===0);){try{for(let i of e.body){let a=await t.executeStatement(i);n+=a.stdout,r+=a.stderr,s=a.exitCode}}catch(i){let a=Ie(i,n,r,t.state.loopDepth);if(n=a.stdout,r=a.stderr,a.action==="break")break;if(a.action==="continue"){e.update&&await $(t,e.update.expression);continue}if(a.action==="error")return x(n,r,a.exitCode??1);throw a.error}e.update&&await $(t,e.update.expression)}}finally{t.state.loopDepth--}return x(n,r,s)}async function An(t,e,n=""){let r="",s="",o=0,i=0,a=n;for(let c of e.redirections)if((c.operator==="<<"||c.operator==="<<-")&&c.target.type==="HereDoc"){let u=c.target,d=await v(t,u.content);u.stripTabs&&(d=d.split(`
105
105
  `).map(h=>h.replace(/^\t+/,"")).join(`
106
106
  `)),a=d}else if(c.operator==="<<<"&&c.target.type==="Word")a=`${await v(t,c.target)}
107
107
  `;else if(c.operator==="<"&&c.target.type==="Word")try{let u=await v(t,c.target),d=t.fs.resolvePath(t.state.cwd,u);a=await t.fs.readFile(d)}catch{let u=await v(t,c.target);return S(`bash: ${u}: No such file or directory
108
- `)}let l=t.state.groupStdin;a&&(t.state.groupStdin=a),t.state.loopDepth++;try{for(;;){i++,i>t.limits.maxLoopIterations&&se(`while loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",r,s);let c=0,u=!1,d=!1,h=t.state.inCondition;t.state.inCondition=!0;try{for(let p of e.condition){let y=await t.executeStatement(p);r+=y.stdout,s+=y.stderr,c=y.exitCode}}catch(p){if(p instanceof z){if(r+=p.stdout,s+=p.stderr,p.levels>1&&t.state.loopDepth>1)throw p.levels--,p.stdout=r,p.stderr=s,t.state.inCondition=h,p;u=!0}else if(p instanceof Q){if(r+=p.stdout,s+=p.stderr,p.levels>1&&t.state.loopDepth>1)throw p.levels--,p.stdout=r,p.stderr=s,t.state.inCondition=h,p;d=!0}else throw t.state.inCondition=h,p}finally{t.state.inCondition=h}if(u)break;if(!d){if(c!==0)break;try{for(let p of e.body){let y=await t.executeStatement(p);r+=y.stdout,s+=y.stderr,o=y.exitCode}}catch(p){let y=Pe(p,r,s,t.state.loopDepth);if(r=y.stdout,s=y.stderr,y.action==="break")break;if(y.action==="continue")continue;if(y.action==="error")return x(r,s,y.exitCode??1);throw y.error}}}}finally{t.state.loopDepth--,t.state.groupStdin=l}return x(r,s,o)}async function wn(t,e){let n="",r="",s=0,o=0;t.state.loopDepth++;try{for(;;){o++,o>t.limits.maxLoopIterations&&se(`until loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",n,r);let i=await tr(t,e.condition);if(n+=i.stdout,r+=i.stderr,i.exitCode===0)break;try{for(let a of e.body){let l=await t.executeStatement(a);n+=l.stdout,r+=l.stderr,s=l.exitCode}}catch(a){let l=Pe(a,n,r,t.state.loopDepth);if(n=l.stdout,r=l.stderr,l.action==="break")break;if(l.action==="continue")continue;if(l.action==="error")return x(n,r,l.exitCode??1);throw l.error}}}finally{t.state.loopDepth--}return x(n,r,s)}async function An(t,e){let n="",r="",s=0,o=await v(t,e.word),i=!1;for(let a=0;a<e.items.length;a++){let l=e.items[a],c=i;if(!i)for(let u of l.patterns){let d=await v(t,u);if(Vr(u)&&(d=Zr(d)),tt(o,d)){c=!0;break}}if(c){let u=await ot(t,l.body,n,r);if(n=u.stdout,r=u.stderr,s=u.exitCode,l.terminator===";;")break;l.terminator===";&"?i=!0:i=!1}else i=!1}return x(n,r,s)}function gn(t,e){return t.state.functions.set(e.name,e),C}async function xn(t,e,n){t.state.callDepth++,t.state.callDepth>t.limits.maxCallDepth&&(t.state.callDepth--,se(`${e.name}: maximum recursion depth (${t.limits.maxCallDepth}) exceeded, increase executionLimits.maxCallDepth`,"recursion")),t.state.localScopes.push(new Map);let r={};for(let o=0;o<n.length;o++)r[String(o+1)]=t.state.env[String(o+1)],t.state.env[String(o+1)]=n[o];r["@"]=t.state.env["@"],r["#"]=t.state.env["#"],t.state.env["@"]=n.join(" "),t.state.env["#"]=String(n.length);let s=()=>{let o=t.state.localScopes.pop();if(o)for(let[i,a]of o)a===void 0?delete t.state.env[i]:t.state.env[i]=a;for(let[i,a]of Object.entries(r))a===void 0?delete t.state.env[i]:t.state.env[i]=a;t.state.callDepth--};try{let o=await t.executeCommand(e.body,"");return s(),o}catch(o){if(s(),o instanceof U)return x(o.stdout,o.stderr,o.exitCode);throw o}}async function Sn(t,e,n){let{stdout:r,stderr:s,exitCode:o}=e;for(let i of n){if(i.target.type==="HereDoc")continue;let a=await v(t,i.target);switch(i.operator){case">":{let l=i.fd??1;if(l===1){let c=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(c)).isDirectory){s+=`bash: ${a}: Is a directory
108
+ `)}let l=t.state.groupStdin;a&&(t.state.groupStdin=a),t.state.loopDepth++;try{for(;;){i++,i>t.limits.maxLoopIterations&&se(`while loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",r,s);let c=0,u=!1,d=!1,h=t.state.inCondition;t.state.inCondition=!0;try{for(let p of e.condition){let y=await t.executeStatement(p);r+=y.stdout,s+=y.stderr,c=y.exitCode}}catch(p){if(p instanceof z){if(r+=p.stdout,s+=p.stderr,p.levels>1&&t.state.loopDepth>1)throw p.levels--,p.stdout=r,p.stderr=s,t.state.inCondition=h,p;u=!0}else if(p instanceof Q){if(r+=p.stdout,s+=p.stderr,p.levels>1&&t.state.loopDepth>1)throw p.levels--,p.stdout=r,p.stderr=s,t.state.inCondition=h,p;d=!0}else throw t.state.inCondition=h,p}finally{t.state.inCondition=h}if(u)break;if(!d){if(c!==0)break;try{for(let p of e.body){let y=await t.executeStatement(p);r+=y.stdout,s+=y.stderr,o=y.exitCode}}catch(p){let y=Ie(p,r,s,t.state.loopDepth);if(r=y.stdout,s=y.stderr,y.action==="break")break;if(y.action==="continue")continue;if(y.action==="error")return x(r,s,y.exitCode??1);throw y.error}}}}finally{t.state.loopDepth--,t.state.groupStdin=l}return x(r,s,o)}async function xn(t,e){let n="",r="",s=0,o=0;t.state.loopDepth++;try{for(;;){o++,o>t.limits.maxLoopIterations&&se(`until loop: too many iterations (${t.limits.maxLoopIterations}), increase executionLimits.maxLoopIterations`,"iterations",n,r);let i=await rr(t,e.condition);if(n+=i.stdout,r+=i.stderr,i.exitCode===0)break;try{for(let a of e.body){let l=await t.executeStatement(a);n+=l.stdout,r+=l.stderr,s=l.exitCode}}catch(a){let l=Ie(a,n,r,t.state.loopDepth);if(n=l.stdout,r=l.stderr,l.action==="break")break;if(l.action==="continue")continue;if(l.action==="error")return x(n,r,l.exitCode??1);throw l.error}}}finally{t.state.loopDepth--}return x(n,r,s)}async function Sn(t,e){let n="",r="",s=0,o=await v(t,e.word),i=!1;for(let a=0;a<e.items.length;a++){let l=e.items[a],c=i;if(!i)for(let u of l.patterns){let d=await v(t,u);if(qr(u)&&(d=Qr(d)),rt(o,d)){c=!0;break}}if(c){let u=await at(t,l.body,n,r);if(n=u.stdout,r=u.stderr,s=u.exitCode,l.terminator===";;")break;l.terminator===";&"?i=!0:i=!1}else i=!1}return x(n,r,s)}function bn(t,e){return t.state.functions.set(e.name,e),C}async function Cn(t,e,n){t.state.callDepth++,t.state.callDepth>t.limits.maxCallDepth&&(t.state.callDepth--,se(`${e.name}: maximum recursion depth (${t.limits.maxCallDepth}) exceeded, increase executionLimits.maxCallDepth`,"recursion")),t.state.localScopes.push(new Map);let r={};for(let o=0;o<n.length;o++)r[String(o+1)]=t.state.env[String(o+1)],t.state.env[String(o+1)]=n[o];r["@"]=t.state.env["@"],r["#"]=t.state.env["#"],t.state.env["@"]=n.join(" "),t.state.env["#"]=String(n.length);let s=()=>{let o=t.state.localScopes.pop();if(o)for(let[i,a]of o)a===void 0?delete t.state.env[i]:t.state.env[i]=a;for(let[i,a]of Object.entries(r))a===void 0?delete t.state.env[i]:t.state.env[i]=a;t.state.callDepth--};try{let o=await t.executeCommand(e.body,"");return s(),o}catch(o){if(s(),o instanceof U)return x(o.stdout,o.stderr,o.exitCode);throw o}}async function Nn(t,e,n){let{stdout:r,stderr:s,exitCode:o}=e;for(let i of n){if(i.target.type==="HereDoc")continue;let a=await v(t,i.target);switch(i.operator){case">":{let l=i.fd??1;if(l===1){let c=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(c)).isDirectory){s+=`bash: ${a}: Is a directory
109
109
  `,o=1,r="";break}}catch{}await t.fs.writeFile(c,r),r=""}else if(l===2)if(a==="/dev/null")s="";else{let c=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(c)).isDirectory){s+=`bash: ${a}: Is a directory
110
110
  `,o=1;break}}catch{}await t.fs.writeFile(c,s),s=""}break}case">>":{let l=i.fd??1;if(l===1){let c=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(c)).isDirectory){s+=`bash: ${a}: Is a directory
111
111
  `,o=1,r="";break}}catch{}await t.fs.appendFile(c,r),r=""}else if(l===2){let c=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(c)).isDirectory){s+=`bash: ${a}: Is a directory
112
112
  `,o=1;break}}catch{}await t.fs.appendFile(c,s),s=""}break}case">&":{let l=i.fd??1;a==="2"||a==="&2"?l===1&&(s+=r,r=""):(a==="1"||a==="&1")&&(r+=s,s="");break}case"&>":{let l=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(l)).isDirectory){s=`bash: ${a}: Is a directory
113
113
  `,o=1,r="";break}}catch{}await t.fs.writeFile(l,r+s),r="",s="";break}case"&>>":{let l=t.fs.resolvePath(t.state.cwd,a);try{if((await t.fs.stat(l)).isDirectory){s=`bash: ${a}: Is a directory
114
- `,o=1,r="";break}}catch{}await t.fs.appendFile(l,r+s),r="",s="";break}}}return x(r,s,o)}var Re=class{ctx;constructor(e,n){this.ctx={state:n,fs:e.fs,commands:e.commands,limits:e.limits,execFn:e.exec,executeScript:this.executeScript.bind(this),executeStatement:this.executeStatement.bind(this),executeCommand:this.executeCommand.bind(this),fetch:e.fetch,sleep:e.sleep}}async executeScript(e){let n="",r="",s=0;for(let o of e.statements)try{let i=await this.executeStatement(o);n+=i.stdout,r+=i.stderr,s=i.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s)}catch(i){if(i instanceof R)throw i.prependOutput(n,r),i;if(i instanceof B)throw i;if(i instanceof J)return n+=i.stdout,r+=i.stderr,s=i.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s),{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}};if(i instanceof X)return n+=i.stdout,r+=i.stderr,s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s),{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}};if(i instanceof fe)return n+=i.stdout,r+=i.stderr,s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s),{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}};if(i instanceof z||i instanceof Q){if(this.ctx.state.loopDepth>0)throw i.prependOutput(n,r),i;n+=i.stdout,r+=i.stderr;continue}throw i instanceof U&&i.prependOutput(n,r),i}return{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}}}async executeStatement(e){this.ctx.state.commandCount++,this.ctx.state.commandCount>this.ctx.limits.maxCommandCount&&se(`too many commands executed (>${this.ctx.limits.maxCommandCount}), increase executionLimits.maxCommandCount`,"commands");let n="",r="",s=0,o=-1,i=!1;for(let a=0;a<e.pipelines.length;a++){let l=e.pipelines[a],c=a>0?e.operators[a-1]:null;if(c==="&&"&&s!==0||c==="||"&&s===0)continue;let u=await this.executePipeline(l);n+=u.stdout,r+=u.stderr,s=u.exitCode,o=a,i=l.negated,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s)}if(this.ctx.state.options.errexit&&s!==0&&o===e.pipelines.length-1&&!i&&!this.ctx.state.inCondition)throw new J(s,n,r);return x(n,r,s)}async executePipeline(e){let n="",r=C,s=0,o=[];for(let i=0;i<e.commands.length;i++){let a=e.commands[i],l=i===e.commands.length-1,c;try{c=await this.executeCommand(a,n)}catch(u){if(u instanceof fe)c={stdout:u.stdout,stderr:u.stderr,exitCode:1};else if(u instanceof R&&e.commands.length>1)c={stdout:u.stdout,stderr:u.stderr,exitCode:u.exitCode};else throw u}o.push(c.exitCode),c.exitCode!==0&&(s=c.exitCode),l?r=c:(n=c.stdout,r={stdout:"",stderr:c.stderr,exitCode:c.exitCode})}for(let i of Object.keys(this.ctx.state.env))i.startsWith("PIPESTATUS_")&&delete this.ctx.state.env[i];for(let i=0;i<o.length;i++)this.ctx.state.env[`PIPESTATUS_${i}`]=String(o[i]);return this.ctx.state.env.PIPESTATUS__length=String(o.length),this.ctx.state.options.pipefail&&s!==0&&(r={...r,exitCode:s}),e.negated&&(r={...r,exitCode:r.exitCode===0?1:0}),r}async executeCommand(e,n){switch(e.type){case"SimpleCommand":return this.executeSimpleCommand(e,n);case"If":return pn(this.ctx,e);case"For":return mn(this.ctx,e);case"CStyleFor":return yn(this.ctx,e);case"While":return En(this.ctx,e,n);case"Until":return wn(this.ctx,e);case"Case":return An(this.ctx,e);case"Subshell":return this.executeSubshell(e,n);case"Group":return this.executeGroup(e,n);case"FunctionDef":return gn(this.ctx,e);case"ArithmeticCommand":return this.executeArithmeticCommand(e);case"ConditionalCommand":return this.executeConditionalCommand(e);default:return C}}async executeSimpleCommand(e,n){try{return await this.executeSimpleCommandInner(e,n)}catch(r){if(r instanceof W)return S(r.stderr);throw r}}async executeSimpleCommandInner(e,n){this.ctx.state.expansionStderr="";let r={};for(let l of e.assignments){let c=l.name;if(l.array){if(/\[.+\]$/.test(c))return x("",`bash: ${c}: cannot assign list to array member
115
- `,1);let A=ce(this.ctx,c);if(A)return A;let m=[];for(let w of l.array){let b=await Ie(this.ctx,w);m.push(...b.values)}let E=0;if(l.append){let w=T(this.ctx,c);w.length>0&&(E=Math.max(...w.map(([I])=>typeof I=="number"?I:0))+1)}else{let w=`${c}_`;for(let b of Object.keys(this.ctx.state.env))b.startsWith(w)&&!b.includes("__")&&delete this.ctx.state.env[b]}for(let w=0;w<m.length;w++)this.ctx.state.env[`${c}_${E+w}`]=m[w];l.append||(this.ctx.state.env[`${c}__length`]=String(m.length));continue}let u=l.value?await v(this.ctx,l.value):"";if(c.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[\]$/))return x("",`bash: ${c}: bad array subscript
116
- `,1);let h=c.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(h){let A=h[1],m=h[2],E=ce(this.ctx,A);if(E)return E;let w=this.ctx.state.associativeArrays?.has(A),b;if(w){let P;if(m.startsWith("'")&&m.endsWith("'"))P=m.slice(1,-1);else if(m.startsWith('"')&&m.endsWith('"')){let k=m.slice(1,-1),O=new M().parseWordFromString(k,!0,!1);P=await v(this.ctx,O)}else if(m.includes("$")){let ue=new M().parseWordFromString(m,!1,!1);P=await v(this.ctx,ue)}else P=m;b=`${A}_${P}`}else{let P;if(/^-?\d+$/.test(m))P=Number.parseInt(m,10);else{try{let k=new M,ue=L(k,m);P=N(this.ctx,ue.expression)}catch{let k=this.ctx.state.env[m];P=k?Number.parseInt(k,10):0}Number.isNaN(P)&&(P=0)}if(P<0){let k=T(this.ctx,A);if(k.length===0)return x("",`bash: ${A}[${m}]: bad array subscript
117
- `,1);if(P=Math.max(...k.map(([O])=>typeof O=="number"?O:0))+1+P,P<0)return x("",`bash: ${A}[${m}]: bad array subscript
118
- `,1)}b=`${A}_${P}`}let I=l.append?(this.ctx.state.env[b]||"")+u:u;e.name?(r[b]=this.ctx.state.env[b],this.ctx.state.env[b]=I):this.ctx.state.env[b]=I;continue}let p=ce(this.ctx,c);if(p)return p;let y=l.append?(this.ctx.state.env[c]||"")+u:u;e.name?(r[c]=this.ctx.state.env[c],this.ctx.state.env[c]=y):this.ctx.state.env[c]=y}if(!e.name)return x("","",this.ctx.state.lastExitCode);for(let l of e.redirections){if((l.operator==="<<"||l.operator==="<<-")&&l.target.type==="HereDoc"){let c=l.target,u=await v(this.ctx,c.content);c.stripTabs&&(u=u.split(`
114
+ `,o=1,r="";break}}catch{}await t.fs.appendFile(l,r+s),r="",s="";break}}}return x(r,s,o)}var Pe=class{ctx;constructor(e,n){this.ctx={state:n,fs:e.fs,commands:e.commands,limits:e.limits,execFn:e.exec,executeScript:this.executeScript.bind(this),executeStatement:this.executeStatement.bind(this),executeCommand:this.executeCommand.bind(this),fetch:e.fetch,sleep:e.sleep}}async executeScript(e){let n="",r="",s=0;for(let o of e.statements)try{let i=await this.executeStatement(o);n+=i.stdout,r+=i.stderr,s=i.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s)}catch(i){if(i instanceof R)throw i.prependOutput(n,r),i;if(i instanceof B)throw i;if(i instanceof J)return n+=i.stdout,r+=i.stderr,s=i.exitCode,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s),{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}};if(i instanceof X)return n+=i.stdout,r+=i.stderr,s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s),{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}};if(i instanceof fe)return n+=i.stdout,r+=i.stderr,s=1,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s),{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}};if(i instanceof z||i instanceof Q){if(this.ctx.state.loopDepth>0)throw i.prependOutput(n,r),i;n+=i.stdout,r+=i.stderr;continue}throw i instanceof U&&i.prependOutput(n,r),i}return{stdout:n,stderr:r,exitCode:s,env:{...this.ctx.state.env}}}async executeStatement(e){this.ctx.state.commandCount++,this.ctx.state.commandCount>this.ctx.limits.maxCommandCount&&se(`too many commands executed (>${this.ctx.limits.maxCommandCount}), increase executionLimits.maxCommandCount`,"commands");let n="",r="",s=0,o=-1,i=!1;for(let a=0;a<e.pipelines.length;a++){let l=e.pipelines[a],c=a>0?e.operators[a-1]:null;if(c==="&&"&&s!==0||c==="||"&&s===0)continue;let u=await this.executePipeline(l);n+=u.stdout,r+=u.stderr,s=u.exitCode,o=a,i=l.negated,this.ctx.state.lastExitCode=s,this.ctx.state.env["?"]=String(s)}if(this.ctx.state.options.errexit&&s!==0&&o===e.pipelines.length-1&&!i&&!this.ctx.state.inCondition)throw new J(s,n,r);return x(n,r,s)}async executePipeline(e){let n="",r=C,s=0,o=[];for(let i=0;i<e.commands.length;i++){let a=e.commands[i],l=i===e.commands.length-1,c;try{c=await this.executeCommand(a,n)}catch(u){if(u instanceof fe)c={stdout:u.stdout,stderr:u.stderr,exitCode:1};else if(u instanceof R&&e.commands.length>1)c={stdout:u.stdout,stderr:u.stderr,exitCode:u.exitCode};else throw u}o.push(c.exitCode),c.exitCode!==0&&(s=c.exitCode),l?r=c:(n=c.stdout,r={stdout:"",stderr:c.stderr,exitCode:c.exitCode})}for(let i of Object.keys(this.ctx.state.env))i.startsWith("PIPESTATUS_")&&delete this.ctx.state.env[i];for(let i=0;i<o.length;i++)this.ctx.state.env[`PIPESTATUS_${i}`]=String(o[i]);return this.ctx.state.env.PIPESTATUS__length=String(o.length),this.ctx.state.options.pipefail&&s!==0&&(r={...r,exitCode:s}),e.negated&&(r={...r,exitCode:r.exitCode===0?1:0}),r}async executeCommand(e,n){switch(e.type){case"SimpleCommand":return this.executeSimpleCommand(e,n);case"If":return En(this.ctx,e);case"For":return wn(this.ctx,e);case"CStyleFor":return gn(this.ctx,e);case"While":return An(this.ctx,e,n);case"Until":return xn(this.ctx,e);case"Case":return Sn(this.ctx,e);case"Subshell":return this.executeSubshell(e,n);case"Group":return this.executeGroup(e,n);case"FunctionDef":return bn(this.ctx,e);case"ArithmeticCommand":return this.executeArithmeticCommand(e);case"ConditionalCommand":return this.executeConditionalCommand(e);default:return C}}async executeSimpleCommand(e,n){try{return await this.executeSimpleCommandInner(e,n)}catch(r){if(r instanceof W)return S(r.stderr);throw r}}async executeSimpleCommandInner(e,n){this.ctx.state.expansionStderr="";let r={};for(let l of e.assignments){let c=l.name;if(l.array){if(/\[.+\]$/.test(c))return x("",`bash: ${c}: cannot assign list to array member
115
+ `,1);let g=ce(this.ctx,c);if(g)return g;let m=[];for(let w of l.array){let b=await ve(this.ctx,w);m.push(...b.values)}let E=0;if(l.append){let w=T(this.ctx,c);w.length>0&&(E=Math.max(...w.map(([I])=>typeof I=="number"?I:0))+1)}else{let w=`${c}_`;for(let b of Object.keys(this.ctx.state.env))b.startsWith(w)&&!b.includes("__")&&delete this.ctx.state.env[b]}for(let w=0;w<m.length;w++)this.ctx.state.env[`${c}_${E+w}`]=m[w];l.append||(this.ctx.state.env[`${c}__length`]=String(m.length));continue}let u=l.value?await v(this.ctx,l.value):"";if(c.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[\]$/))return x("",`bash: ${c}: bad array subscript
116
+ `,1);let h=c.match(/^([a-zA-Z_][a-zA-Z0-9_]*)\[(.+)\]$/);if(h){let g=h[1],m=h[2],E=ce(this.ctx,g);if(E)return E;let w=this.ctx.state.associativeArrays?.has(g),b;if(w){let P;if(m.startsWith("'")&&m.endsWith("'"))P=m.slice(1,-1);else if(m.startsWith('"')&&m.endsWith('"')){let k=m.slice(1,-1),O=new M().parseWordFromString(k,!0,!1);P=await v(this.ctx,O)}else if(m.includes("$")){let ue=new M().parseWordFromString(m,!1,!1);P=await v(this.ctx,ue)}else P=m;b=`${g}_${P}`}else{let P;if(/^-?\d+$/.test(m))P=Number.parseInt(m,10);else{try{let k=new M,ue=L(k,m);P=N(this.ctx,ue.expression)}catch{let k=this.ctx.state.env[m];P=k?Number.parseInt(k,10):0}Number.isNaN(P)&&(P=0)}if(P<0){let k=T(this.ctx,g);if(k.length===0)return x("",`bash: ${g}[${m}]: bad array subscript
117
+ `,1);if(P=Math.max(...k.map(([O])=>typeof O=="number"?O:0))+1+P,P<0)return x("",`bash: ${g}[${m}]: bad array subscript
118
+ `,1)}b=`${g}_${P}`}let I=l.append?(this.ctx.state.env[b]||"")+u:u;e.name?(r[b]=this.ctx.state.env[b],this.ctx.state.env[b]=I):this.ctx.state.env[b]=I;continue}let p=ce(this.ctx,c);if(p)return p;let y=l.append?(this.ctx.state.env[c]||"")+u:u;e.name?(r[c]=this.ctx.state.env[c],this.ctx.state.env[c]=y):this.ctx.state.env[c]=y}if(!e.name)return x("","",this.ctx.state.lastExitCode);for(let l of e.redirections){if((l.operator==="<<"||l.operator==="<<-")&&l.target.type==="HereDoc"){let c=l.target,u=await v(this.ctx,c.content);c.stripTabs&&(u=u.split(`
119
119
  `).map(d=>d.replace(/^\t+/,"")).join(`
120
120
  `)),n=u;continue}if(l.operator==="<<<"&&l.target.type==="Word"){n=`${await v(this.ctx,l.target)}
121
121
  `;continue}if(l.operator==="<"&&l.target.type==="Word")try{let c=await v(this.ctx,l.target),u=this.ctx.fs.resolvePath(this.ctx.state.cwd,c);n=await this.ctx.fs.readFile(u)}catch{let c=await v(this.ctx,l.target);for(let[u,d]of Object.entries(r))d===void 0?delete this.ctx.state.env[u]:this.ctx.state.env[u]=d;return S(`bash: ${c}: No such file or directory
122
- `)}}let s=await v(this.ctx,e.name),o=[],i=[];for(let l of e.args){let c=await Ie(this.ctx,l);for(let u of c.values)o.push(u),i.push(c.quoted)}if(!s){if(e.name.parts.every(c=>c.type==="CommandSubstitution"||c.type==="ParameterExpansion"||c.type==="ArithmeticExpansion")){if(o.length>0){let c=o.shift();return i.shift(),await this.runCommand(c,o,i,n)}return x("","",this.ctx.state.lastExitCode)}return S(`bash: : command not found
123
- `,127)}let a=await this.runCommand(s,o,i,n);a=await Sn(this.ctx,a,e.redirections),this.ctx.state.lastArg=o.length>0?o[o.length-1]:s;for(let[l,c]of Object.entries(r))c===void 0?delete this.ctx.state.env[l]:this.ctx.state.env[l]=c;return this.ctx.state.expansionStderr&&(a={...a,stderr:this.ctx.state.expansionStderr+a.stderr},this.ctx.state.expansionStderr=""),a}async runCommand(e,n,r,s,o=!1){if(e==="cd")return await Lt(this.ctx,n);if(e==="export")return Ut(this.ctx,n);if(e==="unset")return Xt(this.ctx,n);if(e==="exit")return zt(this.ctx,n);if(e==="local")return Ht(this.ctx,n);if(e==="set")return qt(this.ctx,n);if(e==="break")return Ot(this.ctx,n);if(e==="continue")return Tt(this.ctx,n);if(e==="return")return Zt(this.ctx,n);if(e==="eval")return Bt(this.ctx,n);if(e==="shift")return Qt(this.ctx,n);if(e==="source"||e===".")return Kt(this.ctx,n);if(e==="read")return Vt(this.ctx,n,s);if(e==="declare"||e==="typeset")return Mt(this.ctx,n);if(e==="readonly")return Ft(this.ctx,n);if(!o){let u=this.ctx.state.functions.get(e);if(u)return xn(this.ctx,u,n)}if(e===":"||e==="true")return C;if(e==="false")return _(!1);if(e==="let")return Gt(this.ctx,n);if(e==="command"){if(n.length===0)return C;let u=n;for(;u.length>0&&u[0].startsWith("-");)u=u.slice(1);if(u.length===0)return C;let[d,...h]=u;return this.runCommand(d,h,[],s,!0)}if(e==="builtin"){if(n.length===0)return C;let[u,...d]=n;return this.runCommand(u,d,[],s)}if(e==="shopt")return C;if(e==="exec"){if(n.length===0)return C;let[u,...d]=n;return this.runCommand(u,d,[],s)}if(e==="wait")return C;if(e==="type")return this.handleType(n);if(e==="[["){let u=n.lastIndexOf("]]");if(u!==-1){let d=n.slice(0,u);return xe(this.ctx,d)}return S("bash: [[: missing `]]'\n",2)}if(e==="["||e==="test"){let u=n;if(e==="["){if(n[n.length-1]!=="]")return S("[: missing `]'\n",2);u=n.slice(0,-1)}return xe(this.ctx,u)}let i=await this.resolveCommand(e);if(!i)return S(`bash: ${e}: command not found
122
+ `)}}let s=await v(this.ctx,e.name),o=[],i=[];for(let l of e.args){let c=await ve(this.ctx,l);for(let u of c.values)o.push(u),i.push(c.quoted)}if(!s){if(e.name.parts.every(c=>c.type==="CommandSubstitution"||c.type==="ParameterExpansion"||c.type==="ArithmeticExpansion")){if(o.length>0){let c=o.shift();return i.shift(),await this.runCommand(c,o,i,n)}return x("","",this.ctx.state.lastExitCode)}return S(`bash: : command not found
123
+ `,127)}let a=await this.runCommand(s,o,i,n);a=await Nn(this.ctx,a,e.redirections),this.ctx.state.lastArg=o.length>0?o[o.length-1]:s;for(let[l,c]of Object.entries(r))c===void 0?delete this.ctx.state.env[l]:this.ctx.state.env[l]=c;return this.ctx.state.expansionStderr&&(a={...a,stderr:this.ctx.state.expansionStderr+a.stderr},this.ctx.state.expansionStderr=""),a}async runCommand(e,n,r,s,o=!1){if(e==="cd")return await Tt(this.ctx,n);if(e==="export")return Gt(this.ctx,n);if(e==="unset")return Yt(this.ctx,n);if(e==="exit")return Ut(this.ctx,n);if(e==="local")return Vt(this.ctx,n);if(e==="set")return Qt(this.ctx,n);if(e==="break")return Lt(this.ctx,n);if(e==="continue")return Wt(this.ctx,n);if(e==="return")return jt(this.ctx,n);if(e==="eval")return zt(this.ctx,n);if(e==="shift")return Kt(this.ctx,n);if(e==="source"||e===".")return Xt(this.ctx,n);if(e==="read")return Zt(this.ctx,n,s);if(e==="declare"||e==="typeset")return Ft(this.ctx,n);if(e==="readonly")return Bt(this.ctx,n);if(!o){let u=this.ctx.state.functions.get(e);if(u)return Cn(this.ctx,u,n)}if(e===":"||e==="true")return C;if(e==="false")return _(!1);if(e==="let")return Ht(this.ctx,n);if(e==="command"){if(n.length===0)return C;let u=n;for(;u.length>0&&u[0].startsWith("-");)u=u.slice(1);if(u.length===0)return C;let[d,...h]=u;return this.runCommand(d,h,[],s,!0)}if(e==="builtin"){if(n.length===0)return C;let[u,...d]=n;return this.runCommand(u,d,[],s)}if(e==="shopt")return C;if(e==="exec"){if(n.length===0)return C;let[u,...d]=n;return this.runCommand(u,d,[],s)}if(e==="wait")return C;if(e==="type")return this.handleType(n);if(e==="[["){let u=n.lastIndexOf("]]");if(u!==-1){let d=n.slice(0,u);return xe(this.ctx,d)}return S("bash: [[: missing `]]'\n",2)}if(e==="["||e==="test"){let u=n;if(e==="["){if(n[n.length-1]!=="]")return S("[: missing `]'\n",2);u=n.slice(0,-1)}return xe(this.ctx,u)}let i=await this.resolveCommand(e);if(!i)return S(`bash: ${e}: command not found
124
124
  `,127);let{cmd:a,path:l}=i,c={fs:this.ctx.fs,cwd:this.ctx.state.cwd,env:this.ctx.state.env,stdin:s,limits:this.ctx.limits,exec:this.ctx.execFn,fetch:this.ctx.fetch,getRegisteredCommands:()=>Array.from(this.ctx.commands.keys()),sleep:this.ctx.sleep};try{return await a.execute(n,c)}catch(u){return S(`${e}: ${K(u)}
125
125
  `)}}async resolveCommand(e){if(e.includes("/")){let o=this.ctx.fs.resolvePath(this.ctx.state.cwd,e);if(!await this.ctx.fs.exists(o))return null;let i=o.split("/").pop()||e,a=this.ctx.commands.get(i);return a?{cmd:a,path:o}:null}let r=(this.ctx.state.env.PATH||"/bin:/usr/bin").split(":");for(let o of r){if(!o)continue;let i=`${o}/${e}`;if(await this.ctx.fs.exists(i)){let a=this.ctx.commands.get(e);if(a)return{cmd:a,path:i}}}if(!await this.ctx.fs.exists("/bin")){let o=this.ctx.commands.get(e);if(o)return{cmd:o,path:`/bin/${e}`}}return null}async findCommandInPath(e){let n=[],s=(this.ctx.state.env.PATH||"/bin:/usr/bin").split(":");for(let o of s){if(!o)continue;let i=`${o}/${e}`;await this.ctx.fs.exists(i)&&n.push(i)}return n}handleType(e){let n=new Set(["if","then","else","elif","fi","case","esac","for","select","while","until","do","done","in","function","{","}","time","[[","]]","!"]),r=new Set(["cd","export","unset","exit","local","set","break","continue","return","eval","shift","source",".","read","declare","typeset","readonly",":","true","false","let","command","builtin","shopt","exec","wait","type","[","test"]),s="",o="",i=0;for(let a of e)n.has(a)?s+=`${a} is a shell keyword
126
126
  `:r.has(a)?s+=`${a} is a shell builtin
@@ -128,21 +128,21 @@ ${Ze}`):(jt(t,e.slice(n)),C)}return C}function jt(t,e){let n=1;for(;t.state.env[
128
128
  `:this.ctx.commands.has(a)?s+=`${a} is /bin/${a}
129
129
  `:(o+=`bash: type: ${a}: not found
130
130
  `,i=1);return x(s,o,i)}async executeSubshell(e,n=""){let r={...this.ctx.state.env},s=this.ctx.state.cwd,o=this.ctx.state.loopDepth;this.ctx.state.loopDepth=0;let i=this.ctx.state.groupStdin;n&&(this.ctx.state.groupStdin=n);let a="",l="",c=0;try{for(let u of e.body){let d=await this.executeStatement(u);a+=d.stdout,l+=d.stderr,c=d.exitCode}}catch(u){if(this.ctx.state.env=r,this.ctx.state.cwd=s,this.ctx.state.loopDepth=o,this.ctx.state.groupStdin=i,u instanceof B)throw u;if(u instanceof z||u instanceof Q)return a+=u.stdout,l+=u.stderr,x(a,l,0);if(u instanceof R)return a+=u.stdout,l+=u.stderr,x(a,l,u.exitCode);if(u instanceof U)return a+=u.stdout,l+=u.stderr,x(a,l,u.exitCode);if(u instanceof J)throw u.stdout=a+u.stdout,u.stderr=l+u.stderr,u;return x(a,`${l}${K(u)}
131
- `,1)}return this.ctx.state.env=r,this.ctx.state.cwd=s,this.ctx.state.loopDepth=o,this.ctx.state.groupStdin=i,x(a,l,c)}async executeGroup(e,n=""){let r="",s="",o=0,i=this.ctx.state.groupStdin;n&&(this.ctx.state.groupStdin=n);try{for(let a of e.body){let l=await this.executeStatement(a);r+=l.stdout,s+=l.stderr,o=l.exitCode}}catch(a){if(this.ctx.state.groupStdin=i,a instanceof B)throw a;if(Oe(a)||a instanceof J||a instanceof R)throw a.prependOutput(r,s),a;return x(r,`${s}${K(a)}
131
+ `,1)}return this.ctx.state.env=r,this.ctx.state.cwd=s,this.ctx.state.loopDepth=o,this.ctx.state.groupStdin=i,x(a,l,c)}async executeGroup(e,n=""){let r="",s="",o=0,i=this.ctx.state.groupStdin;n&&(this.ctx.state.groupStdin=n);try{for(let a of e.body){let l=await this.executeStatement(a);r+=l.stdout,s+=l.stderr,o=l.exitCode}}catch(a){if(this.ctx.state.groupStdin=i,a instanceof B)throw a;if(_e(a)||a instanceof J||a instanceof R)throw a.prependOutput(r,s),a;return x(r,`${s}${K(a)}
132
132
  `,1)}return this.ctx.state.groupStdin=i,x(r,s,o)}async executeArithmeticCommand(e){try{let n=await $(this.ctx,e.expression.expression);return _(n!==0)}catch(n){return S(`bash: arithmetic expression: ${n.message}
133
133
  `)}}async executeConditionalCommand(e){try{let n=await le(this.ctx,e.expression);return _(n)}catch(n){return S(`bash: conditional expression: ${n.message}
134
- `,2)}}};var Se={maxCallDepth:100,maxCommandCount:1e4,maxLoopIterations:1e4,maxAwkIterations:1e4,maxSedIterations:1e4};function bn(t){return t?{maxCallDepth:t.maxCallDepth??Se.maxCallDepth,maxCommandCount:t.maxCommandCount??Se.maxCommandCount,maxLoopIterations:t.maxLoopIterations??Se.maxLoopIterations,maxAwkIterations:t.maxAwkIterations??Se.maxAwkIterations,maxSedIterations:t.maxSedIterations??Se.maxSedIterations}:{...Se}}function Cn(t){try{let e=new URL(t);return{origin:e.origin,pathname:e.pathname,href:e.href}}catch{return null}}function Ds(t){let e=Cn(t);return e?{origin:e.origin,pathPrefix:e.pathname}:null}function _s(t,e){let n=Cn(t);if(!n)return!1;let r=Ds(e);return!r||n.origin!==r.origin?!1:r.pathPrefix==="/"||r.pathPrefix===""?!0:n.pathname.startsWith(r.pathPrefix)}function rr(t,e){return!e||e.length===0?!1:e.some(n=>_s(t,n))}var ke=class extends Error{constructor(e){super(`Network access denied: URL not in allow-list: ${e}`),this.name="NetworkAccessDeniedError"}},De=class extends Error{constructor(e){super(`Too many redirects (max: ${e})`),this.name="TooManyRedirectsError"}},_e=class extends Error{constructor(e){super(`Redirect target not in allow-list: ${e}`),this.name="RedirectNotAllowedError"}},at=class extends Error{constructor(e,n){super(`HTTP method '${e}' not allowed. Allowed methods: ${n.join(", ")}`),this.name="MethodNotAllowedError"}};var Os=20,Ls=3e4,Ts=["GET","HEAD"],Ws=new Set(["GET","HEAD","OPTIONS"]),Ms=new Set([301,302,303,307,308]);function nr(t){let e=t.maxRedirects??Os,n=t.timeoutMs??Ls,r=t.dangerouslyAllowFullInternetAccess?["GET","HEAD","POST","PUT","DELETE","PATCH","OPTIONS"]:t.allowedMethods??Ts;function s(a){if(!t.dangerouslyAllowFullInternetAccess&&!rr(a,t.allowedUrlPrefixes??[]))throw new ke(a)}function o(a){if(t.dangerouslyAllowFullInternetAccess)return;let l=a.toUpperCase();if(!r.includes(l))throw new at(l,r)}async function i(a,l={}){let c=l.method?.toUpperCase()??"GET";s(a),o(c);let u=a,d=0,h=l.followRedirects??!0,p=l.timeoutMs!==void 0?Math.min(l.timeoutMs,n):n;for(;;){let y=new AbortController,A=setTimeout(()=>y.abort(),p);try{let m={method:c,headers:l.headers,signal:y.signal,redirect:"manual"};l.body&&!Ws.has(c)&&(m.body=l.body);let E=await fetch(u,m);if(Ms.has(E.status)&&h){let w=E.headers.get("location");if(!w)return await Nn(E,u);let b=new URL(w,u).href;if(!t.dangerouslyAllowFullInternetAccess&&!rr(b,t.allowedUrlPrefixes??[]))throw new _e(b);if(d++,d>e)throw new De(e);u=b;continue}return await Nn(E,u)}finally{clearTimeout(A)}}}return i}async function Nn(t,e){let n={};return t.headers.forEach((r,s)=>{n[s.toLowerCase()]=r}),{status:t.status,statusText:t.statusText,headers:n,body:await t.text(),url:e}}var ct=class{fs;commands=new Map;useDefaultLayout=!1;limits;secureFetch;sleepFn;state;constructor(e={}){let n=e.fs??new de(e.files);this.fs=n,this.useDefaultLayout=!e.cwd&&!e.files;let r=e.cwd||(this.useDefaultLayout?"/home/user":"/"),s={HOME:this.useDefaultLayout?"/home/user":"/",PATH:"/bin:/usr/bin",IFS:`
135
- `,OSTYPE:"linux-gnu",MACHTYPE:"x86_64-pc-linux-gnu",HOSTTYPE:"x86_64",PWD:r,OLDPWD:r,...e.env};if(this.limits=bn({...e.executionLimits,...e.maxCallDepth!==void 0&&{maxCallDepth:e.maxCallDepth},...e.maxCommandCount!==void 0&&{maxCommandCount:e.maxCommandCount},...e.maxLoopIterations!==void 0&&{maxLoopIterations:e.maxLoopIterations}}),e.network&&(this.secureFetch=nr(e.network)),this.sleepFn=e.sleep,this.state={env:s,cwd:r,previousDir:"/home/user",functions:new Map,localScopes:[],callDepth:0,sourceDepth:0,commandCount:0,lastExitCode:0,lastArg:"",options:{errexit:!1,pipefail:!1,nounset:!1,xtrace:!1,verbose:!1},inCondition:!1,loopDepth:0},n instanceof de)try{n.mkdirSync("/bin",{recursive:!0}),n.mkdirSync("/usr/bin",{recursive:!0}),this.useDefaultLayout&&(n.mkdirSync("/home/user",{recursive:!0}),n.mkdirSync("/tmp",{recursive:!0}))}catch{}if(r!=="/"&&n instanceof de)try{n.mkdirSync(r,{recursive:!0})}catch{}for(let o of cr(e.commands))this.registerCommand(o);if(e.network)for(let o of lr())this.registerCommand(o)}registerCommand(e){if(this.commands.set(e.name,e),this.fs instanceof de)try{this.fs.writeFileSync(`/bin/${e.name}`,`#!/bin/bash
134
+ `,2)}}};var Se={maxCallDepth:100,maxCommandCount:1e4,maxLoopIterations:1e4,maxAwkIterations:1e4,maxSedIterations:1e4};function $n(t){return t?{maxCallDepth:t.maxCallDepth??Se.maxCallDepth,maxCommandCount:t.maxCommandCount??Se.maxCommandCount,maxLoopIterations:t.maxLoopIterations??Se.maxLoopIterations,maxAwkIterations:t.maxAwkIterations??Se.maxAwkIterations,maxSedIterations:t.maxSedIterations??Se.maxSedIterations}:{...Se}}function vn(t){try{let e=new URL(t);return{origin:e.origin,pathname:e.pathname,href:e.href}}catch{return null}}function Ls(t){let e=vn(t);return e?{origin:e.origin,pathPrefix:e.pathname}:null}function Ts(t,e){let n=vn(t);if(!n)return!1;let r=Ls(e);return!r||n.origin!==r.origin?!1:r.pathPrefix==="/"||r.pathPrefix===""?!0:n.pathname.startsWith(r.pathPrefix)}function nr(t,e){return!e||e.length===0?!1:e.some(n=>Ts(t,n))}var Re=class extends Error{constructor(e){super(`Network access denied: URL not in allow-list: ${e}`),this.name="NetworkAccessDeniedError"}},ke=class extends Error{constructor(e){super(`Too many redirects (max: ${e})`),this.name="TooManyRedirectsError"}},De=class extends Error{constructor(e){super(`Redirect target not in allow-list: ${e}`),this.name="RedirectNotAllowedError"}},ct=class extends Error{constructor(e,n){super(`HTTP method '${e}' not allowed. Allowed methods: ${n.join(", ")}`),this.name="MethodNotAllowedError"}};var Ws=20,Ms=3e4,Fs=["GET","HEAD"],Bs=new Set(["GET","HEAD","OPTIONS"]),zs=new Set([301,302,303,307,308]);function sr(t){let e=t.maxRedirects??Ws,n=t.timeoutMs??Ms,r=t.dangerouslyAllowFullInternetAccess?["GET","HEAD","POST","PUT","DELETE","PATCH","OPTIONS"]:t.allowedMethods??Fs;function s(a){if(!t.dangerouslyAllowFullInternetAccess&&!nr(a,t.allowedUrlPrefixes??[]))throw new Re(a)}function o(a){if(t.dangerouslyAllowFullInternetAccess)return;let l=a.toUpperCase();if(!r.includes(l))throw new ct(l,r)}async function i(a,l={}){let c=l.method?.toUpperCase()??"GET";s(a),o(c);let u=a,d=0,h=l.followRedirects??!0,p=l.timeoutMs!==void 0?Math.min(l.timeoutMs,n):n;for(;;){let y=new AbortController,g=setTimeout(()=>y.abort(),p);try{let m={method:c,headers:l.headers,signal:y.signal,redirect:"manual"};l.body&&!Bs.has(c)&&(m.body=l.body);let E=await fetch(u,m);if(zs.has(E.status)&&h){let w=E.headers.get("location");if(!w)return await In(E,u);let b=new URL(w,u).href;if(!t.dangerouslyAllowFullInternetAccess&&!nr(b,t.allowedUrlPrefixes??[]))throw new De(b);if(d++,d>e)throw new ke(e);u=b;continue}return await In(E,u)}finally{clearTimeout(g)}}}return i}async function In(t,e){let n={};return t.headers.forEach((r,s)=>{n[s.toLowerCase()]=r}),{status:t.status,statusText:t.statusText,headers:n,body:await t.text(),url:e}}var lt=class{fs;commands=new Map;useDefaultLayout=!1;limits;secureFetch;sleepFn;state;constructor(e={}){let n=e.fs??new de(e.files);this.fs=n,this.useDefaultLayout=!e.cwd&&!e.files;let r=e.cwd||(this.useDefaultLayout?"/home/user":"/"),s={HOME:this.useDefaultLayout?"/home/user":"/",PATH:"/bin:/usr/bin",IFS:`
135
+ `,OSTYPE:"linux-gnu",MACHTYPE:"x86_64-pc-linux-gnu",HOSTTYPE:"x86_64",PWD:r,OLDPWD:r,...e.env};if(this.limits=$n({...e.executionLimits,...e.maxCallDepth!==void 0&&{maxCallDepth:e.maxCallDepth},...e.maxCommandCount!==void 0&&{maxCommandCount:e.maxCommandCount},...e.maxLoopIterations!==void 0&&{maxLoopIterations:e.maxLoopIterations}}),e.network&&(this.secureFetch=sr(e.network)),this.sleepFn=e.sleep,this.state={env:s,cwd:r,previousDir:"/home/user",functions:new Map,localScopes:[],callDepth:0,sourceDepth:0,commandCount:0,lastExitCode:0,lastArg:"",options:{errexit:!1,pipefail:!1,nounset:!1,xtrace:!1,verbose:!1},inCondition:!1,loopDepth:0},n instanceof de)try{n.mkdirSync("/bin",{recursive:!0}),n.mkdirSync("/usr/bin",{recursive:!0}),this.useDefaultLayout&&(n.mkdirSync("/home/user",{recursive:!0}),n.mkdirSync("/tmp",{recursive:!0}))}catch{}if(r!=="/"&&n instanceof de)try{n.mkdirSync(r,{recursive:!0})}catch{}for(let o of lr(e.commands))this.registerCommand(o);if(e.network)for(let o of ur())this.registerCommand(o);if(e.customCommands)for(let o of e.customCommands)fr(o)?this.registerCommand(dr(o)):this.registerCommand(o)}registerCommand(e){if(this.commands.set(e.name,e),this.fs instanceof de)try{this.fs.writeFileSync(`/bin/${e.name}`,`#!/bin/bash
136
136
  # Built-in command: ${e.name}
137
137
  `)}catch{}}async exec(e,n){if(this.state.callDepth===0&&(this.state.commandCount=0),this.state.commandCount++,this.state.commandCount>this.limits.maxCommandCount)return{stdout:"",stderr:`bash: maximum command count (${this.limits.maxCommandCount}) exceeded (possible infinite loop). Increase with executionLimits.maxCommandCount option.
138
138
  `,exitCode:1,env:{...this.state.env,...n?.env}};if(!e.trim())return{stdout:"",stderr:"",exitCode:0,env:{...this.state.env,...n?.env}};let r=n?.cwd??this.state.cwd,s={...this.state,env:{...this.state.env,...n?.env,...n?.cwd?{PWD:n.cwd}:{}},cwd:r,functions:new Map(this.state.functions),localScopes:[...this.state.localScopes],options:{...this.state.options}},o=e;n?.rawScript||(o=e.split(`
139
139
  `).map(a=>a.trimStart()).join(`
140
- `));try{let i=oe(o),a={fs:this.fs,commands:this.commands,limits:this.limits,exec:this.exec.bind(this),fetch:this.secureFetch,sleep:this.sleepFn};return await new Re(a,s).executeScript(i)}catch(i){if(i instanceof R)return{stdout:i.stdout,stderr:i.stderr,exitCode:i.exitCode,env:{...this.state.env,...n?.env}};if(i instanceof W)return{stdout:i.stdout,stderr:i.stderr,exitCode:1,env:{...this.state.env,...n?.env}};if(i instanceof B)return{stdout:i.stdout,stderr:i.stderr,exitCode:B.EXIT_CODE,env:{...this.state.env,...n?.env}};if(i.name==="ParseException")return{stdout:"",stderr:`bash: syntax error: ${i.message}
140
+ `));try{let i=oe(o),a={fs:this.fs,commands:this.commands,limits:this.limits,exec:this.exec.bind(this),fetch:this.secureFetch,sleep:this.sleepFn};return await new Pe(a,s).executeScript(i)}catch(i){if(i instanceof R)return{stdout:i.stdout,stderr:i.stderr,exitCode:i.exitCode,env:{...this.state.env,...n?.env}};if(i instanceof W)return{stdout:i.stdout,stderr:i.stderr,exitCode:1,env:{...this.state.env,...n?.env}};if(i instanceof B)return{stdout:i.stdout,stderr:i.stderr,exitCode:B.EXIT_CODE,env:{...this.state.env,...n?.env}};if(i.name==="ParseException")return{stdout:"",stderr:`bash: syntax error: ${i.message}
141
141
  `,exitCode:2,env:{...this.state.env,...n?.env}};if(i instanceof RangeError)return{stdout:"",stderr:`bash: ${i.message}
142
- `,exitCode:1,env:{...this.state.env,...n?.env}};throw i}}async readFile(e){return this.fs.readFile(this.fs.resolvePath(this.state.cwd,e))}async writeFile(e,n){return this.fs.writeFile(this.fs.resolvePath(this.state.cwd,e),n)}getCwd(){return this.state.cwd}getEnv(){return{...this.state.env}}};var F={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"},sr=class{env;rl;running=!0;history=[];isInteractive;constructor(e={}){let n={"/home/user/.bashrc":`# Virtual shell
142
+ `,exitCode:1,env:{...this.state.env,...n?.env}};throw i}}async readFile(e){return this.fs.readFile(this.fs.resolvePath(this.state.cwd,e))}async writeFile(e,n){return this.fs.writeFile(this.fs.resolvePath(this.state.cwd,e),n)}getCwd(){return this.state.cwd}getEnv(){return{...this.state.env}}};var F={reset:"\x1B[0m",bold:"\x1B[1m",dim:"\x1B[2m",red:"\x1B[31m",green:"\x1B[32m",yellow:"\x1B[33m",blue:"\x1B[34m",cyan:"\x1B[36m"},ir=class{env;rl;running=!0;history=[];isInteractive;constructor(e={}){let n={"/home/user/.bashrc":`# Virtual shell
143
143
  export PS1="\\u@virtual:\\w$ "
144
144
  `,"/home/user/.profile":`# User profile
145
- `,"/tmp/.keep":""};this.env=new ct({files:{...n,...e.files},cwd:e.cwd||"/home/user",env:{HOME:"/home/user",USER:"user",SHELL:"/bin/bash",TERM:"xterm-256color",...e.env},network:e.network!==!1?{dangerouslyAllowFullInternetAccess:!0}:void 0}),this.isInteractive=process.stdin.isTTY===!0,this.rl=vn.createInterface({input:process.stdin,output:process.stdout,terminal:this.isInteractive}),this.rl.on("SIGINT",()=>{process.stdout.write(`^C
145
+ `,"/tmp/.keep":""};this.env=new lt({files:{...n,...e.files},cwd:e.cwd||"/home/user",env:{HOME:"/home/user",USER:"user",SHELL:"/bin/bash",TERM:"xterm-256color",...e.env},network:e.network!==!1?{dangerouslyAllowFullInternetAccess:!0}:void 0}),this.isInteractive=process.stdin.isTTY===!0,this.rl=Rn.createInterface({input:process.stdin,output:process.stdout,terminal:this.isInteractive}),this.rl.on("SIGINT",()=>{process.stdout.write(`^C
146
146
  `),this.prompt()}),process.stdin.isTTY&&this.rl.on("close",()=>{this.running=!1,console.log(`
147
147
  Goodbye!`),process.exit(0)})}syncHistory(){let e=this.env.getEnv();e.BASH_HISTORY=JSON.stringify(this.history)}getPrompt(){let e=this.env.getCwd(),n=this.env.getEnv().HOME||"/home/user",r=e;return e===n?r="~":e.startsWith(`${n}/`)&&(r=`~${e.slice(n.length)}`),`${F.green}${F.bold}user@virtual${F.reset}:${F.blue}${F.bold}${r}${F.reset}$ `}async executeCommand(e){let n=e.trim();if(n){if(this.history.push(n),n==="exit"||n.startsWith("exit ")){let r=n.split(/\s+/),s=r[1]?parseInt(r[1],10):0;console.log("exit"),process.exit(s)}this.syncHistory();try{let r=await this.env.exec(n);r.stdout&&process.stdout.write(r.stdout),r.stderr&&process.stderr.write(`${F.red}${r.stderr}${F.reset}`)}catch(r){console.error(`${F.red}Error: ${K(r)}${F.reset}`)}}}printWelcome(){console.log(`
148
148
  ${F.cyan}${F.bold}\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2557
@@ -152,7 +152,7 @@ ${F.cyan}${F.bold}\u2554\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u2550\u
152
152
 
153
153
  Type ${F.green}help${F.reset} for available commands, ${F.green}exit${F.reset} to quit.
154
154
  All operations run on a virtual in-memory filesystem.
155
- `)}prompt(){this.rl.question(this.getPrompt(),async e=>{this.running&&(await this.executeCommand(e),this.prompt())})}async run(){if(this.isInteractive)this.printWelcome(),this.prompt();else{let e=[];this.rl.on("line",n=>{e.push(n)}),await new Promise(n=>{this.rl.on("close",n)});for(let n of e)await this.executeCommand(n)}}};function Fs(){let t=process.argv.slice(2),e={network:!0};for(let n=0;n<t.length;n++)if(t[n]==="--cwd"&&t[n+1])e.cwd=t[++n];else if(t[n]==="--files"&&t[n+1]){let r=t[++n];try{let s=$n.readFileSync(r,"utf-8");e.files=JSON.parse(s)}catch(s){console.error(`Error reading files from ${r}:`,s),process.exit(1)}}else t[n]==="--no-network"?e.network=!1:(t[n]==="--help"||t[n]==="-h")&&(console.log(`
155
+ `)}prompt(){this.rl.question(this.getPrompt(),async e=>{this.running&&(await this.executeCommand(e),this.prompt())})}async run(){if(this.isInteractive)this.printWelcome(),this.prompt();else{let e=[];this.rl.on("line",n=>{e.push(n)}),await new Promise(n=>{this.rl.on("close",n)});for(let n of e)await this.executeCommand(n)}}};function Us(){let t=process.argv.slice(2),e={network:!0};for(let n=0;n<t.length;n++)if(t[n]==="--cwd"&&t[n+1])e.cwd=t[++n];else if(t[n]==="--files"&&t[n+1]){let r=t[++n];try{let s=Pn.readFileSync(r,"utf-8");e.files=JSON.parse(s)}catch(s){console.error(`Error reading files from ${r}:`,s),process.exit(1)}}else t[n]==="--no-network"?e.network=!1:(t[n]==="--help"||t[n]==="-h")&&(console.log(`
156
156
  Usage: npx tsx src/cli/shell.ts [options]
157
157
 
158
158
  Options:
@@ -169,4 +169,4 @@ Network Access:
169
169
  Example:
170
170
  npx tsx src/cli/shell.ts --cwd /app --files ./my-files.json
171
171
  pnpm shell --no-network
172
- `),process.exit(0));return e}var Bs=Fs(),zs=new sr(Bs);zs.run();
172
+ `),process.exit(0));return e}var Gs=Us(),Hs=new ir(Gs);Hs.run();