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/os/streams.c

1600 lines
49 KiB
C
Raw Normal View History

2015-06-18 01:39:03 +01:00
/*************************************************************************
2018-06-14 11:27:43 +01: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: iopreds.c * Last rev: 5/2/88
** mods: * comments: Input/Output C implemented predicates *
* *
*************************************************************************/
2015-06-18 01:39:03 +01:00
#ifdef SCCS
static char SccsId[] = "%W% %G%";
#endif
2017-04-07 23:10:59 +01:00
/**
*
2018-06-14 11:27:43 +01:00
2015-06-18 01:39:03 +01:00
* This file includes the definition of a miscellania of standard predicates
2015-09-21 23:05:36 +01:00
* for yap refering to: Files and GLOBAL_Streams, Simple Input/Output,
2015-06-18 01:39:03 +01:00
*
*/
#include "Yap.h"
#if HAVE_FCNTL_H
/* for O_BINARY and O_TEXT in WIN32 */
#include <fcntl.h>
#endif
2018-06-14 11:27:43 +01:00
#include "YapEval.h"
2015-06-18 01:39:03 +01:00
#include "YapHeap.h"
#include "YapText.h"
2016-07-31 15:55:50 +01:00
#include "Yatom.h"
#include "yapio.h"
2015-06-18 01:39:03 +01:00
#include <stdlib.h>
#if HAVE_STDARG_H
#include <stdarg.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if HAVE_CTYPE_H
#include <ctype.h>
#endif
#if HAVE_WCTYPE_H
#include <wctype.h>
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
2015-09-21 23:05:36 +01:00
#if HAVE_SYS_SELECT_H && !_MSC_VER && !defined(__MINGW32__)
2015-06-18 01:39:03 +01:00
#include <sys/select.h>
#endif
#if HAVE_STRING_H
#include <string.h>
#endif
#if HAVE_SIGNAL_H
#include <signal.h>
#endif
2017-03-02 22:01:32 +00:00
#if HAVE_TERMIOS_H
#include <termios.h>
#endif
2015-06-18 01:39:03 +01:00
#ifdef _WIN32
#if HAVE_IO_H
/* Windows */
#include <io.h>
#endif
#endif
#if !HAVE_STRNCAT
2015-09-21 23:05:36 +01:00
#define strncat(X, Y, Z) strcat(X, Y)
2015-06-18 01:39:03 +01:00
#endif
#if !HAVE_STRNCPY
2015-09-21 23:05:36 +01:00
#define strncpy(X, Y, Z) strcpy(X, Y)
2015-06-18 01:39:03 +01:00
#endif
2015-09-21 23:05:36 +01:00
#if _MSC_VER || defined(__MINGW32__)
2015-06-18 01:39:03 +01:00
#if HAVE_SOCKET
#include <winsock2.h>
#endif
#include <windows.h>
#ifndef S_ISDIR
2015-09-21 23:05:36 +01:00
#define S_ISDIR(x) (((x)&_S_IFDIR) == _S_IFDIR)
2015-06-18 01:39:03 +01:00
#endif
#endif
#include "iopreds.h"
2015-09-21 23:05:36 +01:00
#if _MSC_VER || defined(__MINGW32__)
2015-06-18 01:39:03 +01:00
#define SYSTEM_STAT _stat
//#include <winbase.h>
2015-06-18 01:39:03 +01:00
#else
#define SYSTEM_STAT stat
#endif
static void CloseStream(int sno);
2015-09-21 23:05:36 +01:00
FILE *Yap_GetInputStream(Term t, const char *msg) {
int sno = Yap_CheckStream(t, Input_Stream_f, msg);
FILE *rc;
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
if (!(GLOBAL_Stream[sno].status &
(Tty_Stream_f | Socket_Stream_f | Pipe_Stream_f)))
2015-06-19 10:10:02 +01:00
rc = GLOBAL_Stream[sno].file;
2015-09-21 23:05:36 +01:00
else
rc = NULL;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
FILE *Yap_GetOutputStream(Term t, const char *msg) {
int sno = Yap_CheckStream(t, Output_Stream_f, msg);
FILE *rc;
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
if (!(GLOBAL_Stream[sno].status & (Tty_Stream_f | Socket_Stream_f)))
rc = GLOBAL_Stream[sno].file;
else
rc = NULL;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
int GetFreeStreamD(void) {
CACHE_REGS
2015-09-29 23:44:11 +01:00
LOCK(GLOBAL_StreamDescLock);
2015-09-21 23:05:36 +01:00
int sno;
for (sno = 0; sno < MaxStreams; ++sno) {
2015-06-18 01:39:03 +01:00
if (GLOBAL_Stream[sno].status & Free_Stream_f) {
break;
}
}
if (sno == MaxStreams) {
2015-09-29 23:44:11 +01:00
UNLOCK(GLOBAL_StreamDescLock);
2015-06-18 01:39:03 +01:00
return -1;
}
2015-09-29 23:44:11 +01:00
LOCK(GLOBAL_Stream[sno].streamlock);
2018-06-14 11:27:43 +01:00
GLOBAL_Stream[sno].status &= ~Free_Stream_f;
2015-09-29 23:44:11 +01:00
UNLOCK(GLOBAL_StreamDescLock);
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
2015-06-18 01:39:03 +01:00
return sno;
}
2015-09-21 23:05:36 +01:00
int Yap_GetFreeStreamD(void) { return GetFreeStreamD(); }
2015-06-18 01:39:03 +01:00
2017-03-02 22:01:32 +00:00
/**
*
*/
2018-06-14 11:27:43 +01:00
bool Yap_clearInput(int sno) {
if (!(GLOBAL_Stream[sno].status & Tty_Stream_f) || sno < 3)
return true;
if (GLOBAL_Stream[sno].vfs) {
GLOBAL_Stream[sno].vfs->flush(sno);
return true;
}
2017-03-03 11:21:29 +00:00
#if USE_READLINE
2018-06-14 11:27:43 +01:00
if (GLOBAL_Stream[sno].status & Readline_Stream_f)
return Yap_readline_clear_pending_input(GLOBAL_Stream + sno);
2017-03-02 22:01:32 +00:00
#endif
#if HAVE_FPURGE
2018-06-14 11:27:43 +01:00
fflush(NULL);
return fpurge(GLOBAL_Stream[sno].file) == 0;
#elif HAVE_TCFLUSH
2017-03-03 11:21:29 +00:00
return tcflush(fileno(GLOBAL_Stream[sno].file), TCIOFLUSH) == 0;
2017-03-02 22:01:32 +00:00
#elif MSC_VER
return fflush(GLOBAL_Stream[sno].file) == 0;
#endif
return false;
2018-06-14 11:27:43 +01:00
}
2018-06-14 11:27:43 +01:00
bool Yap_flush(int sno) {
if (!(GLOBAL_Stream[sno].status & Tty_Stream_f))
return true;
2018-06-14 11:27:43 +01:00
if (GLOBAL_Stream[sno].vfs) {
GLOBAL_Stream[sno].vfs->flush(sno);
return true;
}
return fflush(GLOBAL_Stream[sno].file) == 0;
}
2018-06-14 11:27:43 +01:00
static Int clear_input(USES_REGS1) {
int sno =
Yap_CheckStream(ARG1, Input_Stream_f | Socket_Stream_f, "clear_input/1");
2017-03-02 22:01:32 +00:00
if (sno != -1)
UNLOCK(GLOBAL_Stream[sno].streamlock);
return Yap_clearInput(sno);
2017-03-02 22:01:32 +00:00
}
2016-03-05 12:17:23 +00:00
static Term lineCount(int sno) {
Term tout;
/* one has to be somewhat more careful because of terminals */
if (GLOBAL_Stream[sno].status & Tty_Stream_f) {
Int no = 1;
int i;
Atom my_stream;
#if HAVE_SOCKET
if (GLOBAL_Stream[sno].status & Socket_Stream_f)
my_stream = AtomSocket;
else
#endif
if (GLOBAL_Stream[sno].status & Pipe_Stream_f)
my_stream = AtomPipe;
else if (GLOBAL_Stream[sno].status & InMemory_Stream_f)
my_stream = AtomCharsio;
else
my_stream = GLOBAL_Stream[sno].name;
for (i = 0; i < MaxStreams; i++) {
2016-03-05 12:17:23 +00:00
if ((GLOBAL_Stream[i].status &
(Socket_Stream_f | Pipe_Stream_f | InMemory_Stream_f)) &&
!(GLOBAL_Stream[i].status & (Free_Stream_f)) &&
GLOBAL_Stream[i].name == my_stream)
no += GLOBAL_Stream[i].linecount - 1;
}
tout = MkIntTerm(no);
} else
tout = MkIntTerm(GLOBAL_Stream[sno].linecount);
UNLOCK(GLOBAL_Stream[sno].streamlock);
return tout;
}
static Int stream_flags(USES_REGS1) { /* '$stream_flags'(+N,-Flags) */
2015-06-18 01:39:03 +01:00
Term trm;
2015-09-21 23:05:36 +01:00
trm = Deref(ARG1);
if (IsVarTerm(trm) || !IsIntTerm(trm))
2015-06-18 01:39:03 +01:00
return (FALSE);
2015-09-21 23:05:36 +01:00
return (Yap_unify_constant(ARG2,
MkIntTerm(GLOBAL_Stream[IntOfTerm(trm)].status)));
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int p_check_stream(USES_REGS1) { /* '$check_stream'(Stream,Mode) */
Term mode = Deref(ARG2);
int sno = Yap_CheckStream(
ARG1, AtomOfTerm(mode) == AtomRead ? Input_Stream_f : Output_Stream_f,
"check_stream/2");
2015-06-18 01:39:03 +01:00
if (sno != -1)
UNLOCK(GLOBAL_Stream[sno].streamlock);
return sno != -1;
}
2015-09-21 23:05:36 +01:00
static Int p_check_if_stream(USES_REGS1) { /* '$check_stream'(Stream) */
2018-06-14 11:27:43 +01:00
int sno = Yap_CheckStream(ARG1,
Input_Stream_f | Output_Stream_f | Append_Stream_f |
Socket_Stream_f,
2015-09-21 23:05:36 +01:00
"check_stream/1");
2015-06-18 01:39:03 +01:00
if (sno != -1)
UNLOCK(GLOBAL_Stream[sno].streamlock);
return sno != -1;
}
2016-03-05 12:17:23 +00:00
static Int
is_input(int sno USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
bool rc = GLOBAL_Stream[sno].status & Input_Stream_f;
return rc;
}
2016-03-05 12:17:23 +00:00
static Int
is_output(int sno USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-09-21 23:05:36 +01:00
bool rc = GLOBAL_Stream[sno].status & (Output_Stream_f | Append_Stream_f);
2015-06-18 01:39:03 +01:00
return rc;
}
static Int
2015-09-21 23:05:36 +01:00
has_bom(int sno, Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2016-03-06 01:09:02 +00:00
bool rc = GLOBAL_Stream[sno].status & HAS_BOM_f;
if (!IsVarTerm(t2) && !booleanFlag(t2)) {
2016-07-31 15:55:50 +01:00
// Yap_Error( DOMAIN_ERROR_BOOLEAN, t2, " stream_property/2");
2016-03-06 01:09:02 +00:00
return false;
2015-06-18 01:39:03 +01:00
}
if (rc) {
2015-09-21 23:05:36 +01:00
return Yap_unify_constant(t2, TermTrue);
2015-06-18 01:39:03 +01:00
} else {
return Yap_unify_constant(t2, TermFalse);
}
}
2015-06-19 10:10:02 +01:00
2015-06-18 01:39:03 +01:00
static Int
2015-09-21 23:05:36 +01:00
has_reposition(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
bool rc = GLOBAL_Stream[sno].status & Seekable_Stream_f;
if (!IsVarTerm(t2) && !booleanFlag(t2)) {
2016-03-06 01:09:02 +00:00
// Yap_Error( DOMAIN_ERROR_BOOLEAN, t2, " stream_property/2");
return false;
2015-06-18 01:39:03 +01:00
}
if (rc) {
2015-09-21 23:05:36 +01:00
return Yap_unify_constant(t2, TermTrue);
2015-06-18 01:39:03 +01:00
} else {
return Yap_unify_constant(t2, TermFalse);
}
}
2018-06-14 11:27:43 +01:00
bool Yap_SetCurInpPos(
int sno, Int pos USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2018-01-18 14:47:27 +00:00
if (GLOBAL_Stream[sno].vfs) {
2018-06-14 11:27:43 +01:00
if (GLOBAL_Stream[sno].vfs->seek &&
GLOBAL_Stream[sno].vfs->seek(sno, 0L, SEEK_END) == -1) {
2018-01-18 14:47:27 +00:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(SYSTEM_ERROR_INTERNAL, pos,
"fseek failed for set_stream_position/2: %s", strerror(errno));
return (FALSE);
}
2018-06-14 11:27:43 +01:00
} else if (fseek(GLOBAL_Stream[sno].file, pos, SEEK_SET) == -1) {
2018-01-18 14:47:27 +00:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(SYSTEM_ERROR_INTERNAL, MkIntegerTerm(0),
"fseek failed for set_stream_position/2: %s", strerror(errno));
return (FALSE);
}
return true;
}
2016-07-31 15:55:50 +01:00
char *Yap_guessFileName(FILE *file, int sno, char *nameb, size_t max) {
2017-09-19 13:19:13 +01:00
size_t maxs = Yap_Max(1024, max);
2016-07-31 15:55:50 +01:00
if (!nameb) {
nameb = malloc(maxs + 1);
2016-07-31 15:55:50 +01:00
}
if (!file) {
strncpy(nameb, "memory buffer", maxs);
2016-07-31 15:55:50 +01:00
return nameb;
}
int f = fileno(file);
if (f < 0) {
strcpy(nameb, "???");
return nameb;
}
2015-06-18 01:39:03 +01:00
#if __linux__
2018-06-14 11:27:43 +01:00
char *path = malloc(1024);
if (snprintf(path, 1023, "/proc/self/fd/%d", f) &&
readlink(path, nameb, maxs)) {
free(path);
return nameb;
2017-09-19 13:19:13 +01:00
}
2015-06-18 01:39:03 +01:00
#elif __APPLE__
2015-09-21 23:05:36 +01:00
if (fcntl(f, F_GETPATH, nameb) != -1) {
return nameb;
}
#else
2018-06-14 11:27:43 +01:00
TCHAR path = malloc(MAX_PATH + 1);
2017-09-19 13:19:13 +01:00
if (!GetFullPathName(path, MAX_PATH, path, NULL)) {
2018-06-14 11:27:43 +01:00
free(path);
2016-07-31 15:55:50 +01:00
return NULL;
2017-09-19 13:19:13 +01:00
} else {
2015-09-21 23:05:36 +01:00
int i;
unsigned char *ptr = (unsigned char *)nameb;
for (i = 0; i < strlen(path); i++)
2016-07-31 15:55:50 +01:00
ptr += put_utf8(ptr, path[i]);
2015-09-21 23:05:36 +01:00
*ptr = '\0';
2017-09-19 13:19:13 +01:00
free(path);
2015-09-21 23:05:36 +01:00
return nameb;
}
2018-06-14 11:27:43 +01:00
free(path);
2015-06-18 01:39:03 +01:00
#endif
2016-01-04 15:12:44 +00:00
if (!StreamName(sno)) {
return NULL;
}
2015-06-18 01:39:03 +01:00
return RepAtom(AtomOfTerm(StreamName(sno)))->StrOfAE;
}
2015-09-21 23:05:36 +01:00
static Int representation_error(int sno, Term t2 USES_REGS) {
stream_flags_t flags =
GLOBAL_Stream[sno].status & (RepError_Xml_f | RepError_Prolog_f);
2015-06-18 01:39:03 +01:00
/* '$representation_error'(+Stream,-ErrorMessage) */
if (!IsVarTerm(t2) && isatom(t2)) {
2015-09-21 23:05:36 +01:00
return false;
2015-06-18 01:39:03 +01:00
}
if (flags & RepError_Xml_f) {
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermXml);
2015-06-18 01:39:03 +01:00
}
if (flags & RepError_Prolog_f) {
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermProlog);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermError);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int file_name(int sno, Term t2 USES_REGS) {
2016-01-04 15:12:44 +00:00
return Yap_unify_constant(t2, MkAtomTerm(GLOBAL_Stream[sno].name));
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int file_no(int sno, Term t2 USES_REGS) {
2015-06-18 01:39:03 +01:00
int f = Yap_GetStreamFd(sno);
2015-09-21 23:05:36 +01:00
Term rc = MkIntTerm(f);
2015-06-18 01:39:03 +01:00
if (!IsVarTerm(t2) && !IsIntTerm(t2)) {
return false;
}
2015-09-21 23:05:36 +01:00
return Yap_unify_constant(t2, rc);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static bool SetCloseOnAbort(int sno, bool close) {
2015-06-18 01:39:03 +01:00
if (close) {
GLOBAL_Stream[sno].status |= DoNotCloseOnAbort_Stream_f;
} else {
GLOBAL_Stream[sno].status &= ~DoNotCloseOnAbort_Stream_f;
}
return true;
}
2015-09-21 23:05:36 +01:00
static Int has_close_on_abort(
int sno, Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
bool rc = GLOBAL_Stream[sno].status & DoNotCloseOnAbort_Stream_f;
if (!IsVarTerm(t2)) {
2015-09-21 23:05:36 +01:00
return t2 == (rc ? TermTrue : TermFalse);
}
2015-06-18 01:39:03 +01:00
if (rc) {
2015-09-21 23:05:36 +01:00
return Yap_unify_constant(t2, TermTrue);
2015-06-18 01:39:03 +01:00
} else {
return Yap_unify_constant(t2, TermFalse);
}
}
static bool
2015-09-21 23:05:36 +01:00
has_encoding(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2016-02-18 12:10:58 +00:00
const char *s = enc_name(GLOBAL_Stream[sno].encoding);
return Yap_unify(t2, MkAtomTerm(Yap_LookupAtom(s)));
2015-06-18 01:39:03 +01:00
}
static bool
2015-09-21 23:05:36 +01:00
found_eof(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags =
GLOBAL_Stream[sno].status & (Past_Eof_Stream_f | Eof_Stream_f);
2015-06-18 01:39:03 +01:00
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
if (flags & Past_Eof_Stream_f)
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, MkAtomTerm(AtomPast));
2015-06-18 01:39:03 +01:00
if (flags & Eof_Stream_f)
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, MkAtomTerm(AtomAt));
2016-03-05 00:08:04 +00:00
return Yap_unify(t2, MkAtomTerm(AtomAltNot));
2015-06-18 01:39:03 +01:00
}
2018-06-14 11:27:43 +01:00
static bool stream_mode(int sno, Term t2 USES_REGS) {
2018-03-17 10:38:56 +00:00
/* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags = GLOBAL_Stream[sno].status;
2015-06-18 01:39:03 +01:00
if (!IsVarTerm(t2) && !(isatom(t2))) {
2018-03-17 10:38:56 +00:00
return false;
2015-06-18 01:39:03 +01:00
}
if (flags & Input_Stream_f)
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermRead);
2015-06-18 01:39:03 +01:00
if (flags & Append_Stream_f)
2018-03-17 10:38:56 +00:00
return Yap_unify(t2, TermWrite);
2018-06-14 11:27:43 +01:00
if (flags & Output_Stream_f)
2018-03-17 10:38:56 +00:00
return Yap_unify(t2, TermWrite);
2018-06-14 11:27:43 +01:00
return false;
2015-06-18 01:39:03 +01:00
}
static bool
2015-09-21 23:05:36 +01:00
stream_tty(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
stream_flags_t flags = GLOBAL_Stream[sno].status & (Tty_Stream_f);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
if (flags & Tty_Stream_f)
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermTrue);
return Yap_unify(t2, TermFalse);
2015-06-18 01:39:03 +01:00
}
static bool
2015-09-21 23:05:36 +01:00
stream_type(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
stream_flags_t flags = GLOBAL_Stream[sno].status & (Binary_Stream_f);
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
if (flags & Binary_Stream_f)
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermBinary);
return Yap_unify(t2, TermText);
2015-06-18 01:39:03 +01:00
}
static bool
2015-09-21 23:05:36 +01:00
stream_position(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
Term tout = StreamPosition(sno);
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, tout);
2015-06-18 01:39:03 +01:00
}
2016-01-04 15:12:44 +00:00
static bool stream_line_count(
int sno, Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
Term tout = lineCount(sno);
return Yap_unify(t2, tout);
2015-09-21 23:05:36 +01:00
}
2016-01-04 15:12:44 +00:00
static bool stream_line_number(
int sno, Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
Term tout = MkIntegerTerm(GLOBAL_Stream[sno].linecount);
return Yap_unify(t2, tout);
2015-09-21 23:05:36 +01:00
}
static bool
SetBuffering(int sno, Atom at) { /* '$set_bufferingt'(+Stream,-ErrorMessage) */
2015-06-18 01:39:03 +01:00
if (at == AtomFull) {
2015-09-21 23:05:36 +01:00
if (setvbuf(GLOBAL_Stream[sno].file, NULL, _IOFBF, 0) < 0)
2016-01-04 15:12:44 +00:00
return PlIOError(SYSTEM_ERROR_INTERNAL, Yap_MkStream(sno),
"could not set buffer");
2015-06-18 01:39:03 +01:00
} else if (at == AtomLine) {
2015-09-21 23:05:36 +01:00
if (setvbuf(GLOBAL_Stream[sno].file, NULL, _IOLBF, 0) < 0)
2015-09-25 10:57:26 +01:00
return PlIOError(SYSTEM_ERROR_INTERNAL, Yap_MkStream(sno),
2015-09-21 23:05:36 +01:00
"could not set line buffering");
2015-06-18 01:39:03 +01:00
} else if (at == AtomFalse) {
2015-09-21 23:05:36 +01:00
if (setvbuf(GLOBAL_Stream[sno].file, NULL, _IONBF, 0) < 0)
2015-09-25 10:57:26 +01:00
return PlIOError(SYSTEM_ERROR_INTERNAL, Yap_MkStream(sno),
2015-09-21 23:05:36 +01:00
"could not set disable buffering");
2015-06-18 01:39:03 +01:00
} else {
2015-09-21 23:05:36 +01:00
CACHE_REGS
2015-06-18 01:39:03 +01:00
LOCAL_Error_TYPE = DOMAIN_ERROR_OUT_OF_RANGE;
LOCAL_ErrorMessage = "in set_stream/2:buffer";
return false;
}
return true;
}
2015-09-21 23:05:36 +01:00
static bool SetBuffer(int sno,
Int sz) { /* '$set_bufferingt'(+Stream,-ErrorMessage) */
if (setvbuf(GLOBAL_Stream[sno].file, NULL, _IOFBF, sz) < 0) {
2016-01-04 15:12:44 +00:00
return PlIOError(SYSTEM_ERROR_INTERNAL, Yap_MkStream(sno),
"could not set buffer");
2015-06-18 01:39:03 +01:00
}
return true;
}
static bool
2015-09-21 23:05:36 +01:00
eof_action(int sno,
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
stream_flags_t flags =
GLOBAL_Stream[sno].status &
(Eof_Error_Stream_f | Reset_Eof_Stream_f | Push_Eof_Stream_f);
2015-06-18 01:39:03 +01:00
if (!IsVarTerm(t2) && !(isatom(t2))) {
return FALSE;
}
if (flags & Eof_Error_Stream_f) {
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermError);
2015-06-18 01:39:03 +01:00
}
if (flags & Reset_Eof_Stream_f) {
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermReset);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
return Yap_unify(t2, TermEOfCode);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
#define STREAM_PROPERTY_DEFS() \
2016-07-31 15:55:50 +01:00
PAR("alias", filler, STREAM_PROPERTY_ALIAS) \
, PAR("bom", filler, STREAM_PROPERTY_BOM), \
2015-09-21 23:05:36 +01:00
PAR("close_on_abort", filler, STREAM_PROPERTY_CLOSE_ON_ABORT), \
PAR("encoding", filler, STREAM_PROPERTY_ENCODING), \
PAR("end_of_stream", filler, STREAM_PROPERTY_END_OF_STREAM), \
PAR("eof_action", filler, STREAM_PROPERTY_EOF_ACTION), \
PAR("file_name", filler, STREAM_PROPERTY_FILE_NAME), \
PAR("file_no", filler, STREAM_PROPERTY_FILE_NO), \
PAR("input", ok, STREAM_PROPERTY_INPUT), \
PAR("line_count", ok, STREAM_PROPERTY_LINE_COUNT), \
PAR("line_number", ok, STREAM_PROPERTY_LINE_NUMBER), \
PAR("mode", filler, STREAM_PROPERTY_MODE), \
PAR("output", filler, STREAM_PROPERTY_OUTPUT), \
PAR("position", isatom, STREAM_PROPERTY_POSITION), \
PAR("reposition", filler, STREAM_PROPERTY_REPOSITION), \
PAR("representation_errors", filler, \
STREAM_PROPERTY_REPRESENTATION_ERRORS), \
PAR("type", filler, STREAM_PROPERTY_TYPE), \
PAR("tty", filler, STREAM_PROPERTY_TTY), \
PAR(NULL, ok, STREAM_PROPERTY_END)
#define PAR(x, y, z) z
typedef enum stream_property_enum_choices {
STREAM_PROPERTY_DEFS()
} stream_property_choices_t;
2015-06-18 01:39:03 +01:00
#undef PAR
2015-09-21 23:05:36 +01:00
#define PAR(x, y, z) \
{ x, y, z }
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
static const param_t stream_property_defs[] = {STREAM_PROPERTY_DEFS()};
#undef PAR
static bool do_stream_property(int sno,
xarg *args USES_REGS) { /* Init current_stream */
2015-06-18 01:39:03 +01:00
stream_property_choices_t i;
bool rc = true;
2015-09-21 23:05:36 +01:00
for (i = 0; i < STREAM_PROPERTY_END; i++) {
2015-06-18 01:39:03 +01:00
if (args[i].used) {
switch (i) {
2016-03-05 12:17:23 +00:00
case STREAM_PROPERTY_ALIAS: {
Term ta = args[STREAM_PROPERTY_ALIAS].tvalue;
rc = rc & Yap_FetchStreamAlias(sno, ta PASS_REGS);
} break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_BOM:
2015-09-21 23:05:36 +01:00
rc = rc && has_bom(sno, args[STREAM_PROPERTY_BOM].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_CLOSE_ON_ABORT:
2015-09-21 23:05:36 +01:00
rc = rc &&
has_close_on_abort(
sno, args[STREAM_PROPERTY_CLOSE_ON_ABORT].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_ENCODING:
2015-09-21 23:05:36 +01:00
rc = rc &&
has_encoding(sno, args[STREAM_PROPERTY_ENCODING].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_END_OF_STREAM:
2015-09-21 23:05:36 +01:00
rc = rc &&
found_eof(sno,
args[STREAM_PROPERTY_END_OF_STREAM].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_EOF_ACTION:
2015-09-21 23:05:36 +01:00
rc = rc &&
eof_action(sno, args[STREAM_PROPERTY_EOF_ACTION].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_FILE_NAME:
2015-09-21 23:05:36 +01:00
rc = rc &&
file_name(sno, args[STREAM_PROPERTY_FILE_NAME].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_FILE_NO:
2015-09-21 23:05:36 +01:00
rc = rc && file_no(sno, args[STREAM_PROPERTY_FILE_NO].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_INPUT:
2015-09-21 23:05:36 +01:00
rc = rc && is_input(sno PASS_REGS);
break;
case STREAM_PROPERTY_LINE_NUMBER:
2016-01-04 15:12:44 +00:00
rc = rc && stream_line_number(
sno, args[STREAM_PROPERTY_LINE_NUMBER].tvalue PASS_REGS);
2015-09-21 23:05:36 +01:00
break;
case STREAM_PROPERTY_LINE_COUNT:
2016-01-04 15:12:44 +00:00
rc = rc && stream_line_count(
sno, args[STREAM_PROPERTY_LINE_COUNT].tvalue PASS_REGS);
2015-09-21 23:05:36 +01:00
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_MODE:
2015-09-21 23:05:36 +01:00
rc =
rc && stream_mode(sno, args[STREAM_PROPERTY_MODE].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_OUTPUT:
2015-09-21 23:05:36 +01:00
rc = rc && is_output(sno PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_POSITION:
2015-09-21 23:05:36 +01:00
rc = rc && stream_position(
sno, args[STREAM_PROPERTY_POSITION].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_REPOSITION:
2016-03-05 12:17:23 +00:00
rc = rc && has_reposition(
sno, args[STREAM_PROPERTY_REPOSITION].tvalue PASS_REGS);
2015-09-21 23:05:36 +01:00
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_REPRESENTATION_ERRORS:
2015-09-21 23:05:36 +01:00
rc = rc &&
representation_error(
sno,
args[STREAM_PROPERTY_REPRESENTATION_ERRORS].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_TYPE:
2015-09-21 23:05:36 +01:00
rc =
rc && stream_type(sno, args[STREAM_PROPERTY_TYPE].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_TTY:
2015-09-21 23:05:36 +01:00
rc = rc && stream_tty(sno, args[STREAM_PROPERTY_TTY].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case STREAM_PROPERTY_END:
2015-09-21 23:05:36 +01:00
rc = false;
break;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
}
2015-06-18 01:39:03 +01:00
}
return rc;
}
2016-03-05 12:17:23 +00:00
static xarg *generate_property(int sno, Term t2,
stream_property_choices_t p USES_REGS) {
if (p == STREAM_PROPERTY_INPUT)
Yap_unify(t2, MkAtomTerm(AtomInput));
else if (p == STREAM_PROPERTY_OUTPUT)
Yap_unify(t2, MkAtomTerm(AtomOutput));
else {
Functor f = Yap_MkFunctor(Yap_LookupAtom(stream_property_defs[p].name), 1);
Yap_unify(t2, Yap_MkNewApplTerm(f, 1));
}
2018-06-14 11:27:43 +01:00
return Yap_ArgListToVector(t2, stream_property_defs, STREAM_PROPERTY_END,
DOMAIN_ERROR_STREAM_PROPERTY_OPTION);
}
2015-09-21 23:05:36 +01:00
static Int cont_stream_property(USES_REGS1) { /* current_stream */
2018-07-12 11:02:09 +01:00
bool det = false;
2015-07-23 01:33:30 +01:00
xarg *args;
2015-09-21 23:05:36 +01:00
int i = IntOfTerm(EXTRA_CBACK_ARG(2, 1));
stream_property_choices_t p = STREAM_PROPERTY_END;
2015-07-23 01:33:30 +01:00
bool rc;
2016-03-05 12:17:23 +00:00
Term t2 = Deref(ARG2);
Term t1 = Deref(ARG1);
2015-07-23 01:33:30 +01:00
if (IsVarTerm(t2)) {
2016-03-05 12:17:23 +00:00
p = IntOfTerm(EXTRA_CBACK_ARG(2, 2));
args = generate_property(i, t2, p++ PASS_REGS);
2016-03-05 12:17:23 +00:00
EXTRA_CBACK_ARG(2, 2) = MkIntTerm(p % STREAM_PROPERTY_END);
// otherwise, just drop through
} else {
2018-06-14 11:27:43 +01:00
args = Yap_ArgListToVector(t2, stream_property_defs, STREAM_PROPERTY_END,
DOMAIN_ERROR_STREAM_PROPERTY_OPTION);
}
2015-07-23 01:33:30 +01:00
if (args == NULL) {
2016-02-11 14:17:30 +00:00
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
if (LOCAL_Error_TYPE == DOMAIN_ERROR_GENERIC_ARGUMENT)
2016-03-05 12:17:23 +00:00
LOCAL_Error_TYPE = DOMAIN_ERROR_STREAM_PROPERTY_OPTION;
2018-07-12 11:02:09 +01:00
Yap_ThrowError(LOCAL_Error_TYPE, t2, NULL);
return false;
2016-02-11 14:17:30 +00:00
}
2015-07-23 01:33:30 +01:00
cut_fail();
2016-03-05 12:17:23 +00:00
}
LOCK(GLOBAL_Stream[i].streamlock);
if (IsAtomTerm(args[STREAM_PROPERTY_ALIAS].tvalue)) {
// one solution only
i = Yap_CheckAlias(AtomOfTerm(args[STREAM_PROPERTY_ALIAS].tvalue));
2018-07-12 11:02:09 +01:00
UNLOCK(GLOBAL_Stream[i].streamlock);
2016-03-05 12:17:23 +00:00
if (i < 0 || !Yap_unify(ARG1, Yap_MkStream(i))) {
2018-07-12 11:02:09 +01:00
free(args);
cut_fail();
}
2018-07-12 11:02:09 +01:00
det = true;
2016-03-05 12:17:23 +00:00
}
LOCK(GLOBAL_Stream[i].streamlock);
rc = do_stream_property(i, args PASS_REGS);
UNLOCK(GLOBAL_Stream[i].streamlock);
2018-07-12 11:02:09 +01:00
if (!det && IsVarTerm(t1)) {
if (rc)
2016-03-05 12:17:23 +00:00
rc = Yap_unify(ARG1, Yap_MkStream(i));
if (p == STREAM_PROPERTY_END) {
// move to next existing stream
LOCK(GLOBAL_StreamDescLock);
2016-03-05 12:17:23 +00:00
while (++i < MaxStreams && GLOBAL_Stream[i].status & Free_Stream_f) {
}
UNLOCK(GLOBAL_StreamDescLock);
if (i < MaxStreams) {
EXTRA_CBACK_ARG(2, 1) = MkIntTerm(i);
det = false;
} else {
det = true;
2016-03-05 12:17:23 +00:00
}
} else {
det = false;
}
} else {
// done
2018-07-12 11:02:09 +01:00
det = det || (p == STREAM_PROPERTY_END);
2016-03-05 12:17:23 +00:00
}
2016-07-31 15:55:50 +01:00
free(args);
2015-07-23 01:33:30 +01:00
if (rc) {
2015-09-21 23:05:36 +01:00
if (det)
cut_succeed();
else {
return true;
}
} else if (det) {
2015-09-21 23:05:36 +01:00
cut_fail();
} else {
2015-09-21 23:05:36 +01:00
return false;
}
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int stream_property(USES_REGS1) { /* Init current_stream */
2015-06-18 01:39:03 +01:00
Term t1 = Deref(ARG1);
Term t2 = Deref(ARG2);
2015-09-21 23:05:36 +01:00
// Yap_DebugPlWrite(ARG1);fprintf(stderr,", ");
2015-07-23 01:33:30 +01:00
// Yap_DebugPlWrite(ARG2);fprintf(stderr,"\n");
2015-06-18 01:39:03 +01:00
/* make valgrind happy by always filling in memory */
2015-09-21 23:05:36 +01:00
EXTRA_CBACK_ARG(2, 1) = MkIntTerm(0);
EXTRA_CBACK_ARG(2, 2) = MkIntTerm(0);
2015-06-18 01:39:03 +01:00
if (!IsVarTerm(t1)) {
Int i;
2015-07-23 01:33:30 +01:00
xarg *args;
2015-09-21 23:05:36 +01:00
i = Yap_CheckStream(t1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"current_stream/3");
2016-03-05 12:17:23 +00:00
if (i < 0) {
UNLOCK(GLOBAL_Stream[i].streamlock);
return false; // error...
2015-07-23 01:33:30 +01:00
}
2016-03-05 12:17:23 +00:00
EXTRA_CBACK_ARG(2, 1) = MkIntTerm(i);
2016-03-05 00:08:04 +00:00
if (IsVarTerm(t2)) {
return cont_stream_property(PASS_REGS1);
2016-03-05 12:17:23 +00:00
}
2015-09-21 23:05:36 +01:00
args = Yap_ArgListToVector(Deref(ARG2), stream_property_defs,
2018-06-14 11:27:43 +01:00
STREAM_PROPERTY_END,
DOMAIN_ERROR_STREAM_PROPERTY_OPTION);
2015-07-23 01:33:30 +01:00
if (args == NULL) {
2016-02-11 14:17:30 +00:00
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
2016-03-05 12:17:23 +00:00
if (LOCAL_Error_TYPE == DOMAIN_ERROR_PROLOG_FLAG)
LOCAL_Error_TYPE = DOMAIN_ERROR_STREAM_PROPERTY_OPTION;
2016-10-16 23:23:00 +01:00
Yap_Error(LOCAL_Error_TYPE, ARG2, NULL);
2016-03-05 12:17:23 +00:00
return false;
2016-02-11 14:17:30 +00:00
}
2015-09-21 23:05:36 +01:00
UNLOCK(GLOBAL_Stream[i].streamlock);
cut_fail();
2015-07-23 01:33:30 +01:00
}
2015-09-21 23:05:36 +01:00
if (do_stream_property(i, args PASS_REGS)) {
UNLOCK(GLOBAL_Stream[i].streamlock);
2016-07-31 15:55:50 +01:00
free(args);
2015-09-21 23:05:36 +01:00
cut_succeed();
2015-07-23 01:33:30 +01:00
} else {
UNLOCK(GLOBAL_Stream[i].streamlock);
2016-07-31 15:55:50 +01:00
free(args);
2015-07-23 01:33:30 +01:00
cut_fail();
}
2015-06-18 01:39:03 +01:00
} else {
2015-09-21 23:05:36 +01:00
return cont_stream_property(PASS_REGS1);
2015-06-18 01:39:03 +01:00
}
}
2015-09-21 23:05:36 +01:00
#define SET_STREAM_DEFS() \
2016-07-31 15:55:50 +01:00
PAR("alias", isatom, SET_STREAM_ALIAS) \
, PAR("buffer", booleanFlag, SET_STREAM_BUFFER), \
2015-09-21 23:05:36 +01:00
PAR("buffer_size", nat, SET_STREAM_BUFFER_SIZE), \
PAR("close_on_abort", booleanFlag, SET_STREAM_CLOSE_ON_ABORT), \
2015-09-21 23:05:36 +01:00
PAR("encoding", isatom, SET_STREAM_ENCODING), \
PAR("eof_action", isatom, SET_STREAM_EOF_ACTION), \
PAR("file_name", isatom, SET_STREAM_FILE_NAME), \
PAR("line_position", nat, SET_STREAM_LINE_POSITION), \
PAR("newline", filler, SET_STREAM_NEWLINE), \
PAR("record_position", isatom, SET_STREAM_RECORD_POSITION), \
PAR("representation_errors", isatom, SET_STREAM_REPRESENTATION_ERRORS), \
PAR("type", isatom, SET_STREAM_TYPE), \
PAR("tty", filler, SET_STREAM_TTY), PAR(NULL, ok, SET_STREAM_END)
#define PAR(x, y, z) z
typedef enum set_stream_enum_choices {
SET_STREAM_DEFS()
} set_stream_enum_choices_t;
2015-06-18 01:39:03 +01:00
#undef PAR
2015-09-21 23:05:36 +01:00
#define PAR(x, y, z) \
{ x, y, z }
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
static const param_t set_stream_defs[] = {SET_STREAM_DEFS()};
#undef PAR
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
static bool do_set_stream(int sno,
Term opts USES_REGS) { /* Init current_stream */
2015-06-18 01:39:03 +01:00
xarg *args;
set_stream_enum_choices_t i;
bool rc = true;
2018-06-14 11:27:43 +01:00
args = Yap_ArgListToVector(opts, set_stream_defs, SET_STREAM_END,
DOMAIN_ERROR_SET_STREAM_OPTION);
2015-06-18 01:39:03 +01:00
if (args == NULL) {
2016-02-11 14:17:30 +00:00
if (LOCAL_Error_TYPE != YAP_NO_ERROR) {
if (LOCAL_Error_TYPE == DOMAIN_ERROR_GENERIC_ARGUMENT)
2016-03-05 12:17:23 +00:00
LOCAL_Error_TYPE = DOMAIN_ERROR_SET_STREAM_OPTION;
2016-10-16 23:23:00 +01:00
Yap_Error(LOCAL_Error_TYPE, opts, NULL);
2016-02-11 14:17:30 +00:00
}
2015-09-21 23:05:36 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-06-18 01:39:03 +01:00
return false;
}
2015-09-21 23:05:36 +01:00
for (i = 0; i < SET_STREAM_END; i++) {
2015-06-18 01:39:03 +01:00
if (args[i].used) {
Term t = args[i].tvalue;
switch (i) {
case SET_STREAM_ALIAS:
2015-09-21 23:05:36 +01:00
rc = rc && Yap_AddAlias(AtomOfTerm(t), sno);
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_BUFFER:
2015-09-21 23:05:36 +01:00
rc = rc && SetBuffering(sno, AtomOfTerm(t));
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_BUFFER_SIZE:
2015-09-21 23:05:36 +01:00
rc = rc && SetBuffer(sno, IntegerOfTerm(t));
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_CLOSE_ON_ABORT:
2015-09-21 23:05:36 +01:00
rc = rc &&
SetCloseOnAbort(
sno, (args[SET_STREAM_CLOSE_ON_ABORT].tvalue == TermTrue));
break;
2016-03-05 12:17:23 +00:00
case SET_STREAM_ENCODING: {
Term t2 = args[SET_STREAM_ENCODING].tvalue;
Atom atEnc = AtomOfTerm(t2);
GLOBAL_Stream[sno].encoding =
enc_id(atEnc->StrOfAE, (GLOBAL_Stream[sno].status & HAS_BOM_f
? GLOBAL_Stream[sno].encoding
: ENC_OCTET));
Yap_DefaultStreamOps(GLOBAL_Stream + sno);
} break;
2015-09-21 23:05:36 +01:00
case SET_STREAM_EOF_ACTION: {
Term t2 = args[SET_STREAM_EOF_ACTION].tvalue;
if (t2 == TermError) {
GLOBAL_Stream[sno].status |= Eof_Error_Stream_f;
GLOBAL_Stream[sno].status &= ~Reset_Eof_Stream_f;
} else if (t2 == TermReset) {
2016-01-04 15:12:44 +00:00
GLOBAL_Stream[sno].status &= ~Eof_Error_Stream_f;
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].status |= Reset_Eof_Stream_f;
} else if (t2 == TermEOfCode) {
GLOBAL_Stream[sno].status &= ~Eof_Error_Stream_f;
GLOBAL_Stream[sno].status &= ~Reset_Eof_Stream_f;
} else {
LOCAL_Error_TYPE = DOMAIN_ERROR_OUT_OF_RANGE;
LOCAL_ErrorMessage = "in set_stream/2:eof_action";
rc = false;
}
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_FILE_NAME:
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].user_name = args[SET_STREAM_FILE_NAME].tvalue;
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_LINE_POSITION:
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].linepos =
IntegerOfTerm(args[SET_STREAM_FILE_NAME].tvalue);
break;
case SET_STREAM_NEWLINE:
printf("not yet\n");
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_RECORD_POSITION:
2015-09-21 23:05:36 +01:00
if (args[SET_STREAM_RECORD_POSITION].tvalue == TermTrue)
GLOBAL_Stream[sno].status |= Seekable_Stream_f;
else
GLOBAL_Stream[sno].status &= ~Seekable_Stream_f;
break;
case SET_STREAM_REPRESENTATION_ERRORS: {
Term t2 = args[SET_STREAM_EOF_ACTION].tvalue;
if (t2 == TermXml) {
GLOBAL_Stream[sno].status |= RepError_Xml_f;
GLOBAL_Stream[sno].status &= ~RepError_Prolog_f;
} else if (t2 == TermError) {
2015-10-20 08:01:20 +01:00
GLOBAL_Stream[sno].status &= ~RepError_Xml_f;
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].status |= RepError_Prolog_f;
} else if (t2 == TermEOfCode) {
GLOBAL_Stream[sno].status &= ~RepError_Xml_f;
GLOBAL_Stream[sno].status |= RepError_Prolog_f;
} else {
LOCAL_Error_TYPE = DOMAIN_ERROR_OUT_OF_RANGE;
LOCAL_ErrorMessage = "in set_stream/2:eof_action";
rc = false;
}
} break;
case SET_STREAM_TYPE:
rc &= stream_type(sno, args[SET_STREAM_TYPE].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_TTY:
2015-09-21 23:05:36 +01:00
rc &= stream_tty(sno, args[SET_STREAM_TTY].tvalue PASS_REGS);
break;
2015-06-18 01:39:03 +01:00
case SET_STREAM_END:
2015-09-21 23:05:36 +01:00
rc = false;
break;
}
}
2015-06-18 01:39:03 +01:00
}
}
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
}
2015-09-21 23:05:36 +01:00
static Int set_stream(USES_REGS1) { /* Init current_stream */
int sno =
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"set_stream_position/2");
2015-06-18 01:39:03 +01:00
if (sno < 0) {
return false;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
return do_set_stream(sno, Deref(ARG2) PASS_REGS);
2015-06-18 01:39:03 +01:00
}
2018-05-14 16:03:02 +01:00
/**
2015-06-18 01:39:03 +01:00
* Called when you want to close all open streams, except for stdin, stdout
2015-09-21 23:05:36 +01:00
* and stderr
2015-06-18 01:39:03 +01:00
*/
2018-05-14 16:03:02 +01:00
void Yap_CloseStreams(void) {
2015-09-21 23:05:36 +01:00
CACHE_REGS
2015-06-18 01:39:03 +01:00
int sno;
2016-01-04 15:12:44 +00:00
fflush(NULL);
2015-06-18 01:39:03 +01:00
for (sno = 3; sno < MaxStreams; ++sno) {
if (GLOBAL_Stream[sno].status & Free_Stream_f)
continue;
2016-07-31 15:55:50 +01:00
CloseStream(sno);
2015-06-18 01:39:03 +01:00
}
}
2018-05-14 16:03:02 +01:00
/**
* Called when you want to close all temporary streams,
* except for stdin, stdout
* and stderr
*/
void Yap_CloseTemporaryStreams(void) {
CACHE_REGS
int sno;
fflush(NULL);
for (sno = 3; sno < MaxStreams; ++sno) {
if (GLOBAL_Stream[sno].status & Free_Stream_f)
continue;
if (GLOBAL_Stream[sno].status & CloseOnException_Stream_f)
CloseStream(sno);
}
}
2016-01-04 15:12:44 +00:00
static void CloseStream(int sno) {
2015-09-21 23:05:36 +01:00
CACHE_REGS
2018-06-14 11:27:43 +01:00
// fflush(NULL);
2018-06-25 13:44:09 +01:00
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "close stream <%d>",
// sno);
VFS_t *me;
2018-07-09 00:50:00 +01:00
//fprintf( stderr, "- %d\n",sno);
2018-07-06 16:51:19 +01:00
if ((me = GLOBAL_Stream[sno].vfs) != NULL &&
2018-06-14 11:27:43 +01:00
GLOBAL_Stream[sno].file == NULL) {
if (me->close) {
me->close(sno);
}
GLOBAL_Stream[sno].vfs = NULL;
} else if (GLOBAL_Stream[sno].file &&
2018-06-14 11:27:43 +01:00
(GLOBAL_Stream[sno].status & Popen_Stream_f)) {
2018-04-27 13:01:08 +01:00
pclose(GLOBAL_Stream[sno].file);
} else if (GLOBAL_Stream[sno].file &&
2018-06-14 11:27:43 +01:00
!(GLOBAL_Stream[sno].status & (Null_Stream_f | Socket_Stream_f |
InMemory_Stream_f | Pipe_Stream_f)))
2018-04-27 13:01:08 +01:00
fclose(GLOBAL_Stream[sno].file);
2015-06-18 01:39:03 +01:00
#if HAVE_SOCKET
else if (GLOBAL_Stream[sno].status & (Socket_Stream_f)) {
Yap_CloseSocket(GLOBAL_Stream[sno].u.socket.fd,
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].u.socket.flags,
GLOBAL_Stream[sno].u.socket.domain);
2015-06-18 01:39:03 +01:00
}
#endif
else if (GLOBAL_Stream[sno].status & Pipe_Stream_f) {
close(GLOBAL_Stream[sno].u.pipe.fd);
2015-09-21 23:05:36 +01:00
} else if (GLOBAL_Stream[sno].status & (InMemory_Stream_f)) {
2016-03-05 12:17:23 +00:00
Yap_CloseMemoryStream(sno);
2015-06-18 01:39:03 +01:00
}
2018-06-14 11:27:43 +01:00
if (LOCAL_c_input_stream == sno) {
2015-09-21 23:05:36 +01:00
LOCAL_c_input_stream = StdInStream;
}
if (LOCAL_c_output_stream == sno) {
2015-09-21 23:05:36 +01:00
LOCAL_c_output_stream = StdOutStream;
}
if (LOCAL_c_error_stream == sno) {
2015-09-21 23:05:36 +01:00
LOCAL_c_error_stream = StdErrStream;
}
2018-06-14 11:27:43 +01:00
Yap_DeleteAliases(sno);
2018-03-12 15:11:59 +00:00
GLOBAL_Stream[sno].vfs = NULL;
GLOBAL_Stream[sno].file = NULL;
GLOBAL_Stream[sno].status = Free_Stream_f;
2018-06-26 10:13:11 +01:00
// __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "close stream <%d>", sno);
2018-03-12 15:11:59 +00:00
2015-06-18 01:39:03 +01:00
/* if (st->status == Socket_Stream_f|Input_Stream_f|Output_Stream_f) {
Yap_CloseSocket();
}
*/
}
2015-09-21 23:05:36 +01:00
void Yap_CloseStream(int sno) { CloseStream(sno); }
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
void Yap_ReleaseStream(int sno) {
CACHE_REGS
GLOBAL_Stream[sno].status = Free_Stream_f;
2016-08-05 22:44:38 +01:00
GLOBAL_Stream[sno].user_name = 0;
2018-06-14 11:27:43 +01:00
GLOBAL_Stream[sno].vfs = NULL;
GLOBAL_Stream[sno].file = NULL;
2015-06-18 01:39:03 +01:00
Yap_DeleteAliases(sno);
2015-09-21 23:05:36 +01:00
if (LOCAL_c_input_stream == sno) {
LOCAL_c_input_stream = StdInStream;
}
if (LOCAL_c_output_stream == sno) {
2015-09-21 23:05:36 +01:00
LOCAL_c_output_stream = StdOutStream;
}
if (LOCAL_c_error_stream == sno) {
2015-09-21 23:05:36 +01:00
LOCAL_c_error_stream = StdErrStream;
}
2015-06-18 01:39:03 +01:00
/* if (st->status == Socket_Stream_f|Input_Stream_f|Output_Stream_f) {
Yap_CloseSocket();
}
*/
}
2015-09-21 23:05:36 +01:00
static Int current_input(USES_REGS1) { /* current_input(?Stream) */
2015-06-18 01:39:03 +01:00
Term t1 = Deref(ARG1);
if (IsVarTerm(t1)) {
2015-09-21 23:05:36 +01:00
Term t = Yap_MkStream(LOCAL_c_input_stream);
2015-06-18 01:39:03 +01:00
YapBind(VarOfTerm(t1), t);
return TRUE;
2015-09-21 23:05:36 +01:00
} else if (!IsApplTerm(t1) || FunctorOfTerm(t1) != FunctorStream ||
!IsIntTerm((t1 = ArgOfTerm(1, t1)))) {
Yap_Error(DOMAIN_ERROR_STREAM, t1, "current_input/1");
2015-06-18 01:39:03 +01:00
return FALSE;
} else {
return LOCAL_c_input_stream == IntOfTerm(t1);
}
}
2018-06-14 11:27:43 +01:00
bool Yap_SetInputStream(Term sd) {
int sno = Yap_CheckStream(sd, Input_Stream_f, "set_input/1");
if (sno < 0)
return false;
LOCAL_c_input_stream = sno;
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_SetAlias(AtomUserIn, sno);
return true;
}
2015-09-21 23:05:36 +01:00
/** @pred set_input(+ _S_) is iso
2015-06-18 01:39:03 +01:00
* Set stream _S_ as the current input stream. Predicates like read/1
* and get/1 will start using stream _S_ by default.
2015-09-21 23:05:36 +01:00
*
*
* @param Input-mode stream
*
2015-06-18 01:39:03 +01:00
*/
2015-09-21 23:05:36 +01:00
static Int set_input(USES_REGS1) { /* '$show_stream_position'(+Stream,Pos) */
2018-06-14 11:27:43 +01:00
return Yap_SetInputStream(ARG1);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int current_output(USES_REGS1) { /* current_output(?Stream) */
2015-06-18 01:39:03 +01:00
Term t1 = Deref(ARG1);
if (IsVarTerm(t1)) {
2015-09-21 23:05:36 +01:00
Term t = Yap_MkStream(LOCAL_c_output_stream);
2015-06-18 01:39:03 +01:00
YapBind(VarOfTerm(t1), t);
return TRUE;
2015-09-21 23:05:36 +01:00
} else if (!IsApplTerm(t1) || FunctorOfTerm(t1) != FunctorStream ||
!IsIntTerm((t1 = ArgOfTerm(1, t1)))) {
Yap_Error(DOMAIN_ERROR_STREAM, t1, "current_output/1");
2015-06-18 01:39:03 +01:00
return FALSE;
} else {
2015-09-21 23:05:36 +01:00
return (LOCAL_c_output_stream == IntOfTerm(t1));
2015-06-18 01:39:03 +01:00
}
}
2018-06-14 11:27:43 +01:00
bool Yap_SetOutputStream(Term sd) {
int sno =
Yap_CheckStream(sd, Output_Stream_f | Append_Stream_f, "set_output/2");
if (sno < 0)
return false;
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_SetAlias(AtomUserOut, sno);
return true;
}
2018-06-14 11:27:43 +01:00
bool Yap_SetErrorStream(Term sd) {
int sno =
Yap_CheckStream(sd, Output_Stream_f | Append_Stream_f, "set_error/2");
if (sno < 0)
return false;
LOCAL_c_error_stream = sno;
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_SetAlias(AtomUserErr, sno);
return true;
}
2015-09-21 23:05:36 +01:00
/** @pred set_input(+ _S_) is iso
2015-06-18 01:39:03 +01:00
* Set stream _S_ as the current input stream. Predicates like read/1
* and get/1 will start using stream _S_ by default.
2015-09-21 23:05:36 +01:00
*
*
* @param Output-mode stream
*
2015-06-18 01:39:03 +01:00
*/
2015-09-21 23:05:36 +01:00
static Int set_output(USES_REGS1) { /* '$show_stream_position'(+Stream,Pos) */
2018-06-14 11:27:43 +01:00
return Yap_SetOutputStream(ARG1);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int p_user_file_name(USES_REGS1) {
2015-06-18 01:39:03 +01:00
Term tout;
2015-09-21 23:05:36 +01:00
int sno =
2016-07-31 15:55:50 +01:00
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
2015-09-21 23:05:36 +01:00
"user_file_name/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return (FALSE);
2015-07-06 12:03:16 +01:00
tout = Yap_StreamUserName(sno);
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (Yap_unify_constant(ARG2, tout));
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
static Int p_file_name(USES_REGS1) {
2015-06-18 01:39:03 +01:00
Term tout;
2015-09-21 23:05:36 +01:00
int sno = Yap_CheckStream(
ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f, "file_name/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return (FALSE);
#if HAVE_SOCKET
if (GLOBAL_Stream[sno].status & Socket_Stream_f)
tout = MkAtomTerm(AtomSocket);
else
#endif
2015-09-21 23:05:36 +01:00
if (GLOBAL_Stream[sno].status & Pipe_Stream_f)
2015-06-18 01:39:03 +01:00
tout = MkAtomTerm(AtomPipe);
else if (GLOBAL_Stream[sno].status & InMemory_Stream_f)
tout = MkAtomTerm(AtomCharsio);
else
tout = MkAtomTerm(GLOBAL_Stream[sno].name);
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return Yap_unify_constant(ARG2, tout);
2015-06-18 01:39:03 +01:00
}
2015-11-13 13:21:45 +00:00
static Int line_count(USES_REGS1) { /* '$current_line_number'(+Stream,-N) */
2015-06-18 01:39:03 +01:00
Term tout;
int sno =
2015-09-21 23:05:36 +01:00
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"current_line_number/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
2015-11-13 13:21:45 +00:00
return (false);
tout = lineCount(sno);
2015-09-21 23:05:36 +01:00
return (Yap_unify_constant(ARG2, tout));
2015-06-18 01:39:03 +01:00
}
2016-10-16 23:18:51 +01:00
static Int line_position(USES_REGS1) { /* '$line_position'(+Stream,-N) */
2015-06-18 01:39:03 +01:00
Term tout;
2015-09-21 23:05:36 +01:00
int sno =
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"line_position/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return (FALSE);
2015-09-21 23:05:36 +01:00
if (GLOBAL_Stream[sno].status & Tty_Stream_f) {
Int no = 0;
int i;
Atom my_stream = GLOBAL_Stream[sno].name;
for (i = 0; i < MaxStreams; i++) {
if (!(GLOBAL_Stream[i].status & Free_Stream_f) &&
GLOBAL_Stream[i].name == my_stream)
no += GLOBAL_Stream[i].linepos;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
tout = MkIntTerm(no);
} else
tout = MkIntTerm(GLOBAL_Stream[sno].linepos);
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (Yap_unify_constant(ARG2, tout));
2015-06-18 01:39:03 +01:00
}
2016-10-16 23:18:51 +01:00
static Int character_count(USES_REGS1) { /* '$character_count'(+Stream,-N) */
2015-06-18 01:39:03 +01:00
Term tout;
2015-09-21 23:05:36 +01:00
int sno =
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"character_count/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return (FALSE);
2015-09-21 23:05:36 +01:00
if (GLOBAL_Stream[sno].status & Tty_Stream_f) {
Int no = 0;
int i;
Atom my_stream = GLOBAL_Stream[sno].name;
for (i = 0; i < MaxStreams; i++) {
if (!(GLOBAL_Stream[i].status & Free_Stream_f) &&
GLOBAL_Stream[i].name == my_stream)
no += GLOBAL_Stream[i].charcount;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
tout = MkIntTerm(no);
} else if (GLOBAL_Stream[sno].status & Null_Stream_f)
tout = MkIntTerm(GLOBAL_Stream[sno].charcount);
2015-06-18 01:39:03 +01:00
else
2015-09-21 23:05:36 +01:00
tout = MkIntTerm(ftell(GLOBAL_Stream[sno].file));
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (Yap_unify_constant(ARG2, tout));
2015-06-18 01:39:03 +01:00
}
static Int
2015-09-21 23:05:36 +01:00
p_show_stream_flags(USES_REGS1) { /* '$show_stream_flags'(+Stream,Pos) */
2015-06-18 01:39:03 +01:00
Term tout;
int sno =
2015-09-21 23:05:36 +01:00
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"stream_property/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return (FALSE);
tout = MkIntTerm(GLOBAL_Stream[sno].status);
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (Yap_unify(ARG2, tout));
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
Term Yap_StreamPosition(int sno) { return StreamPosition(sno); }
2015-06-18 01:39:03 +01:00
2015-09-21 23:05:36 +01:00
static Int p_show_stream_position(
USES_REGS1) { /* '$show_stream_position'(+Stream,Pos) */
2015-06-18 01:39:03 +01:00
Term tout;
int sno =
2015-09-21 23:05:36 +01:00
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"stream_position/2");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return (FALSE);
tout = StreamPosition(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return Yap_unify(ARG2, tout);
2015-06-18 01:39:03 +01:00
}
static Int
2015-09-21 23:05:36 +01:00
set_stream_position(USES_REGS1) { /* '$set_stream_position'(+Stream,Pos) */
2015-06-18 01:39:03 +01:00
Term tin, tp;
Int char_pos;
2015-09-21 23:05:36 +01:00
int sno =
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
"set_stream_position/2");
2015-06-18 01:39:03 +01:00
if (sno < 0) {
2016-03-30 01:27:54 +01:00
return false;
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
tin = Deref(ARG2);
if (IsVarTerm(tin)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tin, "set_stream_position/2");
return (FALSE);
2015-09-21 23:05:36 +01:00
} else if (!(IsApplTerm(tin))) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
2015-09-21 23:05:36 +01:00
if (FunctorOfTerm(tin) == FunctorStreamPos) {
if (IsVarTerm(tp = ArgOfTerm(1, tin))) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
2015-09-21 23:05:36 +01:00
return (FALSE);
} else if (!IsIntTerm(tp)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
2015-09-21 23:05:36 +01:00
if (!(GLOBAL_Stream[sno].status & Seekable_Stream_f)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
Yap_Error(PERMISSION_ERROR_REPOSITION_STREAM, ARG1,
"set_stream_position/2");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
char_pos = IntOfTerm(tp);
if (IsVarTerm(tp = ArgOfTerm(2, tin))) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
2015-09-21 23:05:36 +01:00
return (FALSE);
} else if (!IsIntTerm(tp)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
GLOBAL_Stream[sno].charcount = char_pos;
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].linecount = IntOfTerm(tp);
if (IsVarTerm(tp = ArgOfTerm(3, tin))) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
2015-09-21 23:05:36 +01:00
return (FALSE);
} else if (!IsIntTerm(tp)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
2015-09-21 23:05:36 +01:00
GLOBAL_Stream[sno].linepos = IntOfTerm(tp);
if (fseek(GLOBAL_Stream[sno].file, (long)(char_pos), 0) == -1) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2016-01-04 15:12:44 +00:00
Yap_Error(SYSTEM_ERROR_INTERNAL, tp,
"fseek failed for set_stream_position/2");
2015-09-21 23:05:36 +01:00
return (FALSE);
2015-06-18 01:39:03 +01:00
}
GLOBAL_Stream[sno].stream_getc = PlGetc;
2015-09-21 23:05:36 +01:00
} else if (FunctorOfTerm(tin) == FunctorStreamEOS) {
if (IsVarTerm(tp = ArgOfTerm(1, tin))) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(INSTANTIATION_ERROR, tp, "set_stream_position/2");
2015-09-21 23:05:36 +01:00
return (FALSE);
2015-06-18 01:39:03 +01:00
} else if (tp != MkAtomTerm(AtomAt)) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(DOMAIN_ERROR_STREAM_POSITION, tin, "set_stream_position/2");
return (FALSE);
}
2015-09-21 23:05:36 +01:00
if (!(GLOBAL_Stream[sno].status & Seekable_Stream_f)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
PlIOError(PERMISSION_ERROR_REPOSITION_STREAM, ARG1,
"set_stream_position/2");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
2018-06-14 11:27:43 +01:00
if (GLOBAL_Stream[sno].vfs) {
if (GLOBAL_Stream[sno].vfs->seek &&
GLOBAL_Stream[sno].vfs->seek(sno, 0L, SEEK_END) == -1) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
PlIOError(SYSTEM_ERROR_INTERNAL, tp,
"fseek failed for set_stream_position/2: %s",
strerror(errno));
return (FALSE);
2018-01-18 14:47:27 +00:00
}
2018-06-14 11:27:43 +01:00
} else if (fseek(GLOBAL_Stream[sno].file, 0L, SEEK_END) == -1) {
2015-09-21 23:05:36 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2016-01-04 15:12:44 +00:00
PlIOError(SYSTEM_ERROR_INTERNAL, tp,
"fseek failed for set_stream_position/2: %s", strerror(errno));
2015-09-21 23:05:36 +01:00
return (FALSE);
2015-06-18 01:39:03 +01:00
}
GLOBAL_Stream[sno].stream_getc = PlGetc;
2016-03-05 12:17:23 +00:00
/* reset the counters */
2015-06-18 01:39:03 +01:00
GLOBAL_Stream[sno].linepos = 0;
GLOBAL_Stream[sno].linecount = 1;
GLOBAL_Stream[sno].charcount = 0;
}
UNLOCK(GLOBAL_Stream[sno].streamlock);
return (TRUE);
}
#if HAVE_SELECT
/* stream_select(+Streams,+TimeOut,-Result) */
2015-09-21 23:05:36 +01:00
static Int p_stream_select(USES_REGS1) {
2015-06-18 01:39:03 +01:00
Term t1 = Deref(ARG1), t2;
fd_set readfds, writefds, exceptfds;
struct timeval timeout, *ptime;
#if _MSC_VER
2015-09-21 23:05:36 +01:00
u_int fdmax = 0;
2015-06-18 01:39:03 +01:00
#else
2015-09-21 23:05:36 +01:00
int fdmax = 0;
2015-06-18 01:39:03 +01:00
#endif
Term tout = TermNil, ti, Head;
if (IsVarTerm(t1)) {
2015-09-21 23:05:36 +01:00
Yap_Error(INSTANTIATION_ERROR, t1, "stream_select/3");
2015-06-18 01:39:03 +01:00
return FALSE;
}
if (!IsPairTerm(t1)) {
2015-09-21 23:05:36 +01:00
Yap_Error(TYPE_ERROR_LIST, t1, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
FD_ZERO(&readfds);
FD_ZERO(&writefds);
FD_ZERO(&exceptfds);
ti = t1;
while (ti != TermNil) {
#if _MSC_VER
u_int fd;
#else
int fd;
#endif
int sno;
Head = HeadOfTerm(ti);
2015-09-21 23:05:36 +01:00
sno = Yap_CheckStream(Head, Input_Stream_f, "stream_select/3");
2015-06-18 01:39:03 +01:00
if (sno < 0)
2015-09-21 23:05:36 +01:00
return (FALSE);
2015-06-18 01:39:03 +01:00
fd = GetStreamFd(sno);
FD_SET(fd, &readfds);
UNLOCK(GLOBAL_Stream[sno].streamlock);
if (fd > fdmax)
fdmax = fd;
ti = TailOfTerm(ti);
}
t2 = Deref(ARG2);
if (IsVarTerm(t2)) {
2015-09-21 23:05:36 +01:00
Yap_Error(INSTANTIATION_ERROR, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
if (IsAtomTerm(t2)) {
if (t2 == MkAtomTerm(AtomOff)) {
/* wait indefinitely */
ptime = NULL;
} else {
2015-09-21 23:05:36 +01:00
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC, t1, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
} else {
Term t21, t22;
if (!IsApplTerm(t2) || FunctorOfTerm(t2) != FunctorModule) {
2015-09-21 23:05:36 +01:00
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
t21 = ArgOfTerm(1, t2);
if (IsVarTerm(t21)) {
2015-09-21 23:05:36 +01:00
Yap_Error(INSTANTIATION_ERROR, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
if (!IsIntegerTerm(t21)) {
2015-09-21 23:05:36 +01:00
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
timeout.tv_sec = IntegerOfTerm(t21);
if (timeout.tv_sec < 0) {
2015-09-21 23:05:36 +01:00
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
t22 = ArgOfTerm(2, t2);
if (IsVarTerm(t22)) {
2015-09-21 23:05:36 +01:00
Yap_Error(INSTANTIATION_ERROR, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
if (!IsIntegerTerm(t22)) {
2015-09-21 23:05:36 +01:00
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
timeout.tv_usec = IntegerOfTerm(t22);
if (timeout.tv_usec < 0) {
2015-09-21 23:05:36 +01:00
Yap_Error(DOMAIN_ERROR_TIMEOUT_SPEC, t2, "stream_select/3");
return (FALSE);
2015-06-18 01:39:03 +01:00
}
ptime = &timeout;
}
/* do the real work */
2015-09-21 23:05:36 +01:00
if (select(fdmax + 1, &readfds, &writefds, &exceptfds, ptime) < 0) {
2015-06-18 01:39:03 +01:00
#if HAVE_STRERROR
2015-09-25 10:57:26 +01:00
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "stream_select/3 (select: %s)",
2015-09-21 23:05:36 +01:00
strerror(errno));
2015-06-18 01:39:03 +01:00
#else
2015-09-25 10:57:26 +01:00
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "stream_select/3 (select)");
2015-06-18 01:39:03 +01:00
#endif
}
while (t1 != TermNil) {
int fd;
int sno;
Head = HeadOfTerm(t1);
2015-09-21 23:05:36 +01:00
sno = Yap_CheckStream(Head, Input_Stream_f, "stream_select/3");
2015-06-18 01:39:03 +01:00
fd = GetStreamFd(sno);
if (FD_ISSET(fd, &readfds))
2015-09-21 23:05:36 +01:00
tout = MkPairTerm(Head, tout);
else
tout = MkPairTerm(TermNil, tout);
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
t1 = TailOfTerm(t1);
}
/* we're done, just pass the info back */
2015-09-21 23:05:36 +01:00
return (Yap_unify(ARG3, tout));
2015-06-18 01:39:03 +01:00
}
#endif
2015-09-21 23:05:36 +01:00
Int Yap_StreamToFileNo(Term t) {
int sno =
Yap_CheckStream(t, (Input_Stream_f | Output_Stream_f), "StreamToFileNo");
2015-06-18 01:39:03 +01:00
if (GLOBAL_Stream[sno].status & Pipe_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (GLOBAL_Stream[sno].u.pipe.fd);
2015-06-18 01:39:03 +01:00
#if HAVE_SOCKET
} else if (GLOBAL_Stream[sno].status & Socket_Stream_f) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (GLOBAL_Stream[sno].u.socket.fd);
2015-06-18 01:39:03 +01:00
#endif
2015-06-19 10:10:02 +01:00
} else if (GLOBAL_Stream[sno].status & (Null_Stream_f)) {
2015-06-18 01:39:03 +01:00
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (-1);
2015-06-18 01:39:03 +01:00
} else {
UNLOCK(GLOBAL_Stream[sno].streamlock);
2015-09-21 23:05:36 +01:00
return (fileno(GLOBAL_Stream[sno].file));
2015-06-18 01:39:03 +01:00
}
}
2015-09-21 23:05:36 +01:00
static Int p_stream(USES_REGS1) {
2015-06-18 01:39:03 +01:00
Term in = Deref(ARG1);
if (IsVarTerm(in))
2015-09-21 23:05:36 +01:00
return (FALSE);
2015-06-18 01:39:03 +01:00
if (IsAtomTerm(in))
2015-09-21 23:05:36 +01:00
return (Yap_CheckAlias(AtomOfTerm(in)) >= 0);
2015-06-18 01:39:03 +01:00
if (IsApplTerm(in))
2015-09-21 23:05:36 +01:00
return (FunctorOfTerm(in) == FunctorStream);
return (FALSE);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
FILE *Yap_FileDescriptorFromStream(Term t) {
int sno = Yap_CheckStream(t, Input_Stream_f | Output_Stream_f,
"FileDescriptorFromStream");
FILE *rc;
2015-06-18 01:39:03 +01:00
if (sno < 0)
return NULL;
2015-09-21 23:05:36 +01:00
if (GLOBAL_Stream[sno].status &
(Null_Stream_f | Socket_Stream_f | Pipe_Stream_f | Free_Stream_f))
2015-06-19 10:10:02 +01:00
rc = NULL;
2015-09-21 23:05:36 +01:00
else
rc = GLOBAL_Stream[sno].file;
UNLOCK(GLOBAL_Stream[sno].streamlock);
return rc;
2015-06-18 01:39:03 +01:00
}
void
Yap_InitBackIO (
2016-07-31 15:55:50 +01:00
void)
2015-06-18 01:39:03 +01:00
{
Yap_InitCPredBack("stream_property", 2, 2, stream_property,
2015-09-21 23:05:36 +01:00
cont_stream_property, SafePredFlag | SyncPredFlag);
2015-06-18 01:39:03 +01:00
}
2015-09-21 23:05:36 +01:00
void Yap_InitIOStreams(void) {
Yap_InitCPred("$stream_flags", 2, stream_flags,
2015-09-21 23:05:36 +01:00
SafePredFlag | SyncPredFlag | HiddenPredFlag);
Yap_InitCPred("$check_stream", 2, p_check_stream,
SafePredFlag | SyncPredFlag | HiddenPredFlag);
Yap_InitCPred("$check_stream", 1, p_check_if_stream,
SafePredFlag | SyncPredFlag | HiddenPredFlag | HiddenPredFlag);
2016-10-16 23:18:51 +01:00
Yap_InitCPred("line_position", 2, line_position,
2015-09-21 23:05:36 +01:00
SafePredFlag | SyncPredFlag | HiddenPredFlag);
2016-10-16 23:18:51 +01:00
Yap_InitCPred("character_count", 2, character_count,
2015-09-21 23:05:36 +01:00
SafePredFlag | SyncPredFlag | HiddenPredFlag);
Yap_InitCPred("$show_stream_flags", 2, p_show_stream_flags,
SafePredFlag | SyncPredFlag | HiddenPredFlag);
Yap_InitCPred("$user_file_name", 2, p_user_file_name,
SafePredFlag | SyncPredFlag),
Yap_InitCPred("$file_name", 2, p_file_name, SafePredFlag | SyncPredFlag),
Yap_InitCPred("current_input", 1, current_input,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("current_output", 1, current_output,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("set_input", 1, set_input, SafePredFlag | SyncPredFlag);
Yap_InitCPred("set_output", 1, set_output, SafePredFlag | SyncPredFlag);
Yap_InitCPred("$stream", 1, p_stream, SafePredFlag | TestPredFlag);
2017-03-02 22:01:32 +00:00
Yap_InitCPred("$clear_input", 1, clear_input, SafePredFlag | TestPredFlag);
2017-03-03 11:21:29 +00:00
2015-06-18 01:39:03 +01:00
#if HAVE_SELECT
2015-09-21 23:05:36 +01:00
Yap_InitCPred("stream_select", 3, p_stream_select,
SafePredFlag | SyncPredFlag);
2015-06-18 01:39:03 +01:00
#endif
2016-01-04 15:12:44 +00:00
Yap_InitCPred("line_count", 2, line_count, SafePredFlag | SyncPredFlag);
2015-09-21 23:05:36 +01:00
Yap_InitCPred("$show_stream_position", 2, p_show_stream_position,
SafePredFlag | SyncPredFlag | HiddenPredFlag);
Yap_InitCPred("set_stream_position", 2, set_stream_position,
SafePredFlag | SyncPredFlag);
Yap_InitCPred("set_stream", 2, set_stream, SafePredFlag | SyncPredFlag);
}