2015-06-18 01:39:03 +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 *
|
|
|
|
* *
|
|
|
|
*************************************************************************/
|
|
|
|
#ifdef SCCS
|
|
|
|
static char SccsId[] = "%W% %G%";
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
#include "Yatom.h"
|
|
|
|
#include "YapHeap.h"
|
|
|
|
#include "yapio.h"
|
|
|
|
#include "eval.h"
|
|
|
|
#include "YapText.h"
|
|
|
|
#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
|
|
|
|
#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
|
|
|
|
#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);
|
|
|
|
UNLOCK(GLOBAL_StreamDescLock);
|
2015-10-08 02:23:45 +01:00
|
|
|
GLOBAL_Stream[sno].encoding = LOCAL_encoding;
|
2015-12-15 09:14:15 +00:00
|
|
|
GLOBAL_Stream[sno].och = '\0';
|
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
|
|
|
|
2015-11-05 16:52:22 +00:00
|
|
|
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) */
|
|
|
|
int sno = Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f |
|
|
|
|
Append_Stream_f | Socket_Stream_f,
|
|
|
|
"check_stream/1");
|
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 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;
|
|
|
|
}
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
static Int is_output(int sno
|
|
|
|
USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
|
|
|
|
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) */
|
2015-06-18 01:39:03 +01:00
|
|
|
bool rc = GLOBAL_Stream[sno].status & Seekable_Stream_f;
|
2016-01-31 10:39:15 +00:00
|
|
|
if (!IsVarTerm(t2) && !booleanFlag(t2)) {
|
2015-06-18 01:39:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
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;
|
2016-01-31 10:39:15 +00:00
|
|
|
if (!IsVarTerm(t2) && !booleanFlag(t2)) {
|
2015-06-18 01:39:03 +01:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
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-09-21 23:05:36 +01:00
|
|
|
char *Yap_guessFileName(int f, int sno, char *nameb, size_t max) {
|
2015-06-18 01:39:03 +01:00
|
|
|
#if __linux__
|
|
|
|
char path[256];
|
2015-10-20 08:01:20 +01:00
|
|
|
if (snprintf(path, 255, "/proc/self/fd/%d", f) && readlink(path, nameb, max))
|
2015-06-18 01:39:03 +01:00
|
|
|
return nameb;
|
|
|
|
#elif __APPLE__
|
2015-09-21 23:05:36 +01:00
|
|
|
if (fcntl(f, F_GETPATH, nameb) != -1) {
|
|
|
|
return nameb;
|
|
|
|
}
|
2015-06-18 01:39:03 +01:00
|
|
|
#elif __WIN32_
|
2015-09-21 23:05:36 +01:00
|
|
|
FILE_NAME_INFO *fni = (FILE_NAME_INFO *)malloc(sizeof(FILE_NAME_INFO) +
|
|
|
|
sizeof(WCHAR) * MAXPATHLEN);
|
2015-06-18 01:39:03 +01:00
|
|
|
HANDLE handle = (HANDLE)_get_osfhandle(f);
|
2015-09-21 23:05:36 +01:00
|
|
|
if (GetFileInformationByHandleEx(handle, FileNameInfo, &fni, max)) {
|
|
|
|
int i;
|
|
|
|
char *ptr = nameb;
|
|
|
|
for (i = 0; i < fni->FileNameLength; i++)
|
|
|
|
*ptr = _PL__utf8_put_char(ptr, fni->FileName[i]);
|
|
|
|
*ptr = '\0';
|
|
|
|
return nameb;
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2016-02-18 12:10:58 +00:00
|
|
|
|
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));
|
|
|
|
return Yap_unify(t2, MkAtomTerm(AtomNot));
|
2015-06-18 01:39:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2015-09-21 23:05:36 +01:00
|
|
|
stream_mode(int sno,
|
|
|
|
Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
|
|
|
|
stream_flags_t flags = GLOBAL_Stream[sno].status &
|
|
|
|
(Input_Stream_f | Output_Stream_f | Append_Stream_f);
|
2015-06-18 01:39:03 +01:00
|
|
|
if (!IsVarTerm(t2) && !(isatom(t2))) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
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 & Output_Stream_f)
|
2015-09-21 23:05:36 +01:00
|
|
|
return Yap_unify(t2, TermWrite);
|
2015-06-18 01:39:03 +01:00
|
|
|
if (flags & Append_Stream_f)
|
2015-09-21 23:05:36 +01:00
|
|
|
return Yap_unify(t2, TermAppend);
|
2015-06-18 01:39:03 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) */
|
2015-09-21 23:05:36 +01:00
|
|
|
Term tout = StreamPosition(GLOBAL_Stream[sno].linecount);
|
|
|
|
return Yap_unify(t2, MkIntegerTerm(tout));
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:12:44 +00:00
|
|
|
static bool stream_line_number(
|
|
|
|
int sno, Term t2 USES_REGS) { /* '$set_output'(+Stream,-ErrorMessage) */
|
2015-09-21 23:05:36 +01:00
|
|
|
Term tout = StreamPosition(GLOBAL_Stream[sno].linecount);
|
|
|
|
return Yap_unify(t2, MkIntegerTerm(tout));
|
|
|
|
}
|
|
|
|
|
|
|
|
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-01-04 15:12:44 +00: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) {
|
|
|
|
case STREAM_PROPERTY_ALIAS:
|
2015-09-21 23:05:36 +01:00
|
|
|
rc = rc & Yap_FetchStreamAlias(
|
|
|
|
sno, args[STREAM_PROPERTY_ALIAS].tvalue 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:
|
2015-09-21 23:05:36 +01:00
|
|
|
rc = rc &&
|
|
|
|
has_reposition(sno, args[STREAM_PROPERTY_MODE].tvalue PASS_REGS);
|
|
|
|
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
|
|
|
}
|
2015-10-08 02:23:45 +01:00
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
2015-06-18 01:39:03 +01:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
static Int cont_stream_property(USES_REGS1) { /* current_stream */
|
2015-07-23 01:33:30 +01:00
|
|
|
bool det;
|
|
|
|
xarg *args;
|
2015-09-21 23:05:36 +01:00
|
|
|
int i = IntOfTerm(EXTRA_CBACK_ARG(2, 1));
|
2015-07-23 01:33:30 +01:00
|
|
|
bool rc;
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
args = Yap_ArgListToVector(Deref(ARG2), stream_property_defs,
|
|
|
|
STREAM_PROPERTY_END);
|
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_PROLOG_FLAG)
|
|
|
|
LOCAL_Error_TYPE = DOMAIN_ERROR_STREAM_PROPERTY_OPTION;
|
|
|
|
Yap_Error( LOCAL_Error_TYPE, LOCAL_Error_Term, NULL );
|
|
|
|
}
|
2015-07-23 01:33:30 +01:00
|
|
|
cut_fail();
|
|
|
|
}
|
|
|
|
LOCK(GLOBAL_StreamDescLock);
|
|
|
|
if (args[STREAM_PROPERTY_ALIAS].tvalue &&
|
|
|
|
IsAtomTerm(args[STREAM_PROPERTY_ALIAS].tvalue)) {
|
|
|
|
// one solution only
|
|
|
|
det = true;
|
|
|
|
i = Yap_CheckAlias(AtomOfTerm(args[STREAM_PROPERTY_ALIAS].tvalue));
|
|
|
|
UNLOCK(GLOBAL_StreamDescLock);
|
|
|
|
if (i < 0) {
|
|
|
|
cut_fail();
|
2015-06-18 01:39:03 +01:00
|
|
|
}
|
2015-07-23 01:33:30 +01:00
|
|
|
rc = true;
|
|
|
|
} else {
|
|
|
|
while (GLOBAL_Stream[i].status & Free_Stream_f) {
|
|
|
|
++i;
|
2015-10-05 10:37:23 +01:00
|
|
|
if (i == MaxStreams) {
|
2016-01-04 15:12:44 +00:00
|
|
|
UNLOCK(GLOBAL_StreamDescLock);
|
|
|
|
cut_fail();
|
2015-10-05 10:37:23 +01:00
|
|
|
}
|
2015-07-06 12:03:16 +01:00
|
|
|
}
|
2015-07-23 01:33:30 +01:00
|
|
|
LOCK(GLOBAL_Stream[i].streamlock);
|
|
|
|
UNLOCK(GLOBAL_StreamDescLock);
|
|
|
|
rc = do_stream_property(i, args PASS_REGS);
|
|
|
|
UNLOCK(GLOBAL_Stream[i].streamlock);
|
2015-06-18 01:39:03 +01:00
|
|
|
}
|
2015-07-23 01:33:30 +01:00
|
|
|
if (rc)
|
2015-09-21 23:05:36 +01:00
|
|
|
rc = Yap_unify(ARG1, Yap_MkStream(i));
|
2015-07-23 01:33:30 +01:00
|
|
|
if (rc) {
|
2015-09-21 23:05:36 +01:00
|
|
|
if (det)
|
|
|
|
cut_succeed();
|
|
|
|
else {
|
|
|
|
EXTRA_CBACK_ARG(2, 1) = MkIntTerm(i + 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (det)
|
|
|
|
cut_fail();
|
|
|
|
else {
|
|
|
|
EXTRA_CBACK_ARG(2, 1) = MkIntTerm(i + 1);
|
|
|
|
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);
|
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);
|
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");
|
2015-07-23 01:33:30 +01:00
|
|
|
if (i < 0) {
|
2015-10-08 02:23:45 +01:00
|
|
|
UNLOCK(GLOBAL_Stream[i].streamlock);
|
2016-01-04 15:12:44 +00:00
|
|
|
cut_fail();
|
2015-07-23 01:33:30 +01:00
|
|
|
}
|
2015-09-21 23:05:36 +01:00
|
|
|
args = Yap_ArgListToVector(Deref(ARG2), stream_property_defs,
|
2016-02-11 14:17:30 +00:00
|
|
|
STREAM_PROPERTY_END);
|
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_PROLOG_FLAG)
|
|
|
|
LOCAL_Error_TYPE = DOMAIN_ERROR_STREAM_PROPERTY_OPTION;
|
|
|
|
Yap_Error( LOCAL_Error_TYPE, LOCAL_Error_Term, NULL );
|
|
|
|
}
|
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);
|
|
|
|
cut_succeed();
|
2015-07-23 01:33:30 +01:00
|
|
|
} else {
|
|
|
|
UNLOCK(GLOBAL_Stream[i].streamlock);
|
|
|
|
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() \
|
|
|
|
PAR("alias", isatom, SET_STREAM_ALIAS), \
|
2016-01-31 10:39:15 +00:00
|
|
|
PAR("buffer", booleanFlag, SET_STREAM_BUFFER), \
|
2015-09-21 23:05:36 +01:00
|
|
|
PAR("buffer_size", nat, SET_STREAM_BUFFER_SIZE), \
|
2016-01-31 10:39:15 +00:00
|
|
|
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;
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
args = Yap_ArgListToVector(opts, set_stream_defs, SET_STREAM_END);
|
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_PROLOG_FLAG)
|
|
|
|
LOCAL_Error_TYPE = DOMAIN_ERROR_SET_STREAM_OPTION;
|
|
|
|
Yap_Error( LOCAL_Error_TYPE, LOCAL_Error_Term, NULL );
|
|
|
|
}
|
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;
|
2015-06-18 01:39:03 +01:00
|
|
|
case SET_STREAM_ENCODING:
|
2016-02-18 12:10:58 +00:00
|
|
|
{
|
|
|
|
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 ) );
|
|
|
|
}
|
2015-09-21 23:05:36 +01:00
|
|
|
break;
|
|
|
|
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) {
|
2016-01-04 14:11:09 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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
|
|
|
*/
|
2015-09-21 23:05:36 +01:00
|
|
|
void Yap_CloseStreams(int loud) {
|
|
|
|
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;
|
|
|
|
if ((GLOBAL_Stream[sno].status & Popen_Stream_f))
|
2015-09-21 23:05:36 +01:00
|
|
|
pclose(GLOBAL_Stream[sno].file);
|
|
|
|
if (GLOBAL_Stream[sno].status & (Pipe_Stream_f | Socket_Stream_f))
|
|
|
|
close(GLOBAL_Stream[sno].u.pipe.fd);
|
2016-01-31 10:39:15 +00:00
|
|
|
#if USE_SOCKET
|
2015-06-18 01:39:03 +01:00
|
|
|
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);
|
2016-01-31 10:39:15 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
else if (GLOBAL_Stream[sno].status & InMemory_Stream_f) {
|
2015-06-18 01:39:03 +01:00
|
|
|
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) {
|
2015-09-21 23:05:36 +01:00
|
|
|
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
|
|
|
|
} else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
|
|
|
|
free(GLOBAL_Stream[sno].u.mem_string.buf);
|
2015-06-18 01:39:03 +01:00
|
|
|
}
|
|
|
|
} else if (!(GLOBAL_Stream[sno].status & Null_Stream_f)) {
|
2015-09-21 23:05:36 +01:00
|
|
|
fclose(GLOBAL_Stream[sno].file);
|
2015-06-18 01:39:03 +01:00
|
|
|
} else {
|
|
|
|
if (loud)
|
2015-09-21 23:05:36 +01:00
|
|
|
fprintf(Yap_stderr, "%% YAP Error: while closing stream: %s\n",
|
|
|
|
RepAtom(GLOBAL_Stream[sno].name)->StrOfAE);
|
2015-06-18 01:39:03 +01:00
|
|
|
}
|
|
|
|
if (LOCAL_c_input_stream == sno) {
|
|
|
|
LOCAL_c_input_stream = StdInStream;
|
|
|
|
} else if (LOCAL_c_output_stream == sno) {
|
|
|
|
LOCAL_c_output_stream = StdOutStream;
|
|
|
|
}
|
|
|
|
GLOBAL_Stream[sno].status = Free_Stream_f;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-04 15:12:44 +00:00
|
|
|
static void CloseStream(int sno) {
|
2015-09-21 23:05:36 +01:00
|
|
|
CACHE_REGS
|
2015-11-05 16:52:22 +00:00
|
|
|
|
2016-01-04 15:12:44 +00:00
|
|
|
fflush(NULL);
|
|
|
|
if (!(GLOBAL_Stream[sno].status &
|
2015-09-21 23:05:36 +01:00
|
|
|
(Null_Stream_f | Socket_Stream_f | InMemory_Stream_f | Pipe_Stream_f)))
|
|
|
|
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-01-31 10:39:15 +00:00
|
|
|
Yap_CloseMemoryStream( sno );
|
2015-06-18 01:39:03 +01:00
|
|
|
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE)
|
|
|
|
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
|
2015-09-21 23:05:36 +01:00
|
|
|
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
|
2015-06-18 01:39:03 +01:00
|
|
|
free(GLOBAL_Stream[sno].u.mem_string.buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GLOBAL_Stream[sno].status = Free_Stream_f;
|
|
|
|
Yap_DeleteAliases(sno);
|
2015-09-21 23:05:36 +01:00
|
|
|
if (LOCAL_c_input_stream == sno) {
|
|
|
|
LOCAL_c_input_stream = StdInStream;
|
|
|
|
} else if (LOCAL_c_output_stream == sno) {
|
|
|
|
LOCAL_c_output_stream = StdOutStream;
|
|
|
|
} else if (LOCAL_c_error_stream == sno) {
|
|
|
|
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
|
|
|
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;
|
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;
|
|
|
|
} else if (LOCAL_c_output_stream == sno) {
|
|
|
|
LOCAL_c_output_stream = StdOutStream;
|
|
|
|
} else if (LOCAL_c_error_stream == sno) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) */
|
|
|
|
int sno = Yap_CheckStream(ARG1, Input_Stream_f, "set_input/1");
|
2015-06-18 01:39:03 +01:00
|
|
|
if (sno < 0)
|
|
|
|
return false;
|
|
|
|
LOCAL_c_input_stream = sno;
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) */
|
2015-06-18 01:39:03 +01:00
|
|
|
int sno =
|
2015-09-21 23:05:36 +01:00
|
|
|
Yap_CheckStream(ARG1, Output_Stream_f | Append_Stream_f, "set_output/2");
|
2015-06-18 01:39:03 +01:00
|
|
|
if (sno < 0)
|
|
|
|
return false;
|
|
|
|
LOCAL_c_output_stream = sno;
|
|
|
|
UNLOCK(GLOBAL_Stream[sno].streamlock);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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 =
|
|
|
|
Yap_CheckStream(ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,
|
|
|
|
"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);
|
2015-06-18 01:39:03 +01:00
|
|
|
/* one has to be somewhat more careful because of terminals */
|
2015-09-21 23:05:36 +01:00
|
|
|
if (GLOBAL_Stream[sno].status & Tty_Stream_f) {
|
|
|
|
Int no = 1;
|
|
|
|
int i;
|
|
|
|
Atom my_stream;
|
2015-06-18 01:39:03 +01:00
|
|
|
#if HAVE_SOCKET
|
2015-09-21 23:05:36 +01:00
|
|
|
if (GLOBAL_Stream[sno].status & Socket_Stream_f)
|
|
|
|
my_stream = AtomSocket;
|
|
|
|
else
|
2015-06-18 01:39:03 +01:00
|
|
|
#endif
|
2015-09-21 23:05:36 +01:00
|
|
|
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++) {
|
|
|
|
if (!(GLOBAL_Stream[i].status & (Free_Stream_f | Socket_Stream_f |
|
|
|
|
Pipe_Stream_f | InMemory_Stream_f)) &&
|
|
|
|
GLOBAL_Stream[i].name == my_stream)
|
|
|
|
no += GLOBAL_Stream[i].linecount - 1;
|
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].linecount);
|
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_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
|
|
|
}
|
|
|
|
|
2015-09-21 23:05:36 +01:00
|
|
|
static Int p_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) {
|
|
|
|
return (FALSE);
|
|
|
|
}
|
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;
|
|
|
|
GLOBAL_Stream[sno].stream_gets = PlGetsFunc();
|
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
|
|
|
}
|
2015-09-21 23:05:36 +01:00
|
|
|
if (fseek(GLOBAL_Stream[sno].file, 0L, SEEK_END) == -1) {
|
|
|
|
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;
|
|
|
|
GLOBAL_Stream[sno].stream_gets = PlGetsFunc();
|
|
|
|
/* reset the counters */
|
|
|
|
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 (void)
|
|
|
|
{
|
2015-09-21 23:05:36 +01:00
|
|
|
Yap_InitCPredBack("stream_property", 2, 1, stream_property,
|
|
|
|
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) {
|
2015-11-05 16:52:22 +00:00
|
|
|
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);
|
|
|
|
Yap_InitCPred("$line_position", 2, p_line_position,
|
|
|
|
SafePredFlag | SyncPredFlag | HiddenPredFlag);
|
|
|
|
Yap_InitCPred("$character_count", 2, p_character_count,
|
|
|
|
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);
|
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);
|
|
|
|
}
|