Fossil SCM

Update the built-in linenoise code to the latest from the canonical website: [https://github.com/antirez/linenoise].

drh 2021-01-15 12:59 trunk
Commit a424972e96d229a0cfd2d693fe1479f8412de69ccb725f083c19f293312afa6c
2 files changed +37 -11 +2
+37 -11
--- src/linenoise.c
+++ src/linenoise.c
@@ -123,10 +123,11 @@
123123
static linenoiseCompletionCallback *completionCallback = NULL;
124124
static linenoiseHintsCallback *hintsCallback = NULL;
125125
static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
126126
127127
static struct termios orig_termios; /* In order to restore at exit.*/
128
+static int maskmode = 0; /* Show "***" instead of input. For passwords. */
128129
static int rawmode = 0; /* For atexit() function to check if restore is needed*/
129130
static int mlmode = 0; /* Multi line mode. Default is single line. */
130131
static int atexit_registered = 0; /* Register atexit just 1 time. */
131132
static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
132133
static int history_len = 0;
@@ -194,10 +195,23 @@
194195
#else
195196
#define lndebug(fmt, ...)
196197
#endif
197198
198199
/* ======================= Low level terminal handling ====================== */
200
+
201
+/* Enable "mask mode". When it is enabled, instead of the input that
202
+ * the user is typing, the terminal will just display a corresponding
203
+ * number of asterisks, like "****". This is useful for passwords and other
204
+ * secrets that should not be displayed. */
205
+void linenoiseMaskModeEnable(void) {
206
+ maskmode = 1;
207
+}
208
+
209
+/* Disable mask mode. */
210
+void linenoiseMaskModeDisable(void) {
211
+ maskmode = 0;
212
+}
199213
200214
/* Set if to use or not the multi line mode. */
201215
void linenoiseSetMultiLine(int ml) {
202216
mlmode = ml;
203217
}
@@ -483,10 +497,12 @@
483497
int hintmaxlen = l->cols-(plen+l->len);
484498
if (hintlen > hintmaxlen) hintlen = hintmaxlen;
485499
if (bold == 1 && color == -1) color = 37;
486500
if (color != -1 || bold != 0)
487501
snprintf(seq,64,"\033[%d;%d;49m",bold,color);
502
+ else
503
+ seq[0] = '\0';
488504
abAppend(ab,seq,strlen(seq));
489505
abAppend(ab,hint,hintlen);
490506
if (color != -1 || bold != 0)
491507
abAppend(ab,"\033[0m",4);
492508
/* Call the function to free the hint returned. */
@@ -521,11 +537,15 @@
521537
/* Cursor to left edge */
522538
snprintf(seq,64,"\r");
523539
abAppend(&ab,seq,strlen(seq));
524540
/* Write the prompt and the current buffer content */
525541
abAppend(&ab,l->prompt,strlen(l->prompt));
526
- abAppend(&ab,buf,len);
542
+ if (maskmode == 1) {
543
+ while (len--) abAppend(&ab,"*",1);
544
+ } else {
545
+ abAppend(&ab,buf,len);
546
+ }
527547
/* Show hits if any. */
528548
refreshShowHints(&ab,l,plen);
529549
/* Erase to right */
530550
snprintf(seq,64,"\x1b[0K");
531551
abAppend(&ab,seq,strlen(seq));
@@ -556,30 +576,35 @@
556576
557577
/* First step: clear all the lines used before. To do so start by
558578
* going to the last row. */
559579
abInit(&ab);
560580
if (old_rows-rpos > 0) {
561
- /* lndebug("go down %d", old_rows-rpos); */
581
+ lndebug("go down %d", old_rows-rpos);
562582
snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
563583
abAppend(&ab,seq,strlen(seq));
564584
}
565585
566586
/* Now for every row clear it, go up. */
567587
for (j = 0; j < old_rows-1; j++) {
568
- /* lndebug("clear+up"); */
588
+ lndebug("clear+up");
569589
snprintf(seq,64,"\r\x1b[0K\x1b[1A");
570590
abAppend(&ab,seq,strlen(seq));
571591
}
572592
573593
/* Clean the top line. */
574
- /* lndebug("clear"); */
594
+ lndebug("clear");
575595
snprintf(seq,64,"\r\x1b[0K");
576596
abAppend(&ab,seq,strlen(seq));
577597
578598
/* Write the prompt and the current buffer content */
579599
abAppend(&ab,l->prompt,strlen(l->prompt));
580
- abAppend(&ab,l->buf,l->len);
600
+ if (maskmode == 1) {
601
+ unsigned int i;
602
+ for (i = 0; i < l->len; i++) abAppend(&ab,"*",1);
603
+ } else {
604
+ abAppend(&ab,l->buf,l->len);
605
+ }
581606
582607
/* Show hits if any. */
583608
refreshShowHints(&ab,l,plen);
584609
585610
/* If we are at the very end of the screen with our prompt, we need to
@@ -586,39 +611,39 @@
586611
* emit a newline and move the prompt to the first column. */
587612
if (l->pos &&
588613
l->pos == l->len &&
589614
(l->pos+plen) % l->cols == 0)
590615
{
591
- /* lndebug("<newline>"); */
616
+ lndebug("<newline>");
592617
abAppend(&ab,"\n",1);
593618
snprintf(seq,64,"\r");
594619
abAppend(&ab,seq,strlen(seq));
595620
rows++;
596621
if (rows > (int)l->maxrows) l->maxrows = rows;
597622
}
598623
599624
/* Move cursor to right position. */
600625
rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
601
- /* lndebug("rpos2 %d", rpos2); */
626
+ lndebug("rpos2 %d", rpos2);
602627
603628
/* Go up till we reach the expected positon. */
604629
if (rows-rpos2 > 0) {
605
- /* lndebug("go-up %d", rows-rpos2); */
630
+ lndebug("go-up %d", rows-rpos2);
606631
snprintf(seq,64,"\x1b[%dA", rows-rpos2);
607632
abAppend(&ab,seq,strlen(seq));
608633
}
609634
610635
/* Set column. */
611636
col = (plen+(int)l->pos) % (int)l->cols;
612
- /* lndebug("set col %d", 1+col); */
637
+ lndebug("set col %d", 1+col);
613638
if (col)
614639
snprintf(seq,64,"\r\x1b[%dC", col);
615640
else
616641
snprintf(seq,64,"\r");
617642
abAppend(&ab,seq,strlen(seq));
618643
619
- /* lndebug("\n"); */
644
+ lndebug("\n");
620645
l->oldpos = l->pos;
621646
622647
if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
623648
abFree(&ab);
624649
}
@@ -643,11 +668,12 @@
643668
l->len++;
644669
l->buf[l->len] = '\0';
645670
if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) {
646671
/* Avoid a full update of the line in the
647672
* trivial case. */
648
- if (write(l->ofd,&c,1) == -1) return -1;
673
+ char d = (maskmode==1) ? '*' : c;
674
+ if (write(l->ofd,&d,1) == -1) return -1;
649675
} else {
650676
refreshLine(l);
651677
}
652678
} else {
653679
memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
654680
--- src/linenoise.c
+++ src/linenoise.c
@@ -123,10 +123,11 @@
123 static linenoiseCompletionCallback *completionCallback = NULL;
124 static linenoiseHintsCallback *hintsCallback = NULL;
125 static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
126
127 static struct termios orig_termios; /* In order to restore at exit.*/
 
128 static int rawmode = 0; /* For atexit() function to check if restore is needed*/
129 static int mlmode = 0; /* Multi line mode. Default is single line. */
130 static int atexit_registered = 0; /* Register atexit just 1 time. */
131 static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
132 static int history_len = 0;
@@ -194,10 +195,23 @@
194 #else
195 #define lndebug(fmt, ...)
196 #endif
197
198 /* ======================= Low level terminal handling ====================== */
 
 
 
 
 
 
 
 
 
 
 
 
 
199
200 /* Set if to use or not the multi line mode. */
201 void linenoiseSetMultiLine(int ml) {
202 mlmode = ml;
203 }
@@ -483,10 +497,12 @@
483 int hintmaxlen = l->cols-(plen+l->len);
484 if (hintlen > hintmaxlen) hintlen = hintmaxlen;
485 if (bold == 1 && color == -1) color = 37;
486 if (color != -1 || bold != 0)
487 snprintf(seq,64,"\033[%d;%d;49m",bold,color);
 
 
488 abAppend(ab,seq,strlen(seq));
489 abAppend(ab,hint,hintlen);
490 if (color != -1 || bold != 0)
491 abAppend(ab,"\033[0m",4);
492 /* Call the function to free the hint returned. */
@@ -521,11 +537,15 @@
521 /* Cursor to left edge */
522 snprintf(seq,64,"\r");
523 abAppend(&ab,seq,strlen(seq));
524 /* Write the prompt and the current buffer content */
525 abAppend(&ab,l->prompt,strlen(l->prompt));
526 abAppend(&ab,buf,len);
 
 
 
 
527 /* Show hits if any. */
528 refreshShowHints(&ab,l,plen);
529 /* Erase to right */
530 snprintf(seq,64,"\x1b[0K");
531 abAppend(&ab,seq,strlen(seq));
@@ -556,30 +576,35 @@
556
557 /* First step: clear all the lines used before. To do so start by
558 * going to the last row. */
559 abInit(&ab);
560 if (old_rows-rpos > 0) {
561 /* lndebug("go down %d", old_rows-rpos); */
562 snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
563 abAppend(&ab,seq,strlen(seq));
564 }
565
566 /* Now for every row clear it, go up. */
567 for (j = 0; j < old_rows-1; j++) {
568 /* lndebug("clear+up"); */
569 snprintf(seq,64,"\r\x1b[0K\x1b[1A");
570 abAppend(&ab,seq,strlen(seq));
571 }
572
573 /* Clean the top line. */
574 /* lndebug("clear"); */
575 snprintf(seq,64,"\r\x1b[0K");
576 abAppend(&ab,seq,strlen(seq));
577
578 /* Write the prompt and the current buffer content */
579 abAppend(&ab,l->prompt,strlen(l->prompt));
580 abAppend(&ab,l->buf,l->len);
 
 
 
 
 
581
582 /* Show hits if any. */
583 refreshShowHints(&ab,l,plen);
584
585 /* If we are at the very end of the screen with our prompt, we need to
@@ -586,39 +611,39 @@
586 * emit a newline and move the prompt to the first column. */
587 if (l->pos &&
588 l->pos == l->len &&
589 (l->pos+plen) % l->cols == 0)
590 {
591 /* lndebug("<newline>"); */
592 abAppend(&ab,"\n",1);
593 snprintf(seq,64,"\r");
594 abAppend(&ab,seq,strlen(seq));
595 rows++;
596 if (rows > (int)l->maxrows) l->maxrows = rows;
597 }
598
599 /* Move cursor to right position. */
600 rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
601 /* lndebug("rpos2 %d", rpos2); */
602
603 /* Go up till we reach the expected positon. */
604 if (rows-rpos2 > 0) {
605 /* lndebug("go-up %d", rows-rpos2); */
606 snprintf(seq,64,"\x1b[%dA", rows-rpos2);
607 abAppend(&ab,seq,strlen(seq));
608 }
609
610 /* Set column. */
611 col = (plen+(int)l->pos) % (int)l->cols;
612 /* lndebug("set col %d", 1+col); */
613 if (col)
614 snprintf(seq,64,"\r\x1b[%dC", col);
615 else
616 snprintf(seq,64,"\r");
617 abAppend(&ab,seq,strlen(seq));
618
619 /* lndebug("\n"); */
620 l->oldpos = l->pos;
621
622 if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
623 abFree(&ab);
624 }
@@ -643,11 +668,12 @@
643 l->len++;
644 l->buf[l->len] = '\0';
645 if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) {
646 /* Avoid a full update of the line in the
647 * trivial case. */
648 if (write(l->ofd,&c,1) == -1) return -1;
 
649 } else {
650 refreshLine(l);
651 }
652 } else {
653 memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
654
--- src/linenoise.c
+++ src/linenoise.c
@@ -123,10 +123,11 @@
123 static linenoiseCompletionCallback *completionCallback = NULL;
124 static linenoiseHintsCallback *hintsCallback = NULL;
125 static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
126
127 static struct termios orig_termios; /* In order to restore at exit.*/
128 static int maskmode = 0; /* Show "***" instead of input. For passwords. */
129 static int rawmode = 0; /* For atexit() function to check if restore is needed*/
130 static int mlmode = 0; /* Multi line mode. Default is single line. */
131 static int atexit_registered = 0; /* Register atexit just 1 time. */
132 static int history_max_len = LINENOISE_DEFAULT_HISTORY_MAX_LEN;
133 static int history_len = 0;
@@ -194,10 +195,23 @@
195 #else
196 #define lndebug(fmt, ...)
197 #endif
198
199 /* ======================= Low level terminal handling ====================== */
200
201 /* Enable "mask mode". When it is enabled, instead of the input that
202 * the user is typing, the terminal will just display a corresponding
203 * number of asterisks, like "****". This is useful for passwords and other
204 * secrets that should not be displayed. */
205 void linenoiseMaskModeEnable(void) {
206 maskmode = 1;
207 }
208
209 /* Disable mask mode. */
210 void linenoiseMaskModeDisable(void) {
211 maskmode = 0;
212 }
213
214 /* Set if to use or not the multi line mode. */
215 void linenoiseSetMultiLine(int ml) {
216 mlmode = ml;
217 }
@@ -483,10 +497,12 @@
497 int hintmaxlen = l->cols-(plen+l->len);
498 if (hintlen > hintmaxlen) hintlen = hintmaxlen;
499 if (bold == 1 && color == -1) color = 37;
500 if (color != -1 || bold != 0)
501 snprintf(seq,64,"\033[%d;%d;49m",bold,color);
502 else
503 seq[0] = '\0';
504 abAppend(ab,seq,strlen(seq));
505 abAppend(ab,hint,hintlen);
506 if (color != -1 || bold != 0)
507 abAppend(ab,"\033[0m",4);
508 /* Call the function to free the hint returned. */
@@ -521,11 +537,15 @@
537 /* Cursor to left edge */
538 snprintf(seq,64,"\r");
539 abAppend(&ab,seq,strlen(seq));
540 /* Write the prompt and the current buffer content */
541 abAppend(&ab,l->prompt,strlen(l->prompt));
542 if (maskmode == 1) {
543 while (len--) abAppend(&ab,"*",1);
544 } else {
545 abAppend(&ab,buf,len);
546 }
547 /* Show hits if any. */
548 refreshShowHints(&ab,l,plen);
549 /* Erase to right */
550 snprintf(seq,64,"\x1b[0K");
551 abAppend(&ab,seq,strlen(seq));
@@ -556,30 +576,35 @@
576
577 /* First step: clear all the lines used before. To do so start by
578 * going to the last row. */
579 abInit(&ab);
580 if (old_rows-rpos > 0) {
581 lndebug("go down %d", old_rows-rpos);
582 snprintf(seq,64,"\x1b[%dB", old_rows-rpos);
583 abAppend(&ab,seq,strlen(seq));
584 }
585
586 /* Now for every row clear it, go up. */
587 for (j = 0; j < old_rows-1; j++) {
588 lndebug("clear+up");
589 snprintf(seq,64,"\r\x1b[0K\x1b[1A");
590 abAppend(&ab,seq,strlen(seq));
591 }
592
593 /* Clean the top line. */
594 lndebug("clear");
595 snprintf(seq,64,"\r\x1b[0K");
596 abAppend(&ab,seq,strlen(seq));
597
598 /* Write the prompt and the current buffer content */
599 abAppend(&ab,l->prompt,strlen(l->prompt));
600 if (maskmode == 1) {
601 unsigned int i;
602 for (i = 0; i < l->len; i++) abAppend(&ab,"*",1);
603 } else {
604 abAppend(&ab,l->buf,l->len);
605 }
606
607 /* Show hits if any. */
608 refreshShowHints(&ab,l,plen);
609
610 /* If we are at the very end of the screen with our prompt, we need to
@@ -586,39 +611,39 @@
611 * emit a newline and move the prompt to the first column. */
612 if (l->pos &&
613 l->pos == l->len &&
614 (l->pos+plen) % l->cols == 0)
615 {
616 lndebug("<newline>");
617 abAppend(&ab,"\n",1);
618 snprintf(seq,64,"\r");
619 abAppend(&ab,seq,strlen(seq));
620 rows++;
621 if (rows > (int)l->maxrows) l->maxrows = rows;
622 }
623
624 /* Move cursor to right position. */
625 rpos2 = (plen+l->pos+l->cols)/l->cols; /* current cursor relative row. */
626 lndebug("rpos2 %d", rpos2);
627
628 /* Go up till we reach the expected positon. */
629 if (rows-rpos2 > 0) {
630 lndebug("go-up %d", rows-rpos2);
631 snprintf(seq,64,"\x1b[%dA", rows-rpos2);
632 abAppend(&ab,seq,strlen(seq));
633 }
634
635 /* Set column. */
636 col = (plen+(int)l->pos) % (int)l->cols;
637 lndebug("set col %d", 1+col);
638 if (col)
639 snprintf(seq,64,"\r\x1b[%dC", col);
640 else
641 snprintf(seq,64,"\r");
642 abAppend(&ab,seq,strlen(seq));
643
644 lndebug("\n");
645 l->oldpos = l->pos;
646
647 if (write(fd,ab.b,ab.len) == -1) {} /* Can't recover from write error. */
648 abFree(&ab);
649 }
@@ -643,11 +668,12 @@
668 l->len++;
669 l->buf[l->len] = '\0';
670 if ((!mlmode && l->plen+l->len < l->cols && !hintsCallback)) {
671 /* Avoid a full update of the line in the
672 * trivial case. */
673 char d = (maskmode==1) ? '*' : c;
674 if (write(l->ofd,&d,1) == -1) return -1;
675 } else {
676 refreshLine(l);
677 }
678 } else {
679 memmove(l->buf+l->pos+1,l->buf+l->pos,l->len-l->pos);
680
--- src/linenoise.h
+++ src/linenoise.h
@@ -63,10 +63,12 @@
6363
int linenoiseHistorySave(const char *filename);
6464
int linenoiseHistoryLoad(const char *filename);
6565
void linenoiseClearScreen(void);
6666
void linenoiseSetMultiLine(int ml);
6767
void linenoisePrintKeyCodes(void);
68
+void linenoiseMaskModeEnable(void);
69
+void linenoiseMaskModeDisable(void);
6870
6971
#ifdef __cplusplus
7072
}
7173
#endif
7274
7375
--- src/linenoise.h
+++ src/linenoise.h
@@ -63,10 +63,12 @@
63 int linenoiseHistorySave(const char *filename);
64 int linenoiseHistoryLoad(const char *filename);
65 void linenoiseClearScreen(void);
66 void linenoiseSetMultiLine(int ml);
67 void linenoisePrintKeyCodes(void);
 
 
68
69 #ifdef __cplusplus
70 }
71 #endif
72
73
--- src/linenoise.h
+++ src/linenoise.h
@@ -63,10 +63,12 @@
63 int linenoiseHistorySave(const char *filename);
64 int linenoiseHistoryLoad(const char *filename);
65 void linenoiseClearScreen(void);
66 void linenoiseSetMultiLine(int ml);
67 void linenoisePrintKeyCodes(void);
68 void linenoiseMaskModeEnable(void);
69 void linenoiseMaskModeDisable(void);
70
71 #ifdef __cplusplus
72 }
73 #endif
74
75

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button