2 * Simulator of microcontrollers (cmd.src/newcmd.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
6 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
10 /* This file is part of microcontroller simulator: ucsim.
12 UCSIM is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 UCSIM is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with UCSIM; see the file COPYING. If not, write to the Free
24 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
34 #include <sys/types.h>
37 # include HEADER_SOCKET
38 # if defined HAVE_SYS_SOCKET_H
39 # include <netinet/in.h>
40 # include <arpa/inet.h>
69 extern "C" int vasprintf(char **strp, const char *format, va_list ap);
70 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
77 cl_prompt_option::cl_prompt_option(class cl_console *console):
84 cl_prompt_option::init(void)
87 help= format_string("Prompt string of console%d", con->get_id());
88 create(con, string_opt, "prompt", help);
89 //fprintf(stderr," **new prompt option %p\"%s\", value=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval);
91 default_option("prompt");
92 //fprintf(stderr,"opt=%p\"%s\" value after default set=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval);
97 cl_prompt_option::option_changed(void)
102 option->get_value(&s);
107 cl_debug_option::cl_debug_option(class cl_console *console):
108 cl_prompt_option(console)
112 cl_debug_option::init(void)
115 help= format_string("Debug messages to console%d", con->get_id());
116 create(con, bool_opt, "debug", help);
118 default_option("debug");
123 cl_debug_option::option_changed(void)
128 option->get_value(&b);
130 con->flags|= CONS_DEBUG;
132 con->flags&= ~CONS_DEBUG;
138 *____________________________________________________________________________
141 cl_console::cl_console(char *fin, char *fout, class cl_app *the_app):
149 if (f= fopen(fin, "r"), in= f, !f)
150 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
153 if (f= fopen(fout, "w"), out= f, !f)
154 fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno));
159 flags|= CONS_INTERACTIVE;
161 ;//fprintf(stderr, "Warning: non-interactive console\n");
164 lines_printed= new cl_ustrings(100, 100, "console_cache");
167 cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app):
177 flags|= CONS_INTERACTIVE;
179 ;//fprintf(stderr, "Warning: non-interactive console\n");
182 lines_printed= new cl_ustrings(100, 100, "console_cache");
186 * use the port number supplied to connect to localhost for
192 connect_to_port(int portnum)
194 int sock= socket(AF_INET,SOCK_STREAM,0);
195 struct sockaddr_in sin;
197 sin.sin_family = AF_INET;
198 sin.sin_port = htons(portnum);
199 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
201 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin))) {
202 fprintf(stderr, "Connect to port %d: %s\n", portnum, strerror(errno));
208 cl_console::cl_console(int portnumber, class cl_app *the_app)
210 int sock= connect_to_port(portnumber);
213 if (!(in= fdopen(sock, "r")))
214 fprintf(stderr, "cannot open port for input\n");
215 if (!(out= fdopen(sock, "w")))
216 fprintf(stderr, "cannot open port for output\n");
217 //fprintf(stderr, "init socket done\n");
219 lines_printed= new cl_ustrings(1, 1, "console_cache");
224 cl_console::clone_for_exec(char *fin)
230 if (fi= fopen(fin, "r"), !fi)
232 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
235 if ((fo= fdopen(dup(fileno(out)), "a")) == 0)
238 fprintf(stderr, "Can't re-open output file: %s\n", strerror(errno));
241 class cl_console *con= new cl_sub_console(this, fi, fo, app);
246 cl_console::init(void)
249 prompt_option= new cl_prompt_option(this);
250 prompt_option->init();
251 null_prompt_option= new cl_optref(this);
252 null_prompt_option->init();
253 null_prompt_option->use("null_prompt");
254 debug_option= new cl_debug_option(this);
255 debug_option->init();
257 flags&= ~CONS_PROMPT;
264 cl_console::~cl_console(void)
271 if (flags & CONS_PROMPT)
276 delete prompt_option;
277 delete null_prompt_option;
290 cl_console::accept_last(void)
294 if (isatty(fileno(in)))
305 cl_console::welcome(void)
307 if (!(flags & CONS_NOWELCOME))
309 dd_printf("uCsim %s, Copyright (C) 1997 Daniel Drotos, Talker Bt.\n"
310 "uCsim comes with ABSOLUTELY NO WARRANTY; for details type "
312 "This is free software, and you are welcome to redistribute it\n"
313 "under certain conditions; type `show c' for details.\n",
319 cl_console::redirect(char *fname, char *mode)
321 if ((rout= fopen(fname, mode)) == NULL)
322 dd_printf("Unable to open file '%s' for %s: %s\n",
323 fname, (mode[0]=='w')?"write":"append", strerror(errno));
327 cl_console::un_redirect(void)
337 cl_console::cmd_do_print(char *format, va_list ap)
344 vfprintf(f, format, ap);
352 cl_console::print_prompt(void)
354 if (flags & (CONS_PROMPT|CONS_FROZEN|CONS_INACTIVE))
357 if (/*app->args->arg_avail('P')*/null_prompt_option->get_value(bool(0)))
359 FILE *Out = get_out();
368 dd_printf("%d%s", id, (prompt && prompt[0])?prompt:"> ");
369 // ((p= app->args->get_sarg(0, "prompt"))?p:"> "));
374 cl_console::dd_printf(char *format, ...)
379 va_start(ap, format);
380 ret= cmd_do_print(format, ap);
387 cl_console::debug(char *format, ...)
389 if ((flags & CONS_DEBUG) == 0)
395 va_start(ap, format);
396 ret= cmd_do_print(format, ap);
403 cl_console::print_bin(long data, int bits)
407 mask= mask << ((bits >= 1)?(bits-1):0);
410 dd_printf("%c", (data&mask)?'1':'0');
416 cl_console::print_char_octal(char c)
418 FILE *Out= get_out();
421 ::print_char_octal(c, Out);
430 cl_console::match(int fdnum)
439 cl_console::get_in_fd(void)
441 if (flags & CONS_INACTIVE)
443 return(in?fileno(in):-1);
447 cl_console::input_avail(void)
452 if ((i= get_in_fd()) < 0)
457 tv.tv_sec= tv.tv_usec= 0;
458 i= select(i+1, &s, NULL, NULL, &tv);
463 cl_console::read_line(void)
468 if (getline(&s, 0, in) < 0)
472 # ifdef HAVE_GETDELIM
474 s= (char *)malloc(n);
475 if (getdelim(&s, &n, '\n', in) < 0)
483 s= (char *)malloc(300);
484 if (fgets(s, 300, in) == NULL)
492 s[strlen(s)-1]= '\0';
493 if (s[strlen(s)-1] == '\r')
494 s[strlen(s)-1]= '\0';
495 flags&= ~CONS_PROMPT;
500 cl_console::proc_input(class cl_cmdset *cmdset)
507 fprintf(out, "End\n");
510 char *cmdstr= read_line();
513 if (flags & CONS_FROZEN)
515 app->get_sim()->stop(resUSER);
516 flags&= ~CONS_FROZEN;
526 class cl_cmdline *cmdline= 0;
528 if (flags & CONS_ECHO)
529 dd_printf("%s\n", cmdstr);
530 cmdline= new cl_cmdline(app, cmdstr, this);
532 if (cmdline->repeat() &&
538 cmdline= last_cmdline;
542 cm= cmdset->get_cmd(cmdline, accept_last());
552 retval= cm->work(app, cmdline, this);
556 last_cmdline= cmdline;
563 class YY_cl_ucsim_parser_CLASS *pars;
564 class cl_ucsim_lexer *lexer;
565 lexer= new cl_ucsim_lexer(cmdstr);
566 pars= new YY_cl_ucsim_parser_CLASS(lexer);
572 retval= interpret(cmdstr);*/
575 //retval= sim->do_cmd(cmd, this);
584 * Old version, sim->do_cmd() falls into this if it doesn't find a new
585 * command object which can handle entered command
589 cl_console::interpret(char *cmd)
591 dd_printf("Unknown command\n");
596 cl_console::set_id(int new_id)
601 set_name(s= format_string("console%d", id));
606 cl_console::set_prompt(char *p)
619 * This console listen on a socket and can accept connection requests
623 cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app)
626 if ((sock= make_server_socket(serverport)) >= 0)
628 if (listen(sock, 10) < 0)
629 fprintf(stderr, "Listen on port %d: %s\n",
630 serverport, strerror(errno));
636 cl_listen_console::match(int fdnum)
638 return(sock == fdnum);
642 cl_listen_console::get_in_fd(void)
648 cl_listen_console::proc_input(class cl_cmdset *cmdset)
651 ACCEPT_SOCKLEN_T size;
652 struct sockaddr_in sock_addr;
653 class cl_commander *cmd;
655 cmd= app->get_commander();
656 size= sizeof(struct sockaddr);
657 newsock= accept(sock, (struct sockaddr*)&sock_addr, &size);
663 if (!(in= fdopen(newsock, "r+")))
664 fprintf(stderr, "cannot open port for input\n");
665 if (!(out= fdopen(newsock, "w+")))
666 fprintf(stderr, "cannot open port for output\n");
667 class cl_console *c= cmd->mk_console(in, out);
668 c->flags|= CONS_INTERACTIVE;
674 #endif /* SOCKET_AVAIL */
681 cl_sub_console::cl_sub_console(class cl_console *the_parent,
682 FILE *fin, FILE *fout, class cl_app *the_app):
683 cl_console(fin, fout, the_app)
688 cl_sub_console::~cl_sub_console(void)
690 class cl_commander *c= app->get_commander();
694 c->activate_console(parent);
699 cl_sub_console::init(void)
701 class cl_commander *c= app->get_commander();
705 c->deactivate_console(parent);
714 * Command interpreter
715 *____________________________________________________________________________
718 cl_commander::cl_commander(class cl_app *the_app, class cl_cmdset *acmdset
719 /*, class cl_sim *asim*/):
723 cons= new cl_list(1, 1, "consoles");
724 actual_console= frozen_console= 0;
729 cl_commander::init(void)
731 class cl_optref console_on_option(this);
732 class cl_optref config_file_option(this);
733 class cl_optref port_number_option(this);
734 class cl_console *con;
736 console_on_option.init();
737 console_on_option.use("console_on");
738 config_file_option.init();
739 config_file_option.use("config_file");
740 port_number_option.init();
743 set_name("Commander");
745 bool need_config= DD_TRUE;
748 if (port_number_option.use("port_number"))
749 add_console(mk_console(port_number_option.get_value((long)0)));
752 /* The following code is commented out because it produces gcc warnings
753 * newcmd.cc: In member function `virtual int cl_commander::init()':
754 * newcmd.cc:785: warning: 'Config' might be used uninitialized in this function
755 * newcmd.cc:786: warning: 'cn' might be used uninitialized in this function
758 char *Config= config_file_option.get_value(Config);
759 char *cn= console_on_option.get_value(cn);
761 /* Here shoud probably be something else, but is still better then the former code... */
762 char *Config= config_file_option.get_value("");
763 char *cn= console_on_option.get_value("");
767 add_console(con= mk_console(cn, cn));
768 exec_on(con, Config);
769 need_config= DD_FALSE;
771 if (cons->get_count() == 0)
773 add_console(con= mk_console(stdin, stdout));
774 exec_on(con, Config);
775 need_config= DD_FALSE;
781 FILE *fc= fopen(Config, "r");
783 fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno));
786 con= mk_console(fc, stderr);
787 con->flags|= CONS_NOWELCOME|CONS_ECHO;
794 cl_commander::~cl_commander(void)
801 cl_commander::mk_console(char *fin, char *fout)
803 return(new cl_console(fin, fout, app));
807 cl_commander::mk_console(FILE *fin, FILE *fout)
809 return(new cl_console(fin, fout, app));
814 cl_commander::mk_console(int portnumber)
816 return(new cl_listen_console(portnumber, app));
821 cl_commander::add_console(class cl_console *console)
825 int i=cons->add(console);
832 cl_commander::del_console(class cl_console *console)
834 cons->disconn(console);
839 cl_commander::activate_console(class cl_console *console)
841 console->flags&= ~CONS_INACTIVE;
842 //console->print_prompt();
847 cl_commander::deactivate_console(class cl_console *console)
849 console->flags|= CONS_INACTIVE;
854 cl_commander::set_fd_set(void)
858 //fprintf(stderr, "** Setting fd set\n");
861 for (i= 0; i < cons->count; i++)
864 class cl_console *c= (class cl_console*)(cons->at(i));
865 if ((fd= c->get_in_fd()) >= 0)
867 if ((c->flags & CONS_FROZEN) == 0 ||
868 (c->flags & CONS_INTERACTIVE) != 0)
870 FD_SET(fd, &read_set);
876 ;//fprintf(stderr, "** Skipping console %p\n",c);
883 * Printing to all consoles
887 cl_commander::all_printf(char *format, ...)
892 for (i= 0; i < cons->count; i++)
894 class cl_console *c= (class cl_console*)(cons->at(i));
896 va_start(ap, format);
897 ret= c->cmd_do_print(format, ap);
904 cl_commander::prompt(void)
908 for (i= 0; i < cons->count; i++)
910 class cl_console *c= (class cl_console*)(cons->at(i));
916 * Printing to actual_console
920 cl_commander::dd_printf(char *format, va_list ap)
923 class cl_console *con;
929 else if (frozen_console)
939 ret= con->cmd_do_print(format, ap);
945 cl_commander::dd_printf(char *format, ...)
950 va_start(ap, format);
951 ret= dd_printf(format, ap);
958 * Printing to consoles which have CONS_DEBUG flag set
962 cl_commander::debug(char *format, ...)
967 for (i= 0; i < cons->count; i++)
969 class cl_console *c= (class cl_console*)(cons->at(i));
970 if (c->flags & CONS_DEBUG)
972 va_start(ap, format);
973 ret= c->cmd_do_print(format, ap);
981 cl_commander::debug(char *format, va_list ap)
985 for (i= 0; i < cons->count; i++)
987 class cl_console *c= (class cl_console*)(cons->at(i));
988 if (c->flags & CONS_DEBUG)
990 ret= c->cmd_do_print(format, ap);
997 cl_commander::flag_printf(int iflags, char *format, ...)
1002 for (i= 0; i < cons->count; i++)
1004 class cl_console *c= (class cl_console*)(cons->at(i));
1005 if ((c->flags & iflags) == iflags)
1007 va_start(ap, format);
1008 ret= c->cmd_do_print(format, ap);
1016 cl_commander::input_avail(void)
1021 tv.tv_sec= tv.tv_usec= 0;
1022 active_set= read_set;
1023 i= select(fd_num, &active_set, NULL, NULL, &tv);
1028 cl_commander::input_avail_on_frozen(void)
1032 if (!frozen_console)
1034 if ((fd= frozen_console->get_in_fd()) >= 0 &&
1037 return(frozen_console->input_avail());
1041 cl_commander::wait_input(void)
1045 active_set= read_set;
1047 i= select(fd_num, &active_set, NULL, NULL, NULL);
1052 cl_commander::proc_input(void)
1056 for (i= 0; i < fd_num; i++)
1057 if (FD_ISSET(i, &active_set))
1059 class cl_console *c;
1061 for (j= 0; j < cons->count; j++)
1063 c= (class cl_console*)(cons->at(j));
1067 int retval= c->proc_input(cmdset);
1074 return(cons->count == 0);
1082 cl_commander::exec_on(class cl_console *cons, char *file_name)
1084 FILE *fi= fopen(file_name, "r");
1089 class cl_console *subcon= cons->clone_for_exec(file_name);
1090 subcon->flags|= CONS_NOWELCOME;
1091 add_console(subcon);
1095 /* End of cmd.src/newcmd.cc */