My Project  UNKNOWN_GIT_VERSION
Macros | Typedefs | Functions | Variables
feread.cc File Reference
#include "kernel/mod2.h"
#include <errno.h>
#include "omalloc/omalloc.h"
#include "misc/options.h"
#include "kernel/oswrapper/feread.h"
#include <unistd.h>
#include "Singular/ipid.h"
#include <stdio.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
 
char * rl_readline_name
 
char * rl_line_buffer
 
CPPFunctionrl_attempted_completion_function
 
FILE * rl_outstream
 
char *(* fe_filename_completion_function )()
 
char *(* fe_readline )(char *)
 
void(* fe_add_history )(char *)
 
char ** fe_rl_readline_name
 
char ** fe_rl_line_buffer
 
char **(* fe_completion_matches )(...)
 
CPPFunction ** fe_rl_attempted_completion_function
 
FILE ** fe_rl_outstream
 
int(* fe_write_history )()
 
int(* fe_history_total_bytes )()
 
void(* fe_using_history )()
 
int(* fe_read_history )(char *)
 

Macro Definition Documentation

◆ rl_completion_matches

#define rl_completion_matches   completion_matches

Definition at line 124 of file feread.cc.

◆ rl_filename_completion_function

#define rl_filename_completion_function   filename_completion_function

Definition at line 123 of file feread.cc.

◆ STDOUT_FILENO

#define STDOUT_FILENO   1

Definition at line 45 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 131 of file feread.cc.

◆ PROC

typedef char*(* PROC) ()

Definition at line 144 of file feread.cc.

◆ RL_CPPFunction

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

Definition at line 146 of file feread.cc.

◆ RL_PROC

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

Definition at line 108 of file feread.cc.

Function Documentation

◆ add_history()

void add_history ( char *  )

◆ command_generator()

char* command_generator ( char *  text,
int  state 
)

Definition at line 54 of file feread.cc.

55 {
56  static int list_index, len;
57  static idhdl h;
58  const char *name;
59 
60  /* If this is a new word to complete, initialize now. This includes
61  saving the length of TEXT for efficiency, and initializing the index
62  variable to 0. */
63  if (state==0)
64  {
65  list_index = 1;
66  len = strlen (text);
67  h=basePack->idroot;
68  }
69 
70  /* Return the next name which partially matches from the command list. */
71  while ((name = iiArithGetCmd(list_index))!=NULL)
72  {
73  list_index++;
74 
75  if (strncmp (name, text, len) == 0)
76  return (strdup(name));
77  }
78  if (len>1)
79  {
80  while (h!=NULL)
81  {
82  name=h->id;
83  h=h->next;
84  if (strncmp (name, text, len) == 0)
85  return (strdup(name));
86  }
87  }
88  /* If no names matched, then return NULL. */
89  return ((char *)NULL);
90 }

◆ fe_fgets()

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

Definition at line 310 of file feread.cc.

311 {
312  if (BVERBOSE(V_PROMPT))
313  {
314  fputs(pr,stdout);
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 EBADF: return NULL; /* stdin got closed */
330  case EINTR: return strcpy(s,"\n"); /* CTRL-C or other signal */
331  default: /* other error */
332  {
333  int errsv = errno;
334  fprintf(stderr,"fgets() failed with errno %d\n%s\n",errsv,strerror(errsv));
335  return NULL;
336  }
337  }
338  }
339  return line;
340 }

◆ fe_fgets_dummy()

char* fe_fgets_dummy ( const char *  ,
char *  ,
int   
)

Definition at line 451 of file feread.cc.

452 {
453  return NULL;
454 }

◆ fe_fgets_stdin_drl()

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

Definition at line 270 of file feread.cc.

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 }

◆ fe_fgets_stdin_emu()

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

Definition at line 254 of file feread.cc.

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

◆ 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 345 of file feread.cc.

