YAP 7.1.0
iopreds.h
1/**************************************************************************
2 * *
3 * File: iopreds.h *
4 * Last rev: 5/2/88 *
5 * mods: *
6 * comments: Input/Output C implemented predicates *
7 * *
8 *************************************************************************/
9
10#ifndef IOPREDS_H
11#define IOPREDS_H 1
12
13#include "Atoms.h"
14#include "Yap.h"
15#include <stdlib.h>
16
17#if defined(_MSC_VER) || defined(__MINGW32__)
18
19#include <windows.h>
20
21#endif
22
23#include <wchar.h>
24
25#include "YapStreams.h"
26
27
28extern bool IsStreamTerm(Term t);
29
30#define Yap_initStream( sno, fd, name, io_mode, file_name, encoding, flags, vfs) Yap_initStream__(__FILE__, __FUNCTION__, __LINE__, sno, fd, name, io_mode, file_name, encoding, flags, vfs)
31extern bool Yap_initStream__(const char *file, const char *func, int line,int sno, FILE *fd, Atom name, const char *io_mode, Term file_name, encoding_t encoding,
32 stream_flags_t flags, void *vfs);
33
34#define Yap_CheckStream(arg, kind, msg) \
35 Yap_CheckStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
36extern int Yap_CheckStream__(const char *, const char *, int, Term, int,
37 const char *);
38#define Yap_CheckTextStream(arg, kind, msg) \
39 Yap_CheckTextStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
40extern int Yap_CheckTextStream__(const char *, const char *, int, Term, int,
41 const char *);
42
43#define Yap_CheckTextReadStream(arg, msg) \
44 Yap_CheckTextReadStream__(__FILE__, __FUNCTION__, __LINE__, arg, msg)
45extern int Yap_CheckTextReadStream__(const char *, const char *, int, Term,
46 const char *);
47#define Yap_CheckTextWriteStream(arg, msg) \
48 Yap_CheckTextWriteStream__(__FILE__, __FUNCTION__, __LINE__, arg, msg)
49extern int Yap_CheckTextWriteStream__(const char *, const char *, int, Term,
50 const char *);
51
52#define Yap_CheckBinaryStream(arg, kind, msg) \
53 Yap_CheckBinaryStream__(__FILE__, __FUNCTION__, __LINE__, arg, kind, msg)
54extern int Yap_CheckBinaryStream__(const char *, const char *, int, Term, int,
55 const char *);
56
57static inline StreamDesc *Yap_GetStreamHandle(Term t) {
58 int sno = Yap_CheckStream(t, 0, "stream search");
59 if (sno < 0)
60 return NULL;
61 return GLOBAL_Stream + sno;
62}
63
64#include "VFS.h"
65
66/*
67 * This file defines main data-structure for stream management,
68 *
69 */
70
71extern size_t Yap_page_size;
72
73#if HAVE_SOCKET
74extern int Yap_sockets_io;
75
76extern Term Yap_InitSocketStream(int, socket_info, socket_domain);
77extern int Yap_CheckSocketStream(Term, const char *);
78extern socket_domain Yap_GetSocketDomain(int);
79extern socket_info Yap_GetSocketStatus(int);
80extern void Yap_UpdateSocketStream(int, socket_info, socket_domain);
81
82/* routines in ypsocks.c */
83Int Yap_CloseSocket(int, socket_info, socket_domain);
84
85#endif /* USE_SOCKET */
86
87extern bool Yap_clearInput(int sno);
88extern Term Yap_read_term(int inp_stream, Term opts, bool clause);
89extern Term Yap_Parse(UInt prio, encoding_t enc, Term cmod);
90
91extern void init_read_data(ReadData _PL_rd, struct stream_desc *s);
92
93typedef int (*GetsFunc)(int, UInt, char *);
94
95extern void Yap_InitStdStreams(void);
96extern Term Yap_StreamPosition(int);
97#define Yap_CloseStream(id) \
98 Yap_CloseStream__( __FILE__, __FUNCTION__, __LINE__, id)
99extern void Yap_CloseStream__( const char *file,
100 const char *function, int lineno,int sno);
101
102static inline Int GetCurInpLine(StreamDesc *inp_stream) {
103 return (inp_stream->linecount);
104}
105
106static inline Int GetCurInpOffset(StreamDesc *inp_stream) {
107 return (inp_stream->charcount+1-inp_stream->linestart);
108}
109
110static inline Int GetCurInpPos(StreamDesc *inp_stream) {
111 return (inp_stream->charcount);
112}
113extern bool Yap_SetCurInpPos(int sno, Int pos USES_REGS);
114
115typedef enum {
116 CREATE_DIRECTORY,
117 CREATE_FILE
118}io_kind_t;
119
120#define PlIOError(type, culprit, ...) \
121 PlIOError__(__FILE__, __FUNCTION__, __LINE__, type, culprit, __VA_ARGS__)
122
123extern Int PlIOError__(const char *, const char *, int, yap_error_number, Term,
124 ...);
125
126
127#define UnixIOError(errorno, io_kind, culprit, ...) \
128 UnixIOError__(__FILE__, __FUNCTION__, __LINE__, errorno, io_kind, culprit, __VA_ARGS__)
129
130extern bool UnixIOError__(const char *, const char *, int, int, io_kind_t, Term, ...);
131
132extern int GetFreeStreamD(void);
133extern Term Yap_MkStream(int n);
134
135extern bool Yap_PrintWarning(Term twarning);
136
137extern void Yap_plwrite(Term, struct stream_desc *, int, CELL *, int, xarg *);
138extern void Yap_WriteAtom(struct stream_desc *s, Atom atom);
139extern bool Yap_WriteTerm(int output_stream, Term t, Term opts USES_REGS);
140extern Term Yap_scan_num(struct stream_desc *);
141
142extern void Yap_DefaultStreamOps(StreamDesc *st);
143extern void Yap_PipeOps(StreamDesc *st);
144extern void Yap_MemOps(StreamDesc *st);
145extern bool Yap_CloseMemoryStream(int sno);
146extern void Yap_ConsolePipeOps(StreamDesc *st);
147extern void Yap_SocketOps(StreamDesc *st);
148extern void Yap_ConsoleSocketOps(StreamDesc *st);
149extern bool Yap_ReadlineOps(StreamDesc *st);
150extern int Yap_OpenBufWriteStream(USES_REGS1);
151extern void Yap_ConsoleOps(StreamDesc *s);
152
153extern void Yap_InitRandomPreds(void);
154extern void Yap_InitSignalPreds(void);
155extern void Yap_InitTimePreds(void);
156
157extern int Yap_set_sigaction(int, void *);
158
159extern void Yap_init_socks(char *host, long interface_port);
160extern void Yap_InitPipes(void);
161extern void Yap_InitMem(void);
162extern void Yap_InitSockets(void);
163extern void Yap_InitSocketLayer(void);
164extern void Yap_InitMems(void);
165extern void Yap_InitConsole(void);
166extern void Yap_InitReadlinePreds(void);
167extern bool Yap_InitReadline(Term);
168extern bool Yap_readline_clear_pending_input(StreamDesc *s);
169extern void Yap_InitChtypes(void);
170extern void Yap_InitCharsio(void);
171extern void Yap_InitFormat(void);
172extern void Yap_InitFiles(void);
173extern void Yap_InitIOStreams(void);
174extern void Yap_InitWriteTPreds(void);
175extern void Yap_InitReadTPreds(void);
176extern void Yap_socketStream(StreamDesc *s);
177extern void Yap_ReadlineFlush(int sno);
178extern int Yap_ReadlinePeekChar(int sno);
179extern int Yap_ReadlineForSIGINT(void);
180extern bool Yap_DoPrompt(StreamDesc *s);
181
182extern int Yap_peek(int sno);
183extern int Yap_MemPeekc(int sno);
184
185extern int Yap_popChar(int sno);
186extern int Yap_popWide(int sno);
187extern int Yap_peekWithGetc(int sno);
188extern int Yap_peekWideWithGetwc(int sno);
189extern int Yap_peekWideWithSeek(int sno);
190 extern int Yap_peekWithSeek(int sno);
191extern int Yap_peekWide(int sno);
192extern int Yap_peekChar(int sno);
193
194
195extern Term Yap_syntax_error(TokEntry *tokptr, int sno, const char *msg);
196
197extern int console_post_process_read_char(int, StreamDesc *);
198extern int console_post_process_eof(StreamDesc *);
199extern int post_process_read_wchar(int, size_t, StreamDesc *);
200extern int post_process_weof(StreamDesc *);
201
202extern bool is_same_tty(FILE *f1, FILE *f2);
203
204extern int ISOWGetc(int sno);
205extern int GetUTF8(int sno);
206extern Term read_line(int sno);
207extern int PlGets(int sno, UInt size, char *buf);
208extern GetsFunc PlGetsFunc(void);
209extern int PlGetc(int sno);
210extern int FilePutc(int sno, int c);
211extern int DefaultGets(int, UInt, char *);
212extern int put_wchar(int sno, wchar_t ch);
213extern Int GetStreamFd(int sno);
214extern int ResetEOF(StreamDesc *s);
215extern int EOFPeek(int sno);
216extern int EOFWPeek(int sno);
217
218extern void Yap_SetAlias(Atom arg, int sno);
219extern bool Yap_AddAlias(Atom arg, int sno);
220extern int Yap_CheckAlias(Atom arg);
221extern int Yap_RemoveAlias(Atom arg, int snoinline);
222extern void Yap_SetAlias(Atom arg, int sno);
223extern void Yap_InitAliases(void);
224extern bool Yap_DeleteAliases(int sno);
225extern int Yap_FindStreamForAlias(Atom al);
226extern bool Yap_FetchStreamAlias(int sno, Term t2 USES_REGS);
227
228extern void count_output_char(int ch, StreamDesc *s);
229
230extern Term Yap_StreamUserName(int sno);
231
232extern void count_output_char(int ch, StreamDesc *s);
233
234char *Yap_VFAlloc(const char *path) ;
235
236inline static Term StreamName(int i) { return (GLOBAL_Stream[i].user_name); }
237
238inline static Atom StreamFullName(int i) { return (Atom)(GLOBAL_Stream[i].name); }
239
240inline static void console_count_output_char(int ch, StreamDesc *s) {
241 CACHE_REGS
242 if (ch == '\n') {
243#if MPWSHELL
244 if (mpwshell && (sno == StdOutStream || sno == StdErrStream) &&
245 !(s->status & Null_Stream_f)) {
246 putc(MPWSEP, s->file);
247 if (!(GLOBAL_Stream[LOCAL_output_stream].status & Null_Stream_f))
248 fflush(stdout);
249 }
250#endif
251 ++s->charcount;
252 ++s->linecount;
253 s->linestart = s->charcount;
254 LOCAL_newline = TRUE;
255 /* Inform we are not at the start of a newline */
256 } else {
257 LOCAL_newline = FALSE;
258#if MAC
259 if ((sno == StdOutStream || sno == StdErrStream) && s->linepos > 200)
260 sno->stream_putc(sno, '\n');
261#endif
262 ++s->charcount;
263 }
264}
265
266inline static Term StreamPositionToTerm(int charcount, int linecount, int linestart) {
267 CACHE_REGS
268 Term sargs[5];
269 sargs[0] = MkIntegerTerm(charcount);
270 sargs[1] = MkIntegerTerm(linecount);
271 sargs[2] = MkIntegerTerm(charcount+1-linestart);
272 sargs[3] = sargs[4] = MkIntTerm(0);
273 return Yap_MkApplTerm(FunctorStreamPos, 5, sargs);
274}
275
276inline static Term StreamPosition(int sno) {
277 Int cpos;
278 cpos = GLOBAL_Stream[sno].charcount;
279 LOCAL_StartCharCount = cpos;
280 LOCAL_StartLineCount = GLOBAL_Stream[sno].linecount;
281 LOCAL_StartLinePos = cpos +1 - GLOBAL_Stream[sno].linestart;
282 return StreamPositionToTerm( cpos, LOCAL_StartLineCount, LOCAL_StartLinePos);
283}
284
285
286inline static Term CurrentPositionToTerm(void) {
287 CACHE_REGS
288 return StreamPositionToTerm( LOCAL_StartCharCount,
289 LOCAL_StartLineCount,
290 LOCAL_StartLinePos );
291}
292
293char *Yap_MemExportStreamPtr(int sno);
294
295
296static inline void freeBuffer(const void *ptr) {
297 CACHE_REGS
298 if (ptr == NULL ||
299 ptr == AuxBase)
300 return;
301 free((void *)ptr);
302}
303
304extern int Yap_encoding_error(YAP_Int ch, int code, struct stream_desc *st);
305extern int Yap_symbol_encoding_error(YAP_Int ch, int code, struct stream_desc *st, const char *s);
306extern int Yap_bad_nl_error( Term t, struct stream_desc *st);
307#endif
308
Main definitions.
void Yap_SetAlias(Atom arg, int sno)
set arg as an alias to sno, but first check if not done before
Definition: alias.c:259
bool Yap_DeleteAliases(int sno)
purge all aliases for stream sno
Definition: alias.c:267
int Yap_RemoveAlias(Atom arg, int sno)
remove an alias arg for a stream
Definition: alias.c:353
int Yap_symbol_encoding_error(YAP_Int ch, int code, struct stream_desc *st, const char *s)
This is a bug while encoding a symbol, and should always result in a syntax error.
Definition: chartypes.c:127
Term Yap_scan_num(struct stream_desc *)
This routine is used when we need to parse a string into a number.
Definition: scanner.c:666
@ encoding
support for coding systens, YAP relies on UTF-8 internally
Definition: YapLFlagInfo.h:83
int EOFPeek(int sno)
caled after EOF found a peek, it just calls console_post_process to conclude the job
Definition: iopreds.c:763
Term Yap_read_term(int sno, Term opts, bool clause)
generic routine to read terms from a stream
Definition: readterm.c:1324
Definition: VFS.h:74
Definition: YapFlags.h:152