Debian Codegraph @

terps/tads/tads3/vmmain.h

raw symbols SCIP
1 /* $Header$ */
2
3 /*
4 * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved.
5 *
6 * Please see the accompanying license file, LICENSE.TXT, for information
7 * on using and copying this software.
8 */
9 /*
10 Name
11 vmmain.h - main entrypoint to run a T3 image file
12 Function
13
14 Notes
15
16 Modified
17 10/07/99 MJRoberts - Creation
18 */
19
20 #ifndef VMMAIN_H
21 #define VMMAIN_H
22
23 #include "vmglob.h"
24
25 /*
26 * Parse a command line to determine the name of the game file specified by
27 * the arguments. If we can find a game file specification, we'll fill in
28 * 'buf' with the filename and return true; if there's no file name
29 * specified, we'll return false.
30 *
31 * Note that our parsing will work for TADS 2 or TADS 3 interpreter command
32 * lines, so this routine can be used to extract the filename from an
33 * ambiguous command line in order to check the file for its type and
34 * thereby resolve which interpreter to use.
35 *
36 * Note that the filename might not come directly from the command
37 * arguments, since it might be implied. If there's no game file directly
38 * specified, but there is an explicit "-r" option to restore a saved game,
39 * we'll pull the game filename out of the saved game file if possible.
40 * Saved game files in both TADS 2 and TADS 3 can store the original game
41 * file name that was being executed at the time the game was saved.
42 */
43 int vm_get_game_arg(int argc, const char *const *argv,
44 char *buf, size_t buflen, int *engine_type);
45
46 /*
47 * Given a game file argument, determine which engine (TADS 2 or TADS 3)
48 * should be used to run the game.
49 *
50 * We'll first check to see if the given file exists. If it does, we'll
51 * read header information from the file to try to identify the game. If
52 * the file does not exist, we will proceed to check default suffixes, if
53 * the suffix arguments are non-null.
54 *
55 * If defexts is not null, it gives an array of default filename suffix
56 * strings, suitable for use with os_defext(). When the name as given
57 * doesn't refer to an existing file, we'll try looking for files with
58 * these suffixes, one at a time.
59 *
60 * Returns one of the VM_GGT_xxx codes.
61 *
62 * If the return value is 2 or 3, we'll fill in the actual_filename buffer
63 * with the full name of the file; if we added a default suffix, the
64 * suffix will be included in this result.
65 */
66 int vm_get_game_type(const char *filename,
67 char *actual_filename,
68 size_t actual_filename_buffer_length,
69 const char *const *defexts, size_t defext_count);
70
71 /*
72 * Returns codes for vm_get_game_type()
73 */
74
75 /* game type is TADS 2 */
76 #define VM_GGT_TADS2 2
77
78 /* game type is TADS 3 */
79 #define VM_GGT_TADS3 3
80
81 /* game file not found (even after trying default extensions) */
82 #define VM_GGT_NOT_FOUND (-1)
83
84 /* game file exists but isn't a valid tads 2 or tads 3 game */
85 #define VM_GGT_INVALID (-2)
86
87 /*
88 * ambiguous filename - the exact filename doesn't exist, and more than
89 * one default suffix version exists
90 */
91 #define VM_GGT_AMBIG (-3)
92
93 /*
94 * determine if a VM_GGT_xxx code refers to a valid engine version:
95 * returns true if the code is an engine version, false if the code is an
96 * error indication
97 */
98 #define vm_ggt_is_valid(code) ((code) > 0)
99
100
101 /*
102 * Parameters structure for vm_run_image. vm_run_image() essentially takes
103 * the parsed version of the execution parameters, and loads and executes
104 * the .t3 file accordingly. The caller is responsible for parsing the
105 * command-line arguments or equivalent. You can use vm_run_image_main()
106 * if you have a standard Unix-style argv/argc and want to do the standard
107 * t3run parsing. If you don't have a standard argv/argc (e.g., you get
108 * your parameters from a GUI dialog, an external config file), you can
109 * bypass the normal t3run argv parsing by setting up this structure from
110 * your parameters and calling vm_run_image(). You can also use this
111 * approach if you want override the standard t3run option syntax and do
112 * your own argv parsing.
113 */
114 struct vm_run_image_params
115 {
116 vm_run_image_params(class CVmMainClientIfc *clientifc,
117 class CVmHostIfc *hostifc,
118 const char *image_file_name)
119 {
120 /* set the required parameters */
121 this->clientifc = clientifc;
122 this->hostifc = hostifc;
123 this->image_file_name = image_file_name;
124
125 /* assume no program arguments */
126 prog_argv = 0;
127 prog_argc = 0;
128
129 /* assume no script or log files */
130 script_file = 0;
131 log_file = 0;
132 cmd_log_file = 0;
133 script_quiet = FALSE;
134
135 /* assume we'll use [More] mode for interactive output */
136 more_mode = TRUE;
137
138 /* assume we're loading from a separate .t3 file */
139 load_from_exe = FALSE;
140
141 /* assume we won't show the VM banner */
142 show_banner = FALSE;
143
144 /* assume we will seed the RNG */
145 seed_rand = TRUE;
146
147 /* use the default character sets from the system */
148 charset = 0;
149 log_charset = 0;
150
151 /* assume we're not loading a saved state file */
152 saved_state = 0;
153
154 /* use default directories */
155 res_dir = 0;
156 file_dir = 0;
157 sandbox_dir = 0;
158
159 /* assume no network configuration */
160 netconfig = 0;
161 }
162
163 /*
164 * the client interface - this lets the host application environment
165 * provide a custom implementation of the main console interface for
166 * the game
167 */
168 class CVmMainClientIfc *clientifc;
169
170 /*
171 * the host interface - this lets the host application environment
172 * customize certain preference settings and data sources
173 */
174 class CVmHostIfc *hostifc;
175
176 /* the image file name */
177 const char *image_file_name;
178
179 /*
180 * The program command-line parameters - these are argv-style parameter
181 * strings to pass to the .t3 program's main(). (Note that these
182 * aren't the arguments to the interpreter - that's what this entire
183 * structure is about, as it contains the pre-digested results of
184 * parsing those parameters. These are instead unparsed parameters
185 * that we're to pass along to the .t3 program.)
186 */
187 const char *const *prog_argv;
188 int prog_argc;
189
190 /*
191 * The input script file. If this is not null, the interpreter will
192 * read console input from the given file and feed it to the .t3
193 * program via inputLine(), etc. If script_quiet is true, we'll read
194 * this input silently, without echoing it or program output to the
195 * console for the duration of the script input.
196 */
197 const char *script_file;
198 int script_quiet;
199
200 /*
201 * The log file. If this is not null, we'll log console output to the
202 * given file.
203 */
204 const char *log_file;
205
206 /*
207 * Command log file. If this is not null, we'll log input lines read
208 * from the console to this file.
209 */
210 const char *cmd_log_file;
211
212 /*
213 * In [More] mode, we pause and await a keystroke after each screenful
214 * of text to give the user a chance to read text before it scrolls off
215 * the screen. This is the default, but sometimes users will want to
216 * run in batch/stdio mode, where there's no pausing.
217 */
218 int more_mode;
219
220 /*
221 * Flag: load from the application executable file. If this is true,
222 * the given image filename is actually the name of the native
223 * application executable file that we're running (i.e., the main
224 * program's argv[0] or equivalent), and it contains an embedded or
225 * attached copy of the .t3 file, embedded via some OS-specific
226 * mechanism.
227 */
228 int load_from_exe;
229
230 /* flag: show the VM version/copyright banner at startup */
231 int show_banner;
232
233 /*
234 * flag: seed the random number generator automatically at startup;
235 * this uses some OS-specific source of true randomness to generate an
236 * initial random state for the RNG
237 */
238 int seed_rand;
239
240 /*
241 * Character set name for the main console. If this is not null, we'll
242 * use this charater set for text displayed to and read from the
243 * interactive console.
244 */
245 const char *charset;
246
247 /* log file character set (if not null) */
248 const char *log_charset;
249
250 /*
251 * Saved state file to restore on startup. If this is not null, we'll
252 * restore this saved state file immediately after loading the .t3
253 * file.
254 */
255 const char *saved_state;
256
257 /* resource directory */
258 const char *res_dir;
259
260 /*
261 * Default working directory for File operations. If this is not null,
262 * the File class will use this as the working directory for file
263 * operations when file names are given as relative paths. If this is
264 * null, the folder containing the .t3 file is the default.
265 */
266 const char *file_dir;
267
268 /*
269 * Sandbox directory for file safety enforcement. If this is not null,
270 * the File class uses this as the sandbox directory for file safety
271 * purposes. If this is null, the default sandbox is the file_dir if
272 * specified, otherwise the folder containing the .t3 file.
273 */
274 const char *sandbox_dir;
275
276 /*
277 * Network configuration object. If not null, we're running in web
278 * host mode, with the network parameters specified in this object.
279 */
280 class TadsNetConfig *netconfig;
281 };
282
283 /*
284 * Execute an image file. We'll return zero on success, or a VM error code
285 * on failure.
286 *
287 * If 'load_from_exe' is true, the image filename given is actually the
288 * name of the native executable file that we're running, and we should
289 * load the image file that's attached to the native executable file via
290 * the system-specific os_exeseek() mechanism.
291 *
292 * If 'script_file' is not null, we'll read console input from the given
293 * file. If 'log_file' is not null, we'll log console output to the given
294 * file. If 'cmd_log_file' is not null, we'll log each line we read from
295 * the console to the given command logging file.
296 *
297 * 'charset' optionally selects a character set to use for text displayed
298 * to or read from the user interface. If this is null, we'll use the
299 * current system character set as indicated by the osifc layer. 'charset'
300 * should usually be null unless explicitly specified by the user.
301 */
302 int vm_run_image(const vm_run_image_params *params);
303
304
305 /*
306 * Execute an image file using argc/argv conventions. We'll parse the
307 * command line and invoke the program.
308 *
309 * The 'executable_name' is the name of the host program; this is used to
310 * prepare "usage" messages.
311 *
312 * If 'defext' is true, we'll try adding a default extension ("t3",
313 * formerly "t3x") to the name of the image file we find if the given
314 * filename doesn't exist. We'll always check to see if the file exists
315 * with the exact given name before we do this, so that we don't add an
316 * extension where none is needed. If the caller doesn't want us to try
317 * adding an extension at all, pass in 'defext' as false.
318 *
319 * If 'test_mode' is true, we'll make some small changes to the program
320 * invocation protocol appropriate to running system tests. In
321 * particular, we'll build the program argument list with only the root
322 * name of the image file, not the full path - this allows the program to
323 * display the argument list without any dependencies on local path name
324 * conventions or the local directory structure, allowing for more easily
325 * portable test scripts.
326 *
327 * If 'hostifc' is null, we'll provide our own default interface. The
328 * caller can provide a custom host interface by passing in a non-null
329 * 'hostifc' value.
330 */
331 int vm_run_image_main(class CVmMainClientIfc *clientifc,
332 const char *executable_name,
333 int argc, char **argv,
334 int defext, int test_mode,
335 class CVmHostIfc *hostifc);
336
337 /*
338 * Show the interface report. This writes an XML listing of the
339 * metaclasses and intrinsic function sets to the standard output.
340 */
341 void vm_interface_report(class CVmMainClientIfc *cli, const char *fname);
342
343 /*
344 * Get the IFID from the GameInfo resource. This looks for the
345 * gameinfo.txt resource in the image file or resource directory, then
346 * scans the contents for its IFID field. If we find an IFID, we return an
347 * allocated buffer containing the IFID string (or the first IFID string,
348 * if the GameInfo contains multiple IFIDs); otherwise we return null. The
349 * caller is responsible for freeing the returned buffer with
350 * lib_free_str().
351 *
352 * Note that the host interface's resource map for the image file is
353 * generally initialized when the image file is loaded, so this routine
354 * must be called AFTER the image file has been loaded through the regular
355 * loader. If you want a more general-purpose GameInfo extractor that
356 * doesn't rely on the interpreter's image loader, you should use the Babel
357 * API tools, which parse the image file directly. This routine is
358 * intended as a lightweight alternative to the Babel tools for use in the
359 * interpreter, where we're certainly already going to invoke the full
360 * image loader anyway.
361 */
362 char *vm_get_ifid(class CVmHostIfc *hostifc);
363
364
365 /* ------------------------------------------------------------------------ */
366 /*
367 * VM Main client services interface. Callers of the vm_run_image
368 * functions must provide an implementation of this interface.
369 */
370 class CVmMainClientIfc
371 {
372 public:
373 virtual ~CVmMainClientIfc() { }
374
375 /*
376 * Set "plain" mode. This should set the console to plain ASCII output
377 * mode, if appropriate. Note that this can be called before
378 * client_init(), and no globals are generally present at this point.
379 *
380 * In most cases, this can make a call to os_plain() to set the
381 * OS-level console to plain mode. Non-console applications generally
382 * need not do anything here at all.
383 */
384 virtual void set_plain_mode() = 0;
385
386 /*
387 * Create the main system console, if desired. This is called during
388 * VM initialization, so it is called prior to client_init(). Returns
389 * the main console object, if desired. If no main console is desired
390 * for this application, return null.
391 */
392 virtual class CVmConsoleMain *create_console(
393 struct vm_globals *globals) = 0;
394
395 /*
396 * Delete the console, if we created one. This is called during VM
397 * termination, so it's called after client_terminate(). If
398 * create_console() doesn't create a console, this routine need do
399 * nothing.
400 */
401 virtual void delete_console(struct vm_globals *globals,
402 class CVmConsoleMain *console) = 0;
403
404 /*
405 * Initialization - we'll invoke this immediately after initializing
406 * the VM (via vm_initialize), so the client can perform any global
407 * initialization desired. The globals are valid at this point because
408 * we have completed VM initialization.
409 *
410 * If script_file is non-null, it gives the name of a file to use as
411 * the source of console input. The client implementation should set
412 * up accordingly; if the standard console (G_console) is being used,
413 * the client can simply use G_console->open_script_file() to set up
414 * scripting.
415 *
416 * If log_file is non-null, it gives the name of a file to use to log
417 * console output. The client shoudl set up logging; if the standard
418 * console if being used, G_console->open_log_file() will do the trick.
419 *
420 * If cmd_log_file is non-null, it gives the name of a file to use to
421 * log commands read from the input (i.e., only command input should be
422 * logged, not other console output). If the standard console is being
423 * used, G_console->open_command_log() will set things up properly.
424 *
425 * If banner_str is non-null, it gives a VM banner string that should
426 * be displayed to the user. If the standard console is being used,
427 * this can be displayed using G_console->format_text().
428 *
429 * The parameters (script_file, log_file, cmd_log_file, and the
430 * presence or absence of banner_str) are taken from the startup
431 * parameters. For a command-line version, for example, these come
432 * from command line options. So, these are necessarily passed down in
433 * some form from the client to begin with; so a client that never
434 * passes these to vm_run_image() or vm_run_image_main() doesn't need
435 * to handle these parameters at all here.
436 */
437 virtual void client_init(struct vm_globals *globals,
438 const char *script_file, int script_quiet,
439 const char *log_file,
440 const char *cmd_log_file,
441 const char *banner_str,
442 int more_mode) = 0;
443
444 /*
445 * Termination - we'll invoke this immediately before terminating the
446 * VM (via vm_terminate). Globals are still valid at this point, but
447 * will be destroyed after this returns.
448 */
449 virtual void client_terminate(struct vm_globals *globals) = 0;
450
451 /*
452 * pre-execution notification - we'll invoke this function just before
453 * starting execution in the loaded image
454 */
455 virtual void pre_exec(struct vm_globals *globals) = 0;
456
457 /*
458 * terminate - we'll invoke this just after execution in the loaded
459 * image terminates
460 */
461 virtual void post_exec(struct vm_globals *globals) = 0;
462
463 /*
464 * Terminate with error - we'll invoke this upon catching an
465 * exception that the image file doesn't handle and which thus
466 * terminates execution. Note that if this is called, post_exec()
467 * will not be called; however, if post_exec() itself throws an
468 * exception, we'll invoke this routine.
469 */
470 virtual void post_exec_err(struct vm_globals *globals) = 0;
471
472 /*
473 * Display an error message. We'll call this with a complete error
474 * message to display. Note that we won't add a newline at the end of
475 * the message, so if the message is to be displayed on a stdio-style
476 * terminal, this routine should display a newline after the message.
477 *
478 * If the implementation normally writes the text to the main output
479 * console (G_console), it must take into the account the possibility
480 * that we have not opened a system console at all (i.e., G_console
481 * could be null), or have not allocated any globals at all (i.e.,
482 * 'globals' could be null).
483 *
484 * If 'add_blank_line' is true, the implementation should add a blank
485 * line after the error, if appropriate for the display device. If
486 * we're displaying the message in an alert box on a GUI, for example,
487 * this can be ignored.
488 */
489 virtual void display_error(struct vm_globals *globals,
490 const struct CVmException *exc,
491 const char *msg, int add_blank_line) = 0;
492 };
493
494 /* ------------------------------------------------------------------------ */
495 /*
496 * Very basic client interface implementation using stdio
497 */
498 class CVmMainClientIfcStdio: public CVmMainClientIfc
499 {
500 public:
501 virtual void set_plain_mode() { }
502 virtual class CVmConsoleMain *create_console(struct vm_globals *)
503 { return 0; }
504 virtual void delete_console(struct vm_globals *, class CVmConsoleMain *) { }
505 virtual void client_init(struct vm_globals *, const char *, int,
506 const char *, const char *, const char *, int) { }
507 virtual void client_terminate(struct vm_globals *) { }
508 virtual void pre_exec(struct vm_globals *) { }
509 virtual void post_exec(struct vm_globals *) { }
510 virtual void post_exec_err(struct vm_globals *) { }
511
512 virtual void display_error(struct vm_globals *,
513 const struct CVmException *,
514 const char *msg, int add_blank_line)
515 {
516 printf(add_blank_line ? "%s\n" : "%s", msg);
517 }
518 };
519
520
521 #endif /* VMMAIN_H */
522