Added Nitfol and Frotz source code.
[projects/chimara/chimara.git] / interpreters / frotz / stream.c
1 /* stream.c - IO stream implementation
2  *      Copyright (c) 1995-1997 Stefan Jokisch
3  *
4  * This file is part of Frotz.
5  *
6  * Frotz is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Frotz is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19  */
20
21 #include "frotz.h"
22
23 /* glkify */
24 zchar console_read_input (int max, zchar *buf, zword timeout, bool continued)
25 {
26     return os_read_line(max, buf, timeout, max, continued);
27 }
28 zchar console_read_key (zword timeout)
29 {
30     return os_read_key(timeout, 0);
31 }
32
33 extern bool validate_click (void);
34
35 extern void replay_open (void);
36 extern void replay_close (void);
37 extern void memory_open (zword, zword, bool);
38 extern void memory_close (void);
39 extern void record_open (void);
40 extern void record_close (void);
41 extern void script_open (void);
42 extern void script_close (void);
43
44 extern void memory_word (const zchar *);
45 extern void memory_new_line (void);
46 extern void record_write_key (zchar);
47 extern void record_write_input (const zchar *, zchar);
48 extern void script_char (zchar);
49 extern void script_word (const zchar *);
50 extern void script_new_line (void);
51 extern void script_write_input (const zchar *, zchar);
52 extern void script_erase_input (const zchar *);
53 extern void script_mssg_on (void);
54 extern void script_mssg_off (void);
55 extern void screen_char (zchar);
56 extern void screen_word (const zchar *);
57 extern void screen_new_line (void);
58 extern void screen_write_input (const zchar *, zchar);
59 extern void screen_erase_input (const zchar *);
60 extern void screen_mssg_on (void);
61 extern void screen_mssg_off (void);
62
63 extern zchar replay_read_key (void);
64 extern zchar replay_read_input (zchar *);
65 extern zchar console_read_key (zword);
66 extern zchar console_read_input (int, zchar *, zword, bool);
67
68 extern int direct_call (zword);
69
70 /*
71  * stream_mssg_on
72  *
73  * Start printing a "debugging" message.
74  *
75  */
76
77 void stream_mssg_on (void)
78 {
79
80     flush_buffer ();
81
82     if (ostream_screen)
83             screen_mssg_on ();
84     if (ostream_script && enable_scripting)
85         script_mssg_on ();
86
87     message = TRUE;
88
89 }/* stream_mssg_on */
90
91 /*
92  * stream_mssg_off
93  *
94  * Stop printing a "debugging" message.
95  *
96  */
97
98 void stream_mssg_off (void)
99 {
100
101     flush_buffer ();
102
103     if (ostream_screen)
104         screen_mssg_off ();
105     if (ostream_script && enable_scripting)
106         script_mssg_off ();
107
108     message = FALSE;
109
110 }/* stream_mssg_off */
111
112 /*
113  * z_output_stream, open or close an output stream.
114  *
115  *      zargs[0] = stream to open (positive) or close (negative)
116  *      zargs[1] = address to redirect output to (stream 3 only)
117  *      zargs[2] = width of redirected output (stream 3 only, optional)
118  *
119  */
120
121 void z_output_stream (void)
122 {
123
124     flush_buffer ();
125
126     switch ((short) zargs[0]) {
127
128     case  1: ostream_screen = TRUE;
129              break;
130     case -1: ostream_screen = FALSE;
131              break;
132     case  2: if (!ostream_script) script_open ();
133              break;
134     case -2: if (ostream_script) script_close ();
135              break;
136     case  3: memory_open (zargs[1], zargs[2], zargc >= 3);
137              break;
138     case -3: memory_close ();
139              break;
140     case  4: if (!ostream_record) record_open ();
141              break;
142     case -4: if (ostream_record) record_close ();
143              break;
144
145     }
146
147 }/* z_output_stream */
148
149 /*
150  * stream_char
151  *
152  * Send a single character to the output stream.
153  *
154  */
155
156 void stream_char (zchar c)
157 {
158
159     if (ostream_screen)
160         screen_char (c);
161     if (ostream_script && enable_scripting)
162         script_char (c);
163
164 }/* stream_char */
165
166 /*
167  * stream_word
168  *
169  * Send a string of characters to the output streams.
170  *
171  */
172
173 void stream_word (const zchar *s)
174 {
175
176     if (ostream_memory && !message)
177
178         memory_word (s);
179
180     else {
181
182         if (ostream_screen)
183             screen_word (s);
184         if (ostream_script && enable_scripting)
185             script_word (s);
186
187     }
188
189 }/* stream_word */
190
191 /*
192  * stream_new_line
193  *
194  * Send a newline to the output streams.
195  *
196  */
197
198 void stream_new_line (void)
199 {
200
201     if (ostream_memory && !message)
202
203         memory_new_line ();
204
205     else {
206
207         if (ostream_screen)
208             screen_new_line ();
209         if (ostream_script && enable_scripting)
210             script_new_line ();
211
212     }
213
214 }/* stream_new_line */
215
216 /*
217  * z_input_stream, select an input stream.
218  *
219  *      zargs[0] = input stream to be selected
220  *
221  */
222
223 void z_input_stream (void)
224 {
225
226     flush_buffer ();
227
228     if (zargs[0] == 0 && istream_replay)
229         replay_close ();
230     if (zargs[0] == 1 && !istream_replay)
231         replay_open ();
232
233 }/* z_input_stream */
234
235 /*
236  * stream_read_key
237  *
238  * Read a single keystroke from the current input stream.
239  *
240  */
241
242 zchar stream_read_key ( zword timeout, zword routine )
243 {
244     zchar key = ZC_BAD;
245
246     flush_buffer ();
247
248     /* Read key from current input stream */
249
250 continue_input:
251
252     do {
253
254         if (istream_replay)
255             key = replay_read_key ();
256         else
257             key = console_read_key (timeout);
258
259     } while (key == ZC_BAD);
260
261     /* Verify mouse clicks */
262
263 /* glkify
264     if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
265         if (!validate_click ())
266             goto continue_input;
267 */
268
269     /* Copy key to the command file */
270
271     if (ostream_record && !istream_replay)
272         record_write_key (key);
273
274     /* Handle timeouts */
275
276     if (key == ZC_TIME_OUT)
277         if (direct_call (routine) == 0)
278             goto continue_input;
279
280     /* Return key */
281
282     return key;
283
284 }/* stream_read_key */
285
286 /*
287  * stream_read_input
288  *
289  * Read a line of input from the current input stream.
290  *
291  */
292
293 zchar stream_read_input ( int max, zchar *buf,
294                           zword timeout, zword routine,
295                           bool no_scripting )
296 {
297     zchar key = ZC_BAD;
298
299     flush_buffer ();
300
301     /* Remove initial input from the transscript file or from the screen */
302
303     if (ostream_script && enable_scripting && !no_scripting)
304         script_erase_input (buf);
305 //glkify    if (istream_replay)
306 //glkify        screen_erase_input (buf);
307
308     /* Read input line from current input stream */
309
310 continue_input:
311
312     do {
313
314         if (istream_replay)
315             key = replay_read_input (buf);
316         else
317             key = console_read_input (max, buf, timeout, key != ZC_BAD);
318
319     } while (key == ZC_BAD);
320
321     /* Verify mouse clicks */
322
323 /* glkify
324     if (key == ZC_SINGLE_CLICK || key == ZC_DOUBLE_CLICK)
325         if (!validate_click ())
326             goto continue_input;
327 */
328
329     /* Copy input line to the command file */
330
331     if (ostream_record && !istream_replay)
332         record_write_input (buf, key);
333
334     /* Handle timeouts */
335
336     if (key == ZC_TIME_OUT)
337         if (direct_call (routine) == 0)
338             goto continue_input;
339
340     /* Copy input line to transscript file or to the screen */
341
342     if (ostream_script && enable_scripting && !no_scripting)
343         script_write_input (buf, key);
344 //glkify    if (istream_replay)
345 //glkify        screen_write_input (buf, key);
346
347     /* Return terminating key */
348
349     return key;
350
351 }/* stream_read_input */