My Project
Macros | Typedefs | Functions | Variables
feread.cc File Reference
#include "kernel/mod2.h"
#include <errno.h>
#include <stdio.h>
#include "omalloc/omalloc.h"
#include "misc/options.h"
#include "kernel/oswrapper/feread.h"
#include "reporter/reporter.h"
#include <unistd.h>
#include "Singular/ipid.h"
#include <stdlib.h>
#include <sys/types.h>
#include <sys/file.h>
#include <sys/stat.h>

Go to the source code of this file.

Macros

#define STDOUT_FILENO   1
 
#define rl_filename_completion_function   filename_completion_function
 
#define rl_completion_matches   completion_matches
 
#define x_rl_line_buffer   (*fe_rl_line_buffer)
 
#define x_rl_completion_matches   (*fe_completion_matches)
 
#define x_rl_filename_completion_function   (*fe_filename_completion_function)
 

Typedefs

typedef char *(* RL_PROC) (const char *, int)
 
typedef char ** CPPFunction()
 
typedef char *(* PROC) ()
 
typedef char ** RL_CPPFunction(const char *, int, int)
 

Functions

static char * fe_fgets_stdin_init (const char *pr, char *s, int size)
 
char * iiArithGetCmd (int)
 
char * command_generator (char *text, int state)
 
char * rl_filename_completion_function (const char *, int)
 
char ** rl_completion_matches (const char *, RL_PROC)
 
char * readline (const char *)
 
void add_history (char *)
 
int write_history ()
 
void using_history ()
 
int read_history (char *)
 
int history_total_bytes ()
 
char * fe_fgets_stdin_rl (const char *pr, char *s, int size)
 
int fe_init_dyn_rl ()
 
char ** singular_completion (char *text, int start, int end)
 
char * fe_fgets_stdin_fe (const char *pr, char *s, int size)
 
char * fe_fgets_stdin_emu (const char *pr, char *s, int size)
 
char * fe_fgets_stdin_drl (const char *pr, char *s, int size)
 
char * fe_fgets (const char *pr, char *s, int size)
 
char * fe_fgets_dummy (const char *, char *, int)
 

Variables

char *(* fe_fgets_stdin )(const char *pr, char *s, int size) = fe_fgets_stdin_init
 
VAR BOOLEAN using_history_called =FALSE
 
EXTERN_VAR char * rl_readline_name
 
EXTERN_VAR char * rl_line_buffer
 
EXTERN_VAR CPPFunctionrl_attempted_completion_function
 
EXTERN_VAR FILE * rl_outstream
 
char *(* fe_filename_completion_function )()
 
char *(* fe_readline )(char *)
 
VAR void(* fe_add_history )(char *)
 
VAR char ** fe_rl_readline_name
 
VAR char ** fe_rl_line_buffer
 
char **(* fe_completion_matches )(...)
 
VAR CPPFunction ** fe_rl_attempted_completion_function
 
VAR FILE ** fe_rl_outstream
 
VAR int(* fe_write_history )()
 
VAR int(* fe_history_total_bytes )()
 
VAR void(* fe_using_history )()
 
VAR int(* fe_read_history )(char *)
 

Macro Definition Documentation

◆ rl_completion_matches

#define rl_completion_matches   completion_matches

Definition at line 123 of file feread.cc.

◆ rl_filename_completion_function

#define rl_filename_completion_function   filename_completion_function

Definition at line 122 of file feread.cc.

◆ STDOUT_FILENO

#define STDOUT_FILENO   1

Definition at line 43 of file feread.cc.

◆ x_rl_completion_matches

#define x_rl_completion_matches   (*fe_completion_matches)

◆ x_rl_filename_completion_function

#define x_rl_filename_completion_function   (*fe_filename_completion_function)

◆ x_rl_line_buffer

#define x_rl_line_buffer   (*fe_rl_line_buffer)

Typedef Documentation

◆ CPPFunction

typedef char** CPPFunction()

Definition at line 130 of file feread.cc.

◆ PROC

typedef char*(* PROC) ()

Definition at line 143 of file feread.cc.

◆ RL_CPPFunction

typedef char** RL_CPPFunction(const char *, int, int)

Definition at line 145 of file feread.cc.

◆ RL_PROC

typedef char*(* RL_PROC) (const char *, int)

Definition at line 107 of file feread.cc.

Function Documentation

◆ add_history()

void add_history ( char *  )

◆ command_generator()

char* command_generator ( char *  text,
int  state 
)

Definition at line 52 of file feread.cc.

53 {
54  STATIC_VAR int list_index, len;
56  const char *name;
57 
58  /* If this is a new word to complete, initialize now. This includes
59  saving the length of TEXT for efficiency, and initializing the index
60  variable to 0. */
61  if (state==0)
62  {
63  list_index = 1;
64  len = strlen (text);
65  h=basePack->idroot;
66  }
67 
68  /* Return the next name which partially matches from the command list. */
69  while ((name = iiArithGetCmd(list_index))!=NULL)
70  {
71  list_index++;
72 
73  if (strncmp (name, text, len) == 0)
74  return (strdup(name));
75  }
76  if (len>1)
77  {
78  while (h!=NULL)
79  {
80  name=h->id;
81  h=h->next;
82  if (strncmp (name, text, len) == 0)
83  return (strdup(name));
84  }
85  }
86  /* If no names matched, then return NULL. */
87  return ((char *)NULL);
88 }
Definition: idrec.h:35
char name(const Variable &v)
Definition: factory.h:189
char * iiArithGetCmd(int)
Definition: iparith.cc:9811
#define STATIC_VAR
Definition: globaldefs.h:7
VAR package basePack
Definition: ipid.cc:58
STATIC_VAR Poly * h
Definition: janet.cc:971
#define strdup
Definition: omAllocFunc.c:18
#define NULL
Definition: omList.c:12

◆ fe_fgets()

char* fe_fgets ( const char *  pr,
char *  s,
int  size 
)

Definition at line 309 of file feread.cc.

310 {
311  if (BVERBOSE(V_PROMPT))
312  {
313  fputs(pr,stdout);
314  }
315  mflush();
316  errno=0;
317  char *line=fgets(s,size,stdin);
318  if (line!=NULL)
319  {
320  for (int i=strlen(line)-1;i>=0;i--) line[i]=line[i]&127;
321  }
322  else
323  {
324  /* NULL can mean various things... */
325  switch(errno)
326  {
327  case 0: return NULL; /*EOF */
328  case EBADF: return NULL; /* stdin got closed */
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%s\n",errsv,strerror(errsv));
334  return NULL;
335  }
336  }
337  }
338  return line;
339 }
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
int i
Definition: cfEzgcd.cc:132
const CanonicalForm int s
Definition: facAbsFact.cc:51
#define BVERBOSE(a)
Definition: options.h:34
#define V_PROMPT
Definition: options.h:53
#define mflush()
Definition: reporter.h:58

◆ fe_fgets_dummy()

char* fe_fgets_dummy ( const char *  pr,
char *  s,
int  size 
)

Definition at line 455 of file feread.cc.

456 {
457  return NULL;
458 }

◆ fe_fgets_stdin_drl()

char* fe_fgets_stdin_drl ( const char *  pr,
char *  s,
int  size 
)

Definition at line 269 of file feread.cc.

270 {
271  if (!BVERBOSE(V_PROMPT))
272  {
273  pr="";
274  }
275  mflush();
276 
277  char *line;
278  line = (*fe_readline) ((char*)pr);
279 
280  if (line==NULL)
281  return NULL;
282 
283  int l=strlen(line);
284  for (int i=l-1;i>=0;i--) line[i]=line[i]&127;
285 
286  if (*line!='\0')
287  {
288  (*fe_add_history) (line);
289  }
290  if (l>=size-1)
291  {
292  strncpy(s,line,size);
293  }
294  else
295  {
296  strncpy(s,line,l);
297  s[l]='\n';
298  s[l+1]='\0';
299  }
300  free (line);
301 
302  return s;
303 }
int l
Definition: cfEzgcd.cc:100
#define free
Definition: omAllocFunc.c:14

◆ fe_fgets_stdin_emu()

char* fe_fgets_stdin_emu ( const char *  pr,
char *  s,
int  size 
)

Definition at line 253 of file feread.cc.

254 {
255  if (!BVERBOSE(V_PROMPT))
256  {
257  pr="";
258  }
259  mflush();
260  return fe_fgets_stdin_fe(pr,s,size);
261 }
char * fe_fgets_stdin_fe(const char *pr, char *s, int size)

