1 /*
2  *   Copyright (c) 2002 by Michael J. Roberts.  All Rights Reserved.
3  *
4  *   Please see the accompanying license file, LICENSE.TXT, for information
5  *   on using and copying this software.
6  */
7 /*
8 Name
9   vmmaincn.h - VM main startup - console helper version
10 Function
11   Implements some helpers for the vmmain module and its users, for
12   implementations using the standard system console (G_console and
13   os_printz):
14 
15   - provides a CVmMainClientIfc implementation that writes errors to
16   G_console (or os_printz, if no G_console is available), and ignores other
17   notifications
18 
19 Notes
20 
21 Modified
22   04/05/02 MJRoberts  - Creation
23 */
24 
25 #include "os.h"
26 #include "t3std.h"
27 #include "vmmain.h"
28 #include "vmconsol.h"
29 
30 
31 /* ------------------------------------------------------------------------ */
32 /*
33  *   Client services interface for T3 VM - console-based version
34  */
35 class CVmMainClientConsole: public CVmMainClientIfc
36 {
37 public:
38     /* set plain ASCII mode */
set_plain_mode()39     void set_plain_mode()
40     {
41         /* set plain mode in the OS-level console */
42         os_plain();
43     }
44 
45     /* create the console */
create_console(struct vm_globals *)46     CVmConsoleMain *create_console(struct vm_globals *)
47     {
48         /* create a standard console and return it */
49         return new CVmConsoleMain();
50     }
51 
52     /* delete the console */
delete_console(struct vm_globals * vmg,CVmConsoleMain * con)53     void delete_console(struct vm_globals *vmg, CVmConsoleMain *con)
54     {
55         /* set up for global access */
56         VMGLOB_PTR(vmg);
57 
58         /* flush any pending buffered output */
59         con->flush(vmg_ VM_NL_NONE);
60 
61         /* delete the output formatter */
62         delete con;
63     }
64 
65     /* initialize */
client_init(struct vm_globals * vmg,const char * script_file,const char * log_file,const char * cmd_log_file,const char * banner_str)66     void client_init(struct vm_globals *vmg,
67                      const char *script_file,
68                      const char *log_file,
69                      const char *cmd_log_file,
70                      const char *banner_str)
71     {
72         /* set up for global access */
73         VMGLOB_PTR(vmg);
74 
75         /* if we have a script file, set up script input on the console */
76         if (script_file != 0)
77             G_console->open_script_file(script_file, TRUE, FALSE);
78 
79         /* if we have a log file, set up logging on the console */
80         if (log_file != 0)
81             G_console->open_log_file(log_file);
82 
83         /* set up command logging on the console if desired */
84         if (cmd_log_file != 0)
85             G_console->open_command_log(cmd_log_file);
86 
87         /* tell the HTML renderer that we're a T3 caller */
88         G_console->format_text(vmg_ "<?T3>");
89 
90         /* show the banner on the console, if desired */
91         if (banner_str != 0)
92         {
93             G_console->format_text(vmg_ banner_str);
94             G_console->write_blank_line(vmg0_);
95         }
96     }
97 
98     /* terminate */
client_terminate(struct vm_globals *)99     void client_terminate(struct vm_globals *) { }
100 
101     /* pre-execution initialization */
pre_exec(struct vm_globals *)102     void pre_exec(struct vm_globals *) { }
103 
104     /* post-execution termination/error termination */
post_exec(struct vm_globals *)105     void post_exec(struct vm_globals *) { }
post_exec_err(struct vm_globals *)106     void post_exec_err(struct vm_globals *) { }
107 
108     /* display an error */
display_error(struct vm_globals * vmg,const char * msg,int add_blank_line)109     void display_error(struct vm_globals *vmg, const char *msg,
110                        int add_blank_line)
111     {
112         CVmConsole *con;
113 
114         /* set up for global access */
115         VMGLOB_PTR(vmg);
116 
117         /* if we have globals, get the console */
118         con = (vmg != 0 ? G_console : 0);
119 
120         /* if we have a console, write to it */
121         if (con != 0)
122         {
123             int old_obey;
124 
125             /* flush any pending buffered output */
126             con->flush(vmg_ VM_NL_NONE);
127 
128             /* put the console in obey-whitespace mode for our message */
129             old_obey = con->set_obey_whitespace(TRUE);
130 
131             /* display the message on the console */
132             con->format_text(vmg_ msg);
133 
134             /* add a blank line if desired */
135             if (add_blank_line)
136                 con->write_blank_line(vmg0_);
137 
138             /* restore console mode */
139             con->set_obey_whitespace(old_obey);
140         }
141         else
142         {
143             /* display the error on the OS-level console */
144             os_printz(msg);
145 
146             /* add a blank line if desired */
147             if (add_blank_line)
148             {
149                 size_t len;
150 
151                 /* add one newline */
152                 os_printz("\n");
153 
154                 /*
155                  *   if the message itself didn't end with a newline, add
156                  *   another newline
157                  */
158                 if ((len = strlen(msg)) == 0 || msg[len-1] != '\n')
159                     os_printz("\n");
160             }
161         }
162     }
163 };
164 
165