346 {
347 #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD)
348  /* Allow conditional parsing of the ~/.inputrc file. */
349  rl_readline_name = (char*)"Singular";
350  /* Tell the completer that we want a crack first. */
351 #ifdef USE_READLINE4
352  rl_attempted_completion_function = (rl_completion_func_t *)singular_completion;
353 #else
355 #endif
356 
357  /* set the output stream */
358  if(!isatty(STDOUT_FILENO))
359  {
360  #ifdef atarist
361  rl_outstream = fopen( "/dev/tty", "w" );
362  #else
363  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
364  if (fn!=NULL) rl_outstream = fopen( fn, "w" );
365  #endif
366  }
367 
368  if(isatty(fileno(stdin)))
369  {
370  /* try to read a history */
371  using_history();
372  char *p = getenv("SINGULARHIST");
373  if (p != NULL)
374  {
375  read_history (p);
376  }
378  return(fe_fgets_stdin_rl(pr,s,size));
379  }
380  else
381  {
383  return(fe_fgets(pr,s,size));
384  }
385 #endif
386 #ifdef HAVE_DYN_RL
387  /* do dynamic loading */
388  int res=fe_init_dyn_rl();
389  if (res!=0)
390  {
391  //if (res==1)
392  // WarnS("dynamic loading of libreadline failed");
393  //else
394  // Warn("dynamic loading failed: %d\n",res);
395  if (res!=1)
396  Warn("dynamic loading failed: %d\n",res);
397  #ifdef HAVE_FEREAD
399  #else
401  #endif
402  return fe_fgets_stdin(pr,s,size);
403  }
404  else if (isatty(STDIN_FILENO))/*and could load libreadline: */
405  {
406  /* Allow conditional parsing of the ~/.inputrc file. */
407  *fe_rl_readline_name = "Singular";
408  /* Tell the completer that we want a crack first. */
410  /* try to read a history */
411  (*fe_using_history)();
412  char *p = getenv("SINGULARHIST");
413  if (p != NULL)
414  {
415  (*fe_read_history) (p);
416  }
417 
418  /* set the output stream */
419  if(!isatty(STDOUT_FILENO))
420  {
421  #ifdef atarist
422  *fe_rl_outstream = fopen( "/dev/tty", "w" );
423  #else
424  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
425  if (fn!=NULL) *fe_rl_outstream = fopen( fn, "w" );
426  #endif
427  }
429  return fe_fgets_stdin_drl(pr,s,size);
430  }
431  else
432  {
434  return fe_fgets(pr,s,size);
435  }
436 #else
437  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
439  return(fe_fgets_stdin_emu(pr,s,size));
440  #else
442  return(fe_fgets(pr,s,size));
443  #endif
444 #endif
445 }

◆ 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 755 of file fereadl.c.

756 {
757  int res=0;
758  loop
759  {
760  fe_rl_hdl=dynl_open("libreadline.so");
761  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.2");
762  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.3");
763  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.4");
764  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.5");
765  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.6");
766  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.7");
767  if (fe_rl_hdl==NULL) { return 1;}
768 
770  dynl_sym(fe_rl_hdl, "filename_completion_function");
771  if (fe_filename_completion_function==NULL) { res=3; break; }
772  fe_readline=dynl_sym(fe_rl_hdl,"readline");
773  if (fe_readline==NULL) { res=4; break; }
774  fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
775  if (fe_add_history==NULL) { res=5; break; }
776  fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name");
777  if (fe_rl_readline_name==NULL) { res=6; break; }
778  fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer");
779  if (fe_rl_line_buffer==NULL) { res=7; break; }
780  fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
781  if (fe_completion_matches==NULL) { res=8; break; }
783  dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
784  if (fe_rl_attempted_completion_function==NULL) { res=9; break; }
785  fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream");
786  if (fe_rl_outstream==NULL) { res=10; break; }
787  fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
788  if (fe_write_history==NULL) { res=11; break; }
789  fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
790  if (fe_history_total_bytes==NULL) { res=12; break; }
791  fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
792  if (fe_using_history==NULL) { res=13; break; }
793  fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
794  if (fe_read_history==NULL) { res=14; break; }
795  break;
796  }
797  if (res!=0) dynl_close(fe_rl_hdl);
798  else
799  {
800  char *p;
801  /* more init stuff: */
802  /* Allow conditional parsing of the ~/.inputrc file. */
803  (*fe_rl_readline_name) = "Singular";
804  /* Tell the completer that we want a crack first. */
805  (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
806  /* try to read a history */
807  (*fe_using_history)();
808  p = getenv("SINGULARHIST");
809  if (p != NULL)
810  {
811  (*fe_read_history) (p);
812  }
813  }
814  return res;
815 }

◆ history_total_bytes()

int history_total_bytes ( )

◆ iiArithGetCmd()

char* iiArithGetCmd ( int  )

Definition at line 9170 of file iparith.cc.

9171 {
9172  if(nPos<0) return NULL;
9173  if(nPos<(int)sArithBase.nCmdUsed)
9174  return sArithBase.sCmds[nPos].name;
9175  return NULL;
9176 }

◆ 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 179 of file feread.cc.

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 }

