~ubuntu-branches/ubuntu/hardy/texmacs/hardy

« back to all changes in this revision

Viewing changes to plugins/shell/src/tm_shell.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Ralf Treinen
  • Date: 2004-04-19 20:34:00 UTC
  • Revision ID: james.westby@ubuntu.com-20040419203400-g4e34ih0315wcn8v
Tags: upstream-1.0.3-R2
ImportĀ upstreamĀ versionĀ 1.0.3-R2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/******************************************************************************
 
3
* MODULE     : tm_shell.cpp
 
4
* DESCRIPTION: TeXmacs shell
 
5
* COPYRIGHT  : (C) 2000  Joris van der Hoeven
 
6
*******************************************************************************
 
7
* This software falls under the GNU general public license and comes WITHOUT
 
8
* ANY WARRANTY WHATSOEVER. See the file $TEXMACS_PATH/LICENSE for more details.
 
9
* If you don't have this file, write to the Free Software Foundation, Inc.,
 
10
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
11
******************************************************************************/
 
12
 
 
13
#include <iostream>
 
14
#include <stdlib.h>
 
15
#include <stdio.h>
 
16
#include <string.h>
 
17
#include <stdio.h>
 
18
#include <signal.h>
 
19
#include <unistd.h>
 
20
#include <sys/time.h>
 
21
#include <sys/wait.h>
 
22
using namespace std;
 
23
 
 
24
typedef char* charp;
 
25
extern charp* environ;
 
26
 
 
27
#define ERROR (-1)
 
28
#define STDIN 0
 
29
#define STDOUT 1
 
30
#define STDERR 2
 
31
#define IN 0
 
32
#define OUT 1
 
33
 
 
34
#define DATA_BEGIN   ((char) 2)
 
35
#define DATA_END     ((char) 5)
 
36
#define DATA_ESCAPE  ((char) 27)
 
37
//#define DATA_BEGIN   "[BEGIN]"
 
38
//#define DATA_END     "[END]"
 
39
//#define DATA_ESCAPE  "[ESCAPE]"
 
40
 
 
41
int   pid;            // process identifier of the child
 
42
int   tochild[2];     // for data going to the child
 
43
int   fromchild[2];   // for data coming from the child
 
44
int   in;             // file descriptor for data going to the child
 
45
int   out;            // file descriptor for data coming from the child
 
46
FILE* fin;            // file associated to in
 
47
 
 
48
/******************************************************************************
 
49
* Handling shell input and output
 
50
******************************************************************************/
 
51
 
 
52
void
 
53
append (charp &s, char c, int& pos, int& max) {
 
54
  if (pos == max) {
 
55
    int i;
 
56
    charp r= s;
 
57
    max <<= 1;
 
58
    s= (charp) malloc (max);
 
59
    for (i=0; i<pos; i++) s[i]=r[i];
 
60
    free (r);
 
61
  }
 
62
  s[pos++]= c;
 
63
}
 
64
 
 
65
void
 
66
shell_output (bool hide= false) {
 
67
  int output_pos= 0;
 
68
  int output_max= 1024;
 
69
  charp output= (charp) malloc (output_max);
 
70
  cout << DATA_BEGIN << "verbatim:";
 
71
 
 
72
  while (true) {
 
73
    fd_set rfds;
 
74
    FD_ZERO (&rfds);
 
75
    FD_SET (out, &rfds);
 
76
    int max_fd= out+1;
 
77
    struct timeval tv;
 
78
    tv.tv_sec  = 0;
 
79
    tv.tv_usec = 100;
 
80
    int nr= select (max_fd, &rfds, NULL, NULL, &tv);
 
81
    if (nr==0) {
 
82
      fflush (stdout);
 
83
      continue;
 
84
    }
 
85
 
 
86
    if (FD_ISSET (out, &rfds)) {
 
87
      int i, r;
 
88
      char outbuf[1024];
 
89
      r = read (out, outbuf, 1024);
 
90
      if (r == ERROR) {
 
91
        cerr << "TeXmacs shell] read failed\n";
 
92
        wait (NULL);
 
93
        exit (1);
 
94
      }
 
95
      else if (r == 0) {
 
96
        kill (pid, SIGKILL);
 
97
        cout << DATA_END;
 
98
        fflush (stdout);
 
99
        exit (0);
 
100
      }
 
101
      else for (i=0; i<r; i++) {
 
102
        append (output, outbuf[i], output_pos, output_max);
 
103
        if (outbuf[i]=='\n') {
 
104
          append (output, '\0', output_pos, output_max);
 
105
          if (hide) hide= false;
 
106
          else cout << output;
 
107
          fflush (stdout);
 
108
          output_pos= 0;
 
109
        }
 
110
      }
 
111
    }
 
112
 
 
113
    if (strncmp (output, "tmshell$ ", 9) == 0)
 
114
      break;
 
115
  }
 
116
 
 
117
  cout << DATA_END;
 
118
  fflush (stdout);
 
119
  free (output);
 
120
}
 
121
 
 
122
void
 
123
shell_input () {
 
124
  char input [10000];
 
125
  cin.getline (input, 10000, '\n');
 
126
  strcat (input, "\n");
 
127
  write (in, input, strlen (input));
 
128
  fflush (fin);
 
129
}
 
130
 
 
131
void
 
132
shell_interrupt (int sig) {
 
133
  cout << DATA_BEGIN << "scheme:(with \"color\" \"red\" \"";
 
134
  cout << "Interrupted TeXmacs shell";
 
135
  cout << "\")" << DATA_END;
 
136
  cout << DATA_END;
 
137
  // kill (pid, SIGINT); // Why does this not work ???
 
138
  kill (pid, SIGKILL);
 
139
  exit (0);
 
140
}
 
141
 
 
142
/******************************************************************************
 
143
* Launching the shell using a fork
 
144
******************************************************************************/
 
145
 
 
146
volatile void
 
147
invoke_shell () {
 
148
  charp argv[3];
 
149
  argv[0] = "sh";
 
150
  argv[1] = "-i";
 
151
  argv[2] = 0;
 
152
  execve("/bin/sh", argv, environ);
 
153
  exit(127);
 
154
}
 
155
 
 
156
int
 
157
main () {
 
158
  setenv ("PS1", "tmshell$ ", true);
 
159
  cout << DATA_BEGIN << "verbatim:";
 
160
  cout << "Shell session inside TeXmacs";
 
161
  pipe (tochild);
 
162
  pipe (fromchild);
 
163
  pid= fork ();
 
164
  if (pid==0) { // the child
 
165
    dup2 (tochild [IN], STDIN);
 
166
    close (tochild [IN]);
 
167
    close (fromchild [IN]);
 
168
    close (tochild [OUT]);
 
169
    dup2 (fromchild [OUT], STDOUT);
 
170
    dup2 (STDOUT, STDERR);
 
171
    close (fromchild [OUT]);
 
172
    invoke_shell ();
 
173
    exit (127);
 
174
  }
 
175
  else { // the main process
 
176
    int sig;
 
177
    out= fromchild [IN];
 
178
    close (fromchild [OUT]);
 
179
    in= tochild [OUT];
 
180
    close (tochild [IN]);
 
181
    fin= fdopen (in, "w");
 
182
    signal (SIGINT, shell_interrupt);
 
183
    shell_output (true);
 
184
    cout << DATA_END;
 
185
    while (true) {
 
186
      shell_input ();
 
187
      shell_output ();
 
188
    }
 
189
  }
 
190
  return 0;
 
191
}