source: git/Singular/feread.cc @ fdc537

spielwiese
Last change on this file since fdc537 was 8a679a, checked in by Hans Schönemann <hannes@…>, 24 years ago
* hannes: static.h added git-svn-id: file:///usr/local/Singular/svn/trunk@4364 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 13.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: feread.cc,v 1.36 2000-05-19 15:45:48 Singular Exp $ */
5/*
6* ABSTRACT: input from ttys, simulating fgets
7*/
8
9
10#include "mod2.h"
11#include "tok.h"
12#include "febase.h"
13#include "mmemory.h"
14
15#include "static.h"
16
17#ifdef HAVE_STATIC
18#undef HAVE_DYN_RL
19#endif
20
21#ifdef HAVE_TCL
22#include "ipid.h"
23#endif
24
25static char * fe_fgets_stdin_init(char *pr,char *s, int size);
26char * (*fe_fgets_stdin)(char *pr,char *s, int size)
27 = fe_fgets_stdin_init;
28
29/* ===================================================================*/
30/* =                   static/dymanic readline                      = */
31/* ===================================================================*/
32#if defined(HAVE_READLINE) || defined(HAVE_DYN_RL)
33
34#include "ipshell.h"
35
36#ifndef STDOUT_FILENO
37#define STDOUT_FILENO 1
38#endif
39
40/* Generator function for command completion.  STATE lets us know whether
41*   to start from scratch; without any state (i.e. STATE == 0), then we
42*   start at the top of the list.
43*/
44char *command_generator (char *text, int state)
45{
46  static int list_index, len;
47  char *name;
48
49  /* If this is a new word to complete, initialize now.  This includes
50     saving the length of TEXT for efficiency, and initializing the index
51     variable to 0. */
52  if (state==0)
53  {
54    list_index = 1;
55    len = strlen (text);
56  }
57
58  /* Return the next name which partially matches from the command list. */
59  while ((name = cmds[list_index].name)!=NULL)
60  {
61    list_index++;
62
63    if (strncmp (name, text, len) == 0)
64      return (strdup(name));
65  }
66
67  /* If no names matched, then return NULL. */
68  return ((char *)NULL);
69}
70#endif
71
72/* ===================================================================*/
73/* =                      static readline                           = */
74/* ===================================================================*/
75/* some procedure are shared with "dynamic readline" */
76#if defined(HAVE_READLINE) && !defined(HAVE_FEREAD)
77#include <unistd.h>
78#include <stdio.h>
79#include <stdlib.h>
80#include <sys/types.h>
81#include <sys/file.h>
82#include <sys/stat.h>
83#include <sys/errno.h>
84
85// #undef READLINE_READLINE_H_OK
86
87extern "C" {
88 #ifdef READLINE_READLINE_H_OK
89  #include <readline/readline.h>
90  #ifdef HAVE_READLINE_HISTORY_H
91   #include <readline/history.h>
92  #endif
93 #else /* declare everything we need explicitely and do not rely on includes */
94  extern char * rl_readline_name;
95  extern char *rl_line_buffer;
96  char *filename_completion_function();
97  typedef char **CPPFunction ();
98  extern char ** completion_matches ();
99  extern CPPFunction * rl_attempted_completion_function;
100  extern FILE * rl_outstream;
101  extern char * readline ();
102  extern void add_history ();
103  extern int write_history ();
104  extern void using_history();
105  extern int read_history();
106  extern int history_total_bytes();
107 #endif /* READLINE_READLINE_H_OK */
108}
109
110
111char * fe_fgets_stdin_rl(char *pr,char *s, int size);
112
113/* Tell the GNU Readline library how to complete.  We want to try to complete
114   on command names  or on filenames if it is preceded by " */
115
116/* Attempt to complete on the contents of TEXT.  START and END show the
117*   region of TEXT that contains the word to complete.  We can use the
118*   entire line in case we want to do some simple parsing.  Return the
119*   array of matches, or NULL if there aren't any.
120*/
121char ** singular_completion (char *text, int start, int end)
122{
123  /* If this word is not in a string, then it may be a command
124     to complete.  Otherwise it may be the name of a file in the current
125     directory. */
126  if (rl_line_buffer[start-1]=='"')
127    return completion_matches (text, filename_completion_function);
128  char **m=completion_matches (text, command_generator);
129  if (m==NULL)
130  {
131    m=(char **)malloc(2*sizeof(char*));
132    m[0]=(char *)malloc(end-start+2);
133    strncpy(m[0],text,end-start+1);
134    m[1]=NULL;
135  }
136  return m;
137}
138
139char * fe_fgets_stdin_rl(char *pr,char *s, int size)
140{
141  if (!BVERBOSE(V_PROMPT))
142  {
143    pr="";
144  }
145  mflush();
146
147  char *line;
148  line = readline (pr);
149
150  if (line==NULL)
151    return NULL;
152
153  if (*line!='\0')
154  {
155    add_history (line);
156  }
157  int l=strlen(line);
158  if (l>=size-1)
159  {
160    strncpy(s,line,size);
161  }
162  else
163  {
164    strncpy(s,line,l);
165    s[l]='\n';
166    s[l+1]='\0';
167  }
168  free (line);
169
170  return s;
171}
172#endif
173
174/* ===================================================================*/
175/* =                    emulated readline                           = */
176/* ===================================================================*/
177#if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
178extern "C" {
179char * fe_fgets_stdin_fe(char *pr,char *s, int size);
180}
181char * fe_fgets_stdin_emu(char *pr,char *s, int size)
182{
183  if (!BVERBOSE(V_PROMPT))
184  {
185    pr="";
186  }
187  mflush();
188  return fe_fgets_stdin_fe(pr,s,size);
189}
190#endif
191
192/* ===================================================================*/
193/* =                     dynamic readline                           = */
194/* ===================================================================*/
195/* some procedure are shared with "static readline" */
196
197#if defined(HAVE_DYN_RL)
198#include <unistd.h>
199//#include <stdio.h>
200//#include <stdlib.h>
201//#include <sys/types.h>
202//#include <sys/file.h>
203//#include <sys/stat.h>
204//#include <sys/errno.h>
205//#include <dlfcn.h>
206#include "mod_raw.h"
207
208extern "C" {
209  char *(*fe_filename_completion_function)(); /* 3 */
210  char *(* fe_readline) ();                   /* 4 */
211  void (*fe_add_history) ();                  /* 5 */
212  char ** fe_rl_readline_name;                /* 6 */
213  char **fe_rl_line_buffer;                   /* 7 */
214  char **(*fe_completion_matches)();          /* 8 */
215  typedef char **CPPFunction ();
216  CPPFunction **fe_rl_attempted_completion_function; /* 9 */
217  FILE ** fe_rl_outstream;                    /* 10 */
218  int (*fe_write_history) ();                 /* 11 */
219  int (*fe_history_total_bytes) ();           /* 12 */
220  void (*fe_using_history) ();                /* 13 */
221  int (*fe_read_history) ();                  /* 14 */
222}
223
224void * fe_rl_hdl=NULL;
225
226char * fe_fgets_stdin_rl(char *pr,char *s, int size);
227static int fe_init_dyn_rl()
228{
229  int res=0;
230  loop
231  {
232    #if defined(HPUX_9) || defined(HPUX_10)
233    fe_rl_hdl=dynl_open("libreadline.sl");
234    if (fe_rl_hdl==NULL)
235      fe_rl_hdl=dynl_open("/lib/libreadline.sl");
236    if (fe_rl_hdl==NULL)
237      fe_rl_hdl=dynl_open("/usr/lib/libreadline.sl");
238    #else
239    fe_rl_hdl=dynl_open("libreadline.so");
240    #endif
241    if (fe_rl_hdl==NULL) { return 1;}
242
243    fe_filename_completion_function=
244      dynl_sym(fe_rl_hdl,"filename_completion_function");
245    if (fe_filename_completion_function==NULL) { res=3; break; }
246    fe_readline=dynl_sym(fe_rl_hdl,"readline");
247    if (fe_readline==NULL) { res=4; break; }
248    fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
249    if (fe_add_history==NULL) { res=5; break; }
250    fe_rl_readline_name=dynl_sym(fe_rl_hdl,"rl_readline_name");
251    if (fe_rl_readline_name==NULL) { res=6; break; }
252    fe_rl_line_buffer=dynl_sym(fe_rl_hdl,"rl_line_buffer");
253    if (fe_rl_line_buffer==NULL) { res=7; break; }
254    fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
255    if (fe_completion_matches==NULL) { res=8; break; }
256    fe_rl_attempted_completion_function=
257      dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
258    if (fe_rl_attempted_completion_function==NULL) { res=9; break; }
259    fe_rl_outstream=dynl_sym(fe_rl_hdl,"rl_outstream");
260    if (fe_rl_outstream==NULL) { res=10; break; }
261    fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
262    if (fe_write_history==NULL) { res=11; break; }
263    fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
264    if (fe_history_total_bytes==NULL) { res=12; break; }
265    fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
266    if (fe_using_history==NULL) { res=13; break; }
267    fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
268    if (fe_read_history==NULL) { res=14; break; }
269    return 0;
270  }
271  dynl_close(fe_rl_hdl);
272  return res;
273}
274
275/* Attempt to complete on the contents of TEXT.  START and END show the
276*   region of TEXT that contains the word to complete.  We can use the
277*   entire line in case we want to do some simple parsing.  Return the
278*   array of matches, or NULL if there aren't any.
279*/
280char ** singular_completion (char *text, int start, int end)
281{
282  /* If this word is not in a string, then it may be a command
283     to complete.  Otherwise it may be the name of a file in the current
284     directory. */
285  if ((*fe_rl_line_buffer)[start-1]=='"')
286    return (*fe_completion_matches) (text, *fe_filename_completion_function);
287  char **m=(*fe_completion_matches) (text, command_generator);
288  if (m==NULL)
289  {
290    m=(char **)malloc(2*sizeof(char*));
291    m[0]=(char *)malloc(end-start+2);
292    strncpy(m[0],text,end-start+1);
293    m[1]=NULL;
294  }
295  return m;
296}
297
298char * fe_fgets_stdin_drl(char *pr,char *s, int size)
299{
300  if (!BVERBOSE(V_PROMPT))
301  {
302    pr="";
303  }
304  mflush();
305
306  char *line;
307  line = (*fe_readline) (pr);
308
309  if (line==NULL)
310    return NULL;
311
312  if (*line!='\0')
313  {
314    (*fe_add_history) (line);
315  }
316  int l=strlen(line);
317  if (l>=size-1)
318  {
319    strncpy(s,line,size);
320  }
321  else
322  {
323    strncpy(s,line,l);
324    s[l]='\n';
325    s[l+1]='\0';
326  }
327  free (line);
328
329  return s;
330}
331#endif
332
333/* ===================================================================*/
334/* =                        fgets                                   = */
335/* ===================================================================*/
336char * fe_fgets(char *pr,char *s, int size)
337{
338  if (BVERBOSE(V_PROMPT))
339  {
340    fprintf(stdout,pr);
341  }
342  mflush();
343  return fgets(s,size,stdin);
344}
345
346/* ===================================================================*/
347/* =       init for static rl, dyn. rl, emu. rl                     = */
348/* ===================================================================*/
349static char * fe_fgets_stdin_init(char *pr,char *s, int size)
350{
351#if defined(HAVE_READLINE) && !defined(HAVE_FEREAD)
352  /* Allow conditional parsing of the ~/.inputrc file. */
353  rl_readline_name = "Singular";
354  /* Tell the completer that we want a crack first. */
355  rl_attempted_completion_function = (CPPFunction *)singular_completion;
356
357  /* set the output stream */
358  if(!isatty(STDOUT_FILENO))
359  {
360    #ifdef atarist
361      rl_outstream = fopen( "/dev/tty", "w" );
362    #else
363      rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
364    #endif
365  }
366
367  /* try to read a history */
368  using_history();
369  char *p = getenv("SINGULARHIST");
370  if (p != NULL)
371  {
372    read_history (p);
373  }
374  fe_fgets_stdin=fe_fgets_stdin_rl;
375  return(fe_fgets_stdin_rl(pr,s,size));
376#endif
377#ifdef HAVE_DYN_RL
378  /* do dynamic loading */
379  int res=fe_init_dyn_rl();
380  if (res!=0)
381  {
382    //if (res==1)
383    //  WarnS("dynamic loading of libreadline failed");
384    //else
385    //  Warn("dynamic loading failed: %d\n",res);
386    if (res!=1)
387      Warn("dynamic loading failed: %d\n",res);
388    fe_fgets_stdin=fe_fgets_stdin_emu;
389    return fe_fgets_stdin_emu(pr,s,size);
390  }
391  /* Allow conditional parsing of the ~/.inputrc file. */
392  (*fe_rl_readline_name) = "Singular";
393  /* Tell the completer that we want a crack first. */
394  (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
395
396  /* set the output stream */
397  if(!isatty(STDOUT_FILENO))
398  {
399    #ifdef atarist
400      *fe_rl_outstream = fopen( "/dev/tty", "w" );
401    #else
402      *fe_rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
403    #endif
404  }
405
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  fe_fgets_stdin=fe_fgets_stdin_drl;
414  return fe_fgets_stdin_drl(pr,s,size);
415#else
416  #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
417    fe_fgets_stdin=fe_fgets_stdin_emu;
418    return(fe_fgets_stdin_emu(pr,s,size));
419  #endif
420#endif
421}
422
423/* ===================================================================*/
424/* =                          TCL                                   = */
425/* ===================================================================*/
426#ifdef HAVE_TCL
427/* tcl: */
428char * fe_fgets_tcl(char *pr,char *s, int size)
429{
430  if(currRing!=NULL) PrintTCLS('P',pr);
431  else               PrintTCLS('U',pr);
432  mflush();
433  return fgets(s,size,stdin);
434}
435
436#endif
437
438/* ===================================================================*/
439/* =                      batch mode                                = */
440/* ===================================================================*/
441/* dummy (for batch mode): */
442char * fe_fgets_dummy(char *pr,char *s, int size)
443{
444  return NULL;
445}
446
447/* ===================================================================*/
448/* =          fe_reset_input_mode (all possibilities)               = */
449/* ===================================================================*/
450void fe_reset_input_mode ()
451{
452#if defined(HAVE_DYN_RL)
453  char *p = getenv("SINGULARHIST");
454  if ((p != NULL) && (fe_history_total_bytes != NULL))
455  {
456    if((*fe_history_total_bytes)()!=0)
457      (*fe_write_history) (p);
458  }
459#endif
460#if defined(HAVE_READLINE) && !defined(HAVE_FEREAD) && !defined(HAVE_DYN_RL)
461  char *p = getenv("SINGULARHIST");
462  if (p != NULL)
463  {
464    if(history_total_bytes()!=0)
465      write_history (p);
466  }
467#endif
468#if !defined(MSDOS) && (defined(HAVE_FEREAD) || defined(HAVE_DYN_RL))
469  #ifndef HAVE_ATEXIT
470  fe_reset_fe(NULL,NULL);
471  #else
472  fe_reset_fe();
473  #endif
474#endif
475}
Note: See TracBrowser for help on using the repository browser.