Back to index

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

Go to the source code of this file.

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

static void append (charp &s, char c, int &pos, int &max)
static void shell_interrupt (int sig)
static void shell_output (bool hide=false)
static void shell_input ()
static void child ()
static void parent ()
int main ()

Variables

charpenviron
static int pid
static int master
static char prompt [] = "tmshell$ "
static const int promptlen = sizeof(prompt)-1
int output_pos
charp output

Define Documentation

#define DATA_BEGIN   ((char) 2)

Definition at line 42 of file tm_shell.cpp.

#define DATA_END   ((char) 5)

Definition at line 43 of file tm_shell.cpp.

#define DATA_ESCAPE   ((char) 27)

Definition at line 44 of file tm_shell.cpp.

#define ERROR   (-1)

Definition at line 36 of file tm_shell.cpp.

#define IN   0

Definition at line 40 of file tm_shell.cpp.

#define OUT   1

Definition at line 41 of file tm_shell.cpp.

#define STDERR   2

Definition at line 39 of file tm_shell.cpp.

#define STDIN   0

Definition at line 37 of file tm_shell.cpp.

#define STDOUT   1

Definition at line 38 of file tm_shell.cpp.


Typedef Documentation

typedef char* charp

Definition at line 33 of file tm_shell.cpp.


Function Documentation

static void append ( charp s,
char  c,
int &  pos,
int &  max 
) [static]

Definition at line 53 of file tm_shell.cpp.

                                                          {
  if (pos == max) {
    int i;
    charp r= s;
    max <<= 1;
    s= (charp) malloc (max);
    for (i=0; i<pos; i++) s[i]=r[i];
    free (r);
  }
  s[pos++]= c;
}

Here is the caller graph for this function:

static void child ( ) [static]

Definition at line 156 of file tm_shell.cpp.

        {
  struct termios t;
  tcgetattr(STDIN, &t);
  t.c_lflag &= ~ECHO;
  tcsetattr(STDIN, TCSANOW, &t);
  setenv ("PS1", prompt, true);
  const charp argv[] = {
    const_cast<charp> ("sh"),
    const_cast<charp> ("-i"),
    NULL };
  execve("/bin/sh", argv, environ);
  exit(127);
}

Here is the caller graph for this function:

int main ( )

Definition at line 182 of file tm_shell.cpp.

        {
  cout << DATA_BEGIN << "verbatim:" << "Shell session inside TeXmacs" << flush;
  pid = forkpty(&master,NULL,NULL,NULL);
  if (pid==0) child();
  cout << " pid = " << pid << "\n" << flush;
  parent();
}

Here is the call graph for this function:

static void parent ( ) [static]

Definition at line 171 of file tm_shell.cpp.

         {
  signal (SIGINT, shell_interrupt);
  shell_output (true);
  cout << DATA_END << flush;
  while (true) {
    shell_input ();
    shell_output ();
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void shell_input ( ) [static]

Definition at line 148 of file tm_shell.cpp.

               {
  char input [10000];
  cin.getline (input, 10000, '\n');
  strcat (input, "\n");
  write (master, input, strlen (input));
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void shell_interrupt ( int  sig) [static]

Definition at line 69 of file tm_shell.cpp.

                          {
  if (output != NULL) {
    int i;
    for (i=0; i<output_pos; i++) cout << output[i];
    cout << endl;
  }
  cout << DATA_BEGIN << "scheme:(with \"color\" \"red\" \""
       << "Interrupted TeXmacs shell"
       << "\")" << DATA_END
       << DATA_END
       << flush ;
  killpg(pid, SIGTERM);
  sleep(1);
  killpg(pid, SIGKILL);
  wait (NULL);
  exit (0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

static void shell_output ( bool  hide = false) [static]

Definition at line 88 of file tm_shell.cpp.

                                {
  static struct timeval tv;
  int output_max= 1024;
  output = (charp) malloc (output_max);
  output_pos= 0;
  cout << DATA_BEGIN << "verbatim:" << flush;
  while (true) {
    fd_set rfds;
    FD_ZERO (&rfds);
    FD_SET (master, &rfds);
    tv.tv_sec = 0;
    tv.tv_usec = 10000;
    int r = select (master+1, &rfds, NULL, NULL, &tv);
    if (r == 0) continue;
    if (r == -1) continue;

    if (FD_ISSET (master, &rfds)) {
      int i, r;
      char outbuf[1024];
      r = read (master, outbuf, 1024);
      if (r == ERROR) {
       cerr << "TeXmacs shell] read failed\n" << flush;
       killpg(pid, SIGTERM);
       sleep(1);
       killpg(pid, SIGKILL);
       wait (NULL);
       exit (1);
      }
      else if (r == 0) {
       wait (NULL);
       cout << DATA_END << flush;
       exit (0);
      }
      else for (i=0; i<r; i++) {
       append (output, outbuf[i], output_pos, output_max);
       if (outbuf[i]=='\n') {
         append (output, '\0', output_pos, output_max);
         if (hide) hide= false; 
         else cout << output << flush;
         output_pos= 0;
       }
      }
    }

    if (output_pos >= promptlen &&
       strncmp(output + output_pos - promptlen, prompt, promptlen) == 0)
      {
       output[output_pos-promptlen]=0;
       if (hide) hide= false; 
       else cout << output << flush;
       output_pos= 0;
       break;
      }
  }

  cout << DATA_END << flush;
  free (output);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

Definition at line 34 of file tm_shell.cpp.

int master [static]

Definition at line 50 of file tm_shell.cpp.

Definition at line 66 of file tm_shell.cpp.

Definition at line 65 of file tm_shell.cpp.

int pid [static]

Definition at line 49 of file tm_shell.cpp.

char prompt[] = "tmshell$ " [static]

Definition at line 51 of file tm_shell.cpp.

const int promptlen = sizeof(prompt)-1 [static]

Definition at line 52 of file tm_shell.cpp.