use SWI user_*.

This commit is contained in:
Vitor Santos Costa 2011-02-13 01:03:08 +00:00
parent 0d0a95ae8a
commit 80f190bc8a
10 changed files with 21 additions and 197 deletions

View File

@ -352,96 +352,9 @@ PlGetsFunc(void)
return PlGets; return PlGets;
} }
static void
InitFileIO(StreamDesc *s)
{
s->stream_gets = PlGetsFunc();
{
/* check if our console is promptable: may be tty or pipe */
if (s->status & (Promptable_Stream_f)) {
/* the putc routine only has to check it is putting out a newline */
s->stream_putc = ConsolePutc;
s->stream_wputc = put_wchar;
/* if a tty have a special routine to call readline */
#if HAVE_LIBREADLINE && HAVE_READLINE_READLINE_H
if (s->status & Tty_Stream_f) {
if (Stream[0].status & Tty_Stream_f &&
is_same_tty(s->u.file.file,Stream[0].u.file.file))
s->stream_putc = ReadlinePutc;
s->stream_wputc = put_wchar;
s->stream_getc = ReadlineGetc;
} else
#endif
{
/* else just PlGet plus checking for prompt */
s->stream_getc = ConsoleGetc;
}
} else {
/* we are reading from a file, no need to check for prompts */
s->stream_putc = FilePutc;
s->stream_wputc = put_wchar;
s->stream_getc = PlGetc;
s->stream_gets = PlGetsFunc();
}
}
s->stream_wgetc = get_wchar;
}
static void
InitStdStream (int sno, SMALLUNSGN flags, YP_File file)
{
StreamDesc *s = &Stream[sno];
s->u.file.file = file;
s->status = flags;
s->linepos = 0;
s->linecount = 1;
s->charcount = 0;
s->encoding = DefaultEncoding();
INIT_LOCK(s->streamlock);
unix_upd_stream_info(s);
/* Getting streams to prompt is a mess because we need for cooperation
between readers and writers to the stream :-(
*/
InitFileIO(s);
switch(sno) {
case 0:
s->u.file.name=AtomUserIn;
break;
case 1:
s->u.file.name=AtomUserOut;
break;
default:
s->u.file.name=AtomUserErr;
break;
}
s->u.file.user_name = MkAtomTerm (s->u.file.name);
if (CharConversionTable != NULL)
s->stream_wgetc_for_read = ISOWGetc;
else
s->stream_wgetc_for_read = s->stream_wgetc;
#if LIGHT
s->status |= Tty_Stream_f|Promptable_Stream_f;
#endif
#if HAVE_SETBUF
if (s->status & Tty_Stream_f &&
sno == 0) {
/* make sure input is unbuffered if it comes from stdin, this
makes life simpler for interrupt handling */
YP_setbuf (stdin, NULL);
// fprintf(stderr,"here I am\n");
}
#endif /* HAVE_SETBUF */
}
static void static void
InitStdStreams (void) InitStdStreams (void)
{ {
InitStdStream (StdInStream, Input_Stream_f, stdin);
InitStdStream (StdOutStream, Output_Stream_f, stdout);
InitStdStream (StdErrStream, Output_Stream_f, stderr);
Yap_c_input_stream = StdInStream; Yap_c_input_stream = StdInStream;
Yap_c_output_stream = StdOutStream; Yap_c_output_stream = StdOutStream;
Yap_c_error_stream = StdErrStream; Yap_c_error_stream = StdErrStream;
@ -4527,7 +4440,7 @@ static Int
format2(UInt stream_flag) format2(UInt stream_flag)
{ {
int old_c_stream = Yap_c_output_stream; int old_c_stream = Yap_c_output_stream;
int mem_stream = FALSE, codes_stream = FALSE; int codes_stream = FALSE;
Int out; Int out;
Term tin = Deref(ARG1); Term tin = Deref(ARG1);
@ -4543,25 +4456,7 @@ format2(UInt stream_flag)
return FALSE; return FALSE;
} }
out = format(Deref(ARG2),Deref(ARG3),Yap_c_output_stream); out = format(Deref(ARG2),Deref(ARG3),Yap_c_output_stream);
if (mem_stream) { {
Term tat;
Term inp = Deref(ARG1);
int stream = Yap_c_output_stream;
Yap_c_output_stream = old_c_stream;
if (out) {
Stream[stream].u.mem_string.buf[Stream[stream].u.mem_string.pos] = '\0';
if (codes_stream) {
tat = Yap_StringToDiffList(Stream[stream].u.mem_string.buf, ArgOfTerm(2,inp));
} else {
tat = MkAtomTerm(Yap_LookupAtom(Stream[stream].u.mem_string.buf));
}
CloseStream(stream);
if (!Yap_unify(tat,ArgOfTerm(1,inp)))
return FALSE;
} else {
CloseStream(stream);
}
} else {
Yap_c_output_stream = old_c_stream; Yap_c_output_stream = old_c_stream;
} }
return out; return out;

View File

@ -61,20 +61,6 @@ typedef struct stream_desc
#endif #endif
YP_File file; YP_File file;
} file; } file;
struct {
char *buf; /* where the file is being read from/written to */
int src; /* where the space comes from, 0 code space, 1 malloc */
Int max_size; /* maximum buffer size (may be changed dynamically) */
UInt pos;
volatile void *error_handler;
} mem_string;
struct {
#if defined(__MINGW32__) || defined(_MSC_VER)
HANDLE hdl;
#else
int fd;
#endif
} pipe;
} u; } u;
Int charcount, linecount, linepos; Int charcount, linecount, linepos;
Int status; Int status;

