📄 newcmd.cc
字号:
pars->yyparse(); delete cmdline; delete pars; } /*if (!cm) retval= interpret(cmdstr);*/ } } //retval= sim->do_cmd(cmd, this); un_redirect(); /*if (!retval) print_prompt();*/ free(cmdstr); return(retval);}/* * Old version, sim->do_cmd() falls into this if it doesn't find a new * command object which can handle entered command */intcl_console::interpret(char *cmd){ FILE *Out= rout?rout:out; fprintf(Out, "Unknown command\n"); return(0);}voidcl_console::set_id(int new_id){ char *s; id= new_id; set_name(s= format_string("console%d", id)); free(s);}voidcl_console::set_prompt(char *p){ if (prompt) free(prompt); if (p && *p) prompt= strdup(p); else prompt= 0;}/* * This console listen on a socket and can accept connection requests */#ifdef SOCKET_AVAILcl_listen_console::cl_listen_console(int serverport, class cl_app *the_app){ app= the_app; if ((sock= make_server_socket(serverport)) >= 0) { if (listen(sock, 10) < 0) fprintf(stderr, "Listen on port %d: %s\n", serverport, strerror(errno)); } in= out= 0;}intcl_listen_console::match(int fdnum){ return(sock == fdnum);}intcl_listen_console::get_in_fd(void){ return(sock);}intcl_listen_console::proc_input(class cl_cmdset *cmdset){ int newsock; ACCEPT_SOCKLEN_T size; struct sockaddr_in sock_addr; class cl_commander *cmd; cmd= app->get_commander(); size= sizeof(struct sockaddr); newsock= accept(sock, (struct sockaddr*)&sock_addr, &size); if (newsock < 0) { perror("accept"); return(0); } if (!(in= fdopen(newsock, "r+"))) fprintf(stderr, "cannot open port for input\n"); if (!(out= fdopen(newsock, "w+"))) fprintf(stderr, "cannot open port for output\n"); class cl_console *c= cmd->mk_console(in, out); c->flags|= CONS_INTERACTIVE; cmd->add_console(c); in= out= 0; return(0);}#endif /* SOCKET_AVAIL *//* * Sub-console */cl_sub_console::cl_sub_console(class cl_console *the_parent, FILE *fin, FILE *fout, class cl_app *the_app): cl_console(fin, fout, the_app){ parent= the_parent;}cl_sub_console::~cl_sub_console(void){ class cl_commander *c= app->get_commander(); if (parent && c) { c->activate_console(parent); }}intcl_sub_console::init(void){ class cl_commander *c= app->get_commander(); if (parent && c) { c->deactivate_console(parent); } cl_console::init(); flags|= CONS_ECHO; return(0);}/* * Command interpreter *____________________________________________________________________________ */cl_commander::cl_commander(class cl_app *the_app, class cl_cmdset *acmdset /*, class cl_sim *asim*/): cl_base(){ app= the_app; cons= new cl_list(1, 1, "consoles"); actual_console= frozen_console= 0; cmdset= acmdset;}intcl_commander::init(void){ class cl_optref console_on_option(this); class cl_optref config_file_option(this); class cl_optref port_number_option(this); class cl_console *con; console_on_option.init(); console_on_option.use("console_on"); config_file_option.init(); config_file_option.use("config_file"); port_number_option.init(); cl_base::init(); set_name("Commander"); bool need_config= DD_TRUE;#ifdef SOCKET_AVAIL if (port_number_option.use("port_number")) add_console(mk_console(port_number_option.get_value((long)0)));#endif /* The following code is commented out because it produces gcc warnings * newcmd.cc: In member function `virtual int cl_commander::init()': * newcmd.cc:785: warning: 'Config' might be used uninitialized in this function * newcmd.cc:786: warning: 'cn' might be used uninitialized in this function */ /* char *Config= config_file_option.get_value(Config); char *cn= console_on_option.get_value(cn); */ /* Here shoud probably be something else, but is still better then the former code... */ char *Config= config_file_option.get_value(""); char *cn= console_on_option.get_value(""); if (cn) { add_console(con= mk_console(cn, cn)); exec_on(con, Config); need_config= DD_FALSE; } if (cons->get_count() == 0) { add_console(con= mk_console(stdin, stdout)); exec_on(con, Config); need_config= DD_FALSE; } if (need_config && Config && *Config) { FILE *fc= fopen(Config, "r"); if (!fc) fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno)); else { con= mk_console(fc, stderr); con->flags|= CONS_NOWELCOME|CONS_ECHO; add_console(con); } } return(0);}cl_commander::~cl_commander(void){ delete cons; delete cmdset;}class cl_console *cl_commander::mk_console(char *fin, char *fout){ return(new cl_console(fin, fout, app));}class cl_console *cl_commander::mk_console(FILE *fin, FILE *fout){ return(new cl_console(fin, fout, app));}#ifdef SOCKET_AVAILclass cl_console *cl_commander::mk_console(int portnumber){ return(new cl_listen_console(portnumber, app));}#endifvoidcl_commander::add_console(class cl_console *console){ if (!console) return; int i=cons->add(console); console->set_id(i); console->init(); set_fd_set();}voidcl_commander::del_console(class cl_console *console){ cons->disconn(console); set_fd_set();}voidcl_commander::activate_console(class cl_console *console){ console->flags&= ~CONS_INACTIVE; //console->print_prompt(); set_fd_set();}voidcl_commander::deactivate_console(class cl_console *console){ console->flags|= CONS_INACTIVE; set_fd_set();}voidcl_commander::set_fd_set(void){ int i; //fprintf(stderr, "** Setting fd set\n"); FD_ZERO(&read_set); fd_num= 0; for (i= 0; i < cons->count; i++) { int fd; class cl_console *c= (class cl_console*)(cons->at(i)); if ((fd= c->get_in_fd()) >= 0) { if ((c->flags & CONS_FROZEN) == 0 || (c->flags & CONS_INTERACTIVE) != 0) { FD_SET(fd, &read_set); if (fd > fd_num) fd_num= fd; } } else ;//fprintf(stderr, "** Skipping console %p\n",c); } fd_num++;}/* * Printing to all consoles */intcl_commander::all_printf(char *format, ...){ va_list ap; int i, ret= 0; for (i= 0; i < cons->count; i++) { class cl_console *c= (class cl_console*)(cons->at(i)); FILE *Out= c->get_out(); if (Out) { va_start(ap, format); ret= c->cmd_do_print(Out, format, ap); va_end(ap); } } return(ret);}voidcl_commander::prompt(void){ int i; for (i= 0; i < cons->count; i++) { class cl_console *c= (class cl_console*)(cons->at(i)); c->print_prompt(); }}intcl_commander::all_print(char *string, int length){ int i; for (i= 0; i < cons->count; i++) { class cl_console *c= (class cl_console*)(cons->at(i)); FILE *Out= c->get_out(); if (Out) { for (int j= 0; j < length; j++) putc(string[j], Out); } } return(0);}/* * Printing to actual_console */FILE *cl_commander::get_out(void){ if (actual_console) return(actual_console->get_out()); else if (frozen_console) return(frozen_console->get_out()); return(0);}intcl_commander::dd_printf(char *format, ...){ va_list ap; int ret= 0; FILE *f; class cl_console *con; if (actual_console) { f= actual_console->get_out(); con= actual_console; } else if (frozen_console) { f= frozen_console->get_out(); con= frozen_console; } else { f= 0; con= 0; } if (/*actual_console && actual_console->out*/f && con) { va_start(ap, format); ret= con->cmd_do_print(f/*actual_console->out*/, format, ap); va_end(ap); } return(ret);}intcl_commander::dd_printf(char *format, va_list ap){ int ret= 0; FILE *f; class cl_console *con; if (actual_console) { f= actual_console->get_out(); con= actual_console; } else if (frozen_console) { f= frozen_console->get_out(); con= frozen_console; } else { f= 0; con= 0; } if (/*actual_console && actual_console->out*/f && con) { ret= con->cmd_do_print(f/*actual_console->out*/, format, ap); } return(ret);}/* * Printing to consoles which have CONS_DEBUG flag set */intcl_commander::debug(char *format, ...){ va_list ap; int i, ret= 0; for (i= 0; i < cons->count; i++) { class cl_console *c= (class cl_console*)(cons->at(i)); FILE *Out= c->get_out(); if (Out && c->flags & CONS_DEBUG) { va_start(ap, format); ret= c->cmd_do_print(Out, format, ap); va_end(ap); } } return(ret);}intcl_commander::debug(char *format, va_list ap){ int i, ret= 0; for (i= 0; i < cons->count; i++) { class cl_console *c= (class cl_console*)(cons->at(i)); FILE *Out= c->get_out(); if (Out && c->flags & CONS_DEBUG) { ret= c->cmd_do_print(Out, format, ap); } } return(ret);}intcl_commander::flag_printf(int iflags, char *format, ...){ va_list ap; int i, ret= 0; for (i= 0; i < cons->count; i++) { class cl_console *c= (class cl_console*)(cons->at(i)); FILE *Out= c->get_out(); if (Out && (c->flags & iflags) == iflags) { va_start(ap, format); ret= c->cmd_do_print(Out, format, ap); va_end(ap); } } return(ret);}intcl_commander::input_avail(void){ struct timeval tv; int i; tv.tv_sec= tv.tv_usec= 0; active_set= read_set; i= select(fd_num, &active_set, NULL, NULL, &tv); return(i);}intcl_commander::input_avail_on_frozen(void){ int fd; if (!frozen_console) return(0); if ((fd= frozen_console->get_in_fd()) >= 0 && !isatty(fd)) return(0); return(frozen_console->input_avail());}intcl_commander::wait_input(void){ int i; active_set= read_set; prompt(); i= select(fd_num, &active_set, NULL, NULL, NULL); return(i);}intcl_commander::proc_input(void){ int i; for (i= 0; i < fd_num; i++) if (FD_ISSET(i, &active_set)) { class cl_console *c; int j; for (j= 0; j < cons->count; j++) { c= (class cl_console*)(cons->at(j)); if (c->match(i)) { actual_console= c; int retval= c->proc_input(cmdset); if (retval) { del_console(c); delete c; } actual_console= 0; return(cons->count == 0); } } } return(0);}voidcl_commander::exec_on(class cl_console *cons, char *file_name){ FILE *fi= fopen(file_name, "r"); if (!cons || !fi) return; class cl_console *subcon= cons->clone_for_exec(file_name); subcon->flags|= CONS_NOWELCOME; add_console(subcon);}/* End of cmd.src/newcmd.cc */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -