Ejemplo n.º 1
0
/* ****************************************************************************
*
* paOptionsParse - 
*/
int paOptionsParse(PaiArgument* paList, char* argV[], int argC)
{
  PaiArgument*  aP;
  char*         valueP;
  int           argNo         = 0;
  int           param         = 1;
  bool          extendedUsage = false;
  char          w[512];

  LM_ENTRY();
  w[0] = 0;

  PA_M(("incoming arg list of %d args", argC));
  while (++argNo < argC)
  {
    char  e[80];
    char  o[80];
    int   eee;
    int*  eP;

    eP  = &eee;
    *eP = PaNotOk;

    PA_M(("Looking up '%s'", argV[argNo]));
    if ((aP = argFind(paList, argV[argNo], STRICT, NULL)) != NULL)
    {
      valueP = argV[++argNo];
      if (aP->type == PaBoolean)
        --argNo;
    }
    else if ((aP = argFind(paList, argV[argNo], UNSTRICT, NULL)) != NULL)
      valueP = &argV[argNo][strlen(aP->option)];
    else if ((aP = argFind(paList, (char*) "", UNSTRICT, &param)) != NULL)
      valueP = argV[argNo];
    else
    {
      snprintf(w, sizeof(w), "%s '%s' not recognized", optOrPar(argV[argNo]), argV[argNo]);
      PA_W(("Warning: '%s'", w));
      PA_WARNING(PasNoSuchOption, w);
      continue;
    }

    LM_T(LmtPaApVals, ("found option '%s'", aP->name));

    if (aP->varP == (void*) &paUsageVar)
    {
      memset(paResultString, 0, sizeof(paResultString));
      if (extendedUsage == false)
      {
        paUsage();
        return -2;
      }
    }
    else if (aP->varP == (void*) &paVersion)
    {
      paVersionPrint();
      exit(0);
    }
    else if (aP->varP == (void*) &paLogDir)
    {
      if (valueP != NULL)
      {
        snprintf(paLogDir, sizeof(paLogDir), "%s", (char*) valueP);
        printf("log directory: '%s'\n", paLogDir);
      }
    }
    else if (aP->varP == (void*) &paEUsageVar)
      extendedUsage = true;
    else if (aP->varP == (void*) &paHelpVar)
    {
      memset(paResultString, 0, sizeof(paResultString));
      paHelp();
      return -2;
    }

    if (aP->used > 0)
    {
      if ((aP->type != PaSList) && (aP->type != PaIList) && (aP->what != PawParameter))
      {
        snprintf(w, sizeof(w), "multiple use of %s", aP->name);
        PA_WARNING(PasMultipleOptionUse, w);
        continue;
      }
    }

    aP->from = PafArgument;

    if (aP->type == PaBoolean)
    {
      if (strlen(argV[argNo]) != strlen(aP->option))
      {
        char tmp[128];
        snprintf(w, sizeof(w), "boolean option '%s' doesn't take parameters", paFullName(aP, e, sizeof(e)));
        snprintf(tmp, sizeof(tmp), "%c%s", argV[argNo][0], &argV[argNo][2]);
        LM_W(("Changing arg %d from '%s' to '%s'", argNo, argV[argNo], tmp));
        snprintf(argV[argNo], strlen(argV[argNo]), "%s", tmp);
        --argNo;
      }

      *((bool*) aP->varP) = (((bool) ((int) aP->def)) == true)? false : true;

      aP->used++;
      continue;
    }

    if ((argNo >= argC) || (paIsOption(paList, valueP) == true))
    {
      REQUIRE(aP);
      break;
    }

    paFullName(aP, o, sizeof(o));

    switch (aP->type)
    {
    case PaInt:
    case PaIntU:
      *((int*) aP->varP) = (int) (int64_t) paGetVal(valueP, eP);
      if (*eP != PaOk)
      {
        return -1;
      }

      LM_T(LmtPaApVals, ("got value %d for %s", *((int*) aP->varP), aP->name));
      break;

    case PaInt64:
    case PaIntU64:
      *((int64_t*) aP->varP) = (int64_t) paGetVal(valueP, eP);
      if (*eP != PaOk)
      {
        return -1;
      }

      LM_T(LmtPaApVals, ("got value %ul for %s", *((int64_t*) aP->varP), aP->name));
      break;

    case PaChar:
    case PaCharU:
      *((char*)  aP->varP) = (char) (int64_t) paGetVal(valueP, eP);
      if (*eP != PaOk)
      {
        return -1;
      }

      LM_T(LmtPaApVals, ("got value %d for %s", *((char*) aP->varP), aP->name));
      break;

    case PaShort:
    case PaShortU:
      *((int16_t*) aP->varP) = (int16_t) (int64_t) paGetVal(valueP, eP);
      if (*eP != PaOk)
      {
        return -1;
      }

      LM_T(LmtPaApVals, ("got value %d for %s", *((int16_t*) aP->varP), aP->name));
      break;

    case PaFloat:
      *((float*) aP->varP) = baStof(valueP);
      *eP = PaOk;
      LM_T(LmtPaApVals, ("got value %f for %s", *((float*) aP->varP), aP->name));
      break;

    case PaDouble:
      *((double*) aP->varP) = baStof(valueP);
      *eP = PaOk;
      LM_T(LmtPaApVals, ("got value %f for %s", *((double*) aP->varP), aP->name));
      break;

    case PaString:
      strcpy((char*) aP->varP, valueP);
      LM_T(LmtPaApVals, ("got value '%s' for %s", (char*) aP->varP, aP->name));
      *eP = PaOk;
      break;

    case PaIList:
      LM_T(LmtPaIList, ("setting list '%s' to var %s", valueP, aP->name));
      iListFix((int*) aP->varP, valueP, eP);
      break;

    case PaSList:
      LM_T(LmtPaSList, ("setting list '%s' to var %s", valueP, aP->name));
      sListFix((char**) aP->varP, valueP, eP);
      break;

    case PaBoolean:
    case PaLastArg:
      LM_T(LmtPaApVals, ("PaList, PaBoolean, PaLastArg ..."));
      *eP = PaOk;
      break;

    default:
      LM_W(("bad type for option '%s'", aP->name));
    }

    if (*eP != PaOk)
    {
      REQUIRE(aP);
    }

    aP->used++;
    LM_V(("%s used %d times", aP->name, aP->used));
  }


  /* checking that all required arguments are set */
  paIterateInit();
  while ((aP = paIterateNext(paList)) != NULL)
  {
    if ((aP->sort == PaReq) && (aP->used == 0))
    {
      snprintf(w, sizeof(w), "%s required", aP->name);
      PA_WARNING(PasRequiredOption, w);
    }
  }

  if (extendedUsage == true)
  {
    paExtendedUsage();
    return -2;
  }

  LM_EXIT();
  return 0;
}
Ejemplo n.º 2
0
/* ****************************************************************************
*
* paRcFileParse - parse startup file
*/
int paRcFileParse(void)
{
  char   dir[1024];
  char   path[1024];
  char   line[512];
  int    lineNo = 0;
  FILE*  fP;
  char   w[512];

  LM_ENTRY();

  if ((paRcFileName == NULL) || (paRcFileName[0] == 0))
  {
    LM_EXIT();
    return 0;
  }

  if (dirFind(dir, sizeof(dir)) == 0)
  {
    LM_T(LmtPaRcFile, ("RC file '%s' found in directory '%s'", paRcFileName, dir));
  }
  else
  {
    return 0;
  }

  snprintf(path, sizeof(path), "%s/%s", dir, paRcFileName);
  if ((fP = fopen(path, "r")) == NULL)
  {
    LM_RE(-1, ("error opening RC file '%s': %s", path, strerror(errno)));
  }

  LM_T(LmtPaRcFile, ("parsing RC file %s", path));

  while (fgets(line, sizeof(line), fP) != NULL)
  {
    char*         delim;
    char*         var;
    char*         val;
    PaiArgument*  aP;
    bool          varFound;
    char          envVarName[128];

    ++lineNo;
    LM_T(LmtPaRcFile, ("got line %d", lineNo));
    newlineStrip(line);
    LM_T(LmtPaRcFile, ("got line %d", lineNo));
    commentStrip(line, '#');
    LM_T(LmtPaRcFile, ("got line %d", lineNo));
    baWsStrip(line);
    LM_T(LmtPaRcFile, ("got line %d", lineNo));
    if (line[0] == 0)
    {
      continue;
    }

    LM_T(LmtPaRcFile, ("got line %d", lineNo));
    delim = strchr(line, '=');
    if (delim == NULL)
    {
      char w[512];

      snprintf(w, sizeof(w), "%s[%d]: no delimiter found", path, lineNo);
      PA_WARNING(PasParseError, w);
      continue;
    }

    *delim = 0;
    var    = line;
    val    = &delim[1];
    baWsStrip(var);
    baWsStrip(val);

    if (var[0] == 0)
    {
      fclose(fP);
      LM_RE(-1, ("%s[%d]: no variable ...", path, lineNo));
    }

    if (val[0] == 0)
    {
      fclose(fP);
      LM_RE(-1, ("%s[%d]: no value for variable %s", path, lineNo, var));
    }

    varFound = false;

    paIterateInit();
    while ((aP = paIterateNext(paiList)) != NULL)
    {
      paEnvName(aP, envVarName, sizeof(envVarName));

      if (strcmp(var, envVarName) == 0)
      {
        aP->from = PafRcFile;
        LM_T(LmtPaRcFileVal, ("got value '%s' for %s", val, envVarName));
        varFound = true;
        break;
      }
    }

    if (varFound == false)
    {
      char w[512];

      snprintf(w, sizeof(w), "%s[%d]: variable '%s' not recognized", path, lineNo, var);
      PA_WARNING(PasNoSuchVariable, w);
      continue;
    }

    switch (aP->type)
    {
    case PaString:
      strcpy((char*) aP->varP, val);
      break;

    case PaBoolean:
      if ((strcmp(val, "TRUE") == 0)
      ||  (strcmp(val, "ON")   == 0)
      ||  (strcmp(val, "yes")   == 0)
      ||  (strcmp(val, "1")    == 0))
        *((bool*) (int64_t) aP->varP) = true;
      else if ((strcmp(val, "FALSE") == 0)
      ||       (strcmp(val, "OFF")   == 0)
      ||       (strcmp(val, "no")   == 0)
      ||       (strcmp(val, "0")     == 0))
        *((bool*) (int64_t) aP->varP) = false;
      else
      {
        snprintf(w, sizeof(w), "bad value '%s' for boolean variable %s", val, envVarName);
        PA_WARNING(PasNoSuchBooleanValue, w);
      }
      break;

    case PaSList:
    case PaIList:
      LM_TODO(("lists ..."));
      break;

    case PaInt:
    case PaIntU:
      *((int64_t*) aP->varP) = baStoi(val);
      break;

    case PaShort:
    case PaShortU:
      *((int16_t*) (int64_t) aP->varP) = baStoi(val);
      break;

    case PaFloat:
      *((float*) (int64_t) aP->varP) = baStof(val);
      break;

    case PaDouble:
      *((double*) (int64_t) aP->varP) = baStod(val);
      break;

    case PaChar:
    case PaCharU:
      *((char*) (int64_t) aP->varP) = baStoi(val);
      break;

    default:
      snprintf(w, sizeof(w), "bad type %d for variable %s", aP->type, envVarName);
      PA_WARNING(PasNoSuchType, w);
    }
  }

  fclose(fP);
  return 0;
}