My Project
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
 
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 121 of file feread.cc.

◆ rl_filename_completion_function

#define rl_filename_completion_function   filename_completion_function

Definition at line 120 of file feread.cc.

◆ STDOUT_FILENO

#define STDOUT_FILENO   1

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

◆ PROC

typedef char*(* PROC) ()

Definition at line 141 of file feread.cc.

◆ RL_CPPFunction

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

Definition at line 143 of file feread.cc.

◆ RL_PROC

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

Definition at line 105 of file feread.cc.

Function Documentation

◆ add_history()

void add_history ( char *  )

◆ command_generator()

char* command_generator ( char *  text,
int  state 
)

Definition at line 50 of file feread.cc.

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

308 {
309  if (BVERBOSE(V_PROMPT))
310  {
311  fputs(pr,stdout);
312  }
313  mflush();
314  errno=0;
315  char *line=fgets(s,size,stdin);
316  if (line!=NULL)
317  {
318  for (int i=strlen(line)-1;i>=0;i--) line[i]=line[i]&127;
319  }
320  else
321  {
322  /* NULL can mean various things... */
323  switch(errno)
324  {
325  case 0: return NULL; /*EOF */
326  case EBADF: return NULL; /* stdin got closed */
327  case EINTR: return strcpy(s,"\n"); /* CTRL-C or other signal */
328  default: /* other error */
329  {
330  int errsv = errno;
331  fprintf(stderr,"fgets() failed with errno %d\n%s\n",errsv,strerror(errsv));
332  return NULL;
333  }
334  }
335  }
336  return line;
337 }
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 453 of file feread.cc.

454 {
455  return NULL;
456 }

◆ fe_fgets_stdin_drl()

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

Definition at line 267 of file feread.cc.

268 {
269  if (!BVERBOSE(V_PROMPT))
270  {
271  pr="";
272  }
273  mflush();
274 
275  char *line;
276  line = (*fe_readline) ((char*)pr);
277 
278  if (line==NULL)
279  return NULL;
280 
281  int l=strlen(line);
282  for (int i=l-1;i>=0;i--) line[i]=line[i]&127;
283 
284  if (*line!='\0')
285  {
286  (*fe_add_history) (line);
287  }
288  if (l>=size-1)
289  {
290  strncpy(s,line,size);
291  }
292  else
293  {
294  strncpy(s,line,l);
295  s[l]='\n';
296  s[l+1]='\0';
297  }
298  free (line);
299 
300  return s;
301 }
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 251 of file feread.cc.

252 {
253  if (!BVERBOSE(V_PROMPT))
254  {
255  pr="";
256  }
257  mflush();
258  return fe_fgets_stdin_fe(pr,s,size);
259 }
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 342 of file feread.cc.

