/Users/huttone/Devel/sedflux-new/sedflux/trunk/ew/sedflux/sedflux_signal.c

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include <signal.h>
00004 #include <glib.h>
00005 #include <utils/utils.h>
00006 #include <sed/sed_sedflux.h>
00007 
00008 void print_choices( int sig_num );
00009 
00010 int
00011 sedflux_set_signal_action( void )
00012 {
00013    gint rtn_val;
00014    struct sigaction new_action, old_action;
00015 
00016    /* Set the new action */
00017    new_action.sa_handler = print_choices;
00018    sigemptyset( &new_action.sa_mask );
00019    new_action.sa_flags = 0;
00020 
00021    /* If the signal has already been set to be ignored, respect that */
00022    rtn_val = sigaction( SIGINT , NULL , &old_action );
00023    if ( old_action.sa_handler != SIG_IGN )
00024       rtn_val = sigaction( SIGINT , &new_action , NULL );
00025 
00026    return rtn_val;
00027 }
00028 
00029 static gboolean __quit_signal = FALSE; //< signal to indicate that the user wishes to quit.
00030 static gboolean __dump_signal = FALSE; //< signal to indicate that the user wishes to dump output.
00031 static gboolean __cpr_signal  = FALSE; //< signal to indicate that the user wishes to create a checkpoint.
00032 
00033 gboolean
00034 sedflux_signal_is_pending( Sedflux_sig_num sig )
00035 {
00036    gboolean is_pending = FALSE;
00037 
00038    if ( sig==SEDFLUX_SIG_QUIT && __quit_signal )
00039       is_pending = TRUE;
00040    if ( sig==SEDFLUX_SIG_DUMP && __dump_signal )
00041       is_pending = TRUE;
00042    if ( sig==SEDFLUX_SIG_CPR  && __cpr_signal )
00043       is_pending = TRUE;
00044    
00045    return is_pending;
00046 }
00047 
00048 void
00049 sedflux_signal_reset( Sedflux_sig_num sig )
00050 {
00051    if ( sig&SEDFLUX_SIG_QUIT && __quit_signal )
00052       __quit_signal = FALSE;
00053    if ( sig&SEDFLUX_SIG_DUMP && __dump_signal )
00054       __dump_signal = FALSE;
00055    if ( sig&SEDFLUX_SIG_CPR  && __cpr_signal )
00056       __cpr_signal  = FALSE;
00057 
00058    return;
00059 }
00060 
00061 void
00062 sedflux_signal_set( Sedflux_sig_num sig )
00063 {
00064    if ( sig&SEDFLUX_SIG_QUIT )
00065       __quit_signal = TRUE;
00066    if ( sig&SEDFLUX_SIG_DUMP )
00067       __dump_signal = TRUE;
00068    if ( sig&SEDFLUX_SIG_CPR  )
00069       __cpr_signal  = TRUE;
00070 
00071    return;
00072 }
00073 
00074 void
00075 print_choices( int sig_num )
00076 {
00077    gboolean is_invalid = FALSE;
00078    char     ch;
00079 
00080    fprintf( stdout , "\n"                                                );
00081    fprintf( stdout , "-----------------------------------------------\n" );
00082    fprintf( stdout , "  (1) End run after this time step.\n"             );
00083    fprintf( stdout , "  (2) Dump results and continue.\n"                );
00084    fprintf( stdout , "  (3) Create a checkpoint.\n"                      );
00085    fprintf( stdout , "  (4) Continue.\n"                                 );
00086    fprintf( stdout , "  (5) Quit immediatly (without saving).\n"         );
00087    fprintf( stdout , "-----------------------------------------------\n" );
00088 
00089    do
00090    {
00091       fprintf( stdout , "   Your choice [4]? " );
00092 
00093       is_invalid = FALSE;
00094 
00095       fscanf(stdin,"%s",&ch);
00096 
00097       if (      g_ascii_strcasecmp( &ch , "1" )==0 )
00098       {
00099          sedflux_signal_set( SEDFLUX_SIG_QUIT );
00100          fprintf(stdout,"   You have opted to quit early...\n\n");
00101       }
00102       else if ( g_ascii_strcasecmp( &ch , "2" )==0 )
00103       {
00104          sedflux_signal_set( SEDFLUX_SIG_DUMP );
00105          fprintf( stdout ,
00106                   "   Temporary output files will be dumped at the end of this time step...\n\n");
00107       }
00108       else if ( g_ascii_strcasecmp( &ch , "3" )==0 )
00109       {
00110          sedflux_signal_set( SEDFLUX_SIG_CPR );
00111          fprintf(stdout,"   Creating a checkpoint/reset file...\n\n");
00112       }
00113       else if (    g_ascii_strcasecmp( &ch , "4" )==0 
00114                 || g_ascii_strcasecmp( &ch , "" )==0 )
00115       {
00116          fprintf(stdout,"   Continuing...\n\n");
00117       }
00118       else if (    g_ascii_strcasecmp( &ch , "5" )==0 )
00119       {
00120          fprintf(stdout,"   Terminating run without saving...\n\n" );
00121          eh_exit( EXIT_SUCCESS );
00122       }
00123       else
00124          is_invalid = TRUE;
00125    }
00126    while ( is_invalid );
00127 
00128    return;
00129 }
00130 
00131 

Generated on Fri Jan 4 18:04:16 2008 for sedflux by  doxygen 1.5.2