clib package
This commit is contained in:
29
packages/clib/demo/cgidemo.pl
Normal file
29
packages/clib/demo/cgidemo.pl
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/pl -q -g main -s
|
||||
/* $Id$
|
||||
|
||||
Part of SWI-Prolog
|
||||
|
||||
This example code is in the public domain
|
||||
*/
|
||||
|
||||
:- use_module(library(cgi)).
|
||||
|
||||
main :-
|
||||
cgi_get_form(Arguments),
|
||||
format('Content-type: text/html~n~n', []),
|
||||
format('<html>~n', []),
|
||||
format('<head>~n', []),
|
||||
format('<title>Simple SWI-Prolog CGI script output</title>~n', []),
|
||||
format('</head>~n~n', []),
|
||||
format('<body>~n', []),
|
||||
format('<h1>Form arguments</h1>'),
|
||||
format('<p>', []),
|
||||
print_args(Arguments),
|
||||
format('<body>~n</html>~n', []),
|
||||
halt.
|
||||
|
||||
print_args([]).
|
||||
print_args([A0|T]) :-
|
||||
A0 =.. [Name, Value],
|
||||
format('<b>~w</b>=<em>~w</em><br>~n', [Name, Value]),
|
||||
print_args(T).
|
33
packages/clib/demo/fork.pl
Normal file
33
packages/clib/demo/fork.pl
Normal file
@@ -0,0 +1,33 @@
|
||||
/* $Id$
|
||||
|
||||
Part of SWI-Prolog
|
||||
|
||||
Author: Jan Wielemaker
|
||||
E-mail: jan@swi.psy.uva.nl
|
||||
WWW: http://www.swi.psy.uva.nl/projects/SWI-Prolog/
|
||||
Copying: GPL-2. See the file COPYING or http://www.gnu.org
|
||||
|
||||
Copyright (C) 1990-2001 SWI, University of Amsterdam. All rights reserved.
|
||||
*/
|
||||
|
||||
:- use_module(library(unix)).
|
||||
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
|
||||
Simple demo illustrating the combination of fork/1 and pipe/2 to make
|
||||
Prolog fork a child to do some work and get back when done. Using
|
||||
wait_for_input/3 you can make the main Prolog task wait for multiple
|
||||
childs to return results.
|
||||
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
fork_demo(Result) :-
|
||||
pipe(Read, Write),
|
||||
fork(Pid),
|
||||
( Pid == child
|
||||
-> close(Read),
|
||||
format(Write, '~q.~n', [hello(world)]),
|
||||
flush_output(Write), % stream is fully buffered!
|
||||
halt
|
||||
; close(Write),
|
||||
read(Read, Result),
|
||||
close(Read)
|
||||
).
|
60
packages/clib/demo/pipe.pl
Normal file
60
packages/clib/demo/pipe.pl
Normal file
@@ -0,0 +1,60 @@
|
||||
/* $Id$
|
||||
|
||||
Part of SWI-Prolog
|
||||
|
||||
Author: Jan Wielemaker
|
||||
E-mail: jan@swi.psy.uva.nl
|
||||
WWW: http://www.swi.psy.uva.nl/projects/SWI-Prolog/
|
||||
Copying: GPL-2. See the file COPYING or http://www.gnu.org
|
||||
|
||||
Copyright (C) 1990-2000 SWI, University of Amsterdam. All rights reserved.
|
||||
*/
|
||||
|
||||
:- module(subcommand,
|
||||
[ pipe_through_command/3 % +Command, +In, -Out
|
||||
]).
|
||||
:- use_module(library(unix)).
|
||||
|
||||
% pipe_through_command(+Command, +In, -Out)
|
||||
%
|
||||
% Pipe text In through command Command and put the output of command
|
||||
% in Out.
|
||||
%
|
||||
% Example:
|
||||
%
|
||||
% ?- pipe_through_command(tr('a-z', 'A-Z'), hello, X)
|
||||
%
|
||||
% X = 'HELLO'
|
||||
|
||||
pipe_through_command(Command, In, Out) :-
|
||||
pipe(ChildIn, MeOut),
|
||||
pipe(MeIn, ChildOut),
|
||||
fork(Pid),
|
||||
( Pid == child,
|
||||
close(MeOut),
|
||||
close(MeIn),
|
||||
dup(ChildIn, 0),
|
||||
dup(ChildOut, 1),
|
||||
close(ChildIn),
|
||||
close(ChildOut),
|
||||
exec(Command)
|
||||
; close(ChildIn),
|
||||
close(ChildOut),
|
||||
write(MeOut, In),
|
||||
close(MeOut),
|
||||
read_input(MeIn, Out),
|
||||
close(MeIn),
|
||||
wait(Pid, _Status) % avoid zombi
|
||||
).
|
||||
|
||||
|
||||
read_input(In, Atom) :-
|
||||
get_code(In, C0),
|
||||
read_input(C0, In, Codes),
|
||||
atom_codes(Atom, Codes).
|
||||
|
||||
read_input(-1, _, []) :- !.
|
||||
read_input(C, In, [C|T]) :-
|
||||
get_code(In, C1),
|
||||
read_input(C1, In, T).
|
||||
|
117
packages/clib/demo/win.pl
Normal file
117
packages/clib/demo/win.pl
Normal file
@@ -0,0 +1,117 @@
|
||||
/* $Id$
|
||||
|
||||
Part of SWI-Prolog
|
||||
|
||||
This example code is in the public domain
|
||||
*/
|
||||
|
||||
:- asserta(library_directory('.')).
|
||||
:- asserta(user:file_search_path(foreign, '.')).
|
||||
:- use_module(library(socket)).
|
||||
|
||||
/*******************************
|
||||
* SOCKET (CLIENT) *
|
||||
*******************************/
|
||||
|
||||
get_http_data(Host, URL) :-
|
||||
tcp_socket(Socket),
|
||||
tcp_connect(Socket, Host:80),
|
||||
tcp_open_socket(Socket, Read, Write),
|
||||
format(Write, 'GET ~w~n~n', URL),
|
||||
flush_output(Write),
|
||||
copy_stream(Read, user_output),
|
||||
close(Read),
|
||||
close(Write).
|
||||
|
||||
copy_stream(In, Out) :-
|
||||
get0(In, C0),
|
||||
copy_stream(C0, In, Out).
|
||||
|
||||
copy_stream(-1, _, _) :- !.
|
||||
copy_stream(C, In, Out) :-
|
||||
put(Out, C),
|
||||
get0(In, C2),
|
||||
copy_stream(C2, In, Out).
|
||||
|
||||
|
||||
/*******************************
|
||||
* SOCKET (SERVER) *
|
||||
*******************************/
|
||||
|
||||
:- dynamic
|
||||
server/1,
|
||||
client/2.
|
||||
|
||||
mkserver :-
|
||||
mkserver(3000).
|
||||
mkserver(Port) :-
|
||||
tcp_socket(Socket),
|
||||
tcp_bind(Socket, Port),
|
||||
tcp_listen(Socket, 5),
|
||||
tcp_open_socket(Socket, Read, _),
|
||||
asserta(server(Read)).
|
||||
|
||||
dispatch :-
|
||||
repeat,
|
||||
\+ dispatch1, !.
|
||||
|
||||
dispatch1 :-
|
||||
findall(C, client(C, _), Clients),
|
||||
server(Server),
|
||||
wait_for_input([Server|Clients], Ready, 0),
|
||||
dispatch(Ready).
|
||||
|
||||
dispatch([]).
|
||||
dispatch([H|T]) :-
|
||||
format('Dispatching ~w ... ', [H]), flush,
|
||||
dispatch_fd(H),
|
||||
format('ok~n'),
|
||||
dispatch(T).
|
||||
|
||||
dispatch_fd(Server) :-
|
||||
server(Server), !,
|
||||
tcp_accept(Server, ClientSocket, Peer),
|
||||
format('Connected from ~w~n', [Peer]),
|
||||
tcp_fcntl(ClientSocket, setfl, nonblock),
|
||||
tcp_open_socket(ClientSocket, Read, Write),
|
||||
format(Write, 'Please to meet you!~n', []),
|
||||
flush_output(Write),
|
||||
assert(client(Read, Write)).
|
||||
dispatch_fd(Client) :-
|
||||
client(Client, Write),
|
||||
format(Write, 'You typed: ', []),
|
||||
copy_stream(Client, Write),
|
||||
( at_end_of_stream(Client)
|
||||
-> format('Closing client ~w~n', [Client]),
|
||||
close(Client),
|
||||
close(Write),
|
||||
retractall(client(Client, _))
|
||||
; flush_output(Write)
|
||||
).
|
||||
|
||||
/*******************************
|
||||
* CLIENT *
|
||||
*******************************/
|
||||
|
||||
client :-
|
||||
client(3000).
|
||||
client(Port) :-
|
||||
tcp_socket(Socket),
|
||||
tcp_connect(Socket, localhost:Port),
|
||||
tcp_open_socket(Socket, In, Out),
|
||||
format(Out, 'Hello World~n', []),
|
||||
flush_output(Out),
|
||||
copy_line(In, user_output),
|
||||
close(In),
|
||||
close(Out).
|
||||
|
||||
copy_line(In, Out) :-
|
||||
get0(In, C0),
|
||||
copy_line(C0, In, Out).
|
||||
|
||||
copy_line(10, _, _) :- !.
|
||||
copy_line(C, In, Out) :-
|
||||
put(Out, C),
|
||||
get0(In, C2),
|
||||
copy_line(C2, In, Out).
|
||||
|
Reference in New Issue
Block a user