Macros | Typedefs | Functions | Variables
feread.cc File Reference
#include <kernel/mod2.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>
#include <sys/errno.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 44 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 53 of file feread.cc.

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 }
Definition: idrec.h:34
char * iiArithGetCmd(int)
Definition: iparith.cc:8991
idhdl next
Definition: idrec.h:38
char name(const Variable &v)
Definition: factory.h:178
#define strdup
Definition: omAllocFunc.c:17
#define NULL
Definition: omList.c:10
package basePack
Definition: ipid.cc:64
const char * id
Definition: idrec.h:39
static Poly * h
Definition: janet.cc:978

§ 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  fprintf(stdout,"%s",pr);
315  }
316  mflush();
317  char *line=fgets(s,size,stdin);
318  if (line!=NULL)
319  for (int i=strlen(line)-1;i>=0;i--) line[i]=line[i]&127;
320  return line;
321 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
#define mflush()
Definition: reporter.h:57
int i
Definition: cfEzgcd.cc:123
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
#define BVERBOSE(a)
Definition: options.h:33
#define V_PROMPT
Definition: options.h:52
#define NULL
Definition: omList.c:10

§ fe_fgets_dummy()

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

Definition at line 432 of file feread.cc.

433 {
434  return NULL;
435 }
#define NULL
Definition: omList.c:10

§ 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 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
#define mflush()
Definition: reporter.h:57
#define free
Definition: omAllocFunc.c:12
int i
Definition: cfEzgcd.cc:123
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
#define BVERBOSE(a)
Definition: options.h:33
#define V_PROMPT
Definition: options.h:52
#define NULL
Definition: omList.c:10
int l
Definition: cfEzgcd.cc:94

§ 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 }
const CanonicalForm int s
Definition: facAbsFact.cc:55
char * fe_fgets_stdin_fe(const char *pr, char *s, int size)
#define mflush()
Definition: reporter.h:57
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
#define BVERBOSE(a)
Definition: options.h:33
#define V_PROMPT
Definition: options.h:52

§ 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 326 of file feread.cc.

327 {
328 #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD)
329  /* Allow conditional parsing of the ~/.inputrc file. */
330  rl_readline_name = "Singular";
331  /* Tell the completer that we want a crack first. */
332 #ifdef USE_READLINE4
333  rl_attempted_completion_function = (rl_completion_func_t *)singular_completion;
334 #else
336 #endif
337 
338  /* set the output stream */
339  if(!isatty(STDOUT_FILENO))
340  {
341  #ifdef atarist
342  rl_outstream = fopen( "/dev/tty", "w" );
343  #else
344  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
345  if (fn!=NULL) rl_outstream = fopen( fn, "w" );
346  #endif
347  }
348 
349  if(isatty(fileno(stdin)))
350  {
351  /* try to read a history */
352  using_history();
353  char *p = getenv("SINGULARHIST");
354  if (p != NULL)
355  {
356  read_history (p);
357  }
359  return(fe_fgets_stdin_rl(pr,s,size));
360  }
361  else
362  {
364  return(fe_fgets(pr,s,size));
365  }
366 #endif
367 #ifdef HAVE_DYN_RL
368  /* do dynamic loading */
369  int res=fe_init_dyn_rl();
370  if (res!=0)
371  {
372  //if (res==1)
373  // WarnS("dynamic loading of libreadline failed");
374  //else
375  // Warn("dynamic loading failed: %d\n",res);
376  if (res!=1)
377  Warn("dynamic loading failed: %d\n",res);
378  #ifdef HAVE_FEREAD
380  #else
382  #endif
383  return fe_fgets_stdin(pr,s,size);
384  }
385  else if (isatty(STDIN_FILENO))/*and could load libreadline: */
386  {
387  /* Allow conditional parsing of the ~/.inputrc file. */
388  *fe_rl_readline_name = "Singular";
389  /* Tell the completer that we want a crack first. */
391  /* try to read a history */
392  (*fe_using_history)();
393  char *p = getenv("SINGULARHIST");
394  if (p != NULL)
395  {
396  (*fe_read_history) (p);
397  }
398 
399  /* set the output stream */
400  if(!isatty(STDOUT_FILENO))
401  {
402  #ifdef atarist
403  *fe_rl_outstream = fopen( "/dev/tty", "w" );
404  #else
405  char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
406  if (fn!=NULL) *fe_rl_outstream = fopen( fn, "w" );
407  #endif
408  }
410  return fe_fgets_stdin_drl(pr,s,size);
411  }
412  else
413  {
415  return fe_fgets(pr,s,size);
416  }
417 #else
418  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
420  return(fe_fgets_stdin_emu(pr,s,size));
421  #else
423  return(fe_fgets(pr,s,size));
424  #endif
425 #endif
426 }
#define STDOUT_FILENO
Definition: feread.cc:44
FILE * rl_outstream
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
CPPFunction * rl_attempted_completion_function
return P p
Definition: myNF.cc:203
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:310
#define STDIN_FILENO
Definition: fereadl.c:54
char * getenv()
int read_history(char *)
poly res
Definition: myNF.cc:322
char ** CPPFunction()
Definition: feread.cc:131
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
void using_history()
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
#define NULL
Definition: omList.c:10
char * rl_readline_name
CPPFunction ** fe_rl_attempted_completion_function
Definition: feread.cc:170
FILE ** fe_rl_outstream
Definition: feread.cc:171
char * fe_fgets_stdin_drl(const char *pr, char *s, int size)
Definition: feread.cc:270
char ** fe_rl_readline_name
Definition: feread.cc:167
#define Warn
Definition: emacs.cc:80

