source: git/Singular/feread.cc @ 0e760d0

spielwiese
Last change on this file since 0e760d0 was 87ad70, checked in by Hans Schönemann <hannes@…>, 26 years ago
FEREAD updated git-svn-id: file:///usr/local/Singular/svn/trunk@2616 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 5.2 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: feread.cc,v 1.20 1998-10-29 13:15:14 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#ifdef HAVE_TCL
15#include "ipid.h"
16#endif
17
18
19#if defined(HAVE_READLINE) && !defined(HAVE_FEREAD)
20#include <unistd.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <sys/types.h>
24#include <sys/file.h>
25#include <sys/stat.h>
26#include <sys/errno.h>
27
28extern "C" {
29 #ifdef READLINE_READLINE_H_OK
30  #include <readline/readline.h>
31  #ifdef HAVE_READLINE_HISTORY_H
32   #include <readline/history.h>
33  #endif
34 #else /* declare everything we need explicitely and do not rely on includes */
35  extern char * rl_readline_name;
36  extern char *rl_line_buffer;
37  char *filename_completion_function();
38  typedef char **CPPFunction ();
39  extern char ** completion_matches ();
40  extern CPPFunction * rl_attempted_completion_function;
41  extern FILE * rl_outstream;
42  char * readline ();
43  void add_history ();
44  int write_history ();
45 #endif /* READLINE_READLINE_H_OK */
46}
47
48#ifndef STDOUT_FILENO
49#define STDOUT_FILENO 1
50#endif
51
52#include "ipshell.h"
53
54BOOLEAN fe_use_fgets=FALSE;
55
56
57/* Tell the GNU Readline library how to complete.  We want to try to complete
58   on command names  or on filenames if it is preceded by " */
59
60/* Generator function for command completion.  STATE lets us know whether
61*   to start from scratch; without any state (i.e. STATE == 0), then we
62*   start at the top of the list.
63*/
64char *command_generator (char *text, int state)
65{
66  static int list_index, len;
67  char *name;
68
69  /* If this is a new word to complete, initialize now.  This includes
70     saving the length of TEXT for efficiency, and initializing the index
71     variable to 0. */
72  if (state==0)
73  {
74    list_index = 1;
75    len = strlen (text);
76  }
77
78  /* Return the next name which partially matches from the command list. */
79  while ((name = cmds[list_index].name)!=NULL)
80  {
81    list_index++;
82
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
91/* Attempt to complete on the contents of TEXT.  START and END show the
92*   region of TEXT that contains the word to complete.  We can use the
93*   entire line in case we want to do some simple parsing.  Return the
94*   array of matches, or NULL if there aren't any.
95*/
96char ** singular_completion (char *text, int start, int end)
97{
98  /* If this word is not in a string, then it may be a command
99     to complete.  Otherwise it may be the name of a file in the current
100     directory. */
101  if (rl_line_buffer[start-1]=='"')
102    return completion_matches (text, filename_completion_function);
103  char **m=completion_matches (text, command_generator);
104  if (m==NULL)
105  {
106    m=(char **)malloc(2*sizeof(char*));
107    m[0]=(char *)malloc(end-start+2);
108    strncpy(m[0],text,end-start+1);
109    m[1]=NULL;
110  }
111  return m;
112}
113
114void fe_set_input_mode(void)
115{
116  /* Allow conditional parsing of the ~/.inputrc file. */
117  rl_readline_name = "Singular";
118  /* Tell the completer that we want a crack first. */
119  rl_attempted_completion_function = (CPPFunction *)singular_completion;
120
121  if(!fe_use_fgets)
122  {
123
124    /* set the output stream */
125    if(!isatty(STDOUT_FILENO))
126    {
127      #ifdef atarist
128        rl_outstream = fopen( "/dev/tty", "w" );
129      #else
130        rl_outstream = fopen( ttyname(fileno(stdin)), "w" );
131      #endif
132    }
133
134    /* try to read a history */
135    using_history();
136    char *p = getenv("SINGULARHIST");
137    if (p != NULL)
138    {
139      read_history (p);
140    }
141  }
142}
143
144void fe_reset_input_mode (void)
145{
146  char *p = getenv("SINGULARHIST");
147  if (p != NULL)
148  {
149    if(!feBatch && !fe_use_fgets && (history_total_bytes()!=0))
150      write_history (p);
151  }
152}
153
154char * fe_fgets_stdin_rl(char *pr,char *s, int size)
155{
156  if (feBatch)
157    return NULL;
158  if(fe_use_fgets)
159  {
160    #ifdef HAVE_TCL
161    if (tclmode)
162    {
163      if(currRing!=NULL) PrintTCLS('P',pr);
164      else               PrintTCLS('U',pr);
165    }
166    else
167    #endif
168    if (BVERBOSE(V_PROMPT))
169    {
170      PrintS(pr);
171    }
172    mflush();
173    return fgets(s,size,stdin);
174  }
175
176  if (!BVERBOSE(V_PROMPT))
177  {
178    pr="";
179  }
180  mflush();
181
182  char *line;
183  line = readline (pr);
184
185  if (line==NULL)
186    return NULL;
187
188  if (*line!='\0')
189  {
190    add_history (line);
191  }
192  int l=strlen(line);
193  if (l>=size-1)
194  {
195    strncpy(s,line,size);
196  }
197  else
198  {
199    strncpy(s,line,l);
200    s[l]='\n';
201    s[l+1]='\0';
202  }
203  free (line);
204
205  return s;
206}
207#endif
208/*--------------------------------------------------------------*/
209#if !defined(HAVE_READLINE) && defined(HAVE_FEREAD)
210extern "C" {
211char * fe_fgets_stdin_fe(char *pr,char *s, int size);
212}
213char * fe_fgets_stdin(char *pr,char *s, int size)
214{
215  if (feBatch)
216    return NULL;
217  if(fe_use_fgets)
218  {
219    #ifdef HAVE_TCL
220    if (tclmode)
221    {
222      if(currRing!=NULL) PrintTCLS('P',pr);
223      else               PrintTCLS('U',pr);
224    }
225    else
226    #endif
227    if (BVERBOSE(V_PROMPT))
228    {
229      PrintS(pr);
230    }
231    mflush();
232    return fgets(s,size,stdin);
233  }
234
235  if (!BVERBOSE(V_PROMPT))
236  {
237    pr="";
238  }
239  mflush();
240
241
242  return fe_fgets_stdin_fe(pr,s,size);
243}
244#endif
Note: See TracBrowser for help on using the repository browser.