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

#define rl_completion_matches   completion_matches

Definition at line 124 of file feread.cc.

#define rl_filename_completion_function   filename_completion_function

Definition at line 123 of file feread.cc.

#define STDOUT_FILENO   1

Definition at line 44 of file feread.cc.

#define x_rl_completion_matches   (*fe_completion_matches)
#define x_rl_filename_completion_function   (*fe_filename_completion_function)
#define x_rl_line_buffer   (*fe_rl_line_buffer)

Typedef Documentation

typedef char** CPPFunction()

Definition at line 131 of file feread.cc.

typedef char*(* PROC) ()

Definition at line 144 of file feread.cc.

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

Definition at line 146 of file feread.cc.

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

Definition at line 108 of file feread.cc.

Function Documentation

void add_history ( char *  )
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:9129
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
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:55
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
char* fe_fgets_dummy ( const char *  ,
char *  ,
int   
)

Definition at line 418 of file feread.cc.

419 {
420  return NULL;
421 }
#define NULL
Definition: omList.c:10
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:55
#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
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:55
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
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 
)
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  rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
345  #endif
346  }
347 
348  /* try to read a history */
349  using_history();
350  char *p = getenv("SINGULARHIST");
351  if (p != NULL)
352  {
353  read_history (p);
354  }
356  return(fe_fgets_stdin_rl(pr,s,size));
357 #endif
358 #ifdef HAVE_DYN_RL
359  /* do dynamic loading */
360  int res=fe_init_dyn_rl();
361  if (res!=0)
362  {
363  //if (res==1)
364  // WarnS("dynamic loading of libreadline failed");
365  //else
366  // Warn("dynamic loading failed: %d\n",res);
367  if (res!=1)
368  Warn("dynamic loading failed: %d\n",res);
369  #ifdef HAVE_FEREAD
371  #else
373  #endif
374  return fe_fgets_stdin(pr,s,size);
375  }
376  else /* could load libreadline: */
377  {
378  /* Allow conditional parsing of the ~/.inputrc file. */
379  *fe_rl_readline_name = "Singular";
380  /* Tell the completer that we want a crack first. */
382  /* try to read a history */
383  (*fe_using_history)();
384  char *p = getenv("SINGULARHIST");
385  if (p != NULL)
386  {
387  (*fe_read_history) (p);
388  }
389  }
390 
391  /* set the output stream */
392  if(!isatty(STDOUT_FILENO))
393  {
394  #ifdef atarist
395  *fe_rl_outstream = fopen( "/dev/tty", "w" );
396  #else
397  *fe_rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
398  #endif
399  }
400 
402  return fe_fgets_stdin_drl(pr,s,size);
403 #else
404  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
406  return(fe_fgets_stdin_emu(pr,s,size));
407  #else
409  return(fe_fgets(pr,s,size));
410  #endif
411 #endif
412 }
#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:756
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
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
char* fe_fgets_stdin_rl ( const char *  pr,
char *  s,
int  size 
)
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) { return 1;}
769 
771  dynl_sym(fe_rl_hdl, "filename_completion_function");
772  if (fe_filename_completion_function==NULL) { res=3; break; }
773  fe_readline=dynl_sym(fe_rl_hdl,"readline");
774  if (fe_readline==NULL) { res=4; break; }
775  fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
776  if (fe_add_history==NULL) { res=5; break; }
777  fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name");
778  if (fe_rl_readline_name==NULL) { res=6; break; }
779  fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer");
780  if (fe_rl_line_buffer==NULL) { res=7; break; }
781  fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
782  if (fe_completion_matches==NULL) { res=8; break; }
784  dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
785  if (fe_rl_attempted_completion_function==NULL) { res=9; break; }
786  fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream");
787  if (fe_rl_outstream==NULL) { res=10; break; }
788  fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
789  if (fe_write_history==NULL) { res=11; break; }
790  fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
791  if (fe_history_total_bytes==NULL) { res=12; break; }
792  fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
793  if (fe_using_history==NULL) { res=13; break; }
794  fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
795  if (fe_read_history==NULL) { res=14; break; }
796  return 0;
797  }
799  if (res==0)
800  {
801  char *p;
802  /* more init stuff: */
803  /* Allow conditional parsing of the ~/.inputrc file. */
804  (*fe_rl_readline_name) = "Singular";
805  /* Tell the completer that we want a crack first. */
806  (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
807  /* try to read a history */
808  (*fe_using_history)();
809  p = getenv("SINGULARHIST");
810  if (p != NULL)
811  {
812  (*fe_read_history) (p);
813  }
814  }
815  return res;
816 }
void * fe_rl_hdl
Definition: fereadl.c:727
int(* fe_read_history)()
Definition: fereadl.c:725
loop
Definition: myNF.cc:98
char *(* fe_filename_completion_function)()
Definition: fereadl.c:714
return P p
Definition: myNF.cc:203
char ** singular_completion(char *text, int start, int end)
Definition: fereadl.c:736
char ** fe_rl_line_buffer
Definition: fereadl.c:718
char * getenv()
void * dynl_sym(void *handle, const char *symbol)
Definition: mod_raw.cc:167
FILE ** fe_rl_outstream
Definition: fereadl.c:721
char **(* fe_completion_matches)()
Definition: fereadl.c:719
void * dynl_open(char *filename)
Definition: mod_raw.cc:153
poly res
Definition: myNF.cc:322
void(* fe_using_history)()
Definition: fereadl.c:724
char ** CPPFunction()
Definition: fereadl.c:712
char ** fe_rl_readline_name
Definition: fereadl.c:717
int(* fe_history_total_bytes)()
Definition: fereadl.c:723
void(* fe_add_history)()
Definition: fereadl.c:716
int dynl_close(void *handle)
Definition: mod_raw.cc:178
#define NULL
Definition: omList.c:10
int(* fe_write_history)()
Definition: fereadl.c:722
CPPFunction ** fe_rl_attempted_completion_function
Definition: fereadl.c:720
char *(* fe_readline)()
Definition: fereadl.c:715
int history_total_bytes ( )
char* iiArithGetCmd ( int  )

