[PATCH] adding the trap ERR feature

Harald van Dijk harald at gigawatt.nl
Fri Aug 13 22:30:45 UTC 2021


Hi,

On 13/08/2021 19:20, Roberto A. Foglietta wrote:
> At the moment when $LINENO is printed by a function the number displayed 
> is the lines from the start of the function.
> Unfortunately, this information is useless because we do not have $FUNCNAME.
> Even if we have $FUNCNAME having always an absolute $LINENO will be more 
> easier.

I'm the one who implemented LINENO handling in dash, which is what 
busybox's is based on. LINENO in functions being relative to the start 
of the function was very much intentional: it is required by POSIX. I 
think just dropping that entirely is wrong. However, at the same time, I 
no longer believe POSIX requires LINENO in functions to _always_ be 
relative to the start of the function.

Here's what POSIX says:

   LINENO
     Set by the shell to a decimal number representing the current
     sequential line number (numbered starting with 1) within a script or
     function before it executes each command. If the user unsets or
     resets LINENO, the variable may lose its special meaning for the
     life of the shell. If the shell is not currently executing a script
     or function, the value of LINENO is unspecified. This volume of
     POSIX.1-2017 specifies the effects of the variable only for systems
     supporting the User Portability Utilities option.

What POSIX fails to specify is what happens when a function is defined 
in a script. In that case, the line number can be either relative to the 
start of the script, or to the start of the function, there is no text 
here to specify which takes precedence. I agree that relative to the 
start of the script is more intuitive, and as this is what other shells 
already did, I believe that a reading of POSIX that disallows that 
traditional behaviour, including my original reading, must be an 
incorrect reading.

However, for functions defined outside of a script, the only way to 
interpret the requirements is for LINENO to remain relative to the start 
of the function as it is now.

I am maintaining my own fork of dash nowadays, and in that I modified it 
to behave like ksh like so: 
<https://github.com/hvdijk/gwsh/commit/2d4e35cdb4728cfa6de1b8a297ecbd7143fb2d08>. 
The commit message shows the effects of the commit, and I think that 
matches what you would like to see. If this commit is of interest to 
busybox, feel free to use it.

Cheers,
Harald van Dijk


More information about the busybox mailing list