| 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 | vmmaincl.cpp - T3 VM main entrypoint - command-line version |
| 15 | Function |
| 16 | This is a command-line entrypoint wrapper for executing a VM program. |
| 17 | This can be used on command-line OS's (DOS, Unix, etc) where a graphical |
| 18 | user interface isn't needed, AND where the OS (or C run-time library) calls |
| 19 | 'main()' with the conventional C-style argv vector consisting of a list of |
| 20 | space delimited tokens. |
| 21 | |
| 22 | If your system doesn't do the standard argv vector construction, you must |
| 23 | replace this module with a custom, OS-specific module that constructs the |
| 24 | C-style argument vector. The OS version should obtain the name of the |
| 25 | program to run (via a file selector or by communication with the graphical |
| 26 | shell, for example), then initialize the VM, invoke vm_run_image(), then |
| 27 | terminate the VM. The OS version must, in other words, do pretty much |
| 28 | everything this version does, but should use its own OS-specific means to |
| 29 | obtain the name of the image file to run rather than reading it from |
| 30 | argc/argv. You must also synthesize an argv vector using the standard C |
| 31 | conventions, minimally specifying one argument (in addition to the standard |
| 32 | argv[0] program name argument) specifying the name of the .t3 file to run. |
| 33 | If you provide a UI to let the user specify other VM startup options and/or |
| 34 | command-line arguments to pass to the .t3 program, you'll have to |
| 35 | synthesize the argv vector with the correct switch syntax described in |
| 36 | vmmaincl.cpp. The layout of the argv vector is: [0]=t3run executable |
| 37 | filename, followed by t3run switches, the .t3 file name, and finally |
| 38 | any user arguments to pass to the .t3 file. |
| 39 | Notes |
| 40 | |
| 41 | Modified |
| 42 | 10/07/99 MJRoberts - Creation |
| 43 | */ |
| 44 | |
| 45 | #include <stdlib.h> |
| 46 | |
| 47 | #include "t3std.h" |
| 48 | #include "vmmain.h" |
| 49 | #include "vmmaincn.h" |
| 50 | #include "vmhostsi.h" |
| 51 | #include "osifcnet.h" |
| 52 | |
| 53 | |
| 54 | /* |
| 55 | * Main entrypoint |
| 56 | */ |
| 57 | int main(int argc, char **argv) |
| 58 | { |
| 59 | int stat; |
| 60 | int i; |
| 61 | CVmHostIfc *hostifc; |
| 62 | CVmMainClientConsole clientifc; |
| 63 | |
| 64 | /* |
| 65 | * Check for a "-plain" option; if it's there, set the terminal to |
| 66 | * plain text mode. We must make this check before doing anything |
| 67 | * else, because os_plain() must be called prior to os_init() if |
| 68 | * it's going to be called at all. |
| 69 | */ |
| 70 | for (i = 1 ; i < argc ; ++i) |
| 71 | { |
| 72 | if (strcmp(argv[i], "-plain") == 0) |
| 73 | { |
| 74 | /* set plain text mode in the OS layer */ |
| 75 | os_plain(); |
| 76 | |
| 77 | /* we've found what we're looking for - no need to look further */ |
| 78 | break; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Initialize the OS layer. Since this is a command-line-only |
| 84 | * implementation, there's no need to ask the OS layer to try to get us |
| 85 | * a filename to run, so pass in null for the prompt and filename |
| 86 | * buffer. |
| 87 | */ |
| 88 | os_init(&argc, argv, 0, 0, 0); |
| 89 | |
| 90 | /* install the OS break handler while we're running */ |
| 91 | os_instbrk(1); |
| 92 | |
| 93 | /* |
| 94 | * If possible, get the full path to the executable. This makes any |
| 95 | * future references to the exe file or its location independent of the |
| 96 | * working directory context. |
| 97 | */ |
| 98 | char exe[OSFNMAX]; |
| 99 | if (os_get_exe_filename(exe, sizeof(exe), argv[0]) |
| 100 | || os_get_abs_filename(exe, sizeof(exe), argv[0])) |
| 101 | argv[0] = exe; |
| 102 | |
| 103 | /* create the host interface */ |
| 104 | hostifc = new CVmHostIfcStdio(exe); |
| 105 | |
| 106 | /* invoke the basic entrypoint */ |
| 107 | stat = vm_run_image_main(&clientifc, "t3run", argc, argv, |
| 108 | TRUE, FALSE, hostifc); |
| 109 | |
| 110 | #ifdef TADSNET |
| 111 | /* |
| 112 | * Disconnect the Web UI, if applicable. Leave any final UI window |
| 113 | * state displayed until the user manually closes it, so that the user |
| 114 | * can read any final messages displayed when the game program |
| 115 | * terminated. |
| 116 | */ |
| 117 | osnet_disconnect_webui(FALSE); |
| 118 | |
| 119 | /* shut down the network layer, if applicable */ |
| 120 | os_net_cleanup(); |
| 121 | #endif |
| 122 | |
| 123 | /* remove the OS break handler */ |
| 124 | os_instbrk(0); |
| 125 | |
| 126 | /* uninitialize the OS layer */ |
| 127 | os_uninit(); |
| 128 | |
| 129 | /* delete the host interface object */ |
| 130 | delete hostifc; |
| 131 | |
| 132 | /* show any unfreed memory */ |
| 133 | t3_list_memory_blocks(0); |
| 134 | |
| 135 | /* exit with status code */ |
| 136 | os_term(stat); |
| 137 | |
| 138 | /* we shouldn't get here, but in case os_term doesn't really exit... */ |
| 139 | AFTER_OS_TERM(return stat;) |
| 140 | } |
| 141 | |
| 142 | /* |
| 143 | * For command-line builds, we don't have any special UI setup that depends |
| 144 | * on the loaded intrinsics, so we can stub out this routine. |
| 145 | */ |
| 146 | void os_init_ui_after_load(class CVmBifTable *, class CVmMetaTable *) |
| 147 | { |
| 148 | } |