import{a as x}from"./chunk-IJXFPKNC.js";import{a as q,b as Z}from"./chunk-VLGZJRPG.js";import{a as _,b as X,c as G}from"./chunk-74CEPOFO.js";var se={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"]},j={name:"grep",async execute(t,s){if(X(t))return _(se);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,v=!1,P=!1,B=!1,$=0,T=0,C=0,W=[],A=[],L=[],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=")){L.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")v=!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 G("grep",e);if(e.length===1)return G("grep",`-${e}`)}}else R===null?R=i:I.push(i)}if(R===null)return{stdout:"",stderr:`grep: missing pattern
`,exitCode:2};let V=d?"fixed":w?"extended":h?"perl":"basic",O,Q;try{let r=Z(R,{mode:V,ignoreCase:l,wholeWord:u,lineRegexp:y});O=r.regex,Q=r.kResetGroup}catch{return{stdout:"",stderr:`grep: invalid regular expression: ${R}
`,exitCode:2}}if(I.length===0&&s.stdin!==void 0){let r=q(s.stdin,O,{invertMatch:c,showLineNumbers:f,countOnly:p,filename:"",onlyMatching:v,beforeContext:T,afterContext:C,maxCount:$,kResetGroup: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="",z="",S=!1,D=!1,F=[];for(let r of I)if(r.includes("*")||r.includes("?")||r.includes("[")){let i=await ne(r,s);if(n)for(let b of i){let o=await H(b.path,s,W,A,L,b.isFile);F.push(...o)}else F.push(...i)}else if(n){let i=await H(r,s,W,A,L);F.push(...i)}else F.push({path:r});let Y=(F.length>1||n)&&!P,k=50;for(let r=0;r<F.length;r+=k){let i=F.slice(r,r+k),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),U=!1;if(o.isFile===void 0?U=(await s.fs.stat(N)).isDirectory:U=!o.isFile,U)return n?null:{error:`grep: ${g}: Is a directory
`};let ee=await s.fs.readFile(N),te=q(ee,O,{invertMatch:c,showLineNumbers:f,countOnly:p,filename:Y?g:"",onlyMatching:v,beforeContext:T,afterContext:C,maxCount:$,kResetGroup:Q});return{file:g,result:te}}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){z+=o.error,o.error.includes("Is a directory")||(D=!0);continue}if(!("file"in o)||!o.result)continue;let{file:g,result:e}=o;if(e.matched){if(S=!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 D?E=2:a?E=M.length>0?0:1:E=S?0:1,B?{stdout:"",stderr:"",exitCode:E}:{stdout:M,stderr:z,exitCode:E}}},J=256;async function K(t,s,l,f,c=0){if(c>=J)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 K(u,s,l,f,c+1);else if(s){let h=s.replace(/^\//,"");x(n,h,{stripQuotes:!0})&&f.push(u)}}}catch{}}async function ne(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 K(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>=J)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 v=t==="."?d.name:`${t}/${d.name}`,P=await H(v,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 v=t==="."?d:`${t}/${d}`,P=await H(v,s,l,f,c,void 0,m+1);n.push(...P)}}}catch{}return n}var ae={name:"fgrep",async execute(t,s){return j.execute(["-F",...t],s)}},fe={name:"egrep",async execute(t,s){return j.execute(["-E",...t],s)}},ue={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},ce={name:"fgrep",flags:[],stdinType:"text",needsArgs:!0},pe={name:"egrep",flags:[],stdinType:"text",needsArgs:!0};export{j as a,ae as b,fe as c,ue as d,ce as e,pe as f};
