Example #1
0
static int parse_args(cfacdbu_t *u, unsigned int argc, char *const *argv)
{
    int optc;
    
    while (CFACDB_TRUE) {
        static struct option long_options[] = {
            {"session",          required_argument, NULL,  's'},
            {"cache",            required_argument, NULL,  'c'},
            {"temperature",      required_argument, NULL,  'T'},
            {"nele-min",         required_argument, NULL,  128},
            {"nele-max",         required_argument, NULL,  129},
            {"info",             no_argument,       NULL,  'i'},
            {"version",          no_argument,       NULL,  'V'},
            {"help",             no_argument,       NULL,  'h'},
            {NULL,               0,                 NULL,    0}
        };
        
        /* `getopt_long' stores the option index here. */
        int option_index = 0;

        optc = getopt_long(argc, argv,
            "is:c:T:Vh",
            long_options, &option_index);

        /* Detect the end of the options. */
        if (optc == -1) {
            break;
        }

        switch (optc) {
        case 'i':
            u->print_info = CFACDB_TRUE;
            break;
        case 'c':
            u->cache_fname = optarg;
            break;
        case 's':
            if (!strcmp(optarg, "all")) {
                u->sid = -1;
            } else {
                u->sid = atoi(optarg);
            }
            break;
        case 'T':
            u->T = atof(optarg);
            if (u->T <= 0.0) {
                fprintf(stderr, " Temperature must be positive!\n");
                return CFACDB_FAILURE;
            }
            break;
        case 128:
            u->nele_min = atoi(optarg);
            if (u->nele_min < 0) {
                fprintf(stderr, " nele-min must be non-negative!\n");
                return CFACDB_FAILURE;
            }
            break;
        case 129:
            u->nele_max = atoi(optarg);
            if (u->nele_max < 0) {
                fprintf(stderr, " nele-max must be non-negative!\n");
                return CFACDB_FAILURE;
            }
            break;
        case 'V':
            verinfo();
            exit(0);
            break;
        case 'h':
            usage(stdout, argv[0]);
            exit(0);
            break;
        case '?':
            /* `getopt_long' already printed an error message. */
            usage(stderr, argv[0]);
            return CFACDB_FAILURE;
        default:
            return CFACDB_FAILURE;
        }
    }
    
    if (optind == argc - 1) {
        u->db_fname = argv[optind];
        optind++;
    } else {
        usage(stderr, argv[0]);
        return CFACDB_FAILURE;
    }
    
    /* Print any remaining command line arguments (not options). */
    if (optind < argc - 1) {
        fprintf(stderr, "unrecognized argument(s): ");
        while (optind < argc - 1) {
            fprintf(stderr, "%s ", argv[++optind]);
        }
        fprintf(stderr, "\n");
        usage(stderr, argv[0]);
        return CFACDB_FAILURE;
    }
    
    if (u->nele_min > u->nele_max) {
        fprintf(stderr, "nele-min > nele-max: %d > %d!\n",
            u->nele_min, u->nele_max);
        return CFACDB_FAILURE;
    }
    
    return CFACDB_SUCCESS;
}
Example #2
0
int insmod(char *modulename) {
  int i;
  module *mods;
  char buf[1024], modulebuf[1024];
  const char *(*verinfo)(const char **);
  struct module_dep *mdp;

  strlcpy(modulebuf, modulename, sizeof(modulebuf));
  delchars(modulebuf,"./\\;");
  
  if (isloaded(modulebuf)) {
    Error("core",ERR_DEBUG,"Tried to load already loaded module: %s",modulebuf);
    return 1;
  }
  
  if (strlen(modulebuf)>100) {
    Error("core",ERR_WARNING,"Module name too long: %s",modulebuf);  
    return 1;
  }

  if ((mdp=getmoduledep(modulebuf))) {
    for (i=0;i<mdp->numparents;i++) {
      if (!isloaded(mdp->parents[i]->name->content)) {
        if (insmod(mdp->parents[i]->name->content)) {
          Error("core",ERR_WARNING,"Error loading dependant module %s (needed by %s)",
                   mdp->parents[i]->name->content,modulebuf);
          return 1;
        }
      }
    }
  } else {
    Error("core",ERR_WARNING,"Loading module %s without dependency information.",modulebuf);
  }

  i=array_getfreeslot(&modules);
  mods=(module *)(modules.content);

  sprintf(buf,"%s/%s%s",moddir->content,modulebuf,modsuffix->content);
  
  mods[i].handle=dlopen(buf,RTLD_NOW|RTLD_GLOBAL);
  
  if(mods[i].handle==NULL) {
    Error("core",ERR_ERROR,"Loading module %s failed: %s",modulebuf,dlerror());
    array_delslot(&modules,i);
    return -1;
  }

  mods[i].name=getsstring(modulebuf,MODULENAMELEN);

  verinfo=dlsym(mods[i].handle,"_version");
  if(verinfo) {
    mods[i].buildid=verinfo(&mods[i].version);
  } else {
    mods[i].version=NULL;
    mods[i].buildid=NULL;
  }

  mods[i].loadedsince = time(NULL);
  Error("core",ERR_INFO,"Loaded module %s OK.",modulebuf);
  
  return 0;
}