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