diff --git a/C/c_interface.c b/C/c_interface.c index ac03546f4..a01f18453 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -1560,7 +1560,7 @@ YAP_ExecuteFirst(PredEntry *pe, CPredicate exec_code) return TRUE; } } else { - (exec_code)(); + return (exec_code)(); } } @@ -2894,9 +2894,6 @@ YAP_Exit(int retval) X_API void YAP_InitSocks(char *host, long port) { -#if USE_SOCKET - Yap_init_socks(host, port); -#endif } X_API void diff --git a/C/iopreds.c b/C/iopreds.c index 672036cfa..c8296f5f8 100644 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -77,9 +77,6 @@ static char SccsId[] = "%W% %G%"; #define strncpy(X,Y,Z) strcpy(X,Y) #endif #if _MSC_VER || defined(__MINGW32__) -#if USE_SOCKET -#include -#endif #include #ifndef S_ISDIR #define S_ISDIR(x) (((x)&_S_IFDIR)==_S_IFDIR) @@ -93,10 +90,6 @@ STATIC_PROTO (int console_post_process_read_char, (int, StreamDesc *)); STATIC_PROTO (int console_post_process_eof, (StreamDesc *)); STATIC_PROTO (int post_process_read_char, (int, StreamDesc *)); STATIC_PROTO (int post_process_eof, (StreamDesc *)); -#if USE_SOCKET -STATIC_PROTO (int SocketPutc, (int, int)); -STATIC_PROTO (int ConsoleSocketPutc, (int, int)); -#endif STATIC_PROTO (int PipePutc, (int, int)); STATIC_PROTO (int ConsolePipePutc, (int, int)); STATIC_PROTO (int ConsolePutc, (int, int)); @@ -109,10 +102,6 @@ STATIC_PROTO (int ISOWGetc, (int)); STATIC_PROTO (int ConsoleGetc, (int)); STATIC_PROTO (int PipeGetc, (int)); STATIC_PROTO (int ConsolePipeGetc, (int)); -#if USE_SOCKET -STATIC_PROTO (int SocketGetc, (int)); -STATIC_PROTO (int ConsoleSocketGetc, (int)); -#endif #if HAVE_LIBREADLINE && HAVE_READLINE_READLINE_H STATIC_PROTO (int ReadlineGetc, (int)); STATIC_PROTO (int ReadlinePutc, (int,int)); @@ -262,7 +251,6 @@ yap_fflush(int sno) if ( (Stream[sno].status & Output_Stream_f) && ! (Stream[sno].status & (Null_Stream_f| - Socket_Stream_f| Pipe_Stream_f| Free_Stream_f)) ) { if (Stream[sno].status & SWI_Stream_f) { @@ -276,17 +264,6 @@ yap_fflush(int sno) static void unix_upd_stream_info (StreamDesc * s) { -#if USE_SOCKET - if (Yap_sockets_io && - s->u.file.file == NULL) - { - s->status |= Socket_Stream_f; - s->u.socket.domain = af_inet; - s->u.socket.flags = client_socket; - s->u.socket.fd = 0; - return; - } -#endif /* USE_SOCKET */ #if _MSC_VER || defined(__MINGW32__) { if ( @@ -346,11 +323,6 @@ p_always_prompt_user(void) s->status |= Promptable_Stream_f; s->stream_gets = DefaultGets; -#if USE_SOCKET - if (s->status & Socket_Stream_f) { - s->stream_getc = ConsoleSocketGetc; - } else -#endif #if HAVE_LIBREADLINE && HAVE_READLINE_READLINE_H if (s->status & Tty_Stream_f) { s->stream_getc = ReadlineGetc; @@ -390,14 +362,6 @@ static void InitFileIO(StreamDesc *s) { s->stream_gets = PlGetsFunc(); -#if USE_SOCKET - if (s->status & Socket_Stream_f) { - /* Console is a socket and socket will prompt */ - s->stream_putc = ConsoleSocketPutc; - s->stream_wputc = put_wchar; - s->stream_getc = ConsoleSocketGetc; - } else -#endif if (s->status & Pipe_Stream_f) { /* Console is a socket and socket will prompt */ s->stream_putc = ConsolePipePutc; @@ -486,15 +450,9 @@ InitStdStream (int sno, SMALLUNSGN flags, YP_File file) static void InitStdStreams (void) { - if (Yap_sockets_io) { - InitStdStream (StdInStream, Input_Stream_f, NULL); - InitStdStream (StdOutStream, Output_Stream_f, NULL); - InitStdStream (StdErrStream, Output_Stream_f, NULL); - } else { - InitStdStream (StdInStream, Input_Stream_f, stdin); - InitStdStream (StdOutStream, Output_Stream_f, stdout); - InitStdStream (StdErrStream, Output_Stream_f, stderr); - } + InitStdStream (StdInStream, Input_Stream_f, stdin); + InitStdStream (StdOutStream, Output_Stream_f, stdout); + InitStdStream (StdErrStream, Output_Stream_f, stderr); Yap_c_input_stream = StdInStream; Yap_c_output_stream = StdOutStream; Yap_c_error_stream = StdErrStream; @@ -797,66 +755,6 @@ IOSWIWideGetc(int sno) return ch; } -#if USE_SOCKET -/* static */ -static int -ConsoleSocketPutc (int sno, int ch) -{ - StreamDesc *s = &Stream[sno]; - char c = ch; -#if MAC || _MSC_VER - if (ch == 10) - { - ch = '\n'; - } -#endif -#if _MSC_VER || defined(__MINGW32__) - send(s->u.socket.fd, &c, sizeof(c), 0); -#else - if (write(s->u.socket.fd, &c, sizeof(c)) < 0) { -#if HAVE_STRERROR - Yap_Error(FATAL_ERROR, TermNil, "no access to console: %s", strerror(errno)); -#else - Yap_Error(FATAL_ERROR, TermNil, "no access to console"); -#endif - } -#endif - count_output_char(ch,s); - return ((int) ch); -} - -static int -SocketPutc (int sno, int ch) -{ - StreamDesc *s = &Stream[sno]; - char c = ch; -#if MAC || _MSC_VER - if (ch == 10) - { - ch = '\n'; - } -#endif -#if _MSC_VER || defined(__MINGW32__) - send(s->u.socket.fd, &c, sizeof(c), 0); -#else - { - int out = 0; - while (!out) { - out = write(s->u.socket.fd, &c, sizeof(c)); - if (out <0) { -#if HAVE_STRERROR - Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream socket: %s", strerror(errno)); -#else - Yap_Error(PERMISSION_ERROR_INPUT_STREAM, TermNil, "error writing stream socket"); -#endif - } - } - } -#endif - return (int) ch; -} - -#endif /* static */ static int @@ -1189,15 +1087,6 @@ EOFGetc(int sno) if (YP_feof (s->u.file.file)) YP_clearerr (s->u.file.file); /* reset our function for reading input */ -#if USE_SOCKET - if (s->status & Socket_Stream_f) { - if (s->status & Promptable_Stream_f) - s->stream_putc = ConsoleSocketPutc; - else - s->stream_putc = SocketPutc; - s->stream_wputc = put_wchar; - } else -#endif if (s->status & Pipe_Stream_f) { if (s->status & Promptable_Stream_f) s->stream_putc = ConsolePipePutc; @@ -1304,83 +1193,6 @@ console_post_process_eof(StreamDesc *s) return EOFCHAR; } -#if USE_SOCKET -/* - sockets cannot use standard FILE *, we have to go through fds, and in the - case of VC++, we have to use the receive routines... -*/ -static int -SocketGetc(int sno) -{ - register StreamDesc *s = &Stream[sno]; - register Int ch; - char c; - int count; - /* should be able to use a buffer */ -#if _MSC_VER || defined(__MINGW32__) - count = recv(s->u.socket.fd, &c, sizeof(char), 0); -#else - count = read(s->u.socket.fd, &c, sizeof(char)); -#endif - if (count == 0) { - s->u.socket.flags = closed_socket; - return post_process_eof(s); - } else if (count > 0) { - ch = c; - } else { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "( socket_getc: %s)", strerror(errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "(socket_getc)"); -#endif - return post_process_eof(s); - } - return post_process_read_char(ch, s); -} - -/* - Basically, the same as console but also sends a prompt and takes care of - finding out whether we are at the start of a newline. -*/ -static int -ConsoleSocketGetc(int sno) -{ - register StreamDesc *s = &Stream[sno]; - int ch; - Int c; - int count; - - /* send the prompt away */ - if (newline) { - char *cptr = Prompt, ch; - /* use the default routine */ - while ((ch = *cptr++) != '\0') { - Stream[StdErrStream].stream_putc(StdErrStream, ch); - } - strncpy(Prompt, RepAtom (AtPrompt)->StrOfAE, MAX_PROMPT); - newline = FALSE; - } - /* should be able to use a buffer */ - Yap_PrologMode |= ConsoleGetcMode; -#if _MSC_VER || defined(__MINGW32__) - count = recv(s->u.socket.fd, (void *)&c, sizeof(char), 0); -#else - count = read(s->u.socket.fd, &c, sizeof(char)); -#endif - Yap_PrologMode &= ~ConsoleGetcMode; - if (count == 0) { - return console_post_process_eof(s); - } else if (count > 0) { - ch = c; - } else { - Yap_Error(SYSTEM_ERROR, TermNil, "read"); - return console_post_process_eof(s); - } - return console_post_process_read_char(ch, s); -} -#endif static int PipeGetc(int sno) @@ -1593,11 +1405,7 @@ PlUnGetc (int sno) if (s->stream_getc != PlUnGetc) return(s->stream_getc(sno)); ch = s->och; - if (s->status & Socket_Stream_f) { - s->stream_getc = SocketGetc; - s->stream_putc = SocketPutc; - s->stream_wputc = put_wchar; - } else if (s->status & Promptable_Stream_f) { + if (s->status & Promptable_Stream_f) { s->stream_putc = ConsolePutc; s->stream_wputc = put_wchar; #if HAVE_LIBREADLINE && HAVE_READLINE_READLINE_H @@ -2043,11 +1851,6 @@ p_stream_flags (void) static Int GetStreamFd(int sno) { -#if USE_SOCKET - if (Stream[sno].status & Socket_Stream_f) { - return(Stream[sno].u.socket.fd); - } else -#endif if (Stream[sno].status & Pipe_Stream_f) { #if _MSC_VER || defined(__MINGW32__) return((Int)(Stream[sno].u.pipe.hdl)); @@ -2068,91 +1871,11 @@ Yap_GetStreamFd(int sno) int Yap_CheckIOStream(Term stream, char * error) { - int sno = CheckStream(stream, Input_Stream_f|Output_Stream_f|Socket_Stream_f, error); + int sno = CheckStream(stream, Input_Stream_f|Output_Stream_f, error); UNLOCK(Stream[sno].streamlock); return(sno); } -#if USE_SOCKET - -Term -Yap_InitSocketStream(int fd, socket_info flags, socket_domain domain) { - StreamDesc *st; - int sno; - - sno = GetFreeStreamD(); - if (sno < 0) { - PlIOError (SYSTEM_ERROR,TermNil, "new stream not available for socket/4"); - return(TermNil); - } - st = &Stream[sno]; - st->u.socket.domain = domain; - st->u.socket.flags = flags; - if (flags & (client_socket|server_session_socket)) { - /* I can read and write from these sockets */ - st->status = (Socket_Stream_f|Input_Stream_f|Output_Stream_f); - } else { - /* oops, I cannot */ - st->status = Socket_Stream_f; - } - st->u.socket.fd = fd; - st->charcount = 0; - st->linecount = 1; - st->linepos = 0; - st->stream_putc = SocketPutc; - st->stream_wputc = put_wchar; - st->stream_getc = SocketGetc; - st->stream_gets = DefaultGets; - st->stream_wgetc = get_wchar; - if (CharConversionTable != NULL) - st->stream_wgetc_for_read = ISOWGetc; - else - st->stream_wgetc_for_read = st->stream_wgetc; - UNLOCK(st->streamlock); - return(MkStream(sno)); -} - -/* given a socket file descriptor, get the corresponding stream descripor */ -int -Yap_CheckSocketStream(Term stream, char * error) -{ - int sno = CheckStream(stream, Socket_Stream_f, error); - UNLOCK(Stream[sno].streamlock); - return sno; -} - -/* given a stream index, get the corresponding domain */ -socket_domain -Yap_GetSocketDomain(int sno) -{ - return(Stream[sno].u.socket.domain); -} - -/* given a stream index, get the corresponding status */ -socket_info -Yap_GetSocketStatus(int sno) -{ - return(Stream[sno].u.socket.flags); -} - -/* update info on a socket, eg, new->server or new->client */ -void -Yap_UpdateSocketStream(int sno, socket_info flags, socket_domain domain) { - StreamDesc *st; - - st = &Stream[sno]; - st->u.socket.domain = domain; - st->u.socket.flags = flags; - if (flags & (client_socket|server_session_socket)) { - /* I can read and write from these sockets */ - st->status = (Socket_Stream_f|Input_Stream_f|Output_Stream_f); - } else { - /* oops, I cannot */ - st->status = Socket_Stream_f; - } -} - -#endif /* USE_SOCKET */ #if _MSC_VER || defined(__MINGW32__) #define SYSTEM_STAT _stat @@ -2279,7 +2002,7 @@ static Int p_change_alias_to_stream (void) return (FALSE); } at = AtomOfTerm(tname); - if ((sno = CheckStream (tstream, Input_Stream_f | Output_Stream_f | Append_Stream_f | Socket_Stream_f, "change_stream_alias/2")) == -1) { + if ((sno = CheckStream (tstream, Input_Stream_f | Output_Stream_f | Append_Stream_f, "change_stream_alias/2")) == -1) { UNLOCK(Stream[sno].streamlock); return(FALSE); } @@ -2449,7 +2172,7 @@ SetAlias (Atom arg, int sno) Int alno = aliasp-FileAliases; aliasp->alias_stream = sno; if (!(Stream[sno].status & - (Null_Stream_f|Socket_Stream_f))) { + (Null_Stream_f))) { switch(alno) { case 0: Yap_stdin = Stream[sno].u.file.file; @@ -2756,7 +2479,7 @@ p_check_stream (void) static Int p_check_if_stream (void) { /* '$check_stream'(Stream) */ - int sno = CheckStream (ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f | Socket_Stream_f, "check_stream/1"); + int sno = CheckStream (ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f , "check_stream/1"); if (sno != -1) UNLOCK(Stream[sno].streamlock); return sno != -1; @@ -2766,13 +2489,8 @@ static Term StreamName(int i) { if (i < 3) return(MkAtomTerm(AtomUser)); -#if USE_SOCKET - if (Stream[i].status & Socket_Stream_f) - return(MkAtomTerm(AtomSocket)); - else -#endif - if (Stream[i].status & Pipe_Stream_f) - return(MkAtomTerm(AtomPipe)); + if (Stream[i].status & Pipe_Stream_f) + return(MkAtomTerm(AtomPipe)); return(Stream[i].u.file.user_name); } @@ -2852,15 +2570,8 @@ Yap_CloseStreams (int loud) if (Stream[sno].status & Pipe_Stream_f) CloseHandle (Stream[sno].u.pipe.hdl); #else - if (Stream[sno].status & (Pipe_Stream_f|Socket_Stream_f)) + if (Stream[sno].status & (Pipe_Stream_f)) close (Stream[sno].u.pipe.fd); -#endif -#if USE_SOCKET - else if (Stream[sno].status & (Socket_Stream_f)) { - Yap_CloseSocket(Stream[sno].u.socket.fd, - Stream[sno].u.socket.flags, - Stream[sno].u.socket.domain); - } #endif else if (Stream[sno].status & (SWI_Stream_f)) { SWIClose(Stream[sno].u.swi_stream.swi_ptr); @@ -2883,15 +2594,8 @@ Yap_CloseStreams (int loud) static void CloseStream(int sno) { - if (!(Stream[sno].status & (Null_Stream_f|Socket_Stream_f|Pipe_Stream_f|SWI_Stream_f))) + if (!(Stream[sno].status & (Null_Stream_f|Pipe_Stream_f|SWI_Stream_f))) YP_fclose (Stream[sno].u.file.file); -#if USE_SOCKET - else if (Stream[sno].status & (Socket_Stream_f)) { - Yap_CloseSocket(Stream[sno].u.socket.fd, - Stream[sno].u.socket.flags, - Stream[sno].u.socket.domain); - } -#endif else if (Stream[sno].status & Pipe_Stream_f) { #if _MSC_VER || defined(__MINGW32__) CloseHandle (Stream[sno].u.pipe.hdl); @@ -2911,10 +2615,6 @@ CloseStream(int sno) { Yap_c_output_stream = StdOutStream; } - /* if (st->status == Socket_Stream_f|Input_Stream_f|Output_Stream_f) { - Yap_CloseSocket(); - } - */ } void @@ -2926,7 +2626,7 @@ Yap_CloseStream(int sno) static Int p_close (void) { /* '$close'(+Stream) */ - Int sno = CheckStream (ARG1, (Input_Stream_f | Output_Stream_f | Socket_Stream_f), "close/2"); + Int sno = CheckStream (ARG1, (Input_Stream_f | Output_Stream_f), "close/2"); if (sno < 0) return (FALSE); if (sno <= StdErrStream) { @@ -3769,11 +3469,6 @@ p_user_file_name (void) int sno = CheckStream (ARG1, Input_Stream_f | Output_Stream_f | Append_Stream_f,"user_file_name/2"); if (sno < 0) return (FALSE); -#if USE_SOCKET - if (Stream[sno].status & Socket_Stream_f) - tout = MkAtomTerm(AtomSocket); - else -#endif if (Stream[sno].status & Pipe_Stream_f) tout = MkAtomTerm(AtomPipe); else @@ -3796,18 +3491,13 @@ p_cur_line_no (void) Int no = 1; int i; Atom my_stream; -#if USE_SOCKET - if (Stream[sno].status & Socket_Stream_f) - my_stream = AtomSocket; - else -#endif if (Stream[sno].status & Pipe_Stream_f) my_stream = AtomPipe; else my_stream = Stream[sno].u.file.name; for (i = 0; i < MaxStreams; i++) { - if (!(Stream[i].status & (Free_Stream_f|Socket_Stream_f|Pipe_Stream_f)) && + if (!(Stream[i].status & (Free_Stream_f|Pipe_Stream_f)) && Stream[i].u.file.name == my_stream) no += Stream[i].linecount - 1; } @@ -5537,11 +5227,6 @@ Yap_StreamToFileNo(Term t) return((Int)(Stream[sno].u.pipe.hdl)); #else return(Stream[sno].u.pipe.fd); -#endif -#if USE_SOCKET - } else if (Stream[sno].status & Socket_Stream_f) { - UNLOCK(Stream[sno].streamlock); - return(Stream[sno].u.socket.fd); #endif } else { UNLOCK(Stream[sno].streamlock); @@ -5704,7 +5389,6 @@ Yap_FileDescriptorFromStream(Term t) if (sno < 0) return NULL; if (Stream[sno].status & (Null_Stream_f| - Socket_Stream_f| Pipe_Stream_f| Free_Stream_f)) return NULL; @@ -5805,9 +5489,6 @@ Yap_InitIOPreds(void) CurrentModule = cm; Yap_InitReadUtil (); -#if USE_SOCKET - Yap_InitSockets (); -#endif InitPlIO (); #if HAVE_LIBREADLINE && HAVE_READLINE_READLINE_H InitReadline(); diff --git a/C/ypsocks.c b/C/ypsocks.c deleted file mode 100755 index 97f4517bb..000000000 --- a/C/ypsocks.c +++ /dev/null @@ -1,1305 +0,0 @@ -/************************************************************************* -* * -* YAP Prolog %W% %G% -* * -* Yap Prolog was developed at NCCUP - Universidade do Porto * -* * -* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * -* * -************************************************************************** -* * -* File: io.h * -* Last rev: 19/2/88 * -* mods: * -* comments: control YAP from sockets. * -* * -*************************************************************************/ - - -#include "Yap.h" - -#include "Yatom.h" -#include "YapHeap.h" -#include "yapio.h" - -#if USE_SOCKET - -#if HAVE_UNISTD_H && !defined(__MINGW32__) && !_MSC_VER -#include -#endif -#if STDC_HEADERS -#include -#endif -#if HAVE_SYS_TYPES_H -#include -#endif -#if HAVE_SYS_TIME_H && !defined(__MINGW32__) && !_MSC_VER -#include -#endif -#ifdef _WIN32 -#if HAVE_IO_H -#include -#endif -#endif -#if _MSC_VER || defined(__MINGW32__) -#include -#include -#else -#if HAVE_SYS_SOCKET_H -#include -#endif -#if HAVE_SYS_UN_H -#include -#endif -#if HAVE_NETDB_H -#include -#endif -#if HAVE_NETINET_IN_H -#include -#endif -#if HAVE_ARPA_INET_H -#include -#endif -#if HAVE_FCNTL_H -#include -#endif -#if HAVE_STRING_H -#include -#endif -#if HAVE_SYS_SELECT_H -#include -#endif -#if HAVE_SYS_PARAM_H -#include -#endif -#endif - -/* make sure we can compile in any platform */ -#ifndef AF_UNSPEC -#define AF_UNSPEC 0 -#endif -#ifndef AF_LOCAL -#define AF_LOCAL AF_UNSPEC -#endif -#ifndef AF_AAL5 -#define AF_AAL5 AF_UNSPEC -#endif -#ifndef AF_APPLETALK -#define AF_APPLETALK AF_UNSPEC -#endif -#ifndef AF_AX25 -#define AF_AX25 AF_UNSPEC -#endif -#ifndef AF_BRIDGE -#define AF_BRIDGE AF_UNSPEC -#endif -#ifndef AF_DECnet -#define AF_DECnet AF_UNSPEC -#endif -#ifndef AF_FILE -#define AF_FILE AF_UNSPEC -#endif -#ifndef AF_INET -#define AF_INET AF_UNSPEC -#endif -#ifndef AF_INET6 -#define AF_INET6 AF_UNSPEC -#endif -#ifndef AF_IPX -#define AF_IPX AF_UNSPEC -#endif -#ifndef AF_LOCAL -#define AF_LOCAL AF_UNSPEC -#endif -#ifndef AF_NETBEUI -#define AF_NETBEUI AF_UNSPEC -#endif -#ifndef AF_NETLINK -#define AF_NETLINK AF_UNSPEC -#endif -#ifndef AF_NETROM -#define AF_NETROM AF_UNSPEC -#endif -#ifndef AF_OSINET -#define AF_OSINET AF_UNSPEC -#endif -#ifndef AF_PACKET -#define AF_PACKET AF_UNSPEC -#endif -#ifndef AF_ROSE -#define AF_ROSE AF_UNSPEC -#endif -#ifndef AF_ROUTE -#define AF_ROUTE AF_UNSPEC -#endif -#ifndef AF_SECURITY -#define AF_SECURITY AF_UNSPEC -#endif -#ifndef AF_SNA -#define AF_SNA AF_UNSPEC -#endif -#ifndef AF_UNIX -#define AF_UNIX AF_UNSPEC -#endif -#ifndef AF_X25 -#define AF_X25 AF_UNSPEC -#endif - -#ifndef SOCK_STREAM -#define SOCK_STREAM -1 -#endif -#ifndef SOCK_DGRAM -#define SOCK_DGRAM -1 -#endif -#ifndef SOCK_RAW -#define SOCK_RAW -1 -#endif -#ifndef SOCK_RDM -#define SOCK_RDM -1 -#endif -#ifndef SOCK_SEQPACKET -#define SOCK_SEQPACKET -1 -#endif -#ifndef SOCK_PACKET -#define SOCK_PACKET -1 -#endif - -#ifndef MAXHOSTNAMELEN -#define MAXHOSTNAMELEN 256 -#endif - -#ifndef BUFSIZ -#define BUFSIZ 256 -#endif - -#if _MSC_VER || defined(__MINGW32__) -#define socket_errno WSAGetLastError() -#define invalid_socket_fd(fd) (fd) == INVALID_SOCKET -#else -#define socket_errno errno -#define invalid_socket_fd(fd) (fd) < 0 -#endif - -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 USE_SOCKET - he = gethostbyname(host); - if (he == NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, "can not get address for host %s: %s", host, strerror(h_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, "can not get address for host"); -#endif - return; - } - - (void) memset((void *) &soadr, '\0', sizeof(struct sockaddr_in)); - soadr.sin_family = AF_INET; - soadr.sin_port = htons((short) interface_port); - - if (he != NULL) { - memcpy((char *) &adr, - (char *) he->h_addr_list[0], (size_t) he->h_length); - } else { - adr.s_addr = inet_addr(host); - } - soadr.sin_addr.s_addr = adr.s_addr; - - s = socket ( AF_INET, SOCK_STREAM, 0); - if (s<0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, "could not create socket: %s", strerror(errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, "could not create socket"); -#endif - return; - } - -#if ENABLE_SO_LINGER - 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 HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_linger)"); -#endif - return; - } -#endif - - r = connect ( s, (struct sockaddr *) &soadr, sizeof(soadr)); - if (r<0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, "connect failed, could not connect to interface: %s", strerror(errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, "connect failed, could not connect to interface"); -#endif - return; - } - /* now reopen stdin stdout and stderr */ -#if HAVE_DUP2 && !defined(__MINGW32__) - if(dup2(s,0)<0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, "could not dup2 stdin: %s", strerror(errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, "could not dup2 stdin"); -#endif - return; - } - if(dup2(s,1)<0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, "could not dup2 stdout: %s", strerror(errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, "could not dup2 stdout"); -#endif - return; - } - if(dup2(s,2)<0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, "could not dup2 stderr: %s", strerror(errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, "could not dup2 stderr"); -#endif - return; - } -#elif _MSC_VER || defined(__MINGW32__) - if(_dup2(s,0)<0) { - fprintf(stderr,"could not dup2 stdin\n"); - return; - } - if(_dup2(s,1)<0) { - fprintf(stderr,"could not dup2 stdout\n"); - return; - } - if(_dup2(s,2)<0) { - fprintf(stderr,"could not dup2 stderr\n"); - return; - } -#else - if(dup2(s,0)<0) { - fprintf(stderr,"could not dup2 stdin\n"); - return; - } - yp_iob[0].cnt = 0; - yp_iob[0].flags = _YP_IO_SOCK | _YP_IO_READ; - if(dup2(s,1)<0) { - fprintf(stderr,"could not dup2 stdout\n"); - return; - } - yp_iob[1].cnt = 0; - yp_iob[1].flags = _YP_IO_SOCK | _YP_IO_WRITE; - if(dup2(s,2)<0) { - fprintf(stderr,"could not dup2 stderr\n"); - return; - } - yp_iob[2].cnt = 0; - yp_iob[2].flags = _YP_IO_SOCK | _YP_IO_WRITE; -#endif - Yap_sockets_io = 1; -#if _MSC_VER || defined(__MINGW32__) - _close(s); -#else - close(s); -#endif -#else /* USE_SOCKET */ - Yap_Error(SYSTEM_ERROR, TermNil, "sockets not installed", strerror(errno)); -#endif /* USE_SOCKET */ -} - -static Int -p_socket(void) -{ - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - Term t3 = Deref(ARG3); - char *sdomain, *stype; - Int domain = AF_UNSPEC, type, protocol; - int fd; - Term out; - - if (IsVarTerm(t1)) { - Yap_Error(INSTANTIATION_ERROR,t1,"socket/4"); - return(FALSE); - } - if (!IsAtomTerm(t1)) { - Yap_Error(TYPE_ERROR_ATOM,t1,"socket/4"); - return(FALSE); - } - if (IsVarTerm(t2)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket/4"); - return(FALSE); - } - if (!IsAtomTerm(t2)) { - Yap_Error(TYPE_ERROR_ATOM,t2,"socket/4"); - return(FALSE); - } - if (IsVarTerm(t3)) { - Yap_Error(INSTANTIATION_ERROR,t3,"socket/4"); - return(FALSE); - } - if (!IsIntTerm(t3)) { - Yap_Error(TYPE_ERROR_ATOM,t3,"socket/4"); - return(FALSE); - } - sdomain = RepAtom(AtomOfTerm(t1))->StrOfAE; - if (sdomain[0] != 'A' || sdomain[1] != 'F' || sdomain[2] != '_') - return(FALSE); /* Error */ - sdomain += 3; - switch (sdomain[0]) { - case 'A': - if (strcmp(sdomain, "AAL5") == 0) - domain = AF_AAL5; - else if (strcmp(sdomain, "APPLETALK") == 0) - domain = AF_APPLETALK; - else if (strcmp(sdomain, "AX25") == 0) - domain = AF_AX25; - break; - case 'B': - if (strcmp(sdomain, "BRIDGE") == 0) - domain = AF_APPLETALK; - break; - case 'D': - if (strcmp(sdomain, "DECnet") == 0) - domain = AF_DECnet; - break; - case 'F': - if (strcmp(sdomain, "FILE") == 0) - domain = AF_FILE; - break; - case 'I': - if (strcmp(sdomain, "INET") == 0) - domain = AF_INET; - else if (strcmp(sdomain, "INET6") == 0) - domain = AF_INET6; - else if (strcmp(sdomain, "IPX") == 0) - domain = AF_IPX; - break; - case 'L': - if (strcmp(sdomain, "LOCAL") == 0) - domain = AF_LOCAL; - break; - case 'N': - if (strcmp(sdomain, "NETBEUI") == 0) - domain = AF_NETBEUI; - else if (strcmp(sdomain, "NETLINK") == 0) - domain = AF_NETLINK; - else if (strcmp(sdomain, "NETROM") == 0) - domain = AF_NETROM; - break; - case 'O': - if (strcmp(sdomain, "OSINET") == 0) - domain = AF_OSINET; - break; - case 'P': - if (strcmp(sdomain, "PACKET") == 0) - domain = AF_PACKET; - break; - case 'R': - if (strcmp(sdomain, "ROSE") == 0) - domain = AF_ROSE; - else if (strcmp(sdomain, "ROUTE") == 0) - domain = AF_ROUTE; - break; - case 'S': - if (strcmp(sdomain, "SECURITY") == 0) - domain = AF_SECURITY; - else if (strcmp(sdomain, "SNA") == 0) - domain = AF_SNA; - break; - case 'U': - if (strcmp(sdomain, "UNIX") == 0) - domain = AF_UNIX; - break; - case 'X': - if (strcmp(sdomain, "X25") == 0) - domain = AF_X25; - break; - } - stype = RepAtom(AtomOfTerm(t2))->StrOfAE; - 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) - type = SOCK_STREAM; - else if (strcmp(stype,"DGRAM") == 0) - type = SOCK_DGRAM; - else if (strcmp(stype,"RAW") == 0) - type = SOCK_RAW; - else if (strcmp(stype,"RDM") == 0) - type = SOCK_RDM; - else if (strcmp(stype,"SEQPACKET") == 0) - type = SOCK_SEQPACKET; - else if (strcmp(stype,"PACKET") == 0) - type = SOCK_PACKET; - else - return(FALSE); - protocol = IntOfTerm(t3); - if (protocol < 0) - return(FALSE); - fd = socket(domain, type, protocol); - if (invalid_socket_fd(fd)) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket/4 (socket: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket/4 (socket)"); -#endif - return(FALSE); - } - if (domain == AF_UNIX || domain == AF_LOCAL ) - out = Yap_InitSocketStream(fd, new_socket, af_unix); - else if (domain == AF_INET ) - out = Yap_InitSocketStream(fd, new_socket, af_inet); - else { - /* ok, we currently don't support these sockets */ -#if _MSC_VER || defined(__MINGW32__) - _close(fd); -#else - close(fd); -#endif - return(FALSE); - } - if (out == TermNil) return(FALSE); - return(Yap_unify(out,ARG4)); -} - -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) { - char bfr; - - if (shutdown(fd, 1) != 0) { - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (close)"); - return(FALSE); - } - /* read all pending characters - from the socket */ - while( recv( fd, &bfr, 1, 0 ) > 0 ); - /* prevent further reading - from the socket */ - if (shutdown(fd, 0) < 0) { - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (close)"); - return(FALSE); - } - - /* close the socket */ - if (closesocket(fd) != 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (close: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (close)"); -#endif - } -#else - if (status == server_session_socket || - status == client_socket) { - if (shutdown(fd,2) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (shutdown: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (shutdown)"); -#endif - return(FALSE); - } - } - if (close(fd) != 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (close: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_close/1 (close)"); -#endif -#endif - return(FALSE); - } - return(TRUE); -} - -static Int -p_socket_close(void) -{ - Term t1 = Deref(ARG1); - int sno; - - if ((sno = Yap_CheckSocketStream(t1, "socket_close/1")) < 0) { - return (FALSE); - } - Yap_CloseStream(sno); - return(TRUE); -} - -static Int -p_socket_bind(void) -{ - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - int sno; - Functor fun; - socket_info status; - int fd; - - if ((sno = Yap_CheckSocketStream(t1, "socket_bind/2")) < 0) { - return (FALSE); - } - status = Yap_GetSocketStatus(sno); - fd = Yap_GetStreamFd(sno); - if (status != new_socket) { - /* ok, this should be an error, as you are trying to bind */ - return(FALSE); - } - if (IsVarTerm(t2)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_bind/2"); - return(FALSE); - } - if (!IsApplTerm(t2)) { - Yap_Error(DOMAIN_ERROR_STREAM,t2,"socket_bind/2"); - return(FALSE); - } - fun = FunctorOfTerm(t2); -#if HAVE_SYS_UN_H - if (fun == FunctorAfUnix || fun == FunctorAfLocal) { - struct sockaddr_un sock; - Term taddr = ArgOfTerm(1, t2); - char *s; - int len; - - if (IsVarTerm(taddr)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_bind/2"); - return(FALSE); - } - if (!IsAtomTerm(taddr)) { - Yap_Error(TYPE_ERROR_ATOM,taddr,"socket_bind/2"); - return(FALSE); - } - s = RepAtom(AtomOfTerm(taddr))->StrOfAE; - sock.sun_family = AF_UNIX; - if ((len = strlen(s)) > 107) /* hit me with a broomstick */ { - Yap_Error(DOMAIN_ERROR_STREAM,taddr,"socket_bind/2"); - return(FALSE); - } - 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 HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (bind: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (bind)"); -#endif - return(FALSE); - } - Yap_UpdateSocketStream(sno, server_socket, af_unix); - return(TRUE); - } else -#endif - if (fun == FunctorAfInet) { - Term thost = ArgOfTerm(1, t2); - Term tport = ArgOfTerm(2, t2); - char *shost; - struct hostent *he; - struct sockaddr_in saddr; - Int port; - - memset((void *)&saddr,(int) 0, sizeof(saddr)); - if (IsVarTerm(thost)) { - saddr.sin_addr.s_addr = INADDR_ANY; - } else if (!IsAtomTerm(thost)) { - Yap_Error(TYPE_ERROR_ATOM,thost,"socket_bind/2"); - return(FALSE); - } else { - shost = RepAtom(AtomOfTerm(thost))->StrOfAE; - if((he=gethostbyname(shost))==NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (gethostbyname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (gethostbyname)"); -#endif - return(FALSE); - } - memcpy((void *)&saddr.sin_addr, (void *)he->h_addr_list[0], he->h_length); - } - if (IsVarTerm(tport)) { - port = 0; - } else { - port = IntOfTerm(tport); - } - saddr.sin_port = htons(port); - saddr.sin_family = AF_INET; - if(bind(fd,(struct sockaddr *)&saddr, sizeof(saddr))==-1) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (bind: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (bind)"); -#endif - return(FALSE); - } - - if (IsVarTerm(tport)) { - /* get the port number */ -#if _WIN32 || defined(__MINGW32__) - int namelen; -#else - unsigned int namelen; -#endif - Term t; - if (getsockname(fd, (struct sockaddr *)&saddr, &namelen) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (getsockname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_bind/2 (getsockname)"); -#endif - return(FALSE); - } - t = MkIntTerm(ntohs(saddr.sin_port)); - Yap_unify(ArgOfTermCell(2, t2),t); - } - Yap_UpdateSocketStream(sno, server_socket, af_inet); - return(TRUE); - } else - return(FALSE); -} - -static Int -p_socket_connect(void) -{ - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - Functor fun; - int sno; - socket_info status; - int fd; - int flag; - Term out; - - if ((sno = Yap_CheckSocketStream(t1, "socket_connect/3")) < 0) { - return (FALSE); - } - if (IsVarTerm(t2)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_connect/3"); - return(FALSE); - } - if (!IsApplTerm(t2)) { - Yap_Error(DOMAIN_ERROR_STREAM,t2,"socket_connect/3"); - return(FALSE); - } - fun = FunctorOfTerm(t2); - fd = Yap_GetStreamFd(sno); - status = Yap_GetSocketStatus(sno); - if (status != new_socket) { - /* ok, this should be an error, as you are trying to bind */ - return(FALSE); - } -#if HAVE_SYS_UN_H - if (fun == FunctorAfUnix) { - struct sockaddr_un sock; - Term taddr = ArgOfTerm(1, t2); - char *s; - int len; - - if (IsVarTerm(taddr)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_connect/3"); - return(FALSE); - } - if (!IsAtomTerm(taddr)) { - Yap_Error(TYPE_ERROR_ATOM,taddr,"socket_connect/3"); - return(FALSE); - } - s = RepAtom(AtomOfTerm(taddr))->StrOfAE; - sock.sun_family = AF_UNIX; - if ((len = strlen(s)) > 107) /* beat me with a broomstick */ { - Yap_Error(DOMAIN_ERROR_STREAM,taddr,"socket_connect/3"); - return(FALSE); - } - 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 HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (connect: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (connect)"); -#endif - return(FALSE); - } - Yap_UpdateSocketStream(sno, client_socket, af_unix); - } else -#endif - if (fun == FunctorAfInet) { - Term thost = ArgOfTerm(1, t2); - Term tport = ArgOfTerm(2, t2); - char *shost; - struct hostent *he; - struct sockaddr_in saddr; - unsigned short int port; - - memset((void *)&saddr,(int) 0, sizeof(saddr)); - if (IsVarTerm(thost)) { - Yap_Error(INSTANTIATION_ERROR,thost,"socket_connect/3"); - return(FALSE); - } else if (!IsAtomTerm(thost)) { - Yap_Error(TYPE_ERROR_ATOM,thost,"socket_connect/3"); - return(FALSE); - } else { - shost = RepAtom(AtomOfTerm(thost))->StrOfAE; - if((he=gethostbyname(shost))==NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (gethostbyname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (gethostbyname)"); -#endif - return(FALSE); - } - memcpy((void *)&saddr.sin_addr, (void *)he->h_addr_list[0], he->h_length); - } - if (IsVarTerm(tport)) { - Yap_Error(INSTANTIATION_ERROR,tport,"socket_connect/3"); - return(FALSE); - } else if (!IsIntegerTerm(tport)) { - Yap_Error(TYPE_ERROR_INTEGER,tport,"socket_connect/3"); - return(FALSE); - } else { - port = (unsigned short int)IntegerOfTerm(tport); - } - saddr.sin_port = htons(port); - saddr.sin_family = AF_INET; -#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!! */ - ling.l_onoff = 1; - ling.l_linger = 0; - if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (void *) &ling, - sizeof(ling)) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_linger: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_linger)"); -#endif - return FALSE; - } - } -#endif - - { - int one = 1; /* code by David MW Powers */ - - if (setsockopt(fd, SOL_SOCKET, SO_BROADCAST, (void *)&one, sizeof(one))) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_broadcast: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (setsockopt_broadcast)"); -#endif - return FALSE; - } - } - - flag = connect(fd,(struct sockaddr *)&saddr, sizeof(saddr)); - if(flag<0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (connect: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_connect/3 (connect)"); -#endif - return FALSE; - } - Yap_UpdateSocketStream(sno, client_socket, af_inet); - } else - return(FALSE); - out = t1; - return(Yap_unify(out,ARG3)); -} - -static Int -p_socket_listen(void) -{ - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - int sno; - socket_info status; - int fd; - Int j; - - if ((sno = Yap_CheckSocketStream(t1, "socket_listen/2")) < 0) { - return (FALSE); - } - if (IsVarTerm(t2)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_listen/2"); - return(FALSE); - } - if (!IsIntTerm(t2)) { - Yap_Error(TYPE_ERROR_INTEGER,t2,"socket_listen/2"); - return(FALSE); - } - j = IntOfTerm(t2); - if (j < 0) { - Yap_Error(DOMAIN_ERROR_STREAM,t1,"socket_listen/2"); - return(FALSE); - } - fd = Yap_GetStreamFd(sno); - status = Yap_GetSocketStatus(sno); - if (status != server_socket) { - /* ok, this should be an error, as you are trying to bind */ - return(FALSE); - } - if (listen(fd,j) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_listen/2 (listen: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_listen/2 (listen)"); -#endif - } - return(TRUE); -} - -static Int -p_socket_accept(void) -{ - Term t1 = Deref(ARG1); - int sno; - socket_info status; - socket_domain domain; - int ofd, fd; - Term out; - - if ((sno = Yap_CheckSocketStream(t1, "socket_accept/3")) < 0) { - return (FALSE); - } - ofd = Yap_GetStreamFd(sno); - status = Yap_GetSocketStatus(sno); - if (status != server_socket) { - /* ok, this should be an error, as you are trying to bind */ - return(FALSE); - } - domain = Yap_GetSocketDomain(sno); -#if HAVE_SYS_UN_H - if (domain == af_unix) { - struct sockaddr_un caddr; - unsigned int len; - - memset((void *)&caddr,(int) 0, sizeof(caddr)); - if ((fd=accept(ofd, (struct sockaddr *)&caddr, &len)) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_accept/3 (accept: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_accept/3 (accept)"); -#endif - } - /* ignore 2nd argument */ - out = Yap_InitSocketStream(fd, server_session_socket, af_unix ); - } else -#endif - if (domain == af_inet) { - struct sockaddr_in caddr; - Term tcli; - char *s; -#if _WIN32 || defined(__MINGW32__) - int len; -#else - unsigned int len; -#endif - - 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, TermNil, - "socket_accept/3 (accept: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_accept/3 (accept)"); -#endif - return(FALSE); - } - if ((s = inet_ntoa(caddr.sin_addr)) == NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_accept/3 (inet_ntoa: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_accept/3 (inet_ntoa)"); -#endif - } - tcli = MkAtomTerm(Yap_LookupAtom(s)); - if (!Yap_unify(ARG2,tcli)) - return(FALSE); - out = Yap_InitSocketStream(fd, server_session_socket, af_inet ); - } else - return(FALSE); - if (out == TermNil) return(FALSE); - return(Yap_unify(out,ARG3)); -} - -static Int -p_socket_buffering(void) -{ - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - Term t4 = Deref(ARG4); - Atom mode; - int fd; - int writing; -#if _WIN32 || defined(__MINGW32__) - int bufsize; - int len; -#else - unsigned int bufsize; - unsigned int len; -#endif - int sno; - - if ((sno = Yap_CheckSocketStream(t1, "socket_buffering/4")) < 0) { - return (FALSE); - } - if (IsVarTerm(t2)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_buffering/4"); - return(FALSE); - } - if (!IsAtomTerm(t2)) { - Yap_Error(TYPE_ERROR_ATOM,t2,"socket_buffering/4"); - return(FALSE); - } - mode = AtomOfTerm(t2); - if (mode == AtomRead) - writing = FALSE; - else if (mode == AtomWrite) - writing = TRUE; - else { - Yap_Error(DOMAIN_ERROR_IO_MODE,t2,"socket_buffering/4"); - return(FALSE); - } - fd = Yap_GetStreamFd(sno); - if (writing) { - getsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&bufsize, &len); - } else { - getsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&bufsize, &len); - } - if (!Yap_unify(ARG3,MkIntegerTerm(bufsize))) - return(FALSE); - if (IsVarTerm(t4)) { - bufsize = BUFSIZ; - } else { - Int siz; - if (!IsIntegerTerm(t4)) { - Yap_Error(TYPE_ERROR_INTEGER,t4,"socket_buffering/4"); - return(FALSE); - } - siz = IntegerOfTerm(t4); - if (siz < 0) { - Yap_Error(DOMAIN_ERROR_NOT_LESS_THAN_ZERO,t4,"socket_buffering/4"); - return(FALSE); - } - bufsize = siz; - } - if (writing) { - setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (void *)&bufsize, sizeof(bufsize)); - } else { - setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (void *)&bufsize, sizeof(bufsize)); - } - return(TRUE); -} - -static Term -select_out_list(Term t1, fd_set *readfds_ptr) -{ - if (t1 == TermNil) { - return(TermNil); - } else { - int fd; - int sno; - Term next = select_out_list(TailOfTerm(t1), readfds_ptr); - Term Head = HeadOfTerm(t1); - - sno = Yap_CheckIOStream(Head,"stream_select/5"); - fd = Yap_GetStreamFd(sno); - if (FD_ISSET(fd, readfds_ptr)) - return(MkPairTerm(Head,next)); - else - return(MkPairTerm(TermNil,next)); - } -} - -static Int -p_socket_select(void) -{ - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - Term t3 = Deref(ARG3); - fd_set readfds, writefds, exceptfds; - struct timeval timeout, *ptime; - -#if _MSC_VER || defined(__MINGW32__) - u_int fdmax=0; -#else - int fdmax=0; -#endif - Int tsec, tusec; - Term tout = TermNil, ti, Head; - - if (IsVarTerm(t1)) { - Yap_Error(INSTANTIATION_ERROR,t1,"socket_select/5"); - return(FALSE); - } - if (!IsPairTerm(t1)) { - Yap_Error(TYPE_ERROR_LIST,t1,"socket_select/5"); - return(FALSE); - } - if (IsVarTerm(t2)) { - Yap_Error(INSTANTIATION_ERROR,t2,"socket_select/5"); - return(FALSE); - } - if (!IsIntegerTerm(t2)) { - Yap_Error(TYPE_ERROR_INTEGER,t2,"socket_select/5"); - return(FALSE); - } - if (IsVarTerm(t3)) { - Yap_Error(INSTANTIATION_ERROR,t3,"socket_select/5"); - return(FALSE); - } - if (!IsIntegerTerm(t3)) { - Yap_Error(TYPE_ERROR_INTEGER,t3,"socket_select/5"); - return(FALSE); - } - FD_ZERO(&readfds); - FD_ZERO(&writefds); - FD_ZERO(&exceptfds); - /* fetch the input streams */ - ti = t1; - while (ti != TermNil) { -#if _MSC_VER || defined(__MINGW32__) - u_int fd; -#else - int fd; -#endif - int sno; - - Head = HeadOfTerm(ti); - sno = Yap_CheckIOStream(Head,"stream_select/5"); - if (sno < 0) - return(FALSE); - fd = Yap_GetStreamFd(sno); - FD_SET(fd, &readfds); - if (fd > fdmax) - fdmax = fd; - ti = TailOfTerm(ti); - } - /* now, check the time */ - tsec = IntegerOfTerm(t2); - tusec = IntegerOfTerm(t3); - if (tsec < 0) /* off */ { - ptime = NULL; - } else { - timeout.tv_sec = tsec; - timeout.tv_usec = tusec; - ptime = &timeout; - } - /* do the real work */ - if (select(fdmax+1, &readfds, &writefds, &exceptfds, ptime) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_select/5 (select: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "socket_select/5 (select)"); -#endif - } - tout = select_out_list(t1, &readfds); - /* we're done, just pass the info back */ - return(Yap_unify(ARG4,tout)); -} - - -static Int -p_current_host(void) { - char oname[MAXHOSTNAMELEN], *name; - Term t1 = Deref(ARG1), out; - - if (!IsVarTerm(t1) && !IsAtomTerm(t1)) { - Yap_Error(TYPE_ERROR_ATOM,t1,"current_host/2"); - return(FALSE); - } - name = oname; - if (gethostname(name, sizeof(oname)) < 0) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "current_host/2 (gethostname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "current_host/2 (gethostname)"); -#endif - return(FALSE); - } - if ((strrchr(name,'.') == NULL)) { - struct hostent *he; - - /* not a fully qualified name, ask the name server */ - if((he=gethostbyname(name))==NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "current_host/2 (gethostbyname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "current_host/2 (gethostbyname)"); -#endif - return(FALSE); - } - name = (char *)(he->h_name); - } - if (IsAtomTerm(t1)) { - char *sin = RepAtom(AtomOfTerm(t1))->StrOfAE; - int faq = (strrchr(sin,'.') != NULL); - - if (faq) -#if _MSC_VER || defined(__MINGW32__) - return(_stricmp(name,sin) == 0); -#else - return(strcasecmp(name,sin) == 0); -#endif - else { - int isize = strlen(sin); - if (isize >= 256) { - Yap_Error(SYSTEM_ERROR, ARG1, - "current_host/2 (input longer than longest FAQ host name)"); - return(FALSE); - } - if (name[isize] != '.') return(FALSE); - name[isize] = '\0'; -#if _MSC_VER || defined(__MINGW32__) - return(_stricmp(name,sin) == 0); -#else - return(strcasecmp(name,sin) == 0); -#endif - } - } else { - out = MkAtomTerm(Yap_LookupAtom(name)); - return(Yap_unify(ARG1,out)); - } -} - -static Int -p_hostname_address(void) { - char *s; - Term t1 = Deref(ARG1); - Term t2 = Deref(ARG2); - Term tin, out; - struct hostent *he; - - if (!IsVarTerm(t1)) { - if (!IsAtomTerm(t1)) { - Yap_Error(TYPE_ERROR_ATOM,t1,"hostname_address/2"); - return(FALSE); - } 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; - s = RepAtom(AtomOfTerm(tin))->StrOfAE; - if (IsVarTerm(t1)) { - if ((he = gethostbyaddr(s, strlen(s), AF_INET)) == NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "hostname_address/2 (gethostbyname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "hostname_address/2 (gethostbyname)"); -#endif - } - out = MkAtomTerm(Yap_LookupAtom((char *)(he->h_name))); - return(Yap_unify(out, ARG1)); - } else { - struct in_addr adr; - if ((he = gethostbyname(s)) == NULL) { -#if HAVE_STRERROR - Yap_Error(SYSTEM_ERROR, TermNil, - "hostname_address/2 (gethostbyname: %s)", strerror(socket_errno)); -#else - Yap_Error(SYSTEM_ERROR, TermNil, - "hostname_address/2 (gethostbyname)"); -#endif - } - 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_InitSockets(void) -{ -#ifdef USE_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("current_host", 1, p_current_host, SafePredFlag); - Yap_InitCPred("hostname_address", 2, p_hostname_address, SafePredFlag); -#if _MSC_VER || defined(__MINGW32__) - { - WSADATA info; - if (WSAStartup(MAKEWORD(2,1), &info) != 0) - exit(1); - } -#endif -#endif -} - diff --git a/H/Yapproto.h b/H/Yapproto.h index 69a169e46..566c322a6 100755 --- a/H/Yapproto.h +++ b/H/Yapproto.h @@ -467,12 +467,6 @@ void STD_PROTO(Yap_InitSWIHash,(void)); int STD_PROTO(Yap_get_stream_handle,(Term, int, int, void *)); Term STD_PROTO(Yap_get_stream_position,(void *)); -/* ypsocks.c */ -void STD_PROTO(Yap_InitSockets,(void)); -#ifdef USE_SOCKET -void STD_PROTO(Yap_init_socks,(char *, long)); -#endif - /* opt.preds.c */ void STD_PROTO(Yap_init_optyap_preds,(void)); diff --git a/H/iopreds.h b/H/iopreds.h index ddff9dd24..df06e9389 100644 --- a/H/iopreds.h +++ b/H/iopreds.h @@ -75,13 +75,6 @@ typedef struct stream_desc int fd; #endif } pipe; -#if USE_SOCKET - struct { - socket_domain domain; - socket_info flags; - int fd; - } socket; -#endif } u; Int charcount, linecount, linepos; Int status; @@ -113,7 +106,6 @@ StreamDesc; #define Eof_Stream_f 0x000010 #define Null_Stream_f 0x000020 #define Tty_Stream_f 0x000040 -#define Socket_Stream_f 0x000080 #define Binary_Stream_f 0x000100 #define Eof_Error_Stream_f 0x000200 #define Reset_Eof_Stream_f 0x000400 @@ -121,10 +113,6 @@ StreamDesc; #define Push_Eof_Stream_f 0x001000 #define Seekable_Stream_f 0x002000 #define Promptable_Stream_f 0x004000 -#if USE_SOCKET -#define Client_Socket_Stream_f 0x008000 -#define Server_Socket_Stream_f 0x010000 -#endif #define Pipe_Stream_f 0x040000 #define Popen_Stream_f 0x080000 #define User_Stream_f 0x100000 diff --git a/Makefile.in b/Makefile.in index 52e7098cb..2e3ec10f3 100755 --- a/Makefile.in +++ b/Makefile.in @@ -248,7 +248,7 @@ C_SOURCES= \ $(srcdir)/packages/udi/rtree.c \ $(srcdir)/packages/udi/rtree_udi.c \ $(srcdir)/C/utilpreds.c $(srcdir)/C/write.c $(srcdir)/console/yap.c \ - $(srcdir)/C/ypsocks.c $(srcdir)/C/ypstdio.c \ + $(srcdir)/C/ypstdio.c \ $(srcdir)/BEAM/eam_am.c $(srcdir)/BEAM/eam_showcode.c \ $(srcdir)/BEAM/eamindex.c $(srcdir)/BEAM/eamamasm.c \ $(srcdir)/BEAM/eam_gc.c $(srcdir)/BEAM/eam_split.c \ @@ -309,7 +309,7 @@ PL_SOURCES= \ $(srcdir)/pl/save.yap \ $(srcdir)/pl/setof.yap \ $(srcdir)/pl/signals.yap \ - $(srcdir)/pl/sockets.yap $(srcdir)/pl/sort.yap \ + $(srcdir)/pl/sort.yap \ $(srcdir)/pl/statistics.yap \ $(srcdir)/pl/strict_iso.yap \ $(srcdir)/pl/tabling.yap $(srcdir)/pl/threads.yap \ @@ -347,7 +347,7 @@ ENGINE_OBJECTS = \ udi.o rtree.o rtree_udi.o\ unify.o userpreds.o utilpreds.o \ write.o \ - blobs.o swi.o ypsocks.o ypstdio.o $(IOLIB_OBJECTS) @MPI_OBJS@ + blobs.o swi.o ypstdio.o $(IOLIB_OBJECTS) @MPI_OBJS@ C_INTERFACE_OBJECTS = \ load_foreign.o load_dl.o load_dld.o load_dyld.o \ diff --git a/library/dialect/swi/fli/swi.c b/library/dialect/swi/fli/swi.c index 27912abf9..449a7c2f7 100755 --- a/library/dialect/swi/fli/swi.c +++ b/library/dialect/swi/fli/swi.c @@ -2746,7 +2746,7 @@ Yap_read_term(term_t t, IOSTREAM *st, term_t vs) if (!Yap_unify(out, Yap_GetFromSlot(t))) { return FALSE; } - if (!Yap_unify(vs, Yap_GetFromSlot(varnames))) { + if (!Yap_unify(varnames, Yap_GetFromSlot(vs))) { return FALSE; } return TRUE; diff --git a/pl/init.yap b/pl/init.yap index 31085f9fd..fdb32373f 100644 --- a/pl/init.yap +++ b/pl/init.yap @@ -73,7 +73,6 @@ otherwise. 'callcount.yap', 'load_foreign.yap', 'save.yap', - 'sockets.yap', 'sort.yap', 'setof.yap', 'statistics.yap', diff --git a/pl/sockets.yap b/pl/sockets.yap deleted file mode 100644 index 0a5cce2fd..000000000 --- a/pl/sockets.yap +++ /dev/null @@ -1,82 +0,0 @@ -/************************************************************************* -* * -* YAP Prolog * -* * -* Yap Prolog was developed at NCCUP - Universidade do Porto * -* * -* Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 * -* * -************************************************************************** -* * -* File: sockets.yap * -* Last rev: * -* mods: * -* comments: Socket predicates * -* * -*************************************************************************/ - -socket(S,D) :- - socket(S, 'SOCK_STREAM', 0, D). - -socket_accept(S,F) :- - socket_accept(S, _, F). - -socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams) :- - '$check_list_for_sockets'(Socks, socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams)), - '$check_list_for_sockets'(Streams, socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams)), - '$select_cp_fds'(Socks, Streams, Fds), - '$check_select_time'(TimeOut, Sec, USec, socket_select(Socks, OutSocks, TimeOut, Streams, OutStreams)), - '$socket_select'(Fds, Sec, USec, NFds), - '$cp_socket_fds'(Socks, NFds, OutSocks, NFdsS), - '$cp_stream_fds'(Streams, NFdsS, OutStreams). - - -/* check whether a list of options is valid */ -'$check_list_for_sockets'(V,G) :- var(V), !, - '$do_error'(instantiation_error,G). -'$check_list_for_sockets'([],_) :- !. -'$check_list_for_sockets'([_|T],G) :- !, - '$check_list_for_sockets'(T,G). -'$check_list_for_sockets'(T,G) :- - '$do_error'(type_error(list,T),G). - -'$select_cp_fds'([], Fds, Fds). -'$select_cp_fds'([_-Fd|L], Fds0, [Fd|Fds]) :- - '$select_cp_fds'(L, Fds0, Fds). - -'$check_select_time'(V, _, _, Goal) :- - var(V), !, - '$do_error'(instantiation_error,Goal). -'$check_select_time'(off, -1, -1, _). -'$check_select_time'(Sec0:USec0, Sec, USec, _) :- - Sec is Sec0, - USec is USec0, - Sec >= 0, USec >= 0. - -'$cp_socket_fds'([], Fds, [], Fds). -'$cp_socket_fds'([_|Scks], [[]|Fds], Out, StrFds) :- !, - '$cp_socket_fds'(Scks, Fds, Out, StrFds). -'$cp_socket_fds'([T-Socket|Scks], [Socket|Fds], [T-connection(Client,Stream)|Out], StrFds) :- - socket_accept(Socket, Client, Stream), - '$cp_socket_fds'(Scks, Fds, Out, StrFds). - -'$cp_stream_fds'([], _, []). -'$cp_stream_fds'([_|Strs], [[]|Fds], Out) :- !, - '$cp_stream_fds'(Strs, Fds, Out). -'$cp_stream_fds'([Stream|Strs], [Stream|Fds], [Stream|Out]) :- - '$cp_stream_fds'(Strs, Fds, Out). - -socket_buffering(Sock, Flag, InSize, OutSize) :- - var(OutSize), OutSize \= InSize, !, - '$do_error'(instantiation_error,socket_buffering(Sock, Flag, InSize, OutSize)). -socket_buffering(Sock, Flag, InSize, OutSize) :- - '$convert_sock_buff'(OutSize, OutNumb), - '$socket_buffering'(Sock, Flag, InNumb, OutNumb), - '$convert_sock_buff'(InSize, InNumb). - -'$convert_sock_buff'(V, V) :- var(V), !. -'$convert_sock_buff'(unbuf, 1) :- !. -'$convert_sock_buff'(fullbuf, _). - - -