View File

@ -932,8 +932,6 @@ RestoreStreams(void)
for (sno = 0; sno < MaxStreams; ++sno) { for (sno = 0; sno < MaxStreams; ++sno) {
if (Stream[sno].status & Free_Stream_f) if (Stream[sno].status & Free_Stream_f)
continue; continue;
if (Stream[sno].status & (Socket_Stream_f|Pipe_Stream_f|InMemory_Stream_f))
continue;
Stream[sno].u.file.user_name = AtomTermAdjust(Stream[sno].u.file.user_name); Stream[sno].u.file.user_name = AtomTermAdjust(Stream[sno].u.file.user_name);
Stream[sno].u.file.name = AtomAdjust(Stream[sno].u.file.name); Stream[sno].u.file.name = AtomAdjust(Stream[sno].u.file.name);
} }

View File

@ -212,34 +212,6 @@ typedef struct VARSTRUCT {
#define EOFCHAR EOF #define EOFCHAR EOF
#if USE_SOCKET
/****************** defines for sockets *********************************/
typedef enum{ /* in YAP, sockets may be in one of 4 possible status */
new_socket,
server_socket,
client_socket,
server_session_socket,
closed_socket
} socket_info;
typedef enum{ /* we accept two domains for the moment, IPV6 may follow */
af_inet, /* IPV4 */
af_unix /* or AF_FILE */
} socket_domain;
Term STD_PROTO(Yap_InitSocketStream,(int, socket_info, socket_domain));
int STD_PROTO(Yap_CheckStream,(Term, int, char *));
int STD_PROTO(Yap_CheckSocketStream,(Term, char *));
socket_domain STD_PROTO(Yap_GetSocketDomain,(int));
socket_info STD_PROTO(Yap_GetSocketStatus,(int));
void STD_PROTO(Yap_UpdateSocketStream,(int, socket_info, socket_domain));
/* routines in ypsocks.c */
Int STD_PROTO(Yap_CloseSocket,(int, socket_info, socket_domain));
#endif /* USE_SOCKET */
/* info on aliases */ /* info on aliases */
typedef struct AliasDescS { typedef struct AliasDescS {
Atom name; Atom name;

View File

@ -73,7 +73,7 @@ lookupBlob(void *blob, size_t len, PL_blob_t *type)
b->NextOfPE = NIL; b->NextOfPE = NIL;
b->KindOfPE = BlobProperty; b->KindOfPE = BlobProperty;
b->blob_t = type; b->blob_t = type;
ae = (AtomEntry *)Yap_AllocCodeSpace(sizeof(AtomEntry)+len); ae = (AtomEntry *)Yap_AllocCodeSpace(sizeof(AtomEntry)+len+sizeof(size_t));
if (!ae) if (!ae)
return NULL; return NULL;
INIT_RWLOCK(ae->ARWLock); INIT_RWLOCK(ae->ARWLock);

View File

@ -362,6 +362,11 @@ initIO()
addHTable(streamAliases, (void *)*np, (void *)(intptr_t)i); addHTable(streamAliases, (void *)*np, (void *)(intptr_t)i);
GD->io_initialised = TRUE; GD->io_initialised = TRUE;
#if __YAP_PROLOG__
Yap_LookupSWIStream(Suser_input);
Yap_LookupSWIStream(Suser_output);
Yap_LookupSWIStream(Suser_error);
#endif
} }

View File

@ -32,7 +32,7 @@ typedef YAP_Term (*Func)(term_t); /* foreign functions */
extern const char *Yap_GetCurrentPredName(void); extern const char *Yap_GetCurrentPredName(void);
extern YAP_Int Yap_GetCurrentPredArity(void); extern YAP_Int Yap_GetCurrentPredArity(void);
extern int Yap_read_term(term_t t, IOSTREAM *st, term_t *vs); extern int Yap_read_term(term_t t, IOSTREAM *st, term_t *vs);
extern int Yap_LookupSWIStream(void *swi_s);
extern atom_t codeToAtom(int chrcode); extern atom_t codeToAtom(int chrcode);

View File

@ -1107,7 +1107,7 @@ bootstrap(F) :-
'$loop'(Stream,Status) :- '$loop'(Stream,Status) :-
%VSC '$change_alias_to_stream'('$loop_stream',Stream), '$change_alias_to_stream'('$loop_stream',Stream),
repeat, repeat,
%VSC ( '$current_stream'(_,_,Stream) -> true %VSC ( '$current_stream'(_,_,Stream) -> true
%VSC ; '$abort_loop'(Stream) %VSC ; '$abort_loop'(Stream)
@ -1381,10 +1381,14 @@ cd(Dir) :- working_directory(_, Dir).
getcwd(Dir) :- working_directory(Dir, Dir). getcwd(Dir) :- working_directory(Dir, Dir).
close(Stream) :-
swi_close(Stream).
close(Stream, Options) :-
swi_close(Stream, Options).
open(File, Type, Stream) :- open(File, Type, Stream) :-
swi_open(File, Type, Stream). swi_open(File, Type, Stream).
open(File, Type, Opts, Stream) :- open(File, Type, Stream, Opts) :-
swi_open(File, Type, Opts, Stream). swi_open(File, Type, Stream, Opts).
open_null_stream(S) :- open_null_stream(S) :-
swi_open_null_stream(S). swi_open_null_stream(S).
stream_property(Stream, Property) :- stream_property(Stream, Property) :-
@ -1396,4 +1400,5 @@ term_to_atom(Term, Atom) :-
swi_term_to_atom(Term, Atom). swi_term_to_atom(Term, Atom).
with_output_to(Output, G) :- with_output_to(Output, G) :-
swi_with_output_to(Output, G). swi_with_output_to(Output, G).

View File

@ -52,6 +52,7 @@ otherwise.
:- compile_expressions. :- compile_expressions.
:- [ :- [
% lists is often used. % lists is often used.
'lists.yap', 'lists.yap',

View File

@ -17,29 +17,6 @@
/* stream predicates */ /* stream predicates */
close(V) :- var(V), !,
'$do_error'(instantiation_error,close(V)).
close(File) :-
atom(File), !,
(
'$access_yap_flags'(8, 0),
current_stream(_,_,Stream),
'$user_file_name'(Stream,File)
->
'$close'(Stream)
;
'$close'(File)
).
close(Stream) :-
'$close'(Stream).
close(V,Opts) :- var(V), !,
'$do_error'(instantiation_error,close(V,Opts)).
close(S,Opts) :-
'$check_io_opts'(Opts,close(S,Opts)),
/* YAP ignores the force/1 flag */
close(S).
/* check whether a list of options is valid */ /* check whether a list of options is valid */
'$check_io_opts'(V,G) :- var(V), !, '$check_io_opts'(V,G) :- var(V), !,
'$do_error'(instantiation_error,G). '$do_error'(instantiation_error,G).
@ -52,11 +29,6 @@ close(S,Opts) :-
'$check_io_opts'(T,G) :- '$check_io_opts'(T,G) :-
'$do_error'(type_error(list,T),G). '$do_error'(type_error(list,T),G).
'$check_opt'(close(_,_),Opt,G) :- !,
(Opt = force(X) ->
'$check_force_opt_arg'(X,G) ;
'$do_error'(domain_error(close_option,Opt),G)
).
'$check_opt'(read_term(_,_),Opt,G) :- '$check_opt'(read_term(_,_),Opt,G) :-
'$check_opt_read'(Opt, G). '$check_opt_read'(Opt, G).
'$check_opt'(stream_property(_,_),Opt,G) :- '$check_opt'(stream_property(_,_),Opt,G) :-
@ -115,16 +87,6 @@ close(S,Opts) :-
'$check_opt_write'(A, G) :- '$check_opt_write'(A, G) :-
'$do_error'(domain_error(write_option,A),G). '$do_error'(domain_error(write_option,A),G).
%
% check force arg
%
'$check_force_opt_arg'(X,G) :- var(X), !,
'$do_error'(instantiation_error,G).
'$check_force_opt_arg'(true,_) :- !.
'$check_force_opt_arg'(false,_) :- !.
'$check_force_opt_arg'(X,G) :-
'$do_error'(domain_error(close_option,force(X)),G).
'$check_read_syntax_errors_arg'(X, G) :- var(X), !, '$check_read_syntax_errors_arg'(X, G) :- var(X), !,
'$do_error'(instantiation_error,G). '$do_error'(instantiation_error,G).
'$check_read_syntax_errors_arg'(dec10,_) :- !. '$check_read_syntax_errors_arg'(dec10,_) :- !.
@ -198,7 +160,7 @@ seeing(File) :- current_input(Stream),
'$user_file_name'(Stream,NFile), '$user_file_name'(Stream,NFile),
( '$user_file_name'(user_input,NFile) -> File = user ; NFile = File). ( '$user_file_name'(user_input,NFile) -> File = user ; NFile = File).
seen :- current_input(Stream), '$close'(Stream), set_input(user). seen :- current_input(Stream), close(Stream), set_input(user).
tell(user) :- !, set_output(user_output). tell(user) :- !, set_output(user_output).
tell(F) :- var(F), !, tell(F) :- var(F), !,
@ -215,7 +177,7 @@ telling(File) :- current_output(Stream),
'$user_file_name'(Stream,NFile), '$user_file_name'(Stream,NFile),
( '$user_file_name'(user_output,NFile) -> File = user ; File = NFile ). ( '$user_file_name'(user_output,NFile) -> File = user ; File = NFile ).
told :- current_output(Stream), '$close'(Stream), set_output(user). told :- current_output(Stream), close(Stream), set_output(user).
/* Term IO */ /* Term IO */