File: io.c

package info (click to toggle)
scilab 4.0-12
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 100,640 kB
  • ctags: 57,333
  • sloc: ansic: 377,889; fortran: 242,862; xml: 179,819; tcl: 42,062; sh: 10,593; ml: 9,441; makefile: 4,377; cpp: 1,354; java: 621; csh: 260; yacc: 247; perl: 130; lex: 126; asm: 72; lisp: 30
file content (437 lines) | stat: -rw-r--r-- 9,912 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*------------------------------------------------------------------------
 *    Copyright (C) 2001-2003 Enpc/Jean-Philippe Chancelier
 *    jpc@cermics.enpc.fr 
 *--------------------------------------------------------------------------*/

#include <string.h> 
#include <stdio.h>
#include <errno.h>
#include <setjmp.h>
#include <ctype.h>
#include <sys/time.h>
#include <stdarg.h>
#include <stdlib.h>

#ifdef aix
#include <sys/select.h>
#endif

#include <gdk/gdkx.h>
#include <gtk/gtk.h>

#include "../graphics/Math.h"
#include "../os_specific/Os_specific.h"
#include "All-extern.h"

/* WITH_TK is eventually define in ../machine.h 
   include by ../graphics/Math.h */ 

#ifdef WITH_TK
#include "../tclsci/TCL_Global.h"
#endif

extern void start_sci_gtk(void);
extern int getdiary();
void C2F(diary) __PARAMS((char *str,int *n));
void diary_nnl __PARAMS((char *str,int *n));

static int BasicScilab = 1;

/*---------------------------------------------------------
 * Input output functions 
 *---------------------------------------------------------*/

void C2F(xscisrn)(char *str,integer *n,long int dummy)
{
  int i;
  for ( i =0 ; i < *n; i++) {
    if ( str[i] == '\n' ) 
      {
	fprintf(stdout,"%c",'\r');
      }
    fprintf(stdout,"%c",str[i]);
  }
}

void C2F(xscisncr)(char *str,integer *n,long int dummy)
{
  int i;
  for ( i =0 ; i < *n; i++)  fprintf(stdout,"%c",str[i]);
}


void C2F(xscistring)(char *str, int *n, long int dummy)
{
  int i ;
  for ( i =0 ; i < *n; i++)  fprintf(stdout,"%c",str[i]);
  fprintf(stdout,"\n");
}

void C2F(xscimore)(int *n)
{
  int n1=0;
  *n=0;
  if ( using_readline() == 1 ) 
    n1= get_one_char("[More (y or n) ?] ");
  else {
    fprintf(stdout,"%s","[More (y or n ) ?] ");
    n1=Xorgetchar(0);
  }
  if ( n1 == 110 )  *n=1;
  fprintf(stdout,"\n");
}

/* I/O Function for C routines no test for xscion here */

void Scisncr(char *str)
{
  fprintf(stdout,"%s",str);
}

void Scistring(str)
     char *str;
{
  fprintf(stdout,"%s",str);
}

/* 
 * sciprint(format,arg1,....)
 */

void  sciprint(char *fmt,...) 
{
  integer lstr;
  va_list ap;
  char s_buf[1024];
  va_start(ap,fmt);

  (void )vfprintf(stdout, fmt, ap );
  if (getdiary()) {
    (void ) vsprintf(s_buf, fmt, ap );
    lstr=strlen(s_buf);
    diary_nnl(s_buf,&lstr);
  }
  va_end(ap);
}

/* 
 * sciprint(format,arg1,....)
 * no diary record 
 */

void  sciprint_nd(char *fmt,...) 
{
  va_list ap;
  va_start(ap,fmt);
  vprintf(fmt, ap );
  va_end(ap);
}

/* 
 * as sciprint but with an added first argument 
 * which is ignored (used in do_printf) 
 */

