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

286 lines
8.6 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 *
* *
*************************************************************************/
#ifndef IOPREDS_H
#define IOPREDS_H 1
2015-07-28 04:22:44 +01:00
#include "Atoms.h"
2016-04-05 02:53:39 +01:00
#include "Yap.h"
#include <stdlib.h>
2015-07-28 04:22:44 +01:00
2015-06-18 01:39:03 +01:00
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <windows.h>
#endif
#include <wchar.h>
2016-07-31 16:28:05 +01:00
#include "YapStreams.h"
2016-10-20 04:44:59 +01:00
INLINE_ONLY EXTERN inline UInt PRED_HASH(FunctorEntry *, Term, UInt);
INLINE_ONLY EXTERN inline bool IsStreamTerm(Term t) {
2016-07-31 16:28:05 +01:00
return !IsVarTerm(t) &&
(IsAtomTerm(t) ||
(IsApplTerm(t) && (FunctorOfTerm(t) == FunctorStream)));
}
extern bool Yap_initStream(int sno, FILE *fd, const char *name, Term file_name,
encoding_t encoding, stream_flags_t flags,
2017-06-05 13:06:12 +01:00
Atom open_mode, void *vfs);
2016-07-31 16:28:05 +01:00
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 *);
2016-07-31 16:28:05 +01:00
#define Yap_CheckBinaryStream(arg, kind, msg) \
2016-04-19 23:30:02 +01:00
Yap_CheckBinaryStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
extern int Yap_CheckBinaryStream__(const char *, const char *, int, Term, int,
2016-07-31 16:28:05 +01:00
const char *);
2016-07-31 16:28:05 +01:00
static inline StreamDesc *Yap_GetStreamHandle(Term t) {
int sno = Yap_CheckStream(t, 0, "stream search");
if (sno < 0)
return NULL;
return GLOBAL_Stream + sno;
}
2016-07-31 16:28:05 +01:00
#include "VFS.h"
2015-06-18 01:39:03 +01:00
2016-07-31 16:28:05 +01:00
/*
* This file defines main data-structure for stream management,
*
*/
2015-06-18 01:39:03 +01:00
2016-07-31 16:28:05 +01:00
extern size_t Yap_page_size;
2015-06-18 01:39:03 +01:00
2016-07-31 16:28:05 +01:00
#if HAVE_SOCKET
extern int Yap_sockets_io;
2015-06-18 01:39:03 +01:00
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 */
extern bool Yap_clearInput(int sno);
2017-11-29 13:47:57 +00:00
extern Term Yap_read_term(int inp_stream, Term opts, bool clause);
2016-07-31 16:28:05 +01:00
extern Term Yap_Parse(UInt prio, encoding_t enc, Term cmod);
2015-06-18 01:39:03 +01:00
2016-07-31 16:28:05 +01:00
extern void init_read_data(ReadData _PL_rd, struct stream_desc *s);
2015-06-18 01:39:03 +01:00
typedef int (*GetsFunc)(int, UInt, char *);
2016-08-07 15:48:21 +01:00
extern void Yap_InitStdStreams(void);
extern Term Yap_StreamPosition(int);
extern void Yap_CloseStream(int sno);
2015-06-18 01:39:03 +01:00
2016-04-10 14:21:17 +01: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-12-04 18:52:42 +00:00
extern Int PlIOError__(const char *, const char *, int, yap_error_number, Term,
...);
2015-09-29 23:44:11 +01:00
2016-07-31 16:28:05 +01:00
extern int GetFreeStreamD(void);
2016-12-04 18:52:42 +00:00
extern Term Yap_MkStream(int n);
2015-07-06 12:03:16 +01:00
2016-07-31 16:28:05 +01:00
extern bool Yap_PrintWarning(Term twarning);
2015-06-18 01:39:03 +01:00
2016-07-31 16:28:05 +01:00
extern void Yap_plwrite(Term, struct stream_desc *, int, int, int);
extern void Yap_WriteAtom(struct stream_desc *s, Atom atom);
2016-12-04 18:52:42 +00:00
extern bool Yap_WriteTerm(int output_stream, Term t, Term opts USES_REGS);
2015-06-18 01:39:03 +01:00
2016-11-08 07:37:36 +00:00
extern Term Yap_scan_num(struct stream_desc *, bool);
2015-06-18 01:39:03 +01:00
2016-07-31 16:28:05 +01:00
extern void Yap_DefaultStreamOps(StreamDesc *st);
extern void Yap_PipeOps(StreamDesc *st);
extern void Yap_MemOps(StreamDesc *st);
extern bool Yap_CloseMemoryStream(int sno);
extern void Yap_ConsolePipeOps(StreamDesc *st);
extern void Yap_SocketOps(StreamDesc *st);
extern void Yap_ConsoleSocketOps(StreamDesc *st);
extern bool Yap_ReadlineOps(StreamDesc *st);
extern int Yap_OpenBufWriteStream(USES_REGS1);
extern void Yap_ConsoleOps(StreamDesc *s);
2015-06-18 01:39:03 +01:00
2016-10-20 04:44:59 +01:00
extern void Yap_InitRandomPreds(void);
extern void Yap_InitSignalPreds(void);
extern void Yap_InitTimePreds(void);
extern void Yap_init_socks(char *host, long interface_port);
extern void Yap_InitPipes(void);
extern void Yap_InitMem(void);
extern void Yap_InitSockets(void);
extern void Yap_InitSocketLayer(void);
extern void Yap_InitMems(void);
extern void Yap_InitConsole(void);
extern void Yap_InitReadlinePreds(void);
2017-03-03 11:21:29 +00:00
extern bool Yap_InitReadline(Term);
2017-10-02 08:58:51 +01:00
extern bool Yap_readline_clear_pending_input(StreamDesc *s);
2016-10-20 04:44:59 +01:00
extern void Yap_InitChtypes(void);
extern void Yap_InitCharsio(void);
extern void Yap_InitFormat(void);
extern void Yap_InitFiles(void);
extern void Yap_InitIOStreams(void);
extern void Yap_InitWriteTPreds(void);
extern void Yap_InitReadTPreds(void);
extern void Yap_socketStream(StreamDesc *s);
extern void Yap_ReadlineFlush(int sno);
2016-02-18 12:10:58 +00:00
Int Yap_ReadlinePeekChar(int sno);
2015-06-18 01:39:03 +01:00
int Yap_ReadlineForSIGINT(void);
2016-07-31 16:28:05 +01:00
bool Yap_DoPrompt(StreamDesc *s);
2015-06-18 01:39:03 +01:00
2016-02-18 12:10:58 +00:00
Int Yap_peek(int sno);
2016-07-31 16:28:05 +01:00
int Yap_MemPeekc(int sno);
2015-10-11 04:23:16 +01: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_wchar(int, size_t, StreamDesc *);
2016-02-18 12:10:58 +00:00
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-10-20 04:44:59 +01:00
extern void Yap_SetAlias(Atom arg, int sno);
2016-02-18 12:10:58 +00:00
bool Yap_AddAlias(Atom arg, int sno);
int Yap_CheckAlias(Atom arg);
int Yap_RemoveAlias(Atom arg, int snoinline);
2016-10-20 04:44:59 +01:00
extern 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);
2016-10-20 04:44:59 +01:00
extern bool Yap_FindStreamForAlias(Atom al);
extern 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
2017-11-29 13:47:57 +00:00
inline static Atom StreamFullName(int i) { return (Atom)(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);
2016-04-18 01:09:10 +01:00
2016-05-30 11:25:28 +01:00
static inline void freeBuffer(const void *ptr) {
CACHE_REGS
2016-12-04 18:52:42 +00:00
if (ptr == NULL || ptr == LOCAL_FileNameBuf || ptr == LOCAL_FileNameBuf2 ||
ptr == AuxBase)
2016-05-30 11:25:28 +01:00
return;
free((void *)ptr);
}
2015-06-18 01:39:03 +01:00
#endif