| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 1999, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmcl23.cpp - command line entrypoint for combined TADS 2/3 interpreter |
| 15 | Function |
| 16 | This is a command-line entrypoint wrapper for executing a compiled |
| 17 | TADS 2 or TADS 3 program. This can be used on command-line OS's (DOS, |
| 18 | Unix, etc) where a graphical user interface is not needed. |
| 19 | |
| 20 | If a graphical user interface to execute a VM program is required, |
| 21 | this module should not be used. Instead, an OS-specific version must be |
| 22 | written; the OS version obtain the name of the program to run (via a file |
| 23 | selector or by communication with the graphical shell, for example), then |
| 24 | must initialize the VM, invoke vm_run_image(), then terminate the VM. The |
| 25 | OS version must, in other words, do pretty much everything this version |
| 26 | does, but should use its own OS-specific means to obtain the name of the |
| 27 | image file to run rather than reading it from argc/argv. |
| 28 | Notes |
| 29 | |
| 30 | Modified |
| 31 | 10/07/99 MJRoberts - Creation |
| 32 | */ |
| 33 | |
| 34 | #include <stdlib.h> |
| 35 | #include <stdio.h> |
| 36 | |
| 37 | #include "os.h" |
| 38 | |
| 39 | /* tads 2 headers */ |
| 40 | #include "trd.h" |
| 41 | |
| 42 | /* tads 3 headers */ |
| 43 | #include "t3std.h" |
| 44 | #include "vmmain.h" |
| 45 | #include "vmvsn.h" |
| 46 | #include "vmmaincn.h" |
| 47 | #include "vmhostsi.h" |
| 48 | #include "osifcnet.h" |
| 49 | |
| 50 | |
| 51 | /* ------------------------------------------------------------------------ */ |
| 52 | /* |
| 53 | * Check for a "-plain" option; if it's there, set the terminal to plain |
| 54 | * text mode. We must make this check before doing anything else, because |
| 55 | * os_plain() must be called prior to os_init() if it's going to be called |
| 56 | * at all. |
| 57 | */ |
| 58 | static void check_plain_option(int argc, char **argv) |
| 59 | { |
| 60 | int i; |
| 61 | |
| 62 | /* scan the argument list for the "-plain" option */ |
| 63 | for (i = 1 ; i < argc ; ++i) |
| 64 | { |
| 65 | if (strcmp(argv[i], "-plain") == 0) |
| 66 | { |
| 67 | /* set plain text mode in the OS layer */ |
| 68 | os_plain(); |
| 69 | |
| 70 | /* we've found what we're looking for - no need to look further */ |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | |
| 77 | /* ------------------------------------------------------------------------ */ |
| 78 | /* |
| 79 | * Invoke the tads 2 main entrypoint with the given command-line arguments |
| 80 | */ |
| 81 | static int main_t2(int argc, char **argv) |
| 82 | { |
| 83 | int stat; |
| 84 | |
| 85 | /* check for a "-plain" option */ |
| 86 | check_plain_option(argc, argv); |
| 87 | |
| 88 | /* initialize the OS layer */ |
| 89 | os_init(&argc, argv, 0, 0, 0); |
| 90 | |
| 91 | /* install the break handler */ |
| 92 | os_instbrk(1); |
| 93 | |
| 94 | /* invoke the tads 2 main entrypoint */ |
| 95 | stat = os0main2(argc, argv, trdmain, "", 0, 0); |
| 96 | |
| 97 | #ifdef TADSNET |
| 98 | /* shut down the network layer */ |
| 99 | os_net_cleanup(); |
| 100 | #endif |
| 101 | |
| 102 | /* done with the break handler */ |
| 103 | os_instbrk(0); |
| 104 | |
| 105 | /* uninitialize the OS layer */ |
| 106 | os_uninit(); |
| 107 | |
| 108 | /* return the status */ |
| 109 | return stat; |
| 110 | } |
| 111 | |
| 112 | /* ------------------------------------------------------------------------ */ |
| 113 | /* |
| 114 | * Invoke the T3 VM with the given command-line arguments |
| 115 | */ |
| 116 | static int main_t3(int argc, char **argv) |
| 117 | { |
| 118 | CVmMainClientConsole clientifc; |
| 119 | int stat; |
| 120 | CVmHostIfc *hostifc = new CVmHostIfcStdio(argv[0]); |
| 121 | |
| 122 | /* check for a "-plain" option */ |
| 123 | check_plain_option(argc, argv); |
| 124 | |
| 125 | /* |
| 126 | * Initialize the OS layer. Since this is a command-line-only |
| 127 | * implementation, there's no need to ask the OS layer to try to get |
| 128 | * us a filename to run, so pass in null for the prompt and filename |
| 129 | * buffer. |
| 130 | */ |
| 131 | os_init(&argc, argv, 0, 0, 0); |
| 132 | |
| 133 | /* invoke the basic entrypoint */ |
| 134 | stat = vm_run_image_main(&clientifc, "t3run", argc, argv, |
| 135 | TRUE, FALSE, hostifc); |
| 136 | |
| 137 | #ifdef TADSNET |
| 138 | /* shut down the network layer */ |
| 139 | os_net_cleanup(); |
| 140 | #endif |
| 141 | |
| 142 | /* uninitialize the OS layer */ |
| 143 | os_uninit(); |
| 144 | |
| 145 | /* done with the host interface */ |
| 146 | delete hostifc; |
| 147 | |
| 148 | /* show any unfreed memory */ |
| 149 | t3_list_memory_blocks(0); |
| 150 | |
| 151 | /* exit with status code */ |
| 152 | os_term(stat); |
| 153 | |
| 154 | /* we shouldn't get here, but in case os_term doesn't really exit... */ |
| 155 | AFTER_OS_TERM(return stat;) |
| 156 | } |
| 157 | |
| 158 | /* ------------------------------------------------------------------------ */ |
| 159 | /* |
| 160 | * Main entrypoint |
| 161 | */ |
| 162 | int main(int argc, char **argv) |
| 163 | { |
| 164 | int stat; |
| 165 | static const char *defexts[] = { "gam", "t3" }; |
| 166 | char prog_arg[OSFNMAX]; |
| 167 | char fname[OSFNMAX]; |
| 168 | int engine_ver; |
| 169 | |
| 170 | /* |
| 171 | * if one of our special usage message arguments was given, show the |
| 172 | * usage |
| 173 | */ |
| 174 | if (argc == 2 && stricmp(argv[1], "-help2") == 0) |
| 175 | { |
| 176 | /* invoke the tads 2 main entrypoint with no arguments */ |
| 177 | main_t2(1, argv); |
| 178 | |
| 179 | /* that's all we need to do with this option */ |
| 180 | AFTER_OS_TERM(os_term(OSEXSUCC);) |
| 181 | } |
| 182 | else if (argc == 2 && stricmp(argv[1], "-help3") == 0) |
| 183 | { |
| 184 | /* invoke the tads 3 main entrypoint with no arguments */ |
| 185 | main_t3(1, argv); |
| 186 | |
| 187 | /* that's all we need to do with this option */ |
| 188 | AFTER_OS_TERM(os_term(OSEXSUCC);) |
| 189 | } |
| 190 | |
| 191 | /* look at the arguments and try to find the program name */ |
| 192 | if (!vm_get_game_arg(argc, argv, prog_arg, sizeof(prog_arg), &engine_ver)) |
| 193 | { |
| 194 | /* |
| 195 | * there's no game file name specified or implied - show the |
| 196 | * generic combined v2/v3 usage message |
| 197 | */ |
| 198 | /* copyright-date-string */ |
| 199 | printf("TADS Interpreter - " |
| 200 | "Copyright (c) 1993, 2012 Michael J. Roberts\n" |
| 201 | "TADS 2 VM version " TADS_RUNTIME_VERSION " / " |
| 202 | "T3 VM version " T3VM_VSN_STRING "\n\n"); |
| 203 | |
| 204 | printf("Error: you didn't specify a game file name on the command " |
| 205 | "line, or the command\n" |
| 206 | "options are incorrect. You must specify the name of " |
| 207 | "the game file you would\n" |
| 208 | "like to run.\n" |
| 209 | "\n" |
| 210 | "If you'd like a list of command-line options for TADS 2 " |
| 211 | "games, specify -help2\n" |
| 212 | "instead of giving a game file name. Or, if you'd like a " |
| 213 | "list of command-line\n" |
| 214 | "options for TADS 3, specify -help3.\n"); |
| 215 | |
| 216 | /* pause (if desired by OS layer) and exit */ |
| 217 | os_expause(); |
| 218 | os_term(OSEXFAIL); |
| 219 | } |
| 220 | |
| 221 | /* determine the type of the game we have */ |
| 222 | if (prog_arg[0] != '\0') |
| 223 | engine_ver = vm_get_game_type(prog_arg, fname, sizeof(fname), |
| 224 | defexts, |
| 225 | sizeof(defexts)/sizeof(defexts[0])); |
| 226 | |
| 227 | /* presume failure */ |
| 228 | stat = OSEXFAIL; |
| 229 | |
| 230 | /* see what we have */ |
| 231 | switch(engine_ver) |
| 232 | { |
| 233 | case VM_GGT_TADS2: |
| 234 | /* run the game using the TADS 2 engine */ |
| 235 | stat = main_t2(argc, argv); |
| 236 | break; |
| 237 | |
| 238 | case VM_GGT_TADS3: |
| 239 | /* run the game using the TADS 3 engine */ |
| 240 | AFTER_OS_TERM(stat =) main_t3(argc, argv); |
| 241 | AFTER_OS_TERM(break;) |
| 242 | |
| 243 | case VM_GGT_INVALID: |
| 244 | /* invalid file type */ |
| 245 | printf("The file you have selected (%s) is not a valid game file.\n", |
| 246 | fname); |
| 247 | break; |
| 248 | |
| 249 | case VM_GGT_NOT_FOUND: |
| 250 | /* file not found */ |
| 251 | printf("The game file (%s) cannot be found.\n", prog_arg); |
| 252 | break; |
| 253 | |
| 254 | case VM_GGT_AMBIG: |
| 255 | /* ambiguous file */ |
| 256 | printf("The game file (%s) cannot be found exactly as given, " |
| 257 | "but multiple game\n" |
| 258 | "files with this name and different default suffixes " |
| 259 | "(.gam, .t3) exist.\n" |
| 260 | "Please specify the full name of the file, including the " |
| 261 | "suffix, that you\n" |
| 262 | "wish to use.\n", |
| 263 | prog_arg); |
| 264 | break; |
| 265 | } |
| 266 | |
| 267 | /* pause (if desired by OS layer) and terminate with our status code */ |
| 268 | os_expause(); |
| 269 | os_term(stat); |
| 270 | AFTER_OS_TERM(return stat;) |
| 271 | } |
| 272 | |
| 273 | /* |
| 274 | * For command-line builds, we don't have any special UI setup that depends |
| 275 | * on the loaded intrinsics, so we can stub out this routine. |
| 276 | */ |
| 277 | void os_init_ui_after_load(class CVmBifTable *, class CVmMetaTable *) |
| 278 | { |
| 279 | } |