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