예제 #1
0
char * util_alloc_PATH_executable(const char * executable) {
  if (util_is_abs_path(executable)) {
    if (util_is_executable(executable))
      return util_alloc_string_copy(executable);
    else
      return NULL;
  } else if (strncmp(executable , "./" , 2) == 0) {
    char * cwd = util_alloc_cwd();
    char * path = util_alloc_filename(cwd , &executable[2] , NULL);

    /* The program has been invoked as ./xxxx */
    if (!(util_is_file(path) && util_is_executable( path ))) {
      free( path );
      path = NULL;
    }
    free( cwd );

    return path;
  } else {
    char * full_path  = NULL;
    char ** path_list = util_alloc_PATH_list();
    int ipath = 0;

    while (true) {
      if (path_list[ipath] != NULL)  {
        char * current_attempt = util_alloc_filename(path_list[ipath] , executable , NULL);
      
        if ( util_is_file( current_attempt ) && util_is_executable( current_attempt )) {
          full_path = current_attempt;
          break;
        } else {
          free(current_attempt);
          ipath++;
        }
      } else
        break;
    }
    
    util_free_NULL_terminated_stringlist(path_list);
    return full_path;
  }
}
예제 #2
0
파일: site_config.c 프로젝트: edbru/ert
bool site_config_set_job_script(site_config_type * site_config, const char * job_script) {
  if (util_is_executable(job_script)) {
    char * job_script_full_path = util_alloc_realpath(job_script);
    {
      site_config->job_script = util_realloc_string_copy(site_config->job_script, job_script_full_path);
      if (!site_config->user_mode)
        site_config->job_script_site = util_realloc_string_copy(site_config->job_script_site, site_config->job_script);
    }
    free(job_script_full_path);
    return true;
  } else
    return false;
}
예제 #3
0
void plot_config_set_viewer(plot_config_type * plot_config , const char * plot_viewer) {
  if (plot_viewer != NULL && util_is_executable( plot_viewer ))
    plot_config->viewer = util_realloc_string_copy(plot_config->viewer , plot_viewer);
  else {
    plot_config->viewer = util_realloc_string_copy(plot_config->viewer , NULL );
    
    fprintf(stderr , "\n ---------------------------------------------------------------------\n");
    fprintf(stderr , " - The ERT variable \"IMAGE_VIEWER\" has not been set to a valid       -\n");
    fprintf(stderr , " - value; this means that ERT can not display the plots for you -    -\n");
    fprintf(stderr , " - sorry. However the plotfiles are available for later viewing with -\n");
    fprintf(stderr , " - your favorite image viewer software.                              -\n");
    fprintf(stderr , " -                                                                   -\n");
    fprintf(stderr , " - To actually set the IMAGE_VIEWER add the following in your        -\n");
    fprintf(stderr , " - configuration file:                                               -\n");  
    fprintf(stderr , " -                                                                   -\n");
    fprintf(stderr , " - IMAGE_VIEWER    /path/to/binary/which/can/display/graphical/files -\n");
    fprintf(stderr , " ---------------------------------------------------------------------\n\n");
  }
}
예제 #4
0
bool config_schema_item_validate_set(const config_schema_item_type * item , stringlist_type * token_list , const char * config_file, const config_path_elm_type * path_elm , config_error_type * error_list) {
  bool OK = true;
  int argc = stringlist_get_size( token_list ) - 1;
  if (item->validate->argc_min >= 0) {
    if (argc < item->validate->argc_min) {
      OK = false;
      {
        char * error_message;
        if (config_file != NULL)
          error_message = util_alloc_sprintf("Error when parsing config_file:\"%s\" Keyword:%s must have at least %d arguments.",config_file , item->kw , item->validate->argc_min);
        else
          error_message = util_alloc_sprintf("Error:: Keyword:%s must have at least %d arguments.",item->kw , item->validate->argc_min);

        config_error_add( error_list , error_message );
      }
    }
  }

  if (item->validate->argc_max >= 0) {
    if (argc > item->validate->argc_max) {
      OK = false;
      {
        char * error_message;

        if (config_file != NULL)
          error_message = util_alloc_sprintf("Error when parsing config_file:\"%s\" Keyword:%s must have maximum %d arguments.",config_file , item->kw , item->validate->argc_max);
        else
          error_message = util_alloc_sprintf("Error:: Keyword:%s must have maximum %d arguments.",item->kw , item->validate->argc_max);

        config_error_add( error_list , error_message );
      }
    }
  }

  /*
     OK - now we have verified that the number of arguments is correct. Then
     we start actually looking at the values.
  */
  if (OK) {
    /* Validating selection set - first common, then indexed */
    if (item->validate->common_selection_set) {
      for (int iarg = 0; iarg < argc; iarg++) {
        if (!set_has_key(item->validate->common_selection_set , stringlist_iget( token_list , iarg + 1))) {
          config_error_add( error_list , util_alloc_sprintf("%s: is not a valid value for: %s.",stringlist_iget( token_list , iarg + 1) , item->kw));
          OK = false;
        }
      }
    } else if (item->validate->indexed_selection_set != NULL) {
      for (int iarg = 0; iarg < argc; iarg++) {
        if ((item->validate->argc_max > 0) || (iarg < item->validate->argc_min)) {  /* Without this test we might go out of range on the indexed selection set. */
          const set_type * selection_set = validate_iget_selection_set( item->validate , iarg);
          if (selection_set) {
            if (!set_has_key( selection_set, stringlist_iget( token_list , iarg + 1))) {
              config_error_add( error_list , util_alloc_sprintf("%s: is not a valid value for item %d of \'%s\'.",stringlist_iget( token_list , iarg + 1) , iarg + 1 , item->kw));
              OK = false;
            }
          }
        }
      }
    }

    /*
      Observe that the following code might rewrite the content of
      argv for arguments referring to path locations.
    */


    /* Validate the TYPE of the various argumnents */
    {
      for (int iarg = 0; iarg < argc; iarg++) {
        const char * value = stringlist_iget(token_list , iarg + 1);
        switch (validate_iget_type( item->validate , iarg)) {
        case(CONFIG_STRING): /* This never fails ... */
          break;
        case(CONFIG_ISODATE):
          if (!util_sscanf_isodate( value , NULL ))
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as an ISO date: YYYY-MM-DD.",value));
          break;
        case(CONFIG_INT):
          if (!util_sscanf_int( value , NULL ))
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as an integer.",value));
          break;
        case(CONFIG_FLOAT):
          if (!util_sscanf_double( value , NULL )) {
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as a floating point number.", value));
            OK = false;
          }
          break;
        case(CONFIG_PATH):
          // As long as we do not reuqire the path to exist it is just a string.
          break;
        case(CONFIG_EXISTING_PATH):
          {
            char * path = config_path_elm_alloc_abspath( path_elm , value );
            if (!util_entry_exists(path)) {
              config_error_add( error_list , util_alloc_sprintf("Can not find entry %s in %s ",value , config_path_elm_get_relpath( path_elm) ));
              OK = false;
            }
            free( path );
          }
          break;
        case(CONFIG_EXECUTABLE):
          {
            /*
              1. If the supplied value is an abolute path - do nothing.
              2. If the supplied is _not_ an absolute path:

                 a. Try if the relocated exists - then use that.
                 b. Else - try if the util_alloc_PATH_executable() exists.
            */
            if (!util_is_abs_path( value )) {
              char * relocated  = __alloc_relocated__(path_elm , value);
              char * path_exe   = util_alloc_PATH_executable( value );

              if (util_file_exists(relocated)) {
                if (util_is_executable(relocated))
                  stringlist_iset_copy( token_list , iarg , relocated);
              } else if (path_exe != NULL)
                stringlist_iset_copy( token_list , iarg , path_exe);
              else
                config_error_add( error_list , util_alloc_sprintf("Could not locate executable:%s ", value));

              free(relocated);
              util_safe_free(path_exe);
            } else {
              if (!util_is_executable( value ))
                config_error_add( error_list , util_alloc_sprintf("Could not locate executable:%s ", value));
            }
          }
          break;
        case(CONFIG_BOOL):
          if (!util_sscanf_bool( value , NULL )) {
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:%s as a boolean.", value));
            OK = false;
          }
          break;
        case(CONFIG_BYTESIZE):
          if (!util_sscanf_bytesize( value , NULL)) {
            config_error_add( error_list , util_alloc_sprintf("Failed to parse:\"%s\" as number of bytes." , value));
            OK = false;
          }
          break;
        default:
          util_abort("%s: config_item_type:%d not recognized \n",__func__ , validate_iget_type(item->validate , iarg));
        }
      }
    }
  }
  return OK;
}