◆ fe_fgets_stdin_fe()

char* fe_fgets_stdin_fe ( const char *  pr,
char *  s,
int  size 
)

◆ fe_fgets_stdin_init()

static char * fe_fgets_stdin_init ( const char *  pr,
char *  s,
int  size 
)
static

Definition at line 344 of file feread.cc.

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 = (char*)"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 
368  if(isatty(fileno(stdin)))
369  {
370  /* try to read a history */
372  using_history();
373  char *p = getenv("SINGULARHIST");
374  if (p==NULL) p=SINGULARHIST_FILE;
375  if (strlen(p) != 0)
376  {
377  read_history (p);
378  }
380  return(fe_fgets_stdin_rl(pr,s,size));
381  }
382  else
383  {
385  return(fe_fgets(pr,s,size));
386  }
387 #endif
388 #ifdef HAVE_DYN_RL
389  /* do dynamic loading */
390  int res=fe_init_dyn_rl();
392  if (res!=0)
393  {
394  //if (res==1)
395  // WarnS("dynamic loading of libreadline failed");
396  //else
397  // Warn("dynamic loading failed: %d\n",res);
398  if (res!=1)
399  Warn("dynamic loading failed: %d\n",res);
400  #ifdef HAVE_FEREAD
402  #else
404  #endif
405  return fe_fgets_stdin(pr,s,size);
406  }
407  else if (isatty(STDIN_FILENO))/*and could load libreadline: */
408  {
409  /* Allow conditional parsing of the ~/.inputrc file. */
410  *fe_rl_readline_name = "Singular";
411  /* Tell the completer that we want a crack first. */
413  /* try to read a history */
414  (*fe_using_history)();
416  char *p = getenv("SINGULARHIST");
417  if (p != NULL)
418  {
419  (*fe_read_history) (p);
420  }
421 
422  /* set the output stream */
423  if(!isatty(STDOUT_FILENO))
424  {
425  #ifdef atarist
426  *fe_rl_outstream = fopen( "/dev/tty", "w" );
427  #else
428  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
429  if (fn!=NULL) *fe_rl_outstream = fopen( fn, "w" );
430  #endif
431  }
433  return fe_fgets_stdin_drl(pr,s,size);
434  }
435  else
436  {
438  return fe_fgets(pr,s,size);
439  }
440 #else
441  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
443  return(fe_fgets_stdin_emu(pr,s,size));
444  #else
446  return(fe_fgets(pr,s,size));
447  #endif
448 #endif
449 }
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int p
Definition: cfModGcd.cc:4078
#define Warn
Definition: emacs.cc:77
CanonicalForm res
Definition: facAbsFact.cc:60
char * getenv()
EXTERN_VAR CPPFunction * rl_attempted_completion_function
Definition: feread.cc:133
EXTERN_VAR FILE * rl_outstream
Definition: feread.cc:134
char * fe_fgets_stdin_emu(const char *pr, char *s, int size)
Definition: feread.cc:253
VAR BOOLEAN using_history_called
Definition: feread.cc:106
VAR char ** fe_rl_readline_name
Definition: feread.cc:166
char ** singular_completion(char *text, int start, int end)
Definition: feread.cc:178
char ** CPPFunction()
Definition: feread.cc:130
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:32
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:309
VAR CPPFunction ** fe_rl_attempted_completion_function
Definition: feread.cc:169
int read_history(char *)
char * fe_fgets_stdin_drl(const char *pr, char *s, int size)
Definition: feread.cc:269
char * fe_fgets_stdin_rl(const char *pr, char *s, int size)
#define STDOUT_FILENO
Definition: feread.cc:43
EXTERN_VAR char * rl_readline_name
Definition: feread.cc:127
void using_history()
VAR FILE ** fe_rl_outstream
Definition: feread.cc:170
int fe_init_dyn_rl()
Definition: fereadl.c:756
#define SINGULARHIST_FILE
Definition: feread.h:20
#define STDIN_FILENO
Definition: fereadl.c:52

◆ fe_fgets_stdin_rl()

char* fe_fgets_stdin_rl ( const char *  pr,
char *  s,
int  size 
)

◆ fe_init_dyn_rl()

