fixes + indenting

This commit is contained in:
Vitor Santos Costa 2016-04-12 16:04:33 +01:00
parent 69bb5c4d08
commit e6c2503563
7 changed files with 384 additions and 115 deletions

View File

@ -171,26 +171,26 @@ loop:
static Int access_path(USES_REGS1) {
Term tname = Deref(ARG1);
char *file_name;
if (IsVarTerm(tname)) {
Yap_Error(INSTANTIATION_ERROR, tname, "access");
return FALSE;
return false;
} else if (!IsAtomTerm(tname)) {
Yap_Error(TYPE_ERROR_ATOM, tname, "access");
return FALSE;
return false;
} else {
#if HAVE_STAT
struct SYSTEM_STAT ss;
char *file_name;
file_name = RepAtom(AtomOfTerm(tname))->StrOfAE;
if (SYSTEM_STAT(file_name, &ss) != 0) {
/* ignore errors while checking a file */
return FALSE;
return true;
}
return TRUE;
return true;
#else
return FALSE;
return false;
#endif
}
}

279
os/fmemopen-android.c Normal file
View File

@ -0,0 +1,279 @@
bplist00Ñ_WebMainResourceÕ
_WebResourceTextEncodingName_WebResourceFrameName^WebResourceURL_WebResourceData_WebResourceMIMETypeUUTF-8P_Phttps://raw.githubusercontent.com/j-jorge/android-stdioext/master/src/fmemopen.cO#H<html><head></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/* Copyright (C) 2007 Eric Blake
* Permission to use, copy, modify, and distribute this software
* is freely granted, provided that this notice is preserved.
*
* Modifications for Android written Jul 2009 by Alan Viverette
*/
/*
FUNCTION
&lt;&lt;fmemopen&gt;&gt;---open a stream around a fixed-length string
INDEX
fmemopen
ANSI_SYNOPSIS
#include &lt;stdio.h&gt;
FILE *fmemopen(void *restrict &lt;[buf]&gt;, size_t &lt;[size]&gt;,
const char *restrict &lt;[mode]&gt;);
DESCRIPTION
&lt;&lt;fmemopen&gt;&gt; creates a seekable &lt;&lt;FILE&gt;&gt; stream that wraps a
fixed-length buffer of &lt;[size]&gt; bytes starting at &lt;[buf]&gt;. The stream
is opened with &lt;[mode]&gt; treated as in &lt;&lt;fopen&gt;&gt;, where append mode
starts writing at the first NUL byte. If &lt;[buf]&gt; is NULL, then
&lt;[size]&gt; bytes are automatically provided as if by &lt;&lt;malloc&gt;&gt;, with
the initial size of 0, and &lt;[mode]&gt; must contain &lt;&lt;+&gt;&gt; so that data
can be read after it is written.
The stream maintains a current position, which moves according to
bytes read or written, and which can be one past the end of the array.
The stream also maintains a current file size, which is never greater
than &lt;[size]&gt;. If &lt;[mode]&gt; starts with &lt;&lt;r&gt;&gt;, the position starts at
&lt;&lt;0&gt;&gt;, and file size starts at &lt;[size]&gt; if &lt;[buf]&gt; was provided. If
&lt;[mode]&gt; starts with &lt;&lt;w&gt;&gt;, the position and file size start at &lt;&lt;0&gt;&gt;,
and if &lt;[buf]&gt; was provided, the first byte is set to NUL. If
&lt;[mode]&gt; starts with &lt;&lt;a&gt;&gt;, the position and file size start at the
location of the first NUL byte, or else &lt;[size]&gt; if &lt;[buf]&gt; was
provided.
When reading, NUL bytes have no significance, and reads cannot exceed
the current file size. When writing, the file size can increase up to
&lt;[size]&gt; as needed, and NUL bytes may be embedded in the stream (see
&lt;&lt;open_memstream&gt;&gt; for an alternative that automatically enlarges the
buffer). When the stream is flushed or closed after a write that
changed the file size, a NUL byte is written at the current position
if there is still room; if the stream is not also open for reading, a
NUL byte is additionally written at the last byte of &lt;[buf]&gt; when the
stream has exceeded &lt;[size]&gt;, so that a write-only &lt;[buf]&gt; is always
NUL-terminated when the stream is flushed or closed (and the initial
&lt;[size]&gt; should take this into account). It is not possible to seek
outside the bounds of &lt;[size]&gt;. A NUL byte written during a flush is
restored to its previous value when seeking elsewhere in the string.
RETURNS
The return value is an open FILE pointer on success. On error,
&lt;&lt;NULL&gt;&gt; is returned, and &lt;&lt;errno&gt;&gt; will be set to EINVAL if &lt;[size]&gt;
is zero or &lt;[mode]&gt; is invalid, ENOMEM if &lt;[buf]&gt; was NULL and memory
could not be allocated, or EMFILE if too many streams are already
open.
PORTABILITY
This function is being added to POSIX 200x, but is not in POSIX 2001.
Supporting OS subroutines required: &lt;&lt;sbrk&gt;&gt;.
*/
#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;errno.h&gt;
#include &lt;string.h&gt;
#include &lt;malloc.h&gt;
#include "stdioext.h"
/* Describe details of an open memstream. */
typedef struct fmemcookie {
void *storage; /* storage to free on close */
char *buf; /* buffer start */
size_t pos; /* current position */
size_t eof; /* current file size */
size_t max; /* maximum file size */
char append; /* nonzero if appending */
char writeonly; /* 1 if write-only */
char saved; /* saved character that lived at pos before write-only NUL */
} fmemcookie;
/* Read up to non-zero N bytes into BUF from stream described by
COOKIE; return number of bytes read (0 on EOF). */
static int
fmemread(void *cookie, char *buf, int n)
{
fmemcookie *c = (fmemcookie *) cookie;
/* Can't read beyond current size, but EOF condition is not an error. */
if (c-&gt;pos &gt; c-&gt;eof)
return 0;
if (n &gt;= c-&gt;eof - c-&gt;pos)
n = c-&gt;eof - c-&gt;pos;
memcpy (buf, c-&gt;buf + c-&gt;pos, n);
c-&gt;pos += n;
return n;
}
/* Write up to non-zero N bytes of BUF into the stream described by COOKIE,
returning the number of bytes written or EOF on failure. */
static int
fmemwrite(void *cookie, const char *buf, int n)
{
fmemcookie *c = (fmemcookie *) cookie;
int adjust = 0; /* true if at EOF, but still need to write NUL. */
/* Append always seeks to eof; otherwise, if we have previously done
a seek beyond eof, ensure all intermediate bytes are NUL. */
if (c-&gt;append)
c-&gt;pos = c-&gt;eof;
else if (c-&gt;pos &gt; c-&gt;eof)
memset (c-&gt;buf + c-&gt;eof, '\0', c-&gt;pos - c-&gt;eof);
/* Do not write beyond EOF; saving room for NUL on write-only stream. */
if (c-&gt;pos + n &gt; c-&gt;max - c-&gt;writeonly)
{
adjust = c-&gt;writeonly;
n = c-&gt;max - c-&gt;pos;
}
/* Now n is the number of bytes being modified, and adjust is 1 if
the last byte is NUL instead of from buf. Write a NUL if
write-only; or if read-write, eof changed, and there is still
room. When we are within the file contents, remember what we
overwrite so we can restore it if we seek elsewhere later. */
if (c-&gt;pos + n &gt; c-&gt;eof)
{
c-&gt;eof = c-&gt;pos + n;
if (c-&gt;eof - adjust &lt; c-&gt;max)
c-&gt;saved = c-&gt;buf[c-&gt;eof - adjust] = '\0';
}
else if (c-&gt;writeonly)
{
if (n)
{
c-&gt;saved = c-&gt;buf[c-&gt;pos + n - adjust];
c-&gt;buf[c-&gt;pos + n - adjust] = '\0';
}
else
adjust = 0;
}
c-&gt;pos += n;
if (n - adjust)
memcpy (c-&gt;buf + c-&gt;pos - n, buf, n - adjust);
else
{
return EOF;
}
return n;
}
/* Seek to position POS relative to WHENCE within stream described by
COOKIE; return resulting position or fail with EOF. */
static fpos_t
fmemseek(void *cookie, fpos_t pos, int whence)
{
fmemcookie *c = (fmemcookie *) cookie;
off_t offset = (off_t) pos;
if (whence == SEEK_CUR)
offset += c-&gt;pos;
else if (whence == SEEK_END)
offset += c-&gt;eof;
if (offset &lt; 0)
{
offset = -1;
}
else if (offset &gt; c-&gt;max)
{
offset = -1;
}
else
{
if (c-&gt;writeonly &amp;&amp; c-&gt;pos &lt; c-&gt;eof)
{
c-&gt;buf[c-&gt;pos] = c-&gt;saved;
c-&gt;saved = '\0';
}
c-&gt;pos = offset;
if (c-&gt;writeonly &amp;&amp; c-&gt;pos &lt; c-&gt;eof)
{
c-&gt;saved = c-&gt;buf[c-&gt;pos];
c-&gt;buf[c-&gt;pos] = '\0';
}
}
return (fpos_t) offset;
}
/* Reclaim resources used by stream described by COOKIE. */
static int
fmemclose(void *cookie)
{
fmemcookie *c = (fmemcookie *) cookie;
free (c-&gt;storage);
return 0;
}
/* Open a memstream around buffer BUF of SIZE bytes, using MODE.
Return the new stream, or fail with NULL. */
FILE *
fmemopen(void *buf, size_t size, const char *mode)
{
FILE *fp;
fmemcookie *c;
int flags;
int dummy;
if ((flags = __sflags (mode, &amp;dummy)) == 0)
return NULL;
if (!size || !(buf || flags &amp; __SAPP))
{
return NULL;
}
if ((fp = (FILE *) __sfp ()) == NULL)
return NULL;
if ((c = (fmemcookie *) malloc (sizeof *c + (buf ? 0 : size))) == NULL)
{
fp-&gt;_flags = 0; /* release */
return NULL;
}
c-&gt;storage = c;
c-&gt;max = size;
/* 9 modes to worry about. */
/* w/a, buf or no buf: Guarantee a NUL after any file writes. */
c-&gt;writeonly = (flags &amp; __SWR) != 0;
c-&gt;saved = '\0';
if (!buf)
{
/* r+/w+/a+, and no buf: file starts empty. */
c-&gt;buf = (char *) (c + 1);
*(char *) buf = '\0';
c-&gt;pos = c-&gt;eof = 0;
c-&gt;append = (flags &amp; __SAPP) != 0;
}
else
{
c-&gt;buf = (char *) buf;
switch (*mode)
{
case 'a':
/* a/a+ and buf: position and size at first NUL. */
buf = memchr (c-&gt;buf, '\0', size);
c-&gt;eof = c-&gt;pos = buf ? (char *) buf - c-&gt;buf : size;
if (!buf &amp;&amp; c-&gt;writeonly)
/* a: guarantee a NUL within size even if no writes. */
c-&gt;buf[size - 1] = '\0';
c-&gt;append = 1;
break;
case 'r':
/* r/r+ and buf: read at beginning, full size available. */
c-&gt;pos = c-&gt;append = 0;
c-&gt;eof = size;
break;
case 'w':
/* w/w+ and buf: write at beginning, truncate to empty. */
c-&gt;pos = c-&gt;append = c-&gt;eof = 0;
*c-&gt;buf = '\0';
break;
default:
abort();
}
}
fp-&gt;_file = -1;
fp-&gt;_flags = flags;
fp-&gt;_cookie = c;
fp-&gt;_read = flags &amp; (__SRD | __SRW) ? fmemread : NULL;
fp-&gt;_write = flags &amp; (__SWR | __SRW) ? fmemwrite : NULL;
fp-&gt;_seek = fmemseek;
fp-&gt;_close = fmemclose;
return fp;
}
</pre></body></html>Ztext/plain (F]l~šî$: $E

