source: git/Singular/fereadl.c @ 52d480

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