import{n as e,r as t,t as n}from"./chunk-74CEPOFO-V0JXh91S.mjs";import{t as r}from"./chunk-J642UCRS-HjD8qpZD.mjs";import{n as i,t as a}from"./chunk-VLGZJRPG-DpYP_EVp.mjs";var o={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`]},s={name:`grep`,async execute(s,c){if(n(s))return t(o);let l=!1,f=!1,p=!1,m=!1,h=!1,g=!1,_=!1,v=!1,y=!1,b=!1,x=!1,S=!1,C=!1,w=!1,T=!1,E=0,D=0,O=0,k=[],A=[],j=[],M=null,N=[];for(let t=0;t<s.length;t++){let n=s[t];if(n.startsWith(`-`)&&n!==`-`){if(n===`-e`&&t+1<s.length){M=s[++t];continue}if(n.startsWith(`--include=`)){k.push(n.slice(10));continue}if(n.startsWith(`--exclude=`)){A.push(n.slice(10));continue}if(n.startsWith(`--exclude-dir=`)){j.push(n.slice(14));continue}if(n.startsWith(`--max-count=`)){E=parseInt(n.slice(12),10);continue}let r=n.match(/^-m(\d+)$/);if(r){E=parseInt(r[1],10);continue}if(n===`-m`&&t+1<s.length){E=parseInt(s[++t],10);continue}let i=n.match(/^-([ABC])(\d+)$/);if(i){let e=parseInt(i[2],10);i[1]===`A`?O=e:i[1]===`B`?D=e:i[1]===`C`&&(D=e,O=e);continue}if((n===`-A`||n===`-B`||n===`-C`)&&t+1<s.length){let e=parseInt(s[++t],10);n===`-A`?O=e:n===`-B`?D=e:(D=e,O=e);continue}let a=n.startsWith(`--`)?[n]:n.slice(1).split(``);for(let t of a)if(t===`i`||t===`--ignore-case`)l=!0;else if(t===`n`||t===`--line-number`)f=!0;else if(t===`v`||t===`--invert-match`)p=!0;else if(t===`c`||t===`--count`)m=!0;else if(t===`l`||t===`--files-with-matches`)h=!0;else if(t===`L`||t===`--files-without-match`)g=!0;else if(t===`r`||t===`R`||t===`--recursive`)_=!0;else if(t===`w`||t===`--word-regexp`)v=!0;else if(t===`x`||t===`--line-regexp`)y=!0;else if(t===`E`||t===`--extended-regexp`)b=!0;else if(t===`P`||t===`--perl-regexp`)x=!0;else if(t===`F`||t===`--fixed-strings`)S=!0;else if(t===`o`||t===`--only-matching`)C=!0;else if(t===`h`||t===`--no-filename`)w=!0;else if(t===`q`||t===`--quiet`||t===`--silent`)T=!0;else{if(t.startsWith(`--`))return e(`grep`,t);if(t.length===1)return e(`grep`,`-${t}`)}}else M===null?M=n:N.push(n)}if(M===null)return{stdout:``,stderr:`grep: missing pattern
`,exitCode:2};let P=S?`fixed`:b?`extended`:x?`perl`:`basic`,F,I;try{let e=i(M,{mode:P,ignoreCase:l,wholeWord:v,lineRegexp:y});F=e.regex,I=e.kResetGroup}catch{return{stdout:``,stderr:`grep: invalid regular expression: ${M}
`,exitCode:2}}if(N.length===0&&c.stdin!==void 0){let e=a(c.stdin,F,{invertMatch:p,showLineNumbers:f,countOnly:m,filename:``,onlyMatching:C,beforeContext:D,afterContext:O,maxCount:E,kResetGroup:I});return T?{stdout:``,stderr:``,exitCode:e.matched?0:1}:{stdout:e.output,stderr:``,exitCode:e.matched?0:1}}if(N.length===0)return{stdout:``,stderr:`grep: no input files
`,exitCode:2};let L=``,R=``,z=!1,B=!1,V=[];for(let e of N)if(e.includes(`*`)||e.includes(`?`)||e.includes(`[`)){let t=await u(e,c);if(_)for(let e of t){let t=await d(e.path,c,k,A,j,e.isFile);V.push(...t)}else V.push(...t)}else if(_){let t=await d(e,c,k,A,j);V.push(...t)}else V.push({path:e});let H=(V.length>1||_)&&!w;for(let e=0;e<V.length;e+=50){let t=V.slice(e,e+50),n=await Promise.all(t.map(async e=>{let t=e.path,n=t.split(`/`).pop()||t;if(A.length>0&&!_&&A.some(e=>r(n,e,{stripQuotes:!0}))||k.length>0&&!_&&!k.some(e=>r(n,e,{stripQuotes:!0})))return null;try{let n=c.fs.resolvePath(c.cwd,t),r=!1;return r=e.isFile===void 0?(await c.fs.stat(n)).isDirectory:!e.isFile,r?_?null:{error:`grep: ${t}: Is a directory
`}:{file:t,result:a(await c.fs.readFile(n),F,{invertMatch:p,showLineNumbers:f,countOnly:m,filename:H?t:``,onlyMatching:C,beforeContext:D,afterContext:O,maxCount:E,kResetGroup:I})}}catch{return{error:`grep: ${t}: No such file or directory
`}}}));for(let e of n){if(e===null)continue;if(`error`in e&&e.error){R+=e.error,e.error.includes(`Is a directory`)||(B=!0);continue}if(!(`file`in e)||!e.result)continue;let{file:t,result:n}=e;if(n.matched){if(z=!0,T)return{stdout:``,stderr:``,exitCode:0};h?L+=`${t}
`:g||(L+=n.output)}else g?L+=`${t}
`:m&&!h&&(L+=n.output)}}let U;return U=B?2:g?L.length>0?0:1:z?0:1,T?{stdout:``,stderr:``,exitCode:U}:{stdout:L,stderr:R,exitCode:U}}},c=256;async function l(e,t,n,i,a=0){if(a>=c)return;let o=n.fs.resolvePath(n.cwd,e);try{if(!(await n.fs.stat(o)).isDirectory){let n=e.split(`/`).pop()||``;t&&r(n,t.replace(/^\//,``),{stripQuotes:!0})&&i.push(e);return}let s=await n.fs.readdir(o);for(let o of s){let s=e===`.`?o:`${e}/${o}`,c=n.fs.resolvePath(n.cwd,s);(await n.fs.stat(c)).isDirectory?await l(s,t,n,i,a+1):t&&r(o,t.replace(/^\//,``),{stripQuotes:!0})&&i.push(s)}}catch{}}async function u(e,t){let n=[],i=e.lastIndexOf(`/`),a,o;if(i===-1?(a=t.cwd,o=e):(a=e.slice(0,i)||`/`,o=e.slice(i+1)),e.includes(`**`)){let n=[],r=e.split(`**`);return await l(r[0].replace(/\/$/,``)||`.`,r[1]||``,t,n),n.map(e=>({path:e}))}let s=t.fs.resolvePath(t.cwd,a);try{if(t.fs.readdirWithFileTypes){let e=await t.fs.readdirWithFileTypes(s);for(let t of e)if(r(t.name,o,{stripQuotes:!0})){let e=i===-1?t.name:`${a}/${t.name}`;n.push({path:e,isFile:t.isFile})}}else{let e=await t.fs.readdir(s);for(let t of e)if(r(t,o,{stripQuotes:!0})){let e=i===-1?t:`${a}/${t}`;n.push({path:e})}}}catch{}return n.sort((e,t)=>e.path.localeCompare(t.path))}async function d(e,t,n=[],i=[],a=[],o,s=0){if(s>=c)return[];let l=t.fs.resolvePath(t.cwd,e),u=[];try{let c,f;if(o!==void 0)c=o,f=!o;else{let e=await t.fs.stat(l);c=e.isFile,f=e.isDirectory}if(c){let t=e.split(`/`).pop()||e;return i.length>0&&i.some(e=>r(t,e,{stripQuotes:!0}))||n.length>0&&!n.some(e=>r(t,e,{stripQuotes:!0}))?[]:[{path:e,isFile:!0}]}if(!f)return[];let p=e.split(`/`).pop()||e;if(a.length>0&&a.some(e=>r(p,e,{stripQuotes:!0})))return[];if(t.fs.readdirWithFileTypes){let r=await t.fs.readdirWithFileTypes(l);for(let o of r){if(o.name.startsWith(`.`))continue;let r=await d(e===`.`?o.name:`${e}/${o.name}`,t,n,i,a,o.isFile,s+1);u.push(...r)}}else{let r=await t.fs.readdir(l);for(let o of r){if(o.startsWith(`.`))continue;let r=await d(e===`.`?o:`${e}/${o}`,t,n,i,a,void 0,s+1);u.push(...r)}}}catch{}return u}var f={name:`fgrep`,async execute(e,t){return s.execute([`-F`,...e],t)}},p={name:`egrep`,async execute(e,t){return s.execute([`-E`,...e],t)}},m={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},h={name:`fgrep`,flags:[],stdinType:`text`,needsArgs:!0},g={name:`egrep`,flags:[],stdinType:`text`,needsArgs:!0};export{g as a,s as i,h as n,m as o,p as r,f as t};
//# sourceMappingURL=chunk-YS3AZT3J-CB0wWPLc.mjs.map