Execute and buffers command result to print it

@throws \InvalidArgumentException When the command couldn't be executed
962742
private function executeWithOutput()
    {
        if (($fp = popen($this->command, "r"))) {
            while (!feof($fp)) {
                echo fread($fp, 1024);
                flush();
            }

            fclose($fp);
        } else {
            throw new \InvalidArgumentException("Cannot execute command: '".$this->command."'");
        }
    }
1363397
public function execute()
    {
        $this->prepareExecute();

        // Show help or not if command has no logic
        if (!count($this->children) && $this->console instanceof AbstractConsole && $this->console->get('show_help')) {
            $this->io->out($this->console->describeCommand($this));

            return $this->postExecute(true);
        }

        if (count($this->children) && count($this->io->getArguments())) {
            $name = $this->io->getArgument(0);

            // Show help if a command also has logic
            if ($this->console instanceof AbstractConsole && $this->console->get('show_help')
                && !isset($this->children[$name])) {
                $this->io->out($this->console->describeCommand($this));

                return $this->postExecute(true);
            }

            try {
                return $this->executeSubCommand($name);
            } catch (CommandNotFoundException $e) {
                $e->getCommand()->renderAlternatives($e->getChild(), $e);

                return $e->getCode();
            } catch (WrongArgumentException $e) {
                $command = $this->getChild($name);

                throw new \RuntimeException(
                    $e->getMessage() . "\n\n[Usage] " . $command->getUsage(),
                    $e->getCode(),
                    $e
                );
            } catch (\Exception $e) {
                throw $e;
            }
        }

        if ($this->handler) {
            if ($this->handler instanceof \Closure) {
                $code = $this->handler;

                return $code($this);
            } elseif (is_callable($this->handler)) {
                return call_user_func($this->handler, $this);
            }
        }

        $result = $this->doExecute();

        return $this->postExecute($result);
    }
319180
public function output()
    {
        $command = $this->_getCommand();
        $content = $this->_exec($command, $this->_Pdf->html());

        if (!empty($content['stdout'])) {
            return $content['stdout'];
        }

        if (!empty($content['stderr'])) {
            throw new Exception(sprintf(
                'System error "%s" when executing command "%s". ' .
                'Try using the binary provided on http://wkhtmltopdf.org/downloads.html',
                $content['stderr'],
                $command
            ));
        }

        throw new Exception("WKHTMLTOPDF didn't return any data");
    }
235637
public function call($command, array $parameters = [], $outputBuffer = null)
    {
        if ($this->ajax() === true) {
            $this->lastOutput = $outputBuffer ?: new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true, new OutputFormatter(true));
            $this->setCatchExceptions(true);
        } else {
            $this->lastOutput = $outputBuffer ?: new BufferedOutput();
            $this->setCatchExceptions(false);
        }

        $command = $command.' '.implode(' ', $parameters);
        $input = new StringInput($command);
        $input->setInteractive(false);

        $result = $this->run($input, $this->lastOutput);

        $this->setCatchExceptions(true);

        return $result;
    }
89229
protected function buildCommand()
    {
        if ($this->error !== null) {
            $this->realCommand .= ' 2> ' . escapeshellarg($this->error->getPath());
            $this->log(
                'Writing error output to: ' . $this->error->getPath(),
                $this->logLevel
            );
        }

        if ($this->output !== null) {
            $this->realCommand .= ' 1> ' . escapeshellarg($this->output->getPath());
            $this->log(
                'Writing standard output to: ' . $this->output->getPath(),
                $this->logLevel
            );
        } elseif ($this->spawn) {
            $this->realCommand .= ' 1>/dev/null';
            $this->log('Sending output to /dev/null', $this->logLevel);
        }

        // If neither output nor error are being written to file
        // then we'll redirect error to stdout so that we can dump
        // it to screen below.

        if ($this->output === null && $this->error === null && $this->passthru === false) {
            $this->realCommand .= ' 2>&1';
        }

        // we ignore the spawn boolean for windows
        if ($this->spawn) {
            $this->realCommand .= ' &';
        }

        $this->realCommand = (string) $this->commandline . $this->realCommand;
    }
917597
protected function compileCommand()
    {
        $output = ProcessUtils::escapeArgument($this->output);

        $redirect = $this->shouldAppendOutput ? ' >> ' : ' > ';

        if (! $this->runInBackground) {
            return $this->command .$redirect .$output .' 2>&1';
        }

        $delimiter = windows_os() ? '&' : ';';

        $phpBinary = ProcessUtils::escapeArgument(
            with(new PhpExecutableFinder)->find(false)
        );

        $forgeBinary = defined('FORGE_BINARY') ? ProcessUtils::escapeArgument(FORGE_BINARY) : 'forge';

        $finished = $phpBinary .' ' .$forgeBinary .' schedule:finish ' . ProcessUtils::escapeArgument($this->mutexName());

        return '(' .$this->command .$redirect .$output .' 2>&1 ' .$delimiter .' ' .$finished .') > '
            . ProcessUtils::escapeArgument($this->getDefaultOutput()) .' 2>&1 &';
    }
