1/* BEGIN LICENSE BLOCK
2 * Version: CMPL 1.1
3 *
4 * The contents of this file are subject to the Cisco-style Mozilla Public
5 * License Version 1.1 (the "License"); you may not use this file except
6 * in compliance with the License.  You may obtain a copy of the License
7 * at www.eclipse-clp.org/license.
8 *
9 * Software distributed under the License is distributed on an "AS IS"
10 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See
11 * the License for the specific language governing rights and limitations
12 * under the License.
13 *
14 * The Original Code is  The ECLiPSe Constraint Logic Programming System.
15 * The Initial Developer of the Original Code is  Cisco Systems, Inc.
16 * Portions created by the Initial Developer are
17 * Copyright (C) 1989-2006 Cisco Systems, Inc.  All Rights Reserved.
18 *
19 * Contributor(s): ECRC GmbH
20 *
21 * END LICENSE BLOCK */
22
23
24/*
25 * SEPIA INCLUDE FILE
26 *
27 * VERSION	$Id: ec_io.h,v 1.5 2013/02/08 15:00:52 jschimpf Exp $
28 */
29
30/*
31 * IDENTIFICATION		io.h
32 *
33 * DESCRIPTION			Definitions concerning the I/O.
34 *
35 * CONTENTS:
36 *
37 */
38
39#define TTY_BUF_SIZE		128
40
41#define NO_PROMPT		D_UNKNOWN
42#define NO_BUF			((unsigned char *) 0)
43#define NO_PROC			((pri *) 0)
44#define NO_STREAM		((stream_id) 0)
45
46#define EOB_MARK		0
47
48#define StreamId(n)		(StreamDescriptors[n])	/* stream_id from int */
49#define StreamNr(nst)		((nst)->nr)	/* int from stream_id */
50
51#define StreamUnit(nst)		(nst)->unit
52#define StreamMode(nst)		(nst)->mode
53#define StreamOutputMode(nst)	(nst)->output_mode
54#define StreamPrintDepth(nst)	(nst)->print_depth
55#define StreamType(nst)		((nst)->mode & STYPE)
56#define StreamNref(nst)		(nst)->nref
57#define StreamCnt(nst)		(nst)->cnt
58#define StreamSize(nst)		(nst)->size
59#define StreamBuf(nst)		(nst)->buf
60#define StreamWBuf(nst)		(nst)->wbuf
61#define StreamPtr(nst)		(nst)->ptr
62#define StreamLexSize(nst)	(nst)->lex_size
63#define StreamLexAux(nst)	(nst)->lex_aux
64#define StreamLine(nst)		(nst)->line
65#define StreamName(nst)		(nst)->name
66#define StreamPath(nst)		(nst)->pathname
67#define StreamOffset(nst)	(nst)->offset
68#define StreamPrompt(nst)	(nst)->prompt
69#define StreamEncoding(nst)	(nst)->encoding
70#define StreamPromptStream(nst)	(nst)->prompt_stream
71#define StreamEvent(nst)	(nst)->event
72#define StreamRand(nst)		(nst)->rand
73#define StreamLastWritten(nst)	(nst)->last_written
74#define StreamPastEof(nst)	((nst)->mode & MEOF)
75#define StreamFILE(nst)		((FILE *) (nst)->stdfile)
76#define StreamMethods(nst)	(* (io_channel_t *) (nst)->methods)
77#define SetStreamMethods(nst,m)	(nst)->methods = (void_ptr) (m);
78
79#define StreamHandle(nst) \
80	( ++StreamNref(nst), ec_handle(&stream_tid, (t_ext_ptr) nst))
81
82
83/* some of the data is used for sockets differently */
84#define SocketInputStream(nst)	StreamPromptStream(nst)
85#define SocketUnix(nst)		StreamPrompt(nst)
86#define SocketType(nst)		StreamLexSize(nst)
87#define SocketConnection(nst)	StreamLexAux(nst)
88
89#define IsOpened(nst)		(StreamMode(nst) != SCLOSED)
90
91#define IsNullStream(nst)	((StreamMode(nst) & STYPE) == SNULL)
92#define IsTty(nst)		((StreamMode(nst) & STYPE) == STTY)
93#define IsStringStream(nst)	((StreamMode(nst) & STYPE) == SSTRING)
94#define IsQueueStream(nst)	((StreamMode(nst) & STYPE) == SQUEUE)
95#define IsPipeStream(nst)	((StreamMode(nst) & STYPE) == SPIPE)
96#define IsSocket(nst)		((StreamMode(nst) & STYPE) == SSOCKET)
97#define IsInvalidSocket(nst)	((StreamMode(nst) & (STYPE | SREAD)) == (SSOCKET | SREAD))
98
99#define IsFileStream(nst)	((StreamMode(nst) & STYPE) == SFILE)
100#define IsReadStream(nst)	(StreamMode(nst) & SREAD)
101#define IsWriteStream(nst)	(StreamMode(nst) & SWRITE)
102#define IsReadWriteStream(nst)	((StreamMode(nst) & SRDWR) == SRDWR)
103#define IsReadlineStream(nst)	(StreamMode(nst) & READLINE)
104
105#define IsTextStream(nst)	(StreamEncoding(nst) > SENC_OCTET)
106
107/* streams which can recover from being at eof */
108#define IsSoftEofStream(nst)	((StreamMode(nst) & SEOF_ACTION) == SEOF_RESET)
109
110#define SystemStream(nst)	(				\
111				    (nst) == current_input_ ||	\
112				    (nst) == current_output_ ||	\
113				    (nst) == current_err_ ||	\
114				    (nst) == warning_output_ ||	\
115				    (nst) == log_output_ ||	\
116				    (nst) == user_input_ ||	\
117				    (nst) == user_output_ ||	\
118				    (nst) == user_err_ ||	\
119				    (nst) == null_		\
120				)
121
122
123/****
124***** THIS IS PRIVATE TO THE I/O PART
125***** BE CAREFUL IF YOU INTEND TO USE THIS
126****/
127
128#define BUFSIZE 	1024
129
130#define NO_UNIT		(-1)
131
132#define MAX_NREF	32767	/* size of the nref field	*/
133
134/* stream mode flags */
135#define SCLOSED		0x0000	/* the channel is closed	*/
136#define SREAD		0x0001	/* READ allowed			*/
137#define SWRITE		0x0002	/* WRITE allowed		*/
138#define SRDWR		0x0003
139#define SAPPEND		0x0004	/* always with SWRITE, never with SREAD	*/
140				/* only in SFILE streams	*/
141#define SMODEBITS	0x0007
142
143/* stream type field */
144#define STYPE_SHIFT	3
145#define STYPE_NUM	((STYPE>>STYPE_SHIFT)+1)
146#define STYPE		0x0038	/* type mask			*/
147#define SFILE		0x0000
148#define SSTRING		0x0008
149#define SPIPE		0x0010
150#define SQUEUE		0x0018
151#define SNULL		0x0020
152#define SSOCKET		0x0028
153#define STTY		0x0030
154/* #define 		0x0038 */
155
156/* other stream properties */
157#define SEOLCR		0x0040	/* output CR(+LF) at end of line */
158#define SSYSTEM		0x0080	/* one of the system streams	*/
159#define DONT_PROMPT	0x0100	/* don't print the next prompt	*/
160#define MREAD		0x0200	/* we have read a buffer	*/
161#define MWRITE		0x0400	/* we wrote into the buffer	*/
162#define MEOF		0x0800	/* eof has been read (past eof)	*/
163#define SSCRAMBLE	0x1000	/* scramble stream data		*/
164#define SYIELD		0x2000	/* queues only: yield on eof or flush */
165#define REPROMPT_ONLY	0x4000	/* suppress initial prompts on this stream */
166#define SFLUSHEOL	0x8000	/* flush stream at end-of-line	*/
167// #define READLINE	0x8000	/* use readline() on this stream */
168#define SNONBLOCKING	0x10000	/* don't block if stream not ready */
169#define SSIGIO		0x20000	/* SIGIO is enabled for this stream */
170#define SNOMACROEXP	0x40000	/* suppress macro expansion	*/
171#define SCOMPRESS	0x80000	/* try to compress output on this stream */
172#define SSELECTABLE	0x100000 /* stream supports select/3 */
173#define SNUMBERUSED	0x200000 /* stream may be referred to by number */
174#define SDELETELOST	0x400000 /* delete file when stream handle is lost */
175#define SDELETECLOSED	0x800000 /* delete file when stream is closed */
176#define SREPOSITION	0x1000000 /* can be repositioned (seek) */
177
178#define SEOF_ACTION	0x6000000 /* eof_action (ISO):			*/
179#define SEOF_ERROR	0x0000000 /* treat past-eof as permission error	*/
180#define SEOF_RESET	0x2000000 /* return eof code and allow repeat	*/
181#define SEOF_CODE	0x4000000 /* return eof code (-1,end_of_file)	*/
182
183
184/* how many characters can be ungotten */
185#define LOOKAHEAD		4
186
187/* encodings (0 binary, >0 text) */
188#define SENC_OCTET	0
189#define SENC_ASCII	1
190#define SENC_LATIN1	2
191#define SENC_NUM	3
192#define SENC_DEFAULT	SENC_LATIN1
193
194/* options for ec_close_stream() */
195#define CLOSE_FORCE	1
196#define CLOSE_LOST	2
197
198/*
199 * action codes for io_rpc()
200 */
201#define IO_FLUSH	1
202#define IO_FILL		2
203#define IO_CLOSE	3
204#define IO_TTYIN	4
205#define IO_BIND		5
206#define IO_CONNECT	6
207#define IO_LISTEN	7
208#define IO_ACCEPT	8
209
210#define Lock_Stream(nst) {              \
211        if (ec_options.parallel_worker) {          \
212           a_mutex_lock(&nst->lock);    \
213        }                               \
214}
215#define Unlock_Stream(nst) {            \
216        if (ec_options.parallel_worker) {          \
217           a_mutex_unlock(&nst->lock);  \
218        }                               \
219}
220
221#define RemoteStream(nst) \
222	(nst->fd_pid && nst->fd_pid != own_pid && nst->aport)
223
224
225/* defines for the output functions */
226
227#define	DEFAULT_OUTPUT_MODE	ATTRIBUTE
228
229#define	CANONICAL		1	/* ignore operators		*/
230#define	FULLDEPTH		2	/* ignore depth			*/
231#define	DOTLIST			4	/* write lists in dot notation	*/
232#define	QUOTED			8	/* print quotes when needed	*/
233#define	VAR_NUMBERS	     0x10	/* print var number only	*/
234#define	VAR_NAMENUM	     0x20	/* print var name (if any) and number */
235#define	PRINT_CALL	     0x40	/* print was called, use portray */
236#define	WRITE_COMPACT	     0x80	/* don't print unnecessary spaces */
237#define	ATTRIBUTE	    0x100	/* print metaterm attribute	*/
238#define	WRITE_GOAL	    0x200	/* printed term is a goal	*/
239#define	STD_ATTR	    0x400	/* attribute in standard form	*/
240#define	NO_MACROS	    0x800	/* don't apply write macros	*/
241#define	WRITE_CLAUSE	   0x1000	/* printed term is a clause	*/
242#define	DONT_QUOTE_NL	   0x2000	/* print newlines even when quoted */
243#define	VAR_ANON	   0x4000	/* print variables as _		*/
244#define	OUT_DOLLAR_VAR	   0x8000	/* print $VAR(I) as variables	*/
245#define	PORTRAY_VAR	  0x10000	/* call portray even for variables */
246#define	TERM_FULLSTOP	  0x20000	/* terminate term with a fullstop */
247#define	TERM_NEWLINE	  0x40000	/* terminate term with a newline */
248#define	OUTPUT_MODES	       19
249
250#define	PORTRAY2	 0x100000	/* a portray/2 predicate exists	*/
251#define	PORTRAY1	 0x200000	/* a portray/1 predicate exists	*/
252#define	VARTERM		 0x400000	/* print variables as '_'(...)	*/
253
254/* context of the term being written */
255
256#define NOARG		0x00000000
257#define ARGTERM		0x00000001
258#define ARGOP		0x00000002
259#define ARGPREF		0x00000004
260#define ARGSIGN		0x00000008
261#define ARGLIST		0x00000010
262#define ARGYF		0x00000020	/* parent is yf[x] */
263#define ARGLAST		0x00000040	/* last subterm of its parent operator*/
264
265
266/*
267 * Types
268 */
269
270typedef struct {
271    int		io_type;
272    int		mode_defaults;
273    int		buf_size_hint;
274    int		(*close) ARGS((int));
275    int		(*ready) ARGS(());
276    int		(*read) ARGS((int,char*,int));
277    int		(*write) ARGS((int,char*,int));
278    int		(*at) ARGS((stream_id nst, long int*));
279    int		(*at_eof) ARGS((stream_id));
280    int		(*buffer_nonempty) ARGS((stream_id));
281    int		(*truncate) ARGS(());
282    int		(*seek) ARGS((stream_id,long,int));
283    int		(*flush) ARGS((stream_id));
284    int		(*size) ARGS((stream_id));
285    int		(*content) ARGS((stream_id,char*));
286    int		(*outf) ARGS((stream_id,char*,int));
287} io_channel_t;
288
289
290/*
291 * EXTERNAL VARIABLE DECLARATIONS:
292 */
293
294Extern int		own_pid;
295Extern void		my_io_aport();
296
297Extern	int	ec_pwrite ARGS((int,int,stream_id,value,type,int,int,dident,type));
298Extern	int	ec_tty_in ARGS((stream_id));
299Extern	int	ec_tty_out ARGS((stream_id, int));
300Extern	int	ec_tty_outs ARGS((stream_id, char*, int));
301Extern	int	ec_seek_stream ARGS((stream_id,long,int));
302Extern	int	ec_stream_at ARGS((stream_id,long*));
303Extern	int	ec_close_stream ARGS((stream_id, int));
304Extern	int	ec_outfw ARGS((stream_id, word));
305Extern	int	ec_outfc ARGS((stream_id, int));
306Extern	int	ec_getch ARGS((stream_id));
307Extern	int	ec_ungetch ARGS((stream_id));
308
309Extern	int	fill_buffer ARGS((stream_id));
310Extern	int	io_flush_out ARGS((stream_id));
311Extern	int	set_sigio ARGS((int));
312Extern	int	reset_sigio ARGS((int));
313Extern	int	ec_stream_set_sigio ARGS((stream_id, int));
314Extern	int	ec_stream_reset_sigio ARGS((stream_id, int));
315Extern	int	ec_setup_stream_sigio_thread ARGS((stream_id));
316Extern	int	ec_reenable_sigio ARGS((stream_id, int, int));
317Extern	void	mark_dids_from_streams ARGS((void));
318Extern	int	p_reset ARGS((void));
319Extern stream_id find_free_stream ARGS((void));
320Extern void	init_stream ARGS((stream_id,int unit,int mode,dident name,
321			dident prompt,stream_id pstream, int size));
322Extern stream_id ec_open_file ARGS((char*,int,int*));
323Extern stream_id get_stream_id ARGS((value,type,int,int*));
324Extern char	*ec_getstring ARGS((stream_id,word,word*));
325Extern 	int	set_stream(dident, stream_id);
326
327Extern t_ext_type stream_tid;
328