Back to index

texmacs  1.0.7.15
Public Member Functions | Public Attributes | Friends
pipe_link_rep Struct Reference
Inheritance diagram for pipe_link_rep:
Inheritance graph
[legend]
Collaboration diagram for pipe_link_rep:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 pipe_link_rep (string cmd)
 ~pipe_link_rep ()
string start ()
void write (string s, int channel)
stringwatch (int channel)
string read (int channel)
void listen (int msecs)
void interrupt ()
void stop ()
void feed (int channel)
void write_packet (string s, int channel)
bool complete_packet (int channel)
string read_packet (int channel, int timeout, bool &success)
void secure_server (string cmd)
void secure_client ()
void set_command (command _cmd)
void apply_command ()

Public Attributes

string cmd
int pid
int pp_in [2]
int pp_out [2]
int pp_err [2]
int in
int out
int err
string outbuf
string errbuf
socket_notifier snout
socket_notifier snerr
bool alive
string secret
command feed_cmd
int ref_count

Friends

class tm_link

Detailed Description

Definition at line 52 of file pipe_link.cpp.


Constructor & Destructor Documentation

Definition at line 82 of file pipe_link.cpp.

                                        : cmd (cmd2) {
  pipe_link_set->insert ((pointer) this);
  in     = pp_in [0]= pp_in [1]= -1;
  out    = pp_out[0]= pp_out[1]= -1;
  err    = pp_err[0]= pp_err[1]= -1;
  outbuf = "";
  errbuf = "";
  alive  = false;
}

Definition at line 92 of file pipe_link.cpp.

                               {
  stop ();
  pipe_link_set->remove ((pointer) this);
}

Here is the call graph for this function:


Member Function Documentation

void tm_link_rep::apply_command ( ) [inline, inherited]

Definition at line 64 of file tm_link.hpp.

{ if (!is_nil (feed_cmd)) feed_cmd->apply (); }

Here is the call graph for this function:

Here is the caller graph for this function:

bool tm_link_rep::complete_packet ( int  channel) [inherited]

