import{a as x}from"./chunk-SNN4IESC.js";import{a as z,b as J}from"./chunk-2TRDZQLB.js";import{c as X}from"./chunk-AH77CRWX.js";import{a as Z,b as j,c as q}from"./chunk-74CEPOFO.js";var ie={name:"grep",summary:"print lines that match patterns",usage:"grep [OPTION]... PATTERN [FILE]...",options:["-E, --extended-regexp    PATTERN is an extended regular expression","-P, --perl-regexp        PATTERN is a Perl 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"]},K={name:"grep",async execute(t,s){if(j(t))return Z(ie);let l=!1,f=!1,c=!1,p=!1,m=!1,a=!1,n=!1,u=!1,y=!1,w=!1,h=!1,d=!1,F=!1,P=!1,B=!1,$=0,T=0,C=0,W=[],A=[],U=[],R=null,I=[];for(let r=0;r<t.length;r++){let i=t[r];if(i.startsWith("-")&&i!=="-"){if(i==="-e"&&r+1<t.length){R=t[++r];continue}if(i.startsWith("--include=")){W.push(i.slice(10));continue}if(i.startsWith("--exclude=")){A.push(i.slice(10));continue}if(i.startsWith("--exclude-dir=")){U.push(i.slice(14));continue}if(i.startsWith("--max-count=")){$=parseInt(i.slice(12),10);continue}let b=i.match(/^-m(\d+)$/);if(b){$=parseInt(b[1],10);continue}if(i==="-m"&&r+1<t.length){$=parseInt(t[++r],10);continue}let o=i.match(/^-([ABC])(\d+)$/);if(o){let e=parseInt(o[2],10);o[1]==="A"?C=e:o[1]==="B"?T=e:o[1]==="C"&&(T=e,C=e);continue}if((i==="-A"||i==="-B"||i==="-C")&&r+1<t.length){let e=parseInt(t[++r],10);i==="-A"?C=e:i==="-B"?T=e:(T=e,C=e);continue}let g=i.startsWith("--")?[i]:i.slice(1).split("");for(let e of g)if(e==="i"||e==="--ignore-case")l=!0;else if(e==="n"||e==="--line-number")f=!0;else if(e==="v"||e==="--invert-match")c=!0;else if(e==="c"||e==="--count")p=!0;else if(e==="l"||e==="--files-with-matches")m=!0;else if(e==="L"||e==="--files-without-match")a=!0;else if(e==="r"||e==="R"||e==="--recursive")n=!0;else if(e==="w"||e==="--word-regexp")u=!0;else if(e==="x"||e==="--line-regexp")y=!0;else if(e==="E"||e==="--extended-regexp")w=!0;else if(e==="P"||e==="--perl-regexp")h=!0;else if(e==="F"||e==="--fixed-strings")d=!0;else if(e==="o"||e==="--only-matching")F=!0;else if(e==="h"||e==="--no-filename")P=!0;else if(e==="q"||e==="--quiet"||e==="--silent")B=!0;else{if(e.startsWith("--"))return q("grep",e);if(e.length===1)return q("grep",`-${e}`)}}else R===null?R=i:I.push(i)}if(R===null)return{stdout:"",stderr:`grep: missing pattern
`,exitCode:2};let ee=d?"fixed":w?"extended":h?"perl":"basic",L,O,Q;try{let r=J(R,{mode:ee,ignoreCase:l,wholeWord:u,lineRegexp:y});L=r.regex,O=r.kResetGroup,Q=r.preFilter}catch{return{stdout:"",stderr:`grep: invalid regular expression: ${R}
`,exitCode:2}}if(I.length===0&&s.stdin!==void 0){let r=z(X(s.stdin),L,{invertMatch:c,showLineNumbers:f,countOnly:p,filename:"",onlyMatching:F,beforeContext:T,afterContext:C,maxCount:$,kResetGroup:O,preFilter:Q});return B?{stdout:"",stderr:"",exitCode:r.matched?0:1}:{stdout:r.output,stderr:"",exitCode:r.matched?0:1}}if(I.length===0)return{stdout:"",stderr:`grep: no input files
`,exitCode:2};let M="",S="",D=!1,k=!1,v=[];for(let r of I)if(r.includes("*")||r.includes("?")||r.includes("[")){let i=await re(r,s);if(n)for(let b of i){let o=await H(b.path,s,W,A,U,b.isFile);v.push(...o)}else v.push(...i)}else if(n){let i=await H(r,s,W,A,U);v.push(...i)}else v.push({path:r});let te=(v.length>1||n)&&!P,_=50;for(let r=0;r<v.length;r+=_){let i=v.slice(r,r+_),b=await Promise.all(i.map(async o=>{let g=o.path,e=g.split("/").pop()||g;if(A.length>0&&!n&&A.some(N=>x(e,N,{stripQuotes:!0}))||W.length>0&&!n&&!W.some(N=>x(e,N,{stripQuotes:!0})))return null;try{let N=s.fs.resolvePath(s.cwd,g),G=!1;if(o.isFile===void 0?G=(await s.fs.stat(N)).isDirectory:G=!o.isFile,G)return n?null:{error:`grep: ${g}: Is a directory
`};let se=await s.fs.readFile(N),ne=z(se,L,{invertMatch:c,showLineNumbers:f,countOnly:p,filename:te?g:"",onlyMatching:F,beforeContext:T,afterContext:C,maxCount:$,kResetGroup:O,preFilter:Q});return{file:g,result:ne}}catch{return{error:`grep: ${g}: No such file or directory
`}}}));for(let o of b){if(o===null)continue;if("error"in o&&o.error){S+=o.error,o.error.includes("Is a directory")||(k=!0);continue}if(!("file"in o)||!o.result)continue;let{file:g,result:e}=o;if(e.matched){if(D=!0,B)return{stdout:"",stderr:"",exitCode:0};m?M+=`${g}
`:a||(M+=e.output)}else a?M+=`${g}
`:p&&!m&&(M+=e.output)}}let E;return k?E=2:a?E=M.length>0?0:1:E=D?0:1,B?{stdout:"",stderr:"",exitCode:E}:{stdout:M,stderr:S,exitCode:E}}},V=256;async function Y(t,s,l,f,c=0){if(c>=V)return;let p=l.fs.resolvePath(l.cwd,t);try{if(!(await l.fs.stat(p)).isDirectory){let n=t.split("/").pop()||"";if(s){let u=s.replace(/^\//,"");x(n,u,{stripQuotes:!0})&&f.push(t)}return}let a=await l.fs.readdir(p);for(let n of a){let u=t==="."?n:`${t}/${n}`,y=l.fs.resolvePath(l.cwd,u);if((await l.fs.stat(y)).isDirectory)await Y(u,s,l,f,c+1);else if(s){let h=s.replace(/^\//,"");x(n,h,{stripQuotes:!0})&&f.push(u)}}}catch{}}async function re(t,s){let l=[],f=t.lastIndexOf("/"),c,p;if(f===-1?(c=s.cwd,p=t):(c=t.slice(0,f)||"/",p=t.slice(f+1)),t.includes("**")){let a=[],n=t.split("**"),u=n[0].replace(/\/$/,"")||".",y=n[1]||"";return await Y(u,y,s,a),a.map(w=>({path:w}))}let m=s.fs.resolvePath(s.cwd,c);try{if(s.fs.readdirWithFileTypes){let a=await s.fs.readdirWithFileTypes(m);for(let n of a)if(x(n.name,p,{stripQuotes:!0})){let u=f===-1?n.name:`${c}/${n.name}`;l.push({path:u,isFile:n.isFile})}}else{let a=await s.fs.readdir(m);for(let n of a)if(x(n,p,{stripQuotes:!0})){let u=f===-1?n:`${c}/${n}`;l.push({path:u})}}}catch{}return l.sort((a,n)=>a.path.localeCompare(n.path))}async function H(t,s,l=[],f=[],c=[],p,m=0){if(m>=V)return[];let a=s.fs.resolvePath(s.cwd,t),n=[];try{let u,y;if(p!==void 0)u=p,y=!p;else{let h=await s.fs.stat(a);u=h.isFile,y=h.isDirectory}if(u){let h=t.split("/").pop()||t;return f.length>0&&f.some(d=>x(h,d,{stripQuotes:!0}))?[]:l.length>0&&!l.some(d=>x(h,d,{stripQuotes:!0}))?[]:[{path:t,isFile:!0}]}if(!y)return[];let w=t.split("/").pop()||t;if(c.length>0&&c.some(h=>x(w,h,{stripQuotes:!0})))return[];if(s.fs.readdirWithFileTypes){let h=await s.fs.readdirWithFileTypes(a);for(let d of h){if(d.name.startsWith("."))continue;let F=t==="."?d.name:`${t}/${d.name}`,P=await H(F,s,l,f,c,d.isFile,m+1);n.push(...P)}}else{let h=await s.fs.readdir(a);for(let d of h){if(d.startsWith("."))continue;let F=t==="."?d:`${t}/${d}`,P=await H(F,s,l,f,c,void 0,m+1);n.push(...P)}}}catch{}return n}var ce={name:"fgrep",async execute(t,s){return K.execute(["-F",...t],s)}},pe={name:"egrep",async execute(t,s){return K.execute(["-E",...t],s)}},he={name:"grep",flags:[{flag:"-E",type:"boolean"},{flag:"-F",type:"boolean"},{flag:"-P",type:"boolean"},{flag:"-i",type:"boolean"},{flag:"-v",type:"boolean"},{flag:"-w",type:"boolean"},{flag:"-x",type:"boolean"},{flag:"-c",type:"boolean"},{flag:"-l",type:"boolean"},{flag:"-L",type:"boolean"},{flag:"-n",type:"boolean"},{flag:"-h",type:"boolean"},{flag:"-o",type:"boolean"},{flag:"-q",type:"boolean"},{flag:"-r",type:"boolean"},{flag:"-m",type:"value",valueHint:"number"},{flag:"-A",type:"value",valueHint:"number"},{flag:"-B",type:"value",valueHint:"number"},{flag:"-C",type:"value",valueHint:"number"},{flag:"-e",type:"value",valueHint:"pattern"}],stdinType:"text",needsArgs:!0},de={name:"fgrep",flags:[],stdinType:"text",needsArgs:!0},ge={name:"egrep",flags:[],stdinType:"text",needsArgs:!0};export{K as a,ce as b,pe as c,he as d,de as e,ge as f};