343 {
344 #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD)
345  /* Allow conditional parsing of the ~/.inputrc file. */
346  rl_readline_name = (char*)"Singular";
347  /* Tell the completer that we want a crack first. */
348 #ifdef USE_READLINE4
349  rl_attempted_completion_function = (rl_completion_func_t *)singular_completion;
350 #else
352 #endif
353 
354  /* set the output stream */
355  if(!isatty(STDOUT_FILENO))
356  {
357  #ifdef atarist
358  rl_outstream = fopen( "/dev/tty", "w" );
359  #else
360  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
361  if (fn!=NULL) rl_outstream = fopen( fn, "w" );
362  #endif
363  }
364 
366  if(isatty(fileno(stdin)))
367  {
368  /* try to read a history */
370  using_history();
371  char *p = getenv("SINGULARHIST");
372  if (p==NULL) p=SINGULARHIST_FILE;
373  if (strlen(p) != 0)
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();
390  if (res!=0)
391  {
392  //if (res==1)
393  // WarnS("dynamic loading of libreadline failed");
394  //else
395  // Warn("dynamic loading failed: %d\n",res);
396  if (res!=1)
397  Warn("dynamic loading failed: %d\n",res);
398  #ifdef HAVE_FEREAD
400  #else
402  #endif
403  return fe_fgets_stdin(pr,s,size);
404  }
405  else if (isatty(STDIN_FILENO))/*and could load libreadline: */
406  {
407  /* Allow conditional parsing of the ~/.inputrc file. */
408  *fe_rl_readline_name = "Singular";
409  /* Tell the completer that we want a crack first. */
411  /* try to read a history */
412  (*fe_using_history)();
414  char *p = getenv("SINGULARHIST");
415  if (p != NULL)
416  {
417  (*fe_read_history) (p);
418  }
419 
420  /* set the output stream */
421  if(!isatty(STDOUT_FILENO))
422  {
423  #ifdef atarist
424  *fe_rl_outstream = fopen( "/dev/tty", "w" );
425  #else
426  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
427  if (fn!=NULL) *fe_rl_outstream = fopen( fn, "w" );
428  #endif
429  }
431  return fe_fgets_stdin_drl(pr,s,size);
432  }
433  else
434  {
436  return fe_fgets(pr,s,size);
437  }
438 #else
439  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
441  return(fe_fgets_stdin_emu(pr,s,size));
442  #else
444  return(fe_fgets(pr,s,size));
445  #endif
446 #endif
447 }
#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:131
EXTERN_VAR FILE * rl_outstream
Definition: feread.cc:132
char * fe_fgets_stdin_emu(const char *pr, char *s, int size)
Definition: feread.cc:251
VAR BOOLEAN using_history_called
Definition: feread.cc:104
VAR char ** fe_rl_readline_name
Definition: feread.cc:164
char ** singular_completion(char *text, int start, int end)
Definition: feread.cc:176
char ** CPPFunction()
Definition: feread.cc:128
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:30
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:307
VAR CPPFunction ** fe_rl_attempted_completion_function
Definition: feread.cc:167
int read_history(char *)
char * fe_fgets_stdin_drl(const char *pr, char *s, int size)
Definition: feread.cc:267
char * fe_fgets_stdin_rl(const char *pr, char *s, int size)
#define STDOUT_FILENO
Definition: feread.cc:41
EXTERN_VAR char * rl_readline_name
Definition: feread.cc:125
void using_history()
VAR FILE ** fe_rl_outstream
Definition: feread.cc:168
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 9779 of file iparith.cc.

9780 {
9781  if(nPos<0) return NULL;
9782  if(nPos<(int)sArithBase.nCmdUsed)
9783  return sArithBase.sCmds[nPos].name;
9784  return NULL;
9785 }
cmdnames * sCmds
array of existing commands
Definition: iparith.cc:183
STATIC_VAR SArithBase sArithBase
Base entry for arithmetic.
Definition: iparith.cc:198
unsigned nCmdUsed
number of commands used
Definition: iparith.cc:188

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

177 {
178  /* If this word is not in a string, then it may be a command
179  to complete. Otherwise it may be the name of a file in the current
180  directory. */
181 #ifdef HAVE_DYN_RL
182  #define x_rl_line_buffer (*fe_rl_line_buffer)
183  #define x_rl_completion_matches (*fe_completion_matches)
184  #define x_rl_filename_completion_function (*fe_filename_completion_function)
185 #else
186  #define x_rl_line_buffer rl_line_buffer
187  #define x_rl_completion_matches rl_completion_matches
188  #define x_rl_filename_completion_function rl_filename_completion_function
189 #endif
190  if ((start>0) && (x_rl_line_buffer[start-1]=='"'))
193 #undef x_rl_line_buffer
194 #undef x_rl_completion_matches
195  if (m==NULL)
196  {
197  m=(char **)malloc(2*sizeof(char*));
198  m[0]=(char *)malloc(end-start+2);
199  strncpy(m[0],text,end-start+1);
200  m[1]=NULL;
201  }
202  return m;
203 }
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:105
#define x_rl_line_buffer
char * command_generator(char *text, int state)
Definition: feread.cc:50
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 163 of file feread.cc.

◆ fe_completion_matches

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

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

◆ fe_filename_completion_function

char*(* fe_filename_completion_function) () ( )

Definition at line 161 of file feread.cc.

◆ fe_history_total_bytes

VAR int(* fe_history_total_bytes) () ( )

Definition at line 170 of file feread.cc.

◆ fe_read_history

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

Definition at line 172 of file feread.cc.

◆ fe_readline

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

Definition at line 162 of file feread.cc.

◆ fe_rl_attempted_completion_function

VAR CPPFunction** fe_rl_attempted_completion_function

Definition at line 167 of file feread.cc.

◆ fe_rl_line_buffer

VAR char** fe_rl_line_buffer

Definition at line 165 of file feread.cc.

◆ fe_rl_outstream

VAR FILE** fe_rl_outstream

Definition at line 168 of file feread.cc.

◆ fe_rl_readline_name

VAR char** fe_rl_readline_name

Definition at line 164 of file feread.cc.

◆ fe_using_history

VAR void(* fe_using_history) () ( )

Definition at line 171 of file feread.cc.

◆ fe_write_history

VAR int(* fe_write_history) () ( )

Definition at line 169 of file feread.cc.

◆ rl_attempted_completion_function

EXTERN_VAR CPPFunction* rl_attempted_completion_function

Definition at line 131 of file feread.cc.

◆ rl_line_buffer

EXTERN_VAR char* rl_line_buffer

Definition at line 126 of file feread.cc.

◆ rl_outstream

EXTERN_VAR FILE* rl_outstream

Definition at line 132 of file feread.cc.

◆ rl_readline_name

EXTERN_VAR char* rl_readline_name

Definition at line 125 of file feread.cc.

◆ using_history_called

VAR BOOLEAN using_history_called =FALSE

Definition at line 104 of file feread.cc.