int  sciprint2(int iv,char *fmt,...) 
{
  int retval,lstr;
  va_list ap;
  char s_buf[1024];
  va_start(ap,fmt);

  retval= vfprintf(stdout, fmt, ap );
  if (getdiary()) {
    (void ) vsprintf(s_buf, fmt, ap );
    lstr=strlen(s_buf);
    diary_nnl(s_buf,&lstr);
  }

  va_end(ap);
  return retval;
}

/************************************************************************ 
 * This routine is called by the Scilab interpreter in the course of 
 * computation to checks for all events except typed text (but testing 
 * Ctrl )
 * and to deal with them 
 * (This function is useless in the gtk version)  
 ************************************************************************/

void xevents1()
{
  if (BasicScilab) return;
#ifdef WITH_TK
  flushTKEvents();
#endif
  /* XXXXXX */
  while ( gtk_events_pending())
    gtk_main_iteration(); 
}

/****************************************
 * main Input function 
 * returns a char and deal with events 
 ****************************************/

/*---------------------------------------------------------------------------
 * Functions to set or to get the scilab status 
 * i.e is it a scilab or a scilab -nw 
 *---------------------------------------------------------------------------*/

static int INXscilab=0;

void SetXsciOn()
{
#ifdef WITH_TK
  initTCLTK();
#endif
  INXscilab=1;
  BasicScilab = 0;
}

/* for Fortran calls */

int C2F(xscion)(int *i)
{
  *i=INXscilab;
  return(0);
}


int C2F(checkevts)(int *i)
{
  *i= Max(INXscilab,1-BasicScilab);
  return(0);
}

/*---------------------------------------------------------------------------
 * when there is no window 
 *    the display can be activated i.e with a plot or an x_menu 
 *    then BasicScilab has to be set to 0 
 *---------------------------------------------------------------------------*/

void SetNotBasic() {
  BasicScilab=0;
}

int GetBasic() { return  BasicScilab;} 



/* 
 * used to start tk at run time when scilab 
 * was started with scilab -nw 
 * open a display with DisplayInit and initialize 
 * Tk. If TK_Started is set to one then the initialization 
 * was correct.
 */

void sci_tk_activate(void)
{
#ifdef WITH_TK
  start_sci_gtk(); /* be sure that gtk is started */
  initTCLTK();
  BasicScilab = 0;
  flushTKEvents();
#endif
}



/*---------------------------------------------------------------------------
 * 
 *  Xorgetchar : function used while in the scilab -nw mode 
 *      by zzledt to get the next typed char (in stdin)
 *      but also checks for X11 events if we are using an X Window 
 *      ( ex a graphic window) with scilab -nw )
 *      
 *      stdin is supposed to be changed 
 *      so as not to wait for <cr> this is done inside zzledt 
 *      (in the following code  the key function is select )
 *---------------------------------------------------------------------------*/


#define IBSIZE 1024
char sci_input_char_buffer[IBSIZE];
int  sci_input_char_buffer_count = 0;

#define SELECT_DEBUG(x)

/*
 * send string s as if it was typed in scilab window 
 * 
 */ 

void write_scilab(char *s)
{
  while ( *s != '\0' && sci_input_char_buffer_count < IBSIZE) 
    {
      sci_input_char_buffer[sci_input_char_buffer_count++]= *s;
      s++;
    }
  sci_input_char_buffer[sci_input_char_buffer_count++]='\n';
}

/* wait for a character and check for pending events */

