2 * Simulator of microcontrollers (cmd.src/newcmdposix.cc)
4 * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5 * Copyright (C) 2006, Borut Razem - borut.razem@siol.net
7 * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
11 /* This file is part of microcontroller simulator: ucsim.
13 UCSIM is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 UCSIM is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with UCSIM; see the file COPYING. If not, write to the Free
25 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
36 #include <sys/types.h>
39 # include HEADER_SOCKET
40 # if defined HAVE_SYS_SOCKET_H
41 # include <netinet/in.h>
42 # include <arpa/inet.h>
67 #include "newcmdposixcl.h"
72 *____________________________________________________________________________
75 cl_console::cl_console(const char *fin, const char *fout, class cl_app *the_app)
82 if (f= fopen(fin, "r"), in= f, !f)
83 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
86 if (f= fopen(fout, "w"), out= f, !f)
87 fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno));
91 flags|= CONS_INTERACTIVE;
93 ;//fprintf(stderr, "Warning: non-interactive console\n");
96 lines_printed= new cl_ustrings(100, 100, "console_cache");
99 cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app)
107 flags|= CONS_INTERACTIVE;
109 ;//fprintf(stderr, "Warning: non-interactive console\n");
112 lines_printed= new cl_ustrings(100, 100, "console_cache");
116 cl_console::clone_for_exec(char *fin)
122 if (fi= fopen(fin, "r"), !fi)
124 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
127 if ((fo= fdopen(dup(fileno(out)), "a")) == 0)
130 fprintf(stderr, "Can't re-open output file: %s\n", strerror(errno));
133 class cl_console *con= new cl_sub_console(this, fi, fo, app);
137 cl_console::~cl_console(void)
144 if (flags & CONS_PROMPT)
149 delete prompt_option;
150 delete null_prompt_option;
167 cl_console::redirect(char *fname, char *mode)
169 if ((rout= fopen(fname, mode)) == NULL)
170 dd_printf("Unable to open file '%s' for %s: %s\n",
171 fname, (mode[0]=='w')?"write":"append", strerror(errno));
175 cl_console::un_redirect(void)
184 cl_console::cmd_do_print(const char *format, va_list ap)
191 ret= vfprintf(f, format, ap);
205 cl_console::read_line(void)
210 if (getline(&s, 0, in) < 0)
212 #elif defined HAVE_GETDELIM
214 s= (char *)malloc(n);
215 if (getdelim(&s, &n, '\n', in) < 0)
220 #elif defined HAVE_FGETS
221 s= (char *)malloc(300);
222 if (fgets(s, 300, in) == NULL)
228 s[strlen(s)-1]= '\0';
229 if (s[strlen(s)-1] == '\r')
230 s[strlen(s)-1]= '\0';
231 flags&= ~CONS_PROMPT;
237 * This console listen on a socket and can accept connection requests
241 cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app)
244 if ((sock= make_server_socket(serverport)) >= 0)
246 if (listen(sock, 10) < 0)
247 fprintf(stderr, "Listen on port %d: %s\n",
248 serverport, strerror(errno));
254 cl_listen_console::proc_input(class cl_cmdset *cmdset)
257 ACCEPT_SOCKLEN_T size;
258 struct sockaddr_in sock_addr;
259 class cl_commander_base *cmd;
262 cmd= app->get_commander();
263 size= sizeof(struct sockaddr);
264 newsock= accept(sock, (struct sockaddr*)&sock_addr, &size);
270 if (!(in= fdopen(newsock, "r")))
271 fprintf(stderr, "cannot open port for input\n");
272 if (!(out= fdopen(newsock, "w")))
273 fprintf(stderr, "cannot open port for output\n");
274 class cl_console_base *c= new cl_console(in, out, app);
275 c->flags|= CONS_INTERACTIVE;
280 #endif /* SOCKET_AVAIL */
287 cl_sub_console::cl_sub_console(class cl_console_base *the_parent,
288 FILE *fin, FILE *fout, class cl_app *the_app):
289 cl_console(fin, fout, the_app)
294 cl_sub_console::~cl_sub_console(void)
296 class cl_commander_base *c= app->get_commander();
300 c->activate_console(parent);
305 cl_sub_console::init(void)
307 class cl_commander_base *c= app->get_commander();
311 c->deactivate_console(parent);
320 * Command interpreter
321 *____________________________________________________________________________
325 cl_commander::init(void)
327 class cl_optref console_on_option(this);
328 class cl_optref config_file_option(this);
329 class cl_optref port_number_option(this);
330 class cl_console_base *con;
332 console_on_option.init();
333 console_on_option.use("console_on");
334 config_file_option.init();
335 config_file_option.use("config_file");
336 port_number_option.init();
339 set_name("Commander");
341 bool need_config= DD_TRUE;
344 if (port_number_option.use("port_number"))
345 add_console(new cl_listen_console(port_number_option.get_value((long)0), app));
348 /* The following code is commented out because it produces gcc warnings
349 * newcmd.cc: In member function `virtual int cl_commander::init()':
350 * newcmd.cc:785: warning: 'Config' might be used uninitialized in this function
351 * newcmd.cc:786: warning: 'cn' might be used uninitialized in this function
354 char *Config= config_file_option.get_value(Config);
355 char *cn= console_on_option.get_value(cn);
357 /* Here shoud probably be something else, but is still better then the former code... */
358 char *Config= config_file_option.get_value("");
359 char *cn= console_on_option.get_value("");
363 add_console(con= new cl_console(cn, cn, app));
364 exec_on(con, Config);
365 need_config= DD_FALSE;
367 if (cons->get_count() == 0)
369 add_console(con= new cl_console(stdin, stdout, app));
370 exec_on(con, Config);
371 need_config= DD_FALSE;
377 FILE *fc= fopen(Config, "r");
379 fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno));
382 con= new cl_console(fc, stderr, app);
383 con->flags|= CONS_NOWELCOME|CONS_ECHO;
391 cl_commander::set_fd_set(void)
397 for (i = 0; i < cons->count; i++)
399 class cl_console *c= dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(i)));
401 if (c->input_active())
403 UCSOCKET_T fd = c->get_in_fd();
406 FD_SET(fd, &read_set);
415 cl_commander::input_avail(void)
417 struct timeval tv = {0, 0};
418 active_set = read_set;
420 int i = select(fd_num, &active_set, NULL, NULL, &tv);
428 cl_commander::wait_input(void)
431 active_set = read_set;
432 int i = select(fd_num, &active_set, NULL, NULL, NULL);
437 cl_commander::proc_input(void)
439 for (int j = 0; j < cons->count; j++)
441 class cl_console *c = dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(j)));
443 if (c->input_active())
445 UCSOCKET_T fd = c->get_in_fd();
448 if (FD_ISSET(fd, &active_set))
451 int retval = c->proc_input(cmdset);
458 return(0 == cons->count);
466 /* End of cmd.src/newcmdposix.cc */