📄 newcmd.cc
字号:
/* * Simulator of microcontrollers (cmd.src/newcmd.cc) * * Copyright (C) 1999,99 Drotos Daniel, Talker Bt. * * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu * *//* This file is part of microcontroller simulator: ucsim.UCSIM is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 2 of the License, or(at your option) any later version.UCSIM is distributed in the hope that it will be useful,but WITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong with UCSIM; see the file COPYING. If not, write to the FreeSoftware Foundation, 59 Temple Place - Suite 330, Boston, MA02111-1307, USA. *//*@1@*/#include "ddconfig.h"#include <stdio.h>#include <errno.h>#include <stdarg.h>#include <stdlib.h>#include <sys/types.h>#ifdef HAVE_SYS_SOCKET_H# include <sys/socket.h># include <netinet/in.h># include <arpa/inet.h># include <netdb.h>#endif#include <sys/time.h>#if FD_HEADER_OK# include HEADER_FD#endif#include <unistd.h>#include "i_string.h"// prj#include "globals.h"#include "utils.h"// sim#include "simcl.h"#include "argcl.h"#include "appcl.h"// local#include "newcmdcl.h"#include "cmdutil.h"extern "C" int vasprintf(char **strp, const char *format, va_list ap);extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);static intcmd_do_print(FILE *f, char *format, va_list ap){ int ret;#ifdef HAVE_VASPRINTF char *msg= NULL; vasprintf(&msg, format, ap); ret= fprintf(f, "%s", msg); free(msg);#else# ifdef HAVE_VSNPRINTF char msg[80*25]; vsnprintf(msg, 80*25, format, ap); ret= fprintf(f, "%s", msg);# else# ifdef HAVE_VPRINTF char msg[80*25]; vsprintf(msg, format, ap); /* Dangerous */ ret= fprintf(f, "%s", msg);# else# ifdef HAVE_DOPRNT /* ??? */ /*strcpy(msg, "Unimplemented printf has called.\n");*/# else /*strcpy(msg, "printf can not be implemented, upgrade your libc.\n");*/# endif# endif# endif#endif fflush(f); return(ret);}/* * Options of console */cl_prompt_option::cl_prompt_option(class cl_console *console): cl_optref(console){ con= console;}intcl_prompt_option::init(void){ char *help; help= format_string("Prompt string of console%d", con->get_id()); create(con, string_opt, "prompt", help); //fprintf(stderr," **new prompt option %p\"%s\", value=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval); free(help); default_option("prompt"); //fprintf(stderr,"opt=%p\"%s\" value after default set=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval); return(0);}voidcl_prompt_option::option_changed(void){ if (!con) return; char *s; option->get_value(&s); con->set_prompt(s);}cl_debug_option::cl_debug_option(class cl_console *console): cl_prompt_option(console){}intcl_debug_option::init(void){ char *help; help= format_string("Debug messages to console%d", con->get_id()); create(con, bool_opt, "debug", help); free(help); default_option("debug"); return(0);}voidcl_debug_option::option_changed(void){ if (!con) return; bool b; option->get_value(&b); if (b) con->flags|= CONS_DEBUG; else con->flags&= ~CONS_DEBUG;}/* * Command console *____________________________________________________________________________ */cl_console::cl_console(char *fin, char *fout, class cl_app *the_app): cl_base(){ FILE *f; last_command= NULL; app= the_app; in= 0; if (fin) if (f= fopen(fin, "r"), in= f, !f) fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno)); out= 0; if (fout) if (f= fopen(fout, "w"), out= f, !f) fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno)); prompt= 0; flags= CONS_NONE; if (in && isatty(fileno(in))) flags|= CONS_INTERACTIVE; else ;//fprintf(stderr, "Warning: non-interactive console\n"); id= 0;}cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app): cl_base(){ last_command= NULL; app= the_app; in = fin; out= fout; rout=(FILE *)0; prompt= 0; flags= CONS_NONE; if (in && isatty(fileno(in))) flags|= CONS_INTERACTIVE; else ;//fprintf(stderr, "Warning: non-interactive console\n"); id= 0;}/* * use the port number supplied to connect to localhost for * (by Sandeep) */#ifdef SOCKET_AVAILstatic intconnect_to_port(int portnum){ int sock= socket(AF_INET,SOCK_STREAM,0); struct sockaddr_in sin; sin.sin_family = AF_INET; sin.sin_port = htons(portnum); sin.sin_addr.s_addr = inet_addr("127.0.0.1"); if (connect(sock, (struct sockaddr *)&sin, sizeof(sin))) { fprintf(stderr, "Connect to port %d: %s\n", portnum, strerror(errno)); return -1; } return sock;}cl_console::cl_console(int portnumber, class cl_app *the_app){ int sock= connect_to_port(portnumber); last_command= NULL; app= the_app; if (!(in= fdopen(sock, "r"))) fprintf(stderr, "cannot open port for input\n"); if (!(out= fdopen(sock, "w"))) fprintf(stderr, "cannot open port for output\n"); //fprintf(stderr, "init socket done\n"); id= 0;}#endifclass cl_console *cl_console::clone_for_exec(char *fin){ FILE *fi= 0, *fo= 0; if (fin) if (fi= fopen(fin, "r"), !fi) { fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno)); return(0); } if ((fo= fdopen(dup(fileno(out)), "a")) == 0) { fclose(fi); fprintf(stderr, "Can't re-open output file: %s\n", strerror(errno)); return(0); } class cl_console *con= new cl_sub_console(this, fi, fo, app); return(con);}intcl_console::init(void){ cl_base::init(); prompt_option= new cl_prompt_option(this); prompt_option->init(); null_prompt_option= new cl_optref(this); null_prompt_option->init(); null_prompt_option->use("null_prompt"); debug_option= new cl_debug_option(this); debug_option->init(); welcome(); flags&= ~CONS_PROMPT; //print_prompt(); return(0);}cl_console::~cl_console(void){ if (in) fclose(in); un_redirect(); if (out) { if (flags & CONS_PROMPT) fprintf(out, "\n"); fflush(out); fclose(out); } delete prompt_option; delete null_prompt_option; delete debug_option;#ifdef SOCKET_AVAIL /* if (sock) { shutdown(sock, 2); close(sock); }*/#endif}/* * Output functions */voidcl_console::welcome(void){ FILE *Out= rout?rout:out; if (!Out || (flags & CONS_NOWELCOME)) return; fprintf(Out, "uCsim %s, Copyright (C) 1997 Daniel Drotos, Talker Bt.\n" "uCsim comes with ABSOLUTELY NO WARRANTY; for details type " "`show w'.\n" "This is free software, and you are welcome to redistribute it\n" "under certain conditions; type `show c' for details.\n", VERSIONSTR); fflush(Out);}voidcl_console::redirect(char *fname, char *mode){ if ((rout= fopen(fname, mode)) == NULL) dd_printf("Unable to open file '%s' for %s: %s\n", fname, (mode[0]=='w')?"write":"append", strerror(errno));}voidcl_console::un_redirect(void){ if (!rout) return; fclose(rout); rout= NULL;} voidcl_console::print_prompt(void){ //char *p; FILE *Out= rout?rout:out; if (flags & (CONS_PROMPT|CONS_FROZEN|CONS_INACTIVE)) return; flags|= CONS_PROMPT; if (!Out) return; if (/*app->args->arg_avail('P')*/null_prompt_option->get_value(bool(0))) putc('\0', Out); else { fprintf(Out, "%d", id); fprintf(Out, "%s", (prompt && prompt[0])?prompt:"> "); // ((p= app->args->get_sarg(0, "prompt"))?p:"> ")); } fflush(Out);}intcl_console::dd_printf(char *format, ...){ va_list ap; int ret= 0; FILE *Out= rout?rout:out; if (Out) { va_start(ap, format); ret= cmd_do_print(Out, format, ap); va_end(ap); } return(ret);}voidcl_console::print_bin(long data, int bits){ long mask= 1; FILE *Out= rout?rout:out; if (!Out) return; mask= mask << ((bits >= 1)?(bits-1):0); while (bits--) { fprintf(Out, "%c", (data&mask)?'1':'0'); mask>>= 1; }}voidcl_console::print_char_octal(char c){ FILE *Out= rout?rout:out; if (Out) ::print_char_octal(c, Out);}/* * Input functions */intcl_console::match(int fdnum){ if (in && fileno(in) == fdnum) return(1); return(0);}intcl_console::get_in_fd(void){ if (flags & CONS_INACTIVE) return(-2); return(in?fileno(in):-1);}intcl_console::input_avail(void){ struct timeval tv; int i; if ((i= get_in_fd()) < 0) return(0); fd_set s; FD_ZERO(&s); FD_SET(i, &s); tv.tv_sec= tv.tv_usec= 0; i= select(i+1, &s, NULL, NULL, &tv); return(i);}char *cl_console::read_line(void){ char *s= NULL;#ifdef HAVE_GETLINE if (getline(&s, 0, in) < 0) return(0);#else# ifdef HAVE_GETDELIM size_t n= 30; s= (char *)malloc(n); if (getdelim(&s, &n, '\n', in) < 0) { free(s); return(0); }# else# ifdef HAVE_FGETS s= (char *)malloc(300); if (fgets(s, 300, in) == NULL) { free(s); return(0); }# endif# endif#endif s[strlen(s)-1]= '\0'; if (s[strlen(s)-1] == '\r') s[strlen(s)-1]= '\0'; flags&= ~CONS_PROMPT; return(s);}intcl_console::proc_input(class cl_cmdset *cmdset){ int retval= 0; un_redirect(); if (feof(in)) { fprintf(out, "End\n"); return(1); } char *cmdstr= read_line(); if (!cmdstr) return(1); if (flags & CONS_FROZEN)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -