just-bash 1.1.0 → 1.2.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.
Files changed (71) hide show
  1. package/dist/Bash.d.ts +18 -0
  2. package/dist/ai/index.d.ts +6 -1
  3. package/dist/bin/chunks/awk2-6XCE67AX.js +20 -0
  4. package/dist/bin/chunks/awk2-DKAZ7LPG.js +20 -0
  5. package/dist/bin/chunks/awk2-GAJ72X3Z.js +20 -0
  6. package/dist/bin/chunks/awk2-HJAPWU2N.js +18 -0
  7. package/dist/bin/chunks/awk2-LVXC5OAC.js +20 -0
  8. package/dist/bin/chunks/awk2-T6HR4EYO.js +20 -0
  9. package/dist/bin/chunks/awk2-UYU2UYZ2.js +20 -0
  10. package/dist/bin/chunks/awk2-W64AZN7P.js +20 -0
  11. package/dist/bin/chunks/find-7BUNQZX4.js +11 -0
  12. package/dist/bin/chunks/find-YATLECXJ.js +11 -0
  13. package/dist/bin/chunks/grep-PHBFGJCV.js +15 -0
  14. package/dist/bin/chunks/sed-B6FXUN7R.js +80 -0
  15. package/dist/bin/chunks/sed-RHLDIXPL.js +76 -0
  16. package/dist/bin/chunks/sed-SYSATJXR.js +75 -0
  17. package/dist/bin/just-bash.js +102 -102
  18. package/dist/bin/shell/chunks/awk2-6XCE67AX.js +20 -0
  19. package/dist/bin/shell/chunks/awk2-DKAZ7LPG.js +20 -0
  20. package/dist/bin/shell/chunks/awk2-GAJ72X3Z.js +20 -0
  21. package/dist/bin/shell/chunks/awk2-HJAPWU2N.js +18 -0
  22. package/dist/bin/shell/chunks/awk2-LVXC5OAC.js +20 -0
  23. package/dist/bin/shell/chunks/awk2-T6HR4EYO.js +20 -0
  24. package/dist/bin/shell/chunks/awk2-UYU2UYZ2.js +20 -0
  25. package/dist/bin/shell/chunks/awk2-W64AZN7P.js +20 -0
  26. package/dist/bin/shell/chunks/find-7BUNQZX4.js +11 -0
  27. package/dist/bin/shell/chunks/find-YATLECXJ.js +11 -0
  28. package/dist/bin/shell/chunks/grep-PHBFGJCV.js +15 -0
  29. package/dist/bin/shell/chunks/sed-B6FXUN7R.js +80 -0
  30. package/dist/bin/shell/chunks/sed-RHLDIXPL.js +76 -0
  31. package/dist/bin/shell/chunks/sed-SYSATJXR.js +75 -0
  32. package/dist/bin/shell/shell.js +124 -128
  33. package/dist/bundle/ai/index.js +423 -386
  34. package/dist/bundle/chunks/awk2-365HINHI.js +19 -0
  35. package/dist/bundle/chunks/awk2-7YCFY7D2.js +19 -0
  36. package/dist/bundle/chunks/awk2-7YL3P4KU.js +19 -0
  37. package/dist/bundle/chunks/awk2-ETXZHQQ4.js +19 -0
  38. package/dist/bundle/chunks/awk2-FMEL7ESV.js +19 -0
  39. package/dist/bundle/chunks/awk2-LE4QUFGK.js +19 -0
  40. package/dist/bundle/chunks/awk2-MGHA6JPJ.js +17 -0
  41. package/dist/bundle/chunks/awk2-XEAK5PBN.js +19 -0
  42. package/dist/bundle/chunks/find-KP4M5DWC.js +10 -0
  43. package/dist/bundle/chunks/find-WM6ZETA7.js +10 -0
  44. package/dist/bundle/chunks/grep-CZTYKNEE.js +14 -0
  45. package/dist/bundle/chunks/sed-2WWCANZT.js +79 -0
  46. package/dist/bundle/chunks/sed-5LZHAGFR.js +74 -0
  47. package/dist/bundle/chunks/sed-V7XSBPTD.js +75 -0
  48. package/dist/bundle/index.js +8 -8
  49. package/dist/commands/awk/ast.d.ts +201 -0
  50. package/dist/commands/awk/awk2.d.ts +7 -0
  51. package/dist/commands/awk/builtins.d.ts +17 -0
  52. package/dist/commands/awk/interpreter/context.d.ts +53 -0
  53. package/dist/commands/awk/interpreter/expressions.d.ts +17 -0
  54. package/dist/commands/awk/interpreter/fields.d.ts +25 -0
  55. package/dist/commands/awk/interpreter/helpers.d.ts +29 -0
  56. package/dist/commands/awk/interpreter/index.d.ts +8 -0
  57. package/dist/commands/awk/interpreter/interpreter.d.ts +50 -0
  58. package/dist/commands/awk/interpreter/statements.d.ts +11 -0
  59. package/dist/commands/awk/interpreter/types.d.ts +13 -0
  60. package/dist/commands/awk/interpreter/variables.d.ts +35 -0
  61. package/dist/commands/awk/interpreter2.d.ts +90 -0
  62. package/dist/commands/awk/lexer.d.ts +100 -0
  63. package/dist/commands/awk/parser2.d.ts +73 -0
  64. package/dist/commands/find/matcher.d.ts +6 -2
  65. package/dist/commands/find/types.d.ts +13 -0
  66. package/dist/commands/sed/executor.d.ts +1 -1
  67. package/dist/commands/sed/lexer.d.ts +74 -0
  68. package/dist/commands/sed/parser.d.ts +4 -0
  69. package/dist/commands/sed/types.d.ts +66 -2
  70. package/dist/index.d.ts +1 -1
  71. package/package.json +1 -1
