Commit 622c777f41d4ec3f7c1c189df35c265eb8ac3cff

antirez 2023-03-26T16:42:22

Multiplexing: implement example using it.

diff --git a/example.c b/example.c
index 8959540..69937a8 100644
--- a/example.c
+++ b/example.c
@@ -1,6 +1,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <sys/select.h>
 #include "linenoise.h"
 
 void completion(const char *buf, linenoiseCompletions *lc) {
@@ -22,6 +23,7 @@ char *hints(const char *buf, int *color, int *bold) {
 int main(int argc, char **argv) {
     char *line;
     char *prgname = argv[0];
+    int async = 0;
 
     /* Parse options, with --multiline we enable multi line editing. */
     while(argc > 1) {
@@ -33,8 +35,10 @@ int main(int argc, char **argv) {
         } else if (!strcmp(*argv,"--keycodes")) {
             linenoisePrintKeyCodes();
             exit(0);
+        } else if (!strcmp(*argv,"--async")) {
+            async = 1;
         } else {
-            fprintf(stderr, "Usage: %s [--multiline] [--keycodes]\n", prgname);
+            fprintf(stderr, "Usage: %s [--multiline] [--keycodes] [--async]\n", prgname);
             exit(1);
         }
     }
@@ -54,8 +58,53 @@ 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) {
+
+    while(1) {
+        if (!async) {
+            line = linenoise("hello> ");
+            if (line == NULL) break;
+        } else {
+            /* Asynchronous mode using the multiplexing API: wait for
+             * data on stdin, and simulate async data coming from some source
+             * using the select(2) timeout. */
+            struct linenoiseState ls;
+            char buf[1024];
+            char *async_retval;
+            int async_len = 0;
+            linenoiseEditStart(&ls,-1,-1,buf,sizeof(buf),"hello> ");
+            while(1) {
+		fd_set readfds;
+		struct timeval tv;
+		int retval;
+
+		FD_ZERO(&readfds);
+		FD_SET(ls.ifd, &readfds);
+		tv.tv_sec = 1; // 1 sec timeout
+		tv.tv_usec = 0;
+
+		retval = select(ls.ifd+1, &readfds, NULL, NULL, &tv);
+		if (retval == -1) {
+		    perror("select()");
+                    exit(1);
+		} else if (retval) {
+		    async_retval = linenoiseEditFeed(&ls,&async_len);
+                    /* A NULL return means: line editing is continuing.
+                     * Otherwise the user hit enter or stopped editing
+                     * (CTRL+C/D). */
+                    if (async_retval != NULL) break;
+		} else {
+		    // Timeout occurred
+                    static int counter = 0;
+                    linenoiseHide(&ls);
+		    printf("Async output %d.\n", counter++);
+                    linenoiseShow(&ls);
+		}
+            }
+            linenoiseEditStop(&ls);
+            if (async_len == -1) exit(0); /* Ctrl+D/C. */
+            line = strdup(async_retval);
+        }
+
         /* Do something with the string. */
         if (line[0] != '\0' && line[0] != '/') {
             printf("echo: '%s'\n", line);