Debian Codegraph @

terps/tads/glk/t23run.cpp

raw symbols SCIP
1 /******************************************************************************
2 * *
3 * Copyright (C) 2006-2009 by Tor Andersson. *
4 * *
5 * This file is part of Gargoyle. *
6 * *
7 * Gargoyle is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 * Gargoyle is distributed in the hope that it will be useful, *
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
15 * GNU General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU General Public License *
18 * along with Gargoyle; if not, write to the Free Software *
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA *
20 * *
21 *****************************************************************************/
22
23 /*
24 t23run.cpp - "command line" entrypoint for combined TADS 2/3 interpreter
25 */
26
27 #include <stdlib.h>
28 #include <stdio.h>
29
30 #include "os.h"
31 #include <string.h>
32
33 /* tads 2 headers */
34 #include "trd.h"
35
36 /* tads 3 headers */
37 #include "t3std.h"
38 #include "vmmain.h"
39 #include "vmvsn.h"
40 #include "vmmaincn.h"
41 #include "vmhostsi.h"
42
43 /* ------------------------------------------------------------------------ */
44
45 extern "C"
46 {
47 #include "glk.h"
48 #include "glkstart.h"
49 }
50
51 int tads_argc;
52 char **tads_argv;
53
54 glkunix_argumentlist_t glkunix_arguments[] =
55 {
56 { (char *) "", glkunix_arg_ValueFollows, (char *) "filename: The game file to load." },
57 { NULL, glkunix_arg_End, NULL }
58 };
59
60 extern "C" int glkunix_startup_code(glkunix_startup_t *data)
61 {
62 tads_argc = data->argc;
63 tads_argv = data->argv;
64 return TRUE;
65 }
66
67 /* ------------------------------------------------------------------------ */
68 /*
69 * Invoke the tads 2 main entrypoint with the given command-line arguments
70 */
71 static int main_t2(int argc, char **argv)
72 {
73 int stat;
74
75 #ifdef GARGLK
76 garglk_set_program_name("TADS " TADS_RUNTIME_VERSION);
77 char *s;
78 s = strrchr(argv[1], '/');
79 if (!s) s = strrchr(argv[1], '\\');
80 garglk_set_story_name(s ? s + 1 : argv[1]);
81 #endif
82
83 /* initialize the OS layer */
84 os_init(&argc, argv, 0, 0, 0);
85
86 /* install the break handler */
87 os_instbrk(1);
88
89 /* invoke the tads 2 main entrypoint */
90 stat = os0main2(argc, argv, trdmain, "", 0, 0);
91
92 /* done with the break handler */
93 os_instbrk(0);
94
95 /* uninitialize the OS layer */
96 os_uninit();
97
98 /* return the status */
99 return stat;
100 }
101
102 /* ------------------------------------------------------------------------ */
103 /*
104 * Invoke the T3 VM with the given command-line arguments
105 */
106 static int main_t3(int argc, char **argv)
107 {
108 CVmMainClientConsole clientifc;
109 int stat;
110 CVmHostIfc *hostifc = new CVmHostIfcStdio(argv[0]);
111
112 #ifdef GARGLK
113 garglk_set_program_name("TADS " T3VM_VSN_STRING);
114 char *s;
115 s = strrchr(argv[1], '/');
116 if (!s) s = strrchr(argv[1], '\\');
117 garglk_set_story_name(s ? s + 1 : argv[1]);
118 #endif
119
120 /*
121 * Initialize the OS layer. Since this is a command-line-only
122 * implementation, there's no need to ask the OS layer to try to get
123 * us a filename to run, so pass in null for the prompt and filename
124 * buffer.
125 */
126 os_init(&argc, argv, 0, 0, 0);
127
128 /* invoke the basic entrypoint */
129 stat = vm_run_image_main(&clientifc, "t3run", argc, argv,
130 TRUE, FALSE, hostifc);
131
132 /* uninitialize the OS layer */
133 os_uninit();
134
135 /* done with the host interface */
136 delete hostifc;
137
138 /* show any unfreed memory */
139 t3_list_memory_blocks(0);
140
141 /* exit with status code */
142 os_term(stat);
143
144 /* we shouldn't get here, but in case os_term doesn't really exit... */
145 return stat;
146 }
147
148 /* ------------------------------------------------------------------------ */
149 /*
150 * Main entrypoint
151 */
152
153 void glk_main(void)
154 {
155 int stat;
156 static const char *defexts[] = { "gam", "t3" };
157 char prog_arg[OSFNMAX];
158 char fname[OSFNMAX];
159 int engine_ver;
160
161 int argc = tads_argc;
162 char **argv = tads_argv;
163
164 winid_t mainwin;
165 char buf[256];
166 char copyright[512];
167 char errorload[512];
168
169 #ifdef GARGLK
170 garglk_set_program_name("TADS " TADS_RUNTIME_VERSION " / " T3VM_VSN_STRING);
171 garglk_set_program_info(
172 "TADS Interpreter by Michael J. Roberts\n"
173 "TADS 2 VM version " TADS_RUNTIME_VERSION "\n"
174 "T3 VM version " T3VM_VSN_STRING "\n"
175 "GlkTADS port by Tor Andersson\n"
176 );
177 #endif
178
179 /*
180 * if one of our special usage message arguments was given, show the
181 * usage
182 */
183 if (argc == 2 && stricmp(argv[1], "-help2") == 0)
184 {
185 /* invoke the tads 2 main entrypoint with no arguments */
186 main_t2(1, argv);
187
188 /* that's all we need to do with this option */
189 os_term(OSEXSUCC);
190 }
191 else if (argc == 2 && stricmp(argv[1], "-help3") == 0)
192 {
193 /* invoke the tads 3 main entrypoint with no arguments */
194 main_t3(1, argv);
195
196 /* that's all we need to do with this option */
197 os_term(OSEXSUCC);
198 }
199
200 /* look at the arguments and try to find the program name */
201 if (!vm_get_game_arg(argc, argv, prog_arg, sizeof(prog_arg), &engine_ver))
202 {
203 /*
204 * there's no game file name specified or implied - show the
205 * generic combined v2/v3 usage message
206 */
207 mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
208 glk_set_window(mainwin);
209
210 /* copyright-date-string */
211 sprintf(copyright,
212 "TADS Interpreter - "
213 "Copyright (c) 1993, 2004 Michael J. Roberts\n"
214 "TADS 2 VM version " TADS_RUNTIME_VERSION " / "
215 "T3 VM version " T3VM_VSN_STRING "\n\n");
216 glk_put_string(copyright);
217
218 sprintf(errorload,
219 "Error: you didn't specify a game file name on the command "
220 "line, or the command "
221 "options are incorrect. You must specify the name of "
222 "the game file you would "
223 "like to run.\n"
224 "\n"
225 "If you'd like a list of command-line options for TADS 2 "
226 "games, specify -help2 "
227 "instead of giving a game file name. Or, if you'd like a "
228 "list of command-line "
229 "options for TADS 3, specify -help3.\n");
230 glk_put_string(errorload);
231
232 /* pause (if desired by OS layer) and exit */
233 os_expause();
234 os_term(OSEXFAIL);
235 }
236
237 /* determine the type of the game we have */
238 engine_ver = vm_get_game_type(prog_arg, fname, sizeof(fname),
239 defexts,
240 sizeof(defexts)/sizeof(defexts[0]));
241
242 /* presume failure */
243 stat = OSEXFAIL;
244
245 /* see what we have */
246 switch(engine_ver)
247 {
248 case VM_GGT_TADS2:
249 /* run the game using the TADS 2 engine */
250 stat = main_t2(argc, argv);
251 break;
252
253 case VM_GGT_TADS3:
254 /* run the game using the TADS 3 engine */
255 stat = main_t3(argc, argv);
256 break;
257
258 case VM_GGT_INVALID:
259 /* invalid file type */
260 sprintf(buf,
261 "The file you have selected (%s) is not a valid game file.\n",
262 fname);
263 mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
264 glk_set_window(mainwin);
265 glk_put_string(buf);
266 break;
267
268 case VM_GGT_NOT_FOUND:
269 /* file not found */
270 sprintf(buf, "The game file (%s) cannot be found.\n", prog_arg);
271 mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
272 glk_set_window(mainwin);
273 glk_put_string(buf);
274 break;
275
276 case VM_GGT_AMBIG:
277 /* ambiguous file */
278 sprintf(buf,"The game file (%s) cannot be found exactly as given, "
279 "but multiple game "
280 "files with this name and different default suffixes "
281 "(.gam, .t3) exist. "
282 "Please specify the full name of the file, including the "
283 "suffix, that you "
284 "wish to use.\n",
285 prog_arg);
286 mainwin = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
287 glk_set_window(mainwin);
288 glk_put_string(buf);
289 break;
290 }
291
292 /* pause (if desired by OS layer) and terminate with our status code */
293 os_expause();
294 os_term(stat);
295 }
296