0
|
1 #include <stdio.h> |
|
2 |
|
3 #include <sys/time.h> |
|
4 #include <sys/types.h> |
|
5 #include <unistd.h> |
|
6 |
|
7 #include "engine.h" |
|
8 #include "engif.h" |
|
9 |
|
10 int pfd1[2]; |
|
11 int pfd2[2]; |
|
12 int outcnt; |
|
13 |
|
14 extern Engine matengine; |
|
15 extern int matopen; |
|
16 extern char *matbufptr; |
|
17 extern int matbufcnt; |
|
18 |
|
19 static int savechar( int outdest, char dat ); |
|
20 |
|
21 int openpipes( void ) |
|
22 { |
|
23 int retval = 0; |
|
24 |
|
25 if( pipe( pfd1 ) == -1 ) |
|
26 { |
|
27 perror("pipe"); |
|
28 retval = -1; |
|
29 } |
|
30 else |
|
31 { |
|
32 if( pipe( pfd2 ) == -1 ) |
|
33 { |
|
34 perror("pipe"); |
|
35 close( pfd1[0] ); /* Needs some error checking */ |
|
36 close( pfd1[1] ); /* Needs some error checking */ |
|
37 retval = -1; |
|
38 } |
|
39 } |
|
40 return retval; |
|
41 } |
|
42 |
|
43 |
|
44 int closepipes( void ) |
|
45 { |
|
46 int retval = 0; |
|
47 |
|
48 /* Needs some error checking */ |
|
49 |
|
50 close( pfd1[0] ); |
|
51 close( pfd1[1] ); |
|
52 close( pfd2[0] ); |
|
53 close( pfd2[1] ); |
|
54 |
|
55 return retval; |
|
56 } |
|
57 |
|
58 |
|
59 int plumbpipes( void ) |
|
60 { |
|
61 int retval = 0; |
|
62 |
|
63 /* Needs some error checking */ |
|
64 |
|
65 close(0); /* close normal stdin */ |
|
66 dup(pfd2[0]); /* make stdin same as pfd2[0] */ |
|
67 close(1); /* close normal stdout */ |
|
68 dup(pfd1[1]); /* make stdout same as pfd1[1] */ |
|
69 |
|
70 return retval; |
|
71 } |
|
72 |
|
73 |
|
74 void cleanhouse( void ) |
|
75 { |
|
76 if( matopen ) |
|
77 engClose( &matengine ); |
|
78 } |
|
79 |
|
80 |
|
81 int getline( char* buf ) |
|
82 { |
|
83 int i; |
|
84 |
|
85 /* Read a line into buf */ |
|
86 i = 0; |
|
87 do |
|
88 { |
|
89 read( pfd1[0], &buf[i], 1 ); |
|
90 i++; |
|
91 if( i == BUFMAX-1 ) |
|
92 buf[i-1] = '\n'; |
|
93 } |
|
94 while( buf[i-1] != '\n' ); |
|
95 buf[i] = '\0'; |
|
96 |
|
97 #ifdef DEBUGAPI |
|
98 printf( "getline: %s", buf ); |
|
99 #endif |
|
100 |
|
101 return 0; |
|
102 } |
|
103 |
|
104 |
|
105 int putline( char* buf ) |
|
106 { |
|
107 int count; |
|
108 |
|
109 /* Write a line from buf */ |
|
110 count = write( pfd2[1], buf, strlen(buf) ); |
|
111 |
|
112 #ifdef DEBUGAPI |
|
113 printf( "putline: %s", buf ); |
|
114 #endif |
|
115 |
|
116 return count; |
|
117 } |
|
118 |
|
119 int flushjunk( void ) |
|
120 { |
|
121 #ifdef DEBUGAPI |
|
122 char temp; |
|
123 int count; |
|
124 fd_set rfds; |
|
125 struct timeval tv; |
|
126 int retval; |
|
127 |
|
128 printf( "flushjunk: " ); |
|
129 |
|
130 count = 0; |
|
131 do |
|
132 { |
|
133 /* Check the Octave pipe to see if it has input. */ |
|
134 FD_ZERO(&rfds); |
|
135 FD_SET(pfd1[0], &rfds); |
|
136 /* Wait up to two seconds. */ |
|
137 tv.tv_sec = 2; |
|
138 tv.tv_usec = 0; |
|
139 |
|
140 retval = select(pfd1[0]+1, &rfds, NULL, NULL, &tv); |
|
141 /* Don't rely on the value of tv now! */ |
|
142 |
|
143 if (retval) |
|
144 { |
|
145 count++; |
|
146 read( pfd1[0], &temp, 1 ); |
|
147 printf( "%c", temp ); |
|
148 } |
|
149 } while( retval ); |
|
150 |
|
151 printf("\n"); |
|
152 return count; |
|
153 #else |
|
154 return 0; |
|
155 #endif |
|
156 } |
|
157 |
|
158 |
|
159 int flushprompt( int outkey ) |
|
160 { |
|
161 char temp; |
|
162 int test, count; |
|
163 |
|
164 /* If outkey=1 : write all engine output to the output buffer */ |
|
165 /* outkey=2 : write all engine output to stdout */ |
|
166 /* otherwise : discard all output from the engine */ |
|
167 |
|
168 count = 0; |
|
169 outcnt = 0; |
|
170 |
|
171 /* printf( "flushprompt: " ); */ |
|
172 |
|
173 test = 1; |
|
174 while( test != 4 ) |
|
175 { |
|
176 read( pfd1[0], &temp, 1 ); |
|
177 switch( test ) |
|
178 { |
|
179 case 0: |
|
180 if( temp == '\n' ) |
|
181 test = 1; |
|
182 else |
|
183 test = 0; |
|
184 break; |
|
185 case 1: |
|
186 if( temp == '\n' ) |
|
187 test = 1; |
|
188 else |
|
189 { |
|
190 if( temp == '>' ) |
|
191 test = 2; |
|
192 else |
|
193 test = 0; |
|
194 } |
|
195 break; |
|
196 case 2: |
|
197 if( temp == '>' ) |
|
198 test = 3; |
|
199 else |
|
200 { |
|
201 test = 0; |
|
202 savechar( outkey, '>' ); /* Save the current ">" char */ |
|
203 } |
|
204 break; |
|
205 case 3: |
|
206 if( temp == ' ' ) |
|
207 test = 4; |
|
208 else |
|
209 { |
|
210 test = 0; |
|
211 savechar( outkey, '>' ); /* Save the previous ">" char */ |
|
212 savechar( outkey, '>' ); /* Save the current ">" char */ |
|
213 } |
|
214 break; |
|
215 } |
|
216 if( test < 2 ) |
|
217 savechar( outkey, temp ); |
|
218 } |
|
219 if( outkey == 2 ) |
|
220 printf( ">> " ); |
|
221 else |
|
222 { |
|
223 if( outkey == 1 ) |
|
224 { |
|
225 /* Add terminating NULL to output buffer */ |
|
226 if( matbufptr != NULL ) |
|
227 if( outcnt < matbufcnt ) |
|
228 { |
|
229 matbufptr[outcnt] = '\0'; |
|
230 outcnt++; |
|
231 } |
|
232 } |
|
233 } |
|
234 |
|
235 return count; |
|
236 } |
|
237 |
|
238 |
|
239 static int savechar( int outdest, char dat ) |
|
240 { |
|
241 /* If outdest=1 : write all engine output to the output buffer */ |
|
242 /* outdest=2 : write all engine output to stdout */ |
|
243 /* otherwise : discard all output from the engine */ |
|
244 |
|
245 switch( outdest ) |
|
246 { |
|
247 case 0: |
|
248 break; |
|
249 case 1: |
|
250 if( matbufptr != NULL ) |
|
251 if( outcnt < matbufcnt-1 ) |
|
252 { |
|
253 matbufptr[outcnt] = dat; |
|
254 outcnt++; |
|
255 } |
|
256 break; |
|
257 case 2: |
|
258 putchar( dat ); |
|
259 break; |
|
260 default: |
|
261 fprintf( stderr, "Engine: unknown output destination specified\n" ); |
|
262 } |
|
263 |
|
264 return 0; |
|
265 } |