be more robust against things like Debian #535970

reverts and rewrites the code from cid 10047C1EBA57E4F4AF0

XXX find out if this is done right
This commit is contained in:
tg 2009-07-06 15:06:25 +00:00
parent 0b4b689c92
commit f349aeab3c
4 changed files with 82 additions and 11 deletions

40
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.289 2009/07/05 13:56:46 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.290 2009/07/06 15:06:23 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -25,7 +25,7 @@
# http://www.research.att.com/~gsf/public/ifs.sh
expected-stdout:
@(#)MIRBSD KSH R38 2009/07/05
@(#)MIRBSD KSH R38 2009/07/06
description:
Check version of shell.
stdin:
@ -4804,6 +4804,42 @@ expected-stdout:
suspend='kill -STOP $$'
type='whence -v'
---
name: aliases-funcdef-1
description:
Check if POSIX functions take precedences over aliases
stdin:
alias foo='echo makro'
foo() {
echo funktion
}
foo
expected-stdout:
funktion
---
name: aliases-funcdef-2
description:
Check if POSIX functions take precedences over aliases
stdin:
alias foo='echo makro'
foo () {
echo funktion
}
foo
expected-stdout:
funktion
---
name: aliases-funcdef-3
description:
Check if aliases take precedences over Korn functions
stdin:
alias foo='echo makro'
function foo {
echo funktion
}
foo
expected-stdout:
makro
---
name: arrays-1
description:
Check if Korn Shell arrays work as expected

40
lex.c
View File

@ -22,7 +22,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.88 2009/06/11 12:42:19 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.89 2009/07/06 15:06:23 tg Exp $");
/*
* states while lexing word
@ -159,7 +159,6 @@ yylex(int cf)
char *wp; /* output word pointer */
char *sp, *dp;
int c2;
bool last_terminal_was_bracket;
Again:
states[0].ls_state = -1;
@ -881,8 +880,14 @@ yylex(int cf)
state == SLETARRAY) /* ONEWORD? */
return (LWORD);
last_terminal_was_bracket = c == '(';
ungetsc(c); /* unget terminator */
/* unget terminator */
ungetsc(c);
/*
* note: the alias-vs-function code below depends on several
* interna: starting from here, source->str is not modified;
* the way getsc() and ungetsc() operate; etc.
*/
/* copy word to unprefixed string ident */
sp = yylval.cp;
@ -914,8 +919,31 @@ yylex(int cf)
}
if ((cf & ALIAS) && (p = ktsearch(&aliases, ident, h)) &&
(p->flag & ISSET)) {
if (last_terminal_was_bracket)
/* prefer functions over aliases */
/*
* this still points to the same character as the
* ungetsc'd terminator from above
*/
const char *cp = source->str;
/* prefer POSIX but not Korn functions over aliases */
while (*cp == ' ' || *cp == '\t')
/*
* this is like getsc() without skipping
* over Source boundaries (including not
* parsing ungetsc'd characters that got
* pushed into an SREREAD) which is what
* we want here anyway: find out whether
* the alias name is followed by a POSIX
* function definition (only the opening
* parenthesis is checked though)
*/
++cp;
/* prefer functions over aliases */
if (*cp == '(' /*)*/)
/*
* delete alias upon encountering function
* definition
*/
ktdelete(p);
else {
Source *s = source;

9
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.171 2009/07/06 09:37:03 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.172 2009/07/06 15:06:24 tg Exp $
.\" $OpenBSD: ksh.1,v 1.129 2009/05/28 06:09:06 jmc Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
@ -738,6 +738,9 @@ Mostly the same as
(see
.Sx Functions
below).
Whitespace (space or tab) after
.Ar name
will be ignored most of the time.
.It Xo Ic time Op Fl p
.Op Ar pipeline
.Xc
@ -2479,6 +2482,10 @@ untouched, so using
inside a function interferes with using
.Ic getopts
outside the function).
.It
Bourne-style function definitions take precedence over alias dereferences
and remove alias definitions upon encounter, while aliases take precedence
over Korn-style functions.
.El
.Pp
In the future, the following differences will also be added:

4
sh.h
View File

@ -122,9 +122,9 @@
#define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.311 2009/07/05 13:56:48 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.312 2009/07/06 15:06:25 tg Exp $");
#endif
#define MKSH_VERSION "R38 2009/07/05"
#define MKSH_VERSION "R38 2009/07/06"
#ifndef MKSH_INCLUDES_ONLY