source: git/kernel/oswrapper/feread.cc @ 54d522

spielwiese
Last change on this file since 54d522 was 54d522, checked in by Hans Schoenemann <hannes@…>, 8 years ago
more tests for unusual tty configurations
  • Property mode set to 100644
File size: 11.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: input from ttys, simulating fgets
6*/
7
8#include <kernel/mod2.h>
9
10// ----------------------------------------
11// system settings:
12
13#undef USE_READLINE4
14
15//----------------------------------------
16#ifdef __CYGWIN__
17#define READLINE_STATIC
18#endif
19#include <omalloc/omalloc.h>
20#include <misc/options.h>
21
22#include <kernel/oswrapper/feread.h>
23
24#ifdef HAVE_STATIC
25#undef HAVE_DYN_RL
26#endif
27
28#if defined(HAVE_DYN_RL)
29#include <unistd.h>
30#endif
31
32static char * fe_fgets_stdin_init(const char *pr,char *s, int size);
33char * (*fe_fgets_stdin)(const char *pr,char *s, int size)
34 = fe_fgets_stdin_init;
35
36extern char *iiArithGetCmd(int);
37
38/* ===================================================================*/
39/* =                   static/dymanic readline                      = */
40/* ===================================================================*/
41#if defined(HAVE_READLINE) || defined(HAVE_DYN_RL) || defined(HAVE_LIBREADLINE)
42
43#ifndef STDOUT_FILENO
44#define STDOUT_FILENO 1
45#endif
46
47/* Generator function for command completion.  STATE lets us know whether
48*   to start from scratch; without any state (i.e. STATE == 0), then we
49*   start at the top of the list.
50*/
51#include <Singular/ipid.h>
52extern "C"
53char *command_generator (char *text, int state)
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}
90#endif
91
92/* ===================================================================*/
93/* =                      static readline                           = */
94/* ===================================================================*/
95/* some procedure are shared with "dynamic readline" */
96#if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE) || defined(HAVE_DYN_RL))
97#include <unistd.h>
98#include <stdio.h>
99#include <stdlib.h>
100#include <sys/types.h>
101#include <sys/file.h>
102#include <sys/stat.h>
103#include <sys/errno.h>
104
105// #undef READLINE_READLINE_H_OK
106
107extern "C" {
108  typedef char * (*RL_PROC)(const char*,int);
109  #ifdef READLINE_READLINE_H_OK
110    #include <readline/readline.h>
111    #ifdef HAVE_READLINE_HISTORY_H
112      #include <readline/history.h>
113    #endif
114  #endif
115
116  #ifdef RL_VERSION_MAJOR
117    #if (RL_VERSION_MAJOR >= 4)
118      #define USE_READLINE4
119    #endif
120  #endif
121
122  #ifndef USE_READLINE4
123    #define rl_filename_completion_function filename_completion_function
124    #define rl_completion_matches           completion_matches
125  #endif
126  #ifndef READLINE_READLINE_H_OK
127    /* declare everything we need explicitely and do not rely on includes */
128    extern char * rl_readline_name;
129    extern char *rl_line_buffer;
130    char *rl_filename_completion_function(const char*, int);
131    typedef char **CPPFunction ();
132
133    extern char ** rl_completion_matches (const char*, RL_PROC);
134    extern CPPFunction * rl_attempted_completion_function;
135    extern FILE * rl_outstream;
136    extern char * readline (const char *);
137    extern void add_history (char *);
138    extern int write_history ();
139    extern void using_history();
140    extern int read_history(char *);
141    extern int history_total_bytes();
142  #endif /* READLINE_READLINE_H_OK */
143
144  typedef char * (*PROC)();
145
146  typedef char **RL_CPPFunction (const char*, int,int);
147}
148
149
150char * fe_fgets_stdin_rl(const char *pr,char *s, int size);
151
152/* Tell the GNU Readline library how to complete.  We want to try to complete
153   on command names  or on filenames if it is preceded by " */
154
155/* Attempt to complete on the contents of TEXT.  START and END show the
156*   region of TEXT that contains the word to complete.  We can use the
157*   entire line in case we want to do some simple parsing.  Return the
158*   array of matches, or NULL if there aren't any.
159*/
160#if defined(HAVE_DYN_RL)
161extern "C"
162{
163  int fe_init_dyn_rl();
164  char *(*fe_filename_completion_function)(); /* 3 */
165  char *(* fe_readline) (char *);             /* 4 */
166  void (*fe_add_history) (char *);            /* 5 */
167  char ** fe_rl_readline_name;                /* 6 */
168  char **fe_rl_line_buffer;                   /* 7 */
169  char **(*fe_completion_matches)(...);          /* 8 */
170  CPPFunction **fe_rl_attempted_completion_function; /* 9 */
171  FILE ** fe_rl_outstream;                    /* 10 */
172  int (*fe_write_history) ();                 /* 11 */
173  int (*fe_history_total_bytes) ();           /* 12 */
174  void (*fe_using_history) ();                /* 13 */
175  int (*fe_read_history) (char *);            /* 14 */
176
177}
178#endif
179char ** singular_completion (char *text, int start, int end)
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]=='"'))
194    return x_rl_completion_matches (text, (RL_PROC)x_rl_filename_completion_function);
195  char **m=x_rl_completion_matches (text, (RL_PROC)command_generator);
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}
207
208#ifndef HAVE_DYN_RL
209char * fe_fgets_stdin_rl(const char *pr,char *s, int size)
210{
211  if (!BVERBOSE(V_PROMPT))
212  {
213    pr="";
214  }
215  mflush();
216
217  char *line;
218  line = readline (pr);
219
220  if (line==NULL)
221    return NULL;
222
223  int l=strlen(line);
224  for (int i=l-1;i>=0;i--) line[i]=line[i]&127;
225
226  if (*line!='\0')
227  {
228    add_history (line);
229  }
230  if (l>=size-1)
231  {
232    strncpy(s,line,size);
233  }
234  else
235  {
236    strncpy(s,line,l);
237    s[l]='\n';
238    s[l+1]='\0';
239  }
240  free (line);
241
242  return s;
243}
244#endif
245#endif
246
247/* ===================================================================*/
248/* =                    emulated readline                           = */
249/* ===================================================================*/
250#if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
251extern "C" {
252char * fe_fgets_stdin_fe(const char *pr,char *s, int size);
253}
254char * fe_fgets_stdin_emu(const char *pr,char *s, int size)
255{
256  if (!BVERBOSE(V_PROMPT))
257  {
258    pr="";
259  }
260  mflush();
261  return fe_fgets_stdin_fe(pr,s,size);
262}
263#endif
264
265/* ===================================================================*/
266/* =                     dynamic readline                           = */
267/* ===================================================================*/
268/* some procedure are shared with "static readline" */
269#if defined(HAVE_DYN_RL)
270char * fe_fgets_stdin_drl(const char *pr,char *s, int size)
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}
305#endif
306
307/* ===================================================================*/
308/* =                        fgets                                   = */
309/* ===================================================================*/
310char * fe_fgets(const char *pr,char *s, int size)
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}
322
323/* ===================================================================*/
324/* =       init for static rl, dyn. rl, emu. rl                     = */
325/* ===================================================================*/
326static char * fe_fgets_stdin_init(const char *pr,char *s, int size)
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
335  rl_attempted_completion_function = (CPPFunction *)singular_completion;
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(stdout)), "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  }
355  fe_fgets_stdin=fe_fgets_stdin_rl;
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
370    fe_fgets_stdin=fe_fgets_stdin_emu;
371    #else
372    fe_fgets_stdin=fe_fgets;
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. */
381    *fe_rl_attempted_completion_function = (CPPFunction *)singular_completion;
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(stdout)), "w" );
398    #endif
399  }
400
401  fe_fgets_stdin=fe_fgets_stdin_drl;
402  return fe_fgets_stdin_drl(pr,s,size);
403#else
404  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
405    fe_fgets_stdin=fe_fgets_stdin_emu;
406    return(fe_fgets_stdin_emu(pr,s,size));
407  #else
408    fe_fgets_stdin=fe_fgets;
409    return(fe_fgets(pr,s,size));
410  #endif
411#endif
412}
413
414/* ===================================================================*/
415/* =                      batch mode                                = */
416/* ===================================================================*/
417/* dummy (for batch mode): */
418char * fe_fgets_dummy(const char */*pr*/,char */*s*/, int /*size*/)
419{
420  return NULL;
421}
422
Note: See TracBrowser for help on using the repository browser.