200514
public function executeCommand($args = array())
    {
        $this->lastOutput = array();

        $command = call_user_func_array('sprintf', $args);
        $this->logger->logDebug($command);

        if ($this->quiet) {
            $this->logger->log('Executing: ' . $command);
        }

        $status = 0;
        $descriptorSpec = array(
            0 => array("pipe", "r"),  // stdin
            1 => array("pipe", "w"),  // stdout
            2 => array("pipe", "w"),  // stderr
        );

        $pipes = array();
        $process = proc_open($command, $descriptorSpec, $pipes, $this->buildPath, null);

        if (is_resource($process)) {
            fclose($pipes[0]);

            stream_set_blocking($pipes[1], 0);
            stream_set_blocking($pipes[2], 0);

            $lastOutput = '';
            $lastError  = '';
            do {
                usleep(200); //Give It Time To Start

                while (($newData = fread($pipes[1], 1024)) && !feof($pipes[1])) {
                    $lastOutput .= $newData;
                }

                while (($newData = fread($pipes[2], 1024)) && !feof($pipes[2])) {
                    $lastError .= $newData;
                }

                $processStatus = proc_get_status($process);
            } while ($processStatus['running']);

            $this->lastOutput = $lastOutput;
            $this->lastError  = $lastError;

            fclose($pipes[1]);
            fclose($pipes[2]);

            proc_close($process);
            $status = $processStatus['exitcode'];
        }

        $this->lastOutput = array_filter(explode(PHP_EOL, $this->lastOutput));

        $shouldOutput = ($this->logExecOutput && ($this->verbose || $status != 0));

        if ($shouldOutput && !empty($this->lastOutput)) {
            $this->logger->log($this->lastOutput);
        }

        if (!empty($this->lastError)) {
            $this->logger->log("\033[0;31m" . $this->lastError . "\033[0m", LogLevel::ERROR);
        }

        $rtn = false;

        if ($status == 0) {
            $rtn = true;
        }

        return $rtn;
    }
951710
public function execute()
    {
        if (count($this->_filecontent)>0) {
            foreach ($this->prog_items as $item) if (isset($this->_filecontent[$item])) {
                if ($item !== 'idrac') {
                    if (!is_null($buffer = $this->_filecontent[$item]) && (($buffer = trim($buffer)) != "")) {
                        switch ($item) {
                            case 'mdstat':
                                $this->execute_mdstat($buffer);
                                break;
                            case 'dmraid':
                                $this->execute_dmraid($buffer);
                                break;
                            case 'megactl':
                                $this->execute_megactl($buffer, false);
                                break;
                            case 'megasasctl':
                                $this->execute_megactl($buffer, true);
                                break;
                            case 'graid':
                                $this->execute_graid($buffer);
                                break;
                            case 'zpool':
                                $this->execute_zpool($buffer);
                                break;
                        }
                    }
                } else {
                    if (is_array($this->_filecontent[$item])) {
                        foreach ($this->_filecontent[$item] as $device=>$buffer) if (($buffer = trim($buffer)) != "") {
                            $this->execute_idrac($buffer, /*'idrac-'.*/$device);
                        }
                    }
                }
            }
        }
    }
319094
protected function write()
    {
        if (!isset($this->pipes[0])) {
            return;
        }
        $input = $this->input;

        if ($input instanceof \Iterator) {
            if (!$input->valid()) {
                $input = null;
            } elseif (is_resource($input = $input->current())) {
                stream_set_blocking($input, 0);
            } elseif (!isset($this->inputBuffer[0])) {
                if (!is_string($input)) {
                    if (!is_scalar($input)) {
                        throw new InvalidArgumentException(sprintf('%s yielded a value of type "%s", but only scalars and stream resources are supported', get_class($this->input), gettype($input)));
                    }
                    $input = (string) $input;
                }
                $this->inputBuffer = $input;
                $this->input->next();
                $input = null;
            } else {
                $input = null;
            }
        }

        $r = $e = array();
        $w = array($this->pipes[0]);

        // let's have a look if something changed in streams
        if (false === $n = @stream_select($r, $w, $e, 0, 0)) {
            return;
        }

        foreach ($w as $stdin) {
            if (isset($this->inputBuffer[0])) {
                $written = fwrite($stdin, $this->inputBuffer);
                $this->inputBuffer = substr($this->inputBuffer, $written);
                if (isset($this->inputBuffer[0])) {
                    return array($this->pipes[0]);
                }
            }

            if ($input) {
                for (;;) {
                    $data = fread($input, self::CHUNK_SIZE);
                    if (!isset($data[0])) {
                        break;
                    }
                    $written = fwrite($stdin, $data);
                    $data = substr($data, $written);
                    if (isset($data[0])) {
                        $this->inputBuffer = $data;

                        return array($this->pipes[0]);
                    }
                }
                if (feof($input)) {
                    if ($this->input instanceof \Iterator) {
                        $this->input->next();
                    } else {
                        $this->input = null;
                    }
                }
            }
        }

        // no input to read on resource, buffer is empty
        if (!isset($this->inputBuffer[0]) && !($this->input instanceof \Iterator ? $this->input->valid() : $this->input)) {
            $this->input = null;
            fclose($this->pipes[0]);
            unset($this->pipes[0]);
        } elseif (!$w) {
            return array($this->pipes[0]);
        }
    }
