]> git.djapps.eu Git - pkg/ggml/sources/llama.cpp/commitdiff
Gate signal support on being on a unixoid system. (#74)
authorMatvey Soloviev <redacted>
Mon, 13 Mar 2023 03:08:01 +0000 (04:08 +0100)
committerMatvey Soloviev <redacted>
Mon, 13 Mar 2023 03:08:01 +0000 (04:08 +0100)
main.cpp

index d1defe27381dbf08e6a2503bf61d205385672bf8..98ccde5dbf63b5a3bf492886e53a005b67bacdd9 100644 (file)
--- a/main.cpp
+++ b/main.cpp
 #include <string>
 #include <vector>
 
+#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
 #include <signal.h>
 #include <unistd.h>
+#endif
 
 #define ANSI_COLOR_RED     "\x1b[31m"
 #define ANSI_COLOR_GREEN   "\x1b[32m"
@@ -747,6 +749,7 @@ bool llama_eval(
 
 static bool is_interacting = false;
 
+#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
 void sigint_handler(int signo) {
     if (signo == SIGINT) {
         if (!is_interacting) {
@@ -756,6 +759,7 @@ void sigint_handler(int signo) {
         }
     }
 }
+#endif
 
 int main(int argc, char ** argv) {
     ggml_time_init();
@@ -822,11 +826,13 @@ int main(int argc, char ** argv) {
     }
     printf("\n");
     if (params.interactive) {
+#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
         struct sigaction sigint_action;
         sigint_action.sa_handler = sigint_handler;
         sigemptyset (&sigint_action.sa_mask);
         sigint_action.sa_flags = 0; 
         sigaction(SIGINT, &sigint_action, NULL);
+#endif
 
         printf("%s: interactive mode on.\n", __func__);
 
@@ -855,7 +861,9 @@ int main(int argc, char ** argv) {
 
     if (params.interactive) {
         printf("== Running in interactive mode. ==\n"
+#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
                " - Press Ctrl+C to interject at any time.\n"
+#endif
                " - Press Return to return control to LLaMa.\n"
                " - If you want to submit another line, end your input in '\\'.\n");
     }
@@ -957,10 +965,15 @@ int main(int argc, char ** argv) {
                 // currently being interactive 
                 bool another_line=true;
                 while (another_line) {
+                    fflush(stdout);
                     char buf[256] = {0};
                     int n_read;
                     if(params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
-                    scanf("%255[^\n]%n%*c", buf, &n_read);
+                    if (scanf("%255[^\n]%n%*c", buf, &n_read) <= 0) {
+                        // presumable empty line, consume the newline
+                        scanf("%*c");
+                        n_read=0;
+                    }
                     if(params.use_color) printf(ANSI_COLOR_RESET);
 
                     if (n_read > 0 && buf[n_read-1]=='\\') {