signed wchar
This commit is contained in:
parent
4d3d9c408d
commit
71f0076ed6
25
os/format.c
25
os/format.c
@ -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;
|
||||
|
11
os/getw.h
11
os/getw.h
@ -24,7 +24,7 @@ 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));
|
||||
@ -80,7 +80,8 @@ case ENC_ISO_UTF8: {
|
||||
int c3 = GETC();
|
||||
if (c3 == -1)
|
||||
return post_process_weof(st);
|
||||
wch = ((ch & 7)<<18) | ((c1 & 0x3f)<<12) | ((c2 & 0x3f)<<6) | (c3 & 0x3f);
|
||||
wch = ((ch & 7) << 18) | ((c1 & 0x3f) << 12) | ((c2 & 0x3f) << 6) |
|
||||
(c3 & 0x3f);
|
||||
return post_process_read_wchar(wch, 4, st);
|
||||
}
|
||||
}
|
||||
@ -105,7 +106,6 @@ case ENC_UTF16_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
|
||||
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
|
||||
{
|
||||
@ -138,7 +138,6 @@ case ENC_UTF16_BE: // check http://unicode.org/faq/utf_bom.html#utf16-3
|
||||
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
|
||||
{
|
||||
@ -199,5 +198,9 @@ case ENC_ISO_UTF32_LE: // check http://unicode.org/faq/utf_bom.html#utf16-3
|
||||
}
|
||||
return post_process_read_wchar(wch, 4, st);
|
||||
}
|
||||
default:
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, MkIntTerm(st->encoding),
|
||||
"Bad Encoding\n");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
96
os/iopreds.c
96
os/iopreds.c
@ -33,12 +33,15 @@ static char SccsId[] = "%W% %G%";
|
||||
*/
|
||||
|
||||
#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"
|
||||
#include <stdlib.h>
|
||||
#if HAVE_UNISTD_H
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
#if HAVE_STDARG_H
|
||||
#include <stdarg.h>
|
||||
#endif
|
||||
@ -96,7 +99,6 @@ static char SccsId[] = "%W% %G%";
|
||||
#endif
|
||||
#include "iopreds.h"
|
||||
|
||||
|
||||
#define GETW get_wchar_from_FILE
|
||||
#define GETC() fgetwc(st->file)
|
||||
#include "getw.h"
|
||||
@ -248,7 +250,7 @@ static void unix_upd_stream_info(StreamDesc *s) {
|
||||
filedes = fileno(s->file);
|
||||
if (isatty(filedes)) {
|
||||
#if HAVE_TTYNAME
|
||||
char *ttys = ttyname(filedes);
|
||||
char *ttys = ttyname_r(filedes, LOCAL_FileNameBuf, YAP_FILENAME_MAX - 1);
|
||||
if (ttys == NULL)
|
||||
s->name = AtomTty;
|
||||
else
|
||||
@ -266,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) {
|
||||
@ -660,7 +661,6 @@ int post_process_read_wchar(int ch, size_t n, StreamDesc *s) {
|
||||
return ch;
|
||||
}
|
||||
|
||||
|
||||
int post_process_weof(StreamDesc *s) {
|
||||
if (!ResetEOF(s)) {
|
||||
s->status |= Eof_Stream_f;
|
||||
@ -692,7 +692,6 @@ int PlGetc(int sno) {
|
||||
return fgetc(s->file);
|
||||
}
|
||||
|
||||
|
||||
// layered version
|
||||
static int get_wchar__(int sno) { return fgetwc(GLOBAL_Stream[sno].file); }
|
||||
|
||||
@ -793,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));
|
||||
@ -811,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));
|
||||
@ -825,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;
|
||||
}
|
||||
@ -838,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));
|
||||
@ -870,8 +864,8 @@ int PlGetc(int sno) {
|
||||
/* 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) {
|
||||
@ -1079,8 +1073,7 @@ int PlGetc(int sno) {
|
||||
}
|
||||
|
||||
bool Yap_initStream(int sno, FILE *fd, const char *name, Term file_name,
|
||||
encoding_t encoding, stream_flags_t flags,
|
||||
Atom open_mode) {
|
||||
encoding_t encoding, stream_flags_t flags, Atom open_mode) {
|
||||
StreamDesc *st = &GLOBAL_Stream[sno];
|
||||
st->status = flags;
|
||||
|
||||
@ -1146,8 +1139,8 @@ int PlGetc(int sno) {
|
||||
}
|
||||
|
||||
#define OPEN_DEFS() \
|
||||
PAR("alias", isatom, OPEN_ALIAS), PAR("bom", booleanFlag, OPEN_BOM), \
|
||||
PAR("buffer", isatom, OPEN_BUFFER), \
|
||||
PAR("alias", isatom, OPEN_ALIAS) \
|
||||
, PAR("bom", booleanFlag, OPEN_BOM), PAR("buffer", isatom, OPEN_BUFFER), \
|
||||
PAR("close_on_abort", booleanFlag, OPEN_CLOSE_ON_ABORT), \
|
||||
PAR("create", isatom, OPEN_CREATE), \
|
||||
PAR("encoding", isatom, OPEN_ENCODING), \
|
||||
@ -1172,8 +1165,8 @@ int PlGetc(int sno) {
|
||||
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;
|
||||
@ -1279,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;
|
||||
@ -1328,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
|
||||
@ -1341,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))
|
||||
@ -1389,8 +1379,7 @@ int PlGetc(int sno) {
|
||||
|
||||
*/
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -1472,13 +1461,11 @@ int PlGetc(int sno) {
|
||||
|
||||
|
||||
*/
|
||||
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 */
|
||||
@ -1549,8 +1536,8 @@ int PlGetc(int 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)) {
|
||||
@ -1613,8 +1600,8 @@ int PlGetc(int sno) {
|
||||
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;
|
||||
@ -1683,8 +1670,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) {
|
||||
@ -1722,8 +1708,7 @@ int PlGetc(int sno) {
|
||||
*/
|
||||
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);
|
||||
@ -1762,8 +1747,8 @@ int PlGetc(int sno) {
|
||||
}
|
||||
|
||||
#define ABSOLUTE_FILE_NAME_DEFS() \
|
||||
PAR("access", isatom, ABSOLUTE_FILE_NAME_ACCESS), \
|
||||
PAR("expand", booleanFlag, ABSOLUTE_FILE_NAME_EXPAND), \
|
||||
PAR("access", isatom, ABSOLUTE_FILE_NAME_ACCESS) \
|
||||
, PAR("expand", booleanFlag, ABSOLUTE_FILE_NAME_EXPAND), \
|
||||
PAR("extensions", ok, ABSOLUTE_FILE_NAME_EXTENSIONS), \
|
||||
PAR("file_errors", is_file_errors, ABSOLUTE_FILE_NAME_FILE_ERRORS), \
|
||||
PAR("file_type", is_file_type, ABSOLUTE_FILE_NAME_FILE_TYPE), \
|
||||
@ -1817,8 +1802,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)
|
||||
@ -1831,8 +1815,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)
|
||||
@ -1849,8 +1832,7 @@ 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));
|
||||
|
17
os/iopreds.h
17
os/iopreds.h
@ -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_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)(
|
||||
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;
|
||||
|
||||
|
96
os/mem.c
96
os/mem.c
@ -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
|
||||
@ -55,6 +55,11 @@ static char SccsId[] = "%W% %G%";
|
||||
FILE *open_memstream(char **buf, size_t *len);
|
||||
#endif
|
||||
|
||||
#if __ANDROID__
|
||||
#undef HAVE_FMEMOPEN
|
||||
#undef HAVE_OPEN_MEMSTREAM
|
||||
#endif
|
||||
|
||||
#if HAVE_FMEMOPEN
|
||||
#define MAY_READ 1
|
||||
#endif
|
||||
@ -73,9 +78,7 @@ FILE * open_memstream (char **buf, size_t *len);
|
||||
static int MemGetc(int);
|
||||
|
||||
/* read from memory */
|
||||
static int
|
||||
MemGetc(int sno)
|
||||
{
|
||||
static int MemGetc(int sno) {
|
||||
register StreamDesc *s = &GLOBAL_Stream[sno];
|
||||
Int ch;
|
||||
int spos;
|
||||
@ -83,8 +86,7 @@ MemGetc(int sno)
|
||||
spos = s->u.mem_string.pos;
|
||||
if (spos == s->u.mem_string.max_size) {
|
||||
return -1;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ch = s->u.mem_string.buf[spos];
|
||||
s->u.mem_string.pos = ++spos;
|
||||
}
|
||||
@ -96,13 +98,10 @@ MemGetc(int sno)
|
||||
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)
|
||||
{
|
||||
if (ch == 10) {
|
||||
ch = '\n';
|
||||
}
|
||||
#endif
|
||||
@ -118,7 +117,8 @@ MemPutc(int sno, int ch)
|
||||
(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;
|
||||
@ -131,7 +131,8 @@ MemPutc(int sno, int ch)
|
||||
#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 {
|
||||
@ -141,7 +142,8 @@ MemPutc(int sno, int ch)
|
||||
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;
|
||||
}
|
||||
@ -157,10 +159,8 @@ MemPutc(int sno, 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;
|
||||
@ -170,7 +170,8 @@ MemPutc(int sno, int ch)
|
||||
|
||||
sno = GetFreeStreamD();
|
||||
if (sno < 0)
|
||||
return (PlIOError (RESOURCE_ERROR_MAX_STREAMS,TermNil, "new stream not available for open_mem_read_stream/1"));
|
||||
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;
|
||||
@ -184,8 +185,7 @@ MemPutc(int sno, int ch)
|
||||
f = NULL;
|
||||
flags = Input_Stream_f | InMemory_Stream_f;
|
||||
#endif
|
||||
Yap_initStream(sno, f, NULL, TermNil,
|
||||
encoding, flags, AtomRead);
|
||||
Yap_initStream(sno, f, NULL, TermNil, encoding, flags, AtomRead);
|
||||
// like any file stream.
|
||||
#if !MAY_READ
|
||||
/* currently these streams are not seekable */
|
||||
@ -248,14 +248,12 @@ open_mem_read_stream (USES_REGS1) /* $open_mem_read_stream(+List,-Stream) */
|
||||
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;
|
||||
|
||||
|
||||
sno = GetFreeStreamD();
|
||||
if (sno < 0)
|
||||
return -1;
|
||||
@ -294,20 +292,19 @@ Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSou
|
||||
return sno;
|
||||
}
|
||||
|
||||
int
|
||||
Yap_OpenBufWriteStream( USES_REGS1 )
|
||||
{
|
||||
int Yap_OpenBufWriteStream(USES_REGS1) {
|
||||
char *nbuf;
|
||||
size_t sz = Yap_page_size;
|
||||
|
||||
|
||||
while ((nbuf = (char *)Yap_AllocAtomSpace(Yap_page_size*sizeof(char))) == NULL) {
|
||||
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
|
||||
@ -318,7 +315,8 @@ open_mem_write_stream (USES_REGS1) /* $open_mem_write_stream(-Stream) */
|
||||
|
||||
sno = Yap_OpenBufWriteStream(PASS_REGS1);
|
||||
if (sno == -1)
|
||||
return (PlIOError (SYSTEM_ERROR_INTERNAL,TermNil, "new stream not available for open_mem_read_stream/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));
|
||||
}
|
||||
@ -331,13 +329,10 @@ open_mem_write_stream (USES_REGS1) /* $open_mem_write_stream(-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)
|
||||
{
|
||||
if (fflush(GLOBAL_Stream[sno].file) == 0) {
|
||||
s = GLOBAL_Stream[sno].nbuf;
|
||||
return s;
|
||||
}
|
||||
@ -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;
|
||||
@ -391,9 +385,7 @@ peek_mem_write_stream ( USES_REGS1 )
|
||||
return (Yap_unify(ARG3, tf));
|
||||
}
|
||||
|
||||
void
|
||||
Yap_MemOps( StreamDesc *st )
|
||||
{
|
||||
void Yap_MemOps(StreamDesc *st) {
|
||||
#if MAY_WRITE
|
||||
st->stream_putc = FilePutc;
|
||||
#else
|
||||
@ -407,8 +399,7 @@ void
|
||||
#endif
|
||||
}
|
||||
|
||||
bool Yap_CloseMemoryStream( int sno )
|
||||
{
|
||||
bool Yap_CloseMemoryStream(int sno) {
|
||||
if (!(GLOBAL_Stream[sno].status & Output_Stream_f)) {
|
||||
#if MAY_WRITE
|
||||
fclose(GLOBAL_Stream[sno].file);
|
||||
@ -436,15 +427,14 @@ bool Yap_CloseMemoryStream( int sno )
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
Yap_InitMems( void )
|
||||
{
|
||||
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_write_stream", 1, open_mem_write_stream,
|
||||
SyncPredFlag);
|
||||
Yap_InitCPred("peek_mem_write_stream", 3, peek_mem_write_stream,
|
||||
SyncPredFlag);
|
||||
CurrentModule = cm;
|
||||
}
|
||||
|
||||
|
196
os/sig.c
196
os/sig.c
@ -29,12 +29,11 @@ static void HandleMatherr(int sig, void *sipv, void *uapv);
|
||||
#define SIG_FREECLAUSES (SIG_PROLOG_OFFSET + 4)
|
||||
#define SIG_PLABORT (SIG_PROLOG_OFFSET + 5)
|
||||
|
||||
static struct signame
|
||||
{ int sig;
|
||||
static struct signame {
|
||||
int sig;
|
||||
const char *name;
|
||||
int flags;
|
||||
} signames[] =
|
||||
{
|
||||
} signames[] = {
|
||||
#ifdef SIGHUP
|
||||
{SIGHUP, "hup", 0},
|
||||
#endif
|
||||
@ -124,15 +123,12 @@ static struct signame
|
||||
{SIG_THREAD_SIGNAL, "prolog:thread_signal", 0},
|
||||
#endif
|
||||
|
||||
{ -1, NULL, 0}
|
||||
};
|
||||
{-1, NULL, 0}};
|
||||
|
||||
typedef void (*signal_handler_t)(int, void *, void *);
|
||||
|
||||
#if HAVE_SIGACTION
|
||||
static void
|
||||
my_signal_info(int sig, void * handler)
|
||||
{
|
||||
static void my_signal_info(int sig, void *handler) {
|
||||
struct sigaction sigact;
|
||||
|
||||
sigact.sa_handler = handler;
|
||||
@ -142,9 +138,7 @@ my_signal_info(int sig, void * handler)
|
||||
sigaction(sig, &sigact, NULL);
|
||||
}
|
||||
|
||||
static void
|
||||
my_signal(int sig, void * handler)
|
||||
{
|
||||
static void my_signal(int sig, void *handler) {
|
||||
struct sigaction sigact;
|
||||
|
||||
sigact.sa_handler = (void *)handler;
|
||||
@ -155,36 +149,29 @@ my_signal(int sig, void * handler)
|
||||
|
||||
#else
|
||||
|
||||
static void
|
||||
my_signal(int sig, void *handler)
|
||||
{
|
||||
signal(sig, handler);
|
||||
}
|
||||
static void my_signal(int sig, void *handler) { signal(sig, handler); }
|
||||
|
||||
static void
|
||||
my_signal_info(int sig, void *handler)
|
||||
{
|
||||
static void my_signal_info(int sig, void *handler) {
|
||||
if (signal(sig, (void *)handler) == SIG_ERR)
|
||||
exit(1);
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
/* SWI emulation */
|
||||
int
|
||||
Yap_signal_index(const char *name)
|
||||
{ struct signame *sn = signames;
|
||||
int Yap_signal_index(const char *name) {
|
||||
struct signame *sn = signames;
|
||||
char tmp[12];
|
||||
|
||||
if ( strncmp(name, "SIG", 3) == 0 && strlen(name) < 12 )
|
||||
{ char *p = (char *)name+3, *q = tmp;
|
||||
while ((*q++ = tolower(*p++))) {};
|
||||
if (strncmp(name, "SIG", 3) == 0 && strlen(name) < 12) {
|
||||
char *p = (char *)name + 3, *q = tmp;
|
||||
while ((*q++ = tolower(*p++))) {
|
||||
};
|
||||
name = tmp;
|
||||
}
|
||||
|
||||
for( ; sn->name; sn++ )
|
||||
{ if ( !strcmp(sn->name, name) )
|
||||
for (; sn->name; sn++) {
|
||||
if (!strcmp(sn->name, name))
|
||||
return sn->sig;
|
||||
}
|
||||
|
||||
@ -199,9 +186,7 @@ Yap_signal_index(const char *name)
|
||||
#endif
|
||||
|
||||
#if HAVE_SIGSEGV
|
||||
static void
|
||||
SearchForTrailFault(void *ptr, int sure)
|
||||
{
|
||||
static void SearchForTrailFault(void *ptr, int sure) {
|
||||
|
||||
/* If the TRAIL is very close to the top of mmaped allocked space,
|
||||
then we can try increasing the TR space and restarting the
|
||||
@ -212,7 +197,8 @@ SearchForTrailFault(void *ptr, int sure)
|
||||
if ((ptr > (void *)LOCAL_TrailTop - 1024 &&
|
||||
TR < (tr_fr_ptr)LOCAL_TrailTop + (64 * 1024))) {
|
||||
if (!Yap_growtrail(64 * 1024, TRUE)) {
|
||||
Yap_Error(RESOURCE_ERROR_TRAIL, TermNil, "YAP failed to reserve %ld bytes in growtrail", K64);
|
||||
Yap_Error(RESOURCE_ERROR_TRAIL, TermNil,
|
||||
"YAP failed to reserve %ld bytes in growtrail", K64);
|
||||
}
|
||||
/* just in case, make sure the OS keeps the signal handler. */
|
||||
/* my_signal_info(SIGSEGV, HandleSIGSEGV); */
|
||||
@ -226,24 +212,21 @@ SearchForTrailFault(void *ptr, int sure)
|
||||
"likely bug in YAP, segmentation violation");
|
||||
}
|
||||
|
||||
|
||||
/* This routine believes there is a continuous space starting from the
|
||||
HeapBase and ending on TrailTop */
|
||||
static void
|
||||
HandleSIGSEGV(int sig, void *sipv, void *uap)
|
||||
{
|
||||
static void HandleSIGSEGV(int sig, void *sipv, void *uap) {
|
||||
CACHE_REGS
|
||||
|
||||
void *ptr = TR;
|
||||
int sure = FALSE;
|
||||
if (LOCAL_PrologMode & ExtendStackMode) {
|
||||
Yap_Error(SYSTEM_ERROR_FATAL, TermNil, "OS memory allocation crashed at address %p, bailing out\n",LOCAL_TrailTop);
|
||||
Yap_Error(SYSTEM_ERROR_FATAL, TermNil,
|
||||
"OS memory allocation crashed at address %p, bailing out\n",
|
||||
LOCAL_TrailTop);
|
||||
}
|
||||
#if (defined(__svr4__) || defined(__SVR4))
|
||||
siginfo_t *sip = sipv;
|
||||
if (
|
||||
sip->si_code != SI_NOINFO &&
|
||||
sip->si_code == SEGV_MAPERR) {
|
||||
if (sip->si_code != SI_NOINFO && sip->si_code == SEGV_MAPERR) {
|
||||
ptr = sip->si_addr;
|
||||
sure = TRUE;
|
||||
}
|
||||
@ -262,20 +245,21 @@ HandleSIGSEGV(int sig, void *sipv, void *uap)
|
||||
#include <fpu_control.h>
|
||||
#endif
|
||||
|
||||
/* by default Linux with glibc is IEEE compliant anyway..., but we will pretend it is not. */
|
||||
static bool
|
||||
set_fpu_exceptions(Term flag)
|
||||
{
|
||||
/* by default Linux with glibc is IEEE compliant anyway..., but we will pretend
|
||||
* it is not. */
|
||||
static bool set_fpu_exceptions(Term flag) {
|
||||
if (flag == TermTrue) {
|
||||
#if HAVE_FESETEXCEPTFLAG
|
||||
fexcept_t excepts;
|
||||
return fesetexceptflag(&excepts, FE_DIVBYZERO| FE_UNDERFLOW|FE_OVERFLOW) == 0;
|
||||
return fesetexceptflag(&excepts,
|
||||
FE_DIVBYZERO | FE_UNDERFLOW | FE_OVERFLOW) == 0;
|
||||
#elif HAVE_FEENABLEEXCEPT
|
||||
/* I shall ignore de-normalization and precision errors */
|
||||
feenableexcept(FE_DIVBYZERO | FE_INVALID | FE_OVERFLOW);
|
||||
#elif _WIN32
|
||||
// Enable zero-divide, overflow and underflow exception
|
||||
_controlfp_s(0, ~(_EM_ZERODIVIDE|_EM_UNDERFLOW|_EM_OVERFLOW), _MCW_EM); // Line B
|
||||
_controlfp_s(0, ~(_EM_ZERODIVIDE | _EM_UNDERFLOW | _EM_OVERFLOW),
|
||||
_MCW_EM); // Line B
|
||||
#elif defined(__hpux)
|
||||
#if HAVE_FESETTRAPENABLE
|
||||
/* From HP-UX 11.0 onwards: */
|
||||
@ -295,7 +279,8 @@ set_fpu_exceptions(Term flag)
|
||||
#endif /* __hpux */
|
||||
#if HAVE_FPU_CONTROL_H && i386 && defined(__GNUC__)
|
||||
/* I shall ignore denormalization and precision errors */
|
||||
int v = _FPU_IEEE & ~(_FPU_MASK_IM|_FPU_MASK_ZM|_FPU_MASK_OM|_FPU_MASK_UM);
|
||||
int v = _FPU_IEEE &
|
||||
~(_FPU_MASK_IM | _FPU_MASK_ZM | _FPU_MASK_OM | _FPU_MASK_UM);
|
||||
_FPU_SETCW(v);
|
||||
#endif
|
||||
#if HAVE_FETESTEXCEPT
|
||||
@ -314,7 +299,8 @@ set_fpu_exceptions(Term flag)
|
||||
feenableexcept(0);
|
||||
#elif _WIN32
|
||||
// Enable zero-divide, overflow and underflow exception
|
||||
_controlfp_s(0, (_EM_ZERODIVIDE|_EM_UNDERFLOW|_EM_OVERFLOW), _MCW_EM); // Line B
|
||||
_controlfp_s(0, (_EM_ZERODIVIDE | _EM_UNDERFLOW | _EM_OVERFLOW),
|
||||
_MCW_EM); // Line B
|
||||
#elif defined(__hpux)
|
||||
#if HAVE_FESETTRAPENABLE
|
||||
fesettrapenable(FE_ALL_EXCEPT);
|
||||
@ -334,16 +320,9 @@ set_fpu_exceptions(Term flag)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
Yap_set_fpu_exceptions(Term flag)
|
||||
{
|
||||
return set_fpu_exceptions(flag);
|
||||
}
|
||||
bool Yap_set_fpu_exceptions(Term flag) { return set_fpu_exceptions(flag); }
|
||||
|
||||
|
||||
yap_error_number
|
||||
Yap_MathException__( USES_REGS1 )
|
||||
{
|
||||
yap_error_number Yap_MathException__(USES_REGS1) {
|
||||
#if HAVE_FETESTEXCEPT
|
||||
int raised;
|
||||
|
||||
@ -415,9 +394,7 @@ Yap_MathException__( USES_REGS1 )
|
||||
return LOCAL_matherror;
|
||||
}
|
||||
|
||||
static Int
|
||||
p_fpe_error( USES_REGS1 )
|
||||
{
|
||||
static Int fpe_error(USES_REGS1) {
|
||||
Yap_Error(LOCAL_matherror, LOCAL_mathtt, LOCAL_mathstring);
|
||||
LOCAL_matherror = YAP_NO_ERROR;
|
||||
LOCAL_mathtt = TermNil;
|
||||
@ -425,9 +402,7 @@ p_fpe_error( USES_REGS1 )
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
static void
|
||||
HandleMatherr(int sig, void *sipv, void *uapv)
|
||||
{
|
||||
static void HandleMatherr(int sig, void *sipv, void *uapv) {
|
||||
CACHE_REGS
|
||||
LOCAL_matherror = Yap_MathException();
|
||||
/* reset the registers so that we don't have trash in abstract machine */
|
||||
@ -436,17 +411,12 @@ HandleMatherr(int sig, void *sipv, void *uapv)
|
||||
|
||||
#endif /* SIGFPE */
|
||||
|
||||
|
||||
|
||||
#if !defined(LIGHT) && !_MSC_VER && !defined(__MINGW32__) && !defined(LIGHT)
|
||||
static RETSIGTYPE
|
||||
ReceiveSignal (int s, void *x, void *y)
|
||||
{
|
||||
static RETSIGTYPE ReceiveSignal(int s, void *x, void *y) {
|
||||
CACHE_REGS
|
||||
LOCAL_PrologMode |= InterruptMode;
|
||||
my_signal(s, ReceiveSignal);
|
||||
switch (s)
|
||||
{
|
||||
switch (s) {
|
||||
case SIGINT:
|
||||
// always direct SIGINT to console
|
||||
Yap_external_signal(0, YAP_INT_SIGNAL);
|
||||
@ -505,8 +475,7 @@ ReceiveSignal (int s, void *x, void *y)
|
||||
#endif
|
||||
|
||||
#if (_MSC_VER || defined(__MINGW32__))
|
||||
static BOOL WINAPI
|
||||
MSCHandleSignal(DWORD dwCtrlType) {
|
||||
static BOOL WINAPI MSCHandleSignal(DWORD dwCtrlType) {
|
||||
if (
|
||||
#if THREADS
|
||||
REMOTE_InterruptsDisabled(0)
|
||||
@ -533,11 +502,8 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* SIGINT can cause problems, if caught before full initialization */
|
||||
void
|
||||
Yap_InitOSSignals (int wid)
|
||||
{
|
||||
void Yap_InitOSSignals(int wid) {
|
||||
if (GLOBAL_PrologShouldHandleInterrupts) {
|
||||
#if !defined(LIGHT) && !_MSC_VER && !defined(__MINGW32__) && !defined(LIGHT)
|
||||
my_signal(SIGQUIT, ReceiveSignal);
|
||||
@ -571,13 +537,10 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
|
||||
#endif /* HAVE_SIGNAL */
|
||||
|
||||
|
||||
/* wrapper for alarm system call */
|
||||
#if _MSC_VER || defined(__MINGW32__)
|
||||
|
||||
static DWORD WINAPI
|
||||
DoTimerThread(LPVOID targ)
|
||||
{
|
||||
static DWORD WINAPI DoTimerThread(LPVOID targ) {
|
||||
Int *time = (Int *)targ;
|
||||
HANDLE htimer;
|
||||
LARGE_INTEGER liDueTime;
|
||||
@ -604,9 +567,7 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
|
||||
#endif
|
||||
|
||||
static Int
|
||||
enable_interrupts( USES_REGS1 )
|
||||
{
|
||||
static Int enable_interrupts(USES_REGS1) {
|
||||
LOCAL_InterruptsDisabled--;
|
||||
if (LOCAL_Signals && !LOCAL_InterruptsDisabled) {
|
||||
CreepFlag = Unsigned(LCL0);
|
||||
@ -616,17 +577,13 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static Int
|
||||
disable_interrupts( USES_REGS1 )
|
||||
{
|
||||
static Int disable_interrupts(USES_REGS1) {
|
||||
LOCAL_InterruptsDisabled++;
|
||||
CalculateStackGap(PASS_REGS1);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
static Int
|
||||
alarm4( USES_REGS1 )
|
||||
{
|
||||
static Int alarm4(USES_REGS1) {
|
||||
Term t = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
Int i1, i2;
|
||||
@ -667,8 +624,7 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
DWORD dwThreadId;
|
||||
HANDLE hThread;
|
||||
|
||||
hThread = CreateThread(
|
||||
NULL, /* no security attributes */
|
||||
hThread = CreateThread(NULL, /* no security attributes */
|
||||
0, /* use default stack size */
|
||||
DoTimerThread, /* thread function */
|
||||
(LPVOID)time, /* argument to thread function */
|
||||
@ -693,7 +649,8 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
new.it_value.tv_usec = i2;
|
||||
if (setitimer(ITIMER_REAL, &new, &old) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "setitimer: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "setitimer: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "setitimer %d", errno);
|
||||
#endif
|
||||
@ -721,9 +678,7 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
#endif
|
||||
}
|
||||
|
||||
static Int
|
||||
virtual_alarm( USES_REGS1 )
|
||||
{
|
||||
static Int virtual_alarm(USES_REGS1) {
|
||||
Term t = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
if (IsVarTerm(t)) {
|
||||
@ -754,8 +709,7 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
DWORD dwThreadId;
|
||||
HANDLE hThread;
|
||||
|
||||
hThread = CreateThread(
|
||||
NULL, /* no security attributes */
|
||||
hThread = CreateThread(NULL, /* no security attributes */
|
||||
0, /* use default stack size */
|
||||
DoTimerThread, /* thread function */
|
||||
(LPVOID)time, /* argument to thread function */
|
||||
@ -780,7 +734,8 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
new.it_value.tv_usec = IntegerOfTerm(t2);
|
||||
if (setitimer(ITIMER_VIRTUAL, &new, &old) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "setitimer: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "setitimer: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, ARG1, "setitimer %d", errno);
|
||||
#endif
|
||||
@ -799,15 +754,13 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
#ifdef VAX
|
||||
|
||||
/* avoid longjmp botch */
|
||||
|
||||
int vax_absmi_fp;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
int eh;
|
||||
int flgs;
|
||||
int ap;
|
||||
@ -825,16 +778,12 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
|
||||
* VaxFramePtr;
|
||||
|
||||
|
||||
VaxFixFrame (dummy)
|
||||
{
|
||||
VaxFixFrame(dummy) {
|
||||
int maxframes = 100;
|
||||
VaxFramePtr fp = (VaxFramePtr)(((int *)&dummy) - 6);
|
||||
while (--maxframes)
|
||||
{
|
||||
while (--maxframes) {
|
||||
fp = (VaxFramePtr)fp->fp;
|
||||
if (fp->flgs == 0)
|
||||
{
|
||||
if (fp->flgs == 0) {
|
||||
if (fp->oldfp >= ®S[6] && fp->oldfp < ®S[REG_SIZE])
|
||||
fp->oldfp = vax_absmi_fp;
|
||||
return;
|
||||
@ -844,17 +793,14 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(_WIN32)
|
||||
|
||||
#include <windows.h>
|
||||
|
||||
int WINAPI win_yap(HANDLE, DWORD, LPVOID);
|
||||
|
||||
int WINAPI win_yap(HANDLE hinst, DWORD reason, LPVOID reserved)
|
||||
{
|
||||
switch (reason)
|
||||
{
|
||||
int WINAPI win_yap(HANDLE hinst, DWORD reason, LPVOID reserved) {
|
||||
switch (reason) {
|
||||
case DLL_PROCESS_ATTACH:
|
||||
break;
|
||||
case DLL_PROCESS_DETACH:
|
||||
@ -872,13 +818,12 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
#ifdef sparc
|
||||
void rw_lock_voodoo(void);
|
||||
|
||||
void
|
||||
rw_lock_voodoo(void) {
|
||||
void rw_lock_voodoo(void) {
|
||||
/* code taken from the Linux kernel, it handles shifting between locks */
|
||||
/* Read/writer locks, as usual this is overly clever to make it as fast as possible. */
|
||||
/* Read/writer locks, as usual this is overly clever to make it as fast as
|
||||
* possible. */
|
||||
/* caches... */
|
||||
__asm__ __volatile__(
|
||||
"___rw_read_enter_spin_on_wlock:\n"
|
||||
__asm__ __volatile__("___rw_read_enter_spin_on_wlock:\n"
|
||||
" orcc %g2, 0x0, %g0\n"
|
||||
" be,a ___rw_read_enter\n"
|
||||
" ldstub [%g1 + 3], %g2\n"
|
||||
@ -926,21 +871,17 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
" bne,a ___rw_write_enter_spin_on_wlock\n"
|
||||
" stb %g0, [%g1 + 3]\n"
|
||||
" retl\n"
|
||||
" mov %g4, %o7\n"
|
||||
);
|
||||
" mov %g4, %o7\n");
|
||||
}
|
||||
#endif /* sparc */
|
||||
|
||||
|
||||
#endif /* YAPOR || THREADS */
|
||||
|
||||
void
|
||||
Yap_InitSignalPreds(void)
|
||||
{
|
||||
void Yap_InitSignalPreds(void) {
|
||||
CACHE_REGS
|
||||
Term cm = CurrentModule;
|
||||
|
||||
Yap_InitCPred ("$fpe_error", 0, p_fpe_error, 0);
|
||||
Yap_InitCPred("$fpe_error", 0, fpe_error, 0);
|
||||
Yap_InitCPred("$alarm", 4, alarm4, SafePredFlag | SyncPredFlag);
|
||||
CurrentModule = HACKS_MODULE;
|
||||
Yap_InitCPred("virtual_alarm", 4, virtual_alarm, SafePredFlag | SyncPredFlag);
|
||||
@ -948,6 +889,3 @@ MSCHandleSignal(DWORD dwCtrlType) {
|
||||
Yap_InitCPred("disable_interrupts", 0, disable_interrupts, SafePredFlag);
|
||||
CurrentModule = cm;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
@ -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;
|
||||
|
273
os/ypsocks.c
273
os/ypsocks.c
@ -15,7 +15,6 @@
|
||||
* *
|
||||
*************************************************************************/
|
||||
|
||||
|
||||
#include "sysbits.h"
|
||||
|
||||
#if HAVE_SOCKET
|
||||
@ -176,21 +175,19 @@
|
||||
#define invalid_socket_fd(fd) (fd) < 0
|
||||
#endif
|
||||
|
||||
void
|
||||
Yap_init_socks(char *host, long interface_port)
|
||||
{
|
||||
void Yap_init_socks(char *host, long interface_port) {
|
||||
int s;
|
||||
int r;
|
||||
struct sockaddr_in soadr;
|
||||
struct in_addr adr;
|
||||
struct hostent *he;
|
||||
|
||||
|
||||
#if HAVE_SOCKET
|
||||
he = gethostbyname(host);
|
||||
if (he == NULL) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "can not get address for host %s: %s", host, strerror(h_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"can not get address for host %s: %s", host, strerror(h_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "can not get address for host");
|
||||
#endif
|
||||
@ -202,8 +199,7 @@ Yap_init_socks(char *host, long interface_port)
|
||||
soadr.sin_port = htons((short)interface_port);
|
||||
|
||||
if (he != NULL) {
|
||||
memcpy((char *) &adr,
|
||||
(char *) he->h_addr_list[0], (size_t) he->h_length);
|
||||
memcpy((char *)&adr, (char *)he->h_addr_list[0], (size_t)he->h_length);
|
||||
} else {
|
||||
adr.s_addr = inet_addr(host);
|
||||
}
|
||||
@ -212,7 +208,8 @@ Yap_init_socks(char *host, long interface_port)
|
||||
s = socket(AF_INET, SOCK_STREAM, 0);
|
||||
if (s < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not create socket: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not create socket: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not create socket");
|
||||
#endif
|
||||
@ -223,11 +220,11 @@ Yap_init_socks(char *host, long interface_port)
|
||||
struct linger ling; /* disables socket lingering. */
|
||||
ling.l_onoff = 1;
|
||||
ling.l_linger = 0;
|
||||
if (setsockopt(s, SOL_SOCKET, SO_LINGER, (void *) &ling,
|
||||
sizeof(ling)) < 0) {
|
||||
if (setsockopt(s, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling)) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno));
|
||||
"socket_connect/3 (setsockopt_linger: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (setsockopt_linger)");
|
||||
@ -239,9 +236,12 @@ Yap_init_socks(char *host, long interface_port)
|
||||
r = connect(s, (struct sockaddr *)&soadr, sizeof(soadr));
|
||||
if (r < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "connect failed, could not connect to interface: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"connect failed, could not connect to interface: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "connect failed, could not connect to interface");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"connect failed, could not connect to interface");
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
@ -249,7 +249,8 @@ Yap_init_socks(char *host, long interface_port)
|
||||
#if HAVE_DUP2 && !defined(__MINGW32__)
|
||||
if (dup2(s, 0) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stdin: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stdin: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stdin");
|
||||
#endif
|
||||
@ -257,7 +258,8 @@ Yap_init_socks(char *host, long interface_port)
|
||||
}
|
||||
if (dup2(s, 1) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stdout: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stdout: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stdout");
|
||||
#endif
|
||||
@ -265,7 +267,8 @@ Yap_init_socks(char *host, long interface_port)
|
||||
}
|
||||
if (dup2(s, 2) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stderr: %s", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stderr: %s",
|
||||
strerror(errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "could not dup2 stderr");
|
||||
#endif
|
||||
@ -311,13 +314,12 @@ Yap_init_socks(char *host, long interface_port)
|
||||
close(s);
|
||||
#endif
|
||||
#else /* HAVE_SOCKET */
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "sockets not installed", strerror(errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "sockets not installed",
|
||||
strerror(errno));
|
||||
#endif /* HAVE_SOCKET */
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket(USES_REGS1)
|
||||
{
|
||||
static Int p_socket(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
Term t3 = Deref(ARG3);
|
||||
@ -425,7 +427,8 @@ p_socket(USES_REGS1)
|
||||
break;
|
||||
}
|
||||
stype = RepAtom(AtomOfTerm(t2))->StrOfAE;
|
||||
if (stype[0] != 'S' || stype[1] != 'O' || stype[2] != 'C' || stype[3] != 'K' || stype[4] != '_')
|
||||
if (stype[0] != 'S' || stype[1] != 'O' || stype[2] != 'C' ||
|
||||
stype[3] != 'K' || stype[4] != '_')
|
||||
return (FALSE); /* Error */
|
||||
stype += 5;
|
||||
if (strcmp(stype, "STREAM") == 0)
|
||||
@ -448,11 +451,10 @@ p_socket(USES_REGS1)
|
||||
fd = socket(domain, type, protocol);
|
||||
if (invalid_socket_fd(fd)) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket/4 (socket: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket/4 (socket: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket/4 (socket)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket/4 (socket)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
@ -469,67 +471,60 @@ p_socket(USES_REGS1)
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
if (out == TermNil) return(FALSE);
|
||||
if (out == TermNil)
|
||||
return (FALSE);
|
||||
return (Yap_unify(out, ARG4));
|
||||
}
|
||||
|
||||
Int
|
||||
Yap_CloseSocket(int fd, socket_info status, socket_domain domain)
|
||||
{
|
||||
Int Yap_CloseSocket(int fd, socket_info status, socket_domain domain) {
|
||||
#if _MSC_VER || defined(__MINGW32__)
|
||||
/* prevent further writing
|
||||
to the socket */
|
||||
if (status == server_session_socket ||
|
||||
status == client_socket) {
|
||||
if (status == server_session_socket || status == client_socket) {
|
||||
char bfr;
|
||||
|
||||
if (shutdown(fd, 1) != 0) {
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (close)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (close)");
|
||||
return (FALSE);
|
||||
}
|
||||
/* read all pending characters
|
||||
from the socket */
|
||||
while( recv( fd, &bfr, 1, 0 ) > 0 );
|
||||
while (recv(fd, &bfr, 1, 0) > 0)
|
||||
;
|
||||
/* prevent further reading
|
||||
from the socket */
|
||||
if (shutdown(fd, 0) < 0) {
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (close)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (close)");
|
||||
return (FALSE);
|
||||
}
|
||||
|
||||
/* close the socket */
|
||||
if (closesocket(fd) != 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (close: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (close: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (close)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (close)");
|
||||
#endif
|
||||
}
|
||||
#else
|
||||
if (status == server_session_socket ||
|
||||
status == client_socket) {
|
||||
if (status == server_session_socket || status == client_socket) {
|
||||
if (shutdown(fd, 2) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (shutdown: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (shutdown: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (shutdown)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (shutdown)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
}
|
||||
if (close(fd) != 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (close: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (close: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_close/1 (close)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_close/1 (close)");
|
||||
#endif
|
||||
#endif
|
||||
return (FALSE);
|
||||
@ -537,9 +532,7 @@ Yap_CloseSocket(int fd, socket_info status, socket_domain domain)
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_close(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_close(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
int sno;
|
||||
|
||||
@ -550,9 +543,7 @@ p_socket_close(USES_REGS1)
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_bind(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_bind(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
int sno;
|
||||
@ -601,16 +592,13 @@ p_socket_bind(USES_REGS1)
|
||||
}
|
||||
sock.sun_family = AF_UNIX;
|
||||
strcpy(sock.sun_path, s);
|
||||
if (bind(fd,
|
||||
(struct sockaddr *)(&sock),
|
||||
((size_t) (((struct sockaddr_un *) 0)->sun_path) + len))
|
||||
< 0) {
|
||||
if (bind(fd, (struct sockaddr *)(&sock),
|
||||
((size_t)(((struct sockaddr_un *)0)->sun_path) + len)) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_bind/2 (bind: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_bind/2 (bind: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_bind/2 (bind)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_bind/2 (bind)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
@ -655,11 +643,10 @@ p_socket_bind(USES_REGS1)
|
||||
saddr.sin_family = AF_INET;
|
||||
if (bind(fd, (struct sockaddr *)&saddr, sizeof(saddr)) == -1) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_bind/2 (bind: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_bind/2 (bind: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_bind/2 (bind)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_bind/2 (bind)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
@ -669,7 +656,7 @@ p_socket_bind(USES_REGS1)
|
||||
#if _WIN32 || defined(__MINGW32__)
|
||||
int namelen;
|
||||
#else
|
||||
unsigned int namelen;
|
||||
socklen_t namelen;
|
||||
#endif
|
||||
Term t;
|
||||
if (getsockname(fd, (struct sockaddr *)&saddr, &namelen) < 0) {
|
||||
@ -691,9 +678,7 @@ p_socket_bind(USES_REGS1)
|
||||
return (FALSE);
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_connect(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_connect(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
Functor fun;
|
||||
@ -744,16 +729,14 @@ p_socket_connect(USES_REGS1)
|
||||
}
|
||||
sock.sun_family = AF_UNIX;
|
||||
strcpy(sock.sun_path, s);
|
||||
if ((flag = connect(fd,
|
||||
(struct sockaddr *)(&sock),
|
||||
((size_t) (((struct sockaddr_un *) 0)->sun_path) + len)))
|
||||
< 0) {
|
||||
if ((flag = connect(
|
||||
fd, (struct sockaddr *)(&sock),
|
||||
((size_t)(((struct sockaddr_un *)0)->sun_path) + len))) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (connect: %s)", strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (connect)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_connect/3 (connect)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
@ -780,7 +763,8 @@ p_socket_connect(USES_REGS1)
|
||||
if ((he = gethostbyname(shost)) == NULL) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (gethostbyname: %s)", strerror(socket_errno));
|
||||
"socket_connect/3 (gethostbyname: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (gethostbyname)");
|
||||
@ -803,14 +787,16 @@ p_socket_connect(USES_REGS1)
|
||||
#if ENABLE_SO_LINGER
|
||||
{
|
||||
struct linger ling; /* For making sockets linger. */
|
||||
/* disabled: I see why no reason why we should throw things away by default!! */
|
||||
/* disabled: I see why no reason why we should throw things away by
|
||||
* default!! */
|
||||
ling.l_onoff = 1;
|
||||
ling.l_linger = 0;
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *) &ling,
|
||||
sizeof(ling)) < 0) {
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *)&ling, sizeof(ling)) <
|
||||
0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno));
|
||||
"socket_connect/3 (setsockopt_linger: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (setsockopt_linger)");
|
||||
@ -826,7 +812,8 @@ p_socket_connect(USES_REGS1)
|
||||
if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(one))) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (setsockopt_broadcast: %s)", strerror(socket_errno));
|
||||
"socket_connect/3 (setsockopt_broadcast: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (setsockopt_broadcast)");
|
||||
@ -841,8 +828,7 @@ p_socket_connect(USES_REGS1)
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (connect: %s)", strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_connect/3 (connect)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_connect/3 (connect)");
|
||||
#endif
|
||||
return FALSE;
|
||||
}
|
||||
@ -853,9 +839,7 @@ p_socket_connect(USES_REGS1)
|
||||
return (Yap_unify(out, ARG3));
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_listen(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_listen(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
int sno;
|
||||
@ -887,19 +871,16 @@ p_socket_listen(USES_REGS1)
|
||||
}
|
||||
if (listen(fd, j) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_listen/2 (listen: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_listen/2 (listen: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_listen/2 (listen)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_listen/2 (listen)");
|
||||
#endif
|
||||
}
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_accept(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_accept(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
int sno;
|
||||
socket_info status;
|
||||
@ -920,16 +901,15 @@ p_socket_accept(USES_REGS1)
|
||||
#if HAVE_SYS_UN_H
|
||||
if (domain == af_unix) {
|
||||
struct sockaddr_un caddr;
|
||||
unsigned int len;
|
||||
socklen_t len;
|
||||
|
||||
memset((void *)&caddr, (int)0, sizeof(caddr));
|
||||
if ((fd = accept(ofd, (struct sockaddr *)&caddr, &len)) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_accept/3 (accept: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_accept/3 (accept: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_accept/3 (accept)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_accept/3 (accept)");
|
||||
#endif
|
||||
}
|
||||
/* ignore 2nd argument */
|
||||
@ -940,21 +920,16 @@ p_socket_accept(USES_REGS1)
|
||||
struct sockaddr_in caddr;
|
||||
Term tcli;
|
||||
char *s;
|
||||
#if _WIN32 || defined(__MINGW32__)
|
||||
int len;
|
||||
#else
|
||||
unsigned int len;
|
||||
#endif
|
||||
socklen_t len;
|
||||
|
||||
len = sizeof(caddr);
|
||||
memset((void *)&caddr, (int)0, sizeof(caddr));
|
||||
if (invalid_socket_fd(fd = accept(ofd, (struct sockaddr *)&caddr, &len))) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_accept/3 (accept: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_accept/3 (accept: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_accept/3 (accept)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_accept/3 (accept)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
@ -963,8 +938,7 @@ p_socket_accept(USES_REGS1)
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_accept/3 (inet_ntoa: %s)", strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_accept/3 (inet_ntoa)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_accept/3 (inet_ntoa)");
|
||||
#endif
|
||||
}
|
||||
tcli = MkAtomTerm(Yap_LookupAtom(s));
|
||||
@ -973,13 +947,12 @@ p_socket_accept(USES_REGS1)
|
||||
out = Yap_InitSocketStream(fd, server_session_socket, af_inet);
|
||||
} else
|
||||
return (FALSE);
|
||||
if (out == TermNil) return(FALSE);
|
||||
if (out == TermNil)
|
||||
return (FALSE);
|
||||
return (Yap_unify(out, ARG3));
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_buffering(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_buffering(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
Term t4 = Deref(ARG4);
|
||||
@ -988,12 +961,11 @@ p_socket_buffering(USES_REGS1)
|
||||
int writing;
|
||||
#if _WIN32 || defined(__MINGW32__)
|
||||
int bufsize;
|
||||
int len;
|
||||
#else
|
||||
unsigned int bufsize;
|
||||
unsigned int len;
|
||||
#endif
|
||||
int sno;
|
||||
socklen_t len;
|
||||
|
||||
if ((sno = Yap_CheckSocketStream(t1, "socket_buffering/4")) < 0) {
|
||||
return (FALSE);
|
||||
@ -1046,9 +1018,7 @@ p_socket_buffering(USES_REGS1)
|
||||
return (TRUE);
|
||||
}
|
||||
|
||||
static Term
|
||||
select_out_list(Term t1, fd_set *readfds_ptr USES_REGS)
|
||||
{
|
||||
static Term select_out_list(Term t1, fd_set *readfds_ptr USES_REGS) {
|
||||
if (t1 == TermNil) {
|
||||
return (TermNil);
|
||||
} else {
|
||||
@ -1066,9 +1036,7 @@ select_out_list(Term t1, fd_set *readfds_ptr USES_REGS)
|
||||
}
|
||||
}
|
||||
|
||||
static Int
|
||||
p_socket_select(USES_REGS1)
|
||||
{
|
||||
static Int p_socket_select(USES_REGS1) {
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
Term t3 = Deref(ARG3);
|
||||
@ -1143,11 +1111,10 @@ p_socket_select(USES_REGS1)
|
||||
/* do the real work */
|
||||
if (select(fdmax + 1, &readfds, &writefds, &exceptfds, ptime) < 0) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_select/5 (select: %s)", strerror(socket_errno));
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_select/5 (select: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"socket_select/5 (select)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "socket_select/5 (select)");
|
||||
#endif
|
||||
}
|
||||
tout = select_out_list(t1, &readfds PASS_REGS);
|
||||
@ -1155,9 +1122,7 @@ p_socket_select(USES_REGS1)
|
||||
return (Yap_unify(ARG4, tout));
|
||||
}
|
||||
|
||||
|
||||
static Int
|
||||
p_current_host(USES_REGS1) {
|
||||
static Int p_current_host(USES_REGS1) {
|
||||
char oname[MAXHOSTNAMELEN], *name;
|
||||
Term t1 = Deref(ARG1), out;
|
||||
|
||||
@ -1171,8 +1136,7 @@ p_current_host(USES_REGS1) {
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"current_host/2 (gethostname: %s)", strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"current_host/2 (gethostname)");
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "current_host/2 (gethostname)");
|
||||
#endif
|
||||
return (FALSE);
|
||||
}
|
||||
@ -1209,7 +1173,8 @@ p_current_host(USES_REGS1) {
|
||||
"current_host/2 (input longer than longest FAQ host name)");
|
||||
return (FALSE);
|
||||
}
|
||||
if (name[isize] != '.') return(FALSE);
|
||||
if (name[isize] != '.')
|
||||
return (FALSE);
|
||||
name[isize] = '\0';
|
||||
#if _MSC_VER || defined(__MINGW32__)
|
||||
return (_stricmp(name, sin) == 0);
|
||||
@ -1223,8 +1188,7 @@ p_current_host(USES_REGS1) {
|
||||
}
|
||||
}
|
||||
|
||||
static Int
|
||||
p_hostname_address(USES_REGS1) {
|
||||
static Int p_hostname_address(USES_REGS1) {
|
||||
char *s;
|
||||
Term t1 = Deref(ARG1);
|
||||
Term t2 = Deref(ARG2);
|
||||
@ -1235,20 +1199,23 @@ p_hostname_address(USES_REGS1) {
|
||||
if (!IsAtomTerm(t1)) {
|
||||
Yap_Error(TYPE_ERROR_ATOM, t1, "hostname_address/2");
|
||||
return (FALSE);
|
||||
} else tin = t1;
|
||||
} else
|
||||
tin = t1;
|
||||
} else if (IsVarTerm(t2)) {
|
||||
Yap_Error(INSTANTIATION_ERROR, t1, "hostname_address/5");
|
||||
return (FALSE);
|
||||
} else if (!IsAtomTerm(t2)) {
|
||||
Yap_Error(TYPE_ERROR_ATOM, t2, "hostname_address/2");
|
||||
return (FALSE);
|
||||
} else tin = t2;
|
||||
} else
|
||||
tin = t2;
|
||||
s = RepAtom(AtomOfTerm(tin))->StrOfAE;
|
||||
if (IsVarTerm(t1)) {
|
||||
if ((he = gethostbyaddr(s, strlen(s), AF_INET)) == NULL) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"hostname_address/2 (gethostbyname: %s)", strerror(socket_errno));
|
||||
"hostname_address/2 (gethostbyname: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"hostname_address/2 (gethostbyname)");
|
||||
@ -1261,32 +1228,35 @@ p_hostname_address(USES_REGS1) {
|
||||
if ((he = gethostbyname(s)) == NULL) {
|
||||
#if HAVE_STRERROR
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"hostname_address/2 (gethostbyname: %s)", strerror(socket_errno));
|
||||
"hostname_address/2 (gethostbyname: %s)",
|
||||
strerror(socket_errno));
|
||||
#else
|
||||
Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil,
|
||||
"hostname_address/2 (gethostbyname)");
|
||||
#endif
|
||||
}
|
||||
memcpy((char *) &adr,
|
||||
(char *) he->h_addr_list[0], (size_t) he->h_length);
|
||||
memcpy((char *)&adr, (char *)he->h_addr_list[0], (size_t)he->h_length);
|
||||
out = MkAtomTerm(Yap_LookupAtom(inet_ntoa(adr)));
|
||||
return (Yap_unify(out, ARG2));
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void
|
||||
Yap_InitSocketLayer(void)
|
||||
{
|
||||
void Yap_InitSocketLayer(void) {
|
||||
#ifdef HAVE_SOCKET
|
||||
Yap_InitCPred("socket", 4, p_socket, SafePredFlag | SyncPredFlag);
|
||||
Yap_InitCPred("socket_close", 1, p_socket_close, SafePredFlag | SyncPredFlag);
|
||||
Yap_InitCPred("socket_bind", 2, p_socket_bind, SafePredFlag | SyncPredFlag);
|
||||
Yap_InitCPred("socket_connect", 3, p_socket_connect, SafePredFlag|SyncPredFlag);
|
||||
Yap_InitCPred("socket_listen", 2, p_socket_listen, SafePredFlag|SyncPredFlag);
|
||||
Yap_InitCPred("socket_accept", 3, p_socket_accept, SafePredFlag|SyncPredFlag);
|
||||
Yap_InitCPred("$socket_buffering", 4, p_socket_buffering, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
||||
Yap_InitCPred("$socket_select", 4, p_socket_select, SafePredFlag|SyncPredFlag|HiddenPredFlag);
|
||||
Yap_InitCPred("socket_connect", 3, p_socket_connect,
|
||||
SafePredFlag | SyncPredFlag);
|
||||
Yap_InitCPred("socket_listen", 2, p_socket_listen,
|
||||
SafePredFlag | SyncPredFlag);
|
||||
Yap_InitCPred("socket_accept", 3, p_socket_accept,
|
||||
SafePredFlag | SyncPredFlag);
|
||||
Yap_InitCPred("$socket_buffering", 4, p_socket_buffering,
|
||||
SafePredFlag | SyncPredFlag | HiddenPredFlag);
|
||||
Yap_InitCPred("$socket_select", 4, p_socket_select,
|
||||
SafePredFlag | SyncPredFlag | HiddenPredFlag);
|
||||
Yap_InitCPred("current_host", 1, p_current_host, SafePredFlag);
|
||||
Yap_InitCPred("hostname_address", 2, p_hostname_address, SafePredFlag);
|
||||
#if _MSC_VER || defined(__MINGW32__)
|
||||
@ -1298,4 +1268,3 @@ Yap_InitSocketLayer(void)
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user