View File

@ -233,9 +233,9 @@ static void unix_upd_stream_info(StreamDesc *s) {
filedes = fileno(s->file);
if (isatty(filedes)) {
#if HAVE_TTYNAME
char *ttys = ttyname_r(filedes, LOCAL_FileNameBuf, YAP_FILENAME_MAX - 1);
if (ttys == NULL)
s->name = AtomTty;
int rc = ttyname_r(filedes, LOCAL_FileNameBuf, YAP_FILENAME_MAX - 1);
if (rc == 0)
s->name = Yap_LookupAtom(LOCAL_FileNameBuf);
else
s->name = AtomTtys;
#else
@ -251,8 +251,6 @@ static void unix_upd_stream_info(StreamDesc *s) {
s->status |= Seekable_Stream_f;
}
void Yap_DefaultStreamOps(StreamDesc *st) {
CACHE_REGS
st->stream_wputc = put_wchar;
@ -266,8 +264,7 @@ void Yap_DefaultStreamOps(StreamDesc *st) {
else if (st->file != NULL) {
if (st->encoding == LOCAL_encoding) {
st->stream_wgetc = get_wchar_from_file;
}
else
} else
st->stream_wgetc = get_wchar_from_FILE;
}
#endif
@ -557,7 +554,7 @@ int ResetEOF(StreamDesc *s) {
/* reset the eof indicator on file */
if (feof(s->file))
clearerr(s->file);
/* reset our function for reading input */
/* reset our function for reading input */
Yap_DefaultStreamOps(s);
/* next, reset our own error indicator */
s->status &= ~Eof_Stream_f;
@ -1276,7 +1273,8 @@ do_open(Term file_name, Term t2,
if ((fd = fopen(fname, io_mode)) == NULL ||
(!(flags & Binary_Stream_f) && binary_file(fname))) {
strncpy(LOCAL_FileNameBuf, fname, MAXPATHLEN);
if (fname != fbuf && fname != LOCAL_FileNameBuf && fname != LOCAL_FileNameBuf2)
if (fname != fbuf && fname != LOCAL_FileNameBuf &&
fname != LOCAL_FileNameBuf2)
free((void *)fname);
fname = LOCAL_FileNameBuf;
UNLOCK(st->streamlock);

View File

@ -217,10 +217,6 @@ int Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp,
buf = malloc(nchars);
st->status |= FreeOnClose_Stream_f;
}
st->nbuf = buf;
if (!st->nbuf) {
return -1;
}
st->nsize = nchars;
st->linepos = 0;
st->charcount = 0;
@ -231,9 +227,17 @@ int Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp,
st->encoding = LOCAL_encoding;
Yap_DefaultStreamOps(st);
#if MAY_WRITE
st->file = open_memstream(&st->nbuf, &st->nsize);
st->status |= Seekable_Stream_f;
#if HAVE_FMEMOPEN
st->file = fmemopen((void *)buf, nchars, "w");
#else
st->file = open_memstream(buf, &st->nsize);
st->status |= Seekable_Stream_f;
#endif
st->nsize = nchars;
st->nbuf = buf;
if (!st->nbuf) {
return -1;
}
st->u.mem_string.pos = 0;
st->u.mem_string.buf = st->nbuf;
st->u.mem_string.max_size = nchars;

View File

@ -25,12 +25,12 @@ static char SccsId[] = "%W% %G%";
*/
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "YapFlags.h"
#include "yapio.h"
#include "eval.h"
#include "YapHeap.h"
#include "YapText.h"
#include "Yatom.h"
#include "eval.h"
#include "yapio.h"
#include <stdlib.h>
#if HAVE_STDARG_H
#include <stdarg.h>
@ -183,8 +183,8 @@ static int parse_quasi_quotations(ReadData _PL_rd ARG_LD) {
#endif /*O_QUASIQUOTATIONS*/
#define READ_DEFS() \
PAR("comments", list_filler, READ_COMMENTS), \
PAR("module", isatom, READ_MODULE), PAR("priority", nat, READ_PRIORITY), \
PAR("comments", list_filler, READ_COMMENTS) \
, PAR("module", isatom, READ_MODULE), PAR("priority", nat, READ_PRIORITY), \
PAR("quasi_quotations", filler, READ_QUASI_QUOTATIONS), \
PAR("term_position", filler, READ_TERM_POSITION), \
PAR("syntax_errors", isatom, READ_SYNTAX_ERRORS), \
@ -288,8 +288,7 @@ static Term syntax_error(TokEntry *errtok, int sno, Term cmod) {
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomGVar, 2), 2, t);
} break;
case String_tok: {
Term t0 =
Yap_CharsToTDQ((char *)info, cmod, ENC_ISO_LATIN1 PASS_REGS);
Term t0 = Yap_CharsToTDQ((char *)info, cmod, ENC_ISO_LATIN1 PASS_REGS);
if (!t0) {
return 0;
}
@ -302,8 +301,7 @@ static Term syntax_error(TokEntry *errtok, int sno, Term cmod) {
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomString, 1), 1, &t0);
} break;
case BQString_tok: {
Term t0 =
Yap_CharsToTBQ((char *)info, cmod, ENC_ISO_LATIN1 PASS_REGS);
Term t0 = Yap_CharsToTBQ((char *)info, cmod, ENC_ISO_LATIN1 PASS_REGS);
ts[0] = Yap_MkApplTerm(Yap_MkFunctor(AtomString, 1), 1, &t0);
} break;
case WBQString_tok: {
@ -365,7 +363,7 @@ static Term syntax_error(TokEntry *errtok, int sno, Term cmod) {
Term Yap_syntax_error(TokEntry *errtok, int sno) {
return syntax_error(errtok, sno, CurrentModule);
}
}
typedef struct FEnv {
Term qq, tp, sp, np, vp, ce;
@ -458,8 +456,7 @@ static xarg *setReadEnv(Term opts, FEnv *fe, struct renv *re, int inp_stream) {
} else {
fe->np = 0;
}
if (args[READ_CHARACTER_ESCAPES].used ||
Yap_CharacterEscapes(fe->cmod)) {
if (args[READ_CHARACTER_ESCAPES].used || Yap_CharacterEscapes(fe->cmod)) {
fe->ce = true;
} else {
fe->ce = false;
@ -804,7 +801,7 @@ static parser_state_t scan(REnv *re, FEnv *fe, int inp_stream) {
if (LOCAL_tokptr->Tok == eot_tok && LOCAL_tokptr->TokInfo == TermNl) {
size_t len = strlen("Empty clause");
char *out = malloc(len + 1);
strncpy(out, "Empty clause",len);
strncpy(out, "Empty clause", len);
LOCAL_ErrorMessage = out;
LOCAL_Error_TYPE = SYNTAX_ERROR;
LOCAL_Error_Term = TermEof;
@ -994,8 +991,8 @@ static Int read_term(
}
#define READ_CLAUSE_DEFS() \
PAR("comments", list_filler, READ_CLAUSE_COMMENTS), \
PAR("module", isatom, READ_CLAUSE_MODULE), \
PAR("comments", list_filler, READ_CLAUSE_COMMENTS) \
, PAR("module", isatom, READ_CLAUSE_MODULE), \
PAR("variable_names", filler, READ_CLAUSE_VARIABLE_NAMES), \
PAR("variables", filler, READ_CLAUSE_VARIABLES), \
PAR("term_position", filler, READ_CLAUSE_TERM_POSITION), \
@ -1258,17 +1255,14 @@ static Int style_checker(USES_REGS1) {
return TRUE;
}
X_API Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp, int prio,
Term *bindings) {
X_API Term Yap_StringToTerm(const char *s, size_t len, encoding_t *encp,
int prio, Term *bindings) {
CACHE_REGS
Term bvar = MkVarTerm(), ctl;
yhandle_t sl;
if (bindings) {
Term enc = MkAtomTerm(Yap_LookupAtom(enc_name(*encp)));
Term encc = Yap_MkApplTerm(Yap_MkFunctor(AtomEncoding, 1), 1, &enc);
ctl = Yap_MkApplTerm(Yap_MkFunctor(AtomVariableNames, 1), 1, &bvar);
ctl = MkPairTerm(ctl, MkPairTerm(encc, TermNil));
sl = Yap_PushHandle(bvar);
} else {
ctl = TermNil;

View File

@ -463,8 +463,7 @@ static bool ChDir(const char *path) {
#else
rc = (chdir(qpath) == 0);
#endif
if (qpath != qp && qpath != LOCAL_FileNameBuf &&
qpath != LOCAL_FileNameBuf2)
if (qpath != qp && qpath != LOCAL_FileNameBuf && qpath != LOCAL_FileNameBuf2)
free((char *)qpath);
return rc;
}
@ -582,7 +581,7 @@ static const char *expandVars(const char *spec, char *u) {
*/
const char *Yap_AbsoluteFile(const char *spec, char *rc0, bool ok) {
const char *p;
char *rc;
const char *rc;
rc = PlExpandVars(spec, NULL, rc0);
if (!rc)
rc = spec;
@ -595,7 +594,6 @@ const char *Yap_AbsoluteFile(const char *spec, char *rc0, bool ok) {
freeBuffer(rc);
return NULL;
}
}
/**

View File

@ -22,18 +22,18 @@
//#undef _POSIX_
#endif
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "yapio.h"
#include "eval.h"
#include "YapText.h"
#include "Yatom.h"
#include "eval.h"
#include "yapio.h"
#if _WIN32 || defined(__MINGW32__)
#include <winsock2.h>
/* Windows */
#include "Shlwapi.h"
#include <direct.h>
#include <io.h>
#include <windows.h>
#include <direct.h>
#include "Shlwapi.h"
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
@ -47,11 +47,10 @@
#include <sys/param.h>
#endif
#ifdef FENV_H
#include <fenv.h>
#endif
#define S_ISDIR(x) (((x)&_S_IFDIR)==_S_IFDIR)
#define S_ISDIR(x) (((x)&_S_IFDIR) == _S_IFDIR)
#if HAVE_STDARG_H
#include <stdarg.h>
#endif
@ -89,10 +88,10 @@
#include <fcntl.h>
#endif
#if !HAVE_STRNCAT
#define strncat(X,Y,Z) strcat(X,Y)
#define strncat(X, Y, Z) strcat(X, Y)
#endif
#if !HAVE_STRNCPY
#define strncpy(X,Y,Z) strcpy(X,Y)
#define strncpy(X, Y, Z) strcpy(X, Y)
#endif
#include "iopreds.h"
@ -152,10 +151,7 @@
#include <readline/readline.h>
#endif
void Yap_InitRandom (void);
void Yap_InitTime (int wid);
void Yap_InitOSSignals (int wid);
void Yap_InitRandom(void);
void Yap_InitTime(int wid);
void Yap_InitOSSignals(int wid);
void Yap_InitWTime(void);