◆ using_history()

void using_history ( )

◆ write_history()

int write_history ( )

Variable Documentation

◆ fe_add_history

void(* fe_add_history) (char *)

Definition at line 166 of file feread.cc.

◆ fe_completion_matches

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

Definition at line 169 of file feread.cc.

◆ fe_fgets_stdin

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

Definition at line 34 of file feread.cc.

◆ fe_filename_completion_function

char*(* fe_filename_completion_function) ()

Definition at line 164 of file feread.cc.

◆ fe_history_total_bytes

int(* fe_history_total_bytes) ()

Definition at line 173 of file feread.cc.

◆ fe_read_history

int(* fe_read_history) (char *)

Definition at line 175 of file feread.cc.

◆ fe_readline

char*(* fe_readline) (char *)

Definition at line 165 of file feread.cc.

◆ fe_rl_attempted_completion_function

CPPFunction** fe_rl_attempted_completion_function

Definition at line 170 of file feread.cc.

◆ fe_rl_line_buffer

char** fe_rl_line_buffer

Definition at line 168 of file feread.cc.

◆ fe_rl_outstream

FILE** fe_rl_outstream

Definition at line 171 of file feread.cc.

◆ fe_rl_readline_name

char** fe_rl_readline_name

Definition at line 167 of file feread.cc.

◆ fe_using_history

void(* fe_using_history) ()

Definition at line 174 of file feread.cc.

◆ fe_write_history

int(* fe_write_history) ()

Definition at line 172 of file feread.cc.

◆ rl_attempted_completion_function

CPPFunction* rl_attempted_completion_function
extern

◆ rl_line_buffer

char* rl_line_buffer
extern

◆ rl_outstream

FILE* rl_outstream
extern

◆ rl_readline_name

