GIML: How to execute other programs
I have have never seen this work on a non-unix system.
The execute command
This command takes the name of the command (give it the full path) and a list
of parameters. The function returns streams for input and output.
val execute = fn : string * string list -> instream * outstream
To find out the full path name of a command such as ls try
which ls at the unix prompt - on my system this gives
/usr/bin/ls
Example: who
This executes the unix who command - we see the first two
users:
val (a,_) = execute("/usr/bin/who",[]);
input_line a;
input_line a;
close_in a;
Example: A Sharespearean insult from America
(* A function to return all the data from a stream into a line list *)
fun streamToList f_in = if end_of_stream f_in then (close_in f_in;nil)
else input_line f_in :: streamToList f_in;
(* See lesson seven for details of filter *)
fun filter f nil = nil
| filter f (h::t) = if f h then h::filter f t else filter f t;
(* prefix returns true iff "small" is a prefix of "big" *)
fun prefix small big = (size big > size small) andalso
substring(big,0,size small)=small;
(* execute the telnet command to connect to the Web server at nova *)
val (x,y) = execute("/usr/bin/telnet",
["nsu.acast.nova.edu","80"]);
(* send the GET command to the Web server - i.e. pretend to be a browser *)
output(y,"GET /Inter-Links/cgi-bin/bard.pl HTTP/1.0\n\n");
(* flush the output as it sits in the buffer otherwise *)
flush_out y;
(* a page of html comes back - to stream x
the line we want is prefixed by *)
val insult = filter (prefix "")(streamToList x);
(* The connection get broken anyway - this closes the process *)
close_out y;