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 <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 | |
---|
61 | struct termios fe_saved_attributes; |
---|
62 | |
---|
63 | static BOOLEAN fe_stdout_is_tty; |
---|
64 | static BOOLEAN fe_stdin_is_tty; |
---|
65 | BOOLEAN fe_use_fgets=FALSE; |
---|
66 | static BOOLEAN fe_is_initialized=FALSE; |
---|
67 | static int pagelength = 24; |
---|
68 | |
---|
69 | FILE * fe_echo; /*the output file for echoed characters*/ |
---|
70 | |
---|
71 | #define fe_hist_max 32 |
---|
72 | char ** fe_hist=NULL; |
---|
73 | short fe_hist_pos; |
---|
74 | BOOLEAN fe_is_raw_tty=0; |
---|
75 | int fe_cursor_pos; /* 0..colmax-1*/ |
---|
76 | int 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 | } |
---|
112 | void 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 | } |
---|
120 | void 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 | |
---|
136 | static char termcap_buff[2048]; |
---|
137 | static int fe_out_char(int c) |
---|
138 | { |
---|
139 | fputc(c,fe_echo); |
---|
140 | return c; |
---|
141 | } |
---|
142 | void 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 */ |
---|
237 | static 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 */ |
---|
253 | static 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 | */ |
---|
269 | static 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 | |
---|
301 | static 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 | |
---|
319 | static 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 | read (STDIN_FILENO, &c, 1); |
---|
328 | if (c == '[') |
---|
329 | { |
---|
330 | /* get command character */ |
---|
331 | c='\0'; |
---|
332 | read (STDIN_FILENO, &c, 1); |
---|
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 | |
---|
353 | static 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 | |
---|
374 | char * 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 | #ifdef hpux |
---|
418 | sel = select (STDIN_FILENO+1, (int *)fdset.fds_bits, NULL, NULL, &tv); |
---|
419 | #else |
---|
420 | sel = select (STDIN_FILENO+1, &fdset, NULL, NULL, &tv); |
---|
421 | #endif |
---|
422 | if (sel==0) |
---|
423 | fe_temp_reset(); |
---|
424 | return s; |
---|
425 | } |
---|
426 | case feCTRL('H'): |
---|
427 | case 127: /*delete the character left of the cursor*/ |
---|
428 | { |
---|
429 | if (i==0) break; |
---|
430 | i--; |
---|
431 | fe_cursor_pos--; |
---|
432 | if(fe_cursor_pos<0) |
---|
433 | { |
---|
434 | fe_cursor_line--; |
---|
435 | fe_cursor_pos=colmax-1; |
---|
436 | fe_set_cursor(s,i); |
---|
437 | } |
---|
438 | else |
---|
439 | { |
---|
440 | fputc('\b',fe_echo); |
---|
441 | } |
---|
442 | /* NO BREAK : next: feCTRL('D') */ |
---|
443 | } |
---|
444 | case feCTRL('D'): /*delete the character under the cursor or eof*/ |
---|
445 | { |
---|
446 | int j; |
---|
447 | if ((i==0) &&(s[0]=='\0')) return NULL; /*eof*/ |
---|
448 | if (s[i]!='\0') |
---|
449 | { |
---|
450 | j=i; |
---|
451 | while(s[j]!='\0') |
---|
452 | { |
---|
453 | s[j]=s[j+1]; |
---|
454 | fputc(s[j],fe_echo); |
---|
455 | j++; |
---|
456 | } |
---|
457 | fputc(' ',fe_echo); |
---|
458 | if (fe_cursor_pos+(j-i)>=colmax) |
---|
459 | { |
---|
460 | fe_set_cursor(s,i); |
---|
461 | } |
---|
462 | else |
---|
463 | { |
---|
464 | while(j>i) |
---|
465 | { |
---|
466 | fputc('\b',fe_echo); |
---|
467 | j--; |
---|
468 | } |
---|
469 | } |
---|
470 | } |
---|
471 | change=1; |
---|
472 | fflush(fe_echo); |
---|
473 | break; |
---|
474 | } |
---|
475 | case feCTRL('A'): /* move the cursor to the beginning of the line */ |
---|
476 | { |
---|
477 | if (i>=colmax-strlen(pr)) |
---|
478 | { |
---|
479 | while (i>=colmax-strlen(pr)) |
---|
480 | { |
---|
481 | i-=colmax; |
---|
482 | fe_cursor_line--; |
---|
483 | } |
---|
484 | i=0; |
---|
485 | fe_cursor_pos=strlen(pr); |
---|
486 | fe_set_cursor(s,i); |
---|
487 | } |
---|
488 | else |
---|
489 | { |
---|
490 | while(i>0) |
---|
491 | { |
---|
492 | i--; |
---|
493 | fputc('\b',fe_echo); |
---|
494 | } |
---|
495 | fe_cursor_pos=strlen(pr); |
---|
496 | } |
---|
497 | break; |
---|
498 | } |
---|
499 | case feCTRL('E'): /* move the cursor to the end of the line */ |
---|
500 | { |
---|
501 | while(s[i]!='\0') |
---|
502 | { |
---|
503 | fputc(s[i],fe_echo); |
---|
504 | i++; |
---|
505 | fe_cursor_pos++; |
---|
506 | if(fe_cursor_pos>=colmax) |
---|
507 | { |
---|
508 | fe_cursor_pos=0; |
---|
509 | if(fe_cursor_line!=(pagelength-1)) |
---|
510 | fe_cursor_line++; |
---|
511 | } |
---|
512 | } |
---|
513 | break; |
---|
514 | } |
---|
515 | case feCTRL('B'): /* move the cursor backward one character */ |
---|
516 | { |
---|
517 | if (i>0) |
---|
518 | { |
---|
519 | i--; |
---|
520 | fputc('\b',fe_echo); |
---|
521 | fe_cursor_pos--; |
---|
522 | if(fe_cursor_pos<0) |
---|
523 | { |
---|
524 | fe_cursor_pos=colmax-1; |
---|
525 | fe_cursor_line--; |
---|
526 | } |
---|
527 | } |
---|
528 | break; |
---|
529 | } |
---|
530 | case feCTRL('F'): /* move the cursor forward one character */ |
---|
531 | { |
---|
532 | if(s[i]!='\0') |
---|
533 | { |
---|
534 | fputc(s[i],fe_echo); |
---|
535 | i++; |
---|
536 | fe_cursor_pos++; |
---|
537 | if(fe_cursor_pos>=colmax) |
---|
538 | { |
---|
539 | fe_cursor_pos=0; |
---|
540 | if(fe_cursor_line!=(pagelength-1)) |
---|
541 | fe_cursor_line++; |
---|
542 | } |
---|
543 | } |
---|
544 | break; |
---|
545 | } |
---|
546 | case feCTRL('U'): /* delete entire input line */ |
---|
547 | { |
---|
548 | fe_ctrl_u(s,&i); |
---|
549 | fe_cursor_pos=strlen(pr); |
---|
550 | memset(s,0,size); |
---|
551 | change=1; |
---|
552 | break; |
---|
553 | } |
---|
554 | #if 0 |
---|
555 | case feCTRL('W'): /* test hist. */ |
---|
556 | { |
---|
557 | int i; |
---|
558 | PrintS("\nstart hist\n"); |
---|
559 | for(i=0;i<fe_hist_max;i++) |
---|
560 | { |
---|
561 | if(fe_hist[i]!=NULL) |
---|
562 | { |
---|
563 | Print("%2d ",i); |
---|
564 | if(i==fe_hist_pos) PrintS("-"); else PrintS(" "); |
---|
565 | if(i==h) PrintS(">"); else PrintS(" "); |
---|
566 | PrintS(fe_hist[i]); |
---|
567 | PrintLn(); |
---|
568 | } |
---|
569 | } |
---|
570 | Print("end hist, next_pos=%d\n",fe_hist_pos); |
---|
571 | break; |
---|
572 | } |
---|
573 | #endif |
---|
574 | case feCTRL('K'): /* delete up to the end of the line */ |
---|
575 | { |
---|
576 | fe_ctrl_k(s,i); |
---|
577 | memset(&(s[i]),'\0',size-i); |
---|
578 | /* s[i]='\0';*/ |
---|
579 | change=1; |
---|
580 | break; |
---|
581 | } |
---|
582 | case feCTRL('L'): /* redraw screen */ |
---|
583 | { |
---|
584 | char t_buf[40]; |
---|
585 | char *t=t_buf; |
---|
586 | fe_cursor_line=i/colmax; |
---|
587 | /*fputs(tgetstr("cl",&t),fe_echo);*/ |
---|
588 | tputs(tgetstr("cl",&t),pagelength,fe_out_char); |
---|
589 | fflush(fe_echo); |
---|
590 | fputs(pr,fe_echo); |
---|
591 | fputs(s,fe_echo); |
---|
592 | fe_set_cursor(s,i); |
---|
593 | break; |
---|
594 | } |
---|
595 | case feCTRL('P'): /* previous line */ |
---|
596 | { |
---|
597 | fe_ctrl_u(s,&i); |
---|
598 | fe_get_hist(s,size,&h,change,-1); |
---|
599 | while(s[i]!='\0') |
---|
600 | { |
---|
601 | fputc(s[i],fe_echo); |
---|
602 | i++; |
---|
603 | } |
---|
604 | fe_cursor_pos=strlen(pr)+i/*strlen(s)*/; |
---|
605 | change=0; |
---|
606 | break; |
---|
607 | } |
---|
608 | case feCTRL('N'): /* next line */ |
---|
609 | { |
---|
610 | fe_ctrl_u(s,&i); |
---|
611 | fe_get_hist(s,size,&h,change,1); |
---|
612 | while(s[i]!='\0') |
---|
613 | { |
---|
614 | fputc(s[i],fe_echo); |
---|
615 | i++; |
---|
616 | } |
---|
617 | fe_cursor_pos=strlen(pr)+i/*strlen(s)*/; |
---|
618 | change=0; |
---|
619 | break; |
---|
620 | } |
---|
621 | default: |
---|
622 | { |
---|
623 | if ((c>=' ')&&(c<=126)) |
---|
624 | { |
---|
625 | fputc (c,fe_echo); |
---|
626 | fe_cursor_pos++; |
---|
627 | if(fe_cursor_pos>=colmax) |
---|
628 | { |
---|
629 | fe_cursor_pos=0; |
---|
630 | if(fe_cursor_line!=(pagelength-1)) |
---|
631 | fe_cursor_line++; |
---|
632 | } |
---|
633 | if (s[i]!='\0') |
---|
634 | { |
---|
635 | /* shift by 1 to the right */ |
---|
636 | int j=i; |
---|
637 | int l; |
---|
638 | while ((s[j]!='\0')&&(j<size-2)) j++; |
---|
639 | l=j-i; |
---|
640 | while (j>i) { s[j]=s[j-1]; j--; } |
---|
641 | /* display */ |
---|
642 | fwrite(s+i+1,l,1,fe_echo); |
---|
643 | fflush(fe_echo); |
---|
644 | /* set cursor */ |
---|
645 | if(fe_cursor_pos+l>=colmax) |
---|
646 | { |
---|
647 | while(fe_cursor_pos+l>=colmax) |
---|
648 | { |
---|
649 | fe_cursor_line--; |
---|
650 | l-=colmax; |
---|
651 | } |
---|
652 | fe_set_cursor(s,i); |
---|
653 | } |
---|
654 | else |
---|
655 | { |
---|
656 | while(l>0) |
---|
657 | { |
---|
658 | l--; |
---|
659 | fputc('\b',fe_echo); |
---|
660 | } |
---|
661 | } |
---|
662 | fflush(fe_echo); |
---|
663 | } |
---|
664 | if (i<size-1) s[i]=c; |
---|
665 | i++; |
---|
666 | change=1; |
---|
667 | } |
---|
668 | } |
---|
669 | } /* switch */ |
---|
670 | fflush(fe_echo); |
---|
671 | } /* loop */ |
---|
672 | } |
---|
673 | /*else*/ |
---|
674 | return fgets(s,size,stdin); |
---|
675 | } |
---|
676 | |
---|
677 | //int main (void) |
---|
678 | //{ |
---|
679 | // char b[200]; |
---|
680 | // char * m_eof; |
---|
681 | // |
---|
682 | // fe_init(); |
---|
683 | // while(1) |
---|
684 | // { |
---|
685 | // m_eof=fe_fgets_stdin_fe("> ",b,200); |
---|
686 | // if (!m_eof) break; |
---|
687 | // printf(">>%s<<\n",b); |
---|
688 | // } |
---|
689 | // |
---|
690 | // return 0; |
---|
691 | //} |
---|
692 | #endif |
---|
693 | |
---|
694 | /* ================================================================ */ |
---|
695 | #if defined(HAVE_DYN_RL) |
---|
696 | #include <unistd.h> |
---|
697 | //#include <stdio.h> |
---|
698 | //#include <stdlib.h> |
---|
699 | //#include <sys/types.h> |
---|
700 | //#include <sys/file.h> |
---|
701 | //#include <sys/stat.h> |
---|
702 | //#include <sys/errno.h> |
---|
703 | //#include <dlfcn.h> |
---|
704 | #include <kernel/mod_raw.h> |
---|
705 | |
---|
706 | typedef char **CPPFunction (); |
---|
707 | |
---|
708 | char *(*fe_filename_completion_function)(); /* 3 */ |
---|
709 | char *(* fe_readline) (); /* 4 */ |
---|
710 | void (*fe_add_history) (); /* 5 */ |
---|
711 | char ** fe_rl_readline_name; /* 6 */ |
---|
712 | char **fe_rl_line_buffer; /* 7 */ |
---|
713 | char **(*fe_completion_matches)(); /* 8 */ |
---|
714 | CPPFunction **fe_rl_attempted_completion_function; /* 9 */ |
---|
715 | FILE ** fe_rl_outstream; /* 10 */ |
---|
716 | int (*fe_write_history) (); /* 11 */ |
---|
717 | int (*fe_history_total_bytes) (); /* 12 */ |
---|
718 | void (*fe_using_history) (); /* 13 */ |
---|
719 | int (*fe_read_history) (); /* 14 */ |
---|
720 | |
---|
721 | void * fe_rl_hdl=NULL; |
---|
722 | |
---|
723 | char *command_generator (char *text, int state); |
---|
724 | |
---|
725 | /* Attempt to complete on the contents of TEXT. START and END show the |
---|
726 | * region of TEXT that contains the word to complete. We can use the |
---|
727 | * entire line in case we want to do some simple parsing. Return the |
---|
728 | * array of matches, or NULL if there aren't any. |
---|
729 | */ |
---|
730 | char ** singular_completion (char *text, int start, int end) |
---|
731 | { |
---|
732 | /* If this word is not in a string, then it may be a command |
---|
733 | to complete. Otherwise it may be the name of a file in the current |
---|
734 | directory. */ |
---|
735 | char **m; |
---|
736 | if ((*fe_rl_line_buffer)[start-1]=='"') |
---|
737 | return (*fe_completion_matches) (text, *fe_filename_completion_function); |
---|
738 | m=(*fe_completion_matches) (text, command_generator); |
---|
739 | if (m==NULL) |
---|
740 | { |
---|
741 | m=(char **)malloc(2*sizeof(char*)); |
---|
742 | m[0]=(char *)malloc(end-start+2); |
---|
743 | strncpy(m[0],text,end-start+1); |
---|
744 | m[1]=NULL; |
---|
745 | } |
---|
746 | return m; |
---|
747 | } |
---|
748 | |
---|
749 | |
---|
750 | int fe_init_dyn_rl() |
---|
751 | { |
---|
752 | int res=0; |
---|
753 | loop |
---|
754 | { |
---|
755 | #if defined(HPUX_9) || defined(HPUX_10) |
---|
756 | fe_rl_hdl=dynl_open("libreadline.sl"); |
---|
757 | if (fe_rl_hdl==NULL) |
---|
758 | fe_rl_hdl=dynl_open("/lib/libreadline.sl"); |
---|
759 | if (fe_rl_hdl==NULL) |
---|
760 | fe_rl_hdl=dynl_open("/usr/lib/libreadline.sl"); |
---|
761 | #else |
---|
762 | fe_rl_hdl=dynl_open("libreadline.so"); |
---|
763 | if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.2"); |
---|
764 | if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.3"); |
---|
765 | if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.4"); |
---|
766 | if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.5"); |
---|
767 | if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.6"); |
---|
768 | if (fe_rl_hdl==NULL) fe_rl_hdl=dynl_open("libreadline.so.7"); |
---|
769 | #endif |
---|
770 | if (fe_rl_hdl==NULL) { return 1;} |
---|
771 | |
---|
772 | fe_filename_completion_function= |
---|
773 | dynl_sym(fe_rl_hdl, "filename_completion_function"); |
---|
774 | if (fe_filename_completion_function==NULL) { res=3; break; } |
---|
775 | fe_readline=dynl_sym(fe_rl_hdl,"readline"); |
---|
776 | if (fe_readline==NULL) { res=4; break; } |
---|
777 | fe_add_history=dynl_sym(fe_rl_hdl,"add_history"); |
---|
778 | if (fe_add_history==NULL) { res=5; break; } |
---|
779 | fe_rl_readline_name=(char**)dynl_sym(fe_rl_hdl,"rl_readline_name"); |
---|
780 | if (fe_rl_readline_name==NULL) { res=6; break; } |
---|
781 | fe_rl_line_buffer=(char**)dynl_sym(fe_rl_hdl,"rl_line_buffer"); |
---|
782 | if (fe_rl_line_buffer==NULL) { res=7; break; } |
---|
783 | fe_completion_matches=dynl_sym(fe_rl_hdl,"completion_matches"); |
---|
784 | if (fe_completion_matches==NULL) { res=8; break; } |
---|
785 | fe_rl_attempted_completion_function= |
---|
786 | dynl_sym(fe_rl_hdl,"rl_attempted_completion_function"); |
---|
787 | if (fe_rl_attempted_completion_function==NULL) { res=9; break; } |
---|
788 | fe_rl_outstream=(FILE**)dynl_sym(fe_rl_hdl,"rl_outstream"); |
---|
789 | if (fe_rl_outstream==NULL) { res=10; break; } |
---|
790 | fe_write_history=dynl_sym(fe_rl_hdl,"write_history"); |
---|
791 | if (fe_write_history==NULL) { res=11; break; } |
---|
792 | fe_history_total_bytes=dynl_sym(fe_rl_hdl,"history_total_bytes"); |
---|
793 | if (fe_history_total_bytes==NULL) { res=12; break; } |
---|
794 | fe_using_history=dynl_sym(fe_rl_hdl,"using_history"); |
---|
795 | if (fe_using_history==NULL) { res=13; break; } |
---|
796 | fe_read_history=dynl_sym(fe_rl_hdl,"read_history"); |
---|
797 | if (fe_read_history==NULL) { res=14; break; } |
---|
798 | return 0; |
---|
799 | } |
---|
800 | dynl_close(fe_rl_hdl); |
---|
801 | if (res==0) |
---|
802 | { |
---|
803 | char *p; |
---|
804 | /* more init stuff: */ |
---|
805 | /* Allow conditional parsing of the ~/.inputrc file. */ |
---|
806 | (*fe_rl_readline_name) = "Singular"; |
---|
807 | /* Tell the completer that we want a crack first. */ |
---|
808 | (*fe_rl_attempted_completion_function) = (CPPFunction *)singular_completion; |
---|
809 | /* try to read a history */ |
---|
810 | (*fe_using_history)(); |
---|
811 | p = getenv("SINGULARHIST"); |
---|
812 | if (p != NULL) |
---|
813 | { |
---|
814 | (*fe_read_history) (p); |
---|
815 | } |
---|
816 | } |
---|
817 | return res; |
---|
818 | } |
---|
819 | #endif |
---|
820 | |
---|
821 | /* ===================================================================*/ |
---|
822 | /* = fe_reset_input_mode (all possibilities) = */ |
---|
823 | /* ===================================================================*/ |
---|
824 | #if defined(HAVE_READLINE) && !defined(HAVE_FEREAD) && !defined(HAVE_DYN_RL) |
---|
825 | extern int history_total_bytes(); |
---|
826 | extern int write_history (const char *); |
---|
827 | #endif |
---|
828 | void fe_reset_input_mode () |
---|
829 | { |
---|
830 | #if defined(HAVE_DYN_RL) |
---|
831 | char *p = getenv("SINGULARHIST"); |
---|
832 | if ((p != NULL) && (fe_history_total_bytes != NULL)) |
---|
833 | { |
---|
834 | if((*fe_history_total_bytes)()!=0) |
---|
835 | (*fe_write_history) (p); |
---|
836 | } |
---|
837 | #endif |
---|
838 | #if defined(HAVE_READLINE) && !defined(HAVE_FEREAD) && !defined(HAVE_DYN_RL) |
---|
839 | char *p = getenv("SINGULARHIST"); |
---|
840 | if (p != NULL) |
---|
841 | { |
---|
842 | if(history_total_bytes()!=0) |
---|
843 | write_history (p); |
---|
844 | } |
---|
845 | #endif |
---|
846 | #if defined(HAVE_FEREAD) |
---|
847 | #ifndef HAVE_ATEXIT |
---|
848 | fe_reset_fe(NULL,NULL); |
---|
849 | #else |
---|
850 | fe_reset_fe(); |
---|
851 | #endif |
---|
852 | #endif |
---|
853 | } |
---|
854 | |
---|