Debian Codegraph @

terps/hugo/heglk/heglkunix.c

raw symbols SCIP
1 /* glkstart.c: Unix-specific startup code -- sample file.
2 Designed by Andrew Plotkin <[email protected]>
3 http://www.eblong.com/zarf/glk/index.html
4
5 This is Unix startup code for the simplest possible kind of Glk
6 program -- no command-line arguments; no startup files; no nothing.
7
8 Remember, this is a sample file. You should copy it into the Glk
9 program you are compiling, and modify it to your needs. This should
10 *not* be compiled into the Glk library itself.
11 */
12
13 #include <stdio.h>
14 #include <string.h>
15 #include <stdarg.h>
16
17 #include "glk.h"
18 #include "glkstart.h"
19 #include "heheader.h"
20
21 static winid_t errorwin = NULL;
22
23 static void glkunix_startup_error (char *fmt, ...)
24 #ifdef __GNUC__
25 __attribute__ ((format (printf, 1, 2)))
26 #endif
27 ;
28
29 glkunix_argumentlist_t glkunix_arguments[] = {
30 { "", glkunix_arg_ValueFollows, "filename: The game file to load." },
31 { NULL, glkunix_arg_End, NULL }
32 };
33
34 /*
35 * Using sprintf() to build an error string would be easier, of course, but
36 * that is subject to buffer overflow at runtime. This still uses sprintf(),
37 * but in a more controlled fashion. (Yes, I know it's probably overkill.)
38 */
39
40 static void
41 glkunix_startup_error (char *fmt, ...)
42 {
43 va_list ap;
44 char int_buf[20]; /* Hopefully big enough */
45 char *p;
46
47 if (errorwin == NULL)
48 {
49 errorwin = glk_window_open (0, 0, 0, wintype_TextBuffer, 0);
50 if (errorwin == NULL)
51 return;
52 }
53
54 glk_set_window (errorwin);
55
56 va_start (ap, fmt);
57 for (p = fmt; *p; p++)
58 {
59 if (*p == '%')
60 {
61 switch (*++p)
62 {
63 case '%':
64 glk_put_char ('%');
65 break;
66
67 case 's':
68 glk_put_string (va_arg (ap, char *));
69 break;
70
71 case 'd':
72 sprintf (int_buf, "%d", va_arg (ap, int));
73 glk_put_string (int_buf);
74 break;
75
76 default:
77 break;
78 }
79 }
80 else
81 glk_put_char (*p);
82 }
83 va_end (ap);
84 }
85
86 char *hugo_path_to_game;
87
88 int
89 glkunix_startup_code (glkunix_startup_t *data)
90 {
91 char *base_name;
92
93 #ifdef GARGLK
94 garglk_set_program_name("Hugo 3.1");
95 garglk_set_program_info(
96 "Hugo 3.1 by Kent Tessman\n"
97 "Graphics support by Simon Baldwin\n"
98 "Sound support by Tor Andersson\n");
99 #endif
100
101 if (data->argc != 2)
102 {
103 base_name = strrchr (data->argv[0], '/');
104 base_name = (base_name != NULL) ? base_name + 1 : data->argv[0];
105 glkunix_startup_error ("Usage: %s game-file\n", base_name);
106 return FALSE;
107 }
108
109 char *s;
110 s = strrchr(data->argv[1], '/');
111 if (!s) s = strrchr(data->argv[1], '\\');
112
113 #ifdef GARGLK
114 garglk_set_story_name(s ? s + 1 : data->argv[1]);
115 #endif
116
117 hugo_path_to_game = calloc(1, strlen(data->argv[1]) + 1);
118 if (hugo_path_to_game == NULL)
119 {
120 glkunix_startup_error ("Error: out of memory");
121 return FALSE;
122 }
123
124 if (s != NULL)
125 {
126 memcpy(hugo_path_to_game, data->argv[1], s - data->argv[1]);
127 }
128 else
129 {
130 strcpy(hugo_path_to_game, data->argv[1]);
131 }
132
133 glkunix_set_base_file(data->argv[1]);
134 game = glkunix_stream_open_pathname (data->argv[1], 0, 0);
135 if (game == NULL)
136 {
137 glkunix_startup_error ("Error: cannot open game file `%s'\n",
138 data->argv[1]);
139 return FALSE;
140 }
141
142 /* This should not happen, but handle it anyway. */
143 if (errorwin != NULL)
144 glk_window_close (errorwin, NULL);
145
146 return TRUE;
147 }