Initial version of donated sources by Avertec, 3.4p5.
[tas-yagle.git] / distrib / sources / tas / xtas / xtas_unix.c
1 /****************************************************************************/
2 /* */
3 /* Chaine de CAO & VLSI Alliance */
4 /* */
5 /* Produit : XTAS Version 5 */
6 /* Fichier : xtas_unix.c */
7 /* */
8 /* (c) copyright 1991-1998 Laboratoire LIP6 equipe ASIM */
9 /* Tous droits reserves */
10 /* Support : e-mail alliance-support@asim.lip6.fr */
11 /* */
12 /* Author(s) : Lionel PROTZENKO Date : 01/08/1997 */
13 /* */
14 /* Modified by : Date : ../../.... */
15 /* Modified by : Date : ../../.... */
16 /* Modified by : Date : ../../.... */
17 /* */
18 /*--------------------------------------------------------------------------*/
19 /* */
20 /* */
21 /*--------------------------------------------------------------------------*/
22
23 /*--------------------------------------------------------------------------*/
24 /* INCLUDE FILES */
25 /*--------------------------------------------------------------------------*/
26 #include "xtas.h"
27
28 int tas_pfd[2];
29 int tas_out[2];
30 sigjmp_buf XtasMyEnv;
31 static void (*XtasOldExitHandler) () = NULL;
32 char XTAS_DEBUG_MODE = 'N' ;
33
34 /*---------------------------------------------------------------------------*/
35 /* */
36 /* FUNCTION : XtasSetBlock */
37 /* */
38 /* IN ARGS : .fd : The file descriptor. */
39 /* .on : Indicator of a blocking read or not. */
40 /* */
41 /* OUT ARGS : ( void ) */
42 /* */
43 /* OBJECT : */
44 /* */
45 /*---------------------------------------------------------------------------*/
46 void XtasSetBlock( fd, on )
47 int fd, on;
48 {
49 static int blockf, nonblockf;
50 static int first = True;
51 int flags;
52
53 if ( first == True )
54 {
55 first = False;
56 if ((flags = fcntl(fd, F_GETFL, 0)) == -1)
57 {
58 fprintf(stderr,"ERREUR FCNTL\n");fflush(stderr);
59
60 }
61 blockf = flags & ~O_NDELAY;
62 blockf = flags & ~O_NONBLOCK;
63 nonblockf = flags | O_NDELAY;
64 nonblockf = flags | O_NONBLOCK;
65 }
66
67 if (fcntl(fd, F_SETFL, on ? blockf : nonblockf ) == -1)
68 {
69 fprintf(stderr,"ERREUR FCNTL\n");fflush(stderr);
70 }
71 }
72
73 /*---------------------------------------------------------------------------*/
74 /* */
75 /* FUNCTION : XtasInitFileErr */
76 /* */
77 /* IN ARGS : ( none ) */
78 /* */
79 /* OUT ARGS : ( void ) */
80 /* */
81 /* OBJECT : Catch stderr */
82 /* */
83 /*---------------------------------------------------------------------------*/
84 void XtasInitFileErr()
85 {
86 char *str ;
87
88 str = getenv("XTAS_DEBUG_MODE") ;
89 if(str != NULL)
90 {
91 XTAS_DEBUG_MODE = (strcmp(str,"yes") == 0 ) ? 'Y' : 'N' ;
92 }
93
94 if (pipe( tas_pfd ) != -1)
95 {
96 close(2);
97 dup2( tas_pfd[1],2 );
98 close( tas_pfd[1] );
99 }
100
101 if (pipe( tas_out ) != -1)
102 {
103 close(1);
104 dup2( tas_out[1],1 );
105 close( tas_out[1] );
106 }
107
108 if(XTAS_DEBUG_MODE == 'N')
109 {
110 signal( SIGBUS, XtasFatalSysError );
111 signal( SIGSEGV, XtasFatalSysError );
112 }
113 }
114
115 /*---------------------------------------------------------------------------*/
116 /* */
117 /* FUNCTION : XtasGetStdoutMess */
118 /* */
119 /* IN ARGS : ( none ) */
120 /* */
121 /* OUT ARGS : ( void ) */
122 /* */
123 /* OBJECT : Catch messages of stdout from the pipe. */
124 /* */
125 /*---------------------------------------------------------------------------*/
126 void XtasGetStdoutMess()
127 {
128 static char buffer[4096];
129 static int i = 0;
130
131 fflush( stderr );
132 i = read(tas_pfd[0],buffer,4095) ;
133
134 if(i == -1)
135 strcpy(buffer,"Fatal Error Occuring While Reading File") ;
136 else
137 buffer[i] = '\0' ;
138
139 if(strlen(buffer) == 0)
140 return ;
141
142 XtasFatalAppError(buffer) ;
143 }
144
145 /*---------------------------------------------------------------------------*/
146 /* */
147 /* FUNCTION : XtasPasqua */
148 /* */
149 /* IN ARGS : ( none ) */
150 /* */
151 /* OUT ARGS : ( void ) */
152 /* */
153 /* OBJECT : Activates the stdout watching. */
154 /* */
155 /*---------------------------------------------------------------------------*/
156 void XtasPasqua()
157 {
158 /*---------------------------------*/
159 /* Catch all stdout messages !!! */
160 /*---------------------------------*/
161 XtasSetBlock( 2, False );
162 XtasSetBlock( 1, False );
163 XtasOldExitHandler = signal( SIGTERM, XtasGetStdoutMess );
164 XtasClearAllStream (); /* avoid warnings from Motif */
165 }
166
167 /*---------------------------------------------------------------------------*/
168 /* */
169 /* FUNCTION : XtasFirePasqua */
170 /* */
171 /* IN ARGS : ( none ) */
172 /* */
173 /* OUT ARGS : ( void ) */
174 /* */
175 /* OBJECT : Deactivates the stdout watching. */
176 /* */
177 /*---------------------------------------------------------------------------*/
178 void XtasFirePasqua()
179 {
180 char c;
181 int nb = 1;
182
183 XtasSetLabelString( XtasDeskMessageField, XTAS_NULINFO );
184
185 /*-------------------*/
186 /* Clean up the pipe */
187 /*-------------------*/
188 while ((nb != -1) && (nb != 0))
189 {
190 XtasSetBlock( tas_pfd[0], False );
191 nb = read(tas_pfd[0], &c, 1);
192 }
193
194 nb = 1;
195 while ((nb != -1) && (nb != 0))
196 {
197 XtasSetBlock( tas_out[0], False );
198 nb = read(tas_out[0], &c, 1);
199 }
200
201 signal(SIGTERM, XtasOldExitHandler);
202 }
203
204 /*---------------------------------------------------------------------------*/
205 /* */
206 /* FUNCTION : XtasFatalSysError */
207 /* */
208 /* IN ARGS : ( none ) */
209 /* */
210 /* OUT ARGS : ( void ) */
211 /* */
212 /* OBJECT : Treatment of System Fatal Errors. */
213 /* */
214 /*---------------------------------------------------------------------------*/
215 void XtasFatalSysError()
216 {
217 XtasDestroyAllFig() ;
218 XtasFirePasqua();
219
220 XalDrawMessage( XtasErrorWidget, XTAS_FSYSERR );
221 XtCloseDisplay( XtDisplay( XtasTopLevel ) );
222
223 XtasExit(1) ;
224 }
225
226 /*---------------------------------------------------------------------------*/
227 /* */
228 /* FUNCTION : XtasExit */
229 /* */
230 /* IN ARGS : ( none ) */
231 /* */
232 /* OUT ARGS : ( void ) */
233 /* */
234 /* OBJECT : Treatment of exit */
235 /* */
236 /*---------------------------------------------------------------------------*/
237 void XtasExit(x)
238 int x ;
239 {
240 MBK_EXIT_KILL = 'N' ;
241 XyagExitErrorMessage(x);
242 EXIT(x);
243 }
244
245 /*---------------------------------------------------------------------------*/
246 /* */
247 /* FUNCTION : XtasFatalAppError */
248 /* */
249 /* IN ARGS : .message : The message to be drawn to the user. */
250 /* */
251 /* OUT ARGS : ( void ) */
252 /* */
253 /* OBJECT : Treatment of Application's Fatal Errors. */
254 /* */
255 /*---------------------------------------------------------------------------*/
256 void XtasFatalAppError( message )
257 char *message;
258 {
259 XtasDestroyAllFig() ;
260 XtasDestroyStbInProgress();
261 XtasFirePasqua();
262
263 if ( XtasErrorWidget )
264 {
265 XtasSetLabelString( XtasDeskMessageField, XTAS_FAPPERR );
266 XalForceUpdate( XtasDeskMessageField );
267 XalDrawMessage( XtasErrorWidget, message );
268 }
269
270 if (XyagJmpEnv != NULL)
271 siglongjmp(*XyagJmpEnv, 1);
272 else
273 siglongjmp(XtasMyEnv, 1);
274 }
275
276 /*---------------------------------------------------------------------------*/
277 /* */
278 /* FUNCTION : XtasGetWarningMess */
279 /* */
280 /* IN ARGS : .message : The message to be drawn to the user. */
281 /* */
282 /* OUT ARGS : ( void ) */
283 /* */
284 /* OBJECT : Treatment of Application's Warning */
285 /* */
286 /*---------------------------------------------------------------------------*/
287 void XtasGetWarningMess( )
288 {
289 char buffer[4096];
290 int i = 0;
291 char *pt ;
292
293 fprintf(stderr,"\n") ;
294 fflush( stderr );
295 i = read(tas_pfd[0],buffer,4095) ;
296
297 if(i == -1)
298 return ;
299
300 buffer[i] = '\0' ;
301
302
303 pt = buffer ;
304
305 while((isspace((int)*pt) != 0) && (*pt != '\0'))
306 pt++ ;
307
308 if(strlen(pt) == 0)
309 return ;
310
311 if ( XtasWarningWidget )
312 {
313 XalForceUpdate( XtasDeskMessageField );
314 XalDrawMessage( XtasWarningWidget, pt );
315 }
316 }
317
318 /*---------------------------------------------------------------------------*/
319 /* */
320 /* FUNCTION : XtasGetOutMess */
321 /* */
322 /* IN ARGS : .message : The message to be drawn to the user. */
323 /* */
324 /* OUT ARGS : ( void ) */
325 /* */
326 /* OBJECT : Treatment of Application's Warning */
327 /* */
328 /*---------------------------------------------------------------------------*/
329 void XtasGetOutMess( )
330 {
331 char buffer[65535];
332 unsigned int i = 0;
333 char *pt ;
334
335 fprintf(stdout,"\n") ;
336 fflush( stdout );
337 i = read(tas_out[0],buffer,65534) ;
338
339 if(i == -1)
340 return ;
341
342 buffer[i] = '\0' ;
343
344 pt = buffer ;
345
346 //while((isspace((int)*pt) != 0) && (*pt != '\0'))
347 // pt++ ;
348
349 if(strlen(pt) == 0)
350 return ;
351
352 if ( XtasTraceWidget )
353 {
354 XalForceUpdate( XtasDeskMessageField );
355 XalDrawMessage( XtasTraceWidget, pt );
356 }
357 }
358 /*---------------------------------------------------------------------------*/
359 /* */
360 /* FUNCTION : XtasClearAllStream */
361 /* */
362 /*---------------------------------------------------------------------------*/
363 void XtasClearAllStream( )
364 {
365 char buffer[65536];
366
367 fprintf(stdout,"\n") ;
368 fflush( stdout );
369 read(tas_out[0],buffer,65536) ;
370 fprintf(stderr,"\n") ;
371 fflush( stderr );
372 read(tas_pfd[0],buffer,65536) ;
373
374 }
375