Jelajahi Sumber

feat: ctrl+c 信号处理

SongZihuan 4 tahun lalu
induk
melakukan
c7bd31563a
5 mengubah file dengan 39 tambahan dan 14 penghapusan
  1. 8 3
      argument/argument.c
  2. 1 0
      include/arguement.h
  3. 1 0
      include/macro.h
  4. 28 10
      main.c
  5. 1 1
      src/inter.c

+ 8 - 3
argument/argument.c

@@ -11,10 +11,11 @@ char *HelloString = "Welcome To VirtualMath ("__TIME__", "__DATE__") \n"
 static const struct option long_option[]={
         {"log-err",required_argument,NULL,'o'},
         {"log-out",required_argument,NULL,'e'},
+        {"not-run-cl",required_argument,NULL,'n'},
         {NULL,0,NULL,0}
 };
 
-static const char *short_option = "o:e:";
+static const char *short_option = "o:e:n";
 
 /**
  * 参数设置, args是全局结构体, 保存全局的参数设置
@@ -25,8 +26,9 @@ static const char *short_option = "o:e:";
 int getArgs(const int argc, char **argv)
 {
     args.out_file = NULL;
-    args.error_file = false;
-    opterr = false;
+    args.error_file = NULL;
+    args.run_commandLine = true;
+    opterr = true;
     int opt;
     while((opt=getopt_long(argc, argv, short_option ,long_option,NULL))!=-1)
     {
@@ -40,6 +42,9 @@ int getArgs(const int argc, char **argv)
             case 'e':
                 args.error_file = memStrcpy(optarg);
                 break;
+            case 'n':
+                args.run_commandLine = false;
+                break;
             case '?':
                 fprintf(stderr, "[Error]: get not success args : -%c\n", (char)optopt);
                 return -1;

+ 1 - 0
include/arguement.h

@@ -5,6 +5,7 @@ extern char *HelloString;
 struct Args{
     char *error_file;
     char *out_file;
+    bool run_commandLine;
 } args;
 
 typedef struct Args Args;

+ 1 - 0
include/macro.h

@@ -10,6 +10,7 @@
 #include <unistd.h>
 #include <ctype.h>
 #include <setjmp.h>
+#include <signal.h>
 
 // 布尔逻辑的定义
 #define bool int

+ 28 - 10
main.c

@@ -1,23 +1,34 @@
 #include "__virtualmath.h"
 
+jmp_buf ctrl_c;
+bool set_ctrl_c = false;
+void sighandler(int signum);
+
 int main(int argc, char *argv[]) {
     Inter *inter = NULL;
-
-    memVirtualMathUseJmp = true;
-    if (setjmp(memVirtualMath_Env) == -1)  // 遇到内存错误
-        return 2;
-
     if (getArgs(argc, argv))
         goto args_error;
 
     inter = makeInter(args.out_file, args.error_file, NULL);
+
+    if (setjmp(ctrl_c) == -1 || setjmp(memVirtualMath_Env) == -1) {
+        freeArgs();
+        freeInter(inter, true);
+        return 1;
+    }
+    memVirtualMathUseJmp = true;
+    set_ctrl_c = true;
+    signal(SIGINT, sighandler);
+
     for (ResultType status = not_return; status != error_return && argv[optind] != NULL; optind++)
         status = runCodeBlock(argv[optind], inter);
-    printf("%s", HelloString);
-    while (1){
-        runCodeStdin(inter);
-        if (ferror(stdin) || feof(stdin))
-            break;
+    if (args.run_commandLine) {
+        printf("%s", HelloString);
+        while (1) {
+            runCodeStdin(inter);
+            if (ferror(stdin) || feof(stdin))
+                break;
+        }
     }
     freeInter(inter, true);
 
@@ -26,6 +37,13 @@ int main(int argc, char *argv[]) {
 }
 
 
+void sighandler(int signum) {
+    if (set_ctrl_c)
+        longjmp(ctrl_c, -1);
+    else
+        exit(1);
+}
+
 /** TODO-szh List
  * 官方函数
  * 官方类

+ 1 - 1
src/inter.c

@@ -129,7 +129,7 @@ void freeInter(Inter *inter, bool show_gc) {
     gc_runDelAll(inter);
     freeBaseInterData(inter);
 #if DEBUG
-    if (show_gc && (printf("Enter '1' to show gc: "), getc(stdin) == '1')) {
+    if (show_gc && (printf("\nEnter '1' to show gc: "), getc(stdin) == '1')) {
         printGC(inter);
         while (getc(stdin) != '\n')
             PASS;