Re: [dev] [sbase] sed 'd' command after 'r' or 'a' (POSIX interpretation question)

From: Quentin Rameau <quinq_AT_fifth.space>
Date: Fri, 17 Jan 2020 15:37:14 +0100

> Hello,

Hi Michael,

> I recently ran into a package (dialog), which uses the following sed
> script to generate its dlg_config.h header:
>
> /_AT_DEFS@/r conftest.frag
> /_AT_DEFS@/d
>
> The intention is to replace _AT_DEFS@ in the header template with the
> contents of a file. However, sbase sed skips writing the contents of
> the file since the pattern space gets deleted later on.
>
> All the other implementations I looked at *do* insert the contents of
> the file.
>
> Here's what POSIX says on the matter:
>
> > The a and r commands schedule text for later output. The text
> > specified for the a command, and the contents of the file specified
> > for the r command, shall be written to standard output just before
> > the next attempt to fetch a line of input when executing the N or n
> > commands, or when reaching the end of the script. If written when
> > reaching the end of the script, and the −n option was not
> > specified, the text shall be written after copying the pattern
> > space to standard output. The contents of the file specified for
> > the r command shall be as of the time the output is written, not
> > the time the r command is applied. The text shall be output in the
> > order in which the a and r commands were applied to the input.
>
> and
>
> > [2addr]d Delete the pattern space and start the next cycle.
>
> Only the cases where n or N are used or the end of the script is
> reached are described. So I guess the question is whether or not
> "start the next cycle" means "jump to the end of the script". It's not
> really clear to me from the POSIX description.
>
> Does anyone have any insight on this issue? I'm thinking we should
> probably change sbase sed to match the other implementations.

I think that strictly speaking, sbase sed is correct.

start the next cycle doesn't mean jump to the end of the script.

The r description explicitely says “If written when reaching the end of
the script […]” which implies it can not be written.
Also, branching commands say that they should branch “to the end of the
script”, not “start a new cycle”.
The d command says “start a new cycle”, not “branch to the end of the
script”.

Also in the rationale, we can read “valid scripts that interrupt the
cyclical nature of the processing through the use of the D, d, q, or
branching commands.”

And “If no commands explicitly started a new cycle, then at the end of
the script the pattern space shall be copied to standard output”

Now, that could be a bug in sed specification, but as it is, I see the
current behaviour as correct according to POSIX.
Received on Fri Jan 17 2020 - 15:37:14 CET

This archive was generated by hypermail 2.3.0 : Fri Jan 17 2020 - 15:48:07 CET