svn commit: r261192 - in head/bin/sh: . tests/parser
Jilles Tjoelker
jilles at FreeBSD.org
Sun Jan 26 21:19:36 UTC 2014
Author: jilles
Date: Sun Jan 26 21:19:33 2014
New Revision: 261192
URL: http://svnweb.freebsd.org/changeset/base/261192
Log:
sh: Allow aliases to force alias substitution on the following word.
If an alias's value ends with a space or tab, the next word is also
checked for aliases.
This is a POSIX feature. It is useful with utilities like command and
nohup (alias them to themselves followed by a space).
Added:
head/bin/sh/tests/parser/alias14.0 (contents, props changed)
head/bin/sh/tests/parser/alias15.0 (contents, props changed)
head/bin/sh/tests/parser/alias15.0.stdout (contents, props changed)
Modified:
head/bin/sh/input.c
head/bin/sh/parser.c
head/bin/sh/parser.h
head/bin/sh/sh.1
head/bin/sh/tests/parser/Makefile
Modified: head/bin/sh/input.c
==============================================================================
--- head/bin/sh/input.c Sun Jan 26 20:51:49 2014 (r261191)
+++ head/bin/sh/input.c Sun Jan 26 21:19:33 2014 (r261192)
@@ -367,12 +367,16 @@ popstring(void)
struct strpush *sp = parsefile->strpush;
INTOFF;
+ if (sp->ap) {
+ if (parsenextc != sp->ap->val &&
+ (parsenextc[-1] == ' ' || parsenextc[-1] == '\t'))
+ forcealias();
+ sp->ap->flag &= ~ALIASINUSE;
+ }
parsenextc = sp->prevstring;
parsenleft = sp->prevnleft;
parselleft = sp->prevlleft;
/*out2fmt_flush("*** calling popstring: restoring to '%s'\n", parsenextc);*/
- if (sp->ap)
- sp->ap->flag &= ~ALIASINUSE;
parsefile->strpush = sp->prev;
if (sp != &(parsefile->basestrpush))
ckfree(sp);
Modified: head/bin/sh/parser.c
==============================================================================
--- head/bin/sh/parser.c Sun Jan 26 20:51:49 2014 (r261191)
+++ head/bin/sh/parser.c Sun Jan 26 21:19:33 2014 (r261192)
@@ -683,6 +683,12 @@ makebinary(int type, union node *n1, uni
}
void
+forcealias(void)
+{
+ checkkwd |= CHKALIAS;
+}
+
+void
fixredir(union node *n, const char *text, int err)
{
TRACE(("Fix redir %s %d\n", text, err));
Modified: head/bin/sh/parser.h
==============================================================================
--- head/bin/sh/parser.h Sun Jan 26 20:51:49 2014 (r261191)
+++ head/bin/sh/parser.h Sun Jan 26 21:19:33 2014 (r261192)
@@ -76,6 +76,7 @@ extern const char *const parsekwd[];
union node *parsecmd(int);
+void forcealias(void);
void fixredir(union node *, const char *, int);
int goodname(const char *);
int isassignment(const char *);
Modified: head/bin/sh/sh.1
==============================================================================
--- head/bin/sh/sh.1 Sun Jan 26 20:51:49 2014 (r261191)
+++ head/bin/sh/sh.1 Sun Jan 26 21:19:33 2014 (r261192)
@@ -32,7 +32,7 @@
.\" from: @(#)sh.1 8.6 (Berkeley) 5/4/95
.\" $FreeBSD$
.\"
-.Dd January 3, 2014
+.Dd January 26, 2014
.Dt SH 1
.Os
.Sh NAME
@@ -533,6 +533,20 @@ would become
.Pp
.Dl "ls -F foobar"
.Pp
+Aliases are also recognized after an alias
+whose value ends with a space or tab.
+For example, if there is also an alias called
+.Dq Li nohup
+with the value
+.Dq Li "nohup " ,
+then the input
+.Pp
+.Dl "nohup lf foobar"
+.Pp
+would become
+.Pp
+.Dl "nohup ls -F foobar"
+.Pp
Aliases provide a convenient way for naive users to
create shorthands for commands without having to learn how
to create functions with arguments.
Modified: head/bin/sh/tests/parser/Makefile
==============================================================================
--- head/bin/sh/tests/parser/Makefile Sun Jan 26 20:51:49 2014 (r261191)
+++ head/bin/sh/tests/parser/Makefile Sun Jan 26 21:19:33 2014 (r261192)
@@ -18,6 +18,8 @@ FILES+= alias10.0
FILES+= alias11.0
FILES+= alias12.0
FILES+= alias13.0
+FILES+= alias14.0
+FILES+= alias15.0 alias15.0.stdout
FILES+= and-pipe-not.0
FILES+= case1.0
FILES+= case2.0
Added: head/bin/sh/tests/parser/alias14.0
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ head/bin/sh/tests/parser/alias14.0 Sun Jan 26 21:19:33 2014 (r261192)
@@ -0,0 +1,6 @@
+# $FreeBSD$
+
+alias command='command '
+alias alias0=exit
+eval 'command alias0 0'
+exit 3
Added: head/bin/sh/tests/parser/alias15.0
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ head/bin/sh/tests/parser/alias15.0 Sun Jan 26 21:19:33 2014 (r261192)
@@ -0,0 +1,12 @@
+# $FreeBSD$
+
+f_echoanddo() {
+ printf '%s\n' "$*"
+ "$@"
+}
+
+alias echoanddo='f_echoanddo '
+alias alias0='echo test2'
+eval 'echoanddo echo test1'
+eval 'echoanddo alias0'
+exit 0
Added: head/bin/sh/tests/parser/alias15.0.stdout
==============================================================================
--- /dev/null 00:00:00 1970 (empty, because file is newly added)
+++ head/bin/sh/tests/parser/alias15.0.stdout Sun Jan 26 21:19:33 2014 (r261192)
@@ -0,0 +1,4 @@
+echo test1
+test1
+echo test2
+test2
More information about the svn-src-all
mailing list