Merge branch ucsim-034-pre3 to main trunk; new version 0.4
[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 #include <sys/types.h>
36 #if FD_HEADER_OK
37 # include HEADER_FD
38 #endif
39
40 // prj
41 #include "pobjcl.h"
42
43 // sim.src
44 #include "appcl.h"
45
46
47 #define SY_ADDR         'a'
48 #define ADDRESS         "a"
49 #define SY_NUMBER       'n'
50 #define NUMBER          "n"
51 #define SY_DATA         'd'
52 #define DATA            "d"
53 #define SY_STRING       's'
54 #define STRING          "s"
55 #define SY_MEMORY       'm'
56 #define MEMORY          "m"
57 #define SY_HW           'h'
58 #define HW              "h"
59 #define SY_DATALIST     'D'
60 #define DATALIST        "D"
61 #define SY_BIT          'b'
62 #define BIT             "b"
63
64 enum cmd_operate_on {
65   operate_on_none,
66   operate_on_app,
67   operate_on_sim,
68   operate_on_uc
69 };
70
71
72 /*
73  * Command line with parameters
74  */
75
76 class cl_cmdline: cl_base
77 {
78 public:
79   class cl_app *app;
80   char *cmd;
81   char *name;
82   class cl_list *params;
83   class cl_ustrings *tokens;
84   char *matched_syntax;
85   class cl_console *con;
86
87 public:
88   cl_cmdline(class cl_app *the_app, char *acmd, class cl_console *acon);
89   virtual ~cl_cmdline(void);
90   virtual int init(void);
91
92   virtual int split(void);
93   virtual int shift(void);
94   virtual int repeat(void);
95   virtual class cl_cmd_arg *param(int num);
96   virtual void insert_param(int pos, class cl_cmd_arg *param);
97   virtual bool syntax_match(class cl_uc *uc, char *syntax);
98   virtual bool set_data_list(class cl_cmd_arg *parm, int *iparm);
99 private:
100   char *skip_delims(char *start);
101 };
102
103
104 /*
105  * Command and container
106  */
107
108 // simple command
109 class cl_cmd: public cl_base
110 {
111 public:
112   enum cmd_operate_on operate_on;
113   class cl_strings *names;
114   int  can_repeat;
115   char *short_help;
116   char *long_help;
117
118 public:
119   cl_cmd(enum cmd_operate_on opon,
120          char *aname,
121          int  can_rep,
122          char *short_hlp,
123          char *long_hlp);
124   virtual ~cl_cmd(void);
125
126   virtual void add_name(char *name);
127   virtual int name_match(char *aname, int strict);
128   virtual int name_match(class cl_cmdline *cmdline, int strict);
129   virtual int syntax_ok(class cl_cmdline *cmdline);
130   virtual int work(class cl_app *app,
131                    class cl_cmdline *cmdline, class cl_console *con);
132   virtual int do_work(class cl_cmdline *cmdline, class cl_console *con);
133   virtual int do_work(class cl_app *app,
134                       class cl_cmdline *cmdline, class cl_console *con);
135   virtual int do_work(class cl_sim *sim,
136                       class cl_cmdline *cmdline, class cl_console *con);
137   virtual int do_work(class cl_uc *uc,
138                       class cl_cmdline *cmdline, class cl_console *con);
139 };
140
141 #define COMMAND_HEAD(CLASS_NAME) \
142 class CLASS_NAME : public cl_cmd\
143 {
144 #define COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
145 class CLASS_NAME : public ANCESTOR \
146 {
147
148 #define COMMAND_METHODS(CLASS_NAME) \
149 public:\
150   CLASS_NAME (char *aname,\
151               int  can_rep,\
152               char *short_help,\
153               char *long_help):\
154     cl_cmd(operate_on_none, aname, can_rep, short_help, long_help) {}\
155   virtual int do_work(class cl_cmdline *cmdline, class cl_console *con);
156
157 #define COMMAND_METHODS_ON(ON,CLASS_NAME) \
158 public:\
159   CLASS_NAME (char *aname,\
160               int  can_rep,\
161               char *short_help,\
162               char *long_help):\
163     cl_cmd(operate_on_ ## ON, aname, can_rep, short_help, long_help) {}\
164   virtual int do_work(class cl_ ## ON * ON ,\
165                       class cl_cmdline *cmdline, class cl_console *con);
166
167 #define COMMAND_METHODS_ANCESTOR(CLASS_NAME,ANCESTOR) \
168 public:\
169   CLASS_NAME (char *aname,\
170               int  can_rep,\
171               char *short_help,\
172               char *long_help):\
173     ANCESTOR (aname, can_rep, short_help, long_help) {}\
174   virtual int do_work(class cl_cmdline *cmdline, class cl_console *con);
175
176 #define COMMAND_METHODS_ANCESTOR_ON(ON,CLASS_NAME,ANCESTOR) \
177 public:\
178   CLASS_NAME (char *aname,\
179               int  can_rep,\
180               char *short_help,\
181               char *long_help):\
182     ANCESTOR (aname, can_rep, short_help, long_help) {}\
183   virtual int do_work(class cl_ ## ON * ON ,\
184                       class cl_cmdline *cmdline, class cl_console *con); \
185
186
187 #define COMMAND_TAIL }
188
189 #define COMMAND(CLASS_NAME) \
190 COMMAND_HEAD(CLASS_NAME) \
191 COMMAND_METHODS(CLASS_NAME) \
192 COMMAND_TAIL
193
194 #define COMMAND_ON(ON,CLASS_NAME) \
195 COMMAND_HEAD(CLASS_NAME) \
196 COMMAND_METHODS_ON(ON,CLASS_NAME) \
197 COMMAND_TAIL
198
199 #define COMMAND_DATA(CLASS_NAME,DATA) \
200 COMMAND_HEAD(CLASS_NAME) \
201 public: DATA ; \
202 COMMAND_METHODS(CLASS_NAME)\
203 COMMAND_TAIL
204
205 #define COMMAND_DATA_ON(ON,CLASS_NAME,DATA) \
206 COMMAND_HEAD(CLASS_NAME) \
207 public: DATA ; \
208 COMMAND_METHODS_ON(ON,CLASS_NAME)\
209 COMMAND_TAIL
210
211 #define COMMAND_ANCESTOR_ON(ON,CLASS_NAME,ANCESTOR) \
212 COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
213 COMMAND_METHODS_ANCESTOR_ON(ON,CLASS_NAME,ANCESTOR) \
214 COMMAND_TAIL
215
216 #define COMMAND_DATA_ANCESTOR(CLASS_NAME,ANCESTOR,DATA) \
217 COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
218 public: DATA ; \
219 COMMAND_METHODS_ANCESTOR(CLASS_NAME,ANCESTOR)\
220 COMMAND_TAIL
221
222 #define COMMAND_DATA_ANCESTOR_ON(ON,CLASS_NAME,ANCESTOR,DATA) \
223 COMMAND_HEAD_ANCESTOR(CLASS_NAME,ANCESTOR) \
224 public: DATA ; \
225 COMMAND_METHODS_ANCESTOR_ON(ON,CLASS_NAME,ANCESTOR)\
226 COMMAND_TAIL
227
228 #define COMMAND_DO_WORK(CLASS_NAME) \
229 int \
230 CLASS_NAME::do_work(class cl_cmdline *cmdline, class cl_console *con)
231 #define COMMAND_DO_WORK_APP(CLASS_NAME) \
232 int \
233 CLASS_NAME::do_work(class cl_app *app,\
234                     class cl_cmdline *cmdline, class cl_console *con)
235 #define COMMAND_DO_WORK_SIM(CLASS_NAME) \
236 int \
237 CLASS_NAME::do_work(class cl_sim *sim,\
238                     class cl_cmdline *cmdline, class cl_console *con)
239 #define COMMAND_DO_WORK_UC(CLASS_NAME) \
240 int \
241 CLASS_NAME::do_work(class cl_uc *uc,\
242                     class cl_cmdline *cmdline, class cl_console *con)
243
244 // Command set is list of cl_cmd objects
245 class cl_cmdset: public cl_list
246 {
247 public:
248   //class cl_sim *sim;
249   class cl_cmd *last_command;
250
251 public:
252   cl_cmdset(void);
253   //cl_cmdset(class cl_sim *asim);
254
255   virtual class cl_cmd *get_cmd(class cl_cmdline *cmdline);
256   virtual class cl_cmd *get_cmd(char *cmd_name);
257   virtual void del(char *name);
258   virtual void replace(char *name, class cl_cmd *cmd);
259 };
260
261 // subset of commands
262 class cl_super_cmd: public cl_cmd
263 {
264 public:
265   class cl_cmdset *commands;
266
267 public:
268   cl_super_cmd(char *aname,
269                int  can_rep,
270                char *short_hlp,
271                char *long_hlp,
272                class cl_cmdset *acommands);
273   virtual ~cl_super_cmd(void);
274
275   virtual int work(class cl_app *app,
276                    class cl_cmdline *cmdline, class cl_console *con);
277 };
278
279
280 /*
281  * Command console
282  */
283
284 class cl_console: public cl_base
285 {
286   friend class cl_commander;
287 protected:
288   FILE *in, *out;
289 public:
290   class cl_app *app;
291   char *last_command;
292   int flags; // See CONS_XXXX
293   char *prompt;
294
295 public:
296   cl_console(void): cl_base() { app= 0; in= out= 0; flags= 0; }
297   cl_console(char *fin, char *fout, class cl_app *the_app);
298   cl_console(FILE *fin, FILE *fout, class cl_app *the_app);
299 #ifdef SOCKET_AVAIL
300   cl_console(int portnumber, class cl_app *the_app);
301 #endif
302   virtual ~cl_console(void);
303   virtual int init(void);
304
305   virtual void welcome(void);
306   virtual void print_prompt(void);
307   virtual int  dd_printf(char *format, ...);
308   virtual void print_bin(long data, int bits);
309   virtual int  match(int fdnum);
310   virtual int  get_in_fd(void);
311   virtual int  input_avail(void);
312   virtual char *read_line(void);
313   virtual int  proc_input(class cl_cmdset *cmdset);
314   virtual bool interpret(char *cmd);
315 };
316
317 #ifdef SOCKET_AVAIL
318 class cl_listen_console: public cl_console
319 {
320 public:
321   int sock;
322 public:
323   cl_listen_console(int serverport, class cl_app *the_app);
324
325   virtual void welcome(void) {}
326   virtual void prompt(void) {}
327
328   virtual int match(int fdnum);
329   virtual int get_in_fd(void);
330   virtual int proc_input(class cl_cmdset *cmdset);
331 };
332 #endif
333
334
335 /*
336  * Command interpreter
337  */
338
339 class cl_commander: public cl_base
340 {
341 public:
342   class cl_app *app;
343   class cl_list *cons;
344   fd_set read_set, active_set;
345   int fd_num;
346   //class cl_sim *sim;
347   class cl_console *actual_console, *frozen_console;
348   class cl_cmdset *cmdset;
349
350 public:
351   cl_commander(class cl_app *the_app,
352                class cl_cmdset *acmdset/*, class cl_sim *asim*/);
353   virtual ~cl_commander(void);
354   virtual int init(void);
355
356   virtual class cl_console *mk_console(char *fin, char *fout);
357   virtual class cl_console *mk_console(FILE *fin, FILE *fout);
358 #ifdef SOCKET_AVAIL
359   virtual class cl_console *mk_console(int portnumber);
360 #endif
361   void add_console(class cl_console *console);
362   void del_console(class cl_console *console);
363   void set_fd_set(void);
364
365   int all_printf(char *format, ...);    // print to all consoles
366   int all_print(char *string, int length);
367   int dd_printf(char *format, ...);     // print to actual_console
368   int debug(char *format, ...);         // print consoles with debug flag set
369   int flag_printf(int iflags, char *format, ...);
370   int input_avail(void);
371   int input_avail_on_frozen(void);
372   int wait_input(void);
373   int proc_input(void);
374 };
375
376
377 #endif
378
379 /* End of cmd.src/cmdcl.h */