129371
protected function execute($command)
    {
        $status = null;
        $result = array();
        exec($command, $result, $status);
        return !$status ? $result : null;
    }
556958
function printOut(){
		for($i=0;$i<sizeof($this->_Buffer);$i++){
			$this->_Buffer[$i]->flush();
		}
	}
35265
public function run(Command $command, OutputFormatter $formatter = null) : Result
    {
        $cmd = $this->processor->run($command->getCommand(), $command->getAcceptableExitCodes());

        if (!$cmd->isSuccessful()) {
            throw new RuntimeException(
                'Command failed:' . PHP_EOL
                . '  exit-code: ' . $cmd->getCode() . PHP_EOL
                . '  message:   ' . $cmd->getStdErr() . PHP_EOL,
                $cmd->getCode()
            );
        }

        $formatted = $formatter !== null ? $formatter->format($cmd->getStdOutAsArray()) : [];
        $result    = new Result($cmd, $formatted);

        return $result;
    }
883378
public function render()
    {
        if ($this->state < self::STATE_INNER_CONTENT) {
            throw new InvalidStateMethodCallException;
        }

        if ($this->state == self::STATE_RESPONSE) {
            return;
        }

        // Deliver the result of the page callback to the browser, or if requested,
        // return it raw, so calling code can do more processing.
        $content = $this->encapsulate(
            function (JoomlaInterface $joomla) {
                $application = $joomla->getApplication();
                $application->execute();
            },
            $this
        );

        $this->response->setContent($content);

        $this->state = self::STATE_RESPONSE;
    }
1365839
protected function execute($command)
    {
        $process = new Process($command);
        $process->setTimeout(null);
        $process->run();

        if (!$process->isSuccessful()) {
            throw new \RuntimeException($process->getErrorOutput());
        }
    }
618911
protected function processCommand($command = '', $argument = '')
    {
        switch ($command) {
            case 'help':
                $this->writeln('
Debug console for phpDaemon.
Allowed commands:

help	eval	exit
');
                break;
            case 'ping':
                $this->writeln('pong');
                break;
            case 'eval':
                ob_start();
                eval($argument);
                $out = ob_get_contents();
                ob_end_clean();
                $this->writeln($out);
                break;
            default:
                $this->writeln('Unknown command "' . $command . '".
Type "help" to get the list of allowed commands.');
        }
    }
332856
public function execute()
    {
        $this->unixParams();
        $output = [];
        $return_var = 0;

        exec($this->the_command, $output, $return_var);

        if ($return_var !== 0) {
            throw new Exception(
                'Your report has an error and couldn\'t be processed! Try to output the command: '.
                escapeshellcmd($this->the_command),
                1
            );
        }

        return $output;
    }
786464
public function execute(...$commandNames)
    {
        $logger = $this->logger;
        $app = $this->getApplication();
        $progname = basename($app->getProgramName());

        // if there is no subcommand to render help, show all available commands.
        $count = count($commandNames);

        if ($count) {
            $cmd = $app;
            for ($i = 0; $cmd && $i < $count; ++$i) {
                $cmd = $cmd->getCommand($commandNames[$i]);
            }

            if (!$cmd) {
                throw new \Exception('Command entry ' . implode(' ', $commandNames) . ' not found');
            }

            if ($brief = $cmd->brief()) {
                $logger->writeln('NAME', 'yellow');
                $logger->writeln("\t<strong_white>" . $cmd->name() . '</strong_white> - ' . $brief);
                $logger->newline();
            }

            if ($aliases = $cmd->aliases()) {
                $logger->writeln('ALIASES', 'yellow');
                $logger->writeln("\t<strong_white>" . implode(', ', $aliases) . '</strong_white>');
                $logger->newline();
            }

            $this->printUsage($cmd);

            $logger->writeln('SYNOPSIS', 'yellow');
            $prototypes = $cmd->getAllCommandPrototype();
            foreach ($prototypes as $prototype) {
                $logger->writeln("\t" . ' ' . $prototype);
            }
            $logger->newline();

            $this->printOptions($cmd);
            $this->printCommand($cmd);
            $this->printHelp($cmd);
        } else {
            // print application
            $cmd = $this->parent;
            $logger->writeln(ucfirst($cmd->brief()), 'strong_white');
            $logger->newline();

            $this->printUsage($cmd);

            $logger->writeln('SYNOPSIS', 'yellow');
            $logger->write("\t" . $progname);
            if (!empty($cmd->getOptionCollection()->options)) {
                $logger->write(' [options]');
            }

            if ($cmd->hasCommands()) {
                $logger->write(' <command>');
            } else {
                foreach ($cmd->getArguments() as $argument) {
                    $logger->write(' <' . $argument->name() . '>');
                }
            }

            $logger->newline();
            $logger->newline();

            $this->printOptions($cmd);
            $this->printCommand($cmd);
            $this->printHelp($cmd);
        }
    }