Debian Codegraph @

terps/tads/tads3/vmmaincn.h

raw symbols SCIP
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 */
39 void set_plain_mode()
40 {
41 /* set plain mode in the OS-level console */
42 os_plain();
43 }
44
45 /* create the console */
46 CVmConsoleMain *create_console(struct vm_globals *vmg)
47 {
48 /* set up for global access */
49 VMGLOB_PTR(vmg);
50
51 /* create a standard console and return it */
52 return new CVmConsoleMain(vmg0_);
53 }
54
55 /* delete the console */
56 void delete_console(struct vm_globals *vmg, CVmConsoleMain *con)
57 {
58 /* set up for global access */
59 VMGLOB_PTR(vmg);
60
61 /* flush any pending buffered output */
62 con->flush(vmg_ VM_NL_NONE);
63
64 /* delete the output formatter */
65 con->delete_obj(vmg0_);
66 }
67
68 /* initialize */
69 void client_init(struct vm_globals *vmg,
70 const char *script_file, int script_quiet,
71 const char *log_file,
72 const char *cmd_log_file,
73 const char *banner_str,
74 int more_mode)
75 {
76 /* set up for global access */
77 VMGLOB_PTR(vmg);
78
79 /* set the [More] mode as desired */
80 G_console->set_more_state(more_mode);
81
82 /* if we have a script file, set up script input on the console */
83 if (script_file != 0)
84 {
85 G_console->open_script_file(
86 vmg_ script_file, script_quiet, FALSE);
87 }
88
89 /* if we have a log file, set up logging on the console */
90 if (log_file != 0)
91 G_console->open_log_file(vmg_ log_file);
92
93 /* set up command logging on the console if desired */
94 if (cmd_log_file != 0)
95 G_console->open_command_log(vmg_ cmd_log_file, TRUE);
96
97 /* tell the HTML renderer that we're a T3 caller */
98 G_console->format_text(vmg_ "<?T3>");
99
100 /* show the banner on the console, if desired */
101 if (banner_str != 0)
102 {
103 G_console->format_text(vmg_ banner_str);
104 G_console->write_blank_line(vmg0_);
105 }
106 }
107
108 /* terminate */
109 void client_terminate(struct vm_globals *) { }
110
111 /* pre-execution initialization */
112 void pre_exec(struct vm_globals *) { }
113
114 /* post-execution termination/error termination */
115 void post_exec(struct vm_globals *) { }
116 void post_exec_err(struct vm_globals *) { }
117
118 /* display an error */
119 void display_error(struct vm_globals *vmg, const struct CVmException *exc,
120 const char *msg, int add_blank_line)
121 {
122 CVmConsole *con;
123 size_t len;
124
125 /* check for a newline at the end of the message */
126 int has_nl = ((len = strlen(msg)) != 0 && msg[len-1] == '\n');
127
128 /* set up for global access */
129 VMGLOB_PTR(vmg);
130
131 /* if we have globals, get the console */
132 con = VMGLOB_IF_AVAIL(G_console);
133
134 /* if we have a console, write to it */
135 if (con != 0)
136 {
137 int old_obey;
138
139 /* flush any pending buffered output */
140 con->flush(vmg_ VM_NL_NONE);
141
142 /* put the console in obey-whitespace mode for our message */
143 old_obey = con->set_obey_whitespace(TRUE);
144
145 /* display the message on the console */
146 con->format_text(vmg_ msg);
147
148 /* add a blank line if desired */
149 if (add_blank_line)
150 con->write_blank_line(vmg0_);
151
152 /* restore console mode */
153 con->set_obey_whitespace(old_obey);
154 }
155 else
156 {
157 /* display the error on the OS-level console */
158 os_printz(msg);
159
160 /* add a blank line if desired */
161 if (add_blank_line)
162 {
163 /* add one newline */
164 os_printz("\n");
165
166 /*
167 * if the message itself didn't end with a newline, add
168 * another newline
169 */
170 if (!has_nl)
171 os_printz("\n");
172 }
173 }
174 }
175 };
176