Imported Upstream version 2.9.0
[debian/cc1111] / sim / ucsim / cmd.src / newcmdwin32cl.h
1 /*
2  * Simulator of microcontrollers (cmd.src/newcmdwin32cl.h)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * Copyright (C) 2006, Borut Razem - borut.razem@siol.net
6  *
7  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
8  *
9  */
10
11 /* This file is part of microcontroller simulator: ucsim.
12
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.
17
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.
22
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
26 02111-1307, USA. */
27 /*@1@*/
28
29 #ifndef CMD_NEWCMDFDCL_HEADER
30 #define CMD_NEWCMDFDCL_HEADER
31
32 #include "newcmdcl.h"
33 #include "cmdutil.h"
34
35
36 class cl_channel
37 {
38 public:
39   cl_channel(void) { set(); }
40   cl_channel(HANDLE _handle, e_handle_type _type = CH_UNDEF) { set(_handle, _type); }
41   cl_channel(FILE *_fp, e_handle_type _type = CH_UNDEF) { set(_fp, _type); }
42
43   void set(void);
44   void set(HANDLE _handle, e_handle_type _type = CH_UNDEF);
45   void set(FILE *_fp, e_handle_type _type = CH_UNDEF);
46
47   void close(void);
48   bool input_avail(void) const { return ::input_avail(handle, type); }
49   enum e_handle_type get_type(void) const { return type; }
50   HANDLE get_handle(void) const { return handle; }
51   FILE *get_fp(void) const { return fp; }
52   bool is_tty(void) const { return CH_FILE != type; }
53   bool is_eof(void) const { return get_fp() ? feof(get_fp()) : true; }
54
55 private:
56   e_handle_type guess_type(void) { return get_handle_type(handle); }
57
58   e_handle_type type;
59   HANDLE handle;
60   FILE *fp;
61 };
62
63
64 /*
65  * Command socket console
66  */
67
68 class cl_console: public cl_console_base
69 {
70   friend class cl_commander;
71
72 protected:
73   cl_channel in, out, rout/*redirected output*/;
74
75 public:
76   cl_console(void) { in = cl_channel(); out = cl_channel(); rout = cl_channel(); }
77   cl_console(const char *fin, const char *fout, class cl_app *the_app);
78   cl_console(FILE *fin, FILE *fout, class cl_app *the_app);
79   cl_console(cl_channel _in, cl_channel _out, class cl_app *the_app);
80
81   int cmd_do_print(const char *format, va_list ap);
82
83   virtual ~cl_console(void);
84   virtual class cl_console *clone_for_exec(char *fin);
85
86   virtual void redirect(char *fname, char *mode);
87   virtual void un_redirect(void);
88   virtual bool is_tty(void) const { return CH_FILE != in.get_type(); }
89   virtual bool is_eof(void) const { return in.is_eof(); }
90   virtual HANDLE get_in_fd(void) { return in.get_handle(); }
91   virtual bool input_avail(void) { return input_active() ? in.input_avail() : false; }
92   virtual char *read_line(void);
93
94 private:
95   class cl_channel *get_out(void) { return (CH_UNDEF != rout.get_type()) ? &rout : &out; }
96 };
97
98 class cl_listen_console: public cl_console
99 {
100 public:
101   cl_listen_console(int serverport, class cl_app *the_app);
102
103   virtual void welcome(void) {}
104   virtual int proc_input(class cl_cmdset *cmdset);
105 };
106
107 class cl_sub_console: public cl_console
108 {
109 private:
110   class cl_console_base *parent;
111
112 public:
113   cl_sub_console(class cl_console_base *the_parent,
114                  cl_channel _in, cl_channel _out, class cl_app *the_app);
115   virtual ~cl_sub_console(void);
116   virtual int init(void);
117 };
118
119
120 /*
121  * Command interpreter
122  */
123
124 class cl_commander: public cl_commander_base
125 {
126 private:
127   fd_set read_set, active_set, console_active_set;
128
129 public:
130   cl_commander(class cl_app *the_app, class cl_cmdset *acmdset)
131     : cl_commander_base(the_app, acmdset)
132   {
133   }
134
135   virtual int init(void);
136   virtual void set_fd_set(void);
137   virtual int input_avail(void) { return input_avail_timeout(0); }
138   virtual int wait_input(void);
139   virtual int proc_input(void);
140
141 private:
142   int console_count(void);
143   int console_input_avail(void);
144   int socket_input_avail(long timeout, bool sleep);
145   int input_avail_timeout(long timeout);
146 };
147
148 #endif
149
150 /* End of cmd.src/newcmdwin32cl.h */