From ee5cbe9fd6bc02f31b4d955606288de36c3d4eab Mon Sep 17 00:00:00 2001 From: Herbert Xu Date: Thu, 7 Jul 2011 13:58:48 +0800 Subject: [SHELL] Optimize dash -c "command" to avoid a fork On Sun, Apr 10, 2011 at 07:36:49AM +0000, Jonathan Nieder wrote: > From: Jilles Tjoelker > Date: Sat, 13 Jun 2009 16:17:45 -0500 > > This change only affects strings passed to -c, when the -s option is > not used. > > Use the EV_EXIT flag to inform the eval machinery that the string > being passed is the entirety of input. This way, a fork may be > omitted in many special cases. > > If there are empty lines after the last command, the evalcmd will not > see the end early enough and forks will not be omitted. The same thing > seems to happen in bash. > > Example: > sh -c 'ps lT' > No longer shows a shell process waiting for ps to finish. > > [jn: ported from FreeBSD SVN r194128. Bugs are mine.] > > Signed-off-by: Jonathan Nieder Instead of detecting EOF using the input layer, I'm going to use the parser instead. In either case, we always have to read ahead in order to complete the parsing of the previous node. Therefore we always know whether there is more to come, except in the case where we see a newline/semicolon or similar. For the purposes of sh -c, this should be sufficient. Signed-off-by: Herbert Xu --- src/parser.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src/parser.c') diff --git a/src/parser.c b/src/parser.c index 528d005..6de2762 100644 --- a/src/parser.c +++ b/src/parser.c @@ -64,7 +64,7 @@ */ /* values returned by readtoken */ -#include "token.h" +#include "token_vars.h" @@ -86,7 +86,7 @@ struct heredoc *heredoclist; /* list of here documents to read */ int doprompt; /* if set, prompt the user */ int needprompt; /* true if interactive and at start of line */ int lasttoken; /* last token read */ -MKINIT int tokpushback; /* last token pushed back */ +int tokpushback; /* last token pushed back */ char *wordtext; /* text of last word returned by readtoken */ int checkkwd; struct nodelist *backquotelist; @@ -210,6 +210,7 @@ list(int nlflag) parseheredoc(); else pungetc(); /* push back EOF on input */ + tokpushback++; return n1; default: if (nlflag == 1) -- cgit 1.4.1