1 /* Copyright (C) 1994, 1995, 1996 Free Software Foundation, Inc.
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2, or (at your option)
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this software; see the file COPYING. If not, write to
15 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
17 * As a special exception, the Free Software Foundation gives permission
18 * for additional uses of the text contained in its release of GUILE.
20 * The exception is that, if you link the GUILE library with other files
21 * to produce an executable, this does not by itself cause the
22 * resulting executable to be covered by the GNU General Public License.
23 * Your use of that executable is in no way restricted on account of
24 * linking the GUILE library code into it.
26 * This exception does not however invalidate any other reasons why
27 * the executable file might be covered by the GNU General Public License.
29 * This exception applies only to the code released by the
30 * Free Software Foundation under the name GUILE. If you copy
31 * code from other Free Software Foundation releases into a copy of
32 * GUILE, as the General Public License permits, the exception does
33 * not apply to the code that you add in this way. To avoid misleading
34 * anyone as to the status of such modified files, you must delete
35 * this exception notice from them.
37 * If you write modifications of your own for GUILE, it is your choice
38 * whether to permit this exception to apply to your modifications.
39 * If you do not wish that, delete this exception notice.
47 #include <sys/param.h>
60 extern char *getenv ();
63 /* {Top Level Evaluation}
65 * Top level evaluation has to establish a dynamic root context,
66 * enable Scheme signal handlers, and catch global escapes (errors, quits,
67 * aborts, restarts, and execs) from the interpreter.
71 /* {Printing Objects to Strings}
76 gscm_portprint_obj (SCM port
, SCM obj
)
79 gscm_portprint_obj (port
, obj
)
84 scm_iprin1 (obj
, port
, 1);
89 struct seval_str_frame
99 _seval_str_fn (void * vframe
)
102 _seval_str_fn (vframe
)
106 struct seval_str_frame
* frame
;
107 frame
= (struct seval_str_frame
*)vframe
;
108 frame
->status
= gscm_seval_str (frame
->answer
, frame
->top
, frame
->str
);
115 gscm_strprint_obj (SCM
* answer
, SCM obj
)
118 gscm_strprint_obj (answer
, obj
)
126 str
= scm_makstr (64, 0);
127 port
= scm_mkstrport (SCM_MAKINUM (0), str
, SCM_OPN
| SCM_WRTNG
, "gscm_strprint_obj");
128 stat
= gscm_portprint_obj (port
, obj
);
132 *answer
= SCM_BOOL_F
;
138 gscm_cstr (char ** answer
, SCM obj
)
141 gscm_cstr (answer
, obj
)
148 *answer
= (char *)malloc (SCM_LENGTH (obj
));
151 stat
= GSCM_OUT_OF_MEM
;
153 memcpy (*answer
, SCM_CHARS (obj
), SCM_LENGTH (obj
));
158 /* {Invoking The Interpreter}
163 gscm_silent_repl (SCM env
)
166 gscm_silent_repl (env
)
172 answer
= SCM_UNSPECIFIED
;
173 while ((source
= scm_read (SCM_UNDEFINED
, SCM_UNDEFINED
, SCM_UNDEFINED
)) != SCM_EOF_VAL
)
174 answer
= scm_eval_x (source
);
180 typedef int setjmp_type
;
182 typedef long setjmp_type
;
187 _eval_port (SCM
* answer
, GSCM_top_level toplvl
, SCM port
, int printp
)
190 _eval_port (answer
, toplvl
, port
, printp
)
192 GSCM_top_level toplvl
;
200 static int deja_vu
= 0;
204 return GSCM_ILLEGALLY_REENTERED
;
207 /* Take over signal handlers for all the interesting signals.
212 /* Default return values:
217 *answer
= SCM_BOOL_F
;
219 /* Perform evalutation under a new dynamic root.
222 SCM_BASE (scm_rootcont
) = (SCM_STACKITEM
*) & i
;
223 #ifdef DEBUG_EXTENSIONS
224 SCM_DFRAME (scm_rootcont
) = last_debug_info_frame
= 0;
226 saved_inp
= scm_cur_inp
;
227 i
= setjmp (SCM_JMPBUF (scm_rootcont
));
228 #ifdef SCM_STACK_CHECK
229 scm_check_stack_p
= 1;
233 scm_gc_heap_lock
= 0;
234 scm_ints_disabled
= 0;
235 /* need to close loading files here. */
240 *answer
= gscm_silent_repl (top_env
);
242 scm_cur_inp
= saved_inp
;
244 status
= gscm_strprint_obj (answer
, *answer
);
248 scm_cur_inp
= saved_inp
;
249 *answer
= scm_exitval
;
251 gscm_strprint_obj (answer
, *answer
);
255 scm_gc_heap_lock
= 1;
256 scm_ints_disabled
= 1;
257 scm_restore_signals ();
264 seval_str (SCM
*answer
, GSCM_top_level toplvl
, char * str
)
267 seval_str (answer
, toplvl
, str
)
269 GSCM_top_level toplvl
;
277 scheme_str
= scm_makfromstr (str
, strlen (str
), 0);
278 port
= scm_mkstrport (SCM_MAKINUM (0), scheme_str
, SCM_OPN
| SCM_RDNG
, "gscm_seval_str");
279 status
= _eval_port (answer
, toplvl
, port
, 0);
286 gscm_seval_str (SCM
*answer
, GSCM_top_level toplvl
, char * str
)
289 gscm_seval_str (answer
, toplvl
, str
)
291 GSCM_top_level toplvl
;
298 status
= seval_str (answer
, toplvl
, str
);
305 format_load_command (char * buf
, char *file_name
)
308 format_load_command (buf
, file_name
)
313 char quoted_name
[MAXPATHLEN
+ 1];
317 for (source
= dest
= 0; file_name
[source
]; ++source
)
319 if (file_name
[source
] == '"')
320 quoted_name
[dest
++] = '\\';
321 quoted_name
[dest
++] = file_name
[source
];
323 quoted_name
[dest
] = 0;
324 sprintf (buf
, "(%%try-load \"%s\")", quoted_name
);
329 gscm_seval_file (SCM
*answer
, GSCM_top_level toplvl
, char * file_name
)
332 gscm_seval_file (answer
, toplvl
, file_name
)
334 GSCM_top_level toplvl
;
338 char command
[MAXPATHLEN
* 3];
339 format_load_command (command
, file_name
);
340 return gscm_seval_str (answer
, toplvl
, command
);
346 eval_str (char ** answer
, GSCM_top_level toplvl
, char * str
)
349 eval_str (answer
, toplvl
, str
)
351 GSCM_top_level toplvl
;
360 scheme_str
= scm_makfromstr (str
, strlen (str
), 0);
361 port
= scm_mkstrport (SCM_MAKINUM(0), scheme_str
, SCM_OPN
| SCM_RDNG
, "gscm_eval_str");
362 status
= _eval_port (&sanswer
, toplvl
, port
, 1);
365 if (status
== GSCM_OK
)
366 status
= gscm_cstr (answer
, sanswer
);
376 gscm_eval_str (char ** answer
, GSCM_top_level toplvl
, char * str
)
379 gscm_eval_str (answer
, toplvl
, str
)
381 GSCM_top_level toplvl
;
388 status
= eval_str (answer
, toplvl
, str
);
396 gscm_eval_file (char ** answer
, GSCM_top_level toplvl
, char * file_name
)
399 gscm_eval_file (answer
, toplvl
, file_name
)
401 GSCM_top_level toplvl
;
405 char command
[MAXPATHLEN
* 3];
406 format_load_command (command
, file_name
);
407 return gscm_eval_str (answer
, toplvl
, command
);
423 static char * gscm_error_msgs
[] =
425 AT(GSCM_OK
) "No error.",
426 AT(GSCM_ERROR
) "ERROR in init file.",
427 AT(GSCM_ILLEGALLY_REENTERED
) "Gscm function was illegally reentered.",
428 AT(GSCM_OUT_OF_MEM
) "Out of memory.",
429 AT(GSCM_ERROR_OPENING_FILE
) "Error opening file.",
430 AT(GSCM_ERROR_OPENING_INIT_FILE
) "Error opening init file."
435 gscm_error_msg (int n
)
442 if ((n
< 0) || (n
> (sizeof (gscm_error_msgs
) / sizeof (char *))))
443 return "Unrecognized error.";
445 return gscm_error_msgs
[n
];
450 /* {Defining New Procedures}
455 gscm_make_subr (SCM (*fn
)(), int req
, int opt
, int varp
, char * doc
)
458 gscm_make_subr (fn
, req
, opt
, varp
, doc
)
466 return scm_make_gsubr ("*anonymous*", req
, opt
, varp
, fn
);
478 SCM_ASSERT (SCM_ICHRP (c
), c
, SCM_ARG1
, "gscm_2_char");
486 gscm_2_str (char ** out
, int * len_out
, SCM
* objp
)
489 gscm_2_str (out
, len_out
, objp
)
495 SCM_ASSERT (SCM_NIMP (*objp
) && SCM_STRINGP (*objp
), *objp
, SCM_ARG3
, "gscm_2_str");
497 *out
= SCM_CHARS (*objp
);
499 *len_out
= SCM_LENGTH (*objp
);
505 gscm_error (char * message
, SCM args
)
508 gscm_error (message
, args
)
516 errsym
= SCM_CAR (scm_intern ("error", 5));
517 str
= scm_makfrom0str (message
);
518 scm_throw (errsym
, scm_cons (str
, args
));
524 gscm_run_scm (int argc
, char ** argv
, FILE * in
, FILE * out
, FILE * err
, GSCM_status (*initfn
)(), char * initfile
, char * initcmd
)
527 gscm_run_scm (argc
, argv
, in
, out
, err
, initfn
, initfile
, initcmd
)
533 GSCM_status (*initfn
)();
542 scm_ports_prehistory ();
543 scm_smob_prehistory ();
544 scm_tables_prehistory ();
545 scm_init_storage (0);
546 scm_start_stack (&i
, in
, out
, err
);
550 /* scm_init_debug (); */
553 scm_init_arbiters ();
557 scm_init_continuations ();
568 scm_init_lvectors ();
573 scm_init_procprop ();
575 scm_init_stackchk ();
576 scm_init_strports ();
582 scm_init_sequences ();
585 scm_init_strorder ();
586 scm_init_mbstrings ();
589 scm_init_variable ();
603 /* Save the argument list to be the return value of (program-arguments).
605 scm_progargs
= scm_makfromstrs (argc
, argv
);
607 scm_gc_heap_lock
= 0;
609 scm_ints_disabled
= 1;
615 if (initfile
== NULL
)
617 initfile
= getenv ("GUILE_INIT_PATH");
618 if (initfile
== NULL
)
619 initfile
= SCM_IMPLINIT
;
622 if (initfile
== NULL
)
630 status
= gscm_seval_file (&answer
, -1, initfile
);
631 if ((status
== GSCM_OK
) && (answer
== SCM_BOOL_F
))
632 status
= GSCM_ERROR_OPENING_INIT_FILE
;
637 if (status
== GSCM_OK
)
639 scm_sysintern ("*stdin*", scm_cur_inp
);
640 status
= gscm_seval_str (0, top
, initcmd
);
649 scm_init_guile (void)