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

Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <string.h>
00003 #include <errno.h>
00004 #include <unistd.h>
00005 #include <glib.h>
00006 #include <glib/gstdio.h>
00007 #include <utils/utils.h>
00008 #include <sed/sed_sedflux.h>
00009 
00010 #include <bio.h>
00011 
00012 #include "sedflux.h"
00013 
00014 Eh_project fill_sedflux_info_file             ( Eh_project p         ,
00015                                                 const gchar* cmd_str ,
00016                                                 const gchar* desc    );
00017 gchar*     sedflux_get_file_name_interactively( gchar **working_dir ,
00018                                                 gchar **in_file     );
00019 
00020 /* Command line options */
00021 static gboolean mode_3d      = FALSE;
00022 static gboolean mode_2d      = FALSE;
00023 static gchar*   init_file    = NULL;
00024 static gchar*   out_file     = NULL; 
00025 static gchar*   working_dir  = NULL;
00026 static gchar*   run_desc     = NULL;
00027 static gboolean just_plume   = FALSE;
00028 static gboolean just_rng     = FALSE;
00029 static gboolean summary      = FALSE;
00030 static gboolean warn         = FALSE;
00031 static gint     verbosity    = -1;
00032 static gboolean verbose      = FALSE;
00033 static gboolean version      = FALSE;
00034 static char**   active_procs = NULL;
00035 
00036 /* Define the command line options */
00037 static GOptionEntry command_line_entries[] =
00038 {
00039    { "3-dim"       , '3' , 0 , G_OPTION_ARG_NONE         , &mode_3d     , "Run in 3D mode"             , NULL     } ,
00040    { "2-dim"       , '2' , 0 , G_OPTION_ARG_NONE         , &mode_2d     , "Run in 2D mode"             , NULL     } ,
00041    { "init-file"   , 'i' , 0 , G_OPTION_ARG_FILENAME     , &init_file   , "Initialization file"        , "<file>" } ,
00042    { "out-file"    , 'o' , 0 , G_OPTION_ARG_FILENAME     , &out_file    , "Output file"                , "<file>" } ,
00043    { "working-dir" , 'd' , 0 , G_OPTION_ARG_FILENAME     , &working_dir , "Working directory"          , "<dir>"  } ,
00044    { "msg"         , 'm' , 0 , G_OPTION_ARG_STRING       , &run_desc    , "Run description"            , "<msg>"  } ,
00045    { "active-proc" , 'a' , 0 , G_OPTION_ARG_STRING_ARRAY , &active_procs, "Specify active process"     , "<name>" } ,
00046    { "just-plume"  , 'p' , 0 , G_OPTION_ARG_NONE         , &just_plume  , "Run just the plume"         , NULL     } ,
00047    { "just-rng"    , 'r' , 0 , G_OPTION_ARG_NONE         , &just_rng    , "Run just the rng processes" , NULL     } ,
00048    { "summary"     , 's' , 0 , G_OPTION_ARG_NONE         , &summary     , "Print a summary and quit"   , NULL     } ,
00049    { "warn"        , 'w' , 0 , G_OPTION_ARG_NONE         , &warn        , "Print warnings"             , NULL     } ,
00050    { "verbosity"   , 'l' , 0 , G_OPTION_ARG_INT          , &verbosity   , "Verbosity level"            , "n"      } ,
00051    { "verbose"     , 'V' , 0 , G_OPTION_ARG_NONE         , &verbose     , "Be verbose"                 , NULL     } ,
00052    { "version"     , 'v' , 0 , G_OPTION_ARG_NONE         , &version     , "Version number"             , NULL     } ,
00053    { NULL }
00054 };
00055 
00056 static gchar* just_plume_procs[] = { "plume"      , "river"  ,  "bbl" , NULL }; //< Processes to run with just-plume option
00057 static gchar* just_rng_procs[]   = { "earthquake" , "storms" , NULL };          //< Process to run with just-rng option
00058 
00059 Sedflux_param_st*
00060 sedflux_parse_command_line( int argc , char *argv[] , GError** error )
00061 {
00062    Sedflux_param_st* p = NULL;
00063 
00064    eh_return_val_if_fail( error==NULL || *error==NULL , NULL );
00065    eh_require( argv );
00066 
00067    {
00068       GError*         tmp_err = NULL;
00069       GOptionContext* context = g_option_context_new( "Run basin filling model sedflux-2.0" );
00070 
00071       g_option_context_add_main_entries( context , command_line_entries , NULL );
00072       g_option_context_add_group( context , bio_get_option_group() );
00073 
00074       g_option_context_parse( context , &argc , &argv , &tmp_err );
00075 
00076       if ( (mode_3d && mode_2d) && !tmp_err )
00077          g_set_error( &tmp_err ,
00078                       SEDFLUX_ERROR ,
00079                       SEDFLUX_ERROR_MULTIPLE_MODES ,
00080                       "Mode must be either 2D or 3D" );
00081 
00082       if ( version )
00083       {
00084          eh_fprint_version_info( stdout          ,
00085                                  PROGRAM_NAME    ,
00086                                  S_MAJOR_VERSION ,
00087                                  S_MINOR_VERSION ,
00088                                  S_MICRO_VERSION );
00089          eh_exit( EXIT_SUCCESS );
00090       }
00091 
00092       if ( !tmp_err )
00093       {
00094          if ( mode_3d )
00095             sed_mode_set( SEDFLUX_MODE_3D );
00096          else
00097             sed_mode_set( SEDFLUX_MODE_2D );
00098 
00099          if      ( verbosity >= 0 )
00100             eh_set_verbosity_level( verbosity );
00101          else if ( verbose )
00102             eh_set_verbosity_level( 4 );
00103          else
00104             eh_set_verbosity_level( 0 );
00105 
00106          if ( !active_procs )
00107          {
00108             if ( just_plume )
00109                active_procs = just_plume_procs;
00110             if ( just_rng )
00111                active_procs = just_rng_procs;
00112          }
00113 
00114          g_option_context_free( context );
00115 
00116          p = eh_new( Sedflux_param_st , 1 );
00117 
00118          p->init_file    = init_file;
00119          p->out_file     = out_file;
00120          p->working_dir  = working_dir;
00121          p->run_desc     = run_desc;
00122          p->just_plume   = just_plume;
00123          p->just_rng     = just_rng;
00124          p->summary      = summary;
00125          p->warn         = warn;
00126          p->verbosity    = verbosity;
00127          p->verbose      = verbose;
00128          p->version      = version;
00129          p->active_procs = active_procs;
00130       }
00131       else
00132          g_propagate_error( error , tmp_err );
00133    }
00134 
00135    return p;
00136 }
00137 
00138 GQuark
00139 sedflux_error_quark( void )
00140 {
00141    return g_quark_from_static_string( "sedflux-error-quark" );
00142 }
00143 
00144 gboolean
00145 sedflux_setup_project_dir( gchar** init_file , gchar** working_dir , GError** error )
00146 {
00147    gboolean rtn_val = TRUE;
00148    GError*  tmp_err = NULL;
00149 
00150    eh_return_val_if_fail( error==NULL || *error==NULL , FALSE );
00151 
00152    /* If not given, prompt the user to supply the initialization file
00153       and working directory. */
00154    if ( !(*init_file) )
00155       sedflux_get_file_name_interactively( working_dir , init_file );
00156 
00157    if ( !(*working_dir) )
00158       (*working_dir) = g_strdup( "." );
00159 
00160    /* Create the working directory */
00161    if ( g_mkdir_with_parents( *working_dir , 0 ) == -1 )
00162       eh_set_file_error_from_errno( &tmp_err , *working_dir , errno );
00163 
00164    /* Make sure the working directory is writable */
00165    if ( !tmp_err && g_access( *working_dir , R_OK|W_OK|X_OK ) == -1 )
00166       eh_set_file_error_from_errno( &tmp_err , *working_dir , errno );
00167 
00168    /* Move to the working directory */
00169    if ( !tmp_err && g_chdir( *working_dir )!=0 )
00170       eh_set_file_error_from_errno( &tmp_err , *working_dir , errno );
00171 
00172    /* Make sure the initialization file is readable */
00173    if ( !tmp_err && g_access( *init_file , R_OK ) == -1 )
00174       eh_set_file_error_from_errno( &tmp_err , *init_file , errno );
00175 
00176    if ( tmp_err )
00177    {
00178       g_propagate_error( error , tmp_err );
00179       rtn_val = FALSE;
00180    }
00181 
00182    return rtn_val;
00183 }
00184 
00185 gchar* copyleft_msg[] =
00186 {
00187 "                                                                             ",
00188 " sedflux - A process based basin fill model.                                 ",
00189 " Copyright (C) 2003 Eric Hutton.                                             ",
00190 "                                                                             ",
00191 " This is free software; you can redistribute it and/or modify it under the   ",
00192 " terms of the GNU General Public License as published by the Free Software   ",
00193 " Foundation; either version 2 of the License, or (at your option) any later  ",
00194 " version.                                                                    ",
00195 "                                                                             ",
00196 " This program is distributed in the hope that it will be useful, but WITHOUT ",
00197 " ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       ",
00198 " FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for   ",
00199 " more details.                                                               ",
00200 "                                                                             ",
00201 " You should have received a copy of the GNU General Public License along with",
00202 " this program; if not, write to the Free Software Foundation, Inc., 59 Temple",
00203 " Place - Suite 330, Boston, MA 02111-1307, USA.                              ",
00204 "                                                                             ",
00205 NULL
00206 };
00207 
00208 gchar*
00209 sedflux_get_file_name_interactively( gchar **working_dir , gchar **in_file )
00210 {
00211    char *dir       = eh_new( char , 2048 );
00212    char *cur_dir   = eh_new( char , 2048 );
00213    char *init_file = g_strdup( "basin.init" );
00214 
00215    eh_require( working_dir );
00216    eh_require( in_file     );
00217 
00218    eh_print_message( stderr , copyleft_msg );
00219 
00220    fprintf( stderr , "---\n" );
00221    fprintf( stderr , "--- This is %s" , PROGRAM_NAME );
00222 
00223    if ( sed_mode_is_3d() )
00224       fprintf( stderr , "3d" );
00225 
00226    eh_fprint_version_info( stderr          ,
00227                            PROGRAM_NAME    ,
00228                            S_MAJOR_VERSION ,
00229                            S_MINOR_VERSION ,
00230                            S_MICRO_VERSION );
00231    fprintf( stderr , "---\n" );
00232 
00233    cur_dir = g_get_current_dir();
00234 
00235    fprintf( stderr , "\n" );
00236    dir       = eh_get_input_val( stdin ,
00237                                  "Set the working directory" ,
00238                                  cur_dir );
00239    init_file = eh_get_input_val( stdin ,
00240                                  "Specify an initialization file" ,
00241                                  init_file );
00242 
00243    fprintf( stderr , "\n" );
00244 
00245    fprintf( stderr , "Working directory        : %s\n" , dir );
00246    fprintf( stderr , "Initialization file      : %s\n" , init_file );
00247 
00248    *working_dir = g_strdup( dir );
00249 
00250    eh_free( *in_file     );
00251    eh_free( *working_dir );
00252 
00253    *working_dir = dir;
00254    *in_file     = init_file;
00255 
00256    eh_free( cur_dir   );
00257 
00258    return *in_file;
00259 }
00260 
00261 gint
00262 sedflux_print_info_file( const gchar* init_file , const gchar* wd , const gchar* cmd_str , const gchar* desc )
00263 {
00264    gint n = 0;
00265 
00266    {
00267       Eh_project proj = eh_create_project( "sedflux" );
00268 
00269       eh_set_project_dir    ( proj , wd );
00270       fill_sedflux_info_file( proj , cmd_str , desc );
00271 
00272       n = eh_write_project_info_file( proj );
00273 
00274       eh_destroy_project    ( proj        );
00275    }
00276    return n;
00277 }
00278 
00279 Eh_project
00280 fill_sedflux_info_file( Eh_project p , const gchar* cmd_str , const gchar* desc )
00281 {
00282    gchar* command = NULL;
00283    gchar* options = NULL;
00284 
00285    eh_require( p       );
00286    eh_require( cmd_str );
00287 
00288    {
00289       gchar** str_array = NULL;
00290 
00291       str_array = g_strsplit( cmd_str , " " , 2 );
00292 
00293       command = str_array[0];
00294       options = str_array[1];
00295 
00296       eh_free( str_array );
00297    }
00298 
00299    if ( p )
00300    {
00301       char* default_str = NULL;
00302       char* desc_str    = NULL;
00303       Eh_project temp = eh_create_project( eh_project_name(p) );
00304 
00305       eh_set_project_dir( temp , eh_project_dir_name(p) );
00306       eh_read_project_info_file( temp );
00307 
00308       if ( !desc )
00309       {
00310          default_str = eh_project_get_info_val( temp , "RUN DESCRIPTION" );
00311          desc_str    = eh_input_str( "RUN DESCRIPTION" , default_str );
00312 
00313          eh_require( desc_str );
00314 
00315          eh_project_add_info_val( p , "RUN DESCRIPTION" , desc_str );
00316       }
00317       else
00318          eh_project_add_info_val( p , "RUN DESCRIPTION" , desc );
00319 
00320       eh_destroy_project( temp );
00321       eh_free( default_str );
00322       eh_free( desc_str );
00323    }
00324 
00325    //---
00326    // Define the sedflux version.
00327    //---
00328    eh_require( p )
00329    {
00330       gchar* version_str = g_strdup( SED_VERSION_S );
00331 
00332       eh_require( version_str );
00333 
00334       eh_project_add_info_val( p , "VERSION" , version_str );
00335 
00336       eh_free( version_str );
00337    }
00338 
00339    //---
00340    // Define the modification time of the executable.
00341    //--
00342    eh_require( command )
00343    {
00344       struct stat stat_buf;
00345       char* mod_str = eh_new( gchar , S_LINEMAX );
00346       GDate *today = g_date_new();
00347 
00348       g_stat( command , &stat_buf );
00349       g_date_set_time( today , time(&(stat_buf.st_mtime)) );
00350       g_date_strftime( mod_str , S_LINEMAX , "%d/%m/%Y" , today );
00351 
00352       eh_require( mod_str );
00353 
00354       eh_project_add_info_val( p , "CREATED" , mod_str );
00355 
00356       g_date_free( today );
00357       eh_free( mod_str );
00358    }
00359 
00360    //---
00361    // Define the name of the executable file and the command-line arguments.
00362    //---
00363    eh_require( options )
00364    {
00365       eh_require( command );
00366       eh_require( options );
00367 
00368       eh_project_add_info_val( p , "PROGRAM" , command );
00369       eh_project_add_info_val( p , "COMMAND-LINE OPTIONS" , options );
00370    }
00371 
00372    eh_free( command );
00373    eh_free( options );
00374 
00375    return p;
00376 }
00377 

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