add mask input mode Signed-off-by: lifubang <lifubang@acmcoder.com>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
diff --git a/example.c b/example.c
index 3a544d3..74358c3 100644
--- a/example.c
+++ b/example.c
@@ -55,6 +55,7 @@ int main(int argc, char **argv) {
*
* The typed string is returned as a malloc() allocated string by
* linenoise, so the user needs to free() it. */
+
while((line = linenoise("hello> ")) != NULL) {
/* Do something with the string. */
if (line[0] != '\0' && line[0] != '/') {
@@ -65,6 +66,10 @@ int main(int argc, char **argv) {
/* The "/historylen" command will change the history len. */
int len = atoi(line+11);
linenoiseHistorySetMaxLen(len);
+ } else if (!strncmp(line, "/mask", 5)) {
+ linenoiseMaskModeEnable();
+ } else if (!strncmp(line, "/unmask", 7)) {
+ linenoiseMaskModeDisable();
} else if (line[0] == '/') {
printf("Unreconized command: %s\n", line);
}
diff --git a/linenoise.c b/linenoise.c
index 10ffd71..8a89835 100644
--- a/linenoise.c
+++ b/linenoise.c
@@ -125,6 +125,7 @@ static linenoiseHintsCallback *hintsCallback = NULL;
static linenoiseFreeHintsCallback *freeHintsCallback = NULL;
static struct termios orig_termios; /* In order to restore at exit.*/
+static int maskmode = 0; /* for mask input mode */
static int rawmode = 0; /* For atexit() function to check if restore is needed*/
static int mlmode = 0; /* Multi line mode. Default is single line. */
static int atexit_registered = 0; /* Register atexit just 1 time. */
@@ -197,6 +198,14 @@ FILE *lndebug_fp = NULL;
/* ======================= Low level terminal handling ====================== */
+void linenoiseMaskModeEnable() {
+ maskmode = 1;
+}
+
+void linenoiseMaskModeDisable() {
+ maskmode = 0;
+}
+
/* Set if to use or not the multi line mode. */
void linenoiseSetMultiLine(int ml) {
mlmode = ml;
@@ -525,7 +534,12 @@ static void refreshSingleLine(struct linenoiseState *l) {
abAppend(&ab,seq,strlen(seq));
/* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt));
- abAppend(&ab,buf,len);
+ if (maskmode == 1) {
+ while (len--) {
+ abAppend(&ab,"*",1);
+ }
+ } else
+ abAppend(&ab,buf,len);
/* Show hits if any. */
refreshShowHints(&ab,l,plen);
/* Erase to right */
@@ -579,7 +593,12 @@ static void refreshMultiLine(struct linenoiseState *l) {
/* Write the prompt and the current buffer content */
abAppend(&ab,l->prompt,strlen(l->prompt));
- abAppend(&ab,l->buf,l->len);
+ if (maskmode == 1) {
+ for (uint i = 0; i < l->len; i++) {
+ abAppend(&ab,"*",1);
+ }
+ } else
+ abAppend(&ab,l->buf,l->len);
/* Show hits if any. */
refreshShowHints(&ab,l,plen);
diff --git a/linenoise.h b/linenoise.h
index ed20232..d031ed0 100644
--- a/linenoise.h
+++ b/linenoise.h
@@ -66,6 +66,9 @@ void linenoiseClearScreen(void);
void linenoiseSetMultiLine(int ml);
void linenoisePrintKeyCodes(void);
+void linenoiseMaskModeEnable();
+void linenoiseMaskModeDisable();
+
#ifdef __cplusplus
}
#endif