/* entry: 
   parse_cpu_list("MYTH_CPU_LIST", a, n) 
   fills integer array a with CPU numbers
   specified in MYTH_CPU_LIST. n is the number
   of elements a can store.  if the number of
   CPUs listed in MYTH_CPU_LIST, then it returns
   -1.
*/
static int myth_parse_cpu_list(const char * var, int * a, int n) {
  char_stream cs[1];
  int_list il[1];
  char * s = getenv(var);
  init_int_list(il, a, n);
  if (s) {
    init_char_stream(cs, s);
    if (!parse_range_list(cs, il)) return -1;
  }
#if 0
  int i;
  for (i = 0; i < il->i; i++) {
    if (i > 0) printf(",");
    printf("%d", il->a[i]);
  }
  printf("\n");
#endif
  return il->i;
}
Пример #2
0
/* ----------------------------------------------------------------------
 * fill the param_t struct
 * return 1 on (acceptable) termination
 *        0 on continue
 *       -1 on error
 */
int process_opts(param_t * params, int argc, char * argv[] )
{
   int ac, ival;

   ENTRY("process_opts");

   memset(params, 0, sizeof(param_t));  /* init everything to 0 */
   params->inputs = NULL;
   params->prefix = "combined_mask";
   init_int_list(&params->IND, 0);
   init_int_list(&params->RESD, 0);

   params->frac = -1.0;
   params->datum = MRI_byte;
   params->verb = 1;
   params->ndsets = 0;

   if( argc < 2 ) { show_help();  RETURN(1); }

   ac = 1;
   while( ac < argc ) {

      /* check for terminal options */
      if( strcmp(argv[ac],"-help") == 0 ) {
         show_help();
         RETURN(1);
      } else if( strcmp(argv[ac],"-hist") == 0 ) {
         int c, len = sizeof(g_history)/sizeof(char *);
         for( c = 0; c < len; c++) fputs(g_history[c], stdout);
         putchar('\n');
         RETURN(1);
      } else if( strcmp(argv[ac],"-ver") == 0 ) {
         puts(g_version);
         RETURN(1);
      }

      /* the remaining options are alphabetical */

      else if( strcmp(argv[ac],"-count") == 0 ) {
         params->count = 1;
         ac++; continue;
      }

      else if( strcmp(argv[ac],"-datum") == 0 ) {
         if( ++ac >= argc ) ERROR_exit("need argument after '-datum'");

         /* output datum can be byte, short or float */
         if     ( ! strcmp(argv[ac],"byte")  ) params->datum = MRI_byte;
         else if( ! strcmp(argv[ac],"short") ) params->datum = MRI_short;
         else if( ! strcmp(argv[ac],"float") ) params->datum = MRI_float;
         else ERROR_exit("-datum '%s' is not supported", argv[ac]);

         ac++; continue;
      }

      /* read in a list of dilations (negatives are erosions) */
      else if( strncmp(argv[ac],"-dilate_in", 10) == 0 ) {
         char * rptr; /* return pointer for strtol */
         int    ndilates = 0;

         if( ++ac >= argc ) ERROR_exit("need argument after '-dilate_inputs'");

         ival = strtol(argv[ac], &rptr, 10);
         while( ac < argc && rptr > argv[ac] ) {
            if( ! add_to_int_list(&params->IND, ival, 1) ) RETURN(-1);
            ndilates++;
            if( ++ac >= argc ) break;
            ival = strtol(argv[ac], &rptr, 10);
         }

         if( ndilates == 0 )
            ERROR_exit("no integral dilations found after -dilate_inputs");

         /* ac is already past last number */ continue;
      }

      /* read in a list of dilations (negatives are erosions) */
      else if( strncmp(argv[ac],"-dilate_result", 11) == 0 ) {
         char * rptr; /* return pointer for strtol */
         int    ndilates = 0;

         if( ++ac >= argc ) ERROR_exit("need argument after '-dilate_result'");

         ival = strtol(argv[ac], &rptr, 10);
         while( ac < argc && rptr > argv[ac] ) {
            if( ! add_to_int_list(&params->RESD, ival, 1) ) RETURN(-1);
            ndilates++;
            if( ++ac >= argc ) break;
            ival = strtol(argv[ac], &rptr, 10);
         }

         if( ndilates == 0 )
            ERROR_exit("no integral dilations found after -dilate_result");

         /* ac is already past last number */ continue;
      }

      /* overlap: -frac, -inter, -union */
      else if( strncmp(argv[ac],"-frac", 5) == 0 ) {
         if( ++ac >= argc ) ERROR_exit("need argument after '-frac'");

         params->frac = atof(argv[ac]);
         if( params->frac < 0.0 )
            ERROR_exit("have -frac < 0 (from %s)", argv[ac]);

         ac++; continue;
      }
      else if( strncmp(argv[ac],"-inter", 6) == 0 ) {
         params->frac = 1.0;
         ac++; continue;
      }
      else if( strcmp(argv[ac],"-union") == 0 ) {
         params->frac = 0.0;
         ac++; continue;
      }

      else if( strcmp(argv[ac],"-fill_holes") == 0 ) {
         params->fill = 1;
         ac++; continue;
      }

      else if( strncmp(argv[ac],"-inputs", 4) == 0 ) {
         /* store list of names from argv */
         ac++;

         params->inputs = argv+ac;      /* pointer to first name */
         params->ndsets = 0;            /* number of datasets    */
         while( ac < argc && argv[ac][0] != '-' ){ params->ndsets++; ac++; }

         if( params->ndsets == 0 ) ERROR_exit("need datasets after '-inputs'");

         /* already incremented: ac++; */  continue;
      }

      else if( strcmp(argv[ac],"-prefix") == 0 ) {
         if( ++ac >= argc ) ERROR_exit("need argument after '-prefix'");

         params->prefix = argv[ac];

         if( !THD_filename_ok(params->prefix) )
            ERROR_exit("Illegal name after -prefix: %s", argv[ac]);
         ac++; continue;
      }

      else if( strcmp(argv[ac],"-quiet") == 0 ) { /* -quiet means -verb 0 */
         params->verb = 0;
         ac++; continue;
      }

      else if( strcmp(argv[ac],"-verb") == 0 ) {
         if( ++ac >= argc ) ERROR_exit("need argument after '-verb'");

         params->verb = atoi(argv[ac]);
         ac++; continue;
      }

      ERROR_message("** unknown option '%s'\n",argv[ac]);
      RETURN(-1);
       
   }

   if( !dilations_are_valid(& params->IND) ||
       !dilations_are_valid(& params->RESD) ) RETURN(-1);

   if( params->ndsets <= 0 ) ERROR_exit("missing -input dataset list");
   if( !params->prefix ) ERROR_exit("missing -prefix option");
   if( params->frac < 0.0 ) {
      if( params->verb ) INFO_message("no -frac option: defaulting to -union");
      params->frac = 0.0;
   }

   if( params->verb > 1 )
      INFO_message("%d datasets, frac = %g, %d IN dilation(s), %d OUT D(s)\n",
             params->ndsets, params->frac, params->IND.num, params->RESD.num);

   RETURN(0);
}