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
|
/* misc.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.
*/
#include <string.h>
#include <signal.h>
#include <stdlib.h>
#include <errno.h>
#include "../tools/tracelog.h"
#include "../tools/i18n.h"
#include "../tools/filesys.h"
#include "misc.h"
#include "fizmo.h"
#include "streams.h"
#include "stack.h"
#include "zpu.h"
#include "routine.h"
#include "variable.h"
#include "../locales/libfizmo_locales.h"
#ifdef ENABLE_DEBUGGER
#include "debugger.h"
#endif // ENABLE_DEBUGGER
#if !defined(__WIN32__)
static struct sigaction fizmo_sigactions;
#endif // defined(__WIN32__)
void opcode_restart(void)
{
TRACE_LOG("Opcode: RESTART.\n");
terminate_interpreter = INTERPRETER_QUIT_RESTART;
}
void opcode_verify(void)
{
uint16_t file_length = (z_mem[0x1a] << 8) | z_mem[0x1b];
uint16_t header_checksum = (z_mem[0x1c] << 8) | z_mem[0x1d];
uint16_t calculated_checksum = 0;
int i, input, scale;
TRACE_LOG("Opcode: VERIFY.\n");
if ( (file_length == 0) && (header_checksum == 0) )
evaluate_branch((uint8_t)1);
if (ver <= 3)
scale = 2;
else if (ver <= 5)
scale = 4;
else
scale = 8;
if (file_length * scale - 1 >= 0x40)
{
if (fsi->setfilepos(
active_z_story->z_story_file,
0x40 + active_z_story->story_file_exec_offset,
SEEK_SET) != 0)
i18n_translate_and_exit(
libfizmo_module_name,
i18n_libfizmo_FATAL_ERROR_READING_STORY_FILE,
-1);
i = 0x40;
while (i < file_length * scale)
{
if ((input = fsi->getchar(active_z_story->z_story_file)) == EOF)
i18n_translate_and_exit(
libfizmo_module_name,
i18n_libfizmo_FATAL_ERROR_READING_STORY_FILE,
-1);
calculated_checksum += input;
i++;
}
}
evaluate_branch(
calculated_checksum == header_checksum ? (uint8_t)1 : (uint8_t)0);
}
void opcode_piracy(void)
{
TRACE_LOG("Opcode: PIRACY.\n");
evaluate_branch(1);
}
void abort_interpreter(int exit_code, z_ucs *error_message)
{
#ifdef THROW_SIGFAULT_ON_ERROR
int x;
#endif // THROW_SIGFAULT_ON_ERROR
deactivate_signal_handlers();
#ifdef ENABLE_DEBUGGER
streams_latin1_output("\nAborting due to:\n");
streams_z_ucs_output(error_message);
streams_latin1_output("\n");
debugger();
debugger_interpreter_stopped();
#endif // ENABLE_DEBUGGER
TRACE_LOG("Aborting interpreter.\n");
if (error_message != NULL)
{
TRACE_LOG("Abort with exit message \"");
TRACE_LOG_Z_UCS(error_message);
TRACE_LOG("\"\n");
}
if (active_sound_interface != NULL)
{
TRACE_LOG("Sound interface at %p.\n", active_sound_interface);
TRACE_LOG("Closing sound interface.\n");
active_sound_interface->close_sound();
}
TRACE_LOG("Closing streams.\n");
close_streams(error_message);
// From the OpenGroup: The value of status may be 0, EXIT_SUCCESS,
// EXIT_FAILURE, [CX] [Option Start] or any other value, though only
// the least significant 8 bits (that is, status & 0377) shall be
// available to a waiting parent process. [Option End]
// -> Thus, we print the exit code seperately.
TRACE_LOG("Output exit code.\n");
//fprintf(stderr, "Exit code: %d.\n", exit_code);
//(void)signal(SIGSEGV, SIG_DFL);
TRACE_LOG("Exit.\n");
#ifdef THROW_SIGFAULT_ON_ERROR
x = *((int*)NULL);
#endif
#if defined(__WIN32__)
exit_code = 0;
#endif // !defined(__WIN32__)
exit(exit_code);
}
#if !defined(__WIN32__)
static void catch_signal_and_abort(int sig_num)
{
TRACE_LOG("Caught signal %d.\n", sig_num);
i18n_translate_and_exit(
libfizmo_module_name,
i18n_libfizmo_CAUGHT_SIGNAL_P0D_ABORTING_INTERPRETER,
-1,
(long)sig_num);
}
#endif // defined(__WIN32__)
void init_signal_handlers(void)
{
#if !defined(__WIN32__)
TRACE_LOG("Initialiazing signal handlers.\n");
sigemptyset(&fizmo_sigactions.sa_mask);
fizmo_sigactions.sa_flags = 0;
fizmo_sigactions.sa_handler = &catch_signal_and_abort;
sigaction(SIGSEGV, &fizmo_sigactions, NULL);
sigaction(SIGTERM, &fizmo_sigactions, NULL);
sigaction(SIGINT, &fizmo_sigactions, NULL);
sigaction(SIGQUIT, &fizmo_sigactions, NULL);
sigaction(SIGBUS, &fizmo_sigactions, NULL);
sigaction(SIGILL, &fizmo_sigactions, NULL);
#endif // !defined(__WIN32__)
}
void deactivate_signal_handlers(void)
{
#if !defined(__WIN32__)
TRACE_LOG("Deactivating signal handlers.\n");
sigemptyset(&fizmo_sigactions.sa_mask);
fizmo_sigactions.sa_flags = 0;
fizmo_sigactions.sa_handler = NULL;
sigaction(SIGSEGV, &fizmo_sigactions, NULL);
sigaction(SIGTERM, &fizmo_sigactions, NULL);
sigaction(SIGINT, &fizmo_sigactions, NULL);
sigaction(SIGQUIT, &fizmo_sigactions, NULL);
sigaction(SIGBUS, &fizmo_sigactions, NULL);
sigaction(SIGILL, &fizmo_sigactions, NULL);
#endif // !defined(__WIN32__)
}
|