chiark / gitweb /
fixed XBITS==4
[moebius2.git] / minimise.c
index bf5b096c89339acdee0428147f503c2f94bfcc41..60f2d646e08d5495f72829ee8513dfaba3011ffd 100644 (file)
@@ -62,6 +62,12 @@ static void badusage(void) {
   exit(8);
 }
 
+static sig_atomic_t quit_requested;
+
+static void sigint_handler(int ignored) {
+  quit_requested= 1;
+}
+
 int main(int argc, const char *const *argv) {
   gsl_multimin_function multimin_function;
   double size;
@@ -70,6 +76,7 @@ int main(int argc, const char *const *argv) {
   FILE *initial_f;
   gsl_vector initial_gsl, step_size_gsl;
   int r, i, size_check_counter= 0;
+  struct sigaction sa;
 
   if (argc==3) {
     input_file= argv[1];
@@ -88,6 +95,13 @@ int main(int argc, const char *const *argv) {
   if (final_file)
     if (asprintf(&final_file_tmp,"%s.new",final_file) <= 0) diee("asprintf");
 
+  memset(&sa,0,sizeof(sa));
+  sa.sa_handler= sigint_handler;
+  sa.sa_flags= SA_RESETHAND;
+  r= sigaction(SIGINT,&sa,0);
+  if (r) diee("sigaction SIGINT");
+
+  mgraph_prepare();
   graph_layout_prepare();
   printing_init();
   energy_init();
@@ -124,6 +138,11 @@ int main(int argc, const char *const *argv) {
                                  &initial_gsl, &step_size_gsl) );
 
   for (;;) {
+    if (quit_requested) {
+      fprintf(stderr,"SIGINT caught.\n");
+      exit(1);
+    }
+
     GA( gsl_multimin_fminimizer_iterate(minimiser) );
 
     if (!(size_check_counter++ % DIM)) {