Definition at line 9129 of file iparith.cc.

9130 {
9131  if(nPos<0) return NULL;
9132  if(nPos<sArithBase.nCmdUsed)
9133  return sArithBase.sCmds[nPos].name;
9134  return NULL;
9135 }
int nCmdUsed
number of commands used
Definition: iparith.cc:198
#define NULL
Definition: omList.c:10
static SArithBase sArithBase
Base entry for arithmetic.
Definition: iparith.cc:208
cmdnames * sCmds
array of existing commands
Definition: iparith.cc:193
int read_history ( char *  )
char* readline ( const char *  )
char** rl_completion_matches ( const char *  ,
RL_PROC   
)
char* rl_filename_completion_function ( const char *  ,
int   
)
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
void using_history ( )
int write_history ( )

Variable Documentation

void(* fe_add_history) (char *)

Definition at line 166 of file feread.cc.

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

Definition at line 169 of file feread.cc.

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

Definition at line 33 of file feread.cc.

char*(* fe_filename_completion_function) ()

Definition at line 164 of file feread.cc.

int(* fe_history_total_bytes) ()

Definition at line 173 of file feread.cc.

int(* fe_read_history) (char *)

Definition at line 175 of file feread.cc.

char*(* fe_readline) (char *)

Definition at line 165 of file feread.cc.

CPPFunction** fe_rl_attempted_completion_function

Definition at line 170 of file feread.cc.

char** fe_rl_line_buffer

Definition at line 168 of file feread.cc.

FILE** fe_rl_outstream

Definition at line 171 of file feread.cc.

char** fe_rl_readline_name

Definition at line 167 of file feread.cc.

void(* fe_using_history) ()

Definition at line 174 of file feread.cc.

int(* fe_write_history) ()

Definition at line 172 of file feread.cc.

CPPFunction* rl_attempted_completion_function
char* rl_line_buffer
FILE* rl_outstream
char* rl_readline_name