1 /*************************************************************************
2 * *
3 * YAP Prolog *
4 * *
5 * Yap Prolog was developed at NCCUP - Universidade do Porto *
6 * *
7 * Copyright L.Damas, V.S.Costa and Universidade do Porto 1985-1997 *
8 * *
9 **************************************************************************
10 * *
11 * File: iopreds.c *
12 * Last rev: 5/2/88 *
13 * mods: *
14 * comments: Input/Output C implemented predicates *
15 * *
16 *************************************************************************/
17 #ifdef SCCS
18 static char SccsId[] = "%W% %G%";
19 #endif
20
21 /*
22 * This file defines main data-structure for stream management,
23 *
24 */
25
26 #if defined(_MSC_VER) || defined(__MINGW32__)
27
28 #include <windows.h>
29
30 #endif
31
32 #include <wchar.h>
33
34 #if HAVE_LIBREADLINE
35
36 #if defined(_MSC_VER) || defined(__MINGW32__)
37
38 FILE *rl_instream, *rl_outstream;
39 #endif
40
41 #endif
42
43 #define MEM_BUF_CODE 0
44 #define MEM_BUF_MALLOC 1
45
46 typedef int (*GetsFunc)(int, UInt, char *);
47
48 typedef struct stream_desc
49 {
50 union {
51 struct {
52 struct io_stream *swi_ptr;
53 } swi_stream;
54 struct {
55 Atom name;
56 Term user_name;
57 #if defined(__MINGW32__) || defined(_MSC_VER)
58 #define PLGETC_BUF_SIZE 4096
59 char *buf, *ptr;
60 int left;
61 #endif
62 YP_File file;
63 } file;
64 struct {
65 char *buf; /* where the file is being read from/written to */
66 int src; /* where the space comes from, 0 code space, 1 malloc */
67 Int max_size; /* maximum buffer size (may be changed dynamically) */
68 UInt pos;
69 volatile void *error_handler;
70 } mem_string;
71 struct {
72 #if defined(__MINGW32__) || defined(_MSC_VER)
73 HANDLE hdl;
74 #else
75 int fd;
76 #endif
77 } pipe;
78 #if USE_SOCKET
79 struct {
80 socket_domain domain;
81 socket_info flags;
82 int fd;
83 } socket;
84 #endif
85 } u;
86 Int charcount, linecount, linepos;
87 Int status;
88 int och;
89 #if defined(YAPOR) || defined(THREADS)
90 lockvar streamlock; /* protect stream access */
91 #endif
92 int (* stream_putc)(int, int); /* function the stream uses for writing */
93 int (* stream_getc)(int); /* function the stream uses for reading */
94 GetsFunc stream_gets; /* function the stream uses for reading a sequence of characters */
95 /* function the stream uses for parser. It may be different if the ISO
96 character conversion is on */
97 int (* stream_wgetc_for_read)(int);
98 int (* stream_wgetc)(int);
99 int (* stream_wputc)(int,wchar_t);
100 encoding_t encoding;
101 mbstate_t mbstate;
102 }
103 StreamDesc;
104
105 #define YAP_ERROR NIL
106
107 #define MaxStreams 64
108
109 #define Free_Stream_f 0x000001
110 #define Output_Stream_f 0x000002
111 #define Input_Stream_f 0x000004
112 #define Append_Stream_f 0x000008
113 #define Eof_Stream_f 0x000010
114 #define Null_Stream_f 0x000020
115 #define Tty_Stream_f 0x000040
116 #define Socket_Stream_f 0x000080
117 #define Binary_Stream_f 0x000100
118 #define Eof_Error_Stream_f 0x000200
119 #define Reset_Eof_Stream_f 0x000400
120 #define Past_Eof_Stream_f 0x000800
121 #define Push_Eof_Stream_f 0x001000
122 #define Seekable_Stream_f 0x002000
123 #define Promptable_Stream_f 0x004000
124 #if USE_SOCKET
125 #define Client_Socket_Stream_f 0x008000
126 #define Server_Socket_Stream_f 0x010000
127 #endif
128 #define InMemory_Stream_f 0x020000
129 #define Pipe_Stream_f 0x040000
130 #define Popen_Stream_f 0x080000
131 #define User_Stream_f 0x100000
132 #define HAS_BOM_f 0x200000
133 #define RepError_Prolog_f 0x400000
134 #define RepError_Xml_f 0x800000
135 #define SWI_Stream_f 0x1000000
136
137 #define EXPAND_FILENAME 0x000080
138
139 #define StdInStream 0
140 #define StdOutStream 1
141 #define StdErrStream 2
142
143 #define ALIASES_BLOCK_SIZE 8
144
145 void STD_PROTO (Yap_InitStdStreams, (void));
146 Term STD_PROTO (Yap_StreamPosition, (int));
147
148 EXTERN inline int
GetCurInpPos(int inp_stream)149 GetCurInpPos (int inp_stream)
150 {
151 return (Stream[inp_stream].linecount);
152 }
153
154