Showing posts with label linux. Show all posts
Showing posts with label linux. Show all posts

What is Erlang open_port and os:cmd?

Sometimes it happens that you need to execute external command. Same happens when you are programming Erlang. Usually it is enough to use os:cmd. It executes the command and returns the result. Like the following

LsOut = os:cmd("ls"), % on unix platform

But sometimes you need to start the process and communicate with it via standard input and output. For example most of linux users know GnuChess program. And may be somebody even tried to start it from the shell without GUI. So, the following simple example will show how to start GnuChess and communicate with the process from Erlang.

-module(pipe).
-author('Sergiy Dzysyak ').

-compile(export_all).

start()->
	spawn(?MODULE, read, []).
	
read() ->
  Port = open_port({spawn,"/usr/bin/gnuchess -xe"},[binary,{line, 255}]),
  do_read(Port).

do_read(Port) ->
  receive
    {Port,{data,Data}} ->
    	io:format("Data: ~p~n",[Data]);
    {Port,eof} ->
      read();
    {go, Go} ->
    	Port ! {self(), {command, Go}};
    Any ->
      io:format("No match fifo_client:do_read/1, ~p~n",[Any])
  end,
  do_read(Port). 

Example is very basic and to stat it you should do the following:

[serg@localhost erl.chess]$ erl
Erlang R14B03 (erts-5.8.4) [source] [64-bit] [rq:1] [async-threads:0] [kernel-poll:false]

Eshell V5.8.4  (abort with ^G)
1> c(pipe).
{ok,pipe}
2> Pid = pipe:start().
<0.38.0>
3> Pid ! {go, <<"e2e4\n">>}.
{go,<<"e2e4\n">>}
Data: {eol,<<"Chess">>}
Data: {eol,<<"Adjusting HashSize to 1024 slots">>}
Data: {eol,<<"1. e2e4">>}
Data: {eol,<<"1. ... e7e5">>}
Data: {eol,<<"My move is: e7e5">>}
4> 

In a similar way communication via linux named pipes can be organized with external programs or even more complicated goals may be achieved. I have not tried to load drivers & communicate with them via port_open, but if I will get a minute, I will try and describe it here in my blog...

Gedit plugin: New from template

I am not sure how many people finding Gedit good enough as an editor for programming purposes, but I am using it exactly that way and quite happy with its functionality. Especially I like its integration with gnome virtual file system, that allows it to work directly on remote hosts, and ability to use and create new plugins for it.
With the release of new version of gnome environment & new version of Gedit text editor I met some problems with the compatibility of old plugins. So, I have to redo some plugins I have used before. First of all I decided to implement plugin that allows creation of new documents from templates.
Plugin is quite simple, but I have not much python programming experience and I should notice that there seems not much documentation available about new Gedit, Gtk Python API.... So, if someone has more experience in Python or have some good ideas regarding the plugin functionality you are welcome to fork Git repo or post a comment here.
Plugin on GitHub: https://github.com/dzysyak/Templates-plugin-for-GEdit3

SSL how to: Get a server certificate from StartSSL.com

In this and a few later articles I am going to decribe how to install TLS/SSL certificate on lighttpd, vsftpd, Postfix, Dovecot, eJabberd and may be Apache. I also wanted to do this using the free SSL provider, StartSSL.
On my new blog you can also find articles on how to use SSL certificate from Erlang profram and probably in some other ways (just preparing nor articles for now).


Article have been moved to my new blog location:

Read complete article: SSL how to: Get a server certificate from StartSSL.com