Commit a0a62ab4 authored by jan.koester's avatar jan.koester
Browse files

test

parent ab1fc1c6
Loading
Loading
Loading
Loading
+43 −2
Changes for src/shell.cpp: 43 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -1866,7 +1866,7 @@ int fresh::Shell::runExternal(const char **cargs){
            // it).
            std::cout.flush();
            std::cerr.flush();
            pid_t pid=fork();
            pid_t pid=_execDirect ? 0 : fork();
            if(pid==0){
                execv(cargs[0],const_cast<char *const *>(cargs));
                // execv() only ever returns on failure -- e.g. this binary
@@ -1900,7 +1900,7 @@ int fresh::Shell::runExternal(const char **cargs){
            // $PATH-search branch) for why this is needed.
            std::cout.flush();
            std::cerr.flush();
            pid_t pid=fork();
            pid_t pid=_execDirect ? 0 : fork();
            if(pid==0){
                execv(candidate.c_str(),const_cast<char *const *>(cargs));
                std::cerr << "fresh: " << candidate << ": " << strerror(errno) << std::endl;
@@ -1937,6 +1937,39 @@ pid_t fresh::Shell::startBackground(const std::string &text){
        _heredocSource=nullptr;   // see runCaptured(): never read more script
        _bgJobs.clear();
        _bgFinished.clear();
        {
            // one simple command (no list, pipeline or compound command,
            // not a builtin/function, no leading assignment): exec it
            std::vector<ShellWord> w=tokenizeShellWords(text);
            bool simple=!w.empty();
            for(const ShellWord &x : w)
                if(x.text==";" || x.text=="&&" || x.text=="||" || x.text=="|" || x.text=="&")
                    simple=false;
            // leading NAME=value assignments only go into the command's
            // environment: look at the command word after them
            size_t cmdIdx=0;
            auto isAssignment=[](const std::string &t){
                size_t eq=t.find('=');
                if(eq==std::string::npos || eq==0) return false;
                if(!(isalpha((unsigned char)t[0]) || t[0]=='_')) return false;
                for(size_t k=1; k<eq; ++k)
                    if(!(isalnum((unsigned char)t[k]) || t[k]=='_')) return false;
                return true;
            };
            while(simple && cmdIdx<w.size() && isAssignment(w[cmdIdx].text)) ++cmdIdx;
            if(cmdIdx>=w.size()) simple=false;   // assignments only
            if(simple){
                const std::string &first=w[cmdIdx].text;
                static const char *kw[]={"if","for","while","until","case","{","!","exec",
                                         "eval","command","function"};
                for(const char *k : kw) if(first==k) simple=false;
                if(first.empty() || first[0]=='(' || first.find('=')!=std::string::npos ||
                   first.find('$')!=std::string::npos || find(first) || hasFunction(first) ||
                   (w.size()>cmdIdx+1 && w[cmdIdx+1].text=="()"))
                    simple=false;
            }
            _execDirect=simple;
        }
        int rc=parse(text);
        std::cout.flush();
        std::cerr.flush();
@@ -3577,6 +3610,14 @@ int fresh::Shell::runPipeline(const std::vector<std::vector<std::string> > &stag
    size_t n=stages.size();
    std::vector<int> pipefds((n>1) ? (n-1)*2 : 0);

    // see _execDirect: the one stage runs in this (background) child
    // itself -- runStageChild() sets up its redirections and execs/_exit()s
    if(n==1 && _execDirect && subshellBodies[0].empty()){
        std::cout.flush();
        std::cerr.flush();
        runStageChild(stages[0],redirs[0],subshellBodies[0],-1,-1,pipefds);
    }

    for(size_t i=0; i<pipefds.size()/2; ++i){
        if(pipe(&pipefds[i*2])!=0){
            std::cerr << "fresh: pipe: " << strerror(errno) << std::endl;
+4 −0
Changes for src/shell.h: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -717,6 +717,10 @@ namespace fresh {
        // ones reaped early (runningBackground()) keep their status here
        std::vector<pid_t> _bgJobs;
        std::map<pid_t,int> _bgFinished;
        // set only in a background child whose list is one simple external
        // command: that command replaces the child (exec, no fork), so $!
        // is the command's own pid and "kill $!" reaches it
        bool _execDirect=false;

        // name -> raw compound-command source text, see
        // collectFunctionDefinitions()'s own comment.