int Xorgetchar(int interrupt)
{
  int i;
  static int c_count = -1;
  static int GtkXsocket,fd_in,fd_out,fd_err;
  static int first = 0,max_plus1;
  static int size_first=0;
  fd_set select_mask,write_mask;
  static struct timeval select_timeout;

  /* try to get terminal size */
  if ( size_first==0 ) { sci_winch_signal(0);size_first++;}


  if ( BasicScilab) return(getchar());

  if ( first == 0) 
    {
      first++;
      GtkXsocket = ConnectionNumber(GDK_DISPLAY());
      fd_in  = fileno(stdin) ;
      fd_out = fileno(stdout);
      fd_err = fileno(stderr);
      max_plus1 = Max(fd_in,GtkXsocket);      
      max_plus1 = Max(fd_out,max_plus1);
      max_plus1 = Max(fd_err,max_plus1);
#ifdef WITH_TK 
      max_plus1 = Max(XTKsocket,max_plus1);
#endif 
      max_plus1++;
    }
  
  for( ; ; ) {
    /* always flush writes before waiting */
#ifdef WITH_TK 
    flushTKEvents();
#endif 
    gdk_flush();
    fflush(stdout); 
    fflush(stderr);
    /* Update the masks and, unless X events are already in the queue,
     * wait for I/O to be possible. 
     */
    FD_ZERO(&select_mask);
    FD_SET(fd_in , &select_mask);
    FD_SET(GtkXsocket, &select_mask);
#ifdef WITH_TK 
    FD_SET(XTKsocket, &select_mask);
#endif 
    FD_ZERO(&write_mask);
    /* XXXX : the two next FD_SET causes select not to wait 
     * and since they do not seam necessary they are commented out  
     */
    /* FD_SET(fd_out,&write_mask);
       FD_SET(fd_err,&write_mask); */

    select_timeout.tv_sec =  0;
    select_timeout.tv_usec = 10;
    while ( gtk_events_pending())
      { 
	gtk_main_iteration(); 
      }
    /* maybe a  handler to be executed  
     * note that if more than one menu were activated they will 
     * all be evaluated 
     */

    if (interrupt&&(C2F(ismenu)()==1)) return(-1);

    /* maybe a new string to execute */
    if ( sci_input_char_buffer_count > 0) 
      {
	sci_input_char_buffer_count--;
	return sci_input_char_buffer[++c_count];
      }
    else
      {
	c_count = -1;
      }

    i = select(max_plus1, &select_mask,&write_mask, (fd_set *)NULL,
	       &select_timeout);
    if (i < 0) {
      if (errno != EINTR)
	{ 
#ifdef DEBUG
	  fprintf(stderr,"error in select\n");
#endif
	  exit(0);
	  continue;
	} 
      continue ;
    }
    if ( i== 0) continue;
    /* if there's something to output */
    if ( FD_ISSET(fd_out,&write_mask)) { 
      fflush(stdout); 
    }
    if ( FD_ISSET(fd_err,&write_mask)) { 
      fflush(stderr); 
    }

    /* if there's something to read */
    if ( FD_ISSET(fd_in,&select_mask )) { 
      return getchar();
      break;
    } 
#ifdef WITH_TK 
    if ( FD_ISSET(XTKsocket,&select_mask )) { 
      flushTKEvents();
    }
#endif 
    if ( FD_ISSET(GtkXsocket,&select_mask)) { 
      /* if there are X events in our queue, it
       * counts as being readable 
       */
      while ( gtk_events_pending()) /*  ||(select_mask & gtk_mask) */
	{ 
	  gtk_main_iteration(); 
	} 
    }
  }
}
int XEvorgetchar(int interrupt)
{
  return Xorgetchar(interrupt);
}
/*---------------------------------------------------------------------------
 *  Dealing with X11 Events.
 *  xevents is called by Xorgetchar and also by DispatchEvents in 
 *  routines/system 
 *  xevents must work for scilab and scilab -nw 
 ---------------------------------------------------------------------------*/

int C2F(sxevents)(void)
{
  /* check the TK case */ 
#ifdef WITH_TK
  flushTKEvents();
#endif
  while ( gtk_events_pending())
    gtk_main_iteration(); 
  return(0);
}

/*-------------------------------------------------------
 * winch signal : window size changed 
 *-------------------------------------------------------*/

void sci_winch_signal(int n) 
{
  int rows,cols;
  /* should not be usefull here since it is supposed to be automatically called */
  sci_get_screen_size (&rows,&cols); 
  if ( rows != 0 && cols != 0 )     C2F(scilines)(&rows,&cols); 
#ifdef DEBUG 
  fprintf(stderr,"windows size changed %d %d\r\n",rows,cols); 
#endif  
}