Back to index

texmacs  1.0.7.15
Classes | Defines | Typedefs | Functions | Variables
tm_maple_5.cpp File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/wait.h>
#include <iostream>

Go to the source code of this file.

Classes

class  string

Defines

#define ERROR   (-1)
#define STDIN   0
#define STDOUT   1
#define STDERR   2
#define IN   0
#define OUT   1
#define DATA_BEGIN   ((char) 2)
#define DATA_END   ((char) 5)
#define DATA_ESCAPE   ((char) 27)

Typedefs

typedef char * charp

Functions

bool operator== (const string &s1, const char *s2)
bool ends (const string &s1, const char *s2)
void shorten (string &s, int n)
ostream & operator<< (ostream &out, const string &s)
string operator* (const string &s1, const string &s2)
stringoperator<< (string &s, const string &s2)
stringoperator<< (string &s, char c)
string get_line ()
void write (int channel, const string &s)
void next_input ()
void maple_output ()
void send (const string &s)
void strip (string &s, char c1, char c2)
void maple_input ()
void maple_interrupt (int sig)
volatile void invoke_maple ()
void init_maple ()
int main ()

Variables

charpenviron
int pid
int tochild [2]
int fromchild [2]
int in
int out
static int counter = 0

Define Documentation

#define DATA_BEGIN   ((char) 2)

Definition at line 36 of file tm_maple_5.cpp.

#define DATA_END   ((char) 5)

Definition at line 37 of file tm_maple_5.cpp.

#define DATA_ESCAPE   ((char) 27)

Definition at line 38 of file tm_maple_5.cpp.

#define ERROR   (-1)

Definition at line 29 of file tm_maple_5.cpp.

#define IN   0

Definition at line 33 of file tm_maple_5.cpp.

#define OUT   1

Definition at line 34 of file tm_maple_5.cpp.

#define STDERR   2

Definition at line 32 of file tm_maple_5.cpp.

#define STDIN   0

Definition at line 30 of file tm_maple_5.cpp.

#define STDOUT   1

Definition at line 31 of file tm_maple_5.cpp.


Typedef Documentation

typedef char* charp

Definition at line 26 of file tm_maple_5.cpp.


Function Documentation

bool ends ( const string s1,
const char *  s2 
) [inline]

Definition at line 83 of file tm_maple_5.cpp.

                                        {
  int n= strlen (s2);
  return s1.n >= n && strcmp (s1.s + s1.n - n, s2) == 0;
}

Here is the call graph for this function:

Definition at line 132 of file tm_maple_5.cpp.

            {
  string input;
  while (true) {
    char c;
    cin.get (c);
    if (c == '\n') break;
    input << c; 
  }
  return input;
}

Here is the caller graph for this function:

void init_maple ( )

Definition at line 293 of file tm_maple_5.cpp.

              {
  cout << DATA_BEGIN << "verbatim:";
  cout << "Maple session inside TeXmacs";
  send ("tmmaple:=5:\n");
  send ("interface(errorbreak=0,screenheight=9999):\n");
  char* tm_path= getenv ("TEXMACS_PATH");
  send ("read (`" * string (tm_path) *
       "/plugins/maple/maple/init-maple.mpl`):\n");
  next_input ();
  cout << DATA_END;
  cout.flush ();
}

Here is the call graph for this function:

Here is the caller graph for this function:

volatile void invoke_maple ( )

Definition at line 282 of file tm_maple_5.cpp.

                {
  charp argv[3];
  argv[0] = const_cast<char*> ("maple");
  argv[1] = const_cast<char*> ("-q");
  argv[2] = 0;
  char* maple_bin= getenv ("TEXMACS_MAPLE_BIN");
  execve (maple_bin, argv, environ);
  exit(127);
}

Here is the caller graph for this function:

int main ( )

Definition at line 307 of file tm_maple_5.cpp.

        {
  pipe (tochild);
  pipe (fromchild);
  pid= fork ();
  if (pid==0) { // the child
    setsid();
    dup2 (tochild [IN], STDIN);
    close (tochild [IN]);
    close (fromchild [IN]);
    close (tochild [OUT]);
    dup2 (fromchild [OUT], STDOUT);
    dup2 (STDOUT, STDERR);
    close (fromchild [OUT]);
    invoke_maple ();
    exit (127);
  }
  else { // the main process
    int sig;
    out= fromchild [IN];
    close (fromchild [OUT]);
    in= tochild [OUT];
    close (tochild [IN]);
    signal (SIGINT, maple_interrupt);
    siginterrupt (SIGINT, 1);
    init_maple ();
    while (true) {
      maple_input ();
      maple_output ();
    }
  }
  return 0;
}

Here is the call graph for this function:

void maple_input ( )

Definition at line 246 of file tm_maple_5.cpp.

               {
  string input= get_line ();
  send ("printf(`tmstart\\n`):\n");
  strip (input, ' ', '\n');
  if (input.n == 0 || input[0] == '?')
    send (input * "\n");
  else if (input[input.n-1] == ':') {
    strip (input, ':', ';');
    send (input * ":\n");
  }
  else {
    strip (input, ':', ';');
    send ("tmresult := tmdummy:\n");
    send (input * ":\n");
    send ("if \" <> tmdummy then tmprint(\") fi:\n");
  }
  send ("printf(`tmend\\n`):\n");
}

