From 80f190bc8aa5390854593687963b2c62e27794b3 Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Sun, 13 Feb 2011 01:03:08 +0000 Subject: [PATCH] use SWI user_*. --- C/iopreds.c | 109 +------------------------------- H/iopreds.h | 14 ---- H/rheap.h | 2 - H/yapio.h | 28 -------- library/dialect/swi/fli/blobs.c | 2 +- packages/PLStream/pl-file.c | 5 ++ packages/PLStream/pl-yap.h | 2 +- pl/boot.yap | 13 ++-- pl/init.yap | 1 + pl/yio.yap | 42 +----------- 10 files changed, 21 insertions(+), 197 deletions(-) diff --git a/C/iopreds.c b/C/iopreds.c index 96a4cf026..c7e931281 100644 --- a/C/iopreds.c +++ b/C/iopreds.c @@ -352,96 +352,9 @@ PlGetsFunc(void) 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 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_output_stream = StdOutStream; Yap_c_error_stream = StdErrStream; @@ -4527,7 +4440,7 @@ static Int format2(UInt stream_flag) { int old_c_stream = Yap_c_output_stream; - int mem_stream = FALSE, codes_stream = FALSE; + int codes_stream = FALSE; Int out; Term tin = Deref(ARG1); @@ -4543,25 +4456,7 @@ format2(UInt stream_flag) return FALSE; } 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; } return out; diff --git a/H/iopreds.h b/H/iopreds.h index abe412eed..3e44b6eec 100644 --- a/H/iopreds.h +++ b/H/iopreds.h @@ -61,20 +61,6 @@ typedef struct stream_desc #endif YP_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; Int charcount, linecount, linepos; Int status; diff --git a/H/rheap.h b/H/rheap.h index 36703f510..7115b4583 100755 --- a/H/rheap.h +++ b/H/rheap.h @@ -932,8 +932,6 @@ RestoreStreams(void) for (sno = 0; sno < MaxStreams; ++sno) { if (Stream[sno].status & Free_Stream_f) 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.name = AtomAdjust(Stream[sno].u.file.name); } diff --git a/H/yapio.h b/H/yapio.h index 6483a0b34..6e77f4469 100644 --- a/H/yapio.h +++ b/H/yapio.h @@ -212,34 +212,6 @@ typedef struct VARSTRUCT { #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 */ typedef struct AliasDescS { Atom name; diff --git a/library/dialect/swi/fli/blobs.c b/library/dialect/swi/fli/blobs.c index eeb98b937..8b057bf5e 100644 --- a/library/dialect/swi/fli/blobs.c +++ b/library/dialect/swi/fli/blobs.c @@ -73,7 +73,7 @@ lookupBlob(void *blob, size_t len, PL_blob_t *type) b->NextOfPE = NIL; b->KindOfPE = BlobProperty; b->blob_t = type; - ae = (AtomEntry *)Yap_AllocCodeSpace(sizeof(AtomEntry)+len); + ae = (AtomEntry *)Yap_AllocCodeSpace(sizeof(AtomEntry)+len+sizeof(size_t)); if (!ae) return NULL; INIT_RWLOCK(ae->ARWLock); diff --git a/packages/PLStream/pl-file.c b/packages/PLStream/pl-file.c index f595ac667..e78679d8c 100755 --- a/packages/PLStream/pl-file.c +++ b/packages/PLStream/pl-file.c @@ -362,6 +362,11 @@ initIO() addHTable(streamAliases, (void *)*np, (void *)(intptr_t)i); GD->io_initialised = TRUE; +#if __YAP_PROLOG__ + Yap_LookupSWIStream(Suser_input); + Yap_LookupSWIStream(Suser_output); + Yap_LookupSWIStream(Suser_error); +#endif } diff --git a/packages/PLStream/pl-yap.h b/packages/PLStream/pl-yap.h index 709391210..ef949ee07 100644 --- a/packages/PLStream/pl-yap.h +++ b/packages/PLStream/pl-yap.h @@ -32,7 +32,7 @@ typedef YAP_Term (*Func)(term_t); /* foreign functions */ extern const char *Yap_GetCurrentPredName(void); extern YAP_Int Yap_GetCurrentPredArity(void); 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); diff --git a/pl/boot.yap b/pl/boot.yap index 832bebf5d..615ac082d 100755 --- a/pl/boot.yap +++ b/pl/boot.yap @@ -1107,7 +1107,7 @@ bootstrap(F) :- '$loop'(Stream,Status) :- -%VSC '$change_alias_to_stream'('$loop_stream',Stream), + '$change_alias_to_stream'('$loop_stream',Stream), repeat, %VSC ( '$current_stream'(_,_,Stream) -> true %VSC ; '$abort_loop'(Stream) @@ -1381,10 +1381,14 @@ cd(Dir) :- working_directory(_, Dir). getcwd(Dir) :- working_directory(Dir, Dir). +close(Stream) :- + swi_close(Stream). +close(Stream, Options) :- + swi_close(Stream, Options). open(File, Type, Stream) :- swi_open(File, Type, Stream). -open(File, Type, Opts, Stream) :- - swi_open(File, Type, Opts, Stream). +open(File, Type, Stream, Opts) :- + swi_open(File, Type, Stream, Opts). open_null_stream(S) :- swi_open_null_stream(S). stream_property(Stream, Property) :- @@ -1396,4 +1400,5 @@ term_to_atom(Term, Atom) :- swi_term_to_atom(Term, Atom). with_output_to(Output, G) :- - swi_with_output_to(Output, G). \ No newline at end of file + swi_with_output_to(Output, G). + diff --git a/pl/init.yap b/pl/init.yap index fdb32373f..5361d2c52 100644 --- a/pl/init.yap +++ b/pl/init.yap @@ -52,6 +52,7 @@ otherwise. :- compile_expressions. + :- [ % lists is often used. 'lists.yap', diff --git a/pl/yio.yap b/pl/yio.yap index 1455fbc09..cee17dd14 100644 --- a/pl/yio.yap +++ b/pl/yio.yap @@ -17,29 +17,6 @@ /* 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_io_opts'(V,G) :- var(V), !, '$do_error'(instantiation_error,G). @@ -52,11 +29,6 @@ close(S,Opts) :- '$check_io_opts'(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'(Opt, G). '$check_opt'(stream_property(_,_),Opt,G) :- @@ -115,16 +87,6 @@ close(S,Opts) :- '$check_opt_write'(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), !, '$do_error'(instantiation_error,G). '$check_read_syntax_errors_arg'(dec10,_) :- !. @@ -198,7 +160,7 @@ seeing(File) :- current_input(Stream), '$user_file_name'(Stream,NFile), ( '$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(F) :- var(F), !, @@ -215,7 +177,7 @@ telling(File) :- current_output(Stream), '$user_file_name'(Stream,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 */