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
35 #include <sys/types.h>
38 # include HEADER_SOCKET
39 # if defined HAVE_SYS_SOCKET_H
40 # include <netinet/in.h>
41 # include <arpa/inet.h>
66 #include "newcmdposixcl.h"
71 *____________________________________________________________________________
74 cl_console::cl_console(char *fin, char *fout, class cl_app *the_app)
81 if (f= fopen(fin, "r"), in= f, !f)
82 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
85 if (f= fopen(fout, "w"), out= f, !f)
86 fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno));
90 flags|= CONS_INTERACTIVE;
92 ;//fprintf(stderr, "Warning: non-interactive console\n");
95 lines_printed= new cl_ustrings(100, 100, "console_cache");
98 cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app)
106 flags|= CONS_INTERACTIVE;
108 ;//fprintf(stderr, "Warning: non-interactive console\n");
111 lines_printed= new cl_ustrings(100, 100, "console_cache");
115 cl_console::clone_for_exec(char *fin)
121 if (fi= fopen(fin, "r"), !fi)
123 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
126 if ((fo= fdopen(dup(fileno(out)), "a")) == 0)
129 fprintf(stderr, "Can't re-open output file: %s\n", strerror(errno));
132 class cl_console *con= new cl_sub_console(this, fi, fo, app);
136 cl_console::~cl_console(void)
143 if (flags & CONS_PROMPT)
148 delete prompt_option;
149 delete null_prompt_option;
166 cl_console::redirect(char *fname, char *mode)
168 if ((rout= fopen(fname, mode)) == NULL)
169 dd_printf("Unable to open file '%s' for %s: %s\n",
170 fname, (mode[0]=='w')?"write":"append", strerror(errno));
174 cl_console::un_redirect(void)
183 cl_console::cmd_do_print(char *format, va_list ap)
190 ret= vfprintf(f, format, ap);
204 cl_console::read_line(void)
209 if (getline(&s, 0, in) < 0)
211 #elif defined HAVE_GETDELIM
213 s= (char *)malloc(n);
214 if (getdelim(&s, &n, '\n', in) < 0)
219 #elif defined HAVE_FGETS
220 s= (char *)malloc(300);
221 if (fgets(s, 300, in) == NULL)
227 s[strlen(s)-1]= '\0';
228 if (s[strlen(s)-1] == '\r')
229 s[strlen(s)-1]= '\0';
230 flags&= ~CONS_PROMPT;
236 * This console listen on a socket and can accept connection requests
240 cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app)
243 if ((sock= make_server_socket(serverport)) >= 0)
245 if (listen(sock, 10) < 0)
246 fprintf(stderr, "Listen on port %d: %s\n",
247 serverport, strerror(errno));
253 cl_listen_console::proc_input(class cl_cmdset *cmdset)
256 ACCEPT_SOCKLEN_T size;
257 struct sockaddr_in sock_addr;
258 class cl_commander_base *cmd;
261 cmd= app->get_commander();
262 size= sizeof(struct sockaddr);
263 newsock= accept(sock, (struct sockaddr*)&sock_addr, &size);
269 if (!(in= fdopen(newsock, "r")))
270 fprintf(stderr, "cannot open port for input\n");
271 if (!(out= fdopen(newsock, "w")))
272 fprintf(stderr, "cannot open port for output\n");
273 class cl_console_base *c= new cl_console(in, out, app);
274 c->flags|= CONS_INTERACTIVE;
279 #endif /* SOCKET_AVAIL */
286 cl_sub_console::cl_sub_console(class cl_console_base *the_parent,
287 FILE *fin, FILE *fout, class cl_app *the_app):
288 cl_console(fin, fout, the_app)
293 cl_sub_console::~cl_sub_console(void)
295 class cl_commander_base *c= app->get_commander();
299 c->activate_console(parent);
304 cl_sub_console::init(void)
306 class cl_commander_base *c= app->get_commander();
310 c->deactivate_console(parent);
319 * Command interpreter
320 *____________________________________________________________________________
324 cl_commander::init(void)
326 class cl_optref console_on_option(this);
327 class cl_optref config_file_option(this);
328 class cl_optref port_number_option(this);
329 class cl_console_base *con;
331 console_on_option.init();
332 console_on_option.use("console_on");
333 config_file_option.init();
334 config_file_option.use("config_file");
335 port_number_option.init();
338 set_name("Commander");
340 bool need_config= DD_TRUE;
343 if (port_number_option.use("port_number"))
344 add_console(new cl_listen_console(port_number_option.get_value((long)0), app));
347 /* The following code is commented out because it produces gcc warnings
348 * newcmd.cc: In member function `virtual int cl_commander::init()':
349 * newcmd.cc:785: warning: 'Config' might be used uninitialized in this function
350 * newcmd.cc:786: warning: 'cn' might be used uninitialized in this function
353 char *Config= config_file_option.get_value(Config);
354 char *cn= console_on_option.get_value(cn);
356 /* Here shoud probably be something else, but is still better then the former code... */
357 char *Config= config_file_option.get_value("");
358 char *cn= console_on_option.get_value("");
362 add_console(con= new cl_console(cn, cn, app));
363 exec_on(con, Config);
364 need_config= DD_FALSE;
366 if (cons->get_count() == 0)
368 add_console(con= new cl_console(stdin, stdout, app));
369 exec_on(con, Config);
370 need_config= DD_FALSE;
376 FILE *fc= fopen(Config, "r");
378 fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno));
381 con= new cl_console(fc, stderr, app);
382 con->flags|= CONS_NOWELCOME|CONS_ECHO;
390 cl_commander::set_fd_set(void)
396 for (i = 0; i < cons->count; i++)
398 class cl_console *c= dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(i)));
400 if (c->input_active())
402 UCSOCKET_T fd = c->get_in_fd();
405 FD_SET(fd, &read_set);
414 cl_commander::input_avail(void)
416 struct timeval tv = {0, 0};
417 active_set = read_set;
419 int i = select(fd_num, &active_set, NULL, NULL, &tv);
427 cl_commander::wait_input(void)
430 active_set = read_set;
431 int i = select(fd_num, &active_set, NULL, NULL, NULL);
436 cl_commander::proc_input(void)
438 for (int j = 0; j < cons->count; j++)
440 class cl_console *c = dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(j)));
442 if (c->input_active())
444 UCSOCKET_T fd = c->get_in_fd();
447 if (FD_ISSET(fd, &active_set))
450 int retval = c->proc_input(cmdset);
457 return(0 == cons->count);
465 /* End of cmd.src/newcmdposix.cc */