Here is the call graph for this function:

Here is the caller graph for this function:

void maple_interrupt ( int  sig)

Definition at line 270 of file tm_maple_5.cpp.

                          {
  killpg (pid, sig);
  cout << DATA_END; // << DATA_END << DATA_END;
  signal (sig, maple_interrupt);
  siginterrupt (sig, 1);
}

Here is the caller graph for this function:

void maple_output ( )

Definition at line 164 of file tm_maple_5.cpp.

                {
  bool show_flag= false;
  string output;
  cout << DATA_BEGIN << "verbatim:";

  while (true) {
    fd_set rfds;
    FD_ZERO (&rfds);
    FD_SET (out, &rfds);
    int max_fd= out+1;
    struct timeval tv;
    tv.tv_sec  = 0;
    tv.tv_usec = 100;
    int nr= select (max_fd, &rfds, NULL, NULL, &tv);
    if (nr==0) {
      cout.flush ();
      continue;
    }

    if (FD_ISSET (out, &rfds)) {
      int i, r;
      char outbuf[1024];
      bool error_flag= false;
      r = read (out, outbuf, 1024);
      if (r == ERROR) {
       next_input ();
       cout << DATA_END << DATA_END;
       cout.flush ();
       return;
      }
      else if (r == 0) {
       killpg (pid, SIGKILL);
       cout << DATA_END;
       cout.flush ();
       exit (0);
      }
      else for (i=0; i<r; i++) {
       output << outbuf[i];
       if (ends (output, "error"))
         error_flag= true;
       if (outbuf[i]=='\n') {
         if (output == "tmstart\n")
           show_flag= true;
         else if (output == "tmend\n") {
           next_input ();
           cout << DATA_END;
           cout.flush ();
           return;
         }
         else if (show_flag) {
           cout << output;
           cout.flush ();
         }
         output= string ();
       }
      }
      if (error_flag) {
       next_input ();
       cout << DATA_END;
       cout.flush ();
       return;
      }
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void next_input ( )

Definition at line 155 of file tm_maple_5.cpp.

              {
  counter++;
  cout << DATA_BEGIN << "channel:prompt" << DATA_END;
  cout << DATA_BEGIN << "scheme:(with \"color\" \"brown\" \"";
  cout << "Maple " << counter << "] ";
  cout << "\")" << DATA_END;
}
string operator* ( const string s1,
const string s2 
)

Definition at line 102 of file tm_maple_5.cpp.

                                                {
  string s (s1.n + s2.n);
  strcpy (s.s, s1.s);
  strcpy (s.s + s1.n, s2.s);
  s.n= s1.n + s2.n;
  return s;
}
ostream& operator<< ( ostream &  out,
const string s 
) [inline]

Definition at line 97 of file tm_maple_5.cpp.

                                            {
  return out << s.s;
}
string& operator<< ( string s,
const string s2 
)

Definition at line 111 of file tm_maple_5.cpp.

                                          {
  if (s.n + s2.n >= s.l) {
    char* old= s.s;
    s.l= ((s.n + s2.n) << 1) + 1;
    s.s= new char[s.l];
    strcpy (s.s, old);
    delete[] old;
  }
  strcpy (s.s + s.n, s2.s);
  s.n += s2.n;
  return s;
}
string& operator<< ( string s,
char  c 
)

Definition at line 125 of file tm_maple_5.cpp.

                                {
  char s2[2];
  s2[0]= c; s2[1]= '\0';
  return s << string (s2);
}
bool operator== ( const string s1,
const char *  s2 
) [inline]

Definition at line 78 of file tm_maple_5.cpp.

                                               {
  return strcmp (s1.s, s2) == 0;
}

Here is the call graph for this function:

void send ( const string s)

Definition at line 235 of file tm_maple_5.cpp.

                       {
  write (in, s);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void shorten ( string s,
int  n 
) [inline]

Definition at line 89 of file tm_maple_5.cpp.

                           {
  if (s.n >= n) {
    s.n -= n;
    s.s[s.n]= '\0';
  }
}

Here is the caller graph for this function:

void strip ( string s,
char  c1,
char  c2 
)

Definition at line 240 of file tm_maple_5.cpp.

                                    {
  while (s.n>0 && (s[s.n-1] == c1 || s[s.n-1] == c2))
    shorten (s, 1);
}

Here is the call graph for this function:

Here is the caller graph for this function:

void write ( int  channel,
const string s 
)

Definition at line 144 of file tm_maple_5.cpp.

                                     {
  write (channel, s.s, s.n);
}

Here is the caller graph for this function:


Variable Documentation

int counter = 0 [static]

Definition at line 152 of file tm_maple_5.cpp.

Definition at line 34 of file tm_shell.cpp.

int fromchild[2]

Definition at line 45 of file tm_maple_5.cpp.

int in

Definition at line 46 of file tm_maple_5.cpp.

int out

Definition at line 47 of file tm_maple_5.cpp.

int pid

Definition at line 43 of file tm_maple_5.cpp.

int tochild[2]

Definition at line 44 of file tm_maple_5.cpp.