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.
- package/dist/Bash.d.ts +18 -0
- package/dist/ai/index.d.ts +6 -1
- package/dist/bin/chunks/awk2-6XCE67AX.js +20 -0
- package/dist/bin/chunks/awk2-DKAZ7LPG.js +20 -0
- package/dist/bin/chunks/awk2-GAJ72X3Z.js +20 -0
- package/dist/bin/chunks/awk2-HJAPWU2N.js +18 -0
- package/dist/bin/chunks/awk2-LVXC5OAC.js +20 -0
- package/dist/bin/chunks/awk2-T6HR4EYO.js +20 -0
- package/dist/bin/chunks/awk2-UYU2UYZ2.js +20 -0
- package/dist/bin/chunks/awk2-W64AZN7P.js +20 -0
- package/dist/bin/chunks/find-7BUNQZX4.js +11 -0
- package/dist/bin/chunks/find-YATLECXJ.js +11 -0
- package/dist/bin/chunks/grep-PHBFGJCV.js +15 -0
- package/dist/bin/chunks/sed-B6FXUN7R.js +80 -0
- package/dist/bin/chunks/sed-RHLDIXPL.js +76 -0
- package/dist/bin/chunks/sed-SYSATJXR.js +75 -0
- package/dist/bin/just-bash.js +102 -102
- package/dist/bin/shell/chunks/awk2-6XCE67AX.js +20 -0
- package/dist/bin/shell/chunks/awk2-DKAZ7LPG.js +20 -0
- package/dist/bin/shell/chunks/awk2-GAJ72X3Z.js +20 -0
- package/dist/bin/shell/chunks/awk2-HJAPWU2N.js +18 -0
- package/dist/bin/shell/chunks/awk2-LVXC5OAC.js +20 -0
- package/dist/bin/shell/chunks/awk2-T6HR4EYO.js +20 -0
- package/dist/bin/shell/chunks/awk2-UYU2UYZ2.js +20 -0
- package/dist/bin/shell/chunks/awk2-W64AZN7P.js +20 -0
- package/dist/bin/shell/chunks/find-7BUNQZX4.js +11 -0
- package/dist/bin/shell/chunks/find-YATLECXJ.js +11 -0
- package/dist/bin/shell/chunks/grep-PHBFGJCV.js +15 -0
- package/dist/bin/shell/chunks/sed-B6FXUN7R.js +80 -0
- package/dist/bin/shell/chunks/sed-RHLDIXPL.js +76 -0
- package/dist/bin/shell/chunks/sed-SYSATJXR.js +75 -0
- package/dist/bin/shell/shell.js +124 -128
- package/dist/bundle/ai/index.js +423 -386
- package/dist/bundle/chunks/awk2-365HINHI.js +19 -0
- package/dist/bundle/chunks/awk2-7YCFY7D2.js +19 -0
- package/dist/bundle/chunks/awk2-7YL3P4KU.js +19 -0
- package/dist/bundle/chunks/awk2-ETXZHQQ4.js +19 -0
- package/dist/bundle/chunks/awk2-FMEL7ESV.js +19 -0
- package/dist/bundle/chunks/awk2-LE4QUFGK.js +19 -0
- package/dist/bundle/chunks/awk2-MGHA6JPJ.js +17 -0
- package/dist/bundle/chunks/awk2-XEAK5PBN.js +19 -0
- package/dist/bundle/chunks/find-KP4M5DWC.js +10 -0
- package/dist/bundle/chunks/find-WM6ZETA7.js +10 -0
- package/dist/bundle/chunks/grep-CZTYKNEE.js +14 -0
- package/dist/bundle/chunks/sed-2WWCANZT.js +79 -0
- package/dist/bundle/chunks/sed-5LZHAGFR.js +74 -0
- package/dist/bundle/chunks/sed-V7XSBPTD.js +75 -0
- package/dist/bundle/index.js +8 -8
- package/dist/commands/awk/ast.d.ts +201 -0
- package/dist/commands/awk/awk2.d.ts +7 -0
- package/dist/commands/awk/builtins.d.ts +17 -0
- package/dist/commands/awk/interpreter/context.d.ts +53 -0
- package/dist/commands/awk/interpreter/expressions.d.ts +17 -0
- package/dist/commands/awk/interpreter/fields.d.ts +25 -0
- package/dist/commands/awk/interpreter/helpers.d.ts +29 -0
- package/dist/commands/awk/interpreter/index.d.ts +8 -0
- package/dist/commands/awk/interpreter/interpreter.d.ts +50 -0
- package/dist/commands/awk/interpreter/statements.d.ts +11 -0
- package/dist/commands/awk/interpreter/types.d.ts +13 -0
- package/dist/commands/awk/interpreter/variables.d.ts +35 -0
- package/dist/commands/awk/interpreter2.d.ts +90 -0
- package/dist/commands/awk/lexer.d.ts +100 -0
- package/dist/commands/awk/parser2.d.ts +73 -0
- package/dist/commands/find/matcher.d.ts +6 -2
- package/dist/commands/find/types.d.ts +13 -0
- package/dist/commands/sed/executor.d.ts +1 -1
- package/dist/commands/sed/lexer.d.ts +74 -0
- package/dist/commands/sed/parser.d.ts +4 -0
- package/dist/commands/sed/types.d.ts +66 -2
- package/dist/index.d.ts +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import{i as O}from"./chunk-LV662IGP.js";import{a as A,b as y,c as k}from"./chunk-GTNBSMZR.js";import"./chunk-5KNEBKYN.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===`
|
|
3
|
+
`?e+=`$
|
|
4
|
+
`: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+=`
|
|
5
|
+
`,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(`
|
|
6
|
+
`);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(`
|
|
7
|
+
`);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+=`
|
|
8
|
+
${e.patternSpace}`:e.holdSpace=e.patternSpace;break;case"get":e.patternSpace=e.holdSpace;break;case"getAppend":e.patternSpace+=`
|
|
9
|
+
${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}
|
|
10
|
+
`});break;case"writeFirstLine":{let s=e.patternSpace.indexOf(`
|
|
11
|
+
`),l=s!==-1?e.patternSpace.slice(0,s):e.patternSpace;e.pendingFileWrites.push({filename:i.filename,content:`${l}
|
|
12
|
+
`});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+=`
|
|
13
|
+
${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===`
|
|
14
|
+
`?(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()!==`
|
|
15
|
+
`;)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===`
|
|
16
|
+
`?(this.advance(),{type:a.NEWLINE,value:`
|
|
17
|
+
`,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()===`
|
|
18
|
+
`)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===`
|
|
19
|
+
`||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===`
|
|
20
|
+
`)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()!==`
|
|
21
|
+
`&&(r+=this.advance());else{if(this.peek()===`
|
|
22
|
+
`)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()!==`
|
|
23
|
+
`&&(s+=this.advance());else{if(this.peek()===`
|
|
24
|
+
`)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===`
|
|
25
|
+
`)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+=`
|
|
26
|
+
`:l==="t"?r+=" ":r+=l}else{if(this.peek()===`
|
|
27
|
+
`)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+=`
|
|
28
|
+
`:l==="t"?s+=" ":s+=l}else{if(this.peek()===`
|
|
29
|
+
`)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()!==`
|
|
30
|
+
`;)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===`
|
|
31
|
+
`||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===`
|
|
32
|
+
`||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===`
|
|
33
|
+
`||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:
|
|
34
|
+
s/regexp/replacement/[flags] substitute
|
|
35
|
+
d delete pattern space
|
|
36
|
+
p print pattern space
|
|
37
|
+
a\\ text append text after line
|
|
38
|
+
i\\ text insert text before line
|
|
39
|
+
c\\ text change (replace) line with text
|
|
40
|
+
h copy pattern space to hold space
|
|
41
|
+
H append pattern space to hold space
|
|
42
|
+
g copy hold space to pattern space
|
|
43
|
+
G append hold space to pattern space
|
|
44
|
+
x exchange pattern and hold spaces
|
|
45
|
+
n read next line into pattern space
|
|
46
|
+
N append next line to pattern space
|
|
47
|
+
y/source/dest/ transliterate characters
|
|
48
|
+
= print line number
|
|
49
|
+
l list pattern space (escape special chars)
|
|
50
|
+
b [label] branch to label
|
|
51
|
+
t [label] branch on substitution
|
|
52
|
+
T [label] branch if no substitution
|
|
53
|
+
:label define label
|
|
54
|
+
q quit
|
|
55
|
+
Q quit without printing
|
|
56
|
+
|
|
57
|
+
Addresses:
|
|
58
|
+
N line number
|
|
59
|
+
$ last line
|
|
60
|
+
/regexp/ lines matching regexp
|
|
61
|
+
N,M range from line N to M
|
|
62
|
+
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(`
|
|
63
|
+
`);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}
|
|
64
|
+
`;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}
|
|
65
|
+
`;!c.deleted&&!c.quitSilent&&(t?c.printed&&(p+=`${c.patternSpace}
|
|
66
|
+
`):p+=`${c.patternSpace}
|
|
67
|
+
`);for(let b of I)p+=`${b}
|
|
68
|
+
`;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(`
|
|
69
|
+
`)){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
|
|
70
|
+
`,exitCode:1}}}if(t.length===0)return{stdout:"",stderr:`sed: no script specified
|
|
71
|
+
`,exitCode:1};let{commands:p,error:d}=B(t,l);if(d)return{stdout:"",stderr:`sed: ${d}
|
|
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;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
|
|
74
|
+
`,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
|
|
75
|
+
`,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};
|