#include "shell.hpp" void shell::c_run (std::string args) { // sanity checks if ( args.empty() ) { carp ("format: run [args] ..."); return; } std::string command = args; bool bg = false; // check if we run in backround if ( command.rfind ("&") + 1 == command.length () ) { bg = true; command.erase (command.length () - 2); } // create io streams for job io saga::job::ostream in; saga::job::istream out; saga::job::istream err; saga::job::job job; try { job = js_.run_job (host_, command, in, out, err); std::cerr << "got here" << std::endl; } catch (std::exception const & e) { std::cout << "run failed: " << e.what() << std::endl; return; } // get job state saga::job::state state = job.get_state (); // check if that worked if ( state != saga::job::Running && state != saga::job::Done ) { carp ("run failed: " + command); return; } if ( bg ) { // store background jobs in process table std::string jobid (job.get_job_id()); int pid = jobs_.add (jobid, command, job); // output std::cout << " [" << pid << "] " << command << std::endl; std::cout << " " << jobid << std::endl; } else { while ( true ) { char buffer[255]; // get stdout out.read (buffer, sizeof (buffer)); err.read (buffer, sizeof (buffer)); if ( out.gcount () > 0 ) { std::cout << std::string (buffer, out.gcount ()); } if ( err.gcount () > 0 ) { std::cout << std::string (buffer, err.gcount ()); } if ( out.fail () && err.fail () ) { break; } } std::cout << std::endl << std::flush; } return; }