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;
797 FILE *fc= fopen(Config, "r");
799 fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno));
802 con= mk_console(fc, stderr);
803 con->flags|= CONS_NOWELCOME|CONS_ECHO;
810 cl_commander::~cl_commander(void)
817 cl_commander::mk_console(char *fin, char *fout)
819 return(new cl_console(fin, fout, app));
823 cl_commander::mk_console(FILE *fin, FILE *fout)
825 return(new cl_console(fin, fout, app));
830 cl_commander::mk_console(int portnumber)
832 return(new cl_listen_console(portnumber, app));
837 cl_commander::add_console(class cl_console *console)
841 int i=cons->add(console);
848 cl_commander::del_console(class cl_console *console)
850 cons->disconn(console);
855 cl_commander::activate_console(class cl_console *console)
857 console->flags&= ~CONS_INACTIVE;
858 //console->print_prompt();
863 cl_commander::deactivate_console(class cl_console *console)
865 console->flags|= CONS_INACTIVE;
870 cl_commander::set_fd_set(void)
874 //fprintf(stderr, "** Setting fd set\n");
877 for (i= 0; i < cons->count; i++)
880 class cl_console *c= (class cl_console*)(cons->at(i));
881 if ((fd= c->get_in_fd()) >= 0)
883 if ((c->flags & CONS_FROZEN) == 0 ||
884 (c->flags & CONS_INTERACTIVE) != 0)
886 FD_SET(fd, &read_set);
892 ;//fprintf(stderr, "** Skipping console %p\n",c);
899 * Printing to all consoles
903 cl_commander::all_printf(char *format, ...)
908 for (i= 0; i < cons->count; i++)
910 class cl_console *c= (class cl_console*)(cons->at(i));
911 FILE *Out= c->get_out();
914 va_start(ap, format);
915 ret= c->cmd_do_print(Out, format, ap);
923 cl_commander::prompt(void)
927 for (i= 0; i < cons->count; i++)
929 class cl_console *c= (class cl_console*)(cons->at(i));
935 cl_commander::all_print(char *string, int length)
939 for (i= 0; i < cons->count; i++)
941 class cl_console *c= (class cl_console*)(cons->at(i));
942 FILE *Out= c->get_out();
945 for (int j= 0; j < length; j++)
946 putc(string[j], Out);
953 * Printing to actual_console
957 cl_commander::dd_printf(char *format, ...)
962 class cl_console *con;
966 f= actual_console->get_out();
969 else if (frozen_console)
971 f= frozen_console->get_out();
979 if (/*actual_console &&
980 actual_console->out*/f &&
983 va_start(ap, format);
984 ret= con->cmd_do_print(f/*actual_console->out*/, format, ap);
991 cl_commander::dd_printf(char *format, va_list ap)
995 class cl_console *con;
999 f= actual_console->get_out();
1000 con= actual_console;
1002 else if (frozen_console)
1004 f= frozen_console->get_out();
1005 con= frozen_console;
1012 if (/*actual_console &&
1013 actual_console->out*/f &&
1016 ret= con->cmd_do_print(f/*actual_console->out*/, format, ap);
1022 * Printing to consoles which have CONS_DEBUG flag set
1026 cl_commander::debug(char *format, ...)
1031 for (i= 0; i < cons->count; i++)
1033 class cl_console *c= (class cl_console*)(cons->at(i));
1034 FILE *Out= c->get_out();
1036 c->flags & CONS_DEBUG)
1038 va_start(ap, format);
1039 ret= c->cmd_do_print(Out, format, ap);
1047 cl_commander::debug(char *format, va_list ap)
1051 for (i= 0; i < cons->count; i++)
1053 class cl_console *c= (class cl_console*)(cons->at(i));
1054 FILE *Out= c->get_out();
1056 c->flags & CONS_DEBUG)
1058 ret= c->cmd_do_print(Out, format, ap);
1065 cl_commander::flag_printf(int iflags, char *format, ...)
1070 for (i= 0; i < cons->count; i++)
1072 class cl_console *c= (class cl_console*)(cons->at(i));
1073 FILE *Out= c->get_out();
1075 (c->flags & iflags) == iflags)
1077 va_start(ap, format);
1078 ret= c->cmd_do_print(Out, format, ap);
1086 cl_commander::input_avail(void)
1091 tv.tv_sec= tv.tv_usec= 0;
1092 active_set= read_set;
1093 i= select(fd_num, &active_set, NULL, NULL, &tv);
1098 cl_commander::input_avail_on_frozen(void)
1102 if (!frozen_console)
1104 if ((fd= frozen_console->get_in_fd()) >= 0 &&
1107 return(frozen_console->input_avail());
1111 cl_commander::wait_input(void)
1115 active_set= read_set;
1117 i= select(fd_num, &active_set, NULL, NULL, NULL);
1122 cl_commander::proc_input(void)
1126 for (i= 0; i < fd_num; i++)
1127 if (FD_ISSET(i, &active_set))
1129 class cl_console *c;
1131 for (j= 0; j < cons->count; j++)
1133 c= (class cl_console*)(cons->at(j));
1137 int retval= c->proc_input(cmdset);
1144 return(cons->count == 0);
1152 cl_commander::exec_on(class cl_console *cons, char *file_name)
1154 FILE *fi= fopen(file_name, "r");
1159 class cl_console *subcon= cons->clone_for_exec(file_name);
1160 subcon->flags|= CONS_NOWELCOME;
1161 add_console(subcon);
1165 /* End of cmd.src/newcmd.cc */