signed wchar

This commit is contained in:
vscosta 2016-04-05 02:53:39 +01:00
parent 4d3d9c408d
commit 71f0076ed6
8 changed files with 2320 additions and 2431 deletions

View File

@ -233,10 +233,10 @@ output is directed to the stream used by format/2.
*/
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "yapio.h"
#include "YapText.h"
#include "Yatom.h"
#include "yapio.h"
#include <stdlib.h>
#if HAVE_UNISTD_H
#include <unistd.h>
@ -257,8 +257,8 @@ output is directed to the stream used by format/2.
#define S_ISDIR(x) (((x)&_S_IFDIR) == _S_IFDIR)
#endif
#endif
#include "iopreds.h"
#include "eval.h"
#include "iopreds.h"
#define FORMAT_MAX_SIZE 1024
@ -618,7 +618,8 @@ static Int doformat(volatile Term otail, volatile Term oargs,
goto do_type_atom_error;
yhandle_t sl = Yap_StartSlots();
// stream is already locked.
Yap_plwrite(t, GLOBAL_Stream + sno, 0, Handle_vars_f | To_heap_f, GLOBAL_MaxPriority);
Yap_plwrite(t, GLOBAL_Stream + sno, 0, Handle_vars_f | To_heap_f,
GLOBAL_MaxPriority);
Yap_CloseSlots(sl);
break;
case 'c': {
@ -810,8 +811,8 @@ static Int doformat(volatile Term otail, volatile Term oargs,
char *pt, *res;
tmpbase = tmp1;
while (!(res = Yap_gmp_to_string(t, tmpbase, TMP_STRING_SIZE,
radix))) {
while (!(
res = Yap_gmp_to_string(t, tmpbase, TMP_STRING_SIZE, radix))) {
if (tmpbase == tmp1) {
tmpbase = NULL;
} else {
@ -871,7 +872,8 @@ static Int doformat(volatile Term otail, volatile Term oargs,
t = targs[targ++];
yhandle_t sl = Yap_StartSlots();
Yap_plwrite(t, GLOBAL_Stream + sno, 0,
Quote_illegal_f | Ignore_ops_f | To_heap_f, GLOBAL_MaxPriority);
Quote_illegal_f | Ignore_ops_f | To_heap_f,
GLOBAL_MaxPriority);
Yap_CloseSlots(sl);
break;
case '@':
@ -910,7 +912,8 @@ static Int doformat(volatile Term otail, volatile Term oargs,
{
Int sl = Yap_InitSlot(args);
Yap_plwrite(t, GLOBAL_Stream + sno, 0,
Handle_vars_f | Use_portray_f | To_heap_f, GLOBAL_MaxPriority);
Handle_vars_f | Use_portray_f | To_heap_f,
GLOBAL_MaxPriority);
args = Yap_GetFromSlot(sl);
Yap_CloseSlots(sl);
}
@ -936,7 +939,8 @@ static Int doformat(volatile Term otail, volatile Term oargs,
t = targs[targ++];
yhandle_t sl0 = Yap_StartSlots();
Yap_plwrite(t, GLOBAL_Stream + sno, 0,
Handle_vars_f | Quote_illegal_f | To_heap_f, GLOBAL_MaxPriority);
Handle_vars_f | Quote_illegal_f | To_heap_f,
GLOBAL_MaxPriority);
Yap_CloseSlots(sl0);
break;
case 'w':
@ -1022,8 +1026,7 @@ static Int doformat(volatile Term otail, volatile Term oargs,
else
finfo.pad_entries[finfo.padders].filler = fptr[-2];
finfo.padders++;
}
break;
} break;
do_instantiation_error:
LOCAL_Error_TYPE = INSTANTIATION_ERROR;
goto do_default_error;

307
os/getw.h
View File

@ -1,5 +1,5 @@
/// compose a wide char from a sequence of getchars
/// compose a wide char from a sequence of getchars
/// this is a slow lane routine, called if no specialised code
/// isavailable.
static int GETW(int sno) {
@ -24,12 +24,12 @@ static int GETW(int sno) {
case ENC_ISO_ANSI: {
char buf[8];
int out;
int wch;
wchar_t wch;
mbstate_t mbstate;
memset((void *)&(mbstate), 0, sizeof(mbstate_t));
buf[0] = ch;
int n=1;
int n = 1;
while ((out = mbrtowc(&wch, buf, 1, &(mbstate))) != 1) {
int ch = buf[0] = GETC();
n++;
@ -38,166 +38,169 @@ static int GETW(int sno) {
}
return post_process_read_wchar(wch, n, st);
}
// UTF-8 works o 8 bits.
case ENC_ISO_UTF8: {
// UTF-8 works o 8 bits.
case ENC_ISO_UTF8: {
int wch;
unsigned char buf[8];
unsigned char buf[8];
if (ch < 0x80) {
return post_process_read_wchar(ch, 1, st);
}
// if ((ch - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
if (ch < 0xe0) { // 2-byte sequence
// Must have valid continuation character
int c1 = buf[0] = GETC();
if (c1 == -1)
if (ch < 0x80) {
return post_process_read_wchar(ch, 1, st);
}
// if ((ch - 0xc2) > (0xf4-0xc2)) return UTF8PROC_ERROR_INVALIDUTF8;
if (ch < 0xe0) { // 2-byte sequence
// Must have valid continuation character
int c1 = buf[0] = GETC();
if (c1 == -1)
return post_process_weof(st);
// if (!utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
wch = ((ch & 0x1f)<<6) | (c1 & 0x3f);
return post_process_read_wchar(wch, 2, st);
}
if (ch < 0xf0) { // 3-byte sequence
//if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
// return UTF8PROC_ERROR_INVALIDUTF8;
// Check for surrogate chars
//if (ch == 0xed && *str > 0x9f)
// return UTF8PROC_ERROR_INVALIDUTF8;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
wch = ((ch & 0xf)<<12) | ((c1 & 0x3f)<<6) | (c2 & 0x3f);
return post_process_read_wchar(wch, 3, st);
} else {
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
int c3 = GETC();
if (c3 == -1)
return post_process_weof(st);
wch = ((ch & 7)<<18) | ((c1 & 0x3f)<<12) | ((c2 & 0x3f)<<6) | (c3 & 0x3f);
return post_process_read_wchar(wch, 4, st);
}
}
case ENC_UTF16_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
// if (!utf_cont(*str)) return UTF8PROC_ERROR_INVALIDUTF8;
wch = ((ch & 0x1f) << 6) | (c1 & 0x3f);
return post_process_read_wchar(wch, 2, st);
}
if (ch < 0xf0) { // 3-byte sequence
// if ((str + 1 >= end) || !utf_cont(*str) || !utf_cont(str[1]))
// return UTF8PROC_ERROR_INVALIDUTF8;
// Check for surrogate chars
// if (ch == 0xed && *str > 0x9f)
// return UTF8PROC_ERROR_INVALIDUTF8;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1 << 8) + ch;
if (wch >= 0xd800 && wch < 0xdc00) {
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
int c3 = GETC();
if (c3 == -1)
return post_process_weof(st);
wch = wch + (((c3 << 8) + c2)<<wch) + SURROGATE_OFFSET;
return post_process_read_wchar(wch, 4, st);
}
return post_process_read_wchar(wch, 2, st);
}
case ENC_UTF16_BE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
if (c1 == -1)
return post_process_weof(st);
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
wch = ((ch & 0xf) << 12) | ((c1 & 0x3f) << 6) | (c2 & 0x3f);
return post_process_read_wchar(wch, 3, st);
} else {
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1) + (ch<<8);
if (wch >= 0xd800 && wch < 0xdc00) {
int c3 = GETC();
if (c3 == -1)
return post_process_weof(st);
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
wch = (((c3 << 8) + c2) << 10) + wch + SURROGATE_OFFSET;
if (c1 == -1)
return post_process_weof(st);
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
int c3 = GETC();
if (c3 == -1)
return post_process_weof(st);
wch = ((ch & 7) << 18) | ((c1 & 0x3f) << 12) | ((c2 & 0x3f) << 6) |
(c3 & 0x3f);
return post_process_read_wchar(wch, 4, st);
}
}
case ENC_UTF16_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1 << 8) + ch;
if (wch >= 0xd800 && wch < 0xdc00) {
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
int c3 = GETC();
if (c3 == -1)
return post_process_weof(st);
wch = wch + (((c3 << 8) + c2) << wch) + SURROGATE_OFFSET;
return post_process_read_wchar(wch, 4, st);
}
return post_process_read_wchar(wch, 2, st);
}
}
return post_process_read_wchar(wch, 2, st);
}
case ENC_UTF16_BE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1) + (ch << 8);
if (wch >= 0xd800 && wch < 0xdc00) {
int c3 = GETC();
if (c3 == -1)
return post_process_weof(st);
int c2 = GETC();
if (c2 == -1)
return post_process_weof(st);
wch = (((c3 << 8) + c2) << 10) + wch + SURROGATE_OFFSET;
return post_process_read_wchar(wch, 4, st);
}
return post_process_read_wchar(wch, 2, st);
}
case ENC_UCS2_BE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
// little-endian: start with big shot
{
int wch;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1) + (ch<<8);
return post_process_read_wchar(wch, 2, st);
}
case ENC_UCS2_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1 << 8) + ch;
if (c1 == -1)
return post_process_weof(st);
wch = (c1) + (ch << 8);
return post_process_read_wchar(wch, 2, st);
}
return post_process_read_wchar(wch, 2, st);
}
case ENC_UCS2_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch;
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (c1 << 8) + ch;
case ENC_ISO_UTF32_BE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch = ch;
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = wch + c1;
return post_process_read_wchar(wch, 2, st);
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (wch << 8 )+c1;
case ENC_ISO_UTF32_BE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch = ch;
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = wch + c1;
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (wch << 8) + c1;
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (wch << 8) + c1;
}
return post_process_read_wchar(wch, 4, st);
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch = (wch << 8) +c1;
}
return post_process_read_wchar(wch, 4, st);
case ENC_ISO_UTF32_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch = ch;
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch += c1 << 8;
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch += c1 << 16;
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch += c1 << 24;
}
return post_process_read_wchar(wch, 4, st);
}
default:
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, MkIntTerm(st->encoding),
"Bad Encoding\n");
return -1;
}
case ENC_ISO_UTF32_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
// little-endian: start with big shot
{
int wch = ch;
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch += c1<<8;
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch += c1<<16;
}
{
int c1 = GETC();
if (c1 == -1)
return post_process_weof(st);
wch += c1<<24;
}
return post_process_read_wchar(wch, 4, st);
}
}
}

