Shells in OCaml
3
fork

Configure Feed

Select the types of activity you want to include in your feed.

Reverse search

This adds the upstream OCaml community support for reverse search.

+539 -681
+536 -630
vendor/ocaml-linenoise/src/linenoise_src.c
··· 10 10 * 11 11 * ------------------------------------------------------------------------ 12 12 * 13 - * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com> 13 + * Copyright (c) 2010-2016, Salvatore Sanfilippo <antirez at gmail dot com> 14 14 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com> 15 15 * 16 16 * All rights reserved. ··· 47 47 * Todo list: 48 48 * - Filter bogus Ctrl+<char> combinations. 49 49 * - Win32 support 50 - * 51 - * Bloat: 52 - * - History search like Ctrl+r in readline? 53 50 * 54 51 * List of escape sequences used by this program, we do everything just 55 52 * with three sequences. In order to be so cheap we may have some ··· 119 116 120 117 #define LINENOISE_DEFAULT_HISTORY_MAX_LEN 100 121 118 #define LINENOISE_MAX_LINE 4096 122 - #define UNUSED(x) (void)(x) 123 119 static char *unsupported_term[] = {"dumb","cons25","emacs",NULL}; 124 120 static linenoiseCompletionCallback *completionCallback = NULL; 125 121 static linenoiseHintsCallback *hintsCallback = NULL; 126 122 static linenoiseFreeHintsCallback *freeHintsCallback = NULL; 127 - static char *linenoiseNoTTY(void); 128 - static void refreshLineWithCompletion(struct linenoiseState *ls, linenoiseCompletions *lc, int flags); 129 - static void refreshLineWithFlags(struct linenoiseState *l, int flags); 130 123 131 124 static struct termios orig_termios; /* In order to restore at exit.*/ 132 - static int maskmode = 0; /* Show "***" instead of input. For passwords. */ 133 125 static int rawmode = 0; /* For atexit() function to check if restore is needed*/ 134 126 static int mlmode = 0; /* Multi line mode. Default is single line. */ 135 127 static int atexit_registered = 0; /* Register atexit just 1 time. */ ··· 139 131 140 132 int linenoiseWasInterrupted = 0; 141 133 134 + /* The linenoiseState structure represents the state during line editing. 135 + * We pass this state to functions implementing specific editing 136 + * functionalities. */ 137 + struct linenoiseState { 138 + int ifd; /* Terminal stdin file descriptor. */ 139 + int ofd; /* Terminal stdout file descriptor. */ 140 + char *buf; /* Edited line buffer. */ 141 + size_t buflen; /* Edited line buffer size. */ 142 + const char *prompt; /* Prompt to display. */ 143 + size_t plen; /* Prompt length. */ 144 + size_t pos; /* Current cursor position. */ 145 + size_t oldpos; /* Previous refresh cursor position. */ 146 + size_t len; /* Current edited line length. */ 147 + size_t cols; /* Number of columns in terminal. */ 148 + size_t maxrows; /* Maximum num of rows used so far (multiline mode) */ 149 + int history_index; /* The history index we are currently editing. */ 150 + }; 151 + 142 152 enum KEY_ACTION{ 143 153 KEY_NULL = 0, /* NULL */ 144 154 CTRL_A = 1, /* Ctrl+a */ ··· 147 157 CTRL_D = 4, /* Ctrl-d */ 148 158 CTRL_E = 5, /* Ctrl-e */ 149 159 CTRL_F = 6, /* Ctrl-f */ 160 + CTRL_G = 7, /* Ctrl-g */ 150 161 CTRL_H = 8, /* Ctrl-h */ 151 162 TAB = 9, /* Tab */ 152 163 CTRL_K = 11, /* Ctrl+k */ ··· 154 165 ENTER = 13, /* Enter */ 155 166 CTRL_N = 14, /* Ctrl-n */ 156 167 CTRL_P = 16, /* Ctrl-p */ 168 + CTRL_R = 18, /* Ctrl-r */ 157 169 CTRL_T = 20, /* Ctrl-t */ 158 170 CTRL_U = 21, /* Ctrl+u */ 159 171 CTRL_W = 23, /* Ctrl+w */ ··· 163 175 164 176 static void linenoiseAtExit(void); 165 177 int linenoiseHistoryAdd(const char *line); 166 - #define REFRESH_CLEAN (1<<0) // Clean the old prompt from the screen 167 - #define REFRESH_WRITE (1<<1) // Rewrite the prompt on the screen. 168 - #define REFRESH_ALL (REFRESH_CLEAN|REFRESH_WRITE) // Do both. 169 178 static void refreshLine(struct linenoiseState *l); 179 + static void refreshLinePrompt(struct linenoiseState *l, const char *prompt); 170 180 171 181 /* Debugging macro. */ 172 182 #if 0 ··· 177 187 lndebug_fp = fopen("/tmp/lndebug.txt","a"); \ 178 188 fprintf(lndebug_fp, \ 179 189 "[%d %d %d] p: %d, rows: %d, rpos: %d, max: %d, oldmax: %d\n", \ 180 - (int)l->len,(int)l->pos,(int)l->oldcolpos,plen,rows,rpos, \ 181 - (int)l->oldrows,old_rows); \ 190 + (int)l->len,(int)l->pos,(int)l->oldpos,plen,rows,rpos, \ 191 + (int)l->maxrows,old_rows); \ 182 192 } \ 183 193 fprintf(lndebug_fp, ", " __VA_ARGS__); \ 184 194 fflush(lndebug_fp); \ ··· 187 197 #define lndebug(fmt, ...) 188 198 #endif 189 199 190 - /* ========================== Encoding functions ============================= */ 191 - 192 - /* Get byte length and column length of the previous character */ 193 - static size_t defaultPrevCharLen(const char *buf, size_t buf_len, size_t pos, size_t *col_len) { 194 - UNUSED(buf); UNUSED(buf_len); UNUSED(pos); 195 - if (col_len != NULL) *col_len = 1; 196 - return 1; 197 - } 198 - 199 - /* Get byte length and column length of the next character */ 200 - static size_t defaultNextCharLen(const char *buf, size_t buf_len, size_t pos, size_t *col_len) { 201 - UNUSED(buf); UNUSED(buf_len); UNUSED(pos); 202 - if (col_len != NULL) *col_len = 1; 203 - return 1; 204 - } 205 - 206 - /* Read bytes of the next character */ 207 - static size_t defaultReadCode(int fd, char *buf, size_t buf_len, int* c) { 208 - if (buf_len < 1) return -1; 209 - int nread = read(fd,&buf[0],1); 210 - if (nread == 1) *c = buf[0]; 211 - return nread; 212 - } 213 - 214 - /* Set default encoding functions */ 215 - static linenoisePrevCharLen *prevCharLen = defaultPrevCharLen; 216 - static linenoiseNextCharLen *nextCharLen = defaultNextCharLen; 217 - static linenoiseReadCode *readCode = defaultReadCode; 218 - 219 - /* Set used defined encoding functions */ 220 - void linenoiseSetEncodingFunctions( 221 - linenoisePrevCharLen *prevCharLenFunc, 222 - linenoiseNextCharLen *nextCharLenFunc, 223 - linenoiseReadCode *readCodeFunc) { 224 - prevCharLen = prevCharLenFunc; 225 - nextCharLen = nextCharLenFunc; 226 - readCode = readCodeFunc; 227 - } 228 - 229 - /* Get column length from begining of buffer to current byte position */ 230 - static size_t columnPos(const char *buf, size_t buf_len, size_t pos) { 231 - size_t ret = 0; 232 - size_t off = 0; 233 - while (off < pos) { 234 - size_t col_len; 235 - size_t len = nextCharLen(buf,buf_len,off,&col_len); 236 - off += len; 237 - ret += col_len; 238 - } 239 - return ret; 240 - } 241 - 242 - /* Get column length from begining of buffer to current byte position for multiline mode*/ 243 - static size_t columnPosForMultiLine(const char *buf, size_t buf_len, size_t pos, size_t cols, size_t ini_pos) { 244 - size_t ret = 0; 245 - size_t colwid = ini_pos; 246 - 247 - size_t off = 0; 248 - while (off < buf_len) { 249 - size_t col_len; 250 - size_t len = nextCharLen(buf,buf_len,off,&col_len); 251 - 252 - int dif = (int)(colwid + col_len) - (int)cols; 253 - if (dif > 0) { 254 - ret += dif; 255 - colwid = col_len; 256 - } else if (dif == 0) { 257 - colwid = 0; 258 - } else { 259 - colwid += col_len; 260 - } 261 - 262 - if (off >= pos) break; 263 - off += len; 264 - ret += col_len; 265 - } 266 - 267 - return ret; 268 - } 269 - 270 200 /* ======================= Low level terminal handling ====================== */ 271 201 272 - /* Enable "mask mode". When it is enabled, instead of the input that 273 - * the user is typing, the terminal will just display a corresponding 274 - * number of asterisks, like "****". This is useful for passwords and other 275 - * secrets that should not be displayed. */ 276 - void linenoiseMaskModeEnable(void) { 277 - maskmode = 1; 278 - } 279 - 280 - /* Disable mask mode. */ 281 - void linenoiseMaskModeDisable(void) { 282 - maskmode = 0; 283 - } 284 - 285 202 /* Set if to use or not the multi line mode. */ 286 203 void linenoiseSetMultiLine(int ml) { 287 204 mlmode = ml; ··· 326 243 raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */ 327 244 328 245 /* put terminal in raw mode after flushing */ 329 - if (tcsetattr(fd,TCSAFLUSH,&raw) < 0) goto fatal; 246 + if (tcsetattr(fd,TCSADRAIN,&raw) < 0) goto fatal; 330 247 rawmode = 1; 331 248 return 0; 332 249 ··· 337 254 338 255 static void disableRawMode(int fd) { 339 256 /* Don't even check the return value as it's too late. */ 340 - if (rawmode && tcsetattr(fd,TCSAFLUSH,&orig_termios) != -1) 257 + if (rawmode && tcsetattr(fd,TCSADRAIN,&orig_termios) != -1) 341 258 rawmode = 0; 342 259 } 343 260 ··· 426 343 free(lc->cvec); 427 344 } 428 345 429 - /* Called by completeLine() and linenoiseShow() to render the current 430 - * edited line with the proposed completion. If the current completion table 431 - * is already available, it is passed as second argument, otherwise the 432 - * function will use the callback to obtain it. 433 - * 434 - * Flags are the same as refreshLine*(), that is REFRESH_* macros. */ 435 - static void refreshLineWithCompletion(struct linenoiseState *ls, linenoiseCompletions *lc, int flags) { 436 - /* Obtain the table of completions if the caller didn't provide one. */ 437 - linenoiseCompletions ctable = { 0, NULL }; 438 - if (lc == NULL) { 439 - completionCallback(ls->buf,&ctable); 440 - lc = &ctable; 441 - } 442 - 443 - /* Show the edited line with completion if possible, or just refresh. */ 444 - if (ls->completion_idx < lc->len) { 445 - struct linenoiseState saved = *ls; 446 - ls->len = ls->pos = strlen(lc->cvec[ls->completion_idx]); 447 - ls->buf = lc->cvec[ls->completion_idx]; 448 - refreshLineWithFlags(ls,flags); 449 - ls->len = saved.len; 450 - ls->pos = saved.pos; 451 - ls->buf = saved.buf; 452 - } else { 453 - refreshLineWithFlags(ls,flags); 454 - } 455 - 456 - /* Free the completions table if needed. */ 457 - if (lc != &ctable) freeCompletions(&ctable); 458 - } 459 - 460 - /* This is an helper function for linenoiseEdit*() and is called when the 346 + /* This is an helper function for linenoiseEdit() and is called when the 461 347 * user types the <tab> key in order to complete the string currently in the 462 348 * input. 463 349 * 464 350 * The state of the editing is encapsulated into the pointed linenoiseState 465 - * structure as described in the structure definition. 466 - * 467 - * If the function returns non-zero, the caller should handle the 468 - * returned value as a byte read from the standard input, and process 469 - * it as usually: this basically means that the function may return a byte 470 - * read from the termianl but not processed. Otherwise, if zero is returned, 471 - * the input was consumed by the completeLine() function to navigate the 472 - * possible completions, and the caller should read for the next characters 473 - * from stdin. */ 474 - static int completeLine(struct linenoiseState *ls, int keypressed) { 351 + * structure as described in the structure definition. */ 352 + static int completeLine(struct linenoiseState *ls) { 475 353 linenoiseCompletions lc = { 0, NULL }; 476 - int nwritten; 477 - char c = keypressed; 354 + int nread, nwritten; 355 + char c = 0; 478 356 479 357 completionCallback(ls->buf,&lc); 480 358 if (lc.len == 0) { 481 359 linenoiseBeep(); 482 - ls->in_completion = 0; 483 360 } else { 484 - switch(c) { 485 - case 9: /* tab */ 486 - if (ls->in_completion == 0) { 487 - ls->in_completion = 1; 488 - ls->completion_idx = 0; 489 - } else { 490 - ls->completion_idx = (ls->completion_idx+1) % (lc.len+1); 491 - if (ls->completion_idx == lc.len) linenoiseBeep(); 492 - } 493 - c = 0; 494 - break; 495 - case 27: /* escape */ 496 - /* Re-show original buffer */ 497 - if (ls->completion_idx < lc.len) refreshLine(ls); 498 - ls->in_completion = 0; 499 - c = 0; 500 - break; 501 - default: 502 - /* Update buffer and return */ 503 - if (ls->completion_idx < lc.len) { 504 - nwritten = snprintf(ls->buf,ls->buflen,"%s", 505 - lc.cvec[ls->completion_idx]); 506 - ls->len = ls->pos = nwritten; 507 - } 508 - ls->in_completion = 0; 509 - break; 510 - } 361 + size_t stop = 0, i = 0; 511 362 512 - /* Show completion or original buffer */ 513 - if (ls->in_completion && ls->completion_idx < lc.len) { 514 - refreshLineWithCompletion(ls,&lc,REFRESH_ALL); 515 - } else { 516 - refreshLine(ls); 363 + while(!stop) { 364 + /* Show completion or original buffer */ 365 + if (i < lc.len) { 366 + struct linenoiseState saved = *ls; 367 + 368 + ls->len = ls->pos = strlen(lc.cvec[i]); 369 + ls->buf = lc.cvec[i]; 370 + refreshLine(ls); 371 + ls->len = saved.len; 372 + ls->pos = saved.pos; 373 + ls->buf = saved.buf; 374 + } else { 375 + refreshLine(ls); 376 + } 377 + 378 + nread = read(ls->ifd,&c,1); 379 + if (nread <= 0) { 380 + freeCompletions(&lc); 381 + return -1; 382 + } 383 + 384 + switch(c) { 385 + case 9: /* tab */ 386 + i = (i+1) % (lc.len+1); 387 + if (i == lc.len) linenoiseBeep(); 388 + break; 389 + case 27: /* escape */ 390 + /* Re-show original buffer */ 391 + if (i < lc.len) refreshLine(ls); 392 + stop = 1; 393 + break; 394 + default: 395 + /* Update buffer and return */ 396 + if (i < lc.len) { 397 + nwritten = snprintf(ls->buf,ls->buflen,"%s",lc.cvec[i]); 398 + ls->len = ls->pos = nwritten; 399 + } 400 + stop = 1; 401 + break; 402 + } 517 403 } 518 404 } 519 405 ··· 589 475 590 476 /* Helper of refreshSingleLine() and refreshMultiLine() to show hints 591 477 * to the right of the prompt. */ 592 - void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int pcollen) { 478 + void refreshShowHints(struct abuf *ab, struct linenoiseState *l, int plen) { 593 479 char seq[64]; 594 - size_t collen = pcollen+columnPos(l->buf,l->len,l->len); 595 - if (hintsCallback && collen < l->cols) { 480 + if (hintsCallback && plen+l->len < l->cols) { 596 481 int color = -1, bold = 0; 597 482 char *hint = hintsCallback(l->buf,&color,&bold); 598 483 if (hint) { 599 484 int hintlen = strlen(hint); 600 - int hintmaxlen = l->cols-collen; 485 + int hintmaxlen = l->cols-(plen+l->len); 601 486 if (hintlen > hintmaxlen) hintlen = hintmaxlen; 602 487 if (bold == 1 && color == -1) color = 37; 603 488 if (color != -1 || bold != 0) 604 489 snprintf(seq,64,"\033[%d;%d;49m",bold,color); 605 - else 606 - seq[0] = '\0'; 607 490 abAppend(ab,seq,strlen(seq)); 608 491 abAppend(ab,hint,hintlen); 609 492 if (color != -1 || bold != 0) ··· 614 497 } 615 498 } 616 499 617 - /* Check if text is an ANSI escape sequence 618 - */ 619 - static int isAnsiEscape(const char *buf, size_t buf_len, size_t* len) { 620 - if (buf_len > 2 && !memcmp("\033[", buf, 2)) { 621 - size_t off = 2; 622 - while (off < buf_len) { 623 - switch (buf[off++]) { 624 - case 'A': case 'B': case 'C': case 'D': case 'E': 625 - case 'F': case 'G': case 'H': case 'J': case 'K': 626 - case 'S': case 'T': case 'f': case 'm': 627 - *len = off; 628 - return 1; 629 - } 630 - } 631 - } 632 - return 0; 633 - } 634 - 635 - /* Get column length of prompt text 636 - */ 637 - static size_t promptTextColumnLen(const char *prompt, size_t plen) { 638 - char buf[LINENOISE_MAX_LINE]; 639 - size_t buf_len = 0; 640 - size_t off = 0; 641 - while (off < plen) { 642 - size_t len; 643 - if (isAnsiEscape(prompt + off, plen - off, &len)) { 644 - off += len; 645 - continue; 646 - } 647 - buf[buf_len++] = prompt[off++]; 648 - } 649 - return columnPos(buf,buf_len,buf_len); 650 - } 651 - 652 500 /* Single line low level line refresh. 653 501 * 654 502 * Rewrite the currently edited line accordingly to the buffer content, 655 - * cursor position, and number of columns of the terminal. 656 - * 657 - * Flags is REFRESH_* macros. The function can just remove the old 658 - * prompt, just write it, or both. */ 659 - static void refreshSingleLine(struct linenoiseState *l, int flags) { 503 + * cursor position, and number of columns of the terminal. */ 504 + static void refreshSingleLine(struct linenoiseState *l, const char *prompt) { 660 505 char seq[64]; 661 - size_t pcollen = promptTextColumnLen(l->prompt,strlen(l->prompt)); 506 + size_t plen = strlen(prompt); 662 507 int fd = l->ofd; 663 508 char *buf = l->buf; 664 509 size_t len = l->len; 665 510 size_t pos = l->pos; 666 511 struct abuf ab; 667 512 668 - while((pcollen+columnPos(buf,len,pos)) >= l->cols) { 669 - int chlen = nextCharLen(buf,len,0,NULL); 670 - buf += chlen; 671 - len -= chlen; 672 - pos -= chlen; 513 + if (plen >= l->cols) { 514 + len=0; // not enough room 515 + plen = l->cols; 673 516 } 674 - while (pcollen+columnPos(buf,len,len) > l->cols) { 675 - len -= prevCharLen(buf,len,len,NULL); 517 + 518 + while((plen+pos) >= l->cols && len>0) { 519 + buf++; 520 + len--; 521 + pos--; 522 + } 523 + while (plen+len > l->cols && len>0) { 524 + len--; 676 525 } 677 526 678 527 abInit(&ab); 679 528 /* Cursor to left edge */ 680 - snprintf(seq,sizeof(seq),"\r"); 529 + snprintf(seq,64,"\r"); 681 530 abAppend(&ab,seq,strlen(seq)); 682 - 683 - if (flags & REFRESH_WRITE) { 684 - /* Write the prompt and the current buffer content */ 685 - abAppend(&ab,l->prompt,strlen(l->prompt)); 686 - if (maskmode == 1) { 687 - while (len--) abAppend(&ab,"*",1); 688 - } else { 689 - abAppend(&ab,buf,len); 690 - } 691 - /* Show hits if any. */ 692 - refreshShowHints(&ab,l,pcollen); 693 - } 694 - 531 + /* Write the prompt and the current buffer content */ 532 + abAppend(&ab,prompt,plen); 533 + if (len > 0) 534 + abAppend(&ab,buf,len); 535 + /* Show hits if any. */ 536 + refreshShowHints(&ab,l,plen); 695 537 /* Erase to right */ 696 - snprintf(seq,sizeof(seq),"\x1b[0K"); 538 + snprintf(seq,64,"\x1b[0K"); 697 539 abAppend(&ab,seq,strlen(seq)); 698 - 699 - if (flags & REFRESH_WRITE) { 700 - /* Move cursor to original position. */ 701 - snprintf(seq,sizeof(seq),"\r\x1b[%dC", (int)(columnPos(buf,len,pos)+pcollen)); 702 - abAppend(&ab,seq,strlen(seq)); 703 - } 704 - 540 + /* Move cursor to original position. */ 541 + snprintf(seq,64,"\r\x1b[%dC", (int)(pos+plen)); 542 + abAppend(&ab,seq,strlen(seq)); 705 543 if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ 706 544 abFree(&ab); 707 545 } ··· 709 547 /* Multi line low level line refresh. 710 548 * 711 549 * Rewrite the currently edited line accordingly to the buffer content, 712 - * cursor position, and number of columns of the terminal. 713 - * 714 - * Flags is REFRESH_* macros. The function can just remove the old 715 - * prompt, just write it, or both. */ 716 - static void refreshMultiLine(struct linenoiseState *l, int flags) { 550 + * cursor position, and number of columns of the terminal. */ 551 + static void refreshMultiLine(struct linenoiseState *l, const char *prompt) { 717 552 char seq[64]; 718 - size_t pcollen = promptTextColumnLen(l->prompt,strlen(l->prompt)); 719 - int colpos = columnPosForMultiLine(l->buf, l->len, l->len, l->cols, pcollen); 720 - int colpos2; /* cursor column position. */ 721 - int rows = (pcollen+colpos+l->cols-1)/l->cols; /* rows used by current buf. */ 722 - int rpos = (pcollen+l->oldcolpos+l->cols)/l->cols; /* cursor relative row. */ 553 + int plen = strlen(prompt); 554 + int rows = (plen+l->len+l->cols-1)/l->cols; /* rows used by current buf. */ 555 + int rpos = (plen+l->oldpos+l->cols)/l->cols; /* cursor relative row. */ 723 556 int rpos2; /* rpos after refresh. */ 724 557 int col; /* colum position, zero-based. */ 725 - int old_rows = l->oldrows; 558 + int old_rows = l->maxrows; 559 + size_t pos = l->pos; 726 560 int fd = l->ofd, j; 727 561 struct abuf ab; 728 562 729 - l->oldrows = rows; 563 + /* Update maxrows if needed. */ 564 + if (rows > (int)l->maxrows) l->maxrows = rows; 730 565 731 566 /* First step: clear all the lines used before. To do so start by 732 567 * going to the last row. */ 733 568 abInit(&ab); 734 - 735 - if (flags & REFRESH_CLEAN) { 736 - if (old_rows-rpos > 0) { 737 - lndebug("go down %d", old_rows-rpos); 738 - snprintf(seq,64,"\x1b[%dB", old_rows-rpos); 739 - abAppend(&ab,seq,strlen(seq)); 740 - } 741 - 742 - /* Now for every row clear it, go up. */ 743 - for (j = 0; j < old_rows-1; j++) { 744 - lndebug("clear+up"); 745 - snprintf(seq,64,"\r\x1b[0K\x1b[1A"); 746 - abAppend(&ab,seq,strlen(seq)); 747 - } 569 + if (old_rows-rpos > 0) { 570 + lndebug("go down %d", old_rows-rpos); 571 + snprintf(seq,64,"\x1b[%dB", old_rows-rpos); 572 + abAppend(&ab,seq,strlen(seq)); 748 573 } 749 574 750 - if (flags & REFRESH_ALL) { 751 - /* Clean the top line. */ 752 - lndebug("clear"); 753 - snprintf(seq,64,"\r\x1b[0K"); 575 + /* Now for every row clear it, go up. */ 576 + for (j = 0; j < old_rows-1; j++) { 577 + lndebug("clear+up"); 578 + snprintf(seq,64,"\r\x1b[0K\x1b[1A"); 754 579 abAppend(&ab,seq,strlen(seq)); 755 580 } 756 581 757 - /* Get column length to cursor position */ 758 - colpos2 = columnPosForMultiLine(l->buf,l->len,l->pos,l->cols,pcollen); 582 + /* Clean the top line. */ 583 + lndebug("clear"); 584 + snprintf(seq,64,"\r\x1b[0K"); 585 + abAppend(&ab,seq,strlen(seq)); 759 586 760 - if (flags & REFRESH_WRITE) { 761 - /* Write the prompt and the current buffer content */ 762 - abAppend(&ab,l->prompt,strlen(l->prompt)); 763 - if (maskmode == 1) { 764 - unsigned int i; 765 - for (i = 0; i < l->len; i++) abAppend(&ab,"*",1); 766 - } else { 767 - abAppend(&ab,l->buf,l->len); 768 - } 587 + /* Write the prompt and the current buffer content */ 588 + abAppend(&ab,prompt,strlen(prompt)); 589 + abAppend(&ab,l->buf,l->len); 769 590 770 - /* Show hits if any. */ 771 - refreshShowHints(&ab,l,pcollen); 591 + /* Show hits if any. */ 592 + refreshShowHints(&ab,l,plen); 772 593 773 - /* If we are at the very end of the screen with our prompt, we need to 774 - * emit a newline and move the prompt to the first column. */ 775 - if (l->pos && 776 - l->pos == l->len && 777 - (colpos2+pcollen) % l->cols == 0) 778 - { 779 - lndebug("<newline>"); 780 - abAppend(&ab,"\n",1); 781 - snprintf(seq,64,"\r"); 782 - abAppend(&ab,seq,strlen(seq)); 783 - rows++; 784 - if (rows > (int)l->oldrows) l->oldrows = rows; 785 - } 594 + /* If we are at the very end of the screen with our prompt, we need to 595 + * emit a newline and move the prompt to the first column. */ 596 + if (pos && 597 + pos == l->len && 598 + (pos+plen) % l->cols == 0) 599 + { 600 + lndebug("<newline>"); 601 + abAppend(&ab,"\n",1); 602 + snprintf(seq,64,"\r"); 603 + abAppend(&ab,seq,strlen(seq)); 604 + rows++; 605 + if (rows > (int)l->maxrows) l->maxrows = rows; 606 + } 786 607 787 - /* Move cursor to right position. */ 788 - rpos2 = (pcollen+colpos2+l->cols)/l->cols; /* Current cursor relative row */ 789 - lndebug("rpos2 %d", rpos2); 790 - 791 - /* Go up till we reach the expected positon. */ 792 - if (rows-rpos2 > 0) { 793 - lndebug("go-up %d", rows-rpos2); 794 - snprintf(seq,64,"\x1b[%dA", rows-rpos2); 795 - abAppend(&ab,seq,strlen(seq)); 796 - } 608 + /* Move cursor to right position. */ 609 + rpos2 = (plen+pos+l->cols)/l->cols; /* current cursor relative row. */ 610 + lndebug("rpos2 %d", rpos2); 797 611 798 - /* Set column. */ 799 - col = (pcollen+colpos2) % l->cols; 800 - lndebug("set col %d", 1+col); 801 - if (col) 802 - snprintf(seq,64,"\r\x1b[%dC", col); 803 - else 804 - snprintf(seq,64,"\r"); 612 + /* Go up till we reach the expected positon. */ 613 + if (rows-rpos2 > 0) { 614 + lndebug("go-up %d", rows-rpos2); 615 + snprintf(seq,64,"\x1b[%dA", rows-rpos2); 805 616 abAppend(&ab,seq,strlen(seq)); 806 617 } 807 618 619 + /* Set column. */ 620 + col = (plen+(int)pos) % (int)l->cols; 621 + lndebug("set col %d", 1+col); 622 + if (col) 623 + snprintf(seq,64,"\r\x1b[%dC", col); 624 + else 625 + snprintf(seq,64,"\r"); 626 + abAppend(&ab,seq,strlen(seq)); 627 + 808 628 lndebug("\n"); 809 - l->oldcolpos = colpos2; 629 + l->oldpos = pos; 810 630 811 631 if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */ 812 632 abFree(&ab); ··· 814 634 815 635 /* Calls the two low level functions refreshSingleLine() or 816 636 * refreshMultiLine() according to the selected mode. */ 817 - static void refreshLineWithFlags(struct linenoiseState *l, int flags) { 637 + static void refreshLinePrompt(struct linenoiseState *l, const char *prompt) { 818 638 if (mlmode) 819 - refreshMultiLine(l,flags); 639 + refreshMultiLine(l, prompt); 820 640 else 821 - refreshSingleLine(l,flags); 641 + refreshSingleLine(l, prompt); 822 642 } 823 643 824 - /* Utility function to avoid specifying REFRESH_ALL all the times. */ 825 644 static void refreshLine(struct linenoiseState *l) { 826 - refreshLineWithFlags(l,REFRESH_ALL); 827 - } 828 - 829 - /* Hide the current line, when using the multiplexing API. */ 830 - void linenoiseHide(struct linenoiseState *l) { 831 - if (mlmode) 832 - refreshMultiLine(l,REFRESH_CLEAN); 833 - else 834 - refreshSingleLine(l,REFRESH_CLEAN); 835 - } 836 - 837 - /* Show the current line, when using the multiplexing API. */ 838 - void linenoiseShow(struct linenoiseState *l) { 839 - if (l->in_completion) { 840 - refreshLineWithCompletion(l,NULL,REFRESH_WRITE); 841 - } else { 842 - refreshLineWithFlags(l,REFRESH_WRITE); 843 - } 645 + refreshLinePrompt(l, l->prompt); 844 646 } 845 647 846 648 /* Insert the character 'c' at cursor current position. 847 649 * 848 650 * On error writing to the terminal -1 is returned, otherwise 0. */ 849 - int linenoiseEditInsert(struct linenoiseState *l, const char *cbuf, int clen) { 850 - if (l->len+clen <= l->buflen) { 651 + int linenoiseEditInsert(struct linenoiseState *l, char c) { 652 + if (l->len < l->buflen) { 851 653 if (l->len == l->pos) { 852 - memcpy(&l->buf[l->pos],cbuf,clen); 853 - l->pos+=clen; 854 - l->len+=clen;; 654 + l->buf[l->pos] = c; 655 + l->pos++; 656 + l->len++; 855 657 l->buf[l->len] = '\0'; 856 - if ((!mlmode && promptTextColumnLen(l->prompt,l->plen)+columnPos(l->buf,l->len,l->len) < l->cols && !hintsCallback)) { 658 + if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) { 857 659 /* Avoid a full update of the line in the 858 660 * trivial case. */ 859 - if (maskmode == 1) { 860 - static const char d = '*'; 861 - if (write(l->ofd,&d,1) == -1) return -1; 862 - } else { 863 - if (write(l->ofd,cbuf,clen) == -1) return -1; 864 - } 661 + if (write(l->ofd,&c,1) == -1) return -1; 865 662 } else { 866 663 refreshLine(l); 867 664 } 868 665 } else { 869 - memmove(l->buf+l->pos+clen,l->buf+l->pos,l->len-l->pos); 870 - memcpy(&l->buf[l->pos],cbuf,clen); 871 - l->pos+=clen; 872 - l->len+=clen; 666 + memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos); 667 + l->buf[l->pos] = c; 668 + l->len++; 669 + l->pos++; 873 670 l->buf[l->len] = '\0'; 874 671 refreshLine(l); 875 672 } ··· 880 677 /* Move cursor on the left. */ 881 678 void linenoiseEditMoveLeft(struct linenoiseState *l) { 882 679 if (l->pos > 0) { 883 - l->pos -= prevCharLen(l->buf,l->len,l->pos,NULL); 680 + l->pos--; 884 681 refreshLine(l); 885 682 } 886 683 } ··· 888 685 /* Move cursor on the right. */ 889 686 void linenoiseEditMoveRight(struct linenoiseState *l) { 890 687 if (l->pos != l->len) { 891 - l->pos += nextCharLen(l->buf,l->len,l->pos,NULL); 688 + l->pos++; 892 689 refreshLine(l); 893 690 } 894 691 } 895 692 693 + /* Move cursor on the left */ 694 + void linenoiseEditMovePrevWord(struct linenoiseState *l) { 695 + while (l->pos > 0 && l->buf[l->pos-1] == ' ') 696 + l->pos--; 697 + while (l->pos > 0 && l->buf[l->pos-1] != ' ') 698 + l->pos--; 699 + refreshLine(l); 700 + } 701 + 702 + /* Move cursor on the right. */ 703 + void linenoiseEditMoveNextWord(struct linenoiseState *l) { 704 + while (l->pos < l->len && l->buf[l->pos-1] == ' ') 705 + l->pos++; 706 + while (l->pos < l->len && l->buf[l->pos-1] != ' ') 707 + l->pos++; 708 + refreshLine(l); 709 + } 710 + 896 711 /* Move cursor to the start of the line. */ 897 712 void linenoiseEditMoveHome(struct linenoiseState *l) { 898 713 if (l->pos != 0) { ··· 939 754 * position. Basically this is what happens with the "Delete" keyboard key. */ 940 755 void linenoiseEditDelete(struct linenoiseState *l) { 941 756 if (l->len > 0 && l->pos < l->len) { 942 - int chlen = nextCharLen(l->buf,l->len,l->pos,NULL); 943 - memmove(l->buf+l->pos,l->buf+l->pos+chlen,l->len-l->pos-chlen); 944 - l->len-=chlen; 757 + memmove(l->buf+l->pos,l->buf+l->pos+1,l->len-l->pos-1); 758 + l->len--; 945 759 l->buf[l->len] = '\0'; 946 760 refreshLine(l); 947 761 } ··· 950 764 /* Backspace implementation. */ 951 765 void linenoiseEditBackspace(struct linenoiseState *l) { 952 766 if (l->pos > 0 && l->len > 0) { 953 - int chlen = prevCharLen(l->buf,l->len,l->pos,NULL); 954 - memmove(l->buf+l->pos-chlen,l->buf+l->pos,l->len-l->pos); 955 - l->pos-=chlen; 956 - l->len-=chlen; 767 + memmove(l->buf+l->pos-1,l->buf+l->pos,l->len-l->pos); 768 + l->pos--; 769 + l->len--; 957 770 l->buf[l->len] = '\0'; 958 771 refreshLine(l); 959 772 } 960 773 } 961 774 962 - /* Delete the previosu word, maintaining the cursor at the start of the 775 + /* Delete the previous word, maintaining the cursor at the start of the 963 776 * current word. */ 964 777 void linenoiseEditDeletePrevWord(struct linenoiseState *l) { 965 778 size_t old_pos = l->pos; ··· 975 788 refreshLine(l); 976 789 } 977 790 978 - /* This function is part of the multiplexed API of Linenoise, that is used 979 - * in order to implement the blocking variant of the API but can also be 980 - * called by the user directly in an event driven program. It will: 981 - * 982 - * 1. Initialize the linenoise state passed by the user. 983 - * 2. Put the terminal in RAW mode. 984 - * 3. Show the prompt. 985 - * 4. Return control to the user, that will have to call linenoiseEditFeed() 986 - * each time there is some data arriving in the standard input. 987 - * 988 - * The user can also call linenoiseEditHide() and linenoiseEditShow() if it 989 - * is required to show some input arriving asyncronously, without mixing 990 - * it with the currently edited line. 991 - * 992 - * When linenoiseEditFeed() returns non-NULL, the user finished with the 993 - * line editing session (pressed enter CTRL-D/C): in this case the caller 994 - * needs to call linenoiseEditStop() to put back the terminal in normal 995 - * mode. This will not destroy the buffer, as long as the linenoiseState 996 - * is still valid in the context of the caller. 997 - * 998 - * The function returns 0 on success, or -1 if writing to standard output 999 - * fails. If stdin_fd or stdout_fd are set to -1, the default is to use 1000 - * STDIN_FILENO and STDOUT_FILENO. 1001 - */ 1002 - int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) { 1003 - /* Populate the linenoise state that we pass to functions implementing 1004 - * specific editing functionalities. */ 1005 - l->in_completion = 0; 1006 - l->ifd = stdin_fd != -1 ? stdin_fd : STDIN_FILENO; 1007 - l->ofd = stdout_fd != -1 ? stdout_fd : STDOUT_FILENO; 1008 - l->buf = buf; 1009 - l->buflen = buflen; 1010 - l->prompt = prompt; 1011 - l->plen = strlen(prompt); 1012 - l->oldcolpos = l->pos = 0; 1013 - l->len = 0; 791 + /* Delete the next word, maintaining the cursor at the same position */ 792 + void linenoiseEditDeleteNextWord(struct linenoiseState *l) { 793 + size_t next_pos = l->pos; 794 + size_t diff; 1014 795 1015 - /* Enter raw mode. */ 1016 - if (enableRawMode(l->ifd) == -1) return -1; 796 + while (next_pos < l->len && l->buf[next_pos] == ' ') 797 + next_pos++; 798 + while (next_pos < l->len && l->buf[next_pos] != ' ') 799 + next_pos++; 800 + diff = next_pos - l->pos; 801 + memmove(l->buf+l->pos,l->buf+next_pos,l->len-next_pos+1); 802 + l->len -= diff; 803 + refreshLine(l); 804 + } 1017 805 1018 - l->cols = getColumns(stdin_fd, stdout_fd); 1019 - l->oldrows = 0; 1020 - l->history_index = 0; 806 + void linenoiseReverseIncrementalSearch(struct linenoiseState *l) { 1021 807 1022 - /* Buffer starts empty. */ 1023 - l->buf[0] = '\0'; 1024 - l->buflen--; /* Make sure there is always space for the nulterm */ 808 + char search_buf[LINENOISE_MAX_LINE]; 809 + char search_prompt[LINENOISE_MAX_LINE]; 810 + int search_len = 0; 811 + int search_pos = history_len - 1; 812 + int search_dir = -1; 813 + char* prompt; 1025 814 1026 - /* If stdin is not a tty, stop here with the initialization. We 1027 - * will actually just read a line from standard input in blocking 1028 - * mode later, in linenoiseEditFeed(). */ 1029 - if (!isatty(l->ifd)) return 0; 815 + int has_match = 1; 1030 816 1031 - /* The latest history entry is always our current buffer, that 1032 - * initially is just an empty string. */ 1033 - linenoiseHistoryAdd(""); 817 + // backup of current input 818 + char *buf; 819 + { 820 + size_t len = 1+ strlen(l->buf); 821 + buf = malloc(len); 822 + if (buf == NULL) return; 823 + memcpy(buf, l->buf, len); 824 + } 1034 825 1035 - if (write(l->ofd,prompt,l->plen) == -1) return -1; 1036 - return 0; 1037 - } 826 + search_buf[0] = 0; 827 + 828 + while (1) { 1038 829 1039 - char *linenoiseEditMore = "If you see this, you are misusing the API: when linenoiseEditFeed() is called, if it returns linenoiseEditMore the user is yet editing the line. See the README file for more information."; 830 + if (!has_match) 831 + prompt = "(failed-reverse-i-search)`%s': "; 832 + else 833 + prompt = "(reverse-i-search)`%s': "; 1040 834 1041 - /* This function is part of the multiplexed API of linenoise, see the top 1042 - * comment on linenoiseEditStart() for more information. Call this function 1043 - * each time there is some data to read from the standard input file 1044 - * descriptor. In the case of blocking operations, this function can just be 1045 - * called in a loop, and block. 1046 - * 1047 - * The function returns linenoiseEditMore to signal that line editing is still 1048 - * in progress, that is, the user didn't yet pressed enter / CTRL-D. Otherwise 1049 - * the function returns the pointer to the heap-allocated buffer with the 1050 - * edited line, that the user should free with linenoiseFree(). 1051 - * 1052 - * On special conditions, NULL is returned and errno is populated: 1053 - * 1054 - * EAGAIN if the user pressed Ctrl-C 1055 - * ENOENT if the user pressed Ctrl-D 1056 - * 1057 - * Some other errno: I/O error. 1058 - */ 1059 - char *linenoiseEditFeed(struct linenoiseState *l) { 1060 - /* Not a TTY, pass control to line reading without character 1061 - * count limits. */ 1062 - if (!isatty(l->ifd)) return linenoiseNoTTY(); 835 + if (!snprintf(search_prompt, sizeof(search_prompt), prompt, search_buf)) { 836 + linenoiseBeep(); 837 + break; 838 + } else { 839 + search_prompt[sizeof(search_prompt)-1] = 0; // crop 840 + } 1063 841 1064 - int c; 1065 - int nread; 1066 - char cbuf[32]; // large enough for any encoding? 1067 - char seq[3]; 842 + l->pos = 0; 843 + refreshLinePrompt(l, search_prompt); 1068 844 1069 - nread = readCode(l->ifd,cbuf,sizeof(cbuf),&c); 1070 - if (nread <= 0) return NULL; 845 + char c; 846 + int new_char = 0; 1071 847 1072 - /* Only autocomplete when the callback is set. It returns < 0 when 1073 - * there was an error reading from fd. Otherwise it will return the 1074 - * character that should be handled next. */ 1075 - if ((l->in_completion || c == 9) && completionCallback != NULL) { 1076 - c = completeLine(l,c); 1077 - /* Return on errors */ 1078 - if (c < 0) return NULL; 1079 - /* Read next character when 0 */ 1080 - if (c == 0) return linenoiseEditMore; 848 + if (read(l->ifd, &c, 1) <= 0) { 849 + l->pos = l->len = snprintf(l->buf, l->buflen, "%s", buf); 850 + l->buf[l->buflen-1] = 0; 851 + refreshLine(l); 852 + free(buf); 853 + return; 1081 854 } 1082 855 1083 856 switch(c) { 1084 - case ENTER: /* enter */ 1085 - history_len--; 1086 - free(history[history_len]); 1087 - if (mlmode) linenoiseEditMoveEnd(l); 1088 - if (hintsCallback) { 1089 - /* Force a refresh without hints to leave the previous 1090 - * line as the user typed it after a newline. */ 1091 - linenoiseHintsCallback *hc = hintsCallback; 1092 - hintsCallback = NULL; 1093 - refreshLine(l); 1094 - hintsCallback = hc; 857 + case BACKSPACE: 858 + case CTRL_H: 859 + if (search_len > 0) { 860 + search_buf[--search_len] = 0; 861 + search_pos = history_len - 1; 862 + } else 863 + linenoiseBeep(); 864 + break; 865 + case CTRL_N: 866 + case CTRL_R: 867 + search_dir = -1; 868 + if (search_pos >= history_len) 869 + search_pos = history_len - 1; 870 + break; 871 + case CTRL_P: 872 + search_dir = 1; 873 + if (search_pos < 0) 874 + search_pos = 0; 875 + break; 876 + case ESC: 877 + case CTRL_G: 878 + l->pos = l->len = snprintf(l->buf, l->buflen, "%s", buf); 879 + l->buf[l->buflen-1] = 0; 880 + free(buf); 881 + refreshLine(l); 882 + return; 883 + case ENTER: 884 + free(buf); 885 + l->pos = l->len; 886 + refreshLine(l); 887 + return; 888 + default: 889 + new_char = 1; 890 + search_buf[search_len] = c; 891 + search_buf[++search_len] = 0; 892 + search_pos = history_len - 1; 893 + break; 894 + } 895 + 896 + has_match = 0; 897 + 898 + if (strlen(search_buf) > 0) { 899 + for (; search_pos >= 0 && search_pos < history_len; search_pos += search_dir) { 900 + if (strstr(history[search_pos], search_buf) && (new_char || strcmp(history[search_pos], l->buf))) { 901 + has_match = 1; 902 + l->len = snprintf(l->buf, l->buflen, "%s", history[search_pos]); 903 + break; 1095 904 } 1096 - return strdup(l->buf); 1097 - case CTRL_C: /* ctrl-c */ 1098 - errno = EAGAIN; 1099 - linenoiseWasInterrupted = 1; 1100 - return NULL; 1101 - case BACKSPACE: /* backspace */ 1102 - case 8: /* ctrl-h */ 1103 - linenoiseEditBackspace(l); 1104 - break; 1105 - case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the 1106 - line is empty, act as end-of-file. */ 1107 - if (l->len > 0) { 1108 - linenoiseEditDelete(l); 1109 - } else { 905 + } 906 + if (!has_match) { 907 + linenoiseBeep(); 908 + // forbid writes if the line is too long 909 + if (search_len > 0 && new_char && search_len+1 >= sizeof(search_buf)) 910 + search_buf[--search_len] = 0; 911 + } 912 + } 913 + } 914 + } 915 + 916 + /* This function is the core of the line editing capability of linenoise. 917 + * It expects 'fd' to be already in "raw mode" so that every key pressed 918 + * will be returned ASAP to read(). 919 + * 920 + * The resulting string is put into 'buf' when the user type enter, or 921 + * when ctrl+d is typed. 922 + * 923 + * The function returns the length of the current buffer. */ 924 + static int linenoiseEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) 925 + { 926 + struct linenoiseState l; 927 + 928 + /* Populate the linenoise state that we pass to functions implementing 929 + * specific editing functionalities. */ 930 + l.ifd = stdin_fd; 931 + l.ofd = stdout_fd; 932 + l.buf = buf; 933 + l.buflen = buflen; 934 + l.prompt = prompt; 935 + l.plen = strlen(prompt); 936 + l.oldpos = l.pos = 0; 937 + l.len = 0; 938 + l.cols = getColumns(stdin_fd, stdout_fd); 939 + l.maxrows = 0; 940 + l.history_index = 0; 941 + 942 + /* Buffer starts empty. */ 943 + l.buf[0] = '\0'; 944 + l.buflen--; /* Make sure there is always space for the nulterm */ 945 + 946 + /* The latest history entry is always our current buffer, that 947 + * initially is just an empty string. */ 948 + linenoiseHistoryAdd(""); 949 + 950 + if (write(l.ofd,prompt,l.plen) == -1) return -1; 951 + while(1) { 952 + char c; 953 + int nread; 954 + char seq[5]; 955 + 956 + nread = read(l.ifd,&c,1); 957 + if (nread <= 0) return l.len; 958 + 959 + /* Only autocomplete when the callback is set. It returns < 0 when 960 + * there was an error reading from fd. Otherwise it will return the 961 + * character that should be handled next. */ 962 + if (c == 9 && completionCallback != NULL) { 963 + c = completeLine(&l); 964 + /* Return on errors */ 965 + if (c < 0) return l.len; 966 + /* Read next character when 0 */ 967 + if (c == 0) continue; 968 + } 969 + 970 + switch(c) { 971 + case ENTER: /* enter */ 1110 972 history_len--; 1111 973 free(history[history_len]); 1112 - errno = ENOENT; 1113 - return NULL; 1114 - } 1115 - break; 1116 - case CTRL_T: /* ctrl-t, swaps current character with previous. */ 1117 - if (l->pos > 0 && l->pos < l->len) { 1118 - int aux = l->buf[l->pos-1]; 1119 - l->buf[l->pos-1] = l->buf[l->pos]; 1120 - l->buf[l->pos] = aux; 1121 - if (l->pos != l->len-1) l->pos++; 1122 - refreshLine(l); 1123 - } 1124 - break; 1125 - case CTRL_B: /* ctrl-b */ 1126 - linenoiseEditMoveLeft(l); 1127 - break; 1128 - case CTRL_F: /* ctrl-f */ 1129 - linenoiseEditMoveRight(l); 1130 - break; 1131 - case CTRL_P: /* ctrl-p */ 1132 - linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV); 1133 - break; 1134 - case CTRL_N: /* ctrl-n */ 1135 - linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT); 1136 - break; 1137 - case ESC: /* escape sequence */ 1138 - /* Read the next two bytes representing the escape sequence. 1139 - * Use two calls to handle slow terminals returning the two 1140 - * chars at different times. */ 1141 - if (read(l->ifd,seq,1) == -1) break; 1142 - if (read(l->ifd,seq+1,1) == -1) break; 974 + if (mlmode) linenoiseEditMoveEnd(&l); 975 + if (hintsCallback) { 976 + /* Force a refresh without hints to leave the previous 977 + * line as the user typed it after a newline. */ 978 + linenoiseHintsCallback *hc = hintsCallback; 979 + hintsCallback = NULL; 980 + refreshLine(&l); 981 + hintsCallback = hc; 982 + } 983 + return (int)l.len; 984 + case CTRL_C: /* ctrl-c */ 985 + errno = EAGAIN; 986 + linenoiseWasInterrupted = 1; 987 + return -1; 988 + case BACKSPACE: /* backspace */ 989 + case 8: /* ctrl-h */ 990 + linenoiseEditBackspace(&l); 991 + break; 992 + case CTRL_D: /* ctrl-d, remove char at right of cursor, or if the 993 + line is empty, act as end-of-file. */ 994 + if (l.len > 0) { 995 + linenoiseEditDelete(&l); 996 + } else { 997 + history_len--; 998 + free(history[history_len]); 999 + return -1; 1000 + } 1001 + break; 1002 + case CTRL_T: /* ctrl-t, swaps current character with previous. */ 1003 + if (l.pos > 0 && l.pos < l.len) { 1004 + int aux = buf[l.pos-1]; 1005 + buf[l.pos-1] = buf[l.pos]; 1006 + buf[l.pos] = aux; 1007 + if (l.pos != l.len-1) l.pos++; 1008 + refreshLine(&l); 1009 + } 1010 + break; 1011 + case CTRL_B: /* ctrl-b */ 1012 + linenoiseEditMoveLeft(&l); 1013 + break; 1014 + case CTRL_F: /* ctrl-f */ 1015 + linenoiseEditMoveRight(&l); 1016 + break; 1017 + case CTRL_P: /* ctrl-p */ 1018 + linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV); 1019 + break; 1020 + case CTRL_R: /* ctrl-r */ 1021 + linenoiseReverseIncrementalSearch(&l); 1022 + break; 1023 + case CTRL_N: /* ctrl-n */ 1024 + linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT); 1025 + break; 1026 + case ESC: /* escape sequence */ 1027 + /* Read the next byte representing the escape sequence */ 1028 + if (read(l.ifd,seq,1) == -1) break; 1143 1029 1144 - /* ESC [ sequences. */ 1145 - if (seq[0] == '[') { 1146 - if (seq[1] >= '0' && seq[1] <= '9') { 1147 - /* Extended escape, read additional byte. */ 1148 - if (read(l->ifd,seq+2,1) == -1) break; 1149 - if (seq[2] == '~') { 1030 + /* alt-b, alt-f, alt-d, alt-backspace */ 1031 + if (seq[0] == 'b') { 1032 + linenoiseEditMovePrevWord(&l); 1033 + break; 1034 + } else if (seq[0] == 'f') { 1035 + linenoiseEditMoveNextWord(&l); 1036 + break; 1037 + } else if (seq[0] == 'd') { 1038 + linenoiseEditDeleteNextWord(&l); 1039 + break; 1040 + } else if (seq[0] == 127) { /* backspace */ 1041 + linenoiseEditDeletePrevWord(&l); 1042 + break; 1043 + } 1044 + 1045 + /* Read a second byte */ 1046 + if (read(l.ifd,seq+1,1) == -1) break; 1047 + 1048 + /* ESC [ sequences. */ 1049 + if (seq[0] == '[') { 1050 + if (seq[1] >= '0' && seq[1] <= '9') { 1051 + /* Extended escape, read additional byte. */ 1052 + if (read(l.ifd,seq+2,1) == -1) break; 1053 + if (seq[2] == '~') { 1054 + switch(seq[1]) { 1055 + case '3': /* Delete key. */ 1056 + linenoiseEditDelete(&l); 1057 + break; 1058 + } 1059 + } else if (seq[2] == ';') { 1060 + /* read additional 2 bytes */ 1061 + if (read(l.ifd,seq+3,1) == -1) break; 1062 + if (read(l.ifd,seq+4,1) == -1) break; 1063 + if (seq[3] == '5') { 1064 + switch (seq[4]) { 1065 + case 'D': /* ctrl-left */ 1066 + linenoiseEditMovePrevWord(&l); 1067 + break; 1068 + case 'C': /* ctrl-right */ 1069 + linenoiseEditMoveNextWord(&l); 1070 + break; 1071 + } 1072 + } 1073 + } 1074 + } else { 1150 1075 switch(seq[1]) { 1151 - case '3': /* Delete key. */ 1152 - linenoiseEditDelete(l); 1076 + case 'A': /* Up */ 1077 + linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_PREV); 1078 + break; 1079 + case 'B': /* Down */ 1080 + linenoiseEditHistoryNext(&l, LINENOISE_HISTORY_NEXT); 1081 + break; 1082 + case 'C': /* Right */ 1083 + linenoiseEditMoveRight(&l); 1084 + break; 1085 + case 'D': /* Left */ 1086 + linenoiseEditMoveLeft(&l); 1087 + break; 1088 + case 'H': /* Home */ 1089 + linenoiseEditMoveHome(&l); 1090 + break; 1091 + case 'F': /* End*/ 1092 + linenoiseEditMoveEnd(&l); 1153 1093 break; 1154 1094 } 1155 1095 } 1156 - } else { 1096 + } 1097 + 1098 + /* ESC O sequences. */ 1099 + else if (seq[0] == 'O') { 1157 1100 switch(seq[1]) { 1158 - case 'A': /* Up */ 1159 - linenoiseEditHistoryNext(l, LINENOISE_HISTORY_PREV); 1160 - break; 1161 - case 'B': /* Down */ 1162 - linenoiseEditHistoryNext(l, LINENOISE_HISTORY_NEXT); 1163 - break; 1164 - case 'C': /* Right */ 1165 - linenoiseEditMoveRight(l); 1166 - break; 1167 - case 'D': /* Left */ 1168 - linenoiseEditMoveLeft(l); 1169 - break; 1170 1101 case 'H': /* Home */ 1171 - linenoiseEditMoveHome(l); 1102 + linenoiseEditMoveHome(&l); 1172 1103 break; 1173 1104 case 'F': /* End*/ 1174 - linenoiseEditMoveEnd(l); 1105 + linenoiseEditMoveEnd(&l); 1175 1106 break; 1176 1107 } 1177 1108 } 1178 - } 1179 - 1180 - /* ESC O sequences. */ 1181 - else if (seq[0] == 'O') { 1182 - switch(seq[1]) { 1183 - case 'H': /* Home */ 1184 - linenoiseEditMoveHome(l); 1185 - break; 1186 - case 'F': /* End*/ 1187 - linenoiseEditMoveEnd(l); 1188 - break; 1189 - } 1109 + break; 1110 + default: 1111 + if (linenoiseEditInsert(&l,c)) return -1; 1112 + break; 1113 + case CTRL_U: /* Ctrl+u, delete the whole line. */ 1114 + buf[0] = '\0'; 1115 + l.pos = l.len = 0; 1116 + refreshLine(&l); 1117 + break; 1118 + case CTRL_K: /* Ctrl+k, delete from current to end of line. */ 1119 + buf[l.pos] = '\0'; 1120 + l.len = l.pos; 1121 + refreshLine(&l); 1122 + break; 1123 + case CTRL_A: /* Ctrl+a, go to the start of the line */ 1124 + linenoiseEditMoveHome(&l); 1125 + break; 1126 + case CTRL_E: /* ctrl+e, go to the end of the line */ 1127 + linenoiseEditMoveEnd(&l); 1128 + break; 1129 + case CTRL_L: /* ctrl+l, clear screen */ 1130 + linenoiseClearScreen(); 1131 + refreshLine(&l); 1132 + break; 1133 + case CTRL_W: /* ctrl+w, delete previous word */ 1134 + linenoiseEditDeletePrevWord(&l); 1135 + break; 1190 1136 } 1191 - break; 1192 - default: 1193 - if (linenoiseEditInsert(l,cbuf,nread)) return NULL; 1194 - break; 1195 - case CTRL_U: /* Ctrl+u, delete the whole line. */ 1196 - l->buf[0] = '\0'; 1197 - l->pos = l->len = 0; 1198 - refreshLine(l); 1199 - break; 1200 - case CTRL_K: /* Ctrl+k, delete from current to end of line. */ 1201 - l->buf[l->pos] = '\0'; 1202 - l->len = l->pos; 1203 - refreshLine(l); 1204 - break; 1205 - case CTRL_A: /* Ctrl+a, go to the start of the line */ 1206 - linenoiseEditMoveHome(l); 1207 - break; 1208 - case CTRL_E: /* ctrl+e, go to the end of the line */ 1209 - linenoiseEditMoveEnd(l); 1210 - break; 1211 - case CTRL_L: /* ctrl+l, clear screen */ 1212 - linenoiseClearScreen(); 1213 - refreshLine(l); 1214 - break; 1215 - case CTRL_W: /* ctrl+w, delete previous word */ 1216 - linenoiseEditDeletePrevWord(l); 1217 - break; 1218 1137 } 1219 - return linenoiseEditMore; 1220 - } 1221 - 1222 - /* This is part of the multiplexed linenoise API. See linenoiseEditStart() 1223 - * for more information. This function is called when linenoiseEditFeed() 1224 - * returns something different than NULL. At this point the user input 1225 - * is in the buffer, and we can restore the terminal in normal mode. */ 1226 - void linenoiseEditStop(struct linenoiseState *l) { 1227 - if (!isatty(l->ifd)) return; 1228 - disableRawMode(l->ifd); 1229 - printf("\n"); 1230 - } 1231 - 1232 - /* This just implements a blocking loop for the multiplexed API. 1233 - * In many applications that are not event-drivern, we can just call 1234 - * the blocking linenoise API, wait for the user to complete the editing 1235 - * and return the buffer. */ 1236 - static char *linenoiseBlockingEdit(int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt) 1237 - { 1238 - struct linenoiseState l; 1239 - 1240 - /* Editing without a buffer is invalid. */ 1241 - if (buflen == 0) { 1242 - errno = EINVAL; 1243 - return NULL; 1244 - } 1245 - 1246 - linenoiseEditStart(&l,stdin_fd,stdout_fd,buf,buflen,prompt); 1247 - char *res; 1248 - while((res = linenoiseEditFeed(&l)) == linenoiseEditMore); 1249 - linenoiseEditStop(&l); 1250 - return res; 1138 + return l.len; 1251 1139 } 1252 1140 1253 1141 /* This special mode is used by linenoise in order to print scan codes ··· 1271 1159 if (memcmp(quit,"quit",sizeof(quit)) == 0) break; 1272 1160 1273 1161 printf("'%c' %02x (%d) (type quit to exit)\n", 1274 - isprint((int)c) ? c : '?', (int)c, (int)c); 1162 + isprint(c) ? c : '?', (int)c, (int)c); 1275 1163 printf("\r"); /* Go left edge manually, we are in raw mode. */ 1276 1164 fflush(stdout); 1277 1165 } 1278 1166 disableRawMode(STDIN_FILENO); 1279 1167 } 1280 1168 1169 + /* This function calls the line editing function linenoiseEdit() using 1170 + * the STDIN file descriptor set in raw mode. */ 1171 + static int linenoiseRaw(char *buf, size_t buflen, const char *prompt) { 1172 + int count; 1173 + 1174 + if (buflen == 0) { 1175 + errno = EINVAL; 1176 + return -1; 1177 + } 1178 + 1179 + if (enableRawMode(STDIN_FILENO) == -1) return -1; 1180 + count = linenoiseEdit(STDIN_FILENO, STDOUT_FILENO, buf, buflen, prompt); 1181 + disableRawMode(STDIN_FILENO); 1182 + printf("\n"); 1183 + return count; 1184 + } 1185 + 1281 1186 /* This function is called when linenoise() is called with the standard 1282 1187 * input file descriptor not attached to a TTY. So for example when the 1283 1188 * program using linenoise is called in pipe or with a file redirected ··· 1321 1226 * something even in the most desperate of the conditions. */ 1322 1227 char *linenoise(const char *prompt) { 1323 1228 char buf[LINENOISE_MAX_LINE]; 1229 + int count; 1324 1230 1325 1231 if (!isatty(STDIN_FILENO)) { 1326 1232 /* Not a tty: read from file / pipe. In this mode we don't want any ··· 1339 1245 } 1340 1246 return strdup(buf); 1341 1247 } else { 1342 - char *retval = linenoiseBlockingEdit(STDIN_FILENO,STDOUT_FILENO,buf,LINENOISE_MAX_LINE,prompt); 1343 - return retval; 1248 + count = linenoiseRaw(buf,LINENOISE_MAX_LINE,prompt); 1249 + if (count == -1) return NULL; 1250 + return strdup(buf); 1344 1251 } 1345 1252 } 1346 1253 ··· 1349 1256 * created with. Useful when the main program is using an alternative 1350 1257 * allocator. */ 1351 1258 void linenoiseFree(void *ptr) { 1352 - if (ptr == linenoiseEditMore) return; // Protect from API misuse. 1353 1259 free(ptr); 1354 1260 } 1355 1261
+3 -51
vendor/ocaml-linenoise/src/linenoise_src.h
··· 7 7 * 8 8 * ------------------------------------------------------------------------ 9 9 * 10 - * Copyright (c) 2010-2023, Salvatore Sanfilippo <antirez at gmail dot com> 10 + * Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com> 11 11 * Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com> 12 12 * 13 13 * All rights reserved. ··· 43 43 extern "C" { 44 44 #endif 45 45 46 - #include <stddef.h> /* For size_t. */ 47 - 48 46 extern int linenoiseWasInterrupted; /* boolean signalling if last call was ctrl-c */ 49 - extern char *linenoiseEditMore; 50 - 51 - /* The linenoiseState structure represents the state during line editing. 52 - * We pass this state to functions implementing specific editing 53 - * functionalities. */ 54 - struct linenoiseState { 55 - int in_completion; /* The user pressed TAB and we are now in completion 56 - * mode, so input is handled by completeLine(). */ 57 - size_t completion_idx; /* Index of next completion to propose. */ 58 - int ifd; /* Terminal stdin file descriptor. */ 59 - int ofd; /* Terminal stdout file descriptor. */ 60 - char *buf; /* Edited line buffer. */ 61 - size_t buflen; /* Edited line buffer size. */ 62 - const char *prompt; /* Prompt to display. */ 63 - size_t plen; /* Prompt length. */ 64 - size_t pos; /* Current cursor position. */ 65 - size_t oldcolpos; /* Previous refresh cursor column position. */ 66 - size_t len; /* Current edited line length. */ 67 - size_t cols; /* Number of columns in terminal. */ 68 - size_t oldrows; /* Rows used by last refrehsed line (multiline mode) */ 69 - int history_index; /* The history index we are currently editing. */ 70 - }; 71 47 72 48 typedef struct linenoiseCompletions { 73 49 size_t len; 74 50 char **cvec; 75 51 } linenoiseCompletions; 76 52 77 - /* Non blocking API. */ 78 - int linenoiseEditStart(struct linenoiseState *l, int stdin_fd, int stdout_fd, char *buf, size_t buflen, const char *prompt); 79 - char *linenoiseEditFeed(struct linenoiseState *l); 80 - void linenoiseEditStop(struct linenoiseState *l); 81 - void linenoiseHide(struct linenoiseState *l); 82 - void linenoiseShow(struct linenoiseState *l); 83 - 84 - /* Blocking API. */ 85 - char *linenoise(const char *prompt); 86 - void linenoiseFree(void *ptr); 87 - 88 - /* Completion API. */ 89 53 typedef void(linenoiseCompletionCallback)(const char *, linenoiseCompletions *); 90 54 typedef char*(linenoiseHintsCallback)(const char *, int *color, int *bold); 91 55 typedef void(linenoiseFreeHintsCallback)(void *); ··· 94 58 void linenoiseSetFreeHintsCallback(linenoiseFreeHintsCallback *); 95 59 void linenoiseAddCompletion(linenoiseCompletions *, const char *); 96 60 97 - /* History API. */ 61 + char *linenoise(const char *prompt); 62 + void linenoiseFree(void *ptr); 98 63 int linenoiseHistoryAdd(const char *line); 99 64 int linenoiseHistorySetMaxLen(int len); 100 65 int linenoiseHistorySave(const char *filename); 101 66 int linenoiseHistoryLoad(const char *filename); 102 - 103 - /* Other utilities. */ 104 67 void linenoiseClearScreen(void); 105 68 void linenoiseSetMultiLine(int ml); 106 69 void linenoisePrintKeyCodes(void); 107 - void linenoiseMaskModeEnable(void); 108 - void linenoiseMaskModeDisable(void); 109 - 110 - typedef size_t (linenoisePrevCharLen)(const char *buf, size_t buf_len, size_t pos, size_t *col_len); 111 - typedef size_t (linenoiseNextCharLen)(const char *buf, size_t buf_len, size_t pos, size_t *col_len); 112 - typedef size_t (linenoiseReadCode)(int fd, char *buf, size_t buf_len, int* c); 113 - 114 - void linenoiseSetEncodingFunctions( 115 - linenoisePrevCharLen *prevCharLenFunc, 116 - linenoiseNextCharLen *nextCharLenFunc, 117 - linenoiseReadCode *readCodeFunc); 118 70 119 71 #ifdef __cplusplus 120 72 }