Debian Codegraph @

terps/scare/os_glk.c

raw symbols SCIP
1 /* vi: set ts=2 shiftwidth=2 expandtab:
2 *
3 * Copyright (C) 2003-2008 Simon Baldwin and Mark J. Tilford
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
17 * USA
18 */
19
20 /*
21 * Module notes:
22 *
23 * o The Glk interface makes no effort to set text colors, background colors,
24 * and so forth, and minimal effort to set fonts and other style effects.
25 */
26
27 #include <assert.h>
28 #include <ctype.h>
29 #include <errno.h>
30 #include <limits.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include "scare.h"
36 #include "glk.h"
37
38 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
39 #include "gi_blorb.h"
40
41 char gamefile[1024];
42
43 static const char *find_last_of(const char *str, const char *chars)
44 {
45 const char *found = NULL;
46 while (*chars != 0) {
47 const char *p = strrchr(str, *chars++);
48 if (p != NULL && (found == NULL || p > found)) {
49 found = p;
50 }
51 }
52
53 return found;
54 }
55 #endif
56
57 #include "scprotos.h" /* for SCARE_VERSION */
58
59 /*
60 * True and false definitions -- usually defined in glkstart.h, but we need
61 * them early, so we'll define them here too. We also need NULL, but that's
62 * normally from stdio.h or one of it's cousins.
63 */
64 #ifndef FALSE
65 # define FALSE 0
66 #endif
67 #ifndef TRUE
68 # define TRUE (!FALSE)
69 #endif
70
71
72 /*---------------------------------------------------------------------*/
73 /* Module variables, miscellaneous other stuff */
74 /*---------------------------------------------------------------------*/
75
76 /* Glk SCARE interface version number. */
77 static const glui32 GSC_PORT_VERSION = 0x00010310;
78
79 /* Two windows, one for the main text, and one for a status line. */
80 static winid_t gsc_main_window = NULL,
81 gsc_status_window = NULL;
82
83 /*
84 * Transcript stream and input log. These are NULL if there is no current
85 * collection of these strings.
86 */
87 static strid_t gsc_transcript_stream = NULL,
88 gsc_inputlog_stream = NULL;
89
90 /* Input read log stream, for reading back an input log. */
91 static strid_t gsc_readlog_stream = NULL;
92
93 /* Options that may be turned off or set by command line flags. */
94 static int gsc_commands_enabled = TRUE,
95 gsc_abbreviations_enabled = TRUE,
96 gsc_unicode_enabled = TRUE;
97
98 /* Adrift game to interpret. */
99 static sc_game gsc_game = NULL;
100
101 /* Special out-of-band os_confirm() options used locally with os_glk. */
102 static const sc_int GSC_CONF_SUBTLE_HINT = INT_MAX,
103 GSC_CONF_UNSUBTLE_HINT = INT_MAX - 1,
104 GSC_CONF_CONTINUE_HINTS = INT_MAX - 2;
105
106 /* Forward declaration of event wait functions, and a short delay. */
107 static void gsc_event_wait_2 (glui32 wait_type_1,
108 glui32 wait_type_2, event_t *event);
109 static void gsc_event_wait (glui32 wait_type, event_t *event);
110 static void gsc_short_delay (void);
111
112
113 /*---------------------------------------------------------------------*/
114 /* Glk port utility functions */
115 /*---------------------------------------------------------------------*/
116
117 /*
118 * gsc_fatal()
119 *
120 * Fatal error handler. The function returns, expecting the caller to
121 * abort() or otherwise handle the error.
122 */
123 static void
124 gsc_fatal (const char *string)
125 {
126 /*
127 * If the failure happens too early for us to have a window, print
128 * the message to stderr.
129 */
130 if (!gsc_main_window)
131 {
132 fprintf (stderr, "\n\nINTERNAL ERROR: %s\n", string);
133
134 fprintf (stderr, "\nPlease record the details of this error, try to"
135 " note down everything you did to cause it, and email"
136 " this information to [email protected].\n\n");
137 return;
138 }
139
140 /* Cancel all possible pending window input events. */
141 glk_cancel_line_event (gsc_main_window, NULL);
142 glk_cancel_char_event (gsc_main_window);
143
144 /* Print a message indicating the error, and exit. */
145 glk_set_window (gsc_main_window);
146 glk_set_style (style_Normal);
147 glk_put_string ("\n\nINTERNAL ERROR: ");
148 glk_put_string ((char *) string);
149
150 glk_put_string ("\n\nPlease record the details of this error, try to"
151 " note down everything you did to cause it, and email"
152 " this information to [email protected].\n\n");
153 }
154
155
156 /*
157 * gsc_malloc()
158 *
159 * Non-failing malloc; call gsc_fatal and exit if memory allocation fails.
160 */
161 static void *
162 gsc_malloc (size_t size)
163 {
164 void *pointer;
165
166 pointer = malloc (size > 0 ? size : 1);
167 if (!pointer)
168 {
169 gsc_fatal ("GLK: Out of system memory");
170 glk_exit ();
171 }
172
173 return pointer;
174 }
175
176
177 /*---------------------------------------------------------------------*/
178 /* Glk port locale data */
179 /*---------------------------------------------------------------------*/
180
181 /* Unicode values up to 256 are equivalent to iso 8859-1. */
182 static const glui32 GSC_ISO_8859_EQUIVALENCE = 256;
183
184 /*
185 * Lookup table pair for converting a given single character into unicode and
186 * iso 8859-1 (the lower byte of the unicode representation, assuming an upper
187 * byte of zero), and an ascii substitute should nothing else be available.
188 * Tables are 256 elements; although the first 128 characters of a codepage
189 * are usually standard ascii, making tables full-sized allows for support of
190 * codepages where they're not (dingbats, for example).
191 */
192 enum { GSC_TABLE_SIZE = 256 };
193 typedef struct {
194 const glui32 unicode[GSC_TABLE_SIZE];
195 const sc_char *const ascii[GSC_TABLE_SIZE];
196 } gsc_codepages_t;
197
198 /*
199 * Locale contains a name and a pair of codepage structures, a main one and
200 * an alternate. The latter is intended for monospaced output.
201 */
202 typedef struct {
203 const sc_char *const name;
204 const gsc_codepages_t main;
205 const gsc_codepages_t alternate;
206 } gsc_locale_t;
207
208
209 /*
210 * Locale for Latin1 -- cp1252 and cp850.
211 *
212 * The ascii representations of characters in this table are based on the
213 * general look of the characters, rather than pronounciation. Accented
214 * characters are generally rendered unaccented, and box drawing, shading,
215 * and other non-alphanumeric glyphs as either a similar shape, or as a
216 * character that might be recognizable as what it's trying to emulate.
217 */
218 static const gsc_locale_t GSC_LATIN1_LOCALE = {
219 "Latin1",
220 /* cp1252 to unicode. */
221 { { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
222 0x0000, 0x000a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
223 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
224 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0021, 0x0022, 0x0023,
225 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c,
226 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
227 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e,
228 0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
229 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 0x0050,
230 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
231 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 0x0060, 0x0061, 0x0062,
232 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b,
233 0x006c, 0x006d, 0x006e, 0x006f, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
234 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d,
235 0x007e, 0x0000, 0x20ac, 0x0000, 0x201a, 0x0192, 0x201e, 0x2026, 0x2020,
236 0x2021, 0x02c6, 0x2030, 0x0160, 0x2039, 0x0152, 0x0000, 0x017d, 0x0000,
237 0x0000, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x02dc,
238 0x2122, 0x0161, 0x203a, 0x0153, 0x0000, 0x017e, 0x0178, 0x00a0, 0x00a1,
239 0x00a2, 0x00a3, 0x00a4, 0x00a5, 0x00a6, 0x00a7, 0x00a8, 0x00a9, 0x00aa,
240 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x00af, 0x00b0, 0x00b1, 0x00b2, 0x00b3,
241 0x00b4, 0x00b5, 0x00b6, 0x00b7, 0x00b8, 0x00b9, 0x00ba, 0x00bb, 0x00bc,
242 0x00bd, 0x00be, 0x00bf, 0x00c0, 0x00c1, 0x00c2, 0x00c3, 0x00c4, 0x00c5,
243 0x00c6, 0x00c7, 0x00c8, 0x00c9, 0x00ca, 0x00cb, 0x00cc, 0x00cd, 0x00ce,
244 0x00cf, 0x00d0, 0x00d1, 0x00d2, 0x00d3, 0x00d4, 0x00d5, 0x00d6, 0x00d7,
245 0x00d8, 0x00d9, 0x00da, 0x00db, 0x00dc, 0x00dd, 0x00de, 0x00df, 0x00e0,
246 0x00e1, 0x00e2, 0x00e3, 0x00e4, 0x00e5, 0x00e6, 0x00e7, 0x00e8, 0x00e9,
247 0x00ea, 0x00eb, 0x00ec, 0x00ed, 0x00ee, 0x00ef, 0x00f0, 0x00f1, 0x00f2,
248 0x00f3, 0x00f4, 0x00f5, 0x00f6, 0x00f7, 0x00f8, 0x00f9, 0x00fa, 0x00fb,
249 0x00fc, 0x00fd, 0x00fe, 0x00ff },
250 /* cp1252 to ascii. */
251 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, " ",
252 "\n", NULL, NULL, "\n", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
253 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
254 " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*",
255 "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5",
256 "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@",
257 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
258 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
259 "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a",
260 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
261 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
262 "x", "y", "z", "{", "|", "}", "~", NULL, "E", NULL, ",",
263 "f", ",,", "...", "+", "#", "^", "%", "S", "<", "OE", NULL,
264 "Z", NULL, NULL, "'", "'", "\"", "\"", "*", "-", "-", "~",
265 "[TM]","s", ">", "oe", NULL, "z", "Y", " ", "!", "c", "GBP",
266 "*", "Y", "|", "S", "\"", "(C)", "a", "<<", "-", "-", "(R)",
267 "-", "o", "+/-", "2", "3", "'", "u", "P", "*", ",", "1",
268 "o", ">>", "1/4", "1/2", "3/4", "?", "A", "A", "A", "A", "A",
269 "A", "AE", "C", "E", "E", "E", "E", "I", "I", "I", "I",
270 "D", "N", "O", "O", "O", "O", "O", "x", "O", "U", "U",
271 "U", "U", "Y", "p", "ss", "a", "a", "a", "a", "a", "a",
272 "ae", "c", "e", "e", "e", "e", "i", "i", "i", "i", "d",
273 "n", "o", "o", "o", "o", "o", "/", "o", "u", "u", "u",
274 "u", "y", "P", "y" } },
275 /* cp850 to unicode. */
276 { { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
277 0x0000, 0x000a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
278 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
279 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0021, 0x0022, 0x0023,
280 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c,
281 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
282 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e,
283 0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
284 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 0x0050,
285 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
286 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 0x0060, 0x0061, 0x0062,
287 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b,
288 0x006c, 0x006d, 0x006e, 0x006f, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
289 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d,
290 0x007e, 0x0000, 0x00c7, 0x00fc, 0x00e9, 0x00e2, 0x00e4, 0x00e0, 0x00e5,
291 0x00e7, 0x00ea, 0x00eb, 0x00e8, 0x00ef, 0x00ee, 0x00ec, 0x00c4, 0x00c5,
292 0x00c9, 0x00e6, 0x00c6, 0x00f4, 0x00f6, 0x00f2, 0x00fb, 0x00f9, 0x00ff,
293 0x00d6, 0x00dc, 0x00f8, 0x00a3, 0x00d8, 0x00d7, 0x0192, 0x00e1, 0x00ed,
294 0x00f3, 0x00fa, 0x00f1, 0x00d1, 0x00aa, 0x00ba, 0x00bf, 0x00ae, 0x00ac,
295 0x00bd, 0x00bc, 0x00a1, 0x00ab, 0x00bb, 0x2591, 0x2592, 0x2593, 0x2502,
296 0x2524, 0x00c1, 0x00c2, 0x00c0, 0x00a9, 0x2563, 0x2551, 0x2557, 0x255d,
297 0x00a2, 0x00a5, 0x2510, 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c,
298 0x00e3, 0x00c3, 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c,
299 0x00a4, 0x00f0, 0x00d0, 0x00ca, 0x00cb, 0x00c8, 0x0131, 0x00cd, 0x00ce,
300 0x00cf, 0x2518, 0x250c, 0x2588, 0x2584, 0x00a6, 0x00cc, 0x2580, 0x00d3,
301 0x00df, 0x00d4, 0x00d2, 0x00f5, 0x00d5, 0x00b5, 0x00fe, 0x00de, 0x00da,
302 0x00db, 0x00d9, 0x00fd, 0x00dd, 0x00af, 0x00b4, 0x00ad, 0x00b1, 0x2017,
303 0x00be, 0x00b6, 0x00a7, 0x00f7, 0x00b8, 0x00b0, 0x00a8, 0x00b7, 0x00b9,
304 0x00b3, 0x00b2, 0x25a0, 0x00a0 },
305 /* cp850 to ascii. */
306 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, " ",
307 "\n", NULL, NULL, "\n", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
308 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
309 " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*",
310 "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5",
311 "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@",
312 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
313 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
314 "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a",
315 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
316 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
317 "x", "y", "z", "{", "|", "}", "~", NULL, "C", "u", "e",
318 "a", "a", "a", "a", "c", "e", "e", "e", "i", "i", "i",
319 "A", "A", "E", "ae", "AE", "o", "o", "o", "u", "u", "y",
320 "O", "U", "o", "GBP", "O", "x", "f", "a", "i", "o", "u",
321 "n", "N", "a", "o", "?", "(R)", "-", "1/2", "1/4", "i", "<<",
322 ">>", "#", "#", "#", "|", "+", "A", "A", "A", "(C)", "+",
323 "|", "+", "+", "c", "Y", "+", "+", "+", "+", "+", "-",
324 "+", "a", "A", "+", "+", "+", "+", "+", "=", "+", "*",
325 "d", "D", "E", "E", "E", "i", "I", "I", "I", "+", "+",
326 ".", ".", "|", "I", ".", "O", "ss", "O", "O", "o", "O",
327 "u", "p", "P", "U", "U", "U", "y", "Y", "-", "'", "-",
328 "+/-", "=", "3/4", "P", "S", "/", ",", "deg", "\"", "*", "1",
329 "3", "2", ".", " " } }
330 };
331
332
333 /*
334 * Locale for Cyrillic -- cp1251 and cp866.
335 *
336 * The ascii representations in this table, for alphabetic characters, follow
337 * linguistic rather than appearance rules, the essence of gost 16876-71.
338 * Capitalized cyrillic letters that translate to multiple ascii characters
339 * have the first ascii character only of the sequence translated. This gives
340 * the best appearance in normal sentences, but is not optimal in a run of
341 * all capitals (headings, for example). For non-alphanumeric characters,
342 * the general appearance and shape of the character being emulated is used.
343 */
344 static const gsc_locale_t GSC_CYRILLIC_LOCALE = {
345 "Cyrillic",
346 /* cp1251 to unicode. */
347 { { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
348 0x0000, 0x000a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
349 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
350 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0021, 0x0022, 0x0023,
351 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c,
352 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
353 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e,
354 0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
355 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 0x0050,
356 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
357 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 0x0060, 0x0061, 0x0062,
358 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b,
359 0x006c, 0x006d, 0x006e, 0x006f, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
360 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d,
361 0x007e, 0x0000, 0x0402, 0x0403, 0x201a, 0x0453, 0x201e, 0x2026, 0x2020,
362 0x2021, 0x20ac, 0x2030, 0x0409, 0x2039, 0x040a, 0x040c, 0x040b, 0x040f,
363 0x0452, 0x2018, 0x2019, 0x201c, 0x201d, 0x2022, 0x2013, 0x2014, 0x0000,
364 0x2122, 0x0459, 0x203a, 0x045a, 0x045c, 0x045b, 0x045f, 0x00a0, 0x040e,
365 0x045e, 0x0408, 0x00a4, 0x0490, 0x00a6, 0x00a7, 0x0401, 0x00a9, 0x0404,
366 0x00ab, 0x00ac, 0x00ad, 0x00ae, 0x0407, 0x00b0, 0x00b1, 0x0406, 0x0456,
367 0x0491, 0x00b5, 0x00b6, 0x00b7, 0x0451, 0x2116, 0x0454, 0x00bb, 0x0458,
368 0x0405, 0x0455, 0x0457, 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415,
369 0x0416, 0x0417, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
370 0x041f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427,
371 0x0428, 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, 0x0430,
372 0x0431, 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439,
373 0x043a, 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, 0x0440, 0x0441, 0x0442,
374 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449, 0x044a, 0x044b,
375 0x044c, 0x044d, 0x044e, 0x044f },
376 /* cp1251 to gost 16876-71 ascii. */
377 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, " ",
378 "\n", NULL, NULL, "\n", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
379 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
380 " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*",
381 "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5",
382 "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@",
383 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
384 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
385 "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a",
386 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
387 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
388 "x", "y", "z", "{", "|", "}", "~", NULL, NULL, NULL, ",",
389 NULL, ",,", "...", "+", "#", "E", "%", NULL, "<", NULL, NULL,
390 NULL, NULL, NULL, "'", "'", "\"", "\"", "*", "-", "-", NULL,
391 "[TM]",NULL, ">", NULL, NULL, NULL, NULL, " ", NULL, NULL, NULL,
392 "*", "G", "|", "S", "Jo", "(C)", "Je", "<<", "-", "-", "(R)",
393 "Ji", "o", "+/-", "I", "i", "g", "u", "P", "*", "jo", NULL,
394 "je", ">>", "j", "S", "s", "ji", "A", "B", "V", "G", "D",
395 "E", "Zh", "Z", "I", "Jj", "K", "L", "M", "N", "O", "P",
396 "R", "S", "T", "U", "F", "Kh", "C", "Ch", "Sh", "Shh", "\"",
397 "Y", "'", "Eh", "Ju", "Ja", "a", "b", "v", "g", "d", "e",
398 "zh", "z", "i", "jj", "k", "l", "m", "n", "o", "p", "r",
399 "s", "t", "u", "f", "kh", "c", "ch", "sh", "shh", "\"", "y",
400 "'", "eh", "ju", "ja" } },
401 /* cp866 to unicode. */
402 { { 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
403 0x0000, 0x000a, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
404 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
405 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0020, 0x0021, 0x0022, 0x0023,
406 0x0024, 0x0025, 0x0026, 0x0027, 0x0028, 0x0029, 0x002a, 0x002b, 0x002c,
407 0x002d, 0x002e, 0x002f, 0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035,
408 0x0036, 0x0037, 0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e,
409 0x003f, 0x0040, 0x0041, 0x0042, 0x0043, 0x0044, 0x0045, 0x0046, 0x0047,
410 0x0048, 0x0049, 0x004a, 0x004b, 0x004c, 0x004d, 0x004e, 0x004f, 0x0050,
411 0x0051, 0x0052, 0x0053, 0x0054, 0x0055, 0x0056, 0x0057, 0x0058, 0x0059,
412 0x005a, 0x005b, 0x005c, 0x005d, 0x005e, 0x005f, 0x0060, 0x0061, 0x0062,
413 0x0063, 0x0064, 0x0065, 0x0066, 0x0067, 0x0068, 0x0069, 0x006a, 0x006b,
414 0x006c, 0x006d, 0x006e, 0x006f, 0x0070, 0x0071, 0x0072, 0x0073, 0x0074,
415 0x0075, 0x0076, 0x0077, 0x0078, 0x0079, 0x007a, 0x007b, 0x007c, 0x007d,
416 0x007e, 0x0000, 0x0410, 0x0411, 0x0412, 0x0413, 0x0414, 0x0415, 0x0416,
417 0x0417, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e, 0x041f,
418 0x0420, 0x0421, 0x0422, 0x0423, 0x0424, 0x0425, 0x0426, 0x0427, 0x0428,
419 0x0429, 0x042a, 0x042b, 0x042c, 0x042d, 0x042e, 0x042f, 0x0430, 0x0431,
420 0x0432, 0x0433, 0x0434, 0x0435, 0x0436, 0x0437, 0x0438, 0x0439, 0x043a,
421 0x043b, 0x043c, 0x043d, 0x043e, 0x043f, 0x2591, 0x2592, 0x2593, 0x2502,
422 0x2524, 0x2561, 0x2562, 0x2556, 0x2555, 0x2563, 0x2551, 0x2557, 0x255d,
423 0x255c, 0x255b, 0x2510, 0x2514, 0x2534, 0x252c, 0x251c, 0x2500, 0x253c,
424 0x255e, 0x255f, 0x255a, 0x2554, 0x2569, 0x2566, 0x2560, 0x2550, 0x256c,
425 0x2567, 0x2568, 0x2564, 0x2565, 0x2559, 0x2558, 0x2552, 0x2553, 0x256b,
426 0x256a, 0x2518, 0x250c, 0x2588, 0x2584, 0x258c, 0x2590, 0x2580, 0x0440,
427 0x0441, 0x0442, 0x0443, 0x0444, 0x0445, 0x0446, 0x0447, 0x0448, 0x0449,
428 0x044a, 0x044b, 0x044c, 0x044d, 0x044e, 0x044f, 0x0401, 0x0451, 0x0404,
429 0x0454, 0x0407, 0x0457, 0x040e, 0x045e, 0x00b0, 0x2022, 0x00b7, 0x221a,
430 0x2116, 0x00a4, 0x25a0, 0x00a0 },
431 /* cp866 to gost 16876-71 ascii. */
432 { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, " ",
433 "\n", NULL, NULL, "\n", NULL, NULL, NULL, NULL, NULL, NULL, NULL,
434 NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
435 " ", "!", "\"", "#", "$", "%", "&", "'", "(", ")", "*",
436 "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5",
437 "6", "7", "8", "9", ":", ";", "<", "=", ">", "?", "@",
438 "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
439 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
440 "W", "X", "Y", "Z", "[", "\\", "]", "^", "_", "`", "a",
441 "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
442 "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w",
443 "x", "y", "z", "{", "|", "}", "~", NULL, "A", "B", "V",
444 "G", "D", "E", "Zh", "Z", "I", "Jj", "K", "L", "M", "N",
445 "O", "P", "R", "S", "T", "U", "F", "Kh", "C", "Ch", "Sh",
446 "Shh", "\"", "Y", "'", "Eh", "Ju", "Ja", "a", "b", "v", "g",
447 "d", "e", "zh", "z", "i", "jj", "k", "l", "m", "n", "o",
448 "p", "#", "#", "#", "|", "+", "+", "+", "+", "+", "+",
449 "|", "+", "+", "+", "+", "+", "+", "+", "+", "+", "-",
450 "+", "+", "+", "+", "+", "+", "+", "+", "|", "+", "+",
451 "+", "+", "+", "+", "+", "+", "+", "+", "+", "+", "+",
452 "+", ".", ".", ".", ".", "r", "s", "t", "u", "f", "kh",
453 "c", "ch", "sh", "shh", "\"", "y", "'", "eh", "ju", "ja", "Jo",
454 "jo", "Je", "je", "Ji", "ji", NULL, NULL, "deg", "*", "*", NULL,
455 NULL, "*", ".", " " } }
456 };
457
458
459 /*---------------------------------------------------------------------*/
460 /* Glk port locale control and conversion functions */
461 /*---------------------------------------------------------------------*/
462
463 #ifdef GLK_MODULE_UNICODE
464 static const sc_bool gsc_has_unicode = TRUE;
465 #else
466 static const sc_bool gsc_has_unicode = FALSE;
467
468 /* Gestalt selector and stubs for non-unicode capable libraries. */
469 static const glui32 gestalt_Unicode = 15;
470
471 static void
472 glk_put_char_uni (glui32 ch)
473 {
474 glui32 unused;
475 unused = ch;
476 gsc_fatal ("GLK: Stub unicode function called");
477 }
478
479 static void
480 glk_request_line_event_uni (winid_t win,
481 glui32 *buf, glui32 maxlen, glui32 initlen)
482 {
483 winid_t unused1;
484 glui32 *unused2;
485 glui32 unused3, unused4;
486 unused1 = win;
487 unused2 = buf;
488 unused3 = maxlen;
489 unused4 = initlen;
490 gsc_fatal ("GLK: Stub unicode function called");
491 }
492
493 #endif
494
495 /*
496 * Known valid character printing range. Some Glk libraries aren't accurate
497 * about what will and what won't print when queried with glk_gestalt(), so
498 * we also make an explicit range check against guaranteed to print chars.
499 */
500 static const glui32 GSC_MIN_PRINTABLE = ' ',
501 GSC_MAX_PRINTABLE = '~';
502
503
504 /* List of pointers to supported and available locales, NULL terminated. */
505 static const gsc_locale_t *const GSC_AVAILABLE_LOCALES[] = {
506 &GSC_LATIN1_LOCALE,
507 &GSC_CYRILLIC_LOCALE,
508 NULL
509 };
510
511 /*
512 * The locale for the game, set below explicitly or on game startup, and
513 * a fallback locale to use in case none has been set.
514 */
515 static const gsc_locale_t *gsc_locale = NULL;
516 static const gsc_locale_t *const gsc_fallback_locale = &GSC_LATIN1_LOCALE;
517
518
519 /*
520 * gsc_set_locale()
521 *
522 * Set a locale explicitly from the name passed in.
523 */
524 static void
525 gsc_set_locale (const sc_char *name)
526 {
527 const gsc_locale_t *matched = NULL;
528 const gsc_locale_t *const *iterator;
529 assert (name);
530
531 /*
532 * Search locales for a matching name, abbreviated if necessary. Stop on
533 * the first match found.
534 */
535 for (iterator = GSC_AVAILABLE_LOCALES; *iterator; iterator++)
536 {
537 const gsc_locale_t *const locale = *iterator;
538
539 if (sc_strncasecmp (name, locale->name, strlen (name)) == 0)
540 {
541 matched = locale;
542 break;
543 }
544 }
545
546 /* If matched, set the global locale. */
547 if (matched)
548 gsc_locale = matched;
549 }
550
551
552 /*
553 * gsc_put_char_uni()
554 *
555 * Wrapper around glk_put_char_uni(). Handles, inelegantly, the problem of
556 * having to write transcripts as ascii.
557 */
558 static void
559 gsc_put_char_uni (glui32 unicode, const char *ascii)
560 {
561 /* If there is an transcript stream, temporarily disconnect it. */
562 if (gsc_transcript_stream)
563 glk_window_set_echo_stream (gsc_main_window, NULL);
564
565 glk_put_char_uni (unicode);
566
567 /* Print ascii to the transcript, then reattach it. */
568 if (gsc_transcript_stream)
569 {
570 if (ascii)
571 glk_put_string_stream (gsc_transcript_stream, (char *) ascii);
572 else
573 glk_put_char_stream (gsc_transcript_stream, '?');
574
575 glk_window_set_echo_stream (gsc_main_window, gsc_transcript_stream);
576 }
577 }
578
579
580 /*
581 * gsc_put_char_locale()
582 *
583 * Write a single character using the supplied locale. Select either the
584 * main or the alternate codepage depending on the flag passed in.
585 */
586 static void
587 gsc_put_char_locale (sc_char ch,
588 const gsc_locale_t *locale, sc_bool is_alternate)
589 {
590 const gsc_codepages_t *codepage;
591 unsigned char character;
592 glui32 unicode;
593 const char *ascii;
594
595 /*
596 * Select either the main or the alternate codepage for this locale, and
597 * retrieve the unicode and ascii representations of the character.
598 */
599 codepage = is_alternate ? &locale->alternate : &locale->main;
600 character = (unsigned char) ch;
601 unicode = codepage->unicode[character];
602 ascii = codepage->ascii[character];
603
604 /*
605 * If a unicode representation exists, use for either iso 8859-1 or, if
606 * possible, direct unicode output.
607 */
608 if (unicode > 0)
609 {
610 /*
611 * If unicode is in the range 1-255, this value is directly equivalent
612 * to the iso 8859-1 representation; otherwise the character has no
613 * direct iso 8859-1 glyph.
614 */
615 if (unicode < GSC_ISO_8859_EQUIVALENCE)
616 {
617 /*
618 * If the iso 8859-1 character is one that this Glk library will
619 * print exactly, print and return. We add a check here for the
620 * guaranteed printable characters, since some Glk libraries don't
621 * return the correct values for gestalt_CharOutput for these.
622 */
623 if (unicode == '\n'
624 || (unicode >= GSC_MIN_PRINTABLE && unicode <= GSC_MAX_PRINTABLE)
625 || glk_gestalt (gestalt_CharOutput,
626 unicode) == gestalt_CharOutput_ExactPrint)
627 {
628 glk_put_char ((unsigned char) unicode);
629 return;
630 }
631 }
632
633 /*
634 * If no usable iso 8859-1 representation, see if unicode is enabled and
635 * if the Glk library can print the character exactly. If yes, output
636 * the character that way.
637 *
638 * TODO Using unicode output currently disrupts transcript output. Any
639 * echo stream connected for a transcript here will be a text rather than
640 * a unicode stream, so probably won't output the character correctly.
641 * For now, if there's a transcript, we try to write ascii output.
642 */
643 if (gsc_unicode_enabled)
644 {
645 if (glk_gestalt (gestalt_CharOutput,
646 unicode) == gestalt_CharOutput_ExactPrint)
647 {
648 gsc_put_char_uni (unicode, ascii);
649 return;
650 }
651 }
652 }
653
654 /*
655 * No success with iso 8859-1 or unicode, so try for an ascii substitute.
656 * Substitute strings use only 7-bit ascii, and so all are safe to print
657 * directly with Glk.
658 */
659 if (ascii)
660 {
661 glk_put_string ((char *) ascii);
662 return;
663 }
664
665 /* No apparent way to output this character, so print a '?'. */
666 glk_put_char ('?');
667 }
668
669
670 /*
671 * gsc_put_char()
672 * gsc_put_char_alternate()
673 * gsc_put_buffer_using()
674 * gsc_put_buffer()
675 * gsc_put_string()
676 * gsc_put_string_alternate()
677 *
678 * Public functions for writing using the current or fallback locale.
679 */
680 static void
681 gsc_put_char (const sc_char character)
682 {
683 const gsc_locale_t *locale;
684
685 locale = gsc_locale ? gsc_locale : gsc_fallback_locale;
686 gsc_put_char_locale (character, locale, FALSE);
687 }
688
689 static void
690 gsc_put_char_alternate (const sc_char character)
691 {
692 const gsc_locale_t *locale;
693
694 locale = gsc_locale ? gsc_locale : gsc_fallback_locale;
695 gsc_put_char_locale (character, locale, TRUE);
696 }
697
698 static void
699 gsc_put_buffer_using (const sc_char *buffer,
700 sc_int length, void (*putchar_function) (sc_char))
701 {
702 sc_int index_;
703
704 for (index_ = 0; index_ < length; index_++)
705 putchar_function (buffer[index_]);
706 }
707
708 static void
709 gsc_put_buffer (const sc_char *buffer, sc_int length)
710 {
711 assert (buffer);
712
713 gsc_put_buffer_using (buffer, length, gsc_put_char);
714 }
715
716 static void
717 gsc_put_string (const sc_char *string)
718 {
719 assert (string);
720
721 gsc_put_buffer_using (string, strlen (string), gsc_put_char);
722 }
723
724 static void
725 gsc_put_string_alternate (const sc_char *string)
726 {
727 assert (string);
728
729 gsc_put_buffer_using (string, strlen (string), gsc_put_char_alternate);
730 }
731
732
733 /*
734 * gsc_unicode_to_locale()
735 * gsc_unicode_buffer_to_locale()
736 *
737 * Convert a unicode character back to an sc_char through a locale. Used for
738 * reverse translations in line input. Returns '?' if there is no translation
739 * available.
740 */
741 static sc_char
742 gsc_unicode_to_locale (glui32 unicode, const gsc_locale_t *locale)
743 {
744 const gsc_codepages_t *codepage;
745 sc_int character;
746
747 /* Always use the main codepage for input. */
748 codepage = &locale->main;
749
750 /*
751 * Search the unicode table sequentially for the input character. This is
752 * inefficient, but because game input is usually not copious, excusable.
753 */
754 for (character = 0; character < GSC_TABLE_SIZE; character++)
755 {
756 if (codepage->unicode[character] == unicode)
757 break;
758 }
759
760 /* Return the character translation, or '?' if none. */
761 return character < GSC_TABLE_SIZE ? (sc_char) character : '?';
762 }
763
764 static void
765 gsc_unicode_buffer_to_locale (const glui32 *unicode, sc_int length,
766 sc_char *buffer, const gsc_locale_t *locale)
767 {
768 sc_int index_;
769
770 for (index_ = 0; index_ < length; index_++)
771 buffer[index_] = gsc_unicode_to_locale (unicode[index_], locale);
772 }
773
774
775 /*
776 * gsc_read_line_locale()
777 *
778 * Read in a line and translate out of the given locale. Returns the count
779 * of characters placed in the buffer.
780 */
781 static sc_int
782 gsc_read_line_locale (sc_char *buffer,
783 sc_int length, const gsc_locale_t *locale)
784 {
785 event_t event;
786
787 /*
788 * If we have unicode, we have to use it to ensure that characters not in
789 * the Latin1 locale are properly translated.
790 */
791 if (gsc_unicode_enabled)
792 {
793 glui32 *unicode;
794
795 /*
796 * Allocate a unicode buffer long enough to hold all the characters,
797 * then read in a unicode line.
798 */
799 unicode = gsc_malloc (length * sizeof (*unicode));
800 glk_request_line_event_uni (gsc_main_window, unicode, length, 0);
801 gsc_event_wait (evtype_LineInput, &event);
802
803 /* Convert the unicode buffer out, then free it. */
804 gsc_unicode_buffer_to_locale (unicode, event.val1, buffer, locale);
805 free (unicode);
806
807 /* Return the count of characters placed in the buffer. */
808 return event.val1;
809 }
810
811 /* No success with unicode, so fall back to standard line input. */
812 glk_request_line_event (gsc_main_window, buffer, length, 0);
813 gsc_event_wait (evtype_LineInput, &event);
814
815 /* Return the count of characters placed in the buffer. */
816 return event.val1;
817 }
818
819
820 /*
821 * gsc_read_line()
822 *
823 * Public function for reading using the current or fallback locale.
824 */
825 static sc_int
826 gsc_read_line (sc_char *buffer, sc_int length)
827 {
828 const gsc_locale_t *locale;
829
830 locale = gsc_locale ? gsc_locale : gsc_fallback_locale;
831 return gsc_read_line_locale (buffer, length, locale);
832 }
833
834
835 /*---------------------------------------------------------------------*/
836 /* Glk port status line functions */
837 /*---------------------------------------------------------------------*/
838
839 /*
840 * Slop for right-justification of status lines, as an attempt to compensate
841 * for the fact that some characters in a games status line may use more than
842 * one position when printed, a particular problem in gost 16876-71 Cyrillic.
843 */
844 static const sc_int GSC_STATUS_SLOP = 10;
845
846 /* Size of saved status buffer used for non-windowing Glk status lines. */
847 enum { GSC_STATUS_BUFFER_LENGTH = 74 };
848
849 /* Whitespace characters, used to detect empty status elements. */
850 static const sc_char *const GSC_WHITESPACE = "\t\n\v\f\r ";
851
852
853 /*
854 * gsc_is_string_usable()
855 *
856 * Return TRUE if string is non-null, not zero-length or contains characters
857 * other than whitespace.
858 */
859 static sc_bool
860 gsc_is_string_usable (const sc_char *string)
861 {
862 /* If non-null, scan for any non-space character. */
863 if (string)
864 {
865 sc_int index_;
866
867 for (index_ = 0; string[index_] != '\0'; index_++)
868 {
869 if (!strchr (GSC_WHITESPACE, string[index_]))
870 return TRUE;
871 }
872 }
873
874 /* NULL, or no characters other than whitespace. */
875 return FALSE;
876 }
877
878
879 /*
880 * gsc_status_update()
881 *
882 * Update the status line from the current game state. This is for windowing
883 * Glk libraries.
884 */
885 static void
886 gsc_status_update (void)
887 {
888 glui32 width, height;
889 int index;
890 assert (gsc_status_window);
891
892 glk_window_get_size (gsc_status_window, &width, &height);
893 if (height > 0)
894 {
895 const sc_char *room;
896
897 glk_window_clear (gsc_status_window);
898 glk_window_move_cursor (gsc_status_window, 0, 0);
899 glk_set_window (gsc_status_window);
900
901 glk_set_style(style_User1);
902 for (index = 0; index < width; index++)
903 glk_put_char (' ');
904 glk_window_move_cursor (gsc_status_window, 0, 0);
905
906 /* See if the game is indicating any current player room. */
907 room = sc_get_game_room (gsc_game);
908 if (!gsc_is_string_usable (room))
909 {
910 /*
911 * Player location is indeterminate, so print out a generic status,
912 * showing the game name and author.
913 */
914 glk_window_move_cursor (gsc_status_window, 1, 0);
915 gsc_put_string (sc_get_game_name (gsc_game));
916 glk_put_string (" | ");
917 gsc_put_string (sc_get_game_author (gsc_game));
918 }
919 else
920 {
921 const sc_char *status;
922 char score[64];
923
924 /* Print the player location. */
925 glk_window_move_cursor (gsc_status_window, 1, 0);
926 gsc_put_string (room);
927
928 /* Get the game's status line, or if none, format score. */
929 status = sc_get_game_status_line (gsc_game);
930 if (!gsc_is_string_usable (status))
931 {
932 sprintf (score, "Score: %ld", sc_get_game_score (gsc_game));
933 status = score;
934 }
935
936 /* Print the status line or score at window right, if it fits. */
937 if (width > strlen (status) + GSC_STATUS_SLOP + 1)
938 {
939 glui32 position;
940
941 position = width - strlen (status) - GSC_STATUS_SLOP;
942 glk_window_move_cursor (gsc_status_window, position - 1, 0);
943 gsc_put_string (status);
944 }
945 }
946
947 glk_set_window (gsc_main_window);
948 }
949 }
950
951
952 /*
953 * gsc_status_safe_strcat()
954 *
955 * Helper for gsc_status_print(), concatenates strings only up to the
956 * available length.
957 */
958 static void
959 gsc_status_safe_strcat (char *dest, size_t length, const char *src)
960 {
961 size_t available, src_length;
962
963 /* Append only as many characters as will fit. */
964 src_length = strlen (src);
965 available = length - strlen (dest) - 1;
966 if (available > 0)
967 strncat (dest, src, src_length < available ? src_length : available);
968 }
969
970
971 /*
972 * gsc_status_print()
973 *
974 * Print the current contents of the completed status line buffer out in the
975 * main window, if it has changed since the last call. This is for non-
976 * windowing Glk libraries.
977 */
978 static void
979 gsc_status_print (void)
980 {
981 static char current_status[GSC_STATUS_BUFFER_LENGTH + 1];
982
983 const sc_char *room;
984
985 /* Do nothing if the game isn't indicating any current player room. */
986 room = sc_get_game_room (gsc_game);
987 if (gsc_is_string_usable (room))
988 {
989 char buffer[GSC_STATUS_BUFFER_LENGTH + 1];
990 const sc_char *status;
991 char score[64];
992
993 /* Make an attempt at a status line, starting with player location. */
994 strcpy (buffer, "");
995 gsc_status_safe_strcat (buffer, sizeof (buffer), room);
996
997 /* Get the game's status line, or if none, format score. */
998 status = sc_get_game_status_line (gsc_game);
999 if (!gsc_is_string_usable (status))
1000 {
1001 sprintf (score, "Score: %ld", sc_get_game_score (gsc_game));
1002 status = score;
1003 }
1004
1005 /* Append the status line or score. */
1006 gsc_status_safe_strcat (buffer, sizeof (buffer), " | ");
1007 gsc_status_safe_strcat (buffer, sizeof (buffer), status);
1008
1009 /* If this matches the current saved status line, do nothing more. */
1010 if (strcmp (buffer, current_status) != 0)
1011 {
1012 /* Bracket, and output the status line buffer. */
1013 glk_put_string ("[ ");
1014 gsc_put_string (buffer);
1015 glk_put_string (" ]\n");
1016
1017 /* Save the details of the printed status buffer. */
1018 strcpy (current_status, buffer);
1019 }
1020 }
1021 }
1022
1023
1024 /*
1025 * gsc_status_notify()
1026 *
1027 * Front end function for updating status. Either updates the status window
1028 * or prints the status line to the main window.
1029 */
1030 static void
1031 gsc_status_notify (void)
1032 {
1033 if (gsc_status_window)
1034 gsc_status_update ();
1035 else
1036 gsc_status_print ();
1037 }
1038
1039
1040 /*
1041 * gsc_status_redraw()
1042 *
1043 * Redraw the contents of any status window with the constructed status string.
1044 * This function should be called on the appropriate Glk window resize and
1045 * arrange events.
1046 */
1047 static void
1048 gsc_status_redraw (void)
1049 {
1050 if (gsc_status_window)
1051 {
1052 winid_t parent;
1053
1054 /*
1055 * Rearrange the status window, without changing its actual arrangement
1056 * in any way. This is a hack to work round incorrect window repainting
1057 * in Xglk; it forces a complete repaint of affected windows on Glk
1058 * window resize and arrange events, and works in part because Xglk
1059 * doesn't check for actual arrangement changes in any way before
1060 * invalidating its windows. The hack should be harmless to Glk
1061 * libraries other than Xglk, moreover, we're careful to activate it
1062 * only on resize and arrange events.
1063 */
1064 parent = glk_window_get_parent (gsc_status_window);
1065 glk_window_set_arrangement (parent,
1066 winmethod_Above | winmethod_Fixed, 1, NULL);
1067 gsc_status_update ();
1068 }
1069 }
1070
1071
1072 /*---------------------------------------------------------------------*/
1073 /* Glk port output functions */
1074 /*---------------------------------------------------------------------*/
1075
1076 /*
1077 * Flag for if the user entered "help" as their last input, or if hints have
1078 * been silenced as a result of already using a Glk command.
1079 */
1080 static int gsc_help_requested = FALSE,
1081 gsc_help_hints_silenced = FALSE;
1082
1083 /* Font descriptor type, encapsulating size and monospaced boolean. */
1084 typedef struct {
1085 sc_bool is_monospaced;
1086 sc_int size;
1087 } gsc_font_size_t;
1088
1089 /* Font stack and attributes for nesting tags. */
1090 enum { GSC_MAX_STYLE_NESTING = 32 };
1091 static gsc_font_size_t gsc_font_stack[GSC_MAX_STYLE_NESTING];
1092 static glui32 gsc_font_index = 0;
1093 static glui32 gsc_attribute_bold = 0,
1094 gsc_attribute_italic = 0,
1095 gsc_attribute_underline = 0,
1096 gsc_attribute_secondary_color = 0;
1097
1098 /* Notional default font size, and limit font sizes. */
1099 static const sc_int GSC_DEFAULT_FONT_SIZE = 12,
1100 GSC_MEDIUM_FONT_SIZE = 14,
1101 GSC_LARGE_FONT_SIZE = 16;
1102
1103 /* Milliseconds per second and timeouts count for delay tags. */
1104 static const glui32 GSC_MILLISECONDS_PER_SECOND = 1000;
1105 static const glui32 GSC_TIMEOUTS_COUNT = 10;
1106
1107 /* Number of hints to refuse before offering to end hint display. */
1108 static const sc_int GSC_HINT_REFUSAL_LIMIT = 5;
1109
1110 /* The keypresses used to cancel any <wait x.x> early. */
1111 static const glui32 GSC_CANCEL_WAIT_1 = ' ',
1112 GSC_CANCEL_WAIT_2 = keycode_Return;
1113
1114
1115 /*
1116 * gsc_output_register_help_request()
1117 * gsc_output_silence_help_hints()
1118 * gsc_output_provide_help_hint()
1119 *
1120 * Register a request for help, and print a note of how to get Glk command
1121 * help from the interpreter unless silenced.
1122 */
1123 static void
1124 gsc_output_register_help_request (void)
1125 {
1126 gsc_help_requested = TRUE;
1127 }
1128
1129 static void
1130 gsc_output_silence_help_hints (void)
1131 {
1132 gsc_help_hints_silenced = TRUE;
1133 }
1134
1135 static void
1136 gsc_output_provide_help_hint (void)
1137 {
1138 if (gsc_help_requested && !gsc_help_hints_silenced)
1139 {
1140 glk_set_style (style_Emphasized);
1141 glk_put_string ("[Try 'glk help' for help on special interpreter"
1142 " commands]\n");
1143
1144 gsc_help_requested = FALSE;
1145 glk_set_style (style_Normal);
1146 }
1147 }
1148
1149
1150 /*
1151 * gsc_set_glk_style()
1152 *
1153 * Set a Glk style based on the top of the font stack and attributes.
1154 */
1155 static void
1156 gsc_set_glk_style (void)
1157 {
1158 sc_bool is_monospaced;
1159 sc_int font_size;
1160
1161 /* Get the current font stack top, or default value. */
1162 if (gsc_font_index > 0)
1163 {
1164 is_monospaced = gsc_font_stack[gsc_font_index - 1].is_monospaced;
1165 font_size = gsc_font_stack[gsc_font_index - 1].size;
1166 }
1167 else
1168 {
1169 is_monospaced = FALSE;
1170 font_size = GSC_DEFAULT_FONT_SIZE;
1171 }
1172
1173 /*
1174 * Map the font and current attributes into a Glk style. Because Glk styles
1175 * aren't cumulative this has to be done by precedences.
1176 */
1177 if (is_monospaced)
1178 {
1179 /*
1180 * No matter the size or attributes, if monospaced use Preformatted
1181 * style, as it's all we have.
1182 */
1183 glk_set_style (style_Preformatted);
1184 }
1185 else
1186 {
1187 /*
1188 * For large and medium point sizes, use Header or Subheader styles
1189 * respectively.
1190 */
1191 if (font_size >= GSC_LARGE_FONT_SIZE)
1192 glk_set_style (style_Header);
1193 else if (font_size >= GSC_MEDIUM_FONT_SIZE)
1194 glk_set_style (style_Subheader);
1195 else
1196 {
1197 /*
1198 * For bold, use Subheader; for italics, underline, or secondary
1199 * color, use Emphasized.
1200 */
1201 if (gsc_attribute_bold > 0)
1202 glk_set_style (style_Subheader);
1203 else if (gsc_attribute_italic > 0
1204 || gsc_attribute_underline > 0
1205 || gsc_attribute_secondary_color > 0)
1206 glk_set_style (style_Emphasized);
1207 else
1208 {
1209 /*
1210 * There's nothing special about this text, so drop down to
1211 * Normal style.
1212 */
1213 glk_set_style (style_Normal);
1214 }
1215 }
1216 }
1217 }
1218
1219
1220 /*
1221 * gsc_handle_font_tag()
1222 * gsc_handle_endfont_tag()
1223 *
1224 * Push the settings of a font tag onto the font stack, and pop on end of
1225 * font tag. Set the appropriate Glk style.
1226 */
1227 static void
1228 gsc_handle_font_tag (const sc_char *argument)
1229 {
1230 /* Ignore the call on stack overrun. */
1231 if (gsc_font_index < GSC_MAX_STYLE_NESTING)
1232 {
1233 sc_char *lower, *face, *size;
1234 sc_bool is_monospaced;
1235 sc_int index_, font_size;
1236
1237 /* Get the current top of stack, or default on empty stack. */
1238 if (gsc_font_index > 0)
1239 {
1240 is_monospaced = gsc_font_stack[gsc_font_index - 1].is_monospaced;
1241 font_size = gsc_font_stack[gsc_font_index - 1].size;
1242 }
1243 else
1244 {
1245 is_monospaced = FALSE;
1246 font_size = GSC_DEFAULT_FONT_SIZE;
1247 }
1248
1249 /* Copy and convert argument to all lowercase. */
1250 lower = gsc_malloc (strlen (argument) + 1);
1251 strcpy (lower, argument);
1252 for (index_ = 0; lower[index_] != '\0'; index_++)
1253 lower[index_] = glk_char_to_lower (lower[index_]);
1254
1255 /* Find any face= portion of the tag argument. */
1256 face = strstr (lower, "face=");
1257 if (face)
1258 {
1259 /*
1260 * There may be plenty of monospaced fonts, but we do only courier
1261 * and terminal.
1262 */
1263 is_monospaced = strncmp (face, "face=\"courier\"", 14) == 0
1264 || strncmp (face, "face=\"terminal\"", 15) == 0;
1265 }
1266
1267 /* Find the size= portion of the tag argument. */
1268 size = strstr (lower, "size=");
1269 if (size)
1270 {
1271 sc_uint value;
1272
1273 /* Deal with incremental and absolute sizes. */
1274 if (strncmp (size, "size=+", 6) == 0
1275 && sscanf (size, "size=+%lu", &value) == 1)
1276 font_size += value;
1277 else if (strncmp (size, "size=-", 6) == 0
1278 && sscanf (size, "size=-%lu", &value) == 1)
1279 font_size -= value;
1280 else if (sscanf (size, "size=%lu", &value) == 1)
1281 font_size = value;
1282 }
1283
1284 /* Done with tag argument copy. */
1285 free (lower);
1286
1287 /*
1288 * Push the new font setting onto the font stack, and set Glk style.
1289 */
1290 gsc_font_stack[gsc_font_index].is_monospaced = is_monospaced;
1291 gsc_font_stack[gsc_font_index++].size = font_size;
1292 gsc_set_glk_style ();
1293 }
1294 }
1295
1296 static void
1297 gsc_handle_endfont_tag (void)
1298 {
1299 /* Unless underrun, pop the font stack and set Glk style. */
1300 if (gsc_font_index > 0)
1301 {
1302 gsc_font_index--;
1303 gsc_set_glk_style ();
1304 }
1305 }
1306
1307
1308 /*
1309 * gsc_handle_attribute_tag()
1310 *
1311 * Increment the required attribute nesting counter, or decrement on end
1312 * tag. Set the appropriate Glk style.
1313 */
1314 static void
1315 gsc_handle_attribute_tag (sc_int tag)
1316 {
1317 /*
1318 * Increment the required attribute nesting counter, and set Glk style.
1319 */
1320 switch (tag)
1321 {
1322 case SC_TAG_BOLD:
1323 gsc_attribute_bold++;
1324 break;
1325 case SC_TAG_ITALICS:
1326 gsc_attribute_italic++;
1327 break;
1328 case SC_TAG_UNDERLINE:
1329 gsc_attribute_underline++;
1330 break;
1331 case SC_TAG_COLOR:
1332 gsc_attribute_secondary_color++;
1333 break;
1334 default:
1335 break;
1336 }
1337 gsc_set_glk_style ();
1338 }
1339
1340 static void
1341 gsc_handle_endattribute_tag (sc_int tag)
1342 {
1343 /*
1344 * Decrement the required attribute nesting counter, unless underrun, and
1345 * set Glk style.
1346 */
1347 switch (tag)
1348 {
1349 case SC_TAG_ENDBOLD:
1350 if (gsc_attribute_bold > 0)
1351 gsc_attribute_bold--;
1352 break;
1353 case SC_TAG_ENDITALICS:
1354 if (gsc_attribute_italic > 0)
1355 gsc_attribute_italic--;
1356 break;
1357 case SC_TAG_ENDUNDERLINE:
1358 if (gsc_attribute_underline > 0)
1359 gsc_attribute_underline--;
1360 break;
1361 case SC_TAG_ENDCOLOR:
1362 if (gsc_attribute_secondary_color > 0)
1363 gsc_attribute_secondary_color--;
1364 break;
1365 default:
1366 break;
1367 }
1368 gsc_set_glk_style ();
1369 }
1370
1371
1372 /*
1373 * gsc_handle_wait_tag()
1374 *
1375 * If Glk offers timers, delay for the requested period. Otherwise, this
1376 * function does nothing.
1377 */
1378 static void
1379 gsc_handle_wait_tag (const sc_char *argument)
1380 {
1381 double delay = 0.0;
1382
1383 /* Ignore the wait tag if the Glk doesn't have timers. */
1384 if (!glk_gestalt (gestalt_Timer, 0))
1385 return;
1386
1387 /* Determine the delay time, and convert to milliseconds. */
1388 if (sscanf (argument, "%lf", &delay) == 1 && delay > 0.0)
1389 {
1390 glui32 milliseconds, timeout;
1391
1392 /*
1393 * Work with timeouts at 1/10 of the wait period, to minimize Glk
1394 * timer jitter. Allow the timeout to be canceled by keypress, as a
1395 * user convenience.
1396 */
1397 milliseconds = (glui32) (delay * GSC_MILLISECONDS_PER_SECOND);
1398 timeout = milliseconds / GSC_TIMEOUTS_COUNT;
1399 if (timeout > 0)
1400 {
1401 glui32 delayed;
1402 sc_bool is_completed;
1403
1404 /* Request timer events, and let a keypress cancel the wait. */
1405 glk_request_char_event (gsc_main_window);
1406 glk_request_timer_events (timeout);
1407
1408 /* Loop until delay completed or canceled by a keypress. */
1409 is_completed = TRUE;
1410 for (delayed = 0; delayed < milliseconds; delayed += timeout)
1411 {
1412 event_t event;
1413
1414 gsc_event_wait_2 (evtype_CharInput, evtype_Timer, &event);
1415 if (event.type == evtype_CharInput)
1416 {
1417 /* Cancel the delay, or reissue the input request. */
1418 if (event.val1 == GSC_CANCEL_WAIT_1
1419 || event.val1 == GSC_CANCEL_WAIT_2)
1420 {
1421 is_completed = FALSE;
1422 break;
1423 }
1424 else
1425 glk_request_char_event (gsc_main_window);
1426 }
1427 }
1428
1429 /* Cancel any pending character input, and stop timers. */
1430 if (is_completed)
1431 glk_cancel_char_event (gsc_main_window);
1432 glk_request_timer_events (0);
1433 }
1434 }
1435 }
1436
1437
1438 /*
1439 * gsc_reset_glk_style()
1440 *
1441 * Drop all stacked fonts and nested attributes, and return to normal Glk
1442 * style.
1443 */
1444 static void
1445 gsc_reset_glk_style (void)
1446 {
1447 /* Reset the font stack and attributes, and set a normal style. */
1448 gsc_font_index = 0;
1449 gsc_attribute_bold = 0;
1450 gsc_attribute_italic = 0;
1451 gsc_attribute_underline = 0;
1452 gsc_attribute_secondary_color = 0;
1453 gsc_set_glk_style ();
1454 }
1455
1456
1457 /*
1458 * os_print_tag()
1459 *
1460 * Interpret selected Adrift output control tags. Not all are implemented
1461 * here; several are ignored.
1462 */
1463 void
1464 os_print_tag (sc_int tag, const sc_char *argument)
1465 {
1466 event_t event;
1467 assert (argument);
1468
1469 switch (tag)
1470 {
1471 case SC_TAG_CLS:
1472 /* Clear the main text display window. */
1473 glk_window_clear (gsc_main_window);
1474 break;
1475
1476 case SC_TAG_FONT:
1477 /* Handle with specific tag handler function. */
1478 gsc_handle_font_tag (argument);
1479 break;
1480
1481 case SC_TAG_ENDFONT:
1482 /* Handle with specific endtag handler function. */
1483 gsc_handle_endfont_tag ();
1484 break;
1485
1486 case SC_TAG_BOLD:
1487 case SC_TAG_ITALICS:
1488 case SC_TAG_UNDERLINE:
1489 case SC_TAG_COLOR:
1490 /* Handle with common attribute tag handler function. */
1491 gsc_handle_attribute_tag (tag);
1492 break;
1493
1494 case SC_TAG_ENDBOLD:
1495 case SC_TAG_ENDITALICS:
1496 case SC_TAG_ENDUNDERLINE:
1497 case SC_TAG_ENDCOLOR:
1498 /* Handle with common attribute endtag handler function. */
1499 gsc_handle_endattribute_tag (tag);
1500 break;
1501
1502 case SC_TAG_CENTER:
1503 case SC_TAG_RIGHT:
1504 case SC_TAG_ENDCENTER:
1505 case SC_TAG_ENDRIGHT:
1506 /*
1507 * We don't center or justify text, but so that things look right we do
1508 * want a newline on starting or ending such a section.
1509 */
1510 glk_put_char ('\n');
1511 break;
1512
1513 case SC_TAG_WAIT:
1514 /*
1515 * Update the status line now only if it has its own window, then
1516 * handle with a specialized handler.
1517 */
1518 if (gsc_status_window)
1519 gsc_status_notify ();
1520 gsc_handle_wait_tag (argument);
1521 break;
1522
1523 case SC_TAG_WAITKEY:
1524 /*
1525 * If reading an input log, ignore; it disrupts replay. Write a newline
1526 * to separate off any unterminated game output instead.
1527 */
1528 if (!gsc_readlog_stream)
1529 {
1530 /* Update the status line now only if it has its own window. */
1531 if (gsc_status_window)
1532 gsc_status_notify ();
1533
1534 /* Request a character event, and wait for it to be filled. */
1535 glk_request_char_event (gsc_main_window);
1536 gsc_event_wait (evtype_CharInput, &event);
1537 }
1538 else
1539 glk_put_char ('\n');
1540 break;
1541
1542 default:
1543 /* Ignore unimplemented and unknown tags. */
1544 break;
1545 }
1546 }
1547
1548
1549 /*
1550 * os_print_string()
1551 *
1552 * Print a text string to the main output window.
1553 */
1554 void
1555 os_print_string (const sc_char *string)
1556 {
1557 sc_bool is_monospaced;
1558 assert (string);
1559 assert (glk_stream_get_current ());
1560
1561 /*
1562 * Get the monospace font setting from the current top of stack, or
1563 * default on empty stack. If set, we may need to use an alternative
1564 * function to write this string.
1565 */
1566 if (gsc_font_index > 0)
1567 is_monospaced = gsc_font_stack[gsc_font_index - 1].is_monospaced;
1568 else
1569 is_monospaced = FALSE;
1570
1571 /*
1572 * The main window should always be the currently set window at this point,
1573 * so we never be attempting monospaced output to the status window.
1574 * Nevertheless, check anyway.
1575 */
1576 if (is_monospaced
1577 && glk_stream_get_current () == glk_window_get_stream (gsc_main_window))
1578 gsc_put_string_alternate (string);
1579 else
1580 gsc_put_string (string);
1581 }
1582
1583
1584 /*
1585 * os_print_string_debug()
1586 *
1587 * Debugging output goes to the main Glk window -- no special effects or
1588 * dedicated debugging window attempted.
1589 */
1590 void
1591 os_print_string_debug (const sc_char *string)
1592 {
1593 assert (string);
1594 assert (glk_stream_get_current ());
1595
1596 gsc_put_string (string);
1597 }
1598
1599
1600 /*
1601 * gsc_styled_string()
1602 * gsc_styled_char()
1603 * gsc_standout_string()
1604 * gsc_standout_char()
1605 * gsc_normal_string()
1606 * gsc_normal_char()
1607 * gsc_header_string()
1608 *
1609 * Convenience functions to print strings in assorted styles. A standout
1610 * string is one that hints that it's from the interpreter, not the game.
1611 */
1612 static void
1613 gsc_styled_string (glui32 style, const char *message)
1614 {
1615 assert (message);
1616
1617 glk_set_style (style);
1618 glk_put_string ((char *) message);
1619 glk_set_style (style_Normal);
1620 }
1621
1622 static void
1623 gsc_styled_char (glui32 style, char c)
1624 {
1625 char buffer[2];
1626
1627 buffer[0] = c;
1628 buffer[1] = '\0';
1629 gsc_styled_string (style, buffer);
1630 }
1631
1632 static void
1633 gsc_standout_string (const char *message)
1634 {
1635 gsc_styled_string (style_Emphasized, message);
1636 }
1637
1638 static void
1639 gsc_standout_char (char c)
1640 {
1641 gsc_styled_char (style_Emphasized, c);
1642 }
1643
1644 static void
1645 gsc_normal_string (const char *message)
1646 {
1647 gsc_styled_string (style_Normal, message);
1648 }
1649
1650 static void
1651 gsc_normal_char (char c)
1652 {
1653 gsc_styled_char (style_Normal, c);
1654 }
1655
1656 static void
1657 gsc_header_string (const char *message)
1658 {
1659 gsc_styled_string (style_Header, message);
1660 }
1661
1662
1663 /*
1664 * os_display_hints()
1665 *
1666 * This is a very basic hints display. In mitigation, very few games use
1667 * hints at all, and those that do are usually sparse in what they hint at, so
1668 * it's sort of good enough for the moment.
1669 */
1670 void
1671 os_display_hints (sc_game game)
1672 {
1673 sc_game_hint hint;
1674 sc_int refused;
1675
1676 /* For each hint, print the question, and confirm hint display. */
1677 refused = 0;
1678 for (hint = sc_get_first_game_hint (game);
1679 hint; hint = sc_get_next_game_hint (game, hint))
1680 {
1681 const sc_char *hint_question, *hint_text;
1682
1683 /* If enough refusals, offer a way out of the loop. */
1684 if (refused >= GSC_HINT_REFUSAL_LIMIT)
1685 {
1686 if (!os_confirm (GSC_CONF_CONTINUE_HINTS))
1687 break;
1688 refused = 0;
1689 }
1690
1691 /* Pop the question. */
1692 hint_question = sc_get_game_hint_question (game, hint);
1693 gsc_normal_char ('\n');
1694 gsc_standout_string (hint_question);
1695 gsc_normal_char ('\n');
1696
1697 /* Print the subtle hint, or on to the next hint. */
1698 hint_text = sc_get_game_subtle_hint (game, hint);
1699 if (hint_text)
1700 {
1701 if (!os_confirm (GSC_CONF_SUBTLE_HINT))
1702 {
1703 refused++;
1704 continue;
1705 }
1706 gsc_normal_char ('\n');
1707 gsc_standout_string (hint_text);
1708 gsc_normal_string ("\n\n");
1709 }
1710
1711 /* Print the less than subtle hint, or on to the next hint. */
1712 hint_text = sc_get_game_unsubtle_hint (game, hint);
1713 if (hint_text)
1714 {
1715 if (!os_confirm (GSC_CONF_UNSUBTLE_HINT))
1716 {
1717 refused++;
1718 continue;
1719 }
1720 gsc_normal_char ('\n');
1721 gsc_standout_string (hint_text);
1722 gsc_normal_string ("\n\n");
1723 }
1724 }
1725 }
1726
1727
1728 /*---------------------------------------------------------------------*/
1729 /* Glk resource handling functions */
1730 /*---------------------------------------------------------------------*/
1731
1732 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
1733 static schanid_t sound_channel;
1734
1735 void
1736 os_play_sound (const sc_char *filepath,
1737 sc_int offset, sc_int length, sc_bool is_looping)
1738 {
1739 if (sound_channel == NULL) {
1740 sound_channel = glk_schannel_create(0);
1741 }
1742
1743 if (sound_channel == NULL) {
1744 return;
1745 }
1746
1747 glui32 id = garglk_add_resource_from_file(giblorb_ID_Snd, gamefile, offset, length);
1748 if (id != 0) {
1749 glk_schannel_play_ext(sound_channel, id, is_looping ? 0xffffffff : 1, 0);
1750 }
1751 }
1752
1753 void
1754 os_stop_sound (void)
1755 {
1756 if (sound_channel != NULL) {
1757 glk_schannel_stop(sound_channel);
1758 }
1759 }
1760 #else
1761 /*
1762 * os_play_sound()
1763 * os_stop_sound()
1764 *
1765 * Stub functions. The unused variables defeat gcc warnings.
1766 */
1767 void
1768 os_play_sound (const sc_char *filepath,
1769 sc_int offset, sc_int length, sc_bool is_looping)
1770 {
1771 const sc_char *unused1;
1772 sc_int unused2, unused3;
1773 sc_bool unused4;
1774 unused1 = filepath;
1775 unused2 = offset;
1776 unused3 = length;
1777 unused4 = is_looping;
1778 }
1779
1780 void
1781 os_stop_sound (void)
1782 {
1783 }
1784 #endif
1785
1786
1787 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
1788 /*
1789 * os_show_graphic()
1790 *
1791 * Use the Gargoyle-specific garglk_add_resource_from_file().
1792 */
1793 void
1794 os_show_graphic (const sc_char *filepath, sc_int offset, sc_int length)
1795 {
1796 glui32 id = garglk_add_resource_from_file(giblorb_ID_Pict, gamefile, offset, length);
1797 if (id != 0) {
1798 glk_image_draw(gsc_main_window, id, imagealign_InlineDown, 0);
1799 }
1800 }
1801 #else
1802 /*
1803 * os_show_graphic()
1804 *
1805 * For graphic-capable Glk libraries on Linux, attempt graphics using xv. The
1806 * graphic capability test isn't really required, it's just a way of having
1807 * graphics behave without surprises; someone using a non-graphical Glk
1808 * probably won't expect graphics to pop up.
1809 *
1810 * For other cases, this is a stub function, with unused variables to defeat
1811 * gcc warnings.
1812 */
1813 #ifdef LINUX_GRAPHICS
1814 static int gsclinux_graphics_enabled = TRUE;
1815 static char *gsclinux_game_file = NULL;
1816 void
1817 os_show_graphic (const sc_char *filepath, sc_int offset, sc_int length)
1818 {
1819 const sc_char *unused1;
1820 unused1 = filepath;
1821
1822 if (length > 0
1823 && gsclinux_graphics_enabled && glk_gestalt (gestalt_Graphics, 0))
1824 {
1825 sc_char *buffer;
1826
1827 /*
1828 * Try to extract data with dd. Assuming that works, background xv to
1829 * display the image, then background a job to delay ten seconds and
1830 * then delete the temporary file containing the image. Systems lacking
1831 * xv can usually use a small script, named xv, to invoke eog or an
1832 * alternative image display binary. Not exactly finessed.
1833 */
1834 assert (gsclinux_game_file);
1835 buffer = gsc_malloc (strlen (gsclinux_game_file) + 128);
1836 sprintf (buffer, "dd if=%s ibs=1c skip=%ld count=%ld obs=100k"
1837 " of=/tmp/scare.jpg 2>/dev/null",
1838 gsclinux_game_file, offset, length);
1839 system (buffer);
1840 free (buffer);
1841 system ("xv /tmp/scare.jpg >/dev/null 2>&1 &");
1842 system ("( sleep 10; rm /tmp/scare.jpg ) >/dev/null 2>&1 &");
1843 }
1844 }
1845 #else
1846 void
1847 os_show_graphic (const sc_char *filepath, sc_int offset, sc_int length)
1848 {
1849 const sc_char *unused1;
1850 sc_int unused2, unused3;
1851 unused1 = filepath;
1852 unused2 = offset;
1853 unused3 = length;
1854 }
1855 #endif
1856 #endif
1857
1858
1859 /*---------------------------------------------------------------------*/
1860 /* Glk command escape functions */
1861 /*---------------------------------------------------------------------*/
1862
1863 /*
1864 * gsc_command_script()
1865 *
1866 * Turn game output scripting (logging) on and off.
1867 */
1868 static void
1869 gsc_command_script (const char *argument)
1870 {
1871 assert (argument);
1872
1873 if (sc_strcasecmp (argument, "on") == 0)
1874 {
1875 frefid_t fileref;
1876
1877 if (gsc_transcript_stream)
1878 {
1879 gsc_normal_string ("Glk transcript is already on.\n");
1880 return;
1881 }
1882
1883 fileref = glk_fileref_create_by_prompt (fileusage_Transcript
1884 | fileusage_TextMode,
1885 filemode_WriteAppend, 0);
1886 if (!fileref)
1887 {
1888 gsc_standout_string ("Glk transcript failed.\n");
1889 return;
1890 }
1891
1892 gsc_transcript_stream = glk_stream_open_file (fileref,
1893 filemode_WriteAppend, 0);
1894 glk_fileref_destroy (fileref);
1895 if (!gsc_transcript_stream)
1896 {
1897 gsc_standout_string ("Glk transcript failed.\n");
1898 return;
1899 }
1900
1901 glk_window_set_echo_stream (gsc_main_window, gsc_transcript_stream);
1902
1903 gsc_normal_string ("Glk transcript is now on.\n");
1904 }
1905
1906 else if (sc_strcasecmp (argument, "off") == 0)
1907 {
1908 if (!gsc_transcript_stream)
1909 {
1910 gsc_normal_string ("Glk transcript is already off.\n");
1911 return;
1912 }
1913
1914 glk_stream_close (gsc_transcript_stream, NULL);
1915 gsc_transcript_stream = NULL;
1916
1917 glk_window_set_echo_stream (gsc_main_window, NULL);
1918
1919 gsc_normal_string ("Glk transcript is now off.\n");
1920 }
1921
1922 else if (strlen (argument) == 0)
1923 {
1924 gsc_normal_string ("Glk transcript is ");
1925 gsc_normal_string (gsc_transcript_stream ? "on" : "off");
1926 gsc_normal_string (".\n");
1927 }
1928
1929 else
1930 {
1931 gsc_normal_string ("Glk transcript can be ");
1932 gsc_standout_string ("on");
1933 gsc_normal_string (", or ");
1934 gsc_standout_string ("off");
1935 gsc_normal_string (".\n");
1936 }
1937 }
1938
1939
1940 /*
1941 * gsc_command_inputlog()
1942 *
1943 * Turn game input logging on and off.
1944 */
1945 static void
1946 gsc_command_inputlog (const char *argument)
1947 {
1948 assert (argument);
1949
1950 if (sc_strcasecmp (argument, "on") == 0)
1951 {
1952 frefid_t fileref;
1953
1954 if (gsc_inputlog_stream)
1955 {
1956 gsc_normal_string ("Glk input logging is already on.\n");
1957 return;
1958 }
1959
1960 fileref = glk_fileref_create_by_prompt (fileusage_InputRecord
1961 | fileusage_BinaryMode,
1962 filemode_WriteAppend, 0);
1963 if (!fileref)
1964 {
1965 gsc_standout_string ("Glk input logging failed.\n");
1966 return;
1967 }
1968
1969 gsc_inputlog_stream = glk_stream_open_file (fileref,
1970 filemode_WriteAppend, 0);
1971 glk_fileref_destroy (fileref);
1972 if (!gsc_inputlog_stream)
1973 {
1974 gsc_standout_string ("Glk input logging failed.\n");
1975 return;
1976 }
1977
1978 gsc_normal_string ("Glk input logging is now on.\n");
1979 }
1980
1981 else if (sc_strcasecmp (argument, "off") == 0)
1982 {
1983 if (!gsc_inputlog_stream)
1984 {
1985 gsc_normal_string ("Glk input logging is already off.\n");
1986 return;
1987 }
1988
1989 glk_stream_close (gsc_inputlog_stream, NULL);
1990 gsc_inputlog_stream = NULL;
1991
1992 gsc_normal_string ("Glk input log is now off.\n");
1993 }
1994
1995 else if (strlen (argument) == 0)
1996 {
1997 gsc_normal_string ("Glk input logging is ");
1998 gsc_normal_string (gsc_inputlog_stream ? "on" : "off");
1999 gsc_normal_string (".\n");
2000 }
2001
2002 else
2003 {
2004 gsc_normal_string ("Glk input logging can be ");
2005 gsc_standout_string ("on");
2006 gsc_normal_string (", or ");
2007 gsc_standout_string ("off");
2008 gsc_normal_string (".\n");
2009 }
2010 }
2011
2012
2013 /*
2014 * gsc_command_readlog()
2015 *
2016 * Set the game input log, to read input from a file.
2017 */
2018 static void
2019 gsc_command_readlog (const char *argument)
2020 {
2021 assert (argument);
2022
2023 if (sc_strcasecmp (argument, "on") == 0)
2024 {
2025 frefid_t fileref;
2026
2027 if (gsc_readlog_stream)
2028 {
2029 gsc_normal_string ("Glk read log is already on.\n");
2030 return;
2031 }
2032
2033 fileref = glk_fileref_create_by_prompt (fileusage_InputRecord
2034 | fileusage_BinaryMode,
2035 filemode_Read, 0);
2036 if (!fileref)
2037 {
2038 gsc_standout_string ("Glk read log failed.\n");
2039 return;
2040 }
2041
2042 if (!glk_fileref_does_file_exist (fileref))
2043 {
2044 glk_fileref_destroy (fileref);
2045 gsc_standout_string ("Glk read log failed.\n");
2046 return;
2047 }
2048
2049 gsc_readlog_stream = glk_stream_open_file (fileref, filemode_Read, 0);
2050 glk_fileref_destroy (fileref);
2051 if (!gsc_readlog_stream)
2052 {
2053 gsc_standout_string ("Glk read log failed.\n");
2054 return;
2055 }
2056
2057 gsc_normal_string ("Glk read log is now on.\n");
2058 }
2059
2060 else if (sc_strcasecmp (argument, "off") == 0)
2061 {
2062 if (!gsc_readlog_stream)
2063 {
2064 gsc_normal_string ("Glk read log is already off.\n");
2065 return;
2066 }
2067
2068 glk_stream_close (gsc_readlog_stream, NULL);
2069 gsc_readlog_stream = NULL;
2070
2071 gsc_normal_string ("Glk read log is now off.\n");
2072 }
2073
2074 else if (strlen (argument) == 0)
2075 {
2076 gsc_normal_string ("Glk read log is ");
2077 gsc_normal_string (gsc_readlog_stream ? "on" : "off");
2078 gsc_normal_string (".\n");
2079 }
2080
2081 else
2082 {
2083 gsc_normal_string ("Glk read log can be ");
2084 gsc_standout_string ("on");
2085 gsc_normal_string (", or ");
2086 gsc_standout_string ("off");
2087 gsc_normal_string (".\n");
2088 }
2089 }
2090
2091
2092 /*
2093 * gsc_command_abbreviations()
2094 *
2095 * Turn abbreviation expansions on and off.
2096 */
2097 static void
2098 gsc_command_abbreviations (const char *argument)
2099 {
2100 assert (argument);
2101
2102 if (sc_strcasecmp (argument, "on") == 0)
2103 {
2104 if (gsc_abbreviations_enabled)
2105 {
2106 gsc_normal_string ("Glk abbreviation expansions are already on.\n");
2107 return;
2108 }
2109
2110 gsc_abbreviations_enabled = TRUE;
2111 gsc_normal_string ("Glk abbreviation expansions are now on.\n");
2112 }
2113
2114 else if (sc_strcasecmp (argument, "off") == 0)
2115 {
2116 if (!gsc_abbreviations_enabled)
2117 {
2118 gsc_normal_string ("Glk abbreviation expansions are already off.\n");
2119 return;
2120 }
2121
2122 gsc_abbreviations_enabled = FALSE;
2123 gsc_normal_string ("Glk abbreviation expansions are now off.\n");
2124 }
2125
2126 else if (strlen (argument) == 0)
2127 {
2128 gsc_normal_string ("Glk abbreviation expansions are ");
2129 gsc_normal_string (gsc_abbreviations_enabled ? "on" : "off");
2130 gsc_normal_string (".\n");
2131 }
2132
2133 else
2134 {
2135 gsc_normal_string ("Glk abbreviation expansions can be ");
2136 gsc_standout_string ("on");
2137 gsc_normal_string (", or ");
2138 gsc_standout_string ("off");
2139 gsc_normal_string (".\n");
2140 }
2141 }
2142
2143
2144 /*
2145 * gsc_command_print_version_number()
2146 * gsc_command_version()
2147 *
2148 * Print out the Glk library version number.
2149 */
2150 static void
2151 gsc_command_print_version_number (glui32 version)
2152 {
2153 char buffer[64];
2154
2155 sprintf (buffer, "%lu.%lu.%lu",
2156 (unsigned long) version >> 16,
2157 (unsigned long) (version >> 8) & 0xff,
2158 (unsigned long) version & 0xff);
2159 gsc_normal_string (buffer);
2160 }
2161
2162 static void
2163 gsc_command_version (const char *argument)
2164 {
2165 glui32 version;
2166 assert (argument);
2167
2168 gsc_normal_string ("This is version ");
2169 gsc_command_print_version_number (GSC_PORT_VERSION);
2170 gsc_normal_string (" of the Glk SCARE port.\n");
2171
2172 version = glk_gestalt (gestalt_Version, 0);
2173 gsc_normal_string ("The Glk library version is ");
2174 gsc_command_print_version_number (version);
2175 gsc_normal_string (".\n");
2176 }
2177
2178
2179 /*
2180 * gsc_command_commands()
2181 *
2182 * Turn command escapes off. Once off, there's no way to turn them back on.
2183 * Commands must be on already to enter this function.
2184 */
2185 static void
2186 gsc_command_commands (const char *argument)
2187 {
2188 assert (argument);
2189
2190 if (sc_strcasecmp (argument, "on") == 0)
2191 {
2192 gsc_normal_string ("Glk commands are already on.\n");
2193 }
2194
2195 else if (sc_strcasecmp (argument, "off") == 0)
2196 {
2197 gsc_commands_enabled = FALSE;
2198 gsc_normal_string ("Glk commands are now off.\n");
2199 }
2200
2201 else if (strlen (argument) == 0)
2202 {
2203 gsc_normal_string ("Glk commands are ");
2204 gsc_normal_string (gsc_commands_enabled ? "on" : "off");
2205 gsc_normal_string (".\n");
2206 }
2207
2208 else
2209 {
2210 gsc_normal_string ("Glk commands can be ");
2211 gsc_standout_string ("on");
2212 gsc_normal_string (", or ");
2213 gsc_standout_string ("off");
2214 gsc_normal_string (".\n");
2215 }
2216 }
2217
2218
2219 /*
2220 * gsc_command_license()
2221 *
2222 * Print licensing terms.
2223 */
2224 static void
2225 gsc_command_license (const char *argument)
2226 {
2227 assert (argument);
2228
2229 gsc_normal_string ("This program is free software; you can redistribute it"
2230 " and/or modify it under the terms of version 2 of the"
2231 " GNU General Public License as published by the Free"
2232 " Software Foundation.\n\n");
2233
2234 gsc_normal_string ("This program is distributed in the hope that it will be"
2235 " useful, but ");
2236 gsc_standout_string ("WITHOUT ANY WARRANTY");
2237 gsc_normal_string ("; without even the implied warranty of ");
2238 gsc_standout_string ("MERCHANTABILITY");
2239 gsc_normal_string (" or ");
2240 gsc_standout_string ("FITNESS FOR A PARTICULAR PURPOSE");
2241 gsc_normal_string (". See the GNU General Public License for more"
2242 " details.\n\n");
2243
2244 gsc_normal_string ("You should have received a copy of the GNU General"
2245 " Public License along with this program; if not, write"
2246 " to the Free Software Foundation, Inc., 59 Temple"
2247 " Place, Suite 330, Boston, MA 02111-1307 USA\n\n");
2248
2249 gsc_normal_string ("Please report any bugs, omissions, or misfeatures to ");
2250 gsc_standout_string ("[email protected]");
2251 gsc_normal_string (".\n");
2252 }
2253
2254
2255 /* Glk subcommands and handler functions. */
2256 typedef const struct
2257 {
2258 const char * const command; /* Glk subcommand. */
2259 void (* const handler) (const char *argument); /* Subcommand handler. */
2260 const int takes_argument; /* Argument flag. */
2261 } gsc_command_t;
2262 typedef gsc_command_t *gsc_commandref_t;
2263
2264 static void gsc_command_summary (const char *argument);
2265 static void gsc_command_help (const char *argument);
2266
2267 static gsc_command_t GSC_COMMAND_TABLE[] = {
2268 {"summary", gsc_command_summary, FALSE},
2269 {"script", gsc_command_script, TRUE},
2270 {"inputlog", gsc_command_inputlog, TRUE},
2271 {"readlog", gsc_command_readlog, TRUE},
2272 {"abbreviations", gsc_command_abbreviations, TRUE},
2273 {"version", gsc_command_version, FALSE},
2274 {"commands", gsc_command_commands, TRUE},
2275 {"license", gsc_command_license, FALSE},
2276 {"help", gsc_command_help, TRUE},
2277 {NULL, NULL, FALSE}
2278 };
2279
2280
2281 /*
2282 * gsc_command_summary()
2283 *
2284 * Report all current Glk settings.
2285 */
2286 static void
2287 gsc_command_summary (const char *argument)
2288 {
2289 gsc_commandref_t entry;
2290 assert (argument);
2291
2292 /*
2293 * Call handlers that have status to report with an empty argument,
2294 * prompting each to print its current setting.
2295 */
2296 for (entry = GSC_COMMAND_TABLE; entry->command; entry++)
2297 {
2298 if (entry->handler == gsc_command_summary
2299 || entry->handler == gsc_command_license
2300 || entry->handler == gsc_command_help)
2301 continue;
2302
2303 entry->handler ("");
2304 }
2305 }
2306
2307
2308 /*
2309 * gsc_command_help()
2310 *
2311 * Document the available Glk commands.
2312 */
2313 static void
2314 gsc_command_help (const char *command)
2315 {
2316 gsc_commandref_t entry, matched;
2317 assert (command);
2318
2319 if (strlen (command) == 0)
2320 {
2321 gsc_normal_string ("Glk commands are");
2322 for (entry = GSC_COMMAND_TABLE; entry->command; entry++)
2323 {
2324 gsc_commandref_t next;
2325
2326 next = entry + 1;
2327 gsc_normal_string (next->command ? " " : " and ");
2328 gsc_standout_string (entry->command);
2329 gsc_normal_string (next->command ? "," : ".\n\n");
2330 }
2331
2332 gsc_normal_string ("Glk commands may be abbreviated, as long as"
2333 " the abbreviation is unambiguous. Use ");
2334 gsc_standout_string ("glk help");
2335 gsc_normal_string (" followed by a Glk command name for help on that"
2336 " command.\n");
2337 return;
2338 }
2339
2340 matched = NULL;
2341 for (entry = GSC_COMMAND_TABLE; entry->command; entry++)
2342 {
2343 if (sc_strncasecmp (command, entry->command, strlen (command)) == 0)
2344 {
2345 if (matched)
2346 {
2347 gsc_normal_string ("The Glk command ");
2348 gsc_standout_string (command);
2349 gsc_normal_string (" is ambiguous. Try ");
2350 gsc_standout_string ("glk help");
2351 gsc_normal_string (" for more information.\n");
2352 return;
2353 }
2354 matched = entry;
2355 }
2356 }
2357 if (!matched)
2358 {
2359 gsc_normal_string ("The Glk command ");
2360 gsc_standout_string (command);
2361 gsc_normal_string (" is not valid. Try ");
2362 gsc_standout_string ("glk help");
2363 gsc_normal_string (" for more information.\n");
2364 return;
2365 }
2366
2367 if (matched->handler == gsc_command_summary)
2368 {
2369 gsc_normal_string ("Prints a summary of all the current Glk SCARE"
2370 " settings.\n");
2371 }
2372
2373 else if (matched->handler == gsc_command_script)
2374 {
2375 gsc_normal_string ("Logs the game's output to a file.\n\nUse ");
2376 gsc_standout_string ("glk script on");
2377 gsc_normal_string (" to begin logging game output, and ");
2378 gsc_standout_string ("glk script off");
2379 gsc_normal_string (" to end it. Glk SCARE will ask you for a file"
2380 " when you turn scripts on.\n");
2381 }
2382
2383 else if (matched->handler == gsc_command_inputlog)
2384 {
2385 gsc_normal_string ("Records the commands you type into a game.\n\nUse ");
2386 gsc_standout_string ("glk inputlog on");
2387 gsc_normal_string (", to begin recording your commands, and ");
2388 gsc_standout_string ("glk inputlog off");
2389 gsc_normal_string (" to turn off input logs. You can play back"
2390 " recorded commands into a game with the ");
2391 gsc_standout_string ("glk readlog");
2392 gsc_normal_string (" command.\n");
2393 }
2394
2395 else if (matched->handler == gsc_command_readlog)
2396 {
2397 gsc_normal_string ("Plays back commands recorded with ");
2398 gsc_standout_string ("glk inputlog on");
2399 gsc_normal_string (".\n\nUse ");
2400 gsc_standout_string ("glk readlog on");
2401 gsc_normal_string (". Command play back stops at the end of the"
2402 " file. You can also play back commands from a"
2403 " text file created using any standard editor.\n");
2404 }
2405
2406 else if (matched->handler == gsc_command_abbreviations)
2407 {
2408 gsc_normal_string ("Controls abbreviation expansion.\n\nGlk SCARE"
2409 " automatically expands several standard single"
2410 " letter abbreviations for you; for example, \"x\""
2411 " becomes \"examine\". Use ");
2412 gsc_standout_string ("glk abbreviations on");
2413 gsc_normal_string (" to turn this feature on, and ");
2414 gsc_standout_string ("glk abbreviations off");
2415 gsc_normal_string (" to turn it off. While the feature is on, you"
2416 " can bypass abbreviation expansion for an"
2417 " individual game command by prefixing it with a"
2418 " single quote.\n");
2419 }
2420
2421 else if (matched->handler == gsc_command_version)
2422 {
2423 gsc_normal_string ("Prints the version numbers of the Glk library"
2424 " and the Glk SCARE port.\n");
2425 }
2426
2427 else if (matched->handler == gsc_command_commands)
2428 {
2429 gsc_normal_string ("Turn off Glk commands.\n\nUse ");
2430 gsc_standout_string ("glk commands off");
2431 gsc_normal_string (" to disable all Glk commands, including this one."
2432 " Once turned off, there is no way to turn Glk"
2433 " commands back on while inside the game.\n");
2434 }
2435
2436 else if (matched->handler == gsc_command_license)
2437 {
2438 gsc_normal_string ("Prints Glk SCARE's software license.\n");
2439 }
2440
2441 else if (matched->handler == gsc_command_help)
2442 gsc_command_help ("");
2443
2444 else
2445 gsc_normal_string ("There is no help available on that Glk command."
2446 " Sorry.\n");
2447 }
2448
2449
2450 /*
2451 * gsc_command_escape()
2452 *
2453 * This function is handed each input line. If the line contains a specific
2454 * Glk port command, handle it and return TRUE, otherwise return FALSE.
2455 */
2456 static int
2457 gsc_command_escape (const char *string)
2458 {
2459 int posn;
2460 char *string_copy, *command, *argument;
2461 assert (string);
2462
2463 /*
2464 * Return FALSE if the string doesn't begin with the Glk command escape
2465 * introducer.
2466 */
2467 posn = strspn (string, "\t ");
2468 if (sc_strncasecmp (string + posn, "glk", strlen ("glk")) != 0)
2469 return FALSE;
2470
2471 /* Take a copy of the string, without any leading space or introducer. */
2472 string_copy = gsc_malloc (strlen (string + posn) + 1 - strlen ("glk"));
2473 strcpy (string_copy, string + posn + strlen ("glk"));
2474
2475 /*
2476 * Find the subcommand; the first word in the string copy. Find its end,
2477 * and ensure it terminates with a NUL.
2478 */
2479 posn = strspn (string_copy, "\t ");
2480 command = string_copy + posn;
2481 posn += strcspn (string_copy + posn, "\t ");
2482 if (string_copy[posn] != '\0')
2483 string_copy[posn++] = '\0';
2484
2485 /*
2486 * Now find any argument data for the command, ensuring it too terminates
2487 * with a NUL.
2488 */
2489 posn += strspn (string_copy + posn, "\t ");
2490 argument = string_copy + posn;
2491 posn += strcspn (string_copy + posn, "\t ");
2492 string_copy[posn] = '\0';
2493
2494 /*
2495 * Try to handle the command and argument as a Glk subcommand. If it
2496 * doesn't run unambiguously, print command usage. Treat an empty command
2497 * as "help".
2498 */
2499 if (strlen (command) > 0)
2500 {
2501 gsc_commandref_t entry, matched;
2502 int matches;
2503
2504 /*
2505 * Search for the first unambiguous table command string matching
2506 * the command passed in.
2507 */
2508 matches = 0;
2509 matched = NULL;
2510 for (entry = GSC_COMMAND_TABLE; entry->command; entry++)
2511 {
2512 if (sc_strncasecmp (command, entry->command, strlen (command)) == 0)
2513 {
2514 matches++;
2515 matched = entry;
2516 }
2517 }
2518
2519 /* If the match was unambiguous, call the command handler. */
2520 if (matches == 1)
2521 {
2522 gsc_normal_char ('\n');
2523 matched->handler (argument);
2524
2525 if (!matched->takes_argument && strlen (argument) > 0)
2526 {
2527 gsc_normal_string ("[The ");
2528 gsc_standout_string (matched->command);
2529 gsc_normal_string (" command ignores arguments.]\n");
2530 }
2531 }
2532
2533 /* No match, or the command was ambiguous. */
2534 else
2535 {
2536 gsc_normal_string ("\nThe Glk command ");
2537 gsc_standout_string (command);
2538 gsc_normal_string (" is ");
2539 gsc_normal_string (matches == 0 ? "not valid" : "ambiguous");
2540 gsc_normal_string (". Try ");
2541 gsc_standout_string ("glk help");
2542 gsc_normal_string (" for more information.\n");
2543 }
2544 }
2545 else
2546 {
2547 gsc_normal_char ('\n');
2548 gsc_command_help ("");
2549 }
2550
2551 /* The string contained a Glk command; return TRUE. */
2552 free (string_copy);
2553 return TRUE;
2554 }
2555
2556
2557 /*---------------------------------------------------------------------*/
2558 /* Glk port input functions */
2559 /*---------------------------------------------------------------------*/
2560
2561 /* Quote used to suppress abbreviation expansion and local commands. */
2562 static const char GSC_QUOTED_INPUT = '\'';
2563
2564
2565 /* Table of single-character command abbreviations. */
2566 typedef const struct
2567 {
2568 const char abbreviation; /* Abbreviation character. */
2569 const char *const expansion; /* Expansion string. */
2570 } gsc_abbreviation_t;
2571 typedef gsc_abbreviation_t *gsc_abbreviationref_t;
2572
2573 static gsc_abbreviation_t GSC_ABBREVIATIONS[] = {
2574 {'c', "close"}, {'g', "again"}, {'i', "inventory"},
2575 {'k', "attack"}, {'l', "look"}, {'p', "open"},
2576 {'q', "quit"}, {'r', "drop"}, {'t', "take"},
2577 {'x', "examine"}, {'y', "yes"}, {'z', "wait"},
2578 {'\0', NULL}
2579 };
2580
2581
2582 /*
2583 * gsc_expand_abbreviations()
2584 *
2585 * Expand a few common one-character abbreviations commonly found in other
2586 * game systems.
2587 */
2588 static void
2589 gsc_expand_abbreviations (char *buffer, int size)
2590 {
2591 char *command, abbreviation;
2592 const char *expansion;
2593 gsc_abbreviationref_t entry;
2594 assert (buffer);
2595
2596 /* Ignore anything that isn't a single letter command. */
2597 command = buffer + strspn (buffer, "\t ");
2598 if (!(strlen (command) == 1
2599 || (strlen (command) > 1 && isspace (command[1]))))
2600 return;
2601
2602 /* Scan the abbreviations table for a match. */
2603 abbreviation = glk_char_to_lower ((unsigned char) command[0]);
2604 expansion = NULL;
2605 for (entry = GSC_ABBREVIATIONS; entry->expansion; entry++)
2606 {
2607 if (entry->abbreviation == abbreviation)
2608 {
2609 expansion = entry->expansion;
2610 break;
2611 }
2612 }
2613
2614 /*
2615 * If a match found, check for a fit, then replace the character with the
2616 * expansion string.
2617 */
2618 if (expansion)
2619 {
2620 if (strlen (buffer) + strlen (expansion) - 1 >= (unsigned int) size)
2621 return;
2622
2623 memmove (command + strlen (expansion) - 1, command, strlen (command) + 1);
2624 memcpy (command, expansion, strlen (expansion));
2625
2626 gsc_standout_string ("[");
2627 gsc_standout_char (abbreviation);
2628 gsc_standout_string (" -> ");
2629 gsc_standout_string (expansion);
2630 gsc_standout_string ("]\n");
2631 }
2632 }
2633
2634
2635 /*
2636 * os_read_line()
2637 *
2638 * Read and return a line of player input.
2639 */
2640 sc_bool
2641 os_read_line (sc_char *buffer, sc_int length)
2642 {
2643 sc_int characters;
2644 assert (buffer && length > 0);
2645
2646 /* If a help request is pending, provide a user hint. */
2647 gsc_output_provide_help_hint ();
2648
2649 /*
2650 * Ensure normal style, update the status line, and issue an input prompt.
2651 */
2652 gsc_reset_glk_style ();
2653 gsc_status_notify ();
2654 glk_put_string (">");
2655
2656 /*
2657 * If we have an input log to read from, use that until it is exhausted.
2658 * On end of file, close the stream and resume input from line requests.
2659 */
2660 if (gsc_readlog_stream)
2661 {
2662 glui32 chars;
2663
2664 /* Get the next line from the log stream. */
2665 chars = glk_get_line_stream (gsc_readlog_stream, buffer, length);
2666 if (chars > 0)
2667 {
2668 /* Echo the line just read in input style. */
2669 glk_set_style (style_Input);
2670 gsc_put_buffer (buffer, chars);
2671 glk_set_style (style_Normal);
2672
2673 /* Return this line as player input. */
2674 return TRUE;
2675 }
2676
2677 /*
2678 * We're at the end of the log stream. Close it, and then continue
2679 * on to request a line from Glk.
2680 */
2681 glk_stream_close (gsc_readlog_stream, NULL);
2682 gsc_readlog_stream = NULL;
2683 }
2684
2685 /*
2686 * No input log being read, or we just hit the end of file on one. Revert
2687 * to normal line input; start by getting a new line from Glk.
2688 */
2689 characters = gsc_read_line (buffer, length - 1);
2690 assert (characters <= length);
2691 buffer[characters] = '\0';
2692
2693 /*
2694 * If neither abbreviations nor local commands are enabled, use the data
2695 * read above without further massaging.
2696 */
2697 if (gsc_abbreviations_enabled || gsc_commands_enabled)
2698 {
2699 char *command;
2700
2701 /*
2702 * If the first non-space input character is a quote, bypass all
2703 * abbreviation expansion and local command recognition, and use the
2704 * unadulterated input, less introductory quote.
2705 */
2706 command = buffer + strspn (buffer, "\t ");
2707 if (command[0] == GSC_QUOTED_INPUT)
2708 {
2709 /* Delete the quote with memmove(). */
2710 memmove (command, command + 1, strlen (command));
2711 }
2712 else
2713 {
2714 /* Check for, and expand, and abbreviated commands. */
2715 if (gsc_abbreviations_enabled)
2716 gsc_expand_abbreviations (buffer, length);
2717
2718 /*
2719 * Check for standalone "help", then for Glk port special commands;
2720 * suppress the interpreter's use of this input for Glk commands by
2721 * returning FALSE.
2722 */
2723 if (gsc_commands_enabled)
2724 {
2725 int posn;
2726
2727 posn = strspn (buffer, "\t ");
2728 if (sc_strncasecmp (buffer + posn, "help", strlen ("help"))== 0)
2729 {
2730 if (strspn (buffer + posn + strlen ("help"), "\t ")
2731 == strlen (buffer + posn + strlen ("help")))
2732 {
2733 gsc_output_register_help_request ();
2734 }
2735 }
2736
2737 if (gsc_command_escape (buffer))
2738 {
2739 gsc_output_silence_help_hints ();
2740 return FALSE;
2741 }
2742 }
2743 }
2744 }
2745
2746 /*
2747 * If there is an input log active, log this input string to it. Note that
2748 * by logging here we get any abbreviation expansions but we won't log glk
2749 * special commands, nor any input read from a current open input log.
2750 */
2751 if (gsc_inputlog_stream)
2752 {
2753 glk_put_string_stream (gsc_inputlog_stream, buffer);
2754 glk_put_char_stream (gsc_inputlog_stream, '\n');
2755 }
2756
2757 return TRUE;
2758 }
2759
2760
2761 /*
2762 * os_read_line_debug()
2763 *
2764 * Read and return a debugger command line. There's no dedicated debugging
2765 * window, so this is just a call to the normal readline, with an additional
2766 * prompt.
2767 */
2768 sc_bool
2769 os_read_line_debug (sc_char *buffer, sc_int length)
2770 {
2771 gsc_output_silence_help_hints ();
2772 gsc_reset_glk_style ();
2773 glk_put_string ("[SCARE debug]");
2774 return os_read_line (buffer, length);
2775 }
2776
2777
2778 /*
2779 * os_confirm()
2780 *
2781 * Confirm a game action with a yes/no prompt.
2782 */
2783 sc_bool
2784 os_confirm (sc_int type)
2785 {
2786 sc_char response;
2787
2788 /*
2789 * Always allow game saves and hint display, and if we're reading from an
2790 * input log, allow everything no matter what, on the assumption that the
2791 * user knows what they are doing.
2792 */
2793 if (gsc_readlog_stream
2794 || type == SC_CONF_SAVE || type == SC_CONF_VIEW_HINTS)
2795 return TRUE;
2796
2797 /* Ensure back to normal style, and update status. */
2798 gsc_reset_glk_style ();
2799 gsc_status_notify ();
2800
2801 /* Prompt for the confirmation, based on the type. */
2802 if (type == GSC_CONF_SUBTLE_HINT)
2803 glk_put_string ("View the subtle hint for this topic");
2804 else if (type == GSC_CONF_UNSUBTLE_HINT)
2805 glk_put_string ("View the unsubtle hint for this topic");
2806 else if (type == GSC_CONF_CONTINUE_HINTS)
2807 glk_put_string ("Continue with hints");
2808 else
2809 {
2810 glk_put_string ("Do you really want to ");
2811 switch (type)
2812 {
2813 case SC_CONF_QUIT:
2814 glk_put_string ("quit");
2815 break;
2816 case SC_CONF_RESTART:
2817 glk_put_string ("restart");
2818 break;
2819 case SC_CONF_SAVE:
2820 glk_put_string ("save");
2821 break;
2822 case SC_CONF_RESTORE:
2823 glk_put_string ("restore");
2824 break;
2825 case SC_CONF_VIEW_HINTS:
2826 glk_put_string ("view hints");
2827 break;
2828 default:
2829 glk_put_string ("do that");
2830 break;
2831 }
2832 }
2833 glk_put_string ("? ");
2834
2835 /* Loop until 'yes' or 'no' returned. */
2836 do
2837 {
2838 event_t event;
2839
2840 /* Wait for a standard key, ignoring Glk special keys. */
2841 do
2842 {
2843 glk_request_char_event (gsc_main_window);
2844 gsc_event_wait (evtype_CharInput, &event);
2845 }
2846 while (event.val1 > UCHAR_MAX);
2847 response = glk_char_to_upper (event.val1);
2848 }
2849 while (response != 'Y' && response != 'N');
2850
2851 /* Echo the confirmation response, and a new line. */
2852 glk_set_style (style_Input);
2853 glk_put_string (response == 'Y' ? "Yes" : "No");
2854 glk_set_style (style_Normal);
2855 glk_put_char ('\n');
2856
2857 /* Use a short delay on restarts, if confirmed. */
2858 if (type == SC_CONF_RESTART && response == 'Y')
2859 gsc_short_delay ();
2860
2861 /* Return TRUE if 'Y' was entered. */
2862 return (response == 'Y');
2863 }
2864
2865
2866 /*---------------------------------------------------------------------*/
2867 /* Glk port event functions */
2868 /*---------------------------------------------------------------------*/
2869
2870 /* Short delay before restarts; 1s, in 100ms segments. */
2871 static const glui32 GSC_DELAY_TIMEOUT = 100;
2872 static const glui32 GSC_DELAY_TIMEOUTS_COUNT = 10;
2873
2874 /*
2875 * gsc_short_delay()
2876 *
2877 * Delay for a short period; used before restarting a completed game, to
2878 * improve the display where 'r', or confirming restart, triggers an otherwise
2879 * immediate, and abrupt, restart.
2880 */
2881 static void
2882 gsc_short_delay (void)
2883 {
2884 /* Ignore the call if the Glk doesn't have timers. */
2885 if (glk_gestalt (gestalt_Timer, 0))
2886 {
2887 glui32 timeout;
2888
2889 /* Timeout in small chunks to minimize Glk jitter. */
2890 glk_request_timer_events (GSC_DELAY_TIMEOUT);
2891 for (timeout = 0; timeout < GSC_DELAY_TIMEOUTS_COUNT; timeout++)
2892 {
2893 event_t event;
2894
2895 gsc_event_wait (evtype_Timer, &event);
2896 }
2897 glk_request_timer_events (0);
2898 }
2899 }
2900
2901
2902 /*
2903 * gsc_event_wait_2()
2904 * gsc_event_wait()
2905 *
2906 * Process Glk events until one of the expected type, or types, arrives.
2907 * Return the event of that type.
2908 */
2909 static void
2910 gsc_event_wait_2 (glui32 wait_type_1, glui32 wait_type_2, event_t * event)
2911 {
2912 assert (event);
2913
2914 do
2915 {
2916 glk_select (event);
2917
2918 switch (event->type)
2919 {
2920 case evtype_Arrange:
2921 case evtype_Redraw:
2922 /* Refresh any sensitive windows on size events. */
2923 gsc_status_redraw ();
2924 break;
2925 }
2926 }
2927 while (!(event->type == wait_type_1 || event->type == wait_type_2));
2928 }
2929
2930 static void
2931 gsc_event_wait (glui32 wait_type, event_t * event)
2932 {
2933 assert (event);
2934
2935 gsc_event_wait_2 (wait_type, evtype_None, event);
2936 }
2937
2938
2939 /*---------------------------------------------------------------------*/
2940 /* Glk port file functions */
2941 /*---------------------------------------------------------------------*/
2942
2943 /*
2944 * os_open_file()
2945 *
2946 * Open a file for save or restore, and return a Glk stream for the opened
2947 * file.
2948 */
2949 void *
2950 os_open_file (sc_bool is_save)
2951 {
2952 glui32 usage, fmode;
2953 frefid_t fileref;
2954 strid_t stream;
2955
2956 usage = fileusage_SavedGame | fileusage_BinaryMode;
2957 fmode = is_save ? filemode_Write : filemode_Read;
2958
2959 fileref = glk_fileref_create_by_prompt (usage, fmode, 0);
2960 if (!fileref)
2961 return NULL;
2962
2963 if (!is_save && !glk_fileref_does_file_exist (fileref))
2964 {
2965 glk_fileref_destroy (fileref);
2966 return NULL;
2967 }
2968
2969 stream = glk_stream_open_file (fileref, fmode, 0);
2970 glk_fileref_destroy (fileref);
2971
2972 return stream;
2973 }
2974
2975
2976 /*
2977 * os_write_file()
2978 * os_read_file()
2979 *
2980 * Write/read the given buffered data to/from the open Glk stream.
2981 */
2982 void
2983 os_write_file (void *opaque, const sc_byte *buffer, sc_int length)
2984 {
2985 strid_t stream = (strid_t) opaque;
2986 assert (opaque && buffer);
2987
2988 glk_put_buffer_stream (stream, (char *) buffer, length);
2989 }
2990
2991 sc_int
2992 os_read_file (void *opaque, sc_byte *buffer, sc_int length)
2993 {
2994 strid_t stream = (strid_t) opaque;
2995 assert (opaque && buffer);
2996
2997 return glk_get_buffer_stream (stream, (char *) buffer, length);
2998 }
2999
3000
3001 /*
3002 * os_close_file()
3003 *
3004 * Close the opened Glk stream.
3005 */
3006 void
3007 os_close_file (void *opaque)
3008 {
3009 strid_t stream = (strid_t) opaque;
3010 assert (opaque);
3011
3012 glk_stream_close (stream, NULL);
3013 }
3014
3015
3016 /*---------------------------------------------------------------------*/
3017 /* main() and options parsing */
3018 /*---------------------------------------------------------------------*/
3019
3020 /* Loading message flush delay timeout. */
3021 static const glui32 GSC_LOADING_TIMEOUT = 100;
3022
3023 /* Enumerated game end options. */
3024 enum gsc_end_option { GAME_RESTART, GAME_UNDO, GAME_QUIT };
3025
3026 /*
3027 * The following value needs to be passed between the startup_code and main
3028 * functions.
3029 */
3030 static char *gsc_game_message = NULL;
3031
3032
3033 /*
3034 * gsc_callback()
3035 *
3036 * Callback function for reading in game and restore file data; fills a
3037 * buffer with TAF or TAS file data from a Glk stream, and returns the byte
3038 * count.
3039 */
3040 static sc_int
3041 gsc_callback (void *opaque, sc_byte *buffer, sc_int length)
3042 {
3043 strid_t stream = (strid_t) opaque;
3044 assert (stream);
3045
3046 return glk_get_buffer_stream (stream, (char *) buffer, length);
3047 }
3048
3049
3050 /*
3051 * gsc_get_ending_option()
3052 *
3053 * Offer the option to restart, undo, or quit. Returns the selected game
3054 * end option. Called on game completion.
3055 */
3056 static enum gsc_end_option
3057 gsc_get_ending_option (void)
3058 {
3059 sc_char response;
3060
3061 /* Ensure back to normal style, and update status. */
3062 gsc_reset_glk_style ();
3063 gsc_status_notify ();
3064
3065 /* Prompt for restart, undo, or quit. */
3066 glk_put_string ("\nWould you like to RESTART, UNDO a turn, or QUIT? ");
3067
3068 /* Loop until 'restart', 'undo' or 'quit'. */
3069 do
3070 {
3071 event_t event;
3072
3073 do
3074 {
3075 glk_request_char_event (gsc_main_window);
3076 gsc_event_wait (evtype_CharInput, &event);
3077 }
3078 while (event.val1 > UCHAR_MAX);
3079 response = glk_char_to_upper (event.val1);
3080 }
3081 while (response != 'R' && response != 'U' && response != 'Q');
3082
3083 /* Echo the confirmation response, and a new line. */
3084 glk_set_style (style_Input);
3085 switch (response)
3086 {
3087 case 'R':
3088 glk_put_string ("Restart");
3089 break;
3090 case 'U':
3091 glk_put_string ("Undo");
3092 break;
3093 case 'Q':
3094 glk_put_string ("Quit");
3095 break;
3096 default:
3097 gsc_fatal ("GLK: Invalid response encountered");
3098 glk_exit ();
3099 }
3100 glk_set_style (style_Normal);
3101 glk_put_char ('\n');
3102
3103 /* Return the appropriate value for response. */
3104 switch (response)
3105 {
3106 case 'R':
3107 return GAME_RESTART;
3108 case 'U':
3109 return GAME_UNDO;
3110 case 'Q':
3111 return GAME_QUIT;
3112 default:
3113 gsc_fatal ("GLK: Invalid response encountered");
3114 glk_exit ();
3115 }
3116
3117 /* Unreachable; supplied to suppress compiler warning. */
3118 return GAME_QUIT;
3119 }
3120
3121
3122 /*
3123 * gsc_startup_code()
3124 * gsc_main
3125 *
3126 * Together, these functions take the place of the original main(). The
3127 * first one is called from the platform-specific startup_code(), to parse
3128 * and generally handle options. The second is called from glk_main, and
3129 * does the real work of running the game.
3130 */
3131 static int
3132 gsc_startup_code (strid_t game_stream, strid_t restore_stream,
3133 sc_uint trace_flags, sc_bool enable_debugger,
3134 sc_bool stable_random, const sc_char *locale)
3135 {
3136 winid_t window;
3137 assert (game_stream);
3138
3139 /* Open a temporary Glk main window. */
3140 window = glk_window_open (0, 0, 0, wintype_TextBuffer, 0);
3141 if (window)
3142 {
3143 /* Clear and initialize the temporary window. */
3144 glk_window_clear (window);
3145 glk_set_window (window);
3146 glk_set_style (style_Normal);
3147
3148 /*
3149 * Display a brief loading game message; here we have to use a timeout
3150 * to ensure that the text is flushed to Glk.
3151 */
3152 glk_put_string ("Loading game...\n");
3153 if (glk_gestalt (gestalt_Timer, 0))
3154 {
3155 event_t event;
3156
3157 glk_request_timer_events (GSC_LOADING_TIMEOUT);
3158 do
3159 {
3160 glk_select (&event);
3161 }
3162 while (event.type != evtype_Timer);
3163 glk_request_timer_events (0);
3164 }
3165 }
3166
3167 /* If the Glk libarary does not support unicode, disable it. */
3168 if (!gsc_has_unicode || !glk_gestalt (gestalt_Unicode, 0))
3169 gsc_unicode_enabled = FALSE;
3170
3171 /*
3172 * If a locale was requested, set it in the core interpreter now. This
3173 * locale will preempt any auto-detected one found from inspecting the
3174 * game on creation. After game creation, the Glk locale is synchronized
3175 * to the core interpreter's locale.
3176 */
3177 if (locale)
3178 sc_set_locale (locale);
3179
3180 /*
3181 * Set tracing flags, then try to create a SCARE game reference from the
3182 * TAF file. Since we need this in our call from glk_main, we have to keep
3183 * it in a module static variable. If we can't open the TAF file, then
3184 * we'll set the pointer to NULL, and complain about it later in main.
3185 * Passing the message string around like this is a nuisance...
3186 */
3187 sc_set_trace_flags (trace_flags);
3188 gsc_game = sc_game_from_callback (gsc_callback, game_stream);
3189 if (!gsc_game)
3190 {
3191 gsc_game = NULL;
3192 gsc_game_message = "Unable to load an Adrift game from the"
3193 " requested file.";
3194 }
3195 else
3196 gsc_game_message = NULL;
3197 glk_stream_close (game_stream, NULL);
3198
3199 /*
3200 * If the game was created successfully and there is a restore stream, try
3201 * to immediately restore the game from that stream.
3202 */
3203 if (gsc_game && restore_stream)
3204 {
3205 if (!sc_load_game_from_callback (gsc_game, gsc_callback, restore_stream))
3206 {
3207 sc_free_game (gsc_game);
3208 gsc_game = NULL;
3209 gsc_game_message = "Unable to restore this Adrift game from the"
3210 " requested file.";
3211 }
3212 else
3213 gsc_game_message = NULL;
3214 }
3215 if (restore_stream)
3216 glk_stream_close (restore_stream, NULL);
3217
3218 /* If successful, set game debugging and synchronize to the core's locale. */
3219 if (gsc_game)
3220 {
3221 sc_set_game_debugger_enabled (gsc_game, enable_debugger);
3222 gsc_set_locale (sc_get_locale ());
3223 }
3224
3225 /* Set portable and predictable random number generation if requested. */
3226 if (stable_random)
3227 {
3228 sc_set_portable_random (TRUE);
3229 sc_reseed_random_sequence (1);
3230 }
3231
3232 /* Close the temporary window. */
3233 if (window)
3234 glk_window_close (window, NULL);
3235
3236 /* Set title of game */
3237 #ifdef GARGLK
3238 garglk_set_story_name(sc_get_game_name(gsc_game));
3239 #endif
3240
3241 /* Game set up, perhaps successfully. */
3242 return TRUE;
3243 }
3244
3245 static void
3246 gsc_main (void)
3247 {
3248 sc_bool is_running;
3249
3250 /* Ensure SCARE internal types have the right sizes. */
3251 if (!(sizeof (sc_byte) == 1 && sizeof (sc_char) == 1
3252 && sizeof (sc_uint) >= 4 && sizeof (sc_int) >= 4
3253 && sizeof (sc_uint) <= 8 && sizeof (sc_int) <= 8))
3254 {
3255 gsc_fatal ("GLK: Types sized incorrectly, recompilation is needed");
3256 glk_exit ();
3257 }
3258
3259 /* Create the Glk window, and set its stream as the current one. */
3260 gsc_main_window = glk_window_open (0, 0, 0, wintype_TextBuffer, 0);
3261 if (!gsc_main_window)
3262 {
3263 gsc_fatal ("GLK: Can't open main window");
3264 glk_exit ();
3265 }
3266 glk_window_clear (gsc_main_window);
3267 glk_set_window (gsc_main_window);
3268 glk_set_style (style_Normal);
3269
3270 /* If there's a problem with the game file, complain now. */
3271 if (!gsc_game)
3272 {
3273 assert (gsc_game_message);
3274 gsc_header_string ("Glk SCARE Error\n\n");
3275 gsc_normal_string (gsc_game_message);
3276 gsc_normal_char ('\n');
3277 glk_exit ();
3278 }
3279
3280 /* Try to create a one-line status window. We can live without it. */
3281 glk_stylehint_set (wintype_TextGrid, style_User1, stylehint_ReverseColor, 1);
3282 gsc_status_window = glk_window_open (gsc_main_window,
3283 winmethod_Above | winmethod_Fixed,
3284 1, wintype_TextGrid, 0);
3285
3286 /* Repeat the game until no more restarts requested. */
3287 is_running = TRUE;
3288 while (is_running)
3289 {
3290 /* Run the game until it ends, or the user quits. */
3291 gsc_status_notify ();
3292 sc_interpret_game (gsc_game);
3293
3294 /*
3295 * If the game did not complete, the user quit explicitly, so leave the
3296 * game repeat loop.
3297 */
3298 if (!sc_has_game_completed (gsc_game))
3299 {
3300 is_running = FALSE;
3301 break;
3302 }
3303
3304 /*
3305 * If reading from an input log, close it now. We need to request a
3306 * user selection, probably modal, and after that we probably don't
3307 * want the follow-on readlog data being used as game input.
3308 */
3309 if (gsc_readlog_stream)
3310 {
3311 glk_stream_close (gsc_readlog_stream, NULL);
3312 gsc_readlog_stream = NULL;
3313 }
3314
3315 /*
3316 * Get user selection of restart, undo a turn, or quit completed game.
3317 * If undo is unavailable (this should not be possible), degrade to
3318 * restart.
3319 */
3320 switch (gsc_get_ending_option ())
3321 {
3322 case GAME_RESTART:
3323 gsc_short_delay ();
3324 sc_restart_game (gsc_game);
3325 break;
3326
3327 case GAME_UNDO:
3328 if (sc_is_game_undo_available (gsc_game))
3329 {
3330 sc_undo_game_turn (gsc_game);
3331 gsc_normal_string ("The previous turn has been undone.\n");
3332 }
3333 else
3334 {
3335 gsc_normal_string ("Sorry, no undo is available.\n");
3336 gsc_short_delay ();
3337 sc_restart_game (gsc_game);
3338 }
3339 break;
3340
3341 case GAME_QUIT:
3342 is_running = FALSE;
3343 break;
3344 }
3345 }
3346
3347 /* All done -- release game resources. */
3348 sc_free_game (gsc_game);
3349
3350 /* Close any open transcript, input log, and/or read log. */
3351 if (gsc_transcript_stream)
3352 {
3353 glk_stream_close (gsc_transcript_stream, NULL);
3354 gsc_transcript_stream = NULL;
3355 }
3356 if (gsc_inputlog_stream)
3357 {
3358 glk_stream_close (gsc_inputlog_stream, NULL);
3359 gsc_inputlog_stream = NULL;
3360 }
3361 if (gsc_readlog_stream)
3362 {
3363 glk_stream_close (gsc_readlog_stream, NULL);
3364 gsc_readlog_stream = NULL;
3365 }
3366 }
3367
3368
3369 /*---------------------------------------------------------------------*/
3370 /* Linkage between Glk entry/exit calls and the real interpreter */
3371 /*---------------------------------------------------------------------*/
3372
3373 /*
3374 * Safety flags, to ensure we always get startup before main, and that
3375 * we only get a call to main once.
3376 */
3377 static int gsc_startup_called = FALSE,
3378 gsc_main_called = FALSE;
3379
3380 /*
3381 * glk_main()
3382 *
3383 * Main entry point for Glk. Here, all startup is done, and we call our
3384 * function to run the game, or to report errors if gsc_game_message is set.
3385 */
3386 void
3387 glk_main (void)
3388 {
3389 assert (gsc_startup_called && !gsc_main_called);
3390 gsc_main_called = TRUE;
3391
3392 /* Call the generic interpreter main function. */
3393 gsc_main ();
3394 }
3395
3396
3397 /*---------------------------------------------------------------------*/
3398 /* Glk linkage relevant only to the UNIX platform */
3399 /*---------------------------------------------------------------------*/
3400 #ifdef TRUE
3401
3402 #include "glkstart.h"
3403
3404 /*
3405 * Glk arguments for UNIX versions of the Glk interpreter.
3406 */
3407 glkunix_argumentlist_t glkunix_arguments[] = {
3408 {(char *) "-nc", glkunix_arg_NoValue,
3409 (char *) "-nc No local handling for Glk special commands"},
3410 {(char *) "-na", glkunix_arg_NoValue,
3411 (char *) "-na Turn off abbreviation expansions"},
3412 {(char *) "-nu", glkunix_arg_NoValue,
3413 (char *) "-nu Turn off any use of Unicode output"},
3414 #ifdef LINUX_GRAPHICS
3415 {(char *) "-ng", glkunix_arg_NoValue,
3416 (char *) "-ng Turn off attempts at game graphics"},
3417 #endif
3418 {(char *) "-r", glkunix_arg_ValueFollows,
3419 (char *) "-r FILE Restore from FILE on starting the game"},
3420 {(char *) "", glkunix_arg_ValueCanFollow,
3421 (char *) "filename game to run"},
3422 {NULL, glkunix_arg_End, NULL}
3423 };
3424
3425
3426 /*
3427 * glkunix_startup_code()
3428 *
3429 * Startup entry point for UNIX versions of Glk interpreter. Glk will call
3430 * glkunix_startup_code() to pass in arguments. On startup, parse arguments
3431 * and open a Glk stream to the game, then call the generic gsc_startup_code()
3432 * to build a game from the stream. On error, set the message in
3433 * gsc_game_message; the core gsc_main() will report it when it's called.
3434 */
3435 int
3436 glkunix_startup_code (glkunix_startup_t * data)
3437 {
3438 int argc = data->argc;
3439 sc_char **argv = data->argv;
3440 int argv_index;
3441 sc_char *restore_from;
3442 const sc_char *locale;
3443 strid_t game_stream, restore_stream;
3444 sc_uint trace_flags;
3445 sc_bool enable_debugger, stable_random;
3446 assert (!gsc_startup_called);
3447 gsc_startup_called = TRUE;
3448
3449 #ifdef GARGLK
3450 garglk_set_program_name("SCARE " SCARE_VERSION);
3451 garglk_set_program_info("SCARE " SCARE_VERSION
3452 " by Simon Baldwin and Mark J. Tilford");
3453 #endif
3454
3455 /* Handle command line arguments. */
3456 restore_from = NULL;
3457 for (argv_index = 1;
3458 argv_index < argc && argv[argv_index][0] == '-'; argv_index++)
3459 {
3460 if (strcmp (argv[argv_index], "-nc") == 0)
3461 {
3462 gsc_commands_enabled = FALSE;
3463 continue;
3464 }
3465 if (strcmp (argv[argv_index], "-na") == 0)
3466 {
3467 gsc_abbreviations_enabled = FALSE;
3468 continue;
3469 }
3470 if (strcmp (argv[argv_index], "-nu") == 0)
3471 {
3472 gsc_unicode_enabled = FALSE;
3473 continue;
3474 }
3475 #ifdef LINUX_GRAPHICS
3476 if (strcmp (argv[argv_index], "-ng") == 0)
3477 {
3478 gsclinux_graphics_enabled = FALSE;
3479 continue;
3480 }
3481 #endif
3482 if (strcmp (argv[argv_index], "-r") == 0)
3483 {
3484 restore_from = argv[++argv_index];
3485 continue;
3486 }
3487 return FALSE;
3488 }
3489
3490 /* On invalid usage, set a complaint message and return. */
3491 if (argv_index != argc - 1)
3492 {
3493 gsc_game = NULL;
3494 if (argv_index < argc - 1)
3495 gsc_game_message = "More than one game file"
3496 " was given on the command line.";
3497 else
3498 gsc_game_message = "No game file was given on the command line.";
3499 return TRUE;
3500 }
3501
3502 /* Open a stream to the TAF file, complain if this fails. */
3503 game_stream = glkunix_stream_open_pathname (argv[argv_index], FALSE, 0);
3504 if (!game_stream)
3505 {
3506 gsc_game = NULL;
3507 gsc_game_message = "Unable to open the requested game file.";
3508 return TRUE;
3509 }
3510 else
3511 gsc_game_message = NULL;
3512
3513 /*
3514 * If a restore requested, open a stream to the TAF (TAS) file, and
3515 * again, complain if this fails.
3516 */
3517 if (restore_from)
3518 {
3519 restore_stream = glkunix_stream_open_pathname (restore_from, FALSE, 0);
3520 if (!restore_stream)
3521 {
3522 glk_stream_close (game_stream, NULL);
3523 gsc_game = NULL;
3524 gsc_game_message = "Unable to open the requested restore file.";
3525 return TRUE;
3526 }
3527 else
3528 gsc_game_message = NULL;
3529 }
3530 else
3531 restore_stream = NULL;
3532
3533 /* Set SCARE trace flags and other general setup from the environment. */
3534 if (getenv ("SC_TRACE_FLAGS"))
3535 trace_flags = strtoul (getenv ("SC_TRACE_FLAGS"), NULL, 0);
3536 else
3537 trace_flags = 0;
3538 enable_debugger = (getenv ("SC_DEBUGGER_ENABLED") != NULL);
3539 stable_random = (getenv ("SC_STABLE_RANDOM_ENABLED") != NULL);
3540 locale = getenv ("SC_LOCALE");
3541
3542 #ifdef LINUX_GRAPHICS
3543 /* Note the path to the game file for graphics extraction. */
3544 gsclinux_game_file = argv[argv_index];
3545 #endif
3546
3547 #ifdef GLK_MODULE_GARGLK_FILE_RESOURCES
3548 #ifdef _WIN32
3549 const char *sep = "/\\";
3550 #else
3551 const char *sep = "/";
3552 #endif
3553 const char *slash = find_last_of(argv[argv_index], sep);
3554 if (slash == NULL) {
3555 snprintf(gamefile, sizeof gamefile, "%s", argv[argv_index]);
3556 } else {
3557 snprintf(gamefile, sizeof gamefile, "%s", slash + 1);
3558 }
3559 #endif
3560
3561 #ifdef GARGLK
3562 glkunix_set_base_file(argv[argv_index]);
3563 #endif
3564
3565 /* Use the generic startup code to complete startup. */
3566 return gsc_startup_code (game_stream, restore_stream, trace_flags,
3567 enable_debugger, stable_random, locale);
3568 }
3569 #endif /* __unix */
3570
3571
3572 /*---------------------------------------------------------------------*/
3573 /* Glk linkage relevant only to the Windows platform */
3574 /*---------------------------------------------------------------------*/
3575 #ifdef GARGLK
3576 #undef _WIN32
3577 #endif
3578
3579 #ifdef _WIN32
3580
3581 #include <windows.h>
3582
3583 #include "WinGlk.h"
3584 #include "resource.h"
3585
3586 /* Windows constants and external definitions. */
3587 static const unsigned int GSCWIN_GLK_INIT_VERSION = 0x601;
3588 extern int InitGlk (unsigned int iVersion);
3589
3590 /*
3591 * WinMain()
3592 *
3593 * Entry point for all Glk applications.
3594 */
3595 int WINAPI
3596 WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
3597 LPSTR lpCmdLine, int nCmdShow)
3598 {
3599 /* Attempt to initialize both the Glk library and SCARE. */
3600 if (!(InitGlk (GSCWIN_GLK_INIT_VERSION) && winglk_startup_code (lpCmdLine)))
3601 return 0;
3602
3603 /* Run the application; no return from this routine. */
3604 glk_main ();
3605 glk_exit ();
3606 return 0;
3607 }
3608
3609
3610 /*
3611 * winglk_startup_code()
3612 *
3613 * Startup entry point for Windows versions of Glk interpreter.
3614 */
3615 int
3616 winglk_startup_code (const char *cmdline)
3617 {
3618 const char *filename, *locale;
3619 frefid_t fileref;
3620 strid_t game_stream;
3621 sc_uint trace_flags;
3622 sc_bool enable_debugger, stable_random;
3623 assert (!gsc_startup_called);
3624 gsc_startup_called = TRUE;
3625
3626 /* Set up application and window. */
3627 winglk_app_set_name ("Scare");
3628 winglk_set_menu_name ("&Scare");
3629 winglk_window_set_title ("Scare Adrift Interpreter");
3630 winglk_set_about_text ("Windows Scare 1.3.10");
3631 winglk_set_gui (IDI_SCARE);
3632 glk_stylehint_set (wintype_TextGrid, style_Normal, stylehint_ReverseColor, 1);
3633
3634 /* Open a stream to the game. */
3635 filename = winglk_get_initial_filename (cmdline,
3636 "Select an Adrift game to run",
3637 "Adrift Files (.taf)|*.taf;All Files (*.*)|*.*||");
3638 if (!filename)
3639 return 0;
3640
3641 fileref = winglk_fileref_create_by_name (fileusage_BinaryMode
3642 | fileusage_Data,
3643 (char *) filename, 0, 0);
3644 if (!fileref)
3645 return 0;
3646
3647 game_stream = glk_stream_open_file (fileref, filemode_Read, 0);
3648 glk_fileref_destroy (fileref);
3649 if (!game_stream)
3650 return 0;
3651
3652 /* Set trace, debugger, and portable random flags. */
3653 if (getenv ("SC_TRACE_FLAGS"))
3654 trace_flags = strtoul (getenv ("SC_TRACE_FLAGS"), NULL, 0);
3655 else
3656 trace_flags = 0;
3657 enable_debugger = (getenv ("SC_DEBUGGER_ENABLED") != NULL);
3658 stable_random = (getenv ("SC_STABLE_RANDOM_ENABLED") != NULL);
3659 locale = getenv ("SC_LOCALE");
3660
3661 /* Use the generic startup code to complete startup. */
3662 return gsc_startup_code (game_stream, NULL, trace_flags,
3663 enable_debugger, stable_random, locale);
3664 }
3665 #endif /* _WIN32 */