feread.cc
Go to the documentation of this file.
1 /****************************************
2 * Computer Algebra System SINGULAR *
3 ****************************************/
4 /*
5 * ABSTRACT: input from ttys, simulating fgets
6 */
7 
8 #include <kernel/mod2.h>
9 
10 // ----------------------------------------
11 // system settings:
12 
13 #undef USE_READLINE4
14 
15 //----------------------------------------
16 #ifdef __CYGWIN__
17 #define READLINE_STATIC
18 #endif
19 #include <omalloc/omalloc.h>
20 #include <misc/options.h>
21 
23 
24 #ifdef HAVE_STATIC
25 #undef HAVE_DYN_RL
26 #endif
27 
28 #if defined(HAVE_DYN_RL)
29 #include <unistd.h>
30 #endif
31 
32 static char * fe_fgets_stdin_init(const char *pr,char *s, int size);
33 char * (*fe_fgets_stdin)(const char *pr,char *s, int size)
35 
36 extern char *iiArithGetCmd(int);
37 
38 /* ===================================================================*/
39 /* = static/dymanic readline = */
40 /* ===================================================================*/
41 #if defined(HAVE_READLINE) || defined(HAVE_DYN_RL) || defined(HAVE_LIBREADLINE)
42 
43 #ifndef STDOUT_FILENO
44 #define STDOUT_FILENO 1
45 #endif
46 
47 /* Generator function for command completion. STATE lets us know whether
48 * to start from scratch; without any state (i.e. STATE == 0), then we
49 * start at the top of the list.
50 */
51 #include <Singular/ipid.h>
52 extern "C"
53 char *command_generator (char *text, int state)
54 {
55  static int list_index, len;
56  static idhdl h;
57  const char *name;
58 
59  /* If this is a new word to complete, initialize now. This includes
60  saving the length of TEXT for efficiency, and initializing the index
61  variable to 0. */
62  if (state==0)
63  {
64  list_index = 1;
65  len = strlen (text);
66  h=basePack->idroot;
67  }
68 
69  /* Return the next name which partially matches from the command list. */
70  while ((name = iiArithGetCmd(list_index))!=NULL)
71  {
72  list_index++;
73 
74  if (strncmp (name, text, len) == 0)
75  return (strdup(name));
76  }
77  if (len>1)
78  {
79  while (h!=NULL)
80  {
81  name=h->id;
82  h=h->next;
83  if (strncmp (name, text, len) == 0)
84  return (strdup(name));
85  }
86  }
87  /* If no names matched, then return NULL. */
88  return ((char *)NULL);
89 }
90 #endif
91 
92 /* ===================================================================*/
93 /* = static readline = */
94 /* ===================================================================*/
95 /* some procedure are shared with "dynamic readline" */
96 #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE) || defined(HAVE_DYN_RL))
97 #include <unistd.h>
98 #include <stdio.h>
99 #include <stdlib.h>
100 #include <sys/types.h>
101 #include <sys/file.h>
102 #include <sys/stat.h>
103 #include <sys/errno.h>
104 
105 // #undef READLINE_READLINE_H_OK
106 
107 extern "C" {
108  typedef char * (*RL_PROC)(const char*,int);
109  #ifdef READLINE_READLINE_H_OK
110  #include <readline/readline.h>
111  #ifdef HAVE_READLINE_HISTORY_H
112  #include <readline/history.h>
113  #endif
114  #endif
115 
116  #ifdef RL_VERSION_MAJOR
117  #if (RL_VERSION_MAJOR >= 4)
118  #define USE_READLINE4
119  #endif
120  #endif
121 
122  #ifndef USE_READLINE4
123  #define rl_filename_completion_function filename_completion_function
124  #define rl_completion_matches completion_matches
125  #endif
126  #ifndef READLINE_READLINE_H_OK
127  /* declare everything we need explicitely and do not rely on includes */
128  extern char * rl_readline_name;
129  extern char *rl_line_buffer;
130  char *rl_filename_completion_function(const char*, int);
131  typedef char **CPPFunction ();
132 
133  extern char ** rl_completion_matches (const char*, RL_PROC);
135  extern FILE * rl_outstream;
136  extern char * readline (const char *);
137  extern void add_history (char *);
138  extern int write_history ();
139  extern void using_history();
140  extern int read_history(char *);
141  extern int history_total_bytes();
142  #endif /* READLINE_READLINE_H_OK */
143 
144  typedef char * (*PROC)();
145 
146  typedef char **RL_CPPFunction (const char*, int,int);
147 }
148 
149 
150 char * fe_fgets_stdin_rl(const char *pr,char *s, int size);
151 
152 /* Tell the GNU Readline library how to complete. We want to try to complete
153  on command names or on filenames if it is preceded by " */
154 
155 /* Attempt to complete on the contents of TEXT. START and END show the
156 * region of TEXT that contains the word to complete. We can use the
157 * entire line in case we want to do some simple parsing. Return the
158 * array of matches, or NULL if there aren't any.
159 */
160 #if defined(HAVE_DYN_RL)
161 extern "C"
162 {
163  int fe_init_dyn_rl();
164  char *(*fe_filename_completion_function)(); /* 3 */
165  char *(* fe_readline) (char *); /* 4 */
166  void (*fe_add_history) (char *); /* 5 */
167  char ** fe_rl_readline_name; /* 6 */
168  char **fe_rl_line_buffer; /* 7 */
169  char **(*fe_completion_matches)(...); /* 8 */
171  FILE ** fe_rl_outstream; /* 10 */
172  int (*fe_write_history) (); /* 11 */
173  int (*fe_history_total_bytes) (); /* 12 */
174  void (*fe_using_history) (); /* 13 */
175  int (*fe_read_history) (char *); /* 14 */
176 
177 }
178 #endif
179 char ** singular_completion (char *text, int start, int end)
180 {
181  /* If this word is not in a string, then it may be a command
182  to complete. Otherwise it may be the name of a file in the current
183  directory. */
184 #ifdef HAVE_DYN_RL
185  #define x_rl_line_buffer (*fe_rl_line_buffer)
186  #define x_rl_completion_matches (*fe_completion_matches)
187  #define x_rl_filename_completion_function (*fe_filename_completion_function)
188 #else
189  #define x_rl_line_buffer rl_line_buffer
190  #define x_rl_completion_matches rl_completion_matches
191  #define x_rl_filename_completion_function rl_filename_completion_function
192 #endif
193  if ((start>0) && (x_rl_line_buffer[start-1]=='"'))
196 #undef x_rl_line_buffer
197 #undef x_rl_completion_matches
198  if (m==NULL)
199  {
200  m=(char **)malloc(2*sizeof(char*));
201  m[0]=(char *)malloc(end-start+2);
202  strncpy(m[0],text,end-start+1);
203  m[1]=NULL;
204  }
205  return m;
206 }
207 
208 #ifndef HAVE_DYN_RL
209 char * fe_fgets_stdin_rl(const char *pr,char *s, int size)
210 {
211  if (!BVERBOSE(V_PROMPT))
212  {
213  pr="";
214  }
215  mflush();
216 
217  char *line;
218  line = readline (pr);
219 
220  if (line==NULL)
221  return NULL;
222 
223  int l=strlen(line);
224  for (int i=l-1;i>=0;i--) line[i]=line[i]&127;
225 
226  if (*line!='\0')
227  {
228  add_history (line);
229  }
230  if (l>=size-1)
231  {
232  strncpy(s,line,size);
233  }
234  else
235  {
236  strncpy(s,line,l);
237  s[l]='\n';
238  s[l+1]='\0';
239  }
240  free (line);
241 
242  return s;
243 }
244 #endif
245 #endif
246 
247 /* ===================================================================*/
248 /* = emulated readline = */
249 /* ===================================================================*/
250 #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
251 extern "C" {
252 char * fe_fgets_stdin_fe(const char *pr,char *s, int size);
253 }
254 char * fe_fgets_stdin_emu(const char *pr,char *s, int size)
255 {
256  if (!BVERBOSE(V_PROMPT))
257  {
258  pr="";
259  }
260  mflush();
261  return fe_fgets_stdin_fe(pr,s,size);
262 }
263 #endif
264 
265 /* ===================================================================*/
266 /* = dynamic readline = */
267 /* ===================================================================*/
268 /* some procedure are shared with "static readline" */
269 #if defined(HAVE_DYN_RL)
270 char * fe_fgets_stdin_drl(const char *pr,char *s, int size)
271 {
272  if (!BVERBOSE(V_PROMPT))
273  {
274  pr="";
275  }
276  mflush();
277 
278  char *line;
279  line = (*fe_readline) ((char*)pr);
280 
281  if (line==NULL)
282  return NULL;
283 
284  int l=strlen(line);
285  for (int i=l-1;i>=0;i--) line[i]=line[i]&127;
286 
287  if (*line!='\0')
288  {
289  (*fe_add_history) (line);
290  }
291  if (l>=size-1)
292  {
293  strncpy(s,line,size);
294  }
295  else
296  {
297  strncpy(s,line,l);
298  s[l]='\n';
299  s[l+1]='\0';
300  }
301  free (line);
302 
303  return s;
304 }
305 #endif
306 
307 /* ===================================================================*/
308 /* = fgets = */
309 /* ===================================================================*/
310 char * fe_fgets(const char *pr,char *s, int size)
311 {
312  if (BVERBOSE(V_PROMPT))
313  {
314  fprintf(stdout,"%s",pr);
315  }
316  mflush();
317  errno=0;
318  char *line=fgets(s,size,stdin);
319  if (line!=NULL)
320  {
321  for (int i=strlen(line)-1;i>=0;i--) line[i]=line[i]&127;
322  }
323  else
324  {
325  /* NULL can mean various things... */
326  switch(errno)
327  {
328  case 0: return NULL; /*EOF */
329  case EINTR: return strcpy(s,"\n"); /* CTRL-C or other signal */
330  default: /* other error */
331  {
332  int errsv = errno;
333  fprintf(stderr,"fgets() failed with errno %d\n",errsv);
334  return NULL;
335  }
336  }
337  }
338  return line;
339 }
340 
341 /* ===================================================================*/
342 /* = init for static rl, dyn. rl, emu. rl = */
343 /* ===================================================================*/
344 static char * fe_fgets_stdin_init(const char *pr,char *s, int size)
345 {
346 #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD)
347  /* Allow conditional parsing of the ~/.inputrc file. */
348  rl_readline_name = "Singular";
349  /* Tell the completer that we want a crack first. */
350 #ifdef USE_READLINE4
351  rl_attempted_completion_function = (rl_completion_func_t *)singular_completion;
352 #else
354 #endif
355 
356  /* set the output stream */
357  if(!isatty(STDOUT_FILENO))
358  {
359  #ifdef atarist
360  rl_outstream = fopen( "/dev/tty", "w" );
361  #else
362  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
363  if (fn!=NULL) rl_outstream = fopen( fn, "w" );
364  #endif
365  }
366 
367  if(isatty(fileno(stdin)))
368  {
369  /* try to read a history */
370  using_history();
371  char *p = getenv("SINGULARHIST");
372  if (p != NULL)
373  {
374  read_history (p);
375  }
377  return(fe_fgets_stdin_rl(pr,s,size));
378  }
379  else
380  {
382  return(fe_fgets(pr,s,size));
383  }
384 #endif
385 #ifdef HAVE_DYN_RL
386  /* do dynamic loading */
387  int res=fe_init_dyn_rl();
388  if (res!=0)
389  {
390  //if (res==1)
391  // WarnS("dynamic loading of libreadline failed");
392  //else
393  // Warn("dynamic loading failed: %d\n",res);
394  if (res!=1)
395  Warn("dynamic loading failed: %d\n",res);
396  #ifdef HAVE_FEREAD
398  #else
400  #endif
401  return fe_fgets_stdin(pr,s,size);
402  }
403  else if (isatty(STDIN_FILENO))/*and could load libreadline: */
404  {
405  /* Allow conditional parsing of the ~/.inputrc file. */
406  *fe_rl_readline_name = "Singular";
407  /* Tell the completer that we want a crack first. */
409  /* try to read a history */
410  (*fe_using_history)();
411  char *p = getenv("SINGULARHIST");
412  if (p != NULL)
413  {
414  (*fe_read_history) (p);
415  }
416 
417  /* set the output stream */
418  if(!isatty(STDOUT_FILENO))
419  {
420  #ifdef atarist
421  *fe_rl_outstream = fopen( "/dev/tty", "w" );
422  #else
423  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
424  if (fn!=NULL) *fe_rl_outstream = fopen( fn, "w" );
425  #endif
426  }
428  return fe_fgets_stdin_drl(pr,s,size);
429  }
430  else
431  {
433  return fe_fgets(pr,s,size);
434  }
435 #else
436  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
438  return(fe_fgets_stdin_emu(pr,s,size));
439  #else
441  return(fe_fgets(pr,s,size));
442  #endif
443 #endif
444 }
445 
446 /* ===================================================================*/
447 /* = batch mode = */
448 /* ===================================================================*/
449 /* dummy (for batch mode): */
450 char * fe_fgets_dummy(const char */*pr*/,char */*s*/, int /*size*/)
451 {
452  return NULL;
453 }
454 
#define STDOUT_FILENO
Definition: feread.cc:44
FILE * rl_outstream
char ** fe_rl_line_buffer
Definition: feread.cc:168
const CanonicalForm int s
Definition: facAbsFact.cc:55
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:33
int fe_init_dyn_rl()
Definition: fereadl.c:766
void(* fe_add_history)(char *)
Definition: feread.cc:166
char * command_generator(char *text, int state)
Definition: feread.cc:53
CPPFunction * rl_attempted_completion_function
return P p
Definition: myNF.cc:203
void(* fe_using_history)()
Definition: feread.cc:174
#define x_rl_filename_completion_function
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:310
char * fe_fgets_dummy(const char *, char *, int)
Definition: feread.cc:450
#define STDIN_FILENO
Definition: fereadl.c:54
char * getenv()
int read_history(char *)
int write_history()
char * fe_fgets_stdin_fe(const char *pr, char *s, int size)
static char * fe_fgets_stdin_init(const char *pr, char *s, int size)
Definition: feread.cc:344
Definition: idrec.h:34
#define rl_completion_matches
Definition: feread.cc:124
char * readline(const char *)
#define mflush()
Definition: reporter.h:57
poly res
Definition: myNF.cc:322
char ** CPPFunction()
Definition: feread.cc:131
int history_total_bytes()
char * fe_fgets_stdin_rl(const char *pr, char *s, int size)
char * fe_fgets_stdin_emu(const char *pr, char *s, int size)
Definition: feread.cc:254
char * rl_line_buffer
char * iiArithGetCmd(int)
Definition: iparith.cc:9049
void * malloc(size_t size)
Definition: omalloc.c:92
void using_history()
int m
Definition: cfEzgcd.cc:119
#define free
Definition: omAllocFunc.c:12
int i
Definition: cfEzgcd.cc:123
idhdl next
Definition: idrec.h:38
char name(const Variable &v)
Definition: factory.h:178
#define strdup
Definition: omAllocFunc.c:17
#define x_rl_line_buffer
char ** singular_completion(char *text, int start, int end)
Definition: feread.cc:179
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
int(* fe_write_history)()
Definition: feread.cc:172
#define BVERBOSE(a)
Definition: options.h:33
#define V_PROMPT
Definition: options.h:52
#define NULL
Definition: omList.c:10
char *(* RL_PROC)(const char *, int)
Definition: feread.cc:108
package basePack
Definition: ipid.cc:64
char * rl_readline_name
void add_history(char *)
CPPFunction ** fe_rl_attempted_completion_function
Definition: feread.cc:170
const char * id
Definition: idrec.h:39
int(* fe_history_total_bytes)()
Definition: feread.cc:173
FILE ** fe_rl_outstream
Definition: feread.cc:171
int(* fe_read_history)(char *)
Definition: feread.cc:175
char * fe_fgets_stdin_drl(const char *pr, char *s, int size)
Definition: feread.cc:270
static Poly * h
Definition: janet.cc:978
char ** fe_rl_readline_name
Definition: feread.cc:167
#define x_rl_completion_matches
int l
Definition: cfEzgcd.cc:94
#define rl_filename_completion_function
Definition: feread.cc:123
char ** RL_CPPFunction(const char *, int, int)
Definition: feread.cc:146
#define Warn
Definition: emacs.cc:80