File: term.c

package info (click to toggle)
mit-scheme 10.1.5-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 84,732 kB
  • sloc: lisp: 476,370; xml: 133,758; ansic: 70,366; sh: 8,696; makefile: 2,239; asm: 2,109
file content (333 lines) | stat: -rw-r--r-- 8,880 bytes parent folder | download | duplicates (2)
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
/* -*-C-*-

Copyright (C) 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994,
    1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
    2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016,
    2017, 2018, 2019 Massachusetts Institute of Technology

This file is part of MIT/GNU Scheme.

MIT/GNU Scheme is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

MIT/GNU Scheme is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with MIT/GNU Scheme; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301,
USA.

*/

#include "scheme.h"
#include "ostop.h"
#include "osio.h"
#include "osfs.h"
#include "osfile.h"
#include "edwin.h"
#include "option.h"

extern long death_blow;
extern void get_band_parameters (unsigned long *, unsigned long *);

#ifdef __WIN32__
#  define USING_MESSAGE_BOX_FOR_FATAL_OUTPUT
#endif

static void edwin_auto_save (void);
static void delete_temp_files (void);

#define BYTES_TO_BLOCKS(n) (((n) + 1023) / 1024)
#define MIN_HEAP_DELTA	50

#ifndef EXIT_SCHEME
#  define EXIT_SCHEME exit
#endif

#ifdef EXIT_SCHEME_DECLARATIONS
EXIT_SCHEME_DECLARATIONS;
#endif

void
init_exit_scheme (void)
{
#ifdef INIT_EXIT_SCHEME
  INIT_EXIT_SCHEME ();
#endif
}

static void
attempt_termination_backout (int code)
{
  outf_flush_error(); /* NOT flush_fatal */
  if ((WITHIN_CRITICAL_SECTION_P ())
      || (code == TERM_HALT)
      || (! (VECTOR_P (fixed_objects))))
    return;
  {
    SCHEME_OBJECT Term_Vector
      = (VECTOR_REF (fixed_objects, Termination_Proc_Vector));
    if ((! (VECTOR_P (Term_Vector)))
	|| (((long) (VECTOR_LENGTH (Term_Vector))) <= code))
      return;
    {
      SCHEME_OBJECT Handler = (VECTOR_REF (Term_Vector, code));
      if (Handler == SHARP_F)
	return;
     Will_Push (CONTINUATION_SIZE
		+ STACK_ENV_EXTRA_SLOTS
		+ ((code == TERM_NO_ERROR_HANDLER) ? 5 : 4));
      SET_RC (RC_HALT);
      SET_EXP (LONG_TO_UNSIGNED_FIXNUM (code));
      SAVE_CONT ();
      if (code == TERM_NO_ERROR_HANDLER)
	STACK_PUSH (LONG_TO_UNSIGNED_FIXNUM (death_blow));
      PUSH_VAL ();		/* Arg 3 */
      PUSH_ENV ();		/* Arg 2 */
      PUSH_EXP ();		/* Arg 1 */
      STACK_PUSH (Handler);	/* The handler function */
      PUSH_APPLY_FRAME_HEADER ((code == TERM_NO_ERROR_HANDLER) ? 4 : 3);
     Pushed ();
      abort_to_interpreter (PRIM_NO_TRAP_APPLY);
    }
  }
}

static void
termination_prefix (int code)
{
  attempt_termination_backout (code);
  OS_restore_external_state ();
  /* TERM_HALT is not an error condition and thus its termination
     message should be considered normal output.  */
  if (code == TERM_HALT)
    {
      if (!option_batch_mode)
	{
	  /* Pick a message "randomly" */
	  unsigned long i = (((unsigned long) Free) >> 3) % MAX_HALT_MESSAGE;
	  outf_console ("\n%s\n", (term_halt_messages[i]));
	  outf_flush_console ();
	}
    }
  else
    {
#ifdef USING_MESSAGE_BOX_FOR_FATAL_OUTPUT
      outf_fatal ("Reason for termination:");
#endif
      outf_fatal ("\n");
      {
	const char * msg = 0;
	if ((code >= 0) && (code <= MAX_TERMINATION))
	  msg = (term_messages[code]);
	if (msg == 0)
	  outf_fatal ("Unknown termination code %#x", code);
	else
	  outf_fatal ("%s", msg);
      }
      if (WITHIN_CRITICAL_SECTION_P ())
	outf_fatal (" within critical section \"%s\"",
		    (CRITICAL_SECTION_NAME ()));
      outf_fatal (".");
#ifndef USING_MESSAGE_BOX_FOR_FATAL_OUTPUT
      outf_fatal ("\n");
#endif
    }
}

static void termination_suffix (int, int, bool) NORETURN;
static void termination_suffix_trace (int) NORETURN;

static void
termination_suffix (int code, int value, bool abnormal_p)
{
#ifdef EXIT_HOOK
  EXIT_HOOK (code, value, abnormal_p);
#endif
  edwin_auto_save ();
  delete_temp_files ();
#ifdef USING_MESSAGE_BOX_FOR_FATAL_OUTPUT
  /* Don't put up message box for ordinary exit.  */
  if (code != TERM_HALT)
#endif
    outf_flush_fatal();
  reset_memory ();
  EXIT_SCHEME (value);
}

static void
termination_suffix_trace (int code)
{
  if (Trace_On_Error)
    {
      outf_error ("\n\n**** Stack trace ****\n\n");
      Back_Trace (ERROR_OUTPUT);
    }
  termination_suffix (code, 1, true);
}

