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

◆ rl_filename_completion_function

#define rl_filename_completion_function   filename_completion_function

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

◆ PROC

typedef char *(* PROC) ()

Definition at line 140 of file feread.cc.

◆ RL_CPPFunction

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

Definition at line 142 of file feread.cc.

◆ RL_PROC

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

Definition at line 104 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 * 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
int name
New type name for int.
Definition: templateForC.h:21

◆ fe_fgets()

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

Definition at line 306 of file feread.cc.

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

448{
449 return NULL;
450}

◆ fe_fgets_stdin_drl()

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

Definition at line 266 of file feread.cc.

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

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

342{
343#if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD)
344 /* Allow conditional parsing of the ~/.inputrc file. */
345 rl_readline_name = (char*)"Singular";
346 /* Tell the completer that we want a crack first. */
347#ifdef USE_READLINE4
349#else
351#endif
352
353 /* set the output stream */
354 if(!isatty(STDOUT_FILENO))
355 {
356 #ifdef atarist
357 rl_outstream = fopen( "/dev/tty", "w" );
358 #else
359 char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
360 if (fn!=NULL) rl_outstream = fopen( fn, "w" );
361 #endif
362 }
363
364 if(isatty(fileno(stdin)))
365 {
366 /* try to read a history */
368 char *p = getenv("SINGULARHIST");
369 if (p != NULL)
370 {
371 read_history (p);
372 }
374 return(fe_fgets_stdin_rl(pr,s,size));
375 }
376 else
377 {
379 return(fe_fgets(pr,s,size));
380 }
381#endif
382#ifdef HAVE_DYN_RL
383 /* do dynamic loading */
384 int res=fe_init_dyn_rl();
385 if (res!=0)
386 {
387 //if (res==1)
388 // WarnS("dynamic loading of libreadline failed");
389 //else
390 // Warn("dynamic loading failed: %d\n",res);
391 if (res!=1)
392 Warn("dynamic loading failed: %d\n",res);
393 #ifdef HAVE_FEREAD
395 #else
397 #endif
398 return fe_fgets_stdin(pr,s,size);
399 }
400 else if (isatty(STDIN_FILENO))/*and could load libreadline: */
401 {
402 /* Allow conditional parsing of the ~/.inputrc file. */
403 *fe_rl_readline_name = "Singular";
404 /* Tell the completer that we want a crack first. */
406 /* try to read a history */
407 (*fe_using_history)();
408 char *p = getenv("SINGULARHIST");
409 if (p != NULL)
410 {
411 (*fe_read_history) (p);
412 }
413
414 /* set the output stream */
415 if(!isatty(STDOUT_FILENO))
416 {
417 #ifdef atarist
418 *fe_rl_outstream = fopen( "/dev/tty", "w" );
419 #else
420 char *fn=ttyname(fileno(stdin));//if stdout is not a tty, maybe stdin is?
421 if (fn!=NULL) *fe_rl_outstream = fopen( fn, "w" );
422 #endif
423 }
425 return fe_fgets_stdin_drl(pr,s,size);
426 }
427 else
428 {
430 return fe_fgets(pr,s,size);
431 }
432#else
433 #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
435 return(fe_fgets_stdin_emu(pr,s,size));
436 #else
438 return(fe_fgets(pr,s,size));
439 #endif
440#endif
441}
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:130
EXTERN_VAR FILE * rl_outstream
Definition: feread.cc:131
char ** CPPFunction()
Definition: feread.cc:127
VAR char ** fe_rl_readline_name
Definition: feread.cc:163
char *(* fe_fgets_stdin)(const char *pr, char *s, int size)
Definition: feread.cc:30
VAR CPPFunction ** fe_rl_attempted_completion_function
Definition: feread.cc:166
int read_history(char *)
char ** singular_completion(char *text, int start, int end)
Definition: feread.cc:175
char * fe_fgets(const char *pr, char *s, int size)
Definition: feread.cc:306
#define STDOUT_FILENO
Definition: feread.cc:41
EXTERN_VAR char * rl_readline_name
Definition: feread.cc:124
void using_history()
char * fe_fgets_stdin_rl(const char *pr, char *s, int size)
char * fe_fgets_stdin_drl(const char *pr, char *s, int size)
Definition: feread.cc:266
char * fe_fgets_stdin_emu(const char *pr, char *s, int size)
Definition: feread.cc:250
VAR FILE ** fe_rl_outstream
Definition: feread.cc:167
int fe_init_dyn_rl()
Definition: fereadl.c:755
#define STDIN_FILENO
Definition: fereadl.c:51

