bool FSyncOpt::parseCmdline(int args_num, char** args_val) 
{
    if (parser.parse(args_num, const_cast<const char **>(args_val), opts, args) == false) {
        return false;
    }

    if (optionSet("verbose")) {
        verbose = VERBOSE;
    } else if (optionSet("quiet")) {
        verbose = QUIET;
    }
    FSyncConfig *config = FSyncConfig::getInstance();

    // Get log options
    StringBuffer logLevelName = opts["loglevel"];
    if (!logLevelName.null()) {
        LogLevel logLevel;
        if (logLevelName == "error") {
            logLevel = LOG_LEVEL_NONE;
        } else if (logLevelName == "info") {
            logLevel = LOG_LEVEL_INFO;	
        } else if (logLevelName == "debug") {
            logLevel = LOG_LEVEL_DEBUG;
        } else {
            fprintf(stderr, "%s: unrecognized log level: '%s'\n",
                parser.getProgramName().c_str(), logLevelName.c_str());

            exit(EXIT_FAILURE);
        }
        config->getDeviceConfig().setLogLevel(logLevel);
        LOG.setLevel(config->getDeviceConfig().getLogLevel());
    }

    // Get server option
    StringBuffer serverUrl = opts["server"];
    if (!serverUrl.null()) {
        config->getAccessConfig().setSyncURL(serverUrl);
    }

    // Get local dir
    StringBuffer dir = opts["dir"];
    if (!dir.null()) {
        config->setSyncPath(dir);
        // TODO: reset anchors if different.
    }

    // Get username
    StringBuffer user = opts["user"];
    if (!user.null()) {
        config->getAccessConfig().setUsername(user);
    }

    // Get password (not secure, it's an example!) 
    StringBuffer pass = opts["password"];
    if (!pass.null()) {
        config->getAccessConfig().setPassword(pass);
    }

    return true;
}
int
postgresDatabaseConnectionOpen (void)
{
    StringInfo connStr = makeStringInfo();
//    OptionConnection *op = getOptions()->optionConnection;

    ACQUIRE_MEM_CONTEXT(memContext);

    /* create connection string */
//    if (op->host)
        appendStringInfo(connStr, " host=%s", getStringOption("connection.host"));
//    if (op->db)
        appendStringInfo(connStr, " dbname=%s", getStringOption("connection.db"));
//    if (op->user)
        appendStringInfo(connStr, " user=%s", getStringOption("connection.user"));
    if (optionSet("connection.passwd"))
        appendStringInfo(connStr, " password=%s", getStringOption("connection.passwd"));
//    if (op->port)
        appendStringInfo(connStr, " port=%u", getIntOption("connection.port"));

    /* try to connect to db */
    plugin->conn = PQconnectdb(connStr->data);

    /* check to see that the backend connection was successfully made */
    if (plugin->conn == NULL || PQstatus(plugin->conn) == CONNECTION_BAD)
    {
        char *error = PQerrorMessage(plugin->conn);
        PQfinish(plugin->conn);
        FATAL_LOG("unable to connect to postgres database %s\n\nfailed "
                "because of:\n%s", connStr->data, error);
    }

    plugin->initialized = TRUE;

    // prepare queries
    prepareLookupQueries();

    // initialize cache
    fillOidToDTMap(GET_CACHE()->oidToDT);

    RELEASE_MEM_CONTEXT();
    return EXIT_SUCCESS;
}
Beispiel #3
0
int checkCommandLineArguments(int argc, char **argv)
{
  int i,j;

  /* This works not that well - but is probably better than no check */
  assertStreamPrint(NULL, !strcmp(FLAG_NAME[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_NAME");
  assertStreamPrint(NULL, !strcmp(FLAG_DESC[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_DESC");
  assertStreamPrint(NULL, !strcmp(FLAG_DETAILED_DESC[FLAG_MAX], "FLAG_MAX"), "unbalanced command line flag structure: FLAG_DETAILED_DESC");

  for(i=0; i<FLAG_MAX; ++i)
  {
    omc_flag[i] = 0;
    omc_flagValue[i] = NULL;
  }

#ifdef USE_DEBUG_OUTPUT
  debugStreamPrint(LOG_STDOUT, 1, "used command line options");
  for(i=1; i<argc; ++i)
    debugStreamPrint(LOG_STDOUT, 0, "%s", argv[i]);
  messageClose(LOG_STDOUT);

  debugStreamPrint(LOG_STDOUT, 1, "interpreted command line options");
#endif

  for(i=1; i<argc; ++i)
  {
    int found=0;

    for(j=1; j<FLAG_MAX; ++j)
    {
      if((FLAG_TYPE[j] == FLAG_TYPE_FLAG) && flagSet(FLAG_NAME[j], 1, argv+i))
      {
        if(omc_flag[j]) {
          warningStreamPrint(LOG_STDOUT, 0, "each command line option can only be used once: %s", argv[i]);
          return 1;
        }

        omc_flag[j] = 1;
        found=1;

#ifdef USE_DEBUG_OUTPUT
        debugStreamPrint(LOG_STDOUT, 0, "-%s", FLAG_NAME[j]);
#endif

        break;
      } else if((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && flagSet(FLAG_NAME[j], 1, argv+i) && (i+1 < argc)) {
        if(omc_flag[j]) {
          warningStreamPrint(LOG_STDOUT, 0, "each command line option can only be used once: %s", argv[i]);
          return 1;
        }

        omc_flag[j] = 1;
        omc_flagValue[j] = (char*)getFlagValue(FLAG_NAME[j], 1, argv+i);
        i++;
        found=1;

#ifdef USE_DEBUG_OUTPUT
        debugStreamPrint(LOG_STDOUT, 0, "-%s %s", FLAG_NAME[j], omc_flagValue[j]);
#endif

        break;
      } else if((FLAG_TYPE[j] == FLAG_TYPE_OPTION) && optionSet(FLAG_NAME[j], 1, argv+i)) {
        if(omc_flag[j]) {
          warningStreamPrint(LOG_STDOUT, 0, "each command line option can only be used once: %s", argv[i]);
          return 1;
        }

        omc_flag[j] = 1;
        omc_flagValue[j] = (char*)getOption(FLAG_NAME[j], 1, argv+i);
        found=1;

#ifdef USE_DEBUG_OUTPUT
        debugStreamPrint(LOG_STDOUT, 0, "-%s=%s", FLAG_NAME[j], omc_flagValue[j]);
#endif
        break;
      }
    }

    if(!found)
    {
#ifdef USE_DEBUG_OUTPUT
  messageClose(LOG_STDOUT);
#endif
      warningStreamPrint(LOG_STDOUT, 0, "invalid command line option: %s", argv[i]);
      return 1;
    }
  }

#ifdef USE_DEBUG_OUTPUT
  messageClose(LOG_STDOUT);
#endif

  return 0;
}