@@ -0,0 +1,10 @@
1
+ import{a as N}from"./chunk-UJMN5NLH.js";import{a as A,b as z}from"./chunk-74CEPOFO.js";function x(e,s){switch(e.type){case"name":return{matches:N(s.name,e.pattern,e.ignoreCase),pruned:!1,printed:!1};case"path":return{matches:N(s.relativePath,e.pattern,e.ignoreCase),pruned:!1,printed:!1};case"regex":try{let t=e.ignoreCase?"i":"";return{matches:new RegExp(e.pattern,t).test(s.relativePath),pruned:!1,printed:!1}}catch{return{matches:!1,pruned:!1,printed:!1}}case"type":return e.fileType==="f"?{matches:s.isFile,pruned:!1,printed:!1}:e.fileType==="d"?{matches:s.isDirectory,pruned:!1,printed:!1}:{matches:!1,pruned:!1,printed:!1};case"empty":return{matches:s.isEmpty,pruned:!1,printed:!1};case"mtime":{let r=(Date.now()-s.mtime)/(1e3*60*60*24),n;return e.comparison==="more"?n=r>e.days:e.comparison==="less"?n=r<e.days:n=Math.floor(r)===e.days,{matches:n,pruned:!1,printed:!1}}case"newer":{let t=s.newerRefTimes.get(e.refPath);return t===void 0?{matches:!1,pruned:!1,printed:!1}:{matches:s.mtime>t,pruned:!1,printed:!1}}case"size":{let t=e.value;switch(e.unit){case"c":t=e.value;break;case"k":t=e.value*1024;break;case"M":t=e.value*1024*1024;break;case"G":t=e.value*1024*1024*1024;break;case"b":t=e.value*512;break}let r;return e.comparison==="more"?r=s.size>t:e.comparison==="less"?r=s.size<t:e.unit==="b"?r=Math.ceil(s.size/512)===e.value:r=s.size===t,{matches:r,pruned:!1,printed:!1}}case"perm":{let t=s.mode&511,r=e.mode&511,n;return e.matchType==="exact"?n=t===r:e.matchType==="all"?n=(t&r)===r:n=(t&r)!==0,{matches:n,pruned:!1,printed:!1}}case"prune":return{matches:!0,pruned:!0,printed:!1};case"print":return{matches:!0,pruned:!1,printed:!0};case"not":{let t=x(e.expr,s);return{matches:!t.matches,pruned:t.pruned,printed:!1}}case"and":{let t=x(e.left,s);if(!t.matches)return{matches:!1,pruned:t.pruned,printed:!1};let r=x(e.right,s);return{matches:r.matches,pruned:t.pruned||r.pruned,printed:t.printed||r.printed}}case"or":{let t=x(e.left,s);if(t.matches)return t;let r=x(e.right,s);return{matches:r.matches,pruned:t.pruned||r.pruned,printed:r.printed}}}}function I(e){let s=[],t=r=>{r&&(r.type==="newer"?s.push(r.refPath):r.type==="not"?t(r.expr):(r.type==="and"||r.type==="or")&&(t(r.left),t(r.right)))};return t(e),s}function $(e,s){let t=[],r=[],n=s;for(;n<e.length;){let l=e[n];if(l==="("||l==="\\("){t.push({type:"lparen"}),n++;continue}if(l===")"||l==="\\)"){t.push({type:"rparen"}),n++;continue}if(l==="-name"&&n+1<e.length)t.push({type:"expr",expr:{type:"name",pattern:e[++n]}});else if(l==="-iname"&&n+1<e.length)t.push({type:"expr",expr:{type:"name",pattern:e[++n],ignoreCase:!0}});else if(l==="-path"&&n+1<e.length)t.push({type:"expr",expr:{type:"path",pattern:e[++n]}});else if(l==="-ipath"&&n+1<e.length)t.push({type:"expr",expr:{type:"path",pattern:e[++n],ignoreCase:!0}});else if(l==="-regex"&&n+1<e.length)t.push({type:"expr",expr:{type:"regex",pattern:e[++n]}});else if(l==="-iregex"&&n+1<e.length)t.push({type:"expr",expr:{type:"regex",pattern:e[++n],ignoreCase:!0}});else if(l==="-type"&&n+1<e.length){let i=e[++n];if(i==="f"||i==="d")t.push({type:"expr",expr:{type:"type",fileType:i}});else return{expr:null,pathIndex:n,error:`find: Unknown argument to -type: ${i}
2
+ `,actions:[]}}else if(l==="-empty")t.push({type:"expr",expr:{type:"empty"}});else if(l==="-mtime"&&n+1<e.length){let i=e[++n],p="exact",f=i;i.startsWith("+")?(p="more",f=i.slice(1)):i.startsWith("-")&&(p="less",f=i.slice(1));let u=parseInt(f,10);Number.isNaN(u)||t.push({type:"expr",expr:{type:"mtime",days:u,comparison:p}})}else if(l==="-newer"&&n+1<e.length){let i=e[++n];t.push({type:"expr",expr:{type:"newer",refPath:i}})}else if(l==="-size"&&n+1<e.length){let i=e[++n],p="exact",f=i;i.startsWith("+")?(p="more",f=i.slice(1)):i.startsWith("-")&&(p="less",f=i.slice(1));let u=f.match(/^(\d+)([ckMGb])?$/);if(u){let m=parseInt(u[1],10),v=u[2]||"b";t.push({type:"expr",expr:{type:"size",value:m,unit:v,comparison:p}})}}else if(l==="-perm"&&n+1<e.length){let i=e[++n],p="exact",f=i;i.startsWith("-")?(p="all",f=i.slice(1)):i.startsWith("/")&&(p="any",f=i.slice(1));let u=parseInt(f,8);Number.isNaN(u)||t.push({type:"expr",expr:{type:"perm",mode:u,matchType:p}})}else if(l==="-prune")t.push({type:"expr",expr:{type:"prune"}});else if(l==="-not"||l==="!")t.push({type:"not"});else if(l==="-o"||l==="-or")t.push({type:"op",op:"or"});else if(l==="-a"||l==="-and")t.push({type:"op",op:"and"});else if(l==="-maxdepth"||l==="-mindepth")n++;else if(l==="-exec"){let i=[];for(n++;n<e.length&&e[n]!==";"&&e[n]!=="+";)i.push(e[n]),n++;if(n>=e.length)return{expr:null,pathIndex:n,error:"find: missing argument to `-exec'\n",actions:[]};let p=e[n]==="+";r.push({type:"exec",command:i,batchMode:p})}else if(l==="-print")t.push({type:"expr",expr:{type:"print"}}),r.push({type:"print"});else if(l==="-print0")r.push({type:"print0"});else if(l==="-delete")r.push({type:"delete"});else{if(l.startsWith("-"))return{expr:null,pathIndex:n,error:`find: unknown predicate '${l}'
3
+ `,actions:[]};if(t.length===0){n++;continue}break}n++}if(t.length===0)return{expr:null,pathIndex:n,actions:r};let g=S(t);return g.error?{expr:null,pathIndex:n,error:g.error,actions:r}:{expr:g.expr,pathIndex:n,actions:r}}function S(e){let s=0;function t(){let i=r();if(!i)return null;for(;s<e.length;){let p=e[s];if(p.type==="op"&&p.op==="or"){s++;let f=r();if(!f)return i;i={type:"or",left:i,right:f}}else break}return i}function r(){let i=n();if(!i)return null;for(;s<e.length;){let p=e[s];if(p.type==="op"&&p.op==="and"){s++;let f=n();if(!f)return i;i={type:"and",left:i,right:f}}else if(p.type==="expr"||p.type==="not"||p.type==="lparen"){let f=n();if(!f)return i;i={type:"and",left:i,right:f}}else break}return i}function n(){if(s<e.length&&e[s].type==="not"){s++;let i=n();return i?{type:"not",expr:i}:null}return g()}function g(){if(s>=e.length)return null;let i=e[s];if(i.type==="lparen"){s++;let p=t();return s<e.length&&e[s].type==="rparen"&&s++,p}return i.type==="expr"?(s++,i.expr):(i.type==="rparen",null)}return{expr:t()}}var F={name:"find",summary:"search for files in a directory hierarchy",usage:"find [path...] [expression]",options:["-name PATTERN file name matches shell pattern PATTERN","-iname PATTERN like -name but case insensitive","-path PATTERN file path matches shell pattern PATTERN","-ipath PATTERN like -path but case insensitive","-regex PATTERN file path matches regular expression PATTERN","-iregex PATTERN like -regex but case insensitive","-type TYPE file is of type: f (regular file), d (directory)","-empty file is empty or directory is empty","-mtime N file's data was modified N*24 hours ago","-newer FILE file was modified more recently than FILE","-size N[ckMGb] file uses N units of space (c=bytes, k=KB, M=MB, G=GB, b=512B blocks)","-perm MODE file's permission bits are exactly MODE (octal)","-perm -MODE all permission bits MODE are set","-perm /MODE any permission bits MODE are set","-maxdepth LEVELS descend at most LEVELS directories","-mindepth LEVELS do not apply tests at levels less than LEVELS","-prune do not descend into this directory","-not, ! negate the following expression","-a, -and logical AND (default)","-o, -or logical OR","-exec CMD {} ; execute CMD on each file ({} is replaced by filename)","-exec CMD {} + execute CMD with multiple files at once","-print print the full file name (default action)","-print0 print the full file name followed by a null character","-delete delete found files/directories"," --help display this help and exit"]},L=new Set(["-name","-iname","-path","-ipath","-regex","-iregex","-type","-maxdepth","-mindepth","-mtime","-newer","-size","-perm"]),K={name:"find",async execute(e,s){if(z(e))return A(F);let t=".",r=null,n=null;for(let a=0;a<e.length;a++){let o=e[a];if(o==="-maxdepth"&&a+1<e.length)r=parseInt(e[++a],10);else if(o==="-mindepth"&&a+1<e.length)n=parseInt(e[++a],10);else if(o==="-exec")for(a++;a<e.length&&e[a]!==";"&&e[a]!=="+";)a++;else!o.startsWith("-")&&o!==";"&&o!=="+"&&o!=="("&&o!==")"&&o!=="\\("&&o!=="\\)"?t=o:L.has(o)&&a++}let{expr:g,error:l,actions:i}=$(e,0);if(l)return{stdout:"",stderr:l,exitCode:1};let p=i.some(a=>a.type==="print"),f=i.length===0,u=s.fs.resolvePath(s.cwd,t);try{await s.fs.stat(u)}catch{return{stdout:"",stderr:`find: ${t}: No such file or directory
4
+ `,exitCode:1}}let m=[],v=I(g),C=new Map;for(let a of v){let o=s.fs.resolvePath(s.cwd,a);try{let c=await s.fs.stat(o);C.set(a,c.mtime?.getTime()??Date.now())}catch{}}async function R(a,o){if(r!==null&&o>r)return;let c;try{c=await s.fs.stat(a)}catch{return}if(!c)return;let d;a===u?d=t.split("/").pop()||t:d=a.split("/").pop()||"";let h=a===u?t:t==="."?`./${a.slice(u.length+1)}`:t+a.slice(u.length),y=null;c.isDirectory&&(y=await s.fs.readdir(a));let W=c.isFile?c.size===0:y!==null&&y.length===0,P=n===null||o>=n,k=!1,D=!1;if(P&&g!==null){let M={name:d,relativePath:h,isFile:c.isFile,isDirectory:c.isDirectory,isEmpty:W,mtime:c.mtime?.getTime()??Date.now(),size:c.size??0,mode:c.mode??420,newerRefTimes:C},E=x(g,M);P=E.matches,k=E.pruned,p?D=E.printed:D=P}else P&&(D=!0);if(D&&m.push(h),y!==null&&!k)for(let M of y){let E=a==="/"?`/${M}`:`${a}/${M}`;await R(E,o+1)}}await R(u,0);let w="",b="",T=0;if(i.length>0)for(let a of i)switch(a.type){case"print":w+=m.length>0?`${m.join(`
5
+ `)}
6
+ `:"";break;case"print0":w+=m.length>0?`${m.join("\0")}\0`:"";break;case"delete":{let o=[...m].sort((c,d)=>d.length-c.length);for(let c of o){let d=s.fs.resolvePath(s.cwd,c);try{await s.fs.rm(d,{recursive:!1})}catch(h){let y=h instanceof Error?h.message:String(h);b+=`find: cannot delete '${c}': ${y}
7
+ `,T=1}}break}case"exec":if(!s.exec)return{stdout:"",stderr:`find: -exec not supported in this context
8
+ `,exitCode:1};if(a.batchMode){let o=[];for(let h of a.command)h==="{}"?o.push(...m):o.push(h);let c=o.map(h=>`"${h}"`).join(" "),d=await s.exec(c);w+=d.stdout,b+=d.stderr,d.exitCode!==0&&(T=d.exitCode)}else for(let o of m){let d=a.command.map(y=>y==="{}"?o:y).map(y=>`"${y}"`).join(" "),h=await s.exec(d);w+=h.stdout,b+=h.stderr,h.exitCode!==0&&(T=h.exitCode)}break}else f&&(w=m.length>0?`${m.join(`
9
+ `)}
10
+ `:"");return{stdout:w,stderr:b,exitCode:T}}};export{K as findCommand};
@@ -0,0 +1,14 @@
1
+ import{a as v}from"./chunk-UJMN5NLH.js";import{a as q,b as G,c as U}from"./chunk-74CEPOFO.js";var J={name:"grep",summary:"print lines that match patterns",usage:"grep [OPTION]... PATTERN [FILE]...",options:["-E, --extended-regexp PATTERN is an extended regular expression","-F, --fixed-strings PATTERN is a set of newline-separated strings","-i, --ignore-case ignore case distinctions","-v, --invert-match select non-matching lines","-w, --word-regexp match only whole words","-x, --line-regexp match only whole lines","-c, --count print only a count of matching lines","-l, --files-with-matches print only names of files with matches","-L, --files-without-match print names of files with no matches","-m NUM, --max-count=NUM stop after NUM matches","-n, --line-number print line number with output lines","-h, --no-filename suppress the file name prefix on output","-o, --only-matching show only nonempty parts of lines that match","-q, --quiet, --silent suppress all normal output","-r, -R, --recursive search directories recursively","-A NUM print NUM lines of trailing context","-B NUM print NUM lines of leading context","-C NUM print NUM lines of context","-e PATTERN use PATTERN for matching"," --include=GLOB search only files matching GLOB"," --exclude=GLOB skip files matching GLOB"," --exclude-dir=DIR skip directories matching DIR"," --help display this help and exit"]},H={name:"grep",async execute(s,e){if(G(s))return q(J);let i=!1,o=!1,f=!1,a=!1,$=!1,d=!1,h=!1,m=!1,p=!1,g=!1,M=!1,P=!1,T=!1,y=!1,R=0,l=0,w=0,c=[],n=[],x=[],N=null,I=[];for(let u=0;u<s.length;u++){let r=s[u];if(r.startsWith("-")&&r!=="-"){if(r==="-e"&&u+1<s.length){N=s[++u];continue}if(r.startsWith("--include=")){c.push(r.slice(10));continue}if(r.startsWith("--exclude=")){n.push(r.slice(10));continue}if(r.startsWith("--exclude-dir=")){x.push(r.slice(14));continue}if(r.startsWith("--max-count=")){R=parseInt(r.slice(12),10);continue}let C=r.match(/^-m(\d+)$/);if(C){R=parseInt(C[1],10);continue}if(r==="-m"&&u+1<s.length){R=parseInt(s[++u],10);continue}let L=r.match(/^-([ABC])(\d+)$/);if(L){let t=parseInt(L[2],10);L[1]==="A"?w=t:L[1]==="B"?l=t:L[1]==="C"&&(l=t,w=t);continue}if((r==="-A"||r==="-B"||r==="-C")&&u+1<s.length){let t=parseInt(s[++u],10);r==="-A"?w=t:r==="-B"?l=t:(l=t,w=t);continue}let S=r.startsWith("--")?[r]:r.slice(1).split("");for(let t of S)if(t==="i"||t==="--ignore-case")i=!0;else if(t==="n"||t==="--line-number")o=!0;else if(t==="v"||t==="--invert-match")f=!0;else if(t==="c"||t==="--count")a=!0;else if(t==="l"||t==="--files-with-matches")$=!0;else if(t==="L"||t==="--files-without-match")d=!0;else if(t==="r"||t==="R"||t==="--recursive")h=!0;else if(t==="w"||t==="--word-regexp")m=!0;else if(t==="x"||t==="--line-regexp")p=!0;else if(t==="E"||t==="--extended-regexp")g=!0;else if(t==="F"||t==="--fixed-strings")M=!0;else if(t==="o"||t==="--only-matching")P=!0;else if(t==="h"||t==="--no-filename")T=!0;else if(t==="q"||t==="--quiet"||t==="--silent")y=!0;else{if(t.startsWith("--"))return U("grep",t);if(t.length===1)return U("grep",`-${t}`)}}else N===null?N=r:I.push(r)}if(N===null)return{stdout:"",stderr:`grep: missing pattern
2
+ `,exitCode:2};let E;M?E=N.replace(/[.*+?^${}()|[\]\\]/g,"\\$&"):g?E=N:E=K(N),m&&(E=`\\b${E}\\b`),p&&(E=`^${E}$`);let F;try{F=new RegExp(E,i?"gi":"g")}catch{return{stdout:"",stderr:`grep: invalid regular expression: ${N}
3
+ `,exitCode:2}}if(I.length===0&&e.stdin){let u=D(e.stdin,F,f,o,a,"",P,l,w,R);return y?{stdout:"",stderr:"",exitCode:u.matched?0:1}:{stdout:u.output,stderr:"",exitCode:u.matched?0:1}}if(I.length===0)return{stdout:"",stderr:`grep: no input files
4
+ `,exitCode:2};let A="",O="",b=!1,k=!1,B=[];for(let u of I)if(u.includes("*")||u.includes("?")||u.includes("[")){let r=await V(u,e);if(h)for(let C of r){let L=await Q(C,e,c,n,x);B.push(...L)}else B.push(...r)}else if(h){let r=await Q(u,e,c,n,x);B.push(...r)}else B.push(u);let z=(B.length>1||h)&&!T;for(let u of B){let r=u.split("/").pop()||u;if(!(n.length>0&&!h&&n.some(C=>v(r,C,{stripQuotes:!0})))&&!(c.length>0&&!h&&!c.some(C=>v(r,C,{stripQuotes:!0}))))try{let C=e.fs.resolvePath(e.cwd,u);if((await e.fs.stat(C)).isDirectory){h||(O+=`grep: ${u}: Is a directory
5
+ `);continue}let S=await e.fs.readFile(C),t=D(S,F,f,o,a,z?u:"",P,l,w,R);if(t.matched){if(b=!0,y)return{stdout:"",stderr:"",exitCode:0};$?A+=`${u}
6
+ `:d||(A+=t.output)}else d?A+=`${u}
7
+ `:a&&!$&&(A+=t.output)}catch{O+=`grep: ${u}: No such file or directory
8
+ `,k=!0}}let W;return k?W=2:d?W=A.length>0?0:1:W=b?0:1,y?{stdout:"",stderr:"",exitCode:W}:{stdout:A,stderr:O,exitCode:W}}};function K(s){let e="",i=0;for(;i<s.length;){let o=s[i];if(o==="\\"&&i+1<s.length){let f=s[i+1];if(f==="|"||f==="("||f===")"){e+=f,i+=2;continue}else if(f==="{"||f==="}"){e+=`\\${f}`,i+=2;continue}}o==="+"||o==="?"||o==="|"||o==="("||o===")"||o==="{"||o==="}"?e+=`\\${o}`:e+=o,i++}return e}function D(s,e,i,o,f,a,$=!1,d=0,h=0,m=0){let p=s.split(`
9
+ `),g=p.length,M=g>0&&p[g-1]===""?g-1:g;if(f){let l=0;for(let c=0;c<M;c++)e.lastIndex=0,e.test(p[c])!==i&&l++;return{output:`${a?`${a}:${l}`:String(l)}
10
+ `,matched:l>0}}if(d===0&&h===0){let l=[],w=!1,c=0;for(let n=0;n<M&&!(m>0&&c>=m);n++){let x=p[n];if(e.lastIndex=0,e.test(x)!==i)if(w=!0,c++,$){e.lastIndex=0;for(let I=e.exec(x);I!==null;I=e.exec(x))l.push(a?`${a}:${I[0]}`:I[0]),I[0].length===0&&e.lastIndex++}else o?l.push(a?`${a}:${n+1}:${x}`:`${n+1}:${x}`):l.push(a?`${a}:${x}`:x)}return{output:l.length>0?`${l.join(`
11
+ `)}
12
+ `:"",matched:w}}let P=[],T=0,y=new Set,R=[];for(let l=0;l<M&&!(m>0&&T>=m);l++)e.lastIndex=0,e.test(p[l])!==i&&(R.push(l),T++);for(let l of R){for(let c=Math.max(0,l-d);c<l;c++)if(!y.has(c)){y.add(c);let n=p[c];o&&(n=`${c+1}-${n}`),a&&(n=`${a}-${n}`),P.push(n)}if(!y.has(l)){y.add(l);let c=p[l];if($){e.lastIndex=0;for(let n=e.exec(c);n!==null;n=e.exec(c))P.push(a?`${a}:${n[0]}`:n[0]),n[0].length===0&&e.lastIndex++}else{let n=c;o&&(n=`${l+1}:${n}`),a&&(n=`${a}:${n}`),P.push(n)}}let w=Math.min(M-1,l+h);for(let c=l+1;c<=w;c++)if(!y.has(c)){y.add(c);let n=p[c];o&&(n=`${c+1}-${n}`),a&&(n=`${a}-${n}`),P.push(n)}}return{output:P.length>0?`${P.join(`
13
+ `)}
14
+ `:"",matched:T>0}}async function Q(s,e,i=[],o=[],f=[]){let a=e.fs.resolvePath(e.cwd,s),$=[];try{if(!(await e.fs.stat(a)).isDirectory){let p=s.split("/").pop()||s;return o.length>0&&o.some(g=>v(p,g,{stripQuotes:!0}))?[]:i.length>0&&!i.some(g=>v(p,g,{stripQuotes:!0}))?[]:[s]}let h=s.split("/").pop()||s;if(f.length>0&&f.some(p=>v(h,p,{stripQuotes:!0})))return[];let m=await e.fs.readdir(a);for(let p of m){if(p.startsWith("."))continue;let g=s==="."?p:`${s}/${p}`,M=await Q(g,e,i,o,f);$.push(...M)}}catch{}return $}async function V(s,e){let i=[],o=s.lastIndexOf("/"),f,a;if(o===-1?(f=e.cwd,a=s):(f=s.slice(0,o)||"/",a=s.slice(o+1)),s.includes("**")){let d=s.split("**"),h=d[0].replace(/\/$/,"")||".",m=d[1]||"";return await j(h,m,e,i),i}let $=e.fs.resolvePath(e.cwd,f);try{let d=await e.fs.readdir($);for(let h of d)if(v(h,a,{stripQuotes:!0})){let m=o===-1?h:`${f}/${h}`;i.push(m)}}catch{}return i.sort()}async function j(s,e,i,o){let f=i.fs.resolvePath(i.cwd,s);try{if(!(await i.fs.stat(f)).isDirectory){let d=s.split("/").pop()||"";if(e){let h=e.replace(/^\//,"");v(d,h,{stripQuotes:!0})&&o.push(s)}return}let $=await i.fs.readdir(f);for(let d of $){let h=s==="."?d:`${s}/${d}`,m=i.fs.resolvePath(i.cwd,h);if((await i.fs.stat(m)).isDirectory)await j(h,e,i,o);else if(e){let g=e.replace(/^\//,"");v(d,g,{stripQuotes:!0})&&o.push(h)}}}catch{}}var Z={name:"fgrep",async execute(s,e){return H.execute(["-F",...s],e)}},_={name:"egrep",async execute(s,e){return H.execute(["-E",...s],e)}};export{_ as egrepCommand,Z as fgrepCommand,H as grepCommand};
@@ -0,0 +1,79 @@
1
+ import{i as g}from"./chunk-JYYFSUWP.js";import{a as D,b as W,c as y}from"./chunk-74CEPOFO.js";var G=1e4;function U(i,e,t){return{patternSpace:"",holdSpace:"",lineNumber:0,totalLines:i,deleted:!1,printed:!1,quit:!1,quitSilent:!1,exitCode:void 0,appendBuffer:[],substitutionMade:!1,lineNumberOutput:[],restartCycle:!1,currentFilename:e,pendingFileReads:[],pendingFileWrites:[],pendingExecute:void 0,rangeStates:t||new Map}}function j(i){return typeof i=="object"&&"first"in i&&"step"in i}function N(i,e,t,n){if(i==="$")return e===t;if(typeof i=="number")return e===i;if(j(i)){let{first:r,step:s}=i;return s===0?e===r:(e-r)%s===0&&e>=r}if(typeof i=="object"&&"pattern"in i)try{return new RegExp(i.pattern).test(n)}catch{return!1}return!1}function Q(i){let e=t=>t===void 0?"undefined":t==="$"?"$":typeof t=="number"?String(t):"pattern"in t?`/${t.pattern}/`:"first"in t?`${t.first}~${t.step}`:"unknown";return`${e(i.start)},${e(i.end)}`}function k(i,e,t,n,r){if(!i||!i.start&&!i.end)return!0;let s=i.start,l=i.end;if(s!==void 0&&l===void 0)return N(s,e,t,n);if(s!==void 0&&l!==void 0){let h=typeof s=="object"&&"pattern"in s,p=typeof l=="object"&&"pattern"in l;if(!h&&!p){let o=typeof s=="number"?s:s==="$"?t:1,a=typeof l=="number"?l:t;return e>=o&&e<=a}if(r){let o=Q(i),a=r.get(o);return a||(a={active:!1},r.set(o,a)),a.active?(N(l,e,t,n)&&(a.active=!1,r.set(o,a)),!0):N(s,e,t,n)?(a.active=!0,a.startLine=e,r.set(o,a),N(l,e,t,n)&&(a.active=!1,r.set(o,a)),!0):!1}return N(s,e,t,n)}return!0}function K(i){let e="",t=0;for(;t<i.length;){if(i[t]==="\\"&&t+1<i.length){let n=i[t+1];if(n==="+"||n==="?"||n==="|"){e+=n,t+=2;continue}if(n==="("||n===")"){e+=n,t+=2;continue}if(n==="{"||n==="}"){e+=n,t+=2;continue}e+=i[t]+n,t+=2;continue}if(i[t]==="+"||i[t]==="?"||i[t]==="|"||i[t]==="("||i[t]===")"){e+=`\\${i[t]}`,t++;continue}e+=i[t],t++}return e}function J(i){let e="";for(let t=0;t<i.length;t++){let n=i[t],r=n.charCodeAt(0);n==="\\"?e+="\\\\":n===" "?e+="\\t":n===`
2
+ `?e+=`$
3
+ `:n==="\r"?e+="\\r":n==="\x07"?e+="\\a":n==="\b"?e+="\\b":n==="\f"?e+="\\f":n==="\v"?e+="\\v":r<32||r>=127?e+=`\\${r.toString(8).padStart(3,"0")}`:e+=n}return`${e}$`}function P(i,e,t){let n="",r=0;for(;r<i.length;){if(i[r]==="\\"&&r+1<i.length){let s=i[r+1];if(s==="&"){n+="&",r+=2;continue}if(s==="n"){n+=`
4
+ `,r+=2;continue}if(s==="t"){n+=" ",r+=2;continue}let l=parseInt(s,10);if(l>=1&&l<=9){n+=t[l-1]||"",r+=2;continue}n+=s,r+=2;continue}if(i[r]==="&"){n+=e,r++;continue}n+=i[r],r++}return n}function V(i,e){let{lineNumber:t,totalLines:n,patternSpace:r}=e;if(i.type!=="label"&&k(i.address,t,n,r,e.rangeStates))switch(i.type){case"substitute":{let s=i,l="";s.global&&(l+="g"),s.ignoreCase&&(l+="i");let h=s.extendedRegex?s.pattern:K(s.pattern);try{let p=new RegExp(h,l),f=p.test(e.patternSpace);if(p.lastIndex=0,f){if(e.substitutionMade=!0,s.nthOccurrence&&s.nthOccurrence>0&&!s.global){let o=0,a=s.nthOccurrence;e.patternSpace=e.patternSpace.replace(new RegExp(h,`g${s.ignoreCase?"i":""}`),(u,...m)=>{if(o++,o===a){let v=m.slice(0,-2);return P(s.replacement,u,v)}return u})}else e.patternSpace=e.patternSpace.replace(p,(o,...a)=>{let u=a.slice(0,-2);return P(s.replacement,o,u)});s.printOnMatch&&(e.printed=!0)}}catch{}break}case"print":e.printed=!0;break;case"printFirstLine":{let s=e.patternSpace.indexOf(`
5
+ `);s!==-1?e.lineNumberOutput.push(e.patternSpace.slice(0,s)):e.lineNumberOutput.push(e.patternSpace);break}case"delete":e.deleted=!0;break;case"deleteFirstLine":{let s=e.patternSpace.indexOf(`
6
+ `);s!==-1?(e.patternSpace=e.patternSpace.slice(s+1),e.restartCycle=!0):e.deleted=!0;break}case"zap":e.patternSpace="";break;case"append":e.appendBuffer.push(i.text);break;case"insert":e.appendBuffer.unshift(`__INSERT__${i.text}`);break;case"change":e.patternSpace=i.text,e.deleted=!0,e.appendBuffer.push(i.text);break;case"hold":e.holdSpace=e.patternSpace;break;case"holdAppend":e.holdSpace?e.holdSpace+=`
7
+ ${e.patternSpace}`:e.holdSpace=e.patternSpace;break;case"get":e.patternSpace=e.holdSpace;break;case"getAppend":e.patternSpace+=`
8
+ ${e.holdSpace}`;break;case"exchange":{let s=e.patternSpace;e.patternSpace=e.holdSpace,e.holdSpace=s;break}case"next":e.printed=!0;break;case"quit":e.quit=!0,i.exitCode!==void 0&&(e.exitCode=i.exitCode);break;case"quitSilent":e.quit=!0,e.quitSilent=!0,i.exitCode!==void 0&&(e.exitCode=i.exitCode);break;case"list":{let s=J(e.patternSpace);e.lineNumberOutput.push(s);break}case"printFilename":e.currentFilename&&e.lineNumberOutput.push(e.currentFilename);break;case"version":break;case"readFile":e.pendingFileReads.push({filename:i.filename,wholeFile:!0});break;case"readFileLine":e.pendingFileReads.push({filename:i.filename,wholeFile:!1});break;case"writeFile":e.pendingFileWrites.push({filename:i.filename,content:`${e.patternSpace}
9
+ `});break;case"writeFirstLine":{let s=e.patternSpace.indexOf(`
10
+ `),l=s!==-1?e.patternSpace.slice(0,s):e.patternSpace;e.pendingFileWrites.push({filename:i.filename,content:`${l}
11
+ `});break}case"execute":i.command?e.pendingExecute={command:i.command,replacePattern:!1}:e.pendingExecute={command:e.patternSpace,replacePattern:!0};break;case"transliterate":e.patternSpace=Y(e.patternSpace,i);break;case"lineNumber":e.lineNumberOutput.push(String(e.lineNumber));break;case"branch":break;case"branchOnSubst":break;case"branchOnNoSubst":break;case"group":break}}function Y(i,e){let t="";for(let n of i){let r=e.source.indexOf(n);r!==-1?t+=e.dest[r]:t+=n}return t}function A(i,e,t,n){let r=new Map;for(let f=0;f<i.length;f++){let o=i[f];o.type==="label"&&r.set(o.name,f)}let s=n?.maxIterations??G,l=0,h=0,p=0;for(;p<i.length;){if(l++,l>s)throw new g(`sed: command execution exceeded maximum iterations (${s})`,"iterations");if(e.deleted||e.quit||e.quitSilent||e.restartCycle)break;let f=i[p];if(f.type==="nextAppend"){if(k(f.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates))if(t&&t.currentLineIndex+h+1<t.lines.length){h++;let o=t.lines[t.currentLineIndex+h];e.patternSpace+=`
12
+ ${o}`,e.lineNumber=t.currentLineIndex+h+1}else{e.quit=!0,e.deleted=!0;break}p++;continue}if(f.type==="branch"){let o=f;if(k(o.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)){if(o.label){let a=r.get(o.label);if(a!==void 0){p=a;continue}}break}p++;continue}if(f.type==="branchOnSubst"){let o=f;if(k(o.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)&&e.substitutionMade){if(e.substitutionMade=!1,o.label){let a=r.get(o.label);if(a!==void 0){p=a;continue}}break}p++;continue}if(f.type==="branchOnNoSubst"){let o=f;if(k(o.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)&&!e.substitutionMade){if(o.label){let a=r.get(o.label);if(a!==void 0){p=a;continue}}break}p++;continue}if(f.type==="group"){let o=f;k(o.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)&&A(o.commands,e,t,n),p++;continue}V(f,e),p++}return h}var c;(function(i){i.NUMBER="NUMBER",i.DOLLAR="DOLLAR",i.PATTERN="PATTERN",i.STEP="STEP",i.LBRACE="LBRACE",i.RBRACE="RBRACE",i.SEMICOLON="SEMICOLON",i.NEWLINE="NEWLINE",i.COMMA="COMMA",i.COMMAND="COMMAND",i.SUBSTITUTE="SUBSTITUTE",i.TRANSLITERATE="TRANSLITERATE",i.LABEL_DEF="LABEL_DEF",i.BRANCH="BRANCH",i.BRANCH_ON_SUBST="BRANCH_ON_SUBST",i.BRANCH_ON_NO_SUBST="BRANCH_ON_NO_SUBST",i.TEXT_CMD="TEXT_CMD",i.FILE_READ="FILE_READ",i.FILE_READ_LINE="FILE_READ_LINE",i.FILE_WRITE="FILE_WRITE",i.FILE_WRITE_LINE="FILE_WRITE_LINE",i.EXECUTE="EXECUTE",i.EOF="EOF",i.ERROR="ERROR"})(c||(c={}));var I=class{input;pos=0;line=1;column=1;constructor(e){this.input=e}tokenize(){let e=[];for(;this.pos<this.input.length;){let t=this.nextToken();t&&e.push(t)}return e.push(this.makeToken(c.EOF,"")),e}makeToken(e,t,n){return{type:e,value:t,line:this.line,column:this.column,...n}}peek(e=0){return this.input[this.pos+e]||""}advance(){let e=this.input[this.pos++]||"";return e===`
13
+ `?(this.line++,this.column=1):this.column++,e}skipWhitespace(){for(;this.pos<this.input.length;){let e=this.peek();if(e===" "||e===" "||e==="\r")this.advance();else if(e==="#")for(;this.pos<this.input.length&&this.peek()!==`
14
+ `;)this.advance();else break}}nextToken(){if(this.skipWhitespace(),this.pos>=this.input.length)return null;let e=this.line,t=this.column,n=this.peek();return n===`
15
+ `?(this.advance(),{type:c.NEWLINE,value:`
16
+ `,line:e,column:t}):n===";"?(this.advance(),{type:c.SEMICOLON,value:";",line:e,column:t}):n==="{"?(this.advance(),{type:c.LBRACE,value:"{",line:e,column:t}):n==="}"?(this.advance(),{type:c.RBRACE,value:"}",line:e,column:t}):n===","?(this.advance(),{type:c.COMMA,value:",",line:e,column:t}):n==="$"?(this.advance(),{type:c.DOLLAR,value:"$",line:e,column:t}):this.isDigit(n)?this.readNumber():n==="/"?this.readPattern():n===":"?this.readLabelDef():this.readCommand()}readNumber(){let e=this.line,t=this.column,n="";for(;this.isDigit(this.peek());)n+=this.advance();if(this.peek()==="~"){this.advance();let r="";for(;this.isDigit(this.peek());)r+=this.advance();let s=parseInt(n,10),l=parseInt(r,10)||0;return{type:c.STEP,value:`${s}~${l}`,first:s,step:l,line:e,column:t}}return{type:c.NUMBER,value:parseInt(n,10),line:e,column:t}}readPattern(){let e=this.line,t=this.column;this.advance();let n="";for(;this.pos<this.input.length&&this.peek()!=="/";)if(this.peek()==="\\")n+=this.advance(),this.pos<this.input.length&&(n+=this.advance());else{if(this.peek()===`
17
+ `)break;n+=this.advance()}return this.peek()==="/"&&this.advance(),{type:c.PATTERN,value:n,pattern:n,line:e,column:t}}readLabelDef(){let e=this.line,t=this.column;this.advance();let n="";for(;this.pos<this.input.length;){let r=this.peek();if(r===" "||r===" "||r===`
18
+ `||r===";"||r==="}"||r==="{")break;n+=this.advance()}return{type:c.LABEL_DEF,value:n,label:n,line:e,column:t}}readCommand(){let e=this.line,t=this.column,n=this.advance();switch(n){case"s":return this.readSubstitute(e,t);case"y":return this.readTransliterate(e,t);case"a":case"i":case"c":return this.readTextCommand(n,e,t);case"b":return this.readBranch(c.BRANCH,"b",e,t);case"t":return this.readBranch(c.BRANCH_ON_SUBST,"t",e,t);case"T":return this.readBranch(c.BRANCH_ON_NO_SUBST,"T",e,t);case"r":return this.readFileCommand(c.FILE_READ,"r",e,t);case"R":return this.readFileCommand(c.FILE_READ_LINE,"R",e,t);case"w":return this.readFileCommand(c.FILE_WRITE,"w",e,t);case"W":return this.readFileCommand(c.FILE_WRITE_LINE,"W",e,t);case"e":return this.readExecute(e,t);case"p":case"P":case"d":case"D":case"h":case"H":case"g":case"G":case"x":case"n":case"N":case"q":case"Q":case"z":case"=":case"l":case"F":case"v":return{type:c.COMMAND,value:n,line:e,column:t};default:return{type:c.ERROR,value:n,line:e,column:t}}}readSubstitute(e,t){let n=this.advance();if(!n||n===`
19
+ `)return{type:c.ERROR,value:"s",line:e,column:t};let r="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")r+=this.advance(),this.pos<this.input.length&&this.peek()!==`
20
+ `&&(r+=this.advance());else{if(this.peek()===`
21
+ `)break;r+=this.advance()}if(this.peek()!==n)return{type:c.ERROR,value:"unterminated substitution pattern",line:e,column:t};this.advance();let s="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")s+=this.advance(),this.pos<this.input.length&&this.peek()!==`
22
+ `&&(s+=this.advance());else{if(this.peek()===`
23
+ `)break;s+=this.advance()}this.peek()===n&&this.advance();let l="";for(;this.pos<this.input.length;){let h=this.peek();if(h==="g"||h==="i"||h==="p"||h==="I"||this.isDigit(h))l+=this.advance();else break}return{type:c.SUBSTITUTE,value:`s${n}${r}${n}${s}${n}${l}`,pattern:r,replacement:s,flags:l,line:e,column:t}}readTransliterate(e,t){let n=this.advance();if(!n||n===`
24
+ `)return{type:c.ERROR,value:"y",line:e,column:t};let r="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\"){this.advance();let l=this.advance();l==="n"?r+=`
25
+ `:l==="t"?r+=" ":r+=l}else{if(this.peek()===`
26
+ `)break;r+=this.advance()}if(this.peek()!==n)return{type:c.ERROR,value:"unterminated transliteration source",line:e,column:t};this.advance();let s="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\"){this.advance();let l=this.advance();l==="n"?s+=`
27
+ `:l==="t"?s+=" ":s+=l}else{if(this.peek()===`
28
+ `)break;s+=this.advance()}return this.peek()===n&&this.advance(),{type:c.TRANSLITERATE,value:`y${n}${r}${n}${s}${n}`,source:r,dest:s,line:e,column:t}}readTextCommand(e,t,n){this.peek()==="\\"&&this.advance(),this.peek()===" "&&this.advance();let r="";for(;this.pos<this.input.length&&this.peek()!==`
29
+ `;)r+=this.advance();return{type:c.TEXT_CMD,value:e,text:r.trim(),line:t,column:n}}readBranch(e,t,n,r){for(;this.peek()===" "||this.peek()===" ";)this.advance();let s="";for(;this.pos<this.input.length;){let l=this.peek();if(l===" "||l===" "||l===`
30
+ `||l===";"||l==="}"||l==="{")break;s+=this.advance()}return{type:e,value:t,label:s||void 0,line:n,column:r}}readFileCommand(e,t,n,r){for(;this.peek()===" "||this.peek()===" ";)this.advance();let s="";for(;this.pos<this.input.length;){let l=this.peek();if(l===`
31
+ `||l===";")break;s+=this.advance()}return{type:e,value:t,filename:s.trim(),line:n,column:r}}readExecute(e,t){for(;this.peek()===" "||this.peek()===" ";)this.advance();let n="";for(;this.pos<this.input.length;){let r=this.peek();if(r===`
32
+ `||r===";")break;n+=this.advance()}return{type:c.EXECUTE,value:"e",command:n.trim()||void 0,line:e,column:t}}isDigit(e){return e>="0"&&e<="9"}};var O=class{scripts;tokens=[];pos=0;extendedRegex=!1;constructor(e,t=!1){this.scripts=e,this.extendedRegex=t}parse(){let e=[];for(let t of this.scripts){let n=new I(t);for(this.tokens=n.tokenize(),this.pos=0;!this.isAtEnd();){if(this.check(c.NEWLINE)||this.check(c.SEMICOLON)){this.advance();continue}let r=this.parseCommand();if(r.error)return{commands:[],error:r.error};r.command&&e.push(r.command)}}return{commands:e}}parseCommand(){let e=this.parseAddressRange();for(;this.check(c.NEWLINE)||this.check(c.SEMICOLON);)this.advance();if(this.isAtEnd())return e&&(e.start!==void 0||e.end!==void 0)?{command:{type:"print",address:e}}:{command:null};let t=this.peek();switch(t.type){case c.COMMAND:return this.parseSimpleCommand(t,e);case c.SUBSTITUTE:return this.parseSubstituteFromToken(t,e);case c.TRANSLITERATE:return this.parseTransliterateFromToken(t,e);case c.LABEL_DEF:return this.advance(),{command:{type:"label",name:t.label||""}};case c.BRANCH:return this.advance(),{command:{type:"branch",address:e,label:t.label}};case c.BRANCH_ON_SUBST:return this.advance(),{command:{type:"branchOnSubst",address:e,label:t.label}};case c.BRANCH_ON_NO_SUBST:return this.advance(),{command:{type:"branchOnNoSubst",address:e,label:t.label}};case c.TEXT_CMD:return this.advance(),this.parseTextCommand(t,e);case c.FILE_READ:return this.advance(),{command:{type:"readFile",address:e,filename:t.filename||""}};case c.FILE_READ_LINE:return this.advance(),{command:{type:"readFileLine",address:e,filename:t.filename||""}};case c.FILE_WRITE:return this.advance(),{command:{type:"writeFile",address:e,filename:t.filename||""}};case c.FILE_WRITE_LINE:return this.advance(),{command:{type:"writeFirstLine",address:e,filename:t.filename||""}};case c.EXECUTE:return this.advance(),{command:{type:"execute",address:e,command:t.command}};case c.LBRACE:return this.parseGroup(e);case c.RBRACE:return{command:null};case c.ERROR:return{command:null,error:`invalid command: ${t.value}`};default:return e&&(e.start!==void 0||e.end!==void 0)?{command:{type:"print",address:e}}:{command:null}}}parseSimpleCommand(e,t){this.advance();let n=e.value;switch(n){case"p":return{command:{type:"print",address:t}};case"P":return{command:{type:"printFirstLine",address:t}};case"d":return{command:{type:"delete",address:t}};case"D":return{command:{type:"deleteFirstLine",address:t}};case"h":return{command:{type:"hold",address:t}};case"H":return{command:{type:"holdAppend",address:t}};case"g":return{command:{type:"get",address:t}};case"G":return{command:{type:"getAppend",address:t}};case"x":return{command:{type:"exchange",address:t}};case"n":return{command:{type:"next",address:t}};case"N":return{command:{type:"nextAppend",address:t}};case"q":return{command:{type:"quit",address:t}};case"Q":return{command:{type:"quitSilent",address:t}};case"z":return{command:{type:"zap",address:t}};case"=":return{command:{type:"lineNumber",address:t}};case"l":return{command:{type:"list",address:t}};case"F":return{command:{type:"printFilename",address:t}};case"v":return{command:{type:"version",address:t}};default:return{command:null,error:`unknown command: ${n}`}}}parseSubstituteFromToken(e,t){this.advance();let n=e.flags||"",r,s=n.match(/(\d+)/);return s&&(r=parseInt(s[1],10)),{command:{type:"substitute",address:t,pattern:e.pattern||"",replacement:e.replacement||"",global:n.includes("g"),ignoreCase:n.includes("i")||n.includes("I"),printOnMatch:n.includes("p"),nthOccurrence:r,extendedRegex:this.extendedRegex}}}parseTransliterateFromToken(e,t){this.advance();let n=e.source||"",r=e.dest||"";return n.length!==r.length?{command:null,error:"transliteration sets must have same length"}:{command:{type:"transliterate",address:t,source:n,dest:r}}}parseTextCommand(e,t){let n=e.value,r=e.text||"";switch(n){case"a":return{command:{type:"append",address:t,text:r}};case"i":return{command:{type:"insert",address:t,text:r}};case"c":return{command:{type:"change",address:t,text:r}};default:return{command:null,error:`unknown text command: ${n}`}}}parseGroup(e){this.advance();let t=[];for(;!this.isAtEnd()&&!this.check(c.RBRACE);){if(this.check(c.NEWLINE)||this.check(c.SEMICOLON)){this.advance();continue}let n=this.parseCommand();if(n.error)return{command:null,error:n.error};n.command&&t.push(n.command)}return this.check(c.RBRACE)?(this.advance(),{command:{type:"group",address:e,commands:t}}):{command:null,error:"unmatched brace in grouped commands"}}parseAddressRange(){let e=this.parseAddress();if(e===void 0)return;let t;return this.check(c.COMMA)&&(this.advance(),t=this.parseAddress()),{start:e,end:t}}parseAddress(){let e=this.peek();switch(e.type){case c.NUMBER:return this.advance(),e.value;case c.DOLLAR:return this.advance(),"$";case c.PATTERN:return this.advance(),{pattern:e.pattern||e.value};case c.STEP:return this.advance(),{first:e.first||0,step:e.step||0};default:return}}peek(){return this.tokens[this.pos]||{type:c.EOF,value:"",line:0,column:0}}advance(){return this.isAtEnd()||this.pos++,this.tokens[this.pos-1]}check(e){return this.peek().type===e}isAtEnd(){return this.peek().type===c.EOF}};function q(i,e=!1){return new O(i,e).parse()}var Z={name:"sed",summary:"stream editor for filtering and transforming text",usage:"sed [OPTION]... {script} [input-file]...",options:["-n, --quiet, --silent suppress automatic printing of pattern space","-e script add the script to commands to be executed","-f script-file read script from file","-i, --in-place edit files in place","-E, -r, --regexp-extended use extended regular expressions"," --help display this help and exit"],description:`Commands:
33
+ s/regexp/replacement/[flags] substitute
34
+ d delete pattern space
35
+ p print pattern space
36
+ a\\ text append text after line
37
+ i\\ text insert text before line
38
+ c\\ text change (replace) line with text
39
+ h copy pattern space to hold space
40
+ H append pattern space to hold space
41
+ g copy hold space to pattern space
42
+ G append hold space to pattern space
43
+ x exchange pattern and hold spaces
44
+ n read next line into pattern space
45
+ N append next line to pattern space
46
+ y/source/dest/ transliterate characters
47
+ = print line number
48
+ l list pattern space (escape special chars)
49
+ b [label] branch to label
50
+ t [label] branch on substitution
51
+ T [label] branch if no substitution
52
+ :label define label
53
+ q quit
54
+ Q quit without printing
55
+
56
+ Addresses:
57
+ N line number
58
+ $ last line
59
+ /regexp/ lines matching regexp
60
+ N,M range from line N to M
61
+ first~step every step-th line starting at first`};async function F(i,e,t,n={}){let{limits:r,filename:s,fs:l,cwd:h}=n,p=i.split(`
62
+ `);p.length>0&&p[p.length-1]===""&&p.pop();let f=p.length,o="",a,u="",m=new Map,v=new Map,x=new Map,L=new Map,H=r?{maxIterations:r.maxSedIterations}:void 0;for(let C=0;C<p.length;C++){let d={...U(f,s,m),patternSpace:p[C],holdSpace:u,lineNumber:C+1,totalLines:f,substitutionMade:!1},_={lines:p,currentLineIndex:C},$=0,X=1e4,B=0;do{if($++,$>X)break;d.restartCycle=!1,d.pendingFileReads=[],d.pendingFileWrites=[];let E=A(e,d,_,H);if(B+=E,l&&h){for(let S of d.pendingFileReads){let b=l.resolvePath(h,S.filename);try{if(S.wholeFile){let R=await l.readFile(b);d.appendBuffer.push(R.replace(/\n$/,""))}else{if(!v.has(b)){let z=await l.readFile(b);v.set(b,z.split(`
63
+ `)),x.set(b,0)}let R=v.get(b),w=x.get(b);R&&w!==void 0&&w<R.length&&(d.appendBuffer.push(R[w]),x.set(b,w+1))}}catch{}}for(let S of d.pendingFileWrites){let b=l.resolvePath(h,S.filename),R=L.get(b)||"";L.set(b,R+S.content)}}_.currentLineIndex+=E}while(d.restartCycle&&!d.deleted&&!d.quit&&!d.quitSilent);C+=B,u=d.holdSpace;for(let E of d.lineNumberOutput)o+=`${E}
64
+ `;let M=[],T=[];for(let E of d.appendBuffer)E.startsWith("__INSERT__")?M.push(E.slice(10)):T.push(E);for(let E of M)o+=`${E}
65
+ `;!d.deleted&&!d.quitSilent&&(t?d.printed&&(o+=`${d.patternSpace}
66
+ `):o+=`${d.patternSpace}
67
+ `);for(let E of T)o+=`${E}
68
+ `;if(d.quit||d.quitSilent){d.exitCode!==void 0&&(a=d.exitCode);break}}if(l&&h)for(let[C,d]of L)try{await l.writeFile(C,d)}catch{}return{output:o,exitCode:a}}var oe={name:"sed",async execute(i,e){if(W(i))return D(Z);let t=[],n=[],r=!1,s=!1,l=!1,h=[];for(let a=0;a<i.length;a++){let u=i[a];if(u==="-n"||u==="--quiet"||u==="--silent")r=!0;else if(u==="-i"||u==="--in-place")s=!0;else if(u.startsWith("-i"))s=!0;else if(u==="-E"||u==="-r"||u==="--regexp-extended")l=!0;else if(u==="-e")a+1<i.length&&t.push(i[++a]);else if(u==="-f")a+1<i.length&&n.push(i[++a]);else{if(u.startsWith("--"))return y("sed",u);if(u.startsWith("-")&&u.length>1){for(let m of u.slice(1))if(m!=="n"&&m!=="e"&&m!=="f"&&m!=="i"&&m!=="E"&&m!=="r")return y("sed",`-${m}`);u.includes("n")&&(r=!0),u.includes("i")&&(s=!0),(u.includes("E")||u.includes("r"))&&(l=!0),u.includes("e")&&!u.includes("n")&&!u.includes("i")&&a+1<i.length&&t.push(i[++a]),u.includes("f")&&!u.includes("e")&&a+1<i.length&&n.push(i[++a])}else!u.startsWith("-")&&t.length===0&&n.length===0?t.push(u):u.startsWith("-")||h.push(u)}}for(let a of n){let u=e.fs.resolvePath(e.cwd,a);try{let m=await e.fs.readFile(u);for(let v of m.split(`
69
+ `)){let x=v.trim();x&&!x.startsWith("#")&&t.push(x)}}catch{return{stdout:"",stderr:`sed: couldn't open file ${a}: No such file or directory
70
+ `,exitCode:1}}}if(t.length===0)return{stdout:"",stderr:`sed: no script specified
71
+ `,exitCode:1};let{commands:p,error:f}=q(t,l);if(f)return{stdout:"",stderr:`sed: ${f}
72
+ `,exitCode:1};if(p.length===0)return{stdout:"",stderr:`sed: no valid commands
73
+ `,exitCode:1};let o="";if(h.length===0){o=e.stdin;try{let a=await F(o,p,r,{limits:e.limits,fs:e.fs,cwd:e.cwd});return{stdout:a.output,stderr:"",exitCode:a.exitCode??0}}catch(a){if(a instanceof g)return{stdout:"",stderr:`sed: ${a.message}
74
+ `,exitCode:g.EXIT_CODE};throw a}}if(s){for(let a of h){let u=e.fs.resolvePath(e.cwd,a);try{let m=await e.fs.readFile(u),v=await F(m,p,r,{limits:e.limits,filename:a,fs:e.fs,cwd:e.cwd});await e.fs.writeFile(u,v.output)}catch(m){return m instanceof g?{stdout:"",stderr:`sed: ${m.message}
75
+ `,exitCode:g.EXIT_CODE}:{stdout:"",stderr:`sed: ${a}: No such file or directory
76
+ `,exitCode:1}}}return{stdout:"",stderr:"",exitCode:0}}for(let a of h){let u=e.fs.resolvePath(e.cwd,a);try{o+=await e.fs.readFile(u)}catch(m){return m instanceof g?{stdout:"",stderr:`sed: ${m.message}
77
+ `,exitCode:g.EXIT_CODE}:{stdout:"",stderr:`sed: ${a}: No such file or directory
78
+ `,exitCode:1}}}try{let a=await F(o,p,r,{limits:e.limits,filename:h.length===1?h[0]:void 0,fs:e.fs,cwd:e.cwd});return{stdout:a.output,stderr:"",exitCode:a.exitCode??0}}catch(a){if(a instanceof g)return{stdout:"",stderr:`sed: ${a.message}
79
+ `,exitCode:g.EXIT_CODE};throw a}}};export{oe as sedCommand};
@@ -0,0 +1,74 @@
1
+ import{i as O}from"./chunk-JYYFSUWP.js";import{a as A,b as y,c as k}from"./chunk-74CEPOFO.js";var M=1e4;function _(i,e){return{patternSpace:"",holdSpace:"",lineNumber:0,totalLines:i,deleted:!1,printed:!1,quit:!1,quitSilent:!1,exitCode:void 0,appendBuffer:[],substitutionMade:!1,lineNumberOutput:[],restartCycle:!1,currentFilename:e,pendingFileReads:[],pendingFileWrites:[],pendingExecute:void 0}}function T(i){return typeof i=="object"&&"first"in i&&"step"in i}function w(i,e,t,n){if(i==="$")return e===t;if(typeof i=="number")return e===i;if(T(i)){let{first:r,step:s}=i;return s===0?e===r:(e-r)%s===0&&e>=r}if(typeof i=="object"&&"pattern"in i)try{return new RegExp(i.pattern).test(n)}catch{return!1}return!1}function g(i,e,t,n){if(!i||!i.start&&!i.end)return!0;let r=i.start,s=i.end;if(r!==void 0&&s===void 0)return w(r,e,t,n);if(r!==void 0&&s!==void 0){let l=typeof r=="number"?r:r==="$"?t:1,h=typeof s=="number"?s:t;return typeof r=="object"&&"pattern"in r&&w(r,e,t,n)?!0:e>=l&&e<=h}return!0}function $(i){let e="";for(let t=0;t<i.length;t++){let n=i[t],r=n.charCodeAt(0);n==="\\"?e+="\\\\":n===" "?e+="\\t":n===`
2
+ `?e+=`$
3
+ `:n==="\r"?e+="\\r":n==="\x07"?e+="\\a":n==="\b"?e+="\\b":n==="\f"?e+="\\f":n==="\v"?e+="\\v":r<32||r>=127?e+=`\\${r.toString(8).padStart(3,"0")}`:e+=n}return`${e}$`}function F(i,e,t){let n="",r=0;for(;r<i.length;){if(i[r]==="\\"&&r+1<i.length){let s=i[r+1];if(s==="&"){n+="&",r+=2;continue}if(s==="n"){n+=`
4
+ `,r+=2;continue}if(s==="t"){n+=" ",r+=2;continue}let l=parseInt(s,10);if(l>=1&&l<=9){n+=t[l-1]||"",r+=2;continue}n+=s,r+=2;continue}if(i[r]==="&"){n+=e,r++;continue}n+=i[r],r++}return n}function D(i,e){let{lineNumber:t,totalLines:n,patternSpace:r}=e;if(i.type!=="label"&&g(i.address,t,n,r))switch(i.type){case"substitute":{let s=i,l="";s.global&&(l+="g"),s.ignoreCase&&(l+="i");try{let h=new RegExp(s.pattern,l),p=e.patternSpace;if(s.nthOccurrence&&s.nthOccurrence>0&&!s.global){let o=0,f=s.nthOccurrence;e.patternSpace=e.patternSpace.replace(new RegExp(s.pattern,`g${s.ignoreCase?"i":""}`),(u,...c)=>{if(o++,o===f){let m=c.slice(0,-2);return F(s.replacement,u,m)}return u})}else e.patternSpace=e.patternSpace.replace(h,(o,...f)=>{let u=f.slice(0,-2);return F(s.replacement,o,u)});p!==e.patternSpace&&(e.substitutionMade=!0,s.printOnMatch&&(e.printed=!0))}catch{}break}case"print":e.printed=!0;break;case"printFirstLine":{let s=e.patternSpace.indexOf(`
5
+ `);s!==-1?e.lineNumberOutput.push(e.patternSpace.slice(0,s)):e.lineNumberOutput.push(e.patternSpace);break}case"delete":e.deleted=!0;break;case"deleteFirstLine":{let s=e.patternSpace.indexOf(`
6
+ `);s!==-1?(e.patternSpace=e.patternSpace.slice(s+1),e.restartCycle=!0):e.deleted=!0;break}case"zap":e.patternSpace="";break;case"append":e.appendBuffer.push(i.text);break;case"insert":e.appendBuffer.unshift(`__INSERT__${i.text}`);break;case"change":e.patternSpace=i.text,e.deleted=!0,e.appendBuffer.push(i.text);break;case"hold":e.holdSpace=e.patternSpace;break;case"holdAppend":e.holdSpace?e.holdSpace+=`
7
+ ${e.patternSpace}`:e.holdSpace=e.patternSpace;break;case"get":e.patternSpace=e.holdSpace;break;case"getAppend":e.patternSpace+=`
8
+ ${e.holdSpace}`;break;case"exchange":{let s=e.patternSpace;e.patternSpace=e.holdSpace,e.holdSpace=s;break}case"next":e.printed=!0;break;case"quit":e.quit=!0,i.exitCode!==void 0&&(e.exitCode=i.exitCode);break;case"quitSilent":e.quit=!0,e.quitSilent=!0,i.exitCode!==void 0&&(e.exitCode=i.exitCode);break;case"list":{let s=$(e.patternSpace);e.lineNumberOutput.push(s);break}case"printFilename":e.currentFilename&&e.lineNumberOutput.push(e.currentFilename);break;case"version":break;case"readFile":e.pendingFileReads.push({filename:i.filename,wholeFile:!0});break;case"readFileLine":e.pendingFileReads.push({filename:i.filename,wholeFile:!1});break;case"writeFile":e.pendingFileWrites.push({filename:i.filename,content:`${e.patternSpace}
9
+ `});break;case"writeFirstLine":{let s=e.patternSpace.indexOf(`
10
+ `),l=s!==-1?e.patternSpace.slice(0,s):e.patternSpace;e.pendingFileWrites.push({filename:i.filename,content:`${l}
11
+ `});break}case"execute":i.command?e.pendingExecute={command:i.command,replacePattern:!1}:e.pendingExecute={command:e.patternSpace,replacePattern:!0};break;case"transliterate":e.patternSpace=W(e.patternSpace,i);break;case"lineNumber":e.lineNumberOutput.push(String(e.lineNumber));break;case"branch":break;case"branchOnSubst":break;case"branchOnNoSubst":break;case"group":break}}function W(i,e){let t="";for(let n of i){let r=e.source.indexOf(n);r!==-1?t+=e.dest[r]:t+=n}return t}function R(i,e,t,n){let r=new Map;for(let d=0;d<i.length;d++){let o=i[d];o.type==="label"&&r.set(o.name,d)}let s=n?.maxIterations??M,l=0,h=0,p=0;for(;p<i.length;){if(l++,l>s)throw new O(`sed: command execution exceeded maximum iterations (${s})`,"iterations");if(e.deleted||e.quit||e.quitSilent||e.restartCycle)break;let d=i[p];if(d.type==="nextAppend"){if(g(d.address,e.lineNumber,e.totalLines,e.patternSpace))if(t&&t.currentLineIndex+h+1<t.lines.length){h++;let o=t.lines[t.currentLineIndex+h];e.patternSpace+=`
12
+ ${o}`,e.lineNumber=t.currentLineIndex+h+1}else{e.quit=!0,e.deleted=!0;break}p++;continue}if(d.type==="branch"){let o=d;if(g(o.address,e.lineNumber,e.totalLines,e.patternSpace)){if(o.label){let f=r.get(o.label);if(f!==void 0){p=f;continue}}break}p++;continue}if(d.type==="branchOnSubst"){let o=d;if(g(o.address,e.lineNumber,e.totalLines,e.patternSpace)&&e.substitutionMade){if(e.substitutionMade=!1,o.label){let f=r.get(o.label);if(f!==void 0){p=f;continue}}break}p++;continue}if(d.type==="branchOnNoSubst"){let o=d;if(g(o.address,e.lineNumber,e.totalLines,e.patternSpace)&&!e.substitutionMade){if(o.label){let f=r.get(o.label);if(f!==void 0){p=f;continue}}break}p++;continue}if(d.type==="group"){let o=d;g(o.address,e.lineNumber,e.totalLines,e.patternSpace)&&R(o.commands,e,t,n),p++;continue}D(d,e),p++}return h}var a;(function(i){i.NUMBER="NUMBER",i.DOLLAR="DOLLAR",i.PATTERN="PATTERN",i.STEP="STEP",i.LBRACE="LBRACE",i.RBRACE="RBRACE",i.SEMICOLON="SEMICOLON",i.NEWLINE="NEWLINE",i.COMMA="COMMA",i.COMMAND="COMMAND",i.SUBSTITUTE="SUBSTITUTE",i.TRANSLITERATE="TRANSLITERATE",i.LABEL_DEF="LABEL_DEF",i.BRANCH="BRANCH",i.BRANCH_ON_SUBST="BRANCH_ON_SUBST",i.BRANCH_ON_NO_SUBST="BRANCH_ON_NO_SUBST",i.TEXT_CMD="TEXT_CMD",i.FILE_READ="FILE_READ",i.FILE_READ_LINE="FILE_READ_LINE",i.FILE_WRITE="FILE_WRITE",i.FILE_WRITE_LINE="FILE_WRITE_LINE",i.EXECUTE="EXECUTE",i.EOF="EOF",i.ERROR="ERROR"})(a||(a={}));var C=class{input;pos=0;line=1;column=1;constructor(e){this.input=e}tokenize(){let e=[];for(;this.pos<this.input.length;){let t=this.nextToken();t&&e.push(t)}return e.push(this.makeToken(a.EOF,"")),e}makeToken(e,t,n){return{type:e,value:t,line:this.line,column:this.column,...n}}peek(e=0){return this.input[this.pos+e]||""}advance(){let e=this.input[this.pos++]||"";return e===`
13
+ `?(this.line++,this.column=1):this.column++,e}skipWhitespace(){for(;this.pos<this.input.length;){let e=this.peek();if(e===" "||e===" "||e==="\r")this.advance();else if(e==="#")for(;this.pos<this.input.length&&this.peek()!==`
14
+ `;)this.advance();else break}}nextToken(){if(this.skipWhitespace(),this.pos>=this.input.length)return null;let e=this.line,t=this.column,n=this.peek();return n===`
15
+ `?(this.advance(),{type:a.NEWLINE,value:`
16
+ `,line:e,column:t}):n===";"?(this.advance(),{type:a.SEMICOLON,value:";",line:e,column:t}):n==="{"?(this.advance(),{type:a.LBRACE,value:"{",line:e,column:t}):n==="}"?(this.advance(),{type:a.RBRACE,value:"}",line:e,column:t}):n===","?(this.advance(),{type:a.COMMA,value:",",line:e,column:t}):n==="$"?(this.advance(),{type:a.DOLLAR,value:"$",line:e,column:t}):this.isDigit(n)?this.readNumber():n==="/"?this.readPattern():n===":"?this.readLabelDef():this.readCommand()}readNumber(){let e=this.line,t=this.column,n="";for(;this.isDigit(this.peek());)n+=this.advance();if(this.peek()==="~"){this.advance();let r="";for(;this.isDigit(this.peek());)r+=this.advance();let s=parseInt(n,10),l=parseInt(r,10)||0;return{type:a.STEP,value:`${s}~${l}`,first:s,step:l,line:e,column:t}}return{type:a.NUMBER,value:parseInt(n,10),line:e,column:t}}readPattern(){let e=this.line,t=this.column;this.advance();let n="";for(;this.pos<this.input.length&&this.peek()!=="/";)if(this.peek()==="\\")n+=this.advance(),this.pos<this.input.length&&(n+=this.advance());else{if(this.peek()===`
17
+ `)break;n+=this.advance()}return this.peek()==="/"&&this.advance(),{type:a.PATTERN,value:n,pattern:n,line:e,column:t}}readLabelDef(){let e=this.line,t=this.column;this.advance();let n="";for(;this.pos<this.input.length;){let r=this.peek();if(r===" "||r===" "||r===`
18
+ `||r===";"||r==="}"||r==="{")break;n+=this.advance()}return{type:a.LABEL_DEF,value:n,label:n,line:e,column:t}}readCommand(){let e=this.line,t=this.column,n=this.advance();switch(n){case"s":return this.readSubstitute(e,t);case"y":return this.readTransliterate(e,t);case"a":case"i":case"c":return this.readTextCommand(n,e,t);case"b":return this.readBranch(a.BRANCH,"b",e,t);case"t":return this.readBranch(a.BRANCH_ON_SUBST,"t",e,t);case"T":return this.readBranch(a.BRANCH_ON_NO_SUBST,"T",e,t);case"r":return this.readFileCommand(a.FILE_READ,"r",e,t);case"R":return this.readFileCommand(a.FILE_READ_LINE,"R",e,t);case"w":return this.readFileCommand(a.FILE_WRITE,"w",e,t);case"W":return this.readFileCommand(a.FILE_WRITE_LINE,"W",e,t);case"e":return this.readExecute(e,t);case"p":case"P":case"d":case"D":case"h":case"H":case"g":case"G":case"x":case"n":case"N":case"q":case"Q":case"z":case"=":case"l":case"F":case"v":return{type:a.COMMAND,value:n,line:e,column:t};default:return{type:a.ERROR,value:n,line:e,column:t}}}readSubstitute(e,t){let n=this.advance();if(!n||n===`
19
+ `)return{type:a.ERROR,value:"s",line:e,column:t};let r="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")r+=this.advance(),this.pos<this.input.length&&this.peek()!==`
20
+ `&&(r+=this.advance());else{if(this.peek()===`
21
+ `)break;r+=this.advance()}if(this.peek()!==n)return{type:a.ERROR,value:"unterminated substitution pattern",line:e,column:t};this.advance();let s="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")s+=this.advance(),this.pos<this.input.length&&this.peek()!==`
22
+ `&&(s+=this.advance());else{if(this.peek()===`
23
+ `)break;s+=this.advance()}this.peek()===n&&this.advance();let l="";for(;this.pos<this.input.length;){let h=this.peek();if(h==="g"||h==="i"||h==="p"||h==="I"||this.isDigit(h))l+=this.advance();else break}return{type:a.SUBSTITUTE,value:`s${n}${r}${n}${s}${n}${l}`,pattern:r,replacement:s,flags:l,line:e,column:t}}readTransliterate(e,t){let n=this.advance();if(!n||n===`
24
+ `)return{type:a.ERROR,value:"y",line:e,column:t};let r="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\"){this.advance();let l=this.advance();l==="n"?r+=`
25
+ `:l==="t"?r+=" ":r+=l}else{if(this.peek()===`
26
+ `)break;r+=this.advance()}if(this.peek()!==n)return{type:a.ERROR,value:"unterminated transliteration source",line:e,column:t};this.advance();let s="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\"){this.advance();let l=this.advance();l==="n"?s+=`
27
+ `:l==="t"?s+=" ":s+=l}else{if(this.peek()===`
28
+ `)break;s+=this.advance()}return this.peek()===n&&this.advance(),{type:a.TRANSLITERATE,value:`y${n}${r}${n}${s}${n}`,source:r,dest:s,line:e,column:t}}readTextCommand(e,t,n){this.peek()==="\\"&&this.advance(),this.peek()===" "&&this.advance();let r="";for(;this.pos<this.input.length&&this.peek()!==`
29
+ `;)r+=this.advance();return{type:a.TEXT_CMD,value:e,text:r.trim(),line:t,column:n}}readBranch(e,t,n,r){for(;this.peek()===" "||this.peek()===" ";)this.advance();let s="";for(;this.pos<this.input.length;){let l=this.peek();if(l===" "||l===" "||l===`
30
+ `||l===";"||l==="}"||l==="{")break;s+=this.advance()}return{type:e,value:t,label:s||void 0,line:n,column:r}}readFileCommand(e,t,n,r){for(;this.peek()===" "||this.peek()===" ";)this.advance();let s="";for(;this.pos<this.input.length;){let l=this.peek();if(l===`
31
+ `||l===";")break;s+=this.advance()}return{type:e,value:t,filename:s.trim(),line:n,column:r}}readExecute(e,t){for(;this.peek()===" "||this.peek()===" ";)this.advance();let n="";for(;this.pos<this.input.length;){let r=this.peek();if(r===`
32
+ `||r===";")break;n+=this.advance()}return{type:a.EXECUTE,value:"e",command:n.trim()||void 0,line:e,column:t}}isDigit(e){return e>="0"&&e<="9"}};var x=class{scripts;tokens=[];pos=0;extendedRegex=!1;constructor(e,t=!1){this.scripts=e,this.extendedRegex=t}parse(){let e=[];for(let t of this.scripts){let n=new C(t);for(this.tokens=n.tokenize(),this.pos=0;!this.isAtEnd();){if(this.check(a.NEWLINE)||this.check(a.SEMICOLON)){this.advance();continue}let r=this.parseCommand();if(r.error)return{commands:[],error:r.error};r.command&&e.push(r.command)}}return{commands:e}}parseCommand(){let e=this.parseAddressRange();for(;this.check(a.NEWLINE)||this.check(a.SEMICOLON);)this.advance();if(this.isAtEnd())return e&&(e.start!==void 0||e.end!==void 0)?{command:{type:"print",address:e}}:{command:null};let t=this.peek();switch(t.type){case a.COMMAND:return this.parseSimpleCommand(t,e);case a.SUBSTITUTE:return this.parseSubstituteFromToken(t,e);case a.TRANSLITERATE:return this.parseTransliterateFromToken(t,e);case a.LABEL_DEF:return this.advance(),{command:{type:"label",name:t.label||""}};case a.BRANCH:return this.advance(),{command:{type:"branch",address:e,label:t.label}};case a.BRANCH_ON_SUBST:return this.advance(),{command:{type:"branchOnSubst",address:e,label:t.label}};case a.BRANCH_ON_NO_SUBST:return this.advance(),{command:{type:"branchOnNoSubst",address:e,label:t.label}};case a.TEXT_CMD:return this.advance(),this.parseTextCommand(t,e);case a.FILE_READ:return this.advance(),{command:{type:"readFile",address:e,filename:t.filename||""}};case a.FILE_READ_LINE:return this.advance(),{command:{type:"readFileLine",address:e,filename:t.filename||""}};case a.FILE_WRITE:return this.advance(),{command:{type:"writeFile",address:e,filename:t.filename||""}};case a.FILE_WRITE_LINE:return this.advance(),{command:{type:"writeFirstLine",address:e,filename:t.filename||""}};case a.EXECUTE:return this.advance(),{command:{type:"execute",address:e,command:t.command}};case a.LBRACE:return this.parseGroup(e);case a.RBRACE:return{command:null};case a.ERROR:return{command:null,error:`invalid command: ${t.value}`};default:return e&&(e.start!==void 0||e.end!==void 0)?{command:{type:"print",address:e}}:{command:null}}}parseSimpleCommand(e,t){this.advance();let n=e.value;switch(n){case"p":return{command:{type:"print",address:t}};case"P":return{command:{type:"printFirstLine",address:t}};case"d":return{command:{type:"delete",address:t}};case"D":return{command:{type:"deleteFirstLine",address:t}};case"h":return{command:{type:"hold",address:t}};case"H":return{command:{type:"holdAppend",address:t}};case"g":return{command:{type:"get",address:t}};case"G":return{command:{type:"getAppend",address:t}};case"x":return{command:{type:"exchange",address:t}};case"n":return{command:{type:"next",address:t}};case"N":return{command:{type:"nextAppend",address:t}};case"q":return{command:{type:"quit",address:t}};case"Q":return{command:{type:"quitSilent",address:t}};case"z":return{command:{type:"zap",address:t}};case"=":return{command:{type:"lineNumber",address:t}};case"l":return{command:{type:"list",address:t}};case"F":return{command:{type:"printFilename",address:t}};case"v":return{command:{type:"version",address:t}};default:return{command:null,error:`unknown command: ${n}`}}}parseSubstituteFromToken(e,t){this.advance();let n=e.flags||"",r,s=n.match(/(\d+)/);return s&&(r=parseInt(s[1],10)),{command:{type:"substitute",address:t,pattern:e.pattern||"",replacement:e.replacement||"",global:n.includes("g"),ignoreCase:n.includes("i")||n.includes("I"),printOnMatch:n.includes("p"),nthOccurrence:r,extendedRegex:this.extendedRegex}}}parseTransliterateFromToken(e,t){this.advance();let n=e.source||"",r=e.dest||"";return n.length!==r.length?{command:null,error:"transliteration sets must have same length"}:{command:{type:"transliterate",address:t,source:n,dest:r}}}parseTextCommand(e,t){let n=e.value,r=e.text||"";switch(n){case"a":return{command:{type:"append",address:t,text:r}};case"i":return{command:{type:"insert",address:t,text:r}};case"c":return{command:{type:"change",address:t,text:r}};default:return{command:null,error:`unknown text command: ${n}`}}}parseGroup(e){this.advance();let t=[];for(;!this.isAtEnd()&&!this.check(a.RBRACE);){if(this.check(a.NEWLINE)||this.check(a.SEMICOLON)){this.advance();continue}let n=this.parseCommand();if(n.error)return{command:null,error:n.error};n.command&&t.push(n.command)}return this.check(a.RBRACE)?(this.advance(),{command:{type:"group",address:e,commands:t}}):{command:null,error:"unmatched brace in grouped commands"}}parseAddressRange(){let e=this.parseAddress();if(e===void 0)return;let t;return this.check(a.COMMA)&&(this.advance(),t=this.parseAddress()),{start:e,end:t}}parseAddress(){let e=this.peek();switch(e.type){case a.NUMBER:return this.advance(),e.value;case a.DOLLAR:return this.advance(),"$";case a.PATTERN:return this.advance(),{pattern:e.pattern||e.value};case a.STEP:return this.advance(),{first:e.first||0,step:e.step||0};default:return}}peek(){return this.tokens[this.pos]||{type:a.EOF,value:"",line:0,column:0}}advance(){return this.isAtEnd()||this.pos++,this.tokens[this.pos-1]}check(e){return this.peek().type===e}isAtEnd(){return this.peek().type===a.EOF}};function B(i,e=!1){return new x(i,e).parse()}var U={name:"sed",summary:"stream editor for filtering and transforming text",usage:"sed [OPTION]... {script} [input-file]...",options:["-n, --quiet, --silent suppress automatic printing of pattern space","-e script add the script to commands to be executed","-f script-file read script from file","-i, --in-place edit files in place","-E, -r, --regexp-extended use extended regular expressions"," --help display this help and exit"],description:`Commands:
33
+ s/regexp/replacement/[flags] substitute
34
+ d delete pattern space
35
+ p print pattern space
36
+ a\\ text append text after line
37
+ i\\ text insert text before line
38
+ c\\ text change (replace) line with text
39
+ h copy pattern space to hold space
40
+ H append pattern space to hold space
41
+ g copy hold space to pattern space
42
+ G append hold space to pattern space
43
+ x exchange pattern and hold spaces
44
+ n read next line into pattern space
45
+ N append next line to pattern space
46
+ y/source/dest/ transliterate characters
47
+ = print line number
48
+ l list pattern space (escape special chars)
49
+ b [label] branch to label
50
+ t [label] branch on substitution
51
+ T [label] branch if no substitution
52
+ :label define label
53
+ q quit
54
+ Q quit without printing
55
+
56
+ Addresses:
57
+ N line number
58
+ $ last line
59
+ /regexp/ lines matching regexp
60
+ N,M range from line N to M
61
+ first~step every step-th line starting at first`};async function S(i,e,t,n={}){let{limits:r,filename:s}=n,l=i.split(`
62
+ `);l.length>0&&l[l.length-1]===""&&l.pop();let h=l.length,p="",d,o="",f=r?{maxIterations:r.maxSedIterations}:void 0;for(let u=0;u<l.length;u++){let c={..._(h,s),patternSpace:l[u],holdSpace:o,lineNumber:u+1,totalLines:h,substitutionMade:!1},m={lines:l,currentLineIndex:u},E=0,v=1e4,N=0;do{if(E++,E>v)break;c.restartCycle=!1;let b=R(e,c,m,f);N+=b,m.currentLineIndex+=b}while(c.restartCycle&&!c.deleted&&!c.quit&&!c.quitSilent);u+=N,o=c.holdSpace;for(let b of c.lineNumberOutput)p+=`${b}
63
+ `;let L=[],I=[];for(let b of c.appendBuffer)b.startsWith("__INSERT__")?L.push(b.slice(10)):I.push(b);for(let b of L)p+=`${b}
64
+ `;!c.deleted&&!c.quitSilent&&(t?c.printed&&(p+=`${c.patternSpace}
65
+ `):p+=`${c.patternSpace}
66
+ `);for(let b of I)p+=`${b}
67
+ `;if(c.quit||c.quitSilent){c.exitCode!==void 0&&(d=c.exitCode);break}}return{output:p,exitCode:d}}var J={name:"sed",async execute(i,e){if(y(i))return A(U);let t=[],n=[],r=!1,s=!1,l=!1,h=[];for(let u=0;u<i.length;u++){let c=i[u];if(c==="-n"||c==="--quiet"||c==="--silent")r=!0;else if(c==="-i"||c==="--in-place")s=!0;else if(c.startsWith("-i"))s=!0;else if(c==="-E"||c==="-r"||c==="--regexp-extended")l=!0;else if(c==="-e")u+1<i.length&&t.push(i[++u]);else if(c==="-f")u+1<i.length&&n.push(i[++u]);else{if(c.startsWith("--"))return k("sed",c);if(c.startsWith("-")&&c.length>1){for(let m of c.slice(1))if(m!=="n"&&m!=="e"&&m!=="f"&&m!=="i"&&m!=="E"&&m!=="r")return k("sed",`-${m}`);c.includes("n")&&(r=!0),c.includes("i")&&(s=!0),(c.includes("E")||c.includes("r"))&&(l=!0),c.includes("e")&&!c.includes("n")&&!c.includes("i")&&u+1<i.length&&t.push(i[++u]),c.includes("f")&&!c.includes("e")&&u+1<i.length&&n.push(i[++u])}else!c.startsWith("-")&&t.length===0&&n.length===0?t.push(c):c.startsWith("-")||h.push(c)}}for(let u of n){let c=e.fs.resolvePath(e.cwd,u);try{let m=await e.fs.readFile(c);for(let E of m.split(`
68
+ `)){let v=E.trim();v&&!v.startsWith("#")&&t.push(v)}}catch{return{stdout:"",stderr:`sed: couldn't open file ${u}: No such file or directory
69
+ `,exitCode:1}}}if(t.length===0)return{stdout:"",stderr:`sed: no script specified
70
+ `,exitCode:1};let{commands:p,error:d}=B(t,l);if(d)return{stdout:"",stderr:`sed: ${d}
71
+ `,exitCode:1};if(p.length===0)return{stdout:"",stderr:`sed: no valid commands
72
+ `,exitCode:1};let o="";if(h.length===0){o=e.stdin;let u=await S(o,p,r,{limits:e.limits,fs:e.fs,cwd:e.cwd});return{stdout:u.output,stderr:"",exitCode:u.exitCode??0}}if(s){for(let u of h){let c=e.fs.resolvePath(e.cwd,u);try{let m=await e.fs.readFile(c),E=await S(m,p,r,{limits:e.limits,filename:u,fs:e.fs,cwd:e.cwd});await e.fs.writeFile(c,E.output)}catch{return{stdout:"",stderr:`sed: ${u}: No such file or directory
73
+ `,exitCode:1}}}return{stdout:"",stderr:"",exitCode:0}}for(let u of h){let c=e.fs.resolvePath(e.cwd,u);try{o+=await e.fs.readFile(c)}catch{return{stdout:"",stderr:`sed: ${u}: No such file or directory
74
+ `,exitCode:1}}}let f=await S(o,p,r,{limits:e.limits,filename:h.length===1?h[0]:void 0,fs:e.fs,cwd:e.cwd});return{stdout:f.output,stderr:"",exitCode:f.exitCode??0}}};export{J as sedCommand};
@@ -0,0 +1,75 @@
1
+ import{i as W}from"./chunk-JYYFSUWP.js";import{a as T,b as D,c as I}from"./chunk-74CEPOFO.js";var G=1e4;function U(i,e,t){return{patternSpace:"",holdSpace:"",lineNumber:0,totalLines:i,deleted:!1,printed:!1,quit:!1,quitSilent:!1,exitCode:void 0,appendBuffer:[],substitutionMade:!1,lineNumberOutput:[],restartCycle:!1,currentFilename:e,pendingFileReads:[],pendingFileWrites:[],pendingExecute:void 0,rangeStates:t||new Map}}function j(i){return typeof i=="object"&&"first"in i&&"step"in i}function N(i,e,t,n){if(i==="$")return e===t;if(typeof i=="number")return e===i;if(j(i)){let{first:r,step:s}=i;return s===0?e===r:(e-r)%s===0&&e>=r}if(typeof i=="object"&&"pattern"in i)try{return new RegExp(i.pattern).test(n)}catch{return!1}return!1}function Q(i){let e=t=>t===void 0?"undefined":t==="$"?"$":typeof t=="number"?String(t):"pattern"in t?`/${t.pattern}/`:"first"in t?`${t.first}~${t.step}`:"unknown";return`${e(i.start)},${e(i.end)}`}function k(i,e,t,n,r){if(!i||!i.start&&!i.end)return!0;let s=i.start,c=i.end;if(s!==void 0&&c===void 0)return N(s,e,t,n);if(s!==void 0&&c!==void 0){let d=typeof s=="object"&&"pattern"in s,h=typeof c=="object"&&"pattern"in c;if(!d&&!h){let l=typeof s=="number"?s:s==="$"?t:1,u=typeof c=="number"?c:t;return e>=l&&e<=u}if(r){let l=Q(i),u=r.get(l);return u||(u={active:!1},r.set(l,u)),u.active?(N(c,e,t,n)&&(u.active=!1,r.set(l,u)),!0):N(s,e,t,n)?(u.active=!0,u.startLine=e,r.set(l,u),N(c,e,t,n)&&(u.active=!1,r.set(l,u)),!0):!1}return N(s,e,t,n)}return!0}function K(i){let e="",t=0;for(;t<i.length;){if(i[t]==="\\"&&t+1<i.length){let n=i[t+1];if(n==="+"||n==="?"||n==="|"){e+=n,t+=2;continue}if(n==="("||n===")"){e+=n,t+=2;continue}if(n==="{"||n==="}"){e+=n,t+=2;continue}e+=i[t]+n,t+=2;continue}if(i[t]==="+"||i[t]==="?"||i[t]==="|"||i[t]==="("||i[t]===")"){e+=`\\${i[t]}`,t++;continue}e+=i[t],t++}return e}function J(i){let e="";for(let t=0;t<i.length;t++){let n=i[t],r=n.charCodeAt(0);n==="\\"?e+="\\\\":n===" "?e+="\\t":n===`
2
+ `?e+=`$
3
+ `:n==="\r"?e+="\\r":n==="\x07"?e+="\\a":n==="\b"?e+="\\b":n==="\f"?e+="\\f":n==="\v"?e+="\\v":r<32||r>=127?e+=`\\${r.toString(8).padStart(3,"0")}`:e+=n}return`${e}$`}function P(i,e,t){let n="",r=0;for(;r<i.length;){if(i[r]==="\\"&&r+1<i.length){let s=i[r+1];if(s==="&"){n+="&",r+=2;continue}if(s==="n"){n+=`
4
+ `,r+=2;continue}if(s==="t"){n+=" ",r+=2;continue}let c=parseInt(s,10);if(c>=1&&c<=9){n+=t[c-1]||"",r+=2;continue}n+=s,r+=2;continue}if(i[r]==="&"){n+=e,r++;continue}n+=i[r],r++}return n}function V(i,e){let{lineNumber:t,totalLines:n,patternSpace:r}=e;if(i.type!=="label"&&k(i.address,t,n,r,e.rangeStates))switch(i.type){case"substitute":{let s=i,c="";s.global&&(c+="g"),s.ignoreCase&&(c+="i");let d=s.extendedRegex?s.pattern:K(s.pattern);try{let h=new RegExp(d,c),m=h.test(e.patternSpace);if(h.lastIndex=0,m){if(e.substitutionMade=!0,s.nthOccurrence&&s.nthOccurrence>0&&!s.global){let l=0,u=s.nthOccurrence;e.patternSpace=e.patternSpace.replace(new RegExp(d,`g${s.ignoreCase?"i":""}`),(p,...o)=>{if(l++,l===u){let b=o.slice(0,-2);return P(s.replacement,p,b)}return p})}else e.patternSpace=e.patternSpace.replace(h,(l,...u)=>{let p=u.slice(0,-2);return P(s.replacement,l,p)});s.printOnMatch&&(e.printed=!0)}}catch{}break}case"print":e.printed=!0;break;case"printFirstLine":{let s=e.patternSpace.indexOf(`
5
+ `);s!==-1?e.lineNumberOutput.push(e.patternSpace.slice(0,s)):e.lineNumberOutput.push(e.patternSpace);break}case"delete":e.deleted=!0;break;case"deleteFirstLine":{let s=e.patternSpace.indexOf(`
6
+ `);s!==-1?(e.patternSpace=e.patternSpace.slice(s+1),e.restartCycle=!0):e.deleted=!0;break}case"zap":e.patternSpace="";break;case"append":e.appendBuffer.push(i.text);break;case"insert":e.appendBuffer.unshift(`__INSERT__${i.text}`);break;case"change":e.patternSpace=i.text,e.deleted=!0,e.appendBuffer.push(i.text);break;case"hold":e.holdSpace=e.patternSpace;break;case"holdAppend":e.holdSpace?e.holdSpace+=`
7
+ ${e.patternSpace}`:e.holdSpace=e.patternSpace;break;case"get":e.patternSpace=e.holdSpace;break;case"getAppend":e.patternSpace+=`
8
+ ${e.holdSpace}`;break;case"exchange":{let s=e.patternSpace;e.patternSpace=e.holdSpace,e.holdSpace=s;break}case"next":e.printed=!0;break;case"quit":e.quit=!0,i.exitCode!==void 0&&(e.exitCode=i.exitCode);break;case"quitSilent":e.quit=!0,e.quitSilent=!0,i.exitCode!==void 0&&(e.exitCode=i.exitCode);break;case"list":{let s=J(e.patternSpace);e.lineNumberOutput.push(s);break}case"printFilename":e.currentFilename&&e.lineNumberOutput.push(e.currentFilename);break;case"version":break;case"readFile":e.pendingFileReads.push({filename:i.filename,wholeFile:!0});break;case"readFileLine":e.pendingFileReads.push({filename:i.filename,wholeFile:!1});break;case"writeFile":e.pendingFileWrites.push({filename:i.filename,content:`${e.patternSpace}
9
+ `});break;case"writeFirstLine":{let s=e.patternSpace.indexOf(`
10
+ `),c=s!==-1?e.patternSpace.slice(0,s):e.patternSpace;e.pendingFileWrites.push({filename:i.filename,content:`${c}
11
+ `});break}case"execute":i.command?e.pendingExecute={command:i.command,replacePattern:!1}:e.pendingExecute={command:e.patternSpace,replacePattern:!0};break;case"transliterate":e.patternSpace=Y(e.patternSpace,i);break;case"lineNumber":e.lineNumberOutput.push(String(e.lineNumber));break;case"branch":break;case"branchOnSubst":break;case"branchOnNoSubst":break;case"group":break}}function Y(i,e){let t="";for(let n of i){let r=e.source.indexOf(n);r!==-1?t+=e.dest[r]:t+=n}return t}function y(i,e,t,n){let r=new Map;for(let m=0;m<i.length;m++){let l=i[m];l.type==="label"&&r.set(l.name,m)}let s=n?.maxIterations??G,c=0,d=0,h=0;for(;h<i.length;){if(c++,c>s)throw new W(`sed: command execution exceeded maximum iterations (${s})`,"iterations");if(e.deleted||e.quit||e.quitSilent||e.restartCycle)break;let m=i[h];if(m.type==="nextAppend"){if(k(m.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates))if(t&&t.currentLineIndex+d+1<t.lines.length){d++;let l=t.lines[t.currentLineIndex+d];e.patternSpace+=`
12
+ ${l}`,e.lineNumber=t.currentLineIndex+d+1}else{e.quit=!0,e.deleted=!0;break}h++;continue}if(m.type==="branch"){let l=m;if(k(l.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)){if(l.label){let u=r.get(l.label);if(u!==void 0){h=u;continue}}break}h++;continue}if(m.type==="branchOnSubst"){let l=m;if(k(l.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)&&e.substitutionMade){if(e.substitutionMade=!1,l.label){let u=r.get(l.label);if(u!==void 0){h=u;continue}}break}h++;continue}if(m.type==="branchOnNoSubst"){let l=m;if(k(l.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)&&!e.substitutionMade){if(l.label){let u=r.get(l.label);if(u!==void 0){h=u;continue}}break}h++;continue}if(m.type==="group"){let l=m;k(l.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates)&&y(l.commands,e,t,n),h++;continue}V(m,e),h++}return d}var a;(function(i){i.NUMBER="NUMBER",i.DOLLAR="DOLLAR",i.PATTERN="PATTERN",i.STEP="STEP",i.LBRACE="LBRACE",i.RBRACE="RBRACE",i.SEMICOLON="SEMICOLON",i.NEWLINE="NEWLINE",i.COMMA="COMMA",i.COMMAND="COMMAND",i.SUBSTITUTE="SUBSTITUTE",i.TRANSLITERATE="TRANSLITERATE",i.LABEL_DEF="LABEL_DEF",i.BRANCH="BRANCH",i.BRANCH_ON_SUBST="BRANCH_ON_SUBST",i.BRANCH_ON_NO_SUBST="BRANCH_ON_NO_SUBST",i.TEXT_CMD="TEXT_CMD",i.FILE_READ="FILE_READ",i.FILE_READ_LINE="FILE_READ_LINE",i.FILE_WRITE="FILE_WRITE",i.FILE_WRITE_LINE="FILE_WRITE_LINE",i.EXECUTE="EXECUTE",i.EOF="EOF",i.ERROR="ERROR"})(a||(a={}));var L=class{input;pos=0;line=1;column=1;constructor(e){this.input=e}tokenize(){let e=[];for(;this.pos<this.input.length;){let t=this.nextToken();t&&e.push(t)}return e.push(this.makeToken(a.EOF,"")),e}makeToken(e,t,n){return{type:e,value:t,line:this.line,column:this.column,...n}}peek(e=0){return this.input[this.pos+e]||""}advance(){let e=this.input[this.pos++]||"";return e===`
13
+ `?(this.line++,this.column=1):this.column++,e}skipWhitespace(){for(;this.pos<this.input.length;){let e=this.peek();if(e===" "||e===" "||e==="\r")this.advance();else if(e==="#")for(;this.pos<this.input.length&&this.peek()!==`
14
+ `;)this.advance();else break}}nextToken(){if(this.skipWhitespace(),this.pos>=this.input.length)return null;let e=this.line,t=this.column,n=this.peek();return n===`
15
+ `?(this.advance(),{type:a.NEWLINE,value:`
16
+ `,line:e,column:t}):n===";"?(this.advance(),{type:a.SEMICOLON,value:";",line:e,column:t}):n==="{"?(this.advance(),{type:a.LBRACE,value:"{",line:e,column:t}):n==="}"?(this.advance(),{type:a.RBRACE,value:"}",line:e,column:t}):n===","?(this.advance(),{type:a.COMMA,value:",",line:e,column:t}):n==="$"?(this.advance(),{type:a.DOLLAR,value:"$",line:e,column:t}):this.isDigit(n)?this.readNumber():n==="/"?this.readPattern():n===":"?this.readLabelDef():this.readCommand()}readNumber(){let e=this.line,t=this.column,n="";for(;this.isDigit(this.peek());)n+=this.advance();if(this.peek()==="~"){this.advance();let r="";for(;this.isDigit(this.peek());)r+=this.advance();let s=parseInt(n,10),c=parseInt(r,10)||0;return{type:a.STEP,value:`${s}~${c}`,first:s,step:c,line:e,column:t}}return{type:a.NUMBER,value:parseInt(n,10),line:e,column:t}}readPattern(){let e=this.line,t=this.column;this.advance();let n="";for(;this.pos<this.input.length&&this.peek()!=="/";)if(this.peek()==="\\")n+=this.advance(),this.pos<this.input.length&&(n+=this.advance());else{if(this.peek()===`
17
+ `)break;n+=this.advance()}return this.peek()==="/"&&this.advance(),{type:a.PATTERN,value:n,pattern:n,line:e,column:t}}readLabelDef(){let e=this.line,t=this.column;this.advance();let n="";for(;this.pos<this.input.length;){let r=this.peek();if(r===" "||r===" "||r===`
18
+ `||r===";"||r==="}"||r==="{")break;n+=this.advance()}return{type:a.LABEL_DEF,value:n,label:n,line:e,column:t}}readCommand(){let e=this.line,t=this.column,n=this.advance();switch(n){case"s":return this.readSubstitute(e,t);case"y":return this.readTransliterate(e,t);case"a":case"i":case"c":return this.readTextCommand(n,e,t);case"b":return this.readBranch(a.BRANCH,"b",e,t);case"t":return this.readBranch(a.BRANCH_ON_SUBST,"t",e,t);case"T":return this.readBranch(a.BRANCH_ON_NO_SUBST,"T",e,t);case"r":return this.readFileCommand(a.FILE_READ,"r",e,t);case"R":return this.readFileCommand(a.FILE_READ_LINE,"R",e,t);case"w":return this.readFileCommand(a.FILE_WRITE,"w",e,t);case"W":return this.readFileCommand(a.FILE_WRITE_LINE,"W",e,t);case"e":return this.readExecute(e,t);case"p":case"P":case"d":case"D":case"h":case"H":case"g":case"G":case"x":case"n":case"N":case"q":case"Q":case"z":case"=":case"l":case"F":case"v":return{type:a.COMMAND,value:n,line:e,column:t};default:return{type:a.ERROR,value:n,line:e,column:t}}}readSubstitute(e,t){let n=this.advance();if(!n||n===`
19
+ `)return{type:a.ERROR,value:"s",line:e,column:t};let r="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")r+=this.advance(),this.pos<this.input.length&&this.peek()!==`
20
+ `&&(r+=this.advance());else{if(this.peek()===`
21
+ `)break;r+=this.advance()}if(this.peek()!==n)return{type:a.ERROR,value:"unterminated substitution pattern",line:e,column:t};this.advance();let s="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")s+=this.advance(),this.pos<this.input.length&&this.peek()!==`
22
+ `&&(s+=this.advance());else{if(this.peek()===`
23
+ `)break;s+=this.advance()}this.peek()===n&&this.advance();let c="";for(;this.pos<this.input.length;){let d=this.peek();if(d==="g"||d==="i"||d==="p"||d==="I"||this.isDigit(d))c+=this.advance();else break}return{type:a.SUBSTITUTE,value:`s${n}${r}${n}${s}${n}${c}`,pattern:r,replacement:s,flags:c,line:e,column:t}}readTransliterate(e,t){let n=this.advance();if(!n||n===`
24
+ `)return{type:a.ERROR,value:"y",line:e,column:t};let r="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\"){this.advance();let c=this.advance();c==="n"?r+=`
25
+ `:c==="t"?r+=" ":r+=c}else{if(this.peek()===`
26
+ `)break;r+=this.advance()}if(this.peek()!==n)return{type:a.ERROR,value:"unterminated transliteration source",line:e,column:t};this.advance();let s="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\"){this.advance();let c=this.advance();c==="n"?s+=`
27
+ `:c==="t"?s+=" ":s+=c}else{if(this.peek()===`
28
+ `)break;s+=this.advance()}return this.peek()===n&&this.advance(),{type:a.TRANSLITERATE,value:`y${n}${r}${n}${s}${n}`,source:r,dest:s,line:e,column:t}}readTextCommand(e,t,n){this.peek()==="\\"&&this.advance(),this.peek()===" "&&this.advance();let r="";for(;this.pos<this.input.length&&this.peek()!==`
29
+ `;)r+=this.advance();return{type:a.TEXT_CMD,value:e,text:r.trim(),line:t,column:n}}readBranch(e,t,n,r){for(;this.peek()===" "||this.peek()===" ";)this.advance();let s="";for(;this.pos<this.input.length;){let c=this.peek();if(c===" "||c===" "||c===`
30
+ `||c===";"||c==="}"||c==="{")break;s+=this.advance()}return{type:e,value:t,label:s||void 0,line:n,column:r}}readFileCommand(e,t,n,r){for(;this.peek()===" "||this.peek()===" ";)this.advance();let s="";for(;this.pos<this.input.length;){let c=this.peek();if(c===`
31
+ `||c===";")break;s+=this.advance()}return{type:e,value:t,filename:s.trim(),line:n,column:r}}readExecute(e,t){for(;this.peek()===" "||this.peek()===" ";)this.advance();let n="";for(;this.pos<this.input.length;){let r=this.peek();if(r===`
32
+ `||r===";")break;n+=this.advance()}return{type:a.EXECUTE,value:"e",command:n.trim()||void 0,line:e,column:t}}isDigit(e){return e>="0"&&e<="9"}};var A=class{scripts;tokens=[];pos=0;extendedRegex=!1;constructor(e,t=!1){this.scripts=e,this.extendedRegex=t}parse(){let e=[];for(let t of this.scripts){let n=new L(t);for(this.tokens=n.tokenize(),this.pos=0;!this.isAtEnd();){if(this.check(a.NEWLINE)||this.check(a.SEMICOLON)){this.advance();continue}let r=this.parseCommand();if(r.error)return{commands:[],error:r.error};r.command&&e.push(r.command)}}return{commands:e}}parseCommand(){let e=this.parseAddressRange();for(;this.check(a.NEWLINE)||this.check(a.SEMICOLON);)this.advance();if(this.isAtEnd())return e&&(e.start!==void 0||e.end!==void 0)?{command:{type:"print",address:e}}:{command:null};let t=this.peek();switch(t.type){case a.COMMAND:return this.parseSimpleCommand(t,e);case a.SUBSTITUTE:return this.parseSubstituteFromToken(t,e);case a.TRANSLITERATE:return this.parseTransliterateFromToken(t,e);case a.LABEL_DEF:return this.advance(),{command:{type:"label",name:t.label||""}};case a.BRANCH:return this.advance(),{command:{type:"branch",address:e,label:t.label}};case a.BRANCH_ON_SUBST:return this.advance(),{command:{type:"branchOnSubst",address:e,label:t.label}};case a.BRANCH_ON_NO_SUBST:return this.advance(),{command:{type:"branchOnNoSubst",address:e,label:t.label}};case a.TEXT_CMD:return this.advance(),this.parseTextCommand(t,e);case a.FILE_READ:return this.advance(),{command:{type:"readFile",address:e,filename:t.filename||""}};case a.FILE_READ_LINE:return this.advance(),{command:{type:"readFileLine",address:e,filename:t.filename||""}};case a.FILE_WRITE:return this.advance(),{command:{type:"writeFile",address:e,filename:t.filename||""}};case a.FILE_WRITE_LINE:return this.advance(),{command:{type:"writeFirstLine",address:e,filename:t.filename||""}};case a.EXECUTE:return this.advance(),{command:{type:"execute",address:e,command:t.command}};case a.LBRACE:return this.parseGroup(e);case a.RBRACE:return{command:null};case a.ERROR:return{command:null,error:`invalid command: ${t.value}`};default:return e&&(e.start!==void 0||e.end!==void 0)?{command:{type:"print",address:e}}:{command:null}}}parseSimpleCommand(e,t){this.advance();let n=e.value;switch(n){case"p":return{command:{type:"print",address:t}};case"P":return{command:{type:"printFirstLine",address:t}};case"d":return{command:{type:"delete",address:t}};case"D":return{command:{type:"deleteFirstLine",address:t}};case"h":return{command:{type:"hold",address:t}};case"H":return{command:{type:"holdAppend",address:t}};case"g":return{command:{type:"get",address:t}};case"G":return{command:{type:"getAppend",address:t}};case"x":return{command:{type:"exchange",address:t}};case"n":return{command:{type:"next",address:t}};case"N":return{command:{type:"nextAppend",address:t}};case"q":return{command:{type:"quit",address:t}};case"Q":return{command:{type:"quitSilent",address:t}};case"z":return{command:{type:"zap",address:t}};case"=":return{command:{type:"lineNumber",address:t}};case"l":return{command:{type:"list",address:t}};case"F":return{command:{type:"printFilename",address:t}};case"v":return{command:{type:"version",address:t}};default:return{command:null,error:`unknown command: ${n}`}}}parseSubstituteFromToken(e,t){this.advance();let n=e.flags||"",r,s=n.match(/(\d+)/);return s&&(r=parseInt(s[1],10)),{command:{type:"substitute",address:t,pattern:e.pattern||"",replacement:e.replacement||"",global:n.includes("g"),ignoreCase:n.includes("i")||n.includes("I"),printOnMatch:n.includes("p"),nthOccurrence:r,extendedRegex:this.extendedRegex}}}parseTransliterateFromToken(e,t){this.advance();let n=e.source||"",r=e.dest||"";return n.length!==r.length?{command:null,error:"transliteration sets must have same length"}:{command:{type:"transliterate",address:t,source:n,dest:r}}}parseTextCommand(e,t){let n=e.value,r=e.text||"";switch(n){case"a":return{command:{type:"append",address:t,text:r}};case"i":return{command:{type:"insert",address:t,text:r}};case"c":return{command:{type:"change",address:t,text:r}};default:return{command:null,error:`unknown text command: ${n}`}}}parseGroup(e){this.advance();let t=[];for(;!this.isAtEnd()&&!this.check(a.RBRACE);){if(this.check(a.NEWLINE)||this.check(a.SEMICOLON)){this.advance();continue}let n=this.parseCommand();if(n.error)return{command:null,error:n.error};n.command&&t.push(n.command)}return this.check(a.RBRACE)?(this.advance(),{command:{type:"group",address:e,commands:t}}):{command:null,error:"unmatched brace in grouped commands"}}parseAddressRange(){let e=this.parseAddress();if(e===void 0)return;let t;return this.check(a.COMMA)&&(this.advance(),t=this.parseAddress()),{start:e,end:t}}parseAddress(){let e=this.peek();switch(e.type){case a.NUMBER:return this.advance(),e.value;case a.DOLLAR:return this.advance(),"$";case a.PATTERN:return this.advance(),{pattern:e.pattern||e.value};case a.STEP:return this.advance(),{first:e.first||0,step:e.step||0};default:return}}peek(){return this.tokens[this.pos]||{type:a.EOF,value:"",line:0,column:0}}advance(){return this.isAtEnd()||this.pos++,this.tokens[this.pos-1]}check(e){return this.peek().type===e}isAtEnd(){return this.peek().type===a.EOF}};function q(i,e=!1){return new A(i,e).parse()}var Z={name:"sed",summary:"stream editor for filtering and transforming text",usage:"sed [OPTION]... {script} [input-file]...",options:["-n, --quiet, --silent suppress automatic printing of pattern space","-e script add the script to commands to be executed","-f script-file read script from file","-i, --in-place edit files in place","-E, -r, --regexp-extended use extended regular expressions"," --help display this help and exit"],description:`Commands:
33
+ s/regexp/replacement/[flags] substitute
34
+ d delete pattern space
35
+ p print pattern space
36
+ a\\ text append text after line
37
+ i\\ text insert text before line
38
+ c\\ text change (replace) line with text
39
+ h copy pattern space to hold space
40
+ H append pattern space to hold space
41
+ g copy hold space to pattern space
42
+ G append hold space to pattern space
43
+ x exchange pattern and hold spaces
44
+ n read next line into pattern space
45
+ N append next line to pattern space
46
+ y/source/dest/ transliterate characters
47
+ = print line number
48
+ l list pattern space (escape special chars)
49
+ b [label] branch to label
50
+ t [label] branch on substitution
51
+ T [label] branch if no substitution
52
+ :label define label
53
+ q quit
54
+ Q quit without printing
55
+
56
+ Addresses:
57
+ N line number
58
+ $ last line
59
+ /regexp/ lines matching regexp
60
+ N,M range from line N to M
61
+ first~step every step-th line starting at first`};async function F(i,e,t,n={}){let{limits:r,filename:s,fs:c,cwd:d}=n,h=i.split(`
62
+ `);h.length>0&&h[h.length-1]===""&&h.pop();let m=h.length,l="",u,p="",o=new Map,b=new Map,C=new Map,R=new Map,H=r?{maxIterations:r.maxSedIterations}:void 0;for(let v=0;v<h.length;v++){let f={...U(m,s,o),patternSpace:h[v],holdSpace:p,lineNumber:v+1,totalLines:m,substitutionMade:!1},O={lines:h,currentLineIndex:v},_=0,X=1e4,$=0;do{if(_++,_>X)break;f.restartCycle=!1,f.pendingFileReads=[],f.pendingFileWrites=[];let E=y(e,f,O,H);if($+=E,c&&d){for(let S of f.pendingFileReads){let g=c.resolvePath(d,S.filename);try{if(S.wholeFile){let x=await c.readFile(g);f.appendBuffer.push(x.replace(/\n$/,""))}else{if(!b.has(g)){let z=await c.readFile(g);b.set(g,z.split(`
63
+ `)),C.set(g,0)}let x=b.get(g),w=C.get(g);x&&w!==void 0&&w<x.length&&(f.appendBuffer.push(x[w]),C.set(g,w+1))}}catch{}}for(let S of f.pendingFileWrites){let g=c.resolvePath(d,S.filename),x=R.get(g)||"";R.set(g,x+S.content)}}O.currentLineIndex+=E}while(f.restartCycle&&!f.deleted&&!f.quit&&!f.quitSilent);v+=$,p=f.holdSpace;for(let E of f.lineNumberOutput)l+=`${E}
64
+ `;let B=[],M=[];for(let E of f.appendBuffer)E.startsWith("__INSERT__")?B.push(E.slice(10)):M.push(E);for(let E of B)l+=`${E}
65
+ `;!f.deleted&&!f.quitSilent&&(t?f.printed&&(l+=`${f.patternSpace}
66
+ `):l+=`${f.patternSpace}
67
+ `);for(let E of M)l+=`${E}
68
+ `;if(f.quit||f.quitSilent){f.exitCode!==void 0&&(u=f.exitCode);break}}if(c&&d)for(let[v,f]of R)try{await c.writeFile(v,f)}catch{}return{output:l,exitCode:u}}var le={name:"sed",async execute(i,e){if(D(i))return T(Z);let t=[],n=[],r=!1,s=!1,c=!1,d=[];for(let p=0;p<i.length;p++){let o=i[p];if(o==="-n"||o==="--quiet"||o==="--silent")r=!0;else if(o==="-i"||o==="--in-place")s=!0;else if(o.startsWith("-i"))s=!0;else if(o==="-E"||o==="-r"||o==="--regexp-extended")c=!0;else if(o==="-e")p+1<i.length&&t.push(i[++p]);else if(o==="-f")p+1<i.length&&n.push(i[++p]);else{if(o.startsWith("--"))return I("sed",o);if(o.startsWith("-")&&o.length>1){for(let b of o.slice(1))if(b!=="n"&&b!=="e"&&b!=="f"&&b!=="i"&&b!=="E"&&b!=="r")return I("sed",`-${b}`);o.includes("n")&&(r=!0),o.includes("i")&&(s=!0),(o.includes("E")||o.includes("r"))&&(c=!0),o.includes("e")&&!o.includes("n")&&!o.includes("i")&&p+1<i.length&&t.push(i[++p]),o.includes("f")&&!o.includes("e")&&p+1<i.length&&n.push(i[++p])}else!o.startsWith("-")&&t.length===0&&n.length===0?t.push(o):o.startsWith("-")||d.push(o)}}for(let p of n){let o=e.fs.resolvePath(e.cwd,p);try{let b=await e.fs.readFile(o);for(let C of b.split(`
69
+ `)){let R=C.trim();R&&!R.startsWith("#")&&t.push(R)}}catch{return{stdout:"",stderr:`sed: couldn't open file ${p}: No such file or directory
70
+ `,exitCode:1}}}if(t.length===0)return{stdout:"",stderr:`sed: no script specified
71
+ `,exitCode:1};let{commands:h,error:m}=q(t,c);if(m)return{stdout:"",stderr:`sed: ${m}
72
+ `,exitCode:1};if(h.length===0)return{stdout:"",stderr:`sed: no valid commands
73
+ `,exitCode:1};let l="";if(d.length===0){l=e.stdin;let p=await F(l,h,r,{limits:e.limits,fs:e.fs,cwd:e.cwd});return{stdout:p.output,stderr:"",exitCode:p.exitCode??0}}if(s){for(let p of d){let o=e.fs.resolvePath(e.cwd,p);try{let b=await e.fs.readFile(o),C=await F(b,h,r,{limits:e.limits,filename:p,fs:e.fs,cwd:e.cwd});await e.fs.writeFile(o,C.output)}catch{return{stdout:"",stderr:`sed: ${p}: No such file or directory
74
+ `,exitCode:1}}}return{stdout:"",stderr:"",exitCode:0}}for(let p of d){let o=e.fs.resolvePath(e.cwd,p);try{l+=await e.fs.readFile(o)}catch{return{stdout:"",stderr:`sed: ${p}: No such file or directory
75
+ `,exitCode:1}}}let u=await F(l,h,r,{limits:e.limits,filename:d.length===1?d[0]:void 0,fs:e.fs,cwd:e.cwd});return{stdout:u.output,stderr:"",exitCode:u.exitCode??0}}};export{le as sedCommand};