§ 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 766 of file fereadl.c.

767 {
768  int res=0;
769  loop
770  {
771  fe_rl_hdl=dynl_open("libreadline.so");
772  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.2");
773  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.3");
774  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.4");
775  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.5");
776  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.6");
777  if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.7");
778  if (fe_rl_hdl==NULL) { return 1;}
779 
781  dynl_sym(fe_rl_hdl, "filename_completion_function");
782  if (fe_filename_completion_function==NULL) { res=3; break; }
783  fe_readline=dynl_sym(fe_rl_hdl,"readline");
784  if (fe_readline==NULL) { res=4; break; }
785  fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
786  if (fe_add_history==NULL) { res=5; break; }
787  fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name");
788  if (fe_rl_readline_name==NULL) { res=6; break; }
789  fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer");
790  if (fe_rl_line_buffer==NULL) { res=7; break; }
791  fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
792  if (fe_completion_matches==NULL) { res=8; break; }
794  dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
795  if (fe_rl_attempted_completion_function==NULL) { res=9; break; }
796  fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream");
797  if (fe_rl_outstream==NULL) { res=10; break; }
798  fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
799  if (fe_write_history==NULL) { res=11; break; }
800  fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
801  if (fe_history_total_bytes==NULL) { res=12; break; }
802  fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
803  if (fe_using_history==NULL) { res=13; break; }
804  fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
805  if (fe_read_history==NULL) { res=14; break; }
806  break;
807  }
808  if (res!=0) dynl_close(fe_rl_hdl);
809  else
810  {
811  char *p;
812  /* more init stuff: */
813  /* Allow conditional parsing of the ~/.inputrc file. */
814  (*fe_rl_readline_name) = "Singular";
815  /* Tell the completer that we want a crack first. */
816  (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
817  /* try to read a history */
818  (*fe_using_history)();
819  p = getenv("SINGULARHIST");
820  if (p != NULL)
821  {
822  (*fe_read_history) (p);
823  }
824  }
825  return res;
826 }
void * fe_rl_hdl
Definition: fereadl.c:737
int(* fe_read_history)()
Definition: fereadl.c:735
loop
Definition: myNF.cc:98
char *(* fe_filename_completion_function)()
Definition: fereadl.c:724
return P p
Definition: myNF.cc:203
char ** singular_completion(char *text, int start, int end)
Definition: fereadl.c:746
char ** fe_rl_line_buffer
Definition: fereadl.c:728
char * getenv()
void * dynl_sym(void *handle, const char *symbol)
Definition: mod_raw.cc:167
FILE ** fe_rl_outstream
Definition: fereadl.c:731
char **(* fe_completion_matches)()
Definition: fereadl.c:729
void * dynl_open(char *filename)
Definition: mod_raw.cc:153
poly res
Definition: myNF.cc:322
void(* fe_using_history)()
Definition: fereadl.c:734
char ** CPPFunction()
Definition: fereadl.c:722
char ** fe_rl_readline_name
Definition: fereadl.c:727
int(* fe_history_total_bytes)()
Definition: fereadl.c:733
void(* fe_add_history)()
Definition: fereadl.c:726
int dynl_close(void *handle)
Definition: mod_raw.cc:178
#define NULL
Definition: omList.c:10
int(* fe_write_history)()
Definition: fereadl.c:732
CPPFunction ** fe_rl_attempted_completion_function
Definition: fereadl.c:730
char *(* fe_readline)()
Definition: fereadl.c:725

§ history_total_bytes()

int history_total_bytes ( )

§ iiArithGetCmd()

char* iiArithGetCmd ( int  )

Definition at line 8991 of file iparith.cc.

8992 {
8993  if(nPos<0) return NULL;
8994  if(nPos<(int)sArithBase.nCmdUsed)
8995  return sArithBase.sCmds[nPos].name;
8996  return NULL;
8997 }
#define NULL
Definition: omList.c:10
unsigned nCmdUsed
number of commands used
Definition: iparith.cc:196
static SArithBase sArithBase
Base entry for arithmetic.
Definition: iparith.cc:206
cmdnames * sCmds
array of existing commands
Definition: iparith.cc:191

§ 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 }
char * command_generator(char *text, int state)
Definition: feread.cc:53
#define x_rl_filename_completion_function
void * malloc(size_t size)
Definition: omalloc.c:92
int m
Definition: cfEzgcd.cc:119
#define x_rl_line_buffer
#define NULL
Definition: omList.c:10
char *(* RL_PROC)(const char *, int)
Definition: feread.cc:108
#define x_rl_completion_matches

§ 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 33 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

§ rl_line_buffer

char* rl_line_buffer

§ rl_outstream

FILE* rl_outstream

§ rl_readline_name

char* rl_readline_name