forked from Hyperion/HyperionOS
2 lines
6.6 KiB
Plaintext
2 lines
6.6 KiB
Plaintext
local a=syscall.getTask(syscall.getpid()).name;local b={}local c={}local d=false;local e=false;local f={...}local g=1;while g<=#f do local h=f[g]if h=="-n"then d=true elseif h=="-i"then e=true elseif h=="-e"then g=g+1;if not f[g]then print(a..": option -e requires an argument")syscall.exit(1)return end;table.insert(b,f[g])elseif h:sub(1,2)=="-e"then table.insert(b,h:sub(3))elseif h=="--help"then print("Usage: "..a.." [OPTION]... SCRIPT [FILE...]")print(" "..a.." [OPTION]... -e SCRIPT... [FILE...]")print("Stream editor. Reads FILE(s) (or stdin) line by line,")print("applies SCRIPT, and writes results to stdout.")print("")print("Commands:")print(" s/REGEX/REPL/[flags] substitute (flags: g global, i ignore-case, p print)")print(" d delete line (skip to next)")print(" p print current line")print(" q quit")print(" = print current line number")print(" y/src/dst/ transliterate characters")print("")print("Addressing (prefix any command):")print(" N line number N")print(" $ last line")print(" /REGEX/ lines matching regex")print(" N,M line range")print(" N,/REGEX/ from line N until regex match")print("")print("Options:")print(" -n suppress default output")print(" -e SCRIPT add script expression")print(" -i edit file in-place")print(" --help display this help and exit")return elseif h:sub(1,1)=="-"then print(a..": unknown option: "..h)syscall.exit(1)return else if#b==0 then table.insert(b,h)else table.insert(c,h)end end;g=g+1 end;if#b==0 then print(a..": no script specified")syscall.exit(1)return end;local i=table.concat(b,"\n")local function j(k)return k:gsub("([%(%)%.%%%+%-%*%?%[%^%$])","%%%1")end;local function l(m,n)m=m:gsub("\\%(","("):gsub("\\%)",")")m=m:gsub("\\1","%%1"):gsub("\\2","%%2")return m end;local function o(k,p,q)local r={}while p<=#k do local s=k:sub(p,p)if s=="\\"and p<#k then p=p+1;local t=k:sub(p,p)if t==q then table.insert(r,q)elseif t=="n"then table.insert(r,"\n")else table.insert(r,"\\"..t)end elseif s==q then return table.concat(r),p+1 else table.insert(r,s)end;p=p+1 end;return table.concat(r),p end;local function u(k,p)local s=k:sub(p,p)if s==""then return nil,p end;if s:match("%d")then local v=k:match("^(%d+)",p)return{type="line",n=tonumber(v)},p+#v elseif s=="$"then return{type="last"},p+1 elseif s=="/"then local m,w=o(k,p+1,"/")return{type="regex",pat=m},w end;return nil,p end;local function x(y)local z={}local p=1;local A=#y;local function B()while p<=A and(y:sub(p,p)==" "or y:sub(p,p)=="\t")do p=p+1 end end;while p<=A do B()if p>A then break end;local s=y:sub(p,p)if s=="\n"or s==";"then p=p+1 elseif s=="#"then while p<=A and y:sub(p,p)~="\n"do p=p+1 end else local C,D;C,p=u(y,p)B()if C and p<=A and y:sub(p,p)==","then p=p+1;B()D,p=u(y,p)end;B()if p>A then break end;local E=y:sub(p,p)p=p+1;if E=="s"then local q=y:sub(p,p)p=p+1;local m,F=o(y,p,q)p=F;local G,H=o(y,p,q)p=H;local I=""while p<=A and y:sub(p,p):match("[giIp]")do I=I..y:sub(p,p)p=p+1 end;table.insert(z,{addr1=C,addr2=D,cmd="s",pat=m,repl=G,flags=I})elseif E=="y"then local q=y:sub(p,p)p=p+1;local J,F=o(y,p,q)p=F;local K,H=o(y,p,q)p=H;table.insert(z,{addr1=C,addr2=D,cmd="y",src=J,dst=K})elseif E=="d"or E=="p"or E=="q"or E=="="then table.insert(z,{addr1=C,addr2=D,cmd=E})elseif E=="{"then local L=1;local M=p;while p<=A and L>0 do local N=y:sub(p,p)if N=="{"then L=L+1 elseif N=="}"then L=L-1 end;p=p+1 end;local O=y:sub(M,p-2)local P=x(O)for Q,R in ipairs(P)do R.addr1=R.addr1 or C;R.addr2=R.addr2 or D end;for Q,R in ipairs(P)do table.insert(z,R)end elseif E=="\n"or E==";"then else end end end;return z end;local z=x(i)local S={}local function T(E,U,V,W,X)local Y=E.addr1;local Z=E.addr2;if not Y then return true end;local function _(a0,a1,a2)if a0.type=="line"then return a1==a0.n elseif a0.type=="last"then return W elseif a0.type=="regex"then return a2:find(l(a0.pat))~=nil end;return false end;if not Z then return _(Y,U,V)end;if S[X]then local a3;if Z.type=="line"then a3=U>=Z.n elseif Z.type=="last"then a3=W elseif Z.type=="regex"then a3=V:find(l(Z.pat))~=nil end;if a3 then S[X]=false end;return true else if _(Y,U,V)then if Z.type=="line"and Z.n<=U then else S[X]=true end;return true end;return false end end;local function a4(V,m,G,I)local a5=I:find("g")~=nil;local n=I:find("[iI]")~=nil;local a6=l(m,n)local function a7(a8,...)local a9={...}local r={}local aa=G;local ab=1;while ab<=#aa do local ac=aa:sub(ab,ab)if ac=="&"then table.insert(r,a8)elseif ac=="\\"and ab<#aa then ab=ab+1;local t=aa:sub(ab,ab)if t:match("%d")then local ad=tonumber(t)table.insert(r,a9[ad]or"")elseif t=="n"then table.insert(r,"\n")else table.insert(r,t)end else table.insert(r,ac)end;ab=ab+1 end;return table.concat(r)end;local ae;local af=false;if a5 then ae=V:gsub(a6,a7)af=ae~=V else local k,ag,a8;local ah={V:find(a6)}if ah[1]then k=ah[1]ag=ah[2]local a9={}for X=3,#ah do a9[#a9+1]=ah[X]end;local ai=V:sub(k,ag)local aj=a7(ai,table.unpack(a9))ae=V:sub(1,k-1)..aj..V:sub(ag+1)af=true else ae=V end end;return ae,af end;local function ak(V,y,al)local r={}for X=1,#V do local s=V:sub(X,X)local ad=y:find(s,1,true)if ad and ad<=#al then table.insert(r,al:sub(ad,ad))else table.insert(r,s)end end;return table.concat(r)end;local function am(an,ao)local ap=#an;for U,V in ipairs(an)do local W=U==ap;local aq=false;local ar=false;local as=false;local at=V:gsub("\n$","")for X,E in ipairs(z)do if T(E,U,at,W,X)then if E.cmd=="d"then aq=true;break elseif E.cmd=="p"then table.insert(ao,at)elseif E.cmd=="="then table.insert(ao,tostring(U))elseif E.cmd=="q"then if not d then table.insert(ao,at)end;as=true;break elseif E.cmd=="s"then local au,af=a4(at,E.pat,E.repl,E.flags)at=au;if af and E.flags:find("p")then table.insert(ao,at)end elseif E.cmd=="y"then at=ak(at,E.src,E.dst)end end end;if as then break end;if not aq and not d then table.insert(ao,at)end end end;local function av(aw)local an={}local ax=""while true do local ay=syscall.read(aw,1024)if not ay or ay==""then break end;ax=ax..ay end;for V in(ax.."\n"):gmatch("([^\n]*)\n")do table.insert(an,V)end;if ax~=""and ax:sub(-1)~="\n"and an[#an]==""then table.remove(an)end;return an end;local function az(aA)local aw;if aA then local aB,aC=pcall(function()aw=syscall.open(aA,"r")end)if not aB then print(a..": "..aA..": "..tostring(aC))return false end else aw=0 end;local an=av(aw)if aA then syscall.close(aw)end;S={}local ao={}am(an,ao)if e and aA then local aD=syscall.open(aA,"w")for Q,aE in ipairs(ao)do syscall.write(aD,aE.."\n")end;syscall.close(aD)else for Q,aE in ipairs(ao)do print(aE)end end;return true end;if#c==0 then az(nil)else for Q,aF in ipairs(c)do local aG=aF;if aG:sub(1,1)~="/"then aG=syscall.getcwd().."/"..aF end;az(aG)end end
|