This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
yap-6.3/os/iopreds.h

429 lines
12 KiB
C
Raw Normal View History

2015-06-18 01:39:03 +01:00
/**************************************************************************
* *
2016-02-18 12:10:58 +00:00
* File: iopreds.h *
2015-06-18 01:39:03 +01:00
* Last rev: 5/2/88 *
2016-02-18 12:10:58 +00:00
* mods: *
2015-06-18 01:39:03 +01:00
* comments: Input/Output C implemented predicates *
* *
*************************************************************************/
#ifdef SCCS
static char SccsId[] = "%W% %G%";
#endif
#ifndef IOPREDS_H
#define IOPREDS_H 1
#if _WIN32
#define USE_SOCKET 1
#define HAVE_SOCKET 1
#endif
2015-07-28 04:22:44 +01:00
#include <stdlib.h>
#include "Yap.h"
#include "Atoms.h"
2015-06-18 01:39:03 +01:00
/*
2015-07-28 04:22:44 +01:00
* This file defines main data-structure for stream management,
2015-06-18 01:39:03 +01:00
*
*/
extern size_t Yap_page_size;
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <windows.h>
#endif
#include <wchar.h>
2016-02-18 12:10:58 +00:00
#define Yap_CheckStream(arg, kind, msg) \
Yap_CheckStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckStream__(const char *, const char *, int, Term, int,
const char *);
#define Yap_CheckTextStream(arg, kind, msg) \
Yap_CheckTextStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckTextStream__(const char *, const char *, int, Term, int,
const char *);
extern bool Yap_initStream(int sno, FILE *fd, const char *name, Term file_name,
2016-02-18 12:10:58 +00:00
encoding_t encoding, stream_flags_t flags,
Atom open_mode);
2015-06-18 01:39:03 +01:00
#if HAVE_SOCKET
extern int Yap_sockets_io;
/****************** defines for sockets *********************************/
2016-02-18 12:10:58 +00:00
typedef enum { /* in YAP, sockets may be in one of 4 possible status */
new_socket,
server_socket,
client_socket,
server_session_socket,
closed_socket
2015-06-18 01:39:03 +01:00
} socket_info;
2016-02-18 12:10:58 +00:00
typedef enum { /* we accept two domains for the moment, IPV6 may follow */
af_inet, /* IPV4 */
af_unix /* or AF_FILE */
2015-06-18 01:39:03 +01:00
} socket_domain;
2016-02-18 12:10:58 +00:00
extern Term Yap_InitSocketStream(int, socket_info, socket_domain);
extern int Yap_CheckSocketStream(Term, const char *);
extern socket_domain Yap_GetSocketDomain(int);
extern socket_info Yap_GetSocketStatus(int);
extern void Yap_UpdateSocketStream(int, socket_info, socket_domain);
2015-06-18 01:39:03 +01:00
/* routines in ypsocks.c */
Int Yap_CloseSocket(int, socket_info, socket_domain);
#endif /* USE_SOCKET */
/************ SWI compatible support for unicode representations ************/
2016-02-18 12:10:58 +00:00
typedef struct yap_io_position {
int64_t byteno; /* byte-position in file */
int64_t charno; /* character position in file */
long int lineno; /* lineno in file */
long int linepos; /* position in line */
intptr_t reserved[2]; /* future extensions */
2015-06-18 01:39:03 +01:00
} yapIOPOS;
#ifndef _PL_STREAM_H
2016-02-18 12:10:58 +00:00
typedef struct {
Atom file; /* current source file */
yapIOPOS position; /* Line, line pos, char and byte */
2015-06-18 01:39:03 +01:00
} yapSourceLocation;
#endif
#define RD_MAGIC 0xefebe128
typedef struct vlist_struct_t {
struct VARSTRUCT *ve;
struct vlist_struct_t *next;
} vlist_t;
typedef struct qq_struct_t {
unsigned char *text;
yapIOPOS start, mid, end;
vlist_t *vlist;
struct qq_struct_t *next;
} qq_t;
2016-02-18 12:10:58 +00:00
typedef struct read_data_t {
unsigned char *here; /* current character */
unsigned char *base; /* base of clause */
unsigned char *end; /* end of the clause */
unsigned char *token_start; /* start of most recent read token */
int magic; /* RD_MAGIC */
struct stream_desc *stream;
FILE *f; /* file. of known */
Term position; /* Line, line pos, char and byte */
void *posp; /* position pointer */
size_t posi; /* position number */
Term subtpos; /* Report Subterm positions */
bool cycles; /* Re-establish cycles */
yapSourceLocation start_of_term; /* Position of start of term */
struct mod_entry *module; /* Current source module */
unsigned int flags; /* Module syntax flags */
int styleCheck; /* style-checking mask */
bool backquoted_string; /* Read `hello` as string */
int *char_conversion_table; /* active conversion table */
Atom on_error; /* Handling of syntax errors */
int has_exception; /* exception is raised */
Term exception; /* raised exception */
Term variables; /* report variables */
Term singles; /* Report singleton variables */
Term varnames; /* Report variables+names */
int strictness; /* Strictness level */
2015-06-18 01:39:03 +01:00
#ifdef O_QUASIQUOTATIONS
2016-02-18 12:10:58 +00:00
Term quasi_quotations; /* User option quasi_quotations(QQ) */
Term qq; /* Quasi quoted list */
Term qq_tail; /* Tail of the quoted stuff */
2015-06-18 01:39:03 +01:00
#endif
2016-02-18 12:10:58 +00:00
Term comments; /* Report comments */
2015-06-18 01:39:03 +01:00
} read_data, *ReadData;
Term Yap_read_term(int inp_stream, Term opts, int nargs);
2016-02-18 12:10:58 +00:00
Term Yap_Parse(UInt prio);
2015-06-18 01:39:03 +01:00
void init_read_data(ReadData _PL_rd, struct stream_desc *s);
typedef int (*GetsFunc)(int, UInt, char *);
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
2016-02-18 12:10:58 +00:00
typedef struct mem_desc {
char *buf; /* where the file is being read from/written to */
int src; /* where the space comes from, 0 code space, 1 malloc */
Int max_size; /* maximum buffer size (may be changed dynamically) */
UInt pos; /* cursor */
2015-06-18 01:39:03 +01:00
volatile void *error_handler;
} memHandle;
2016-02-18 12:10:58 +00:00
typedef struct stream_desc {
2015-06-18 01:39:03 +01:00
Atom name;
Term user_name;
2016-02-18 12:10:58 +00:00
FILE *file;
2015-07-23 01:33:30 +01:00
// useful in memory streams
char *nbuf;
size_t nsize;
2015-06-18 01:39:03 +01:00
union {
struct {
#define PLGETC_BUF_SIZE 4096
2016-02-18 12:10:58 +00:00
unsigned char *buf, *ptr;
2015-06-18 01:39:03 +01:00
int left;
} file;
memHandle mem_string;
struct {
int fd;
} pipe;
#if HAVE_SOCKET
struct {
socket_domain domain;
socket_info flags;
int fd;
} socket;
#endif
struct {
2016-02-18 12:10:58 +00:00
const unsigned char *buf, *ptr;
2015-06-18 01:39:03 +01:00
} irl;
} u;
Int charcount, linecount, linepos;
stream_flags_t status;
int och;
#if defined(YAPOR) || defined(THREADS)
2016-02-18 12:10:58 +00:00
lockvar streamlock; /* protect stream access */
2015-06-18 01:39:03 +01:00
#endif
2016-02-18 12:10:58 +00:00
int (*stream_putc)(int, int); /* function the stream uses for writing */
int (*stream_getc)(int); /* function the stream uses for reading */
GetsFunc stream_gets; /* function the stream uses for reading a sequence of
characters */
2015-06-18 01:39:03 +01:00
/* function the stream uses for parser. It may be different if the ISO
character conversion is on */
2016-02-18 12:10:58 +00:00
int (*stream_wgetc_for_read)(int);
int (*stream_wgetc)(int);
int (*stream_wputc)(int, wchar_t);
2015-06-18 01:39:03 +01:00
encoding_t encoding;
2016-02-18 12:10:58 +00:00
} StreamDesc;
2015-08-07 22:57:53 +01:00
2016-02-18 12:10:58 +00:00
static inline bool IsStreamTerm(Term t) {
2015-06-18 01:39:03 +01:00
return !IsVarTerm(t) &&
2016-02-18 12:10:58 +00:00
(IsAtomTerm(t) ||
(IsApplTerm(t) && (FunctorOfTerm(t) == FunctorStream)));
2015-06-18 01:39:03 +01:00
}
2016-02-18 12:10:58 +00:00
static inline StreamDesc *Yap_GetStreamHandle(Term t) {
int sno = Yap_CheckStream(t, 0, "stream search");
2015-06-18 01:39:03 +01:00
if (sno < 0)
return NULL;
2016-02-18 12:10:58 +00:00
return GLOBAL_Stream + sno;
2015-06-18 01:39:03 +01:00
}
#define YAP_ERROR NIL
#define MaxStreams 64
2016-02-18 12:10:58 +00:00
#define EXPAND_FILENAME 0x000080
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
#define StdInStream 0
#define StdOutStream 1
#define StdErrStream 2
2015-06-18 01:39:03 +01:00
#define ALIASES_BLOCK_SIZE 8
2016-02-18 12:10:58 +00:00
void Yap_InitStdStreams(void);
Term Yap_StreamPosition(int);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
static inline int GetCurInpPos(StreamDesc *inp_stream) {
2015-06-18 01:39:03 +01:00
return (inp_stream->linecount);
}
2016-02-18 12:10:58 +00:00
#define PlIOError(type, culprit, ...) \
PlIOError__(__FILE__, __FUNCTION__, __LINE__, type, culprit, __VA_ARGS__)
2015-09-29 23:44:11 +01:00
2016-02-18 12:10:58 +00:00
Int PlIOError__(const char *, const char *, int, yap_error_number, Term, ...);
2015-09-29 23:44:11 +01:00
2015-06-18 01:39:03 +01:00
int GetFreeStreamD(void);
2016-02-18 12:10:58 +00:00
Term Yap_MkStream(int n);
2015-07-06 12:03:16 +01:00
2016-02-18 12:10:58 +00:00
bool Yap_PrintWarning(Term twarning);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
void Yap_plwrite(Term, struct stream_desc *, int, int, int);
void Yap_WriteAtom(struct stream_desc *s, Atom atom);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
Term Yap_scan_num(struct stream_desc *);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
void Yap_DefaultStreamOps(StreamDesc *st);
void Yap_PipeOps(StreamDesc *st);
void Yap_MemOps(StreamDesc *st);
bool Yap_CloseMemoryStream(int sno);
void Yap_ConsolePipeOps(StreamDesc *st);
void Yap_SocketOps(StreamDesc *st);
void Yap_ConsoleSocketOps(StreamDesc *st);
bool Yap_ReadlineOps(StreamDesc *st);
int Yap_OpenBufWriteStream(USES_REGS1);
void Yap_ConsoleOps(StreamDesc *s);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
void Yap_InitRandomPreds(void);
void Yap_InitSignalPreds(void);
void Yap_InitTimePreds(void);
2015-11-05 16:57:24 +00:00
2015-06-18 01:39:03 +01:00
void Yap_init_socks(char *host, long interface_port);
2016-02-18 12:10:58 +00:00
void Yap_InitPipes(void);
void Yap_InitMem(void);
void Yap_InitSockets(void);
2015-06-18 01:39:03 +01:00
void Yap_InitSocketLayer(void);
2016-02-18 12:10:58 +00:00
void Yap_InitMems(void);
void Yap_InitConsole(void);
void Yap_InitReadlinePreds(void);
bool Yap_InitReadline(Term);
2015-06-18 01:39:03 +01:00
void Yap_InitChtypes(void);
void Yap_InitCharsio(void);
void Yap_InitFormat(void);
void Yap_InitFiles(void);
void Yap_InitIOStreams(void);
void Yap_InitWriteTPreds(void);
void Yap_InitReadTPreds(void);
2016-02-18 12:10:58 +00:00
void Yap_socketStream(StreamDesc *s);
void Yap_ReadlineFlush(int sno);
Int Yap_ReadlinePeekChar(int sno);
2015-06-18 01:39:03 +01:00
int Yap_ReadlineForSIGINT(void);
2016-02-18 12:10:58 +00:00
bool Yap_ReadlinePrompt(StreamDesc *s);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
Int Yap_peek(int sno);
2015-10-11 04:23:16 +01:00
2016-02-18 12:10:58 +00:00
Term Yap_syntax_error(TokEntry *tokptr, int sno);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
int console_post_process_read_char(int, StreamDesc *);
int console_post_process_eof(StreamDesc *);
int post_process_read_char(int, StreamDesc *);
int post_process_eof(StreamDesc *);
int post_process_weof(StreamDesc *);
2015-06-18 01:39:03 +01:00
bool is_same_tty(FILE *f1, FILE *f2);
2016-02-18 12:10:58 +00:00
int ISOWGetc(int sno);
int GetUTF8(int sno);
2015-06-18 01:39:03 +01:00
Term read_line(int sno);
2016-02-18 12:10:58 +00:00
int PlGets(int sno, UInt size, char *buf);
2015-06-18 01:39:03 +01:00
GetsFunc PlGetsFunc(void);
2016-02-18 12:10:58 +00:00
int PlGetc(int sno);
int FilePutc(int sno, int c);
int DefaultGets(int, UInt, char *);
2015-06-18 01:39:03 +01:00
int put_wchar(int sno, wchar_t ch);
Int GetStreamFd(int sno);
int ResetEOF(StreamDesc *s);
2015-12-15 09:14:15 +00:00
int EOFPeek(int sno);
int EOFWPeek(int sno);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
void Yap_SetAlias(Atom arg, int sno);
bool Yap_AddAlias(Atom arg, int sno);
int Yap_CheckAlias(Atom arg);
int Yap_RemoveAlias(Atom arg, int snoinline);
void Yap_SetAlias(Atom arg, int sno);
2015-06-18 01:39:03 +01:00
void Yap_InitAliases(void);
2016-02-18 12:10:58 +00:00
void Yap_DeleteAliases(int sno);
bool Yap_FindStreamForAlias(Atom al);
bool Yap_FetchStreamAlias(int sno, Term t2 USES_REGS);
2015-06-18 01:39:03 +01:00
INLINE_ONLY inline EXTERN void count_output_char(int ch, StreamDesc *s);
2015-07-06 12:03:16 +01:00
Term Yap_StreamUserName(int sno);
2016-02-18 12:10:58 +00:00
INLINE_ONLY inline EXTERN void count_output_char(int ch, StreamDesc *s) {
if (ch == '\n') {
2015-06-18 01:39:03 +01:00
#if MPWSHELL
2016-02-18 12:10:58 +00:00
if (mpwshell && (sno == StdOutStream || sno == StdErrStream) &&
!(s->status & Null_Stream_f)) {
putc(MPWSEP, s->file);
if (!(GLOBAL_Stream[LOCAL_output_stream].status & Null_Stream_f))
fflush(stdout);
}
2015-06-18 01:39:03 +01:00
#endif
2016-02-18 12:10:58 +00:00
/* Inform that we have written a newline */
++s->charcount;
++s->linecount;
s->linepos = 0;
} else {
2015-06-18 01:39:03 +01:00
#if MAC
2016-02-18 12:10:58 +00:00
if ((sno == StdOutStream || sno == StdErrStream) && s->linepos > 200)
2015-06-18 01:39:03 +01:00
sno->stream_putc(sno, '\n');
#endif
++s->charcount;
++s->linepos;
}
}
2016-02-18 12:10:58 +00:00
inline static Term StreamName(int i) { return (GLOBAL_Stream[i].user_name); }
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
inline static Atom StreamFullName(int i) { return (GLOBAL_Stream[i].name); }
2015-09-29 23:44:11 +01:00
2016-02-18 12:10:58 +00:00
inline static void console_count_output_char(int ch, StreamDesc *s) {
CACHE_REGS
if (ch == '\n') {
2015-06-18 01:39:03 +01:00
#if MPWSHELL
2016-02-18 12:10:58 +00:00
if (mpwshell && (sno == StdOutStream || sno == StdErrStream) &&
!(s->status & Null_Stream_f)) {
putc(MPWSEP, s->file);
if (!(GLOBAL_Stream[LOCAL_output_stream].status & Null_Stream_f))
fflush(stdout);
2015-06-18 01:39:03 +01:00
}
2016-02-18 12:10:58 +00:00
#endif
++s->charcount;
++s->linecount;
s->linepos = 0;
LOCAL_newline = TRUE;
/* Inform we are not at the start of a newline */
} else {
2015-06-18 01:39:03 +01:00
LOCAL_newline = FALSE;
#if MAC
2016-02-18 12:10:58 +00:00
if ((sno == StdOutStream || sno == StdErrStream) && s->linepos > 200)
2015-06-18 01:39:03 +01:00
sno->stream_putc(sno, '\n');
#endif
++s->charcount;
++s->linepos;
}
}
2016-02-18 12:10:58 +00:00
inline static Term StreamPosition(int sno) {
CACHE_REGS
2015-06-18 01:39:03 +01:00
Term sargs[5];
Int cpos;
cpos = GLOBAL_Stream[sno].charcount;
2016-02-18 12:10:58 +00:00
sargs[0] = MkIntegerTerm(LOCAL_StartCharCount = cpos);
sargs[1] = MkIntegerTerm(LOCAL_StartLineCount = GLOBAL_Stream[sno].linecount);
sargs[2] = MkIntegerTerm(LOCAL_StartLinePos = GLOBAL_Stream[sno].linepos);
sargs[3] = sargs[4] = MkIntTerm(0);
return Yap_MkApplTerm(FunctorStreamPos, 5, sargs);
2015-06-18 01:39:03 +01:00
}
2016-02-18 12:10:58 +00:00
inline static Term CurrentPositionToTerm(void) {
CACHE_REGS
Term sargs[5];
2016-02-18 12:10:58 +00:00
sargs[0] = MkIntegerTerm(LOCAL_StartCharCount);
sargs[1] = MkIntegerTerm(LOCAL_StartLineCount);
sargs[2] = MkIntegerTerm(LOCAL_StartLinePos);
sargs[3] = sargs[4] = MkIntTerm(0);
return Yap_MkApplTerm(FunctorStreamPos, 5, sargs);
}
2015-06-18 01:39:03 +01:00
extern FILE *Yap_stdin;
extern FILE *Yap_stdout;
extern FILE *Yap_stderr;
2016-02-18 12:10:58 +00:00
char *Yap_MemExportStreamPtr(int sno);
2015-06-18 01:39:03 +01:00
#endif