Update interpreters to latest Garglk codebase
[projects/chimara/chimara.git] / interpreters / glulxe / unixstrt.c
1 /* unixstrt.c: Unix-specific code for Glulxe.
2     Designed by Andrew Plotkin <erkyrath@eblong.com>
3     http://eblong.com/zarf/glulx/index.html
4 */
5
6 #include "glk.h"
7 #include "glulxe.h"
8 #include "glkstart.h" /* This comes with the Glk library. */
9 #include <string.h>
10
11 /* The only command-line argument is the filename. */
12 glkunix_argumentlist_t glkunix_arguments[] = {
13   { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
14   { NULL, glkunix_arg_End, NULL }
15 };
16
17 int glkunix_startup_code(glkunix_startup_t *data)
18 {
19   /* It turns out to be more convenient if we return TRUE from here, even 
20      when an error occurs, and display an error in glk_main(). */
21   char *cx;
22   unsigned char buf[12];
23   int res;
24
25 #ifdef GARGLK
26   garglk_set_program_name("Glulxe 0.4.7");
27   garglk_set_program_info("Glulxe 0.4.7 by Andrew Plotkin");
28 #endif
29
30   if (data->argc <= 1) {
31     init_err = "You must supply the name of a game file.";
32 #ifdef GARGLK
33     return TRUE; /* Hack! but I want error message in glk window */
34 #endif
35         return FALSE;
36   }
37   cx = data->argv[1];
38     
39   gamefile = glkunix_stream_open_pathname(cx, FALSE, 1);
40   if (!gamefile) {
41     init_err = "The game file could not be opened.";
42     init_err2 = cx;
43     return TRUE;
44   }
45
46 #ifdef GARGLK
47   cx = strrchr(data->argv[1], '/');
48   if (!cx) cx = strrchr(data->argv[1], '\\');
49   garglk_set_story_name(cx ? cx + 1 : data->argv[1]);
50 #endif
51
52   /* Now we have to check to see if it's a Blorb file. */
53
54   glk_stream_set_position(gamefile, 0, seekmode_Start);
55   res = glk_get_buffer_stream(gamefile, (char *)buf, 12);
56   if (!res) {
57     init_err = "The data in this stand-alone game is too short to read.";
58     return TRUE;
59   }
60     
61   if (buf[0] == 'G' && buf[1] == 'l' && buf[2] == 'u' && buf[3] == 'l') {
62     locate_gamefile(FALSE);
63     return TRUE;
64   }
65   else if (buf[0] == 'F' && buf[1] == 'O' && buf[2] == 'R' && buf[3] == 'M'
66     && buf[8] == 'I' && buf[9] == 'F' && buf[10] == 'R' && buf[11] == 'S') {
67     locate_gamefile(TRUE);
68     return TRUE;
69   }
70   else {
71     init_err = "This is neither a Glulx game file nor a Blorb file "
72       "which contains one.";
73     return TRUE;
74   }
75 }
76