source: git/Singular/fereadl.c @ bdda8c2

spielwiese
Last change on this file since bdda8c2 was bdda8c2, checked in by Hans Schoenemann <hannes@…>, 11 years ago
add: handling signals in system calls, p2
  • Property mode set to 100644
File size: 21.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: input from ttys, simulating fgets
6*/
7
8
9#include "config.h"
10#include <kernel/mod2.h>
11#include <omalloc/omalloc.h>
12
13// #include <kernel/febase.h>
14// #include <kernel/structs.h>
15
16
17#ifdef HAVE_FEREAD
18  #include <unistd.h>
19  #include <stdio.h>
20  #include <stdlib.h>
21  #include <sys/time.h>
22  #include <sys/types.h>
23  #include <string.h>
24
25  #if 0
26    #include <pc.h>
27  #else
28    #ifdef SunOS_5
29      /* solaris special, found with v 5.7 */
30      #define _XOPEN_SOURCE_EXTENDED
31      #include "/usr/xpg4/include/term.h"
32    #endif
33    #if 0
34      #ifndef SunOS_5
35        #include <term.h>
36      #endif
37    #elif HAVE_TERMCAP_H
38      #ifndef SunOS_5
39      #include <termcap.h>
40      #endif
41    #endif
42    #if defined(HAVE_TERMIOS_H) && ! defined(TCSANOW)
43      #include <termios.h>
44    #endif
45    #if defined(HAVE_TERM_H) && ! defined(TCSANOW)
46      #include <term.h>
47    #endif
48
49  #endif
50
51
52#ifndef STDIN_FILENO
53  #define STDIN_FILENO 0
54#endif
55#ifndef STDOUT_FILENO
56  #define STDOUT_FILENO 1
57#endif
58
59#define feCTRL(C) ((C) & 0x1F)    /* <ctrl> character  */
60
61struct termios fe_saved_attributes;
62
63static BOOLEAN fe_stdout_is_tty;
64static BOOLEAN fe_stdin_is_tty;
65BOOLEAN fe_use_fgets=FALSE;
66static BOOLEAN fe_is_initialized=FALSE;
67static int     pagelength = 24;
68
69FILE *  fe_echo; /*the output file for echoed characters*/
70
71#define fe_hist_max 32
72char ** fe_hist=NULL;
73short   fe_hist_pos;
74BOOLEAN fe_is_raw_tty=0;
75int     fe_cursor_pos; /* 0..colmax-1*/
76int     fe_cursor_line; /* 0..pagelength-1*/
77
78#ifndef HAVE_ATEXIT
79  int on_exit(void (*f)(int, void *), void *arg);
80  #ifdef HAVE_FEREAD
81    void fe_reset_fe (int i, void *v)
82  #endif
83#else
84  #ifdef HAVE_FEREAD
85    void fe_reset_fe (void)
86  #endif
87#endif
88{
89  if (fe_stdin_is_tty)
90  {
91    int i;
92    if (fe_is_raw_tty)
93    {
94      tcsetattr (STDIN_FILENO, TCSANOW, &fe_saved_attributes);
95      fe_is_raw_tty=0;
96    }
97    if (fe_hist!=NULL)
98    {
99      for(i=fe_hist_max-1;i>=0;i--)
100      {
101        if (fe_hist[i] != NULL) omFree((ADDRESS)fe_hist[i]);
102      }
103      omFreeSize((ADDRESS)fe_hist,fe_hist_max*sizeof(char *));
104      fe_hist=NULL;
105    }
106    if (!fe_stdout_is_tty)
107    {
108      fclose(fe_echo);
109    }
110  }
111}
112void fe_temp_reset (void)
113{
114  if (fe_is_raw_tty)
115  {
116    tcsetattr (STDIN_FILENO, TCSANOW, &fe_saved_attributes);
117    fe_is_raw_tty=0;
118  }
119}
120void fe_temp_set (void)
121{
122  if(fe_is_raw_tty==0)
123  {
124    struct termios tattr;
125
126    /* Set the funny terminal modes. */
127    tcgetattr (STDIN_FILENO, &tattr);
128    tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
129    tattr.c_cc[VMIN] = 1;
130    tattr.c_cc[VTIME] = 0;
131    tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
132    fe_is_raw_tty=1;
133  }
134}
135
136static char termcap_buff[2048];
137static int fe_out_char(int c)
138{
139  fputc(c,fe_echo);
140  return c;
141}
142void fe_init (void)
143{
144  fe_is_initialized=TRUE;
145  if ((!fe_use_fgets) && (isatty (STDIN_FILENO)))
146  {
147    /* Make sure stdin is a terminal. */
148    char *term=getenv("TERM");
149
150    /*setup echo*/
151    if(isatty(STDOUT_FILENO))
152    {
153      fe_stdout_is_tty=1;
154      fe_echo=stdout;
155    }
156    else
157    {
158      fe_stdout_is_tty=0;
159      fe_echo = fopen( ttyname(fileno(stdin)), "w" );
160    }
161    /* Save the terminal attributes so we can restore them later. */
162    {
163      struct termios tattr;
164      tcgetattr (STDIN_FILENO, &fe_saved_attributes);
165      #ifdef HAVE_FEREAD
166        #ifdef HAVE_ATEXIT
167          atexit(fe_reset_fe);
168        #else
169          on_exit(fe_reset_fe,NULL);
170        #endif
171      #endif
172
173      /* Set the funny terminal modes. */
174      tcgetattr (STDIN_FILENO, &tattr);
175      tattr.c_lflag &= ~(ICANON|ECHO); /* Clear ICANON and ECHO. */
176      tattr.c_cc[VMIN] = 1;
177      tattr.c_cc[VTIME] = 0;
178      tcsetattr (STDIN_FILENO, TCSAFLUSH, &tattr);
179      /*ospeed=cfgetospeed(&tattr);*/
180    }
181    if(term==NULL)
182    {
183      printf("need TERM\n");
184    }
185    else if(tgetent(termcap_buff,term)<=0)
186    {
187      printf("could not access termcap data base\n");
188    }
189    else
190    {
191      #ifndef ix86_Win
192      extern char *BC;
193      extern char *UP;
194      extern char PC;
195      #endif
196      /* OB: why this ? HS: char t_buf[128] does not work with glibc2 systems */
197      char *t_buf=(char *)omAlloc(128);
198      /*char t_buf[128];*/
199      char *temp;
200      char** t_buf_ptr= &t_buf;
201      /* Extract information that termcap functions use.  */
202      temp = tgetstr ("pc", t_buf_ptr);
203      PC = (temp!=NULL) ? *temp : '\0';
204      BC=tgetstr("le",t_buf_ptr);
205      UP=tgetstr("up",t_buf_ptr);
206
207      /* Extract information we will use */
208      colmax=tgetnum("co");
209      pagelength=tgetnum("li");
210      fe_cursor_line=pagelength-1;
211
212      /* init screen */
213      temp = tgetstr ("ti", t_buf_ptr);
214      #if 0
215      if (temp!=NULL) tputs(temp,1,fe_out_char);
216      #endif
217
218      /* printf("TERM=%s, co=%d, li=%d\n",term,colmax,pagelength);*/
219    }
220
221    fe_stdin_is_tty=1;
222    fe_is_raw_tty=1;
223
224    /* setup history */
225    fe_hist=(char **)omAlloc0(fe_hist_max*sizeof(char *));
226    omMarkAsStaticAddr(fe_hist);
227    fe_hist_pos=0;
228  }
229  else
230  {
231    fe_stdin_is_tty=0;
232    fe_echo=stdout;
233  }
234}
235
236/* delete to end of line */
237static void fe_ctrl_k(char *s,int i)
238{
239  int j=i;
240  while(s[j]!='\0')
241  {
242    fputc(' ',fe_echo);
243    j++;
244  }
245  while(j>i)
246  {
247    fputc('\b',fe_echo);
248    j--;
249  }
250}
251
252/* delete the line */
253static void fe_ctrl_u(char *s,int *i)
254{
255  fe_ctrl_k(s,*i);
256  while((*i)>0)
257  {
258    (*i)--;
259    fputc('\b',fe_echo);
260    fputc(' ',fe_echo);
261    fputc('\b',fe_echo);
262  }
263}
264
265/*2
266* add s to the history
267* if s is no the previous one, duplicate it
268*/
269static void fe_add_hist(char *s)
270{
271  if (s[0]!='\0') /* skip empty lines */
272  {
273    /* compare this line*/
274    if (fe_hist_pos!=0)
275    {
276      if ((fe_hist[fe_hist_pos-1]!=NULL)
277      && (strcmp(fe_hist[fe_hist_pos-1],s)==0))
278        return;
279    }
280    else
281    {
282      if ((fe_hist[fe_hist_max-1]!=NULL)
283      && (strcmp(fe_hist[fe_hist_max-1],s)==0))
284        return;
285    }
286    /* normal case: enter a new line */
287    /* first free the slot at position fe_hist_pos */
288    if (fe_hist[fe_hist_pos]!=NULL)
289    {
290      omFree((ADDRESS)fe_hist[fe_hist_pos]);
291    }
292    /* and store a duplicate */
293    fe_hist[fe_hist_pos]=omStrDup(s);
294    omMarkAsStaticAddr(fe_hist[fe_hist_pos]);
295    /* increment fe_hist_pos in a circular manner */
296    fe_hist_pos++;
297    if (fe_hist_pos==fe_hist_max) fe_hist_pos=0;
298  }
299}
300
301static void fe_get_hist(char *s, int size, int *pos,int change, int incr)
302{
303  if (change)
304    fe_add_hist(s);
305  do
306  {
307    (*pos)+=incr;
308    if((*pos)>=fe_hist_max) (*pos)-=fe_hist_max;
309    else if((*pos)<0)       (*pos)+=fe_hist_max;
310  }
311  while (((*pos)!=0)&&(fe_hist[(*pos)]==NULL));
312  memset(s,0,size);
313  if (fe_hist[(*pos)]!=NULL)
314  {
315    strncpy(s,fe_hist[(*pos)],size-2);
316  }
317}
318
319static int fe_getchar()
320{
321  char c='\0';
322  while (1!=read (STDIN_FILENO, &c, 1));
323  if (c == 033)
324  {
325    /* check for CSI */
326    c='\0';
327    while((-1 == read (STDIN_FILENO, &c, 1)) && (errno == EINTR));
328    if (c == '[')
329    {
330      /* get command character */
331      c='\0';
332      while((-1 == read (STDIN_FILENO, &c, 1)) && (errno == EINTR));
333      switch (c)
334      {
335        case 'D': /* left arrow key */
336          c = feCTRL('B')/*002*/;
337          break;
338        case 'C': /* right arrow key */
339          c = feCTRL('F')/*006*/;
340          break;
341        case 'A': /* up arrow key */
342          c = feCTRL('P')/*020*/;
343          break;
344        case 'B': /* down arrow key */
345          c = feCTRL('N')/*016*/;
346          break;
347      }
348    }
349  }
350  return c;
351}
352
353static void fe_set_cursor(char *s,int i)
354{
355  char tgoto_buf[40];
356  if (0)/*(fe_cursor_pos>1) && (i>0))*/
357  {
358    /*fputs(tgoto(tgetstr("cm",&tgoto_buf),fe_cursor_pos-1,fe_cursor_line),fe_echo);*/
359    tputs(
360      tgoto(tgetstr("cm",(char **)&tgoto_buf),fe_cursor_pos-1,fe_cursor_line),
361      pagelength,fe_out_char);
362    fputc(s[i-1],fe_echo);
363  }
364  else
365  {
366    /*fputs(
367      tgoto(tgetstr("cm",&tgoto_buf),fe_cursor_pos,fe_cursor_line),fe_echo);*/
368    tputs(tgoto(tgetstr("cm",(char **)&tgoto_buf),fe_cursor_pos,fe_cursor_line),
369      pagelength,fe_out_char);
370  }
371  fflush(fe_echo);
372}
373
374char * fe_fgets_stdin_fe(char *pr,char *s, int size)
375{
376  if(!fe_is_initialized)
377    fe_init();
378  if (fe_stdin_is_tty)
379  {
380    int h=fe_hist_pos;
381    int change=0;
382    char c;
383    int i=0;
384
385    if (fe_is_raw_tty==0)
386    {
387      fe_temp_set();
388    }
389
390    fputs(pr,fe_echo); fflush(fe_echo);
391    fe_cursor_pos=strlen(pr); /* prompt */
392
393    memset(s,0,size);
394
395    loop
396    {
397      c=fe_getchar();
398      switch(c)
399      {
400        case feCTRL('M'):
401        case feCTRL('J'):
402        {
403          fd_set fdset;
404          struct timeval tv;
405          int sel;
406
407          fe_add_hist(s);
408          i=strlen(s);
409          if (i<size-1) s[i]='\n';
410          fputc('\n',fe_echo);
411          fflush(fe_echo);
412
413          FD_ZERO (&fdset);
414          FD_SET(STDIN_FILENO, &fdset);
415          tv.tv_sec = 0;
416          tv.tv_usec = 0;
417          do
418          {
419            sel = select (STDIN_FILENO+1,
420#ifdef hpux
421                          (int *)fdset.fds_bits,
422#else
423                          &fdset,
424#endif
425                          NULL, NULL, &tv);
426          } while( (sel == -1) && (errno == EINTR) );
427          if (sel==0)
428            fe_temp_reset();
429          return s;
430        }
431        case feCTRL('H'):
432        case 127:       /*delete the character left of the cursor*/
433        {
434          if (i==0) break;
435          i--;
436          fe_cursor_pos--;
437          if(fe_cursor_pos<0)
438          {
439            fe_cursor_line--;
440            fe_cursor_pos=colmax-1;
441            fe_set_cursor(s,i);
442          }
443          else
444          {
445            fputc('\b',fe_echo);
446          }
447          /* NO BREAK : next: feCTRL('D') */
448        }
449        case feCTRL('D'):  /*delete the character under the cursor or eof*/
450        {
451          int j;
452          if ((i==0) &&(s[0]=='\0')) return NULL; /*eof*/
453          if (s[i]!='\0')
454          {
455            j=i;
456            while(s[j]!='\0')
457            {
458              s[j]=s[j+1];
459              fputc(s[j],fe_echo);
460              j++;
461            }
462            fputc(' ',fe_echo);
463            if (fe_cursor_pos+(j-i)>=colmax)
464            {
465              fe_set_cursor(s,i);
466            }
467            else
468            {
469              while(j>i)
470              {
471                fputc('\b',fe_echo);
472                j--;
473              }
474            }
475          }
476          change=1;
477          fflush(fe_echo);
478          break;
479        }
480        case feCTRL('A'):  /* move the cursor to the beginning of the line */
481        {
482          if (i>=colmax-strlen(pr))
483          {
484            while (i>=colmax-strlen(pr))
485            {
486              i-=colmax;
487              fe_cursor_line--;
488            }
489            i=0;
490            fe_cursor_pos=strlen(pr);
491            fe_set_cursor(s,i);
492          }
493          else
494          {
495            while(i>0)
496            {
497              i--;
498              fputc('\b',fe_echo);
499            }
500            fe_cursor_pos=strlen(pr);
501          }
502          break;
503        }
504        case feCTRL('E'): /* move the cursor to the end of the line */
505        {
506          while(s[i]!='\0')
507          {
508            fputc(s[i],fe_echo);
509            i++;
510            fe_cursor_pos++;
511            if(fe_cursor_pos>=colmax)
512            {
513              fe_cursor_pos=0;
514              if(fe_cursor_line!=(pagelength-1))
515                fe_cursor_line++;
516            }
517          }
518          break;
519        }
520        case feCTRL('B'): /* move the cursor backward one character */
521        {
522          if (i>0)
523          {
524            i--;
525            fputc('\b',fe_echo);
526            fe_cursor_pos--;
527            if(fe_cursor_pos<0)
528            {
529              fe_cursor_pos=colmax-1;
530              fe_cursor_line--;
531            }
532          }
533          break;
534        }
535        case feCTRL('F'): /* move the cursor forward  one character */
536        {
537          if(s[i]!='\0')
538          {
539            fputc(s[i],fe_echo);
540            i++;
541            fe_cursor_pos++;
542            if(fe_cursor_pos>=colmax)
543            {
544              fe_cursor_pos=0;
545              if(fe_cursor_line!=(pagelength-1))
546                fe_cursor_line++;
547            }
548          }
549          break;
550        }
551        case feCTRL('U'): /* delete entire input line */
552        {
553          fe_ctrl_u(s,&i);
554          fe_cursor_pos=strlen(pr);
555          memset(s,0,size);
556          change=1;
557          break;
558        }
559        #if 0
560        case feCTRL('W'): /* test hist. */
561        {
562          int i;
563          PrintS("\nstart hist\n");
564          for(i=0;i<fe_hist_max;i++)
565          {
566            if(fe_hist[i]!=NULL)
567            {
568              Print("%2d ",i);
569              if(i==fe_hist_pos) PrintS("-"); else PrintS(" ");
570              if(i==h) PrintS(">"); else PrintS(" ");
571              PrintS(fe_hist[i]);
572              PrintLn();
573            }
574          }
575          Print("end hist, next_pos=%d\n",fe_hist_pos);
576          break;
577        }
578        #endif
579        case feCTRL('K'): /* delete up to the end of the line */
580        {
581          fe_ctrl_k(s,i);
582          memset(&(s[i]),'\0',size-i);
583          /* s[i]='\0';*/
584          change=1;
585          break;
586        }
587        case feCTRL('L'): /* redraw screen */
588        {
589          char t_buf[40];
590          char *t=t_buf;
591          fe_cursor_line=i/colmax;
592          /*fputs(tgetstr("cl",&t),fe_echo);*/
593          tputs(tgetstr("cl",&t),pagelength,fe_out_char);
594          fflush(fe_echo);
595          fputs(pr,fe_echo);
596          fputs(s,fe_echo);
597          fe_set_cursor(s,i);
598          break;
599        }
600        case feCTRL('P'): /* previous line */
601        {
602          fe_ctrl_u(s,&i);
603          fe_get_hist(s,size,&h,change,-1);
604          while(s[i]!='\0')
605          {
606            fputc(s[i],fe_echo);
607            i++;
608          }
609          fe_cursor_pos=strlen(pr)+i/*strlen(s)*/;
610          change=0;
611          break;
612        }
613        case feCTRL('N'): /* next line */
614        {
615          fe_ctrl_u(s,&i);
616          fe_get_hist(s,size,&h,change,1);
617          while(s[i]!='\0')
618          {
619            fputc(s[i],fe_echo);
620            i++;
621          }
622          fe_cursor_pos=strlen(pr)+i/*strlen(s)*/;
623          change=0;
624          break;
625        }
626        default:
627        {
628          if ((c>=' ')&&(c<=126))
629          {
630            fputc (c,fe_echo);
631            fe_cursor_pos++;
632            if(fe_cursor_pos>=colmax)
633            {
634              fe_cursor_pos=0;
635              if(fe_cursor_line!=(pagelength-1))
636                fe_cursor_line++;
637            }
638            if (s[i]!='\0')
639            {
640              /* shift by 1 to the right */
641              int j=i;
642              int l;
643              while ((s[j]!='\0')&&(j<size-2)) j++;
644              l=j-i;
645              while (j>i) { s[j]=s[j-1]; j--; }
646              /* display */
647              fwrite(s+i+1,l,1,fe_echo);
648              fflush(fe_echo);
649              /* set cursor */
650              if(fe_cursor_pos+l>=colmax)
651              {
652                while(fe_cursor_pos+l>=colmax)
653                {
654                  fe_cursor_line--;
655                  l-=colmax;
656                }
657                fe_set_cursor(s,i);
658              }
659              else
660              {
661                while(l>0)
662                {
663                  l--;
664                  fputc('\b',fe_echo);
665                }
666              }
667              fflush(fe_echo);
668            }
669            if (i<size-1) s[i]=c;
670            i++;
671            change=1;
672          }
673        }
674      } /* switch */
675      fflush(fe_echo);
676    } /* loop */
677  }
678  /*else*/
679    return fgets(s,size,stdin);
680}
681
682//int main (void)
683//{
684//  char b[200];
685//  char * m_eof;
686//
687//  fe_init();
688//  while(1)
689//  {
690//    m_eof=fe_fgets_stdin_fe("> ",b,200);
691//    if (!m_eof) break;
692//    printf(">>%s<<\n",b);
693//  }
694//
695//  return 0;
696//}
697#endif
698
699/* ================================================================ */
700#if defined(HAVE_DYN_RL)
701#include <unistd.h>
702//#include <stdio.h>
703//#include <stdlib.h>
704//#include <sys/types.h>
705//#include <sys/file.h>
706//#include <sys/stat.h>
707//#include <sys/errno.h>
708//#include <dlfcn.h>
709#include <kernel/mod_raw.h>
710
711  typedef char **CPPFunction ();
712
713  char *(*fe_filename_completion_function)(); /* 3 */
714  char *(* fe_readline) ();                   /* 4 */
715  void (*fe_add_history) ();                  /* 5 */
716  char ** fe_rl_readline_name;                /* 6 */
717  char **fe_rl_line_buffer;                   /* 7 */
718  char **(*fe_completion_matches)();          /* 8 */
719  CPPFunction **fe_rl_attempted_completion_function; /* 9 */
720  FILE ** fe_rl_outstream;                    /* 10 */
721  int (*fe_write_history) ();                 /* 11 */
722  int (*fe_history_total_bytes) ();           /* 12 */
723  void (*fe_using_history) ();                /* 13 */
724  int (*fe_read_history) ();                  /* 14 */
725
726void * fe_rl_hdl=NULL;
727
728char *command_generator (char *text, int state);
729
730/* Attempt to complete on the contents of TEXT.  START and END show the
731*   region of TEXT that contains the word to complete.  We can use the
732*   entire line in case we want to do some simple parsing.  Return the
733*   array of matches, or NULL if there aren't any.
734*/
735char ** singular_completion (char *text, int start, int end)
736{
737  /* If this word is not in a string, then it may be a command
738     to complete.  Otherwise it may be the name of a file in the current
739     directory. */
740  char **m;
741  if ((*fe_rl_line_buffer)[start-1]=='"')
742    return (*fe_completion_matches) (text, *fe_filename_completion_function);
743  m=(*fe_completion_matches) (text, command_generator);
744  if (m==NULL)
745  {
746    m=(char **)malloc(2*sizeof(char*));
747    m[0]=(char *)malloc(end-start+2);
748    strncpy(m[0],text,end-start+1);
749    m[1]=NULL;
750  }
751  return m;
752}
753
754
755int fe_init_dyn_rl()
756{
757  int res=0;
758  loop
759  {
760    #if defined(HPUX_9) || defined(HPUX_10)
761    fe_rl_hdl=dynl_open("libreadline.sl");
762    if (fe_rl_hdl==NULL)
763      fe_rl_hdl=dynl_open("/lib/libreadline.sl");
764    if (fe_rl_hdl==NULL)
765      fe_rl_hdl=dynl_open("/usr/lib/libreadline.sl");
766    #else
767    fe_rl_hdl=dynl_open("libreadline.so");
768    if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.2");
769    if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.3");
770    if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.4");
771    if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.5");
772    if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.6");
773    if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.7");
774    #endif
775    if (fe_rl_hdl==NULL) { return 1;}
776
777    fe_filename_completion_function=
778      dynl_sym(fe_rl_hdl, "filename_completion_function");
779    if (fe_filename_completion_function==NULL) { res=3; break; }
780    fe_readline=dynl_sym(fe_rl_hdl,"readline");
781    if (fe_readline==NULL) { res=4; break; }
782    fe_add_history=dynl_sym(fe_rl_hdl,"add_history");
783    if (fe_add_history==NULL) { res=5; break; }
784    fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name");
785    if (fe_rl_readline_name==NULL) { res=6; break; }
786    fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer");
787    if (fe_rl_line_buffer==NULL) { res=7; break; }
788    fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches");
789    if (fe_completion_matches==NULL) { res=8; break; }
790    fe_rl_attempted_completion_function=
791      dynl_sym(fe_rl_hdl,"rl_attempted_completion_function");
792    if (fe_rl_attempted_completion_function==NULL) { res=9; break; }
793    fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream");
794    if (fe_rl_outstream==NULL) { res=10; break; }
795    fe_write_history=dynl_sym(fe_rl_hdl,"write_history");
796    if (fe_write_history==NULL) { res=11; break; }
797    fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes");
798    if (fe_history_total_bytes==NULL) { res=12; break; }
799    fe_using_history=dynl_sym(fe_rl_hdl,"using_history");
800    if (fe_using_history==NULL) { res=13; break; }
801    fe_read_history=dynl_sym(fe_rl_hdl,"read_history");
802    if (fe_read_history==NULL) { res=14; break; }
803    return 0;
804  }
805  dynl_close(fe_rl_hdl);
806  if (res==0)
807  {
808    char *p;
809    /* more init stuff: */
810    /* Allow conditional parsing of the ~/.inputrc file. */
811    (*fe_rl_readline_name) = "Singular";
812    /* Tell the completer that we want a crack first. */
813    (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion;
814    /* try to read a history */
815    (*fe_using_history)();
816    p = getenv("SINGULARHIST");
817    if (p != NULL)
818    {
819      (*fe_read_history) (p);
820    }
821  }
822  return res;
823}
824#endif
825
826/* ===================================================================*/
827/* =          fe_reset_input_mode (all possibilities)               = */
828/* ===================================================================*/
829#if defined(HAVE_READLINE) && !defined(HAVE_FEREAD) && !defined(HAVE_DYN_RL)
830extern int history_total_bytes();
831extern int write_history (const char *);
832#endif
833void fe_reset_input_mode ()
834{
835#if defined(HAVE_DYN_RL)
836  char *p = getenv("SINGULARHIST");
837  if ((p != NULL) && (fe_history_total_bytes != NULL))
838  {
839    if((*fe_history_total_bytes)()!=0)
840      (*fe_write_history) (p);
841  }
842#endif
843#if defined(HAVE_READLINE) && !defined(HAVE_FEREAD) && !defined(HAVE_DYN_RL)
844  char *p = getenv("SINGULARHIST");
845  if (p != NULL)
846  {
847    if(history_total_bytes()!=0)
848      write_history (p);
849  }
850#endif
851#if defined(HAVE_FEREAD)
852  #ifndef HAVE_ATEXIT
853  fe_reset_fe(NULL,NULL);
854  #else
855  fe_reset_fe();
856  #endif
857#endif
858}
859
Note: See TracBrowser for help on using the repository browser.