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>
62 extern "C" int vasprintf(char **strp, const char *format, va_list ap);
63 extern "C" int vsnprintf(char *str, size_t size,const char *format,va_list ap);
66 cmd_do_print(FILE *f, char *format, va_list ap)
71 vasprintf(&msg, format, ap);
72 ret= fprintf(f, "%s", msg);
75 # ifdef HAVE_VSNPRINTF
77 vsnprintf(msg, 80*25, format, ap);
78 ret= fprintf(f, "%s", msg);
82 vsprintf(msg, format, ap); /* Dangerous */
83 ret= fprintf(f, "%s", msg);
87 /*strcpy(msg, "Unimplemented printf has called.\n");*/
89 /*strcpy(msg, "printf can not be implemented, upgrade your libc.\n");*/
103 cl_prompt_option::cl_prompt_option(class cl_console *console):
110 cl_prompt_option::init(void)
113 help= format_string("Prompt string of console%d", con->get_id());
114 create(con, string_opt, "prompt", help);
115 //fprintf(stderr," **new prompt option %p\"%s\", value=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval);
117 default_option("prompt");
118 //fprintf(stderr,"opt=%p\"%s\" value after default set=%p str=%p\n",option,object_name(option),option->get_value(),option->get_value()->sval);
123 cl_prompt_option::option_changed(void)
128 option->get_value(&s);
133 cl_debug_option::cl_debug_option(class cl_console *console):
134 cl_prompt_option(console)
138 cl_debug_option::init(void)
141 help= format_string("Debug messages to console%d", con->get_id());
142 create(con, bool_opt, "debug", help);
144 default_option("debug");
149 cl_debug_option::option_changed(void)
154 option->get_value(&b);
156 con->flags|= CONS_DEBUG;
158 con->flags&= ~CONS_DEBUG;
164 *____________________________________________________________________________
167 cl_console::cl_console(char *fin, char *fout, class cl_app *the_app):
176 if (f= fopen(fin, "r"), in= f, !f)
177 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
180 if (f= fopen(fout, "w"), out= f, !f)
181 fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno));
186 flags|= CONS_INTERACTIVE;
188 ;//fprintf(stderr, "Warning: non-interactive console\n");
192 cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app):
204 flags|= CONS_INTERACTIVE;
206 ;//fprintf(stderr, "Warning: non-interactive console\n");
211 * use the port number supplied to connect to localhost for
217 connect_to_port(int portnum)
219 int sock= socket(AF_INET,SOCK_STREAM,0);
220 struct sockaddr_in sin;
222 sin.sin_family = AF_INET;
223 sin.sin_port = htons(portnum);
224 sin.sin_addr.s_addr = inet_addr("127.0.0.1");
226 if (connect(sock, (struct sockaddr *)&sin, sizeof(sin))) {
227 fprintf(stderr, "Connect to port %d: %s\n", portnum, strerror(errno));
233 cl_console::cl_console(int portnumber, class cl_app *the_app)
235 int sock= connect_to_port(portnumber);
239 if (!(in= fdopen(sock, "r")))
240 fprintf(stderr, "cannot open port for input\n");
241 if (!(out= fdopen(sock, "w")))
242 fprintf(stderr, "cannot open port for output\n");
243 //fprintf(stderr, "init socket done\n");
249 cl_console::clone_for_exec(char *fin)
254 if (fi= fopen(fin, "r"), !fi)
256 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
259 if ((fo= fdopen(dup(fileno(out)), "a")) == 0)
262 fprintf(stderr, "Can't re-open output file: %s\n", strerror(errno));
265 class cl_console *con= new cl_sub_console(this, fi, fo, app);
270 cl_console::init(void)
273 prompt_option= new cl_prompt_option(this);
274 prompt_option->init();
275 null_prompt_option= new cl_optref(this);
276 null_prompt_option->init();
277 null_prompt_option->use("null_prompt");
278 debug_option= new cl_debug_option(this);
279 debug_option->init();
281 flags&= ~CONS_PROMPT;
286 cl_console::~cl_console(void)
293 if (flags & CONS_PROMPT)
298 delete prompt_option;
299 delete null_prompt_option;
316 cl_console::welcome(void)
318 FILE *Out= rout?rout:out;
321 (flags & CONS_NOWELCOME))
323 fprintf(Out, "uCsim %s, Copyright (C) 1997 Daniel Drotos, Talker Bt.\n"
324 "uCsim comes with ABSOLUTELY NO WARRANTY; for details type "
326 "This is free software, and you are welcome to redistribute it\n"
327 "under certain conditions; type `show c' for details.\n",
333 cl_console::redirect(char *fname, char *mode)
335 if ((rout= fopen(fname, mode)) == NULL)
336 dd_printf("Unable to open file '%s' for %s: %s\n",
337 fname, (mode[0]=='w')?"write":"append", strerror(errno));
341 cl_console::un_redirect(void)
350 cl_console::print_prompt(void)
353 FILE *Out= rout?rout:out;
355 if (flags & (CONS_PROMPT|CONS_FROZEN|CONS_INACTIVE))
360 if (/*app->args->arg_avail('P')*/null_prompt_option->get_value(bool(0)))
364 fprintf(Out, "%d", id);
365 fprintf(Out, "%s", (prompt && prompt[0])?prompt:"> ");
366 // ((p= app->args->get_sarg(0, "prompt"))?p:"> "));
372 cl_console::dd_printf(char *format, ...)
376 FILE *Out= rout?rout:out;
380 va_start(ap, format);
381 ret= cmd_do_print(Out, format, ap);
388 cl_console::print_bin(long data, int bits)
391 FILE *Out= rout?rout:out;
395 mask= mask << ((bits >= 1)?(bits-1):0);
398 fprintf(Out, "%c", (data&mask)?'1':'0');
404 cl_console::print_char_octal(char c)
406 FILE *Out= rout?rout:out;
409 ::print_char_octal(c, Out);
418 cl_console::match(int fdnum)
427 cl_console::get_in_fd(void)
429 if (flags & CONS_INACTIVE)
431 return(in?fileno(in):-1);
435 cl_console::input_avail(void)
440 if ((i= get_in_fd()) < 0)
445 tv.tv_sec= tv.tv_usec= 0;
446 i= select(i+1, &s, NULL, NULL, &tv);
451 cl_console::read_line(void)
456 if (getline(&s, 0, in) < 0)
460 # ifdef HAVE_GETDELIM
462 s= (char *)malloc(n);
463 if (getdelim(&s, &n, '\n', in) < 0)
471 s= (char *)malloc(300);
472 if (fgets(s, 300, in) == NULL)
480 s[strlen(s)-1]= '\0';
481 if (s[strlen(s)-1] == '\r')
482 s[strlen(s)-1]= '\0';
483 flags&= ~CONS_PROMPT;
488 cl_console::proc_input(class cl_cmdset *cmdset)
495 fprintf(out, "End\n");
498 char *cmdstr= read_line();
501 if (flags & CONS_FROZEN)
503 app->get_sim()->stop(resUSER);
504 flags&= ~CONS_FROZEN;
514 class cl_cmdline *cmdline;
516 if (flags & CONS_ECHO)
517 dd_printf("%s\n", cmdstr);
518 cmdline= new cl_cmdline(app, cmdstr, this);
520 cm= cmdset->get_cmd(cmdline);
522 retval= cm->work(app, cmdline, this);
525 retval= interpret(cmdstr);
528 //retval= sim->do_cmd(cmd, this);
537 * Old version, sim->do_cmd() falls into this if it doesn't find a new
538 * command object which can handle entered command
542 cl_console::interpret(char *cmd)
544 FILE *Out= rout?rout:out;
546 fprintf(Out, "Unknown command\n");
551 cl_console::set_id(int new_id)
556 set_name(s= format_string("console%d", id));
561 cl_console::set_prompt(char *p)
574 * This console listen on a socket and can accept connection requests
578 cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app)
582 if ((sock= make_server_socket(serverport)) >= 0)
584 if (listen(sock, 10) < 0)
585 fprintf(stderr, "Listen on port %d: %s\n",
586 serverport, strerror(errno));
592 cl_listen_console::match(int fdnum)
594 return(sock == fdnum);
598 cl_listen_console::get_in_fd(void)
604 cl_listen_console::proc_input(class cl_cmdset *cmdset)
607 ACCEPT_SOCKLEN_T size;
608 struct sockaddr_in sock_addr;
609 class cl_commander *cmd;
611 cmd= app->get_commander();
612 size= sizeof(struct sockaddr);
613 newsock= accept(sock, (struct sockaddr*)&sock_addr, &size);
619 if (!(in= fdopen(newsock, "r+")))
620 fprintf(stderr, "cannot open port for input\n");
621 if (!(out= fdopen(newsock, "w+")))
622 fprintf(stderr, "cannot open port for output\n");
623 class cl_console *c= cmd->mk_console(in, out);
624 c->flags|= CONS_INTERACTIVE;
630 #endif /* SOCKET_AVAIL */
637 cl_sub_console::cl_sub_console(class cl_console *the_parent,
638 FILE *fin, FILE *fout, class cl_app *the_app):
639 cl_console(fin, fout, the_app)
644 cl_sub_console::~cl_sub_console(void)
646 class cl_commander *c= app->get_commander();
650 c->activate_console(parent);
655 cl_sub_console::init(void)
657 class cl_commander *c= app->get_commander();
661 c->deactivate_console(parent);
670 * Command interpreter
671 *____________________________________________________________________________
674 cl_commander::cl_commander(class cl_app *the_app, class cl_cmdset *acmdset
675 /*, class cl_sim *asim*/):
679 cons= new cl_list(1, 1);
680 actual_console= frozen_console= 0;
685 cl_commander::init(void)
687 class cl_optref console_on_option(this);
689 console_on_option.init();
690 console_on_option.use("console_on");
693 set_name("Commander");
695 char *Config= app->args->get_sarg(0, "Config");
698 class cl_console *con= mk_console(0/*"/dev/tty"*/);
702 //if (app->args->arg_avail('c'))
704 char *cn= console_on_option.get_value(cn);
706 add_console(mk_console(cn, cn));
709 if (app->args->arg_avail('Z'))
710 add_console(mk_console(app->args->get_iarg(0, "Zport")));
711 if (app->args->arg_avail('r'))
712 add_console(mk_console(app->args->get_iarg('r', 0)));
714 if (cons->get_count() == 0)
715 add_console(mk_console(stdin, stdout));
719 cl_commander::~cl_commander(void)
726 cl_commander::mk_console(char *fin, char *fout)
728 return(new cl_console(fin, fout, app));
732 cl_commander::mk_console(FILE *fin, FILE *fout)
734 return(new cl_console(fin, fout, app));
739 cl_commander::mk_console(int portnumber)
741 return(new cl_listen_console(portnumber, app));
746 cl_commander::add_console(class cl_console *console)
750 int i=cons->add(console);
757 cl_commander::del_console(class cl_console *console)
759 cons->disconn(console);
764 cl_commander::activate_console(class cl_console *console)
766 console->flags&= ~CONS_INACTIVE;
767 //console->print_prompt();
772 cl_commander::deactivate_console(class cl_console *console)
774 console->flags|= CONS_INACTIVE;
779 cl_commander::set_fd_set(void)
783 //fprintf(stderr, "** Setting fd set\n");
786 for (i= 0; i < cons->count; i++)
789 class cl_console *c= (class cl_console*)(cons->at(i));
790 if ((fd= c->get_in_fd()) >= 0)
792 if ((c->flags & CONS_FROZEN) == 0 ||
793 (c->flags & CONS_INTERACTIVE) != 0)
795 FD_SET(fd, &read_set);
801 ;//fprintf(stderr, "** Skipping console %p\n",c);
808 * Printing to all consoles
812 cl_commander::all_printf(char *format, ...)
817 for (i= 0; i < cons->count; i++)
819 class cl_console *c= (class cl_console*)(cons->at(i));
820 FILE *Out= c->get_out();
823 va_start(ap, format);
824 ret= cmd_do_print(Out, format, ap);
832 cl_commander::prompt(void)
836 for (i= 0; i < cons->count; i++)
838 class cl_console *c= (class cl_console*)(cons->at(i));
844 cl_commander::all_print(char *string, int length)
848 for (i= 0; i < cons->count; i++)
850 class cl_console *c= (class cl_console*)(cons->at(i));
851 FILE *Out= c->get_out();
854 for (int j= 0; j < length; j++)
855 putc(string[j], Out);
862 * Printing to actual_console
866 cl_commander::dd_printf(char *format, ...)
873 f= actual_console->get_out();
874 else if (frozen_console)
875 f= frozen_console->get_out();
878 if (/*actual_console &&
879 actual_console->out*/f)
881 va_start(ap, format);
882 ret= cmd_do_print(f/*actual_console->out*/, format, ap);
889 * Printing to consoles which have CONS_DEBUG flag set
893 cl_commander::debug(char *format, ...)
898 for (i= 0; i < cons->count; i++)
900 class cl_console *c= (class cl_console*)(cons->at(i));
901 FILE *Out= c->get_out();
903 c->flags & CONS_DEBUG)
905 va_start(ap, format);
906 ret= cmd_do_print(Out, format, ap);
914 cl_commander::flag_printf(int iflags, char *format, ...)
919 for (i= 0; i < cons->count; i++)
921 class cl_console *c= (class cl_console*)(cons->at(i));
922 FILE *Out= c->get_out();
924 (c->flags & iflags) == iflags)
926 va_start(ap, format);
927 ret= cmd_do_print(Out, format, ap);
935 cl_commander::input_avail(void)
940 tv.tv_sec= tv.tv_usec= 0;
941 active_set= read_set;
942 i= select(fd_num, &active_set, NULL, NULL, &tv);
947 cl_commander::input_avail_on_frozen(void)
953 if ((fd= frozen_console->get_in_fd()) >= 0 &&
956 return(frozen_console->input_avail());
960 cl_commander::wait_input(void)
964 active_set= read_set;
966 i= select(fd_num, &active_set, NULL, NULL, NULL);
971 cl_commander::proc_input(void)
975 for (i= 0; i < fd_num; i++)
976 if (FD_ISSET(i, &active_set))
980 for (j= 0; j < cons->count; j++)
982 c= (class cl_console*)(cons->at(j));
986 int retval= c->proc_input(cmdset);
993 return(cons->count == 0);
1001 /* End of cmd.src/newcmd.cc */