#!/usr/bin/env node
import{k as R}from"./chunk-K5IXNHO5.js";import{a as y}from"./chunk-SE4C7FJY.js";import{a as z,b as X,c as _}from"./chunk-GTNBSMZR.js";import"./chunk-KGOUQS5A.js";var M=new Map([["alnum","a-zA-Z0-9"],["alpha","a-zA-Z"],["ascii","\\x00-\\x7F"],["blank"," \\t"],["cntrl","\\x00-\\x1F\\x7F"],["digit","0-9"],["graph","!-~"],["lower","a-z"],["print"," -~"],["punct","!-/:-@\\[-`{-~"],["space"," \\t\\n\\r\\f\\v"],["upper","A-Z"],["word","a-zA-Z0-9_"],["xdigit","0-9A-Fa-f"]]);function T(i){let e="",t=0,n=!1;for(;t<i.length;){if(i[t]==="["&&!n){if(i[t+1]==="["&&i[t+2]===":"){let s=i.indexOf(":]]",t+3);if(s!==-1){let r=i.slice(t+3,s),a=M.get(r);if(a){e+=`[${a}]`,t=s+3;continue}}}if(i[t+1]==="^"&&i[t+2]==="["&&i[t+3]===":"){let s=i.indexOf(":]]",t+4);if(s!==-1){let r=i.slice(t+4,s),a=M.get(r);if(a){e+=`[^${a}]`,t=s+3;continue}}}e+="[",t++,n=!0,t<i.length&&i[t]==="^"&&(e+="^",t++),t<i.length&&i[t]==="]"&&(e+="\\]",t++);continue}if(n){if(i[t]==="]"){e+="]",t++,n=!1;continue}if(i[t]==="["&&i[t+1]===":"){let s=i.indexOf(":]",t+2);if(s!==-1){let r=i.slice(t+2,s),a=M.get(r);if(a){e+=a,t=s+2;continue}}}if(i[t]==="\\"&&t+1<i.length){e+=i[t]+i[t+1],t+=2;continue}e+=i[t],t++;continue}if(i[t]==="\\"&&t+1<i.length){let s=i[t+1];if(s==="+"||s==="?"||s==="|"){e+=s,t+=2;continue}if(s==="("||s===")"){e+=s,t+=2;continue}if(s==="{"||s==="}"){e+=s,t+=2;continue}if(s==="t"){e+="	",t+=2;continue}if(s==="n"){e+=`
`,t+=2;continue}if(s==="r"){e+="\r",t+=2;continue}e+=i[t]+s,t+=2;continue}if(i[t]==="+"||i[t]==="?"||i[t]==="|"||i[t]==="("||i[t]===")"){e+=`\\${i[t]}`,t++;continue}if(i[t]==="^"&&!(e===""||e.endsWith("("))){e+="\\^",t++;continue}if(i[t]==="$"){let s=t===i.length-1,r=t+2<i.length&&i[t+1]==="\\"&&i[t+2]===")";if(!s&&!r){e+="\\$",t++;continue}}e+=i[t],t++}return e}function B(i){let e="",t=!1;for(let n=0;n<i.length;n++)i[n]==="["&&!t?(t=!0,e+="[",n++,n<i.length&&i[n]==="^"&&(e+="^",n++),n<i.length&&i[n]==="]"&&(e+="]",n++),n--):i[n]==="]"&&t?(t=!1,e+="]"):!t&&i[n]==="{"&&i[n+1]===","?(e+="{0,",n++):e+=i[n];return e}function G(i){let e="";for(let t=0;t<i.length;t++){let n=i[t],s=n.charCodeAt(0);n==="\\"?e+="\\\\":n==="	"?e+="\\t":n===`
`?e+=`$
`:n==="\r"?e+="\\r":n==="\x07"?e+="\\a":n==="\b"?e+="\\b":n==="\f"?e+="\\f":n==="\v"?e+="\\v":s<32||s>=127?e+=`\\${s.toString(8).padStart(3,"0")}`:e+=n}return`${e}$`}var ie=1e4;function j(i,e,t){return{patternSpace:"",holdSpace:"",lineNumber:0,totalLines:i,deleted:!1,printed:!1,quit:!1,quitSilent:!1,exitCode:void 0,errorMessage:void 0,appendBuffer:[],substitutionMade:!1,lineNumberOutput:[],nCommandOutput:[],restartCycle:!1,inDRestartedCycle:!1,currentFilename:e,pendingFileReads:[],pendingFileWrites:[],pendingExecute:void 0,rangeStates:t||new Map,linesConsumedInCycle:0}}function se(i){return typeof i=="object"&&"first"in i&&"step"in i}function re(i){return typeof i=="object"&&"offset"in i}function O(i,e,t,n,s){if(i==="$")return e===t;if(typeof i=="number")return e===i;if(se(i)){let{first:r,step:a}=i;return a===0?e===r:(e-r)%a===0&&e>=r}if(typeof i=="object"&&"pattern"in i)try{let r=i.pattern;r===""&&s?.lastPattern?r=s.lastPattern:r!==""&&s&&(s.lastPattern=r);let a=B(T(r));return y(a).test(n)}catch{return!1}return!1}function P(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 ae(i,e,t,n,s,r){if(!i||!i.start&&!i.end)return!0;let a=i.start,c=i.end;if(a!==void 0&&c===void 0)return O(a,e,t,n,r);if(a!==void 0&&c!==void 0){let o=typeof a=="object"&&"pattern"in a,u=typeof c=="object"&&"pattern"in c,b=re(c);if(b&&s){let m=P(i),d=s.get(m);if(d||(d={active:!1},s.set(m,d)),d.active){let h=d.startLine||e;return e>=h+c.offset&&(d.active=!1,s.set(m,d)),!0}else return O(a,e,t,n,r)?(d.active=!0,d.startLine=e,s.set(m,d),c.offset===0&&(d.active=!1,s.set(m,d)),!0):!1}if(!o&&!u&&!b){let m=typeof a=="number"?a:a==="$"?t:1,d=typeof c=="number"?c:t;if(m<=d)return e>=m&&e<=d;if(s){let h=P(i),f=s.get(h);return f||(f={active:!1},s.set(h,f)),!f.completed&&e>=m?(f.completed=!0,s.set(h,f),!0):!1}return!1}if(s){let m=P(i),d=s.get(m);if(d||(d={active:!1},s.set(m,d)),d.active)return O(c,e,t,n,r)&&(d.active=!1,typeof a=="number"&&(d.completed=!0),s.set(m,d)),!0;{if(d.completed)return!1;let h=!1;return typeof a=="number"?h=e>=a:h=O(a,e,t,n,r),h?(d.active=!0,d.startLine=e,s.set(m,d),O(c,e,t,n,r)&&(d.active=!1,typeof a=="number"&&(d.completed=!0),s.set(m,d)),!0):!1}}return O(a,e,t,n,r)}return!0}function N(i,e,t,n,s,r){let a=ae(i,e,t,n,s,r);return i?.negated?!a:a}function ce(i,e,t,n){let s="",r=0,a=!1;for(;r<=i.length;){e.lastIndex=r;let c=e.exec(i);if(!c){s+=i.slice(r);break}if(c.index!==r){s+=i.slice(r,c.index),r=c.index,a=!1;continue}let o=c[0],u=c.slice(1);if(a&&o.length===0){if(r<i.length)s+=i[r],r++;else break;a=!1;continue}if(s+=n(o,u),a=!1,o.length===0)if(r<i.length)s+=i[r],r++;else break;else r+=o.length,a=!0}return s}function D(i,e,t){let n="",s=0;for(;s<i.length;){if(i[s]==="\\"&&s+1<i.length){let r=i[s+1];if(r==="&"){n+="&",s+=2;continue}if(r==="n"){n+=`
`,s+=2;continue}if(r==="t"){n+="	",s+=2;continue}if(r==="r"){n+="\r",s+=2;continue}let a=parseInt(r,10);if(a===0){n+=e,s+=2;continue}if(a>=1&&a<=9){n+=t[a-1]||"",s+=2;continue}n+=r,s+=2;continue}if(i[s]==="&"){n+=e,s++;continue}n+=i[s],s++}return n}function le(i,e){let{lineNumber:t,totalLines:n,patternSpace:s}=e;if(i.type!=="label"&&N(i.address,t,n,s,e.rangeStates,e))switch(i.type){case"substitute":{let r=i,a="";r.global&&(a+="g"),r.ignoreCase&&(a+="i");let c=r.pattern;c===""&&e.lastPattern?c=e.lastPattern:c!==""&&(e.lastPattern=c);let o=B(r.extendedRegex?c:T(c));try{let u=y(o,a),b=u.test(e.patternSpace);if(u.lastIndex=0,b){if(e.substitutionMade=!0,r.nthOccurrence&&r.nthOccurrence>0&&!r.global){let g=0,m=r.nthOccurrence,d=y(o,`g${r.ignoreCase?"i":""}`);e.patternSpace=d.replace(e.patternSpace,(h,...f)=>{if(g++,g===m){let E=f.slice(0,-2);return D(r.replacement,h,E)}return h})}else if(r.global){let g=y(o,`g${r.ignoreCase?"i":""}`);e.patternSpace=ce(e.patternSpace,g,r.replacement,(m,d)=>D(r.replacement,m,d))}else e.patternSpace=u.replace(e.patternSpace,(g,...m)=>{let d=m.slice(0,-2);return D(r.replacement,g,d)});r.printOnMatch&&e.lineNumberOutput.push(e.patternSpace)}}catch{}break}case"print":e.lineNumberOutput.push(e.patternSpace);break;case"printFirstLine":{let r=e.patternSpace.indexOf(`
`);r!==-1?e.lineNumberOutput.push(e.patternSpace.slice(0,r)):e.lineNumberOutput.push(e.patternSpace);break}case"delete":e.deleted=!0;break;case"deleteFirstLine":{let r=e.patternSpace.indexOf(`
`);r!==-1?(e.patternSpace=e.patternSpace.slice(r+1),e.restartCycle=!0,e.inDRestartedCycle=!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.deleted=!0,e.changedText=i.text;break;case"hold":e.holdSpace=e.patternSpace;break;case"holdAppend":e.holdSpace?e.holdSpace+=`
${e.patternSpace}`:e.holdSpace=e.patternSpace;break;case"get":e.patternSpace=e.holdSpace;break;case"getAppend":e.patternSpace+=`
${e.holdSpace}`;break;case"exchange":{let r=e.patternSpace;e.patternSpace=e.holdSpace,e.holdSpace=r;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 r=G(e.patternSpace);e.lineNumberOutput.push(r);break}case"printFilename":e.currentFilename&&e.lineNumberOutput.push(e.currentFilename);break;case"version":{let r=[4,8,0];if(i.minVersion){let a=i.minVersion.split("."),c=[],o=!1;for(let u of a){let b=parseInt(u,10);if(Number.isNaN(b)||b<0){e.quit=!0,e.exitCode=1,e.errorMessage=`sed: invalid version string: ${i.minVersion}`,o=!0;break}c.push(b)}if(!o){for(;c.length<3;)c.push(0);for(let u=0;u<3;u++){if(c[u]>r[u]){e.quit=!0,e.exitCode=1,e.errorMessage=`sed: this is not GNU sed version ${i.minVersion}`;break}if(c[u]<r[u])break}}}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}
`});break;case"writeFirstLine":{let r=e.patternSpace.indexOf(`
`),a=r!==-1?e.patternSpace.slice(0,r):e.patternSpace;e.pendingFileWrites.push({filename:i.filename,content:`${a}
`});break}case"execute":i.command?e.pendingExecute={command:i.command,replacePattern:!1}:e.pendingExecute={command:e.patternSpace,replacePattern:!0};break;case"transliterate":e.patternSpace=oe(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 oe(i,e){let t="";for(let n of i){let s=e.source.indexOf(n);s!==-1?t+=e.dest[s]:t+=n}return t}function W(i,e,t,n){let s=new Map;for(let o=0;o<i.length;o++){let u=i[o];u.type==="label"&&s.set(u.name,o)}let r=n?.maxIterations??ie,a=0,c=0;for(;c<i.length;){if(a++,a>r)throw new R(`sed: command execution exceeded maximum iterations (${r})`,"iterations");if(e.deleted||e.quit||e.quitSilent||e.restartCycle)break;let o=i[c];if(o.type==="next"){if(N(o.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates,e))if(e.nCommandOutput.push(e.patternSpace),t&&t.currentLineIndex+e.linesConsumedInCycle+1<t.lines.length){e.linesConsumedInCycle++;let u=t.lines[t.currentLineIndex+e.linesConsumedInCycle];e.patternSpace=u,e.lineNumber=t.currentLineIndex+e.linesConsumedInCycle+1,e.substitutionMade=!1}else{e.quit=!0,e.deleted=!0;break}c++;continue}if(o.type==="nextAppend"){if(N(o.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates,e))if(t&&t.currentLineIndex+e.linesConsumedInCycle+1<t.lines.length){e.linesConsumedInCycle++;let u=t.lines[t.currentLineIndex+e.linesConsumedInCycle];e.patternSpace+=`
${u}`,e.lineNumber=t.currentLineIndex+e.linesConsumedInCycle+1}else{e.quit=!0;break}c++;continue}if(o.type==="branch"){let u=o;if(N(u.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates,e)){if(u.label){let b=s.get(u.label);if(b!==void 0){c=b;continue}e.branchRequest=u.label;break}break}c++;continue}if(o.type==="branchOnSubst"){let u=o;if(N(u.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates,e)&&e.substitutionMade){if(e.substitutionMade=!1,u.label){let b=s.get(u.label);if(b!==void 0){c=b;continue}e.branchRequest=u.label;break}break}c++;continue}if(o.type==="branchOnNoSubst"){let u=o;if(N(u.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates,e)&&!e.substitutionMade){if(u.label){let b=s.get(u.label);if(b!==void 0){c=b;continue}e.branchRequest=u.label;break}break}c++;continue}if(o.type==="group"){let u=o;if(N(u.address,e.lineNumber,e.totalLines,e.patternSpace,e.rangeStates,e)&&(W(u.commands,e,t,n),e.branchRequest)){let b=s.get(e.branchRequest);if(b!==void 0){e.branchRequest=void 0,c=b;continue}break}c++;continue}le(o,e),c++}return e.linesConsumedInCycle}var l;(function(i){i.NUMBER="NUMBER",i.DOLLAR="DOLLAR",i.PATTERN="PATTERN",i.STEP="STEP",i.RELATIVE_OFFSET="RELATIVE_OFFSET",i.LBRACE="LBRACE",i.RBRACE="RBRACE",i.SEMICOLON="SEMICOLON",i.NEWLINE="NEWLINE",i.COMMA="COMMA",i.NEGATION="NEGATION",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.VERSION="VERSION",i.EOF="EOF",i.ERROR="ERROR"})(l||(l={}));var w=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(l.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===`
`?(this.line++,this.column=1):this.column++,e}readEscapedString(e){let t="";for(;this.pos<this.input.length&&this.peek()!==e;)if(this.peek()==="\\"){this.advance();let n=this.advance();n==="n"?t+=`
`:n==="t"?t+="	":t+=n}else{if(this.peek()===`
`)return null;t+=this.advance()}return t}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()!==`
`;)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===`
`?(this.advance(),{type:l.NEWLINE,value:`
`,line:e,column:t}):n===";"?(this.advance(),{type:l.SEMICOLON,value:";",line:e,column:t}):n==="{"?(this.advance(),{type:l.LBRACE,value:"{",line:e,column:t}):n==="}"?(this.advance(),{type:l.RBRACE,value:"}",line:e,column:t}):n===","?(this.advance(),{type:l.COMMA,value:",",line:e,column:t}):n==="!"?(this.advance(),{type:l.NEGATION,value:"!",line:e,column:t}):n==="$"?(this.advance(),{type:l.DOLLAR,value:"$",line:e,column:t}):this.isDigit(n)?this.readNumber():n==="+"&&this.isDigit(this.input[this.pos+1]||"")?this.readRelativeOffset():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 s="";for(;this.isDigit(this.peek());)s+=this.advance();let r=parseInt(n,10),a=parseInt(s,10)||0;return{type:l.STEP,value:`${r}~${a}`,first:r,step:a,line:e,column:t}}return{type:l.NUMBER,value:parseInt(n,10),line:e,column:t}}readRelativeOffset(){let e=this.line,t=this.column;this.advance();let n="";for(;this.isDigit(this.peek());)n+=this.advance();let s=parseInt(n,10)||0;return{type:l.RELATIVE_OFFSET,value:`+${s}`,offset:s,line:e,column:t}}readPattern(){let e=this.line,t=this.column;this.advance();let n="",s=!1;for(;this.pos<this.input.length;){let r=this.peek();if(r==="/"&&!s)break;if(r==="\\")n+=this.advance(),this.pos<this.input.length&&this.peek()!==`
`&&(n+=this.advance());else{if(r===`
`)break;r==="["&&!s?(s=!0,n+=this.advance(),this.peek()==="^"&&(n+=this.advance()),this.peek()==="]"&&(n+=this.advance())):r==="]"&&s?(s=!1,n+=this.advance()):n+=this.advance()}}return this.peek()==="/"&&this.advance(),{type:l.PATTERN,value:n,pattern:n,line:e,column:t}}readLabelDef(){let e=this.line,t=this.column;for(this.advance();this.peek()===" "||this.peek()==="	";)this.advance();let n="";for(;this.pos<this.input.length;){let s=this.peek();if(s===" "||s==="	"||s===`
`||s===";"||s==="}"||s==="{")break;n+=this.advance()}return{type:l.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(l.BRANCH,"b",e,t);case"t":return this.readBranch(l.BRANCH_ON_SUBST,"t",e,t);case"T":return this.readBranch(l.BRANCH_ON_NO_SUBST,"T",e,t);case"r":return this.readFileCommand(l.FILE_READ,"r",e,t);case"R":return this.readFileCommand(l.FILE_READ_LINE,"R",e,t);case"w":return this.readFileCommand(l.FILE_WRITE,"w",e,t);case"W":return this.readFileCommand(l.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":return{type:l.COMMAND,value:n,line:e,column:t};case"v":return this.readVersion(e,t);default:return{type:l.ERROR,value:n,line:e,column:t}}}readSubstitute(e,t){let n=this.advance();if(!n||n===`
`)return{type:l.ERROR,value:"s",line:e,column:t};let s="",r=!1;for(;this.pos<this.input.length;){let o=this.peek();if(o===n&&!r)break;if(o==="\\")this.advance(),this.pos<this.input.length&&this.peek()!==`
`?this.peek()===n&&!r?s+=this.advance():(s+="\\",s+=this.advance()):s+="\\";else{if(o===`
`)break;o==="["&&!r?(r=!0,s+=this.advance(),this.peek()==="^"&&(s+=this.advance()),this.peek()==="]"&&(s+=this.advance())):o==="]"&&r?(r=!1,s+=this.advance()):s+=this.advance()}}if(this.peek()!==n)return{type:l.ERROR,value:"unterminated substitution pattern",line:e,column:t};this.advance();let a="";for(;this.pos<this.input.length&&this.peek()!==n;)if(this.peek()==="\\")if(this.advance(),this.pos<this.input.length){let o=this.peek();o==="\\"?(this.advance(),this.pos<this.input.length&&this.peek()===`
`?(a+=`
`,this.advance()):a+="\\"):o===`
`?(a+=`
`,this.advance()):a+=`\\${this.advance()}`}else a+="\\";else{if(this.peek()===`
`)break;a+=this.advance()}this.peek()===n&&this.advance();let c="";for(;this.pos<this.input.length;){let o=this.peek();if(o==="g"||o==="i"||o==="p"||o==="I"||this.isDigit(o))c+=this.advance();else break}return{type:l.SUBSTITUTE,value:`s${n}${s}${n}${a}${n}${c}`,pattern:s,replacement:a,flags:c,line:e,column:t}}readTransliterate(e,t){let n=this.advance();if(!n||n===`
`)return{type:l.ERROR,value:"y",line:e,column:t};let s=this.readEscapedString(n);if(s===null||this.peek()!==n)return{type:l.ERROR,value:"unterminated transliteration source",line:e,column:t};this.advance();let r=this.readEscapedString(n);if(r===null||this.peek()!==n)return{type:l.ERROR,value:"unterminated transliteration dest",line:e,column:t};this.advance();let a=this.peek();for(;a===" "||a==="	";)this.advance(),a=this.peek();return a!==""&&a!==";"&&a!==`
`&&a!=="}"?{type:l.ERROR,value:"extra text at the end of a transform command",line:e,column:t}:{type:l.TRANSLITERATE,value:`y${n}${s}${n}${r}${n}`,source:s,dest:r,line:e,column:t}}readTextCommand(e,t,n){let s=!1;this.peek()==="\\"&&this.pos+1<this.input.length&&(this.input[this.pos+1]===`
`||this.input[this.pos+1]===" "||this.input[this.pos+1]==="	")&&(s=!0,this.advance()),(this.peek()===" "||this.peek()==="	")&&this.advance(),this.peek()==="\\"&&this.pos+1<this.input.length&&(this.input[this.pos+1]===" "||this.input[this.pos+1]==="	")&&this.advance(),s&&this.peek()===`
`&&this.advance();let r="";for(;this.pos<this.input.length;){let a=this.peek();if(a===`
`){if(r.endsWith("\\")){r=`${r.slice(0,-1)}
`,this.advance();continue}break}if(a==="\\"&&this.pos+1<this.input.length){let c=this.input[this.pos+1];if(c==="n"){r+=`
`,this.advance(),this.advance();continue}if(c==="t"){r+="	",this.advance(),this.advance();continue}if(c==="r"){r+="\r",this.advance(),this.advance();continue}}r+=this.advance()}return{type:l.TEXT_CMD,value:e,text:r,line:t,column:n}}readBranch(e,t,n,s){for(;this.peek()===" "||this.peek()==="	";)this.advance();let r="";for(;this.pos<this.input.length;){let a=this.peek();if(a===" "||a==="	"||a===`
`||a===";"||a==="}"||a==="{")break;r+=this.advance()}return{type:e,value:t,label:r||void 0,line:n,column:s}}readVersion(e,t){for(;this.peek()===" "||this.peek()==="	";)this.advance();let n="";for(;this.pos<this.input.length;){let s=this.peek();if(s===" "||s==="	"||s===`
`||s===";"||s==="}"||s==="{")break;n+=this.advance()}return{type:l.VERSION,value:"v",label:n||void 0,line:e,column:t}}readFileCommand(e,t,n,s){for(;this.peek()===" "||this.peek()==="	";)this.advance();let r="";for(;this.pos<this.input.length;){let a=this.peek();if(a===`
`||a===";")break;r+=this.advance()}return{type:e,value:t,filename:r.trim(),line:n,column:s}}readExecute(e,t){for(;this.peek()===" "||this.peek()==="	";)this.advance();let n="";for(;this.pos<this.input.length;){let s=this.peek();if(s===`
`||s===";")break;n+=this.advance()}return{type:l.EXECUTE,value:"e",command:n.trim()||void 0,line:e,column:t}}isDigit(e){return e>="0"&&e<="9"}};var q=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 w(t);for(this.tokens=n.tokenize(),this.pos=0;!this.isAtEnd();){if(this.check(l.NEWLINE)||this.check(l.SEMICOLON)){this.advance();continue}let s=this.parseCommand();if(s.error)return{commands:[],error:s.error};s.command&&e.push(s.command)}}return{commands:e}}parseCommand(){let e=this.parseAddressRange();if(e?.error)return{command:null,error:e.error};let t=e?.address;for(this.check(l.NEGATION)&&(this.advance(),t&&(t.negated=!0));this.check(l.NEWLINE)||this.check(l.SEMICOLON);)this.advance();if(this.isAtEnd())return t&&(t.start!==void 0||t.end!==void 0)?{command:null,error:"command expected"}:{command:null};let n=this.peek();switch(n.type){case l.COMMAND:return this.parseSimpleCommand(n,t);case l.SUBSTITUTE:return this.parseSubstituteFromToken(n,t);case l.TRANSLITERATE:return this.parseTransliterateFromToken(n,t);case l.LABEL_DEF:return this.advance(),{command:{type:"label",name:n.label||""}};case l.BRANCH:return this.advance(),{command:{type:"branch",address:t,label:n.label}};case l.BRANCH_ON_SUBST:return this.advance(),{command:{type:"branchOnSubst",address:t,label:n.label}};case l.BRANCH_ON_NO_SUBST:return this.advance(),{command:{type:"branchOnNoSubst",address:t,label:n.label}};case l.TEXT_CMD:return this.advance(),this.parseTextCommand(n,t);case l.FILE_READ:return this.advance(),{command:{type:"readFile",address:t,filename:n.filename||""}};case l.FILE_READ_LINE:return this.advance(),{command:{type:"readFileLine",address:t,filename:n.filename||""}};case l.FILE_WRITE:return this.advance(),{command:{type:"writeFile",address:t,filename:n.filename||""}};case l.FILE_WRITE_LINE:return this.advance(),{command:{type:"writeFirstLine",address:t,filename:n.filename||""}};case l.EXECUTE:return this.advance(),{command:{type:"execute",address:t,command:n.command}};case l.VERSION:return this.advance(),{command:{type:"version",address:t,minVersion:n.label}};case l.LBRACE:return this.parseGroup(t);case l.RBRACE:return{command:null};case l.ERROR:return{command:null,error:`invalid command: ${n.value}`};default:return t&&(t.start!==void 0||t.end!==void 0)?{command:null,error:"command expected"}:{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}};default:return{command:null,error:`unknown command: ${n}`}}}parseSubstituteFromToken(e,t){this.advance();let n=e.flags||"",s,r=n.match(/(\d+)/);return r&&(s=parseInt(r[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:s,extendedRegex:this.extendedRegex}}}parseTransliterateFromToken(e,t){this.advance();let n=e.source||"",s=e.dest||"";return n.length!==s.length?{command:null,error:"transliteration sets must have same length"}:{command:{type:"transliterate",address:t,source:n,dest:s}}}parseTextCommand(e,t){let n=e.value,s=e.text||"";switch(n){case"a":return{command:{type:"append",address:t,text:s}};case"i":return{command:{type:"insert",address:t,text:s}};case"c":return{command:{type:"change",address:t,text:s}};default:return{command:null,error:`unknown text command: ${n}`}}}parseGroup(e){this.advance();let t=[];for(;!this.isAtEnd()&&!this.check(l.RBRACE);){if(this.check(l.NEWLINE)||this.check(l.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(l.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;if(this.check(l.RELATIVE_OFFSET))t={offset:this.advance().offset||0};else if(this.check(l.COMMA)&&(this.advance(),t=this.parseAddress(),t===void 0))return{error:"expected context address"};return{address:{start:e,end:t}}}parseAddress(){let e=this.peek();switch(e.type){case l.NUMBER:return this.advance(),e.value;case l.DOLLAR:return this.advance(),"$";case l.PATTERN:return this.advance(),{pattern:e.pattern||e.value};case l.STEP:return this.advance(),{first:e.first||0,step:e.step||0};case l.RELATIVE_OFFSET:return this.advance(),{offset:e.offset||0};default:return}}peek(){return this.tokens[this.pos]||{type:l.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===l.EOF}};function Z(i,e=!1){let t=!1,n=!1,s=[];for(let o=0;o<i.length;o++){let u=i[o];if(s.length===0&&o===0){let b=u.match(/^#([nr]+)\s*(?:\n|$)/i);if(b){let g=b[1].toLowerCase();g.includes("n")&&(t=!0),g.includes("r")&&(n=!0),u=u.slice(b[0].length)}}if(s.length>0&&s[s.length-1].endsWith("\\")){let b=s[s.length-1];s[s.length-1]=`${b}
${u}`}else s.push(u)}let r=s.join(`
`),c=new q([r],e||n).parse();if(!c.error&&c.commands.length>0){let o=ue(c.commands);if(o)return{commands:[],error:o,silentMode:t,extendedRegexMode:n}}return{...c,silentMode:t,extendedRegexMode:n}}function ue(i){let e=new Set;K(i,e);let t=Q(i,e);if(t)return`undefined label '${t}'`}function K(i,e){for(let t of i)t.type==="label"?e.add(t.name):t.type==="group"&&K(t.commands,e)}function Q(i,e){for(let t of i){if((t.type==="branch"||t.type==="branchOnSubst"||t.type==="branchOnNoSubst")&&t.label&&!e.has(t.label))return t.label;if(t.type==="group"){let n=Q(t.commands,e);if(n)return n}}}var he={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:
  s/regexp/replacement/[flags]  substitute
  d                             delete pattern space
  p                             print pattern space
  a\\ text                       append text after line
  i\\ text                       insert text before line
  c\\ text                       change (replace) line with text
  h                             copy pattern space to hold space
  H                             append pattern space to hold space
  g                             copy hold space to pattern space
  G                             append hold space to pattern space
  x                             exchange pattern and hold spaces
  n                             read next line into pattern space
  N                             append next line to pattern space
  y/source/dest/                transliterate characters
  =                             print line number
  l                             list pattern space (escape special chars)
  b [label]                     branch to label
  t [label]                     branch on substitution
  T [label]                     branch if no substitution
  :label                        define label
  q                             quit
  Q                             quit without printing

Addresses:
  N                             line number
  $                             last line
  /regexp/                      lines matching regexp
  N,M                           range from line N to M
  first~step                    every step-th line starting at first`};async function U(i,e,t,n={}){let{limits:s,filename:r,fs:a,cwd:c}=n,o=i.endsWith(`
`),u=i.split(`
`);u.length>0&&u[u.length-1]===""&&u.pop();let b=u.length,g="",m,d=!1,h="",f,E=new Map,C=new Map,S=new Map,F=new Map,J=s?{maxIterations:s.maxSedIterations}:void 0;for(let k=0;k<u.length;k++){let p={...j(b,r,E),patternSpace:u[k],holdSpace:h,lastPattern:f,lineNumber:k+1,totalLines:b,substitutionMade:!1},Y={lines:u,currentLineIndex:k},H=0,ee=1e4;p.linesConsumedInCycle=0;do{if(H++,H>ee)break;if(p.restartCycle=!1,p.pendingFileReads=[],p.pendingFileWrites=[],W(e,p,Y,J),a&&c){for(let v of p.pendingFileReads){let x=a.resolvePath(c,v.filename);try{if(v.wholeFile){let I=await a.readFile(x);p.appendBuffer.push(I.replace(/\n$/,""))}else{if(!C.has(x)){let ne=await a.readFile(x);C.set(x,ne.split(`
`)),S.set(x,0)}let I=C.get(x),A=S.get(x);I&&A!==void 0&&A<I.length&&(p.appendBuffer.push(I[A]),S.set(x,A+1))}}catch{}}for(let v of p.pendingFileWrites){let x=a.resolvePath(c,v.filename),I=F.get(x)||"";F.set(x,I+v.content)}}}while(p.restartCycle&&!p.deleted&&!p.quit&&!p.quitSilent);if(k+=p.linesConsumedInCycle,h=p.holdSpace,f=p.lastPattern,!t)for(let v of p.nCommandOutput)g+=`${v}
`;let te=p.lineNumberOutput.length>0;for(let v of p.lineNumberOutput)g+=`${v}
`;let V=[],$=[];for(let v of p.appendBuffer)v.startsWith("__INSERT__")?V.push(v.slice(10)):$.push(v);for(let v of V)g+=`${v}
`;let L=!1;!p.deleted&&!p.quitSilent?t?p.printed&&(g+=`${p.patternSpace}
`,L=!0):(g+=`${p.patternSpace}
`,L=!0):p.changedText!==void 0&&(g+=`${p.changedText}
`,L=!0);for(let v of $)g+=`${v}
`;if(d=(te||L)&&$.length===0,p.quit||p.quitSilent){if(p.exitCode!==void 0&&(m=p.exitCode),p.errorMessage)return{output:"",exitCode:m||1,errorMessage:p.errorMessage};break}}if(a&&c)for(let[k,p]of F)try{await a.writeFile(k,p)}catch{}return!o&&d&&g.endsWith(`
`)&&(g=g.slice(0,-1)),{output:g,exitCode:m}}var Ie={name:"sed",async execute(i,e){if(X(i))return z(he);let t=[],n=[],s=!1,r=!1,a=!1,c=[];for(let h=0;h<i.length;h++){let f=i[h];if(f==="-n"||f==="--quiet"||f==="--silent")s=!0;else if(f==="-i"||f==="--in-place")r=!0;else if(f.startsWith("-i"))r=!0;else if(f==="-E"||f==="-r"||f==="--regexp-extended")a=!0;else if(f==="-e")h+1<i.length&&t.push(i[++h]);else if(f==="-f")h+1<i.length&&n.push(i[++h]);else{if(f.startsWith("--"))return _("sed",f);if(f==="-")c.push(f);else if(f.startsWith("-")&&f.length>1){for(let E of f.slice(1))if(E!=="n"&&E!=="e"&&E!=="f"&&E!=="i"&&E!=="E"&&E!=="r")return _("sed",`-${E}`);f.includes("n")&&(s=!0),f.includes("i")&&(r=!0),(f.includes("E")||f.includes("r"))&&(a=!0),f.includes("e")&&!f.includes("n")&&!f.includes("i")&&h+1<i.length&&t.push(i[++h]),f.includes("f")&&!f.includes("e")&&h+1<i.length&&n.push(i[++h])}else!f.startsWith("-")&&t.length===0&&n.length===0?t.push(f):f.startsWith("-")||c.push(f)}}for(let h of n){let f=e.fs.resolvePath(e.cwd,h);try{let E=await e.fs.readFile(f);for(let C of E.split(`
`)){let S=C.trim();S&&!S.startsWith("#")&&t.push(S)}}catch{return{stdout:"",stderr:`sed: couldn't open file ${h}: No such file or directory
`,exitCode:1}}}if(t.length===0)return{stdout:"",stderr:`sed: no script specified
`,exitCode:1};let{commands:o,error:u,silentMode:b}=Z(t,a);if(u)return{stdout:"",stderr:`sed: ${u}
`,exitCode:1};let g=!!(s||b);if(r){if(c.length===0)return{stdout:"",stderr:`sed: -i requires at least one file argument
`,exitCode:1};for(let h of c){if(h==="-")continue;let f=e.fs.resolvePath(e.cwd,h);try{let E=await e.fs.readFile(f),C=await U(E,o,g,{limits:e.limits,filename:h,fs:e.fs,cwd:e.cwd});if(C.errorMessage)return{stdout:"",stderr:`${C.errorMessage}
`,exitCode:C.exitCode??1};await e.fs.writeFile(f,C.output)}catch(E){return E instanceof R?{stdout:"",stderr:`sed: ${E.message}
`,exitCode:R.EXIT_CODE}:{stdout:"",stderr:`sed: ${h}: No such file or directory
`,exitCode:1}}}return{stdout:"",stderr:"",exitCode:0}}let m="";if(c.length===0){m=e.stdin;try{let h=await U(m,o,g,{limits:e.limits,fs:e.fs,cwd:e.cwd});return{stdout:h.output,stderr:h.errorMessage?`${h.errorMessage}
`:"",exitCode:h.exitCode??0}}catch(h){if(h instanceof R)return{stdout:"",stderr:`sed: ${h.message}
`,exitCode:R.EXIT_CODE};throw h}}let d=!1;for(let h of c){let f;if(h==="-")d?f="":(f=e.stdin,d=!0);else{let E=e.fs.resolvePath(e.cwd,h);try{f=await e.fs.readFile(E)}catch(C){return C instanceof R?{stdout:"",stderr:`sed: ${C.message}
`,exitCode:R.EXIT_CODE}:{stdout:"",stderr:`sed: ${h}: No such file or directory
`,exitCode:1}}}m.length>0&&f.length>0&&!m.endsWith(`
`)&&(m+=`
`),m+=f}try{let h=await U(m,o,g,{limits:e.limits,filename:c.length===1?c[0]:void 0,fs:e.fs,cwd:e.cwd});return{stdout:h.output,stderr:h.errorMessage?`${h.errorMessage}
`:"",exitCode:h.exitCode??0}}catch(h){if(h instanceof R)return{stdout:"",stderr:`sed: ${h.message}
`,exitCode:R.EXIT_CODE};throw h}}};export{Ie as sedCommand};
