2 * Simulator of microcontrollers (cmd.src/newcmdwin32.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>
56 #include "newcmdwin32cl.h"
61 *____________________________________________________________________________
68 handle = INVALID_HANDLE_VALUE;
73 cl_channel::set(HANDLE _handle, e_handle_type _type)
75 assert(INVALID_HANDLE_VALUE != _handle);
79 type = (_type == CH_UNDEF) ? guess_type() : _type;
83 cl_channel::set(FILE *_fp, e_handle_type _type)
87 handle = (HANDLE)_get_osfhandle(fileno(fp));
88 assert(INVALID_HANDLE_VALUE != handle);
89 type = (_type == CH_UNDEF) ? guess_type() : _type;
93 cl_channel::close(void)
95 assert(INVALID_HANDLE_VALUE != handle);
97 if (CH_SOCKET == type)
99 shutdown((SOCKET)handle, SD_BOTH);
100 closesocket((SOCKET)handle);
104 else if (CH_SOCKET != type)
108 handle = INVALID_HANDLE_VALUE;
114 *____________________________________________________________________________
117 cl_console::cl_console(const char *fin, const char *fout, class cl_app *the_app)
124 if (!(f = fopen(fin, "r")))
125 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
131 if (!(f = fopen(fout, "w")))
132 fprintf(stderr, "Can't open `%s': %s\n", fout, strerror(errno));
139 flags |= CONS_INTERACTIVE;
141 ;//fprintf(stderr, "Warning: non-interactive console\n");
143 lines_printed = new cl_ustrings(100, 100, "console_cache");
146 cl_console::cl_console(FILE *fin, FILE *fout, class cl_app *the_app)
155 flags |= CONS_INTERACTIVE;
157 ;//fprintf(stderr, "Warning: non-interactive console\n");
159 lines_printed = new cl_ustrings(100, 100, "console_cache");
162 cl_console::cl_console(cl_channel _in, cl_channel _out, class cl_app *the_app)
171 flags |= CONS_INTERACTIVE;
173 ;//fprintf(stderr, "Warning: non-interactive console\n");
175 lines_printed= new cl_ustrings(100, 100, "console_cache");
179 cl_console::clone_for_exec(char *fin)
185 if (!(fi = fopen(fin, "r")))
187 fprintf(stderr, "Can't open `%s': %s\n", fin, strerror(errno));
190 cl_channel ch_in = cl_channel(fi, CH_FILE);
191 class cl_console *con= new cl_sub_console(this, ch_in, out, app);
195 cl_console::~cl_console(void)
197 if (CH_UNDEF != in.get_type())
200 if (CH_UNDEF != out.get_type())
202 if (flags & CONS_PROMPT)
206 delete prompt_option;
207 delete null_prompt_option;
217 cl_console::redirect(char *fname, char *mode)
219 FILE *fp = fopen(fname, mode);
221 dd_printf("Unable to open file '%s' for %s: %s\n",
222 fname, (mode[0]=='w') ? "write" : "append", strerror(errno));
223 out.set(fp, CH_FILE);
227 cl_console::un_redirect(void)
229 if (CH_UNDEF != rout.get_type())
234 cl_console::cmd_do_print(const char *format, va_list ap)
236 FILE *f = get_out()->get_fp();
240 int ret = vfprintf(f, format, ap);
253 cl_console::read_line(void)
257 TRACE("%d-%s\n", get_id(), __PRETTY_FUNCTION__);
260 FILE *fp = in.get_fp();
264 if (getline(&s, 0, fp) < 0)
266 #elif defined HAVE_GETDELIM
268 s = (char *)malloc(n);
269 if (getdelim(&s, &n, '\n', fp) < 0)
274 #elif defined HAVE_FGETS
275 s = (char *)malloc(BUF_LEN);
276 if (fgets(s, BUF_LEN, fp) == NULL)
282 s[strlen(s)-1]= '\0';
283 if (s[strlen(s)-1] == '\r')
284 s[strlen(s)-1]= '\0';
285 flags&= ~CONS_PROMPT;
291 * This console cl_listen_console on a socket and can accept connection requests
294 cl_listen_console::cl_listen_console(int serverport, class cl_app *the_app)
299 if (INVALID_SOCKET != (sock = make_server_socket(serverport)))
301 if (SOCKET_ERROR == listen(sock, 10))
302 fprintf(stderr, "Can't listen on port %d: %d\n", serverport, WSAGetLastError());
304 in.set((HANDLE)sock, CH_SOCKET);
308 cl_listen_console::proc_input(class cl_cmdset *cmdset)
310 class cl_commander_base *cmd = app->get_commander();
312 struct sockaddr_in sock_addr;
313 ACCEPT_SOCKLEN_T size = sizeof(struct sockaddr);
314 SOCKET newsock = accept((SOCKET)get_in_fd(), (struct sockaddr*)&sock_addr, &size);
316 if (INVALID_SOCKET == newsock)
318 fprintf(stderr, "Can't accept: %d\n", WSAGetLastError());
322 int fh = _open_osfhandle((intptr_t)newsock, _O_TEXT);
325 fprintf(stderr, "Can't _open_osfhandle\n");
327 FILE *fp = fdopen(fh, "r");
329 fprintf(stderr, "Can't open port for input\n");
330 cl_channel ch_in = cl_channel(fp, CH_SOCKET);
332 fh = _open_osfhandle((intptr_t)newsock, _O_TEXT);
335 fprintf(stderr, "Can't _open_osfhandle\n");
337 fp = fdopen(fh, "w");
339 fprintf(stderr, "Can't open port for output\n");
340 cl_channel ch_out = cl_channel(fp, CH_SOCKET);
342 class cl_console_base *c = new cl_console(ch_in, ch_out, app);
343 c->flags |= CONS_INTERACTIVE;
354 cl_sub_console::cl_sub_console(class cl_console_base *the_parent,
355 cl_channel _in, cl_channel _out, class cl_app *the_app):
356 cl_console(_in, _out, the_app)
361 cl_sub_console::~cl_sub_console(void)
363 class cl_commander_base *c = app->get_commander();
367 c->activate_console(parent);
372 cl_sub_console::init(void)
374 class cl_commander_base *c = app->get_commander();
378 c->deactivate_console(parent);
387 * Command interpreter
388 *____________________________________________________________________________
392 cl_commander::init(void)
394 TRACE("%s\n", __PRETTY_FUNCTION__);
396 class cl_optref console_on_option(this);
397 class cl_optref config_file_option(this);
398 class cl_optref port_number_option(this);
399 class cl_console_base *con;
401 console_on_option.init();
402 console_on_option.use("console_on");
403 config_file_option.init();
404 config_file_option.use("config_file");
405 port_number_option.init();
408 set_name("Commander");
410 bool need_config = DD_TRUE;
412 if (port_number_option.use("port_number"))
413 add_console(new cl_listen_console(port_number_option.get_value((long)0), app));
415 /* The following code is commented out because it produces gcc warnings
416 * newcmd.cc: In member function `virtual int cl_commander::init()':
417 * newcmd.cc:785: warning: 'Config' might be used uninitialized in this function
418 * newcmd.cc:786: warning: 'cn' might be used uninitialized in this function
421 char *Config= config_file_option.get_value(Config);
422 char *cn= console_on_option.get_value(cn);
424 /* Here shoud probably be something else, but is still better then the former code... */
425 char *Config = config_file_option.get_value("");
426 char *cn = console_on_option.get_value("");
430 add_console(con = new cl_console(cn, cn, app));
431 exec_on(con, Config);
432 need_config = DD_FALSE;
434 if (cons->get_count() == 0)
436 add_console(con = new cl_console(stdin, stdout, app));
437 exec_on(con, Config);
438 need_config = DD_FALSE;
440 if (need_config && Config && *Config)
442 FILE *fc = fopen(Config, "r");
444 fprintf(stderr, "Can't open `%s': %s\n", Config, strerror(errno));
447 con = new cl_console(fc, stderr, app);
448 con->flags |= CONS_NOWELCOME | CONS_ECHO;
456 cl_commander::set_fd_set(void)
458 TRACE("%s\n", __PRETTY_FUNCTION__);
464 for (i = 0; i < cons->count; i++)
466 class cl_console *c= dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(i)));
468 if (c->input_active() && CH_SOCKET == c->in.get_type())
470 HANDLE fd = c->get_in_fd();
471 assert(INVALID_HANDLE_VALUE != fd);
473 FD_SET((SOCKET)fd, &read_set);
479 cl_commander::console_count(void)
483 for (int j = 0; j < cons->count; j++)
485 class cl_console *c = dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(j)));
487 if (c->input_active())
489 switch (c->in.get_type())
507 cl_commander::console_input_avail(void)
511 FD_ZERO(&console_active_set);
512 for (int j = 0; j < cons->count; j++)
514 class cl_console *c = dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(j)));
516 if (c->input_avail())
518 HANDLE fd = c->get_in_fd();
519 assert(INVALID_HANDLE_VALUE != fd);
521 switch (c->in.get_type())
526 FD_SET((SOCKET)fd, &console_active_set);
540 cl_commander::socket_input_avail(long timeout, bool sleep)
542 active_set = read_set;
544 if (active_set.fd_count)
546 struct timeval tv = {0, 0};
548 struct timeval *tvp = sleep ? NULL : &tv;
550 int i = select(0, &active_set, NULL, NULL, tvp);
551 if (SOCKET_ERROR == i)
553 fprintf(stderr, "Can't select: %d\n", WSAGetLastError());
561 Sleep(timeout / 1000);
567 cl_commander::input_avail_timeout(long timeout)
569 TRACE("%s\n", __PRETTY_FUNCTION__);
572 if (0 != (n = console_input_avail()))
573 FD_ZERO(&active_set);
575 n = socket_input_avail(timeout, false);
580 #define CONSOLE_TIMEOUT 300000
583 cl_commander::wait_input(void)
585 TRACE("%s\n", __PRETTY_FUNCTION__);
589 if (0 < console_count())
593 while (0 == (n = input_avail_timeout(CONSOLE_TIMEOUT)))
600 FD_ZERO(&console_active_set);
601 return socket_input_avail(0, true);
606 cl_commander::proc_input(void)
608 TRACE("%s\n", __PRETTY_FUNCTION__);
610 for (int j = 0; j < cons->count; j++)
612 class cl_console *c = dynamic_cast<class cl_console*>((class cl_console_base*)(cons->at(j)));
614 if (c->input_active())
616 HANDLE fd = c->get_in_fd();
617 assert(INVALID_HANDLE_VALUE != fd);
619 if (FD_ISSET(fd, &active_set) || FD_ISSET(fd, &console_active_set))
622 if (c->proc_input(cmdset))
628 return 0 == cons->count;
636 /* End of cmd.src/newcmdwin32.cc */