Hoi,
via a recent message from Chris Pinnock to the list I became aware
of the book ``Ed Mastery'' by Michael W. Lucas. At once I bought
and read it. Although it is not on the mastery level it claims and
I would have liked it to be, it still was fun to read.
This brought me back to my ed interest. I like ed a lot and despite
my young age, I've actually programmed with ed for fun and have
prepared the troff slides for a talk on early Unix tools (like ed)
with ed alone. I use the Heirloom version of ed.
Anyways, I wondered about the possibility to give multiple
addresses ... more than two for relative address searches.
For example, to print the context of the first occurance of `argv'
within the main function, you can use:
/^main(/;/\<argv\>/-2;+4n
For the last occurance it's even one level more:
/^main(/;/^}/;?\<argv\>?-2;+4n
(The semicolons mean that the next search or relative addressing
starts at the result of the previous one. I.e. in this case: We go
to the `main' function, from there go to the function end, then
backwards to `argv' minus two lines and print (with line numbers)
this line and four lines more.)
The manpage of 6th Edition mentiones this possibility to give more
than two addresses:
Commands may require zero, one, or two addresses. Commands
which require no addresses regard the presence of an address
as an error. Commands which accept one or two addresses
assume default addresses when insufficient are given. If
more addresses are given than such a command requires, the
last one or two (depending on what is accepted) are used.
http://man.cat-v.org/unix-6th/1/ed
You can see it in the sources as well:
https://www.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s1/ed.c
(Search for ';' to find the line. There's a loop processing the
addresses.)
V5 ed(1) is in assembler, however, which I cannot read. Thus there
must have been a complete rewrite, maybe introducing this feature
at that point. (I don't know where to find v5 manpage to check
that as well.)
I wonder how using multiple addresses for setting starting points
for relative searches came to be. When was it implemented and what
use cases drove this features back in the days? Or was it more an
accident that was introduced by the implementation, which turned
out to be useful? Or maybe it existed already in earlier versions
of ed, althoug maybe undocumented.
For reference, POSIX writes:
Commands accept zero, one, or two addresses. If more than the
required number of addresses are provided to a command that
requires zero addresses, it shall be an error. Otherwise, if more
than the required number of addresses are provided to a command,
the addresses specified first shall be evaluated and then discarded
until the maximum number of valid addresses remain, for the
specified command.
https://pubs.opengroup.org/onlinepubs/9699919799/utilities/ed.html
Here more explanation rom the rationale section:
Any number of addresses can be provided to commands taking
addresses; for example, "1,2,3,4,5p" prints lines 4 and 5, because
two is the greatest valid number of addresses accepted by the print
command. This, in combination with the <semicolon> delimiter,
permits users to create commands based on ordered patterns in the
file. For example, the command "3;/foo/;+2p" will display the first
line after line 3 that contains the pattern foo, plus the next two
lines. Note that the address "3;" must still be evaluated before
being discarded, because the search origin for the "/foo/" command
depends on this.
As far as I can see, multiple addresses make only sense with the
semicolon separator, because the comma separator does not change
the state, thus previous addresses can have no effect on later
addresses. The implementation just does not forbid them, for
simplicity reasons.
meillo