source: git/kernel/feread.cc @ 16f511

spielwiese
Last change on this file since 16f511 was 16f511, checked in by Oleksandr Motsak <motsak@…>, 11 years ago
Fixed the usage of "config.h" (if defined HAVE_CONFIG_H)
  • Property mode set to 100644
File size: 10.8 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: input from ttys, simulating fgets
6*/
7
8#ifdef HAVE_CONFIG_H
9#include "config.h"
10#endif /* HAVE_CONFIG_H */
11#include <kernel/mod2.h>
12
13// ----------------------------------------
14// system settings:
15
16#undef USE_READLINE4
17
18//----------------------------------------
19#ifdef ix86_Win
20#define READLINE_STATIC
21#endif
22#include <kernel/febase.h>
23#include <omalloc/omalloc.h>
24#include <misc/options.h>
25
26#ifdef HAVE_STATIC
27#undef HAVE_DYN_RL
28#endif
29
30#if defined(HAVE_DYN_RL)
31#include <unistd.h>
32#endif
33
34static char * fe_fgets_stdin_init(const char *pr,char *s, int size);
35char * (*fe_fgets_stdin)(const char *pr,char *s, int size)
36 = fe_fgets_stdin_init;
37
38extern char *iiArithGetCmd(int);
39
40/* ===================================================================*/
41/* =                   static/dymanic readline                      = */
42/* ===================================================================*/
43#if defined(HAVE_READLINE) || defined(HAVE_DYN_RL) || defined(HAVE_LIBREADLINE)
44
45#ifndef STDOUT_FILENO
46#define STDOUT_FILENO 1
47#endif
48
49/* Generator function for command completion.  STATE lets us know whether
50*   to start from scratch; without any state (i.e. STATE == 0), then we
51*   start at the top of the list.
52*/
53extern "C"
54char *command_generator (char *text, int state)
55{
56  static int list_index, len;
57  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  }
67
68  /* Return the next name which partially matches from the command list. */
69  while ((name = iiArithGetCmd(list_index))!=NULL)
70  {
71    list_index++;
72
73    if (strncmp (name, text, len) == 0)
74      return (strdup(name));
75  }
76
77  /* If no names matched, then return NULL. */
78  return ((char *)NULL);
79}
80#endif
81
82/* ===================================================================*/
83/* =                      static readline                           = */
84/* ===================================================================*/
85/* some procedure are shared with "dynamic readline" */
86#if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE) || defined(HAVE_DYN_RL))
87#include <unistd.h>
88#include <stdio.h>
89#include <stdlib.h>
90#include <sys/types.h>
91#include <sys/file.h>
92#include <sys/stat.h>
93#include <sys/errno.h>
94
95// #undef READLINE_READLINE_H_OK
96
97extern "C" {
98  typedef char * (*RL_PROC)(const char*,int);
99  #ifdef READLINE_READLINE_H_OK
100    #include <readline/readline.h>
101    #ifdef HAVE_READLINE_HISTORY_H
102      #include <readline/history.h>
103    #endif
104  #endif
105
106  #ifdef RL_VERSION_MAJOR
107    #if (RL_VERSION_MAJOR >= 4)
108      #define USE_READLINE4
109    #endif
110  #endif
111
112  #ifndef USE_READLINE4
113    #define rl_filename_completion_function filename_completion_function
114    #define rl_completion_matches           completion_matches
115  #endif
116  #ifndef READLINE_READLINE_H_OK 
117    /* declare everything we need explicitely and do not rely on includes */
118    extern char * rl_readline_name;
119    extern char *rl_line_buffer;
120    char *rl_filename_completion_function(const char*, int);
121    typedef char **CPPFunction ();
122
123    extern char ** rl_completion_matches (const char*, RL_PROC);
124    extern CPPFunction * rl_attempted_completion_function;
125    extern FILE * rl_outstream;
126    extern char * readline (const char *);
127    extern void add_history (char *);
128    extern int write_history ();
129    extern void using_history();
130    extern int read_history(char *);
131    extern int history_total_bytes();
132  #endif /* READLINE_READLINE_H_OK */
133
134  typedef char * (*PROC)();
135
136  typedef char **RL_CPPFunction (const char*, int,int);
137}
138
139
140char * fe_fgets_stdin_rl(const char *pr,char *s, int size);
141
142/* Tell the GNU Readline library how to complete.  We want to try to complete
143   on command names  or on filenames if it is preceded by " */
144
145/* Attempt to complete on the contents of TEXT.  START and END show the
146*   region of TEXT that contains the word to complete.  We can use the
147*   entire line in case we want to do some simple parsing.  Return the
148*   array of matches, or NULL if there aren't any.
149*/
150#if defined(HAVE_DYN_RL)
151extern "C" 
152{
153  int fe_init_dyn_rl();
154  char *(*fe_filename_completion_function)(); /* 3 */
155  char *(* fe_readline) (char *);             /* 4 */
156  void (*fe_add_history) (char *);            /* 5 */
157  char ** fe_rl_readline_name;                /* 6 */
158  char **fe_rl_line_buffer;                   /* 7 */
159  char **(*fe_completion_matches)(...);          /* 8 */
160  CPPFunction **fe_rl_attempted_completion_function; /* 9 */
161  FILE ** fe_rl_outstream;                    /* 10 */
162  int (*fe_write_history) ();                 /* 11 */
163  int (*fe_history_total_bytes) ();           /* 12 */
164  void (*fe_using_history) ();                /* 13 */
165  int (*fe_read_history) (char *);            /* 14 */
166
167}
168#endif
169char ** singular_completion (char *text, int start, int end)
170{
171  /* If this word is not in a string, then it may be a command
172     to complete.  Otherwise it may be the name of a file in the current
173     directory. */
174#ifdef HAVE_DYN_RL
175  #define x_rl_line_buffer (*fe_rl_line_buffer)
176  #define x_rl_completion_matches (*fe_completion_matches)
177  #define x_rl_filename_completion_function (*fe_filename_completion_function)
178#else
179  #define x_rl_line_buffer rl_line_buffer
180  #define x_rl_completion_matches rl_completion_matches
181  #define x_rl_filename_completion_function rl_filename_completion_function
182#endif
183  if (x_rl_line_buffer[start-1]=='"')
184    return x_rl_completion_matches (text, (RL_PROC)x_rl_filename_completion_function);
185  char **m=x_rl_completion_matches (text, (RL_PROC)command_generator);
186#undef x_rl_line_buffer
187#undef x_rl_completion_matches
188  if (m==NULL)
189  {
190    m=(char **)malloc(2*sizeof(char*));
191    m[0]=(char *)malloc(end-start+2);
192    strncpy(m[0],text,end-start+1);
193    m[1]=NULL;
194  }
195  return m;
196}
197
198#ifndef HAVE_DYN_RL
199char * fe_fgets_stdin_rl(const char *pr,char *s, int size)
200{
201  if (!BVERBOSE(V_PROMPT))
202  {
203    pr="";
204  }
205  mflush();
206
207  char *line;
208  line = readline (pr);
209
210  if (line==NULL)
211    return NULL;
212
213  if (*line!='\0')
214  {
215    add_history (line);
216  }
217  int l=strlen(line);
218  if (l>=size-1)
219  {
220    strncpy(s,line,size);
221  }
222  else
223  {
224    strncpy(s,line,l);
225    s[l]='\n';
226    s[l+1]='\0';
227  }
228  free (line);
229
230  return s;
231}
232#endif
233#endif
234
235/* ===================================================================*/
236/* =                    emulated readline                           = */
237/* ===================================================================*/
238#if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
239extern "C" {
240char * fe_fgets_stdin_fe(const char *pr,char *s, int size);
241}
242char * fe_fgets_stdin_emu(const char *pr,char *s, int size)
243{
244  if (!BVERBOSE(V_PROMPT))
245  {
246    pr="";
247  }
248  mflush();
249  return fe_fgets_stdin_fe(pr,s,size);
250}
251#endif
252
253/* ===================================================================*/
254/* =                     dynamic readline                           = */
255/* ===================================================================*/
256/* some procedure are shared with "static readline" */
257#if defined(HAVE_DYN_RL)
258char * fe_fgets_stdin_drl(const char *pr,char *s, int size)
259{
260  if (!BVERBOSE(V_PROMPT))
261  {
262    pr="";
263  }
264  mflush();
265
266  char *line;
267  line = (*fe_readline) ((char*)pr);
268
269  if (line==NULL)
270    return NULL;
271
272  if (*line!='\0')
273  {
274    (*fe_add_history) (line);
275  }
276  int l=strlen(line);
277  if (l>=size-1)
278  {
279    strncpy(s,line,size);
280  }
281  else
282  {
283    strncpy(s,line,l);
284    s[l]='\n';
285    s[l+1]='\0';
286  }
287  free (line);
288
289  return s;
290}
291#endif
292
293/* ===================================================================*/
294/* =                        fgets                                   = */
295/* ===================================================================*/
296char * fe_fgets(const char *pr,char *s, int size)
297{
298  if (BVERBOSE(V_PROMPT))
299  {
300    fprintf(stdout,"%s",pr);
301  }
302  mflush();
303  return fgets(s,size,stdin);
304}
305
306/* ===================================================================*/
307/* =       init for static rl, dyn. rl, emu. rl                     = */
308/* ===================================================================*/
309static char * fe_fgets_stdin_init(const char *pr,char *s, int size)
310{
311#if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD)
312  /* Allow conditional parsing of the ~/.inputrc file. */
313  rl_readline_name = "Singular";
314  /* Tell the completer that we want a crack first. */
315#ifdef USE_READLINE4
316  rl_attempted_completion_function = (rl_completion_func_t *)singular_completion;
317#else
318  rl_attempted_completion_function = (CPPFunction *)singular_completion;
319#endif
320
321  /* set the output stream */
322  if(!isatty(STDOUT_FILENO))
323  {
324    #ifdef atarist
325      rl_outstream = fopen( "/dev/tty", "w" );
326    #else
327      rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
328    #endif
329  }
330
331  /* try to read a history */
332  using_history();
333  char *p = getenv("SINGULARHIST");
334  if (p != NULL)
335  {
336    read_history (p);
337  }
338  fe_fgets_stdin=fe_fgets_stdin_rl;
339  return(fe_fgets_stdin_rl(pr,s,size));
340#endif
341#ifdef HAVE_DYN_RL
342  /* do dynamic loading */
343  int res=fe_init_dyn_rl();
344  if (res!=0)
345  {
346    //if (res==1)
347    //  WarnS("dynamic loading of libreadline failed");
348    //else
349    //  Warn("dynamic loading failed: %d\n",res);
350    if (res!=1)
351      Warn("dynamic loading failed: %d\n",res);
352    #ifdef HAVE_FEREAD
353    fe_fgets_stdin=fe_fgets_stdin_emu;
354    #else
355    fe_fgets_stdin=fe_fgets;
356    #endif
357    return fe_fgets_stdin(pr,s,size);
358  }
359  else /* could load libreadline: */
360  {
361    /* Allow conditional parsing of the ~/.inputrc file. */
362    *fe_rl_readline_name = "Singular";
363    /* Tell the completer that we want a crack first. */
364    *fe_rl_attempted_completion_function = (CPPFunction *)singular_completion;
365    /* try to read a history */
366    (*fe_using_history)();
367    char *p = getenv("SINGULARHIST");
368    if (p != NULL)
369    {
370      (*fe_read_history) (p);
371    }
372  }
373
374  /* set the output stream */
375  if(!isatty(STDOUT_FILENO))
376  {
377    #ifdef atarist
378      *fe_rl_outstream = fopen( "/dev/tty", "w" );
379    #else
380      *fe_rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
381    #endif
382  }
383
384  fe_fgets_stdin=fe_fgets_stdin_drl;
385  return fe_fgets_stdin_drl(pr,s,size);
386#else
387  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
388    fe_fgets_stdin=fe_fgets_stdin_emu;
389    return(fe_fgets_stdin_emu(pr,s,size));
390  #else
391    fe_fgets_stdin=fe_fgets;
392    return(fe_fgets(pr,s,size));
393  #endif
394#endif
395}
396
397/* ===================================================================*/
398/* =                      batch mode                                = */
399/* ===================================================================*/
400/* dummy (for batch mode): */
401char * fe_fgets_dummy(const char */*pr*/,char */*s*/, int /*size*/)
402{
403  return NULL;
404}
405
Note: See TracBrowser for help on using the repository browser.