This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Ports


| Hi everybody, I'm new to this list and I've some
| problem to interface Guile with my C++ program. 
| All I want to do is redirect all output (like
| errors) to a graphical console (a la Gimp). I think
| to best thing to do is to redirect to standard and
| error output to a pipe and read from it. 

I think that could still be done, see the program below.  However I
think there's a problem with using a pipe within a single
process/thread: if Guile was to write more than PIPE_BUF characters it
would block.

| I'm using the 1.3.4 version of Guile and I'm wondering 
| why the scm_standard_stream_to_port() function 
| disapeared in from the 1.3 version?? It might be very 
| useful! (it is now replaced with a file descriptor 
| convertion function). 

It  disappeared because  Guile's port  system  is no  longer based  on
stdio.

However there are going to be situations where connection to stdio is
necessary.  At the moment it isn't handled conveniently.  I think a
reasonable solution may be to add a FILE * to the internal fport
structure, but only initialise it (using fdopen on the port's
descriptor and making the stream unbuffered) if the FILE * was
requested via some procedure.

| I've tried to make a new output port for Guile (for 
| function scm_set_current_output_port() and 
| scm_set_current_error_port()) but it doesn't seems
| to work. Maybe I'm badly using pipes?

Guile's output/error ports could be set to a new port constructed by
calling scm_fdopen on the writing end of your pipe.  

| 
| In other words is there is another method to do such
| a thing?

The best solution may be to create a string port and set it as Guile's
output/error ports (I think that's what SCWM does).

Here's the program mentioned above:

#include <unistd.h>

void error (const char *msg)
{
  write (2, msg, strlen (msg));
  write (2, "\n", 1);
  exit (1);
}

int main ()
{
  int fd[2];

  if (pipe (fd) == -1)
    error ("pipe");

  /* do something similar for file descriptor 2.  */
  if (dup2 (fd[1], 1) == -1)
    error ("dup2");
  
  if (close (fd[1]) == -1)
    error ("close");

  /* pretend this is Guile's output port writing to file descriptor 1.  */
  if (write (1, "test\n", 5) != 5)
    error ("write");

  {
    char buf[5];

    /* take the data from the pipe.  */    
    if (read (fd[0], buf, 5) != 5)
      error ("read");

    if (write (2, buf, 5) != 5)
      error ("write 2");
  }

  return 0;
}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]