void
Microcode_Termination (int code)
{
  termination_prefix (code);
  termination_suffix_trace (code);
}

void
termination_normal (const int value)
{
  termination_prefix (TERM_HALT);
  termination_suffix (TERM_HALT, value, false);
}

void
termination_init_error (void)
{
  termination_prefix (TERM_EXIT);
  termination_suffix (TERM_EXIT, 1, true);
}

void
termination_end_of_computation (void)
{
  termination_prefix (TERM_END_OF_COMPUTATION);
  Print_Expression (GET_VAL, "Final result");
  outf_console("\n");
  termination_suffix (TERM_END_OF_COMPUTATION, 0, false);
}

void
termination_trap (void)
{
  /* This claims not to be abnormal so that the user will
     not be asked a second time about dumping core. */
  termination_prefix (TERM_TRAP);
  termination_suffix (TERM_TRAP, 1, false);
}

void
termination_no_error_handler (void)
{
  /* This does not print a back trace because the caller printed one. */
  termination_prefix (TERM_NO_ERROR_HANDLER);
  if (death_blow == ERR_FASL_FILE_TOO_BIG)
    {
      unsigned long heap_size;
      unsigned long const_size;
      get_band_parameters ((&heap_size), (&const_size));
      outf_fatal ("Try again with values at least as large as\n");
      outf_fatal ("  --heap %lu\n",
		  (MIN_HEAP_DELTA + (BYTES_TO_BLOCKS (heap_size))));
      outf_fatal ("  --constant %lu\n", (BYTES_TO_BLOCKS (const_size)));
    }
  termination_suffix (TERM_NO_ERROR_HANDLER, 1, true);
}

void
termination_gc_out_of_space (void)
{
  termination_prefix (TERM_GC_OUT_OF_SPACE);
  outf_fatal ("You are out of space at the end of a garbage collection!\n");
  outf_fatal
    ("Free = %#lx; heap_alloc_limit = %#lx; heap_end = %#lx\n",
     ((unsigned long) Free),
     ((unsigned long) heap_alloc_limit),
     ((unsigned long) heap_end));
  outf_fatal ("# words needed = %lu; # words available = %lu\n",
	      gc_space_needed, HEAP_AVAILABLE);
  termination_suffix_trace (TERM_GC_OUT_OF_SPACE);
}

void
termination_eof (void)
{
  Microcode_Termination (TERM_EOF);
}

void
termination_signal (const char * signal_name)
{
  if (signal_name != 0)
    {
      termination_prefix (TERM_SIGNAL);
      outf_fatal ("Killed by %s.\n", signal_name);
    }
  else
    attempt_termination_backout (TERM_SIGNAL);
  termination_suffix_trace (TERM_SIGNAL);
}

static void
edwin_auto_save (void)
{
  static SCHEME_OBJECT position;
  static struct interpreter_state_s new_state;

  position =
    ((VECTOR_P (fixed_objects))
     ? (VECTOR_REF (fixed_objects, FIXOBJ_EDWIN_AUTO_SAVE))
     : EMPTY_LIST);
  while (PAIR_P (position))
    {
      SCHEME_OBJECT entry = (PAIR_CAR (position));
      position = (PAIR_CDR (position));
      if ((PAIR_P (entry))
	  && (GROUP_P (PAIR_CAR (entry)))
	  && (STRING_P (PAIR_CDR (entry)))
	  && ((GROUP_MODIFIED_P (PAIR_CAR (entry))) == SHARP_T))
	{
	  SCHEME_OBJECT group = (PAIR_CAR (entry));
	  char * namestring = (STRING_POINTER (PAIR_CDR (entry)));
	  unsigned long length;
	  unsigned char * start = (GROUP_TEXT (group, (&length)));
	  unsigned char * end = (start + length);
	  unsigned char * gap_start = (start + (GROUP_GAP_START (group)));
	  unsigned char * gap_end = (start + (GROUP_GAP_END (group)));
	  if ((start < gap_start) || (gap_end < end))
	    {
	      bind_interpreter_state (&new_state);
	      if ((setjmp (interpreter_catch_env)) == 0)
		{
		  Tchannel channel;
		  outf_error_line ("Auto-saving file \"%s\"", namestring);
		  channel = (OS_open_output_file (namestring));
		  if (start < gap_start)
		    OS_channel_write (channel, start, (gap_start - start));
		  if (gap_end < end)
		    OS_channel_write (channel, gap_end, (end - gap_end));
		  OS_channel_close (channel);
		}
	      unbind_interpreter_state (&new_state);
	    }
	}
    }
}

static void
delete_temp_files (void)
{
  static SCHEME_OBJECT position;
  static struct interpreter_state_s new_state;

  position =
    ((VECTOR_P (fixed_objects))
     ? (VECTOR_REF (fixed_objects, FIXOBJ_FILES_TO_DELETE))
     : EMPTY_LIST);
  while (PAIR_P (position))
    {
      SCHEME_OBJECT entry = (PAIR_CAR (position));
      position = (PAIR_CDR (position));
      if (STRING_P (entry))
	{
	  bind_interpreter_state (&new_state);
	  if ((setjmp (interpreter_catch_env)) == 0)
	    OS_file_remove (STRING_POINTER (entry));
	  unbind_interpreter_state (&new_state);
	}
    }
}