Definition at line 52 of file tm_link.cpp.

                                         {
  return message_complete (watch (channel));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pipe_link_rep::feed ( int  channel)

Definition at line 238 of file pipe_link.cpp.

                                {
#ifndef __MINGW32__
  if ((!alive) || ((channel != LINK_OUT) && (channel != LINK_ERR))) return;
  int r;
  char tempout[1024];
  if (channel == LINK_OUT) r = ::read (out, tempout, 1024);
  else r = ::read (err, tempout, 1024);
  if (r == -1) {
    cerr << "TeXmacs] read failed for#'" << cmd << "'\n";
    wait (NULL);
  }
  else if (r == 0) {
    if (-1 != killpg(pid,SIGTERM)) {
      sleep(2);
      killpg(pid,SIGKILL);
    }

    alive= false;
    remove_notifier (snout);      
    remove_notifier (snerr);      
  }
  else {
    if (DEBUG_IO) cout << debug_io_string (string (tempout, r));
    if (channel == LINK_OUT) outbuf << string (tempout, r);
    else errbuf << string (tempout, r);
  }
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

void pipe_link_rep::interrupt ( ) [virtual]

Implements tm_link_rep.

Definition at line 310 of file pipe_link.cpp.

                          {
#ifndef __MINGW32__
  if (!alive) return;
  killpg (pid, SIGINT);
#endif
}
void pipe_link_rep::listen ( int  msecs) [virtual]

Implements tm_link_rep.

Definition at line 291 of file pipe_link.cpp.

                                {
  if (!alive) return;
  time_t wait_until= texmacs_time () + msecs;
  while ((outbuf == "") && (errbuf == "")) {
    fd_set rfds;
    FD_ZERO (&rfds);
    FD_SET (out, &rfds);
    FD_SET (err, &rfds);
    struct timeval tv;
    tv.tv_sec  = msecs / 1000;
    tv.tv_usec = 1000 * (msecs % 1000);
    int nr= select (max (out, err) + 1, &rfds, NULL, NULL, &tv);
    if (nr != 0 && FD_ISSET (out, &rfds)) feed (LINK_OUT);
    if (nr != 0 && FD_ISSET (err, &rfds)) feed (LINK_ERR);
    if (texmacs_time () - wait_until > 0) break;
  }
}

Here is the call graph for this function:

string pipe_link_rep::read ( int  channel) [virtual]

Implements tm_link_rep.

Definition at line 276 of file pipe_link.cpp.

                                {
  if (channel == LINK_OUT) {
    string r= outbuf;
    outbuf= "";
    return r;
  }
  else if (channel == LINK_ERR) {
    string r= errbuf;
    errbuf= "";
    return r;
  }
  else return string("");
}

Here is the caller graph for this function:

string tm_link_rep::read_packet ( int  channel,
int  timeout,
bool &  success 
) [inherited]

Definition at line 57 of file tm_link.cpp.

                                                                 {
  success= false;
  string& r= watch (channel);
  time_t start= texmacs_time ();
  while (!message_complete (r)) {
    int n= N(r);
    if (timeout > 0) listen (timeout);
    if (N(r) == n && (texmacs_time () - start >= timeout)) return "";
  }
  if (channel == LINK_OUT && N(r) > 0 && r[0] == '!') {
    secure_server (message_receive (r));
    return "";
  }
  else {
    string back= message_receive (r);
    if (secret != "") back= secret_decode (back, secret);
    success= true;
    return back;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void tm_link_rep::secure_client ( ) [inherited]

Definition at line 92 of file tm_link.cpp.

                            {
  if (secret != "") return;
  write ("!", LINK_IN);
  write_packet (rsa_my_public_key (), LINK_IN);
  bool success;
  string r= read_packet (LINK_OUT, 10000, success);
  if (!success) { stop (); return; }
  secret= rsa_decode (r, rsa_my_private_key ());
}

Here is the call graph for this function:

Here is the caller graph for this function:

void tm_link_rep::secure_server ( string  cmd) [inherited]

Definition at line 83 of file tm_link.cpp.

                                                {
  if (secret != "") return;
  string k= secret_generate ();
  string s= rsa_encode (k, client_public);
  write_packet (s, LINK_IN);
  secret= k;
}

Here is the call graph for this function:

Here is the caller graph for this function:

void tm_link_rep::set_command ( command  _cmd) [inline, inherited]

Definition at line 63 of file tm_link.hpp.

{ feed_cmd = _cmd; }

Implements tm_link_rep.

Definition at line 147 of file pipe_link.cpp.

                      {
#ifndef __MINGW32__
  if (alive) return "busy";
  if (DEBUG_AUTO) cout << "TeXmacs] Launching '" << cmd << "'\n";

  int e1= pipe (pp_in ); (void) e1;
  int e2= pipe (pp_out); (void) e2;
  int e3= pipe (pp_err); (void) e3;
  pid= fork ();
  if (pid==0) { // the child
    setsid();
    close (pp_in  [OUT]);
    close (pp_out [IN ]);
    close (pp_err [IN ]);
    dup2  (pp_in  [IN ], STDIN );
    close (pp_in  [IN ]);
    dup2  (pp_out [OUT], STDOUT);
    close (pp_out [OUT]);
    dup2  (pp_err [OUT], STDERR);
    close (pp_err [OUT]);

    execute_shell (cmd);
    exit (127);
    // exit (system (cmd) != 0);
  }
  else { // the main process
    in = pp_in  [OUT];
    close (pp_in [IN]);
    out= pp_out [IN ];
    close (pp_out [OUT]);
    err= pp_err [IN ];
    close (pp_err [OUT]);

    alive= true;
    snout = socket_notifier (out, &pipe_callback, this, NULL);
    snerr = socket_notifier (err, &pipe_callback, this, NULL);
    add_notifier (snout);
    add_notifier (snerr);
    
    if (/* !banner */ true) return "ok";
    else {
      int r;
      char outbuf[1024];
      r= ::read (out, outbuf, 1024);
      if (r == 1 && outbuf[0] == TERMCHAR) return "ok";
      alive= false;
      if (-1 != killpg(pid,SIGTERM)) {
       sleep(2);
       killpg(pid,SIGKILL);
      }
      wait (NULL);
      if (r == -1) return "Error: the application does not reply";
      else
       return "Error: the application did not send its usual startup banner";
    }
  }
#else
  return "Error: pipes not implemented";
#endif
}

Here is the call graph for this function:

void pipe_link_rep::stop ( ) [virtual]

Implements tm_link_rep.

Definition at line 318 of file pipe_link.cpp.

                     {
#ifndef __MINGW32__
  if (!alive) return;
  if (-1 != killpg(pid,SIGTERM)) {
    sleep(2);
    killpg(pid,SIGKILL);
  }
  alive= false;    
  close (in);
  alive= false;
  wait (NULL);

  remove_notifier (snout);
  remove_notifier (snerr);
#endif
}

Here is the call graph for this function:

Here is the caller graph for this function:

string & pipe_link_rep::watch ( int  channel) [virtual]

Implements tm_link_rep.

Definition at line 268 of file pipe_link.cpp.

                                 {
  static string empty_string= "";
  if (channel == LINK_OUT) return outbuf;
  else if (channel == LINK_ERR) return errbuf;
  else return empty_string;
}
void pipe_link_rep::write ( string  s,
int  channel 
) [virtual]

Implements tm_link_rep.

Definition at line 226 of file pipe_link.cpp.

                                           {
#ifndef __MINGW32__
  if ((!alive) || (channel != LINK_IN)) return;
  if (DEBUG_IO) cout << "[INPUT]" << debug_io_string (s);
  char* _s= as_charp (s);
  int err= ::write (in, _s, N(s));
  (void) err;
  tm_delete_array (_s);
#endif
}

Here is the call graph for this function:

void tm_link_rep::write_packet ( string  s,
int  channel 
) [inherited]

Definition at line 46 of file tm_link.cpp.

                                                {
  if (secret != "") s= secret_encode (s, secret);
  write ((as_string (N (s)) * "\n") * s, channel);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Friends And Related Function Documentation

friend class tm_link [friend, inherited]

Definition at line 66 of file tm_link.hpp.


Member Data Documentation

bool tm_link_rep::alive [inherited]

Definition at line 40 of file tm_link.hpp.

Definition at line 53 of file pipe_link.cpp.

Definition at line 60 of file pipe_link.cpp.

Definition at line 63 of file pipe_link.cpp.

Definition at line 43 of file tm_link.hpp.

Definition at line 58 of file pipe_link.cpp.

Definition at line 59 of file pipe_link.cpp.

Definition at line 62 of file pipe_link.cpp.

Definition at line 54 of file pipe_link.cpp.

Definition at line 57 of file pipe_link.cpp.

Definition at line 55 of file pipe_link.cpp.

Definition at line 56 of file pipe_link.cpp.

int abstract_struct::ref_count [inherited]

Definition at line 142 of file basic.hpp.

Definition at line 41 of file tm_link.hpp.

Definition at line 65 of file pipe_link.cpp.

Definition at line 65 of file pipe_link.cpp.


The documentation for this struct was generated from the following file: