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