Commit f31e883a0852ff6a09a7fa152f337c6360fef5c7

Salvatore Sanfilippo 2020-03-02T16:56:27

Merge pull request #182 from lifubang/masking add mask input mode

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