X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=sim%2Fucsim%2Fcmd.src%2Fnewcmd.cc;h=4717409eaf72fdb037af51559a84c086b1da680f;hb=c1eaba671c4e92256210763db2c0bd062deace1d;hp=6de180b1b6f8b8a0a5a693d07c670e6aaaeb4059;hpb=0e1bba0730e55b3d40c1c644d94befc99c87270d;p=fw%2Fsdcc diff --git a/sim/ucsim/cmd.src/newcmd.cc b/sim/ucsim/cmd.src/newcmd.cc index 6de180b1..4717409e 100644 --- a/sim/ucsim/cmd.src/newcmd.cc +++ b/sim/ucsim/cmd.src/newcmd.cc @@ -32,19 +32,26 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include #include #include -#ifdef HAVE_SYS_SOCKET_H -# include -# include -# include -# include -#endif #include +#ifdef SOCKET_AVAIL +# include HEADER_SOCKET +# if defined HAVE_SYS_SOCKET_H +# include +# include +# include +# endif +#endif #if FD_HEADER_OK # include HEADER_FD #endif -#include +#ifdef HAVE_UNISTD_H +# include +#endif #include "i_string.h" +#include "cmdlexcl.h" +#include "cmdpars.h" + // prj #include "globals.h" #include "utils.h" @@ -59,43 +66,6 @@ Software Foundation, 59 Temple Place - Suite 330, Boston, MA #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 int -cmd_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 */ @@ -169,7 +139,6 @@ cl_console::cl_console(char *fin, char *fout, class cl_app *the_app): { FILE *f; - last_command= NULL; app= the_app; in= 0; if (fin) @@ -186,13 +155,14 @@ cl_console::cl_console(char *fin, char *fout, class cl_app *the_app): flags|= CONS_INTERACTIVE; else ;//fprintf(stderr, "Warning: non-interactive console\n"); + rout= 0; id= 0; + lines_printed= new cl_ustrings(100, 100, "console_cache"); } 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; @@ -203,7 +173,9 @@ cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app): flags|= CONS_INTERACTIVE; else ;//fprintf(stderr, "Warning: non-interactive console\n"); + rout= 0; id= 0; + lines_printed= new cl_ustrings(100, 100, "console_cache"); } /* @@ -233,7 +205,6 @@ 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"); @@ -241,6 +212,7 @@ cl_console::cl_console(int portnumber, class cl_app *the_app) fprintf(stderr, "cannot open port for output\n"); //fprintf(stderr, "init socket done\n"); id= 0; + lines_printed= new cl_ustrings(1, 1, "console_cache"); } #endif @@ -249,12 +221,13 @@ 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 (!fin) + return(0); + 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); @@ -279,6 +252,8 @@ cl_console::init(void) welcome(); flags&= ~CONS_PROMPT; //print_prompt(); + last_command= 0; + last_cmdline= 0; return(0); } @@ -290,7 +265,7 @@ cl_console::~cl_console(void) if (out) { if (flags & CONS_PROMPT) - fprintf(out, "\n"); + fprintf(out, "\n"); fflush(out); fclose(out); } @@ -307,6 +282,17 @@ cl_console::~cl_console(void) } +bool +cl_console::accept_last(void) +{ + if (!in) + return(DD_FALSE); + if (isatty(fileno(in))) + return(DD_TRUE); + return(DD_FALSE); +} + + /* * Output functions */ @@ -314,18 +300,15 @@ cl_console::~cl_console(void) void cl_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); + if (!(flags & CONS_NOWELCOME)) + { + dd_printf("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); + } } void @@ -333,7 +316,7 @@ cl_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)); + fname, (mode[0]=='w')?"write":"append", strerror(errno)); } void @@ -344,27 +327,45 @@ cl_console::un_redirect(void) fclose(rout); rout= NULL; } - + + +int +cl_console::cmd_do_print(char *format, va_list ap) +{ + int ret; + FILE *f = get_out(); + + if (f) + { + ret= vfprintf(f, format, ap); + fflush(f); + } + else + ret= 0; + + return(ret); +} + void cl_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); + { + FILE *Out = get_out(); + if (Out) + { + putc('\0', Out); + fflush(Out); + } + } else { - fprintf(Out, "%d", id); - fprintf(Out, "%s", (prompt && prompt[0])?prompt:"> "); - // ((p= app->args->get_sarg(0, "prompt"))?p:"> ")); + dd_printf("%d%s", id, (prompt && prompt[0])?prompt:"> "); + // ((p= app->args->get_sarg(0, "prompt"))?p:"> ")); } - fflush(Out); } int @@ -372,14 +373,27 @@ cl_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); - } + va_start(ap, format); + ret= cmd_do_print(format, ap); + va_end(ap); + + return(ret); +} + +int +cl_console::debug(char *format, ...) +{ + if ((flags & CONS_DEBUG) == 0) + return(0); + + va_list ap; + int ret= 0; + + va_start(ap, format); + ret= cmd_do_print(format, ap); + va_end(ap); + return(ret); } @@ -387,14 +401,11 @@ void cl_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'); + dd_printf("%c", (data&mask)?'1':'0'); mask>>= 1; } } @@ -402,7 +413,7 @@ cl_console::print_bin(long data, int bits) void cl_console::print_char_octal(char c) { - FILE *Out= rout?rout:out; + FILE *Out= get_out(); if (Out) ::print_char_octal(c, Out); @@ -413,15 +424,6 @@ cl_console::print_char_octal(char c) * Input functions */ -int -cl_console::match(int fdnum) -{ - if (in && - fileno(in) == fdnum) - return(1); - return(0); -} - int cl_console::get_in_fd(void) { @@ -434,7 +436,7 @@ int cl_console::input_avail(void) { struct timeval tv; - int i; + UCSOCKET_T i; if ((i= get_in_fd()) < 0) return(0); @@ -454,9 +456,7 @@ cl_console::read_line(void) #ifdef HAVE_GETLINE if (getline(&s, 0, in) < 0) return(0); -#else - -# ifdef HAVE_GETDELIM +#elif defined HAVE_GETDELIM size_t n= 30; s= (char *)malloc(n); if (getdelim(&s, &n, '\n', in) < 0) @@ -464,17 +464,13 @@ cl_console::read_line(void) free(s); return(0); } -# else - -# ifdef HAVE_FGETS +#elif defined 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') @@ -506,23 +502,58 @@ cl_console::proc_input(class cl_cmdset *cmdset) else { if (cmdstr && - *cmdstr == '\004') - retval= 1; + *cmdstr == '\004') + retval= 1; else - { - class cl_cmdline *cmdline; - class cl_cmd *cm; - if (flags & CONS_ECHO) - dd_printf("%s\n", cmdstr); - cmdline= new cl_cmdline(app, cmdstr, this); - cmdline->init(); - cm= cmdset->get_cmd(cmdline); - if (cm) - retval= cm->work(app, cmdline, this); - delete cmdline; - if (!cm) - retval= interpret(cmdstr); - } + { + class cl_cmdline *cmdline= 0; + class cl_cmd *cm= 0; + if (flags & CONS_ECHO) + dd_printf("%s\n", cmdstr); + cmdline= new cl_cmdline(app, cmdstr, this); + cmdline->init(); + if (cmdline->repeat() && + accept_last() && + last_command) + { + cm= last_command; + delete cmdline; + cmdline= last_cmdline; + } + else + { + cm= cmdset->get_cmd(cmdline, accept_last()); + if (last_cmdline) + { + delete last_cmdline; + last_cmdline= 0; + } + last_command= 0; + } + if (cm) + { + retval= cm->work(app, cmdline, this); + if (cm->can_repeat) + { + last_command= cm; + last_cmdline= cmdline; + } + else + delete cmdline; + } + else + { + class YY_cl_ucsim_parser_CLASS *pars; + class cl_ucsim_lexer *lexer; + lexer= new cl_ucsim_lexer(cmdstr); + pars= new YY_cl_ucsim_parser_CLASS(lexer); + pars->yyparse(); + delete cmdline; + delete pars; + } + /*if (!cm) + retval= interpret(cmdstr);*/ + } } //retval= sim->do_cmd(cmd, this); un_redirect(); @@ -540,9 +571,7 @@ cl_console::proc_input(class cl_cmdset *cmdset) int cl_console::interpret(char *cmd) { - FILE *Out= rout?rout:out; - - fprintf(Out, "Unknown command\n"); + dd_printf("Unknown command\n"); return(0); } @@ -576,23 +605,16 @@ cl_console::set_prompt(char *p) cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app) { - last_command= NULL; 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)); + fprintf(stderr, "Listen on port %d: %s\n", + serverport, strerror(errno)); } in= out= 0; } -int -cl_listen_console::match(int fdnum) -{ - return(sock == fdnum); -} - int cl_listen_console::get_in_fd(void) { @@ -634,7 +656,7 @@ cl_listen_console::proc_input(class cl_cmdset *cmdset) */ cl_sub_console::cl_sub_console(class cl_console *the_parent, - FILE *fin, FILE *fout, class cl_app *the_app): + FILE *fin, FILE *fout, class cl_app *the_app): cl_console(fin, fout, the_app) { parent= the_parent; @@ -671,11 +693,11 @@ cl_sub_console::init(void) */ cl_commander::cl_commander(class cl_app *the_app, class cl_cmdset *acmdset - /*, class cl_sim *asim*/): + /*, class cl_sim *asim*/): cl_base() { app= the_app; - cons= new cl_list(1, 1); + cons= new cl_list(1, 1, "consoles"); actual_console= frozen_console= 0; cmdset= acmdset; } @@ -684,34 +706,65 @@ int cl_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"); - char *Config= app->args->get_sarg(0, "Config"); - if (Config) - { - class cl_console *con= mk_console(0/*"/dev/tty"*/); - add_console(con); - } + bool need_config= DD_TRUE; - //if (app->args->arg_avail('c')) - { - char *cn= console_on_option.get_value(cn); - if (cn) - add_console(mk_console(cn, cn)); - } #ifdef SOCKET_AVAIL - if (app->args->arg_avail('Z')) - add_console(mk_console(app->args->get_iarg(0, "Zport"))); - if (app->args->arg_avail('r')) - add_console(mk_console(app->args->get_iarg('r', 0))); + 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(mk_console(stdin, stdout)); + { + 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); } @@ -784,20 +837,20 @@ cl_commander::set_fd_set(void) fd_num= 0; for (i= 0; i < cons->count; i++) { - int fd; + UCSOCKET_T 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; - } - } + { + 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); + ;//fprintf(stderr, "** Skipping console %p\n",c); } fd_num++; } @@ -816,13 +869,10 @@ cl_commander::all_printf(char *format, ...) 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= cmd_do_print(Out, format, ap); - va_end(ap); - } + + va_start(ap, format); + ret= c->cmd_do_print(format, ap); + va_end(ap); } return(ret); } @@ -839,51 +889,48 @@ cl_commander::prompt(void) } } -int -cl_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 */ int -cl_commander::dd_printf(char *format, ...) +cl_commander::dd_printf(char *format, va_list ap) { - 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; - if (/*actual_console && - actual_console->out*/f) { - va_start(ap, format); - ret= cmd_do_print(f/*actual_console->out*/, format, ap); - va_end(ap); + con= 0; + } + if (con) + { + ret= con->cmd_do_print(format, ap); } return(ret); } +int +cl_commander::dd_printf(char *format, ...) +{ + va_list ap; + int ret= 0; + + va_start(ap, format); + ret= dd_printf(format, ap); + va_end(ap); + + return(ret); +} + /* * Printing to consoles which have CONS_DEBUG flag set */ @@ -897,14 +944,28 @@ cl_commander::debug(char *format, ...) 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= cmd_do_print(Out, format, ap); - va_end(ap); - } + if (c->flags & CONS_DEBUG) + { + va_start(ap, format); + ret= c->cmd_do_print(format, ap); + va_end(ap); + } + } + return(ret); +} + +int +cl_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)); + if (c->flags & CONS_DEBUG) + { + ret= c->cmd_do_print(format, ap); + } } return(ret); } @@ -918,14 +979,12 @@ cl_commander::flag_printf(int iflags, char *format, ...) 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= cmd_do_print(Out, format, ap); - va_end(ap); - } + if ((c->flags & iflags) == iflags) + { + va_start(ap, format); + ret= c->cmd_do_print(format, ap); + va_end(ap); + } } return(ret); } @@ -969,31 +1028,38 @@ cl_commander::wait_input(void) int cl_commander::proc_input(void) { - int i; + for (int j = 0; j < cons->count; j++) + { + class cl_console *c = (class cl_console*)(cons->at(j)); + + int fd = c->get_in_fd(); + if (fd >= 0 && FD_ISSET(fd, &active_set)) + { + actual_console = c; + int retval = c->proc_input(cmdset); + if (retval) + { + del_console(c); + delete c; + } + actual_console = 0; + return(0 == cons->count); + } + } + return 0; +} - 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); +void +cl_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); }