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,7 +692,6 @@ int PlGetc(int sno) {
return fgetc(s->file); return fgetc(s->file);
} }
// layered version // layered version
static int get_wchar__(int sno) { return fgetwc(GLOBAL_Stream[sno].file); } static int get_wchar__(int sno) { return fgetwc(GLOBAL_Stream[sno].file); }
@ -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));
@ -873,8 +864,8 @@ int PlGetc(int sno) {
/* 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) {
@ -1082,8 +1073,7 @@ int PlGetc(int sno) {
} }
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;
@ -1175,8 +1165,8 @@ int PlGetc(int sno) {
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;
@ -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))
@ -1392,8 +1379,7 @@ int PlGetc(int sno) {
*/ */
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);
} }
@ -1475,13 +1461,11 @@ int PlGetc(int sno) {
*/ */
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 */
@ -1552,8 +1536,8 @@ int PlGetc(int 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)) {
@ -1615,8 +1599,8 @@ int PlGetc(int sno) {
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;
@ -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) {
@ -1724,8 +1707,7 @@ int PlGetc(int sno) {
*/ */
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);
@ -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,8 +1831,7 @@ 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));