diff --git a/C/c_interface.c b/C/c_interface.c index 85ce12a72..cafa94147 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -2106,45 +2106,42 @@ X_API void YAP_ClearExceptions(void) { X_API int YAP_InitConsult(int mode, const char *fname, char **full, int *osnop) { - CACHE_REGS - int sno; - BACKUP_MACHINE_REGS(); - const char *fl = NULL; - int lvl = push_text_stack(); - if (mode == YAP_BOOT_MODE) { - mode = YAP_CONSULT_MODE; - } - if (fname == NULL || fname[0] == '\0') { - fl = Yap_BOOTFILE; - } - if (fname) { - fl = Yap_AbsoluteFile(fname, true); - if (!fl || !fl[0]) { - pop_text_stack(lvl); - *full = NULL; - return -1; - } else { - *full = pop_output_text_stack(lvl, fl); + CACHE_REGS + int sno; + BACKUP_MACHINE_REGS(); + const char *fl = NULL; + if (mode == YAP_BOOT_MODE) { + mode = YAP_CONSULT_MODE; } - } else { - pop_text_stack(lvl); + if (fname == NULL || fname[0] == '\0') { + fl = Yap_BOOTFILE; + } + if (!fname || !(fl = Yap_AbsoluteFile(fname, true)) || !fl[0]) { + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "failed ABSOLUTEFN %s ", fl); + *full = NULL; + return -1; } + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "done init_ consult %s ",fl); - lvl = push_text_stack(); +int lvl = push_text_stack(); char *d = Malloc(strlen(fl) + 1); strcpy(d, fl); bool consulted = (mode == YAP_CONSULT_MODE); Term tat = MkAtomTerm(Yap_LookupAtom(d)); sno = Yap_OpenStream(tat, "r", MkAtomTerm(Yap_LookupAtom(fname)), LOCAL_encoding); - if (sno < 0 || !Yap_ChDir(dirname((char *)d))) { + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "OpenStream got %d ",sno); pop_text_stack(lvl); + if (sno < 0 || !Yap_ChDir(dirname((char *)d))) { *full = NULL; return -1; } LOCAL_PrologMode = UserMode; - - Yap_init_consult(consulted, pop_output_text_stack__(lvl, fl)); +*full = pop_output_text_stack__(lvl, fl); + Yap_init_consult(consulted,*full); RECOVER_MACHINE_REGS(); UNLOCK(GLOBAL_Stream[sno].streamlock); return sno; diff --git a/C/yap-args.c b/C/yap-args.c index ae4867f11..439ebfc64 100755 --- a/C/yap-args.c +++ b/C/yap-args.c @@ -153,22 +153,25 @@ const char *Yap_BINDIR, *Yap_ROOTDIR, *Yap_SHAREDIR, *Yap_LIBDIR, *Yap_DLLDIR, * recursive consulting. * * */ -static bool consult(const char *b_file USES_REGS) { +static bool load_file(const char *b_file USES_REGS) { Term t; int c_stream, osno, oactive; - Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1); + + Functor functor_query = Yap_MkFunctor(Yap_LookupAtom("?-"), 1); Functor functor_command1 = Yap_MkFunctor(Yap_LookupAtom(":-"), 1); Functor functor_compile2 = Yap_MkFunctor(Yap_LookupAtom("c_compile"), 1); /* consult in C */ int lvl = push_text_stack(); char *full; - /* the consult mode does not matter here, really */ + /* the consult mode does not matter here, really */ if ((osno = Yap_CheckAlias(AtomLoopStream)) < 0) { osno = 0; } c_stream = YAP_InitConsult(YAP_BOOT_MODE, b_file, &full, &oactive); - if (c_stream < 0) { + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "done init_ consult %s ",b_file); + if (c_stream < 0) { fprintf(stderr, "[ FATAL ERROR: could not open file %s ]\n", b_file); pop_text_stack(lvl); exit(1); @@ -177,6 +180,8 @@ static bool consult(const char *b_file USES_REGS) { pop_text_stack(lvl); return false; } + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "do reset %s ",b_file); do { CACHE_REGS @@ -197,7 +202,7 @@ static bool consult(const char *b_file USES_REGS) { FunctorOfTerm(t) == functor_command1)) { t = ArgOfTerm(1, t); if (IsApplTerm(t) && FunctorOfTerm(t) == functor_compile2) { - consult(RepAtom(AtomOfTerm(ArgOfTerm(1, t)))->StrOfAE); + load_file(RepAtom(AtomOfTerm(ArgOfTerm(1, t)))->StrOfAE); Yap_ResetException(LOCAL_ActiveError); } else { YAP_RunGoalOnce(t); @@ -221,16 +226,82 @@ static bool consult(const char *b_file USES_REGS) { return true; } +static const char * EOLIST ="EOLINE"; + + + static bool is_dir( const char *path, const void *info) { + + if (Yap_isDirectory( path )) + return true; + char s[YAP_FILENAME_MAX + 1]; + Int i = strlen(path); + strncpy(s, path, YAP_FILENAME_MAX); + while (--i) { + if (Yap_dir_separator((int)path[i])) + break; + } + if (i == 0) { + s[0] = '.'; + i = 1; + } + s[i] = '\0'; + return + strcmp(info,s) == 0 || + Yap_isDirectory( s ); + } + + static bool is_file( const char *path, const void *info) { + + 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); + + /// /// -static const char *sel(bool dir, bool ok1, const char *s1, bool ok2, - const char *s2, ...) { - if (ok1 && s1) - return s1; - if (ok2) - return s2; - return NULL; -} + 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); +__android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "try %s", s1); + + va_start(ap, s1); + while (fmt != EOLIST) { + __android_log_print( ANDROID_LOG_INFO, "YAPDroid", "loop %s", fmt); + + if (fmt == NULL || fmt[0]=='\0') { + fmt = va_arg(ap, const char *); + continue; + } + strncpy(buf, fmt, FILENAME_MAX); // Yap_AbsoluteFile(fmt,true), FILENAME_MAX); + __android_log_print( ANDROID_LOG_INFO, "YAPDroid", "triyimh %s", buf); + if (test(buf,info)) { + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "got %s", buf); + buf = realloc(buf, strlen(buf) + 1); + va_end(ap); + return buf; + } + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "tried %s, failed", buf); + fmt = va_arg(ap, const char *); + } + + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "failed search "); + va_end(ap); + free(buf); +return NULL; + } + static const char *join(const char *s0, const char *s1) { CACHE_REGS @@ -256,103 +327,130 @@ static void Yap_set_locations(YAP_init_args *iap) { // --_not useful in Android, WIN32; /// -- DESTDIR/ in Anaconda /// -- /usr/locall in most Unix style systems - Yap_ROOTDIR = sel(true, iap->ROOTDIR != NULL, iap->ROOTDIR, true, +#if 0 + Yap_ROOTDIR = sel( is_dir, NULL, + iap->ROOTDIR, + getenv("YAPROOTDIR"), #if __ANDROID__ - NULL, + "/", #else - join(getenv("DESTDIR"), YAP_ROOTDIR), + 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 - false); - /// BINDIR: where the OS stores header files, namely libYap... - Yap_BINDIR = sel(true, iap->BINDIR != NULL, iap->BINDIR, true, -#if __ANDROID__ - NULL, -#else + EOLIST + ); + + /// BINDIR: where the OS stores header files, namely libYap... + Yap_BINDIR = sel( is_dir, Yap_ROOTDIR, iap->BINDIR, + getenv("YAPBINDIR"), +#if !defined(__ANDROID__) join(getenv("DESTDIR"), YAP_BINDIR), #endif - false); + join(Yap_ROOTDIR, "/bin"), + EOLIST); + /// LIBDIR: where the OS stores dynamic libraries, namely libYap... - Yap_LIBDIR = sel(true, iap->LIBDIR != NULL, iap->LIBDIR, true, -#if __ANDROID__ - NULL, -#else + Yap_LIBDIR = sel( is_dir, Yap_ROOTDIR, iap->LIBDIR, +#if !defined(__ANDROID__) join(getenv("DESTDIR"), YAP_LIBDIR), #endif - false); + join(Yap_ROOTDIR, "/lib"), + EOLIST); + /// DLLDIR: where libraries can find expicitely loaded DLLs - Yap_DLLDIR = sel(true, iap->DLLDIR != NULL, iap->DLLDIR, true, -#if __ANDROID__ - NULL, -#else - join(getenv("DESTDIR"), YAP_DLLDIR), + Yap_DLLDIR = sel(is_dir, Yap_LIBDIR, iap->DLLDIR, + getenv("YAPLIBDIR"), +#if !defined(__ANDROID__) + join(getenv("DESTDIR"), YAP_DLLDIR), + join(Yap_LIBDIR, "/yap"), #endif - false); + EOLIST); + /// INCLUDEDIR: where the OS stores header files, namely libYap... - Yap_INCLUDEDIR = sel(true, iap->INCLUDEDIR != NULL, iap->INCLUDEDIR, true, -#if __ANDROID__ - NULL, -#else - join(getenv("DESTDIR"), YAP_INCLUDEDIR), + Yap_INCLUDEDIR = sel(is_dir, Yap_ROOTDIR, iap->INCLUDEDIR, +#if !defined(__ANDROID__) + join(getenv("DESTDIR"), YAP_INCLUDEDIR), #endif - false); + join(Yap_ROOTDIR, "/include"), + EOLIST); + /// SHAREDIR: where OS & ARCH independent files live - Yap_SHAREDIR = sel(true, iap->SHAREDIR != NULL, iap->SHAREDIR, true, -#if __ANDROID__ - "/assets", -#else - join(getenv("DESTDIR"), YAP_SHAREDIR), + Yap_SHAREDIR = sel( is_dir, Yap_ROOTDIR, iap->SHAREDIR, + getenv("YAPSHAREDIR"), +#if !defined(__ANDROID__) + join(getenv("DESTDIR"), YAP_SHAREDIR), + join(Yap_ROOTDIR, "/share"), + "/assets", #endif - false); + join(Yap_ROOTDIR, "/files"), + EOLIST); /// PLDIR: where we can find Prolog files - Yap_PLDIR = sel(true, iap->PLDIR != NULL, iap->PLDIR, true, + Yap_PLDIR = sel( is_dir, Yap_SHAREDIR, iap->PLDIR, #if __ANDROID__ + YAP_PLDIR, "/assets/Yap", #else join(getenv("DESTDIR"), YAP_PLDIR), + join(Yap_SHAREDIR, "/Yap"), #endif - false); + EOLIST); /// ``COMMONSDIR: Prolog Commons - Yap_COMMONSDIR = sel(true, iap->COMMONSDIR != NULL, iap->COMMONSDIR, true, + Yap_COMMONSDIR = sel(is_dir, Yap_SHAREDIR, iap->COMMONSDIR, #if __ANDROID__ "/assets/PrologCommons", #else join(getenv("DESTDIR"), YAP_SHAREDIR "/PrologCommons"), + join(Yap_SHAREDIR, "PrologCommons"), #endif - false); + EOLIST); /// BOOTPLDIR: where we can find Prolog bootstrap files - Yap_BOOTSTRAP = sel(true, iap->BOOTSTRAP != NULL, iap->BOOTSTRAP, true, -#if __ANDROID__ - "/assets/Yap/pl/boot.yap", -#else - join(getenv("DESTDIR"), YAP_BOOTSTRAP), -#endif - false); + Yap_BOOTSTRAP = sel( is_file, NULL, iap->BOOTSTRAP, + YAP_BOOTSTRAP, + EOLIST); /// BOOTFILE: where we can find the core Prolog boot file - Yap_BOOTFILE = sel(false, iap->BOOTFILE != NULL, iap->BOOTFILE, true, + +const char * Yap_PLBOOTDIR = sel( is_dir, Yap_PLDIR, #if __ANDROID__ - "/assets/Yap/pl/boot.yap", + "/assets/Yap/pl", #else - join(getenv("DESTDIR"), YAP_BOOTFILE), + join(Yap_PLDIR, "/pl"), +#endif + EOLIST); + + Yap_BOOTFILE = sel( is_wfile, Yap_PLBOOTDIR, iap->BOOTFILE, +#if __ANDROID__ + "/assets/Yap/pl/boot.yap", +#else + join(Yap_PLBOOTDIR, "/boot.yap"), +#endif + EOLIST); #endif - false); /// STARTUP: where we can find the core Prolog bootstrap file Yap_OUTPUT_STARTUP = - sel(false, iap->OUTPUT_STARTUP != NULL, iap->OUTPUT_STARTUP, true, -#if __ANDROID__ - NULL, + sel( is_wfile, Yap_AbsoluteFile(".",false), iap->OUTPUT_STARTUP, +#if defined(__ANDROID__) +EOLIST, #else - join(getenv("DESTDIR"), YAP_OUTPUT_STARTUP), + YAP_OUTPUT_STARTUP, #endif - false); + "startup.yss", +EOLIST); + Yap_INPUT_STARTUP = - sel(false, iap->INPUT_STARTUP != NULL, iap->INPUT_STARTUP, true, + sel( is_wfile, Yap_DLLDIR, iap->INPUT_STARTUP, #if __ANDROID__ - NULL, +EOLIST, #else - join(getenv("DESTDIR"), YAP_INPUT_STARTUP), + join(getenv("DESTDIR"), YAP_INPUT_STARTUP), #endif - false); - if (Yap_ROOTDIR) + "startup.yss", + join(Yap_DLLDIR, "/startup.yss"), + EOLIST); + + if (Yap_ROOTDIR) setAtomicGlobalPrologFlag(HOME_FLAG, MkAtomTerm(Yap_LookupAtom(Yap_ROOTDIR))); if (Yap_PLDIR) @@ -461,17 +559,18 @@ X_API YAP_file_type_t Yap_InitDefaults(void *x, char *saved_state, int argc, LOCAL_TextBuffer = Yap_InitTextAllocator(); YAP_init_args *iap = x; memset(iap, 0, sizeof(YAP_init_args)); + iap->Argc = argc; + iap->Argv = argv; #if __ANDROID__ iap->boot_file_type = YAP_BOOT_PL; iap->INPUT_STARTUP = NULL; iap->assetManager = NULL; + return YAP_BOOT_PL; #else iap->boot_file_type = YAP_QLY; iap->INPUT_STARTUP = saved_state; -#endif - iap->Argc = argc; - iap->Argv = argv; return YAP_QLY; +#endif } /** @@ -1006,7 +1105,8 @@ X_API void YAP_Init(YAP_init_args *yap_init) { bool try_restore = yap_init->boot_file_type == YAP_QLY; bool do_bootstrap = yap_init->boot_file_type == YAP_BOOT_PL; struct ssz_t minfo; - + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "start init "); if (YAP_initialized) /* ignore repeated calls to YAP_Init */ return; @@ -1039,16 +1139,20 @@ X_API void YAP_Init(YAP_init_args *yap_init) { Yap_ExecutionMode = yap_init->ExecutionMode; Yap_set_locations(yap_init); - if (do_bootstrap || !try_restore || + if (Yap_INPUT_STARTUP==NULL) + try_restore = false; + if (do_bootstrap || !try_restore || !Yap_SavedInfo(Yap_INPUT_STARTUP, &minfo.Trail, &minfo.Stack, &minfo.Heap)) { init_globals(yap_init); start_modules(); CurrentModule = PROLOG_MODULE; - TermEof = MkAtomTerm(Yap_LookupAtom("end_of_file")); + TermEof = MkAtomTerm(Yap_LookupAtom("end_of_file")); LOCAL_consult_level = -1; - consult(Yap_BOOTSTRAP PASS_REGS); + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "init %s ", Yap_BOOTSTRAP); + load_file(Yap_BOOTSTRAP PASS_REGS); setAtomicGlobalPrologFlag(RESOURCE_DATABASE_FLAG, MkAtomTerm(Yap_LookupAtom(Yap_BOOTFILE))); setBooleanGlobalPrologFlag(SAVED_PROGRAM_FLAG, false); @@ -1056,6 +1160,8 @@ X_API void YAP_Init(YAP_init_args *yap_init) { if (yap_init->QuietMode) { setVerbosity(TermSilent); } + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "restore %s ",Yap_INPUT_STARTUP ); Yap_Restore(Yap_INPUT_STARTUP); init_globals(yap_init); diff --git a/CMakeLists.txt b/CMakeLists.txt index fa886fc2e..9b31c68d8 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -320,6 +320,7 @@ set(YAP_ROOTDIR ${prefix}) # libdir defined above set(YAP_DATADIR ${CMAKE_INSTALL_FULL_DATADIR}) set(YAP_INCLUDEDIR ${CMAKE_INSTALL_FULL_INCLUDEDIR}/Yap) +set(YAP_BINDIR ${CMAKE_INSTALL_FULL_BINDIR}) set(YAP_LIBDIR ${CMAKE_INSTALL_FULL_LIBDIR}) set(YAP_DLLDIR ${CMAKE_INSTALL_FULL_LIBDIR}/Yap) set(YAP_PLDIR ${CMAKE_INSTALL_FULL_DATADIR}/Yap) diff --git a/CXX/yapq.hh b/CXX/yapq.hh index 7fcf764c0..c7a96a9f7 100644 --- a/CXX/yapq.hh +++ b/CXX/yapq.hh @@ -270,6 +270,8 @@ public: inline void setArgv(char **fl) { Argv = fl; }; inline char **getArgv() { return Argv; }; + + inline void setROOTDIR(char *fl) { ROOTDIR = fl; }; }; /** @@ -293,7 +295,14 @@ public: YAPEngine(YAPEngineArgs *cargs) { engine_args = cargs; // doInit(cargs->boot_file_type); + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "start engine "); +#ifdef __ANDROID__ + doInit(YAP_BOOT_PL, cargs); + +#else doInit(YAP_QLY, cargs); +#endif }; /// construct a new engine, including aaccess to callbacks /// construct a new engine using argc/argv list of arguments YAPEngine(int argc, char *argv[], diff --git a/config.h.cmake b/config.h.cmake index f24df26d4..16547a3bc 100644 --- a/config.h.cmake +++ b/config.h.cmake @@ -2030,7 +2030,7 @@ significant byte first (like Motorola and SPARC, unlike Intel). */ /* name of Commons library */ #ifndef YAP_COMMONSDIR -#define YAP COMMONSDIR "${YAP_DATADIR}/PrologCommmons" +#define YAP_COMMONSDIR "${YAP_DATADIR}/PrologCommmons" #endif diff --git a/os/assets.c b/os/assets.c index ed1dac028..b19fb5b15 100644 --- a/os/assets.c +++ b/os/assets.c @@ -77,18 +77,20 @@ open_asset(VFS_t *me, const char *fname, const char *io_mode, int sno) { // AAssetDir *dp = AAssetManager_openDir( Yap_assetManager(), dirname(dir) ); // strcpy(dir, fname); // char *d = basename(dir); - am = AAssetManager_open(Yap_assetManager(), fname, mode); + am = AAssetManager_open(Yap_assetManager(), fname, io_mode); //if (am==NULL) // __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "failed open %s <%s>", fname, strerror(errno) ); - + __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "open %s <%s>", fname, io_mode); // while (dp) { // char *f = AAssetDir_getNextFileName(dp); -// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "open %s <%s>", f, d); +// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "open %s <%s>", fname, mode); // if (f && strcasecmp(d,f) == 0) { // // } // } if (!am) { + __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "failed %s <%s>", fname, io_mode); + return NULL; } // try not to use it as an asset @@ -111,7 +113,7 @@ open_asset(VFS_t *me, const char *fname, const char *io_mode, int sno) { st->vfs = NULL; st->vfs_handle = NULL; st->status = Seekable_Stream_f|Input_Stream_f; - return st; + return st->file; } else { // should be done, but if not GLOBAL_Stream[sno].vfs_handle = am; diff --git a/os/iopreds.c b/os/iopreds.c index f163624ff..dae2ecfd4 100644 --- a/os/iopreds.c +++ b/os/iopreds.c @@ -1135,7 +1135,7 @@ bool Yap_initStream(int sno, FILE *fd, Atom name, const char *io_mode, ANDROID_LOG_INFO, "YAPDroid", "init %s %s:%s stream <%d>", io_mode, CurrentModule == 0 ? "prolog" : RepAtom(AtomOfTerm(CurrentModule))->StrOfAE, - name, sno); + RepAtom(name)->StrOfAE, sno); if (io_mode == NULL) Yap_Error(PERMISSION_ERROR_NEW_ALIAS_FOR_STREAM, MkIntegerTerm(sno), "File opened with NULL Permissions"); @@ -1242,74 +1242,80 @@ static bool fill_stream(int sno, StreamDesc *st, Term tin, const char *io_mode, st->file = NULL; if (fname) { - if ((vfsp = vfs_owner(fname)) != NULL && - vfsp->open(vfsp, fname, io_mode, sno)) { - // read, write, append - user_name = st->user_name; - st->vfs = vfsp; - UNLOCK(st->streamlock); - } else { - st->file = fopen(fname, io_mode); - if (st->file == NULL) { - UNLOCK(st->streamlock); - if (errno == ENOENT && !strchr(io_mode, 'r')) { - PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s: %s", fname, - strerror(errno)); - } else { - PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, tin, "%s: %s", fname, - strerror(errno)); - } - } - st->vfs = NULL; - } - if (!st->file && !st->vfs) { - PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s", fname); - /* extract BACK info passed through the stream descriptor */ - return false; - } - } else if (IsApplTerm(tin)) { - Functor f = FunctorOfTerm(tin); - if (f == FunctorAtom || f == FunctorString || f == FunctorCodes1 || - f == FunctorCodes || f == FunctorChars1 || f == FunctorChars) { - if (strchr(io_mode, 'r')) { - return Yap_OpenBufWriteStream(PASS_REGS1); - } else { - int j = push_text_stack(); - const char *buf; + if ((vfsp = vfs_owner(fname)) != NULL) { + if (vfsp->open(vfsp, fname, io_mode, sno)) { + // read, write, append + user_name = st->user_name; + st->vfs = vfsp; + UNLOCK(st->streamlock); + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "got %d ", sno); + } else { + UNLOCK(st->streamlock); + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "failed %s ",fname); +return false; + } + } else { + st->file = fopen(fname, io_mode); + if (st->file == NULL) { + __android_log_print( + ANDROID_LOG_INFO, "YAPDroid", "failed %s ",fname); + UNLOCK(st->streamlock); + if (errno == ENOENT && !strchr(io_mode, 'r')) { + PlIOError(EXISTENCE_ERROR_SOURCE_SINK, tin, "%s: %s", fname, + strerror(errno)); + } else { + PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, tin, "%s: %s", fname, + strerror(errno)); + } + return false; + } - buf = Yap_TextTermToText(tin PASS_REGS); - if (!buf) { - pop_text_stack(j); - return false; - } - buf = pop_output_text_stack(j, buf); - Atom nat = Yap_LookupAtom(Yap_StrPrefix(buf, 32)); - sno = Yap_open_buf_read_stream(buf, strlen(buf) + 1, &LOCAL_encoding, - MEM_BUF_MALLOC, nat, - MkAtomTerm(NameOfFunctor(f))); - pop_text_stack(j); - return Yap_OpenBufWriteStream(PASS_REGS1); - } - } else if (!strcmp(RepAtom(NameOfFunctor(f))->StrOfAE, "popen")) { - const char *buf; - int i = push_text_stack(); - buf = Yap_TextTermToText(ArgOfTerm(1, tin) PASS_REGS); - if (buf == NULL) { - return false; } + } else if (IsApplTerm(tin)) { + Functor f = FunctorOfTerm(tin); + if (f == FunctorAtom || f == FunctorString || f == FunctorCodes1 || + f == FunctorCodes || f == FunctorChars1 || f == FunctorChars) { + if (strchr(io_mode, 'r')) { + return Yap_OpenBufWriteStream(PASS_REGS1); + } else { + int j = push_text_stack(); + const char *buf; + + buf = Yap_TextTermToText(tin PASS_REGS); + if (!buf) { + pop_text_stack(j); + return false; + } + buf = pop_output_text_stack(j, buf); + Atom nat = Yap_LookupAtom(Yap_StrPrefix(buf, 32)); + sno = Yap_open_buf_read_stream(buf, strlen(buf) + 1, &LOCAL_encoding, + MEM_BUF_MALLOC, nat, + MkAtomTerm(NameOfFunctor(f))); + pop_text_stack(j); + return Yap_OpenBufWriteStream(PASS_REGS1); + } + } else if (!strcmp(RepAtom(NameOfFunctor(f))->StrOfAE, "popen")) { + const char *buf; + int i = push_text_stack(); + buf = Yap_TextTermToText(ArgOfTerm(1, tin) PASS_REGS); + if (buf == NULL) { + return false; + } #if _WIN32 - st->file = _popen(buf, io_mode); + st->file = _popen(buf, io_mode); #else - st->file = popen(buf, io_mode); + st->file = popen(buf, io_mode); #endif - fname = "popen"; - user_name = tin; - st->status |= Popen_Stream_f; - pop_text_stack(i); - } else { - Yap_ThrowError(DOMAIN_ERROR_SOURCE_SINK, tin, "open"); - } - } + fname = "popen"; + user_name = tin; + st->status |= Popen_Stream_f; + pop_text_stack(i); + } else { + Yap_ThrowError(DOMAIN_ERROR_SOURCE_SINK, tin, "open"); + } + } if (!strchr(io_mode, 'b') && binary_file(fname)) { st->status |= Binary_Stream_f; } diff --git a/os/sysbits.c b/os/sysbits.c index a211b7ed6..1f0794b2b 100644 --- a/os/sysbits.c +++ b/os/sysbits.c @@ -355,7 +355,7 @@ bool Yap_ChDir(const char *path) { int lvl = push_text_stack(); const char *qpath = Yap_AbsoluteFile(path, true); - //__android_log_print(ANDROID_LOG_INFO, "YAPDroid", "chdir %s", path); + __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "chdir %s", path); VFS_t *v; if ((v = vfs_owner(qpath))) { rc = v->chdir(v, (qpath));