Select Git revision
sed.pmod 6.09 KiB
#pike __REAL_VERSION__
//! edit commands supported:
//! @pre{
//! <firstline>,<lastline><edit command>
//! ^^ numeral (17) ^^
//! or relative (+17, -17)
//! or a search regexp (/regexp/)
//! or multiple (17/regexp//regexp/+2)
//! @}
//!
//! @xml{
//! <matrix>
//! <r><c><b>Command</b></c><c><b>Action</b></c></r>
//! <r><c>D</c><c>Delete first line in space</c></r>
//! <r><c>G</c><c>Insert hold space</c></r>
//! <r><c>H</c><c>Append current space to hold space</c></r>
//! <r><c>P</c><c>Print current data</c></r>
//! <r><c>a<string></c><c>Insert</c></r>
//! <r><c>c<string></c><c>Change current space</c></r>
//! <r><c>d</c><c>Delete current space</c></r>
//! <r><c>h</c><c>Copy current space to hold space</c></r>
//! <r><c>i<string></c><c>Print string</c></r>
//! <r><c>l</c><c>Print current space</c></r>
//! <r><c>p</c><c>Print first line in data</c></r>
//! <r><c>q</c><c>Quit evaluating</c></r>
//! <r><c>s/regexp/with/x</c><c>Replace</c></r>
//! <r><c>y/chars/chars/</c><c>Replace chars</c></r>
//! </matrix>
//! @}
//!
//! where line is numeral, first 'line'==0
protected array sedreplace(string s,object re,string with,
array whatin,int first,int lastmod,
multiset flags)
{
array a;
string w=0;
array pr=({});
if (!(a=re->split(s)))
return 0;
if (first)
{
array wa;
wa=sedreplace(a[0],re,with,whatin,first,lastmod,flags);
if (wa)
if (!flags["g"])
return ({wa[0],wa[1]+s[sizeof(a[0])..]});
else
pr=wa[0],w=wa[1];
else
w=a[0];
}
string t=
replace(with,whatin[..sizeof(a)-first+lastmod-1],
a[first..sizeof(a)+lastmod-1]);
if (flags["p"]) pr+=({t});
s=(w||"")+t;
if (flags["g"])
{
if (lastmod)
{
array wa;
wa=sedreplace(a[-1],re,with,whatin,first,lastmod,flags);