1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: feread.cc,v 1.5 2005-04-30 16:38:26 Singular Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: input from ttys, simulating fgets |
---|
7 | */ |
---|
8 | |
---|
9 | #include "mod2.h" |
---|
10 | |
---|
11 | // ---------------------------------------- |
---|
12 | // system settings: |
---|
13 | |
---|
14 | #undef USE_READLINE4 |
---|
15 | |
---|
16 | |
---|
17 | //---------------------------------------- |
---|
18 | #ifdef ix86_Win |
---|
19 | #define READLINE_STATIC |
---|
20 | #endif |
---|
21 | #include "febase.h" |
---|
22 | #ifdef ix86_Win |
---|
23 | #define OM_NO_MALLOC_MACROS |
---|
24 | #endif |
---|
25 | #ifdef ppcMac_darwin |
---|
26 | #define OM_NO_MALLOC_MACROS |
---|
27 | #endif |
---|
28 | #include "omalloc.h" |
---|
29 | |
---|
30 | #include "../Singular/static.h" |
---|
31 | |
---|
32 | #ifdef HAVE_STATIC |
---|
33 | #undef HAVE_DYN_RL |
---|
34 | #endif |
---|
35 | |
---|
36 | #ifdef ix86_Linux |
---|
37 | #undef HAVE_FEREAD |
---|
38 | // not compatible with glibc2 |
---|
39 | #endif |
---|
40 | |
---|
41 | #if defined(HAVE_DYN_RL) |
---|
42 | #include <unistd.h> |
---|
43 | #endif |
---|
44 | |
---|
45 | static char * fe_fgets_stdin_init(char *pr,char *s, int size); |
---|
46 | char * (*fe_fgets_stdin)(char *pr,char *s, int size) |
---|
47 | = fe_fgets_stdin_init; |
---|
48 | |
---|
49 | /* ===================================================================*/ |
---|
50 | /* = static/dymanic readline = */ |
---|
51 | /* ===================================================================*/ |
---|
52 | #if defined(HAVE_READLINE) || defined(HAVE_DYN_RL) || defined(HAVE_LIBREADLINE) |
---|
53 | |
---|
54 | #include "../Singular/ipshell.h" |
---|
55 | |
---|
56 | #ifndef STDOUT_FILENO |
---|
57 | #define STDOUT_FILENO 1 |
---|
58 | #endif |
---|
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 | */ |
---|
64 | extern "C" |
---|
65 | char *command_generator (char *text, int state) |
---|
66 | { |
---|
67 | static int list_index, len; |
---|
68 | char *name; |
---|
69 | |
---|
70 | /* If this is a new word to complete, initialize now. This includes |
---|
71 | saving the length of TEXT for efficiency, and initializing the index |
---|
72 | variable to 0. */ |
---|
73 | if (state==0) |
---|
74 | { |
---|
75 | list_index = 1; |
---|
76 | len = strlen (text); |
---|
77 | } |
---|
78 | |
---|
79 | /* Return the next name which partially matches from the command list. */ |
---|
80 | while ((name = cmds[list_index].name)!=NULL) |
---|
81 | { |
---|
82 | list_index++; |
---|
83 | |
---|
84 | if (strncmp (name, text, len) == 0) |
---|
85 | return (strdup(name)); |
---|
86 | } |
---|
87 | |
---|
88 | /* If no names matched, then return NULL. */ |
---|
89 | return ((char *)NULL); |
---|
90 | } |
---|
91 | #endif |
---|
92 | |
---|
93 | /* ===================================================================*/ |
---|
94 | /* = static readline = */ |
---|
95 | /* ===================================================================*/ |
---|
96 | /* some procedure are shared with "dynamic readline" */ |
---|
97 | #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE) || defined(HAVE_DYN_RL)) && !defined(HAVE_FEREAD) |
---|
98 | #include <unistd.h> |
---|
99 | #include <stdio.h> |
---|
100 | #include <stdlib.h> |
---|
101 | #include <sys/types.h> |
---|
102 | #include <sys/file.h> |
---|
103 | #include <sys/stat.h> |
---|
104 | #include <sys/errno.h> |
---|
105 | |
---|
106 | // #undef READLINE_READLINE_H_OK |
---|
107 | |
---|
108 | extern "C" { |
---|
109 | typedef char * (*RL_PROC)(const char*,int); |
---|
110 | #ifdef READLINE_READLINE_H_OK |
---|
111 | #include <readline/readline.h> |
---|
112 | #ifdef HAVE_READLINE_HISTORY_H |
---|
113 | #include <readline/history.h> |
---|
114 | |
---|
115 | #endif |
---|
116 | #endif |
---|
117 | #ifdef RL_VERSION_MAJOR |
---|
118 | #if (RL_VERSION_MAJOR >= 4) |
---|
119 | #define USE_READLINE4 |
---|
120 | #endif |
---|
121 | #endif |
---|
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 (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 | typedef char * (*PROC)(); |
---|
144 | |
---|
145 | typedef char **RL_CPPFunction (const char*, int,int); |
---|
146 | } |
---|
147 | |
---|
148 | |
---|
149 | char * fe_fgets_stdin_rl(char *pr,char *s, int size); |
---|
150 | |
---|
151 | /* Tell the GNU Readline library how to complete. We want to try to complete |
---|
152 | on command names or on filenames if it is preceded by " */ |
---|
153 | |
---|
154 | /* Attempt to complete on the contents of TEXT. START and END show the |
---|
155 | * region of TEXT that contains the word to complete. We can use the |
---|
156 | * entire line in case we want to do some simple parsing. Return the |
---|
157 | * array of matches, or NULL if there aren't any. |
---|
158 | */ |
---|
159 | #if defined(HAVE_DYN_RL) |
---|
160 | extern "C" |
---|
161 | { |
---|
162 | int fe_init_dyn_rl(); |
---|
163 | char *(*fe_filename_completion_function)(); /* 3 */ |
---|
164 | char *(* fe_readline) (char *); /* 4 */ |
---|
165 | void (*fe_add_history) (char *); /* 5 */ |
---|
166 | char ** fe_rl_readline_name; /* 6 */ |
---|
167 | char **fe_rl_line_buffer; /* 7 */ |
---|
168 | char **(*fe_completion_matches)(); /* 8 */ |
---|
169 | CPPFunction **fe_rl_attempted_completion_function; /* 9 */ |
---|
170 | FILE ** fe_rl_outstream; /* 10 */ |
---|
171 | int (*fe_write_history) (); /* 11 */ |
---|
172 | int (*fe_history_total_bytes) (); /* 12 */ |
---|
173 | void (*fe_using_history) (); /* 13 */ |
---|
174 | int (*fe_read_history) (char *); /* 14 */ |
---|
175 | |
---|
176 | } |
---|
177 | #endif |
---|
178 | char ** singular_completion (char *text, int start, int end) |
---|
179 | { |
---|
180 | /* If this word is not in a string, then it may be a command |
---|
181 | to complete. Otherwise it may be the name of a file in the current |
---|
182 | directory. */ |
---|
183 | #ifdef HAVE_DYN_RL |
---|
184 | #define x_rl_line_buffer (*fe_rl_line_buffer) |
---|
185 | #define x_rl_completion_matches (*fe_completion_matches) |
---|
186 | #define x_rl_filename_completion_function (*fe_filename_completion_function) |
---|
187 | #else |
---|
188 | #define x_rl_line_buffer rl_line_buffer |
---|
189 | #define x_rl_completion_matches rl_completion_matches |
---|
190 | #define x_rl_filename_completion_function rl_filename_completion_function |
---|
191 | #endif |
---|
192 | if (x_rl_line_buffer[start-1]=='"') |
---|
193 | return x_rl_completion_matches (text, (RL_PROC)x_rl_filename_completion_function); |
---|
194 | char **m=x_rl_completion_matches (text, (RL_PROC)command_generator); |
---|
195 | #undef x_rl_line_buffer |
---|
196 | #undef x_rl_completion_matches |
---|
197 | if (m==NULL) |
---|
198 | { |
---|
199 | m=(char **)malloc(2*sizeof(char*)); |
---|
200 | m[0]=(char *)malloc(end-start+2); |
---|
201 | strncpy(m[0],text,end-start+1); |
---|
202 | m[1]=NULL; |
---|
203 | } |
---|
204 | return m; |
---|
205 | } |
---|
206 | |
---|
207 | #ifndef HAVE_DYN_RL |
---|
208 | char * fe_fgets_stdin_rl(char *pr,char *s, int size) |
---|
209 | { |
---|
210 | if (!BVERBOSE(V_PROMPT)) |
---|
211 | { |
---|
212 | pr=""; |
---|
213 | } |
---|
214 | mflush(); |
---|
215 | |
---|
216 | char *line; |
---|
217 | line = readline (pr); |
---|
218 | |
---|
219 | if (line==NULL) |
---|
220 | return NULL; |
---|
221 | |
---|
222 | if (*line!='\0') |
---|
223 | { |
---|
224 | add_history (line); |
---|
225 | } |
---|
226 | int l=strlen(line); |
---|
227 | if (l>=size-1) |
---|
228 | { |
---|
229 | strncpy(s,line,size); |
---|
230 | } |
---|
231 | else |
---|
232 | { |
---|
233 | strncpy(s,line,l); |
---|
234 | s[l]='\n'; |
---|
235 | s[l+1]='\0'; |
---|
236 | } |
---|
237 | free (line); |
---|
238 | |
---|
239 | return s; |
---|
240 | } |
---|
241 | #endif |
---|
242 | #endif |
---|
243 | |
---|
244 | /* ===================================================================*/ |
---|
245 | /* = emulated readline = */ |
---|
246 | /* ===================================================================*/ |
---|
247 | #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD) |
---|
248 | extern "C" { |
---|
249 | char * fe_fgets_stdin_fe(char *pr,char *s, int size); |
---|
250 | } |
---|
251 | char * fe_fgets_stdin_emu(char *pr,char *s, int size) |
---|
252 | { |
---|
253 | if (!BVERBOSE(V_PROMPT)) |
---|
254 | { |
---|
255 | pr=""; |
---|
256 | } |
---|
257 | mflush(); |
---|
258 | return fe_fgets_stdin_fe(pr,s,size); |
---|
259 | } |
---|
260 | #endif |
---|
261 | |
---|
262 | /* ===================================================================*/ |
---|
263 | /* = dynamic readline = */ |
---|
264 | /* ===================================================================*/ |
---|
265 | /* some procedure are shared with "static readline" */ |
---|
266 | #if defined(HAVE_DYN_RL) |
---|
267 | char * fe_fgets_stdin_drl(char *pr,char *s, int size) |
---|
268 | { |
---|
269 | if (!BVERBOSE(V_PROMPT)) |
---|
270 | { |
---|
271 | pr=""; |
---|
272 | } |
---|
273 | mflush(); |
---|
274 | |
---|
275 | char *line; |
---|
276 | line = (*fe_readline) (pr); |
---|
277 | |
---|
278 | if (line==NULL) |
---|
279 | return NULL; |
---|
280 | |
---|
281 | if (*line!='\0') |
---|
282 | { |
---|
283 | (*fe_add_history) (line); |
---|
284 | } |
---|
285 | int l=strlen(line); |
---|
286 | if (l>=size-1) |
---|
287 | { |
---|
288 | strncpy(s,line,size); |
---|
289 | } |
---|
290 | else |
---|
291 | { |
---|
292 | strncpy(s,line,l); |
---|
293 | s[l]='\n'; |
---|
294 | s[l+1]='\0'; |
---|
295 | } |
---|
296 | free (line); |
---|
297 | |
---|
298 | return s; |
---|
299 | } |
---|
300 | #endif |
---|
301 | |
---|
302 | /* ===================================================================*/ |
---|
303 | /* = fgets = */ |
---|
304 | /* ===================================================================*/ |
---|
305 | char * fe_fgets(char *pr,char *s, int size) |
---|
306 | { |
---|
307 | if (BVERBOSE(V_PROMPT)) |
---|
308 | { |
---|
309 | fprintf(stdout,pr); |
---|
310 | } |
---|
311 | mflush(); |
---|
312 | return fgets(s,size,stdin); |
---|
313 | } |
---|
314 | |
---|
315 | /* ===================================================================*/ |
---|
316 | /* = init for static rl, dyn. rl, emu. rl = */ |
---|
317 | /* ===================================================================*/ |
---|
318 | static char * fe_fgets_stdin_init(char *pr,char *s, int size) |
---|
319 | { |
---|
320 | #if (defined(HAVE_READLINE) || defined(HAVE_LIBREADLINE)) && !defined(HAVE_DYN_RL) && !defined(HAVE_FEREAD) |
---|
321 | /* Allow conditional parsing of the ~/.inputrc file. */ |
---|
322 | rl_readline_name = "Singular"; |
---|
323 | /* Tell the completer that we want a crack first. */ |
---|
324 | #ifdef USE_READLINE4 |
---|
325 | rl_attempted_completion_function = (rl_completion_func_t *)singular_completion; |
---|
326 | #else |
---|
327 | rl_attempted_completion_function = (CPPFunction *)singular_completion; |
---|
328 | #endif |
---|
329 | |
---|
330 | /* set the output stream */ |
---|
331 | if(!isatty(STDOUT_FILENO)) |
---|
332 | { |
---|
333 | #ifdef atarist |
---|
334 | rl_outstream = fopen( "/dev/tty", "w" ); |
---|
335 | #else |
---|
336 | rl_outstream = fopen( ttyname(fileno(stdin)), "w" ); |
---|
337 | #endif |
---|
338 | } |
---|
339 | |
---|
340 | /* try to read a history */ |
---|
341 | using_history(); |
---|
342 | char *p = getenv("SINGULARHIST"); |
---|
343 | if (p != NULL) |
---|
344 | { |
---|
345 | read_history (p); |
---|
346 | } |
---|
347 | fe_fgets_stdin=fe_fgets_stdin_rl; |
---|
348 | return(fe_fgets_stdin_rl(pr,s,size)); |
---|
349 | #endif |
---|
350 | #ifdef HAVE_DYN_RL |
---|
351 | /* do dynamic loading */ |
---|
352 | int res=fe_init_dyn_rl(); |
---|
353 | if (res!=0) |
---|
354 | { |
---|
355 | //if (res==1) |
---|
356 | // WarnS("dynamic loading of libreadline failed"); |
---|
357 | //else |
---|
358 | // Warn("dynamic loading failed: %d\n",res); |
---|
359 | if (res!=1) |
---|
360 | Warn("dynamic loading failed: %d\n",res); |
---|
361 | #ifdef HAVE_FEREAD |
---|
362 | fe_fgets_stdin=fe_fgets_stdin_emu; |
---|
363 | #else |
---|
364 | fe_fgets_stdin=fe_fgets; |
---|
365 | #endif |
---|
366 | return fe_fgets_stdin(pr,s,size); |
---|
367 | } |
---|
368 | else /* could load libreadline: */ |
---|
369 | { |
---|
370 | /* Allow conditional parsing of the ~/.inputrc file. */ |
---|
371 | *fe_rl_readline_name = "Singular"; |
---|
372 | /* Tell the completer that we want a crack first. */ |
---|
373 | *fe_rl_attempted_completion_function = (CPPFunction *)singular_completion; |
---|
374 | /* try to read a history */ |
---|
375 | (*fe_using_history)(); |
---|
376 | char *p = getenv("SINGULARHIST"); |
---|
377 | if (p != NULL) |
---|
378 | { |
---|
379 | (*fe_read_history) (p); |
---|
380 | } |
---|
381 | } |
---|
382 | |
---|
383 | /* set the output stream */ |
---|
384 | if(!isatty(STDOUT_FILENO)) |
---|
385 | { |
---|
386 | #ifdef atarist |
---|
387 | *fe_rl_outstream = fopen( "/dev/tty", "w" ); |
---|
388 | #else |
---|
389 | *fe_rl_outstream = fopen( ttyname(fileno(stdin)), "w" ); |
---|
390 | #endif |
---|
391 | } |
---|
392 | |
---|
393 | fe_fgets_stdin=fe_fgets_stdin_drl; |
---|
394 | return fe_fgets_stdin_drl(pr,s,size); |
---|
395 | #else |
---|
396 | #if !defined(HAVE_READLINE) && defined(HAVE_FEREAD) |
---|
397 | fe_fgets_stdin=fe_fgets_stdin_emu; |
---|
398 | return(fe_fgets_stdin_emu(pr,s,size)); |
---|
399 | #else |
---|
400 | fe_fgets_stdin=fe_fgets; |
---|
401 | return(fe_fgets(pr,s,size)); |
---|
402 | #endif |
---|
403 | #endif |
---|
404 | } |
---|
405 | |
---|
406 | /* ===================================================================*/ |
---|
407 | /* = TCL = */ |
---|
408 | /* ===================================================================*/ |
---|
409 | #ifdef HAVE_TCL |
---|
410 | /* tcl: */ |
---|
411 | char * fe_fgets_tcl(char *pr,char *s, int size) |
---|
412 | { |
---|
413 | if(currRing!=NULL) PrintTCLS('P',pr); |
---|
414 | else PrintTCLS('U',pr); |
---|
415 | mflush(); |
---|
416 | return fgets(s,size,stdin); |
---|
417 | } |
---|
418 | |
---|
419 | #endif |
---|
420 | |
---|
421 | /* ===================================================================*/ |
---|
422 | /* = batch mode = */ |
---|
423 | /* ===================================================================*/ |
---|
424 | /* dummy (for batch mode): */ |
---|
425 | char * fe_fgets_dummy(char *pr,char *s, int size) |
---|
426 | { |
---|
427 | return NULL; |
---|
428 | } |
---|
429 | |
---|