This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/C/yap-args.c

1254 lines
34 KiB
C
Raw Permalink Normal View History

2012-06-22 10:01:32 +01:00
/*************************************************************************
2017-12-05 15:14:57 +00:00
* *
* Yap Prolog *
* *
* Yap Prolog Was Developed At Nccup - Universidade Do Porto *
* *
* Copyright L.Damas, V.S.Costa And Universidade Do Porto 1985-1997 *
* *
**************************************************************************
* *
* File: Yap.C * Last
2017-12-10 11:42:51 +00:00
*Rev:
2017-12-10 01:22:45 +00:00
* Mods:
2017-12-05 15:14:57 +00:00
** Comments: Yap's Main File: parse arguments *
* *
*************************************************************************/
2012-06-22 10:01:32 +01:00
/* static char SccsId[] = "X 4.3.3"; */
2013-11-16 00:27:02 +00:00
#include "Yap.h"
2013-11-25 10:34:50 +00:00
#include "YapHeap.h"
2016-01-31 10:21:50 +00:00
#include "YapInterface.h"
2017-12-10 01:22:45 +00:00
#include "YapStreams.h"
2018-02-14 10:37:07 +00:00
#include "iopreds.h"
2017-12-05 15:14:57 +00:00
2015-06-19 01:30:13 +01:00
#if HAVE_UNISTD_H
2017-12-05 15:14:57 +00:00
2015-06-19 01:30:13 +01:00
#include <unistd.h>
2017-12-05 15:14:57 +00:00
2015-06-19 01:30:13 +01:00
#endif
2013-11-15 10:38:00 +00:00
#if HAVE_STDINT_H
2017-12-05 15:14:57 +00:00
2013-11-15 10:38:00 +00:00
#include <stdint.h>
2017-12-05 15:14:57 +00:00
2013-11-15 10:38:00 +00:00
#endif
2017-12-05 15:14:57 +00:00
2016-04-05 08:07:28 +01:00
#include <stdlib.h>
2017-12-05 15:14:57 +00:00
2018-06-18 10:51:01 +01:00
#include <stddef.h>
2012-06-22 10:01:32 +01:00
#ifdef _MSC_VER /* Microsoft's Visual C++ Compiler */
2016-01-31 10:21:50 +00:00
#ifdef HAVE_UNISTD_H
#undef HAVE_UNISTD_H
2012-06-22 10:01:32 +01:00
#endif
#endif
#include <stdio.h>
2017-12-05 15:14:57 +00:00
2012-06-22 10:01:32 +01:00
#if HAVE_STRING_H
2017-12-05 15:14:57 +00:00
2012-06-22 10:01:32 +01:00
#include <string.h>
2017-12-05 15:14:57 +00:00
2012-06-22 10:01:32 +01:00
#endif
#if HAVE_ERRNO_H
2017-12-05 15:14:57 +00:00
#include <errno.h>
2017-12-05 15:14:57 +00:00
#endif
#if HAVE_DIRECT_H
#include <direct.h>
#endif
2012-06-22 10:01:32 +01:00
2017-12-10 11:42:51 +00:00
#if HAVE_LIBGEN_H
2017-11-15 12:18:19 +00:00
#include <libgen.h>
#endif
2018-01-18 14:47:27 +00:00
X_API bool YAP_initialized = false;
static int n_mdelays = 0;
static YAP_delaymodule_t *m_delays;
static void init_globals(YAP_init_args *yap_init) {
GLOBAL_FAST_BOOT_FLAG = yap_init->FastBoot;
#if defined(YAPOR) || defined(TABLING)
Yap_init_root_frames();
#endif /* YAPOR || TABLING */
#ifdef YAPOR
Yap_init_yapor_workers();
2019-01-21 01:11:42 +00:00
if (
2018-01-18 14:47:27 +00:00
#if YAPOR_THREADS
2019-01-21 01:11:42 +00:00
Yap_thread_self() != 0
2018-01-18 14:47:27 +00:00
#else
2019-01-21 01:11:42 +00:00
worker_id != 0
2018-01-18 14:47:27 +00:00
#endif
2019-01-21 01:11:42 +00:00
) {
2018-01-18 14:47:27 +00:00
#if defined(YAPOR_COPY) || defined(YAPOR_SBA)
/*
In the SBA we cannot just happily inherit registers
from the other workers
*/
Yap_InitYaamRegs(worker_id, true);
#endif /* YAPOR_COPY || YAPOR_SBA */
#ifndef YAPOR_THREADS
Yap_InitPreAllocCodeSpace(0);
#endif /* YAPOR_THREADS */
/* slaves, waiting for work */
CurrentModule = USER_MODULE;
P = GETWORK_FIRST_TIME;
Yap_exec_absmi(FALSE, YAP_EXEC_ABSMI);
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
2019-01-21 01:11:42 +00:00
"abstract machine unexpected exit (YAP_Init)");
2018-01-18 14:47:27 +00:00
}
#endif /* YAPOR */
RECOVER_MACHINE_REGS();
/* make sure we do this after restore */
if (yap_init->MaxStackSize) {
GLOBAL_AllowLocalExpansion = FALSE;
} else {
GLOBAL_AllowLocalExpansion = TRUE;
}
if (yap_init->MaxGlobalSize) {
GLOBAL_AllowGlobalExpansion = FALSE;
} else {
GLOBAL_AllowGlobalExpansion = TRUE;
}
if (yap_init->MaxTrailSize) {
GLOBAL_AllowTrailExpansion = FALSE;
} else {
GLOBAL_AllowTrailExpansion = TRUE;
}
if (yap_init->PrologRCFile) {
Yap_PutValue(AtomConsultOnBoot,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(yap_init->PrologRCFile)));
2018-01-18 14:47:27 +00:00
/*
This must be done again after restore, as yap_flags
has been overwritten ....
*/
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
2019-01-21 01:11:42 +00:00
yap_init->HaltAfterBoot);
2018-01-18 14:47:27 +00:00
}
if (yap_init->PrologTopLevelGoal) {
Yap_PutValue(AtomTopLevelGoal,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(yap_init->PrologTopLevelGoal)));
2018-01-18 14:47:27 +00:00
}
if (yap_init->PrologGoal) {
Yap_PutValue(AtomInitGoal,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(yap_init->PrologGoal)));
2018-01-18 14:47:27 +00:00
}
if (yap_init->PrologAddPath) {
Yap_PutValue(AtomExtendFileSearchPath,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(yap_init->PrologAddPath)));
2018-01-18 14:47:27 +00:00
}
if (yap_init->QuietMode) {
2019-03-12 10:51:39 +00:00
setBooleanLocalPrologFlag(VERBOSE_LOAD_FLAG, TermFalse);
2018-01-18 14:47:27 +00:00
}
}
2019-01-21 01:11:42 +00:00
2017-12-10 01:22:45 +00:00
const char *Yap_BINDIR, *Yap_ROOTDIR, *Yap_SHAREDIR, *Yap_LIBDIR, *Yap_DLLDIR,
2019-01-21 01:11:42 +00:00
*Yap_PLDIR, *Yap_BOOTSTRAP, *Yap_COMMONSDIR, *Yap_INPUT_STARTUP,
*Yap_OUTPUT_STARTUP, *Yap_SOURCEBOOT, *Yap_INCLUDEDIR, *Yap_PLBOOTDIR;
2017-12-05 15:14:57 +00:00
2018-03-17 10:38:56 +00:00
/**
2018-07-17 11:43:57 +01:00
* consult loop in C: used to boot the system, butt supports goal execution and
* recursive consulting.
2018-03-17 10:38:56 +00:00
*
* */
2018-12-14 10:29:12 +00:00
static bool load_file(const char *b_file USES_REGS) {
2017-12-10 11:42:51 +00:00
Term t;
2018-02-14 00:13:13 +00:00
int c_stream, osno, oactive;
2018-12-14 10:29:12 +00:00
2019-01-21 01:11:42 +00:00
Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1);
2017-12-10 11:42:51 +00:00
Functor functor_command1 = Yap_MkFunctor(Yap_LookupAtom(":-"), 1);
2018-01-19 14:38:26 +00:00
Functor functor_compile2 = Yap_MkFunctor(Yap_LookupAtom("c_compile"), 1);
2018-01-22 13:53:17 +00:00
2018-03-17 10:38:56 +00:00
/* consult in C */
2018-02-14 10:37:07 +00:00
int lvl = push_text_stack();
2019-01-31 11:52:03 +00:00
2018-07-02 16:48:00 +01:00
char *full;
2019-01-21 01:11:42 +00:00
/* the consult mode does not matter here, really */
2018-06-18 12:13:33 +01:00
if ((osno = Yap_CheckAlias(AtomLoopStream)) < 0) {
2018-02-14 10:37:07 +00:00
osno = 0;
2018-06-18 12:13:33 +01:00
}
2018-04-07 19:45:18 +01:00
c_stream = YAP_InitConsult(YAP_BOOT_MODE, b_file, &full, &oactive);
2019-01-21 01:11:42 +00:00
__android_log_print(
ANDROID_LOG_INFO, "YAPDroid", "done init_consult %s ",b_file);
if (c_stream < 0) {
2019-01-22 03:08:26 +00:00
fprintf(stderr, "[ FATAL ERROR: could not open file %s\n", b_file);
2018-03-02 21:18:24 +00:00
pop_text_stack(lvl);
2017-12-10 11:42:51 +00:00
exit(1);
}
2018-02-14 10:37:07 +00:00
if (!Yap_AddAlias(AtomLoopStream, c_stream)) {
pop_text_stack(lvl);
2018-06-18 22:53:53 +01:00
return false;
2018-02-14 10:37:07 +00:00
}
2018-12-14 10:29:12 +00:00
__android_log_print(
2019-01-21 01:11:42 +00:00
ANDROID_LOG_INFO, "YAPDroid", "do reset %s ",b_file);
2019-01-22 03:08:26 +00:00
t = 0;
2019-01-21 01:11:42 +00:00
while (t != TermEof) {
2017-12-10 11:42:51 +00:00
CACHE_REGS
2019-01-21 01:11:42 +00:00
YAP_Reset(YAP_FULL_RESET, false);
Yap_StartSlots();
2018-06-18 12:16:36 +01:00
Term vs = MkVarTerm(), pos = MkVarTerm();
2019-01-31 11:52:03 +00:00
2018-02-14 00:13:13 +00:00
t = YAP_ReadClauseFromStream(c_stream, vs, pos);
2018-01-22 13:53:17 +00:00
// Yap_GetNèwSlot(t);
2019-01-21 01:11:42 +00:00
if (t == TermEof || t == TermNil) {
continue;
} else if (t == 0) {
fprintf(stderr, "%s:" Int_FORMAT " :0: error: SYNTAX ERROR\n",
b_file, GLOBAL_Stream[c_stream].linecount);
//
// {
// char buu[1024];
//1
// YAP_WriteBuffer(t, buu, 1023, 0);
// fprintf(stderr, "[ %s ]\n" , buu);
// }
continue;
} else if (IsVarTerm(t)) {
fprintf(stderr, "%s:" Int_FORMAT ":0: error: unbound or NULL parser output\n\n",
b_file,
GLOBAL_Stream[c_stream].linecount);
continue;
} else if (IsApplTerm(t) &&
(FunctorOfTerm(t) == functor_query ||
FunctorOfTerm(t) == functor_command1)) {
2018-01-18 14:47:27 +00:00
t = ArgOfTerm(1, t);
if (IsApplTerm(t) && FunctorOfTerm(t) == functor_compile2) {
2019-01-21 01:11:42 +00:00
load_file(RepAtom(AtomOfTerm(ArgOfTerm(1, t)))->StrOfAE);
2018-11-23 10:55:05 +00:00
Yap_ResetException(LOCAL_ActiveError);
2019-01-21 01:11:42 +00:00
continue;
2018-01-18 14:47:27 +00:00
} else {
2019-01-21 01:11:42 +00:00
YAP_RunGoalOnce(t);
2018-01-18 14:47:27 +00:00
}
2017-12-10 11:42:51 +00:00
} else {
2018-07-17 11:43:57 +01:00
YAP_CompileClause(t);
2017-12-10 01:22:45 +00:00
}
2019-01-21 01:11:42 +00:00
2018-04-09 02:00:51 +01:00
yap_error_descriptor_t *errd;
2019-01-21 01:11:42 +00:00
if ((errd = Yap_GetException(LOCAL_ActiveError)) &&
(errd->errorNo != YAP_NO_ERROR)) {
fprintf(stderr, "%s:" Int_FORMAT ":0: error: %s/%s %s\n\n", b_file, errd->errorLine, errd->errorAsText, errd->classAsText, errd->errorMsg);
2017-12-10 01:22:45 +00:00
}
2019-01-21 01:11:42 +00:00
}
2017-12-10 01:22:45 +00:00
BACKUP_MACHINE_REGS();
2018-02-14 10:37:07 +00:00
YAP_EndConsult(c_stream, &osno, full);
2018-07-02 16:48:00 +01:00
if (!Yap_AddAlias(AtomLoopStream, osno)) {
pop_text_stack(lvl);
return false;
}
2018-07-17 11:43:57 +01:00
pop_text_stack(lvl);
2019-01-21 01:11:42 +00:00
return t == TermEof;
2017-12-10 01:22:45 +00:00
}
2018-12-14 10:29:12 +00:00
static const char * EOLIST ="EOLINE";
2019-01-21 01:11:42 +00:00
static bool is_install;
2018-12-14 10:29:12 +00:00
2019-01-21 01:11:42 +00:00
static bool is_dir( const char *path, const void *info) {
if (is_install)
return true;
2018-12-14 10:29:12 +00:00
2019-01-21 01:11:42 +00:00
if (Yap_isDirectory( path ))
return true;
char s[YAP_FILENAME_MAX + 1];
Int i = strlen(path);
strncpy(s, path, YAP_FILENAME_MAX);
2018-12-14 10:29:12 +00:00
while (--i) {
if (Yap_dir_separator((int)path[i]))
2019-01-21 01:11:42 +00:00
break;
2018-12-14 10:29:12 +00:00
}
if (i == 0) {
s[0] = '.';
i = 1;
}
s[i] = '\0';
2019-01-09 09:32:09 +00:00
if (info == NULL)
2019-01-21 01:11:42 +00:00
return true;
2018-12-14 10:29:12 +00:00
return
strcmp(info,s) == 0 ||
Yap_isDirectory( s );
2019-01-21 01:11:42 +00:00
}
static bool is_file( const char *path, const void *info) {
if (is_install)
return true;
return Yap_Exists( path );
}
static bool is_wfile( const char *path, const void *info) {
return true;
}
typedef bool testf(const char *s, const void *info);
2018-12-14 10:29:12 +00:00
///
///
2019-01-21 01:11:42 +00:00
static const char *sel(
testf test, const void *info, const char *s1, ...) {
const char *fmt = s1;
va_list ap;
char *buf = malloc(FILENAME_MAX + 1);
va_start(ap, s1);
while (fmt != EOLIST) {
if (fmt == NULL || fmt[0]=='\0') {
fmt = va_arg(ap, const char *);
continue;
}
strncpy(buf, fmt, FILENAME_MAX); // Yap_AbsoluteFile(fmt,true), FILENAME_MAX);
2018-12-14 10:29:12 +00:00
if (test(buf,info)) {
2019-01-21 01:11:42 +00:00
buf = realloc(buf, strlen(buf) + 1);
va_end(ap);
return buf;
2018-12-14 10:29:12 +00:00
}
2019-01-21 01:11:42 +00:00
fmt = va_arg(ap, const char *);
}
va_end(ap);
free(buf);
return NULL;
}
2018-12-14 10:29:12 +00:00
2017-12-05 15:14:57 +00:00
2018-03-02 21:18:24 +00:00
static const char *join(const char *s0, const char *s1) {
CACHE_REGS
2018-03-02 21:18:24 +00:00
2019-01-21 01:11:42 +00:00
if (!s0 || s0[0] == '\0') {
if (s1 && s1[0])
return s1;
else
return NULL;
}
2018-03-02 21:18:24 +00:00
if (!s1 || s1[0] == '\0')
return s0;
2018-02-26 21:38:19 +00:00
// int lvl = push_text_stack();
2019-01-09 09:32:09 +00:00
char *buf = malloc(strlen(s0)+strlen(s1) + 2);
2018-03-02 21:18:24 +00:00
strcpy(buf, s0);
2019-01-09 09:32:09 +00:00
if (Yap_dir_separator(s0[strlen(s0)-1])) {
2019-01-21 01:11:42 +00:00
if (Yap_dir_separator(s1[0])) {
s1 += 1;
}
2019-01-09 09:32:09 +00:00
} else {
if (!Yap_dir_separator(s1[0]-1)) {
2019-01-21 01:11:42 +00:00
strcat(buf, "/");
}
2019-01-09 09:32:09 +00:00
}
2018-03-02 21:18:24 +00:00
strcat(buf, s1);
return buf;
2017-12-10 01:22:45 +00:00
}
2017-12-05 15:14:57 +00:00
2018-03-02 21:18:24 +00:00
static void Yap_set_locations(YAP_init_args *iap) {
2019-01-21 01:11:42 +00:00
is_install= iap->install;
/// ROOT_DIR is the home of the YAP system. It can be:
/// -- provided by the user;
/// -- obtained from DESTDIR + DE=efalkRoot
///
/// It is:
// --_not useful in Android, WIN32;
/// -- DESTDIR/ in Anaconda
2019-01-21 01:11:42 +00:00
/// -- /usr/loca77l in most Unix style systems
Yap_ROOTDIR = sel( is_dir, NULL,
iap->ROOTDIR,
getenv("YAPROOTDIR"),
join(getenv("DESTDIR"), YAP_ROOTDIR),
2019-01-09 09:32:09 +00:00
2018-02-21 17:41:00 +00:00
#if __ANDROID__
2019-01-21 01:11:42 +00:00
"/",
2018-02-21 17:41:00 +00:00
#else
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), YAP_ROOTDIR),
join(getenv("DESTDIR"), join(getenv("ḦOME"),".local")),
join(getenv("DESTDIR"), "/usr/local"),
join(getenv("DESTDIR"), "/usr"),
join(getenv("DESTDIR"), "/opt"),
#endif
2019-01-21 01:11:42 +00:00
EOLIST
);
__android_log_print(
ANDROID_LOG_INFO,"YAPDroid", "Yap_ROOTDIR %s", Yap_ROOTDIR);
2018-12-14 10:29:12 +00:00
2019-01-21 01:11:42 +00:00
/// BINDIR: where the OS stores header files, namely libYap...
Yap_BINDIR = sel( is_dir, Yap_ROOTDIR, iap->BINDIR,
getenv("YAPBINDIR"),
2018-12-14 10:29:12 +00:00
#if !defined(__ANDROID__)
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), YAP_BINDIR),
#endif
2019-01-21 01:11:42 +00:00
join(Yap_ROOTDIR, "bin"),
EOLIST);
2018-12-14 10:29:12 +00:00
/// LIBDIR: where the OS stores dynamic libraries, namely libYap...
2019-01-21 01:11:42 +00:00
Yap_LIBDIR = sel( is_dir, Yap_ROOTDIR, iap->LIBDIR,
2018-12-14 10:29:12 +00:00
#if !defined(__ANDROID__)
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), YAP_LIBDIR),
#endif
2019-01-21 01:11:42 +00:00
join(Yap_ROOTDIR, "lib"),
EOLIST);
2018-12-14 10:29:12 +00:00
/// DLLDIR: where libraries can find expicitely loaded DLLs
2019-01-21 01:11:42 +00:00
Yap_DLLDIR = sel(is_dir, Yap_LIBDIR, iap->DLLDIR,
getenv("YAPLIBDIR"),
join(getenv("DESTDIR"), YAP_DLLDIR),
join(Yap_DLLDIR, "Yap"),
EOLIST);
2018-12-14 10:29:12 +00:00
/// INCLUDEDIR: where the OS stores header files, namely libYap...
2019-01-21 01:11:42 +00:00
Yap_INCLUDEDIR = sel(is_dir, Yap_ROOTDIR, iap->INCLUDEDIR,
2018-12-14 10:29:12 +00:00
#if !defined(__ANDROID__)
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), YAP_INCLUDEDIR),
#endif
2019-01-09 09:32:09 +00:00
join(Yap_ROOTDIR, "include"),
2018-12-14 10:29:12 +00:00
EOLIST);
2019-01-09 09:32:09 +00:00
2019-01-21 01:11:42 +00:00
/// SHAREDIR: where OS & ARCH independent files live
Yap_SHAREDIR = sel( is_dir, Yap_ROOTDIR, iap->SHAREDIR,
getenv("YAPSHAREDIR"),
2019-01-09 09:32:09 +00:00
#if __ANDROID__
2019-01-21 01:11:42 +00:00
"/data/data/pt.up.yap/files",
"/assets",
#endif
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), YAP_SHAREDIR),
join(Yap_ROOTDIR, "share"),
join(Yap_ROOTDIR, "files"),
EOLIST);
__android_log_print(
ANDROID_LOG_INFO,"YAPDroid", "Yap_SHAREDIR %s", Yap_SHAREDIR);
2019-01-09 09:32:09 +00:00
2019-01-21 01:11:42 +00:00
/// PLDIR: where we can find Prolog files
2018-12-14 10:29:12 +00:00
Yap_PLDIR = sel( is_dir, Yap_SHAREDIR, iap->PLDIR,
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), join(Yap_SHAREDIR, "Yap")),
EOLIST);
2018-12-14 10:29:12 +00:00
2019-01-21 01:11:42 +00:00
__android_log_print(
ANDROID_LOG_INFO, "YAPDroid","Yap_PLDIR %s", Yap_PLDIR);
2019-01-09 09:32:09 +00:00
2019-01-21 01:11:42 +00:00
/// ``COMMONSDIR: Prolog Commons
2019-01-09 09:32:09 +00:00
Yap_COMMONSDIR = sel(is_dir, Yap_SHAREDIR, iap->COMMONSDIR,
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), join(Yap_SHAREDIR, "PrologCommons")),
EOLIST);
2019-01-09 09:32:09 +00:00
/// SOURCEBOOT: booting from the Prolog boot file at compilation-time so we should not assume pl is installed.
2019-01-21 01:11:42 +00:00
Yap_SOURCEBOOT = sel( is_file, Yap_AbsoluteFile("pl",false), iap->SOURCEBOOT,
YAP_SOURCEBOOT,
"boot.yap",
"../pl/boot.yap",
EOLIST);
__android_log_print(
ANDROID_LOG_INFO, "YAPDroid","Yap_SOURCEBOOT %s", Yap_SOURCEBOOT);
Yap_PLBOOTDIR = sel( is_dir, Yap_PLDIR, iap->BOOTDIR,
join(getenv("DESTDIR"),join(Yap_PLDIR, "pl")),
EOLIST);
__android_log_print(
ANDROID_LOG_INFO, "YAPDroid","Yap_BOOTSTRAP %s", Yap_BOOTSTRAP);
/// BOOTSTRAP: booting from the Prolog boot file after YAP is installed
Yap_BOOTSTRAP = sel( is_file, Yap_PLBOOTDIR, iap->BOOTSTRAP,
join(getenv("DESTDIR"),YAP_BOOTSTRAP),
join(getenv("DESTDIR"),join(Yap_PLBOOTDIR, "boot.yap")),
EOLIST);
__android_log_print(
ANDROID_LOG_INFO,"YAPDroid", "Yap_BOOTSTRAP %s", Yap_PLBOOTDIR);
2018-03-02 21:18:24 +00:00
/// STARTUP: where we can find the core Prolog bootstrap file
Yap_OUTPUT_STARTUP =
2019-01-21 01:11:42 +00:00
sel( is_wfile, ".", iap->OUTPUT_STARTUP,
YAP_OUTPUT_STARTUP,
join(getenv("DESTDIR"), join(Yap_DLLDIR, "startup.yss")),
join(getenv("DESTDIR"), join(Yap_DLLDIR,iap->OUTPUT_STARTUP)),
"startup.yss",
EOLIST);
2018-12-14 10:29:12 +00:00
2018-03-02 21:18:24 +00:00
Yap_INPUT_STARTUP =
2018-12-14 14:53:39 +00:00
sel( is_file, Yap_DLLDIR, iap->INPUT_STARTUP,
2019-01-21 01:11:42 +00:00
"startup.yss",
join(getenv("DESTDIR"), join(Yap_DLLDIR, "startup.yss")),
2019-01-09 09:32:09 +00:00
#if !defined(__ANDROID__)
2019-01-21 01:11:42 +00:00
join(getenv("DESTDIR"), YAP_INPUT_STARTUP),
2018-02-21 17:41:00 +00:00
#endif
2019-01-21 01:11:42 +00:00
"/usr/local/lib/Yap/startup.yss",
"/usr/lib/Yap/startup.yss",
EOLIST);
2018-12-14 10:29:12 +00:00
2019-01-21 01:11:42 +00:00
if (Yap_ROOTDIR)
2017-12-20 00:29:15 +00:00
setAtomicGlobalPrologFlag(HOME_FLAG,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(Yap_ROOTDIR)));
2017-12-10 11:42:51 +00:00
if (Yap_PLDIR)
setAtomicGlobalPrologFlag(PROLOG_LIBRARY_DIRECTORY_FLAG,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(Yap_PLDIR)));
2017-12-10 11:42:51 +00:00
if (Yap_DLLDIR)
setAtomicGlobalPrologFlag(PROLOG_FOREIGN_DIRECTORY_FLAG,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(Yap_DLLDIR)));
2017-12-05 15:14:57 +00:00
}
2016-07-31 10:47:55 +01:00
static void print_usage(void) {
2017-12-10 01:22:45 +00:00
fprintf(stderr, "\n[ Valid switches for command line arguments: ]\n");
fprintf(stderr, " -? Shows this screen\n");
2019-01-09 09:32:09 +00:00
fprintf(stderr, " -B Used during compilation: boot from ../pl/boot.yap and generate a saved state. \n");
2017-12-10 01:22:45 +00:00
fprintf(stderr, " -b Boot file \n");
fprintf(stderr, " -dump-runtime-variables\n");
fprintf(stderr, " -f initialization file or \"none\"\n");
fprintf(stderr, " -g Run Goal Before Top-Level \n");
fprintf(stderr, " -z Run Goal Before Top-Level \n");
fprintf(stderr, " -q start with informational messages off\n");
fprintf(stderr, " -l load Prolog file\n");
fprintf(stderr, " -L run Prolog file and exit\n");
fprintf(stderr, " -p extra path for file-search-path\n");
fprintf(stderr, " -hSize Heap area in Kbytes (default: %d, minimum: %d)\n",
2019-01-21 01:11:42 +00:00
DefHeapSpace, MinHeapSpace);
2017-12-10 01:22:45 +00:00
fprintf(stderr,
2019-01-21 01:11:42 +00:00
" -sSize Stack area in Kbytes (default: %d, minimum: %d)\n",
DefStackSpace, MinStackSpace);
2017-12-10 01:22:45 +00:00
fprintf(stderr,
2019-01-21 01:11:42 +00:00
" -tSize Trail area in Kbytes (default: %d, minimum: %d)\n",
DefTrailSpace, MinTrailSpace);
2017-12-10 01:22:45 +00:00
fprintf(stderr, " -GSize Max Area for Global Stack\n");
fprintf(stderr,
2019-01-21 01:11:42 +00:00
" -LSize Max Area for Local Stack (number must follow L)\n");
2017-12-10 01:22:45 +00:00
fprintf(stderr, " -TSize Max Area for Trail (number must follow T)\n");
fprintf(stderr, " -nosignals disable signal handling from Prolog\n");
fprintf(stderr, "\n[Execution Modes]\n");
fprintf(stderr, " -J0 Interpreted mode (default)\n");
fprintf(stderr, " -J1 Mixed mode only for user predicates\n");
fprintf(stderr, " -J2 Mixed mode for all predicates\n");
fprintf(stderr, " -J3 Compile all user predicates\n");
fprintf(stderr, " -J4 Compile all predicates\n");
2012-06-22 10:01:32 +01:00
#ifdef TABLING
2017-12-10 01:22:45 +00:00
fprintf(stderr,
2019-01-21 01:11:42 +00:00
" -ts Maximum table space area in Mbytes (default: unlimited)\n");
2012-06-22 10:01:32 +01:00
#endif /* TABLING */
2019-01-21 01:11:42 +00:00
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA) || \
defined(YAPOR_THREADS)
2017-12-10 01:22:45 +00:00
fprintf(stderr, " -w Number of workers (default: %d)\n",
2019-01-21 01:11:42 +00:00
DEFAULT_NUMBERWORKERS);
2017-12-10 01:22:45 +00:00
fprintf(stderr,
2019-01-21 01:11:42 +00:00
" -sl Loop scheduler executions before look for hiden "
"shared work (default: %d)\n",
DEFAULT_SCHEDULERLOOP);
2017-12-10 01:22:45 +00:00
fprintf(stderr, " -d Value of delayed release of load (default: %d)\n",
2019-01-21 01:11:42 +00:00
DEFAULT_DELAYEDRELEASELOAD);
2012-06-22 10:01:32 +01:00
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA || YAPOR_THREADS */
2017-12-10 01:22:45 +00:00
/* nf: Preprocessor */
/* fprintf(stderr," -DVar=Name Persistent definition\n"); */
fprintf(stderr, "\n");
2012-06-22 10:01:32 +01:00
}
2016-01-31 10:21:50 +00:00
static int myisblank(int c) {
2017-12-10 01:22:45 +00:00
switch (c) {
case ' ':
case '\t':
case '\n':
case '\r':
return TRUE;
default:
return FALSE;
}
2012-06-22 10:01:32 +01:00
}
2016-01-31 10:21:50 +00:00
static char *add_end_dot(char arg[]) {
2017-12-10 01:22:45 +00:00
int sz = strlen(arg), i;
i = sz;
while (i && myisblank(arg[--i]))
;
if (i && arg[i] != ',') {
char *p = (char *)malloc(sz + 2);
if (!p)
return NULL;
strncpy(p, arg, sz);
p[sz] = '.';
p[sz + 1] = '\0';
return p;
}
return arg;
2012-06-22 10:01:32 +01:00
}
2016-01-31 10:21:50 +00:00
static int dump_runtime_variables(void) {
2017-12-10 01:22:45 +00:00
fprintf(stdout, "CC=\"%s\"\n", C_CC);
fprintf(stdout, "YAP_ROOTDIR=\"%s\"\n", YAP_ROOTDIR);
fprintf(stdout, "YAP_LIBS=\"%s\"\n", C_LIBS);
fprintf(stdout, "YAP_SHLIB_SUFFIX=\"%s\"\n", SO_EXT);
fprintf(stdout, "YAP_VERSION=%s\n", YAP_NUMERIC_VERSION);
exit(0);
return 1;
2012-06-22 10:01:32 +01:00
}
2017-12-05 15:14:57 +00:00
X_API YAP_file_type_t Yap_InitDefaults(void *x, char *saved_state, int argc,
2019-01-21 01:11:42 +00:00
char *argv[]) {
2017-12-10 01:22:45 +00:00
if (!LOCAL_TextBuffer)
LOCAL_TextBuffer = Yap_InitTextAllocator();
YAP_init_args *iap = x;
memset(iap, 0, sizeof(YAP_init_args));
2019-01-21 01:11:42 +00:00
iap->Argc = argc;
iap->Argv = argv;
2016-07-31 10:47:55 +01:00
#if __ANDROID__
2019-01-09 09:32:09 +00:00
iap->boot_file_type = YAP_PL;
2018-06-15 11:09:04 +01:00
iap->INPUT_STARTUP = NULL;
2017-12-10 01:22:45 +00:00
iap->assetManager = NULL;
2019-01-09 09:32:09 +00:00
return YAP_PL;
2016-07-31 10:47:55 +01:00
#else
2017-12-10 01:22:45 +00:00
iap->boot_file_type = YAP_QLY;
2018-02-25 00:29:08 +00:00
iap->INPUT_STARTUP = saved_state;
2017-12-10 01:22:45 +00:00
return YAP_QLY;
2018-12-14 10:29:12 +00:00
#endif
2016-07-31 10:47:55 +01:00
}
2012-06-22 10:01:32 +01:00
2017-12-05 15:14:57 +00:00
/**
2019-01-09 09:32:09 +00:00
* @short Parse command line
2017-12-05 15:14:57 +00:00
* @param argc number of arguments
* @param argv arguments
* @param iap options, see YAP_init_args
* @return boot from saved state or restore; error
*/
2019-01-09 09:32:09 +00:00
X_API YAP_file_type_t YAP_parse_yap_arguments(int argc, char *argv[], YAP_init_args *iap) {
2017-12-10 01:22:45 +00:00
char *p;
size_t *ssize;
2017-11-15 12:18:19 +00:00
2017-12-10 01:22:45 +00:00
Yap_InitDefaults(iap, NULL, argc, argv);
while (--argc > 0) {
p = *++argv;
if (*p == '-')
switch (*++p) {
case 'b':
2019-01-21 01:11:42 +00:00
iap->boot_file_type = YAP_PL;
if (p[1])
iap->BOOTSTRAP = p + 1;
else if (argv[1] && *argv[1] != '-') {
iap->BOOTSTRAP = *++argv;
argc--;
}
break;
2017-12-10 01:22:45 +00:00
case 'B':
2019-01-21 01:11:42 +00:00
iap->boot_file_type = YAP_SOURCE_PL;
if (p[1])
iap->SOURCEBOOT = p + 1;
else if (argv[1] && *argv[1] != '-') {
iap->SOURCEBOOT = *++argv;
argc--;
}
iap->install = true;
break;
2017-12-10 01:22:45 +00:00
case '?':
2019-01-21 01:11:42 +00:00
print_usage();
exit(EXIT_SUCCESS);
2017-12-10 01:22:45 +00:00
case 'q':
2019-01-21 01:11:42 +00:00
iap->QuietMode = TRUE;
break;
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA) || \
defined(YAPOR_THREADS)
2017-12-10 01:22:45 +00:00
case 'w':
2019-01-21 01:11:42 +00:00
ssize = &(iap->NumberWorkers);
goto GetSize;
2017-12-10 01:22:45 +00:00
case 'd':
2019-01-21 01:11:42 +00:00
if (!strcmp("dump-runtime-variables", p))
return dump_runtime_variables();
ssize = &(iap->DelayedReleaseLoad);
goto GetSize;
2012-06-22 10:01:32 +01:00
#else
2017-12-10 01:22:45 +00:00
case 'd':
2019-01-21 01:11:42 +00:00
if (!strcmp("dump-runtime-variables", p))
return dump_runtime_variables();
2012-06-22 10:01:32 +01:00
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA || YAPOR_THREADS */
2017-12-10 01:22:45 +00:00
case 'F':
2019-01-21 01:11:42 +00:00
/* just ignore for now */
argc--;
argv++;
break;
2017-12-10 01:22:45 +00:00
case 'f':
2019-01-21 01:11:42 +00:00
iap->FastBoot = TRUE;
if (argc > 1 && argv[1][0] != '-') {
argc--;
argv++;
if (strcmp(*argv, "none")) {
iap->PrologRCFile = *argv;
}
break;
}
break;
// execution mode
2017-12-10 01:22:45 +00:00
case 'J':
2019-01-21 01:11:42 +00:00
switch (p[1]) {
case '0':
iap->ExecutionMode = YAPC_INTERPRETED;
break;
case '1':
iap->ExecutionMode = YAPC_MIXED_MODE_USER;
break;
case '2':
iap->ExecutionMode = YAPC_MIXED_MODE_ALL;
break;
case '3':
iap->ExecutionMode = YAPC_COMPILE_USER;
break;
case '4':
iap->ExecutionMode = YAPC_COMPILE_ALL;
break;
default:
fprintf(stderr, "[ YAP unrecoverable error: unknown switch -%c%c ]\n",
*p, p[1]);
exit(EXIT_FAILURE);
}
p++;
break;
2017-12-10 01:22:45 +00:00
case 'G':
2019-01-21 01:11:42 +00:00
ssize = &(iap->MaxGlobalSize);
goto GetSize;
break;
2017-12-10 01:22:45 +00:00
case 's':
case 'S':
2019-01-21 01:11:42 +00:00
ssize = &(iap->StackSize);
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA) || \
defined(YAPOR_THREADS)
if (p[1] == 'l') {
p++;
ssize = &(iap->SchedulerLoop);
}
2012-06-22 10:01:32 +01:00
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA || YAPOR_THREADS */
2019-01-21 01:11:42 +00:00
goto GetSize;
2017-12-10 01:22:45 +00:00
case 'a':
case 'A':
2019-01-21 01:11:42 +00:00
ssize = &(iap->AttsSize);
goto GetSize;
2017-12-10 01:22:45 +00:00
case 'T':
2019-01-21 01:11:42 +00:00
ssize = &(iap->MaxTrailSize);
goto get_trail_size;
2017-12-10 01:22:45 +00:00
case 't':
2019-01-21 01:11:42 +00:00
ssize = &(iap->TrailSize);
2012-06-22 10:01:32 +01:00
#ifdef TABLING
2019-01-21 01:11:42 +00:00
if (p[1] == 's') {
p++;
ssize = &(iap->MaxTableSpaceSize);
}
2012-06-22 10:01:32 +01:00
#endif /* TABLING */
2017-12-10 01:22:45 +00:00
get_trail_size:
2019-01-21 01:11:42 +00:00
if (*++p == '\0') {
if (argc > 1)
--argc, p = *++argv;
else {
fprintf(stderr,
"[ YAP unrecoverable error: missing size in flag %s ]",
argv[0]);
print_usage();
exit(EXIT_FAILURE);
}
}
{
unsigned long int i = 0, ch;
while ((ch = *p++) >= '0' && ch <= '9')
i = i * 10 + ch - '0';
switch (ch) {
case 'M':
case 'm':
i *= 1024;
ch = *p++;
break;
case 'g':
i *= 1024 * 1024;
ch = *p++;
break;
case 'k':
case 'K':
ch = *p++;
break;
}
if (ch) {
iap->PrologTopLevelGoal = add_end_dot(*argv);
} else {
*ssize = i;
}
}
break;
2017-12-10 01:22:45 +00:00
case 'h':
case 'H':
2019-01-21 01:11:42 +00:00
ssize = &(iap->HeapSize);
2017-12-10 01:22:45 +00:00
GetSize:
2019-01-21 01:11:42 +00:00
if (*++p == '\0') {
if (argc > 1)
--argc, p = *++argv;
else {
fprintf(stderr,
"[ YAP unrecoverable error: missing size in flag %s ]",
argv[0]);
print_usage();
exit(EXIT_FAILURE);
}
}
{
unsigned long int i = 0, ch;
while ((ch = *p++) >= '0' && ch <= '9')
i = i * 10 + ch - '0';
switch (ch) {
case 'M':
case 'm':
i *= 1024;
ch = *p++;
break;
case 'g':
case 'G':
i *= 1024 * 1024;
ch = *p++;
break;
case 'k':
case 'K':
ch = *p++;
break;
}
if (ch) {
fprintf(
stderr,
"[ YAP unrecoverable error: illegal size specification %s ]",
argv[-1]);
Yap_exit(1);
}
*ssize = i;
}
break;
2012-06-22 10:01:32 +01:00
#ifdef DEBUG
2017-12-10 01:22:45 +00:00
case 'P':
2019-01-21 01:11:42 +00:00
if (p[1] != '\0') {
while (p[1] != '\0') {
int ch = p[1];
if (ch >= 'A' && ch <= 'Z')
ch += ('a' - 'A');
if (ch >= 'a' && ch <= 'z')
GLOBAL_Option[ch - 96] = 1;
p++;
}
} else {
YAP_SetOutputMessage();
}
break;
2012-06-22 10:01:32 +01:00
#endif
2017-12-10 01:22:45 +00:00
case 'L':
2019-01-21 01:11:42 +00:00
if (p[1] && p[1] >= '0' &&
p[1] <= '9') /* hack to emulate SWI's L local option */
{
ssize = &(iap->MaxStackSize);
goto GetSize;
}
iap->QuietMode = TRUE;
iap->HaltAfterBoot = true;
2017-12-10 01:22:45 +00:00
case 'l':
2019-01-21 01:11:42 +00:00
p++;
2019-02-15 13:50:24 +00:00
iap->QuietMode = TRUE;
2019-01-21 01:11:42 +00:00
if (!*++argv) {
fprintf(stderr,
"%% YAP unrecoverable error: missing load file name\n");
exit(1);
} else if (!strcmp("--", *argv)) {
/* shell script, the next entry should be the file itself */
iap->PrologRCFile = argv[1];
argc = 1;
break;
} else {
iap->PrologRCFile = *argv;
argc--;
}
if (*p) {
/* we have something, usually, of the form:
-L --
FileName
ExtraArgs
*/
/* being called from a script */
while (*p && (*p == ' ' || *p == '\t'))
p++;
if (p[0] == '-' && p[1] == '-') {
/* ignore what is next */
argc = 1;
}
}
break;
/* run goal before top-level */
2017-12-10 01:22:45 +00:00
case 'g':
2019-01-21 01:11:42 +00:00
if ((*argv)[0] == '\0')
iap->PrologGoal = *argv;
else {
argc--;
if (argc == 0) {
fprintf(stderr, " [ YAP unrecoverable error: missing "
"initialization goal for option 'g' ]\n");
exit(EXIT_FAILURE);
}
argv++;
iap->PrologGoal = *argv;
}
break;
/* run goal as top-level */
2017-12-10 01:22:45 +00:00
case 'z':
2019-01-21 01:11:42 +00:00
if ((*argv)[0] == '\0')
iap->PrologTopLevelGoal = *argv;
else {
argc--;
if (argc == 0) {
fprintf(stderr, " [ YAP unrecoverable error: missing goal for "
"option 'z' ]\n");
exit(EXIT_FAILURE);
}
argv++;
iap->PrologTopLevelGoal = add_end_dot(*argv);
}
iap->HaltAfterBoot = true;
break;
2017-12-10 01:22:45 +00:00
case 'n':
2019-01-21 01:11:42 +00:00
if (!strcmp("nosignals", p)) {
iap->PrologCannotHandleInterrupts = true;
break;
}
break;
2017-12-10 01:22:45 +00:00
case '-':
2019-01-21 01:11:42 +00:00
if (!strcmp("-nosignals", p)) {
iap->PrologCannotHandleInterrupts = true;
break;
} else if (!strncmp("-output-saved-state=", p,
strlen("-output-saved-state="))) {
iap->OUTPUT_STARTUP = p + strlen("-output-saved-state=");
} else if (!strncmp("-home=", p, strlen("-home="))) {
iap->ROOTDIR = p + strlen("-home=");
} else if (!strncmp("-system-library-directory=", p,
strlen("-system-library-directory="))) {
iap->LIBDIR = p + strlen("-system-library-directory=");
} else if (!strncmp("-system-shared-directory=", p,
strlen("-system-shared-directory="))) {
iap->SHAREDIR = p + strlen("-system-shared-directory=");
} else if (!strncmp("-prolog-library-directory=", p,
strlen("-prolog-library-directory="))) {
iap->PLDIR = p + strlen("-prolog-library-directory=");
} else if (!strncmp("-dll-library-directory=", p,
strlen("-dll-library-directory="))) {
iap->DLLDIR = p + strlen("-dll-library-directory=");
} else if (!strncmp("-home=", p, strlen("-home="))) {
iap->ROOTDIR = p + strlen("-home=");
} else if (!strncmp("-cwd=", p, strlen("-cwd="))) {
if (!Yap_ChDir(p + strlen("-cwd="))) {
fprintf(stderr, " [ YAP unrecoverable error in setting cwd: %s ]\n",
strerror(errno));
}
} else if (!strncmp("-stack=", p, strlen("-stack="))) {
ssize = &(iap->StackSize);
p += strlen("-stack=");
goto GetSize;
} else if (!strncmp("-trail=", p, strlen("-trail="))) {
ssize = &(iap->TrailSize);
p += strlen("-trail=");
goto GetSize;
} else if (!strncmp("-heap=", p, strlen("-heap="))) {
ssize = &(iap->HeapSize);
p += strlen("-heap=");
goto GetSize;
} else if (!strncmp("-max-stack=", p, strlen("-max-stack="))) {
ssize = &(iap->MaxStackSize);
p += strlen("-max-stack=");
goto GetSize;
} else if (!strncmp("-max-trail=", p, strlen("-max-trail="))) {
ssize = &(iap->MaxTrailSize);
p += strlen("-max-trail=");
goto GetSize;
} else if (!strncmp("-max-heap=", p, strlen("-max-heap="))) {
ssize = &(iap->MaxHeapSize);
p += strlen("-max-heap=");
goto GetSize;
} else if (!strncmp("-goal=", p, strlen("-goal="))) {
iap->PrologGoal = p + strlen("-goal=");
} else if (!strncmp("-top-level=", p, strlen("-top-level="))) {
iap->PrologTopLevelGoal = p + strlen("-top-level=");
} else if (!strncmp("-table=", p, strlen("-table="))) {
ssize = &(iap->MaxTableSpaceSize);
p += strlen("-table=");
goto GetSize;
} else if (!strncmp("-", p, strlen("-="))) {
ssize = &(iap->MaxTableSpaceSize);
p += strlen("-table=");
/* skip remaining arguments */
argc = 1;
}
break;
2017-12-10 01:22:45 +00:00
case 'p':
2019-01-21 01:11:42 +00:00
if ((*argv)[0] == '\0')
iap->PrologAddPath = *argv;
else {
argc--;
if (argc == 0) {
fprintf(stderr, " [ YAP unrecoverable error: missing paths for "
"option 'p' ]\n");
exit(EXIT_FAILURE);
}
argv++;
iap->PrologAddPath = *argv;
}
break;
/* nf: Begin preprocessor code */
2017-12-10 01:22:45 +00:00
case 'D': {
2019-01-21 01:11:42 +00:00
char *var, *value;
++p;
var = p;
if (var == NULL || *var == '\0')
break;
while (*p != '=' && *p != '\0')
++p;
if (*p == '\0')
break;
*p = '\0';
++p;
value = p;
if (*value == '\0')
break;
if (iap->def_c == YAP_MAX_YPP_DEFS)
break;
iap->def_var[iap->def_c] = var;
iap->def_value[iap->def_c] = value;
++(iap->def_c);
break;
2017-12-10 01:22:45 +00:00
}
2019-01-21 01:11:42 +00:00
/* End preprocessor code */
2017-12-10 01:22:45 +00:00
default: {
2019-01-21 01:11:42 +00:00
fprintf(stderr, "[ YAP unrecoverable error: unknown switch -%c ]\n",
*p);
print_usage();
exit(EXIT_FAILURE);
2017-12-10 01:22:45 +00:00
}
}
else {
2018-02-25 00:29:08 +00:00
iap->INPUT_STARTUP = p;
2012-06-22 10:01:32 +01:00
}
2017-12-10 01:22:45 +00:00
}
return iap->boot_file_type;
}
/**
YAP_DelayInit()
ensures initialization is done after engine creation.
It receives a pointer to function and a string describing
the module.
*/
X_API bool YAP_DelayInit(YAP_ModInit_t f, const char s[]) {
if (m_delays) {
m_delays = realloc(m_delays, (n_mdelays + 1) * sizeof(YAP_delaymodule_t));
} else {
m_delays = malloc(sizeof(YAP_delaymodule_t));
}
m_delays[n_mdelays].f = f;
m_delays[n_mdelays].s = s;
n_mdelays++;
return true;
}
bool Yap_LateInit(const char s[]) {
int i;
for (i = 0; i < n_mdelays; i++) {
if (!strcmp(m_delays[i].s, s)) {
m_delays[i].f();
return true;
}
}
return false;
}
struct ssz_t {
size_t Heap, Stack, Trail;
};
2018-06-27 23:49:42 +01:00
bool Yap_Embedded;
2017-12-10 01:22:45 +00:00
static void init_hw(YAP_init_args *yap_init, struct ssz_t *spt) {
Yap_page_size = Yap_InitPageSize(); /* init memory page size, required by
2019-01-21 01:11:42 +00:00
later functions */
2017-12-10 01:22:45 +00:00
#if defined(YAPOR_COPY) || defined(YAPOR_COW) || defined(YAPOR_SBA)
Yap_init_yapor_global_local_memory();
#endif /* YAPOR_COPY || YAPOR_COW || YAPOR_SBA */
2018-06-27 23:49:42 +01:00
if (yap_init->Embedded) {
yap_init->install = false;
GLOBAL_PrologShouldHandleInterrupts =
2019-01-21 01:11:42 +00:00
yap_init->PrologCannotHandleInterrupts = true;
2018-06-27 23:49:42 +01:00
} else {
2017-12-10 01:22:45 +00:00
GLOBAL_PrologShouldHandleInterrupts =
2019-01-21 01:11:42 +00:00
!yap_init->PrologCannotHandleInterrupts;
2018-06-27 23:49:42 +01:00
}
2018-07-17 11:43:57 +01:00
Yap_InitSysbits(0); /* init signal handling and time, required by later
2019-01-21 01:11:42 +00:00
functions */
2018-07-17 11:43:57 +01:00
GLOBAL_argv = yap_init->Argv;
GLOBAL_argc = yap_init->Argc;
2017-12-10 01:22:45 +00:00
#if __ANDROID__
// if (yap_init->assetManager)
2018-03-02 21:18:24 +00:00
// Yap_InitAssetManager();
2017-12-10 01:22:45 +00:00
#endif
if (yap_init->TrailSize == 0) {
if (spt->Trail == 0)
spt->Trail = DefTrailSpace;
} else {
spt->Trail = yap_init->TrailSize;
}
// Atts = yap_init->AttsSize;
if (yap_init->StackSize == 0) {
spt->Stack = DefStackSpace;
} else {
spt->Stack = yap_init->StackSize;
}
#ifndef USE_SYSTEM_MALLOC
if (yap_init->HeapSize == 0) {
if (spt->Heap == 0)
spt->Heap = DefHeapSpace;
} else {
spt->Heap = yap_init->HeapSize;
}
#endif
}
2018-03-14 00:41:05 +00:00
static void end_init(YAP_init_args *iap) {
2017-12-10 01:22:45 +00:00
YAP_initialized = true;
2018-07-17 11:43:57 +01:00
if (iap->HaltAfterBoot)
Yap_exit(0);
2018-01-22 13:53:17 +00:00
LOCAL_PrologMode &= ~BootMode;
2018-06-15 11:09:04 +01:00
CurrentModule = USER_MODULE;
2019-01-09 09:32:09 +00:00
LOCAL_SourceModule = USER_MODULE;
2017-12-10 01:22:45 +00:00
}
2018-03-02 21:18:24 +00:00
static void start_modules(void) {
Term cm = CurrentModule;
size_t i;
for (i = 0; i < n_mdelays; i++) {
CurrentModule = MkAtomTerm(YAP_LookupAtom(m_delays[i].s));
m_delays[i].f();
}
CurrentModule = cm;
}
2017-12-10 01:22:45 +00:00
/* this routine is supposed to be called from an external program
that wants to control Yap */
2018-03-14 00:41:05 +00:00
X_API void YAP_Init(YAP_init_args *yap_init) {
bool try_restore = yap_init->boot_file_type == YAP_QLY;
2019-01-09 09:32:09 +00:00
bool do_bootstrap = yap_init->boot_file_type == YAP_PL ||
2019-01-21 01:11:42 +00:00
yap_init->boot_file_type == YAP_SOURCE_PL;
2017-12-10 01:22:45 +00:00
struct ssz_t minfo;
2019-01-21 01:11:42 +00:00
__android_log_print(
ANDROID_LOG_INFO, "YAPDroid", "start init ");
2017-12-10 01:22:45 +00:00
if (YAP_initialized)
2018-03-02 21:18:24 +00:00
/* ignore repeated calls to YAP_Init */
2018-03-14 00:41:05 +00:00
return;
2017-12-10 01:22:45 +00:00
if (!LOCAL_TextBuffer)
LOCAL_TextBuffer = Yap_InitTextAllocator();
2018-06-27 23:49:42 +01:00
Yap_Embedded = yap_init->Embedded;
2017-12-10 01:22:45 +00:00
minfo.Trail = 0, minfo.Stack = 0, minfo.Trail = 0;
init_hw(yap_init, &minfo);
Yap_InitWorkspace(yap_init, minfo.Heap, minfo.Stack, minfo.Trail, 0,
2019-01-21 01:11:42 +00:00
yap_init->MaxTableSpaceSize, yap_init->NumberWorkers,
yap_init->SchedulerLoop, yap_init->DelayedReleaseLoad);
2017-12-10 01:22:45 +00:00
//
CACHE_REGS
2019-01-09 09:32:09 +00:00
CurrentModule = PROLOG_MODULE;
2018-06-15 13:50:55 +01:00
2019-01-21 01:11:42 +00:00
if (yap_init->QuietMode) {
2018-07-17 11:43:57 +01:00
setVerbosity(TermSilent);
}
2018-01-18 14:47:27 +00:00
if (yap_init->PrologRCFile != NULL) {
2017-12-10 01:22:45 +00:00
/*
This must be done before restore, otherwise
restore will print out messages ....
*/
setBooleanGlobalPrologFlag(HALT_AFTER_CONSULT_FLAG,
2019-01-21 01:11:42 +00:00
yap_init->HaltAfterBoot);
2017-12-10 01:22:45 +00:00
}
/* tell the system who should cope with interrupts */
Yap_ExecutionMode = yap_init->ExecutionMode;
Yap_set_locations(yap_init);
2018-02-14 00:13:13 +00:00
2018-12-14 10:29:12 +00:00
if (Yap_INPUT_STARTUP==NULL)
try_restore = false;
2019-01-09 09:32:09 +00:00
if (do_bootstrap || !try_restore ||
2018-03-14 00:41:05 +00:00
!Yap_SavedInfo(Yap_INPUT_STARTUP, &minfo.Trail, &minfo.Stack,
2019-01-21 01:11:42 +00:00
&minfo.Heap)) {
2018-01-22 13:53:17 +00:00
init_globals(yap_init);
2018-01-18 14:47:27 +00:00
2018-01-22 13:53:17 +00:00
start_modules();
2019-01-21 01:11:42 +00:00
TermEof = MkAtomTerm(Yap_LookupAtom("end_of_file"));
2018-07-17 11:43:57 +01:00
LOCAL_consult_level = -1;
2018-12-14 10:29:12 +00:00
__android_log_print(
2019-01-21 01:11:42 +00:00
ANDROID_LOG_INFO, "YAPDroid", "init %s ", Yap_BOOTSTRAP);
2019-01-09 09:32:09 +00:00
if (yap_init->install) {
2019-01-21 01:11:42 +00:00
load_file(Yap_SOURCEBOOT PASS_REGS);
setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG,
MkAtomTerm(Yap_LookupAtom(Yap_SOURCEBOOT)));
2019-01-09 09:32:09 +00:00
}
else {
2019-01-21 01:11:42 +00:00
load_file(Yap_BOOTSTRAP PASS_REGS);
setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG,
MkAtomTerm(Yap_LookupAtom(Yap_BOOTSTRAP)));
2019-01-09 09:32:09 +00:00
}
CurrentModule = LOCAL_SourceModule = TermUser;
2019-01-21 01:11:42 +00:00
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, false);
2017-12-10 01:22:45 +00:00
} else {
2018-10-11 07:47:28 +01:00
if (yap_init->QuietMode) {
2019-01-21 01:11:42 +00:00
setVerbosity(TermSilent);
}
2018-12-14 10:29:12 +00:00
__android_log_print(
2019-01-21 01:11:42 +00:00
ANDROID_LOG_INFO, "YAPDroid", "restore %s ",Yap_INPUT_STARTUP );
2018-03-14 00:41:05 +00:00
Yap_Restore(Yap_INPUT_STARTUP);
2019-01-21 01:11:42 +00:00
CurrentModule = LOCAL_SourceModule = TermUser;
2018-01-18 14:47:27 +00:00
init_globals(yap_init);
start_modules();
2018-06-18 10:51:01 +01:00
if (yap_init->install && Yap_OUTPUT_STARTUP) {
2018-07-17 11:43:57 +01:00
setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG,
2019-01-21 01:11:42 +00:00
MkAtomTerm(Yap_LookupAtom(Yap_INPUT_STARTUP)));
2018-07-17 11:43:57 +01:00
setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, true);
}
LOCAL_consult_level = -1;
2018-06-18 22:53:53 +01:00
}
2019-01-31 12:46:35 +00:00
2018-03-14 00:41:05 +00:00
YAP_RunGoalOnce(TermInitProlog);
2018-07-17 11:43:57 +01:00
if (yap_init->install && Yap_OUTPUT_STARTUP) {
Term t = MkAtomTerm(Yap_LookupAtom(Yap_OUTPUT_STARTUP));
Term g = Yap_MkApplTerm(Yap_MkFunctor(Yap_LookupAtom("qsave_program"), 1),
2019-01-21 01:11:42 +00:00
1, &t);
2018-02-14 00:13:13 +00:00
2018-07-17 11:43:57 +01:00
YAP_RunGoalOnce(g);
}
2018-06-18 22:53:53 +01:00
2018-07-17 11:43:57 +01:00
end_init(yap_init);
2017-12-10 01:22:45 +00:00
}
#if (DefTrailSpace < MinTrailSpace)
#undef DefTrailSpace
#define DefTrailSpace MinTrailSpace
#endif
#if (DefStackSpace < MinStackSpace)
#undef DefStackSpace
#define DefStackSpace MinStackSpace
#endif
#if (DefHeapSpace < MinHeapSpace)
#undef DefHeapSpace
#define DefHeapSpace MinHeapSpace
#endif
#define DEFAULT_NUMBERWORKERS 1
#define DEFAULT_SCHEDULERLOOP 10
#define DEFAULT_DELAYEDRELEASELOAD 3
2018-03-14 00:41:05 +00:00
X_API void YAP_FastInit(char *saved_state, int argc, char *argv[]) {
2017-12-10 01:22:45 +00:00
YAP_init_args init_args;
YAP_file_type_t out;
if ((out = Yap_InitDefaults(&init_args, saved_state, argc, argv)) !=
YAP_FOUND_BOOT_ERROR)
2018-03-14 00:41:05 +00:00
YAP_Init(&init_args);
2017-12-10 01:22:45 +00:00
if (out == YAP_FOUND_BOOT_ERROR) {
Yap_Error(init_args.ErrorNo, TermNil, init_args.ErrorCause);
}
}