1 /*
2  * Simulator of microcontrollers (cmd.src/newcmdcl.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_NEWCMDCL_HEADER
30 #define CMD_NEWCMDCL_HEADER
31 
32 
33 #include "ddconfig.h"
34 
35 #include <stdio.h>
36 #include <stdarg.h>
37 #include <sys/types.h>
38 
39 // prj
40 #include "pobjcl.h"
41 #include "optioncl.h"
42 
43 // sim.src
44 //#include "appcl.h"
45 
46 // local, cmd
47 #include "commandcl.h"
48 
49 
50 // Flags of consoles
51 enum con_flags {
52   CONS_NONE        = 0,
53   CONS_DEBUG       = 0x01,   // Print debug messages on this console
54   CONS_FROZEN      = 0x02,   // Console is frozen (g command issued)
55   CONS_INTERACTIVE = 0x08,   // Interactive console
56   CONS_NOWELCOME   = 0x10,   // Do not print welcome message
57   CONS_INACTIVE    = 0x20,   // Do not do any action
58   CONS_ECHO        = 0x40,   // Echo commands
59   CONS_REDIRECTED  = 0x80,   // Console is actually redirected
60 };
61 
62 #define SY_ADDR         'a'
63 #define ADDRESS         "a"
64 #define SY_NUMBER       'n'
65 #define NUMBER          "n"
66 #define SY_DATA         'd'
67 #define DATA            "d"
68 #define SY_STRING       's'
69 #define STRING          "s"
70 #define SY_MEMORY       'm'
71 #define MEMORY          "m"
72 #define SY_HW           'h'
73 #define HW              "h"
74 #define SY_DATALIST     'D'
75 #define DATALIST        "D"
76 #define SY_BIT          'b'
77 #define BIT             "b"
78 #define SY_CELL		'c'
79 #define CELL		"c"
80 
81 
82 class cl_prompt_option: public cl_optref
83 {
84 protected:
85   class cl_console_base *con;
86 public:
87   cl_prompt_option(class cl_console_base *console);
88   virtual int init(void);
89   virtual void option_changed(void);
90 };
91 
92 class cl_debug_option: public cl_prompt_option
93 {
94 public:
95   cl_debug_option(class cl_console_base *console);
96   virtual int init(void);
97   virtual void option_changed(void);
98 };
99 
100 /*
101  * Command console
102  */
103 
104 class cl_console_base: public cl_base
105 {
106  protected:
107   class cl_prompt_option *prompt_option;
108   class cl_optref *null_prompt_option;
109   class cl_debug_option *debug_option;
110   class cl_ustrings *lines_printed;
111   class cl_cmd *last_command;
112   //class cl_cmdline *last_cmdline;
113   chars last_cmd;
114   chars startup_command;
115 
116   char nl;
117   chars lbuf;
118 
119   int tu_bg_color, tu_fg_color;
120  public:
121     int prev_quit;
122 
123  public:
124   cl_console_base(void);
125   virtual ~cl_console_base(void);
126 
127   virtual class cl_console_base *clone_for_exec(char *fin) = 0;
128 
129   virtual void redirect(char *fname, char *mode) = 0;
130   virtual void un_redirect(void) = 0;
131   virtual bool is_tty(void) const = 0;
132   virtual bool is_eof(void) const = 0;
133   virtual bool input_avail(void) = 0;
134   virtual int read_line(void) = 0;
135   virtual class cl_f *get_fout(void)= 0;
136   virtual class cl_f *get_fin(void)= 0;
137   virtual void drop_files(void)= 0; // do not close, just ignore
138   virtual void close_files(bool close_in, bool close_out)= 0;
139   virtual void replace_files(bool close_old, cl_f *new_in, cl_f *new_out)= 0;
140 
141   virtual int init(void);
142   virtual void set_startup(chars the);
get_startup()143   virtual chars *get_startup() { return &startup_command; }
has_startup()144   virtual bool has_startup() { return startup_command.nempty(); }
145 
146   virtual void welcome(void);
147   virtual int proc_input(class cl_cmdset *cmdset);
need_check(void)148   virtual bool need_check(void) { return false; }
149 
150   virtual void print_prompt(void);
151   virtual int dd_printf(const char *format, ...);
152   virtual int dd_cprintf(const char *color_name, const char *format, ...);
153   virtual chars get_color_ansiseq(const char *color_name, bool add_reset= false);
154   virtual void dd_color(const char *color_name);
155   virtual int write(char *buf, int count);
156   virtual int debug(const char *format, ...);
157   virtual void print_bin(long data, int bits);
158   virtual void print_char_octal(char c);
159   virtual int cmd_do_print(const char *format, va_list ap);
160   virtual int cmd_do_cprint(const char *color_name, const char *format, va_list ap);
161   //virtual void flush(void);
162   virtual void tu_cls(void);
163   virtual void tu_clc(void);
164   virtual void tu_cll(void);
165   virtual void tu_go(int x1, int y1);
166   virtual void tu_save(void);
167   virtual void tu_restore(void);
168   virtual void tu_hide(void);
169   virtual void tu_show(void);
170   virtual void tu_color(int bg, int fg);
171   virtual void tu_mouse_on(void);
172   virtual void tu_mouse_off(void);
173   virtual void tu_reset(void);
174 
175   virtual bool interpret(char *cmd);
get_id(void)176   virtual int get_id(void) const { return(id); }
177   virtual void set_id(int new_id);
178   virtual void set_prompt(char *p);
179 
180   virtual bool input_active(void) const;
181   //virtual bool accept_last(void) { return /*is_tty() ? DD_TRUE : DD_FALSE;*/flags&CONS_INTERACTIVE; }
prevent_quit(void)182   virtual bool prevent_quit(void) { return (prev_quit>=0)?prev_quit:true; }
183 
184  private:
185   int flags; // See CONS_XXXX
186  public:
187   virtual int set_flag(int flag, bool value);
188   virtual void set_interactive(bool new_val);
189   virtual bool get_flag(int flag);
get_flags()190   virtual int get_flags() { return flags; };
is_interactive()191   virtual bool is_interactive() { return get_flag(CONS_INTERACTIVE); }
is_frozen()192   virtual bool is_frozen() { return get_flag(CONS_FROZEN); }
193   virtual bool set_cooked(bool new_val);
194 
195  protected:
196   class cl_app *app;
197   char *prompt;
198   int id;
199 };
200 
201 class cl_console_dummy: public cl_console_base
202 {
203  public:
cl_console_dummy(void)204  cl_console_dummy(void): cl_console_base() {}
205 
clone_for_exec(char * fin)206   virtual class cl_console_base *clone_for_exec(char *fin) { return NULL; }
207 
redirect(char * fname,char * mode)208   virtual void redirect(char *fname, char *mode) {}
un_redirect(void)209   virtual void un_redirect(void) {}
is_tty(void)210   virtual bool is_tty(void) const { return false; }
is_eof(void)211   virtual bool is_eof(void) const { return false; }
input_avail(void)212   virtual bool input_avail(void) { return false; }
read_line(void)213   virtual int read_line(void) { return 0; }
get_fout(void)214   virtual class cl_f *get_fout(void) { return NULL; }
get_fin(void)215   virtual class cl_f *get_fin(void) { return NULL; }
drop_files(void)216   virtual void drop_files(void) {}
close_files(bool close_in,bool close_out)217   virtual void close_files(bool close_in, bool close_out) {}
replace_files(bool close_old,cl_f * new_in,cl_f * new_out)218   virtual void replace_files(bool close_old, cl_f *new_in, cl_f *new_out) {}
219 };
220 
221 /*
222  * Command interpreter
223  */
224 
225 class cl_commander_base: public cl_base
226 {
227  public:
228   class cl_app *app;
229   class cl_list *cons;
230   class cl_console_base *actual_console, *frozen_console, *config_console;
231   class cl_cmdset *cmdset;
232  protected:
233   class cl_list *active_inputs;
234   class cl_list *check_list;
235 
236  public:
237   cl_commander_base(class cl_app *the_app, class cl_cmdset *acmdset);
238   virtual ~cl_commander_base(void);
239 
240   virtual void add_console(class cl_console_base *console);
241   virtual void del_console(class cl_console_base *console);
242   virtual void activate_console(class cl_console_base *console);
243   virtual void deactivate_console(class cl_console_base *console);
244   virtual int consoles_prevent_quit(void);
245 
246   //void prompt(void);
247   int all_printf(const char *format, ...);        // print to all consoles
248   int dd_printf(const char *format, va_list ap);  // print to actual_console
249   int dd_cprintf(const char *color_name, const char *format, va_list ap);  // print to actual_console
250   int dd_printf(const char *format, ...);         // print to actual_console
251   int dd_cprintf(const char *color_name, const char *format, ...);         // print to actual_console
252   int debug(const char *format, ...);             // print consoles with debug flag set
253   int debug(const char *format, va_list ap);      // print consoles with debug flag set
254   int flag_printf(int iflags, const char *format, ...);
255   int input_avail_on_frozen(void);
256   class cl_console_base *exec_on(class cl_console_base *cons, char *file_name);
257 
258   virtual int init(void) = 0;
259   virtual void update_active(void) = 0;
260   virtual int proc_input(void) = 0;
261   virtual int input_avail(void) = 0;
262   virtual int wait_input(void) = 0;
check(void)263   virtual void check(void) { return; }
264 };
265 
266 
267 #endif
268 
269 /* End of cmd.src/newcmdcl.h */
270