File: fizmo-console.c

package info (click to toggle)
fizmo 0.7.2-2
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 3,924 kB
  • sloc: ansic: 27,167; makefile: 1,083; cpp: 257
file content (426 lines) | stat: -rw-r--r-- 10,743 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426

/* fizmo_console.c
 *
 * This file is part of fizmo.
 *
 * Copyright (c) 2009-2012 Christoph Ender.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */


#ifdef ENABLE_READCHAR_VIA_TERMIOS
#include <termios.h>
#include <unistd.h>
#include <stdlib.h>
#endif // ENABLE_READCHAR_VIA_TERMIOS

#include <string.h>

#include <interpreter/fizmo.h>
#include <interpreter/text.h>
#include <interpreter/streams.h>
#include <interpreter/wordwrap.h>
#include <tools/unused.h>
#include <tools/types.h>
#include <tools/i18n.h>
#include <tools/tracelog.h>

static char* interface_name = "fizmo-console";
static char* interface_version = "0.7.2";
int line_length = -1;
bool disable_hyphenation = false;
static WORDWRAP *output_wordwrapper;


static char *simple_c_get_interface_name()
{ return interface_name; }

static bool simple_c_return_false()
{ return false; }

static bool simple_c_return_true()
{ return true; }

static uint8_t simple_c_get_screen_height()
{ return 24; }

static uint8_t simple_c_get_screen_width()
{ return 80; }

static uint8_t simple_c_return_1()
{ return 1; }

static z_colour simple_c_get_default_foreground_colour()
{ return Z_COLOUR_BLACK; }

static z_colour simple_c_get_default_background_colour()
{ return Z_COLOUR_WHITE; }

static uint8_t simple_c_return_0()
{ return 0; }

static int simple_c_parse_config_parameter(char *UNUSED(key),
    char *UNUSED(value))
{ return 1; }

static char *simple_c_get_config_value(char *UNUSED(key))
{ return NULL; }

static char **simple_c_get_config_option_names()
{ return NULL; }

static void simple_c_link_interface_to_story(struct z_story *UNUSED(story))
{
  putc('\n', stdout);
}

static void simple_c_reset_interface()
{ }

static void simple_c_set_buffer_mode(uint8_t UNUSED(new_buffer_mode))
{ }

static void z_ucs_output_wordwrap_destination(z_ucs *z_ucs_output,
    void *UNUSED(dummyparameter))
{
  while (*z_ucs_output != 0)
  {
    if ((*z_ucs_output & 0xffffff80) != 0)
      putchar('?');
    else
      putchar((char)*z_ucs_output);

    z_ucs_output++;
  }
}

static void simple_c_interface_output_z_ucs(z_ucs *z_ucs_output)
{
  if (line_length == -1)
    z_ucs_output_wordwrap_destination(z_ucs_output, NULL);
  else
    wordwrap_wrap_z_ucs(output_wordwrapper, z_ucs_output);
}

static int simple_c_close_interface(z_ucs *error_message)
{
  if (error_message != NULL)
    simple_c_interface_output_z_ucs(error_message);
  return 0;
}

static int16_t simple_c_interface_read_line(zscii *dest,
    uint16_t maximum_length, uint16_t UNUSED(tenth_seconds),
    uint32_t UNUSED(verification_routine), uint8_t UNUSED(preloaded_input),
    int *UNUSED(tenth_seconds_elapsed), bool UNUSED(disable_command_history),
    bool UNUSED(return_on_escape))
{
  int input;
  int current_length = 0;
  uint8_t input_size = 0;
  zscii input_zscii;

  if (line_length != -1)
    wordwrap_flush_output(output_wordwrapper);

  if ((input = fgetc(stdin)) == EOF)
    exit(-1);

  while ((input != 10) && (input != 13))
  {
    if (current_length < maximum_length)
    {
      input_zscii = unicode_char_to_zscii_input_char(input & 0xff);

      if ((input_zscii == 0xff) || (input_zscii == 27))
        input_zscii = '?';

      *(dest++) = input_zscii;

      input_size++;
    }

    if ((input = fgetc(stdin)) == EOF)
      exit(-1);
  }

  return input_size;
}

static int simple_c_interface_read_char(uint16_t UNUSED(tenth_seconds),
    uint32_t UNUSED(verification_routine), int *UNUSED(tenth_seconds_elapsed))
{
  int result;
#ifdef ENABLE_READCHAR_VIA_TERMIOS
  struct termios org_opts, new_opts;
#endif // ENABLE_READCHAR_VIA_TERMIOS

  if (line_length != -1)
    wordwrap_flush_output(output_wordwrapper);

#ifdef ENABLE_READCHAR_VIA_TERMIOS

  tcgetattr(STDIN_FILENO, &org_opts);
  memcpy(&new_opts, &org_opts, sizeof(new_opts));
  new_opts.c_lflag
    &= ~(ICANON | ECHO | ECHOE | ECHOK | ECHONL | ECHOPRT | ECHOKE | ICRNL);
  tcsetattr(STDIN_FILENO, TCSANOW, &new_opts);
  result = getchar();
  result = unicode_char_to_zscii_input_char(result & 0xff);
  if (result == 0xff)
    result = -1;
  tcsetattr(STDIN_FILENO, TCSANOW, &org_opts);

  return result;

#else

  zscii input_zscii;
  int input;

  if ((input = fgetc(stdin)) == EOF)
    exit(-1);

  result = -1;

  while ((input != 10) && (input != 13))
  {
    if (result == -1)
    {
      if ((input_zscii=unicode_char_to_zscii_input_char(input & 0xff)) != 0xff)
        result = input_zscii;
    }

    if ((input = fgetc(stdin)) == EOF)
      exit(-1);
  }

  if (result == -1)
    result = 10;

#endif // ENABLE_READCHAR_VIA_TERMIOS

  return result;
}