int fe_init_dyn_rl ( )

Definition at line 756 of file fereadl.c.

757 {
758  int res=0;
759  loop
760  {
761  fe_rl_hdl=dynl_open("libreadline.so");
762  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.2");
763  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.3");
764  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.4");
765  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.5");
766  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.6");
767  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.7");
768  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.8");
769  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.9");
770  if (fe_rl_hdl==NULL) { return 1;}
771 
773  dynl_sym(fe_rl_hdl, "filename_completion_function");
774  if (fe_filename_completion_function==NULL) { res=3; break; }
775  fe_readline=dynl_sym(fe_rl_hdl,"readline");
776  if (fe_readline==NULL) { res=4; break; }
777  fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
778  if (fe_add_history==NULL) { res=5; break; }
779  fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name");
780  if (fe_rl_readline_name==NULL) { res=6; break; }
781  fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer");
782  if (fe_rl_line_buffer==NULL) { res=7; break; }
783  fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
784  if (fe_completion_matches==NULL) { res=8; break; }
786  dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
787  if (fe_rl_attempted_completion_function==NULL) { res=9; break; }
788  fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream");
789  if (fe_rl_outstream==NULL) { res=10; break; }
790  fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
791  if (fe_write_history==NULL) { res=11; break; }
792  fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
793  if (fe_history_total_bytes==NULL) { res=12; break; }
794  fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
795  if (fe_using_history==NULL) { res=13; break; }
796  fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
797  if (fe_read_history==NULL) { res=14; break; }
798  break;
799  }
801  if (res!=0) dynl_close(fe_rl_hdl);
802  else
803  {
804  char *p;
805  /* more init stuff: */
806  /* Allow conditional parsing of the ~/.inputrc file. */
807  (*fe_rl_readline_name) = "Singular";
808  /* Tell the completer that we want a crack first. */
809  (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
810  /* try to read a history */
812  (*fe_using_history)();
813  p = getenv("SINGULARHIST");
814  if (p==NULL) p=SINGULARHIST_FILE;
815  if (strlen(p) != 0)
816  {
817  (*fe_read_history) (p);
818  }
819  }
820  return res;
821 }
VAR char ** fe_rl_readline_name
Definition: fereadl.c:717
char ** singular_completion(char *text, int start, int end)
Definition: fereadl.c:736
VAR char ** fe_rl_line_buffer
Definition: fereadl.c:718
char ** CPPFunction()
Definition: fereadl.c:712
VAR int(* fe_read_history)()
Definition: fereadl.c:725
VAR void(* fe_add_history)()
Definition: fereadl.c:716
VAR int(* fe_write_history)()
Definition: fereadl.c:722
VAR void(* fe_using_history)()
Definition: fereadl.c:724
char **(* fe_completion_matches)()
Definition: fereadl.c:719
VAR CPPFunction ** fe_rl_attempted_completion_function
Definition: fereadl.c:720
VAR int(* fe_history_total_bytes)()
Definition: fereadl.c:723
VAR void * fe_rl_hdl
Definition: fereadl.c:727
char *(* fe_readline)()
Definition: fereadl.c:715
VAR FILE ** fe_rl_outstream
Definition: fereadl.c:721
char *(* fe_filename_completion_function)()
Definition: fereadl.c:714
void * dynl_sym(void *handle, const char *symbol)
Definition: mod_raw.cc:159
int dynl_close(void *handle)
Definition: mod_raw.cc:170
void * dynl_open(char *filename)
Definition: mod_raw.cc:142
#define loop
Definition: structs.h:75

◆ history_total_bytes()

int history_total_bytes ( )

◆ iiArithGetCmd()

char* iiArithGetCmd ( int  nPos)

Definition at line 9811 of file iparith.cc.

9812 {
9813  if(nPos<0) return NULL;
9814  if(nPos<(int)sArithBase.nCmdUsed)
9815  return sArithBase.sCmds[nPos].name;
9816  return NULL;
9817 }
cmdnames * sCmds
array of existing commands
Definition: iparith.cc:184
STATIC_VAR SArithBase sArithBase
Base entry for arithmetic.
Definition: iparith.cc:199
unsigned nCmdUsed
number of commands used
Definition: iparith.cc:189

◆ read_history()

int read_history ( char *  )

◆ readline()

char* readline ( const char *  )

◆ rl_completion_matches()

char** rl_completion_matches ( const char *  ,
RL_PROC   
)

◆ rl_filename_completion_function()

char* rl_filename_completion_function ( const char *  ,
int   
)

◆ singular_completion()

char** singular_completion ( char *  text,
int  start,
int  end 
)

Definition at line 178 of file feread.cc.

179 {
180  /* If this word is not in a string, then it may be a command
181  to complete. Otherwise it may be the name of a file in the current
182  directory. */
183 #ifdef HAVE_DYN_RL
184  #define x_rl_line_buffer (*fe_rl_line_buffer)
185  #define x_rl_completion_matches (*fe_completion_matches)
186  #define x_rl_filename_completion_function (*fe_filename_completion_function)
187 #else
188  #define x_rl_line_buffer rl_line_buffer
189  #define x_rl_completion_matches rl_completion_matches
190  #define x_rl_filename_completion_function rl_filename_completion_function
191 #endif
192  if ((start>0) && (x_rl_line_buffer[start-1]=='"'))
195 #undef x_rl_line_buffer
196 #undef x_rl_completion_matches
197  if (m==NULL)
198  {
199  m=(char **)malloc(2*sizeof(char*));
200  m[0]=(char *)malloc(end-start+2);
201  strncpy(m[0],text,end-start+1);
202  m[1]=NULL;
203  }
204  return m;
205 }
int m
Definition: cfEzgcd.cc:128
#define x_rl_filename_completion_function
#define x_rl_completion_matches
char *(* RL_PROC)(const char *, int)
Definition: feread.cc:107
#define x_rl_line_buffer
char * command_generator(char *text, int state)
Definition: feread.cc:52
void * malloc(size_t size)
Definition: omalloc.c:85

◆ using_history()

void using_history ( )

◆ write_history()

int write_history ( )

Variable Documentation

◆ fe_add_history

VAR void(* fe_add_history) (char *) ( char *  )

Definition at line 165 of file feread.cc.

◆ fe_completion_matches

char**(* fe_completion_matches) (...) (   ...)

Definition at line 168 of file feread.cc.

◆ fe_fgets_stdin

char*(* fe_fgets_stdin) (const char *pr, char *s, int size) ( const char *  pr,
char *  s,
int  size 
) = fe_fgets_stdin_init

Definition at line 32 of file feread.cc.

◆ fe_filename_completion_function

char*(* fe_filename_completion_function) () ( )

Definition at line 163 of file feread.cc.

◆ fe_history_total_bytes

VAR int(* fe_history_total_bytes) () ( )

Definition at line 172 of file feread.cc.

◆ fe_read_history

VAR int(* fe_read_history) (char *) ( char *  )

Definition at line 174 of file feread.cc.

◆ fe_readline

char*(* fe_readline) (char *) ( char *  )

Definition at line 164 of file feread.cc.

◆ fe_rl_attempted_completion_function

VAR CPPFunction** fe_rl_attempted_completion_function

Definition at line 169 of file feread.cc.

◆ fe_rl_line_buffer

VAR char** fe_rl_line_buffer

Definition at line 167 of file feread.cc.

◆ fe_rl_outstream

VAR FILE** fe_rl_outstream

Definition at line 170 of file feread.cc.

◆ fe_rl_readline_name

VAR char** fe_rl_readline_name

Definition at line 166 of file feread.cc.

◆ fe_using_history

VAR void(* fe_using_history) () ( )

Definition at line 173 of file feread.cc.

◆ fe_write_history

VAR int(* fe_write_history) () ( )

Definition at line 171 of file feread.cc.

◆ rl_attempted_completion_function

EXTERN_VAR CPPFunction* rl_attempted_completion_function

Definition at line 133 of file feread.cc.

◆ rl_line_buffer

EXTERN_VAR char* rl_line_buffer

Definition at line 128 of file feread.cc.

◆ rl_outstream

EXTERN_VAR FILE* rl_outstream

Definition at line 134 of file feread.cc.

◆ rl_readline_name

EXTERN_VAR char* rl_readline_name

Definition at line 127 of file feread.cc.

◆ using_history_called

VAR BOOLEAN using_history_called =FALSE

Definition at line 106 of file feread.cc.