YAP 7.1.0
yio.yap
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: yio.yap *
12* Last rev: *
13* mods: *
14* comments: Input output predicates *
15* *
16*************************************************************************/
17
18
19
20:- system_module( '$_yio', [at_end_of_line/0,
22 consult_depth/1,
30 fileerrors/0,
32 nofileerrors/0,
35 read/1,
36 read/2,
37 sformat/3,
46 ttynl/0,
50 write_depth/2], ['$default_expand'/1,
51 '$extend_file_search_path'/1,
52 '$set_default_expand'/1]).
53
54:- '$system_catch'/4use_system_module( '$_boot', []).
55
56:- '$do_error'/2use_system_module( '$_errors', []).
57
58/** @defgroup InputOutput Input/Output Predicates
59 ⁸ @ingroup Builtins
60 *
61 * @{
62 *
63 * Some of the Input/Output predicates described below will in certain conditions
64 * provide error messages and abort only if the file_errors flag is set.
65 * If this flag is cleared the same predicates will just fail. Details on
66 * setting and clearing this flag are given under 7.7.
67 * @}
68 */
69
70/* stream predicates */
71
72/** @defgroup IOSockets YAP Old Style Socket and Pipe Interface
73 * @ingroup InputOutput
74 * @{
75 *
76 * Autoload the socket/pipe library
77 *
78 * */
79
80
81/** @pred socket(+ _DOMAIN_,- _SOCKET_)
82 *
83 * Call socket/4 with _TYPE_ bound to `SOCK_STREAM'` and
84 * _PROTOCOL_ bound to `0`.
85 *
86 *
87*/
88
89/** @pred socket(+ _DOMAIN_,+ _TYPE_,+ _PROTOCOL_,- _SOCKET_)
90 *
91 * Corresponds to the BSD system call `socket`. Create a socket for
92 * domain _DOMAIN_ of type _TYPE_ and protocol
93 * _PROTOCOL_. Both _DOMAIN_ and _TYPE_ should be atoms,
94 * whereas _PROTOCOL_ must be an integer.
95 * The new socket object is
96 * accessible through a descriptor bound to the variable _SOCKET_.
97 *
98 * The current implementation of YAP accepts socket
99 * domains `AF_INET` and `AF_UNIX`.
100 * Socket types depend on the
101 * underlying operating system, but at least the following types are
102 * supported: `SOCK_STREAM` and `SOCK_DGRAM` (untested in 6.3).
103 *
104 *
105*/
106
107/** @pred socket_connect(+ _SOCKET_, + _PORT_, - _STREAM_)
108 *
109 * Interface to system call `connect`, used for web clients: connect
110 * socket _SOCKET_ to _PORT_. The connection results in the
111 * read/write stream _STREAM_.
112 *
113 * Port information depends on the domain:
114 *
115 * + 'AF_UNIX'(+ _FILENAME_)
116 * connect to socket at file _FILENAME_.
117 *
118 * + 'AF_INET'(+ _HOST_,+ _PORT_)
119 * Connect to socket at host _HOST_ and port _PORT_.
120*/
121
122
123/** @pred open_pipe_streams(Read, Write)
124 *
125 * Autoload old pipe access interface
126 *
127*/
128
129%! @}
130
131
132
133%! @addtogroup WriteTerm
134%! @ingroup InputOutput
135%% @{
136
137
138/* meaning of flags for '$write' is
139 1 quote illegal atoms
140 2 ignore operator declarations
141 4 output '$VAR'(N) terms as A, B, C, ...
142 8 use portray(_)
143
144 flags are defined in yapio.h
145*/
146
147/* @pred user:portray(T)
148
149 This is the user defined hook that is called by write_term/2 with
150 the portray option and print/1.
151 */
152'$portray'(T) :-
153 \+ '$undefined'(portray(_),user),
154 catch(user:portray(T),Error,'$Error'(Error)), catch,
155 set_value('$portray',true), set_value.
156'$portray'(_) :- set_value('$portray',false), set_value.
157
158%! @}
159
160%! @addtogroup Format
161%! @ingroup InputOutput
162
163%% @{
164
165/** @pred format(+ _T_)
166
167Print formatted output to the current output stream.
168
169
170*/
171format(T) :-
172 format(T, []).
173
174%! @}
175
176/**
177 * @addtogroup CharIO
178 * @ingroup InputOutput
179 *
180 * @{
181 *
182 * @brief character I/O
183 */
184
185/** @pred ttyget(- _C_)
186
187
188The same as `get(C)`, but from stream user_input.
189
190
191*/
192ttyget(N) :- get(user_input,N).
193
194/** @pred ttyget0(- _C_)
195
196
197The same as `get0(C)`, but from stream user_input.
198
199
200*/
201ttyget0(N) :- get0(user_input,N).
202
203/** @pred ttyskip(- _C_)
204
205
206Like skip/1, but always using stream user_input.
207stream.
208
209
210*/
211ttyskip(N) :- N1 is N, '$skip'(user_input,N1).
212
213/** @pred ttyput(+ _N_)
214
215
216As `put(N)` but always to user_output.
217
218
219*/
220ttyput(N) :- N1 is N, put(user_output,N1).
221
222/** @pred ttynl
223
224
225Outputs a new line to stream user_output.
226
227*/
228put :- nl(user_output).
229
230%! @}
231
232%! @addtogroup StreamM
233%! @ingroup InputOutput
234%! @{
235
236/** @pred current_line_number(- _LineNumber_)
237
238Unify _LineNumber_ with the line number for the current output stream.
239
240*/
242 current_input(Stream),
243 line_count(Stream, N).
244
245/** @pred current_line_number(+ _Stream_,- _LineNumber_)
246
247Unify _LineNumber_ with the line number for _Stream_.
248
249*/
250current_line_number(Stream,N) :-
251 line_count(Stream, N).
252
253/** @pred stream_position(+ _Stream_,- _StreamPosition_)
254
255Unify _StreamPosition_ with the packaged information of position on
256current stream _Stream_. Use stream_position_data/3 to
257retrieve information on character or line count.
258
259*/
260stream_position(Stream, Position) :-
261 stream_property(Stream, position(Position)).
262
263/** @pred stream_position(+ _Stream_,- _StreamPosition_, +_NewPosition_)
264
265Unify _StreamPosition_ with the packaged information of position on
266current stream _Stream_ an then moves to position _NewPosition_.
267
268*/
269stream_position(Stream, Position, NewPosition) :-
270 stream_property(Stream, position(Position)),
271 set_stream_position(Stream, NewPosition).
272
273/** @pred at_end_of_line
274
275 Tests whether the next character in the current input stream is a line break character.
276*/
277
278set_stream_position :-
279 current_input(S),
281
282/** @pred at_end_of_line( +Stream )
283
284 Tests whether the next character in the stream is a line break character.
285*/
286at_end_of_line(S) :-
287 stream_property(S, end_of_stream(past)), stream_property.
288at_end_of_line(S) :-
289 peek_code(S,N), ( N = 10 -> true ; N = -1).
290
291/** @pred current_char_conversion(? _IN_,? _OUT_) is iso
292
293
294If _IN_ is unbound give all current character
295translations. Otherwise, give the translation for _IN_, if one
296exists.
297
298
299*/
301 var(X), var,
302 '$all_char_conversions'(List),
303 '$fetch_char_conversion'(List,X,Y).
305 '$current_char_conversion'(X,Y).
306
307
308'$fetch_char_conversion'([X,Y|_],X,Y).
309'$fetch_char_conversion'([_,_|List],X,Y) :-
310 '$fetch_char_conversion'(List,X,Y).
311
312split_path_file(File, Path, Name) :-
313 file_directory_name(File, Path),
314 file_base_name(File, Name).
315
316%! @}
317
318%! @addtogroup StreamM
319%! @{
320
321/** @pred current_stream( _F_, _M_, _S_)
322
323
324Defines the relation: The stream _S_ is opened on the file _F_
325in mode _M_. It might be used to obtain all open streams (by
326backtracking) or to access the stream for a file _F_ in mode
327 _M_, or to find properties for a stream _S_. Notice that some
328streams might not be associated to a file: in this case YAP tries to
329return the file number. If that is not available, YAP unifies _F_
330with _S_.
331*/
332current_stream(File, Mode, Stream) :-
333 stream_property(Stream, mode(Mode)),
334 '$stream_name'(Stream, File).
335
336'$stream_name'(Stream, File) :-
337 stream_property(Stream, file_name(File)), stream_property.
338'$stream_name'(Stream, file_no(File)) :-
339 stream_property(Stream, file_no(File)), stream_property.
340'$stream_name'(Stream, Stream).
341
342'$extend_file_search_path'(P) :-
343 atom_codes(P,S),
344 '$env_separator'(ES),
345 /** @pred stream_position_data(+ _Field_,+ _StreamPosition_,- _Info_)
346 Extract values from stream position objects.
347
348
349 '$stream_position' is of the format '$stream_position'(Byte, Char,
350 Line, LinePos). Given the packaged stream position term
351 _StreamPosition_, unify _Info_ with _Field_ `line_count`,
352 `byte_count`, or `char_count`.
353
354 */
355stream_position_data(Prop, Term, Value) :-
356 nonvar(Prop), nonvar,
357 ( '$stream_position_field'(Prop, Pos)
358 -> arg(Pos, Term, Value)
359 ; '$do_error'(domain_error(stream_position_data), Prop)
360 ).
361stream_position_data(Prop, Term, Value) :-
362 '$stream_position_field'(Prop, Pos),
363 arg(Pos, Term, Value).
364
365'$stream_position_field'(char_count, 1).
366'$stream_position_field'(line_count, 2).
367'$stream_position_field'(line_position, 3).
368'$stream_position_field'(byte_count, 4).
369
370'$set_encoding'(Enc) :-
371 set_stream(loop_stream, encoding(Enc)).
372
373%! @}
374
375/**
376 * @defgroup FilesM File and Directory Operations
377 * @ingroup InputOutput
378 * @{
379 *
380 */
381'$codes_to_chars'(String0, String, String0) :- String0 == String, '$codes_to_chars'.
382'$codes_to_chars'(String0, [Code|String], [Char|Chars]) :-
383 atom_codes(Char, [Code]),
384 '$codes_to_chars'(String0, String, Chars).
385
386
387
388/** @pred exists(+ _F_)
389
390Checks if file _F_ exists in the current directory.
391
392*/
393exists(F) :-
394 absolute_file_name(F, _, [file_errors(fail),access(exist),expand(true)]).
395
396
397/** @pred file_exists(+ _File__)
398
399The atom _File_ corresponds to an existing file or directory.
400
401
402*/
403file_exists(IFile) :-
404 absolute_file_name(IFile, _File, [expand(true), solutions(first), access(exist)]).
405
406/** @pred rename(+F , +G)
407
408 Renames the single file _F_ to _G_.
409*/
410rename(IFile, OFile) :-
411 absolute_file_name(IFile, IF, [access(read),expand(true)]),
412 absolute_file_name(OFile, OF, [expand(true)]),
413 '$rename'(IF, OF).
414
415/** @pred access_file(+F , +G)
416
417 Verify whether file F respects property _G_. The file is processed
418 with absolute_file_name.
419*/
420access_file(IFile, Access) :-
421 absolute_file_name(IFile, _IF, [access(Access),expand(true)]).
422
423/** @pred prolog_file_name( +File, -PrologFileaName)
424
425Unify _PrologFileName_ with the Prolog file associated to _File_.
426
427*/
428prolog_file_name(File, PrologFileName) :-
429 var(File), var,
430 '$do_error'(instantiation_error, prolog_file_name(File, PrologFileName)).
431prolog_file_name(user, Out) :- prolog_file_name, Out = prolog_file_name.
432prolog_file_name(File, PrologFileName) :-
433 atom(File), atom,
434 atom:true_file_name(File, PrologFileName).
435prolog_file_name(File, PrologFileName) :-
436 '$do_error'(type_error(atom,File), prolog_file_name(File, PrologFileName)).
437
438 /** @pred fileerrors
439
440 Switches on the file_errors flag so that in certain error conditions
441 Input/Output predicates will produce an appropriated message and abort.
442
443 */
444'$do_error' :-
445 yap_flag(file_errors, _, error).
446
447
448 /**
449 @pred nofileerrors
450
451 Switches off the `file_errors` flag, so that the predicates see/1,
452 tell/1, open/3 and close/1 just fail, instead of producing
453 an error message and aborting whenever the specified file cannot be
454 opened or closed.
455
456 */
457yap_flag :-
458 yap_flag(file_errors, _, fail).
459
460/**
461@}
462*/
463
absolute_file_name( -File:atom, +Path:atom, +Options:list)
catch( : Goal,+ Exception,+ Action)
current_input(+ S)
format(+ T)
read(- T)
read(+ Stream, -Term )
stream_property( Stream, Prop )
Definition: top.yap:2
get(S, - C)
nl(+ S)
peek_code(+ S, - C)
ttyget0(- C)
ttyget(- C)
ttyput(+ N)
ttyskip(- C)
access_file(+F , +G)
exists(+ F)
file_exists(+ _File__)
prolog_file_name( +File, -PrologFileaName)
rename(+F , +G)
format(+ T, :L)
open_pipe_streams(Read, Write)
socket(+ DOMAIN,- SOCKET)
socket(+ DOMAIN,+ TYPE,+ PROTOCOL,- SOCKET)
socket_connect(+ SOCKET, + PORT, - STREAM)
atom_codes(?Atom, ?Codes)
at_end_of_line( +Stream )
current_char_conversion(? IN,? OUT)
current_line_number(- LineNumber)
current_line_number(+ Stream,- LineNumber)
current_stream( F, M, S)
stream_position(+ Stream,- StreamPosition)
stream_position(+ Stream,- StreamPosition, +_NewPosition_)
stream_position_data(+ Field,+ StreamPosition,- Info)
display(+ T)
display(+ S, T)
set_value(+ A,+ C)
arg(+ N,+ T, A)
atom( T)
nonvar( T)
var( T)