fix eof handling + indent

This commit is contained in:
Vítor Santos Costa 2016-02-26 17:45:45 +00:00
parent 62e0f0bece
commit 54985f4678

View File

@ -96,7 +96,6 @@ static char SccsId[] = "%W% %G%";
#endif #endif
#include "iopreds.h" #include "iopreds.h"
#define GETW get_wchar_from_FILE #define GETW get_wchar_from_FILE
#define GETC() fgetwc(st->file) #define GETC() fgetwc(st->file)
#include "getw.h" #include "getw.h"
@ -269,7 +268,6 @@ static void unix_upd_stream_info(StreamDesc *s) {
s->status |= Seekable_Stream_f; s->status |= Seekable_Stream_f;
} }
static void InitFileIO(StreamDesc *s) { static void InitFileIO(StreamDesc *s) {
CACHE_REGS CACHE_REGS
if (s->status & Socket_Stream_f) { if (s->status & Socket_Stream_f) {
@ -663,7 +661,6 @@ int post_process_read_wchar(int ch, ssize_t n, StreamDesc *s) {
return ch; return ch;
} }
int post_process_weof(StreamDesc *s) { int post_process_weof(StreamDesc *s) {
if (!ResetEOF(s)) { if (!ResetEOF(s)) {
s->status |= Eof_Stream_f; s->status |= Eof_Stream_f;
@ -695,19 +692,18 @@ int PlGetc(int sno) {
return fgetc(s->file); return fgetc(s->file);
} }
// layered version
static int get_wchar__(int sno) { return fgetwc(GLOBAL_Stream[sno].file); }
// layered version static int get_wchar_from_file(int sno) {
static int get_wchar__(int sno) { return fgetwc(GLOBAL_Stream[sno].file); }
static int get_wchar_from_file(int sno) {
return post_process_read_wchar(get_wchar__(sno), 1, GLOBAL_Stream + sno); return post_process_read_wchar(get_wchar__(sno), 1, GLOBAL_Stream + sno);
} }
#ifndef MB_LEN_MAX #ifndef MB_LEN_MAX
#define MB_LEN_MAX 6 #define MB_LEN_MAX 6
#endif #endif
static int handle_write_encoding_error(int sno, wchar_t ch) { static int handle_write_encoding_error(int sno, wchar_t ch) {
if (GLOBAL_Stream[sno].status & RepError_Xml_f) { if (GLOBAL_Stream[sno].status & RepError_Xml_f) {
/* use HTML/XML encoding in ASCII */ /* use HTML/XML encoding in ASCII */
int i = ch, digits = 1; int i = ch, digits = 1;
@ -740,9 +736,9 @@ int PlGetc(int sno) {
(unsigned long int)ch, sno); (unsigned long int)ch, sno);
return -1; return -1;
} }
} }
int put_wchar(int sno, wchar_t ch) { int put_wchar(int sno, wchar_t ch) {
/* pass the bucck if we can */ /* pass the bucck if we can */
switch (GLOBAL_Stream[sno].encoding) { switch (GLOBAL_Stream[sno].encoding) {
case ENC_OCTET: case ENC_OCTET:
@ -796,8 +792,7 @@ int PlGetc(int sno) {
} }
return ch; return ch;
break; break;
case ENC_UTF16_LE: case ENC_UTF16_LE: {
{
if (ch < 0x10000) { if (ch < 0x10000) {
GLOBAL_Stream[sno].stream_putc(sno, (ch & 0xff)); GLOBAL_Stream[sno].stream_putc(sno, (ch & 0xff));
GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8)); GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8));
@ -814,8 +809,7 @@ int PlGetc(int sno) {
} }
return ch; return ch;
} }
case ENC_UTF16_BE: case ENC_UTF16_BE: {
{
// computations // computations
if (ch < 0x10000) { if (ch < 0x10000) {
GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8)); GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8));
@ -828,12 +822,10 @@ int PlGetc(int sno) {
GLOBAL_Stream[sno].stream_putc(sno, (lead & 0xff)); GLOBAL_Stream[sno].stream_putc(sno, (lead & 0xff));
GLOBAL_Stream[sno].stream_putc(sno, (trail >> 8)); GLOBAL_Stream[sno].stream_putc(sno, (trail >> 8));
GLOBAL_Stream[sno].stream_putc(sno, (trail & 0xff)); GLOBAL_Stream[sno].stream_putc(sno, (trail & 0xff));
} }
return ch; return ch;
} }
case ENC_UCS2_LE: case ENC_UCS2_LE: {
{
if (ch >= 0x10000) { if (ch >= 0x10000) {
return 0; return 0;
} }
@ -841,8 +833,7 @@ int PlGetc(int sno) {
GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8)); GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8));
return ch; return ch;
} }
case ENC_UCS2_BE: case ENC_UCS2_BE: {
{
// computations // computations
if (ch < 0x10000) { if (ch < 0x10000) {
GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8)); GLOBAL_Stream[sno].stream_putc(sno, (ch >> 8));
@ -868,34 +859,34 @@ int PlGetc(int sno) {
} }
} }
return -1; return -1;
} }
/* used by user-code to read characters from the current input stream */ /* used by user-code to read characters from the current input stream */
int Yap_PlGetchar(void) { int Yap_PlGetchar(void) {
CACHE_REGS CACHE_REGS
return (GLOBAL_Stream[LOCAL_c_input_stream].stream_getc( return (
LOCAL_c_input_stream)); GLOBAL_Stream[LOCAL_c_input_stream].stream_getc(LOCAL_c_input_stream));
} }
int Yap_PlGetWchar(void) { int Yap_PlGetWchar(void) {
CACHE_REGS CACHE_REGS
return get_wchar(LOCAL_c_input_stream); return get_wchar(LOCAL_c_input_stream);
} }
/* avoid using a variable to call a function */ /* avoid using a variable to call a function */
int Yap_PlFGetchar(void) { int Yap_PlFGetchar(void) {
CACHE_REGS CACHE_REGS
return (PlGetc(LOCAL_c_input_stream)); return (PlGetc(LOCAL_c_input_stream));
} }
Term Yap_MkStream(int n) { Term Yap_MkStream(int n) {
Term t[1]; Term t[1];
t[0] = MkIntTerm(n); t[0] = MkIntTerm(n);
return (Yap_MkApplTerm(FunctorStream, 1, t)); return (Yap_MkApplTerm(FunctorStream, 1, t));
} }
/* given a stream index, get the corresponding fd */ /* given a stream index, get the corresponding fd */
Int GetStreamFd(int sno) { Int GetStreamFd(int sno) {
#if HAVE_SOCKET #if HAVE_SOCKET
if (GLOBAL_Stream[sno].status & Socket_Stream_f) { if (GLOBAL_Stream[sno].status & Socket_Stream_f) {
return (GLOBAL_Stream[sno].u.socket.fd); return (GLOBAL_Stream[sno].u.socket.fd);
@ -907,11 +898,11 @@ int PlGetc(int sno) {
return (-1); return (-1);
} }
return (fileno(GLOBAL_Stream[sno].file)); return (fileno(GLOBAL_Stream[sno].file));
} }
Int Yap_GetStreamFd(int sno) { return GetStreamFd(sno); } Int Yap_GetStreamFd(int sno) { return GetStreamFd(sno); }
static int binary_file(const char *file_name) { static int binary_file(const char *file_name) {
#if HAVE_STAT #if HAVE_STAT
#if _MSC_VER || defined(__MINGW32__) #if _MSC_VER || defined(__MINGW32__)
struct _stat ss; struct _stat ss;
@ -928,9 +919,9 @@ int PlGetc(int sno) {
#else #else
return (FALSE); return (FALSE);
#endif #endif
} }
static int write_bom(int sno, StreamDesc *st) { static int write_bom(int sno, StreamDesc *st) {
/* dump encoding */ /* dump encoding */
switch (st->encoding) { switch (st->encoding) {
case ENC_ISO_UTF8: case ENC_ISO_UTF8:
@ -983,9 +974,9 @@ int PlGetc(int sno) {
default: default:
return true; return true;
} }
} }
static void check_bom(int sno, StreamDesc *st) { static void check_bom(int sno, StreamDesc *st) {
int ch1, ch2, ch3, ch4; int ch1, ch2, ch3, ch4;
ch1 = fgetc(st->file); ch1 = fgetc(st->file);
@ -1079,11 +1070,10 @@ int PlGetc(int sno) {
default: default:
ungetc(ch1, st->file); ungetc(ch1, st->file);
} }
} }
bool Yap_initStream(int sno, FILE *fd, const char *name, Term file_name, bool Yap_initStream(int sno, FILE *fd, const char *name, Term file_name,
encoding_t encoding, stream_flags_t flags, encoding_t encoding, stream_flags_t flags, Atom open_mode) {
Atom open_mode) {
StreamDesc *st = &GLOBAL_Stream[sno]; StreamDesc *st = &GLOBAL_Stream[sno];
st->status = flags; st->status = flags;
@ -1117,9 +1107,9 @@ int PlGetc(int sno) {
Yap_DefaultStreamOps(st); Yap_DefaultStreamOps(st);
} }
return true; return true;
} }
static bool open_header(int sno, Atom open_mode) { static bool open_header(int sno, Atom open_mode) {
if (open_mode == AtomWrite) { if (open_mode == AtomWrite) {
const char *ptr; const char *ptr;
const char s[] = "#!"; const char s[] = "#!";
@ -1146,7 +1136,7 @@ int PlGetc(int sno) {
} }
} }
return true; return true;
} }
#define OPEN_DEFS() \ #define OPEN_DEFS() \
PAR("alias", isatom, OPEN_ALIAS), PAR("bom", booleanFlag, OPEN_BOM), \ PAR("alias", isatom, OPEN_ALIAS), PAR("bom", booleanFlag, OPEN_BOM), \
@ -1165,18 +1155,18 @@ int PlGetc(int sno) {
PAR("wait", booleanFlag, OPEN_WAIT), PAR(NULL, ok, OPEN_END) PAR("wait", booleanFlag, OPEN_WAIT), PAR(NULL, ok, OPEN_END)
#define PAR(x, y, z) z #define PAR(x, y, z) z
typedef enum open_enum_choices { OPEN_DEFS() } open_choices_t; typedef enum open_enum_choices { OPEN_DEFS() } open_choices_t;
#undef PAR #undef PAR
#define PAR(x, y, z) \ #define PAR(x, y, z) \
{ x, y, z } { x, y, z }
static const param_t open_defs[] = {OPEN_DEFS()}; static const param_t open_defs[] = {OPEN_DEFS()};
#undef PAR #undef PAR
static Int do_open( static Int
Term file_name, Term t2, do_open(Term file_name, Term t2,
Term tlist USES_REGS) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */ Term tlist USES_REGS) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
Atom open_mode; Atom open_mode;
int sno; int sno;
@ -1266,7 +1256,7 @@ int PlGetc(int sno) {
s_encoding = "default"; s_encoding = "default";
} }
// default encoding, no bom yet // default encoding, no bom yet
encoding = enc_id( s_encoding, ENC_OCTET); encoding = enc_id(s_encoding, ENC_OCTET);
// only set encoding after getting BOM // only set encoding after getting BOM
bool ok = (args[OPEN_EXPAND_FILENAME].used bool ok = (args[OPEN_EXPAND_FILENAME].used
? args[OPEN_EXPAND_FILENAME].tvalue == TermTrue ? args[OPEN_EXPAND_FILENAME].tvalue == TermTrue
@ -1282,8 +1272,7 @@ int PlGetc(int sno) {
// Skip scripts that start with !#/.. or similar // Skip scripts that start with !#/.. or similar
bool script = bool script =
(args[OPEN_SCRIPT].used ? args[OPEN_SCRIPT].tvalue == TermTrue (args[OPEN_SCRIPT].used ? args[OPEN_SCRIPT].tvalue == TermTrue : false);
: false);
// binary type // binary type
if (args[OPEN_TYPE].used) { if (args[OPEN_TYPE].used) {
Term t = args[OPEN_TYPE].tvalue; Term t = args[OPEN_TYPE].tvalue;
@ -1331,11 +1320,11 @@ int PlGetc(int sno) {
fname = LOCAL_FileNameBuf; fname = LOCAL_FileNameBuf;
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
if (errno == ENOENT) if (errno == ENOENT)
return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name, "%s: %s", return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name, "%s: %s", fname,
fname, strerror(errno))); strerror(errno)));
else { else {
return (PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, file_name, return (PlIOError(PERMISSION_ERROR_OPEN_SOURCE_SINK, file_name, "%s: %s",
"%s: %s", fname, strerror(errno))); fname, strerror(errno)));
} }
} }
#if MAC #if MAC
@ -1344,11 +1333,9 @@ int PlGetc(int sno) {
} }
#endif #endif
flags &= ~(Free_Stream_f); flags &= ~(Free_Stream_f);
if (!Yap_initStream(sno, fd, fname, file_name, encoding, flags, if (!Yap_initStream(sno, fd, fname, file_name, encoding, flags, open_mode))
open_mode))
return false; return false;
if (!Yap_initStream(sno, fd, fname, file_name, encoding, flags, if (!Yap_initStream(sno, fd, fname, file_name, encoding, flags, open_mode))
open_mode))
return false; return false;
if (open_mode == AtomWrite) { if (open_mode == AtomWrite) {
if (needs_bom && !write_bom(sno, st)) if (needs_bom && !write_bom(sno, st))
@ -1358,10 +1345,10 @@ int PlGetc(int sno) {
} }
// follow declaration unless there is v // follow declaration unless there is v
if (st->status & HAS_BOM_f) if (st->status & HAS_BOM_f)
st->encoding = enc_id( s_encoding, st->encoding); st->encoding = enc_id(s_encoding, st->encoding);
else else
st->encoding = encoding; st->encoding = encoding;
Yap_DefaultStreamOps( st); Yap_DefaultStreamOps(st);
if (script) if (script)
open_header(sno, open_mode); open_header(sno, open_mode);
@ -1370,102 +1357,101 @@ int PlGetc(int sno) {
Term t = Yap_MkStream(sno); Term t = Yap_MkStream(sno);
return (Yap_unify(ARG3, t)); return (Yap_unify(ARG3, t));
} }
} }
/** @pred open(+ _F_,+ _M_,- _S_) is iso /** @pred open(+ _F_,+ _M_,- _S_) is iso
Opens the file with name _F_ in mode _M_ (`read`, `write` or Opens the file with name _F_ in mode _M_ (`read`, `write` or
`append`), returning _S_ unified with the stream name. `append`), returning _S_ unified with the stream name.
Yap allows 64 streams opened at the same time. If you need more, Yap allows 64 streams opened at the same time. If you need more,
redefine the MaxStreams constant. Each stream is either an input or redefine the MaxStreams constant. Each stream is either an input or
an output stream but not both. There are always 3 open streams: an output stream but not both. There are always 3 open streams:
user_input for reading, user_output for writing and user_error for user_input for reading, user_output for writing and user_error for
writing. If there is no ambiguity, the atoms user_input and writing. If there is no ambiguity, the atoms user_input and
user_output may be referred to as `user`. user_output may be referred to as `user`.
The `file_errors` flag controls whether errors are reported when in The `file_errors` flag controls whether errors are reported when in
mode `read` or `append` the file _F_ does not exist or is not mode `read` or `append` the file _F_ does not exist or is not
readable, and whether in mode `write` or `append` the file is not readable, and whether in mode `write` or `append` the file is not
writable. writable.
*/ */
static Int open3( static Int open3(USES_REGS1) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
USES_REGS1) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
return do_open(Deref(ARG1), Deref(ARG2), TermNil PASS_REGS); return do_open(Deref(ARG1), Deref(ARG2), TermNil PASS_REGS);
} }
/** @pred open(+ _F_,+ _M_,- _S_,+ _Opts_) is iso /** @pred open(+ _F_,+ _M_,- _S_,+ _Opts_) is iso
Opens the file with name _F_ in mode _M_ (`read`, `write` or Opens the file with name _F_ in mode _M_ (`read`, `write` or
`append`), returning _S_ unified with the stream name, and following `append`), returning _S_ unified with the stream name, and following
these options: these options:
+ `type(+ _T_)` is iso + `type(+ _T_)` is iso
Specify whether the stream is a `text` stream (default), or a Specify whether the stream is a `text` stream (default), or a
`binary` stream. `binary` stream.
+ `reposition(+ _Bool_)` is iso + `reposition(+ _Bool_)` is iso
Specify whether it is possible to reposition the stream (`true`), or Specify whether it is possible to reposition the stream (`true`), or
not (`false`). By default, YAP enables repositioning for all not (`false`). By default, YAP enables repositioning for all
files, except terminal files and sockets. files, except terminal files and sockets.
+ `eof(+ _Action_)` is iso + `eof(+ _Action_)` is iso
Specify the action to take if attempting to input characters from a Specify the action to take if attempting to input characters from a
stream where we have previously found an `end_of_file`. The possible stream where we have previously found an `end_of_file`. The possible
actions are `error`, that raises an error, `reset`, that tries to actions are `error`, that raises an error, `reset`, that tries to
reset the stream and is used for `tty` type files, and `eof_code`, reset the stream and is used for `tty` type files, and `eof_code`,
which generates a new `end_of_file` (default for non-tty files). which generates a new `end_of_file` (default for non-tty files).
+ `alias(+ _Name_)` is iso + `alias(+ _Name_)` is iso
Specify an alias to the stream. The alias <tt>Name</tt> must be an atom. Specify an alias to the stream. The alias <tt>Name</tt> must be an atom.
The The
alias can be used instead of the stream descriptor for every operation alias can be used instead of the stream descriptor for every operation
concerning the stream. concerning the stream.
The operation will fail and give an error if the alias name is already The operation will fail and give an error if the alias name is already
in use. YAP allows several aliases for the same file, but only in use. YAP allows several aliases for the same file, but only
one is returned by stream_property/2 one is returned by stream_property/2
+ `bom(+ _Bool_)` + `bom(+ _Bool_)`
If present and `true`, a BOM (<em>Byte Order Mark</em>) was If present and `true`, a BOM (<em>Byte Order Mark</em>) was
detected while opening the file for reading or a BOM was written while detected while opening the file for reading or a BOM was written while
opening the stream. See BOM for details. opening the stream. See BOM for details.
+ `encoding(+ _Encoding_)` + `encoding(+ _Encoding_)`
Set the encoding used for text. See Encoding for an overview of Set the encoding used for text. See Encoding for an overview of
wide character and encoding issues. wide character and encoding issues.
+ `representation_errors(+ _Mode_)` + `representation_errors(+ _Mode_)`
Change the behaviour when writing characters to the stream that cannot Change the behaviour when writing characters to the stream that cannot
be represented by the encoding. The behaviour is one of `error` be represented by the encoding. The behaviour is one of `error`
(throw and Input/Output error exception), `prolog` (write `\u...\` (throw and Input/Output error exception), `prolog` (write `\u...\`
escape code or `xml` (write `\&#...;` XML character entity). escape code or `xml` (write `\&#...;` XML character entity).
The initial mode is `prolog` for the user streams and The initial mode is `prolog` for the user streams and
`error` for all other streams. See also Encoding. `error` for all other streams. See also Encoding.
+ `expand_filename(+ _Mode_)` + `expand_filename(+ _Mode_)`
If _Mode_ is `true` then do filename expansion, then ask Prolog If _Mode_ is `true` then do filename expansion, then ask Prolog
to do file name expansion before actually trying to opening the file: to do file name expansion before actually trying to opening the file:
this includes processing `~` characters and processing `$` this includes processing `~` characters and processing `$`
environment variables at the beginning of the file. Otherwise, just try environment variables at the beginning of the file. Otherwise, just try
to open the file using the given name. to open the file using the given name.
The default behavior is given by the Prolog flag The default behavior is given by the Prolog flag
open_expands_filename. open_expands_filename.
+ `script( + _Boolean_ )` YAP extension. + `script( + _Boolean_ )` YAP extension.
The file may be a Prolog script. In `read` mode just check for The file may be a Prolog script. In `read` mode just check for
initial lines if they start with the hash symbol, and skip them. In initial lines if they start with the hash symbol, and skip them. In
@ -1474,14 +1460,12 @@ int PlGetc(int sno) {
permissions as executable. In `append` mode ignore the flag. permissions as executable. In `append` mode ignore the flag.
*/ */
static Int open4( static Int open4(USES_REGS1) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
USES_REGS1) { /* '$open'(+File,+Mode,?Stream,-ReturnCode) */
return do_open(Deref(ARG1), Deref(ARG2), Deref(ARG4) PASS_REGS); return do_open(Deref(ARG1), Deref(ARG2), Deref(ARG4) PASS_REGS);
} }
static Int p_file_expansion( static Int p_file_expansion(USES_REGS1) { /* '$file_expansion'(+File,-Name) */
USES_REGS1) { /* '$file_expansion'(+File,-Name) */
Term file_name = Deref(ARG1); Term file_name = Deref(ARG1);
/* we know file_name is bound */ /* we know file_name is bound */
@ -1494,9 +1478,9 @@ int PlGetc(int sno) {
return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name, return (PlIOError(EXISTENCE_ERROR_SOURCE_SINK, file_name,
"absolute_file_name/3")); "absolute_file_name/3"));
return (Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf)))); return (Yap_unify(ARG2, MkAtomTerm(Yap_LookupAtom(LOCAL_FileNameBuf))));
} }
static Int p_open_null_stream(USES_REGS1) { static Int p_open_null_stream(USES_REGS1) {
Term t; Term t;
StreamDesc *st; StreamDesc *st;
int sno = GetFreeStreamD(); int sno = GetFreeStreamD();
@ -1527,9 +1511,9 @@ int PlGetc(int sno) {
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
t = Yap_MkStream(sno); t = Yap_MkStream(sno);
return (Yap_unify(ARG1, t)); return (Yap_unify(ARG1, t));
} }
int Yap_OpenStream(FILE * fd, char *name, Term file_name, int flags) { int Yap_OpenStream(FILE *fd, char *name, Term file_name, int flags) {
CACHE_REGS CACHE_REGS
int sno; int sno;
Atom at; Atom at;
@ -1547,13 +1531,13 @@ int PlGetc(int sno) {
at = AtomRead; at = AtomRead;
Yap_initStream(sno, fd, name, file_name, LOCAL_encoding, flags, at); Yap_initStream(sno, fd, name, file_name, LOCAL_encoding, flags, at);
return sno; return sno;
} }
#define CheckStream(arg, kind, msg) \ #define CheckStream(arg, kind, msg) \
CheckStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg) CheckStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
static int CheckStream__(const char *file, const char *f, int line, static int CheckStream__(const char *file, const char *f, int line, Term arg,
Term arg, int kind, const char *msg) { int kind, const char *msg) {
int sno = -1; int sno = -1;
arg = Deref(arg); arg = Deref(arg);
if (IsVarTerm(arg)) { if (IsVarTerm(arg)) {
@ -1608,15 +1592,15 @@ int PlGetc(int sno) {
PlIOError__(file, f, line, PERMISSION_ERROR_INPUT_STREAM, arg, msg); PlIOError__(file, f, line, PERMISSION_ERROR_INPUT_STREAM, arg, msg);
} }
return (sno); return (sno);
} }
int Yap_CheckStream__(const char *file, const char *f, int line, Term arg, int Yap_CheckStream__(const char *file, const char *f, int line, Term arg,
int kind, const char *msg) { int kind, const char *msg) {
return CheckStream__(file, f, line, arg, kind, msg); return CheckStream__(file, f, line, arg, kind, msg);
} }
int Yap_CheckTextStream__(const char *file, const char *f, int line, int Yap_CheckTextStream__(const char *file, const char *f, int line, Term arg,
Term arg, int kind, const char *msg) { int kind, const char *msg) {
int sno; int sno;
if ((sno = CheckStream__(file, f, line, arg, kind, msg)) < 0) if ((sno = CheckStream__(file, f, line, arg, kind, msg)) < 0)
return -1; return -1;
@ -1631,10 +1615,10 @@ int PlGetc(int sno) {
return -1; return -1;
} }
return sno; return sno;
} }
/* used from C-interface */ /* used from C-interface */
int Yap_GetFreeStreamDForReading(void) { int Yap_GetFreeStreamDForReading(void) {
int sno = GetFreeStreamD(); int sno = GetFreeStreamD();
StreamDesc *s; StreamDesc *s;
@ -1648,16 +1632,16 @@ int PlGetc(int sno) {
Yap_DefaultStreamOps(s); Yap_DefaultStreamOps(s);
UNLOCK(s->streamlock); UNLOCK(s->streamlock);
return sno; return sno;
} }
/** /**
* @pred always_prompt_user * @pred always_prompt_user
* *
* Ensure that the stream always prompts before asking the standard input * Ensure that the stream always prompts before asking the standard input
stream for data. stream for data.
*/ */
static Int always_prompt_user(USES_REGS1) { static Int always_prompt_user(USES_REGS1) {
StreamDesc *s = GLOBAL_Stream + StdInStream; StreamDesc *s = GLOBAL_Stream + StdInStream;
s->status |= Promptable_Stream_f; s->status |= Promptable_Stream_f;
@ -1671,9 +1655,9 @@ int PlGetc(int sno) {
} else } else
Yap_ConsoleOps(s, false); Yap_ConsoleOps(s, false);
return (TRUE); return (TRUE);
} }
static Int close1 /** @pred close(+ _S_) is iso static Int close1 /** @pred close(+ _S_) is iso
Closes the stream _S_. If _S_ does not stand for a stream Closes the stream _S_. If _S_ does not stand for a stream
@ -1685,8 +1669,7 @@ int PlGetc(int sno) {
(USES_REGS1) { /* '$close'(+GLOBAL_Stream) */ (USES_REGS1) { /* '$close'(+GLOBAL_Stream) */
Int sno = CheckStream( Int sno = CheckStream(
ARG1, (Input_Stream_f | Output_Stream_f | Socket_Stream_f), ARG1, (Input_Stream_f | Output_Stream_f | Socket_Stream_f), "close/2");
"close/2");
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
if (sno <= StdErrStream) { if (sno <= StdErrStream) {
@ -1696,36 +1679,35 @@ int PlGetc(int sno) {
Yap_CloseStream(sno); Yap_CloseStream(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return (TRUE); return (TRUE);
} }
#define CLOSE_DEFS() \ #define CLOSE_DEFS() \
PAR("force", booleanFlag, CLOSE_FORCE), PAR(NULL, ok, CLOSE_END) PAR("force", booleanFlag, CLOSE_FORCE), PAR(NULL, ok, CLOSE_END)
#define PAR(x, y, z) z #define PAR(x, y, z) z
typedef enum close_enum_choices { CLOSE_DEFS() } close_choices_t; typedef enum close_enum_choices { CLOSE_DEFS() } close_choices_t;
#undef PAR #undef PAR
#define PAR(x, y, z) \ #define PAR(x, y, z) \
{ x, y, z } { x, y, z }
static const param_t close_defs[] = {CLOSE_DEFS()}; static const param_t close_defs[] = {CLOSE_DEFS()};
#undef PAR #undef PAR
/** @pred close(+ _S_,+ _O_) is iso /** @pred close(+ _S_,+ _O_) is iso
Closes the stream _S_, following options _O_. Closes the stream _S_, following options _O_.
The only valid options are `force(true)` and `force(false)`. The only valid options are `force(true)` and `force(false)`.
YAP currently ignores these options. YAP currently ignores these options.
*/ */
static Int close2(USES_REGS1) { /* '$close'(+GLOBAL_Stream) */ static Int close2(USES_REGS1) { /* '$close'(+GLOBAL_Stream) */
Int sno = CheckStream( Int sno = CheckStream(
ARG1, (Input_Stream_f | Output_Stream_f | Socket_Stream_f), ARG1, (Input_Stream_f | Output_Stream_f | Socket_Stream_f), "close/2");
"close/2");
Term tlist; Term tlist;
if (sno < 0) if (sno < 0)
return (FALSE); return (FALSE);
@ -1749,9 +1731,9 @@ int PlGetc(int sno) {
Yap_CloseStream(sno); Yap_CloseStream(sno);
UNLOCK(GLOBAL_Stream[sno].streamlock); UNLOCK(GLOBAL_Stream[sno].streamlock);
return (TRUE); return (TRUE);
} }
Term read_line(int sno) { Term read_line(int sno) {
CACHE_REGS CACHE_REGS
Term tail; Term tail;
Int ch; Int ch;
@ -1761,7 +1743,7 @@ int PlGetc(int sno) {
} }
tail = read_line(sno); tail = read_line(sno);
return (MkPairTerm(MkIntTerm(ch), tail)); return (MkPairTerm(MkIntTerm(ch), tail));
} }
#define ABSOLUTE_FILE_NAME_DEFS() \ #define ABSOLUTE_FILE_NAME_DEFS() \
PAR("access", isatom, ABSOLUTE_FILE_NAME_ACCESS), \ PAR("access", isatom, ABSOLUTE_FILE_NAME_ACCESS), \
@ -1778,20 +1760,20 @@ int PlGetc(int sno) {
#define PAR(x, y, z) z #define PAR(x, y, z) z
typedef enum ABSOLUTE_FILE_NAME_enum_ { typedef enum ABSOLUTE_FILE_NAME_enum_ {
ABSOLUTE_FILE_NAME_DEFS() ABSOLUTE_FILE_NAME_DEFS()
} absolute_file_name_choices_t; } absolute_file_name_choices_t;
#undef PAR #undef PAR
#define PAR(x, y, z) \ #define PAR(x, y, z) \
{ x, y, z } { x, y, z }
static const param_t absolute_file_name_search_defs[] = { static const param_t absolute_file_name_search_defs[] = {
ABSOLUTE_FILE_NAME_DEFS()}; ABSOLUTE_FILE_NAME_DEFS()};
#undef PAR #undef PAR
static Int abs_file_parameters(USES_REGS1) { static Int abs_file_parameters(USES_REGS1) {
Term t[ABSOLUTE_FILE_NAME_END]; Term t[ABSOLUTE_FILE_NAME_END];
Term tlist = Deref(ARG1), tf; Term tlist = Deref(ARG1), tf;
/* get options */ /* get options */
@ -1819,8 +1801,7 @@ int PlGetc(int sno) {
t[ABSOLUTE_FILE_NAME_RELATIVE_TO] = gethdir(TermDot); t[ABSOLUTE_FILE_NAME_RELATIVE_TO] = gethdir(TermDot);
} }
if (args[ABSOLUTE_FILE_NAME_FILE_TYPE].used) if (args[ABSOLUTE_FILE_NAME_FILE_TYPE].used)
t[ABSOLUTE_FILE_NAME_FILE_TYPE] = t[ABSOLUTE_FILE_NAME_FILE_TYPE] = args[ABSOLUTE_FILE_NAME_FILE_TYPE].tvalue;
args[ABSOLUTE_FILE_NAME_FILE_TYPE].tvalue;
else else
t[ABSOLUTE_FILE_NAME_FILE_TYPE] = TermTxt; t[ABSOLUTE_FILE_NAME_FILE_TYPE] = TermTxt;
if (args[ABSOLUTE_FILE_NAME_ACCESS].used) if (args[ABSOLUTE_FILE_NAME_ACCESS].used)
@ -1833,8 +1814,7 @@ int PlGetc(int sno) {
else else
t[ABSOLUTE_FILE_NAME_FILE_ERRORS] = TermError; t[ABSOLUTE_FILE_NAME_FILE_ERRORS] = TermError;
if (args[ABSOLUTE_FILE_NAME_SOLUTIONS].used) if (args[ABSOLUTE_FILE_NAME_SOLUTIONS].used)
t[ABSOLUTE_FILE_NAME_SOLUTIONS] = t[ABSOLUTE_FILE_NAME_SOLUTIONS] = args[ABSOLUTE_FILE_NAME_SOLUTIONS].tvalue;
args[ABSOLUTE_FILE_NAME_SOLUTIONS].tvalue;
else else
t[ABSOLUTE_FILE_NAME_SOLUTIONS] = TermFirst; t[ABSOLUTE_FILE_NAME_SOLUTIONS] = TermFirst;
if (args[ABSOLUTE_FILE_NAME_EXPAND].used) if (args[ABSOLUTE_FILE_NAME_EXPAND].used)
@ -1851,14 +1831,13 @@ int PlGetc(int sno) {
args[ABSOLUTE_FILE_NAME_VERBOSE_FILE_SEARCH].tvalue; args[ABSOLUTE_FILE_NAME_VERBOSE_FILE_SEARCH].tvalue;
else else
t[ABSOLUTE_FILE_NAME_VERBOSE_FILE_SEARCH] = t[ABSOLUTE_FILE_NAME_VERBOSE_FILE_SEARCH] =
(trueGlobalPrologFlag(VERBOSE_FILE_SEARCH_FLAG) ? TermTrue (trueGlobalPrologFlag(VERBOSE_FILE_SEARCH_FLAG) ? TermTrue : TermFalse);
: TermFalse);
tf = Yap_MkApplTerm(Yap_MkFunctor(AtomOpt, ABSOLUTE_FILE_NAME_END), tf = Yap_MkApplTerm(Yap_MkFunctor(AtomOpt, ABSOLUTE_FILE_NAME_END),
ABSOLUTE_FILE_NAME_END, t); ABSOLUTE_FILE_NAME_END, t);
return (Yap_unify(ARG2, tf)); return (Yap_unify(ARG2, tf));
} }
static Int get_abs_file_parameter(USES_REGS1) { static Int get_abs_file_parameter(USES_REGS1) {
Term t = Deref(ARG1), topts = ARG2; Term t = Deref(ARG1), topts = ARG2;
/* get options */ /* get options */
/* done */ /* done */
@ -1868,9 +1847,9 @@ int PlGetc(int sno) {
return Yap_unify(ARG3, ArgOfTerm(i + 1, topts)); return Yap_unify(ARG3, ArgOfTerm(i + 1, topts));
Yap_Error(DOMAIN_ERROR_ABSOLUTE_FILE_NAME_OPTION, ARG1, NULL); Yap_Error(DOMAIN_ERROR_ABSOLUTE_FILE_NAME_OPTION, ARG1, NULL);
return false; return false;
} }
void Yap_InitPlIO(void) { void Yap_InitPlIO(void) {
Int i; Int i;
Yap_stdin = stdin; Yap_stdin = stdin;
@ -1883,9 +1862,9 @@ int PlGetc(int sno) {
GLOBAL_Stream[i].status = Free_Stream_f; GLOBAL_Stream[i].status = Free_Stream_f;
} }
InitStdStreams(); InitStdStreams();
} }
void Yap_InitIOPreds(void) { void Yap_InitIOPreds(void) {
/* here the Input/Output predicates */ /* here the Input/Output predicates */
Yap_InitCPred("always_prompt_user", 0, always_prompt_user, Yap_InitCPred("always_prompt_user", 0, always_prompt_user,
SafePredFlag | SyncPredFlag); SafePredFlag | SyncPredFlag);
@ -1920,4 +1899,4 @@ int PlGetc(int sno) {
Yap_InitSignalPreds(); Yap_InitSignalPreds();
Yap_InitSysPreds(); Yap_InitSysPreds();
Yap_InitTimePreds(); Yap_InitTimePreds();
} }