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>
35 #ifdef HAVE_SYS_SOCKET_H
36 # include <sys/socket.h>
37 # include <netinet/in.h>
38 # include <arpa/inet.h>
65 extern "C" int vasprintf(char **strp, const char *format, va_list ap);
66 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
73 cl_prompt_option::cl_prompt_option(class cl_console *console):
80 cl_prompt_option::init(void)
83 help= format_string("Prompt string of console%d", con->get_id());
84 create(con, string_opt, "prompt", help);
85 //fprintf(stderr," **new prompt option %p\"%s\", value=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval);
87 default_option("prompt");
88 //fprintf(stderr,"opt=%p\"%s\" value after default set=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval);
93 cl_prompt_option::option_changed(void)
98 option->get_value(&s);
103 cl_debug_option::cl_debug_option(class cl_console *console):
104 cl_prompt_option(console)
108 cl_debug_option::init(void)
111 help= format_string("Debug messages to console%d", con->get_id());
112 create(con, bool_opt, "debug", help);
114 default_option("debug");
119 cl_debug_option::option_changed(void)
124 option->get_value(&b);
126 con->flags|= CONS_DEBUG;
128 con->flags&= ~CONS_DEBUG;
134 *____________________________________________________________________________
137 cl_console::cl_console(char *fin, char *fout, class cl_app *the_app):
145 if (f= fopen(fin, "r"), in= f, !f)
146 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
149 if (f= fopen(fout, "w"), out= f, !f)
150 fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno));
155 flags|= CONS_INTERACTIVE;
157 ;//fprintf(stderr, "Warning: non-interactive console\n");
160 lines_printed= new cl_ustrings(100, 100, "console_cache");
163 cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app):
173 flags|= CONS_INTERACTIVE;
175 ;//fprintf(stderr, "Warning: non-interactive console\n");
178 lines_printed= new cl_ustrings(100, 100, "console_cache");
182 * use the port number supplied to connect to localhost for
188 connect_to_port(int portnum)
190 int sock= socket(AF_INET,SOCK_STREAM,0);
191 struct sockaddr_in sin;
193 sin.sin_family = AF_INET;
194 sin.sin_port = htons(portnum);
195 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
197 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin))) {
198 fprintf(stderr, "Connect to port %d: %s\n", portnum, strerror(errno));
204 cl_console::cl_console(int portnumber, class cl_app *the_app)
206 int sock= connect_to_port(portnumber);
209 if (!(in= fdopen(sock, "r")))
210 fprintf(stderr, "cannot open port for input\n");
211 if (!(out= fdopen(sock, "w")))
212 fprintf(stderr, "cannot open port for output\n");
213 //fprintf(stderr, "init socket done\n");
215 lines_printed= new cl_ustrings(1, 1, "console_cache");
220 cl_console::clone_for_exec(char *fin)
226 if (fi= fopen(fin, "r"), !fi)
228 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
231 if ((fo= fdopen(dup(fileno(out)), "a")) == 0)
234 fprintf(stderr, "Can't re-open output file: %s\n", strerror(errno));
237 class cl_console *con= new cl_sub_console(this, fi, fo, app);
242 cl_console::init(void)
245 prompt_option= new cl_prompt_option(this);
246 prompt_option->init();
247 null_prompt_option= new cl_optref(this);
248 null_prompt_option->init();
249 null_prompt_option->use("null_prompt");
250 debug_option= new cl_debug_option(this);
251 debug_option->init();
253 flags&= ~CONS_PROMPT;
260 cl_console::~cl_console(void)
267 if (flags & CONS_PROMPT)
272 delete prompt_option;
273 delete null_prompt_option;
286 cl_console::accept_last(void)
290 if (isatty(fileno(in)))
301 cl_console::welcome(void)
303 FILE *Out= rout?rout:out;
306 (flags & CONS_NOWELCOME))
308 fprintf(Out, "uCsim %s, Copyright (C) 1997 Daniel Drotos, Talker Bt.\n"
309 "uCsim comes with ABSOLUTELY NO WARRANTY; for details type "
311 "This is free software, and you are welcome to redistribute it\n"
312 "under certain conditions; type `show c' for details.\n",
318 cl_console::redirect(char *fname, char *mode)
320 if ((rout= fopen(fname, mode)) == NULL)
321 dd_printf("Unable to open file '%s' for %s: %s\n",
322 fname, (mode[0]=='w')?"write":"append", strerror(errno));
326 cl_console::un_redirect(void)
336 cl_console::cmd_do_print(FILE *f, char *format, va_list ap)
339 #ifdef HAVE_VASPRINTF
341 vasprintf(&msg, format, ap);
342 ret= fprintf(f, "%s", msg);
345 # ifdef HAVE_VSNPRINTF
347 vsnprintf(msg, 80*25, format, ap);
348 ret= fprintf(f, "%s", msg);
352 vsprintf(msg, format, ap); /* Dangerous */
353 ret= fprintf(f, "%s", msg);
357 /*strcpy(msg, "Unimplemented printf has called.\n");*/
359 /*strcpy(msg, "printf can not be implemented, upgrade your libc.\n");*/
369 cl_console::print_prompt(void)
372 FILE *Out= rout?rout:out;
374 if (flags & (CONS_PROMPT|CONS_FROZEN|CONS_INACTIVE))
379 if (/*app->args->arg_avail('P')*/null_prompt_option->get_value(bool(0)))
383 fprintf(Out, "%d", id);
384 fprintf(Out, "%s", (prompt && prompt[0])?prompt:"> ");
385 // ((p= app->args->get_sarg(0, "prompt"))?p:"> "));
391 cl_console::dd_printf(char *format, ...)
395 FILE *Out= rout?rout:out;
399 va_start(ap, format);
400 ret= cmd_do_print(Out, format, ap);
407 cl_console::debug(char *format, ...)
409 if ((flags & CONS_DEBUG) == 0)
414 FILE *Out= rout?rout:out;
418 va_start(ap, format);
419 ret= cmd_do_print(Out, format, ap);
426 cl_console::print_bin(long data, int bits)
429 FILE *Out= rout?rout:out;
433 mask= mask << ((bits >= 1)?(bits-1):0);
436 fprintf(Out, "%c", (data&mask)?'1':'0');
442 cl_console::print_char_octal(char c)
444 FILE *Out= rout?rout:out;
447 ::print_char_octal(c, Out);
456 cl_console::match(int fdnum)
465 cl_console::get_in_fd(void)
467 if (flags & CONS_INACTIVE)
469 return(in?fileno(in):-1);
473 cl_console::input_avail(void)
478 if ((i= get_in_fd()) < 0)
483 tv.tv_sec= tv.tv_usec= 0;
484 i= select(i+1, &s, NULL, NULL, &tv);
489 cl_console::read_line(void)
494 if (getline(&s, 0, in) < 0)
498 # ifdef HAVE_GETDELIM
500 s= (char *)malloc(n);
501 if (getdelim(&s, &n, '\n', in) < 0)
509 s= (char *)malloc(300);
510 if (fgets(s, 300, in) == NULL)
518 s[strlen(s)-1]= '\0';
519 if (s[strlen(s)-1] == '\r')
520 s[strlen(s)-1]= '\0';
521 flags&= ~CONS_PROMPT;
526 cl_console::proc_input(class cl_cmdset *cmdset)
533 fprintf(out, "End\n");
536 char *cmdstr= read_line();
539 if (flags & CONS_FROZEN)
541 app->get_sim()->stop(resUSER);
542 flags&= ~CONS_FROZEN;
552 class cl_cmdline *cmdline= 0;
554 if (flags & CONS_ECHO)
555 dd_printf("%s\n", cmdstr);
556 cmdline= new cl_cmdline(app, cmdstr, this);
558 if (cmdline->repeat() &&
564 cmdline= last_cmdline;
568 cm= cmdset->get_cmd(cmdline, accept_last());
578 retval= cm->work(app, cmdline, this);
582 last_cmdline= cmdline;
589 class YY_cl_ucsim_parser_CLASS *pars;
590 class cl_ucsim_lexer *lexer;
591 lexer= new cl_ucsim_lexer(cmdstr);
592 pars= new YY_cl_ucsim_parser_CLASS(lexer);
598 retval= interpret(cmdstr);*/
601 //retval= sim->do_cmd(cmd, this);
610 * Old version, sim->do_cmd() falls into this if it doesn't find a new
611 * command object which can handle entered command
615 cl_console::interpret(char *cmd)
617 FILE *Out= rout?rout:out;
619 fprintf(Out, "Unknown command\n");
624 cl_console::set_id(int new_id)
629 set_name(s= format_string("console%d", id));
634 cl_console::set_prompt(char *p)
647 * This console listen on a socket and can accept connection requests
651 cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app)
654 if ((sock= make_server_socket(serverport)) >= 0)
656 if (listen(sock, 10) < 0)
657 fprintf(stderr, "Listen on port %d: %s\n",
658 serverport, strerror(errno));
664 cl_listen_console::match(int fdnum)
666 return(sock == fdnum);
670 cl_listen_console::get_in_fd(void)
676 cl_listen_console::proc_input(class cl_cmdset *cmdset)
679 ACCEPT_SOCKLEN_T size;
680 struct sockaddr_in sock_addr;
681 class cl_commander *cmd;
683 cmd= app->get_commander();
684 size= sizeof(struct sockaddr);
685 newsock= accept(sock, (struct sockaddr*)&sock_addr, &size);
691 if (!(in= fdopen(newsock, "r+")))
692 fprintf(stderr, "cannot open port for input\n");
693 if (!(out= fdopen(newsock, "w+")))
694 fprintf(stderr, "cannot open port for output\n");
695 class cl_console *c= cmd->mk_console(in, out);
696 c->flags|= CONS_INTERACTIVE;
702 #endif /* SOCKET_AVAIL */
709 cl_sub_console::cl_sub_console(class cl_console *the_parent,
710 FILE *fin, FILE *fout, class cl_app *the_app):
711 cl_console(fin, fout, the_app)
716 cl_sub_console::~cl_sub_console(void)
718 class cl_commander *c= app->get_commander();
722 c->activate_console(parent);
727 cl_sub_console::init(void)
729 class cl_commander *c= app->get_commander();
733 c->deactivate_console(parent);
742 * Command interpreter
743 *____________________________________________________________________________
746 cl_commander::cl_commander(class cl_app *the_app, class cl_cmdset *acmdset
747 /*, class cl_sim *asim*/):
751 cons= new cl_list(1, 1, "consoles");
752 actual_console= frozen_console= 0;
757 cl_commander::init(void)
759 class cl_optref console_on_option(this);
760 class cl_optref config_file_option(this);
761 class cl_optref port_number_option(this);
762 class cl_console *con;
764 console_on_option.init();
765 console_on_option.use("console_on");
766 config_file_option.init();
767 config_file_option.use("config_file");
768 port_number_option.init();
771 set_name("Commander");
773 bool need_config= DD_TRUE;
776 if (port_number_option.use("port_number"))
777 add_console(mk_console(port_number_option.get_value((long)0)));
780 char *Config= config_file_option.get_value(Config);
781 char *cn= console_on_option.get_value(cn);
785 add_console(con= mk_console(cn, cn));
786 exec_on(con, Config);
787 need_config= DD_FALSE;
789 if (cons->get_count() == 0)
791 add_console(con= mk_console(stdin, stdout));
792 exec_on(con, Config);
793 need_config= DD_FALSE;
799 FILE *fc= fopen(Config, "r");
801 fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno));
804 con= mk_console(fc, stderr);
805 con->flags|= CONS_NOWELCOME|CONS_ECHO;
812 cl_commander::~cl_commander(void)
819 cl_commander::mk_console(char *fin, char *fout)
821 return(new cl_console(fin, fout, app));
825 cl_commander::mk_console(FILE *fin, FILE *fout)
827 return(new cl_console(fin, fout, app));
832 cl_commander::mk_console(int portnumber)
834 return(new cl_listen_console(portnumber, app));
839 cl_commander::add_console(class cl_console *console)
843 int i=cons->add(console);
850 cl_commander::del_console(class cl_console *console)
852 cons->disconn(console);
857 cl_commander::activate_console(class cl_console *console)
859 console->flags&= ~CONS_INACTIVE;
860 //console->print_prompt();
865 cl_commander::deactivate_console(class cl_console *console)
867 console->flags|= CONS_INACTIVE;
872 cl_commander::set_fd_set(void)
876 //fprintf(stderr, "** Setting fd set\n");
879 for (i= 0; i < cons->count; i++)
882 class cl_console *c= (class cl_console*)(cons->at(i));
883 if ((fd= c->get_in_fd()) >= 0)
885 if ((c->flags & CONS_FROZEN) == 0 ||
886 (c->flags & CONS_INTERACTIVE) != 0)
888 FD_SET(fd, &read_set);
894 ;//fprintf(stderr, "** Skipping console %p\n",c);
901 * Printing to all consoles
905 cl_commander::all_printf(char *format, ...)
910 for (i= 0; i < cons->count; i++)
912 class cl_console *c= (class cl_console*)(cons->at(i));
913 FILE *Out= c->get_out();
916 va_start(ap, format);
917 ret= c->cmd_do_print(Out, format, ap);
925 cl_commander::prompt(void)
929 for (i= 0; i < cons->count; i++)
931 class cl_console *c= (class cl_console*)(cons->at(i));
937 cl_commander::all_print(char *string, int length)
941 for (i= 0; i < cons->count; i++)
943 class cl_console *c= (class cl_console*)(cons->at(i));
944 FILE *Out= c->get_out();
947 for (int j= 0; j < length; j++)
948 putc(string[j], Out);
955 * Printing to actual_console
959 cl_commander::get_out(void)
962 return(actual_console->get_out());
963 else if (frozen_console)
964 return(frozen_console->get_out());
969 cl_commander::dd_printf(char *format, ...)
974 class cl_console *con;
978 f= actual_console->get_out();
981 else if (frozen_console)
983 f= frozen_console->get_out();
991 if (/*actual_console &&
992 actual_console->out*/f &&
995 va_start(ap, format);
996 ret= con->cmd_do_print(f/*actual_console->out*/, format, ap);
1003 cl_commander::dd_printf(char *format, va_list ap)
1007 class cl_console *con;
1011 f= actual_console->get_out();
1012 con= actual_console;
1014 else if (frozen_console)
1016 f= frozen_console->get_out();
1017 con= frozen_console;
1024 if (/*actual_console &&
1025 actual_console->out*/f &&
1028 ret= con->cmd_do_print(f/*actual_console->out*/, format, ap);
1034 * Printing to consoles which have CONS_DEBUG flag set
1038 cl_commander::debug(char *format, ...)
1043 for (i= 0; i < cons->count; i++)
1045 class cl_console *c= (class cl_console*)(cons->at(i));
1046 FILE *Out= c->get_out();
1048 c->flags & CONS_DEBUG)
1050 va_start(ap, format);
1051 ret= c->cmd_do_print(Out, format, ap);
1059 cl_commander::debug(char *format, va_list ap)
1063 for (i= 0; i < cons->count; i++)
1065 class cl_console *c= (class cl_console*)(cons->at(i));
1066 FILE *Out= c->get_out();
1068 c->flags & CONS_DEBUG)
1070 ret= c->cmd_do_print(Out, format, ap);
1077 cl_commander::flag_printf(int iflags, char *format, ...)
1082 for (i= 0; i < cons->count; i++)
1084 class cl_console *c= (class cl_console*)(cons->at(i));
1085 FILE *Out= c->get_out();
1087 (c->flags & iflags) == iflags)
1089 va_start(ap, format);
1090 ret= c->cmd_do_print(Out, format, ap);
1098 cl_commander::input_avail(void)
1103 tv.tv_sec= tv.tv_usec= 0;
1104 active_set= read_set;
1105 i= select(fd_num, &active_set, NULL, NULL, &tv);
1110 cl_commander::input_avail_on_frozen(void)
1114 if (!frozen_console)
1116 if ((fd= frozen_console->get_in_fd()) >= 0 &&
1119 return(frozen_console->input_avail());
1123 cl_commander::wait_input(void)
1127 active_set= read_set;
1129 i= select(fd_num, &active_set, NULL, NULL, NULL);
1134 cl_commander::proc_input(void)
1138 for (i= 0; i < fd_num; i++)
1139 if (FD_ISSET(i, &active_set))
1141 class cl_console *c;
1143 for (j= 0; j < cons->count; j++)
1145 c= (class cl_console*)(cons->at(j));
1149 int retval= c->proc_input(cmdset);
1156 return(cons->count == 0);
1164 cl_commander::exec_on(class cl_console *cons, char *file_name)
1166 FILE *fi= fopen(file_name, "r");
1171 class cl_console *subcon= cons->clone_for_exec(file_name);
1172 subcon->flags|= CONS_NOWELCOME;
1173 add_console(subcon);
1177 /* End of cmd.src/newcmd.cc */