Debian Codegraph @

terps/git/git_unix.c

raw symbols SCIP
1 // $Id: git_unix.c,v 1.5 2004/01/25 18:44:51 iain Exp $
2
3 // unixstrt.c: Unix-specific code for Glulxe.
4 // Designed by Andrew Plotkin <[email protected]>
5 // http://www.eblong.com/zarf/glulx/index.html
6
7 #include "git.h"
8 #include <glk.h>
9 #include <glkstart.h> // This comes with the Glk library.
10
11 #ifdef USE_MMAP
12 #include <fcntl.h>
13 #include <sys/mman.h>
14 #include <sys/stat.h>
15 #include <errno.h>
16 #endif
17
18 // The only command-line argument is the filename.
19 glkunix_argumentlist_t glkunix_arguments[] =
20 {
21 { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
22 { NULL, glkunix_arg_End, NULL }
23 };
24
25 #define CACHE_SIZE (256 * 1024L)
26 #define UNDO_SIZE (2 * 1024 * 1024L)
27
28 #ifdef GARGLK
29
30 #include <string.h>
31
32 int gHasInited = 0;
33 char * gStartupError = 0;
34
35 void fatalError (const char * s)
36 {
37 winid_t win;
38 if (!gHasInited)
39 {
40 win = glk_window_open(0, 0, 0, wintype_TextBuffer, 0);
41 glk_set_window(win);
42 }
43 /* pray that this goes somewhere reasonable... */
44 glk_put_string("\n*** fatal error: ");
45 glk_put_string((char*)s);
46 glk_put_string(" ***\n");
47 glk_exit();
48 }
49
50 #else
51
52 void fatalError (const char * s)
53 {
54 fprintf (stderr, "*** fatal error: %s ***\n", s);
55 exit (1);
56 }
57
58 #endif /* GARGLK */
59
60 #ifdef USE_MMAP
61 // Fast loader that uses some fancy Unix features.
62
63 const char * gFilename = 0;
64
65 int glkunix_startup_code(glkunix_startup_t *data)
66 {
67 #ifdef GARGLK
68 {
69 char buf[255];
70 sprintf(buf, "Git %d.%d.%d", GIT_MAJOR, GIT_MINOR, GIT_PATCH);
71 garglk_set_program_name(buf);
72 sprintf(buf, "Git %d.%d.%d by Iain Merrick and David Kinder\n",
73 GIT_MAJOR, GIT_MINOR, GIT_PATCH);
74 garglk_set_program_info(buf);
75 }
76 #endif /* GARGLK */
77
78 if (data->argc <= 1)
79 {
80 #ifdef GARGLK
81 gStartupError = "No file given";
82 return 1;
83 #else
84 printf ("usage: git gamefile.ulx\n");
85 return 0;
86 #endif
87 }
88
89 #ifdef GARGLK
90 {
91 char *s;
92 s = strrchr(data->argv[1], '\\');
93 if (s) garglk_set_story_name(s+1);
94 s = strrchr(data->argv[1], '/');
95 if (s) garglk_set_story_name(s+1);
96 }
97 #endif /* GARGLK */
98
99 gFilename = data->argv[1];
100 return 1;
101 }
102
103 void glk_main ()
104 {
105 int file;
106 struct stat info;
107 git_uint8 * ptr;
108
109 #ifdef GARGLK
110 if (gStartupError)
111 fatalError(gStartupError);
112 #endif
113
114 file = open (gFilename, O_RDONLY);
115 if (file < 0)
116 goto error;
117
118 if (fstat (file, &info) != 0)
119 goto error;
120
121 if (info.st_size < 256)
122 {
123 #ifdef GARGLK
124 fatalError("This is too small to be a glulx file.");
125 #else
126 fprintf (stderr, "This is too small to be a glulx file.\n");
127 exit (1);
128 #endif
129 }
130
131 ptr = mmap (NULL, info.st_size, PROT_READ, MAP_PRIVATE, file, 0);
132 if (ptr == MAP_FAILED)
133 goto error;
134
135 #ifdef GARGLK
136 gHasInited = 1;
137 #endif
138
139 git (ptr, info.st_size, CACHE_SIZE, UNDO_SIZE);
140 munmap ((void*) ptr, info.st_size);
141 return;
142
143 error:
144 #ifdef GARGLK
145 sprintf(errmsg, "git: %s", strerror(errno));
146 fatalError(errmsg);
147 #else
148 perror ("git");
149 exit (errno);
150 #endif
151 }
152
153 #else
154 // Generic loader that should work anywhere.
155
156 strid_t gStream = 0;
157
158 int glkunix_startup_code(glkunix_startup_t *data)
159 {
160 #ifdef GARGLK
161 {
162 char buf[255];
163 sprintf(buf, "Git %d.%d.%d", GIT_MAJOR, GIT_MINOR, GIT_PATCH);
164 garglk_set_program_name(buf);
165 sprintf(buf, "Git %d.%d.%d by Iain Merrick and David Kinder\n",
166 GIT_MAJOR, GIT_MINOR, GIT_PATCH);
167 garglk_set_program_info(buf);
168 }
169 #endif /* GARGLK */
170
171 if (data->argc <= 1)
172 {
173 #ifdef GARGLK
174 gStartupError = "No file given";
175 return 1;
176 #else
177 printf ("usage: git gamefile.ulx\n");
178 return 0;
179 #endif
180 }
181
182 #ifdef GARGLK
183 {
184 char *s;
185 s = strrchr(data->argv[1], '\\');
186 if (s) garglk_set_story_name(s+1);
187 s = strrchr(data->argv[1], '/');
188 if (s) garglk_set_story_name(s+1);
189 }
190 #endif /* GARGLK */
191
192 gStream = glkunix_stream_open_pathname ((char*) data->argv[1], 0, 0);
193 return 1;
194 }
195
196 void glk_main ()
197 {
198 #ifdef GARGLK
199 if (gStartupError)
200 fatalError(gStartupError);
201 #endif
202
203 if (gStream == NULL)
204 fatalError ("could not open game file");
205
206 #ifdef GARGLK
207 gHasInited = 1;
208 #endif
209
210 gitWithStream (gStream, CACHE_SIZE, UNDO_SIZE);
211 }
212
213 #endif // USE_MMAP
214
215 #ifdef USE_NATIVE_RANDOM
216
217 glui32 native_random()
218 {
219 return arc4random();
220 }
221
222 #endif // USE_NATIVE_RANDOM