File: readline.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 (166 lines) | stat: -rw-r--r-- 3,845 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
/*-----------------------------------------------------
 * XXXXXX experience 
 * zzledt with readline 
 *-----------------------------------------------------*/

#include <string.h> 
#include <stdio.h>
#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/time.h>
#ifdef aix
#include <sys/select.h>
#endif

#include <readline/readline.h>

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

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

#include "../graphics/Math.h"

extern char sci_input_char_buffer[];
extern int  sci_input_char_buffer_count;

static int stop;
static char * full_line = NULL;
static int sci_readline(void);
static void process_line(char *line);
static char Sci_Prompt[10];


void C2F(zzledt)(char *buffer,int *  buf_size, int * len_line,int * eof,
		 long int dummy1)
{
  rl_callback_handler_install(Sci_Prompt, process_line);
  stop = 1;
  set_echo_mode(TRUE);
  set_is_reading(TRUE);
  sci_readline();
  /* now full_line contains the line */
  strcpy(buffer,full_line);
  *len_line = strlen(full_line);
  *eof = FALSE;
  set_is_reading(FALSE);
  if ( stop == 2 )
    free(full_line);
}

static int sci_readline()
{
  int i;
  static int GtkXsocket,fd_in,fd_out,fd_err;
  static int first = 0,max_plus1;
  fd_set select_mask,write_mask;
  static struct timeval select_timeout;
  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) + 1;
      select_timeout.tv_sec = 50;
      select_timeout.tv_usec = 0;
    }
  
  while (stop == 1)  {
    /* always flush writes before waiting */
    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);
    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); */

    while ( gtk_events_pending())
      { 
	gtk_main_iteration(); 
      }
    /* maybe a new sentence to execute is already available */
    if ( sci_input_char_buffer_count > 0) 
      {
	full_line  = sci_input_char_buffer;
	sci_input_char_buffer_count = 0 ; 
	stop = 3; 
	return ; 
      }
    /* select */
    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;
	} 
    }
    /* 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 )) { 
      rl_callback_read_char();
    } 
    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(); 
	}
    }
  }
}

void process_line(char *line)
{
  if ( line != NULL ) 
    { 
      if (line[0] != '\0' ) add_history(line);
      full_line = line ;
      rl_callback_handler_install("", process_line);
      stop = 2;
    }
}


void C2F(setprlev)(pause)
     int *pause;
{
  if ( *pause == 0 ) 
    sprintf(Sci_Prompt,"-->");
  else if ( *pause > 0 )
    sprintf(Sci_Prompt,"-%d->",*pause);
  else
    sprintf(Sci_Prompt,">>");
}