misc_bugs_when_compiling_problog

This commit is contained in:
Vitor Santos Costa
2016-04-14 12:00:09 +01:00
parent 29fba0238d
commit 1aa20e24b7
29 changed files with 100 additions and 191 deletions

View File

@@ -113,7 +113,7 @@ Int Yap_peek(int sno) {
}
#endif
/* buffer the character */
if (s->encoding == LOCAL_encoding) {
if (s->encoding == Yap_SystemEncoding() && 0) {
ch = fgetwc(s->file);
ungetwc(ch, s->file);
return ch;
@@ -122,7 +122,7 @@ Int Yap_peek(int sno) {
olinecount = s->linecount;
olinepos = s->linepos;
ch = s->stream_wgetc(sno);
if (ch == EOFCHAR) {
if (ch == EOFCHAR) {
s->stream_getc = EOFPeek;
s->stream_wgetc = EOFWPeek;
s->status |= Push_Eof_Stream_f;
@@ -136,7 +136,7 @@ Int Yap_peek(int sno) {
unsigned char cs[8];
size_t n = put_utf8(cs, ch);
while (n--) {
ungetc(cs[n - 1], s->file);
ungetc(cs[n], s->file);
}
} else if (s->encoding == ENC_UTF16_BE) {
/* do the ungetc as if a write .. */

View File

@@ -59,6 +59,9 @@ static char SccsId[] = "%W% %G%";
#if HAVE_WCTYPE_H
#include <wctype.h>
#endif
#if HAVE_LOCALE_H
#include <locale.h>
#endif
#ifdef _WIN32
#if HAVE_IO_H
/* Windows */
@@ -135,29 +138,40 @@ static encoding_t enc_os_default( encoding_t rc)\
return rc;
}
static encoding_t DefaultEncoding(void) {
int i = 0;
while (encvs[i]) {
char *v = getenv(encvs[i]);
encoding_t Yap_SystemEncoding(void) {
int i = -1;
while (i == -1 || encvs[i]) {
char *v;
if ( i == -1 ) {
if ((v = setlocale(LC_CTYPE, NULL)) == NULL ||
!strcmp(v,"C")) {
if ((v = getenv("LC_CTYPE")))
setlocale(LC_CTYPE, v);
else if ((v = getenv("LANG")))
setlocale(LC_CTYPE, v);
}
} else {
v = getenv(encvs[i]);
}
if (v) {
int j = 0;
size_t sz = strlen(v);
const char *coding;
while ((coding = ematches[j].s) != NULL) {
size_t sz2 = strlen(coding);
if (sz2 > sz) {
j++;
continue;
}
if (!strcmp(coding+(sz-sz2), v) ) {
return enc_os_default(ematches[j].e);
char *v1;
if ((v1 = strstr(v, coding)) &&
strlen(v1) == strlen(coding)) {
return ematches[j].e;
}
j++;
}
}
i++;
}
return enc_os_default(ENC_ISO_ASCII);
return ENC_ISO_ASCII;
}
static encoding_t DefaultEncoding(void) {
return enc_os_default(Yap_SystemEncoding());
}
encoding_t Yap_DefaultEncoding(void) {

View File

@@ -43,6 +43,7 @@ typedef enum {
/// read the current environment, as set by the user or as Initial
encoding_t Yap_DefaultEncoding(void);
encoding_t Yap_SystemEncoding(void);
void Yap_SetDefaultEncoding(encoding_t new_encoding);
#if HAVE_XLOCALE_H

View File

@@ -261,8 +261,8 @@ void Yap_DefaultStreamOps(StreamDesc *st) {
Yap_ConsoleOps(st);
}
#ifndef _WIN32
else if (st->file != NULL) {
if (st->encoding == LOCAL_encoding) {
else if (st->file != NULL && 0 && !(st->status & InMemory_Stream_f)) {
if (st->encoding == Yap_SystemEncoding()) {
st->stream_wgetc = get_wchar_from_file;
} else
st->stream_wgetc = get_wchar_from_FILE;
@@ -1137,7 +1137,6 @@ do_open(Term file_name, Term t2,
const char *s_encoding;
encoding_t encoding;
Term tenc;
// original file name
if (IsVarTerm(file_name)) {
Yap_Error(INSTANTIATION_ERROR, file_name, "open/3");

View File

@@ -130,10 +130,10 @@ int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp,
encoding = LOCAL_encoding;
#if MAY_READ
// like any file stream.
f = fmemopen((void *)nbuf, nchars, "r");
st->file = f = fmemopen((void *)nbuf, nchars, "r");
flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
#else
f = NULL;
sT->file = f = NULL;
flags = Input_Stream_f | InMemory_Stream_f;
#endif
Yap_initStream(sno, f, NULL, TermNil, encoding, flags, AtomRead);
@@ -369,7 +369,8 @@ bool Yap_CloseMemoryStream(int sno) {
} else {
#if MAY_READ
fclose(GLOBAL_Stream[sno].file);
Yap_FreeAtomSpace(GLOBAL_Stream[sno].nbuf);
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
free(GLOBAL_Stream[sno].nbuf);
#else
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE)
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);

View File

@@ -890,42 +890,7 @@ void Yap_CloseStreams(int loud) {
for (sno = 3; sno < MaxStreams; ++sno) {
if (GLOBAL_Stream[sno].status & Free_Stream_f)
continue;
if ((GLOBAL_Stream[sno].status & Popen_Stream_f)) {
#if _MSC_VER
_pclose(GLOBAL_Stream[sno].file);
#else
pclose(GLOBAL_Stream[sno].file);
#endif
}
if (GLOBAL_Stream[sno].status & (Pipe_Stream_f | Socket_Stream_f))
close(GLOBAL_Stream[sno].u.pipe.fd);
#if USE_SOCKET
else if (GLOBAL_Stream[sno].status & (Socket_Stream_f)) {
Yap_CloseSocket(GLOBAL_Stream[sno].u.socket.fd,
GLOBAL_Stream[sno].u.socket.flags,
GLOBAL_Stream[sno].u.socket.domain);
}
#endif
else if (GLOBAL_Stream[sno].status & InMemory_Stream_f) {
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) {
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);
}
} else if (!(GLOBAL_Stream[sno].status & Null_Stream_f)) {
fclose(GLOBAL_Stream[sno].file);
} else {
if (loud)
fprintf(Yap_stderr, "%% YAP Error: while closing stream: %s\n",
RepAtom(GLOBAL_Stream[sno].name)->StrOfAE);
}
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;
CloseStream( sno );
}
}
@@ -947,10 +912,16 @@ static void CloseStream(int sno) {
close(GLOBAL_Stream[sno].u.pipe.fd);
} else if (GLOBAL_Stream[sno].status & (InMemory_Stream_f)) {
Yap_CloseMemoryStream(sno);
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE)
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);
if (GLOBAL_Stream[sno].file == NULL) {
char *s = GLOBAL_Stream[sno].u.mem_string.buf;
if (s == LOCAL_FileNameBuf ||
s == LOCAL_FileNameBuf2)
return;
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE)
Yap_FreeAtomSpace(s);
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(s);
}
}
}
GLOBAL_Stream[sno].status = Free_Stream_f;

View File

@@ -521,14 +521,17 @@ static const char *myrealpath(const char *path, char *out) {
if (errno == ENOENT || errno == EACCES) {
char base[YAP_FILENAME_MAX];
strncpy(base, path, YAP_FILENAME_MAX - 1);
rc = realpath(dirname((char *)path), NULL);
rc = realpath(dirname(base), NULL);
if (rc) {
const char *b = basename(base);
size_t e = strlen(rc);
size_t bs = strlen(b);
rc = realloc(rc, e + bs + 2);
if (rc != out &&
rc != base) {
rc = realloc(rc, e + bs + 2);
}
#if _WIN32
if (rc[e - 1] != '\\' && rc[e - 1] != '/') {
rc[e] = '\\';
@@ -797,7 +800,7 @@ static Term
*
* @return
*/
static Int prolog_realpath(USES_REGS1) {
static Int real_path(USES_REGS1) {
Term t1 = Deref(ARG1);
const char *cmd;
@@ -2145,7 +2148,7 @@ void Yap_InitSysPreds(void) {
Yap_InitCPred("prolog_to_os_filename", 2, prolog_to_os_filename,
SyncPredFlag);
Yap_InitCPred("absolute_file_system_path", 2, absolute_file_system_path, 0);
Yap_InitCPred("real_path", 2, prolog_realpath, 0);
Yap_InitCPred("real_path", 2, real_path, 0);
Yap_InitCPred("true_file_name", 2, true_file_name, SyncPredFlag);
Yap_InitCPred("true_file_name", 3, true_file_name3, SyncPredFlag);
#ifdef _WIN32