version 0.2.39: fix of arith insts and start of re-structure
[fw/sdcc] / sim / ucsim / cmd.src / newcmdcl.h
1 /*
2  * Simulator of microcontrollers (cmd.src/cmdcl.h)
3  *
4  * Copyright (C) 1999,99 Drotos Daniel, Talker Bt.
5  * 
6  * To contact author send email to drdani@mazsola.iit.uni-miskolc.hu
7  *
8  */
9
10 /* This file is part of microcontroller simulator: ucsim.
11
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.
16
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.
21
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
25 02111-1307, USA. */
26 /*@1@*/
27
28 #ifndef CMD_NEWCMDCL_HEADER
29 #define CMD_NEWCMDCL_HEADER
30
31
32 #include "ddconfig.h"
33
34 #include <stdio.h>
35 #if FD_HEADER_OK
36 # include HEADER_FD
37 #endif
38
39 #include "pobjcl.h"
40
41
42 #define SY_ADDR         'a'
43 #define ADDRESS         "a"
44 #define SY_NUMBER       'n'
45 #define NUMBER          "n"
46 #define SY_DATA         'd'
47 #define DATA            "d"
48 #define SY_STRING       's'
49 #define STRING          "s"
50 #define SY_MEMORY       'm'
51 #define MEMORY          "m"
52 #define SY_HW           'h'
53 #define HW              "h"
54 #define SY_DATALIST     'D'
55 #define DATALIST        "D"
56 #define SY_BIT          'b'
57 #define BIT             "b"
58
59 /*
60  * Command line with parameters
61  */
62
63 class cl_cmdline: cl_base
64 {
65 public:
66   char *cmd;
67   char *name;
68   class cl_list *params;
69   class cl_ustrings *tokens;
70   char *matched_syntax;
71   class cl_console *con;
72
73 public:
74   cl_cmdline(char *acmd, class cl_console *acon);
75   virtual ~cl_cmdline(void);
76   virtual int init(void);
77
78   virtual int split(void);
79   virtual int shift(void);
80   virtual int repeat(void);
81   virtual class cl_cmd_arg *param(int num);
82   virtual void insert_param(int pos, class cl_cmd_arg *param);
83   virtual bool syntax_match(class cl_sim *sim, char *syntax);
84   virtual bool set_data_list(class cl_cmd_arg *parm, int *iparm);
85 private:
86   char *skip_delims(char *start);
87 };
88
89
90 /*
91  * Command and container
92  */
93
94 // simple command
95 class cl_cmd: public cl_base
96 {
97 public:
98   //class cl_sim *sim;
99   class cl_strings *names;
100   int  can_repeat;
101   char *short_help;
102   char *long_help;
103
104 public:
105   cl_cmd(//class cl_sim *asim,
106          char *aname,
107          int  can_rep,
108          char *short_hlp,
109          char *long_hlp);
110   ~cl_cmd(void);
111
112   virtual void add_name(char *name);
113   virtual int name_match(char *aname, int strict);
114   virtual int name_match(class cl_cmdline *cmdline, int strict);
115   virtual int syntax_ok(class cl_cmdline *cmdline);
116   virtual int work(class cl_sim *sim,
117                    class cl_cmdline *cmdline, class cl_console *con);
118   virtual int do_work(class cl_sim *sim,
119                       class cl_cmdline *cmdline, class cl_console *con);
120 };
121
122 #define COMMAND_HEAD(CLASS_NAME) \
123 class CLASS_NAME : public cl_cmd\
124 {
125 #define COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
126 class CLASS_NAME : public ANCESTOR \
127 {
128
129 #define COMMAND_METHODS(CLASS_NAME) \
130 public:\
131   CLASS_NAME (char *aname,\
132               int  can_rep,\
133               char *chort_help,\
134               char *long_help):\
135     cl_cmd(aname, can_rep, short_help, long_help) {}\
136   virtual int do_work(class cl_sim *sim,\
137                       class cl_cmdline *cmdline, class cl_console *con);
138 #define COMMAND_METHODS_ANCESTOR(CLASS_NAME,ANCESTOR) \
139 public:\
140   CLASS_NAME (char *aname,\
141               int  can_rep,\
142               char *chort_help,\
143               char *long_help):\
144     ANCESTOR (aname, can_rep, short_help, long_help) {}\
145   virtual int do_work(class cl_sim *sim,\
146                       class cl_cmdline *cmdline, class cl_console *con);
147
148 #define COMMAND_TAIL \
149 }
150
151 #define COMMAND(CLASS_NAME) \
152 COMMAND_HEAD(CLASS_NAME) \
153 COMMAND_METHODS(CLASS_NAME) \
154 COMMAND_TAIL
155
156 #define COMMAND_DATA(CLASS_NAME,DATA) \
157 COMMAND_HEAD(CLASS_NAME) \
158 public: DATA ; \
159 COMMAND_METHODS(CLASS_NAME)\
160 COMMAND_TAIL
161
162 #define COMMAND_ANCESTOR(CLASS_NAME,ANCESTOR) \
163 COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
164 COMMAND_METHODS_ANCESTOR(CLASS_NAME,ANCESTOR) \
165 COMMAND_TAIL
166
167 #define COMMAND_DATA_ANCESTOR(CLASS_NAME,ANCESTOR,DATA) \
168 COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
169 public: DATA ; \
170 COMMAND_METHODS_ANCESTOR(CLASS_NAME,ANCESTOR)\
171 COMMAND_TAIL
172
173
174 // Command set is list of cl_cmd objects
175 class cl_cmdset: public cl_list
176 {
177 public:
178   class cl_sim *sim;
179   class cl_cmd *last_command;
180
181 public:
182   cl_cmdset(class cl_sim *asim);
183
184   virtual class cl_cmd *get_cmd(class cl_cmdline *cmdline);
185   virtual class cl_cmd *get_cmd(char *cmd_name);
186   virtual void del(char *name);
187   virtual void replace(char *name, class cl_cmd *cmd);
188 };
189
190 // subset of commands
191 class cl_super_cmd: public cl_cmd
192 {
193 public:
194   class cl_cmdset *commands;
195
196 public:
197   cl_super_cmd(//class cl_sim *asim,
198                char *aname,
199                int  can_rep,
200                char *short_hlp,
201                char *long_hlp,
202                class cl_cmdset *acommands);
203   ~cl_super_cmd(void);
204
205   virtual int work(class cl_sim *sim,
206                    class cl_cmdline *cmdline, class cl_console *con);
207 };
208
209
210 /*
211  * Command console
212  */
213
214 class cl_console: public cl_base
215 {
216   friend class cl_commander;
217 protected:
218   FILE *in, *out;
219 public:
220   class cl_sim *sim;
221   char *last_command;
222   int flags; // See CONS_XXXX
223   char *prompt;
224
225 public:
226   cl_console(): cl_base() {}
227   cl_console(char *fin, char *fout, class cl_sim *asim);
228   cl_console(FILE *fin, FILE *fout, class cl_sim *asim);
229 #ifdef SOCKET_AVAIL
230   cl_console(int portnumber, class cl_sim *asim);
231 #endif
232   ~cl_console(void);
233   virtual int init(void);
234
235   virtual void welcome(void);
236   virtual void print_prompt(void);
237   virtual int  printf(char *format, ...);
238   virtual void print_bin(long data, int bits);
239   virtual int  match(int fdnum);
240   virtual int  get_in_fd(void);
241   virtual int  input_avail(void);
242   virtual char *read_line(void);
243   virtual int  proc_input(class cl_cmdset *cmdset);
244   virtual bool interpret(char *cmd);
245 };
246
247 #ifdef SOCKET_AVAIL
248 class cl_listen_console: public cl_console
249 {
250 public:
251   int sock;
252 public:
253   cl_listen_console(int serverport, class cl_sim *asim);
254
255   virtual void welcome(void) {}
256   virtual void prompt(void) {}
257
258   virtual int match(int fdnum);
259   virtual int get_in_fd(void);
260   virtual int proc_input(class cl_cmdset *cmdset);
261 };
262 #endif
263
264
265 /*
266  * Command interpreter
267  */
268
269 class cl_commander: public cl_base
270 {
271 public:
272   class cl_list *cons;
273   fd_set read_set, active_set;
274   int fd_num;
275   class cl_sim *sim;
276   class cl_console *actual_console, *frozen_console;
277   class cl_cmdset *cmdset;
278
279 public:
280   cl_commander(class cl_cmdset *acmdset, class cl_sim *asim);
281   ~cl_commander(void);
282   virtual int init(void);
283
284   virtual class cl_console *mk_console(char *fin, char *fout,
285                                        class cl_sim *asim);
286   virtual class cl_console *mk_console(FILE *fin, FILE *fout,
287                                        class cl_sim *asim);
288 #ifdef SOCKET_AVAIL
289   virtual class cl_console *mk_console(int portnumber, class cl_sim *asim);
290 #endif
291   void add_console(class cl_console *console);
292   void del_console(class cl_console *console);
293   void set_fd_set(void);
294
295   int all_printf(char *format, ...);    // print to all consoles
296   int all_print(char *string, int length);
297   int printf(char *format, ...);        // print to actual_console
298   int debug(char *format, ...);         // print consoles with debug flag set
299   int flag_printf(int iflags, char *format, ...);
300   int input_avail(void);
301   int input_avail_on_frozen(void);
302   int wait_input(void);
303   int proc_input(void);
304 };
305
306
307 #endif
308
309 /* End of cmd.src/cmdcl.h */