Skip to content

Commit a169bb8

Browse files
committed
Gate signal support on being on a unixoid system. (#74)
1 parent 460c482 commit a169bb8

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

main.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
#include <string>
1212
#include <vector>
1313

14+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
1415
#include <signal.h>
1516
#include <unistd.h>
17+
#endif
1618

1719
#define ANSI_COLOR_RED "\x1b[31m"
1820
#define ANSI_COLOR_GREEN "\x1b[32m"
@@ -747,6 +749,7 @@ bool llama_eval(
747749

748750
static bool is_interacting = false;
749751

752+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
750753
void sigint_handler(int signo) {
751754
if (signo == SIGINT) {
752755
if (!is_interacting) {
@@ -756,6 +759,7 @@ void sigint_handler(int signo) {
756759
}
757760
}
758761
}
762+
#endif
759763

760764
int main(int argc, char ** argv) {
761765
ggml_time_init();
@@ -822,11 +826,13 @@ int main(int argc, char ** argv) {
822826
}
823827
printf("\n");
824828
if (params.interactive) {
829+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
825830
struct sigaction sigint_action;
826831
sigint_action.sa_handler = sigint_handler;
827832
sigemptyset (&sigint_action.sa_mask);
828833
sigint_action.sa_flags = 0;
829834
sigaction(SIGINT, &sigint_action, NULL);
835+
#endif
830836

831837
printf("%s: interactive mode on.\n", __func__);
832838

@@ -855,7 +861,9 @@ int main(int argc, char ** argv) {
855861

856862
if (params.interactive) {
857863
printf("== Running in interactive mode. ==\n"
864+
#if defined (__unix__) || (defined (__APPLE__) && defined (__MACH__))
858865
" - Press Ctrl+C to interject at any time.\n"
866+
#endif
859867
" - Press Return to return control to LLaMa.\n"
860868
" - If you want to submit another line, end your input in '\\'.\n");
861869
}
@@ -957,10 +965,15 @@ int main(int argc, char ** argv) {
957965
// currently being interactive
958966
bool another_line=true;
959967
while (another_line) {
968+
fflush(stdout);
960969
char buf[256] = {0};
961970
int n_read;
962971
if(params.use_color) printf(ANSI_BOLD ANSI_COLOR_GREEN);
963-
scanf("%255[^\n]%n%*c", buf, &n_read);
972+
if (scanf("%255[^\n]%n%*c", buf, &n_read) <= 0) {
973+
// presumable empty line, consume the newline
974+
scanf("%*c");
975+
n_read=0;
976+
}
964977
if(params.use_color) printf(ANSI_COLOR_RESET);
965978

966979
if (n_read > 0 && buf[n_read-1]=='\\') {

0 commit comments

Comments
 (0)