◆ 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) fe_rl_hdl=dynl_open("libreadline.so.8");
768 if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.9");
769 if (fe_rl_hdl==NULL) { return 1;}
770
772 dynl_sym(fe_rl_hdl, "filename_completion_function");
773 if (fe_filename_completion_function==NULL) { res=3; break; }
774 fe_readline=dynl_sym(fe_rl_hdl,"readline");
775 if (fe_readline==NULL) { res=4; break; }
776 fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
777 if (fe_add_history==NULL) { res=5; break; }
778 fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name");
779 if (fe_rl_readline_name==NULL) { res=6; break; }
780 fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer");
781 if (fe_rl_line_buffer==NULL) { res=7; break; }
782 fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
783 if (fe_completion_matches==NULL) { res=8; break; }
785 dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
787 fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream");
788 if (fe_rl_outstream==NULL) { res=10; break; }
789 fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
790 if (fe_write_history==NULL) { res=11; break; }
791 fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
792 if (fe_history_total_bytes==NULL) { res=12; break; }
793 fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
794 if (fe_using_history==NULL) { res=13; break; }
795 fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
796 if (fe_read_history==NULL) { res=14; break; }
797 break;
798 }
799 if (res!=0) dynl_close(fe_rl_hdl);
800 else
801 {
802 char *p;
803 /* more init stuff: */
804 /* Allow conditional parsing of the ~/.inputrc file. */
805 (*fe_rl_readline_name) = "Singular";
806 /* Tell the completer that we want a crack first. */
807 (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
808 /* try to read a history */
809 (*fe_using_history)();
810 p = getenv("SINGULARHIST");
811 if (p != NULL)
812 {
813 (*fe_read_history) (p);
814 }
815 }
816 return res;
817}
char ** CPPFunction()
Definition: fereadl.c:711
VAR char ** fe_rl_readline_name
Definition: fereadl.c:716
VAR char ** fe_rl_line_buffer
Definition: fereadl.c:717
char *(* fe_readline)()
Definition: fereadl.c:714
VAR int(* fe_read_history)()
Definition: fereadl.c:724
VAR void(* fe_add_history)()
Definition: fereadl.c:715
VAR int(* fe_write_history)()
Definition: fereadl.c:721
VAR void(* fe_using_history)()
Definition: fereadl.c:723
VAR CPPFunction ** fe_rl_attempted_completion_function
Definition: fereadl.c:719
VAR int(* fe_history_total_bytes)()
Definition: fereadl.c:722
char ** singular_completion(char *text, int start, int end)
Definition: fereadl.c:735
char **(* fe_completion_matches)()
Definition: fereadl.c:718
VAR void * fe_rl_hdl
Definition: fereadl.c:726
char *(* fe_filename_completion_function)()
Definition: fereadl.c:713
VAR FILE ** fe_rl_outstream
Definition: fereadl.c:720
int dynl_close(void *handle)
Definition: mod_raw.cc:170
void * dynl_sym(void *handle, const char *symbol)
Definition: mod_raw.cc:159
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 175 of file feread.cc.

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

◆ fe_completion_matches

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

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

◆ fe_history_total_bytes

VAR int(* fe_history_total_bytes) () ( )

Definition at line 169 of file feread.cc.

◆ fe_read_history

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

Definition at line 171 of file feread.cc.

◆ fe_readline

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

Definition at line 161 of file feread.cc.

◆ fe_rl_attempted_completion_function

VAR CPPFunction** fe_rl_attempted_completion_function

Definition at line 166 of file feread.cc.

◆ fe_rl_line_buffer

VAR char** fe_rl_line_buffer

Definition at line 164 of file feread.cc.

◆ fe_rl_outstream

VAR FILE** fe_rl_outstream

Definition at line 167 of file feread.cc.

◆ fe_rl_readline_name

VAR char** fe_rl_readline_name

Definition at line 163 of file feread.cc.

◆ fe_using_history

VAR void(* fe_using_history) () ( )

Definition at line 170 of file feread.cc.

◆ fe_write_history

VAR int(* fe_write_history) () ( )

Definition at line 168 of file feread.cc.

◆ rl_attempted_completion_function

EXTERN_VAR CPPFunction* rl_attempted_completion_function

Definition at line 130 of file feread.cc.

◆ rl_line_buffer

EXTERN_VAR char* rl_line_buffer

Definition at line 125 of file feread.cc.

◆ rl_outstream

EXTERN_VAR FILE* rl_outstream

Definition at line 131 of file feread.cc.

◆ rl_readline_name

EXTERN_VAR char* rl_readline_name

Definition at line 124 of file feread.cc.