char* rl_readline_name
extern
dynl_close
int dynl_close(void *handle)
Definition: mod_raw.cc:178
getenv
char * getenv()
read_history
int read_history(char *)
fe_filename_completion_function
char *(* fe_filename_completion_function)()
Definition: fereadl.c:713
fe_completion_matches
char **(* fe_completion_matches)()
Definition: fereadl.c:718
STDOUT_FILENO
#define STDOUT_FILENO
Definition: feread.cc:45
fe_read_history
int(* fe_read_history)()
Definition: fereadl.c:724
fe_fgets_stdin
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:34
loop
#define loop
Definition: structs.h:78
fe_rl_line_buffer
char ** fe_rl_line_buffer
Definition: fereadl.c:717
using_history
void using_history()
fe_init_dyn_rl
int fe_init_dyn_rl()
Definition: fereadl.c:755
fe_fgets_stdin_rl
char * fe_fgets_stdin_rl(const char *pr, char *s, int size)
singular_completion
char ** singular_completion(char *text, int start, int end)
Definition: fereadl.c:735
x_rl_filename_completion_function
#define x_rl_filename_completion_function
fe_fgets
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:310
x_rl_line_buffer
#define x_rl_line_buffer
STDIN_FILENO
#define STDIN_FILENO
Definition: fereadl.c:51
i
int i
Definition: cfEzgcd.cc:125
res
CanonicalForm res
Definition: facAbsFact.cc:64
singular_completion
char ** singular_completion(char *text, int start, int end)
Definition: feread.cc:179
malloc
void * malloc(size_t size)
Definition: omalloc.c:92
fe_using_history
void(* fe_using_history)()
Definition: fereadl.c:723
h
static Poly * h
Definition: janet.cc:972
fe_rl_outstream
FILE ** fe_rl_outstream
Definition: fereadl.c:720
CPPFunction
char ** CPPFunction()
Definition: fereadl.c:711
size
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
fe_rl_readline_name
char ** fe_rl_readline_name
Definition: fereadl.c:716
free
#define free
Definition: omAllocFunc.c:12
fe_history_total_bytes
int(* fe_history_total_bytes)()
Definition: fereadl.c:722
RL_PROC
char *(* RL_PROC)(const char *, int)
Definition: feread.cc:108
fe_write_history
int(* fe_write_history)()
Definition: fereadl.c:721
fe_rl_attempted_completion_function
CPPFunction ** fe_rl_attempted_completion_function
Definition: feread.cc:170
fe_fgets_stdin_fe
char * fe_fgets_stdin_fe(const char *pr, char *s, int size)
rl_readline_name
char * rl_readline_name
SArithBase::sCmds
cmdnames * sCmds
array of existing commands
Definition: iparith.cc:182
fe_fgets_stdin_drl
char * fe_fgets_stdin_drl(const char *pr, char *s, int size)
Definition: feread.cc:270
dynl_open
void * dynl_open(char *filename)
Definition: mod_raw.cc:153
V_PROMPT
#define V_PROMPT
Definition: options.h:54
idrec
Definition: idrec.h:35
fe_rl_outstream
FILE ** fe_rl_outstream
Definition: feread.cc:171
CPPFunction
char ** CPPFunction()
Definition: feread.cc:131
fe_rl_readline_name
char ** fe_rl_readline_name
Definition: feread.cc:167
BVERBOSE
#define BVERBOSE(a)
Definition: options.h:35
x_rl_completion_matches
#define x_rl_completion_matches
iiArithGetCmd
char * iiArithGetCmd(int)
Definition: iparith.cc:9170
fe_fgets_stdin_emu
char * fe_fgets_stdin_emu(const char *pr, char *s, int size)
Definition: feread.cc:254
sArithBase
static SArithBase sArithBase
Base entry for arithmetic.
Definition: iparith.cc:197
name
char name(const Variable &v)
Definition: factory.h:180
strdup
#define strdup
Definition: omAllocFunc.c:17
dynl_sym
void * dynl_sym(void *handle, const char *symbol)
Definition: mod_raw.cc:167
fe_readline
char *(* fe_readline)()
Definition: fereadl.c:714
m
int m
Definition: cfEzgcd.cc:121
basePack
package basePack
Definition: ipid.cc:60
NULL
#define NULL
Definition: omList.c:10
mflush
#define mflush()
Definition: reporter.h:57
l
int l
Definition: cfEzgcd.cc:93
rl_outstream
FILE * rl_outstream
fe_add_history
void(* fe_add_history)()
Definition: fereadl.c:715
Warn
#define Warn
Definition: emacs.cc:77
SArithBase::nCmdUsed
unsigned nCmdUsed
number of commands used
Definition: iparith.cc:187
p
int p
Definition: cfModGcd.cc:4019
s
const CanonicalForm int s
Definition: facAbsFact.cc:55
rl_attempted_completion_function
CPPFunction * rl_attempted_completion_function
command_generator
char * command_generator(char *text, int state)
Definition: feread.cc:54
fe_rl_attempted_completion_function
CPPFunction ** fe_rl_attempted_completion_function
Definition: fereadl.c:719
fe_rl_hdl
void * fe_rl_hdl
Definition: fereadl.c:726