File diff suppressed because it is too large Load Diff

View File

@ -18,9 +18,9 @@ static char SccsId[] = "%W% %G%";
#define HAVE_SOCKET 1
#endif
#include <stdlib.h>
#include "Yap.h"
#include "Atoms.h"
#include "Yap.h"
#include <stdlib.h>
/*
* This file defines main data-structure for stream management,
@ -206,12 +206,17 @@ typedef struct stream_desc {
#if defined(YAPOR) || defined(THREADS)
lockvar streamlock; /* protect stream access */
#endif
int (*stream_putc)(int, int); /** function the stream uses for writing a single octet */
int (*stream_wputc)(int, int); /** function the stream uses for writing a character */
int (*stream_getc)(int); /** function the stream uses for reading an octet. */
int (*stream_wgetc)(int); /** function the stream uses for reading a character. */
int (*stream_putc)(
int, int); /** function the stream uses for writing a single octet */
int (*stream_wputc)(
int, wchar_t); /** function the stream uses for writing a character */
int (*stream_getc)(int); /** function the stream uses for reading an octet. */
int (*stream_wgetc)(
int); /** function the stream uses for reading a character. */
int (*stream_wgetc_for_read)(int); /* function the stream uses for parser. It may be different from above if the ISO character conversion is on */
int (*stream_wgetc_for_read)(
int); /* function the stream uses for parser. It may be different
from above if the ISO character conversion is on */
encoding_t encoding; /** current encoding for stream */
} StreamDesc;
@ -270,7 +275,7 @@ void Yap_ConsolePipeOps(StreamDesc *st);
void Yap_SocketOps(StreamDesc *st);
void Yap_ConsoleSocketOps(StreamDesc *st);
bool Yap_ReadlineOps(StreamDesc *st);
int Yap_OpenBufWriteStream(USES_REGS1);
int Yap_OpenBufWriteStream(USES_REGS1);
void Yap_ConsoleOps(StreamDesc *s, bool recursive);
void Yap_InitRandomPreds(void);

