Friday, December 24, 2010

Getting to the root of the gnuplot troubles

As I mentioned in my previous post, the gnuplot in Ubuntu does not work with Lush and that you have to build it yourself. The make-or-break difference between Ubuntu's and your own built is that Ubuntu's gnuplot links libeditline whereas your own built should link the GNU readline library. That's how far I got last time I looked at it, but I did not dig deeper to understand what gnuplot with libeditline does differently from gnuplot with libreadline.

The reason that the specific command-line library could have anything to do with the problem I am seeing with Ubuntu's gnuplot is that, unlike any other gnuplot interface I have seen, Lush's interface is bidirectional. The nice thing about it is that you can create a Gnuplot instance and use it to interact with gnuplot from within the Lush shell.

? (libload "gnuplot/gnuplot")
[gnuplot.lsh]
[gnuplot-config.lsh]
= "/home/juenglin/downloads/lush2-ref/packages/gnuplot/gnuplot.lsh"
? (defvar gp (new Gnuplot))
= gp
? (gp "show version")

G N U P L O T
Version 4.4 patchlevel 2
last modified Wed Sep 22 12:10:34 PDT 2010
System: Linux 2.6.32-26-generic

Copyright (C) 1986-1993, 1998, 2004, 2007-2010
Thomas Williams, Colin Kelley and many others

gnuplot home: http://www.gnuplot.info
faq, bugs, etc: type "help seeking-assistance"
immediate help: type "help"
plot window: hit 'h'

= ()
? (gp "show terminal")

terminal type is wxt 0

= ()

A bidirectional interface is more complicated to implement than a unidirectional interface. The nuts and bolts of a unidirectional interface consist of creating a pipe, spawning a gnuplot process and making the read end of the pipe the processe's standard input. Use the write end of the pipe to send commands and data to gnuplot and you are basically done.

What makes a bidirectional interface more complicated is not so much that one needs to use two pipes, but the difficulty of staying in sync with gnuplot without getting into a deadlock. The approach of the current implementation of Lush's bidirectional interface is to launch gnuplot in interactive mode and to use the gnuplot prompt as a cue for when to stop reading from the receiving pipe. The protocol basically mimics the interaction a user has within the gnuplot shell: enter a command, type return, see gnuplot's answer and a new gnuplot prompt.

Now, what's wrong with libeditline? It turns out that when running gnuplot with libeditline in this mode, gnuplot never sends a prompt. I haven't quite figured out why that is the case. But as an experiment I connected to gnuplot via a pseudo terminal and it turns out that gnuplot with libeditline then sends the prompt and the interface works. Unfortunately, using pseudo terminals for the interface is not a solution as the communication seems to go a lot slower than with pipes.

No comments: