connection_type get_connection(const command& cmd) { connection_type connection; connection.retries = cmd.get_server_connect_retries_setting(); connection.timeout_seconds = cmd.get_server_connect_timeout_seconds_setting(); connection.server = cmd.get_server_url_setting(); connection.server_public_key = cmd.get_server_server_public_key_setting(); connection.client_private_key = cmd.get_server_client_private_key_setting(); return connection; }
caf::message run(const command& cmd, caf::actor_system& sys, caf::settings& options, command::argument_iterator first, command::argument_iterator last) { using caf::get_or; using caf::make_message; VAST_TRACE(VAST_ARG(std::string(cmd.name)), VAST_ARG("args", first, last), VAST_ARG(options)); // Parse arguments for this command. auto [state, position] = cmd.options.parse(options, first, last); bool has_subcommand; switch(state) { default: return make_error_msg(cmd, ec::unrecognized_option, *position); case caf::pec::success: has_subcommand = false; break; case caf::pec::not_an_option: has_subcommand = position != last; break; } if (position != last && detail::starts_with(*position, "-")) return make_error_msg(cmd, ec::unrecognized_option, *position); // Check for help option. if (get_or<bool>(options, "help", false) || (has_subcommand && *position == "help")) { helptext(cmd, std::cerr); return caf::none; } // Invoke cmd.run if no subcommand was defined. if (!has_subcommand) { // Commands without a run implementation require subcommands. if (cmd.run == nullptr) return make_error_msg(cmd, ec::missing_subcommand, ""); return cmd.run(cmd, sys, options, position, last); } // Consume CLI arguments if we have arguments but don't have subcommands. if (cmd.children.empty()) return cmd.run(cmd, sys, options, position, last); // Dispatch to subcommand. // TODO: We need to copy the iterator here, because structured binding cannot // be captured. Clang reports the error "reference to local binding // 'position' declared in enclosing function 'vast::command::run'" when // trying to use the structured binding inside the lambda. // See also: https://stackoverflow.com/questions/46114214. Remove this // workaround when all supported compilers accept accessing structured // bindings from lambdas. auto i = std::find_if(cmd.children.begin(), cmd.children.end(), [p = position](auto& x) { return x->name == *p; }); if (i == cmd.children.end()) return make_error_msg(cmd, ec::invalid_subcommand, *position); return run(**i, sys, options, position + 1, last); }
int show_log_helper( command::log_opt_t& result, const command& cmd, const show_commands_t& show_commands ) { constexpr auto option = store_info<S>::option; if ( cmd.is_set( option ) ) { result = boost::any_cast<std::shared_ptr<show_store_entry<S>>>( show_commands.at( option ) )->log(); } return 0; }
int show_helper( bool& result, command& cmd, const environment::ptr& env, show_commands_t& show_commands, const std::string& dotname ) { constexpr auto option = store_info<S>::option; if ( cmd.is_set( option ) ) { result = boost::any_cast<std::shared_ptr<show_store_entry<S>>>( show_commands[option] )->operator()( env->store<S>().current(), dotname, cmd ); } return 0; }
void store_write_io_type<circuit, io_quipper_tag_t>( const circuit& circ, const std::string& filename, const command& cmd ) { if ( cmd.is_set( "ascii" ) ) { write_quipper_ascii( circ, filename ); } else { write_quipper( circ, filename ); } }
void store_write_io_type<circuit, io_tikz_tag_t>( const circuit& circ, const std::string& filename, const command& cmd ) { create_tikz_settings ct_settings; ct_settings.draw_io = !cmd.is_set( "hideio" ); std::ofstream os( filename.c_str() ); if ( cmd.is_set( "standalone" ) ) { os << "\\documentclass{standalone}" << std::endl << "\\usepackage{tikz}" << std::endl << std::endl << "\\begin{document}" << std::endl; } create_image( os, circ, ct_settings ); if ( cmd.is_set( "standalone" ) ) { os << "\\end{document}" << std::endl; } }
circuit store_read_io_type<circuit, io_real_tag_t>( const std::string& filename, const command& cmd ) { if ( cmd.is_set( "string" ) ) { return circuit_from_string( cmd.vm["string"].as<std::string>() ); } else { circuit circ; read_realization( circ, filename ); return circ; } }
binary_truth_table store_read_io_type<binary_truth_table, io_spec_tag_t>( const std::string& filename, const command& cmd ) { if ( cmd.is_set( "permutation" ) ) { std::vector<unsigned> perm; parse_string_list( perm, cmd.vm["permutation"].as<std::string>() ); return permutation_to_truth_table( perm ); } else { binary_truth_table spec; read_specification( spec, filename ); return spec; } }
bool show_store_entry<rcbdd>::operator()( rcbdd& bdd, const std::string& dotname, const command& cmd ) { using namespace std::placeholders; auto * fd = fopen( dotname.c_str(), "w" ); if ( cmd.is_set( "add" ) ) { bdd.manager().DumpDot( std::vector<ADD>{bdd.chi().Add()}, 0, 0, fd ); } else { bdd.manager().DumpDot( std::vector<BDD>{bdd.chi()}, 0, 0, fd ); } fclose( fd ); return true; }
boost::optional<command::iterator> metashell::parse_pragma(const command& cmd_) { command::iterator i = skip_whitespace(cmd_.begin(), cmd_.end()); if ( i != cmd_.end() && ( i->type() == token_type::p_pragma || i->type() == token_type::operator_pound ) ) { i = skip_whitespace(skip(i), cmd_.end()); if ( i != cmd_.end() && i->type() == token_type::identifier && (i->value() == "metashell" || i->value() == "msh") ) { i = skip_whitespace(skip(i), cmd_.end()); if (i == cmd_.end() || i->value().empty()) { throw exception("The name of the metashell pragma is missing."); } else if (i->type() == token_type::identifier) { return i; } else { std::ostringstream s; s << "Invalid pragma name " << i->value(); throw exception(s.str()); } } } return boost::optional<command::iterator>(); }
connection_type get_connection(const command& cmd) { if (is_testnet(cmd.get_general_network_setting())) return connection_type { cmd.get_general_retries_setting(), period_ms(cmd.get_general_wait_setting()), cmd.get_testnet_cert_file_setting(), cmd.get_testnet_url_setting(), cmd.get_testnet_server_cert_key_setting() }; else return connection_type { cmd.get_general_retries_setting(), period_ms(cmd.get_general_wait_setting()), cmd.get_mainnet_cert_file_setting(), cmd.get_mainnet_url_setting(), cmd.get_mainnet_server_cert_key_setting() }; }
void command_completion_visitor::visit( const command& obj ) { // FIXME: push aliases too? alternatives.push_back( obj.get_name() ); }
int executor::runCom(command& thisCom,BuildIn& shellBuild) { int myPid[10]; int cpos=0,rpos=0; if(cpos>=thisCom.tokLen) return(0); char path[]="PATH=/bin:/usr/bin:./"; putenv(path); if(thisCom.toks[0]->cat==9){ if(thisCom.toks[0]->tok.compare("cd")==0){ shellBuild.handleCD(thisCom); }else if(thisCom.toks[0]->tok.compare("exit")==0){ shellBuild.handleExit(thisCom); }else if(thisCom.toks[0]->tok.compare("fg")==0){ shellBuild.handleFG(thisCom); }else if(thisCom.toks[0]->tok.compare("jobs")==0){ shellBuild.handleJobs(thisCom); }else{ cout << "Error: Invalid Build-In\n"; } }else if(thisCom.toks[0]->cat==8) { thisCom.getNextCom(cpos); inRed.checkRed(thisCom,cpos,thisCom.tokLen); outRed.checkRed(thisCom,cpos,thisCom.tokLen); inRed.checkRed(thisCom,cpos,thisCom.tokLen); outRed.checkRed(thisCom,cpos,thisCom.tokLen); thisCom.getAllRecur(cpos); outRed.checkRed(thisCom,cpos,thisCom.tokLen); pipef.creatPipe(thisCom.pipeLen-1); while(rpos<thisCom.pipeLen) { myPid[rpos]=fork(); if (myPid[rpos]==-1) { cout<<"Can not create new process\n"; exit(-1); } else if(myPid[rpos]==0) { signal(SIGINT,SIG_DFL); signal(SIGTSTP,SIG_DFL); if(debugmode) cout<<"This is child "<<rpos<<"\n"; pipef.setPipe(rpos); if(rpos==0) if(inRed.setInRed()!=0) exit(-1); if(rpos==thisCom.pipeLen-1) if(outRed.setOutRed()!=0) exit(-1); if(debugmode) cout<<"Child "<<rpos<<" "<<thisCom.pipeArg[rpos][0]<<" jump to new process\n"; if(execvp(thisCom.pipeArg[rpos][0],thisCom.pipeArg[rpos])==-1) { if(errno==ENOENT) { cerr<<thisCom.pipeArg[rpos][0]<<": command not found\n"; } else { cerr<<thisCom.pipeArg[rpos][0]<<": unknown error\n"; } } exit(-1); } else { if(debugmode) cout<<"This is father after "<<rpos<<" fork\n"; rpos++; } } if(debugmode) cout<<"Father return after wait\n"; string comName=""; for (int i=0; i<thisCom.tokLen; i++) { if (i!=0) comName+=" "; comName+=thisCom.toks[i]->tok; } for(int i=thisCom.pipeLen; i<10; i++) myPid[i]=-1; int fd[2],fd2[2]; if(thisCom.pipeLen<3) { fd2[0]=-1; fd2[1]=-1; } else { fd2[0]=pipef.pipefd[1][0]; fd2[1]=pipef.pipefd[1][1]; } if(thisCom.pipeLen<2) { fd[0]=-1; fd[1]=-1; } else { fd[0]=pipef.pipefd[0][0]; fd[1]=pipef.pipefd[0][1]; } shellBuild.waitingFor(comName,myPid,fd,fd2); inRed.reSet(); outRed.reSet(); } return 0; }
inline command::rule_t file_exists_if_set( const command& cmd, const std::string& filename, const std::string& argname ) { return { [&cmd, filename, argname]() { return !cmd.is_set( argname ) || boost::filesystem::exists( filename ); }, argname + " does not exist" }; }