352
os/mem.c
View File

@ -8,7 +8,7 @@
* *
**************************************************************************
* *
* File: sockets.c *
* File: sockets.c *
* Last rev: 5/2/88 *
* mods: *
* comments: Input/Output C implemented predicates *
@ -24,8 +24,8 @@ static char SccsId[] = "%W% %G%";
*/
#include "Yap.h"
#include "Yatom.h"
#include "YapHeap.h"
#include "Yatom.h"
#include "yapio.h"
#include <stdlib.h>
#if HAVE_UNISTD_H
@ -38,13 +38,13 @@ static char SccsId[] = "%W% %G%";
#if HAVE_IO_H
/* Windows */
#include <io.h>
#endif
#endif
#if HAVE_SOCKET
#include <winsock2.h>
#endif
#include <windows.h>
#ifndef S_ISDIR
#define S_ISDIR(x) (((x)&_S_IFDIR)==_S_IFDIR)
#define S_ISDIR(x) (((x)&_S_IFDIR) == _S_IFDIR)
#endif
#endif
#include "iopreds.h"
@ -52,14 +52,19 @@ static char SccsId[] = "%W% %G%";
#include "fmemopen.h"
#define HAVE_FMEMOPEN 1
#define HAVE_OPEN_MEMSTREAM 1
FILE * open_memstream (char **buf, size_t *len);
FILE *open_memstream(char **buf, size_t *len);
#endif
#if HAVE_FMEMOPEN
#if __ANDROID__
#undef HAVE_FMEMOPEN
#undef HAVE_OPEN_MEMSTREAM
#endif
#if HAVE_FMEMOPEN
#define MAY_READ 1
#endif
#if HAVE_OPEN_MEMSTREAM
#if HAVE_OPEN_MEMSTREAM
#define MAY_READ 1
#define MAY_WRITE 1
#endif
@ -70,44 +75,38 @@ FILE * open_memstream (char **buf, size_t *len);
#endif
#if !MAY_READ
static int MemGetc( int);
static int MemGetc(int);
/* read from memory */
static int
MemGetc(int sno)
{
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
int spos;
static int MemGetc(int sno) {
register StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
int spos;
spos = s->u.mem_string.pos;
if (spos == s->u.mem_string.max_size) {
return -1;
}
else {
ch = s->u.mem_string.buf[spos];
s->u.mem_string.pos = ++spos;
}
return ch;
spos = s->u.mem_string.pos;
if (spos == s->u.mem_string.max_size) {
return -1;
} else {
ch = s->u.mem_string.buf[spos];
s->u.mem_string.pos = ++spos;
}
return ch;
}
#endif
#if !MAY_WRITE
static int MemPutc( int, int);
static int MemPutc(int, int);
/* static */
static int
MemPutc(int sno, int ch)
{
static int MemPutc(int sno, int ch) {
StreamDesc *s = &GLOBAL_Stream[sno];
#if MAC || _MSC_VER
if (ch == 10)
{
ch = '\n';
}
if (ch == 10) {
ch = '\n';
}
#endif
s->u.mem_string.buf[s->u.mem_string.pos++] = ch;
if (s->u.mem_string.pos >= s->u.mem_string.max_size -8) {
if (s->u.mem_string.pos >= s->u.mem_string.max_size - 8) {
int old_src = s->u.mem_string.src, new_src;
/* oops, we have reached an overflow */
@ -115,78 +114,79 @@ MemPutc(int sno, int ch)
char *newbuf;
if (old_src == MEM_BUF_CODE &&
(newbuf = Yap_AllocAtomSpace(new_max_size*sizeof(char))) != NULL) {
(newbuf = Yap_AllocAtomSpace(new_max_size * sizeof(char))) != NULL) {
new_src = MEM_BUF_CODE;
#if HAVE_MEMMOVE
memmove((void *)newbuf, (void *)s->u.mem_string.buf, (size_t)((s->u.mem_string.pos)*sizeof(char)));
memmove((void *)newbuf, (void *)s->u.mem_string.buf,
(size_t)((s->u.mem_string.pos) * sizeof(char)));
#else
{
Int n = s->u.mem_string.pos;
char *to = newbuf;
char *from = s->u.mem_string.buf;
while (n-- >= 0) {
*to++ = *from++;
{
Int n = s->u.mem_string.pos;
char *to = newbuf;
char *from = s->u.mem_string.buf;
while (n-- >= 0) {
*to++ = *from++;
}
}
}
#endif
Yap_FreeAtomSpace(s->u.mem_string.buf);
#if !HAVE_SYSTEM_MALLOC
} else if ((newbuf = (ADDR)realloc(s->u.mem_string.buf, new_max_size*sizeof(char))) != NULL) {
} else if ((newbuf = (ADDR)realloc(s->u.mem_string.buf,
new_max_size * sizeof(char))) != NULL) {
new_src = MEM_BUF_MALLOC;
#endif
} else {
if (GLOBAL_Stream[sno].u.mem_string.error_handler) {
CACHE_REGS
LOCAL_Error_Size = new_max_size*sizeof(char);
save_machine_regs();
longjmp(*(jmp_buf *)GLOBAL_Stream[sno].u.mem_string.error_handler,1);
CACHE_REGS
LOCAL_Error_Size = new_max_size * sizeof(char);
save_machine_regs();
longjmp(*(jmp_buf *)GLOBAL_Stream[sno].u.mem_string.error_handler, 1);
} else {
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, "YAP could not grow heap for writing to string");
Yap_Error(RESOURCE_ERROR_HEAP, TermNil,
"YAP could not grow heap for writing to string");
}
return -1;
}
if (old_src == MEM_BUF_CODE) {
if (old_src == MEM_BUF_CODE) {
}
s->u.mem_string.buf = newbuf;
s->u.mem_string.max_size = new_max_size;
s->u.mem_string.src = new_src;
}
count_output_char(ch,s);
return ((int) ch);
count_output_char(ch, s);
return ((int)ch);
}
#endif
int
Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp, memBufSource src)
{
int Yap_open_buf_read_stream(const char *nbuf, size_t nchars, encoding_t *encp,
memBufSource src) {
CACHE_REGS
int sno;
StreamDesc *st;
FILE *f;
encoding_t encoding;
stream_flags_t flags;
sno = GetFreeStreamD();
if (sno < 0)
return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_mem_read_stream/1"));
st = GLOBAL_Stream+sno;
return (PlIOError(RESOURCE_ERROR_MAX_STREAMS, TermNil,
"new stream not available for open_mem_read_stream/1"));
st = GLOBAL_Stream + sno;
if (encp)
encoding = *encp;
else
encoding = LOCAL_encoding;
#if MAY_READ
// like any file stream.
f = fmemopen( (void *)nbuf, nchars, "r");
f = fmemopen((void *)nbuf, nchars, "r");
flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
#else
f = NULL;
flags = Input_Stream_f | InMemory_Stream_f;
#endif
Yap_initStream(sno, f, NULL, TermNil,
encoding, flags, AtomRead);
// like any file stream.
Yap_initStream(sno, f, NULL, TermNil, encoding, flags, AtomRead);
// like any file stream.
#if !MAY_READ
/* currently these streams are not seekable */
st->status = Input_Stream_f | InMemory_Stream_f;
@ -196,13 +196,13 @@ MemPutc(int sno, int ch)
st->u.mem_string.error_handler = NULL;
st->u.mem_string.src = src;
#endif
Yap_MemOps( st );
Yap_MemOps(st);
UNLOCK(st->streamlock);
return sno;
}
static Int
open_mem_read_stream (USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
open_mem_read_stream(USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
{
Term t, ti;
int sno;
@ -222,10 +222,10 @@ open_mem_read_stream (USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
ti = TailOfTerm(ti);
}
}
while ((nbuf = (char *)Yap_AllocAtomSpace((sl+1)*sizeof(char))) == NULL) {
if (!Yap_growheap(FALSE, (sl+1)*sizeof(char), NULL)) {
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
return(FALSE);
while ((nbuf = (char *)Yap_AllocAtomSpace((sl + 1) * sizeof(char))) == NULL) {
if (!Yap_growheap(FALSE, (sl + 1) * sizeof(char), NULL)) {
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
return (FALSE);
}
}
ti = Deref(ARG1);
@ -244,101 +244,96 @@ open_mem_read_stream (USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
}
nbuf[nchars] = '\0';
sno = Yap_open_buf_read_stream(nbuf, nchars, &LOCAL_encoding, MEM_BUF_CODE);
t = Yap_MkStream (sno);
return (Yap_unify (ARG2, t));
t = Yap_MkStream(sno);
return (Yap_unify(ARG2, t));
}
int
Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSource sr)
{
int Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp,
memBufSource sr) {
CACHE_REGS
int sno;
StreamDesc *st;
int sno;
StreamDesc *st;
sno = GetFreeStreamD();
if (sno < 0)
return -1;
st = GLOBAL_Stream+sno;
st->status = Output_Stream_f | InMemory_Stream_f;
if (!buf) {
if (!nchars) {
nchars = Yap_page_size;
}
buf = malloc( nchars );
st->status |= FreeOnClose_Stream_f;
sno = GetFreeStreamD();
if (sno < 0)
return -1;
st = GLOBAL_Stream + sno;
st->status = Output_Stream_f | InMemory_Stream_f;
if (!buf) {
if (!nchars) {
nchars = Yap_page_size;
}
st->nbuf = buf;
if(!st->nbuf) {
return -1;
}
st->nsize = nchars;
st->linepos = 0;
st->charcount = 0;
st->linecount = 1;
if (encp)
st->encoding = *encp;
else
st->encoding = LOCAL_encoding;
Yap_DefaultStreamOps( st );
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;
st->linecount = 1;
if (encp)
st->encoding = *encp;
else
st->encoding = LOCAL_encoding;
Yap_DefaultStreamOps(st);
#if MAY_WRITE
st->file = open_memstream(&st->nbuf, &st->nsize);
st->status |= Seekable_Stream_f;
st->file = open_memstream(&st->nbuf, &st->nsize);
st->status |= Seekable_Stream_f;
#else
st->u.mem_string.pos = 0;
st->u.mem_string.buf = st->nbuf;
st->u.mem_string.max_size = nchars;
#endif
Yap_MemOps( st );
UNLOCK(st->streamlock);
return sno;
st->u.mem_string.pos = 0;
st->u.mem_string.buf = st->nbuf;
st->u.mem_string.max_size = nchars;
#endif
Yap_MemOps(st);
UNLOCK(st->streamlock);
return sno;
}
int
Yap_OpenBufWriteStream( USES_REGS1 )
{
int Yap_OpenBufWriteStream(USES_REGS1) {
char *nbuf;
size_t sz = Yap_page_size;
size_t sz = Yap_page_size;
while ((nbuf = (char *)Yap_AllocAtomSpace(Yap_page_size*sizeof(char))) == NULL) {
if (!Yap_growheap(FALSE, Yap_page_size*sizeof(char), NULL)) {
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
while ((nbuf = (char *)Yap_AllocAtomSpace(Yap_page_size * sizeof(char))) ==
NULL) {
if (!Yap_growheap(FALSE, Yap_page_size * sizeof(char), NULL)) {
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
return -1;
}
}
return Yap_open_buf_write_stream(nbuf, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0);
return Yap_open_buf_write_stream(
nbuf, sz, &GLOBAL_Stream[LOCAL_c_output_stream].encoding, 0);
}
static Int
open_mem_write_stream (USES_REGS1) /* $open_mem_write_stream(-Stream) */
open_mem_write_stream(USES_REGS1) /* $open_mem_write_stream(-Stream) */
{
Term t;
int sno;
sno = Yap_OpenBufWriteStream( PASS_REGS1 );
sno = Yap_OpenBufWriteStream(PASS_REGS1);
if (sno == -1)
return (PlIOError (SYSTEM_ERROR_INTERNAL,TermNil, "new stream not available for open_mem_read_stream/1"));
t = Yap_MkStream (sno);
return (Yap_unify (ARG1, t));
return (PlIOError(SYSTEM_ERROR_INTERNAL, TermNil,
"new stream not available for open_mem_read_stream/1"));
t = Yap_MkStream(sno);
return (Yap_unify(ARG1, t));
}
/**
/**
* Yap_PeekMemwriteStream() shows the current buffer for a memory stream.
*
*
* @param sno, the in-memory stream
*
*
* @return temporary buffer, discarded by close and may be moved away
* by other writes..
*/
char *
Yap_MemExportStreamPtr( int sno )
{
char *Yap_MemExportStreamPtr(int sno) {
#if MAY_WRITE
char *s;
if (fflush(GLOBAL_Stream[sno].file) == 0)
{
s = GLOBAL_Stream[sno].nbuf;
if (fflush(GLOBAL_Stream[sno].file) == 0) {
s = GLOBAL_Stream[sno].nbuf;
return s;
}
return NULL;
@ -347,11 +342,10 @@ Yap_MemExportStreamPtr( int sno )
#endif
}
static Int
peek_mem_write_stream ( USES_REGS1 )
{ /* '$peek_mem_write_stream'(+GLOBAL_Stream,?S0,?S) */
Int sno = Yap_CheckStream (ARG1, (Output_Stream_f | InMemory_Stream_f), "close/2");
static Int peek_mem_write_stream(
USES_REGS1) { /* '$peek_mem_write_stream'(+GLOBAL_Stream,?S0,?S) */
Int sno =
Yap_CheckStream(ARG1, (Output_Stream_f | InMemory_Stream_f), "close/2");
Int i;
Term tf = ARG2;
CELL *HI;
@ -359,27 +353,27 @@ peek_mem_write_stream ( USES_REGS1 )
if (sno < 0)
return (FALSE);
restart:
restart:
HI = HR;
#if MAY_WRITE
if (fflush(GLOBAL_Stream[sno].file) == 0) {
ptr = GLOBAL_Stream[sno].nbuf;
i = GLOBAL_Stream[sno].nsize;
}
ptr = GLOBAL_Stream[sno].nbuf;
i = GLOBAL_Stream[sno].nsize;
}
#else
ptr = GLOBAL_Stream[sno].u.mem_string.buf;
i = GLOBAL_Stream[sno].u.mem_string.pos;
ptr = GLOBAL_Stream[sno].u.mem_string.buf;
i = GLOBAL_Stream[sno].u.mem_string.pos;
#endif
while (i > 0) {
--i;
tf = MkPairTerm(MkIntTerm(ptr[i]),tf);
tf = MkPairTerm(MkIntTerm(ptr[i]), tf);
if (HR + 1024 >= ASP) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
HR = HI;
if (!Yap_gcl((ASP-HI)*sizeof(CELL), 3, ENV, Yap_gcP()) ) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return(FALSE);
if (!Yap_gcl((ASP - HI) * sizeof(CELL), 3, ENV, Yap_gcP())) {
UNLOCK(GLOBAL_Stream[sno].streamlock);
Yap_Error(RESOURCE_ERROR_STACK, TermNil, LOCAL_ErrorMessage);
return (FALSE);
}
i = GLOBAL_Stream[sno].u.mem_string.pos;
tf = ARG2;
@ -388,16 +382,14 @@ peek_mem_write_stream ( USES_REGS1 )
}
}
UNLOCK(GLOBAL_Stream[sno].streamlock);
return (Yap_unify(ARG3,tf));
return (Yap_unify(ARG3, tf));
}
void
Yap_MemOps( StreamDesc *st )
{
void Yap_MemOps(StreamDesc *st) {
#if MAY_WRITE
st->stream_putc = FilePutc;
#else
st->stream_putc = MemPutc;
st->stream_putc = MemPutc;
#endif
#if MAY_READ
@ -407,44 +399,42 @@ void
#endif
}
bool Yap_CloseMemoryStream( int sno )
{
if (!(GLOBAL_Stream[sno].status & Output_Stream_f) ) {
bool Yap_CloseMemoryStream(int sno) {
if (!(GLOBAL_Stream[sno].status & Output_Stream_f)) {
#if MAY_WRITE
fclose(GLOBAL_Stream[sno].file);
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);
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(GLOBAL_Stream[sno].u.mem_string.buf);
}
#endif
} else {
#if MAY_READ
fclose(GLOBAL_Stream[sno].file);
Yap_FreeAtomSpace(GLOBAL_Stream[sno].nbuf);
fclose(GLOBAL_Stream[sno].file);
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)
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);
}
#endif
}
return true;
#endif
} else {
#if MAY_READ
fclose(GLOBAL_Stream[sno].file);
Yap_FreeAtomSpace(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);
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(GLOBAL_Stream[sno].u.mem_string.buf);
}
#endif
}
return true;
}
void
Yap_InitMems( void )
{
CACHE_REGS
void Yap_InitMems(void) {
CACHE_REGS
Term cm = CurrentModule;
CurrentModule = CHARSIO_MODULE;
Yap_InitCPred ("open_mem_read_stream", 2, open_mem_read_stream, SyncPredFlag);
Yap_InitCPred ("open_mem_write_stream", 1, open_mem_write_stream, SyncPredFlag);
Yap_InitCPred ("peek_mem_write_stream", 3, peek_mem_write_stream, SyncPredFlag);
Yap_InitCPred("open_mem_read_stream", 2, open_mem_read_stream, SyncPredFlag);
Yap_InitCPred("open_mem_write_stream", 1, open_mem_write_stream,
SyncPredFlag);
Yap_InitCPred("peek_mem_write_stream", 3, peek_mem_write_stream,
SyncPredFlag);
CurrentModule = cm;
}

1094
os/sig.c

File diff suppressed because it is too large Load Diff

View File

@ -635,6 +635,7 @@ static Term
do_glob(const char *spec, bool glob_vs_wordexp) {
CACHE_REGS
char u[YAP_FILENAME_MAX + 1];
char *espec = u;
if (spec == NULL) {
return TermNil;
}
@ -642,7 +643,6 @@ static Term
{
WIN32_FIND_DATA find;
HANDLE hFind;
const char *espec;
CELL *dest;
Term tf;
@ -673,7 +673,6 @@ static Term
return tf;
}
#elif HAVE_WORDEXP || HAVE_GLOB
char *espec = u;
strncpy(espec, spec, sizeof(u));
/* Expand the string for the program to run. */
size_t pathcount;

File diff suppressed because it is too large Load Diff