static void simple_c_show_status(z_ucs *UNUSED(room_description),
    int UNUSED(status_line_mode), int16_t UNUSED(parameter1),
    int16_t UNUSED(parameter2))
{ }

static void simple_c_set_text_style(z_style UNUSED(text_style))
{ }

static void simple_c_set_colour(z_colour UNUSED(foreground),
    z_colour UNUSED(background), int16_t UNUSED(window))
{ }

static void simple_c_set_font(z_font UNUSED(font_type))
{ }

static void simple_c_split_window(int16_t UNUSED(nof_lines))
{ }

static void simple_c_set_window(int16_t UNUSED(window_number))
{ }

static void simple_c_erase_window(int16_t UNUSED(window_number))
{ }

static void simple_c_set_cursor(int16_t UNUSED(line), int16_t UNUSED(column),
    int16_t UNUSED(window))
{ }

static uint16_t simple_c_get_cursor_row()
{ return 0; }

static uint16_t simple_c_get_cursor_column()
{ return 0;}

static void simple_c_erase_line_value(uint16_t UNUSED(start_position))
{ }

static void simple_c_erase_line_pixels(uint16_t UNUSED(start_position))
{ }

static void simple_c_output_interface_info()
{
  (void)streams_latin1_output(interface_name);
  (void)streams_latin1_output(" interface version ");
  (void)streams_latin1_output(interface_version);
  (void)streams_latin1_output("\n");
}

static void simple_c_game_was_restored_and_history_modified()
{ }

struct z_screen_interface simple_c_interface =
{
  &simple_c_get_interface_name,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_true,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_return_false,
  &simple_c_get_screen_height,
  &simple_c_get_screen_width,
  &simple_c_get_screen_height,
  &simple_c_get_screen_width,
  &simple_c_return_1,
  &simple_c_return_1,
  &simple_c_get_default_foreground_colour,
  &simple_c_get_default_background_colour,
  &simple_c_return_0,
  &simple_c_parse_config_parameter,
  &simple_c_get_config_value,
  &simple_c_get_config_option_names,
  &simple_c_link_interface_to_story,
  &simple_c_reset_interface,
  &simple_c_close_interface,
  &simple_c_set_buffer_mode,
  &simple_c_interface_output_z_ucs,
  &simple_c_interface_read_line,
  &simple_c_interface_read_char,
  &simple_c_show_status,
  &simple_c_set_text_style,
  &simple_c_set_colour,
  &simple_c_set_font,
  &simple_c_split_window,
  &simple_c_set_window,
  &simple_c_erase_window,
  &simple_c_set_cursor,
  &simple_c_get_cursor_row,
  &simple_c_get_cursor_column,
  &simple_c_erase_line_value,
  &simple_c_erase_line_pixels,
  &simple_c_output_interface_info,
  &simple_c_return_false,
  &simple_c_game_was_restored_and_history_modified
};


static void print_syntax()
{
  puts("\nUsage: fizmo-console [options] <story-file>\n");

  printf("fizmo-console version %s.\n", interface_version);
  printf("libfizmo version %s.\n\n", FIZMO_VERSION);

  puts("Valid options are:");
  puts(" -ll, --line-length: Wrap words into lines of given length.");
  puts(" -dh, --disable-hyphenation: Disable hyphenation.");
  puts(" -h,  --help: Show help message and exit.\n");
}


int main(int argc, char *argv[])
{
  z_file *story_stream;
  char *story_filename = NULL;
  int i;

#ifdef ENABLE_TRACING
  turn_on_trace();
#endif // ENABLE_TRACING

  fizmo_register_screen_interface(&simple_c_interface);

  for (i=1; i<argc; i++)
  {
    if ( (strcmp(argv[i], "-h") == 0)
        || (strcmp(argv[i], "--help") == 0) )
    {
      print_syntax();
      exit(EXIT_SUCCESS);
    }
    else if ( (strcmp(argv[i], "-ll") == 0)
        || (strcmp(argv[i], "--line-length") == 0) )
    {
      if (++i == argc)
      {
        print_syntax();
        exit(EXIT_FAILURE);
      }

      line_length = atoi(argv[i]);
    }
    else if ( (strcmp(argv[i], "-dh") == 0)
        || (strcmp(argv[i], "--disable-hyphenation") == 0) )
    {
      disable_hyphenation = true;
    }
    else
    {
      if (story_filename != NULL)
      {
        print_syntax();
        exit(EXIT_FAILURE);
      }
      story_filename = argv[i];
    }
  }

  if (story_filename == NULL)
  {
    print_syntax();
    exit(EXIT_FAILURE);
  }

  if (line_length > 0)
  {
    output_wordwrapper = wordwrap_new_wrapper(
        line_length,
        &z_ucs_output_wordwrap_destination,
        (void*)NULL,
#if defined (__WIN32__)
        false,
#else
        true,
#endif // defined (__WIN32__)
        0,
        false,
        disable_hyphenation == false ? true : false);
  }

  if ((story_stream
      = fsi->openfile(story_filename, FILETYPE_DATA, FILEACCESS_READ)) == NULL)
  {
    printf("Could not open file \"%s\".\n", argv[1]);
    return -1;
  }
  else
  {
    fizmo_start(story_stream, NULL, NULL, -1, -1);
    return 0;
  }
}