4e629055f097e6379d6a6fd0dbc92e12d848187c
[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
10 /* The only command-line argument is the filename. */
11 glkunix_argumentlist_t glkunix_arguments[] = {
12   { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
13   { NULL, glkunix_arg_End, NULL }
14 };
15
16 int glkunix_startup_code(glkunix_startup_t *data)
17 {
18   /* It turns out to be more convenient if we return TRUE from here, even 
19      when an error occurs, and display an error in glk_main(). */
20   char *cx;
21   unsigned char buf[12];
22   int res;
23
24   if (data->argc <= 1) {
25     init_err = "You must supply the name of a game file.";
26     return FALSE;
27   }
28   cx = data->argv[1];
29     
30   gamefile = glkunix_stream_open_pathname(cx, FALSE, 1);
31   if (!gamefile) {
32     init_err = "The game file could not be opened.";
33     init_err2 = cx;
34     return TRUE;
35   }
36
37   /* Now we have to check to see if it's a Blorb file. */
38
39   glk_stream_set_position(gamefile, 0, seekmode_Start);
40   res = glk_get_buffer_stream(gamefile, (char *)buf, 12);
41   if (!res) {
42     init_err = "The data in this stand-alone game is too short to read.";
43     return TRUE;
44   }
45     
46   if (buf[0] == 'G' && buf[1] == 'l' && buf[2] == 'u' && buf[3] == 'l') {
47     locate_gamefile(FALSE);
48     return TRUE;
49   }
50   else if (buf[0] == 'F' && buf[1] == 'O' && buf[2] == 'R' && buf[3] == 'M'
51     && buf[8] == 'I' && buf[9] == 'F' && buf[10] == 'R' && buf[11] == 'S') {
52     locate_gamefile(TRUE);
53     return TRUE;
54   }
55   else {
56     init_err = "This is neither a Glulx game file nor a Blorb file "
57       "which contains one.";
58     return TRUE;
59   }
60 }
61