Op 20-03-17 om 05:06 schreef Thorsten Glaser:
> Martijn Dekker dixit:
> 
>> LINENO is set to zero during the execution of aliases or commands in
>> 'eval'. This behaviour is different from that of other shells.
> 
> Patches welcome ☺

OK, it turns out that LINENO==0 within alias expansion and LINENO==0
within 'eval' execution are actually two separate problems requiring
separate fixes.

The simple patch attached
- preserves LINENO for alias expansion, and
- makes LINENO within 'eval' act like ksh88, which is good enough for
me. I guess I'd prefer it to act like ksh93, *ash, yash and native zsh,
but I can't figure out how to make that happen.

> (What do you *use* LINENO for? I’ve *never* seen it used *at all*.)

Well, LINENO is POSIX so it ought to work in any case. But, since you
asked...

Modernish <https://github.com/modernish/modernish> includes the
var/setlocal module which provides a stack-based implementation of code
blocks with local variables and shell options. Internally it uses a
temporary shell function. It's basically equivalent to anonymous
functions in zsh, but (like the rest of modernish) works on all POSIX
shells.

It's based on two aliases like this:

alias setlocal='{ _Msh_sL_temp() { _Msh_doSetLocal "${LINENO-}"'

alias endlocal='} && { _Msh_sL_temp "$@"; _Msh_doEndLocal "$?"
"${LINENO-}"; }; }'

LINENO is passed to the internal handling functions for use in error
messages (such as incorrect arguments or stack corruption). On mksh,
those error messages always indicate line 0, because $LINENO is expanded
while expanding an alias.

>> Output on various shells:
>>            mksh: 1 2 0 0 0 6
>>            lksh: 1 2 0 0 0 6
> 
> Don’t forget “lksh -o posix”, which is likely more important
> to check than just lksh.

Same result.

Thanks,

- M.

Index: funcs.c
===================================================================
RCS file: /cvs/src/bin/mksh/funcs.c,v
retrieving revision 1.330
diff -u -r1.330 funcs.c
--- funcs.c	19 Mar 2017 20:59:50 -0000	1.330
+++ funcs.c	20 Mar 2017 18:06:44 -0000
@@ -2410,6 +2410,7 @@
 
 	savef = Flag(FERREXIT);
 	Flag(FERREXIT) |= 0x80;
+	s->line = current_lineno;
 	rv = shell(s, false);
 	Flag(FERREXIT) = savef;
 	source = saves;
Index: lex.c
===================================================================
RCS file: /cvs/src/bin/mksh/lex.c,v
retrieving revision 1.230
diff -u -r1.230 lex.c
--- lex.c	12 Mar 2017 02:04:39 -0000	1.230
+++ lex.c	20 Mar 2017 18:06:45 -0000
@@ -1064,6 +1064,7 @@
 				s->u.tblp = p;
 				s->flags |= SF_HASALIAS;
 				s->next = source;
+				s->line = source->line;
 				if (source->type == SEOF) {
 					/* prevent infinite recursion at EOS */
 					source->u.tblp = p;

Reply via email to