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